simplified and cleaned up wxGTK's focus handling
[wxWidgets.git] / src / gtk / toplevel.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/toplevel.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 // ============================================================================
14 // declarations
15 // ============================================================================
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 #ifdef __VMS
22 #define XIconifyWindow XICONIFYWINDOW
23 #endif
24
25 #include "wx/toplevel.h"
26
27 #ifndef WX_PRECOMP
28 #include "wx/frame.h"
29 #include "wx/icon.h"
30 #include "wx/log.h"
31 #include "wx/app.h"
32 #endif
33
34 #include "wx/gtk/private.h"
35 #include "wx/evtloop.h"
36 #include "wx/sysopt.h"
37
38 #include <gtk/gtk.h>
39 #include <gdk/gdkx.h>
40
41 #include "wx/gtk/private/win_gtk.h"
42
43 #include "wx/unix/utilsx11.h"
44
45 // XA_CARDINAL
46 #include <X11/Xatom.h>
47
48 #if wxUSE_LIBHILDON
49 #include <hildon-widgets/hildon-program.h>
50 #include <hildon-widgets/hildon-window.h>
51 #endif // wxUSE_LIBHILDON
52
53 // ----------------------------------------------------------------------------
54 // data
55 // ----------------------------------------------------------------------------
56
57 // this is incremented while a modal dialog is shown
58 int wxOpenModalDialogsCount = 0;
59
60 // the frame that is currently active (i.e. its child has focus). It is
61 // used to generate wxActivateEvents
62 static wxTopLevelWindowGTK *g_activeFrame = (wxTopLevelWindowGTK*) NULL;
63 static wxTopLevelWindowGTK *g_lastActiveFrame = (wxTopLevelWindowGTK*) NULL;
64
65 // if we detect that the app has got/lost the focus, we set this variable to
66 // either TRUE or FALSE and an activate event will be sent during the next
67 // OnIdle() call and it is reset to -1: this value means that we shouldn't
68 // send any activate events at all
69 static int g_sendActivateEvent = -1;
70
71 //-----------------------------------------------------------------------------
72 // RequestUserAttention related functions
73 //-----------------------------------------------------------------------------
74
75 extern "C" {
76 static void wxgtk_window_set_urgency_hint (GtkWindow *win,
77 gboolean setting)
78 {
79 wxASSERT_MSG( GTK_WIDGET_REALIZED(win), wxT("wxgtk_window_set_urgency_hint: GdkWindow not realized") );
80 GdkWindow *window = GTK_WIDGET(win)->window;
81 XWMHints *wm_hints;
82
83 wm_hints = XGetWMHints(GDK_WINDOW_XDISPLAY(window), GDK_WINDOW_XWINDOW(window));
84
85 if (!wm_hints)
86 wm_hints = XAllocWMHints();
87
88 if (setting)
89 wm_hints->flags |= XUrgencyHint;
90 else
91 wm_hints->flags &= ~XUrgencyHint;
92
93 XSetWMHints(GDK_WINDOW_XDISPLAY(window), GDK_WINDOW_XWINDOW(window), wm_hints);
94 XFree(wm_hints);
95 }
96
97 static gboolean gtk_frame_urgency_timer_callback( wxTopLevelWindowGTK *win )
98 {
99 #if GTK_CHECK_VERSION(2,7,0)
100 if(!gtk_check_version(2,7,0))
101 gtk_window_set_urgency_hint(GTK_WINDOW( win->m_widget ), FALSE);
102 else
103 #endif
104 wxgtk_window_set_urgency_hint(GTK_WINDOW( win->m_widget ), FALSE);
105
106 win->m_urgency_hint = -2;
107 return FALSE;
108 }
109 }
110
111 //-----------------------------------------------------------------------------
112 // "focus_in_event"
113 //-----------------------------------------------------------------------------
114
115 extern "C" {
116 static gboolean gtk_frame_focus_in_callback( GtkWidget *widget,
117 GdkEvent *WXUNUSED(event),
118 wxTopLevelWindowGTK *win )
119 {
120 switch ( g_sendActivateEvent )
121 {
122 case -1:
123 // we've got focus from outside, synthetize wxActivateEvent
124 g_sendActivateEvent = 1;
125 break;
126
127 case 0:
128 // another our window just lost focus, it was already ours before
129 // - don't send any wxActivateEvent
130 g_sendActivateEvent = -1;
131 break;
132 }
133
134 g_activeFrame = win;
135 g_lastActiveFrame = g_activeFrame;
136
137 // wxPrintf( wxT("active: %s\n"), win->GetTitle().c_str() );
138
139 // MR: wxRequestUserAttention related block
140 switch( win->m_urgency_hint )
141 {
142 default:
143 g_source_remove( win->m_urgency_hint );
144 // no break, fallthrough to remove hint too
145 case -1:
146 #if GTK_CHECK_VERSION(2,7,0)
147 if(!gtk_check_version(2,7,0))
148 gtk_window_set_urgency_hint(GTK_WINDOW( widget ), FALSE);
149 else
150 #endif
151 {
152 wxgtk_window_set_urgency_hint(GTK_WINDOW( widget ), FALSE);
153 }
154
155 win->m_urgency_hint = -2;
156 break;
157
158 case -2: break;
159 }
160
161 wxLogTrace(wxT("activate"), wxT("Activating frame %p (from focus_in)"), g_activeFrame);
162 wxActivateEvent event(wxEVT_ACTIVATE, true, g_activeFrame->GetId());
163 event.SetEventObject(g_activeFrame);
164 g_activeFrame->HandleWindowEvent(event);
165
166 return FALSE;
167 }
168 }
169
170 //-----------------------------------------------------------------------------
171 // "focus_out_event"
172 //-----------------------------------------------------------------------------
173
174 extern "C" {
175 static
176 gboolean gtk_frame_focus_out_callback(GtkWidget * WXUNUSED(widget),
177 GdkEventFocus *WXUNUSED(gdk_event),
178 wxTopLevelWindowGTK * WXUNUSED(win))
179 {
180 // if the focus goes out of our app alltogether, OnIdle() will send
181 // wxActivateEvent, otherwise gtk_window_focus_in_callback() will reset
182 // g_sendActivateEvent to -1
183 g_sendActivateEvent = 0;
184
185 // wxASSERT_MSG( (g_activeFrame == win), wxT("TLW deactivatd although it wasn't active") );
186
187 // wxPrintf( wxT("inactive: %s\n"), win->GetTitle().c_str() );
188
189 if (g_activeFrame)
190 {
191 wxLogTrace(wxT("activate"), wxT("Activating frame %p (from focus_in)"), g_activeFrame);
192 wxActivateEvent event(wxEVT_ACTIVATE, false, g_activeFrame->GetId());
193 event.SetEventObject(g_activeFrame);
194 g_activeFrame->HandleWindowEvent(event);
195
196 g_activeFrame = NULL;
197 }
198
199 return FALSE;
200 }
201 }
202
203 //-----------------------------------------------------------------------------
204 // "size_allocate" from m_wxwindow
205 //-----------------------------------------------------------------------------
206
207 extern "C" {
208 static void
209 size_allocate(GtkWidget*, GtkAllocation* alloc, wxTopLevelWindowGTK* win)
210 {
211 if (win->m_oldClientWidth != alloc->width ||
212 win->m_oldClientHeight != alloc->height)
213 {
214 win->m_oldClientWidth = alloc->width;
215 win->m_oldClientHeight = alloc->height;
216
217 wxSize size(win->m_widget->allocation.width,
218 win->m_widget->allocation.height);
219 size += win->m_decorSize;
220 win->m_width = size.x;
221 win->m_height = size.y;
222
223 if (!win->IsIconized())
224 {
225 wxSizeEvent event(size, win->GetId());
226 event.SetEventObject(win);
227 win->HandleWindowEvent(event);
228 }
229 // else the window is currently unmapped, don't generate size events
230 }
231 }
232 }
233
234 // ----------------------------------------------------------------------------
235 // "size_request"
236 // ----------------------------------------------------------------------------
237
238 extern "C" {
239 static
240 void wxgtk_tlw_size_request_callback(GtkWidget * WXUNUSED(widget),
241 GtkRequisition *requisition,
242 wxTopLevelWindowGTK *win)
243 {
244 // we must return the size of the window without WM decorations, otherwise
245 // GTK+ gets confused, so don't call just GetSize() here
246 win->GTKDoGetSize(&requisition->width, &requisition->height);
247 }
248 }
249
250 //-----------------------------------------------------------------------------
251 // "delete_event"
252 //-----------------------------------------------------------------------------
253
254 extern "C" {
255 static gboolean
256 gtk_frame_delete_callback( GtkWidget *WXUNUSED(widget),
257 GdkEvent *WXUNUSED(event),
258 wxTopLevelWindowGTK *win )
259 {
260 if (win->IsEnabled() &&
261 (wxOpenModalDialogsCount == 0 || (win->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG) ||
262 win->IsGrabbed()))
263 win->Close();
264
265 return TRUE;
266 }
267 }
268
269 //-----------------------------------------------------------------------------
270 // "configure_event"
271 //-----------------------------------------------------------------------------
272
273 extern "C" {
274 static gboolean
275 gtk_frame_configure_callback( GtkWidget* widget,
276 GdkEventConfigure *WXUNUSED(event),
277 wxTopLevelWindowGTK *win )
278 {
279 if (!win->m_hasVMT || !win->IsShown())
280 return FALSE;
281
282 wxPoint point;
283 gtk_window_get_position((GtkWindow*)widget, &point.x, &point.y);
284
285 win->m_x = point.x;
286 win->m_y = point.y;
287 wxMoveEvent mevent(point, win->GetId());
288 mevent.SetEventObject( win );
289 win->HandleWindowEvent( mevent );
290
291 return FALSE;
292 }
293 }
294
295 //-----------------------------------------------------------------------------
296 // "realize" from m_widget
297 //-----------------------------------------------------------------------------
298
299 // we cannot MWM hints and icons before the widget has been realized,
300 // so we do this directly after realization
301
302 extern "C" {
303 static void
304 gtk_frame_realized_callback( GtkWidget * WXUNUSED(widget),
305 wxTopLevelWindowGTK *win )
306 {
307 // All this is for Motif Window Manager "hints" and is supposed to be
308 // recognized by other WM as well. Not tested.
309 gdk_window_set_decorations(win->m_widget->window,
310 (GdkWMDecoration)win->m_gdkDecor);
311 gdk_window_set_functions(win->m_widget->window,
312 (GdkWMFunction)win->m_gdkFunc);
313
314 // GTK's shrinking/growing policy
315 if ((win->m_gdkFunc & GDK_FUNC_RESIZE) == 0)
316 gtk_window_set_resizable(GTK_WINDOW(win->m_widget), FALSE);
317 else
318 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 1, 1, 1);
319
320 // reset the icon
321 wxIconBundle iconsOld = win->GetIcons();
322 if ( !iconsOld.IsEmpty() )
323 {
324 win->SetIcon( wxNullIcon );
325 win->SetIcons( iconsOld );
326 }
327 }
328 }
329
330 //-----------------------------------------------------------------------------
331 // "map_event" from m_widget
332 //-----------------------------------------------------------------------------
333
334 extern "C" {
335 static gboolean
336 gtk_frame_map_callback( GtkWidget*,
337 GdkEvent * WXUNUSED(event),
338 wxTopLevelWindow *win )
339 {
340 const bool wasIconized = win->IsIconized();
341
342 win->SetIconizeState(false);
343
344 if (wasIconized)
345 {
346 // Because GetClientSize() returns (0,0) when IsIconized() is true,
347 // a size event must be generated, just in case GetClientSize() was
348 // called while iconized. This specifically happens when restoring a
349 // tlw that was "rolled up" with some WMs.
350 // Queue a resize rather than sending size event directly to allow
351 // children to be made visible first.
352 win->m_oldClientWidth = 0;
353 gtk_widget_queue_resize(win->m_wxwindow);
354 }
355
356 return false;
357 }
358 }
359
360 //-----------------------------------------------------------------------------
361 // "unmap_event" from m_widget
362 //-----------------------------------------------------------------------------
363
364 extern "C" {
365 static gboolean
366 gtk_frame_unmap_callback( GtkWidget * WXUNUSED(widget),
367 GdkEvent * WXUNUSED(event),
368 wxTopLevelWindow *win )
369 {
370 win->SetIconizeState(true);
371 return false;
372 }
373 }
374
375 //-----------------------------------------------------------------------------
376 // "property_notify_event" from m_widget
377 //-----------------------------------------------------------------------------
378
379 extern "C" {
380 static gboolean property_notify_event(
381 GtkWidget*, GdkEventProperty* event, wxTopLevelWindowGTK* win)
382 {
383 // Watch for changes to _NET_FRAME_EXTENTS property
384 static GdkAtom property = gdk_atom_intern("_NET_FRAME_EXTENTS", false);
385 if (event->state == GDK_PROPERTY_NEW_VALUE && event->atom == property)
386 {
387 Atom xproperty = gdk_x11_atom_to_xatom_for_display(
388 gdk_drawable_get_display(event->window), property);
389 Atom type;
390 int format;
391 gulong nitems, bytes_after;
392 guchar* data;
393 Status status = XGetWindowProperty(
394 gdk_x11_drawable_get_xdisplay(event->window),
395 gdk_x11_drawable_get_xid(event->window),
396 xproperty,
397 0, 4, false, XA_CARDINAL,
398 &type, &format, &nitems, &bytes_after, &data);
399 if (status == Success && data && nitems == 4)
400 {
401 long* p = (long*)data;
402 const wxSize decorSize =
403 wxSize(int(p[0] + p[1]), int(p[2] + p[3]));
404 win->GTKUpdateDecorSize(decorSize);
405 }
406 if (data)
407 XFree(data);
408 }
409 return false;
410 }
411 }
412
413 BEGIN_EVENT_TABLE(wxTopLevelWindowGTK, wxTopLevelWindowBase)
414 EVT_SYS_COLOUR_CHANGED(wxTopLevelWindowGTK::OnSysColourChanged)
415 END_EVENT_TABLE()
416
417 // ----------------------------------------------------------------------------
418 // wxTopLevelWindowGTK creation
419 // ----------------------------------------------------------------------------
420
421 void wxTopLevelWindowGTK::Init()
422 {
423 m_mainWidget = (GtkWidget*) NULL;
424 m_isIconized = false;
425 m_fsIsShowing = false;
426 m_themeEnabled = true;
427 m_gdkDecor = m_gdkFunc = 0;
428 m_grabbed = false;
429 m_deferShow = true;
430
431 m_urgency_hint = -2;
432 }
433
434 bool wxTopLevelWindowGTK::Create( wxWindow *parent,
435 wxWindowID id,
436 const wxString& title,
437 const wxPoint& pos,
438 const wxSize& sizeOrig,
439 long style,
440 const wxString &name )
441 {
442 // always create a frame of some reasonable, even if arbitrary, size (at
443 // least for MSW compatibility)
444 wxSize size = sizeOrig;
445 size.x = WidthDefault(size.x);
446 size.y = HeightDefault(size.y);
447
448 wxTopLevelWindows.Append( this );
449
450 if (!PreCreation( parent, pos, size ) ||
451 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
452 {
453 wxFAIL_MSG( wxT("wxTopLevelWindowGTK creation failed") );
454 return false;
455 }
456
457 m_title = title;
458
459 // NB: m_widget may be !=NULL if it was created by derived class' Create,
460 // e.g. in wxTaskBarIconAreaGTK
461 if (m_widget == NULL)
462 {
463 #if wxUSE_LIBHILDON
464 // we must create HildonWindow and not a normal GtkWindow as the latter
465 // doesn't look correctly in Maemo environment and it must also be
466 // registered with the main program object
467 m_widget = hildon_window_new();
468 hildon_program_add_window(wxTheApp->GetHildonProgram(),
469 HILDON_WINDOW(m_widget));
470 #else // !wxUSE_LIBHILDON
471 m_widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
472 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG)
473 {
474 // Tell WM that this is a dialog window and make it center
475 // on parent by default (this is what GtkDialog ctor does):
476 gtk_window_set_type_hint(GTK_WINDOW(m_widget),
477 GDK_WINDOW_TYPE_HINT_DIALOG);
478 gtk_window_set_position(GTK_WINDOW(m_widget),
479 GTK_WIN_POS_CENTER_ON_PARENT);
480 }
481 else
482 {
483 if (style & wxFRAME_TOOL_WINDOW)
484 {
485 gtk_window_set_type_hint(GTK_WINDOW(m_widget),
486 GDK_WINDOW_TYPE_HINT_UTILITY);
487
488 // On some WMs, like KDE, a TOOL_WINDOW will still show
489 // on the taskbar, but on Gnome a TOOL_WINDOW will not.
490 // For consistency between WMs and with Windows, we
491 // should set the NO_TASKBAR flag which will apply
492 // the set_skip_taskbar_hint if it is available,
493 // ensuring no taskbar entry will appear.
494 style |= wxFRAME_NO_TASKBAR;
495 }
496 }
497 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
498 }
499
500 wxWindow *topParent = wxGetTopLevelParent(m_parent);
501 if (topParent && (((GTK_IS_WINDOW(topParent->m_widget)) &&
502 (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG)) ||
503 (style & wxFRAME_FLOAT_ON_PARENT)))
504 {
505 gtk_window_set_transient_for( GTK_WINDOW(m_widget),
506 GTK_WINDOW(topParent->m_widget) );
507 }
508
509 if (style & wxFRAME_NO_TASKBAR)
510 {
511 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(m_widget), TRUE);
512 }
513
514 if (style & wxSTAY_ON_TOP)
515 {
516 gtk_window_set_keep_above(GTK_WINDOW(m_widget), TRUE);
517 }
518
519 #if 0
520 if (!name.empty())
521 gtk_window_set_role( GTK_WINDOW(m_widget), wxGTK_CONV( name ) );
522 #endif
523
524 gtk_window_set_title( GTK_WINDOW(m_widget), wxGTK_CONV( title ) );
525 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
526
527 g_signal_connect (m_widget, "delete_event",
528 G_CALLBACK (gtk_frame_delete_callback), this);
529
530 // m_mainWidget is a GtkVBox, holding the bars and client area (m_wxwindow)
531 m_mainWidget = gtk_vbox_new(false, 0);
532 gtk_widget_show( m_mainWidget );
533 GTK_WIDGET_UNSET_FLAGS( m_mainWidget, GTK_CAN_FOCUS );
534 gtk_container_add( GTK_CONTAINER(m_widget), m_mainWidget );
535
536 // m_wxwindow is the client area
537 m_wxwindow = wxPizza::New();
538 gtk_widget_show( m_wxwindow );
539 gtk_container_add( GTK_CONTAINER(m_mainWidget), m_wxwindow );
540
541 // we donm't allow the frame to get the focus as otherwise
542 // the frame will grab it at arbitrary focus changes
543 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
544
545 if (m_parent) m_parent->AddChild( this );
546
547 g_signal_connect(m_wxwindow, "size_allocate",
548 G_CALLBACK(size_allocate), this);
549
550 g_signal_connect (m_widget, "size_request",
551 G_CALLBACK (wxgtk_tlw_size_request_callback), this);
552 PostCreation();
553
554 if ((m_x != -1) || (m_y != -1))
555 gtk_widget_set_uposition( m_widget, m_x, m_y );
556
557 // we cannot set MWM hints and icons before the widget has
558 // been realized, so we do this directly after realization
559 g_signal_connect (m_widget, "realize",
560 G_CALLBACK (gtk_frame_realized_callback), this);
561
562 // map and unmap for iconized state
563 g_signal_connect (m_widget, "map_event",
564 G_CALLBACK (gtk_frame_map_callback), this);
565 g_signal_connect (m_widget, "unmap_event",
566 G_CALLBACK (gtk_frame_unmap_callback), this);
567
568 // for wxMoveEvent
569 g_signal_connect (m_widget, "configure_event",
570 G_CALLBACK (gtk_frame_configure_callback), this);
571
572 // activation
573 g_signal_connect_after (m_widget, "focus_in_event",
574 G_CALLBACK (gtk_frame_focus_in_callback), this);
575 g_signal_connect_after (m_widget, "focus_out_event",
576 G_CALLBACK (gtk_frame_focus_out_callback), this);
577
578 gtk_widget_add_events(m_widget, GDK_PROPERTY_CHANGE_MASK);
579 g_signal_connect(m_widget, "property_notify_event",
580 G_CALLBACK(property_notify_event), this);
581
582 // decorations
583 if ((style & wxSIMPLE_BORDER) || (style & wxNO_BORDER))
584 {
585 m_gdkDecor = 0;
586 m_gdkFunc = 0;
587 }
588 else
589 {
590 m_gdkDecor = GDK_DECOR_BORDER;
591 m_gdkFunc = GDK_FUNC_MOVE;
592
593 // All this is for Motif Window Manager "hints" and is supposed to be
594 // recognized by other WMs as well.
595 if ((style & wxCAPTION) != 0)
596 {
597 m_gdkDecor |= GDK_DECOR_TITLE;
598 }
599 if ((style & wxCLOSE_BOX) != 0)
600 {
601 m_gdkFunc |= GDK_FUNC_CLOSE;
602 }
603 if ((style & wxSYSTEM_MENU) != 0)
604 {
605 m_gdkDecor |= GDK_DECOR_MENU;
606 }
607 if ((style & wxMINIMIZE_BOX) != 0)
608 {
609 m_gdkFunc |= GDK_FUNC_MINIMIZE;
610 m_gdkDecor |= GDK_DECOR_MINIMIZE;
611 }
612 if ((style & wxMAXIMIZE_BOX) != 0)
613 {
614 m_gdkFunc |= GDK_FUNC_MAXIMIZE;
615 m_gdkDecor |= GDK_DECOR_MAXIMIZE;
616 }
617 if ((style & wxRESIZE_BORDER) != 0)
618 {
619 m_gdkFunc |= GDK_FUNC_RESIZE;
620 m_gdkDecor |= GDK_DECOR_RESIZEH;
621 }
622 }
623
624 gtk_window_set_default_size(GTK_WINDOW(m_widget), m_width, m_height);
625
626 return true;
627 }
628
629 wxTopLevelWindowGTK::~wxTopLevelWindowGTK()
630 {
631 #if wxUSE_LIBHILDON
632 // it can also be a (standard) dialog
633 if ( HILDON_IS_WINDOW(m_widget) )
634 {
635 hildon_program_remove_window(wxTheApp->GetHildonProgram(),
636 HILDON_WINDOW(m_widget));
637 }
638 #endif // wxUSE_LIBHILDON
639
640 if (m_grabbed)
641 {
642 wxFAIL_MSG(_T("Window still grabbed"));
643 RemoveGrab();
644 }
645
646 m_isBeingDeleted = true;
647
648 // it may also be GtkScrolledWindow in the case of an MDI child
649 if (GTK_IS_WINDOW(m_widget))
650 {
651 gtk_window_set_focus( GTK_WINDOW(m_widget), NULL );
652 }
653
654 if (g_activeFrame == this)
655 g_activeFrame = NULL;
656 if (g_lastActiveFrame == this)
657 g_lastActiveFrame = NULL;
658 }
659
660 bool wxTopLevelWindowGTK::EnableCloseButton( bool enable )
661 {
662 if (enable)
663 m_gdkFunc |= GDK_FUNC_CLOSE;
664 else
665 m_gdkFunc &= ~GDK_FUNC_CLOSE;
666
667 if (GTK_WIDGET_REALIZED(m_widget) && (m_widget->window))
668 gdk_window_set_functions( m_widget->window, (GdkWMFunction)m_gdkFunc );
669
670 return true;
671 }
672
673 bool wxTopLevelWindowGTK::ShowFullScreen(bool show, long)
674 {
675 if (show == m_fsIsShowing)
676 return false; // return what?
677
678 m_fsIsShowing = show;
679
680 wxX11FullScreenMethod method =
681 wxGetFullScreenMethodX11((WXDisplay*)GDK_DISPLAY(),
682 (WXWindow)GDK_ROOT_WINDOW());
683
684 // NB: gtk_window_fullscreen() uses freedesktop.org's WMspec extensions
685 // to switch to fullscreen, which is not always available. We must
686 // check if WM supports the spec and use legacy methods if it
687 // doesn't.
688 if ( method == wxX11_FS_WMSPEC )
689 {
690 if (show)
691 gtk_window_fullscreen( GTK_WINDOW( m_widget ) );
692 else
693 gtk_window_unfullscreen( GTK_WINDOW( m_widget ) );
694 }
695 else
696 {
697 GdkWindow *window = m_widget->window;
698
699 if (show)
700 {
701 GetPosition( &m_fsSaveFrame.x, &m_fsSaveFrame.y );
702 GetSize( &m_fsSaveFrame.width, &m_fsSaveFrame.height );
703
704 int screen_width,screen_height;
705 wxDisplaySize( &screen_width, &screen_height );
706
707 gint client_x, client_y, root_x, root_y;
708 gint width, height;
709
710 m_fsSaveGdkFunc = m_gdkFunc;
711 m_fsSaveGdkDecor = m_gdkDecor;
712 m_gdkFunc = m_gdkDecor = 0;
713 gdk_window_set_decorations(window, (GdkWMDecoration)0);
714 gdk_window_set_functions(window, (GdkWMFunction)0);
715
716 gdk_window_get_origin (m_widget->window, &root_x, &root_y);
717 gdk_window_get_geometry (m_widget->window, &client_x, &client_y,
718 &width, &height, NULL);
719
720 gdk_window_move_resize (m_widget->window, -client_x, -client_y,
721 screen_width + 1, screen_height + 1);
722
723 wxSetFullScreenStateX11((WXDisplay*)GDK_DISPLAY(),
724 (WXWindow)GDK_ROOT_WINDOW(),
725 (WXWindow)GDK_WINDOW_XWINDOW(window),
726 show, &m_fsSaveFrame, method);
727 }
728 else // hide
729 {
730 m_gdkFunc = m_fsSaveGdkFunc;
731 m_gdkDecor = m_fsSaveGdkDecor;
732 gdk_window_set_decorations(window, (GdkWMDecoration)m_gdkDecor);
733 gdk_window_set_functions(window, (GdkWMFunction)m_gdkFunc);
734
735 wxSetFullScreenStateX11((WXDisplay*)GDK_DISPLAY(),
736 (WXWindow)GDK_ROOT_WINDOW(),
737 (WXWindow)GDK_WINDOW_XWINDOW(window),
738 show, &m_fsSaveFrame, method);
739
740 SetSize(m_fsSaveFrame.x, m_fsSaveFrame.y,
741 m_fsSaveFrame.width, m_fsSaveFrame.height);
742 }
743 }
744
745 // documented behaviour is to show the window if it's still hidden when
746 // showing it full screen
747 if (show)
748 Show();
749
750 return true;
751 }
752
753 // ----------------------------------------------------------------------------
754 // overridden wxWindow methods
755 // ----------------------------------------------------------------------------
756
757 bool wxTopLevelWindowGTK::Show( bool show )
758 {
759 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
760
761 const bool wasRealized = GTK_WIDGET_REALIZED(m_widget);
762 bool deferShow =
763 show && m_deferShow && !wasRealized &&
764 g_signal_handler_find(m_widget,
765 GSignalMatchType(G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DATA),
766 g_signal_lookup("property_notify_event", GTK_TYPE_WIDGET),
767 0, NULL, NULL, this);
768 if (deferShow)
769 {
770 // Initial show. If WM supports _NET_REQUEST_FRAME_EXTENTS, defer
771 // calling gtk_widget_show() until _NET_FRAME_EXTENTS property
772 // notification is received, so correct frame extents are known.
773 // This allows resizing m_widget to keep the overall size in sync with
774 // what wxWidgets expects it to be without an obvious change in the
775 // window size immediately after it becomes visible.
776
777 // Realize m_widget, so m_widget->window can be used. Realizing causes
778 // the widget tree to be size_allocated, which generates size events in
779 // the wrong order. So temporarily remove child from m_widget while
780 // realizing.
781 GtkWidget* child = GTK_BIN(m_widget)->child;
782 if (child)
783 {
784 g_object_ref(child);
785 gtk_container_remove(GTK_CONTAINER(m_widget), child);
786 }
787 gtk_widget_realize(m_widget);
788 if (child)
789 {
790 gtk_container_add(GTK_CONTAINER(m_widget), child);
791 g_object_unref(child);
792 }
793
794 m_deferShow =
795 deferShow = gdk_x11_screen_supports_net_wm_hint(
796 gdk_drawable_get_screen(m_widget->window),
797 gdk_atom_intern("_NET_REQUEST_FRAME_EXTENTS", false)) != 0;
798 }
799 if (deferShow)
800 {
801 // send _NET_REQUEST_FRAME_EXTENTS
802 XClientMessageEvent xevent;
803 memset(&xevent, 0, sizeof(xevent));
804 xevent.type = ClientMessage;
805 xevent.window = gdk_x11_drawable_get_xid(m_widget->window);
806 xevent.message_type = gdk_x11_atom_to_xatom_for_display(
807 gdk_drawable_get_display(m_widget->window),
808 gdk_atom_intern("_NET_REQUEST_FRAME_EXTENTS", false));
809 xevent.format = 32;
810 Display* display = gdk_x11_drawable_get_xdisplay(m_widget->window);
811 XSendEvent(display, DefaultRootWindow(display), false,
812 SubstructureNotifyMask | SubstructureRedirectMask,
813 (XEvent*)&xevent);
814
815 // defer calling gtk_widget_show()
816 m_isShown = true;
817 return true;
818 }
819
820 if (show && !wasRealized)
821 {
822 // size_allocate signals occur in reverse order (bottom to top).
823 // Things work better if the initial wxSizeEvents are sent (from the
824 // top down), before the initial size_allocate signals occur.
825 wxSizeEvent event(GetSize(), GetId());
826 event.SetEventObject(this);
827 HandleWindowEvent(event);
828 }
829
830 bool change = wxTopLevelWindowBase::Show(show);
831
832 if (change && !show)
833 {
834 // make sure window has a non-default position, so when it is shown
835 // again, it won't be repositioned by WM as if it were a new window
836 // Note that this must be done _after_ the window is hidden.
837 gtk_window_move((GtkWindow*)m_widget, m_x, m_y);
838 }
839
840 return change;
841 }
842
843 void wxTopLevelWindowGTK::Raise()
844 {
845 gtk_window_present( GTK_WINDOW( m_widget ) );
846 }
847
848 void wxTopLevelWindowGTK::DoMoveWindow(int WXUNUSED(x), int WXUNUSED(y), int WXUNUSED(width), int WXUNUSED(height) )
849 {
850 wxFAIL_MSG( wxT("DoMoveWindow called for wxTopLevelWindowGTK") );
851 }
852
853 // ----------------------------------------------------------------------------
854 // window geometry
855 // ----------------------------------------------------------------------------
856
857 void wxTopLevelWindowGTK::GTKDoGetSize(int *width, int *height) const
858 {
859 wxSize size(m_width, m_height);
860 size -= m_decorSize;
861 if (size.x < 0) size.x = 0;
862 if (size.y < 0) size.y = 0;
863 if (width) *width = size.x;
864 if (height) *height = size.y;
865 }
866
867 void wxTopLevelWindowGTK::DoSetSize( int x, int y, int width, int height, int sizeFlags )
868 {
869 wxCHECK_RET( m_widget, wxT("invalid frame") );
870
871 // deal with the position first
872 int old_x = m_x;
873 int old_y = m_y;
874
875 if ( !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
876 {
877 // -1 means "use existing" unless the flag above is specified
878 if ( x != -1 )
879 m_x = x;
880 if ( y != -1 )
881 m_y = y;
882 }
883 else // wxSIZE_ALLOW_MINUS_ONE
884 {
885 m_x = x;
886 m_y = y;
887 }
888
889 if ( m_x != old_x || m_y != old_y )
890 {
891 gtk_window_move( GTK_WINDOW(m_widget), m_x, m_y );
892 }
893
894 const wxSize oldSize(m_width, m_height);
895 if (width >= 0)
896 m_width = width;
897 if (height >= 0)
898 m_height = height;
899 ConstrainSize();
900 if (m_width != oldSize.x || m_height != oldSize.y)
901 {
902 int w, h;
903 GTKDoGetSize(&w, &h);
904 gtk_window_resize(GTK_WINDOW(m_widget), w, h);
905
906 GetClientSize(&m_oldClientWidth, &m_oldClientHeight);
907 wxSizeEvent event(GetSize(), GetId());
908 event.SetEventObject(this);
909 HandleWindowEvent(event);
910 }
911 }
912
913 void wxTopLevelWindowGTK::DoSetClientSize(int width, int height)
914 {
915 if (m_deferShow && !m_isShown)
916 // Since client size is being explicitly set, don't change it later
917 m_deferShow = false;
918 wxTopLevelWindowBase::DoSetClientSize(width, height);
919 }
920
921 void wxTopLevelWindowGTK::DoGetClientSize( int *width, int *height ) const
922 {
923 wxASSERT_MSG(m_widget, wxT("invalid frame"));
924
925 if ( IsIconized() )
926 {
927 // for consistency with wxMSW, client area is supposed to be empty for
928 // the iconized windows
929 if ( width )
930 *width = 0;
931 if ( height )
932 *height = 0;
933 }
934 else
935 {
936 GTKDoGetSize(width, height);
937 }
938 }
939
940 void wxTopLevelWindowGTK::DoSetSizeHints( int minW, int minH,
941 int maxW, int maxH,
942 int incW, int incH )
943 {
944 wxTopLevelWindowBase::DoSetSizeHints( minW, minH, maxW, maxH, incW, incH );
945
946 const wxSize minSize = GetMinSize();
947 const wxSize maxSize = GetMaxSize();
948 GdkGeometry hints;
949 int hints_mask = 0;
950 if (minSize.x > 0 || minSize.y > 0)
951 {
952 hints_mask |= GDK_HINT_MIN_SIZE;
953 hints.min_width = minSize.x - m_decorSize.x;
954 if (hints.min_width < 0)
955 hints.min_width = 0;
956 hints.min_height = minSize.y - m_decorSize.y;
957 if (hints.min_height < 0)
958 hints.min_height = 0;
959 }
960 if (maxSize.x > 0 || maxSize.y > 0)
961 {
962 hints_mask |= GDK_HINT_MAX_SIZE;
963 hints.max_width = maxSize.x - m_decorSize.x;
964 if (hints.max_width < 0)
965 hints.max_width = INT_MAX;
966 hints.max_height = maxSize.y - m_decorSize.y;
967 if (hints.max_height < 0)
968 hints.max_height = INT_MAX;
969 }
970 if (incW > 0 || incH > 0)
971 {
972 hints_mask |= GDK_HINT_RESIZE_INC;
973 hints.width_inc = incW > 0 ? incW : 1;
974 hints.height_inc = incH > 0 ? incH : 1;
975 }
976 gtk_window_set_geometry_hints(
977 (GtkWindow*)m_widget, NULL, &hints, (GdkWindowHints)hints_mask);
978 }
979
980 void wxTopLevelWindowGTK::GTKUpdateDecorSize(const wxSize& decorSize)
981 {
982 if (m_decorSize != decorSize)
983 {
984 const wxSize diff = decorSize - m_decorSize;
985 m_decorSize = decorSize;
986 bool resized = false;
987 if (m_deferShow)
988 {
989 // keep overall size unchanged by shrinking m_widget
990 int w, h;
991 GTKDoGetSize(&w, &h);
992 // but not if size would be less than minimum, it won't take effect
993 const wxSize minSize = GetMinSize();
994 if (w >= minSize.x && h >= minSize.y)
995 {
996 gtk_window_resize(GTK_WINDOW(m_widget), w, h);
997 resized = true;
998 }
999 }
1000 if (!resized)
1001 {
1002 // adjust overall size to match change in frame extents
1003 m_width += diff.x;
1004 m_height += diff.y;
1005 if (m_width < 0) m_width = 0;
1006 if (m_height < 0) m_height = 0;
1007 if (!m_deferShow)
1008 {
1009 m_oldClientWidth = 0;
1010 gtk_widget_queue_resize(m_wxwindow);
1011 }
1012 }
1013 }
1014 if (m_deferShow)
1015 {
1016 // gtk_widget_show() was deferred, do it now
1017 m_deferShow = false;
1018 GetClientSize(&m_oldClientWidth, &m_oldClientHeight);
1019 wxSizeEvent sizeEvent(GetSize(), GetId());
1020 sizeEvent.SetEventObject(this);
1021 HandleWindowEvent(sizeEvent);
1022
1023 gtk_widget_show(m_widget);
1024
1025 wxShowEvent showEvent(GetId(), true);
1026 showEvent.SetEventObject(this);
1027 HandleWindowEvent(showEvent);
1028 }
1029 }
1030
1031 void wxTopLevelWindowGTK::OnInternalIdle()
1032 {
1033 wxWindow::OnInternalIdle();
1034
1035 // Synthetize activate events.
1036 if ( g_sendActivateEvent != -1 )
1037 {
1038 bool activate = g_sendActivateEvent != 0;
1039
1040 // if (!activate) wxPrintf( wxT("de") );
1041 // wxPrintf( wxT("activate\n") );
1042
1043 // do it only once
1044 g_sendActivateEvent = -1;
1045
1046 wxTheApp->SetActive(activate, (wxWindow *)g_lastActiveFrame);
1047 }
1048 }
1049
1050 // ----------------------------------------------------------------------------
1051 // frame title/icon
1052 // ----------------------------------------------------------------------------
1053
1054 void wxTopLevelWindowGTK::SetTitle( const wxString &title )
1055 {
1056 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
1057
1058 if ( title == m_title )
1059 return;
1060
1061 m_title = title;
1062
1063 gtk_window_set_title( GTK_WINDOW(m_widget), wxGTK_CONV( title ) );
1064 }
1065
1066 void wxTopLevelWindowGTK::SetIcons( const wxIconBundle &icons )
1067 {
1068 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
1069
1070 wxTopLevelWindowBase::SetIcons( icons );
1071
1072 GList *list = NULL;
1073
1074 const size_t numIcons = icons.GetIconCount();
1075 for ( size_t i = 0; i < numIcons; i++ )
1076 {
1077 list = g_list_prepend(list, icons.GetIconByIndex(i).GetPixbuf());
1078 }
1079
1080 gtk_window_set_icon_list(GTK_WINDOW(m_widget), list);
1081 g_list_free(list);
1082 }
1083
1084 // ----------------------------------------------------------------------------
1085 // frame state: maximized/iconized/normal
1086 // ----------------------------------------------------------------------------
1087
1088 void wxTopLevelWindowGTK::Maximize(bool maximize)
1089 {
1090 if (maximize)
1091 gtk_window_maximize( GTK_WINDOW( m_widget ) );
1092 else
1093 gtk_window_unmaximize( GTK_WINDOW( m_widget ) );
1094 }
1095
1096 bool wxTopLevelWindowGTK::IsMaximized() const
1097 {
1098 if(!m_widget->window)
1099 return false;
1100
1101 return gdk_window_get_state(m_widget->window) & GDK_WINDOW_STATE_MAXIMIZED;
1102 }
1103
1104 void wxTopLevelWindowGTK::Restore()
1105 {
1106 // "Present" seems similar enough to "restore"
1107 gtk_window_present( GTK_WINDOW( m_widget ) );
1108 }
1109
1110 void wxTopLevelWindowGTK::Iconize( bool iconize )
1111 {
1112 if (iconize)
1113 gtk_window_iconify( GTK_WINDOW( m_widget ) );
1114 else
1115 gtk_window_deiconify( GTK_WINDOW( m_widget ) );
1116 }
1117
1118 bool wxTopLevelWindowGTK::IsIconized() const
1119 {
1120 return m_isIconized;
1121 }
1122
1123 void wxTopLevelWindowGTK::SetIconizeState(bool iconize)
1124 {
1125 if ( iconize != m_isIconized )
1126 {
1127 m_isIconized = iconize;
1128 (void)SendIconizeEvent(iconize);
1129 }
1130 }
1131
1132 void wxTopLevelWindowGTK::AddGrab()
1133 {
1134 if (!m_grabbed)
1135 {
1136 m_grabbed = true;
1137 gtk_grab_add( m_widget );
1138 wxGUIEventLoop().Run();
1139 gtk_grab_remove( m_widget );
1140 }
1141 }
1142
1143 void wxTopLevelWindowGTK::RemoveGrab()
1144 {
1145 if (m_grabbed)
1146 {
1147 gtk_main_quit();
1148 m_grabbed = false;
1149 }
1150 }
1151
1152
1153 // helper
1154 static bool do_shape_combine_region(GdkWindow* window, const wxRegion& region)
1155 {
1156 if (window)
1157 {
1158 if (region.IsEmpty())
1159 {
1160 gdk_window_shape_combine_mask(window, NULL, 0, 0);
1161 }
1162 else
1163 {
1164 gdk_window_shape_combine_region(window, region.GetRegion(), 0, 0);
1165 return true;
1166 }
1167 }
1168 return false;
1169 }
1170
1171
1172 bool wxTopLevelWindowGTK::SetShape(const wxRegion& region)
1173 {
1174 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
1175 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1176
1177 GdkWindow *window = NULL;
1178 if (m_wxwindow)
1179 {
1180 do_shape_combine_region(m_wxwindow->window, region);
1181 }
1182 window = m_widget->window;
1183 return do_shape_combine_region(window, region);
1184 }
1185
1186 bool wxTopLevelWindowGTK::IsActive()
1187 {
1188 return (this == (wxTopLevelWindowGTK*)g_activeFrame);
1189 }
1190
1191 void wxTopLevelWindowGTK::RequestUserAttention(int flags)
1192 {
1193 bool new_hint_value = false;
1194
1195 // FIXME: This is a workaround to focus handling problem
1196 // If RequestUserAttention is called for example right after a wxSleep, OnInternalIdle hasn't
1197 // yet been processed, and the internal focus system is not up to date yet.
1198 // wxYieldIfNeeded ensures the processing of it, but can have unwanted side effects - MR
1199 ::wxYieldIfNeeded();
1200
1201 if(m_urgency_hint >= 0)
1202 g_source_remove(m_urgency_hint);
1203
1204 m_urgency_hint = -2;
1205
1206 if( GTK_WIDGET_REALIZED(m_widget) && !IsActive() )
1207 {
1208 new_hint_value = true;
1209
1210 if (flags & wxUSER_ATTENTION_INFO)
1211 {
1212 m_urgency_hint = g_timeout_add(5000, (GSourceFunc)gtk_frame_urgency_timer_callback, this);
1213 } else {
1214 m_urgency_hint = -1;
1215 }
1216 }
1217
1218 #if GTK_CHECK_VERSION(2,7,0)
1219 if(!gtk_check_version(2,7,0))
1220 gtk_window_set_urgency_hint(GTK_WINDOW( m_widget ), new_hint_value);
1221 else
1222 #endif
1223 wxgtk_window_set_urgency_hint(GTK_WINDOW( m_widget ), new_hint_value);
1224 }
1225
1226 void wxTopLevelWindowGTK::SetWindowStyleFlag( long style )
1227 {
1228 // Store which styles were changed
1229 long styleChanges = style ^ m_windowStyle;
1230
1231 // Process wxWindow styles. This also updates the internal variable
1232 // Therefore m_windowStyle bits carry now the _new_ style values
1233 wxWindow::SetWindowStyleFlag(style);
1234
1235 // just return for now if widget does not exist yet
1236 if (!m_widget)
1237 return;
1238
1239 if ( styleChanges & wxSTAY_ON_TOP )
1240 {
1241 gtk_window_set_keep_above(GTK_WINDOW(m_widget),
1242 m_windowStyle & wxSTAY_ON_TOP);
1243 }
1244
1245 if ( styleChanges & wxFRAME_NO_TASKBAR )
1246 {
1247 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(m_widget),
1248 m_windowStyle & wxFRAME_NO_TASKBAR);
1249 }
1250 }
1251
1252 /* Get the X Window between child and the root window.
1253 This should usually be the WM managed XID */
1254 static Window wxGetTopmostWindowX11(Display *dpy, Window child)
1255 {
1256 Window root, parent;
1257 Window* children;
1258 unsigned int nchildren;
1259
1260 XQueryTree(dpy, child, &root, &parent, &children, &nchildren);
1261 XFree(children);
1262
1263 while (parent != root) {
1264 child = parent;
1265 XQueryTree(dpy, child, &root, &parent, &children, &nchildren);
1266 XFree(children);
1267 }
1268
1269 return child;
1270 }
1271
1272 bool wxTopLevelWindowGTK::SetTransparent(wxByte alpha)
1273 {
1274 if (!m_widget || !m_widget->window)
1275 return false;
1276
1277 Display* dpy = GDK_WINDOW_XDISPLAY (m_widget->window);
1278 // We need to get the X Window that has the root window as the immediate parent
1279 // and m_widget->window as a child. This should be the X Window that the WM manages and
1280 // from which the opacity property is checked from.
1281 Window win = wxGetTopmostWindowX11(dpy, GDK_WINDOW_XID (m_widget->window));
1282
1283
1284 // Using pure Xlib to not have a GTK version check mess due to gtk2.0 not having GdkDisplay
1285 if (alpha == 0xff)
1286 XDeleteProperty(dpy, win, XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", False));
1287 else
1288 {
1289 long opacity = alpha * 0x1010101L;
1290 XChangeProperty(dpy, win, XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", False),
1291 XA_CARDINAL, 32, PropModeReplace,
1292 (unsigned char *) &opacity, 1L);
1293 }
1294 XSync(dpy, False);
1295 return true;
1296 }
1297
1298 bool wxTopLevelWindowGTK::CanSetTransparent()
1299 {
1300 // allow to override automatic detection as it's far from perfect
1301 static const wxChar *SYSOPT_TRANSPARENT = wxT("gtk.tlw.can-set-transparent");
1302 if ( wxSystemOptions::HasOption(SYSOPT_TRANSPARENT) )
1303 {
1304 return wxSystemOptions::GetOptionInt(SYSOPT_TRANSPARENT) != 0;
1305 }
1306
1307 #if GTK_CHECK_VERSION(2,10,0)
1308 if (!gtk_check_version(2,10,0))
1309 {
1310 return (gtk_widget_is_composited (m_widget));
1311 }
1312 else
1313 #endif // In case of lower versions than gtk+-2.10.0 we could look for _NET_WM_CM_Sn ourselves
1314 {
1315 return false;
1316 }
1317
1318 #if 0 // Don't be optimistic here for the sake of wxAUI
1319 int opcode, event, error;
1320 // Check for the existence of a RGBA visual instead?
1321 return XQueryExtension(gdk_x11_get_default_xdisplay (),
1322 "Composite", &opcode, &event, &error);
1323 #endif
1324 }
1325
1326 void wxTopLevelWindowGTK::OnSysColourChanged(wxSysColourChangedEvent& event)
1327 {
1328 // We don't know the order in which top-level windows will
1329 // be notified, so we need to clear the system objects
1330 // for each top-level window.
1331 extern void wxClearGtkSystemObjects();
1332 wxClearGtkSystemObjects();
1333
1334 // wxWindowBase::OnSysColourChanged will propagate event
1335 // to children
1336 event.Skip();
1337 }