chiark / gitweb /
ce17a73405d7df5b051a669dffa2d78d8df98e32
[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 2 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, 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 this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 #include "disobedience.h"
21 #include "popup.h"
22
23 void popup(GtkWidget **menup,
24            GdkEventButton *event,
25            struct menuitem *items,
26            int nitems,
27            void *extra) {
28   GtkWidget *menu = *menup;
29
30   /* Create the menu if it does not yet exist */
31   if(!menu) {
32     menu = *menup = gtk_menu_new();
33     g_signal_connect(menu, "destroy",
34                      G_CALLBACK(gtk_widget_destroyed), menup);
35     for(int n = 0; n < nitems; ++n) {
36       items[n].w = gtk_menu_item_new_with_label(items[n].name);
37       gtk_menu_attach(GTK_MENU(menu), items[n].w, 0, 1, n, n + 1);
38     }
39     set_tool_colors(menu);
40   }
41   /* Configure item sensitivity */
42   for(int n = 0; n < nitems; ++n) {
43     if(items[n].handlerid)
44       g_signal_handler_disconnect(items[n].w,
45                                   items[n].handlerid);
46     gtk_widget_set_sensitive(items[n].w,
47                              items[n].sensitive(extra));
48     items[n].handlerid = g_signal_connect(items[n].w,
49                                           "activate",
50                                           G_CALLBACK(items[n].activate),
51                                           extra);
52   }
53   /* Pop up the menu */
54   gtk_widget_show_all(menu);
55   gtk_menu_popup(GTK_MENU(menu), 0, 0, 0, 0,
56                  event->button, event->time);
57 }
58
59 /** @brief Make sure that something is selected
60  * @param widget Tree view
61  * @param event Mouse event (the hovered row will be selected)
62  */
63 void ensure_selected(GtkTreeView *treeview,
64                      GdkEventButton *event) {
65   GtkTreeSelection *selection = gtk_tree_view_get_selection(treeview);
66   if(gtk_tree_selection_count_selected_rows(selection) == 0) {
67     GtkTreePath *path;
68     if(gtk_tree_view_get_path_at_pos(treeview,
69                                      event->x, event->y,
70                                      &path,
71                                      NULL,
72                                      NULL, NULL)) 
73       gtk_tree_selection_select_path(selection, path);
74     gtk_tree_path_free(path);
75   }
76 }
77
78 /*
79 Local Variables:
80 c-basic-offset:2
81 comment-column:40
82 fill-column:79
83 indent-tabs-mode:nil
84 End:
85 */