]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/widget.h
Merge commit 'bf6dd1f2d186c799adf11f1e744a1ff57aa8d335' into garux-merge
[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 <uilib/uilib.h>
27 #include <gdk/gdk.h>
28 #include "generic/callback.h"
29 #include "warnings.h"
30 #include "debugging/debugging.h"
31 #include "property.h"
32
33 #define GARUX_GTK_WORKAROUND
34 #ifndef GARUX_GTK_WORKAROUND
35 inline bool widget_is_visible( GtkWidget* widget ){
36         return GTK_WIDGET_VISIBLE( widget ) != FALSE;
37 }
38
39 inline void widget_set_visible( GtkWidget* widget, bool show ){
40         if ( show ) {
41                 /* workaround for gtk 2.24 issue: not displayed glwidget after toggle */
42                 GtkWidget* glwidget = GTK_WIDGET( g_object_get_data( G_OBJECT( widget ), "glwidget" ) );
43                 if ( glwidget ){
44                         //if ( widget_is_visible( glwidget ) )
45                                 //globalOutputStream() << "glwidget have been already visible :0\n"; /* is not hidden aswell, according to this */
46                         gtk_widget_hide( glwidget );
47                         gtk_widget_show( glwidget );
48                 }
49                 gtk_widget_show( widget );
50         }
51         else
52         {
53                 gtk_widget_hide( widget );
54         }
55 }
56 #endif
57
58 class ToggleItem {
59     Callback<void(const Callback<void(bool)> &)> m_exportCallback;
60     typedef std::list<Callback<void(bool)>> ImportCallbacks;
61     ImportCallbacks m_importCallbacks;
62 public:
63     ToggleItem(const Callback<void(const Callback<void(bool)> &)> &exportCallback) : m_exportCallback(exportCallback)
64     {
65     }
66
67     void update()
68     {
69         for (ImportCallbacks::iterator i = m_importCallbacks.begin(); i != m_importCallbacks.end(); ++i) {
70             m_exportCallback(*i);
71         }
72     }
73
74     void addCallback(const Callback<void(bool)> &callback)
75     {
76         m_importCallbacks.push_back(callback);
77         m_exportCallback(callback);
78     }
79
80     typedef MemberCaller<ToggleItem, void(const Callback<void(bool)> &), &ToggleItem::addCallback> AddCallbackCaller;
81 };
82
83 class ToggleShown {
84     bool m_shownDeferred;
85
86     ToggleShown(const ToggleShown &other); // NOT COPYABLE
87     ToggleShown &operator=(const ToggleShown &other); // NOT ASSIGNABLE
88
89     static gboolean notify_visible(ui::Widget widget, gpointer dummy, ToggleShown *self);
90
91     static gboolean destroy(ui::Widget widget, ToggleShown *self);
92
93 public:
94     ui::Widget m_widget;
95     ToggleItem m_item;
96
97     ToggleShown(bool shown)
98             : m_shownDeferred(shown), m_widget(ui::null), m_item(ActiveCaller(*this))
99     {
100     }
101
102     void update();
103
104     bool active() const;
105
106     void exportActive(const Callback<void(bool)> &importCallback);
107
108     typedef MemberCaller<ToggleShown, void(const Callback<void(bool)> &), &ToggleShown::exportActive> ActiveCaller;
109
110     void set(bool shown);
111
112     void toggle();
113
114     typedef MemberCaller<ToggleShown, void(), &ToggleShown::toggle> ToggleCaller;
115
116     void connect(ui::Widget widget);
117 };
118
119
120 void widget_queue_draw(ui::Widget &widget);
121
122 typedef ReferenceCaller<ui::Widget, void(), widget_queue_draw> WidgetQueueDrawCaller;
123
124
125 void widget_make_default(ui::Widget widget);
126
127 class WidgetFocusPrinter {
128     const char *m_name;
129
130     static gboolean focus_in(ui::Widget widget, GdkEventFocus *event, WidgetFocusPrinter *self);
131
132     static gboolean focus_out(ui::Widget widget, GdkEventFocus *event, WidgetFocusPrinter *self);
133
134 public:
135     WidgetFocusPrinter(const char *name) : m_name(name)
136     {
137     }
138
139     void connect(ui::Widget widget);
140 };
141
142 #endif