aboutsummaryrefslogtreecommitdiffstats
path: root/lib/egg/eggdropdowntoolbutton.c
blob: 0ffe2f588d896a85aac61d3e88e00a524e05026d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* GTK - The GIMP Toolkit
 *
 * Copyright (C) 2003 Ricardo Fernandez Pascual
 * Copyright (C) 2004 Paolo Borelli
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


#include "config.h"

#include "eggdropdowntoolbutton.h"

#include <gtk/gtktogglebutton.h>
#include <gtk/gtkarrow.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtkmenu.h>
#include <gtk/gtkmain.h>
#include <gdk/gdkkeysyms.h>


#define EGG_DROPDOWN_TOOL_BUTTON_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EGG_TYPE_DROPDOWN_TOOL_BUTTON, EggDropdownToolButtonPrivate))

struct _EggDropdownToolButtonPrivate
{
  GtkWidget *button;
  GtkWidget *arrow_button;
  GtkMenu   *menu;
};

static void egg_dropdown_tool_button_init       (EggDropdownToolButton      *button);
static void egg_dropdown_tool_button_class_init (EggDropdownToolButtonClass *klass);
static void egg_dropdown_tool_button_finalize   (GObject *object);

enum {
  MENU_ACTIVATED,
  LAST_SIGNAL
};

static gint signals[LAST_SIGNAL];

static GObjectClass *parent_class = NULL;

GType
egg_dropdown_tool_button_get_type (void)
{
  static GType type = 0;

  if (type == 0)
    {
      static const GTypeInfo info =
    {
      sizeof (EggDropdownToolButtonClass),
      (GBaseInitFunc) 0,
      (GBaseFinalizeFunc) 0,
      (GClassInitFunc) egg_dropdown_tool_button_class_init,
      (GClassFinalizeFunc) 0,
      NULL,
      sizeof (EggDropdownToolButton),
      0, /* n_preallocs */
      (GInstanceInitFunc) egg_dropdown_tool_button_init
    };

      type = g_type_register_static (GTK_TYPE_TOOL_BUTTON,
                                     "EggDropdownToolButton",
                                     &info, 0);
    }

  return type;
}

static gboolean
egg_dropdown_tool_button_set_tooltip (GtkToolItem *tool_item,
                                      GtkTooltips *tooltips,
                                      const char *tip_text,
                                      const char *tip_private)
{
  EggDropdownToolButton *button;

  g_return_val_if_fail (EGG_IS_DROPDOWN_TOOL_BUTTON (tool_item), FALSE);

  button = EGG_DROPDOWN_TOOL_BUTTON (tool_item);
  gtk_tooltips_set_tip (tooltips, button->priv->button, tip_text, tip_private);

  return TRUE;
}

static void
egg_dropdown_tool_button_class_init (EggDropdownToolButtonClass *klass)
{
  GObjectClass *object_class;
  GtkToolItemClass *toolitem_class;
  GtkToolButtonClass *toolbutton_class;

  parent_class = g_type_class_peek_parent (klass);

  object_class = (GObjectClass *)klass;
  toolitem_class = (GtkToolItemClass *)klass;
  toolbutton_class = (GtkToolButtonClass *)klass;

  object_class->finalize = egg_dropdown_tool_button_finalize;
  toolitem_class->set_tooltip = egg_dropdown_tool_button_set_tooltip;

  signals[MENU_ACTIVATED] =
    g_signal_new ("menu-activated",
                  G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (EggDropdownToolButtonClass, menu_activated),
          NULL, NULL,
          g_cclosure_marshal_VOID__VOID,
          G_TYPE_NONE, 0);

  g_type_class_add_private (object_class, sizeof (EggDropdownToolButtonPrivate));
}

