Fri Oct 27 19:45:32 2006 John Ellis <johne@verizon.net>
[geeqie.git] / src / ui_menu.c
1 /*
2  * (SLIK) SimpLIstic sKin functions
3  * (C) 2004 John Ellis
4  *
5  * Author: John Ellis
6  *
7  * This software is released under the GNU General Public License (GNU GPL).
8  * Please read the included file COPYING for more information.
9  * This software comes with no warranty of any kind, use at your own risk!
10  */
11
12 #ifdef HAVE_CONFIG_H
13 #  include "config.h"
14 #endif
15 #include "intl.h"
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <gtk/gtk.h>
22
23 #include "ui_menu.h"
24
25
26 /*
27  *-----------------------------------------------------------------------------
28  * menu items
29  *-----------------------------------------------------------------------------
30  */ 
31
32 static void menu_item_finish(GtkWidget *menu, GtkWidget *item, GCallback func, gpointer data)
33 {
34         if (func) g_signal_connect(G_OBJECT(item), "activate", func, data);
35         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
36         gtk_widget_show(item);
37 }
38
39 GtkWidget *menu_item_add(GtkWidget *menu, const gchar *label,
40                          GCallback func, gpointer data)
41 {
42         GtkWidget *item;
43
44         item = gtk_menu_item_new_with_mnemonic(label);
45         menu_item_finish(menu, item, func, data);
46
47         return item;
48 }
49
50 GtkWidget *menu_item_add_stock(GtkWidget *menu, const gchar *label, const gchar *stock_id,
51                                GCallback func, gpointer data)
52 {
53         GtkWidget *item;
54         GtkWidget *image;
55
56         item = gtk_image_menu_item_new_with_mnemonic(label);
57         image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
58         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
59         gtk_widget_show(image);
60         menu_item_finish(menu, item, func, data);
61
62         return item;
63 }
64
65 GtkWidget *menu_item_add_sensitive(GtkWidget *menu, const gchar *label, gint sensitive,
66                                    GCallback func, gpointer data)
67 {
68         GtkWidget *item;
69
70         item = menu_item_add(menu, label, func, data);
71         gtk_widget_set_sensitive(item, sensitive);
72
73         return item;
74 }
75
76 GtkWidget *menu_item_add_stock_sensitive(GtkWidget *menu, const gchar *label, const gchar *stock_id, gint sensitive,
77                                          GCallback func, gpointer data)
78 {
79         GtkWidget *item;
80
81         item = menu_item_add_stock(menu, label, stock_id, func, data);
82         gtk_widget_set_sensitive(item, sensitive);
83         
84         return item;
85 }
86
87 GtkWidget *menu_item_add_check(GtkWidget *menu, const gchar *label, gint active,
88                                GCallback func, gpointer data)
89 {
90         GtkWidget *item;
91
92         item = gtk_check_menu_item_new_with_mnemonic(label);
93         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), active);
94         menu_item_finish(menu, item, func, data);
95
96         return item;
97 }
98
99 void menu_item_add_divider(GtkWidget *menu)
100 {
101         GtkWidget *item = gtk_menu_item_new();
102         gtk_widget_set_sensitive(item, FALSE);
103         gtk_menu_shell_append(GTK_MENU_SHELL(menu),item);
104         gtk_widget_show(item);
105 }
106
107 GtkWidget *menu_item_add_simple(GtkWidget *menu, const gchar *label,
108                                 GCallback func, gpointer data)
109 {
110         GtkWidget *item = gtk_menu_item_new_with_label(label);
111         menu_item_finish(menu, item, func, data);
112
113         return item;
114 }
115
116 /*
117  *-----------------------------------------------------------------------------
118  * popup menus
119  *-----------------------------------------------------------------------------
120  */ 
121
122 static void popup_menu_short_lived_cb(GtkWidget *widget, gpointer data)
123 {
124         /* destroy the menu */
125         g_object_unref(G_OBJECT(data));
126 }
127
128 GtkWidget *popup_menu_short_lived(void)
129 {
130         GtkWidget *menu;
131
132         menu = gtk_menu_new();
133
134         /* take ownership of menu */
135 #ifdef GTK_OBJECT_FLOATING
136         /* GTK+ < 2.10 */
137         g_object_ref(G_OBJECT(menu));
138         gtk_object_sink(GTK_OBJECT(menu));
139 #else
140         /* GTK+ >= 2.10 */
141         g_object_ref_sink(G_OBJECT(menu));
142 #endif
143
144         g_signal_connect(G_OBJECT(menu), "selection_done",
145                          G_CALLBACK(popup_menu_short_lived_cb), menu);
146         return menu;
147 }
148
149 gint popup_menu_position_clamp(GtkMenu *menu, gint *x, gint *y, gint height)
150 {
151         gint adjusted = FALSE;
152         gint w, h;
153         gint xw, xh;
154         
155         w = GTK_WIDGET(menu)->requisition.width;
156         h = GTK_WIDGET(menu)->requisition.height;
157         xw = gdk_screen_width();
158         xh = gdk_screen_height();
159
160         if (*x + w > xw)
161                 {
162                 *x = xw - w;
163                 adjusted = TRUE;
164                 }
165         if (*y + h > xh)
166                 {
167                 if (height)
168                         {
169                         *y = MAX(0, *y - h - height);
170                         }
171                 else
172                         {
173                         *y = xh - h;
174                         }
175                 adjusted = TRUE;
176                 };
177
178         if (*x < 0)
179                 {
180                 *x = 0;
181                 adjusted = TRUE;
182                 }
183         if (*y < 0)
184                 {
185                 *y = 0;
186                 adjusted = TRUE;
187                 }
188
189         return adjusted;
190 }
191