libyui-gtk  2.43.7
 All Classes
ygtkmenubutton.c
1 /********************************************************************
2  * YaST2-GTK - http://en.opensuse.org/YaST2-GTK *
3  ********************************************************************/
4 
5 /* YGtkMenuButton widget */
6 // check the header file for information about this widget
7 
8 #include <yui/Libyui_config.h>
9 #include "ygtkmenubutton.h"
10 #include <gtk/gtk.h>
11 #include <gdk/gdkkeysyms.h>
12 #include "YGMacros.h"
13 
14 //** YGtkPopupWindow
15 
16 G_DEFINE_TYPE (YGtkPopupWindow, ygtk_popup_window, GTK_TYPE_WINDOW)
17 
18 static void ygtk_popup_window_init (YGtkPopupWindow *popup)
19 {
20  GtkWindow *window = GTK_WINDOW (popup);
21  gtk_window_set_resizable (window, FALSE);
22 
23  GtkWidget *frame = gtk_frame_new (NULL);
24  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
25  gtk_widget_show (frame);
26  gtk_container_add (GTK_CONTAINER (window), frame);
27 }
28 
29 static void ygtk_popup_window_hide (GtkWidget *widget)
30 {
31  gtk_grab_remove (widget);
32  GTK_WIDGET_CLASS (ygtk_popup_window_parent_class)->hide (widget);
33 }
34 
35 static gboolean ygtk_popup_window_key_press_event (GtkWidget *widget, GdkEventKey *event)
36 {
37  if (event->keyval == GDK_KEY_Escape) {
38  gtk_widget_hide (widget);
39  return TRUE;
40  }
41  return GTK_WIDGET_CLASS (ygtk_popup_window_parent_class)->key_press_event
42  (widget, event);
43 }
44 
45 static gboolean ygtk_popup_window_button_press_event (GtkWidget *widget,
46  GdkEventButton *event)
47 {
48  // NOTE: You can't rely on events x and y since the event may take place
49  // outside of the window.
50  // So, we'll check if this widget (or any of its kids) got the event
51  // If that's not the case, close the menu
52 
53  GtkWidget *child = gtk_get_event_widget ((GdkEvent *) event);
54  if (child != widget)
55  while (child) {
56  if (child == widget)
57  return FALSE;
58  child = gtk_widget_get_parent(child);
59  }
60  gtk_widget_hide (widget);
61  return TRUE;
62 }
63 
64 GtkWidget *ygtk_popup_window_new (GtkWidget *child)
65 {
66  GtkWidget *widget = g_object_new (YGTK_TYPE_POPUP_WINDOW,
67  "type", GTK_WINDOW_POPUP, NULL);
68  GtkWidget *frame = gtk_bin_get_child (GTK_BIN (widget));
69  gtk_container_add (GTK_CONTAINER (frame), child);
70  return widget;
71 }
72 
73 static void ygtk_popup_window_frame_position (GtkWidget *widget, gint *x, gint *y)
74 { // don't let it go outside the screen
75  GtkRequisition req;
76  gtk_widget_get_preferred_size(widget, &req, NULL);
77 
78  GdkScreen *screen = gtk_widget_get_screen (widget);
79  gint monitor_num = gdk_screen_get_monitor_at_window (screen, gtk_widget_get_root_window (widget));
80  GdkRectangle monitor;
81  gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
82 
83  if (*x < monitor.x)
84  *x = monitor.x;
85  else if (*x + req.width > monitor.x + monitor.width)
86  *x = monitor.x + monitor.width - req.width;
87 
88  if (*y < monitor.y)
89  *y = monitor.y;
90  else if (*y + req.height > monitor.y + monitor.height)
91  *y = monitor.y + monitor.height - req.height;
92 }
93 
94 void ygtk_popup_window_popup (GtkWidget *widget, gint x, gint y, guint activate_time)
95 {
96  ygtk_popup_window_frame_position (widget, &x, &y);
97 
98  gtk_grab_add (widget);
99  gtk_window_move (GTK_WINDOW (widget), x, y);
100  gtk_widget_grab_focus (widget);
101  gtk_widget_show (widget);
102 
103  GdkWindow *window = gtk_widget_get_window (widget);
104  GdkDisplay *display = gdk_window_get_display (window);
105  GdkDeviceManager *device_manager = gdk_display_get_device_manager (display);
106  GdkDevice *pointer = gdk_device_manager_get_client_pointer (device_manager);
107 
108  // grab this with your teeth
109  if (gdk_device_grab (pointer, window, GDK_OWNERSHIP_NONE, TRUE,
110  GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK,
111  NULL, activate_time) == 0) {
112  GdkDevice *keyboard;
113  keyboard = gdk_device_get_associated_device (pointer);
114  if (gdk_device_grab (keyboard, window, GDK_OWNERSHIP_NONE, TRUE,
115  GDK_KEY_PRESS | GDK_KEY_RELEASE, NULL, activate_time) != 0)
116  gdk_device_ungrab (pointer, activate_time);
117  }
118 }
119 
120 static void ygtk_popup_window_class_init (YGtkPopupWindowClass *klass)
121 {
122  ygtk_popup_window_parent_class = g_type_class_peek_parent (klass);
123 
124  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
125  widget_class->key_press_event = ygtk_popup_window_key_press_event;
126  widget_class->button_press_event = ygtk_popup_window_button_press_event;
127  widget_class->hide = ygtk_popup_window_hide;
128 }
129 
130 //** YGtkMenuButton
131 
132 G_DEFINE_TYPE (YGtkMenuButton, ygtk_menu_button, GTK_TYPE_TOGGLE_BUTTON)
133 
134 static void ygtk_menu_button_init (YGtkMenuButton *button)
135 {
136 }
137 
138 static void ygtk_menu_button_free_popup (YGtkMenuButton *button)
139 {
140  if (button->popup) {
141  gtk_widget_destroy (GTK_WIDGET (button->popup));
142  g_object_unref (G_OBJECT (button->popup));
143  button->popup = NULL;
144  }
145 }
146 
147 static void ygtk_menu_button_finalize (GObject *object)
148 {
149  ygtk_menu_button_free_popup (YGTK_MENU_BUTTON (object));
150  G_OBJECT_CLASS (ygtk_menu_button_parent_class)->finalize (object);
151 }
152 
153 static void ygtk_menu_button_get_popup_pos (YGtkMenuButton *button, gint *x, gint *y)
154 {
155  GtkWidget *widget = GTK_WIDGET (button);
156  GtkAllocation button_alloc;
157  gtk_widget_get_allocation(widget, &button_alloc);
158 
159  // the popup would look awful if smaller than the button
160  GtkRequisition req;
161  gtk_widget_get_preferred_size (button->popup, &req, NULL);
162  int popup_width = req.width, popup_height = req.height;
163  if (button_alloc.width > req.width) {
164  gtk_widget_set_size_request (button->popup, button_alloc.width, -1);
165  popup_width = button_alloc.width;
166  }
167 
168  gdk_window_get_origin (gtk_widget_get_window(widget), x, y);
169  *x += button_alloc.x - popup_width*button->xalign;
170  *y += (button_alloc.y-popup_height) + (button_alloc.height+popup_height)*button->yalign;
171 
172  // GTK doesn't push up menus if they are near the bottom, but we will...
173  int screen_height;
174  screen_height = gdk_screen_get_height (gtk_widget_get_screen (widget));
175  if (*y > screen_height - popup_height)
176  *y -= popup_height + button_alloc.height;
177 }
178 
179 static void ygtk_menu_button_get_menu_pos (GtkMenu *menu, gint *x, gint *y,
180  gboolean *push_in, gpointer data)
181 {
182  ygtk_menu_button_get_popup_pos (YGTK_MENU_BUTTON (data), x, y);
183  *push_in = TRUE;
184 }
185 
186 static void ygtk_menu_button_show_popup (YGtkMenuButton *button)
187 {
188  GtkWidget *popup = button->popup;
189  if (!popup)
190  return;
191 
192  guint activate_time = gtk_get_current_event_time();
193  if (GTK_IS_MENU (popup))
194  gtk_menu_popup (GTK_MENU (popup), NULL, NULL, ygtk_menu_button_get_menu_pos,
195  button, 0, activate_time);
196  else { // GTK_IS_WINDOW
197  gint x, y;
198  ygtk_menu_button_get_popup_pos (button, &x, &y);
199  ygtk_popup_window_popup (popup, x, y, activate_time);
200  }
201 }
202 
203 static void ygtk_menu_button_hide_popup (YGtkMenuButton *button)
204 {
205  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), FALSE);
206 }
207 
208 static void ygtk_menu_button_button_toggle (GtkToggleButton *button)
209 {
210  if (gtk_toggle_button_get_active (button))
211  ygtk_menu_button_show_popup (YGTK_MENU_BUTTON (button));
212  else
213  ygtk_menu_button_hide_popup (YGTK_MENU_BUTTON (button));
214 }
215 
216 static gint ygtk_menu_button_button_press (GtkWidget *widget, GdkEventButton *event)
217 {
218  if (event->type == GDK_BUTTON_PRESS && event->button == 1) {
219  if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))) {
220  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
221  ygtk_menu_button_show_popup (YGTK_MENU_BUTTON (widget));
222  }
223  else
224  ygtk_menu_button_hide_popup (YGTK_MENU_BUTTON (widget));
225  return TRUE;
226  }
227  return FALSE;
228 }
229 
230 GtkWidget *ygtk_menu_button_new (void)
231 {
232  return g_object_new (YGTK_TYPE_MENU_BUTTON, NULL);
233 }
234 
235 GtkWidget *ygtk_menu_button_new_with_label (const gchar *label)
236 {
237  GtkWidget *button = ygtk_menu_button_new();
238  ygtk_menu_button_set_label (YGTK_MENU_BUTTON (button), label);
239  return button;
240 }
241 
242 void ygtk_menu_button_set_label (YGtkMenuButton *button, const gchar *label)
243 {
244  if (!button->label) {
245  GtkWidget *hbox, *arrow;
246  hbox = YGTK_HBOX_NEW(4);
247  gtk_box_set_homogeneous (GTK_BOX (hbox), FALSE);
248  arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_IN);
249  button->label = gtk_label_new ("");
250  gtk_box_pack_start (GTK_BOX (hbox), button->label, TRUE, TRUE, 0);
251  gtk_box_pack_start (GTK_BOX (hbox), arrow, FALSE, TRUE, 0);
252  gtk_container_add (GTK_CONTAINER (button), hbox);
253  gtk_widget_show_all (hbox);
254  }
255  if (label && *label) {
256  gtk_widget_show (button->label);
257  gtk_label_set_text_with_mnemonic (GTK_LABEL (button->label), label);
258  }
259  else
260  gtk_widget_hide (button->label);
261 }
262 
263 static void menu_button_hide_popup (GtkWidget *widget, YGtkMenuButton *button)
264 { ygtk_menu_button_hide_popup (button); }
265 
266 void ygtk_menu_button_set_popup_align (YGtkMenuButton *button, GtkWidget *popup,
267  gfloat xalign, gfloat yalign)
268 {
269  ygtk_menu_button_free_popup (button);
270  button->xalign = xalign;
271  button->yalign = yalign;
272 
273  if (!GTK_IS_MENU (popup) && !IS_YGTK_POPUP_WINDOW (popup)) {
274  // install widget on a YGtkPopupMenu
275  button->popup = ygtk_popup_window_new (popup);
276  }
277  else
278  button->popup = popup;
279 
280  g_object_ref_sink (G_OBJECT (button->popup));
281  g_signal_connect (G_OBJECT (button->popup), "hide",
282  G_CALLBACK (menu_button_hide_popup), button);
283 }
284 
285 void ygtk_menu_button_set_popup (YGtkMenuButton *button, GtkWidget *popup)
286 {
287  ygtk_menu_button_set_popup_align (button, popup, 0.0, 1.0);
288 }
289 
290 static void ygtk_menu_button_class_init (YGtkMenuButtonClass *klass)
291 {
292  ygtk_menu_button_parent_class = g_type_class_peek_parent (klass);
293 
294  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
295  gobject_class->finalize = ygtk_menu_button_finalize;
296 
297  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
298  widget_class->button_press_event = ygtk_menu_button_button_press;
299 
300  GtkToggleButtonClass *toggle_button_class = GTK_TOGGLE_BUTTON_CLASS (klass);
301  toggle_button_class->toggled = ygtk_menu_button_button_toggle;
302 }