Typos and other fixes
[wxWidgets.git] / src / x11 / toplevel.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: x11/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for X11
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 24.09.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2002 Julian Smart
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "toplevel.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/toplevel.h"
34 #include "wx/string.h"
35 #include "wx/log.h"
36 #include "wx/intl.h"
37 #include "wx/frame.h"
38 #endif //WX_PRECOMP
39
40 #include "wx/x11/private.h"
41 #include "X11/Xatom.h"
42 #include "X11/Xutil.h"
43
44 // list of all frames and modeless dialogs
45 // wxWindowList wxModelessWindows;
46
47 // ----------------------------------------------------------------------------
48 // wxTopLevelWindowX11 creation
49 // ----------------------------------------------------------------------------
50
51 void wxTopLevelWindowX11::Init()
52 {
53 m_iconized =
54 m_maximizeOnShow = FALSE;
55
56 // unlike (almost?) all other windows, frames are created hidden
57 m_isShown = FALSE;
58
59 // Data to save/restore when calling ShowFullScreen
60 m_fsStyle = 0;
61 m_fsIsMaximized = FALSE;
62 m_fsIsShowing = FALSE;
63 }
64
65 bool wxTopLevelWindowX11::Create(wxWindow *parent,
66 wxWindowID id,
67 const wxString& title,
68 const wxPoint& pos,
69 const wxSize& size,
70 long style,
71 const wxString& name)
72 {
73 // init our fields
74 Init();
75
76 m_windowStyle = style;
77 m_parent = parent;
78
79 SetName(name);
80
81 m_windowId = id == -1 ? NewControlId() : id;
82
83 if (parent)
84 parent->AddChild(this);
85
86 wxTopLevelWindows.Append(this);
87
88 Display *xdisplay = wxGlobalDisplay();
89 int xscreen = DefaultScreen( xdisplay );
90 Visual *xvisual = DefaultVisual( xdisplay, xscreen );
91 Window xparent = RootWindow( xdisplay, xscreen );
92
93 XSetWindowAttributes xattributes;
94 XSizeHints size_hints;
95 XWMHints wm_hints;
96
97 long xattributes_mask =
98 CWEventMask |
99 CWBorderPixel | CWBackPixel;
100 xattributes.background_pixel = BlackPixel( xdisplay, xscreen );
101 xattributes.border_pixel = BlackPixel( xdisplay, xscreen );
102 xattributes.override_redirect = False;
103
104 Window xwindow = XCreateWindow( xdisplay, xparent, pos.x, pos.y, size.x, size.y,
105 0, DefaultDepth(xdisplay,xscreen), InputOutput, xvisual, xattributes_mask, &xattributes );
106 m_mainWidget = (WXWindow) xwindow;
107
108 XSelectInput( xdisplay, xwindow,
109 ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
110 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
111 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
112 PropertyChangeMask );
113
114 wxAddWindowToTable( xwindow, (wxWindow*) this );
115
116 XSetTransientForHint( xdisplay, xwindow, xparent );
117
118 size_hints.flags = PSize;
119 size_hints.width = size.x;
120 size_hints.height = size.y;
121 XSetWMNormalHints( xdisplay, xwindow, &size_hints);
122
123 wm_hints.flags = InputHint | StateHint /* | WindowGroupHint */;
124 wm_hints.input = True;
125 wm_hints.initial_state = NormalState;
126 XSetWMHints( xdisplay, xwindow, &wm_hints);
127
128 Atom wm_delete_window = XInternAtom( xdisplay, "WM_DELETE_WINDOW", False);
129 XSetWMProtocols( xdisplay, xwindow, &wm_delete_window, 1);
130
131 wxSetWMDecorations((Window) GetMainWindow(), style);
132
133 SetTitle(title);
134
135 return TRUE;
136 }
137
138 wxTopLevelWindowX11::~wxTopLevelWindowX11()
139 {
140 wxTopLevelWindows.DeleteObject(this);
141
142 // If this is the last top-level window, exit.
143 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
144 {
145 wxTheApp->SetTopWindow(NULL);
146
147 if (wxTheApp->GetExitOnFrameDelete())
148 {
149 // Signal to the app that we're going to close
150 wxTheApp->ExitMainLoop();
151 }
152 }
153 }
154
155 // ----------------------------------------------------------------------------
156 // wxTopLevelWindowX11 showing
157 // ----------------------------------------------------------------------------
158
159 bool wxTopLevelWindowX11::Show(bool show)
160 {
161 if ( !wxWindowBase::Show(show) )
162 return FALSE;
163
164 return wxWindowX11::Show(show);
165 }
166
167 // ----------------------------------------------------------------------------
168 // wxTopLevelWindowX11 maximize/minimize
169 // ----------------------------------------------------------------------------
170
171 void wxTopLevelWindowX11::Maximize(bool maximize)
172 {
173 // TODO
174 }
175
176 bool wxTopLevelWindowX11::IsMaximized() const
177 {
178 // TODO
179 return TRUE;
180 }
181
182 void wxTopLevelWindowX11::Iconize(bool iconize)
183 {
184 if (!m_iconized && GetMainWindow())
185 {
186 if (XIconifyWindow(wxGlobalDisplay(),
187 (Window) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
188 m_iconized = TRUE;
189 }
190 }
191
192 bool wxTopLevelWindowX11::IsIconized() const
193 {
194 return m_iconized;
195 }
196
197 void wxTopLevelWindowX11::Restore()
198 {
199 // This is the way to deiconify the window, according to the X FAQ
200 if (m_iconized && GetMainWindow())
201 {
202 XMapWindow(wxGlobalDisplay(), (Window) GetMainWindow());
203 m_iconized = FALSE;
204 }
205 }
206
207 // ----------------------------------------------------------------------------
208 // wxTopLevelWindowX11 fullscreen
209 // ----------------------------------------------------------------------------
210
211 bool wxTopLevelWindowX11::ShowFullScreen(bool show, long style)
212 {
213 if (show)
214 {
215 if (IsFullScreen())
216 return FALSE;
217
218 m_fsIsShowing = TRUE;
219 m_fsStyle = style;
220
221 // TODO
222
223 return TRUE;
224 }
225 else
226 {
227 if (!IsFullScreen())
228 return FALSE;
229
230 m_fsIsShowing = FALSE;
231
232 // TODO
233 return TRUE;
234 }
235 }
236
237 // ----------------------------------------------------------------------------
238 // wxTopLevelWindowX11 misc
239 // ----------------------------------------------------------------------------
240
241 void wxTopLevelWindowX11::SetIcon(const wxIcon& icon)
242 {
243 // this sets m_icon
244 wxTopLevelWindowBase::SetIcon(icon);
245
246 if (icon.Ok() && GetMainWindow())
247 {
248 XWMHints *wmHints = XAllocWMHints();
249 wmHints->icon_pixmap = (Pixmap) icon.GetPixmap();
250
251 wmHints->flags = IconPixmapHint;
252
253 if (icon.GetMask())
254 {
255 wmHints->flags |= IconMaskHint;
256 wmHints->icon_mask = (Pixmap) icon.GetMask()->GetPixmap();
257 }
258
259 XSetWMHints(wxGlobalDisplay(), (Window) GetMainWindow(),
260 wmHints);
261 XFree(wmHints);
262 }
263 }
264
265 void wxTopLevelWindowX11::SetTitle(const wxString& title)
266 {
267 m_title = title;
268 if (GetMainWindow())
269 {
270 XStoreName(wxGlobalDisplay(), (Window) GetMainWindow(),
271 (const char*) title);
272 XSetIconName(wxGlobalDisplay(), (Window) GetMainWindow(),
273 (const char*) title);
274
275 // Use this if the platform doesn't supply the above functions.
276 #if 0
277 XTextProperty textProperty;
278 textProperty.value = (unsigned char*) title;
279 textProperty.encoding = XA_STRING;
280 textProperty.format = 8;
281 textProperty.nitems = 1;
282
283 XSetTextProperty(wxGlobalDisplay(), (Window) GetMainWindow(),
284 & textProperty, WM_NAME);
285 #endif
286 }
287 }
288
289 wxString wxTopLevelWindowX11::GetTitle() const
290 {
291 return m_title;
292 }
293
294 #ifndef MWM_DECOR_BORDER
295 /* bit definitions for MwmHints.flags */
296 #define MWM_HINTS_FUNCTIONS (1L << 0)
297 #define MWM_HINTS_DECORATIONS (1L << 1)
298 #define MWM_HINTS_INPUT_MODE (1L << 2)
299 #define MWM_HINTS_STATUS (1L << 3)
300
301 #define MWM_DECOR_ALL (1L << 0)
302 #define MWM_DECOR_BORDER (1L << 1)
303 #define MWM_DECOR_RESIZEH (1L << 2)
304 #define MWM_DECOR_TITLE (1L << 3)
305 #define MWM_DECOR_MENU (1L << 4)
306 #define MWM_DECOR_MINIMIZE (1L << 5)
307 #define MWM_DECOR_MAXIMIZE (1L << 6)
308 #endif
309
310 struct MwmHints {
311 long flags;
312 long functions;
313 long decorations;
314 long input_mode;
315 };
316
317 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
318
319 // Set the window manager decorations according to the
320 // given wxWindows style
321 bool wxSetWMDecorations(Window w, long style)
322 {
323 if (!wxMWMIsRunning(w))
324 return FALSE;
325
326 Atom mwm_wm_hints = XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False);
327 MwmHints hints;
328 hints.flags = 0;
329 hints.decorations = 0;
330
331 if (style & wxRESIZE_BORDER)
332 {
333 hints.flags |= MWM_HINTS_DECORATIONS;
334 hints.decorations |= MWM_DECOR_RESIZEH;
335 }
336
337 if (style & wxSYSTEM_MENU)
338 {
339 hints.flags |= MWM_HINTS_DECORATIONS;
340 hints.decorations |= MWM_DECOR_MENU;
341 }
342
343 if ((style & wxCAPTION) ||
344 (style & wxTINY_CAPTION_HORIZ) ||
345 (style & wxTINY_CAPTION_VERT))
346 {
347 hints.flags |= MWM_HINTS_DECORATIONS;
348 hints.decorations |= MWM_DECOR_TITLE;
349 }
350
351 if (style & wxTHICK_FRAME)
352 {
353 hints.flags |= MWM_HINTS_DECORATIONS;
354 hints.decorations |= MWM_DECOR_BORDER;
355 }
356
357 if (style & wxTHICK_FRAME)
358 {
359 hints.flags |= MWM_HINTS_DECORATIONS;
360 hints.decorations |= MWM_DECOR_BORDER;
361 }
362
363 if (style & wxMINIMIZE_BOX)
364 {
365 hints.flags |= MWM_HINTS_DECORATIONS;
366 hints.decorations |= MWM_DECOR_MINIMIZE;
367 }
368
369 if (style & wxMAXIMIZE_BOX)
370 {
371 hints.flags |= MWM_HINTS_DECORATIONS;
372 hints.decorations |= MWM_DECOR_MAXIMIZE;
373 }
374
375 XChangeProperty(wxGlobalDisplay(),
376 w,
377 mwm_wm_hints, mwm_wm_hints,
378 32, PropModeReplace,
379 (unsigned char *) &hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
380
381 return TRUE;
382 }
383
384 bool wxMWMIsRunning(Window w)
385 {
386 Display *dpy = (Display*)wxGetDisplay();
387 Atom motifWmInfo = XInternAtom(dpy, "_MOTIF_WM_INFO", False);
388
389 unsigned long length, bytesafter;
390 unsigned char value[20];
391 unsigned char *ptr = &value[0];
392 int ret, format;
393 Atom type;
394
395 type = format = length = 0;
396 value[0] = 0;
397
398 ret = XGetWindowProperty(wxGlobalDisplay(), w, motifWmInfo,
399 0L, 2, False, motifWmInfo,
400 &type, &format, &length, &bytesafter, &ptr);
401
402 return (ret == Success);
403 }
404