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