chiark / gitweb /
More comments.
[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 /** @file disobedience/popup.c
19  * @brief Disobedience popup menus
20  */
21 #include "disobedience.h"
22 #include "popup.h"
23
24 void popup(GtkWidget **menup,
25            GdkEventButton *event,
26            struct menuitem *items,
27            int nitems,
28            void *extra) {
29   GtkWidget *menu = *menup;
30
31   /* Create the menu if it does not yet exist */
32   if(!menu) {
33     menu = *menup = gtk_menu_new();
34     g_signal_connect(menu, "destroy",
35                      G_CALLBACK(gtk_widget_destroyed), menup);
36     for(int n = 0; n < nitems; ++n) {
37       items[n].w = gtk_menu_item_new_with_label(items[n].name);
38       gtk_menu_attach(GTK_MENU(menu), items[n].w, 0, 1, n, n + 1);
39     }
40     set_tool_colors(menu);
41   }
42   /* Configure item sensitivity */
43   for(int n = 0; n < nitems; ++n) {
44     if(items[n].handlerid)
45       g_signal_handler_disconnect(items[n].w,
46                                   items[n].handlerid);
47     gtk_widget_set_sensitive(items[n].w,
48                              items[n].sensitive(extra));
49     items[n].handlerid = g_signal_connect(items[n].w,
50                                           "activate",
51                                           G_CALLBACK(items[n].activate),
52                                           extra);
53   }
54   /* Pop up the menu */
55   gtk_widget_show_all(menu);
56   gtk_menu_popup(GTK_MENU(menu), 0, 0, 0, 0,
57                  event->button, event->time);
58 }
59
60 /** @brief Make sure the right thing is selected
61  * @param treeview Tree view
62  * @param event Mouse event
63  */
64 void ensure_selected(GtkTreeView *treeview,
65                      GdkEventButton *event) {
66   GtkTreeSelection *selection = gtk_tree_view_get_selection(treeview);
67   /* Get the path of the hovered item */
68   GtkTreePath *path;
69   if(!gtk_tree_view_get_path_at_pos(treeview,
70                                     event->x, event->y,
71                                     &path,
72                                     NULL,
73                                     NULL, NULL))
74     return;                     /* If there isn't one, do nothing */
75   if(!gtk_tree_selection_path_is_selected(selection, path)) {
76     /* We're hovered over one thing but it's not the selected row.  This is
77      * very confusing for the poor old user so we select the hovered row. */
78     gtk_tree_selection_unselect_all(selection);
79     gtk_tree_selection_select_path(selection, path);
80   }
81   gtk_tree_path_free(path);
82 }
83
84 /*
85 Local Variables:
86 c-basic-offset:2
87 comment-column:40
88 fill-column:79
89 indent-tabs-mode:nil
90 End:
91 */