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
|
/*
* Copyright (C) 2002 Christophe Fergeau
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ephy-arrow-toolbutton.h"
#include "ephy-marshal.h"
#include "ephy-gui.h"
#include "ephy-debug.h"
#include <gtk/gtkarrow.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtktogglebutton.h>
#include <gtk/gtkmenu.h>
#include <gtk/gtkmain.h>
#define EPHY_ARROW_TOOLBUTTON_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_ARROW_TOOLBUTTON, EphyArrowToolButtonPrivate))
struct EphyArrowToolButtonPrivate
{
GtkWidget *arrow_widget;
GtkWidget *button;
GtkMenu *menu;
};
enum EphyArrowToolButtonSignalsEnum {
EPHY_ARROW_TOOL_BUTTON_MENU_ACTIVATED,
EPHY_ARROW_TOOL_BUTTON_LAST_SIGNAL
};
/* GObject boilerplate code */
static void ephy_arrow_toolbutton_init (EphyArrowToolButton *arrow_toolbutton);
static void ephy_arrow_toolbutton_class_init (EphyArrowToolButtonClass *klass);
static void ephy_arrow_toolbutton_finalize (GObject *object);
static GObjectClass *parent_class = NULL;
static gint EphyArrowToolButtonSignals[EPHY_ARROW_TOOL_BUTTON_LAST_SIGNAL];
GType
ephy_arrow_toolbutton_get_type (void)
{
static GType ephy_arrow_toolbutton_type = 0;
if (ephy_arrow_toolbutton_type == 0)
{
static const GTypeInfo our_info =
{
sizeof (EphyArrowToolButtonClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) ephy_arrow_toolbutton_class_init,
NULL,
NULL, /* class_data */
sizeof (EphyArrowToolButton),
0, /* n_preallocs */
(GInstanceInitFunc) ephy_arrow_toolbutton_init
};
ephy_arrow_toolbutton_type = g_type_register_static (GTK_TYPE_TOOL_BUTTON,
"EphyArrowToolButton",
&our_info, 0);
}
return ephy_arrow_toolbutton_type;
}
static gboolean
ephy_arrow_toolbutton_set_tooltip (GtkToolItem *tool_item,
GtkTooltips *tooltips,
const char *tip_text,
const char *tip_private)
{
EphyArrowToolButton *button = EPHY_ARROW_TOOLBUTTON (tool_item);
g_return_val_if_fail (EPHY_IS_ARROW_TOOLBUTTON (button), FALSE);
gtk_tooltips_set_tip (tooltips, button->priv->button, tip_text, tip_private);
return TRUE;
}
static void
ephy_arrow_toolbutton_class_init (EphyArrowToolButtonClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkToolItemClass *tool_item_class = GTK_TOOL_ITEM_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = ephy_arrow_toolbutton_finalize;
tool_item_class->set_tooltip = ephy_arrow_toolbutton_set_tooltip;
EphyArrowToolButtonSignals[EPHY_ARROW_TOOL_BUTTON_MENU_ACTIVATED] =
g_signal_new
("menu-activated", G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP,
G_STRUCT_OFFSET (EphyArrowToolButtonClass, menu_activated),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
g_type_class_add_private (object_class, sizeof (EphyArrowToolButtonPrivate));
}
static void
button_state_changed_cb (GtkWidget *widget,
GtkStateType previous_state,
EphyArrowToolButton *b)
{
EphyArrowToolButtonPrivate *p = b->priv;
GtkWidget *button;
GtkStateType state = GTK_WIDGET_STATE (widget);
button = (widget == p->arrow_widget) ? p->button : p->arrow_widget;
g_signal_handlers_block_by_func
(G_OBJECT (button),
G_CALLBACK (button_state_changed_cb),
b);
if (state == GTK_STATE_PRELIGHT &&
previous_state != GTK_STATE_ACTIVE)
{
gtk_widget_set_state (button, state);
}
else if (state == GTK_STATE_NORMAL)
{
gtk_widget_set_state (button, state);
}
else if (state == GTK_STATE_ACTIVE)
{
gtk_widget_set_state (button, GTK_STATE_NORMAL);
}
g_signal_handlers_unblock_by_func
(G_OBJECT (button),
G_CALLBACK (button_state_changed_cb),
b);
}
static void
popup_menu_under_arrow (EphyArrowToolButton *b, GdkEventButton *event)
{
EphyArrowToolButtonPrivate *p = b->priv;
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (p->arrow_widget), TRUE);
LOG ("Emit menu activated signal");
g_signal_emit (b, EphyArrowToolButtonSignals[EPHY_ARROW_TOOL_BUTTON_MENU_ACTIVATED], 0);
gtk_menu_popup (p->menu, NULL, NULL, ephy_gui_menu_position_under_widget, b,
event ? event->button : 0,
event ? event->time : gtk_get_current_event_time ());
}
static void
menu_deactivated_cb (GtkMenuShell *ms, EphyArrowToolButton *b)
{
EphyArrowToolButtonPrivate *p = b->priv;
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (p->arrow_widget), FALSE);
}
static gboolean
arrow_button_press_event_cb (GtkWidget *widget, GdkEventButton *event, EphyArrowToolButton *b)
{
popup_menu_under_arrow (b, event);
return TRUE;
}
static gboolean
arrow_key_press_event_cb (GtkWidget *widget, GdkEventKey *event, EphyArrowToolButton *b)
{
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 (b, NULL);
}
return FALSE;
}
static void
ephy_arrow_toolbutton_init (EphyArrowToolButton *arrowtb)
{
GtkWidget *hbox;
GtkWidget *arrow;
GtkWidget *arrow_button;
GtkWidget *real_button;
arrowtb->priv = EPHY_ARROW_TOOLBUTTON_GET_PRIVATE (arrowtb);
gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (arrowtb), FALSE);
hbox = gtk_hbox_new (FALSE, 0);
gtk_widget_show (hbox);
real_button = GTK_BIN (arrowtb)->child;
g_object_ref (real_button);
gtk_container_remove (GTK_CONTAINER (arrowtb), real_button);
gtk_container_add (GTK_CONTAINER (hbox), real_button);
gtk_container_add (GTK_CONTAINER (arrowtb), hbox);
g_object_unref (real_button);
arrow_button = gtk_toggle_button_new ();
gtk_widget_show (arrow_button);
arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
gtk_widget_show (arrow);
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);
arrowtb->priv->button = real_button;
arrowtb->priv->arrow_widget = arrow_button;
arrowtb->priv->menu = GTK_MENU (gtk_menu_new ());
g_object_ref (arrowtb->priv->menu);
gtk_object_sink (GTK_OBJECT (arrowtb->priv->menu));
g_signal_connect (arrowtb->priv->menu, "deactivate",
G_CALLBACK (menu_deactivated_cb), arrowtb);
g_signal_connect (real_button, "state_changed",
G_CALLBACK (button_state_changed_cb),
arrowtb);
g_signal_connect (arrow_button, "state_changed",
G_CALLBACK (button_state_changed_cb),
arrowtb);
g_signal_connect (arrow_button, "key_press_event",
G_CALLBACK (arrow_key_press_event_cb),
arrowtb);
g_signal_connect (arrow_button, "button_press_event",
G_CALLBACK (arrow_button_press_event_cb),
arrowtb);
}
static void
ephy_arrow_toolbutton_finalize (GObject *object)
{
EphyArrowToolButton *arrow_toolbutton = EPHY_ARROW_TOOLBUTTON (object);
g_object_unref (arrow_toolbutton->priv->menu);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
GtkMenuShell *
ephy_arrow_toolbutton_get_menu (EphyArrowToolButton *b)
{
return GTK_MENU_SHELL (b->priv->menu);
}
|