]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/popupwin.cpp
1bda6c9850bd9c677d1bca4476ec723f5bf006fa
[wxWidgets.git] / src / gtk1 / popupwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: popupwin.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "popupwin.h"
12 #endif
13
14 #include "wx/defs.h"
15
16 #if wxUSE_POPUPWIN
17
18 #include "wx/popupwin.h"
19 #include "wx/frame.h"
20 #include "wx/app.h"
21 #include "wx/cursor.h"
22
23 #include <gdk/gdk.h>
24 #include <gtk/gtk.h>
25 #include <gdk/gdkkeysyms.h>
26
27 #include "wx/gtk/win_gtk.h"
28
29 //-----------------------------------------------------------------------------
30 // idle system
31 //-----------------------------------------------------------------------------
32
33 extern void wxapp_install_idle_handler();
34 extern bool g_isIdle;
35
36 //-----------------------------------------------------------------------------
37 // "focus" from m_window
38 //-----------------------------------------------------------------------------
39
40 static gint gtk_dialog_focus_callback( GtkWidget *widget, GtkDirectionType WXUNUSED(d), wxWindow *WXUNUSED(win) )
41 {
42 if (g_isIdle)
43 wxapp_install_idle_handler();
44
45 // This disables GTK's tab traversal
46 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "focus" );
47 return TRUE;
48 }
49
50 //-----------------------------------------------------------------------------
51 // "delete_event"
52 //-----------------------------------------------------------------------------
53
54 bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxPopupWindow *win )
55 {
56 if (g_isIdle)
57 wxapp_install_idle_handler();
58
59 if (win->IsEnabled())
60 win->Close();
61
62 return TRUE;
63 }
64
65 //-----------------------------------------------------------------------------
66 // "size_allocate"
67 //-----------------------------------------------------------------------------
68
69 static void gtk_dialog_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxPopupWindow *win )
70 {
71 if (g_isIdle)
72 wxapp_install_idle_handler();
73
74 if (!win->m_hasVMT) return;
75
76 if ((win->m_width != alloc->width) || (win->m_height != alloc->height))
77 {
78 win->m_width = alloc->width;
79 win->m_height = alloc->height;
80 win->GtkUpdateSize();
81 }
82 }
83
84 //-----------------------------------------------------------------------------
85 // "realize" from m_widget
86 //-----------------------------------------------------------------------------
87
88 /* we cannot MWM hints and icons before the widget has been realized,
89 so we do this directly after realization */
90
91 static gint
92 gtk_dialog_realized_callback( GtkWidget * WXUNUSED(widget), wxPopupWindow *win )
93 {
94 if (g_isIdle)
95 wxapp_install_idle_handler();
96
97 /* all this is for Motif Window Manager "hints" and is supposed to be
98 recognized by other WM as well. not tested. */
99 long decor = (long) GDK_DECOR_BORDER;
100 long func = (long) GDK_FUNC_MOVE ;
101
102 gdk_window_set_decorations( win->m_widget->window, (GdkWMDecoration)decor);
103 gdk_window_set_functions( win->m_widget->window, (GdkWMFunction)func);
104
105 /* GTK's shrinking/growing policy */
106 if ((win->GetWindowStyle() & wxRESIZE_BORDER) == 0)
107 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 0, 0, 1);
108 else
109 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 1, 1, 1);
110
111 return FALSE;
112 }
113
114 //-----------------------------------------------------------------------------
115 // InsertChild for wxPopupWindow
116 //-----------------------------------------------------------------------------
117
118 /* Callback for wxFrame. This very strange beast has to be used because
119 * C++ has no virtual methods in a constructor. We have to emulate a
120 * virtual function here as wxWindows requires different ways to insert
121 * a child in container classes. */
122
123 static void wxInsertChildInDialog( wxPopupWindow* parent, wxWindow* child )
124 {
125 gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow),
126 GTK_WIDGET(child->m_widget),
127 child->m_x,
128 child->m_y,
129 child->m_width,
130 child->m_height );
131
132 if (parent->HasFlag(wxTAB_TRAVERSAL))
133 {
134 /* we now allow a window to get the focus as long as it
135 doesn't have any children. */
136 GTK_WIDGET_UNSET_FLAGS( parent->m_wxwindow, GTK_CAN_FOCUS );
137 }
138 }
139
140 //-----------------------------------------------------------------------------
141 // wxPopupWindow
142 //-----------------------------------------------------------------------------
143
144 BEGIN_EVENT_TABLE(wxPopupWindow, wxPopupWindowBase)
145 EVT_SIZE(wxPopupWindow::OnSize)
146 END_EVENT_TABLE()
147
148 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow)
149
150 bool wxPopupWindow::Create( wxWindow *parent, int style )
151 {
152 m_needParent = FALSE;
153
154 if (!PreCreation( parent, wxDefaultPosition, wxDefaultSize ) ||
155 !CreateBase( parent, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, "popup" ))
156 {
157 wxFAIL_MSG( wxT("wxPopupWindow creation failed") );
158 return FALSE;
159 }
160
161 // All dialogs should really have this style
162 m_windowStyle |= wxTAB_TRAVERSAL;
163
164 m_insertCallback = (wxInsertChildFunction) wxInsertChildInDialog;
165
166 m_widget = gtk_window_new( GTK_WINDOW_POPUP );
167
168 if ((m_parent) && (GTK_IS_WINDOW(m_parent->m_widget)))
169 gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(m_parent->m_widget) );
170
171 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
172
173 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
174 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
175
176 m_wxwindow = gtk_pizza_new();
177 gtk_widget_show( m_wxwindow );
178 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
179
180 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
181
182 if (m_parent) m_parent->AddChild( this );
183
184 PostCreation();
185
186 /* we cannot set MWM hints before the widget has
187 been realized, so we do this directly after realization */
188 gtk_signal_connect( GTK_OBJECT(m_widget), "realize",
189 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback), (gpointer) this );
190
191 /* the user resized the frame by dragging etc. */
192 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
193 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
194
195 /* disable native tab traversal */
196 gtk_signal_connect( GTK_OBJECT(m_widget), "focus",
197 GTK_SIGNAL_FUNC(gtk_dialog_focus_callback), (gpointer)this );
198
199 return TRUE;
200 }
201
202 void wxPopupWindow::DoMoveWindow(int WXUNUSED(x), int WXUNUSED(y), int WXUNUSED(width), int WXUNUSED(height) )
203 {
204 wxFAIL_MSG( wxT("DoMoveWindow called for wxPopupWindow") );
205 }
206
207 void wxPopupWindow::DoSetSize( int x, int y, int width, int height, int sizeFlags )
208 {
209 wxASSERT_MSG( (m_widget != NULL), wxT("invalid dialog") );
210 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid dialog") );
211
212 if (m_resizing) return; /* I don't like recursions */
213 m_resizing = TRUE;
214
215 int old_x = m_x;
216 int old_y = m_y;
217
218 int old_width = m_width;
219 int old_height = m_height;
220
221 if ((sizeFlags & wxSIZE_ALLOW_MINUS_ONE) == 0)
222 {
223 if (x != -1) m_x = x;
224 if (y != -1) m_y = y;
225 if (width != -1) m_width = width;
226 if (height != -1) m_height = height;
227 }
228 else
229 {
230 m_x = x;
231 m_y = y;
232 m_width = width;
233 m_height = height;
234 }
235
236 /*
237 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
238 {
239 if (width == -1) m_width = 80;
240 }
241
242 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
243 {
244 if (height == -1) m_height = 26;
245 }
246 */
247
248 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
249 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
250 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
251 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
252
253 if ((m_x != -1) || (m_y != -1))
254 {
255 if ((m_x != old_x) || (m_y != old_y))
256 {
257 /* we set the position here and when showing the dialog
258 for the first time in idle time */
259 gtk_widget_set_uposition( m_widget, m_x, m_y );
260 }
261 }
262
263 if ((m_width != old_width) || (m_height != old_height))
264 {
265 gtk_widget_set_usize( m_widget, m_width, m_height );
266
267 /* actual resizing is deferred to GtkOnSize in idle time and
268 when showing the dialog */
269 m_sizeSet = FALSE;
270
271 }
272
273 m_resizing = FALSE;
274 }
275
276 void wxPopupWindow::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
277 {
278 // due to a bug in gtk, x,y are always 0
279 // m_x = x;
280 // m_y = y;
281
282 if ((m_height == height) && (m_width == width) && (m_sizeSet)) return;
283 if (!m_wxwindow) return;
284
285 m_width = width;
286 m_height = height;
287
288 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
289 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
290 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
291 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
292
293 /* set size hints */
294 gint flag = 0; // GDK_HINT_POS;
295 if ((m_minWidth != -1) || (m_minHeight != -1)) flag |= GDK_HINT_MIN_SIZE;
296 if ((m_maxWidth != -1) || (m_maxHeight != -1)) flag |= GDK_HINT_MAX_SIZE;
297 GdkGeometry geom;
298 geom.min_width = m_minWidth;
299 geom.min_height = m_minHeight;
300 geom.max_width = m_maxWidth;
301 geom.max_height = m_maxHeight;
302 gtk_window_set_geometry_hints( GTK_WINDOW(m_widget),
303 (GtkWidget*) NULL,
304 &geom,
305 (GdkWindowHints) flag );
306
307 m_sizeSet = TRUE;
308
309 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
310 event.SetEventObject( this );
311 GetEventHandler()->ProcessEvent( event );
312 }
313
314 void wxPopupWindow::OnInternalIdle()
315 {
316 if (!m_sizeSet && GTK_WIDGET_REALIZED(m_wxwindow))
317 GtkOnSize( m_x, m_y, m_width, m_height );
318
319 wxWindow::OnInternalIdle();
320 }
321
322 bool wxPopupWindow::Show( bool show )
323 {
324 if (show && !m_sizeSet)
325 {
326 /* by calling GtkOnSize here, we don't have to call
327 either after showing the frame, which would entail
328 much ugly flicker nor from within the size_allocate
329 handler, because GTK 1.1.X forbids that. */
330
331 GtkOnSize( m_x, m_y, m_width, m_height );
332 }
333
334 bool ret = wxWindow::Show( show );
335
336 return ret;
337 }
338
339 #endif // wxUSE_POPUPWIN