]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/mediasource/extra/netradiant-src/libs/gtkutil/button.cpp
Rename the compiled fteqcc to fteqcc-win32 (as that's what it is)
[voretournament/voretournament.git] / misc / mediasource / extra / netradiant-src / 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/gtkradiobutton.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(GtkWidget* widget, gpointer data)
34 {
35   (*reinterpret_cast<Callback*>(data))();
36 }
37
38 void button_connect_callback(GtkButton* button, const Callback& 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 guint toggle_button_connect_callback(GtkToggleButton* button, const Callback& callback)
48 {
49 #if 1
50   guint handler = g_signal_connect_swapped(G_OBJECT(button), "toggled", G_CALLBACK(callback.getThunk()), callback.getEnvironment());
51 #else
52   guint handler = g_signal_connect_closure(G_OBJECT(button), "toggled", create_cclosure(G_CALLBACK(clicked_closure_callback), callback), TRUE);
53 #endif
54   g_object_set_data(G_OBJECT(button), "handler", gint_to_pointer(handler));
55   return handler;
56 }
57  
58 void button_set_icon(GtkButton* button, const char* icon)
59 {
60   GtkImage* image = new_local_image(icon);
61   gtk_widget_show(GTK_WIDGET(image));
62   gtk_container_add(GTK_CONTAINER(button), GTK_WIDGET(image));
63 }
64
65 void toggle_button_set_active_no_signal(GtkToggleButton* button, gboolean active)
66 {
67   //globalOutputStream() << "set active: " << active << "\n";
68   guint handler_id = gpointer_to_int(g_object_get_data(G_OBJECT(button), "handler"));
69   //guint signal_id = g_signal_lookup("toggled", G_OBJECT_TYPE (button));
70   //globalOutputStream() << "signal_id: " << signal_id << "\n";
71   //guint found = g_signal_handler_find(G_OBJECT(button), G_SIGNAL_MATCH_ID, signal_id, 0, 0, 0, 0);
72   //globalOutputStream() << " handler found: " << found << "\n";
73   g_signal_handler_block(G_OBJECT(button), handler_id);
74   gtk_toggle_button_set_active(button, active);
75   g_signal_handler_unblock(G_OBJECT(button), handler_id);
76 }
77
78
79 void radio_button_print_state(GtkRadioButton* button)
80 {
81   globalOutputStream() << "toggle button: ";
82   for(GSList* radio = gtk_radio_button_group(button); radio != 0; radio = g_slist_next(radio))
83   {
84     globalOutputStream() << gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (radio->data));
85   }
86   globalOutputStream() << "\n";
87 }
88
89 GtkToggleButton* radio_button_get_nth(GtkRadioButton* radio, int index)
90 {
91   GSList *group = gtk_radio_button_group(radio);
92   return GTK_TOGGLE_BUTTON(g_slist_nth_data(group, g_slist_length(group) - index - 1));
93 }
94
95 void radio_button_set_active(GtkRadioButton* radio, int index)
96 {
97   //radio_button_print_state(radio);
98   gtk_toggle_button_set_active(radio_button_get_nth(radio, index), TRUE);
99   //radio_button_print_state(radio);
100 }
101
102 void radio_button_set_active_no_signal(GtkRadioButton* radio, int index)
103 {
104   {
105     for(GSList* l = gtk_radio_button_get_group(radio); l != 0; l = g_slist_next(l))
106     {
107       g_signal_handler_block(G_OBJECT(l->data), gpointer_to_int(g_object_get_data(G_OBJECT(l->data), "handler")));
108     }
109   }
110   radio_button_set_active(radio, index);
111   {
112     for(GSList* l = gtk_radio_button_get_group(radio); l != 0; l = g_slist_next(l))
113     {
114       g_signal_handler_unblock(G_OBJECT(l->data), gpointer_to_int(g_object_get_data(G_OBJECT(l->data), "handler")));
115     }
116   }
117 }
118
119 int radio_button_get_active(GtkRadioButton* radio)
120 {
121   //radio_button_print_state(radio);
122   GSList *group = gtk_radio_button_group(radio);
123   int index = g_slist_length(group) - 1;
124   for (; group != 0; group = g_slist_next(group))
125   {
126     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(group->data)))
127     {
128       break;
129     }
130     else
131     {
132       index--;
133     }
134   }
135   return index;
136 }
137