widget.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #if !defined(INCLUDED_GTKUTIL_WIDGET_H)
00023 #define INCLUDED_GTKUTIL_WIDGET_H
00024
00025 #include <list>
00026 #include <gtk/gtkwidget.h>
00027 #include "generic/callback.h"
00028 #include "debugging/debugging.h"
00029
00030 inline void widget_set_size (GtkWidget* widget, int width, int height)
00031 {
00032 if (height == 0) {
00033 GtkRequisition size;
00034 gtk_widget_size_request(widget, &size);
00035 gtk_widget_set_size_request(widget, width, size.height);
00036 } else {
00037 gtk_widget_set_size_request(widget, width, height);
00038 }
00039 }
00040
00041 inline void widget_set_visible (GtkWidget* widget, bool shown)
00042 {
00043 if (shown) {
00044 gtk_widget_show(widget);
00045 } else {
00046 gtk_widget_hide(widget);
00047 }
00048 }
00049
00050 inline bool widget_is_visible (GtkWidget* widget)
00051 {
00052 return GTK_WIDGET_VISIBLE(widget) != FALSE;
00053 }
00054
00055 inline void widget_toggle_visible (GtkWidget* widget)
00056 {
00057 widget_set_visible(widget, !widget_is_visible(widget));
00058 }
00059
00060 class ToggleItem
00061 {
00062 BoolExportCallback m_exportCallback;
00063 typedef std::list<BoolImportCallback> ImportCallbacks;
00064 ImportCallbacks m_importCallbacks;
00065 public:
00066 ToggleItem (const BoolExportCallback& exportCallback) :
00067 m_exportCallback(exportCallback)
00068 {
00069 }
00070
00071 void update ()
00072 {
00073 for (ImportCallbacks::iterator i = m_importCallbacks.begin(); i != m_importCallbacks.end(); ++i) {
00074 m_exportCallback(*i);
00075 }
00076 }
00077
00078 void addCallback (const BoolImportCallback& callback)
00079 {
00080 m_importCallbacks.push_back(callback);
00081 m_exportCallback(callback);
00082 }
00083 typedef MemberCaller1<ToggleItem, const BoolImportCallback&, &ToggleItem::addCallback> AddCallbackCaller;
00084 };
00085
00086 class ToggleShown
00087 {
00088 bool m_shownDeferred;
00089
00090 ToggleShown (const ToggleShown& other);
00091 ToggleShown& operator= (const ToggleShown& other);
00092
00093 static gboolean notify_visible (GtkWidget* widget, gpointer dummy, ToggleShown* self)
00094 {
00095 self->update();
00096 return FALSE;
00097 }
00098 static gboolean destroy (GtkWidget* widget, ToggleShown* self)
00099 {
00100 self->m_shownDeferred = GTK_WIDGET_VISIBLE(self->m_widget) != FALSE;
00101 self->m_widget = 0;
00102 return FALSE;
00103 }
00104 public:
00105 GtkWidget* m_widget;
00106 ToggleItem m_item;
00107
00108 ToggleShown (bool shown) :
00109 m_shownDeferred(shown), m_widget(0), m_item(ActiveCaller(*this))
00110 {
00111 }
00112 void update ()
00113 {
00114 m_item.update();
00115 }
00116 bool active () const
00117 {
00118 if (m_widget == 0) {
00119 return m_shownDeferred;
00120 } else {
00121 return GTK_WIDGET_VISIBLE(m_widget) != FALSE;
00122 }
00123 }
00124 void exportActive (const BoolImportCallback& importCallback)
00125 {
00126 importCallback(active());
00127 }
00128 typedef MemberCaller1<ToggleShown, const BoolImportCallback&, &ToggleShown::exportActive> ActiveCaller;
00129 void set (bool shown)
00130 {
00131 if (m_widget == 0) {
00132 m_shownDeferred = shown;
00133 } else {
00134 widget_set_visible(m_widget, shown);
00135 }
00136 }
00137 void toggle ()
00138 {
00139 widget_toggle_visible(m_widget);
00140 }
00141 typedef MemberCaller<ToggleShown, &ToggleShown::toggle> ToggleCaller;
00142 void connect (GtkWidget* widget)
00143 {
00144 m_widget = widget;
00145 widget_set_visible(m_widget, m_shownDeferred);
00146 g_signal_connect(G_OBJECT(m_widget), "notify::visible", G_CALLBACK(notify_visible), this);
00147 g_signal_connect(G_OBJECT(m_widget), "destroy", G_CALLBACK(destroy), this);
00148 update();
00149 }
00150 };
00151
00152 inline void widget_queue_draw (GtkWidget& widget)
00153 {
00154 gtk_widget_queue_draw(&widget);
00155 }
00156 typedef ReferenceCaller<GtkWidget, widget_queue_draw> WidgetQueueDrawCaller;
00157
00158 inline void widget_make_default (GtkWidget* widget)
00159 {
00160 GTK_WIDGET_SET_FLAGS(widget, GTK_CAN_DEFAULT);
00161 gtk_widget_grab_default(widget);
00162 }
00163
00164 #endif