1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "popupwin.h"
18 #include "wx/popupwin.h"
21 #include "wx/cursor.h"
25 #include <gdk/gdkkeysyms.h>
27 #include "wx/gtk/win_gtk.h"
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern void wxapp_install_idle_handler();
36 //-----------------------------------------------------------------------------
37 // "focus" from m_window
38 //-----------------------------------------------------------------------------
40 static gint
gtk_dialog_focus_callback( GtkWidget
*widget
, GtkDirectionType
WXUNUSED(d
), wxWindow
*WXUNUSED(win
) )
43 wxapp_install_idle_handler();
45 // This disables GTK's tab traversal
46 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "focus" );
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxPopupWindow
*win
)
57 wxapp_install_idle_handler();
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
69 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxPopupWindow
*win
)
72 wxapp_install_idle_handler();
74 if (!win
->m_hasVMT
) return;
76 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
78 win
->m_width
= alloc
->width
;
79 win
->m_height
= alloc
->height
;
84 //-----------------------------------------------------------------------------
85 // "realize" from m_widget
86 //-----------------------------------------------------------------------------
88 /* we cannot MWM hints and icons before the widget has been realized,
89 so we do this directly after realization */
92 gtk_dialog_realized_callback( GtkWidget
* WXUNUSED(widget
), wxPopupWindow
*win
)
95 wxapp_install_idle_handler();
97 /* all this is for Motif Window Manager "hints" and is supposed to be
98 recognized by other WM as well. not tested. */
99 long decor
= (long) GDK_DECOR_BORDER
;
100 long func
= (long) GDK_FUNC_MOVE
;
102 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
103 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
105 /* GTK's shrinking/growing policy */
106 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) == 0)
107 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
109 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
114 //-----------------------------------------------------------------------------
115 // InsertChild for wxPopupWindow
116 //-----------------------------------------------------------------------------
118 /* Callback for wxFrame. This very strange beast has to be used because
119 * C++ has no virtual methods in a constructor. We have to emulate a
120 * virtual function here as wxWindows requires different ways to insert
121 * a child in container classes. */
123 static void wxInsertChildInDialog( wxPopupWindow
* parent
, wxWindow
* child
)
125 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
126 GTK_WIDGET(child
->m_widget
),
132 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
134 /* we now allow a window to get the focus as long as it
135 doesn't have any children. */
136 GTK_WIDGET_UNSET_FLAGS( parent
->m_wxwindow
, GTK_CAN_FOCUS
);
140 //-----------------------------------------------------------------------------
142 //-----------------------------------------------------------------------------
144 BEGIN_EVENT_TABLE(wxPopupWindow
,wxPopupWindowBase
)
145 #ifdef __WXUNIVERSAL__
146 EVT_SIZE(wxPopupWindow::OnSize
)
150 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow
, wxWindow
)
152 wxPopupWindow::~wxPopupWindow()
156 bool wxPopupWindow::Create( wxWindow
*parent
, int style
)
158 m_needParent
= FALSE
;
160 if (!PreCreation( parent
, wxDefaultPosition
, wxDefaultSize
) ||
161 !CreateBase( parent
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("popup") ))
163 wxFAIL_MSG( wxT("wxPopupWindow creation failed") );
167 // All dialogs should really have this style
168 m_windowStyle
|= wxTAB_TRAVERSAL
;
170 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
172 m_widget
= gtk_window_new( GTK_WINDOW_POPUP
);
174 if ((m_parent
) && (GTK_IS_WINDOW(m_parent
->m_widget
)))
175 gtk_window_set_transient_for( GTK_WINDOW(m_widget
), GTK_WINDOW(m_parent
->m_widget
) );
177 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
179 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
180 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
182 m_wxwindow
= gtk_pizza_new();
183 gtk_widget_show( m_wxwindow
);
184 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
186 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
188 if (m_parent
) m_parent
->AddChild( this );
192 /* we cannot set MWM hints before the widget has
193 been realized, so we do this directly after realization */
194 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
195 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
197 /* the user resized the frame by dragging etc. */
198 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
199 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
201 /* disable native tab traversal */
202 gtk_signal_connect( GTK_OBJECT(m_widget
), "focus",
203 GTK_SIGNAL_FUNC(gtk_dialog_focus_callback
), (gpointer
)this );
208 void wxPopupWindow::DoMoveWindow(int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(width
), int WXUNUSED(height
) )
210 wxFAIL_MSG( wxT("DoMoveWindow called for wxPopupWindow") );
213 void wxPopupWindow::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
215 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
216 wxASSERT_MSG( (m_wxwindow
!= NULL
), wxT("invalid dialog") );
218 if (m_resizing
) return; /* I don't like recursions */
224 int old_width
= m_width
;
225 int old_height
= m_height
;
227 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
229 if (x
!= -1) m_x
= x
;
230 if (y
!= -1) m_y
= y
;
231 if (width
!= -1) m_width
= width
;
232 if (height
!= -1) m_height
= height
;
243 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
245 if (width == -1) m_width = 80;
248 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
250 if (height == -1) m_height = 26;
254 int minWidth
= GetMinWidth(),
255 minHeight
= GetMinHeight(),
256 maxWidth
= GetMaxWidth(),
257 maxHeight
= GetMaxHeight();
259 if ((minWidth
!= -1) && (m_width
< minWidth
)) m_width
= minWidth
;
260 if ((minHeight
!= -1) && (m_height
< minHeight
)) m_height
= minHeight
;
261 if ((maxWidth
!= -1) && (m_width
> maxWidth
)) m_width
= maxWidth
;
262 if ((maxHeight
!= -1) && (m_height
> maxHeight
)) m_height
= maxHeight
;
264 if ((m_x
!= -1) || (m_y
!= -1))
266 if ((m_x
!= old_x
) || (m_y
!= old_y
))
268 /* we set the position here and when showing the dialog
269 for the first time in idle time */
270 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
274 if ((m_width
!= old_width
) || (m_height
!= old_height
))
276 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
278 /* actual resizing is deferred to GtkOnSize in idle time and
279 when showing the dialog */
287 void wxPopupWindow::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
289 // due to a bug in gtk, x,y are always 0
293 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
294 if (!m_wxwindow
) return;
299 int minWidth
= GetMinWidth(),
300 minHeight
= GetMinHeight(),
301 maxWidth
= GetMaxWidth(),
302 maxHeight
= GetMaxHeight();
304 if ((minWidth
!= -1) && (m_width
< minWidth
)) m_width
= minWidth
;
305 if ((minHeight
!= -1) && (m_height
< minHeight
)) m_height
= minHeight
;
306 if ((maxWidth
!= -1) && (m_width
> maxWidth
)) m_width
= maxWidth
;
307 if ((maxHeight
!= -1) && (m_height
> maxHeight
)) m_height
= maxHeight
;
310 gint flag
= 0; // GDK_HINT_POS;
311 if ((minWidth
!= -1) || (minHeight
!= -1)) flag
|= GDK_HINT_MIN_SIZE
;
312 if ((maxWidth
!= -1) || (maxHeight
!= -1)) flag
|= GDK_HINT_MAX_SIZE
;
314 geom
.min_width
= minWidth
;
315 geom
.min_height
= minHeight
;
316 geom
.max_width
= maxWidth
;
317 geom
.max_height
= maxHeight
;
318 gtk_window_set_geometry_hints( GTK_WINDOW(m_widget
),
321 (GdkWindowHints
) flag
);
325 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
326 event
.SetEventObject( this );
327 GetEventHandler()->ProcessEvent( event
);
330 void wxPopupWindow::OnInternalIdle()
332 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
333 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
335 wxWindow::OnInternalIdle();
338 bool wxPopupWindow::Show( bool show
)
340 if (show
&& !m_sizeSet
)
342 /* by calling GtkOnSize here, we don't have to call
343 either after showing the frame, which would entail
344 much ugly flicker nor from within the size_allocate
345 handler, because GTK 1.1.X forbids that. */
347 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
350 bool ret
= wxWindow::Show( show
);
355 #endif // wxUSE_POPUPWIN