3 * Copyright (C) 2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
7 * gLabels 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.
12 * gLabels 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.
17 * You should have received a copy of the GNU General Public License
18 * along with gLabels. If not, see <http://www.gnu.org/licenses/>.
23 #include "field-button.h"
25 #include <glib/gi18n.h>
28 #include "field-button-menu.h"
33 /*========================================================*/
35 /*========================================================*/
37 /** GL_FIELD_BUTTON Private fields */
38 struct _glFieldButtonPrivate {
51 /*========================================================*/
52 /* Private globals. */
53 /*========================================================*/
55 static guint signals[LAST_SIGNAL] = {0};
58 /*========================================================*/
59 /* Private function prototypes. */
60 /*========================================================*/
62 static void gl_field_button_finalize (GObject *object);
65 button_press_event_cb (GtkWidget *widget,
66 GdkEventButton *event,
70 menu_key_selected_cb (glFieldButtonMenu *menu,
75 menu_selection_done_cb (GtkMenuShell *object,
79 /*****************************************************************************/
80 /* Object infrastructure. */
81 /*****************************************************************************/
82 G_DEFINE_TYPE (glFieldButton, gl_field_button, GTK_TYPE_TOGGLE_BUTTON);
85 /*****************************************************************************/
86 /* Class Init Function. */
87 /*****************************************************************************/
89 gl_field_button_class_init (glFieldButtonClass *class)
91 GObjectClass *gobject_class = (GObjectClass *) class;
93 gl_field_button_parent_class = g_type_class_peek_parent (class);
95 gobject_class->finalize = gl_field_button_finalize;
97 signals[KEY_SELECTED] =
98 g_signal_new ("key_selected",
99 G_OBJECT_CLASS_TYPE (gobject_class),
101 G_STRUCT_OFFSET (glFieldButtonClass, key_selected),
103 gl_marshal_VOID__STRING,
104 G_TYPE_NONE, 1, G_TYPE_STRING);
108 /*****************************************************************************/
109 /* Object Instance Init Function. */
110 /*****************************************************************************/
112 gl_field_button_init (glFieldButton *this)
117 this->priv = g_new0 (glFieldButtonPrivate, 1);
119 hbox = gtk_hbox_new (FALSE, 3);
120 gtk_container_add (GTK_CONTAINER (this), hbox);
122 this->priv->label = gtk_label_new ("");
123 gtk_misc_set_alignment (GTK_MISC (this->priv->label), 0.0, 0.5);
124 gtk_box_pack_start (GTK_BOX (hbox), this->priv->label, TRUE, TRUE, 0);
126 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_IN);
127 gtk_box_pack_end (GTK_BOX (hbox), arrow, FALSE, FALSE, 0);
129 g_signal_connect (this, "button_press_event",
130 G_CALLBACK(button_press_event_cb), this);
134 /*****************************************************************************/
135 /* Finalize Method. */
136 /*****************************************************************************/
138 gl_field_button_finalize (GObject *object)
142 g_return_if_fail (object && IS_GL_FIELD_BUTTON (object));
143 this = GL_FIELD_BUTTON (object);
145 g_object_ref_sink (this->priv->menu);
148 G_OBJECT_CLASS (gl_field_button_parent_class)->finalize (object);
152 /*****************************************************************************/
153 /** New Object Generator. */
154 /*****************************************************************************/
156 gl_field_button_new (const gchar *name)
160 this = g_object_new (TYPE_GL_FIELD_BUTTON, NULL);
162 gtk_label_set_text (GTK_LABEL (this->priv->label), name);
164 this->priv->menu = gl_field_button_menu_new ();
166 gtk_widget_show_all (this->priv->menu);
168 g_signal_connect (this->priv->menu, "key_selected",
169 G_CALLBACK (menu_key_selected_cb), this);
170 g_signal_connect (this->priv->menu, "selection_done",
171 G_CALLBACK (menu_selection_done_cb), this);
173 return GTK_WIDGET (this);
177 /*****************************************************************************/
179 /*****************************************************************************/
181 gl_field_button_set_keys (glFieldButton *this,
184 gl_field_button_menu_set_keys (GL_FIELD_BUTTON_MENU (this->priv->menu),
187 gtk_widget_show_all (this->priv->menu);
191 /*****************************************************************************/
192 /* Menu positioning function. */
193 /*****************************************************************************/
195 menu_position_function (GtkMenu *menu,
202 gint w_screen, h_screen;
204 gint x_window, y_window;
205 GtkAllocation *allocation;
206 gint x_this, y_this, h_this;
207 GtkRequisition menu_requisition;
213 screen = gtk_widget_get_screen (GTK_WIDGET (this));
214 w_screen = gdk_screen_get_width (screen);
215 h_screen = gdk_screen_get_height (screen);
218 * Absolute position of "this" window on screen.
220 window = gtk_widget_get_window (GTK_WIDGET (this));
221 gdk_window_get_origin (window, &x_window, &y_window);
224 * Position and size of "this" inside window
226 allocation = >K_WIDGET (this)->allocation;
227 x_this = allocation->x;
228 y_this = allocation->y;
229 h_this = allocation->height;
234 gtk_widget_size_request (this->priv->menu, &menu_requisition);
235 h_menu = menu_requisition.height;
236 w_menu = menu_requisition.width;
239 * Default position anchored to lower left corner of "this".
241 *x = x_window + x_this;
242 *y = y_window + y_this + h_this;
245 * Adjust vertical position if menu if extends past bottom of screen.
247 if ( (*y + h_menu) > h_screen )
249 *y = y_window + y_this - h_menu;
253 *y = h_screen - h_menu;
258 * Adjust horizontal position if menu if extends past edge of screen.
260 if ( (*x + w_menu) > w_screen )
262 *x = w_screen - w_menu;
270 /*****************************************************************************/
271 /* Button "button_press_event" callback. */
272 /*****************************************************************************/
274 button_press_event_cb (GtkWidget *widget,
275 GdkEventButton *event,
278 switch (event->button)
282 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (this), TRUE);
284 gtk_menu_popup (GTK_MENU (this->priv->menu),
286 (GtkMenuPositionFunc)menu_position_function, this,
287 event->button, event->time);
299 /*****************************************************************************/
300 /* Menu "key selected" callback. */
301 /*****************************************************************************/
303 menu_key_selected_cb (glFieldButtonMenu *menu,
307 g_signal_emit (this, signals[KEY_SELECTED], 0, field);
311 /*****************************************************************************/
312 /* Menu "selection done" callback. */
313 /*****************************************************************************/
315 menu_selection_done_cb (GtkMenuShell *object,
318 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (this), FALSE);
324 * Local Variables: -- emacs
326 * c-basic-offset: 8 -- emacs
327 * tab-width: 8 -- emacs
328 * indent-tabs-mode: nil -- emacs