]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/button.cpp
allow undo “make detail/structural”, <3 @SpiKe, thanks @Garux, fix #76
[xonotic/netradiant.git] / libs / gtkutil / button.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant 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
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "button.h"
23
24 #include <gtk/gtk.h>
25
26 #include "stream/textstream.h"
27 #include "stream/stringstream.h"
28 #include "generic/callback.h"
29
30 #include "image.h"
31 #include "pointer.h"
32
33 void clicked_closure_callback(ui::Widget widget, gpointer data)
34 {
35     (*reinterpret_cast<Callback<void()> *>( data ))();
36 }
37
38 void button_connect_callback(ui::Button button, const Callback<void()> &callback)
39 {
40 #if 1
41     g_signal_connect_swapped(G_OBJECT(button), "clicked", G_CALLBACK(callback.getThunk()), callback.getEnvironment());
42 #else
43     g_signal_connect_closure( G_OBJECT( button ), "clicked", create_cclosure( G_CALLBACK( clicked_closure_callback ), callback ), FALSE );
44 #endif
45 }
46
47 void button_connect_callback(ui::ToolButton button, const Callback<void()> &callback)
48 {
49 #if 1
50     g_signal_connect_swapped(G_OBJECT(button), "clicked", G_CALLBACK(callback.getThunk()), callback.getEnvironment());
51 #else
52     g_signal_connect_closure( G_OBJECT( button ), "clicked", create_cclosure( G_CALLBACK( clicked_closure_callback ), callback ), FALSE );
53 #endif
54 }
55
56 guint toggle_button_connect_callback(ui::ToggleButton button, const Callback<void()> &callback)
57 {
58 #if 1
59     guint handler = g_signal_connect_swapped(G_OBJECT(button), "toggled", G_CALLBACK(callback.getThunk()),
60                                              callback.getEnvironment());
61 #else
62     guint handler = g_signal_connect_closure( G_OBJECT( button ), "toggled", create_cclosure( G_CALLBACK( clicked_closure_callback ), callback ), TRUE );
63 #endif
64     g_object_set_data(G_OBJECT(button), "handler", gint_to_pointer(handler));
65     return handler;
66 }
67
68 guint toggle_button_connect_callback(ui::ToggleToolButton button, const Callback<void()> &callback)
69 {
70 #if 1
71     guint handler = g_signal_connect_swapped(G_OBJECT(button), "toggled", G_CALLBACK(callback.getThunk()),
72                                              callback.getEnvironment());
73 #else
74     guint handler = g_signal_connect_closure( G_OBJECT( button ), "toggled", create_cclosure( G_CALLBACK( clicked_closure_callback ), callback ), TRUE );
75 #endif
76     g_object_set_data(G_OBJECT(button), "handler", gint_to_pointer(handler));
77     return handler;
78 }
79
80 void button_set_icon(ui::Button button, const char *icon)
81 {
82     ui::Image image = ui::Image(new_local_image(icon));
83     image.show();
84     button.add(image);
85 }
86
87 void toggle_button_set_active_no_signal(ui::ToggleButton button, gboolean active)
88 {
89     //globalOutputStream() << "set active: " << active << "\n";
90     guint handler_id = gpointer_to_int(g_object_get_data(G_OBJECT(button), "handler"));
91     //guint signal_id = g_signal_lookup("toggled", G_OBJECT_TYPE (button));
92     //globalOutputStream() << "signal_id: " << signal_id << "\n";
93     //guint found = g_signal_handler_find(G_OBJECT(button), G_SIGNAL_MATCH_ID, signal_id, 0, 0, 0, 0);
94     //globalOutputStream() << " handler found: " << found << "\n";
95     g_signal_handler_block(G_OBJECT(button), handler_id);
96     gtk_toggle_button_set_active(button, active);
97     g_signal_handler_unblock(G_OBJECT(button), handler_id);
98 }
99
100 void toggle_button_set_active_no_signal(ui::ToggleToolButton button, gboolean active)
101 {
102     guint handler_id = gpointer_to_int(g_object_get_data(G_OBJECT(button), "handler"));
103     g_signal_handler_block(G_OBJECT(button), handler_id);
104     gtk_toggle_tool_button_set_active(button, active);
105     g_signal_handler_unblock(G_OBJECT(button), handler_id);
106 }
107
108
109 void radio_button_print_state(ui::RadioButton button)
110 {
111     globalOutputStream() << "toggle button: ";
112     for (GSList *radio = gtk_radio_button_get_group(button); radio != 0; radio = g_slist_next(radio)) {
113         globalOutputStream() << gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radio->data));
114     }
115     globalOutputStream() << "\n";
116 }
117
118 ui::ToggleButton radio_button_get_nth(ui::RadioButton radio, int index)
119 {
120     GSList *group = gtk_radio_button_get_group(radio);
121     return ui::ToggleButton::from(g_slist_nth_data(group, g_slist_length(group) - index - 1));
122 }
123
124 void radio_button_set_active(ui::RadioButton radio, int index)
125 {
126     //radio_button_print_state(radio);
127     gtk_toggle_button_set_active(radio_button_get_nth(radio, index), TRUE);
128     //radio_button_print_state(radio);
129 }
130
131 void radio_button_set_active_no_signal(ui::RadioButton radio, int index)
132 {
133     {
134         for (GSList *l = gtk_radio_button_get_group(radio); l != 0; l = g_slist_next(l)) {
135             g_signal_handler_block(G_OBJECT(l->data), gpointer_to_int(g_object_get_data(G_OBJECT(l->data), "handler")));
136         }
137     }
138     radio_button_set_active(radio, index);
139     {
140         for (GSList *l = gtk_radio_button_get_group(radio); l != 0; l = g_slist_next(l)) {
141             g_signal_handler_unblock(G_OBJECT(l->data),
142                                      gpointer_to_int(g_object_get_data(G_OBJECT(l->data), "handler")));
143         }
144     }
145 }
146
147 int radio_button_get_active(ui::RadioButton radio)
148 {
149     //radio_button_print_state(radio);
150     GSList *group = gtk_radio_button_get_group(radio);
151     int index = g_slist_length(group) - 1;
152     for (; group != 0; group = g_slist_next(group)) {
153         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(group->data))) {
154             break;
155         } else {
156             index--;
157         }
158     }
159     return index;
160 }