Revert back to single splash screen. Splash screen file is now detected automatically...
[synfig.git] / synfig-studio / src / gui / splash.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file splash.cpp
3 **      \brief writeme
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **      Copyright (c) 2008 Paul Wise
11 **
12 **      This package is free software; you can redistribute it and/or
13 **      modify it under the terms of the GNU General Public License as
14 **      published by the Free Software Foundation; either version 2 of
15 **      the License, or (at your option) any later version.
16 **
17 **      This package is distributed in the hope that it will be useful,
18 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
19 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 **      General Public License for more details.
21 **      \endlegal
22 **
23 ** === N O T E S ===========================================================
24 **
25 ** ========================================================================= */
26
27 /* === H E A D E R S ======================================================= */
28
29 #ifdef USING_PCH
30 #       include "pch.h"
31 #else
32 #ifdef HAVE_CONFIG_H
33 #       include <config.h>
34 #endif
35
36 #include <iostream>
37 #include <string>
38
39 #include <ETL/stringf>
40
41 #include <gtkmm/image.h>
42 #include <gtkmm/label.h>
43 #include <gtkmm/frame.h>
44 #include <gtkmm/fixed.h>
45
46 #include <synfig/general.h>
47
48 #include "splash.h"
49 #include "app.h"
50
51 #include "general.h"
52
53 #endif
54
55 using namespace std;
56 using namespace etl;
57 using namespace studio;
58
59 /* === M A C R O S ========================================================= */
60
61 #ifndef VERSION
62 #define VERSION "unknown"
63 #define PACKAGE "synfigstudio"
64 #endif
65
66 #ifdef WIN32
67 #       ifdef IMAGE_DIR
68 #               undef IMAGE_DIR
69 #               define IMAGE_DIR "share\\pixmaps"
70 #       endif
71 #endif
72
73 #ifndef IMAGE_DIR
74 #       define IMAGE_DIR "/usr/local/share/pixmaps"
75 #endif
76
77 #ifndef IMAGE_EXT
78 #       define IMAGE_EXT        "png"
79 #endif
80
81 /* === G L O B A L S ======================================================= */
82
83 /* === P R O C E D U R E S ================================================= */
84
85 class studio::SplashProgress : public synfig::ProgressCallback
86 {
87         Splash &splash;
88
89 public:
90
91         SplashProgress(Splash &splash):splash(splash) { }
92
93         virtual bool task(const std::string &task)
94         {
95                 if(splash.tasklabel)
96                 {
97                         splash.tasklabel->set_label(task);
98                         splash.tasklabel->show();
99                 }
100                 else
101                 {
102                         cerr<<task<<endl;
103                 }
104
105                 while(studio::App::events_pending())studio::App::iteration(false);
106                 return true;
107         }
108
109         virtual bool error(const std::string &task)
110         {
111                 if(splash.tasklabel)
112                 {
113                         splash.tasklabel->set_label(_("ERROR:")+task);
114                         splash.tasklabel->show();
115                 }
116                 else
117                 {
118                         cerr<<task<<endl;
119                 }
120
121                 while(studio::App::events_pending())studio::App::iteration(false);
122                 return true;
123         }
124
125         virtual bool warning(const std::string &task)
126         {
127                 if(splash.tasklabel)
128                 {
129                         splash.tasklabel->set_label(_("WARNING:")+task);
130                         splash.tasklabel->show();
131                 }
132                 else
133                 {
134                         cerr<<task<<endl;
135                 }
136
137                 while(studio::App::events_pending())studio::App::iteration(false);
138                 return true;
139         }
140
141         virtual bool amount_complete(int current, int total)
142         {
143                 if(splash.progressbar)
144                 {
145                         splash.progressbar->set_fraction((float)current/(float)total);
146                         splash.progressbar->show();
147                 }
148                 else
149                         cerr<<current<<'/'<<total<<endl;
150
151                 while(studio::App::events_pending())studio::App::iteration(false);
152                 return true;
153         }
154 }; // END of class SplashProgress
155
156 /* === M E T H O D S ======================================================= */
157
158 Splash::Splash():
159         Gtk::Window(getenv("SYNFIG_DISABLE_POPUP_WINDOWS") ? Gtk::WINDOW_TOPLEVEL : Gtk::WINDOW_POPUP)
160 {
161         std::string imagepath;
162 #ifdef WIN32
163         imagepath=App::get_base_path()+ETL_DIRECTORY_SEPARATOR+IMAGE_DIR;
164 #else
165         imagepath=IMAGE_DIR;
166 #endif
167         char* synfig_root=getenv("SYNFIG_ROOT");
168         if(synfig_root) {
169                 imagepath=synfig_root;
170                 imagepath+=ETL_DIRECTORY_SEPARATOR;
171                 imagepath+="share";
172                 imagepath+=ETL_DIRECTORY_SEPARATOR;
173                 imagepath+="pixmaps";
174                 imagepath+=ETL_DIRECTORY_SEPARATOR;
175                 imagepath+="synfigstudio";
176         }
177         imagepath+=ETL_DIRECTORY_SEPARATOR;
178
179         // Create the splash image
180         Gtk::Image* splash_image = manage(new class Gtk::Image());
181         /* Dual-splash code:
182         srand(time(NULL));
183         const float ran = rand()/float(RAND_MAX);
184         int number = 1;
185         if(ran >0.499999)
186                 number = 2;
187         //synfig::info("%s", strprintf("%d",number).c_str());
188         splash_image->set(imagepath+"splash_screen"+strprintf("%d",number)+"."IMAGE_EXT);
189         */
190         splash_image->set(imagepath+"splash_screen."IMAGE_EXT);
191         splash_image->set_alignment(0.5,0.5);
192         splash_image->set_padding(0,0);
193
194         // Get the image size
195         int image_w = 350; int image_h = 0;
196         Glib::RefPtr<Gdk::Pixbuf> pixbuf = splash_image->get_pixbuf();
197         if( pixbuf ){
198                 image_w = pixbuf->get_width();
199                 image_h = pixbuf->get_height();
200         }
201
202         // Create the progress bar
203         progressbar = manage(new class Gtk::ProgressBar());
204         progressbar->set_size_request(image_w,24);
205
206         // Create the current task label
207         tasklabel = manage(new class Gtk::Label());
208         tasklabel->set_size_request(image_w,24);
209         tasklabel->set_use_underline(false);
210
211         // Create the Gtk::Fixed container and put all of the widgets into it
212         Gtk::Fixed* fixed = manage(new class Gtk::Fixed());
213         if( pixbuf ) fixed->put(*splash_image, 0, 0);
214         fixed->put(*progressbar, 0, image_h+24);
215         fixed->put(*tasklabel, 0, image_h);
216
217         // Create shadow around the outside of the window
218         Gtk::Frame* frame = manage(new class Gtk::Frame());
219         frame->set_shadow_type(Gtk::SHADOW_OUT);
220         frame->add(*fixed);
221
222         // Set up the parameters for this pop-up window
223         set_title("Synfig Studio "VERSION);
224         set_modal(false);
225         property_window_position().set_value(Gtk::WIN_POS_CENTER);
226         set_resizable(false);
227         set_type_hint(Gdk::WINDOW_TYPE_HINT_SPLASHSCREEN);
228         set_auto_startup_notification(false);
229         try {
230                 set_icon_from_file(imagepath+"synfig_icon."+IMAGE_EXT);
231         } catch(...) {
232                 synfig::warning("Unable to open "+imagepath+"synfig_icon."+IMAGE_EXT);
233         }
234         add(*frame);
235
236         // show everything off
237         if( pixbuf ) splash_image->show();
238         fixed->show();
239         frame->show();
240
241         // Once the splash is shown, we want startup stuff to continue as normal
242         signal_map().connect(sigc::mem_fun(*this, &Splash::enable_startup_notification));
243
244         cb=new SplashProgress(*this);
245 }
246
247 Splash::~Splash()
248 {
249         delete cb;
250 }
251
252 synfig::ProgressCallback *
253 Splash::get_callback()
254 {
255         return cb;
256 }
257
258 void
259 Splash::enable_startup_notification(){
260         set_auto_startup_notification(true);
261 }