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 //-----------------------------------------------------------------------------
41 static gint
gtk_popup_button_press (GtkWidget
*widget
, GdkEvent
*gdk_event
, wxPopupWindow
* win
)
43 GtkWidget
*child
= gtk_get_event_widget (gdk_event
);
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
58 child
= child
->parent
;
62 wxFocusEvent
event( wxEVT_KILL_FOCUS
, win
->GetId() );
63 event
.SetEventObject( win
);
65 (void)win
->GetEventHandler()->ProcessEvent( event
);
70 //-----------------------------------------------------------------------------
71 // "focus" from m_window
72 //-----------------------------------------------------------------------------
74 static gint
gtk_dialog_focus_callback( GtkWidget
*widget
, GtkDirectionType
WXUNUSED(d
), wxWindow
*WXUNUSED(win
) )
77 wxapp_install_idle_handler();
79 // This disables GTK's tab traversal
80 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "focus" );
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
88 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxPopupWindow
*win
)
91 wxapp_install_idle_handler();
99 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
103 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxPopupWindow
*win
)
106 wxapp_install_idle_handler();
108 if (!win
->m_hasVMT
) return;
110 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
112 win
->m_width
= alloc
->width
;
113 win
->m_height
= alloc
->height
;
114 win
->GtkUpdateSize();
118 //-----------------------------------------------------------------------------
119 // "realize" from m_widget
120 //-----------------------------------------------------------------------------
122 /* we cannot MWM hints and icons before the widget has been realized,
123 so we do this directly after realization */
126 gtk_dialog_realized_callback( GtkWidget
* WXUNUSED(widget
), wxPopupWindow
*win
)
129 wxapp_install_idle_handler();
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
;
136 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
137 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
139 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
144 //-----------------------------------------------------------------------------
145 // InsertChild for wxPopupWindow
146 //-----------------------------------------------------------------------------
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
150 * virtual function here as wxWidgets requires different ways to insert
151 * a child in container classes. */
153 static void wxInsertChildInDialog( wxPopupWindow
* parent
, wxWindow
* child
)
155 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
156 GTK_WIDGET(child
->m_widget
),
162 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
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
);
170 //-----------------------------------------------------------------------------
172 //-----------------------------------------------------------------------------
174 BEGIN_EVENT_TABLE(wxPopupWindow
,wxPopupWindowBase
)
175 #ifdef __WXUNIVERSAL__
176 EVT_SIZE(wxPopupWindow::OnSize
)
180 wxPopupWindow::~wxPopupWindow()
184 bool wxPopupWindow::Create( wxWindow
*parent
, int style
)
186 m_needParent
= FALSE
;
188 if (!PreCreation( parent
, wxDefaultPosition
, wxDefaultSize
) ||
189 !CreateBase( parent
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("popup") ))
191 wxFAIL_MSG( wxT("wxPopupWindow creation failed") );
195 // All dialogs should really have this style
196 m_windowStyle
|= wxTAB_TRAVERSAL
;
198 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
200 m_widget
= gtk_window_new( GTK_WINDOW_POPUP
);
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
) );
205 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
207 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
208 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
210 m_wxwindow
= gtk_pizza_new();
211 gtk_widget_show( m_wxwindow
);
212 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
214 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
216 if (m_parent
) m_parent
->AddChild( this );
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 );
225 // the user resized the frame by dragging etc.
226 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
227 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
229 // disable native tab traversal
230 gtk_signal_connect( GTK_OBJECT(m_widget
), "focus",
231 GTK_SIGNAL_FUNC(gtk_dialog_focus_callback
), (gpointer
)this );
233 gtk_signal_connect (GTK_OBJECT(m_widget
), "button_press_event",
234 GTK_SIGNAL_FUNC(gtk_popup_button_press
), (gpointer
)this );
239 void wxPopupWindow::DoMoveWindow(int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(width
), int WXUNUSED(height
) )
241 wxFAIL_MSG( wxT("DoMoveWindow called for wxPopupWindow") );
244 void wxPopupWindow::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
246 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
247 wxASSERT_MSG( (m_wxwindow
!= NULL
), wxT("invalid dialog") );
249 if (m_resizing
) return; /* I don't like recursions */
255 int old_width
= m_width
;
256 int old_height
= m_height
;
258 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
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
;
274 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
276 if (width == -1) m_width = 80;
279 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
281 if (height == -1) m_height = 26;
285 int minWidth
= GetMinWidth(),
286 minHeight
= GetMinHeight(),
287 maxWidth
= GetMaxWidth(),
288 maxHeight
= GetMaxHeight();
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
;
295 if ((m_x
!= -1) || (m_y
!= -1))
297 if ((m_x
!= old_x
) || (m_y
!= old_y
))
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
);
305 if ((m_width
!= old_width
) || (m_height
!= old_height
))
307 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
309 /* actual resizing is deferred to GtkOnSize in idle time and
310 when showing the dialog */
318 void wxPopupWindow::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
320 // due to a bug in gtk, x,y are always 0
324 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
325 if (!m_wxwindow
) return;
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
334 int minWidth
= GetMinWidth(),
335 minHeight
= GetMinHeight();
337 if ((minWidth
!= -1) && (m_width
< minWidth
)) m_width
= minWidth
;
338 if ((minHeight
!= -1) && (m_height
< minHeight
)) m_height
= minHeight
;
341 gint flag
= GDK_HINT_MAX_SIZE
; // GDK_HINT_POS;
342 if ((minWidth
!= -1) || (minHeight
!= -1)) flag
|= GDK_HINT_MIN_SIZE
;
344 geom
.min_width
= minWidth
;
345 geom
.min_height
= minHeight
;
346 geom
.max_width
= m_width
;
347 geom
.max_height
= m_height
;
348 gtk_window_set_geometry_hints( GTK_WINDOW(m_widget
),
351 (GdkWindowHints
) flag
);
356 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
357 event
.SetEventObject( this );
358 GetEventHandler()->ProcessEvent( event
);
361 void wxPopupWindow::OnInternalIdle()
363 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
364 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
366 wxWindow::OnInternalIdle();
369 bool wxPopupWindow::Show( bool show
)
371 if (show
&& !m_sizeSet
)
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. */
378 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
381 bool ret
= wxWindow::Show( show
);
386 #endif // wxUSE_POPUPWIN