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