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 // TODO: For dialogs, this should be wxSYS_COLOUR_3DFACE
96 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
97 m_backgroundColour
.CalcPixel( (WXColormap
) cm
);
100 XSetWindowAttributes xattributes
;
101 XSizeHints size_hints
;
104 long xattributes_mask
=
106 CWBorderPixel
| CWBackPixel
;
107 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
108 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
110 // TODO: if we want no border, caption etc.,
111 // I think we set this to True to remove decorations
112 xattributes
.override_redirect
= False
;
126 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
127 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
128 m_mainWidget
= (WXWindow
) xwindow
;
130 XSelectInput( xdisplay
, xwindow
,
131 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
132 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
133 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
134 PropertyChangeMask
);
136 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
138 // Set background to None which will prevent X11 from clearing the
139 // background completely.
140 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
142 // Messes up window management
143 // XSetTransientForHint( xdisplay, xwindow, xparent );
145 size_hints
.flags
= PSize
;
146 size_hints
.width
= size2
.x
;
147 size_hints
.height
= size2
.y
;
148 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
150 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
151 wm_hints
.input
= True
;
152 wm_hints
.initial_state
= NormalState
;
153 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
155 Atom wm_delete_window
= XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
156 XSetWMProtocols( xdisplay
, xwindow
, &wm_delete_window
, 1);
158 wxSetWMDecorations((Window
) GetMainWindow(), style
);
165 wxTopLevelWindowX11::~wxTopLevelWindowX11()
167 wxTopLevelWindows
.DeleteObject(this);
169 // If this is the last top-level window, exit.
170 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
172 wxTheApp
->SetTopWindow(NULL
);
174 if (wxTheApp
->GetExitOnFrameDelete())
176 // Signal to the app that we're going to close
177 wxTheApp
->ExitMainLoop();
182 // ----------------------------------------------------------------------------
183 // wxTopLevelWindowX11 showing
184 // ----------------------------------------------------------------------------
186 bool wxTopLevelWindowX11::Show(bool show
)
188 return wxWindowX11::Show(show
);
191 // ----------------------------------------------------------------------------
192 // wxTopLevelWindowX11 maximize/minimize
193 // ----------------------------------------------------------------------------
195 void wxTopLevelWindowX11::Maximize(bool maximize
)
200 bool wxTopLevelWindowX11::IsMaximized() const
206 void wxTopLevelWindowX11::Iconize(bool iconize
)
208 if (!m_iconized
&& GetMainWindow())
210 if (XIconifyWindow(wxGlobalDisplay(),
211 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
216 bool wxTopLevelWindowX11::IsIconized() const
221 void wxTopLevelWindowX11::Restore()
223 // This is the way to deiconify the window, according to the X FAQ
224 if (m_iconized
&& GetMainWindow())
226 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
231 // ----------------------------------------------------------------------------
232 // wxTopLevelWindowX11 fullscreen
233 // ----------------------------------------------------------------------------
235 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
242 m_fsIsShowing
= TRUE
;
254 m_fsIsShowing
= FALSE
;
261 // ----------------------------------------------------------------------------
262 // wxTopLevelWindowX11 misc
263 // ----------------------------------------------------------------------------
265 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
268 wxTopLevelWindowBase::SetIcon(icon
);
270 if (icon
.Ok() && GetMainWindow())
272 XWMHints
*wmHints
= XAllocWMHints();
273 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
275 wmHints
->flags
= IconPixmapHint
;
279 wmHints
->flags
|= IconMaskHint
;
280 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
283 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
288 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
293 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
294 (const char*) title
);
295 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
296 (const char*) title
);
298 // Use this if the platform doesn't supply the above functions.
300 XTextProperty textProperty
;
301 textProperty
.value
= (unsigned char*) title
;
302 textProperty
.encoding
= XA_STRING
;
303 textProperty
.format
= 8;
304 textProperty
.nitems
= 1;
306 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
307 & textProperty
, WM_NAME
);
312 wxString
wxTopLevelWindowX11::GetTitle() const
317 #ifndef MWM_DECOR_BORDER
318 /* bit definitions for MwmHints.flags */
319 #define MWM_HINTS_FUNCTIONS (1L << 0)
320 #define MWM_HINTS_DECORATIONS (1L << 1)
321 #define MWM_HINTS_INPUT_MODE (1L << 2)
322 #define MWM_HINTS_STATUS (1L << 3)
324 #define MWM_DECOR_ALL (1L << 0)
325 #define MWM_DECOR_BORDER (1L << 1)
326 #define MWM_DECOR_RESIZEH (1L << 2)
327 #define MWM_DECOR_TITLE (1L << 3)
328 #define MWM_DECOR_MENU (1L << 4)
329 #define MWM_DECOR_MINIMIZE (1L << 5)
330 #define MWM_DECOR_MAXIMIZE (1L << 6)
340 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
342 // Set the window manager decorations according to the
343 // given wxWindows style
344 bool wxSetWMDecorations(Window w
, long style
)
346 if (!wxMWMIsRunning(w
))
349 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
352 hints
.decorations
= 0;
354 if (style
& wxRESIZE_BORDER
)
356 wxLogDebug("MWM_DECOR_RESIZEH");
357 hints
.flags
|= MWM_HINTS_DECORATIONS
;
358 hints
.decorations
|= MWM_DECOR_RESIZEH
;
361 if (style
& wxSYSTEM_MENU
)
363 wxLogDebug("MWM_DECOR_MENU");
364 hints
.flags
|= MWM_HINTS_DECORATIONS
;
365 hints
.decorations
|= MWM_DECOR_MENU
;
368 if ((style
& wxCAPTION
) ||
369 (style
& wxTINY_CAPTION_HORIZ
) ||
370 (style
& wxTINY_CAPTION_VERT
))
372 wxLogDebug("MWM_DECOR_TITLE");
373 hints
.flags
|= MWM_HINTS_DECORATIONS
;
374 hints
.decorations
|= MWM_DECOR_TITLE
;
377 if ((style
& wxTHICK_FRAME
) || (style
& wxSIMPLE_BORDER
) || (style
& wxCAPTION
))
379 wxLogDebug("MWM_DECOR_BORDER");
380 hints
.flags
|= MWM_HINTS_DECORATIONS
;
381 hints
.decorations
|= MWM_DECOR_BORDER
;
384 if (style
& wxMINIMIZE_BOX
)
386 wxLogDebug("MWM_DECOR_MINIMIZE");
387 hints
.flags
|= MWM_HINTS_DECORATIONS
;
388 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
391 if (style
& wxMAXIMIZE_BOX
)
393 wxLogDebug("MWM_DECOR_MAXIMIZE");
394 hints
.flags
|= MWM_HINTS_DECORATIONS
;
395 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
398 XChangeProperty(wxGlobalDisplay(),
400 mwm_wm_hints
, mwm_wm_hints
,
402 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
407 bool wxMWMIsRunning(Window w
)
409 Display
*dpy
= (Display
*)wxGetDisplay();
410 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
412 unsigned long length
, bytesafter
;
413 unsigned char value
[20];
414 unsigned char *ptr
= &value
[0];
418 type
= format
= length
= 0;
421 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
422 0L, 2, False
, motifWmInfo
,
423 &type
, &format
, &length
, &bytesafter
, &ptr
);
425 return (ret
== Success
);
428 // For implementation purposes - sometimes decorations make the client area
430 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
432 // In fact wxFrame::GetClientAreaOrigin
433 // does the required calculation already.
435 if (this->IsKindOf(CLASSINFO(wxFrame
)))
437 wxFrame
* frame
= (wxFrame
*) this;
438 if (frame
->GetMenuBar())
439 return wxPoint(0, frame
->GetMenuBar()->GetSize().y
);
442 return wxPoint(0, 0);
445 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
447 wxWindowX11::DoGetClientSize(width
, height
);
448 // Done by wxTopLevelWindow
450 if (this->IsKindOf(CLASSINFO(wxFrame
)))
452 wxFrame
* frame
= (wxFrame
*) this;
453 if (frame
->GetMenuBar())
454 (*height
) -= frame
->GetMenuBar()->GetSize().y
;
455 if (frame
->GetStatusBar())
456 (*height
) -= frame
->GetStatusBar()->GetSize().y
;
461 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
463 wxWindowX11::DoSetClientSize(width
, height
);
466 if (!GetMainWindow())
469 XWindowChanges windowChanges
;
474 windowChanges
.width
= width
;
475 valueMask
|= CWWidth
;
479 windowChanges
.height
= height
;
480 valueMask
|= CWHeight
;
482 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
483 valueMask
, & windowChanges
);
487 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
490 msg
.Printf("Setting pos: %d, %d", x
, y
);
492 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
494 wxPoint pt
= GetPosition();
495 msg
.Printf("After, pos: %d, %d", pt
.x
, pt
.y
);
498 XSync(wxGlobalDisplay(), False
);
502 msg
.Printf("Before setting size: %d, %d", w
, h
);
504 if (!GetMainWindow())
507 XWindowChanges windowChanges
;
510 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
513 AdjustForParentClientOrigin( x
, yy
, sizeFlags
);
517 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
520 AdjustForParentClientOrigin( xx
, y
, sizeFlags
);
524 if (width
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
526 windowChanges
.width
= width
/* - m_borderSize*2 */;
527 valueMask
|= CWWidth
;
529 if (height
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
531 windowChanges
.height
= height
/* -m_borderSize*2*/;
532 valueMask
|= CWHeight
;
535 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
536 valueMask
, & windowChanges
);
537 XSync(wxGlobalDisplay(), False
);
539 msg
.Printf("Tried to set to %d, %d. After setting size: %d, %d", width
, height
, w
, h
);
544 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
546 XSync(wxGlobalDisplay(), False
);
547 Window window
= (Window
) m_mainWidget
;
553 wxLogDebug("Translating...");
555 XTranslateCoordinates(wxGlobalDisplay(), window
, XDefaultRootWindow(wxGlobalDisplay()),
556 0, 0, & offsetX
, & offsetY
, & childWindow
);
559 msg
.Printf("Offset: %d, %d", offsetX
, offsetY
);
562 XWindowAttributes attr
;
563 Status status
= XGetWindowAttributes(wxGlobalDisplay(), window
, & attr
);
568 *x
= attr
.x
+ offsetX
;
569 *y
= attr
.y
+ offsetY
;