/app/widgets/gimpview-popup.c

https://gitlab.com/marcelosabino/gimp · C · 243 lines · 169 code · 52 blank · 22 comment · 8 complexity · 6ffaeb9ea3f39b8ca1d3747ac7032868 MD5 · raw file

  1. /* GIMP - The GNU Image Manipulation Program
  2. * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3. *
  4. * gimpview-popup.c
  5. * Copyright (C) 2003-2006 Michael Natterer <mitch@gimp.org>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "config.h"
  21. #include <gegl.h>
  22. #include <gtk/gtk.h>
  23. #include "libgimpwidgets/gimpwidgets.h"
  24. #include "widgets-types.h"
  25. #include "core/gimpcontext.h"
  26. #include "core/gimpviewable.h"
  27. #include "gimpview.h"
  28. #include "gimpviewrenderer.h"
  29. #include "gimpview-popup.h"
  30. #define VIEW_POPUP_DELAY 150
  31. typedef struct _GimpViewPopup GimpViewPopup;
  32. struct _GimpViewPopup
  33. {
  34. GtkWidget *widget;
  35. GimpContext *context;
  36. GimpViewable *viewable;
  37. gint popup_width;
  38. gint popup_height;
  39. gboolean dot_for_dot;
  40. gint button;
  41. gint button_x;
  42. gint button_y;
  43. guint timeout_id;
  44. GtkWidget *popup;
  45. };
  46. /* local function prototypes */
  47. static void gimp_view_popup_hide (GimpViewPopup *popup);
  48. static gboolean gimp_view_popup_button_release (GtkWidget *widget,
  49. GdkEventButton *bevent,
  50. GimpViewPopup *popup);
  51. static void gimp_view_popup_unmap (GtkWidget *widget,
  52. GimpViewPopup *popup);
  53. static void gimp_view_popup_drag_begin (GtkWidget *widget,
  54. GdkDragContext *context,
  55. GimpViewPopup *popup);
  56. static gboolean gimp_view_popup_timeout (GimpViewPopup *popup);
  57. /* public functions */
  58. gboolean
  59. gimp_view_popup_show (GtkWidget *widget,
  60. GdkEventButton *bevent,
  61. GimpContext *context,
  62. GimpViewable *viewable,
  63. gint view_width,
  64. gint view_height,
  65. gboolean dot_for_dot)
  66. {
  67. GimpViewPopup *popup;
  68. gint popup_width;
  69. gint popup_height;
  70. g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
  71. g_return_val_if_fail (bevent != NULL, FALSE);
  72. g_return_val_if_fail (context == NULL || GIMP_IS_CONTEXT (context), FALSE);
  73. g_return_val_if_fail (GIMP_IS_VIEWABLE (viewable), FALSE);
  74. if (! gimp_viewable_get_popup_size (viewable,
  75. view_width,
  76. view_height,
  77. dot_for_dot,
  78. &popup_width,
  79. &popup_height))
  80. return FALSE;
  81. popup = g_slice_new0 (GimpViewPopup);
  82. popup->widget = widget;
  83. popup->context = context;
  84. popup->viewable = viewable;
  85. popup->popup_width = popup_width;
  86. popup->popup_height = popup_height;
  87. popup->dot_for_dot = dot_for_dot;
  88. popup->button = bevent->button;
  89. popup->button_x = bevent->x_root;
  90. popup->button_y = bevent->y_root;
  91. g_signal_connect (widget, "button-release-event",
  92. G_CALLBACK (gimp_view_popup_button_release),
  93. popup);
  94. g_signal_connect (widget, "unmap",
  95. G_CALLBACK (gimp_view_popup_unmap),
  96. popup);
  97. g_signal_connect (widget, "drag-begin",
  98. G_CALLBACK (gimp_view_popup_drag_begin),
  99. popup);
  100. popup->timeout_id = g_timeout_add (VIEW_POPUP_DELAY,
  101. (GSourceFunc) gimp_view_popup_timeout,
  102. popup);
  103. g_object_set_data_full (G_OBJECT (widget), "gimp-view-popup", popup,
  104. (GDestroyNotify) gimp_view_popup_hide);
  105. gtk_grab_add (widget);
  106. return TRUE;
  107. }
  108. /* private functions */
  109. static void
  110. gimp_view_popup_hide (GimpViewPopup *popup)
  111. {
  112. if (popup->timeout_id)
  113. g_source_remove (popup->timeout_id);
  114. if (popup->popup)
  115. gtk_widget_destroy (popup->popup);
  116. g_signal_handlers_disconnect_by_func (popup->widget,
  117. gimp_view_popup_button_release,
  118. popup);
  119. g_signal_handlers_disconnect_by_func (popup->widget,
  120. gimp_view_popup_unmap,
  121. popup);
  122. g_signal_handlers_disconnect_by_func (popup->widget,
  123. gimp_view_popup_drag_begin,
  124. popup);
  125. gtk_grab_remove (popup->widget);
  126. g_slice_free (GimpViewPopup, popup);
  127. }
  128. static gboolean
  129. gimp_view_popup_button_release (GtkWidget *widget,
  130. GdkEventButton *bevent,
  131. GimpViewPopup *popup)
  132. {
  133. if (bevent->button == popup->button)
  134. g_object_set_data (G_OBJECT (popup->widget), "gimp-view-popup", NULL);
  135. return FALSE;
  136. }
  137. static void
  138. gimp_view_popup_unmap (GtkWidget *widget,
  139. GimpViewPopup *popup)
  140. {
  141. g_object_set_data (G_OBJECT (popup->widget), "gimp-view-popup", NULL);
  142. }
  143. static void
  144. gimp_view_popup_drag_begin (GtkWidget *widget,
  145. GdkDragContext *context,
  146. GimpViewPopup *popup)
  147. {
  148. g_object_set_data (G_OBJECT (popup->widget), "gimp-view-popup", NULL);
  149. }
  150. static gboolean
  151. gimp_view_popup_timeout (GimpViewPopup *popup)
  152. {
  153. GtkWidget *window;
  154. GtkWidget *frame;
  155. GtkWidget *view;
  156. GdkScreen *screen;
  157. GdkRectangle rect;
  158. gint monitor;
  159. gint x;
  160. gint y;
  161. popup->timeout_id = 0;
  162. screen = gtk_widget_get_screen (popup->widget);
  163. window = gtk_window_new (GTK_WINDOW_POPUP);
  164. gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
  165. gtk_window_set_screen (GTK_WINDOW (window), screen);
  166. gtk_window_set_transient_for (GTK_WINDOW (window),
  167. GTK_WINDOW (gtk_widget_get_toplevel (popup->widget)));
  168. frame = gtk_frame_new (NULL);
  169. gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
  170. gtk_container_add (GTK_CONTAINER (window), frame);
  171. gtk_widget_show (frame);
  172. view = gimp_view_new_full (popup->context,
  173. popup->viewable,
  174. popup->popup_width,
  175. popup->popup_height,
  176. 0, TRUE, FALSE, FALSE);
  177. gimp_view_renderer_set_dot_for_dot (GIMP_VIEW (view)->renderer,
  178. popup->dot_for_dot);
  179. gtk_container_add (GTK_CONTAINER (frame), view);
  180. gtk_widget_show (view);
  181. x = popup->button_x - (popup->popup_width / 2);
  182. y = popup->button_y - (popup->popup_height / 2);
  183. monitor = gdk_screen_get_monitor_at_point (screen, x, y);
  184. gdk_screen_get_monitor_workarea (screen, monitor, &rect);
  185. x = CLAMP (x, rect.x, rect.x + rect.width - popup->popup_width);
  186. y = CLAMP (y, rect.y, rect.y + rect.height - popup->popup_height);
  187. gtk_window_move (GTK_WINDOW (window), x, y);
  188. gtk_widget_show (window);
  189. popup->popup = window;
  190. return FALSE;
  191. }