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 // Messes up window management
139 // XSetTransientForHint( xdisplay, xwindow, xparent );
141 size_hints
.flags
= PSize
;
142 size_hints
.width
= size2
.x
;
143 size_hints
.height
= size2
.y
;
144 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
146 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
147 wm_hints
.input
= True
;
148 wm_hints
.initial_state
= NormalState
;
149 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
151 Atom wm_delete_window
= XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
152 XSetWMProtocols( xdisplay
, xwindow
, &wm_delete_window
, 1);
154 wxSetWMDecorations((Window
) GetMainWindow(), style
);
161 wxTopLevelWindowX11::~wxTopLevelWindowX11()
163 wxTopLevelWindows
.DeleteObject(this);
165 // If this is the last top-level window, exit.
166 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
168 wxTheApp
->SetTopWindow(NULL
);
170 if (wxTheApp
->GetExitOnFrameDelete())
172 // Signal to the app that we're going to close
173 wxTheApp
->ExitMainLoop();
178 // ----------------------------------------------------------------------------
179 // wxTopLevelWindowX11 showing
180 // ----------------------------------------------------------------------------
182 bool wxTopLevelWindowX11::Show(bool show
)
184 return wxWindowX11::Show(show
);
187 // ----------------------------------------------------------------------------
188 // wxTopLevelWindowX11 maximize/minimize
189 // ----------------------------------------------------------------------------
191 void wxTopLevelWindowX11::Maximize(bool maximize
)
196 bool wxTopLevelWindowX11::IsMaximized() const
202 void wxTopLevelWindowX11::Iconize(bool iconize
)
204 if (!m_iconized
&& GetMainWindow())
206 if (XIconifyWindow(wxGlobalDisplay(),
207 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
212 bool wxTopLevelWindowX11::IsIconized() const
217 void wxTopLevelWindowX11::Restore()
219 // This is the way to deiconify the window, according to the X FAQ
220 if (m_iconized
&& GetMainWindow())
222 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
227 // ----------------------------------------------------------------------------
228 // wxTopLevelWindowX11 fullscreen
229 // ----------------------------------------------------------------------------
231 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
238 m_fsIsShowing
= TRUE
;
250 m_fsIsShowing
= FALSE
;
257 // ----------------------------------------------------------------------------
258 // wxTopLevelWindowX11 misc
259 // ----------------------------------------------------------------------------
261 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
264 wxTopLevelWindowBase::SetIcon(icon
);
266 if (icon
.Ok() && GetMainWindow())
268 XWMHints
*wmHints
= XAllocWMHints();
269 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
271 wmHints
->flags
= IconPixmapHint
;
275 wmHints
->flags
|= IconMaskHint
;
276 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
279 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
284 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
289 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
290 (const char*) title
);
291 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
292 (const char*) title
);
294 // Use this if the platform doesn't supply the above functions.
296 XTextProperty textProperty
;
297 textProperty
.value
= (unsigned char*) title
;
298 textProperty
.encoding
= XA_STRING
;
299 textProperty
.format
= 8;
300 textProperty
.nitems
= 1;
302 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
303 & textProperty
, WM_NAME
);
308 wxString
wxTopLevelWindowX11::GetTitle() const
313 #ifndef MWM_DECOR_BORDER
314 /* bit definitions for MwmHints.flags */
315 #define MWM_HINTS_FUNCTIONS (1L << 0)
316 #define MWM_HINTS_DECORATIONS (1L << 1)
317 #define MWM_HINTS_INPUT_MODE (1L << 2)
318 #define MWM_HINTS_STATUS (1L << 3)
320 #define MWM_DECOR_ALL (1L << 0)
321 #define MWM_DECOR_BORDER (1L << 1)
322 #define MWM_DECOR_RESIZEH (1L << 2)
323 #define MWM_DECOR_TITLE (1L << 3)
324 #define MWM_DECOR_MENU (1L << 4)
325 #define MWM_DECOR_MINIMIZE (1L << 5)
326 #define MWM_DECOR_MAXIMIZE (1L << 6)
336 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
338 // Set the window manager decorations according to the
339 // given wxWindows style
340 bool wxSetWMDecorations(Window w
, long style
)
342 if (!wxMWMIsRunning(w
))
345 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
348 hints
.decorations
= 0;
350 if (style
& wxRESIZE_BORDER
)
352 wxLogDebug("MWM_DECOR_RESIZEH");
353 hints
.flags
|= MWM_HINTS_DECORATIONS
;
354 hints
.decorations
|= MWM_DECOR_RESIZEH
;
357 if (style
& wxSYSTEM_MENU
)
359 wxLogDebug("MWM_DECOR_MENU");
360 hints
.flags
|= MWM_HINTS_DECORATIONS
;
361 hints
.decorations
|= MWM_DECOR_MENU
;
364 if ((style
& wxCAPTION
) ||
365 (style
& wxTINY_CAPTION_HORIZ
) ||
366 (style
& wxTINY_CAPTION_VERT
))
368 wxLogDebug("MWM_DECOR_TITLE");
369 hints
.flags
|= MWM_HINTS_DECORATIONS
;
370 hints
.decorations
|= MWM_DECOR_TITLE
;
373 if ((style
& wxTHICK_FRAME
) || (style
& wxSIMPLE_BORDER
) || (style
& wxCAPTION
))
375 wxLogDebug("MWM_DECOR_BORDER");
376 hints
.flags
|= MWM_HINTS_DECORATIONS
;
377 hints
.decorations
|= MWM_DECOR_BORDER
;
380 if (style
& wxMINIMIZE_BOX
)
382 wxLogDebug("MWM_DECOR_MINIMIZE");
383 hints
.flags
|= MWM_HINTS_DECORATIONS
;
384 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
387 if (style
& wxMAXIMIZE_BOX
)
389 wxLogDebug("MWM_DECOR_MAXIMIZE");
390 hints
.flags
|= MWM_HINTS_DECORATIONS
;
391 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
394 XChangeProperty(wxGlobalDisplay(),
396 mwm_wm_hints
, mwm_wm_hints
,
398 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
403 bool wxMWMIsRunning(Window w
)
405 Display
*dpy
= (Display
*)wxGetDisplay();
406 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
408 unsigned long length
, bytesafter
;
409 unsigned char value
[20];
410 unsigned char *ptr
= &value
[0];
414 type
= format
= length
= 0;
417 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
418 0L, 2, False
, motifWmInfo
,
419 &type
, &format
, &length
, &bytesafter
, &ptr
);
421 return (ret
== Success
);
424 // For implementation purposes - sometimes decorations make the client area
426 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
428 // In fact wxFrame::GetClientAreaOrigin
429 // does the required calculation already.
431 if (this->IsKindOf(CLASSINFO(wxFrame
)))
433 wxFrame
* frame
= (wxFrame
*) this;
434 if (frame
->GetMenuBar())
435 return wxPoint(0, frame
->GetMenuBar()->GetSize().y
);
438 return wxPoint(0, 0);
441 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
443 wxWindowX11::DoGetClientSize(width
, height
);
444 // Done by wxTopLevelWindow
446 if (this->IsKindOf(CLASSINFO(wxFrame
)))
448 wxFrame
* frame
= (wxFrame
*) this;
449 if (frame
->GetMenuBar())
450 (*height
) -= frame
->GetMenuBar()->GetSize().y
;
451 if (frame
->GetStatusBar())
452 (*height
) -= frame
->GetStatusBar()->GetSize().y
;
457 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
459 wxWindowX11::DoSetClientSize(width
, height
);
461 if (!GetMainWindow())
464 XWindowChanges windowChanges
;
469 windowChanges
.width
= width
;
470 valueMask
|= CWWidth
;
474 windowChanges
.height
= height
;
475 valueMask
|= CWHeight
;
477 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
478 valueMask
, & windowChanges
);