2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
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.
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.
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
22 #if !defined( INCLUDED_GTKUTIL_WIDGET_H )
23 #define INCLUDED_GTKUTIL_WIDGET_H
26 #include <gtk/gtkwidget.h>
27 #include "generic/callback.h"
29 #include "debugging/debugging.h"
31 inline bool widget_is_visible( GtkWidget* widget ){
32 return GTK_WIDGET_VISIBLE( widget ) != FALSE;
35 inline void widget_set_visible( GtkWidget* widget, bool 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" ) );
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 );
45 gtk_widget_show( widget );
49 gtk_widget_hide( widget );
54 inline void widget_toggle_visible( GtkWidget* widget ){
55 widget_set_visible( widget, !widget_is_visible( widget ) );
60 BoolExportCallback m_exportCallback;
61 typedef std::list<BoolImportCallback> ImportCallbacks;
62 ImportCallbacks m_importCallbacks;
64 ToggleItem( const BoolExportCallback& exportCallback ) : m_exportCallback( exportCallback ){
68 for ( ImportCallbacks::iterator i = m_importCallbacks.begin(); i != m_importCallbacks.end(); ++i )
70 m_exportCallback( *i );
74 void addCallback( const BoolImportCallback& callback ){
75 m_importCallbacks.push_back( callback );
76 m_exportCallback( callback );
78 typedef MemberCaller1<ToggleItem, const BoolImportCallback&, &ToggleItem::addCallback> AddCallbackCaller;
85 ToggleShown( const ToggleShown& other ); // NOT COPYABLE
86 ToggleShown& operator=( const ToggleShown& other ); // NOT ASSIGNABLE
88 static gboolean notify_visible( GtkWidget* widget, gpointer dummy, ToggleShown* self ){
92 static gboolean destroy( GtkWidget* widget, ToggleShown* self ){
93 self->m_shownDeferred = GTK_WIDGET_VISIBLE( self->m_widget ) != FALSE;
101 ToggleShown( bool shown )
102 : m_shownDeferred( shown ), m_widget( 0 ), m_item( ActiveCaller( *this ) ){
107 bool active() const {
108 if ( m_widget == 0 ) {
109 return m_shownDeferred;
113 return GTK_WIDGET_VISIBLE( m_widget ) != FALSE;
116 void exportActive( const BoolImportCallback& importCallback ){
117 importCallback( active() );
119 typedef MemberCaller1<ToggleShown, const BoolImportCallback&, &ToggleShown::exportActive> ActiveCaller;
120 void set( bool shown ){
121 if ( m_widget == 0 ) {
122 m_shownDeferred = shown;
126 widget_set_visible( m_widget, shown );
130 widget_toggle_visible( m_widget );
132 typedef MemberCaller<ToggleShown, &ToggleShown::toggle> ToggleCaller;
133 void connect( GtkWidget* 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 );
143 inline void widget_queue_draw( GtkWidget& widget ){
144 gtk_widget_queue_draw( &widget );
146 typedef ReferenceCaller<GtkWidget, widget_queue_draw> WidgetQueueDrawCaller;
149 inline void widget_make_default( GtkWidget* widget ){
150 GTK_WIDGET_SET_FLAGS( widget, GTK_CAN_DEFAULT );
151 gtk_widget_grab_default( widget );
154 class WidgetFocusPrinter
158 static gboolean focus_in( GtkWidget *widget, GdkEventFocus *event, WidgetFocusPrinter* self ){
159 globalOutputStream() << self->m_name << " takes focus\n";
162 static gboolean focus_out( GtkWidget *widget, GdkEventFocus *event, WidgetFocusPrinter* self ){
163 globalOutputStream() << self->m_name << " loses focus\n";
167 WidgetFocusPrinter( const char* name ) : m_name( name ){
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 );