Added wxPopupWindow to GTK port, wxComboBox had to
[wxWidgets.git] / src / gtk / popupwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: popupwin.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "popupwin.h"
12 #endif
13
14 #include "wx/popupwin.h"
15 #include "wx/frame.h"
16 #include "wx/app.h"
17 #include "wx/cursor.h"
18
19 #include <gdk/gdk.h>
20 #include <gtk/gtk.h>
21 #include <gdk/gdkkeysyms.h>
22
23 #include "wx/gtk/win_gtk.h"
24
25 //-----------------------------------------------------------------------------
26 // idle system
27 //-----------------------------------------------------------------------------
28
29 extern void wxapp_install_idle_handler();
30 extern bool g_isIdle;
31
32 //-----------------------------------------------------------------------------
33 // "focus" from m_window
34 //-----------------------------------------------------------------------------
35
36 static gint gtk_dialog_focus_callback( GtkWidget *widget, GtkDirectionType WXUNUSED(d), wxWindow *WXUNUSED(win) )
37 {
38 if (g_isIdle)
39 wxapp_install_idle_handler();
40
41 // This disables GTK's tab traversal
42 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "focus" );
43 return TRUE;
44 }
45
46 //-----------------------------------------------------------------------------
47 // "delete_event"
48 //-----------------------------------------------------------------------------
49
50 bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxPopupWindow *win )
51 {
52 if (g_isIdle)
53 wxapp_install_idle_handler();
54
55 if (win->IsEnabled())
56 win->Close();
57
58 return TRUE;
59 }
60
61 //-----------------------------------------------------------------------------
62 // "size_allocate"
63 //-----------------------------------------------------------------------------
64
65 static void gtk_dialog_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxPopupWindow *win )
66 {
67 if (g_isIdle)
68 wxapp_install_idle_handler();
69
70 if (!win->m_hasVMT) return;
71
72 if ((win->m_width != alloc->width) || (win->m_height != alloc->height))
73 {
74 win->m_width = alloc->width;
75 win->m_height = alloc->height;
76 win->GtkUpdateSize();
77 }
78 }
79
80 //-----------------------------------------------------------------------------
81 // "realize" from m_widget
82 //-----------------------------------------------------------------------------
83
84 /* we cannot MWM hints and icons before the widget has been realized,
85 so we do this directly after realization */
86
87 static gint
88 gtk_dialog_realized_callback( GtkWidget * WXUNUSED(widget), wxPopupWindow *win )
89 {
90 if (g_isIdle)
91 wxapp_install_idle_handler();
92
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 ;
97
98 gdk_window_set_decorations( win->m_widget->window, (GdkWMDecoration)decor);
99 gdk_window_set_functions( win->m_widget->window, (GdkWMFunction)func);
100
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);
104 else
105 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 1, 1, 1);
106
107 return FALSE;
108 }
109
110 //-----------------------------------------------------------------------------
111 // InsertChild for wxPopupWindow
112 //-----------------------------------------------------------------------------
113
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. */
118
119 static void wxInsertChildInDialog( wxPopupWindow* parent, wxWindow* child )
120 {
121 gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow),
122 GTK_WIDGET(child->m_widget),
123 child->m_x,
124 child->m_y,
125 child->m_width,
126 child->m_height );
127
128 if (parent->HasFlag(wxTAB_TRAVERSAL))
129 {
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 );
133 }
134 }
135
136 //-----------------------------------------------------------------------------
137 // wxPopupWindow
138 //-----------------------------------------------------------------------------
139
140 BEGIN_EVENT_TABLE(wxPopupWindow,wxPopupWindowBase)
141 EVT_SIZE (wxPopupWindow::OnSize)
142 END_EVENT_TABLE()
143
144 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow,wxPopupWindowBase)
145
146 bool wxPopupWindow::Create( wxWindow *parent, int style )
147 {
148 m_needParent = FALSE;
149
150 if (!PreCreation( parent, wxDefaultPosition, wxDefaultSize ) ||
151 !CreateBase( parent, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, "popup" ))
152 {
153 wxFAIL_MSG( wxT("wxPopupWindow creation failed") );
154 return FALSE;
155 }
156
157 // All dialogs should really have this style
158 m_windowStyle |= wxTAB_TRAVERSAL;
159
160 m_insertCallback = (wxInsertChildFunction) wxInsertChildInDialog;
161
162 m_widget = gtk_window_new( GTK_WINDOW_POPUP );
163
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) );
166
167 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
168
169 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
170 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
171
172 m_wxwindow = gtk_pizza_new();
173 gtk_widget_show( m_wxwindow );
174 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
175
176 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
177
178 if (m_parent) m_parent->AddChild( this );
179
180 PostCreation();
181
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 );
186
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 );
190
191 /* disable native tab traversal */
192 gtk_signal_connect( GTK_OBJECT(m_widget), "focus",
193 GTK_SIGNAL_FUNC(gtk_dialog_focus_callback), (gpointer)this );
194
195 return TRUE;
196 }
197
198 void wxPopupWindow::DoMoveWindow(int WXUNUSED(x), int WXUNUSED(y), int WXUNUSED(width), int WXUNUSED(height) )
199 {
200 wxFAIL_MSG( wxT("DoMoveWindow called for wxPopupWindow") );
201 }
202
203 void wxPopupWindow::DoSetSize( int x, int y, int width, int height, int sizeFlags )
204 {
205 wxASSERT_MSG( (m_widget != NULL), wxT("invalid dialog") );
206 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid dialog") );
207
208 if (m_resizing) return; /* I don't like recursions */
209 m_resizing = TRUE;
210
211 int old_x = m_x;
212 int old_y = m_y;
213
214 int old_width = m_width;
215 int old_height = m_height;
216
217 if ((sizeFlags & wxSIZE_ALLOW_MINUS_ONE) == 0)
218 {
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;
223 }
224 else
225 {
226 m_x = x;
227 m_y = y;
228 m_width = width;
229 m_height = height;
230 }
231
232 /*
233 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
234 {
235 if (width == -1) m_width = 80;
236 }
237
238 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
239 {
240 if (height == -1) m_height = 26;
241 }
242 */
243
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;
248
249 if ((m_x != -1) || (m_y != -1))
250 {
251 if ((m_x != old_x) || (m_y != old_y))
252 {
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 );
256 }
257 }
258
259 if ((m_width != old_width) || (m_height != old_height))
260 {
261 gtk_widget_set_usize( m_widget, m_width, m_height );
262
263 /* actual resizing is deferred to GtkOnSize in idle time and
264 when showing the dialog */
265 m_sizeSet = FALSE;
266
267 }
268
269 m_resizing = FALSE;
270 }
271
272 void wxPopupWindow::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
273 {
274 // due to a bug in gtk, x,y are always 0
275 // m_x = x;
276 // m_y = y;
277
278 if ((m_height == height) && (m_width == width) && (m_sizeSet)) return;
279 if (!m_wxwindow) return;
280
281 m_width = width;
282 m_height = height;
283
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;
288
289 /* set size hints */
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;
293 GdkGeometry geom;
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),
299 (GtkWidget*) NULL,
300 &geom,
301 (GdkWindowHints) flag );
302
303 m_sizeSet = TRUE;
304
305 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
306 event.SetEventObject( this );
307 GetEventHandler()->ProcessEvent( event );
308 }
309
310 void wxPopupWindow::OnInternalIdle()
311 {
312 if (!m_sizeSet && GTK_WIDGET_REALIZED(m_wxwindow))
313 GtkOnSize( m_x, m_y, m_width, m_height );
314
315 wxWindow::OnInternalIdle();
316 }
317
318 bool wxPopupWindow::Show( bool show )
319 {
320 if (show && !m_sizeSet)
321 {
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. */
326
327 GtkOnSize( m_x, m_y, m_width, m_height );
328 }
329
330 bool ret = wxWindow::Show( show );
331
332 return ret;
333 }
334