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"
40 #include "wx/x11/private.h"
43 bool wxMWMIsRunning(Window w
);
45 // ----------------------------------------------------------------------------
46 // wxTopLevelWindowX11 creation
47 // ----------------------------------------------------------------------------
49 void wxTopLevelWindowX11::Init()
52 m_maximizeOnShow
= FALSE
;
54 // unlike (almost?) all other windows, frames are created hidden
57 // Data to save/restore when calling ShowFullScreen
59 m_fsIsMaximized
= FALSE
;
60 m_fsIsShowing
= FALSE
;
63 bool wxTopLevelWindowX11::Create(wxWindow
*parent
,
65 const wxString
& title
,
74 m_windowStyle
= style
;
79 m_windowId
= id
== -1 ? NewControlId() : id
;
82 parent
->AddChild(this);
84 wxTopLevelWindows
.Append(this);
86 Display
*xdisplay
= wxGlobalDisplay();
87 int xscreen
= DefaultScreen( xdisplay
);
88 Visual
*xvisual
= DefaultVisual( xdisplay
, xscreen
);
89 Window xparent
= RootWindow( xdisplay
, xscreen
);
91 XSetWindowAttributes xattributes
;
92 XSizeHints size_hints
;
95 long xattributes_mask
=
97 CWBorderPixel
| CWBackPixel
;
98 xattributes
.background_pixel
= BlackPixel( xdisplay
, xscreen
);
99 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
100 xattributes
.override_redirect
= False
;
102 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos
.x
, pos
.y
, size
.x
, size
.y
,
103 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
104 m_mainWidget
= (WXWindow
) xwindow
;
106 XSelectInput( xdisplay
, xwindow
,
107 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
108 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
109 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
110 PropertyChangeMask
);
112 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
114 XSetTransientForHint( xdisplay
, xwindow
, xparent
);
116 size_hints
.flags
= PSize
;
117 size_hints
.width
= size
.x
;
118 size_hints
.height
= size
.y
;
119 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
121 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
122 wm_hints
.input
= True
;
123 wm_hints
.initial_state
= NormalState
;
124 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
126 Atom wm_delete_window
= XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
127 XSetWMProtocols( xdisplay
, xwindow
, &wm_delete_window
, 1);
129 wxSetWMDecorations((Window
) GetMainWindow(), style
);
136 wxTopLevelWindowX11::~wxTopLevelWindowX11()
138 wxTopLevelWindows
.DeleteObject(this);
140 // If this is the last top-level window, exit.
141 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
143 wxTheApp
->SetTopWindow(NULL
);
145 if (wxTheApp
->GetExitOnFrameDelete())
147 // Signal to the app that we're going to close
148 wxTheApp
->ExitMainLoop();
153 // ----------------------------------------------------------------------------
154 // wxTopLevelWindowX11 showing
155 // ----------------------------------------------------------------------------
157 bool wxTopLevelWindowX11::Show(bool show
)
159 return wxWindowX11::Show(show
);
162 // ----------------------------------------------------------------------------
163 // wxTopLevelWindowX11 maximize/minimize
164 // ----------------------------------------------------------------------------
166 void wxTopLevelWindowX11::Maximize(bool maximize
)
171 bool wxTopLevelWindowX11::IsMaximized() const
177 void wxTopLevelWindowX11::Iconize(bool iconize
)
179 if (!m_iconized
&& GetMainWindow())
181 if (XIconifyWindow(wxGlobalDisplay(),
182 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
187 bool wxTopLevelWindowX11::IsIconized() const
192 void wxTopLevelWindowX11::Restore()
194 // This is the way to deiconify the window, according to the X FAQ
195 if (m_iconized
&& GetMainWindow())
197 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
202 // ----------------------------------------------------------------------------
203 // wxTopLevelWindowX11 fullscreen
204 // ----------------------------------------------------------------------------
206 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
213 m_fsIsShowing
= TRUE
;
225 m_fsIsShowing
= FALSE
;
232 // ----------------------------------------------------------------------------
233 // wxTopLevelWindowX11 misc
234 // ----------------------------------------------------------------------------
236 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
239 wxTopLevelWindowBase::SetIcon(icon
);
241 if (icon
.Ok() && GetMainWindow())
243 XWMHints
*wmHints
= XAllocWMHints();
244 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
246 wmHints
->flags
= IconPixmapHint
;
250 wmHints
->flags
|= IconMaskHint
;
251 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetPixmap();
254 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
259 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
264 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
265 (const char*) title
);
266 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
267 (const char*) title
);
269 // Use this if the platform doesn't supply the above functions.
271 XTextProperty textProperty
;
272 textProperty
.value
= (unsigned char*) title
;
273 textProperty
.encoding
= XA_STRING
;
274 textProperty
.format
= 8;
275 textProperty
.nitems
= 1;
277 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
278 & textProperty
, WM_NAME
);
283 wxString
wxTopLevelWindowX11::GetTitle() const
288 #ifndef MWM_DECOR_BORDER
289 /* bit definitions for MwmHints.flags */
290 #define MWM_HINTS_FUNCTIONS (1L << 0)
291 #define MWM_HINTS_DECORATIONS (1L << 1)
292 #define MWM_HINTS_INPUT_MODE (1L << 2)
293 #define MWM_HINTS_STATUS (1L << 3)
295 #define MWM_DECOR_ALL (1L << 0)
296 #define MWM_DECOR_BORDER (1L << 1)
297 #define MWM_DECOR_RESIZEH (1L << 2)
298 #define MWM_DECOR_TITLE (1L << 3)
299 #define MWM_DECOR_MENU (1L << 4)
300 #define MWM_DECOR_MINIMIZE (1L << 5)
301 #define MWM_DECOR_MAXIMIZE (1L << 6)
311 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
313 // Set the window manager decorations according to the
314 // given wxWindows style
315 bool wxSetWMDecorations(Window w
, long style
)
317 if (!wxMWMIsRunning(w
))
320 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
323 hints
.decorations
= 0;
325 if (style
& wxRESIZE_BORDER
)
327 hints
.flags
|= MWM_HINTS_DECORATIONS
;
328 hints
.decorations
|= MWM_DECOR_RESIZEH
;
331 if (style
& wxSYSTEM_MENU
)
333 hints
.flags
|= MWM_HINTS_DECORATIONS
;
334 hints
.decorations
|= MWM_DECOR_MENU
;
337 if ((style
& wxCAPTION
) ||
338 (style
& wxTINY_CAPTION_HORIZ
) ||
339 (style
& wxTINY_CAPTION_VERT
))
341 hints
.flags
|= MWM_HINTS_DECORATIONS
;
342 hints
.decorations
|= MWM_DECOR_TITLE
;
345 if (style
& wxTHICK_FRAME
)
347 hints
.flags
|= MWM_HINTS_DECORATIONS
;
348 hints
.decorations
|= MWM_DECOR_BORDER
;
351 if (style
& wxTHICK_FRAME
)
353 hints
.flags
|= MWM_HINTS_DECORATIONS
;
354 hints
.decorations
|= MWM_DECOR_BORDER
;
357 if (style
& wxMINIMIZE_BOX
)
359 hints
.flags
|= MWM_HINTS_DECORATIONS
;
360 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
363 if (style
& wxMAXIMIZE_BOX
)
365 hints
.flags
|= MWM_HINTS_DECORATIONS
;
366 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
369 XChangeProperty(wxGlobalDisplay(),
371 mwm_wm_hints
, mwm_wm_hints
,
373 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
378 bool wxMWMIsRunning(Window w
)
380 Display
*dpy
= (Display
*)wxGetDisplay();
381 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
383 unsigned long length
, bytesafter
;
384 unsigned char value
[20];
385 unsigned char *ptr
= &value
[0];
389 type
= format
= length
= 0;
392 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
393 0L, 2, False
, motifWmInfo
,
394 &type
, &format
, &length
, &bytesafter
, &ptr
);
396 return (ret
== Success
);