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