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 bool wxMWMIsRunning(Window w
);
48 // ----------------------------------------------------------------------------
49 // wxTopLevelWindowX11 creation
50 // ----------------------------------------------------------------------------
52 void wxTopLevelWindowX11::Init()
55 m_maximizeOnShow
= FALSE
;
57 // unlike (almost?) all other windows, frames are created hidden
60 // Data to save/restore when calling ShowFullScreen
62 m_fsIsMaximized
= FALSE
;
63 m_fsIsShowing
= FALSE
;
68 bool wxTopLevelWindowX11::Create(wxWindow
*parent
,
70 const wxString
& title
,
79 m_windowStyle
= style
;
84 m_windowId
= id
== -1 ? NewControlId() : id
;
87 parent
->AddChild(this);
89 wxTopLevelWindows
.Append(this);
91 Display
*xdisplay
= wxGlobalDisplay();
92 int xscreen
= DefaultScreen( xdisplay
);
93 Visual
*xvisual
= DefaultVisual( xdisplay
, xscreen
);
94 Window xparent
= RootWindow( xdisplay
, xscreen
);
95 Colormap cm
= DefaultColormap( xdisplay
, xscreen
);
97 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
98 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
100 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
101 m_backgroundColour
.CalcPixel( (WXColormap
) cm
);
117 XSetWindowAttributes xattributes
;
118 XSizeHints size_hints
;
120 long xattributes_mask
=
122 CWBorderPixel
| CWBackPixel
;
124 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
125 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
127 // TODO: if we want no border, caption etc.,
128 // I think we set this to True to remove decorations
130 xattributes
.override_redirect
= False
;
134 long backColor
, foreColor
;
135 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
136 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
138 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
139 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
141 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
142 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
144 m_mainWidget
= (WXWindow
) xwindow
;
148 extraFlags
|= GR_EVENT_MASK_CLOSE_REQ
;
151 XSelectInput( xdisplay
, xwindow
,
165 StructureNotifyMask
|
169 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
171 // Set background to None which will prevent X11 from clearing the
172 // background completely.
173 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
176 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
178 if (GetParent() && GetParent()->GetMainWindow())
180 Window xparentwindow
= (Window
) GetParent()->GetMainWindow();
181 XSetTransientForHint( xdisplay
, xwindow
, xparentwindow
);
185 size_hints
.flags
= PSize
| PPosition
;
186 size_hints
.x
= pos2
.x
;
187 size_hints
.y
= pos2
.y
;
188 size_hints
.width
= size2
.x
;
189 size_hints
.height
= size2
.y
;
190 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
193 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
194 wm_hints
.input
= True
;
195 wm_hints
.initial_state
= NormalState
;
196 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
198 Atom wm_protocols
[2];
199 wm_protocols
[0] = XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
200 wm_protocols
[1] = XInternAtom( xdisplay
, "WM_TAKE_FOCUS", False
);
201 XSetWMProtocols( xdisplay
, xwindow
, wm_protocols
, 2);
204 wxSetWMDecorations( xwindow
, style
);
211 wxTopLevelWindowX11::~wxTopLevelWindowX11()
213 wxTopLevelWindows
.DeleteObject(this);
215 // If this is the last top-level window, exit.
216 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
218 wxTheApp
->SetTopWindow(NULL
);
220 if (wxTheApp
->GetExitOnFrameDelete())
222 // Signal to the app that we're going to close
223 wxTheApp
->ExitMainLoop();
228 // ----------------------------------------------------------------------------
229 // wxTopLevelWindowX11 showing
230 // ----------------------------------------------------------------------------
232 bool wxTopLevelWindowX11::Show(bool show
)
234 return wxWindowX11::Show(show
);
237 // ----------------------------------------------------------------------------
238 // wxTopLevelWindowX11 maximize/minimize
239 // ----------------------------------------------------------------------------
241 void wxTopLevelWindowX11::Maximize(bool maximize
)
246 bool wxTopLevelWindowX11::IsMaximized() const
252 void wxTopLevelWindowX11::Iconize(bool iconize
)
254 if (!m_iconized
&& GetMainWindow())
256 if (XIconifyWindow(wxGlobalDisplay(),
257 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
262 bool wxTopLevelWindowX11::IsIconized() const
267 void wxTopLevelWindowX11::Restore()
269 // This is the way to deiconify the window, according to the X FAQ
270 if (m_iconized
&& GetMainWindow())
272 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
277 // ----------------------------------------------------------------------------
278 // wxTopLevelWindowX11 fullscreen
279 // ----------------------------------------------------------------------------
281 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
288 m_fsIsShowing
= TRUE
;
300 m_fsIsShowing
= FALSE
;
307 // ----------------------------------------------------------------------------
308 // wxTopLevelWindowX11 misc
309 // ----------------------------------------------------------------------------
311 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
314 wxTopLevelWindowBase::SetIcon(icon
);
316 if (icon
.Ok() && GetMainWindow())
320 XWMHints
*wmHints
= XAllocWMHints();
321 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
323 wmHints
->flags
= IconPixmapHint
;
327 wmHints
->flags
|= IconMaskHint
;
328 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
331 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
337 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
342 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
343 (const char*) title
);
344 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
345 (const char*) title
);
347 // Use this if the platform doesn't supply the above functions.
349 XTextProperty textProperty
;
350 textProperty
.value
= (unsigned char*) title
;
351 textProperty
.encoding
= XA_STRING
;
352 textProperty
.format
= 8;
353 textProperty
.nitems
= 1;
355 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
356 & textProperty
, WM_NAME
);
361 wxString
wxTopLevelWindowX11::GetTitle() const
366 #ifndef MWM_DECOR_BORDER
367 /* bit definitions for MwmHints.flags */
368 #define MWM_HINTS_FUNCTIONS (1L << 0)
369 #define MWM_HINTS_DECORATIONS (1L << 1)
370 #define MWM_HINTS_INPUT_MODE (1L << 2)
371 #define MWM_HINTS_STATUS (1L << 3)
373 #define MWM_DECOR_ALL (1L << 0)
374 #define MWM_DECOR_BORDER (1L << 1)
375 #define MWM_DECOR_RESIZEH (1L << 2)
376 #define MWM_DECOR_TITLE (1L << 3)
377 #define MWM_DECOR_MENU (1L << 4)
378 #define MWM_DECOR_MINIMIZE (1L << 5)
379 #define MWM_DECOR_MAXIMIZE (1L << 6)
389 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
391 // Set the window manager decorations according to the
392 // given wxWindows style
393 bool wxSetWMDecorations(Window w
, long style
)
396 GR_WM_PROPERTIES wmProp
;
401 if (style
& wxRESIZE_BORDER
)
403 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
404 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
407 if (style
& wxSYSTEM_MENU
)
409 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
410 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
413 if ((style
& wxCAPTION
) ||
414 (style
& wxTINY_CAPTION_HORIZ
) ||
415 (style
& wxTINY_CAPTION_VERT
))
417 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
418 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
420 // The default dialog style doesn't include any kind
421 // of border, which is a bit odd. Anyway, inclusion
422 // of a caption surely implies a border.
423 style
|= wxTHICK_FRAME
;
426 if (style
& wxTHICK_FRAME
)
428 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
429 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
432 if (style
& wxSIMPLE_BORDER
)
434 wmProp
.props
|= GR_WM_PROPS_BORDER
;
435 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
438 if (style
& wxMINIMIZE_BOX
)
442 if (style
& wxMAXIMIZE_BOX
)
444 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
445 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
448 if (((style
& wxBORDER
) != wxBORDER
) && ((style
& wxTHICK_FRAME
) != wxTHICK_FRAME
)
449 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
451 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
452 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
455 GrSetWMProperties(w
, & wmProp
);
458 if (!wxMWMIsRunning(w
))
461 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
464 hints
.decorations
= 0;
466 if (style
& wxRESIZE_BORDER
)
468 // wxLogDebug("MWM_DECOR_RESIZEH");
469 hints
.flags
|= MWM_HINTS_DECORATIONS
;
470 hints
.decorations
|= MWM_DECOR_RESIZEH
;
473 if (style
& wxSYSTEM_MENU
)
475 // wxLogDebug("MWM_DECOR_MENU");
476 hints
.flags
|= MWM_HINTS_DECORATIONS
;
477 hints
.decorations
|= MWM_DECOR_MENU
;
480 if ((style
& wxCAPTION
) ||
481 (style
& wxTINY_CAPTION_HORIZ
) ||
482 (style
& wxTINY_CAPTION_VERT
))
484 // wxLogDebug("MWM_DECOR_TITLE");
485 hints
.flags
|= MWM_HINTS_DECORATIONS
;
486 hints
.decorations
|= MWM_DECOR_TITLE
;
489 if ((style
& wxTHICK_FRAME
) || (style
& wxSIMPLE_BORDER
) || (style
& wxCAPTION
))
491 // wxLogDebug("MWM_DECOR_BORDER");
492 hints
.flags
|= MWM_HINTS_DECORATIONS
;
493 hints
.decorations
|= MWM_DECOR_BORDER
;
496 if (style
& wxMINIMIZE_BOX
)
498 // wxLogDebug("MWM_DECOR_MINIMIZE");
499 hints
.flags
|= MWM_HINTS_DECORATIONS
;
500 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
503 if (style
& wxMAXIMIZE_BOX
)
505 // wxLogDebug("MWM_DECOR_MAXIMIZE");
506 hints
.flags
|= MWM_HINTS_DECORATIONS
;
507 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
510 XChangeProperty(wxGlobalDisplay(),
512 mwm_wm_hints
, mwm_wm_hints
,
514 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
520 bool wxMWMIsRunning(Window w
)
525 Display
*dpy
= (Display
*)wxGetDisplay();
526 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
528 unsigned long length
, bytesafter
;
529 unsigned char value
[20];
530 unsigned char *ptr
= &value
[0];
534 type
= format
= length
= 0;
537 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
538 0L, 2, False
, motifWmInfo
,
539 &type
, &format
, &length
, &bytesafter
, &ptr
);
541 return (ret
== Success
);
545 // For implementation purposes - sometimes decorations make the client area
547 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
549 // In fact wxFrame::GetClientAreaOrigin
550 // does the required calculation already.
552 if (this->IsKindOf(CLASSINFO(wxFrame
)))
554 wxFrame
* frame
= (wxFrame
*) this;
555 if (frame
->GetMenuBar())
556 return wxPoint(0, frame
->GetMenuBar()->GetSize().y
);
559 return wxPoint(0, 0);
562 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
564 wxWindowX11::DoGetClientSize(width
, height
);
565 // Done by wxTopLevelWindow
567 if (this->IsKindOf(CLASSINFO(wxFrame
)))
569 wxFrame
* frame
= (wxFrame
*) this;
570 if (frame
->GetMenuBar())
571 (*height
) -= frame
->GetMenuBar()->GetSize().y
;
572 if (frame
->GetStatusBar())
573 (*height
) -= frame
->GetStatusBar()->GetSize().y
;
578 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
580 wxWindowX11::DoSetClientSize(width
, height
);
583 if (!GetMainWindow())
586 XWindowChanges windowChanges
;
591 windowChanges
.width
= width
;
592 valueMask
|= CWWidth
;
596 windowChanges
.height
= height
;
597 valueMask
|= CWHeight
;
599 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
600 valueMask
, & windowChanges
);
604 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
606 // wxLogDebug( "Setting pos: %d, %d", x, y );
607 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
609 wxPoint pt
= GetPosition();
610 // wxLogDebug( "After, pos: %d, %d", pt.x, pt.y );
612 XSync(wxGlobalDisplay(), False
);
616 msg
.Printf("Before setting size: %d, %d", w
, h
);
618 if (!GetMainWindow())
621 XWindowChanges windowChanges
;
624 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
627 AdjustForParentClientOrigin( x
, yy
, sizeFlags
);
631 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
634 AdjustForParentClientOrigin( xx
, y
, sizeFlags
);
638 if (width
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
640 windowChanges
.width
= width
/* - m_borderSize*2 */;
641 valueMask
|= CWWidth
;
643 if (height
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
645 windowChanges
.height
= height
/* -m_borderSize*2*/;
646 valueMask
|= CWHeight
;
649 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
650 valueMask
, & windowChanges
);
651 XSync(wxGlobalDisplay(), False
);
653 msg
.Printf("Tried to set to %d, %d. After setting size: %d, %d", width
, height
, w
, h
);
658 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
660 XSync(wxGlobalDisplay(), False
);
661 Window window
= (Window
) m_mainWidget
;
668 // wxLogDebug("Translating...");
670 XTranslateCoordinates(wxGlobalDisplay(), window
, XDefaultRootWindow(wxGlobalDisplay()),
671 0, 0, & offsetX
, & offsetY
, & childWindow
);
673 // wxLogDebug("Offset: %d, %d", offsetX, offsetY);
676 XWindowAttributes attr
;
677 Status status
= XGetWindowAttributes(wxGlobalDisplay(), window
, & attr
);
682 *x
= attr
.x
+ offsetX
;
683 *y
= attr
.y
+ offsetY
;