]> icculus.org git repositories - divverent/netradiant.git/blob - libs/gtkutil/window.h
new toys :P
[divverent/netradiant.git] / libs / gtkutil / window.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_WINDOW_H)
23 #define INCLUDED_GTKUTIL_WINDOW_H
24
25 #include <gtk/gtkwindow.h>
26
27 #include "debugging/debugging.h"
28 #include "generic/callback.h"
29 #include "widget.h"
30
31 inline gboolean window_focus_in_clear_focus_widget(GtkWidget* widget, GdkEventKey* event, gpointer data)
32 {
33   gtk_window_set_focus(GTK_WINDOW(widget), NULL);
34   return FALSE;
35 }
36
37 inline guint window_connect_focus_in_clear_focus_widget(GtkWindow* window)
38 {
39   return g_signal_connect(G_OBJECT(window), "focus_in_event", G_CALLBACK(window_focus_in_clear_focus_widget), NULL);
40 }
41
42
43 unsigned int connect_floating(GtkWindow* main_window, GtkWindow* floating);
44 GtkWindow* create_floating_window(const char* title, GtkWindow* parent);
45 void destroy_floating_window(GtkWindow* window);
46
47 GtkWindow* create_persistent_floating_window(const char* title, GtkWindow* main_window);
48 gboolean persistent_floating_window_delete(GtkWindow* floating, GdkEvent *event, GtkWindow* main_window);
49
50 void window_remove_minmax(GtkWindow* window);
51
52 typedef struct _GtkScrolledWindow GtkScrolledWindow;
53 GtkScrolledWindow* create_scrolled_window(GtkPolicyType hscrollbar_policy, GtkPolicyType vscrollbar_policy, int border = 0);
54
55
56 struct WindowPosition
57 {
58   int x, y, w, h;
59
60   WindowPosition()
61   {
62   }
63   WindowPosition(int _x, int _y, int _w, int _h)
64     : x(_x), y(_y), w(_w), h(_h)
65   {
66   }
67 };
68
69 const WindowPosition c_default_window_pos(50, 25, 400, 300);
70
71 inline void window_get_position(GtkWindow* window, WindowPosition& position)
72 {
73   ASSERT_MESSAGE(window != 0, "error saving window position");
74
75   gtk_window_get_position(window, &position.x, &position.y);
76   gtk_window_get_size(window, &position.w, &position.h);
77 }
78
79 inline void window_set_position(GtkWindow* window, const WindowPosition& position)
80 {
81   gtk_window_set_gravity(window, GDK_GRAVITY_STATIC);
82
83   GdkScreen* screen = gdk_screen_get_default();
84   if(position.x < 0
85     || position.y < 0
86     || position.x > gdk_screen_get_width(screen)
87     || position.y > gdk_screen_get_height(screen))
88   {
89     gtk_window_set_position(window, GTK_WIN_POS_CENTER_ON_PARENT);
90   }
91   else
92   {
93     gtk_window_move(window, position.x, position.y);
94   }
95
96   gtk_window_set_default_size(window, position.w, position.h);
97 }
98
99 inline void WindowPosition_Parse(WindowPosition& position, const char* value)
100 {
101   if(sscanf(value, "%d %d %d %d", &position.x, &position.y, &position.w, &position.h) != 4)
102   {
103     position = WindowPosition(c_default_window_pos); // ensure sane default value for window position
104   }
105 }
106 typedef ReferenceCaller1<WindowPosition, const char*, WindowPosition_Parse> WindowPositionImportStringCaller;
107
108 inline void WindowPosition_Write(const WindowPosition& position, const StringImportCallback& importCallback)
109 {
110   char buffer[64];
111   sprintf(buffer, "%d %d %d %d", position.x, position.y, position.w, position.h);
112   importCallback(buffer);
113 }
114 typedef ConstReferenceCaller1<WindowPosition, const StringImportCallback&, WindowPosition_Write> WindowPositionExportStringCaller;
115
116
117
118 class WindowPositionTracker
119 {
120   WindowPosition m_position;
121
122   static gboolean configure(GtkWidget* widget, GdkEventConfigure *event, WindowPositionTracker* self)
123   {
124     self->m_position = WindowPosition(event->x, event->y, event->width, event->height);
125     return FALSE;
126   }
127
128 public:
129   WindowPositionTracker()
130     : m_position(c_default_window_pos)
131   {
132   }
133
134   void sync(GtkWindow* window)
135   {
136     window_set_position(window, m_position);
137   }
138
139   void connect(GtkWindow* window)
140   {
141     sync(window);
142     g_signal_connect(G_OBJECT(window), "configure_event", G_CALLBACK(configure), this);
143   }
144
145   const WindowPosition& getPosition() const
146   {
147     return m_position;
148   }
149
150   //hack
151   void setPosition(const WindowPosition& position)
152   {
153     m_position = position;
154   }
155 };
156
157
158 inline void WindowPositionTracker_importString(WindowPositionTracker& self, const char* value)
159 {
160   WindowPosition position;
161   WindowPosition_Parse(position, value);
162   self.setPosition(position);
163 }
164 typedef ReferenceCaller1<WindowPositionTracker, const char*, WindowPositionTracker_importString> WindowPositionTrackerImportStringCaller;
165
166 inline void WindowPositionTracker_exportString(const WindowPositionTracker& self, const StringImportCallback& importer)
167 {
168   WindowPosition_Write(self.getPosition(), importer);
169 }
170 typedef ConstReferenceCaller1<WindowPositionTracker, const StringImportCallback&, WindowPositionTracker_exportString> WindowPositionTrackerExportStringCaller;
171
172
173
174 #endif