Added border for top level windows with captions, though
[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 m_hasBgCol = TRUE;
99
100 XSetWindowAttributes xattributes;
101 XSizeHints size_hints;
102 XWMHints wm_hints;
103
104 long xattributes_mask =
105 CWOverrideRedirect |
106 CWBorderPixel | CWBackPixel;
107 xattributes.background_pixel = m_backgroundColour.GetPixel();
108 xattributes.border_pixel = BlackPixel( xdisplay, xscreen );
109
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;
113
114 wxSize size2(size);
115 if (size2.x == -1)
116 size2.x = 100;
117 if (size2.y == -1)
118 size2.y = 100;
119
120 wxPoint pos2(pos);
121 if (pos2.x == -1)
122 pos2.x = 100;
123 if (pos2.y == -1)
124 pos2.y = 100;
125
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;
129
130 XSelectInput( xdisplay, xwindow,
131 ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
132 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
133 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
134 PropertyChangeMask );
135
136 wxAddWindowToTable( xwindow, (wxWindow*) this );
137
138 // Messes up window management
139 // XSetTransientForHint( xdisplay, xwindow, xparent );
140
141 size_hints.flags = PSize;
142 size_hints.width = size2.x;
143 size_hints.height = size2.y;
144 XSetWMNormalHints( xdisplay, xwindow, &size_hints);
145
146 wm_hints.flags = InputHint | StateHint /* | WindowGroupHint */;
147 wm_hints.input = True;
148 wm_hints.initial_state = NormalState;
149 XSetWMHints( xdisplay, xwindow, &wm_hints);
150
151 Atom wm_delete_window = XInternAtom( xdisplay, "WM_DELETE_WINDOW", False);
152 XSetWMProtocols( xdisplay, xwindow, &wm_delete_window, 1);
153
154 wxSetWMDecorations((Window) GetMainWindow(), style);
155
156 SetTitle(title);
157
158 return TRUE;
159 }
160
161 wxTopLevelWindowX11::~wxTopLevelWindowX11()
162 {
163 wxTopLevelWindows.DeleteObject(this);
164
165 // If this is the last top-level window, exit.
166 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
167 {
168 wxTheApp->SetTopWindow(NULL);
169
170 if (wxTheApp->GetExitOnFrameDelete())
171 {
172 // Signal to the app that we're going to close
173 wxTheApp->ExitMainLoop();
174 }
175 }
176 }
177
178 // ----------------------------------------------------------------------------
179 // wxTopLevelWindowX11 showing
180 // ----------------------------------------------------------------------------
181
182 bool wxTopLevelWindowX11::Show(bool show)
183 {
184 return wxWindowX11::Show(show);
185 }
186
187 // ----------------------------------------------------------------------------
188 // wxTopLevelWindowX11 maximize/minimize
189 // ----------------------------------------------------------------------------
190
191 void wxTopLevelWindowX11::Maximize(bool maximize)
192 {
193 // TODO
194 }
195
196 bool wxTopLevelWindowX11::IsMaximized() const
197 {
198 // TODO
199 return TRUE;
200 }
201
202 void wxTopLevelWindowX11::Iconize(bool iconize)
203 {
204 if (!m_iconized && GetMainWindow())
205 {
206 if (XIconifyWindow(wxGlobalDisplay(),
207 (Window) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
208 m_iconized = TRUE;
209 }
210 }
211
212 bool wxTopLevelWindowX11::IsIconized() const
213 {
214 return m_iconized;
215 }
216
217 void wxTopLevelWindowX11::Restore()
218 {
219 // This is the way to deiconify the window, according to the X FAQ
220 if (m_iconized && GetMainWindow())
221 {
222 XMapWindow(wxGlobalDisplay(), (Window) GetMainWindow());
223 m_iconized = FALSE;
224 }
225 }
226
227 // ----------------------------------------------------------------------------
228 // wxTopLevelWindowX11 fullscreen
229 // ----------------------------------------------------------------------------
230
231 bool wxTopLevelWindowX11::ShowFullScreen(bool show, long style)
232 {
233 if (show)
234 {
235 if (IsFullScreen())
236 return FALSE;
237
238 m_fsIsShowing = TRUE;
239 m_fsStyle = style;
240
241 // TODO
242
243 return TRUE;
244 }
245 else
246 {
247 if (!IsFullScreen())
248 return FALSE;
249
250 m_fsIsShowing = FALSE;
251
252 // TODO
253 return TRUE;
254 }
255 }
256
257 // ----------------------------------------------------------------------------
258 // wxTopLevelWindowX11 misc
259 // ----------------------------------------------------------------------------
260
261 void wxTopLevelWindowX11::SetIcon(const wxIcon& icon)
262 {
263 // this sets m_icon
264 wxTopLevelWindowBase::SetIcon(icon);
265
266 if (icon.Ok() && GetMainWindow())
267 {
268 XWMHints *wmHints = XAllocWMHints();
269 wmHints->icon_pixmap = (Pixmap) icon.GetPixmap();
270
271 wmHints->flags = IconPixmapHint;
272
273 if (icon.GetMask())
274 {
275 wmHints->flags |= IconMaskHint;
276 wmHints->icon_mask = (Pixmap) icon.GetMask()->GetBitmap();
277 }
278
279 XSetWMHints(wxGlobalDisplay(), (Window) GetMainWindow(), wmHints);
280 XFree(wmHints);
281 }
282 }
283
284 void wxTopLevelWindowX11::SetTitle(const wxString& title)
285 {
286 m_title = title;
287 if (GetMainWindow())
288 {
289 XStoreName(wxGlobalDisplay(), (Window) GetMainWindow(),
290 (const char*) title);
291 XSetIconName(wxGlobalDisplay(), (Window) GetMainWindow(),
292 (const char*) title);
293
294 // Use this if the platform doesn't supply the above functions.
295 #if 0
296 XTextProperty textProperty;
297 textProperty.value = (unsigned char*) title;
298 textProperty.encoding = XA_STRING;
299 textProperty.format = 8;
300 textProperty.nitems = 1;
301
302 XSetTextProperty(wxGlobalDisplay(), (Window) GetMainWindow(),
303 & textProperty, WM_NAME);
304 #endif
305 }
306 }
307
308 wxString wxTopLevelWindowX11::GetTitle() const
309 {
310 return m_title;
311 }
312
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)
319
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)
327 #endif
328
329 struct MwmHints {
330 long flags;
331 long functions;
332 long decorations;
333 long input_mode;
334 };
335
336 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
337
338 // Set the window manager decorations according to the
339 // given wxWindows style
340 bool wxSetWMDecorations(Window w, long style)
341 {
342 if (!wxMWMIsRunning(w))
343 return FALSE;
344
345 Atom mwm_wm_hints = XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False);
346 MwmHints hints;
347 hints.flags = 0;
348 hints.decorations = 0;
349
350 if (style & wxRESIZE_BORDER)
351 {
352 wxLogDebug("MWM_DECOR_RESIZEH");
353 hints.flags |= MWM_HINTS_DECORATIONS;
354 hints.decorations |= MWM_DECOR_RESIZEH;
355 }
356
357 if (style & wxSYSTEM_MENU)
358 {
359 wxLogDebug("MWM_DECOR_MENU");
360 hints.flags |= MWM_HINTS_DECORATIONS;
361 hints.decorations |= MWM_DECOR_MENU;
362 }
363
364 if ((style & wxCAPTION) ||
365 (style & wxTINY_CAPTION_HORIZ) ||
366 (style & wxTINY_CAPTION_VERT))
367 {
368 wxLogDebug("MWM_DECOR_TITLE");
369 hints.flags |= MWM_HINTS_DECORATIONS;
370 hints.decorations |= MWM_DECOR_TITLE;
371 }
372
373 if ((style & wxTHICK_FRAME) || (style & wxSIMPLE_BORDER) || (style & wxCAPTION))
374 {
375 wxLogDebug("MWM_DECOR_BORDER");
376 hints.flags |= MWM_HINTS_DECORATIONS;
377 hints.decorations |= MWM_DECOR_BORDER;
378 }
379
380 if (style & wxMINIMIZE_BOX)
381 {
382 wxLogDebug("MWM_DECOR_MINIMIZE");
383 hints.flags |= MWM_HINTS_DECORATIONS;
384 hints.decorations |= MWM_DECOR_MINIMIZE;
385 }
386
387 if (style & wxMAXIMIZE_BOX)
388 {
389 wxLogDebug("MWM_DECOR_MAXIMIZE");
390 hints.flags |= MWM_HINTS_DECORATIONS;
391 hints.decorations |= MWM_DECOR_MAXIMIZE;
392 }
393
394 XChangeProperty(wxGlobalDisplay(),
395 w,
396 mwm_wm_hints, mwm_wm_hints,
397 32, PropModeReplace,
398 (unsigned char *) &hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
399
400 return TRUE;
401 }
402
403 bool wxMWMIsRunning(Window w)
404 {
405 Display *dpy = (Display*)wxGetDisplay();
406 Atom motifWmInfo = XInternAtom(dpy, "_MOTIF_WM_INFO", False);
407
408 unsigned long length, bytesafter;
409 unsigned char value[20];
410 unsigned char *ptr = &value[0];
411 int ret, format;
412 Atom type;
413
414 type = format = length = 0;
415 value[0] = 0;
416
417 ret = XGetWindowProperty(wxGlobalDisplay(), w, motifWmInfo,
418 0L, 2, False, motifWmInfo,
419 &type, &format, &length, &bytesafter, &ptr);
420
421 return (ret == Success);
422 }
423
424 // For implementation purposes - sometimes decorations make the client area
425 // smaller
426 wxPoint wxTopLevelWindowX11::GetClientAreaOrigin() const
427 {
428 // In fact wxFrame::GetClientAreaOrigin
429 // does the required calculation already.
430 #if 0
431 if (this->IsKindOf(CLASSINFO(wxFrame)))
432 {
433 wxFrame* frame = (wxFrame*) this;
434 if (frame->GetMenuBar())
435 return wxPoint(0, frame->GetMenuBar()->GetSize().y);
436 }
437 #endif
438 return wxPoint(0, 0);
439 }
440
441 void wxTopLevelWindowX11::DoGetClientSize( int *width, int *height ) const
442 {
443 wxWindowX11::DoGetClientSize(width, height);
444 // Done by wxTopLevelWindow
445 #if 0
446 if (this->IsKindOf(CLASSINFO(wxFrame)))
447 {
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;
453 }
454 #endif
455 }
456
457 void wxTopLevelWindowX11::DoSetClientSize(int width, int height)
458 {
459 wxWindowX11::DoSetClientSize(width, height);
460 #if 0
461 if (!GetMainWindow())
462 return;
463
464 XWindowChanges windowChanges;
465 int valueMask = 0;
466
467 if (width != -1)
468 {
469 windowChanges.width = width ;
470 valueMask |= CWWidth;
471 }
472 if (height != -1)
473 {
474 windowChanges.height = height ;
475 valueMask |= CWHeight;
476 }
477 XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(),
478 valueMask, & windowChanges);
479 #endif
480 }