static void
button_state_changed_cb (GtkWidget             *widget,
             GtkStateType           previous_state,
             EggDropdownToolButton *button)
{
  EggDropdownToolButtonPrivate *priv = button->priv;
  GtkWidget *b;
  GtkStateType state = GTK_WIDGET_STATE (widget);

  b = (widget == priv->arrow_button) ? priv->button : priv->arrow_button;

  g_signal_handlers_block_by_func (G_OBJECT (b),
                                   G_CALLBACK (button_state_changed_cb),
                                   button);

  if (state == GTK_STATE_PRELIGHT &&
      previous_state != GTK_STATE_ACTIVE)
    {
      gtk_widget_set_state (b, state);
    }
  else if (state == GTK_STATE_NORMAL)
    {
      gtk_widget_set_state (b, state);
    }
  else if (state == GTK_STATE_ACTIVE)
    {
      gtk_widget_set_state (b, GTK_STATE_NORMAL);
    }

  g_signal_handlers_unblock_by_func (G_OBJECT (b),
                                     G_CALLBACK (button_state_changed_cb),
                                     button);
}

static void
menu_position_func (GtkMenu               *menu,
            int                   *x,
            int                   *y,
            gboolean              *push_in,
            EggDropdownToolButton *button)
{
  EggDropdownToolButtonPrivate *priv;
  GtkWidget *widget;
  GtkRequisition menu_requisition;
  int max_x, max_y;

  priv = button->priv;
  widget = GTK_WIDGET (button);

  gdk_window_get_origin (widget->window, x, y);
  gtk_widget_size_request (GTK_WIDGET (priv->menu), &menu_requisition);

  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
    *x += widget->allocation.x +
          widget->allocation.width -
          menu_requisition.width;
  else
    *x += widget->allocation.x;

  *y += widget->allocation.y + widget->allocation.height;

  /* Make sure we are on the screen.  */
  max_x = MAX (0, gdk_screen_width () - menu_requisition.width);
  max_y = MAX (0, gdk_screen_height () - menu_requisition.height);

  *x = CLAMP (*x, 0, max_x);
  *y = CLAMP (*y, 0, max_y);
}

static void
popup_menu_under_arrow (EggDropdownToolButton *button,
                        GdkEventButton        *event)
{
  EggDropdownToolButtonPrivate *priv = button->priv;

  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), TRUE);

  g_signal_emit (button, signals[MENU_ACTIVATED], 0);

  if (!priv->menu)
    return;

  gtk_menu_popup (priv->menu, NULL, NULL,
                  (GtkMenuPositionFunc) menu_position_func,
                  button,
                  event ? event->button : 0,
                  event ? event->time : gtk_get_current_event_time ());
}

static gboolean
arrow_button_press_event_cb (GtkWidget             *widget,
                             GdkEventButton        *event,
                             EggDropdownToolButton *button)
{
  popup_menu_under_arrow (button, event);

  return TRUE;
}

static gboolean
arrow_key_press_event_cb (GtkWidget             *widget,
                          GdkEventKey           *event,
                          EggDropdownToolButton *button)
{
  if (event->keyval == GDK_space    ||
      event->keyval == GDK_KP_Space ||
      event->keyval == GDK_Return   ||
      event->keyval == GDK_KP_Enter ||
      event->keyval == GDK_Menu)
    {
      popup_menu_under_arrow (button, NULL);
    }

  return FALSE;
}

/* right click on the button shows the menu */
static gboolean
button_button_press_event_cb (GtkWidget             *widget,
                              GdkEventButton        *event, 
                              EggDropdownToolButton *button)
{
  if (event->button == 3)
    {
      popup_menu_under_arrow (button, event);

      return TRUE;
    }

  return FALSE;
}

static void
button_popup_menu_cb (GtkWidget             *widget,
                      EggDropdownToolButton *button)
{
    popup_menu_under_arrow (button, NULL);
}

