Dislaying popup tip windows seems to work right now.
[wxWidgets.git] / src / gtk1 / 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/defs.h"
15
16 #if wxUSE_POPUPWIN
17
18 #include "wx/popupwin.h"
19 #include "wx/frame.h"
20 #include "wx/app.h"
21 #include "wx/cursor.h"
22
23 #include <gdk/gdk.h>
24 #include <gtk/gtk.h>
25 #include <gdk/gdkkeysyms.h>
26
27 #include "wx/gtk/win_gtk.h"
28
29 //-----------------------------------------------------------------------------
30 // idle system
31 //-----------------------------------------------------------------------------
32
33 extern void wxapp_install_idle_handler();
34 extern bool g_isIdle;
35
36 //-----------------------------------------------------------------------------
37 // "focus" from m_window
38 //-----------------------------------------------------------------------------
39
40 static gint gtk_dialog_focus_callback( GtkWidget *widget, GtkDirectionType WXUNUSED(d), wxWindow *WXUNUSED(win) )
41 {
42 if (g_isIdle)
43 wxapp_install_idle_handler();
44
45 // This disables GTK's tab traversal
46 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "focus" );
47 return TRUE;
48 }
49
50 //-----------------------------------------------------------------------------
51 // "delete_event"
52 //-----------------------------------------------------------------------------
53
54 bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxPopupWindow *win )
55 {
56 if (g_isIdle)
57 wxapp_install_idle_handler();
58
59 if (win->IsEnabled())
60 win->Close();
61
62 return TRUE;
63 }
64
65 //-----------------------------------------------------------------------------
66 // "size_allocate"
67 //-----------------------------------------------------------------------------
68
69 static void gtk_dialog_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxPopupWindow *win )
70 {
71 if (g_isIdle)
72 wxapp_install_idle_handler();
73
74 if (!win->m_hasVMT) return;
75
76 if ((win->m_width != alloc->width) || (win->m_height != alloc->height))
77 {
78 win->m_width = alloc->width;
79 win->m_height = alloc->height;
80 win->GtkUpdateSize();
81 }
82 }
83
84 //-----------------------------------------------------------------------------
85 // "realize" from m_widget
86 //-----------------------------------------------------------------------------
87
88 /* we cannot MWM hints and icons before the widget has been realized,
89 so we do this directly after realization */
90
91 static gint
92 gtk_dialog_realized_callback( GtkWidget * WXUNUSED(widget), wxPopupWindow *win )
93 {
94 if (g_isIdle)
95 wxapp_install_idle_handler();
96
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 ;
101
102 gdk_window_set_decorations( win->m_widget->window, (GdkWMDecoration)decor);
103 gdk_window_set_functions( win->m_widget->window, (GdkWMFunction)func);
104
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);
108 else
109 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 1, 1, 1);
110
111 return FALSE;
112 }
113
114 //-----------------------------------------------------------------------------
115 // InsertChild for wxPopupWindow
116 //-----------------------------------------------------------------------------
117
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. */
122
123 static void wxInsertChildInDialog( wxPopupWindow* parent, wxWindow* child )
124 {
125 gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow),
126 GTK_WIDGET(child->m_widget),
127 child->m_x,
128 child->m_y,
129 child->m_width,
130 child->m_height );
131
132 if (parent->HasFlag(wxTAB_TRAVERSAL))
133 {
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 );
137 }
138 }
139
140 //-----------------------------------------------------------------------------
141 // wxPopupWindow
142 //-----------------------------------------------------------------------------
143
144 BEGIN_EVENT_TABLE(wxPopupWindow,wxPopupWindowBase)
145 #ifdef __WXUNIVERSAL__
146 EVT_SIZE(wxPopupWindow::OnSize)
147 #endif
148 END_EVENT_TABLE()
149
150 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow)
151
152 wxPopupWindow::~wxPopupWindow()
153 {
154 if (GTK_WIDGET_HAS_GRAB(m_widget))
155 gtk_grab_remove( m_widget );
156 }
157
158 bool wxPopupWindow::Create( wxWindow *parent, int style )
159 {
160 m_needParent = FALSE;
161
162 if (!PreCreation( parent, wxDefaultPosition, wxDefaultSize ) ||
163 !CreateBase( parent, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, "popup" ))
164 {
165 wxFAIL_MSG( wxT("wxPopupWindow creation failed") );
166 return FALSE;
167 }
168
169 // All dialogs should really have this style
170 m_windowStyle |= wxTAB_TRAVERSAL;
171
172 m_insertCallback = (wxInsertChildFunction) wxInsertChildInDialog;
173
174 m_widget = gtk_window_new( GTK_WINDOW_POPUP );
175
176 if ((m_parent) && (GTK_IS_WINDOW(m_parent->m_widget)))
177 gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(m_parent->m_widget) );
178
179 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
180
181 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
182 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
183
184 m_wxwindow = gtk_pizza_new();
185 gtk_widget_show( m_wxwindow );
186 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
187
188 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
189
190 if (m_parent) m_parent->AddChild( this );
191
192 PostCreation();
193
194 /* we cannot set MWM hints before the widget has
195 been realized, so we do this directly after realization */
196 gtk_signal_connect( GTK_OBJECT(m_widget), "realize",
197 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback), (gpointer) this );
198
199 /* the user resized the frame by dragging etc. */
200 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
201 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
202
203 /* disable native tab traversal */
204 gtk_signal_connect( GTK_OBJECT(m_widget), "focus",
205 GTK_SIGNAL_FUNC(gtk_dialog_focus_callback), (gpointer)this );
206
207 return TRUE;
208 }
209
210 void wxPopupWindow::DoMoveWindow(int WXUNUSED(x), int WXUNUSED(y), int WXUNUSED(width), int WXUNUSED(height) )
211 {
212 wxFAIL_MSG( wxT("DoMoveWindow called for wxPopupWindow") );
213 }
214
215 void wxPopupWindow::DoSetSize( int x, int y, int width, int height, int sizeFlags )
216 {
217 wxASSERT_MSG( (m_widget != NULL), wxT("invalid dialog") );
218 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid dialog") );
219
220 if (m_resizing) return; /* I don't like recursions */
221 m_resizing = TRUE;
222
223 int old_x = m_x;
224 int old_y = m_y;
225
226 int old_width = m_width;
227 int old_height = m_height;
228
229 if ((sizeFlags & wxSIZE_ALLOW_MINUS_ONE) == 0)
230 {
231 if (x != -1) m_x = x;
232 if (y != -1) m_y = y;
233 if (width != -1) m_width = width;
234 if (height != -1) m_height = height;
235 }
236 else
237 {
238 m_x = x;
239 m_y = y;
240 m_width = width;
241 m_height = height;
242 }
243
244 /*
245 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
246 {
247 if (width == -1) m_width = 80;
248 }
249
250 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
251 {
252 if (height == -1) m_height = 26;
253 }
254 */
255
256 int minWidth = GetMinWidth(),
257 minHeight = GetMinHeight(),
258 maxWidth = GetMaxWidth(),
259 maxHeight = GetMaxHeight();
260
261 if ((minWidth != -1) && (m_width < minWidth)) m_width = minWidth;
262 if ((minHeight != -1) && (m_height < minHeight)) m_height = minHeight;
263 if ((maxWidth != -1) && (m_width > maxWidth)) m_width = maxWidth;
264 if ((maxHeight != -1) && (m_height > maxHeight)) m_height = maxHeight;
265
266 if ((m_x != -1) || (m_y != -1))
267 {
268 if ((m_x != old_x) || (m_y != old_y))
269 {
270 /* we set the position here and when showing the dialog
271 for the first time in idle time */
272 gtk_widget_set_uposition( m_widget, m_x, m_y );
273 }
274 }
275
276 if ((m_width != old_width) || (m_height != old_height))
277 {
278 gtk_widget_set_usize( m_widget, m_width, m_height );
279
280 /* actual resizing is deferred to GtkOnSize in idle time and
281 when showing the dialog */
282 m_sizeSet = FALSE;
283
284 }
285
286 m_resizing = FALSE;
287 }
288
289 void wxPopupWindow::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
290 {
291 // due to a bug in gtk, x,y are always 0
292 // m_x = x;
293 // m_y = y;
294
295 if ((m_height == height) && (m_width == width) && (m_sizeSet)) return;
296 if (!m_wxwindow) return;
297
298 m_width = width;
299 m_height = height;
300
301 int minWidth = GetMinWidth(),
302 minHeight = GetMinHeight(),
303 maxWidth = GetMaxWidth(),
304 maxHeight = GetMaxHeight();
305
306 if ((minWidth != -1) && (m_width < minWidth)) m_width = minWidth;
307 if ((minHeight != -1) && (m_height < minHeight)) m_height = minHeight;
308 if ((maxWidth != -1) && (m_width > maxWidth)) m_width = maxWidth;
309 if ((maxHeight != -1) && (m_height > maxHeight)) m_height = maxHeight;
310
311 /* set size hints */
312 gint flag = 0; // GDK_HINT_POS;
313 if ((minWidth != -1) || (minHeight != -1)) flag |= GDK_HINT_MIN_SIZE;
314 if ((maxWidth != -1) || (maxHeight != -1)) flag |= GDK_HINT_MAX_SIZE;
315 GdkGeometry geom;
316 geom.min_width = minWidth;
317 geom.min_height = minHeight;
318 geom.max_width = maxWidth;
319 geom.max_height = maxHeight;
320 gtk_window_set_geometry_hints( GTK_WINDOW(m_widget),
321 (GtkWidget*) NULL,
322 &geom,
323 (GdkWindowHints) flag );
324
325 m_sizeSet = TRUE;
326
327 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
328 event.SetEventObject( this );
329 GetEventHandler()->ProcessEvent( event );
330 }
331
332 void wxPopupWindow::OnInternalIdle()
333 {
334 if (!m_sizeSet && GTK_WIDGET_REALIZED(m_wxwindow))
335 GtkOnSize( m_x, m_y, m_width, m_height );
336
337 wxWindow::OnInternalIdle();
338 }
339
340 bool wxPopupWindow::Show( bool show )
341 {
342 if (show && !m_sizeSet)
343 {
344 /* by calling GtkOnSize here, we don't have to call
345 either after showing the frame, which would entail
346 much ugly flicker nor from within the size_allocate
347 handler, because GTK 1.1.X forbids that. */
348
349 GtkOnSize( m_x, m_y, m_width, m_height );
350 }
351
352 if (!show)
353 gtk_grab_remove( m_widget );
354
355 bool ret = wxWindow::Show( show );
356
357 if (show)
358 gtk_grab_add( m_widget );
359
360 return ret;
361 }
362
363 #endif // wxUSE_POPUPWIN