1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "popupwin.h"
14 #include "wx/popupwin.h"
17 #include "wx/cursor.h"
21 #include <gdk/gdkkeysyms.h>
23 #include "wx/gtk/win_gtk.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
32 //-----------------------------------------------------------------------------
33 // "focus" from m_window
34 //-----------------------------------------------------------------------------
36 static gint
gtk_dialog_focus_callback( GtkWidget
*widget
, GtkDirectionType
WXUNUSED(d
), wxWindow
*WXUNUSED(win
) )
39 wxapp_install_idle_handler();
41 // This disables GTK's tab traversal
42 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "focus" );
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxPopupWindow
*win
)
53 wxapp_install_idle_handler();
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxPopupWindow
*win
)
68 wxapp_install_idle_handler();
70 if (!win
->m_hasVMT
) return;
72 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
74 win
->m_width
= alloc
->width
;
75 win
->m_height
= alloc
->height
;
80 //-----------------------------------------------------------------------------
81 // "realize" from m_widget
82 //-----------------------------------------------------------------------------
84 /* we cannot MWM hints and icons before the widget has been realized,
85 so we do this directly after realization */
88 gtk_dialog_realized_callback( GtkWidget
* WXUNUSED(widget
), wxPopupWindow
*win
)
91 wxapp_install_idle_handler();
93 /* all this is for Motif Window Manager "hints" and is supposed to be
94 recognized by other WM as well. not tested. */
95 long decor
= (long) GDK_DECOR_BORDER
;
96 long func
= (long) GDK_FUNC_MOVE
;
98 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
99 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
101 /* GTK's shrinking/growing policy */
102 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) == 0)
103 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
105 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
110 //-----------------------------------------------------------------------------
111 // InsertChild for wxPopupWindow
112 //-----------------------------------------------------------------------------
114 /* Callback for wxFrame. This very strange beast has to be used because
115 * C++ has no virtual methods in a constructor. We have to emulate a
116 * virtual function here as wxWindows requires different ways to insert
117 * a child in container classes. */
119 static void wxInsertChildInDialog( wxPopupWindow
* parent
, wxWindow
* child
)
121 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
122 GTK_WIDGET(child
->m_widget
),
128 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
130 /* we now allow a window to get the focus as long as it
131 doesn't have any children. */
132 GTK_WIDGET_UNSET_FLAGS( parent
->m_wxwindow
, GTK_CAN_FOCUS
);
136 //-----------------------------------------------------------------------------
138 //-----------------------------------------------------------------------------
140 BEGIN_EVENT_TABLE(wxPopupWindow
,wxPopupWindowBase
)
141 EVT_SIZE (wxPopupWindow::OnSize
)
144 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow
,wxPopupWindowBase
)
146 bool wxPopupWindow::Create( wxWindow
*parent
, int style
)
148 m_needParent
= FALSE
;
150 if (!PreCreation( parent
, wxDefaultPosition
, wxDefaultSize
) ||
151 !CreateBase( parent
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, "popup" ))
153 wxFAIL_MSG( wxT("wxPopupWindow creation failed") );
157 // All dialogs should really have this style
158 m_windowStyle
|= wxTAB_TRAVERSAL
;
160 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
162 m_widget
= gtk_window_new( GTK_WINDOW_POPUP
);
164 if ((m_parent
) && (GTK_IS_WINDOW(m_parent
->m_widget
)))
165 gtk_window_set_transient_for( GTK_WINDOW(m_widget
), GTK_WINDOW(m_parent
->m_widget
) );
167 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
169 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
170 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
172 m_wxwindow
= gtk_pizza_new();
173 gtk_widget_show( m_wxwindow
);
174 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
176 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
178 if (m_parent
) m_parent
->AddChild( this );
182 /* we cannot set MWM hints before the widget has
183 been realized, so we do this directly after realization */
184 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
185 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
187 /* the user resized the frame by dragging etc. */
188 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
189 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
191 /* disable native tab traversal */
192 gtk_signal_connect( GTK_OBJECT(m_widget
), "focus",
193 GTK_SIGNAL_FUNC(gtk_dialog_focus_callback
), (gpointer
)this );
198 void wxPopupWindow::DoMoveWindow(int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(width
), int WXUNUSED(height
) )
200 wxFAIL_MSG( wxT("DoMoveWindow called for wxPopupWindow") );
203 void wxPopupWindow::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
205 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
206 wxASSERT_MSG( (m_wxwindow
!= NULL
), wxT("invalid dialog") );
208 if (m_resizing
) return; /* I don't like recursions */
214 int old_width
= m_width
;
215 int old_height
= m_height
;
217 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
219 if (x
!= -1) m_x
= x
;
220 if (y
!= -1) m_y
= y
;
221 if (width
!= -1) m_width
= width
;
222 if (height
!= -1) m_height
= height
;
233 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
235 if (width == -1) m_width = 80;
238 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
240 if (height == -1) m_height = 26;
244 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
245 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
246 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
247 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
249 if ((m_x
!= -1) || (m_y
!= -1))
251 if ((m_x
!= old_x
) || (m_y
!= old_y
))
253 /* we set the position here and when showing the dialog
254 for the first time in idle time */
255 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
259 if ((m_width
!= old_width
) || (m_height
!= old_height
))
261 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
263 /* actual resizing is deferred to GtkOnSize in idle time and
264 when showing the dialog */
272 void wxPopupWindow::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
274 // due to a bug in gtk, x,y are always 0
278 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
279 if (!m_wxwindow
) return;
284 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
285 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
286 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
287 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
290 gint flag
= 0; // GDK_HINT_POS;
291 if ((m_minWidth
!= -1) || (m_minHeight
!= -1)) flag
|= GDK_HINT_MIN_SIZE
;
292 if ((m_maxWidth
!= -1) || (m_maxHeight
!= -1)) flag
|= GDK_HINT_MAX_SIZE
;
294 geom
.min_width
= m_minWidth
;
295 geom
.min_height
= m_minHeight
;
296 geom
.max_width
= m_maxWidth
;
297 geom
.max_height
= m_maxHeight
;
298 gtk_window_set_geometry_hints( GTK_WINDOW(m_widget
),
301 (GdkWindowHints
) flag
);
305 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
306 event
.SetEventObject( this );
307 GetEventHandler()->ProcessEvent( event
);
310 void wxPopupWindow::OnInternalIdle()
312 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
313 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
315 wxWindow::OnInternalIdle();
318 bool wxPopupWindow::Show( bool show
)
320 if (show
&& !m_sizeSet
)
322 /* by calling GtkOnSize here, we don't have to call
323 either after showing the frame, which would entail
324 much ugly flicker nor from within the size_allocate
325 handler, because GTK 1.1.X forbids that. */
327 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
330 bool ret
= wxWindow::Show( show
);