static void
egg_dropdown_tool_button_init (EggDropdownToolButton *button)
{
  GtkWidget *hbox;
  GtkWidget *arrow;
  GtkWidget *arrow_button;
  GtkWidget *real_button;

  button->priv = EGG_DROPDOWN_TOOL_BUTTON_GET_PRIVATE (button);

  gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);

  hbox = gtk_hbox_new (FALSE, 0);

  real_button = GTK_BIN (button)->child;
  g_object_ref (real_button);
  gtk_container_remove (GTK_CONTAINER (button), real_button);
  gtk_container_add (GTK_CONTAINER (hbox), real_button);
  g_object_unref (real_button);

  arrow_button = gtk_toggle_button_new ();
  arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
  gtk_button_set_relief (GTK_BUTTON (arrow_button), GTK_RELIEF_NONE);
  gtk_container_add (GTK_CONTAINER (arrow_button), arrow);

  gtk_box_pack_end (GTK_BOX (hbox), arrow_button,
                    FALSE, FALSE, 0);
  gtk_widget_show_all (hbox);

  gtk_container_add (GTK_CONTAINER (button), hbox);

  button->priv->button = real_button;
  button->priv->arrow_button = arrow_button;

  g_signal_connect (real_button, "state_changed",
                    G_CALLBACK (button_state_changed_cb), button);
  g_signal_connect (real_button, "button_press_event",
                    G_CALLBACK (button_button_press_event_cb), button);
  g_signal_connect (real_button, "popup_menu",
                    G_CALLBACK (button_popup_menu_cb), button);
  g_signal_connect (arrow_button, "state_changed",
                    G_CALLBACK (button_state_changed_cb), button);
  g_signal_connect (arrow_button, "key_press_event",
                    G_CALLBACK (arrow_key_press_event_cb), button);
  g_signal_connect (arrow_button, "button_press_event",
                    G_CALLBACK (arrow_button_press_event_cb), button);
}

static void
egg_dropdown_tool_button_finalize (GObject *object)
{
  EggDropdownToolButton *button;

  button = EGG_DROPDOWN_TOOL_BUTTON (object);

  if (button->priv->menu)
    g_object_unref (button->priv->menu);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

GtkToolItem *
egg_dropdown_tool_button_new (void)
{
  EggDropdownToolButton *button;

  button = g_object_new (EGG_TYPE_DROPDOWN_TOOL_BUTTON,
             NULL);

  return GTK_TOOL_ITEM (button);
}

GtkToolItem *
egg_dropdown_tool_button_new_from_stock (const gchar *stock_id)
{
  EggDropdownToolButton *button;

  g_return_val_if_fail (stock_id != NULL, NULL);

  button = g_object_new (EGG_TYPE_DROPDOWN_TOOL_BUTTON,
             "stock_id", stock_id,
             NULL);

  return GTK_TOOL_ITEM (button);
}

/* Callback for the "deactivate" signal on the pop-up menu.
 * This is used so that we unset the state of the toggle button
 * when the pop-up menu disappears. 
 */
static int
menu_deactivate_cb (GtkMenuShell          *menu_shell,
            EggDropdownToolButton *button)
{
  EggDropdownToolButtonPrivate *priv = button->priv;

  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);

  return TRUE;
}

void
egg_dropdown_tool_button_set_menu (EggDropdownToolButton *button,
                                   GtkMenuShell          *menu)
{
  EggDropdownToolButtonPrivate *priv;

  g_return_if_fail (EGG_IS_DROPDOWN_TOOL_BUTTON (button));
  g_return_if_fail (GTK_IS_MENU (menu));

  priv = button->priv;

  if (priv->menu != GTK_MENU (menu))
    {
      if (priv->menu)
        g_object_unref (priv->menu);

      priv->menu = GTK_MENU (menu);

      g_object_ref (priv->menu);
      gtk_object_sink (GTK_OBJECT (priv->menu));

      g_signal_connect (button->priv->menu, "deactivate",
                        G_CALLBACK (menu_deactivate_cb), button);
    }
}

GtkMenuShell *
egg_dropdown_tool_button_get_menu (EggDropdownToolButton *button)
{
  return GTK_MENU_SHELL (button->priv->menu);
}

void
egg_dropdown_tool_button_set_arrow_tooltip (EggDropdownToolButton *button,
                                            GtkTooltips *tooltips,
                                            const gchar *tip_text,
                                            const gchar *tip_private)
{
  g_return_if_fail (EGG_IS_DROPDOWN_TOOL_BUTTON (button));

  gtk_tooltips_set_tip (tooltips, button->priv->arrow_button, tip_text, tip_private);
}