Implements PXEGeek's http://wiki.synfig.com/Wish_list entry: "Optionally display...
[synfig.git] / synfig-studio / trunk / src / gtkmm / dock_navigator.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file dock_navigator.cpp
3 **      \brief Dock Nagivator File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "dock_navigator.h"
33 #include "canvasview.h"
34 #include "workarea.h"
35
36 #include <cassert>
37 #include <synfig/canvas.h>
38 #include <synfig/context.h>
39 #include <synfig/target_scanline.h>
40 #include <synfig/surface.h>
41
42 #include <gtkmm/separator.h>
43
44 #include "asyncrenderer.h"
45
46 #endif
47
48 /* === U S I N G =========================================================== */
49
50 using namespace std;
51 using namespace etl;
52 using namespace synfig;
53
54 /* === M A C R O S ========================================================= */
55
56 const double log_10_2 = log(2.0);
57
58 /* === G L O B A L S ======================================================= */
59
60 /* === P R O C E D U R E S ================================================= */
61
62 /* === M E T H O D S ======================================================= */
63
64 /* === E N T R Y P O I N T ================================================= */
65 studio::Widget_NavView::Widget_NavView(CanvasView::LooseHandle cv)
66 :canvview(cv),
67 adj_zoom(0,-4,4,1,2),
68 scrolling(false),
69 surface(new synfig::Surface)
70 {
71         attach(drawto,0,4,0,1);
72
73         attach(*manage(new Gtk::HSeparator),0,4,1,2,Gtk::SHRINK|Gtk::FILL,Gtk::SHRINK|Gtk::FILL);
74
75         //zooming stuff
76         attach(zoom_print,0,1,2,3,Gtk::SHRINK|Gtk::FILL,Gtk::SHRINK|Gtk::FILL);
77         zoom_print.set_size_request(40,-1);
78
79         Gtk::HScale *s = manage(new Gtk::HScale(adj_zoom));
80         s->set_draw_value(false);
81         //s->set_update_policy(Gtk::UPDATE_DELAYED);
82         //s->signal_event().connect(sigc::mem_fun(*this,&Dock_Navigator::on_scroll_event));
83         attach(*s,1,4,2,3,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK|Gtk::FILL);
84
85         show_all();
86
87         adj_zoom.signal_value_changed().connect(sigc::mem_fun(*this,&Widget_NavView::on_number_modify));
88
89         if(cv)
90         {
91                 drawto.signal_expose_event().connect(sigc::mem_fun(*this,&Widget_NavView::on_expose_draw));
92                 drawto.signal_event().connect(sigc::mem_fun(*this,&Widget_NavView::on_mouse_event));
93
94                 drawto.add_events(Gdk::BUTTON_MOTION_MASK|Gdk::BUTTON_PRESS_MASK);
95
96                 //get_canvas_view()->canvas_interface()->signal_dirty_preview()
97                 //                              .connect(sigc::mem_fun(*this,&Widget_NavView::on_dirty_preview));
98                 get_canvas_view()->work_area->signal_rendering()
99                                                 .connect(sigc::mem_fun(*this,&Widget_NavView::on_dirty_preview));
100
101                 get_canvas_view()->work_area->signal_view_window_changed()
102                                                 .connect(sigc::mem_fun(*this,&Widget_NavView::on_workarea_view_change));
103
104                 //update with this canvas' view
105                 on_workarea_view_change();
106
107                 dirty = true;
108                 queue_draw();
109         }
110
111         adj_zoom.set_value(0);
112 }
113
114 studio::Widget_NavView::~Widget_NavView()
115 {
116 }
117
118
119 static void freegu8(const guint8 *p)
120 {
121         delete [] p;
122 }
123
124 void studio::Widget_NavView::on_start_render()
125 {
126         if(dirty)
127         {
128                 //synfig::warning("Nav: Starting render");
129                 //synfig::warning("Nav: Rendering canvas");
130                 etl::handle<Target_Scanline>    targ = surface_target(surface.get());
131
132                 targ->set_canvas(get_canvas_view()->get_canvas());
133                 targ->set_remove_alpha();
134                 targ->set_avoid_time_sync();
135                 targ->set_quality(get_canvas_view()->get_work_area()->get_quality());
136                 //synfig::info("Set the quality level to: %d", get_canvas_view()->get_work_area()->get_quality());
137
138                 //this should set it to render a single frame
139                 RendDesc        r = get_canvas_view()->get_canvas()->rend_desc();
140                 r.set_time(get_canvas_view()->canvas_interface()->get_time());
141
142                 //this changes the size of the canvas to the closest thing we can find
143                 int sw = r.get_w(), sh = r.get_h();
144
145                 //synfig::warning("Nav: source image is %d x %d", sw,sh);
146
147                 //resize so largest dimension is 128
148                 int dw = sw > sh ? 128 : sw*128/sh,
149                         dh = sh > sw ? 128 : sh*128/sw;
150
151                 //synfig::warning("Nav: dest image is %d x %d", dw,dh);
152
153                 r.set_w(dw);
154                 r.set_h(dh);
155
156                 //get the pw and ph
157                 //float pw = r.get_pw();
158                 //float ph = r.get_ph();
159
160                 //synfig::warning("Nav: pixel size is %f x %f", pw,ph);
161
162                 //this renders that single frame
163                 targ->set_rend_desc(&r);
164
165                 //synfig::warning("Nav: Building async renderer and starting it...");
166
167                 renderer = new AsyncRenderer(targ);
168                 renderer->signal_success().connect(sigc::mem_fun(*this,&Widget_NavView::on_finish_render));
169                 renderer->start();
170                 dirty = false;
171         }
172 }
173
174 void studio::Widget_NavView::on_finish_render()
175 {
176         //convert it into our pixmap
177         PixelFormat pf(PF_RGB);
178
179         //synfig::warning("Nav: It hath succeeded!!!");
180
181         //assert(renderer && renderer->has_success());
182         DEBUGPOINT();
183         //synfig::warning("Nav: now we know it really succeeded");
184         if(!*surface)
185         {
186                 synfig::warning("dock_navigator: Bad surface");
187                 return;
188         }
189
190         int w = 0, h = 0;
191         int dw = surface->get_w();
192         int dh = surface->get_h();
193
194         if(prev)
195         {
196                 w = prev->get_width();
197                 h = prev->get_height();
198         }
199
200         if(w != dw || h != dh || !prev)
201         {
202                 const int total_bytes(dw*dh*synfig::channels(pf));
203
204                 //synfig::warning("Nav: Updating the pixbuf to be the right size, etc. (%d bytes)", total_bytes);
205
206                 prev.clear();
207                 guint8 *bytes = new guint8[total_bytes]; //24 bits per pixel
208
209                 //convert into our buffered dataS
210                 //synfig::warning("Nav: converting color format into buffer");
211                 convert_color_format((unsigned char *)bytes, (*surface)[0], dw*dh, pf, App::gamma);
212
213                 prev =
214                 Gdk::Pixbuf::create_from_data(
215                         bytes,  // pointer to the data
216                         Gdk::COLORSPACE_RGB, // the colorspace
217                         ((pf&PF_A)==PF_A), // has alpha?
218                         8, // bits per sample
219                         dw,     // width
220                         dh,     // height
221                         dw*synfig::channels(pf), // stride (pitch)
222                         SigC::slot(freegu8)
223                 );
224         }
225         else
226         {
227                 //synfig::warning("Nav: Don't need to resize");
228                 //convert into our buffered dataS
229                 //synfig::warning("Nav: converting color format into buffer");
230                 if(prev) //just in case we're stupid
231                 {
232                         convert_color_format((unsigned char *)prev->get_pixels(), (*surface)[0], dw*dh, pf, App::gamma);
233                 }
234         }
235         queue_draw();
236 }
237
238 /*      zoom slider is on exponential scale
239
240         map: -4,4 -> small number,1600 with 100 at 0
241
242         f(x) = 100*2^x
243 */
244
245 static double unit_to_zoom(double f)
246 {
247         return pow(2.0,f);
248 }
249
250 static double zoom_to_unit(double f)
251 {
252         if(f > 0)
253         {
254                 return log(f) / log_10_2;
255         }else return -999999.0;
256 }
257
258 bool studio::Widget_NavView::on_expose_draw(GdkEventExpose *exp)
259 {
260         //print out the zoom
261         //HACK kind of...
262         //zoom_print.set_text(strprintf("%.1f%%",100*unit_to_zoom(adj_zoom.get_value())));
263
264         //draw the good stuff
265         on_start_render();
266
267         //if we've got a preview etc. display it...
268         if(get_canvas_view() && prev)
269         {
270                 //axis transform from units to pixel coords
271                 float xaxis = 0, yaxis = 0;
272
273                 int canvw = get_canvas_view()->get_canvas()->rend_desc().get_w();
274                 //int canvh = get_canvas_view()->get_canvas()->rend_desc().get_h();
275
276                 float pw = get_canvas_view()->get_canvas()->rend_desc().get_pw();
277                 float ph = get_canvas_view()->get_canvas()->rend_desc().get_ph();
278
279                 int w = prev->get_width();
280                 int h = prev->get_height();
281
282                 //scale up/down to the nearest pixel ratio...
283                 //and center in center
284                 int offx=0, offy=0;
285
286                 float sx, sy;
287                 int nw,nh;
288
289                 sx = drawto.get_width() / (float)w;
290                 sy = drawto.get_height() / (float)h;
291
292                 //synfig::warning("Nav redraw: now to scale the bitmap: %.3f x %.3f",sx,sy);
293
294                 //round to smallest scale (fit entire thing in window without distortion)
295                 if(sx > sy) sx = sy;
296                 //else sy = sx;
297
298                 //scaling and stuff
299                 // the point to navpixel space conversion should be:
300                 //              (navpixels / canvpixels) * (canvpixels / canvsize)
301                 //      or (navpixels / prevpixels) * (prevpixels / navpixels)
302                 xaxis = sx * w / (float)canvw;
303                 yaxis = xaxis/ph;
304                 xaxis /= pw;
305
306                 //scale to a new pixmap and then copy over to the window
307                 nw = (int)(w*sx);
308                 nh = (int)(h*sx);
309
310                 //must now center to be cool
311                 offx = (drawto.get_width() - nw)/2;
312                 offy = (drawto.get_height() - nh)/2;
313
314                 //trivial escape
315                 if(nw == 0 || nh == 0)return true;
316
317                 //draw to drawing area
318                 Glib::RefPtr<Gdk::GC>   gc = Gdk::GC::create(drawto.get_window());
319
320                 //synfig::warning("Nav: Scaling pixmap to off (%d,%d) with size (%d,%d)", offx,offy,nw, nh);
321                 Glib::RefPtr<Gdk::Pixbuf> scalepx = prev->scale_simple(nw,nh,Gdk::INTERP_NEAREST);
322
323                 //synfig::warning("Nav: Drawing scaled bitmap");
324                 drawto.get_window()->draw_pixbuf(
325                         gc, //GC
326                         scalepx, //pixbuf
327                         0, 0,   // Source X and Y
328                         offx, offy,     // Dest X and Y
329                         -1,-1,  // Width and Height
330                         Gdk::RGB_DITHER_MAX, // RgbDither
331                         2, 2 // Dither offset X and Y
332                 );
333
334                 //draw fancy red rectangle around focus point
335                 const Point &wtl = get_canvas_view()->work_area->get_window_tl(),
336                                         &wbr = get_canvas_view()->work_area->get_window_br();
337
338                 gc->set_rgb_fg_color(Gdk::Color("#ff0000"));
339                 gc->set_line_attributes(2,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
340
341                 //it must be clamped to the drawing area though
342                 int l=0,rw=0,t=0,rh=0;
343                 const Point fp = -get_canvas_view()->work_area->get_focus_point();
344
345                 //get focus point in normal space
346                 rw = (int)(abs((wtl[0]-wbr[0])*xaxis));
347                 rh = (int)(abs((wtl[1]-wbr[1])*yaxis));
348
349                 //transform into pixel space
350                 l = (int)(drawto.get_width()/2 + fp[0]*xaxis - rw/2);
351                 t = (int)(drawto.get_height()/2 + fp[1]*yaxis - rh/2);
352
353                 //coord system:
354                 // tl : (offx,offy)
355                 // axis multipliers = xaxis,yaxis
356                 //synfig::warning("Nav: tl (%f,%f), br (%f,%f)", wtl[0],wtl[1],wbr[0],wbr[1]);
357                 //synfig::warning("Nav: tl (%f,%f), br (%f,%f)", wtl[0],wtl[1],wbr[0],wbr[1]);
358                 //synfig::warning("Nav: Drawing Rectangle (%d,%d) with dim (%d,%d)", l,t,rw,rh);
359                 drawto.get_window()->draw_rectangle(gc,false,l,t,rw,rh);
360         }
361
362         return false; //draw everything else too
363 }
364
365 void studio::Widget_NavView::on_dirty_preview()
366 {
367         dirty = true;
368         queue_draw();
369 }
370
371 bool studio::Widget_NavView::on_scroll_event(GdkEvent *event)
372 {
373         if(get_canvas_view() && get_canvas_view()->get_work_area())
374         {
375                 double z = unit_to_zoom(adj_zoom.get_value());
376
377                 switch(event->type)
378                 {
379                         case GDK_BUTTON_PRESS:
380                         {
381                                 if(event->button.button == 1)
382                                 {
383                                         scrolling = true;
384                                         get_canvas_view()->get_work_area()->set_zoom(z);
385                                         scrolling = false;
386                                 }
387                                 break;
388                         }
389
390                         case GDK_MOTION_NOTIFY:
391                         {
392                                 if(Gdk::ModifierType(event->motion.state) & Gdk::BUTTON1_MASK)
393                                 {
394                                         scrolling = true;
395                                         get_canvas_view()->get_work_area()->set_zoom(z);
396                                         scrolling = false;
397                                 }
398                                 break;
399                         }
400
401                         default:
402                                 break;
403                 }
404         }
405
406         return false;
407 }
408
409 void studio::Widget_NavView::on_number_modify()
410 {
411         double z = unit_to_zoom(adj_zoom.get_value());
412         zoom_print.set_text(strprintf("%.1f%%",z*100.0));
413         //synfig::warning("Updating zoom to %f",adj_zoom.get_value());
414
415         if(get_canvas_view() && z != get_canvas_view()->get_work_area()->get_zoom())
416         {
417                 scrolling = true;
418                 get_canvas_view()->get_work_area()->set_zoom(z);
419                 scrolling = false;
420         }
421 }
422
423 void studio::Widget_NavView::on_workarea_view_change()
424 {
425         double wz = get_canvas_view()->get_work_area()->get_zoom();
426         double z = zoom_to_unit(wz);
427
428         //synfig::warning("Updating zoom to %f -> %f",wz,z);
429         if(!scrolling && z != adj_zoom.get_value())
430         {
431                 adj_zoom.set_value(z);
432                 //adj_zoom.value_changed();
433         }
434         queue_draw();
435 }
436
437 bool studio::Widget_NavView::on_mouse_event(GdkEvent * e)
438 {
439         Point p;
440         bool    setpos = false;
441
442         if(e->type == GDK_BUTTON_PRESS && e->button.button == 1)
443         {
444                 p[0] = e->button.x - drawto.get_width()/2;
445                 p[1] = e->button.y - drawto.get_height()/2;
446
447                 setpos = true;
448         }
449
450         if(e->type == GDK_MOTION_NOTIFY && (Gdk::ModifierType(e->motion.state) & Gdk::BUTTON1_MASK))
451         {
452                 p[0] = e->motion.x - drawto.get_width()/2;
453                 p[1] = e->motion.y - drawto.get_height()/2;
454
455                 setpos = true;
456         }
457
458         if(setpos && prev && get_canvas_view())
459         {
460                 const Point &tl = get_canvas_view()->get_canvas()->rend_desc().get_tl();
461                 const Point &br = get_canvas_view()->get_canvas()->rend_desc().get_br();
462
463                 float max = abs((br[0]-tl[0]) / drawto.get_width());
464
465                 if((prev->get_width() / drawto.get_width()) < (prev->get_height() / drawto.get_height()))
466                         max = abs((br[1]-tl[1]) / drawto.get_height());
467
468                 float signx = (br[0]-tl[0]) < 0 ? -1 : 1;
469                 float signy = (br[1]-tl[1]) < 0 ? -1 : 1;
470
471                 Point pos;
472
473                 pos[0] = p[0] * max * signx;
474                 pos[1] = p[1] * max * signy;
475
476                 get_canvas_view()->get_work_area()->set_focus_point(-pos);
477
478                 return true;
479         }
480
481         return false;
482 }
483
484 //Navigator Dock Definitions
485
486 studio::Dock_Navigator::Dock_Navigator()
487 :Dock_CanvasSpecific("navigator",_("Navigator"),Gtk::StockID("synfig-navigator"))
488 {
489         add(dummy);
490 }
491
492 studio::Dock_Navigator::~Dock_Navigator()
493 {
494 }
495
496 void studio::Dock_Navigator::changed_canvas_view_vfunc(etl::loose_handle<CanvasView> canvas_view)
497 {
498         if(canvas_view)
499         {
500                 Widget *v = canvas_view->get_ext_widget("navview");
501
502                 if(!v)
503                 {
504                         v = new Widget_NavView(canvas_view);
505                         canvas_view->set_ext_widget("navview",v);
506                 }
507
508                 add(*v);
509         }else
510         {
511                 clear_previous();
512                 //add(dummy);
513         }
514 }