1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "popupwin.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
19 #include "wx/popupwin.h"
22 #include "wx/cursor.h"
26 #include <gdk/gdkkeysyms.h>
28 #include "wx/gtk/win_gtk.h"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern void wxapp_install_idle_handler();
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
42 static gint
gtk_popup_button_press (GtkWidget
*widget
, GdkEvent
*gdk_event
, wxPopupWindow
* win
)
44 GtkWidget
*child
= gtk_get_event_widget (gdk_event
);
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
59 child
= child
->parent
;
63 wxFocusEvent
event( wxEVT_KILL_FOCUS
, win
->GetId() );
64 event
.SetEventObject( win
);
66 (void)win
->GetEventHandler()->ProcessEvent( event
);
72 //-----------------------------------------------------------------------------
73 // "focus" from m_window
74 //-----------------------------------------------------------------------------
77 static gint
gtk_dialog_focus_callback( GtkWidget
*widget
, GtkDirectionType
WXUNUSED(d
), wxWindow
*WXUNUSED(win
) )
80 wxapp_install_idle_handler();
82 // This disables GTK's tab traversal
83 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "focus" );
88 //-----------------------------------------------------------------------------
90 //-----------------------------------------------------------------------------
93 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxPopupWindow
*win
)
96 wxapp_install_idle_handler();
105 //-----------------------------------------------------------------------------
107 //-----------------------------------------------------------------------------
110 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxPopupWindow
*win
)
113 wxapp_install_idle_handler();
115 if (!win
->m_hasVMT
) return;
117 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
119 win
->m_width
= alloc
->width
;
120 win
->m_height
= alloc
->height
;
121 win
->GtkUpdateSize();
126 //-----------------------------------------------------------------------------
127 // "realize" from m_widget
128 //-----------------------------------------------------------------------------
130 /* we cannot MWM hints and icons before the widget has been realized,
131 so we do this directly after realization */
135 gtk_dialog_realized_callback( GtkWidget
* WXUNUSED(widget
), wxPopupWindow
*win
)
138 wxapp_install_idle_handler();
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
;
145 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
146 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
148 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
154 //-----------------------------------------------------------------------------
155 // InsertChild for wxPopupWindow
156 //-----------------------------------------------------------------------------
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. */
163 static void wxInsertChildInDialog( wxPopupWindow
* parent
, wxWindow
* child
)
165 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
166 GTK_WIDGET(child
->m_widget
),
172 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
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
);
180 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
184 BEGIN_EVENT_TABLE(wxPopupWindow
,wxPopupWindowBase
)
185 #ifdef __WXUNIVERSAL__
186 EVT_SIZE(wxPopupWindow::OnSize
)
190 wxPopupWindow::~wxPopupWindow()
194 bool wxPopupWindow::Create( wxWindow
*parent
, int style
)
196 m_needParent
= FALSE
;
198 if (!PreCreation( parent
, wxDefaultPosition
, wxDefaultSize
) ||
199 !CreateBase( parent
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("popup") ))
201 wxFAIL_MSG( wxT("wxPopupWindow creation failed") );
205 // All dialogs should really have this style
206 m_windowStyle
|= wxTAB_TRAVERSAL
;
208 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
210 m_widget
= gtk_window_new( GTK_WINDOW_POPUP
);
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
) );
215 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
217 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
218 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
220 m_wxwindow
= gtk_pizza_new();
221 gtk_widget_show( m_wxwindow
);
222 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
224 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
226 if (m_parent
) m_parent
->AddChild( this );
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 );
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 );
239 // disable native tab traversal
240 gtk_signal_connect( GTK_OBJECT(m_widget
), "focus",
241 GTK_SIGNAL_FUNC(gtk_dialog_focus_callback
), (gpointer
)this );
243 gtk_signal_connect (GTK_OBJECT(m_widget
), "button_press_event",
244 GTK_SIGNAL_FUNC(gtk_popup_button_press
), (gpointer
)this );
249 void wxPopupWindow::DoMoveWindow(int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(width
), int WXUNUSED(height
) )
251 wxFAIL_MSG( wxT("DoMoveWindow called for wxPopupWindow") );
254 void wxPopupWindow::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
256 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
257 wxASSERT_MSG( (m_wxwindow
!= NULL
), wxT("invalid dialog") );
259 if (m_resizing
) return; /* I don't like recursions */
265 int old_width
= m_width
;
266 int old_height
= m_height
;
268 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
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
;
284 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
286 if (width == -1) m_width = 80;
289 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
291 if (height == -1) m_height = 26;
295 int minWidth
= GetMinWidth(),
296 minHeight
= GetMinHeight(),
297 maxWidth
= GetMaxWidth(),
298 maxHeight
= GetMaxHeight();
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
;
305 if ((m_x
!= -1) || (m_y
!= -1))
307 if ((m_x
!= old_x
) || (m_y
!= old_y
))
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
);
315 if ((m_width
!= old_width
) || (m_height
!= old_height
))
317 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
319 /* actual resizing is deferred to GtkOnSize in idle time and
320 when showing the dialog */
328 void wxPopupWindow::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
330 // due to a bug in gtk, x,y are always 0
334 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
335 if (!m_wxwindow
) return;
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
344 int minWidth
= GetMinWidth(),
345 minHeight
= GetMinHeight();
347 if ((minWidth
!= -1) && (m_width
< minWidth
)) m_width
= minWidth
;
348 if ((minHeight
!= -1) && (m_height
< minHeight
)) m_height
= minHeight
;
351 gint flag
= GDK_HINT_MAX_SIZE
; // GDK_HINT_POS;
352 if ((minWidth
!= -1) || (minHeight
!= -1)) flag
|= GDK_HINT_MIN_SIZE
;
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
),
361 (GdkWindowHints
) flag
);
366 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
367 event
.SetEventObject( this );
368 GetEventHandler()->ProcessEvent( event
);
371 void wxPopupWindow::OnInternalIdle()
373 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
374 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
376 wxWindow::OnInternalIdle();
379 bool wxPopupWindow::Show( bool show
)
381 if (show
&& !m_sizeSet
)
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. */
388 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
391 bool ret
= wxWindow::Show( show
);
396 #endif // wxUSE_POPUPWIN