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