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
);
99 XSetWindowAttributes xattributes
;
100 XSizeHints size_hints
;
103 long xattributes_mask
=
105 CWBorderPixel
| CWBackPixel
;
106 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
107 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
108 xattributes
.override_redirect
= False
;
110 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos
.x
, pos
.y
, size
.x
, size
.y
,
111 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
112 m_mainWidget
= (WXWindow
) xwindow
;
114 XSelectInput( xdisplay
, xwindow
,
115 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
116 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
117 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
118 PropertyChangeMask
);
120 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
122 XSetTransientForHint( xdisplay
, xwindow
, xparent
);
124 size_hints
.flags
= PSize
;
125 size_hints
.width
= size
.x
;
126 size_hints
.height
= size
.y
;
127 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
129 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
130 wm_hints
.input
= True
;
131 wm_hints
.initial_state
= NormalState
;
132 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
134 Atom wm_delete_window
= XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
135 XSetWMProtocols( xdisplay
, xwindow
, &wm_delete_window
, 1);
137 wxSetWMDecorations((Window
) GetMainWindow(), style
);
144 wxTopLevelWindowX11
::~wxTopLevelWindowX11()
146 wxTopLevelWindows
.DeleteObject(this);
148 // If this is the last top-level window, exit.
149 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
151 wxTheApp
->SetTopWindow(NULL
);
153 if (wxTheApp
->GetExitOnFrameDelete())
155 // Signal to the app that we're going to close
156 wxTheApp
->ExitMainLoop();
161 // ----------------------------------------------------------------------------
162 // wxTopLevelWindowX11 showing
163 // ----------------------------------------------------------------------------
165 bool wxTopLevelWindowX11
::Show(bool show
)
167 return wxWindowX11
::Show(show
);
170 // ----------------------------------------------------------------------------
171 // wxTopLevelWindowX11 maximize/minimize
172 // ----------------------------------------------------------------------------
174 void wxTopLevelWindowX11
::Maximize(bool maximize
)
179 bool wxTopLevelWindowX11
::IsMaximized() const
185 void wxTopLevelWindowX11
::Iconize(bool iconize
)
187 if (!m_iconized
&& GetMainWindow())
189 if (XIconifyWindow(wxGlobalDisplay(),
190 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
195 bool wxTopLevelWindowX11
::IsIconized() const
200 void wxTopLevelWindowX11
::Restore()
202 // This is the way to deiconify the window, according to the X FAQ
203 if (m_iconized
&& GetMainWindow())
205 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
210 // ----------------------------------------------------------------------------
211 // wxTopLevelWindowX11 fullscreen
212 // ----------------------------------------------------------------------------
214 bool wxTopLevelWindowX11
::ShowFullScreen(bool show
, long style
)
221 m_fsIsShowing
= TRUE
;
233 m_fsIsShowing
= FALSE
;
240 // ----------------------------------------------------------------------------
241 // wxTopLevelWindowX11 misc
242 // ----------------------------------------------------------------------------
244 void wxTopLevelWindowX11
::SetIcon(const wxIcon
& icon
)
247 wxTopLevelWindowBase
::SetIcon(icon
);
249 if (icon
.Ok() && GetMainWindow())
251 XWMHints
*wmHints
= XAllocWMHints();
252 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
254 wmHints
->flags
= IconPixmapHint
;
258 wmHints
->flags
|= IconMaskHint
;
259 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetPixmap();
262 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
267 void wxTopLevelWindowX11
::SetTitle(const wxString
& title
)
272 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
273 (const char*) title
);
274 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
275 (const char*) title
);
277 // Use this if the platform doesn't supply the above functions.
279 XTextProperty textProperty
;
280 textProperty
.value
= (unsigned char*) title
;
281 textProperty
.encoding
= XA_STRING
;
282 textProperty
.format
= 8;
283 textProperty
.nitems
= 1;
285 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
286 & textProperty
, WM_NAME
);
291 wxString wxTopLevelWindowX11
::GetTitle() const
296 #ifndef MWM_DECOR_BORDER
297 /* bit definitions for MwmHints.flags */
298 #define MWM_HINTS_FUNCTIONS (1L << 0)
299 #define MWM_HINTS_DECORATIONS (1L << 1)
300 #define MWM_HINTS_INPUT_MODE (1L << 2)
301 #define MWM_HINTS_STATUS (1L << 3)
303 #define MWM_DECOR_ALL (1L << 0)
304 #define MWM_DECOR_BORDER (1L << 1)
305 #define MWM_DECOR_RESIZEH (1L << 2)
306 #define MWM_DECOR_TITLE (1L << 3)
307 #define MWM_DECOR_MENU (1L << 4)
308 #define MWM_DECOR_MINIMIZE (1L << 5)
309 #define MWM_DECOR_MAXIMIZE (1L << 6)
319 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
321 // Set the window manager decorations according to the
322 // given wxWindows style
323 bool wxSetWMDecorations(Window w
, long style
)
325 if (!wxMWMIsRunning(w
))
328 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
331 hints
.decorations
= 0;
333 if (style
& wxRESIZE_BORDER
)
335 hints
.flags
|= MWM_HINTS_DECORATIONS
;
336 hints
.decorations
|= MWM_DECOR_RESIZEH
;
339 if (style
& wxSYSTEM_MENU
)
341 hints
.flags
|= MWM_HINTS_DECORATIONS
;
342 hints
.decorations
|= MWM_DECOR_MENU
;
345 if ((style
& wxCAPTION
) ||
346 (style
& wxTINY_CAPTION_HORIZ
) ||
347 (style
& wxTINY_CAPTION_VERT
))
349 hints
.flags
|= MWM_HINTS_DECORATIONS
;
350 hints
.decorations
|= MWM_DECOR_TITLE
;
353 if (style
& wxTHICK_FRAME
)
355 hints
.flags
|= MWM_HINTS_DECORATIONS
;
356 hints
.decorations
|= MWM_DECOR_BORDER
;
359 if (style
& wxTHICK_FRAME
)
361 hints
.flags
|= MWM_HINTS_DECORATIONS
;
362 hints
.decorations
|= MWM_DECOR_BORDER
;
365 if (style
& wxMINIMIZE_BOX
)
367 hints
.flags
|= MWM_HINTS_DECORATIONS
;
368 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
371 if (style
& wxMAXIMIZE_BOX
)
373 hints
.flags
|= MWM_HINTS_DECORATIONS
;
374 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
377 XChangeProperty(wxGlobalDisplay(),
379 mwm_wm_hints
, mwm_wm_hints
,
381 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
386 bool wxMWMIsRunning(Window w
)
388 Display
*dpy
= (Display
*)wxGetDisplay();
389 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
391 unsigned long length
, bytesafter
;
392 unsigned char value
[20];
393 unsigned char *ptr
= &value
[0];
397 type
= format
= length
= 0;
400 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
401 0L, 2, False
, motifWmInfo
,
402 &type
, &format
, &length
, &bytesafter
, &ptr
);
404 return (ret
== Success
);
407 // For implementation purposes - sometimes decorations make the client area
409 wxPoint wxTopLevelWindowX11
::GetClientAreaOrigin() const
411 // In fact wxFrame::GetClientAreaOrigin
412 // does the required calculation already.
414 if (this->IsKindOf(CLASSINFO(wxFrame
)))
416 wxFrame
* frame
= (wxFrame
*) this;
417 if (frame
->GetMenuBar())
418 return wxPoint(0, frame
->GetMenuBar()->GetSize().y
);
421 return wxPoint(0, 0);
424 void wxTopLevelWindowX11
::DoGetClientSize( int *width
, int *height
) const
426 wxWindowX11
::DoGetClientSize(width
, height
);
427 // Done by wxTopLevelWindow
429 if (this->IsKindOf(CLASSINFO(wxFrame
)))
431 wxFrame
* frame
= (wxFrame
*) this;
432 if (frame
->GetMenuBar())
433 (*height
) -= frame
->GetMenuBar()->GetSize().y
;
434 if (frame
->GetStatusBar())
435 (*height
) -= frame
->GetStatusBar()->GetSize().y
;
440 void wxTopLevelWindowX11
::DoSetClientSize(int width
, int height
)
442 wxWindowX11
::DoSetClientSize(width
, height
);
444 if (!GetMainWindow())
447 XWindowChanges windowChanges
;
452 windowChanges
.width
= width
;
453 valueMask
|= CWWidth
;
457 windowChanges
.height
= height
;
458 valueMask
|= CWHeight
;
460 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
461 valueMask
, & windowChanges
);