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
;
66 bool wxTopLevelWindowX11::Create(wxWindow
*parent
,
68 const wxString
& title
,
77 m_windowStyle
= style
;
82 m_windowId
= id
== -1 ? NewControlId() : id
;
85 parent
->AddChild(this);
87 wxTopLevelWindows
.Append(this);
89 Display
*xdisplay
= wxGlobalDisplay();
90 int xscreen
= DefaultScreen( xdisplay
);
91 Visual
*xvisual
= DefaultVisual( xdisplay
, xscreen
);
92 Window xparent
= RootWindow( xdisplay
, xscreen
);
93 Colormap cm
= DefaultColormap( xdisplay
, xscreen
);
95 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
96 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
98 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
99 m_backgroundColour
.CalcPixel( (WXColormap
) cm
);
102 XSetWindowAttributes xattributes
;
103 XSizeHints size_hints
;
106 long xattributes_mask
=
108 CWBorderPixel
| CWBackPixel
;
109 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
110 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
112 // TODO: if we want no border, caption etc.,
113 // I think we set this to True to remove decorations
114 xattributes
.override_redirect
= False
;
128 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
129 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
130 m_mainWidget
= (WXWindow
) xwindow
;
132 XSelectInput( xdisplay
, xwindow
,
133 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
134 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
135 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
136 PropertyChangeMask
);
138 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
140 // Set background to None which will prevent X11 from clearing the
141 // background completely.
142 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
144 // Messes up window management
145 // XSetTransientForHint( xdisplay, xwindow, xparent );
147 size_hints
.flags
= PSize
;
148 size_hints
.width
= size2
.x
;
149 size_hints
.height
= size2
.y
;
150 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
152 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
153 wm_hints
.input
= True
;
154 wm_hints
.initial_state
= NormalState
;
155 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
157 Atom wm_delete_window
= XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
158 XSetWMProtocols( xdisplay
, xwindow
, &wm_delete_window
, 1);
160 wxSetWMDecorations((Window
) GetMainWindow(), style
);
167 wxTopLevelWindowX11::~wxTopLevelWindowX11()
169 wxTopLevelWindows
.DeleteObject(this);
171 // If this is the last top-level window, exit.
172 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
174 wxTheApp
->SetTopWindow(NULL
);
176 if (wxTheApp
->GetExitOnFrameDelete())
178 // Signal to the app that we're going to close
179 wxTheApp
->ExitMainLoop();
184 // ----------------------------------------------------------------------------
185 // wxTopLevelWindowX11 showing
186 // ----------------------------------------------------------------------------
188 bool wxTopLevelWindowX11::Show(bool show
)
190 return wxWindowX11::Show(show
);
193 // ----------------------------------------------------------------------------
194 // wxTopLevelWindowX11 maximize/minimize
195 // ----------------------------------------------------------------------------
197 void wxTopLevelWindowX11::Maximize(bool maximize
)
202 bool wxTopLevelWindowX11::IsMaximized() const
208 void wxTopLevelWindowX11::Iconize(bool iconize
)
210 if (!m_iconized
&& GetMainWindow())
212 if (XIconifyWindow(wxGlobalDisplay(),
213 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
218 bool wxTopLevelWindowX11::IsIconized() const
223 void wxTopLevelWindowX11::Restore()
225 // This is the way to deiconify the window, according to the X FAQ
226 if (m_iconized
&& GetMainWindow())
228 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
233 // ----------------------------------------------------------------------------
234 // wxTopLevelWindowX11 fullscreen
235 // ----------------------------------------------------------------------------
237 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
244 m_fsIsShowing
= TRUE
;
256 m_fsIsShowing
= FALSE
;
263 // ----------------------------------------------------------------------------
264 // wxTopLevelWindowX11 misc
265 // ----------------------------------------------------------------------------
267 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
270 wxTopLevelWindowBase::SetIcon(icon
);
272 if (icon
.Ok() && GetMainWindow())
274 XWMHints
*wmHints
= XAllocWMHints();
275 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
277 wmHints
->flags
= IconPixmapHint
;
281 wmHints
->flags
|= IconMaskHint
;
282 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
285 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
290 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
295 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
296 (const char*) title
);
297 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
298 (const char*) title
);
300 // Use this if the platform doesn't supply the above functions.
302 XTextProperty textProperty
;
303 textProperty
.value
= (unsigned char*) title
;
304 textProperty
.encoding
= XA_STRING
;
305 textProperty
.format
= 8;
306 textProperty
.nitems
= 1;
308 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
309 & textProperty
, WM_NAME
);
314 wxString
wxTopLevelWindowX11::GetTitle() const
319 #ifndef MWM_DECOR_BORDER
320 /* bit definitions for MwmHints.flags */
321 #define MWM_HINTS_FUNCTIONS (1L << 0)
322 #define MWM_HINTS_DECORATIONS (1L << 1)
323 #define MWM_HINTS_INPUT_MODE (1L << 2)
324 #define MWM_HINTS_STATUS (1L << 3)
326 #define MWM_DECOR_ALL (1L << 0)
327 #define MWM_DECOR_BORDER (1L << 1)
328 #define MWM_DECOR_RESIZEH (1L << 2)
329 #define MWM_DECOR_TITLE (1L << 3)
330 #define MWM_DECOR_MENU (1L << 4)
331 #define MWM_DECOR_MINIMIZE (1L << 5)
332 #define MWM_DECOR_MAXIMIZE (1L << 6)
342 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
344 // Set the window manager decorations according to the
345 // given wxWindows style
346 bool wxSetWMDecorations(Window w
, long style
)
348 if (!wxMWMIsRunning(w
))
351 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
354 hints
.decorations
= 0;
356 if (style
& wxRESIZE_BORDER
)
358 wxLogDebug("MWM_DECOR_RESIZEH");
359 hints
.flags
|= MWM_HINTS_DECORATIONS
;
360 hints
.decorations
|= MWM_DECOR_RESIZEH
;
363 if (style
& wxSYSTEM_MENU
)
365 wxLogDebug("MWM_DECOR_MENU");
366 hints
.flags
|= MWM_HINTS_DECORATIONS
;
367 hints
.decorations
|= MWM_DECOR_MENU
;
370 if ((style
& wxCAPTION
) ||
371 (style
& wxTINY_CAPTION_HORIZ
) ||
372 (style
& wxTINY_CAPTION_VERT
))
374 wxLogDebug("MWM_DECOR_TITLE");
375 hints
.flags
|= MWM_HINTS_DECORATIONS
;
376 hints
.decorations
|= MWM_DECOR_TITLE
;
379 if ((style
& wxTHICK_FRAME
) || (style
& wxSIMPLE_BORDER
) || (style
& wxCAPTION
))
381 wxLogDebug("MWM_DECOR_BORDER");
382 hints
.flags
|= MWM_HINTS_DECORATIONS
;
383 hints
.decorations
|= MWM_DECOR_BORDER
;
386 if (style
& wxMINIMIZE_BOX
)
388 wxLogDebug("MWM_DECOR_MINIMIZE");
389 hints
.flags
|= MWM_HINTS_DECORATIONS
;
390 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
393 if (style
& wxMAXIMIZE_BOX
)
395 wxLogDebug("MWM_DECOR_MAXIMIZE");
396 hints
.flags
|= MWM_HINTS_DECORATIONS
;
397 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
400 XChangeProperty(wxGlobalDisplay(),
402 mwm_wm_hints
, mwm_wm_hints
,
404 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
409 bool wxMWMIsRunning(Window w
)
411 Display
*dpy
= (Display
*)wxGetDisplay();
412 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
414 unsigned long length
, bytesafter
;
415 unsigned char value
[20];
416 unsigned char *ptr
= &value
[0];
420 type
= format
= length
= 0;
423 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
424 0L, 2, False
, motifWmInfo
,
425 &type
, &format
, &length
, &bytesafter
, &ptr
);
427 return (ret
== Success
);
430 // For implementation purposes - sometimes decorations make the client area
432 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
434 // In fact wxFrame::GetClientAreaOrigin
435 // does the required calculation already.
437 if (this->IsKindOf(CLASSINFO(wxFrame
)))
439 wxFrame
* frame
= (wxFrame
*) this;
440 if (frame
->GetMenuBar())
441 return wxPoint(0, frame
->GetMenuBar()->GetSize().y
);
444 return wxPoint(0, 0);
447 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
449 wxWindowX11::DoGetClientSize(width
, height
);
450 // Done by wxTopLevelWindow
452 if (this->IsKindOf(CLASSINFO(wxFrame
)))
454 wxFrame
* frame
= (wxFrame
*) this;
455 if (frame
->GetMenuBar())
456 (*height
) -= frame
->GetMenuBar()->GetSize().y
;
457 if (frame
->GetStatusBar())
458 (*height
) -= frame
->GetStatusBar()->GetSize().y
;
463 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
465 wxWindowX11::DoSetClientSize(width
, height
);
468 if (!GetMainWindow())
471 XWindowChanges windowChanges
;
476 windowChanges
.width
= width
;
477 valueMask
|= CWWidth
;
481 windowChanges
.height
= height
;
482 valueMask
|= CWHeight
;
484 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
485 valueMask
, & windowChanges
);
489 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
492 msg
.Printf("Setting pos: %d, %d", x
, y
);
494 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
496 wxPoint pt
= GetPosition();
497 msg
.Printf("After, pos: %d, %d", pt
.x
, pt
.y
);
500 XSync(wxGlobalDisplay(), False
);
504 msg
.Printf("Before setting size: %d, %d", w
, h
);
506 if (!GetMainWindow())
509 XWindowChanges windowChanges
;
512 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
515 AdjustForParentClientOrigin( x
, yy
, sizeFlags
);
519 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
522 AdjustForParentClientOrigin( xx
, y
, sizeFlags
);
526 if (width
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
528 windowChanges
.width
= width
/* - m_borderSize*2 */;
529 valueMask
|= CWWidth
;
531 if (height
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
533 windowChanges
.height
= height
/* -m_borderSize*2*/;
534 valueMask
|= CWHeight
;
537 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
538 valueMask
, & windowChanges
);
539 XSync(wxGlobalDisplay(), False
);
541 msg
.Printf("Tried to set to %d, %d. After setting size: %d, %d", width
, height
, w
, h
);
546 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
548 XSync(wxGlobalDisplay(), False
);
549 Window window
= (Window
) m_mainWidget
;
555 wxLogDebug("Translating...");
557 XTranslateCoordinates(wxGlobalDisplay(), window
, XDefaultRootWindow(wxGlobalDisplay()),
558 0, 0, & offsetX
, & offsetY
, & childWindow
);
561 msg
.Printf("Offset: %d, %d", offsetX
, offsetY
);
564 XWindowAttributes attr
;
565 Status status
= XGetWindowAttributes(wxGlobalDisplay(), window
, & attr
);
570 *x
= attr
.x
+ offsetX
;
571 *y
= attr
.y
+ offsetY
;