No issues found
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
3 /* nautilus-ui-utilities.c - helper functions for GtkUIManager stuff
4
5 Copyright (C) 2004 Red Hat, Inc.
6
7 The Gnome Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 The Gnome Library 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 GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public
18 License along with the Gnome Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
21
22 Authors: Alexander Larsson <alexl@redhat.com>
23 */
24
25 #include <config.h>
26
27 #include "nautilus-ui-utilities.h"
28 #include "nautilus-icon-info.h"
29 #include <eel/eel-graphic-effects.h>
30
31 #include <gio/gio.h>
32 #include <gtk/gtk.h>
33
34 void
35 nautilus_ui_unmerge_ui (GtkUIManager *ui_manager,
36 guint *merge_id,
37 GtkActionGroup **action_group)
38 {
39 if (*merge_id != 0) {
40 gtk_ui_manager_remove_ui (ui_manager,
41 *merge_id);
42 *merge_id = 0;
43 }
44 if (*action_group != NULL) {
45 gtk_ui_manager_remove_action_group (ui_manager,
46 *action_group);
47 *action_group = NULL;
48 }
49 }
50
51 void
52 nautilus_ui_prepare_merge_ui (GtkUIManager *ui_manager,
53 const char *name,
54 guint *merge_id,
55 GtkActionGroup **action_group)
56 {
57 *merge_id = gtk_ui_manager_new_merge_id (ui_manager);
58 *action_group = gtk_action_group_new (name);
59 gtk_action_group_set_translation_domain (*action_group, GETTEXT_PACKAGE);
60 gtk_ui_manager_insert_action_group (ui_manager, *action_group, 0);
61 g_object_unref (*action_group); /* owned by ui manager */
62 }
63
64 static void
65 extension_action_callback (GtkAction *action,
66 gpointer callback_data)
67 {
68 nautilus_menu_item_activate (NAUTILUS_MENU_ITEM (callback_data));
69 }
70
71 GtkAction *
72 nautilus_action_from_menu_item (NautilusMenuItem *item)
73 {
74 char *name, *label, *tip, *icon_name;
75 gboolean sensitive, priority;
76 GtkAction *action;
77 GdkPixbuf *pixbuf;
78
79 g_object_get (G_OBJECT (item),
80 "name", &name, "label", &label,
81 "tip", &tip, "icon", &icon_name,
82 "sensitive", &sensitive,
83 "priority", &priority,
84 NULL);
85
86 action = gtk_action_new (name,
87 label,
88 tip,
89 NULL);
90
91 if (icon_name != NULL) {
92 pixbuf = nautilus_ui_get_menu_icon (icon_name);
93 if (pixbuf != NULL) {
94 gtk_action_set_gicon (action, G_ICON (pixbuf));
95 g_object_unref (pixbuf);
96 }
97 }
98
99 gtk_action_set_sensitive (action, sensitive);
100 g_object_set (action, "is-important", priority, NULL);
101
102 g_signal_connect_data (action, "activate",
103 G_CALLBACK (extension_action_callback),
104 g_object_ref (item),
105 (GClosureNotify)g_object_unref, 0);
106
107 g_free (name);
108 g_free (label);
109 g_free (tip);
110 g_free (icon_name);
111
112 return action;
113 }
114
115 GdkPixbuf *
116 nautilus_ui_get_menu_icon (const char *icon_name)
117 {
118 NautilusIconInfo *info;
119 GdkPixbuf *pixbuf;
120 int size;
121
122 size = nautilus_get_icon_size_for_stock_size (GTK_ICON_SIZE_MENU);
123
124 if (g_path_is_absolute (icon_name)) {
125 info = nautilus_icon_info_lookup_from_path (icon_name, size);
126 } else {
127 info = nautilus_icon_info_lookup_from_name (icon_name, size);
128 }
129 pixbuf = nautilus_icon_info_get_pixbuf_nodefault_at_size (info, size);
130 g_object_unref (info);
131
132 return pixbuf;
133 }
134
135 char *
136 nautilus_escape_action_name (const char *action_name,
137 const char *prefix)
138 {
139 GString *s;
140
141 if (action_name == NULL) {
142 return NULL;
143 }
144
145 s = g_string_new (prefix);
146
147 while (*action_name != 0) {
148 switch (*action_name) {
149 case '\\':
150 g_string_append (s, "\\\\");
151 break;
152 case '/':
153 g_string_append (s, "\\s");
154 break;
155 case '&':
156 g_string_append (s, "\\a");
157 break;
158 case '"':
159 g_string_append (s, "\\q");
160 break;
161 default:
162 g_string_append_c (s, *action_name);
163 }
164
165 action_name ++;
166 }
167 return g_string_free (s, FALSE);
168 }
169
170 static GdkPixbuf *
171 nautilus_get_thumbnail_frame (void)
172 {
173 static GdkPixbuf *thumbnail_frame = NULL;
174
175 if (thumbnail_frame == NULL) {
176 GInputStream *stream = g_resources_open_stream
177 ("/org/gnome/nautilus/icons/thumbnail_frame.png", 0, NULL);
178 if (stream != NULL) {
179 thumbnail_frame = gdk_pixbuf_new_from_stream (stream, NULL, NULL);
180 g_object_unref (stream);
181 }
182 }
183
184 return thumbnail_frame;
185 }
186
187 #define NAUTILUS_THUMBNAIL_FRAME_LEFT 3
188 #define NAUTILUS_THUMBNAIL_FRAME_TOP 3
189 #define NAUTILUS_THUMBNAIL_FRAME_RIGHT 3
190 #define NAUTILUS_THUMBNAIL_FRAME_BOTTOM 3
191
192 void
193 nautilus_ui_frame_image (GdkPixbuf **pixbuf)
194 {
195 GdkPixbuf *pixbuf_with_frame, *frame;
196 int left_offset, top_offset, right_offset, bottom_offset;
197
198 frame = nautilus_get_thumbnail_frame ();
199 if (frame == NULL) {
200 return;
201 }
202
203 left_offset = NAUTILUS_THUMBNAIL_FRAME_LEFT;
204 top_offset = NAUTILUS_THUMBNAIL_FRAME_TOP;
205 right_offset = NAUTILUS_THUMBNAIL_FRAME_RIGHT;
206 bottom_offset = NAUTILUS_THUMBNAIL_FRAME_BOTTOM;
207
208 pixbuf_with_frame = eel_embed_image_in_frame
209 (*pixbuf, frame,
210 left_offset, top_offset, right_offset, bottom_offset);
211 g_object_unref (*pixbuf);
212
213 *pixbuf = pixbuf_with_frame;
214 }