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 //-----------------------------------------------------------------------------
38 // "focus" from m_window
39 //-----------------------------------------------------------------------------
41 static gint
gtk_dialog_focus_callback( GtkWidget
*widget
, GtkDirectionType
WXUNUSED(d
), wxWindow
*WXUNUSED(win
) )
44 wxapp_install_idle_handler();
46 // This disables GTK's tab traversal
47 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "focus" );
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxPopupWindow
*win
)
58 wxapp_install_idle_handler();
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
70 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxPopupWindow
*win
)
73 wxapp_install_idle_handler();
75 if (!win
->m_hasVMT
) return;
77 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
79 win
->m_width
= alloc
->width
;
80 win
->m_height
= alloc
->height
;
85 //-----------------------------------------------------------------------------
86 // "realize" from m_widget
87 //-----------------------------------------------------------------------------
89 /* we cannot MWM hints and icons before the widget has been realized,
90 so we do this directly after realization */
93 gtk_dialog_realized_callback( GtkWidget
* WXUNUSED(widget
), wxPopupWindow
*win
)
96 wxapp_install_idle_handler();
98 /* all this is for Motif Window Manager "hints" and is supposed to be
99 recognized by other WM as well. not tested. */
100 long decor
= (long) GDK_DECOR_BORDER
;
101 long func
= (long) GDK_FUNC_MOVE
;
103 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
104 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
106 /* GTK's shrinking/growing policy */
107 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) == 0)
108 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
110 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
115 //-----------------------------------------------------------------------------
116 // InsertChild for wxPopupWindow
117 //-----------------------------------------------------------------------------
119 /* Callback for wxFrame. This very strange beast has to be used because
120 * C++ has no virtual methods in a constructor. We have to emulate a
121 * virtual function here as wxWidgets requires different ways to insert
122 * a child in container classes. */
124 static void wxInsertChildInDialog( wxPopupWindow
* parent
, wxWindow
* child
)
126 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
127 GTK_WIDGET(child
->m_widget
),
133 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
135 /* we now allow a window to get the focus as long as it
136 doesn't have any children. */
137 GTK_WIDGET_UNSET_FLAGS( parent
->m_wxwindow
, GTK_CAN_FOCUS
);
141 //-----------------------------------------------------------------------------
143 //-----------------------------------------------------------------------------
145 BEGIN_EVENT_TABLE(wxPopupWindow
,wxPopupWindowBase
)
146 #ifdef __WXUNIVERSAL__
147 EVT_SIZE(wxPopupWindow::OnSize
)
151 wxPopupWindow::~wxPopupWindow()
155 bool wxPopupWindow::Create( wxWindow
*parent
, int style
)
157 m_needParent
= FALSE
;
159 if (!PreCreation( parent
, wxDefaultPosition
, wxDefaultSize
) ||
160 !CreateBase( parent
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("popup") ))
162 wxFAIL_MSG( wxT("wxPopupWindow creation failed") );
166 // All dialogs should really have this style
167 m_windowStyle
|= wxTAB_TRAVERSAL
;
169 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
171 m_widget
= gtk_window_new( GTK_WINDOW_POPUP
);
173 if ((m_parent
) && (GTK_IS_WINDOW(m_parent
->m_widget
)))
174 gtk_window_set_transient_for( GTK_WINDOW(m_widget
), GTK_WINDOW(m_parent
->m_widget
) );
176 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
178 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
179 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
181 m_wxwindow
= gtk_pizza_new();
182 gtk_widget_show( m_wxwindow
);
183 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
185 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
187 if (m_parent
) m_parent
->AddChild( this );
191 /* we cannot set MWM hints before the widget has
192 been realized, so we do this directly after realization */
193 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
194 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
196 /* the user resized the frame by dragging etc. */
197 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
198 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
200 /* disable native tab traversal */
201 gtk_signal_connect( GTK_OBJECT(m_widget
), "focus",
202 GTK_SIGNAL_FUNC(gtk_dialog_focus_callback
), (gpointer
)this );
207 void wxPopupWindow::DoMoveWindow(int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(width
), int WXUNUSED(height
) )
209 wxFAIL_MSG( wxT("DoMoveWindow called for wxPopupWindow") );
212 void wxPopupWindow::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
214 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
215 wxASSERT_MSG( (m_wxwindow
!= NULL
), wxT("invalid dialog") );
217 if (m_resizing
) return; /* I don't like recursions */
223 int old_width
= m_width
;
224 int old_height
= m_height
;
226 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
228 if (x
!= -1) m_x
= x
;
229 if (y
!= -1) m_y
= y
;
230 if (width
!= -1) m_width
= width
;
231 if (height
!= -1) m_height
= height
;
242 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
244 if (width == -1) m_width = 80;
247 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
249 if (height == -1) m_height = 26;
253 int minWidth
= GetMinWidth(),
254 minHeight
= GetMinHeight(),
255 maxWidth
= GetMaxWidth(),
256 maxHeight
= GetMaxHeight();
258 if ((minWidth
!= -1) && (m_width
< minWidth
)) m_width
= minWidth
;
259 if ((minHeight
!= -1) && (m_height
< minHeight
)) m_height
= minHeight
;
260 if ((maxWidth
!= -1) && (m_width
> maxWidth
)) m_width
= maxWidth
;
261 if ((maxHeight
!= -1) && (m_height
> maxHeight
)) m_height
= maxHeight
;
263 if ((m_x
!= -1) || (m_y
!= -1))
265 if ((m_x
!= old_x
) || (m_y
!= old_y
))
267 /* we set the position here and when showing the dialog
268 for the first time in idle time */
269 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
273 if ((m_width
!= old_width
) || (m_height
!= old_height
))
275 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
277 /* actual resizing is deferred to GtkOnSize in idle time and
278 when showing the dialog */
286 void wxPopupWindow::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
288 // due to a bug in gtk, x,y are always 0
292 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
293 if (!m_wxwindow
) return;
298 int minWidth
= GetMinWidth(),
299 minHeight
= GetMinHeight(),
300 maxWidth
= GetMaxWidth(),
301 maxHeight
= GetMaxHeight();
303 if ((minWidth
!= -1) && (m_width
< minWidth
)) m_width
= minWidth
;
304 if ((minHeight
!= -1) && (m_height
< minHeight
)) m_height
= minHeight
;
305 if ((maxWidth
!= -1) && (m_width
> maxWidth
)) m_width
= maxWidth
;
306 if ((maxHeight
!= -1) && (m_height
> maxHeight
)) m_height
= maxHeight
;
309 gint flag
= 0; // GDK_HINT_POS;
310 if ((minWidth
!= -1) || (minHeight
!= -1)) flag
|= GDK_HINT_MIN_SIZE
;
311 if ((maxWidth
!= -1) || (maxHeight
!= -1)) flag
|= GDK_HINT_MAX_SIZE
;
313 geom
.min_width
= minWidth
;
314 geom
.min_height
= minHeight
;
315 geom
.max_width
= maxWidth
;
316 geom
.max_height
= maxHeight
;
317 gtk_window_set_geometry_hints( GTK_WINDOW(m_widget
),
320 (GdkWindowHints
) flag
);
324 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
325 event
.SetEventObject( this );
326 GetEventHandler()->ProcessEvent( event
);
329 void wxPopupWindow::OnInternalIdle()
331 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
332 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
334 wxWindow::OnInternalIdle();
337 bool wxPopupWindow::Show( bool show
)
339 if (show
&& !m_sizeSet
)
341 /* by calling GtkOnSize here, we don't have to call
342 either after showing the frame, which would entail
343 much ugly flicker nor from within the size_allocate
344 handler, because GTK 1.1.X forbids that. */
346 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
349 bool ret
= wxWindow::Show( show
);
354 #endif // wxUSE_POPUPWIN