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 void widget_set_visible( GtkWidget* widget, bool shown ){
33 gtk_widget_show( widget );
37 gtk_widget_hide( widget );
41 inline bool widget_is_visible( GtkWidget* widget ){
42 return GTK_WIDGET_VISIBLE( widget ) != FALSE;
45 inline void widget_toggle_visible( GtkWidget* widget ){
46 widget_set_visible( widget, !widget_is_visible( widget ) );
51 BoolExportCallback m_exportCallback;
52 typedef std::list<BoolImportCallback> ImportCallbacks;
53 ImportCallbacks m_importCallbacks;
55 ToggleItem( const BoolExportCallback& exportCallback ) : m_exportCallback( exportCallback ){
59 for ( ImportCallbacks::iterator i = m_importCallbacks.begin(); i != m_importCallbacks.end(); ++i )
61 m_exportCallback( *i );
65 void addCallback( const BoolImportCallback& callback ){
66 m_importCallbacks.push_back( callback );
67 m_exportCallback( callback );
69 typedef MemberCaller1<ToggleItem, const BoolImportCallback&, &ToggleItem::addCallback> AddCallbackCaller;
76 ToggleShown( const ToggleShown& other ); // NOT COPYABLE
77 ToggleShown& operator=( const ToggleShown& other ); // NOT ASSIGNABLE
79 static gboolean notify_visible( GtkWidget* widget, gpointer dummy, ToggleShown* self ){
83 static gboolean destroy( GtkWidget* widget, ToggleShown* self ){
84 self->m_shownDeferred = GTK_WIDGET_VISIBLE( self->m_widget ) != FALSE;
92 ToggleShown( bool shown )
93 : m_shownDeferred( shown ), m_widget( 0 ), m_item( ActiveCaller( *this ) ){
99 if ( m_widget == 0 ) {
100 return m_shownDeferred;
104 return GTK_WIDGET_VISIBLE( m_widget ) != FALSE;
107 void exportActive( const BoolImportCallback& importCallback ){
108 importCallback( active() );
110 typedef MemberCaller1<ToggleShown, const BoolImportCallback&, &ToggleShown::exportActive> ActiveCaller;
111 void set( bool shown ){
112 if ( m_widget == 0 ) {
113 m_shownDeferred = shown;
117 widget_set_visible( m_widget, shown );
121 widget_toggle_visible( m_widget );
123 typedef MemberCaller<ToggleShown, &ToggleShown::toggle> ToggleCaller;
124 void connect( GtkWidget* widget ){
126 widget_set_visible( m_widget, m_shownDeferred );
127 g_signal_connect( G_OBJECT( m_widget ), "notify::visible", G_CALLBACK( notify_visible ), this );
128 g_signal_connect( G_OBJECT( m_widget ), "destroy", G_CALLBACK( destroy ), this );
134 inline void widget_queue_draw( GtkWidget& widget ){
135 gtk_widget_queue_draw( &widget );
137 typedef ReferenceCaller<GtkWidget, widget_queue_draw> WidgetQueueDrawCaller;
140 inline void widget_make_default( GtkWidget* widget ){
141 GTK_WIDGET_SET_FLAGS( widget, GTK_CAN_DEFAULT );
142 gtk_widget_grab_default( widget );
145 class WidgetFocusPrinter
149 static gboolean focus_in( GtkWidget *widget, GdkEventFocus *event, WidgetFocusPrinter* self ){
150 globalOutputStream() << self->m_name << " takes focus\n";
153 static gboolean focus_out( GtkWidget *widget, GdkEventFocus *event, WidgetFocusPrinter* self ){
154 globalOutputStream() << self->m_name << " loses focus\n";
158 WidgetFocusPrinter( const char* name ) : m_name( name ){
160 void connect( GtkWidget* widget ){
161 g_signal_connect( G_OBJECT( widget ), "focus_in_event", G_CALLBACK( focus_in ), this );
162 g_signal_connect( G_OBJECT( widget ), "focus_out_event", G_CALLBACK( focus_out ), this );