Background set correctly
[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 #include "wx/menu.h"
39 #include "wx/statusbr.h"
40 #endif //WX_PRECOMP
41
42 #include "wx/settings.h"
43 #include "wx/x11/private.h"
44 #include "X11/Xutil.h"
45
46 bool wxMWMIsRunning(Window w);
47
48 // ----------------------------------------------------------------------------
49 // wxTopLevelWindowX11 creation
50 // ----------------------------------------------------------------------------
51
52 void wxTopLevelWindowX11::Init()
53 {
54 m_iconized =
55 m_maximizeOnShow = FALSE;
56
57 // unlike (almost?) all other windows, frames are created hidden
58 m_isShown = FALSE;
59
60 // Data to save/restore when calling ShowFullScreen
61 m_fsStyle = 0;
62 m_fsIsMaximized = FALSE;
63 m_fsIsShowing = FALSE;
64 }
65
66 bool wxTopLevelWindowX11::Create(wxWindow *parent,
67 wxWindowID id,
68 const wxString& title,
69 const wxPoint& pos,
70 const wxSize& size,
71 long style,
72 const wxString& name)
73 {
74 // init our fields
75 Init();
76
77 m_windowStyle = style;
78 m_parent = parent;
79
80 SetName(name);
81
82 m_windowId = id == -1 ? NewControlId() : id;
83
84 if (parent)
85 parent->AddChild(this);
86
87 wxTopLevelWindows.Append(this);
88
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 );
94
95 // TODO: For dialogs, this should be wxSYS_COLOUR_3DFACE
96 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
97 m_backgroundColour.CalcPixel( (WXColormap) cm );
98
99 XSetWindowAttributes xattributes;
100 XSizeHints size_hints;
101 XWMHints wm_hints;
102
103 long xattributes_mask =
104 CWOverrideRedirect |
105 CWBorderPixel | CWBackPixel;
106 xattributes.background_pixel = m_backgroundColour.GetPixel();
107 xattributes.border_pixel = BlackPixel( xdisplay, xscreen );
108 xattributes.override_redirect = False;
109
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;
113
114 XSelectInput( xdisplay, xwindow,
115 ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
116 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
117 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
118 PropertyChangeMask );
119
120 wxAddWindowToTable( xwindow, (wxWindow*) this );
121
122 XSetTransientForHint( xdisplay, xwindow, xparent );
123
124 size_hints.flags = PSize;
125 size_hints.width = size.x;
126 size_hints.height = size.y;
127 XSetWMNormalHints( xdisplay, xwindow, &size_hints);
128
129 wm_hints.flags = InputHint | StateHint /* | WindowGroupHint */;
130 wm_hints.input = True;
131 wm_hints.initial_state = NormalState;
132 XSetWMHints( xdisplay, xwindow, &wm_hints);
133
134 Atom wm_delete_window = XInternAtom( xdisplay, "WM_DELETE_WINDOW", False);
135 XSetWMProtocols( xdisplay, xwindow, &wm_delete_window, 1);
136
137 wxSetWMDecorations((Window) GetMainWindow(), style);
138
139 SetTitle(title);
140
141 return TRUE;
142 }
143
144 wxTopLevelWindowX11::~wxTopLevelWindowX11()
145 {
146 wxTopLevelWindows.DeleteObject(this);
147
148 // If this is the last top-level window, exit.
149 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
150 {
151 wxTheApp->SetTopWindow(NULL);
152
153 if (wxTheApp->GetExitOnFrameDelete())
154 {
155 // Signal to the app that we're going to close
156 wxTheApp->ExitMainLoop();
157 }
158 }
159 }
160
161 // ----------------------------------------------------------------------------
162 // wxTopLevelWindowX11 showing
163 // ----------------------------------------------------------------------------
164
165 bool wxTopLevelWindowX11::Show(bool show)
166 {
167 return wxWindowX11::Show(show);
168 }
169
170 // ----------------------------------------------------------------------------
171 // wxTopLevelWindowX11 maximize/minimize
172 // ----------------------------------------------------------------------------
173
174 void wxTopLevelWindowX11::Maximize(bool maximize)
175 {
176 // TODO
177 }
178
179 bool wxTopLevelWindowX11::IsMaximized() const
180 {
181 // TODO
182 return TRUE;
183 }
184
185 void wxTopLevelWindowX11::Iconize(bool iconize)
186 {
187 if (!m_iconized && GetMainWindow())
188 {
189 if (XIconifyWindow(wxGlobalDisplay(),
190 (Window) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
191 m_iconized = TRUE;
192 }
193 }
194
195 bool wxTopLevelWindowX11::IsIconized() const
196 {
197 return m_iconized;
198 }
199
200 void wxTopLevelWindowX11::Restore()
201 {
202 // This is the way to deiconify the window, according to the X FAQ
203 if (m_iconized && GetMainWindow())
204 {
205 XMapWindow(wxGlobalDisplay(), (Window) GetMainWindow());
206 m_iconized = FALSE;
207 }
208 }
209
210 // ----------------------------------------------------------------------------
211 // wxTopLevelWindowX11 fullscreen
212 // ----------------------------------------------------------------------------
213
214 bool wxTopLevelWindowX11::ShowFullScreen(bool show, long style)
215 {
216 if (show)
217 {
218 if (IsFullScreen())
219 return FALSE;
220
221 m_fsIsShowing = TRUE;
222 m_fsStyle = style;
223
224 // TODO
225
226 return TRUE;
227 }
228 else
229 {
230 if (!IsFullScreen())
231 return FALSE;
232
233 m_fsIsShowing = FALSE;
234
235 // TODO
236 return TRUE;
237 }
238 }
239
240 // ----------------------------------------------------------------------------
241 // wxTopLevelWindowX11 misc
242 // ----------------------------------------------------------------------------
243
244 void wxTopLevelWindowX11::SetIcon(const wxIcon& icon)
245 {
246 // this sets m_icon
247 wxTopLevelWindowBase::SetIcon(icon);
248
249 if (icon.Ok() && GetMainWindow())
250 {
251 XWMHints *wmHints = XAllocWMHints();
252 wmHints->icon_pixmap = (Pixmap) icon.GetPixmap();
253
254 wmHints->flags = IconPixmapHint;
255
256 if (icon.GetMask())
257 {
258 wmHints->flags |= IconMaskHint;
259 wmHints->icon_mask = (Pixmap) icon.GetMask()->GetPixmap();
260 }
261
262 XSetWMHints(wxGlobalDisplay(), (Window) GetMainWindow(), wmHints);
263 XFree(wmHints);
264 }
265 }
266
267 void wxTopLevelWindowX11::SetTitle(const wxString& title)
268 {
269 m_title = title;
270 if (GetMainWindow())
271 {
272 XStoreName(wxGlobalDisplay(), (Window) GetMainWindow(),
273 (const char*) title);
274 XSetIconName(wxGlobalDisplay(), (Window) GetMainWindow(),
275 (const char*) title);
276
277 // Use this if the platform doesn't supply the above functions.
278 #if 0
279 XTextProperty textProperty;
280 textProperty.value = (unsigned char*) title;
281 textProperty.encoding = XA_STRING;
282 textProperty.format = 8;
283 textProperty.nitems = 1;
284
285 XSetTextProperty(wxGlobalDisplay(), (Window) GetMainWindow(),
286 & textProperty, WM_NAME);
287 #endif
288 }
289 }
290
291 wxString wxTopLevelWindowX11::GetTitle() const
292 {
293 return m_title;
294 }
295
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)
302
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)
310 #endif
311
312 struct MwmHints {
313 long flags;
314 long functions;
315 long decorations;
316 long input_mode;
317 };
318
319 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
320
321 // Set the window manager decorations according to the
322 // given wxWindows style
323 bool wxSetWMDecorations(Window w, long style)
324 {
325 if (!wxMWMIsRunning(w))
326 return FALSE;
327
328 Atom mwm_wm_hints = XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False);
329 MwmHints hints;
330 hints.flags = 0;
331 hints.decorations = 0;
332
333 if (style & wxRESIZE_BORDER)
334 {
335 hints.flags |= MWM_HINTS_DECORATIONS;
336 hints.decorations |= MWM_DECOR_RESIZEH;
337 }
338
339 if (style & wxSYSTEM_MENU)
340 {
341 hints.flags |= MWM_HINTS_DECORATIONS;
342 hints.decorations |= MWM_DECOR_MENU;
343 }
344
345 if ((style & wxCAPTION) ||
346 (style & wxTINY_CAPTION_HORIZ) ||
347 (style & wxTINY_CAPTION_VERT))
348 {
349 hints.flags |= MWM_HINTS_DECORATIONS;
350 hints.decorations |= MWM_DECOR_TITLE;
351 }
352
353 if (style & wxTHICK_FRAME)
354 {
355 hints.flags |= MWM_HINTS_DECORATIONS;
356 hints.decorations |= MWM_DECOR_BORDER;
357 }
358
359 if (style & wxTHICK_FRAME)
360 {
361 hints.flags |= MWM_HINTS_DECORATIONS;
362 hints.decorations |= MWM_DECOR_BORDER;
363 }
364
365 if (style & wxMINIMIZE_BOX)
366 {
367 hints.flags |= MWM_HINTS_DECORATIONS;
368 hints.decorations |= MWM_DECOR_MINIMIZE;
369 }
370
371 if (style & wxMAXIMIZE_BOX)
372 {
373 hints.flags |= MWM_HINTS_DECORATIONS;
374 hints.decorations |= MWM_DECOR_MAXIMIZE;
375 }
376
377 XChangeProperty(wxGlobalDisplay(),
378 w,
379 mwm_wm_hints, mwm_wm_hints,
380 32, PropModeReplace,
381 (unsigned char *) &hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
382
383 return TRUE;
384 }
385
386 bool wxMWMIsRunning(Window w)
387 {
388 Display *dpy = (Display*)wxGetDisplay();
389 Atom motifWmInfo = XInternAtom(dpy, "_MOTIF_WM_INFO", False);
390
391 unsigned long length, bytesafter;
392 unsigned char value[20];
393 unsigned char *ptr = &value[0];
394 int ret, format;
395 Atom type;
396
397 type = format = length = 0;
398 value[0] = 0;
399
400 ret = XGetWindowProperty(wxGlobalDisplay(), w, motifWmInfo,
401 0L, 2, False, motifWmInfo,
402 &type, &format, &length, &bytesafter, &ptr);
403
404 return (ret == Success);
405 }
406
407 // For implementation purposes - sometimes decorations make the client area
408 // smaller
409 wxPoint wxTopLevelWindowX11::GetClientAreaOrigin() const
410 {
411 // In fact wxFrame::GetClientAreaOrigin
412 // does the required calculation already.
413 #if 0
414 if (this->IsKindOf(CLASSINFO(wxFrame)))
415 {
416 wxFrame* frame = (wxFrame*) this;
417 if (frame->GetMenuBar())
418 return wxPoint(0, frame->GetMenuBar()->GetSize().y);
419 }
420 #endif
421 return wxPoint(0, 0);
422 }
423
424 void wxTopLevelWindowX11::DoGetClientSize( int *width, int *height ) const
425 {
426 wxWindowX11::DoGetClientSize(width, height);
427 // Done by wxTopLevelWindow
428 #if 0
429 if (this->IsKindOf(CLASSINFO(wxFrame)))
430 {
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;
436 }
437 #endif
438 }
439
440 void wxTopLevelWindowX11::DoSetClientSize(int width, int height)
441 {
442 wxWindowX11::DoSetClientSize(width, height);
443 #if 0
444 if (!GetMainWindow())
445 return;
446
447 XWindowChanges windowChanges;
448 int valueMask = 0;
449
450 if (width != -1)
451 {
452 windowChanges.width = width ;
453 valueMask |= CWWidth;
454 }
455 if (height != -1)
456 {
457 windowChanges.height = height ;
458 valueMask |= CWHeight;
459 }
460 XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(),
461 valueMask, & windowChanges);
462 #endif
463 }