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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
|
/*
* File: selection.c
*
* Copyright 2004 Sebastian Geerken <s.geerken@ping.de>
*
* 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 of the License, or
* (at your option) any later version.
*/
/*
* This Gtk+ widget is a variant of GtkButton, which adds two features:
*
* 1. the possibility to react on different mouse buttons (not only button
* 1), and
* 2. a clean way to attach menus.
*
* To archieve this use one of the following functions:
*
* a_Gtk_ext_button_set_inactive,
* a_Gtk_ext_button_set_command,
* a_Gtk_ext_button_attach_menu, or
* a_Gtk_ext_button_attach_menu_creator.
*
* See comments there for more informations.
*
* About signals: For the command button mode, there are some new signals,
* "clicked1", "clicked2", and "clicked3", which are emitted for the respective
* mouse button. The signal "clicked" should be connected to as well, since
* it is still used for non-mouse event handling, e.g. when then button is
* activated by the <Enter> key.
*/
#include "gtk_ext_button.h"
#include <gtk/gtklabel.h>
#include <gtk/gtkmain.h>
#include <gtk/gtksignal.h>
static void Gtk_ext_button_class_init (GtkExtButtonClass *klass);
static void Gtk_ext_button_init (GtkExtButton *button);
static void Gtk_ext_button_destroy (GtkObject *object);
static gint Gtk_ext_button_button_press (GtkWidget *widget,
GdkEventButton *event);
static gint Gtk_ext_button_button_release (GtkWidget *widget,
GdkEventButton *event);
static gint Gtk_ext_button_enter_notify (GtkWidget *widget,
GdkEventCrossing *event);
static gint Gtk_ext_button_leave_notify (GtkWidget *widget,
GdkEventCrossing *event);
static void Gtk_ext_button_menu_hidden (GtkExtButton *ext_button);
static gint clicked_signals[3];
/*
* Create a new GtkExtButton with no child.
*/
GtkWidget *a_Gtk_ext_button_new ()
{
return gtk_type_new (a_Gtk_ext_button_get_type ());
}
/*
* Create a new GtkExtButton with a label.
*/
GtkWidget *a_Gtk_ext_button_new_with_label (const gchar *label)
{
GtkWidget *button;
GtkWidget *label_widget;
button = a_Gtk_ext_button_new ();
label_widget = gtk_label_new (label);
gtk_misc_set_alignment (GTK_MISC (label_widget), 0.5, 0.5);
gtk_container_add (GTK_CONTAINER (button), label_widget);
gtk_widget_show (label_widget);
return button;
}
/*
* Standard Gtk+ function.
*/
guint a_Gtk_ext_button_get_type ()
{
static gint type = 0;
if (!type) {
GtkTypeInfo info = {
"GtkExtButton",
sizeof (GtkExtButton),
sizeof (GtkExtButtonClass),
(GtkClassInitFunc) Gtk_ext_button_class_init,
(GtkObjectInitFunc) Gtk_ext_button_init,
(GtkArgSetFunc)NULL,
(GtkArgGetFunc)NULL,
(GtkClassInitFunc)NULL
};
type = gtk_type_unique (gtk_button_get_type (), &info);
}
return type;
}
/*
* Standard Gtk+ function.
*/
static void Gtk_ext_button_class_init(GtkExtButtonClass *klass)
{
GtkObjectClass *object_class;
GtkWidgetClass *widget_class;
object_class = (GtkObjectClass*) klass;
object_class->destroy = Gtk_ext_button_destroy;
clicked_signals[0] =
gtk_signal_new ("clicked1",
GTK_RUN_FIRST | GTK_RUN_ACTION,
object_class->type,
GTK_SIGNAL_OFFSET (GtkExtButtonClass, clicked1),
gtk_marshal_NONE__NONE, GTK_TYPE_NONE, 0);
clicked_signals[1] =
gtk_signal_new ("clicked2",
GTK_RUN_FIRST | GTK_RUN_ACTION,
object_class->type,
GTK_SIGNAL_OFFSET (GtkExtButtonClass, clicked2),
gtk_marshal_NONE__NONE, GTK_TYPE_NONE, 0);
clicked_signals[2] =
gtk_signal_new ("clicked3",
GTK_RUN_FIRST | GTK_RUN_ACTION,
object_class->type,
GTK_SIGNAL_OFFSET (GtkExtButtonClass, clicked3),
gtk_marshal_NONE__NONE, GTK_TYPE_NONE, 0);
gtk_object_class_add_signals (object_class, clicked_signals, 3);
widget_class = (GtkWidgetClass*)klass;
widget_class->button_press_event = Gtk_ext_button_button_press;
widget_class->button_release_event = Gtk_ext_button_button_release;
widget_class->enter_notify_event = Gtk_ext_button_enter_notify;
widget_class->leave_notify_event = Gtk_ext_button_leave_notify;
}
/*
* Standard Gtk+ function.
*/
static void Gtk_ext_button_init(GtkExtButton *button)
{
int i;
GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
button->pressed_button = 0;
button->active_menu = NULL;
button->menu_signal_id = -1;
for(i = 0; i < 5; i++)
button->action[i] = GTK_EXT_BUTTON_INACTIVE;
}
/*
* Standard Gtk+ function.
*/
static void Gtk_ext_button_destroy (GtkObject *object)
{
GtkExtButton *button;
button = GTK_EXT_BUTTON (object);
if (button->menu_signal_id != -1)
gtk_signal_disconnect (GTK_OBJECT (button->active_menu),
button->menu_signal_id);
}
/*
* This is used for gtk_menu_popup, to position the menu.
*/
static void Gtk_ext_button_position_menu (GtkMenu *menu,
gint *x,
gint *y,
gpointer user_data)
{
GtkWidget *widget;
GtkRequisition requisition;
widget = GTK_WIDGET (user_data);
gdk_window_get_origin (widget->window, x, y);
gtk_widget_size_request (GTK_WIDGET (menu), &requisition);
if (*y + widget->allocation.height + requisition.height
> gdk_screen_height())
/* Show menu above button, since there is not enough space below. */
*y -= requisition.height;
else
/* Show menu below button. */
*y += widget->allocation.height;
/* If the menu does not fit horizontilly, adjust position. */
if (*x + requisition.width > gdk_screen_width ())
*x = gdk_screen_width () - requisition.width;
}
/*
* Standard Gtk+ function.
*/
static gint Gtk_ext_button_button_press (GtkWidget *widget,
GdkEventButton *event)
{
GtkButton *button;
GtkExtButton *ext_button;
GtkStateType new_state;
GtkMenu *menu;
button = GTK_BUTTON (widget);
ext_button = GTK_EXT_BUTTON (widget);
if (ext_button->pressed_button) {
/* Already a button pressed. */
return FALSE;
} else if (event->button >= 1 && event->button <= 3 &&
ext_button->action[event->button - 1]
!= GTK_EXT_BUTTON_INACTIVE) {
ext_button->pressed_button = event->button;
gtk_grab_add (widget);
button->button_down = TRUE;
new_state = (button->in_button ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL);
if (GTK_WIDGET_STATE (button) != new_state) {
gtk_widget_set_state (GTK_WIDGET (button), new_state);
gtk_widget_queue_draw (GTK_WIDGET (button));
}
menu = NULL;
switch(ext_button->action[event->button - 1]) {
case GTK_EXT_BUTTON_INACTIVE:
g_assert_not_reached ();
break;
case GTK_EXT_BUTTON_COMMAND:
/* Nothing to do anymore. */
break;
case GTK_EXT_BUTTON_MENU:
menu = ext_button->action_data[event->button - 1].menu;
break;
case GTK_EXT_BUTTON_MENU_CREATOR:
menu =
ext_button->action_data[event->button - 1].creator.func (
ext_button,
ext_button->action_data[event->button - 1].creator.data);
break;
}
if (menu) {
ext_button->active_menu = menu;
gtk_menu_popup (menu, NULL, widget, Gtk_ext_button_position_menu,
widget, event->button, event->time);
ext_button->menu_signal_id =
gtk_signal_connect_object (GTK_OBJECT (menu), "hide",
GTK_SIGNAL_FUNC (
Gtk_ext_button_menu_hidden),
(gpointer) button);
}
return TRUE;
} else
return FALSE;
}
/*
* Standard Gtk+ function.
*/
static gint Gtk_ext_button_button_release (GtkWidget *widget,
GdkEventButton *event)
{
GtkButton *button;
GtkExtButton *ext_button;
GtkStateType new_state;
ext_button = GTK_EXT_BUTTON (widget);
/* Only react on the button initially pressed. */
if (event->button == ext_button->pressed_button) {
button = GTK_BUTTON (widget);
if (button->in_button)
gtk_signal_emit (GTK_OBJECT (widget),
clicked_signals[event->button - 1]);
gtk_grab_remove (widget);
button->button_down = FALSE;
ext_button->pressed_button = 0;
new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
if (GTK_WIDGET_STATE (button) != new_state) {
gtk_widget_set_state (GTK_WIDGET (button), new_state);
/* We _draw () instead of queue_draw so that if the operation
* blocks, the label doesn't vanish.
*/
gtk_widget_draw (GTK_WIDGET (button), NULL);
}
}
return TRUE;
}
/*
* Standard Gtk+ function.
*/
static gint Gtk_ext_button_enter_notify (GtkWidget *widget,
GdkEventCrossing *event)
{
GtkButton *button;
GtkWidget *event_widget;
GtkStateType new_state;
button = GTK_BUTTON (widget);
event_widget = gtk_get_event_widget ((GdkEvent*) event);
if ((event_widget == widget) && (event->detail != GDK_NOTIFY_INFERIOR)) {
button->in_button = TRUE;
new_state =
(button->button_down ? GTK_STATE_ACTIVE : GTK_STATE_PRELIGHT);
if (GTK_WIDGET_STATE (button) != new_state) {
gtk_widget_set_state (GTK_WIDGET (button), new_state);
gtk_widget_queue_draw (GTK_WIDGET (button));
}
}
return FALSE;
}
/*
* Standard Gtk+ function.
*/
static gint Gtk_ext_button_leave_notify (GtkWidget *widget,
GdkEventCrossing *event)
{
GtkButton *button;
GtkExtButton *ext_button;
GtkWidget *event_widget;
button = GTK_BUTTON (widget);
event_widget = gtk_get_event_widget ((GdkEvent*) event);
if ((event_widget == widget) &&
(event->detail != GDK_NOTIFY_INFERIOR)) {
button->in_button = FALSE;
ext_button = GTK_EXT_BUTTON (widget);
/* If a men is attached for this mouse button, we keep the button widget
* inset. */
if (ext_button->pressed_button == 0 ||
ext_button->action[ext_button->pressed_button -1]
== GTK_EXT_BUTTON_COMMAND) {
/* Otherwise, normal behavior. */
if (GTK_WIDGET_STATE (button) != GTK_STATE_NORMAL) {
gtk_widget_set_state (GTK_WIDGET (button), GTK_STATE_NORMAL);
gtk_widget_queue_draw (GTK_WIDGET (button));
}
}
}
return FALSE;
}
/*
* This function is called, when a popped up menu is hidden again, to reset
* the state of the button.
*/
static void Gtk_ext_button_menu_hidden (GtkExtButton *ext_button)
{
GtkButton *button;
g_return_if_fail (ext_button->menu_signal_id != -1);
g_return_if_fail (ext_button->active_menu != NULL);
gtk_signal_disconnect (GTK_OBJECT (ext_button->active_menu),
ext_button->menu_signal_id);
ext_button->menu_signal_id = -1;
ext_button->active_menu = NULL;
gtk_grab_remove (GTK_WIDGET (ext_button));
button = GTK_BUTTON (ext_button);
button->in_button = FALSE;
button->button_down = FALSE;
ext_button->pressed_button = 0;
gtk_widget_set_state (GTK_WIDGET (button), GTK_STATE_NORMAL);
gtk_widget_queue_draw (GTK_WIDGET (button));
}
/*
* Set no action for the specific mouse button. This is the default.
*/
void a_Gtk_ext_button_set_inactive (GtkExtButton *button,
gint button_no)
{
g_return_if_fail (button_no >= 1 && button_no <= 3);
button->action[button_no - 1] = GTK_EXT_BUTTON_INACTIVE;
}
/*
* Make the button behave like normal command button.
*/
void a_Gtk_ext_button_set_command (GtkExtButton *button,
gint button_no)
{
g_return_if_fail (button_no >= 1 && button_no <= 3);
button->action[button_no - 1] = GTK_EXT_BUTTON_COMMAND;
}
/*
* Attach a fixed menu to the button, which is popped up, when the user
* presses the respective button.
*/
void a_Gtk_ext_button_attach_menu (GtkExtButton *button,
gint button_no,
GtkMenu *menu)
{
g_return_if_fail (button_no >= 1 && button_no <= 3);
button->action[button_no - 1] = GTK_EXT_BUTTON_MENU;
button->action_data[button_no - 1].menu = menu;
}
/*
* Attach a dynamically created menu to the button, which is popped up, when
* the user presses the respective button.
*
* When the respective button has been pressed, the creator function is called
* with the button, and the value of the argument data, the return value must
* be the menu, which is then popped up.
*/
void a_Gtk_ext_button_attach_menu_creator (GtkExtButton *button,
gint button_no,
GtkExtButtonMenuCreator *creator,
gpointer data)
{
g_return_if_fail (button_no >= 1 && button_no <= 3);
button->action[button_no - 1] = GTK_EXT_BUTTON_MENU_CREATOR;
button->action_data[button_no - 1].creator.func = creator;
button->action_data[button_no - 1].creator.data = data;
}
|