chiark / gitweb /
Switch to GPL v3
[disorder] / disobedience / popup.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2008 Richard Kettlewell
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #include "disobedience.h"
19 #include "popup.h"
20
21 void popup(GtkWidget **menup,
22            GdkEventButton *event,
23            struct menuitem *items,
24            int nitems,
25            void *extra) {
26   GtkWidget *menu = *menup;
27
28   /* Create the menu if it does not yet exist */
29   if(!menu) {
30     menu = *menup = gtk_menu_new();
31     g_signal_connect(menu, "destroy",
32                      G_CALLBACK(gtk_widget_destroyed), menup);
33     for(int n = 0; n < nitems; ++n) {
34       items[n].w = gtk_menu_item_new_with_label(items[n].name);
35       gtk_menu_attach(GTK_MENU(menu), items[n].w, 0, 1, n, n + 1);
36     }
37     set_tool_colors(menu);
38   }
39   /* Configure item sensitivity */
40   for(int n = 0; n < nitems; ++n) {
41     if(items[n].handlerid)
42       g_signal_handler_disconnect(items[n].w,
43                                   items[n].handlerid);
44     gtk_widget_set_sensitive(items[n].w,
45                              items[n].sensitive(extra));
46     items[n].handlerid = g_signal_connect(items[n].w,
47                                           "activate",
48                                           G_CALLBACK(items[n].activate),
49                                           extra);
50   }
51   /* Pop up the menu */
52   gtk_widget_show_all(menu);
53   gtk_menu_popup(GTK_MENU(menu), 0, 0, 0, 0,
54                  event->button, event->time);
55 }
56
57 /** @brief Make sure the right thing is selected
58  * @param widget Tree view
59  * @param event Mouse event
60  */
61 void ensure_selected(GtkTreeView *treeview,
62                      GdkEventButton *event) {
63   GtkTreeSelection *selection = gtk_tree_view_get_selection(treeview);
64   /* Get the path of the hovered item */
65   GtkTreePath *path;
66   if(!gtk_tree_view_get_path_at_pos(treeview,
67                                     event->x, event->y,
68                                     &path,
69                                     NULL,
70                                     NULL, NULL))
71     return;                     /* If there isn't one, do nothing */
72   if(!gtk_tree_selection_path_is_selected(selection, path)) {
73     /* We're hovered over one thing but it's not the selected row.  This is
74      * very confusing for the poor old user so we select the hovered row. */
75     gtk_tree_selection_unselect_all(selection);
76     gtk_tree_selection_select_path(selection, path);
77   }
78   gtk_tree_path_free(path);
79 }
80
81 /*
82 Local Variables:
83 c-basic-offset:2
84 comment-column:40
85 fill-column:79
86 indent-tabs-mode:nil
87 End:
88 */