11 #include "icons/img_unknown.xpm"
13 static gchar *zoom_as_text(gint zoom, gfloat scale);
14 static void set_zoom_label(GtkWidget *label, gint zoom, gfloat scale);
15 static void set_info_label(GtkWidget *label, gint width, gint height, gint size, gint unknown);
16 static void set_window_title(ImageWindow *imd, gchar *text);
18 static gint image_area_size_top_window(ImageWindow *imd, gint w, gint h);
20 static void image_area_recalc_size(ImageWindow *imd, GtkAllocation *allocation);
22 static void image_area_redraw(ImageWindow *imd);
23 static gint image_area_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data);
24 static gint image_area_update_cb(GtkWidget *widget, GdkEventConfigure *event, gpointer data);
26 static void set_mouse_cursor (GdkWindow *window, gint icon);
27 static void image_area_mouse_moved(GtkWidget *widget, GdkEventButton *bevent, gpointer data);
28 static void image_area_mouse_pressed(GtkWidget *widget, GdkEventButton *bevent, gpointer data);
29 static void image_area_mouse_released(GtkWidget *widget, GdkEventButton *bevent, gpointer data);
30 static void image_area_mouse_drag(GtkWidget *widget, GdkDragContext *context, gpointer data);
33 *-----------------------------------------------------------------------------
34 * image status widget update routines (private)
35 *-----------------------------------------------------------------------------
38 static gchar *zoom_as_text(gint zoom, gfloat scale)
43 if (zoom > 1) l = zoom;
44 if (zoom < -1) r = -zoom;
45 if (zoom == 0 && scale != 0)
47 if (scale < 1) r = 1 / scale + 0.5;
50 return g_strdup_printf("%d :%s%d", l, approx, r);
53 static void set_zoom_label(GtkWidget *label, gint zoom, gfloat scale)
56 buf = zoom_as_text(zoom, scale);
57 gtk_label_set(GTK_LABEL(label), buf);
61 static void set_info_label(GtkWidget *label, gint width, gint height, gint size, gint unknown)
65 sprintf(buf, _("( ? x ? ) %d bytes"), size);
67 sprintf(buf, _("( %d x %d ) %d bytes"), width, height, size);
68 gtk_label_set(GTK_LABEL(label), buf);
71 static void set_window_title(ImageWindow *imd, gchar *text)
74 if (!imd->top_window) return;
78 title = g_strconcat(imd->title, imd->image_name, text, NULL);
82 title = g_strconcat(imd->image_name, text, NULL);
85 gtk_window_set_title(GTK_WINDOW(imd->top_window), title);
90 *-----------------------------------------------------------------------------
91 * fit window to image utility (private)
92 *-----------------------------------------------------------------------------
95 static gint image_area_size_top_window(ImageWindow *imd, gint w, gint h)
97 if (!imd->top_window) return FALSE;
98 if (imd == full_screen_image) return FALSE;
99 if (imd == normal_image && !toolwindow) return FALSE;
100 if (!fit_window) return FALSE;
102 if (imd == normal_image)
104 /* account for border frame */
109 if (limit_window_size)
111 gint sw = gdk_screen_width() * max_window_size / 100;
112 gint sh = gdk_screen_height() * max_window_size / 100;
118 /* to cheat on a prob a little, don't resize if within 1 either way...
119 ...dumb off by 1 errors! ;) */
121 /* if (w >= (imd->top_window)->allocation.width - 1 &&
122 w <= (imd->top_window)->allocation.width + 1 &&
123 h >= (imd->top_window)->allocation.height - 1 &&
124 h <= (imd->top_window)->allocation.height + 1)
127 if (debug) printf("auto sized to %d x %d\n", w, h);
129 gdk_window_resize(imd->top_window->window, w, h);
130 gtk_widget_set_usize(imd->top_window, w, h);
136 *-----------------------------------------------------------------------------
137 * image widget zoom/recalc routines
138 *-----------------------------------------------------------------------------
141 void image_area_scroll(ImageWindow *imd, gint x, gint y)
145 GtkAdjustment *h = gtk_viewport_get_hadjustment(GTK_VIEWPORT(imd->viewport));
146 gfloat val = h->value + x;
147 if (val < h->lower) val = h->lower;
148 if (val > h->upper - h->page_size) val = h->upper - h->page_size;
149 gtk_adjustment_set_value(GTK_ADJUSTMENT(h), val);
154 GtkAdjustment *v = gtk_viewport_get_vadjustment(GTK_VIEWPORT(imd->viewport));
155 gfloat val = v->value + y;
156 if (val < v->lower) val = v->lower;
157 if (val > v->upper - v->page_size) val = v->upper - v->page_size;
158 gtk_adjustment_set_value(GTK_ADJUSTMENT(v), val);
162 gint image_area_get_zoom(ImageWindow *imd)
167 void image_area_adjust_zoom(ImageWindow *imd, gint increment)
169 gint zoom = imd->zoom;
172 while (increment < 0)
175 if (zoom == 0 || zoom == -1) zoom = -2;
178 if (zoom < -8) zoom = -8;
182 while (increment > 0)
185 if (zoom == -1) zoom = 1;
188 if (zoom > 3) zoom = 3;
190 if (zoom != imd->zoom)
191 image_area_set_zoom(imd, zoom);
194 void image_area_set_zoom(ImageWindow *imd, gint zoom)
196 if (zoom == imd->zoom && imd->width > 0 && imd->height > 0) return;
199 image_area_recalc_size(imd, NULL);
201 gtk_widget_set_usize (imd->table, imd->width, imd->height);
202 gtk_drawing_area_size(GTK_DRAWING_AREA(imd->image), imd->width, imd->height);
205 static void image_area_recalc_size(ImageWindow *imd, GtkAllocation *allocation)
208 gfloat scale_factor = 1;
210 w = imd->image_data->rgb_width;
211 h = imd->image_data->rgb_height;
214 ww = allocation->width;
215 wh = allocation->height;
219 ww = (imd->eventbox)->allocation.width;
220 wh = (imd->eventbox)->allocation.height;
223 if (imd == normal_image)
225 /* account for frame */
230 if (imd->zoom == 0) /* zoom to fit */
232 if (imd == normal_image && imd->width == 0 && imd->height == 0 &&
233 fit_window && toolwindow)
235 if (limit_window_size)
237 ww = (gdk_screen_width() * max_window_size / 100) - 4;
238 wh = (gdk_screen_height() * max_window_size / 100) - 4;
246 if (w > ww || h > wh)
248 if ((gfloat)ww / w > (gfloat)wh / h)
250 scale_factor = (gfloat) wh / h;
252 w = w * scale_factor + 0.5;
257 scale_factor = (gfloat)ww / w;
259 h = h * scale_factor + 0.5;
266 else if (imd->zoom > 0) /* zoom orig, in */
268 scale_factor = imd->zoom;
269 w = w * scale_factor;
270 h = h * scale_factor;
272 else if (imd->zoom < -1) /* zoom out */
274 scale_factor = (- imd->zoom);
275 w = w / scale_factor;
276 h = h / scale_factor;
282 if (debug) printf("recalc %d x %d @ %f\n", w, h, scale_factor);
286 set_zoom_label(imd->zoom_label, imd->zoom, scale_factor);
289 /* this is causing problems with resizing
290 if (imd->top_window && imd->show_title_zoom)
292 gchar *buf = zoom_as_text(imd->zoom, scale_factor);
293 gchar *zbuf = g_strconcat(" [ ", buf, "]", NULL);
295 set_window_title(imd, zbuf);
300 if (image_area_size_top_window(imd, w, h))
303 imd->artificial_size = TRUE;
304 gtk_grab_add (info_zoom);
305 while(gtk_events_pending()) gtk_main_iteration();
306 gtk_grab_remove(info_zoom);
307 imd->artificial_size = FALSE;
312 *-----------------------------------------------------------------------------
313 * image widget set/get image information
314 *-----------------------------------------------------------------------------
317 void image_area_set_path(ImageWindow *imd, gchar *newpath)
319 if (!imd->image_path || !newpath) return;
321 g_free(imd->image_path);
322 imd->image_path = g_strdup(newpath);
323 imd->image_name = filename_from_path(imd->image_path);
327 set_window_title(imd, NULL);
331 gchar *image_area_get_path(ImageWindow *imd)
333 return imd->image_path;
336 gchar *image_area_get_name(ImageWindow *imd)
338 return imd->image_name;
341 void image_area_set_image(ImageWindow *imd, gchar *path, gint zoom)
343 if (path && imd->image_path && !strcmp(path, imd->image_path)) return;
345 g_free(imd->image_path);
348 imd->image_path = g_strdup(path);
349 imd->image_name = filename_from_path(imd->image_path);
353 imd->image_path = NULL;
354 imd->image_name = " ";
358 if (imd->image_data) gdk_imlib_destroy_image(imd->image_data);
359 if (path && isfile(path))
361 imd->image_data = gdk_imlib_load_image(path);
362 if (!imd->image_data)
364 imd->image_data = gdk_imlib_create_image_from_xpm_data((gchar **)img_unknown_xpm);
369 imd->unknown = FALSE;
371 imd->size = filesize(path);
376 imd->image_data = gdk_imlib_create_image_from_xpm_data((gchar **)img_unknown_xpm);
378 imd->image_data = gdk_imlib_create_image_from_data((char *)logo, NULL, logo_width, logo_height);
383 imd->width = imd->old_width = 0;
384 imd->height = imd->old_height = 0;
388 set_window_title(imd, NULL);
392 set_info_label(imd->info_label, imd->image_data->rgb_width, imd->image_data->rgb_height, imd->size, imd->unknown);
395 /* do info area updates here */
398 image_area_set_zoom(imd, zoom);
402 *-----------------------------------------------------------------------------
403 * image widget redraw/callbacks (private)
404 *-----------------------------------------------------------------------------
407 static void image_area_redraw(ImageWindow *imd)
409 GdkBitmap *mask = NULL;
411 if (debug) printf("redrawn %d x %d\n", imd->width, imd->height);
413 if (!imd->image_data) return;
415 if (imd->width == imd->old_width && imd->height == imd->old_height)
417 if (debug) printf("redraw cancelled\n");
421 if (imd->image_pixmap) gdk_imlib_free_pixmap(imd->image_pixmap);
422 imd->image_pixmap = NULL;
424 gdk_imlib_render(imd->image_data, imd->width, imd->height);
425 imd->image_pixmap = gdk_imlib_move_image(imd->image_data);
426 mask = gdk_imlib_move_mask(imd->image_data);
428 gdk_window_set_back_pixmap(imd->image->window, imd->image_pixmap, FALSE);
429 gdk_window_shape_combine_mask (imd->image->window, mask, 0, 0);
430 gdk_window_clear(imd->image->window);
433 imd->old_width = imd->width;
434 imd->old_height = imd->height;
437 static gint image_area_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
439 ImageWindow *imd = data;
448 if (imd->artificial_size) return FALSE;
450 h = gtk_viewport_get_hadjustment(GTK_VIEWPORT(imd->viewport));
451 v = gtk_viewport_get_vadjustment(GTK_VIEWPORT(imd->viewport));
454 h_max = allocation->width;
456 v_max = allocation->height;
458 if (imd == normal_image)
464 if (h_pos > h->upper - h_max) h_pos = h->upper - h_max;
465 if (v_pos > v->upper - v_max) v_pos = v->upper - v_max;
469 imd->new_img = FALSE;
470 gtk_adjustment_clamp_page(h, 0.0, h_max);
471 gtk_adjustment_clamp_page(v, 0.0, v_max);
475 gtk_adjustment_clamp_page(h, h_pos, h_max);
476 gtk_adjustment_clamp_page(v, v_pos, v_max);
479 gtk_adjustment_changed(h);
480 gtk_adjustment_changed(v);
482 if (!imd->image_data || imd->zoom != 0) return FALSE;
486 image_area_recalc_size(imd, allocation);
487 if (old_w != imd->width || old_h != imd->height)
489 gtk_widget_set_usize (imd->table, imd->width, imd->height);
490 gtk_drawing_area_size(GTK_DRAWING_AREA(imd->image), imd->width, imd->height);
493 if (debug) printf("sized %d x %d (%d x %d)\n", allocation->width, allocation->height, imd->width, imd->height);
498 static gint image_area_update_cb(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
500 ImageWindow *imd = data;
502 if (imd->artificial_size) return FALSE;
504 image_area_redraw(imd);
510 *-----------------------------------------------------------------------------
511 * image widget mouse routines (private)
512 *-----------------------------------------------------------------------------
515 static void set_mouse_cursor (GdkWindow *window, gint icon)
525 cursor = gdk_cursor_new (icon);
528 gdk_window_set_cursor (window, cursor);
530 if (cursor) gdk_cursor_destroy (cursor);
533 static void image_area_mouse_moved(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
535 ImageWindow *imd = data;
541 if (!imd->in_drag || !gdk_pointer_is_grabbed()) return;
543 if (imd->drag_moved < 4)
549 set_mouse_cursor (imd->eventbox->window, GDK_FLEUR);
552 h = gtk_viewport_get_hadjustment(GTK_VIEWPORT(imd->viewport));
553 v = gtk_viewport_get_vadjustment(GTK_VIEWPORT(imd->viewport));
555 x = imd->drag_last_x - bevent->x;
556 y = imd->drag_last_y - bevent->y;
559 if (h->upper - h->page_size > 0)
561 val = (float)h->value + x;
562 if (val < 0 ) val = 0;
563 if (val > h->upper - h->page_size) val = h->upper - h->page_size;
565 gtk_adjustment_set_value (GTK_ADJUSTMENT(h), val);
569 if (v->upper - v->page_size > 0)
572 if (val < 0 ) val = 0;
573 if (val > v->upper - v->page_size) val = v->upper - v->page_size;
575 gtk_adjustment_set_value (GTK_ADJUSTMENT(v), val);
578 gtk_adjustment_value_changed(h);
579 gtk_adjustment_value_changed(v);
581 imd->drag_last_x = bevent->x;
582 imd->drag_last_y = bevent->y;
585 static void image_area_mouse_pressed(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
587 ImageWindow *imd = data;
588 switch (bevent->button)
592 imd->drag_last_x = bevent->x;
593 imd->drag_last_y = bevent->y;
595 gdk_pointer_grab (imd->eventbox->window, FALSE,
596 GDK_POINTER_MOTION_MASK |
597 GDK_BUTTON_RELEASE_MASK,
598 NULL, NULL, bevent->time);
599 gtk_grab_add (imd->eventbox);
606 imd->func_btn3(imd, bevent, imd->data_btn3);
611 gtk_widget_grab_focus(imd->viewport);
614 static void image_area_mouse_released(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
616 ImageWindow *imd = data;
617 if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB (imd->eventbox))
619 gtk_grab_remove (imd->eventbox);
620 gdk_pointer_ungrab (bevent->time);
621 set_mouse_cursor (imd->eventbox->window, -1);
624 if (bevent->button == 1)
626 if (imd->drag_moved < 4 && imd->func_btn1)
627 imd->func_btn1(imd, bevent, imd->data_btn1);
630 if (bevent->button == 2)
632 if (imd->drag_moved < 4 && imd->func_btn2)
633 imd->func_btn2(imd, bevent, imd->data_btn2);
636 imd->in_drag = FALSE;
639 static void image_area_mouse_drag(GtkWidget *widget, GdkDragContext *context, gpointer data)
641 ImageWindow *imd = data;
646 *-----------------------------------------------------------------------------
647 * image widget setup routines
648 *-----------------------------------------------------------------------------
651 void image_area_set_topwindow(ImageWindow *imd, GtkWidget *window, gchar *title, gint show_zoom)
653 imd->top_window = window;
654 imd->show_title_zoom = show_zoom;
658 imd->title = g_strdup(title);
663 void image_area_set_labels(ImageWindow *imd, GtkWidget *info, GtkWidget *zoom)
665 imd->info_label = info;
666 imd->zoom_label = zoom;
669 void image_area_set_button(ImageWindow *imd, gint button,
670 void (*func)(ImageWindow *, GdkEventButton *, gpointer), gpointer data)
675 imd->func_btn1 = func;
676 imd->data_btn1 = data;
679 imd->func_btn2 = func;
680 imd->data_btn2 = data;
683 imd->func_btn3 = func;
684 imd->data_btn3 = data;
689 ImageWindow *image_area_new(GtkWidget *top_window)
695 imd = g_new0(ImageWindow, 1);
698 imd->top_window = top_window;
699 imd->title = g_strdup("GQview - ");
700 imd->show_title_zoom = FALSE;
701 imd->new_img = FALSE;
703 imd->eventbox = gtk_event_box_new();
705 gtk_signal_connect(GTK_OBJECT(imd->eventbox),"motion_notify_event",
706 GTK_SIGNAL_FUNC(image_area_mouse_moved), imd);
707 gtk_signal_connect(GTK_OBJECT(imd->eventbox),"button_press_event",
708 GTK_SIGNAL_FUNC(image_area_mouse_pressed), imd);
709 gtk_signal_connect(GTK_OBJECT(imd->eventbox),"button_release_event",
710 GTK_SIGNAL_FUNC(image_area_mouse_released), imd);
711 gtk_widget_set_events(imd->eventbox, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK);
714 h_adj = gtk_adjustment_new(0.0,0.0,0.0,1.0,1.0,1.0);
715 v_adj = gtk_adjustment_new(0.0,0.0,0.0,1.0,1.0,1.0);
717 imd->viewport = gtk_viewport_new (GTK_ADJUSTMENT(h_adj), GTK_ADJUSTMENT(v_adj));
718 gtk_container_add(GTK_CONTAINER(imd->eventbox), imd->viewport);
720 /* table for resize */
721 imd->table = gtk_table_new (1,1,TRUE);
722 gtk_container_add(GTK_CONTAINER (imd->viewport), imd->table);
725 imd->image = gtk_drawing_area_new();
726 gtk_table_attach(GTK_TABLE (imd->table),imd->image,0,1,0,1,GTK_EXPAND,GTK_EXPAND,0,0);
728 gtk_signal_connect(GTK_OBJECT(imd->eventbox),"size_allocate",GTK_SIGNAL_FUNC(image_area_size_cb), imd);
729 gtk_signal_connect(GTK_OBJECT(imd->image),"configure_event",GTK_SIGNAL_FUNC(image_area_update_cb), imd);
731 gtk_signal_connect(GTK_OBJECT(imd->viewport),"drag_begin",
732 GTK_SIGNAL_FUNC(image_area_mouse_drag), imd);
737 void image_area_free(ImageWindow *imd)
739 g_free(imd->image_path);
741 if (imd->image_pixmap) gdk_imlib_free_pixmap(imd->image_pixmap);
742 if (imd->image_data) gdk_imlib_destroy_image(imd->image_data);
747 gint get_default_zoom(ImageWindow *imd)
751 if (zoom_mode == ZOOM_RESET_ORIGINAL)
755 else if (zoom_mode == ZOOM_RESET_FIT_WINDOW)
763 zoom = image_area_get_zoom(imd);