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