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