/
gtk-utils.c
171 lines (136 loc) · 4.19 KB
/
gtk-utils.c
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
/*
* mtpc
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <string.h>
#include <glib.h>
#include "gtk-utils.h"
#include "glib-utils.h"
GtkBuilder * _gtk_builder_new_from_file(const char *ui_file)
{
char *filename;
GtkBuilder *builder;
GError *error = NULL;
filename = g_build_filename(MTPC_UI_DIR, ui_file, NULL);
builder = gtk_builder_new();
if (!gtk_builder_add_from_file(builder, filename, &error)) {
g_warning("%s\n", error->message);
g_clear_error(&error);
}
g_free(filename);
return builder;
}
void _gtk_widget_hide(GtkWidget *widget)
{
if (widget != NULL)
gtk_widget_hide(widget);
}
void _gtk_widget_show(GtkWidget *widget)
{
if (widget != NULL)
gtk_widget_show(widget);
}
/* -- _gtk_menu_ask_drag_drop_action --(from gthumb) */
typedef struct {
GMainLoop *loop;
GdkDragAction action_name;
} DropActionData;
static void
ask_drag_drop_action_menu_deactivate_cb(GtkMenuShell *menushell,
gpointer user_data)
{
DropActionData *drop_data = user_data;
if (g_main_loop_is_running(drop_data->loop))
g_main_loop_quit(drop_data->loop);
}
static void
ask_drag_drop_action_item_activate_cb(GtkMenuItem *menuitem,
gpointer user_data)
{
DropActionData *drop_data = user_data;
drop_data->action_name = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menuitem), "drop-action"));
if (g_main_loop_is_running(drop_data->loop))
g_main_loop_quit(drop_data->loop);
}
static void
_gtk_menu_ask_drag_drop_action_append_item(GtkWidget *menu,
const char *label,
GdkDragAction actions,
GdkDragAction action,
DropActionData *drop_data)
{
GtkWidget *item;
item = gtk_menu_item_new_with_mnemonic(label);
g_object_set_data(G_OBJECT(item), "drop-action", GINT_TO_POINTER(action));
gtk_widget_set_sensitive(item, ((actions & action) != 0));
gtk_widget_show(item);
gtk_menu_shell_append(GTK_MENU_SHELL (menu), item);
g_signal_connect(item,
"activate",
G_CALLBACK(ask_drag_drop_action_item_activate_cb),
drop_data);
}
GdkDragAction
_gtk_menu_ask_drag_drop_action(GtkWidget *widget,
GdkDragAction actions,
guint32 activate_time)
{
DropActionData drop_data;
GtkWidget *menu;
GtkWidget *item;
drop_data.action_name = 0;
drop_data.loop = g_main_loop_new(NULL, FALSE);
menu = gtk_menu_new();
gtk_menu_set_screen(GTK_MENU(menu), gtk_widget_get_screen(widget));
_gtk_menu_ask_drag_drop_action_append_item(menu,
_("_Copy Here"),
actions,
GDK_ACTION_COPY,
&drop_data);
_gtk_menu_ask_drag_drop_action_append_item(menu,
_("_Move Here"),
actions,
GDK_ACTION_MOVE,
&drop_data);
_gtk_menu_ask_drag_drop_action_append_item(menu,
_("_Link Here"),
actions,
GDK_ACTION_LINK,
&drop_data);
item = gtk_separator_menu_item_new();
gtk_widget_show(item);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
item = gtk_menu_item_new_with_label(_("Cancel"));
gtk_widget_show(item);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
g_signal_connect(menu,
"deactivate",
G_CALLBACK(ask_drag_drop_action_menu_deactivate_cb),
&drop_data);
gtk_menu_popup(GTK_MENU(menu),
NULL,
NULL,
NULL,
NULL,
0,
activate_time);
gtk_grab_add(menu);
g_main_loop_run(drop_data.loop);
gtk_grab_remove(menu);
gtk_widget_destroy(menu);
g_main_loop_unref(drop_data.loop);
return drop_data.action_name;
}