diff options
Diffstat (limited to 'src/fs/gnunet-fs-gtk_main-window-meta-data-context-menu.c')
-rw-r--r-- | src/fs/gnunet-fs-gtk_main-window-meta-data-context-menu.c | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/src/fs/gnunet-fs-gtk_main-window-meta-data-context-menu.c b/src/fs/gnunet-fs-gtk_main-window-meta-data-context-menu.c new file mode 100644 index 00000000..bf3421ee --- /dev/null +++ b/src/fs/gnunet-fs-gtk_main-window-meta-data-context-menu.c | |||
@@ -0,0 +1,193 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file src/fs/gnunet-fs-gtk_main-window-meta-data-context-menu.c | ||
23 | * @brief context menu for the 'meta data' tree view in the main window | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "gnunet-fs-gtk.h" | ||
27 | #include "gnunet-fs-gtk_download-save-as.h" | ||
28 | #include "gnunet-fs-gtk_event-handler.h" | ||
29 | #include <string.h> | ||
30 | |||
31 | |||
32 | |||
33 | /** | ||
34 | * Helper function of GNUNET_GTK_FS_metadata_copy_selection_activated | ||
35 | * which copies the (selected) entries from the tree view to the | ||
36 | * GList. | ||
37 | * | ||
38 | * @param model the tree model with the data | ||
39 | * @param path unused | ||
40 | * @param iter position in the model to access | ||
41 | * @param user_data 'GList**' where we should store the types and values found | ||
42 | */ | ||
43 | static void | ||
44 | copy_metadata_to_clipboard (GtkTreeModel * model, GtkTreePath * path, | ||
45 | GtkTreeIter * iter, gpointer user_data) | ||
46 | { | ||
47 | GList **l = user_data; | ||
48 | gchar *type; | ||
49 | gchar *value; | ||
50 | |||
51 | gtk_tree_model_get (model, iter, 2, &type, 3, &value, -1); | ||
52 | *l = g_list_prepend (*l, type); | ||
53 | *l = g_list_prepend (*l, value); | ||
54 | } | ||
55 | |||
56 | |||
57 | /** | ||
58 | * User activated metadata pop up menu "Copy selection" entry. | ||
59 | * | ||
60 | * @param menuitem the 'copy selection' menu item | ||
61 | * @param user_data the GtkBuilder of the main window | ||
62 | */ | ||
63 | void | ||
64 | GNUNET_GTK_FS_metadata_copy_selection_activated (GtkMenuItem * menuitem, | ||
65 | gpointer user_data) | ||
66 | { | ||
67 | GtkBuilder *builder = GTK_BUILDER (user_data); | ||
68 | GtkTreeView *tree; | ||
69 | GtkClipboard *cb; | ||
70 | GList *pairs; | ||
71 | GList *pos; | ||
72 | GList *value; | ||
73 | GList *type; | ||
74 | guint total_len; | ||
75 | gchar *s; | ||
76 | gchar *p; | ||
77 | |||
78 | tree = | ||
79 | GTK_TREE_VIEW (gtk_builder_get_object | ||
80 | (builder, "GNUNET_GTK_main_window_metadata_treeview")); | ||
81 | pairs = NULL; | ||
82 | gtk_tree_selection_selected_foreach (gtk_tree_view_get_selection (tree), | ||
83 | ©_metadata_to_clipboard, &pairs); | ||
84 | if (NULL == pairs) | ||
85 | return; /* nothing selected */ | ||
86 | total_len = 0; | ||
87 | pairs = g_list_reverse (pairs); | ||
88 | for (pos = pairs; NULL != pos; pos = value->next) | ||
89 | { | ||
90 | type = pos; | ||
91 | value = pos->next; | ||
92 | GNUNET_assert (NULL != value); | ||
93 | total_len += | ||
94 | strlen ((gchar *) type->data) + strlen ((gchar *) value->data) + | ||
95 | 2 /* ": " */ + ((NULL != value->next) ? 1 : 0) /* "\n" */ ; | ||
96 | } | ||
97 | GNUNET_assert (total_len > 0); | ||
98 | total_len++; /* "\0" */ | ||
99 | s = g_new0 (gchar, total_len); | ||
100 | if (NULL == s) | ||
101 | { | ||
102 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); | ||
103 | return; | ||
104 | } | ||
105 | p = s; | ||
106 | for (pos = pairs; NULL != pos; pos = value->next) | ||
107 | { | ||
108 | type = pos; | ||
109 | value = pos->next; | ||
110 | GNUNET_assert (NULL != value); | ||
111 | p = g_stpcpy (p, (gchar *) type->data); | ||
112 | p = g_stpcpy (p, ": "); | ||
113 | p = g_stpcpy (p, (gchar *) value->data); | ||
114 | if (NULL != value->next) | ||
115 | p = g_stpcpy (p, "\n"); | ||
116 | } | ||
117 | g_list_foreach (pairs, (GFunc) &g_free, NULL); | ||
118 | g_list_free (pairs); | ||
119 | cb = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD); | ||
120 | gtk_clipboard_set_text (cb, s, -1); | ||
121 | gtk_clipboard_store (cb); | ||
122 | g_free (s); | ||
123 | } | ||
124 | |||
125 | |||
126 | /** | ||
127 | * Got asked to pop up the context menu in the metadata treeview in | ||
128 | * the main window. Do it. | ||
129 | * | ||
130 | * @param button which button caused the event (0 for none) | ||
131 | * @param event_time time of the event (current time or 'event->time') | ||
132 | * @param user_data the gtk builder of the main window | ||
133 | */ | ||
134 | static void | ||
135 | do_metadata_popup_menu (int button, | ||
136 | int event_time, | ||
137 | GtkBuilder *builder) | ||
138 | { | ||
139 | GtkMenu *menu; | ||
140 | |||
141 | menu = GTK_MENU (gtk_builder_get_object (builder, "metadata_popup_menu")); | ||
142 | gtk_menu_popup (menu, NULL, NULL, NULL, builder, button, event_time); | ||
143 | } | ||
144 | |||
145 | |||
146 | /** | ||
147 | * Got a button press event on the metadata treeview in the main window. | ||
148 | * If it was a right click, pop up the context menu. | ||
149 | * | ||
150 | * @param widget the tree view widget | ||
151 | * @param user_data the gtk builder of the main window | ||
152 | */ | ||
153 | gboolean | ||
154 | GNUNET_GTK_main_window_metadata_treeview_button_press_event_cb (GtkWidget * | ||
155 | widget, | ||
156 | GdkEventButton * | ||
157 | event, | ||
158 | gpointer | ||
159 | user_data) | ||
160 | { | ||
161 | GtkBuilder *builder = GTK_BUILDER (user_data); | ||
162 | |||
163 | /* Ignore double-clicks and triple-clicks */ | ||
164 | if ( (event->button != 3) || (event->type != GDK_BUTTON_PRESS) ) | ||
165 | return FALSE; | ||
166 | do_metadata_popup_menu (event->button, | ||
167 | event->time, | ||
168 | builder); | ||
169 | return TRUE; | ||
170 | } | ||
171 | |||
172 | |||
173 | /** | ||
174 | * Metadata treeview in the main window got the 'popup-menu' signal. | ||
175 | * Pop up the menu. | ||
176 | * | ||
177 | * @param widget the tree view widget | ||
178 | * @param user_data the gtk builder of the main window | ||
179 | * @return TRUE we did it | ||
180 | */ | ||
181 | gboolean | ||
182 | GNUNET_GTK_main_window_metadata_treeview_popup_menu_cb (GtkWidget * widget, | ||
183 | gpointer user_data) | ||
184 | { | ||
185 | GtkBuilder *builder = GTK_BUILDER (user_data); | ||
186 | |||
187 | do_metadata_popup_menu (0 /* no button */, | ||
188 | gtk_get_current_event_time (), | ||
189 | builder); | ||
190 | return TRUE; | ||
191 | } | ||
192 | |||
193 | /* end of gnunet-fs-gtk_meta-data-context-menu.c */ | ||