1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: x11/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for X11
4 // Author: Julian Smart
8 // Copyright: (c) 2002 Julian Smart
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "toplevel.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/toplevel.h"
34 #include "wx/string.h"
39 #include "wx/statusbr.h"
42 #include "wx/settings.h"
43 #include "wx/x11/private.h"
44 #include "X11/Xutil.h"
46 #include "wx/unix/utilsx11.h"
48 bool wxMWMIsRunning(Window w
);
50 // ----------------------------------------------------------------------------
51 // wxTopLevelWindowX11 creation
52 // ----------------------------------------------------------------------------
54 void wxTopLevelWindowX11::Init()
57 m_maximizeOnShow
= FALSE
;
59 // unlike (almost?) all other windows, frames are created hidden
62 // Data to save/restore when calling ShowFullScreen
64 m_fsIsMaximized
= FALSE
;
65 m_fsIsShowing
= FALSE
;
67 m_needResizeInIdle
= FALSE
;
75 bool wxTopLevelWindowX11::Create(wxWindow
*parent
,
77 const wxString
& title
,
86 m_windowStyle
= style
;
91 m_windowId
= id
== -1 ? NewControlId() : id
;
94 parent
->AddChild(this);
96 wxTopLevelWindows
.Append(this);
98 Display
*xdisplay
= wxGlobalDisplay();
99 int xscreen
= DefaultScreen( xdisplay
);
100 Visual
*xvisual
= DefaultVisual( xdisplay
, xscreen
);
101 Window xparent
= RootWindow( xdisplay
, xscreen
);
102 Colormap cm
= DefaultColormap( xdisplay
, xscreen
);
104 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
105 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
107 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
108 m_backgroundColour
.CalcPixel( (WXColormap
) cm
);
128 XSetWindowAttributes xattributes
;
130 long xattributes_mask
=
131 CWBorderPixel
| CWBackPixel
;
133 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
134 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
136 if (HasFlag( wxNO_BORDER
))
138 xattributes_mask
|= CWOverrideRedirect
;
139 xattributes
.override_redirect
= True
;
142 if (HasFlag( wxNO_FULL_REPAINT_ON_RESIZE
))
144 xattributes_mask
|= CWBitGravity
;
145 xattributes
.bit_gravity
= NorthWestGravity
;
148 xattributes_mask
|= CWEventMask
;
149 xattributes
.event_mask
=
150 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
151 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
152 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
155 Window xwindow
= XCreateWindow( xdisplay
, xparent
, m_x
, m_y
, m_width
, m_height
,
156 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
158 long backColor
, foreColor
;
159 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
160 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
162 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, m_x
, m_y
, m_width
, m_height
,
163 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
166 m_mainWindow
= (WXWindow
) xwindow
;
167 m_clientWindow
= (WXWindow
) xwindow
;
168 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
171 XSelectInput( xdisplay
, xwindow
,
172 GR_EVENT_MASK_CLOSE_REQ
|
185 StructureNotifyMask
|
190 // Set background to None which will prevent X11 from clearing the
191 // background completely.
192 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
195 if (HasFlag( wxSTAY_ON_TOP
))
197 Window xroot
= RootWindow( xdisplay
, xscreen
);
198 XSetTransientForHint( xdisplay
, xwindow
, xroot
);
202 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
204 if (GetParent() && GetParent()->GetMainWindow())
206 Window xparentwindow
= (Window
) GetParent()->GetMainWindow();
207 XSetTransientForHint( xdisplay
, xwindow
, xparentwindow
);
212 XSizeHints size_hints
;
213 size_hints
.flags
= PSize
| PPosition
| PWinGravity
;
216 size_hints
.width
= m_width
;
217 size_hints
.height
= m_height
;
218 size_hints
.win_gravity
= NorthWestGravity
;
219 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
222 wm_hints
.flags
= InputHint
| StateHint
;
225 wm_hints
.flags
|= WindowGroupHint
;
226 wm_hints
.window_group
= (Window
) GetParent()->GetMainWindow();
228 wm_hints
.input
= True
;
229 wm_hints
.initial_state
= NormalState
;
230 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
232 Atom wm_protocols
[2];
233 wm_protocols
[0] = XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
234 wm_protocols
[1] = XInternAtom( xdisplay
, "WM_TAKE_FOCUS", False
);
235 XSetWMProtocols( xdisplay
, xwindow
, wm_protocols
, 2);
239 wxSetWMDecorations( xwindow
, style
);
246 wxTopLevelWindowX11::~wxTopLevelWindowX11()
248 wxTopLevelWindows
.DeleteObject(this);
250 // If this is the last top-level window, exit.
251 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
253 wxTheApp
->SetTopWindow(NULL
);
255 if (wxTheApp
->GetExitOnFrameDelete())
257 // Signal to the app that we're going to close
258 wxTheApp
->ExitMainLoop();
263 void wxTopLevelWindowX11::OnInternalIdle()
265 wxWindow::OnInternalIdle();
267 if (m_needResizeInIdle
)
269 wxSizeEvent
event( GetClientSize(), GetId() );
270 event
.SetEventObject( this );
271 GetEventHandler()->ProcessEvent( event
);
273 m_needResizeInIdle
= FALSE
;
277 // ----------------------------------------------------------------------------
278 // wxTopLevelWindowX11 showing
279 // ----------------------------------------------------------------------------
281 bool wxTopLevelWindowX11::Show(bool show
)
283 // Nano-X has to force a size event,
284 // else there's no initial size.
288 if (show
&& m_needResizeInIdle
)
291 wxSizeEvent
event(GetSize(), GetId());
292 event
.SetEventObject(this);
293 GetEventHandler()->ProcessEvent(event
);
295 m_needResizeInIdle
= FALSE
;
299 // This does the layout _before_ the
300 // window is shown, else the items are
301 // drawn first at the wrong positions,
302 // then at the correct positions.
309 bool ret
= wxWindowX11::Show(show
);
314 // ----------------------------------------------------------------------------
315 // wxTopLevelWindowX11 maximize/minimize
316 // ----------------------------------------------------------------------------
318 void wxTopLevelWindowX11::Maximize(bool maximize
)
323 bool wxTopLevelWindowX11::IsMaximized() const
329 void wxTopLevelWindowX11::Iconize(bool iconize
)
331 if (!m_iconized
&& GetMainWindow())
333 if (XIconifyWindow(wxGlobalDisplay(),
334 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
339 bool wxTopLevelWindowX11::IsIconized() const
344 void wxTopLevelWindowX11::Restore()
346 // This is the way to deiconify the window, according to the X FAQ
347 if (m_iconized
&& GetMainWindow())
349 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
354 // ----------------------------------------------------------------------------
355 // wxTopLevelWindowX11 fullscreen
356 // ----------------------------------------------------------------------------
358 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
365 m_fsIsShowing
= TRUE
;
377 m_fsIsShowing
= FALSE
;
384 // ----------------------------------------------------------------------------
385 // wxTopLevelWindowX11 misc
386 // ----------------------------------------------------------------------------
388 void wxTopLevelWindowX11::DoSetIcon(const wxIcon
& icon
)
390 if (icon
.Ok() && GetMainWindow())
394 XWMHints
*wmHints
= XAllocWMHints();
395 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
397 wmHints
->flags
= IconPixmapHint
;
401 wmHints
->flags
|= IconMaskHint
;
402 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
405 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
411 void wxTopLevelWindowX11::SetIcons(const wxIconBundle
& icons
)
414 wxTopLevelWindowBase::SetIcons( icons
);
416 DoSetIcon( icons
.GetIcon( -1 ) );
417 wxSetIconsX11( wxGlobalDisplay(), GetMainWindow(), icons
);
420 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
426 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
427 (const char*) title
);
428 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
429 (const char*) title
);
433 wxString
wxTopLevelWindowX11::GetTitle() const
438 // For implementation purposes - sometimes decorations make the client area
440 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
442 // wxFrame::GetClientAreaOrigin
443 // does the required calculation already.
444 return wxPoint(0, 0);
447 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
455 void wxTopLevelWindowX11::DoGetSize( int *width
, int *height
) const
457 // TODO add non-client size
465 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
467 int old_width
= m_width
;
468 int old_height
= m_height
;
473 if (m_width
== old_width
&& m_height
== old_height
)
476 // wxLogDebug("DoSetClientSize: %s (%ld) %dx%d", GetClassInfo()->GetClassName(), GetId(), width, height);
479 XSizeHints size_hints
;
480 size_hints
.flags
= PSize
;
481 size_hints
.width
= width
;
482 size_hints
.height
= height
;
483 XSetWMNormalHints( wxGlobalDisplay(), (Window
) GetMainWindow(), &size_hints
);
486 wxWindowX11::DoSetClientSize(width
, height
);
489 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
493 int old_width
= m_width
;
494 int old_height
= m_height
;
496 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
499 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
502 if (width
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
505 if (height
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
508 if (m_x
== old_x
&& m_y
== old_y
&& m_width
== old_width
&& m_height
== old_height
)
511 // wxLogDebug("DoSetSize: %s (%ld) %d, %d %dx%d", GetClassInfo()->GetClassName(), GetId(), x, y, width, height);
514 XSizeHints size_hints
;
515 size_hints
.flags
= 0;
516 size_hints
.flags
|= PPosition
;
517 size_hints
.flags
|= PSize
;
520 size_hints
.width
= m_width
;
521 size_hints
.height
= m_height
;
522 XSetWMNormalHints( wxGlobalDisplay(), (Window
) GetMainWindow(), &size_hints
);
525 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
528 Display
*display
= wxGlobalDisplay();
529 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
530 Window parent_window
= window
,
531 next_parent
= window
;
533 // search for the parent that is child of ROOT, because the WM may
534 // reparent twice and notify only the next parent (like FVWM)
535 while (next_parent
!= root
) {
542 parent_window
= next_parent
;
543 XQueryTree(display
, parent_window
, &root
,
544 &next_parent
, &theChildren
, &n
);
545 XFree(theChildren
); // not needed
548 XWindowChanges windowChanges
;
551 windowChanges
.width
= width
;
552 windowChanges
.height
= height
;
553 windowChanges
.stack_mode
= 0;
554 int valueMask
= CWX
| CWY
| CWWidth
| CWHeight
;
556 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
560 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
566 windowChanges
.width
= wxMax(1, width
);
567 valueMask
|= CWWidth
;
571 windowChanges
.height
= wxMax(1, height
);
572 valueMask
|= CWHeight
;
575 XConfigureWindow( display
, parent_window
, valueMask
, &windowChanges
);
579 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
581 XSync(wxGlobalDisplay(), False
);
582 Window window
= (Window
) m_mainWindow
;
586 Display
*display
= wxGlobalDisplay();
587 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
588 Window parent_window
= window
,
589 next_parent
= window
;
591 // search for the parent that is child of ROOT, because the WM may
592 // reparent twice and notify only the next parent (like FVWM)
593 while (next_parent
!= root
) {
600 parent_window
= next_parent
;
601 XQueryTree(display
, parent_window
, &root
,
602 &next_parent
, &theChildren
, &n
);
603 XFree(theChildren
); // not needed
606 int xx
, yy
; unsigned int dummy
;
607 XGetGeometry(display
, parent_window
, &root
,
608 &xx
, &yy
, &dummy
, &dummy
, &dummy
, &dummy
);
612 XWindowAttributes attr
;
613 Status status
= XGetWindowAttributes( wxGlobalDisplay(), parent_window
, & attr
);
628 #ifndef MWM_DECOR_BORDER
630 #define MWM_HINTS_FUNCTIONS (1L << 0)
631 #define MWM_HINTS_DECORATIONS (1L << 1)
632 #define MWM_HINTS_INPUT_MODE (1L << 2)
633 #define MWM_HINTS_STATUS (1L << 3)
635 #define MWM_DECOR_ALL (1L << 0)
636 #define MWM_DECOR_BORDER (1L << 1)
637 #define MWM_DECOR_RESIZEH (1L << 2)
638 #define MWM_DECOR_TITLE (1L << 3)
639 #define MWM_DECOR_MENU (1L << 4)
640 #define MWM_DECOR_MINIMIZE (1L << 5)
641 #define MWM_DECOR_MAXIMIZE (1L << 6)
643 #define MWM_FUNC_ALL (1L << 0)
644 #define MWM_FUNC_RESIZE (1L << 1)
645 #define MWM_FUNC_MOVE (1L << 2)
646 #define MWM_FUNC_MINIMIZE (1L << 3)
647 #define MWM_FUNC_MAXIMIZE (1L << 4)
648 #define MWM_FUNC_CLOSE (1L << 5)
650 #define MWM_INPUT_MODELESS 0
651 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
652 #define MWM_INPUT_SYSTEM_MODAL 2
653 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
654 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
656 #define MWM_TEAROFF_WINDOW (1L<<0)
667 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
669 // Set the window manager decorations according to the
670 // given wxWindows style
671 bool wxSetWMDecorations(Window w
, long style
)
674 GR_WM_PROPERTIES wmProp
;
679 if (style
& wxRESIZE_BORDER
)
681 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
682 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
685 if (style
& wxSYSTEM_MENU
)
687 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
688 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
691 if ((style
& wxCAPTION
) ||
692 (style
& wxTINY_CAPTION_HORIZ
) ||
693 (style
& wxTINY_CAPTION_VERT
))
695 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
696 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
698 // The default dialog style doesn't include any kind
699 // of border, which is a bit odd. Anyway, inclusion
700 // of a caption surely implies a border.
701 style
|= wxTHICK_FRAME
;
704 if (style
& wxTHICK_FRAME
)
706 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
707 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
710 if (style
& wxSIMPLE_BORDER
)
712 wmProp
.props
|= GR_WM_PROPS_BORDER
;
713 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
716 if (style
& wxMINIMIZE_BOX
)
720 if (style
& wxMAXIMIZE_BOX
)
722 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
723 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
726 if (((style
& wxBORDER
) != wxBORDER
) && ((style
& wxTHICK_FRAME
) != wxTHICK_FRAME
)
727 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
729 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
730 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
733 GrSetWMProperties(w
, & wmProp
);
737 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
738 if (mwm_wm_hints
== 0)
742 hints
.flags
= MWM_HINTS_DECORATIONS
| MWM_HINTS_FUNCTIONS
;
743 hints
.decorations
= 0;
746 if ((style
& wxSIMPLE_BORDER
) || (style
& wxNO_BORDER
))
752 hints
.decorations
= MWM_DECOR_BORDER
;
753 hints
.functions
= MWM_FUNC_MOVE
;
755 if ((style
& wxCAPTION
) != 0)
756 hints
.decorations
|= MWM_DECOR_TITLE
;
758 if ((style
& wxSYSTEM_MENU
) != 0)
760 hints
.functions
|= MWM_FUNC_CLOSE
;
761 hints
.decorations
|= MWM_DECOR_MENU
;
764 if ((style
& wxMINIMIZE_BOX
) != 0)
766 hints
.functions
|= MWM_FUNC_MINIMIZE
;
767 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
770 if ((style
& wxMAXIMIZE_BOX
) != 0)
772 hints
.functions
|= MWM_FUNC_MAXIMIZE
;
773 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
776 if ((style
& wxRESIZE_BORDER
) != 0)
778 hints
.functions
|= MWM_FUNC_RESIZE
;
779 hints
.decorations
|= MWM_DECOR_RESIZEH
;
783 XChangeProperty(wxGlobalDisplay(),
785 mwm_wm_hints
, mwm_wm_hints
,
787 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);