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