1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/popupwin.h"
18 #include "wx/cursor.h"
22 #include <gdk/gdkkeysyms.h>
24 #include "wx/gtk/win_gtk.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern void wxapp_install_idle_handler();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
38 static gint
gtk_popup_button_press (GtkWidget
*widget
, GdkEvent
*gdk_event
, wxPopupWindow
* win
)
40 GtkWidget
*child
= gtk_get_event_widget (gdk_event
);
42 /* We don't ask for button press events on the grab widget, so
43 * if an event is reported directly to the grab widget, it must
44 * be on a window outside the application (and thus we remove
45 * the popup window). Otherwise, we check if the widget is a child
46 * of the grab widget, and only remove the popup window if it
55 child
= child
->parent
;
59 wxFocusEvent
event( wxEVT_KILL_FOCUS
, win
->GetId() );
60 event
.SetEventObject( win
);
62 (void)win
->GetEventHandler()->ProcessEvent( event
);
68 //-----------------------------------------------------------------------------
69 // "focus" from m_window
70 //-----------------------------------------------------------------------------
73 static gint
gtk_dialog_focus_callback( GtkWidget
*widget
, GtkDirectionType
WXUNUSED(d
), wxWindow
*WXUNUSED(win
) )
76 wxapp_install_idle_handler();
78 // This disables GTK's tab traversal
79 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "focus" );
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
89 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxPopupWindow
*win
)
92 wxapp_install_idle_handler();
101 //-----------------------------------------------------------------------------
102 // "realize" from m_widget
103 //-----------------------------------------------------------------------------
105 /* we cannot MWM hints and icons before the widget has been realized,
106 so we do this directly after realization */
110 gtk_dialog_realized_callback( GtkWidget
* WXUNUSED(widget
), wxPopupWindow
*win
)
113 wxapp_install_idle_handler();
115 /* all this is for Motif Window Manager "hints" and is supposed to be
116 recognized by other WM as well. not tested. */
117 long decor
= (long) GDK_DECOR_BORDER
;
118 long func
= (long) GDK_FUNC_MOVE
;
120 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
121 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
123 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
129 //-----------------------------------------------------------------------------
130 // InsertChild for wxPopupWindow
131 //-----------------------------------------------------------------------------
133 /* Callback for wxFrame. This very strange beast has to be used because
134 * C++ has no virtual methods in a constructor. We have to emulate a
135 * virtual function here as wxWidgets requires different ways to insert
136 * a child in container classes. */
138 static void wxInsertChildInDialog( wxPopupWindow
* parent
, wxWindow
* child
)
140 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
141 GTK_WIDGET(child
->m_widget
),
147 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
149 /* we now allow a window to get the focus as long as it
150 doesn't have any children. */
151 GTK_WIDGET_UNSET_FLAGS( parent
->m_wxwindow
, GTK_CAN_FOCUS
);
155 //-----------------------------------------------------------------------------
157 //-----------------------------------------------------------------------------
159 BEGIN_EVENT_TABLE(wxPopupWindow
,wxPopupWindowBase
)
160 #ifdef __WXUNIVERSAL__
161 EVT_SIZE(wxPopupWindow::OnSize
)
165 wxPopupWindow::~wxPopupWindow()
169 bool wxPopupWindow::Create( wxWindow
*parent
, int style
)
171 m_needParent
= FALSE
;
173 if (!PreCreation( parent
, wxDefaultPosition
, wxDefaultSize
) ||
174 !CreateBase( parent
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("popup") ))
176 wxFAIL_MSG( wxT("wxPopupWindow creation failed") );
180 // Unlike windows, top level windows are created hidden by default.
183 // All dialogs should really have this style
184 m_windowStyle
|= wxTAB_TRAVERSAL
;
186 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
188 m_widget
= gtk_window_new( GTK_WINDOW_POPUP
);
190 if ((m_parent
) && (GTK_IS_WINDOW(m_parent
->m_widget
)))
191 gtk_window_set_transient_for( GTK_WINDOW(m_widget
), GTK_WINDOW(m_parent
->m_widget
) );
193 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
195 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
196 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
198 m_wxwindow
= gtk_pizza_new();
199 gtk_widget_show( m_wxwindow
);
200 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
202 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
204 if (m_parent
) m_parent
->AddChild( this );
208 /* we cannot set MWM hints before the widget has
209 been realized, so we do this directly after realization */
210 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
211 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
213 // disable native tab traversal
214 gtk_signal_connect( GTK_OBJECT(m_widget
), "focus",
215 GTK_SIGNAL_FUNC(gtk_dialog_focus_callback
), (gpointer
)this );
217 gtk_signal_connect (GTK_OBJECT(m_widget
), "button_press_event",
218 GTK_SIGNAL_FUNC(gtk_popup_button_press
), (gpointer
)this );
223 void wxPopupWindow::DoMoveWindow(int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(width
), int WXUNUSED(height
) )
225 wxFAIL_MSG( wxT("DoMoveWindow called for wxPopupWindow") );
228 void wxPopupWindow::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
230 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
231 wxASSERT_MSG( (m_wxwindow
!= NULL
), wxT("invalid dialog") );
233 if (m_resizing
) return; /* I don't like recursions */
239 int old_width
= m_width
;
240 int old_height
= m_height
;
242 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
244 if (x
!= -1) m_x
= x
;
245 if (y
!= -1) m_y
= y
;
246 if (width
!= -1) m_width
= width
;
247 if (height
!= -1) m_height
= height
;
258 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
260 if (width == -1) m_width = 80;
263 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
265 if (height == -1) m_height = 26;
269 int minWidth
= GetMinWidth(),
270 minHeight
= GetMinHeight(),
271 maxWidth
= GetMaxWidth(),
272 maxHeight
= GetMaxHeight();
274 if ((minWidth
!= -1) && (m_width
< minWidth
)) m_width
= minWidth
;
275 if ((minHeight
!= -1) && (m_height
< minHeight
)) m_height
= minHeight
;
276 if ((maxWidth
!= -1) && (m_width
> maxWidth
)) m_width
= maxWidth
;
277 if ((maxHeight
!= -1) && (m_height
> maxHeight
)) m_height
= maxHeight
;
279 if ((m_x
!= -1) || (m_y
!= -1))
281 if ((m_x
!= old_x
) || (m_y
!= old_y
))
283 /* we set the position here and when showing the dialog
284 for the first time in idle time */
285 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
289 if ((m_width
!= old_width
) || (m_height
!= old_height
))
291 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
293 /* actual resizing is deferred to GtkOnSize in idle time and
294 when showing the dialog */
302 void wxPopupWindow::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
304 // due to a bug in gtk, x,y are always 0
308 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
309 if (!m_wxwindow
) return;
314 /* FIXME: is this a hack? */
315 /* Since for some reason GTK will revert to using maximum size ever set
316 for this window, we have to set geometry hints maxsize to match size
317 given. Also set the to that minsize since resizing isn't possible
321 gint flag
= GDK_HINT_MAX_SIZE
| GDK_HINT_MIN_SIZE
; // GDK_HINT_POS;
323 geom
.min_width
= m_width
;
324 geom
.min_height
= m_height
;
325 geom
.max_width
= m_width
;
326 geom
.max_height
= m_height
;
327 gtk_window_set_geometry_hints( GTK_WINDOW(m_widget
),
330 (GdkWindowHints
) flag
);
335 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
336 event
.SetEventObject( this );
337 GetEventHandler()->ProcessEvent( event
);
340 void wxPopupWindow::OnInternalIdle()
342 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
343 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
345 wxWindow::OnInternalIdle();
348 bool wxPopupWindow::Show( bool show
)
350 if (show
&& !m_sizeSet
)
352 /* by calling GtkOnSize here, we don't have to call
353 either after showing the frame, which would entail
354 much ugly flicker nor from within the size_allocate
355 handler, because GTK 1.1.X forbids that. */
357 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
360 bool ret
= wxWindow::Show( show
);
365 #endif // wxUSE_POPUPWIN