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