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
);
105 XSetWindowAttributes xattributes
;
106 XSizeHints size_hints
;
108 long xattributes_mask
=
110 CWBorderPixel
| CWBackPixel
;
112 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
113 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
115 // TODO: if we want no border, caption etc.,
116 // I think we set this to True to remove decorations
118 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
;
152 XSelectInput( xdisplay
, xwindow
,
166 StructureNotifyMask
|
170 XSelectInput( xdisplay
, xwindow
,
184 StructureNotifyMask
|
189 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
191 // Set background to None which will prevent X11 from clearing the
192 // background completely.
193 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
196 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
198 if (GetParent() && GetParent()->GetMainWindow())
200 Window xparentwindow
= (Window
) GetParent()->GetMainWindow();
201 XSetTransientForHint( xdisplay
, xwindow
, xparentwindow
);
205 size_hints
.flags
= PSize
| PPosition
;
206 size_hints
.x
= pos2
.x
;
207 size_hints
.y
= pos2
.y
;
208 size_hints
.width
= size2
.x
;
209 size_hints
.height
= size2
.y
;
210 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
213 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
214 wm_hints
.input
= True
;
215 wm_hints
.initial_state
= NormalState
;
216 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
218 Atom wm_protocols
[2];
219 wm_protocols
[0] = XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
220 wm_protocols
[1] = XInternAtom( xdisplay
, "WM_TAKE_FOCUS", False
);
221 XSetWMProtocols( xdisplay
, xwindow
, wm_protocols
, 2);
225 GR_WM_PROPERTIES props
;
226 props
.flags
= GR_WM_FLAGS_TITLE
;
227 props
.title
= (GR_CHAR
*) "Hello";
228 GrSetWMProperties(xwindow
, &props
);
230 wxSetWMDecorations( xwindow
, style
);
238 wxTopLevelWindowX11
::~wxTopLevelWindowX11()
240 wxTopLevelWindows
.DeleteObject(this);
242 // If this is the last top-level window, exit.
243 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
245 wxTheApp
->SetTopWindow(NULL
);
247 if (wxTheApp
->GetExitOnFrameDelete())
249 // Signal to the app that we're going to close
250 wxTheApp
->ExitMainLoop();
255 // ----------------------------------------------------------------------------
256 // wxTopLevelWindowX11 showing
257 // ----------------------------------------------------------------------------
259 bool wxTopLevelWindowX11
::Show(bool show
)
261 return wxWindowX11
::Show(show
);
264 // ----------------------------------------------------------------------------
265 // wxTopLevelWindowX11 maximize/minimize
266 // ----------------------------------------------------------------------------
268 void wxTopLevelWindowX11
::Maximize(bool maximize
)
273 bool wxTopLevelWindowX11
::IsMaximized() const
279 void wxTopLevelWindowX11
::Iconize(bool iconize
)
281 if (!m_iconized
&& GetMainWindow())
283 if (XIconifyWindow(wxGlobalDisplay(),
284 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
289 bool wxTopLevelWindowX11
::IsIconized() const
294 void wxTopLevelWindowX11
::Restore()
296 // This is the way to deiconify the window, according to the X FAQ
297 if (m_iconized
&& GetMainWindow())
299 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
304 // ----------------------------------------------------------------------------
305 // wxTopLevelWindowX11 fullscreen
306 // ----------------------------------------------------------------------------
308 bool wxTopLevelWindowX11
::ShowFullScreen(bool show
, long style
)
315 m_fsIsShowing
= TRUE
;
327 m_fsIsShowing
= FALSE
;
334 // ----------------------------------------------------------------------------
335 // wxTopLevelWindowX11 misc
336 // ----------------------------------------------------------------------------
338 void wxTopLevelWindowX11
::SetIcon(const wxIcon
& icon
)
341 wxTopLevelWindowBase
::SetIcon(icon
);
343 if (icon
.Ok() && GetMainWindow())
347 XWMHints
*wmHints
= XAllocWMHints();
348 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
350 wmHints
->flags
= IconPixmapHint
;
354 wmHints
->flags
|= IconMaskHint
;
355 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
358 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
364 void wxTopLevelWindowX11
::SetTitle(const wxString
& title
)
369 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
370 (const char*) title
);
371 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
372 (const char*) title
);
374 // Use this if the platform doesn't supply the above functions.
376 XTextProperty textProperty
;
377 textProperty
.value
= (unsigned char*) title
;
378 textProperty
.encoding
= XA_STRING
;
379 textProperty
.format
= 8;
380 textProperty
.nitems
= 1;
382 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
383 & textProperty
, WM_NAME
);
388 wxString wxTopLevelWindowX11
::GetTitle() const
393 #ifndef MWM_DECOR_BORDER
394 /* bit definitions for MwmHints.flags */
395 #define MWM_HINTS_FUNCTIONS (1L << 0)
396 #define MWM_HINTS_DECORATIONS (1L << 1)
397 #define MWM_HINTS_INPUT_MODE (1L << 2)
398 #define MWM_HINTS_STATUS (1L << 3)
400 #define MWM_DECOR_ALL (1L << 0)
401 #define MWM_DECOR_BORDER (1L << 1)
402 #define MWM_DECOR_RESIZEH (1L << 2)
403 #define MWM_DECOR_TITLE (1L << 3)
404 #define MWM_DECOR_MENU (1L << 4)
405 #define MWM_DECOR_MINIMIZE (1L << 5)
406 #define MWM_DECOR_MAXIMIZE (1L << 6)
416 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
418 // Set the window manager decorations according to the
419 // given wxWindows style
420 bool wxSetWMDecorations(Window w
, long style
)
423 GR_WM_PROPERTIES wmProp
;
427 if (style
& wxRESIZE_BORDER
)
429 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
430 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
433 if (style
& wxSYSTEM_MENU
)
435 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
436 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
439 if ((style
& wxCAPTION
) ||
440 (style
& wxTINY_CAPTION_HORIZ
) ||
441 (style
& wxTINY_CAPTION_VERT
))
443 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
444 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
447 if (style
& wxTHICK_FRAME
)
449 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
450 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
453 if (style
& wxSIMPLE_BORDER
)
455 wmProp
.props
|= GR_WM_PROPS_BORDER
;
456 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
459 if (style
& wxMINIMIZE_BOX
)
463 if (style
& wxMAXIMIZE_BOX
)
465 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
466 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
469 if (((style
& wxBORDER
) != wxBORDER
) && ((style
& wxTHICK_FRAME
) != wxTHICK_FRAME
)
470 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
472 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
473 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
476 GrSetWMProperties(w
, & wmProp
);
479 if (!wxMWMIsRunning(w
))
482 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
485 hints
.decorations
= 0;
487 if (style
& wxRESIZE_BORDER
)
489 // wxLogDebug("MWM_DECOR_RESIZEH");
490 hints
.flags
|= MWM_HINTS_DECORATIONS
;
491 hints
.decorations
|= MWM_DECOR_RESIZEH
;
494 if (style
& wxSYSTEM_MENU
)
496 // wxLogDebug("MWM_DECOR_MENU");
497 hints
.flags
|= MWM_HINTS_DECORATIONS
;
498 hints
.decorations
|= MWM_DECOR_MENU
;
501 if ((style
& wxCAPTION
) ||
502 (style
& wxTINY_CAPTION_HORIZ
) ||
503 (style
& wxTINY_CAPTION_VERT
))
505 // wxLogDebug("MWM_DECOR_TITLE");
506 hints
.flags
|= MWM_HINTS_DECORATIONS
;
507 hints
.decorations
|= MWM_DECOR_TITLE
;
510 if ((style
& wxTHICK_FRAME
) || (style
& wxSIMPLE_BORDER
) || (style
& wxCAPTION
))
512 // wxLogDebug("MWM_DECOR_BORDER");
513 hints
.flags
|= MWM_HINTS_DECORATIONS
;
514 hints
.decorations
|= MWM_DECOR_BORDER
;
517 if (style
& wxMINIMIZE_BOX
)
519 // wxLogDebug("MWM_DECOR_MINIMIZE");
520 hints
.flags
|= MWM_HINTS_DECORATIONS
;
521 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
524 if (style
& wxMAXIMIZE_BOX
)
526 // wxLogDebug("MWM_DECOR_MAXIMIZE");
527 hints
.flags
|= MWM_HINTS_DECORATIONS
;
528 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
531 XChangeProperty(wxGlobalDisplay(),
533 mwm_wm_hints
, mwm_wm_hints
,
535 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
541 bool wxMWMIsRunning(Window w
)
546 Display
*dpy
= (Display
*)wxGetDisplay();
547 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
549 unsigned long length
, bytesafter
;
550 unsigned char value
[20];
551 unsigned char *ptr
= &value
[0];
555 type
= format
= length
= 0;
558 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
559 0L, 2, False
, motifWmInfo
,
560 &type
, &format
, &length
, &bytesafter
, &ptr
);
562 return (ret
== Success
);
566 // For implementation purposes - sometimes decorations make the client area
568 wxPoint wxTopLevelWindowX11
::GetClientAreaOrigin() const
570 // In fact wxFrame::GetClientAreaOrigin
571 // does the required calculation already.
573 if (this->IsKindOf(CLASSINFO(wxFrame
)))
575 wxFrame
* frame
= (wxFrame
*) this;
576 if (frame
->GetMenuBar())
577 return wxPoint(0, frame
->GetMenuBar()->GetSize().y
);
580 return wxPoint(0, 0);
583 void wxTopLevelWindowX11
::DoGetClientSize( int *width
, int *height
) const
585 wxWindowX11
::DoGetClientSize(width
, height
);
586 // Done by wxTopLevelWindow
588 if (this->IsKindOf(CLASSINFO(wxFrame
)))
590 wxFrame
* frame
= (wxFrame
*) this;
591 if (frame
->GetMenuBar())
592 (*height
) -= frame
->GetMenuBar()->GetSize().y
;
593 if (frame
->GetStatusBar())
594 (*height
) -= frame
->GetStatusBar()->GetSize().y
;
599 void wxTopLevelWindowX11
::DoSetClientSize(int width
, int height
)
601 wxWindowX11
::DoSetClientSize(width
, height
);
604 if (!GetMainWindow())
607 XWindowChanges windowChanges
;
612 windowChanges
.width
= width
;
613 valueMask
|= CWWidth
;
617 windowChanges
.height
= height
;
618 valueMask
|= CWHeight
;
620 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
621 valueMask
, & windowChanges
);
625 void wxTopLevelWindowX11
::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
627 // wxLogDebug( "Setting pos: %d, %d", x, y );
628 wxWindowX11
::DoSetSize(x
, y
, width
, height
, sizeFlags
);
630 wxPoint pt
= GetPosition();
631 // wxLogDebug( "After, pos: %d, %d", pt.x, pt.y );
633 XSync(wxGlobalDisplay(), False
);
637 msg
.Printf("Before setting size: %d, %d", w
, h
);
639 if (!GetMainWindow())
642 XWindowChanges windowChanges
;
645 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
648 AdjustForParentClientOrigin( x
, yy
, sizeFlags
);
652 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
655 AdjustForParentClientOrigin( xx
, y
, sizeFlags
);
659 if (width
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
661 windowChanges
.width
= width
/* - m_borderSize*2 */;
662 valueMask
|= CWWidth
;
664 if (height
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
666 windowChanges
.height
= height
/* -m_borderSize*2*/;
667 valueMask
|= CWHeight
;
670 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
671 valueMask
, & windowChanges
);
672 XSync(wxGlobalDisplay(), False
);
674 msg
.Printf("Tried to set to %d, %d. After setting size: %d, %d", width
, height
, w
, h
);
679 void wxTopLevelWindowX11
::DoGetPosition(int *x
, int *y
) const
681 XSync(wxGlobalDisplay(), False
);
682 Window window
= (Window
) m_mainWidget
;
689 // wxLogDebug("Translating...");
691 XTranslateCoordinates(wxGlobalDisplay(), window
, XDefaultRootWindow(wxGlobalDisplay()),
692 0, 0, & offsetX
, & offsetY
, & childWindow
);
694 // wxLogDebug("Offset: %d, %d", offsetX, offsetY);
697 XWindowAttributes attr
;
698 Status status
= XGetWindowAttributes(wxGlobalDisplay(), window
, & attr
);
703 *x
= attr
.x
+ offsetX
;
704 *y
= attr
.y
+ offsetY
;