]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/widget.h
Radiant:
[xonotic/netradiant.git] / libs / gtkutil / widget.h
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 #if !defined( INCLUDED_GTKUTIL_WIDGET_H )
23 #define INCLUDED_GTKUTIL_WIDGET_H
24
25 #include <list>
26 #include <gtk/gtkwidget.h>
27 #include "generic/callback.h"
28 #include "warnings.h"
29 #include "debugging/debugging.h"
30
31 inline bool widget_is_visible( GtkWidget* widget ){
32         return GTK_WIDGET_VISIBLE( widget ) != FALSE;
33 }
34
35 inline void widget_set_visible( GtkWidget* widget, bool show ){
36         if ( show ) {
37                 /* workaround for gtk 2.24 issue: not displayed glwidget after toggle */
38                 GtkWidget* glwidget = GTK_WIDGET( g_object_get_data( G_OBJECT( widget ), "glwidget" ) );
39                 if ( glwidget ){
40                         //if ( widget_is_visible( glwidget ) )
41                                 //globalOutputStream() << "glwidget have been already visible :0\n"; /* is not hidden aswell, according to this */
42                         gtk_widget_hide( glwidget );
43                         gtk_widget_show( glwidget );
44                 }
45                 gtk_widget_show( widget );
46         }
47         else
48         {
49                 gtk_widget_hide( widget );
50         }
51 }
52
53
54 inline void widget_toggle_visible( GtkWidget* widget ){
55         widget_set_visible( widget, !widget_is_visible( widget ) );
56 }
57
58 class ToggleItem
59 {
60 BoolExportCallback m_exportCallback;
61 typedef std::list<BoolImportCallback> ImportCallbacks;
62 ImportCallbacks m_importCallbacks;
63 public:
64 ToggleItem( const BoolExportCallback& exportCallback ) : m_exportCallback( exportCallback ){
65 }
66
67 void update(){
68         for ( ImportCallbacks::iterator i = m_importCallbacks.begin(); i != m_importCallbacks.end(); ++i )
69         {
70                 m_exportCallback( *i );
71         }
72 }
73
74 void addCallback( const BoolImportCallback& callback ){
75         m_importCallbacks.push_back( callback );
76         m_exportCallback( callback );
77 }
78 typedef MemberCaller1<ToggleItem, const BoolImportCallback&, &ToggleItem::addCallback> AddCallbackCaller;
79 };
80
81 class ToggleShown
82 {
83 bool m_shownDeferred;
84
85 ToggleShown( const ToggleShown& other ); // NOT COPYABLE
86 ToggleShown& operator=( const ToggleShown& other ); // NOT ASSIGNABLE
87
88 static gboolean notify_visible( GtkWidget* widget, gpointer dummy, ToggleShown* self ){
89         self->update();
90         return FALSE;
91 }
92 static gboolean destroy( GtkWidget* widget, ToggleShown* self ){
93         self->m_shownDeferred = GTK_WIDGET_VISIBLE( self->m_widget ) != FALSE;
94         self->m_widget = 0;
95         return FALSE;
96 }
97 public:
98 GtkWidget* m_widget;
99 ToggleItem m_item;
100
101 ToggleShown( bool shown )
102         : m_shownDeferred( shown ), m_widget( 0 ), m_item( ActiveCaller( *this ) ){
103 }
104 void update(){
105         m_item.update();
106 }
107 bool active() const {
108         if ( m_widget == 0 ) {
109                 return m_shownDeferred;
110         }
111         else
112         {
113                 return GTK_WIDGET_VISIBLE( m_widget ) != FALSE;
114         }
115 }
116 void exportActive( const BoolImportCallback& importCallback ){
117         importCallback( active() );
118 }
119 typedef MemberCaller1<ToggleShown, const BoolImportCallback&, &ToggleShown::exportActive> ActiveCaller;
120 void set( bool shown ){
121         if ( m_widget == 0 ) {
122                 m_shownDeferred = shown;
123         }
124         else
125         {
126                 widget_set_visible( m_widget, shown );
127         }
128 }
129 void toggle(){
130         widget_toggle_visible( m_widget );
131 }
132 typedef MemberCaller<ToggleShown, &ToggleShown::toggle> ToggleCaller;
133 void connect( GtkWidget* widget ){
134         m_widget = widget;
135         widget_set_visible( m_widget, m_shownDeferred );
136         g_signal_connect( G_OBJECT( m_widget ), "notify::visible", G_CALLBACK( notify_visible ), this );
137         g_signal_connect( G_OBJECT( m_widget ), "destroy", G_CALLBACK( destroy ), this );
138         update();
139 }
140 };
141
142
143 inline void widget_queue_draw( GtkWidget& widget ){
144         gtk_widget_queue_draw( &widget );
145 }
146 typedef ReferenceCaller<GtkWidget, widget_queue_draw> WidgetQueueDrawCaller;
147
148
149 inline void widget_make_default( GtkWidget* widget ){
150         GTK_WIDGET_SET_FLAGS( widget, GTK_CAN_DEFAULT );
151         gtk_widget_grab_default( widget );
152 }
153
154 class WidgetFocusPrinter
155 {
156 const char* m_name;
157
158 static gboolean focus_in( GtkWidget *widget, GdkEventFocus *event, WidgetFocusPrinter* self ){
159         globalOutputStream() << self->m_name << " takes focus\n";
160         return FALSE;
161 }
162 static gboolean focus_out( GtkWidget *widget, GdkEventFocus *event, WidgetFocusPrinter* self ){
163         globalOutputStream() << self->m_name << " loses focus\n";
164         return FALSE;
165 }
166 public:
167 WidgetFocusPrinter( const char* name ) : m_name( name ){
168 }
169 void connect( GtkWidget* widget ){
170         g_signal_connect( G_OBJECT( widget ), "focus_in_event", G_CALLBACK( focus_in ), this );
171         g_signal_connect( G_OBJECT( widget ), "focus_out_event", G_CALLBACK( focus_out ), this );
172 }
173 };
174
175 #endif