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
;
151 XSelectInput( xdisplay
, xwindow
,
152 extraFlags
| ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
153 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
154 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
155 PropertyChangeMask
);
157 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
159 // Set background to None which will prevent X11 from clearing the
160 // background completely.
161 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
164 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
166 if (GetParent() && GetParent()->GetMainWindow())
168 Window xparentwindow
= (Window
) GetParent()->GetMainWindow();
169 XSetTransientForHint( xdisplay
, xwindow
, xparentwindow
);
173 size_hints
.flags
= PSize
| PPosition
;
174 size_hints
.x
= pos2
.x
;
175 size_hints
.y
= pos2
.y
;
176 size_hints
.width
= size2
.x
;
177 size_hints
.height
= size2
.y
;
178 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
181 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
182 wm_hints
.input
= True
;
183 wm_hints
.initial_state
= NormalState
;
184 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
186 Atom wm_protocols
[2];
187 wm_protocols
[0] = XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
188 wm_protocols
[1] = XInternAtom( xdisplay
, "WM_TAKE_FOCUS", False
);
189 XSetWMProtocols( xdisplay
, xwindow
, wm_protocols
, 2);
192 wxSetWMDecorations( xwindow
, style
);
199 wxTopLevelWindowX11::~wxTopLevelWindowX11()
201 wxTopLevelWindows
.DeleteObject(this);
203 // If this is the last top-level window, exit.
204 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
206 wxTheApp
->SetTopWindow(NULL
);
208 if (wxTheApp
->GetExitOnFrameDelete())
210 // Signal to the app that we're going to close
211 wxTheApp
->ExitMainLoop();
216 // ----------------------------------------------------------------------------
217 // wxTopLevelWindowX11 showing
218 // ----------------------------------------------------------------------------
220 bool wxTopLevelWindowX11::Show(bool show
)
222 return wxWindowX11::Show(show
);
225 // ----------------------------------------------------------------------------
226 // wxTopLevelWindowX11 maximize/minimize
227 // ----------------------------------------------------------------------------
229 void wxTopLevelWindowX11::Maximize(bool maximize
)
234 bool wxTopLevelWindowX11::IsMaximized() const
240 void wxTopLevelWindowX11::Iconize(bool iconize
)
242 if (!m_iconized
&& GetMainWindow())
244 if (XIconifyWindow(wxGlobalDisplay(),
245 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
250 bool wxTopLevelWindowX11::IsIconized() const
255 void wxTopLevelWindowX11::Restore()
257 // This is the way to deiconify the window, according to the X FAQ
258 if (m_iconized
&& GetMainWindow())
260 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
265 // ----------------------------------------------------------------------------
266 // wxTopLevelWindowX11 fullscreen
267 // ----------------------------------------------------------------------------
269 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
276 m_fsIsShowing
= TRUE
;
288 m_fsIsShowing
= FALSE
;
295 // ----------------------------------------------------------------------------
296 // wxTopLevelWindowX11 misc
297 // ----------------------------------------------------------------------------
299 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
302 wxTopLevelWindowBase::SetIcon(icon
);
304 if (icon
.Ok() && GetMainWindow())
308 XWMHints
*wmHints
= XAllocWMHints();
309 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
311 wmHints
->flags
= IconPixmapHint
;
315 wmHints
->flags
|= IconMaskHint
;
316 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
319 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
325 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
330 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
331 (const char*) title
);
332 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
333 (const char*) title
);
335 // Use this if the platform doesn't supply the above functions.
337 XTextProperty textProperty
;
338 textProperty
.value
= (unsigned char*) title
;
339 textProperty
.encoding
= XA_STRING
;
340 textProperty
.format
= 8;
341 textProperty
.nitems
= 1;
343 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
344 & textProperty
, WM_NAME
);
349 wxString
wxTopLevelWindowX11::GetTitle() const
354 #ifndef MWM_DECOR_BORDER
355 /* bit definitions for MwmHints.flags */
356 #define MWM_HINTS_FUNCTIONS (1L << 0)
357 #define MWM_HINTS_DECORATIONS (1L << 1)
358 #define MWM_HINTS_INPUT_MODE (1L << 2)
359 #define MWM_HINTS_STATUS (1L << 3)
361 #define MWM_DECOR_ALL (1L << 0)
362 #define MWM_DECOR_BORDER (1L << 1)
363 #define MWM_DECOR_RESIZEH (1L << 2)
364 #define MWM_DECOR_TITLE (1L << 3)
365 #define MWM_DECOR_MENU (1L << 4)
366 #define MWM_DECOR_MINIMIZE (1L << 5)
367 #define MWM_DECOR_MAXIMIZE (1L << 6)
377 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
379 // Set the window manager decorations according to the
380 // given wxWindows style
381 bool wxSetWMDecorations(Window w
, long style
)
384 GR_WM_PROPERTIES wmProp
;
388 if (style
& wxRESIZE_BORDER
)
390 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
391 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
394 if (style
& wxSYSTEM_MENU
)
396 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
397 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
400 if ((style
& wxCAPTION
) ||
401 (style
& wxTINY_CAPTION_HORIZ
) ||
402 (style
& wxTINY_CAPTION_VERT
))
404 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
405 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
408 if (style
& wxTHICK_FRAME
)
410 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
411 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
414 if (style
& wxSIMPLE_BORDER
)
416 wmProp
.props
|= GR_WM_PROPS_BORDER
;
417 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
420 if (style
& wxMINIMIZE_BOX
)
424 if (style
& wxMAXIMIZE_BOX
)
426 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
427 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
430 if (((style
& wxBORDER
) != wxBORDER
) && ((style
& wxTHICK_FRAME
) != wxTHICK_FRAME
)
431 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
433 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
434 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
437 GrSetWMProperties(w
, & wmProp
);
440 if (!wxMWMIsRunning(w
))
443 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
446 hints
.decorations
= 0;
448 if (style
& wxRESIZE_BORDER
)
450 wxLogDebug("MWM_DECOR_RESIZEH");
451 hints
.flags
|= MWM_HINTS_DECORATIONS
;
452 hints
.decorations
|= MWM_DECOR_RESIZEH
;
455 if (style
& wxSYSTEM_MENU
)
457 wxLogDebug("MWM_DECOR_MENU");
458 hints
.flags
|= MWM_HINTS_DECORATIONS
;
459 hints
.decorations
|= MWM_DECOR_MENU
;
462 if ((style
& wxCAPTION
) ||
463 (style
& wxTINY_CAPTION_HORIZ
) ||
464 (style
& wxTINY_CAPTION_VERT
))
466 wxLogDebug("MWM_DECOR_TITLE");
467 hints
.flags
|= MWM_HINTS_DECORATIONS
;
468 hints
.decorations
|= MWM_DECOR_TITLE
;
471 if ((style
& wxTHICK_FRAME
) || (style
& wxSIMPLE_BORDER
) || (style
& wxCAPTION
))
473 wxLogDebug("MWM_DECOR_BORDER");
474 hints
.flags
|= MWM_HINTS_DECORATIONS
;
475 hints
.decorations
|= MWM_DECOR_BORDER
;
478 if (style
& wxMINIMIZE_BOX
)
480 wxLogDebug("MWM_DECOR_MINIMIZE");
481 hints
.flags
|= MWM_HINTS_DECORATIONS
;
482 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
485 if (style
& wxMAXIMIZE_BOX
)
487 wxLogDebug("MWM_DECOR_MAXIMIZE");
488 hints
.flags
|= MWM_HINTS_DECORATIONS
;
489 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
492 XChangeProperty(wxGlobalDisplay(),
494 mwm_wm_hints
, mwm_wm_hints
,
496 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
502 bool wxMWMIsRunning(Window w
)
507 Display
*dpy
= (Display
*)wxGetDisplay();
508 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
510 unsigned long length
, bytesafter
;
511 unsigned char value
[20];
512 unsigned char *ptr
= &value
[0];
516 type
= format
= length
= 0;
519 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
520 0L, 2, False
, motifWmInfo
,
521 &type
, &format
, &length
, &bytesafter
, &ptr
);
523 return (ret
== Success
);
527 // For implementation purposes - sometimes decorations make the client area
529 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
531 // In fact wxFrame::GetClientAreaOrigin
532 // does the required calculation already.
534 if (this->IsKindOf(CLASSINFO(wxFrame
)))
536 wxFrame
* frame
= (wxFrame
*) this;
537 if (frame
->GetMenuBar())
538 return wxPoint(0, frame
->GetMenuBar()->GetSize().y
);
541 return wxPoint(0, 0);
544 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
546 wxWindowX11::DoGetClientSize(width
, height
);
547 // Done by wxTopLevelWindow
549 if (this->IsKindOf(CLASSINFO(wxFrame
)))
551 wxFrame
* frame
= (wxFrame
*) this;
552 if (frame
->GetMenuBar())
553 (*height
) -= frame
->GetMenuBar()->GetSize().y
;
554 if (frame
->GetStatusBar())
555 (*height
) -= frame
->GetStatusBar()->GetSize().y
;
560 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
562 wxWindowX11::DoSetClientSize(width
, height
);
565 if (!GetMainWindow())
568 XWindowChanges windowChanges
;
573 windowChanges
.width
= width
;
574 valueMask
|= CWWidth
;
578 windowChanges
.height
= height
;
579 valueMask
|= CWHeight
;
581 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
582 valueMask
, & windowChanges
);
586 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
589 msg
.Printf("Setting pos: %d, %d", x
, y
);
591 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
593 wxPoint pt
= GetPosition();
594 msg
.Printf("After, pos: %d, %d", pt
.x
, pt
.y
);
597 XSync(wxGlobalDisplay(), False
);
601 msg
.Printf("Before setting size: %d, %d", w
, h
);
603 if (!GetMainWindow())
606 XWindowChanges windowChanges
;
609 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
612 AdjustForParentClientOrigin( x
, yy
, sizeFlags
);
616 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
619 AdjustForParentClientOrigin( xx
, y
, sizeFlags
);
623 if (width
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
625 windowChanges
.width
= width
/* - m_borderSize*2 */;
626 valueMask
|= CWWidth
;
628 if (height
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
630 windowChanges
.height
= height
/* -m_borderSize*2*/;
631 valueMask
|= CWHeight
;
634 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
635 valueMask
, & windowChanges
);
636 XSync(wxGlobalDisplay(), False
);
638 msg
.Printf("Tried to set to %d, %d. After setting size: %d, %d", width
, height
, w
, h
);
643 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
645 XSync(wxGlobalDisplay(), False
);
646 Window window
= (Window
) m_mainWidget
;
653 wxLogDebug("Translating...");
655 XTranslateCoordinates(wxGlobalDisplay(), window
, XDefaultRootWindow(wxGlobalDisplay()),
656 0, 0, & offsetX
, & offsetY
, & childWindow
);
659 msg
.Printf("Offset: %d, %d", offsetX
, offsetY
);
663 XWindowAttributes attr
;
664 Status status
= XGetWindowAttributes(wxGlobalDisplay(), window
, & attr
);
669 *x
= attr
.x
+ offsetX
;
670 *y
= attr
.y
+ offsetY
;