wxX11:
[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 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG)
96 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
97 else
98 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
99 m_backgroundColour.CalcPixel( (WXColormap) cm );
100 m_hasBgCol = TRUE;
101
102 XSetWindowAttributes xattributes;
103 XSizeHints size_hints;
104 XWMHints wm_hints;
105
106 long xattributes_mask =
107 CWOverrideRedirect |
108 CWBorderPixel | CWBackPixel;
109 xattributes.background_pixel = m_backgroundColour.GetPixel();
110 xattributes.border_pixel = BlackPixel( xdisplay, xscreen );
111
112 // TODO: if we want no border, caption etc.,
113 // I think we set this to True to remove decorations
114 xattributes.override_redirect = False;
115
116 wxSize size2(size);
117 if (size2.x == -1)
118 size2.x = 100;
119 if (size2.y == -1)
120 size2.y = 100;
121
122 wxPoint pos2(pos);
123 if (pos2.x == -1)
124 pos2.x = 100;
125 if (pos2.y == -1)
126 pos2.y = 100;
127
128 Window xwindow = XCreateWindow( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
129 0, DefaultDepth(xdisplay,xscreen), InputOutput, xvisual, xattributes_mask, &xattributes );
130 m_mainWidget = (WXWindow) xwindow;
131
132 XSelectInput( xdisplay, xwindow,
133 ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
134 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
135 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
136 PropertyChangeMask );
137
138 wxAddWindowToTable( xwindow, (wxWindow*) this );
139
140 // Set background to None which will prevent X11 from clearing the
141 // background completely.
142 XSetWindowBackgroundPixmap( xdisplay, xwindow, None );
143
144 // Messes up window management
145 // XSetTransientForHint( xdisplay, xwindow, xparent );
146
147 size_hints.flags = PSize;
148 size_hints.width = size2.x;
149 size_hints.height = size2.y;
150 XSetWMNormalHints( xdisplay, xwindow, &size_hints);
151
152 wm_hints.flags = InputHint | StateHint /* | WindowGroupHint */;
153 wm_hints.input = True;
154 wm_hints.initial_state = NormalState;
155 XSetWMHints( xdisplay, xwindow, &wm_hints);
156
157 Atom wm_delete_window = XInternAtom( xdisplay, "WM_DELETE_WINDOW", False);
158 XSetWMProtocols( xdisplay, xwindow, &wm_delete_window, 1);
159
160 wxSetWMDecorations((Window) GetMainWindow(), style);
161
162 SetTitle(title);
163
164 return TRUE;
165 }
166
167 wxTopLevelWindowX11::~wxTopLevelWindowX11()
168 {
169 wxTopLevelWindows.DeleteObject(this);
170
171 // If this is the last top-level window, exit.
172 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
173 {
174 wxTheApp->SetTopWindow(NULL);
175
176 if (wxTheApp->GetExitOnFrameDelete())
177 {
178 // Signal to the app that we're going to close
179 wxTheApp->ExitMainLoop();
180 }
181 }
182 }
183
184 // ----------------------------------------------------------------------------
185 // wxTopLevelWindowX11 showing
186 // ----------------------------------------------------------------------------
187
188 bool wxTopLevelWindowX11::Show(bool show)
189 {
190 return wxWindowX11::Show(show);
191 }
192
193 // ----------------------------------------------------------------------------
194 // wxTopLevelWindowX11 maximize/minimize
195 // ----------------------------------------------------------------------------
196
197 void wxTopLevelWindowX11::Maximize(bool maximize)
198 {
199 // TODO
200 }
201
202 bool wxTopLevelWindowX11::IsMaximized() const
203 {
204 // TODO
205 return TRUE;
206 }
207
208 void wxTopLevelWindowX11::Iconize(bool iconize)
209 {
210 if (!m_iconized && GetMainWindow())
211 {
212 if (XIconifyWindow(wxGlobalDisplay(),
213 (Window) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
214 m_iconized = TRUE;
215 }
216 }
217
218 bool wxTopLevelWindowX11::IsIconized() const
219 {
220 return m_iconized;
221 }
222
223 void wxTopLevelWindowX11::Restore()
224 {
225 // This is the way to deiconify the window, according to the X FAQ
226 if (m_iconized && GetMainWindow())
227 {
228 XMapWindow(wxGlobalDisplay(), (Window) GetMainWindow());
229 m_iconized = FALSE;
230 }
231 }
232
233 // ----------------------------------------------------------------------------
234 // wxTopLevelWindowX11 fullscreen
235 // ----------------------------------------------------------------------------
236
237 bool wxTopLevelWindowX11::ShowFullScreen(bool show, long style)
238 {
239 if (show)
240 {
241 if (IsFullScreen())
242 return FALSE;
243
244 m_fsIsShowing = TRUE;
245 m_fsStyle = style;
246
247 // TODO
248
249 return TRUE;
250 }
251 else
252 {
253 if (!IsFullScreen())
254 return FALSE;
255
256 m_fsIsShowing = FALSE;
257
258 // TODO
259 return TRUE;
260 }
261 }
262
263 // ----------------------------------------------------------------------------
264 // wxTopLevelWindowX11 misc
265 // ----------------------------------------------------------------------------
266
267 void wxTopLevelWindowX11::SetIcon(const wxIcon& icon)
268 {
269 // this sets m_icon
270 wxTopLevelWindowBase::SetIcon(icon);
271
272 if (icon.Ok() && GetMainWindow())
273 {
274 XWMHints *wmHints = XAllocWMHints();
275 wmHints->icon_pixmap = (Pixmap) icon.GetPixmap();
276
277 wmHints->flags = IconPixmapHint;
278
279 if (icon.GetMask())
280 {
281 wmHints->flags |= IconMaskHint;
282 wmHints->icon_mask = (Pixmap) icon.GetMask()->GetBitmap();
283 }
284
285 XSetWMHints(wxGlobalDisplay(), (Window) GetMainWindow(), wmHints);
286 XFree(wmHints);
287 }
288 }
289
290 void wxTopLevelWindowX11::SetTitle(const wxString& title)
291 {
292 m_title = title;
293 if (GetMainWindow())
294 {
295 XStoreName(wxGlobalDisplay(), (Window) GetMainWindow(),
296 (const char*) title);
297 XSetIconName(wxGlobalDisplay(), (Window) GetMainWindow(),
298 (const char*) title);
299
300 // Use this if the platform doesn't supply the above functions.
301 #if 0
302 XTextProperty textProperty;
303 textProperty.value = (unsigned char*) title;
304 textProperty.encoding = XA_STRING;
305 textProperty.format = 8;
306 textProperty.nitems = 1;
307
308 XSetTextProperty(wxGlobalDisplay(), (Window) GetMainWindow(),
309 & textProperty, WM_NAME);
310 #endif
311 }
312 }
313
314 wxString wxTopLevelWindowX11::GetTitle() const
315 {
316 return m_title;
317 }
318
319 #ifndef MWM_DECOR_BORDER
320 /* bit definitions for MwmHints.flags */
321 #define MWM_HINTS_FUNCTIONS (1L << 0)
322 #define MWM_HINTS_DECORATIONS (1L << 1)
323 #define MWM_HINTS_INPUT_MODE (1L << 2)
324 #define MWM_HINTS_STATUS (1L << 3)
325
326 #define MWM_DECOR_ALL (1L << 0)
327 #define MWM_DECOR_BORDER (1L << 1)
328 #define MWM_DECOR_RESIZEH (1L << 2)
329 #define MWM_DECOR_TITLE (1L << 3)
330 #define MWM_DECOR_MENU (1L << 4)
331 #define MWM_DECOR_MINIMIZE (1L << 5)
332 #define MWM_DECOR_MAXIMIZE (1L << 6)
333 #endif
334
335 struct MwmHints {
336 long flags;
337 long functions;
338 long decorations;
339 long input_mode;
340 };
341
342 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
343
344 // Set the window manager decorations according to the
345 // given wxWindows style
346 bool wxSetWMDecorations(Window w, long style)
347 {
348 if (!wxMWMIsRunning(w))
349 return FALSE;
350
351 Atom mwm_wm_hints = XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False);
352 MwmHints hints;
353 hints.flags = 0;
354 hints.decorations = 0;
355
356 if (style & wxRESIZE_BORDER)
357 {
358 wxLogDebug("MWM_DECOR_RESIZEH");
359 hints.flags |= MWM_HINTS_DECORATIONS;
360 hints.decorations |= MWM_DECOR_RESIZEH;
361 }
362
363 if (style & wxSYSTEM_MENU)
364 {
365 wxLogDebug("MWM_DECOR_MENU");
366 hints.flags |= MWM_HINTS_DECORATIONS;
367 hints.decorations |= MWM_DECOR_MENU;
368 }
369
370 if ((style & wxCAPTION) ||
371 (style & wxTINY_CAPTION_HORIZ) ||
372 (style & wxTINY_CAPTION_VERT))
373 {
374 wxLogDebug("MWM_DECOR_TITLE");
375 hints.flags |= MWM_HINTS_DECORATIONS;
376 hints.decorations |= MWM_DECOR_TITLE;
377 }
378
379 if ((style & wxTHICK_FRAME) || (style & wxSIMPLE_BORDER) || (style & wxCAPTION))
380 {
381 wxLogDebug("MWM_DECOR_BORDER");
382 hints.flags |= MWM_HINTS_DECORATIONS;
383 hints.decorations |= MWM_DECOR_BORDER;
384 }
385
386 if (style & wxMINIMIZE_BOX)
387 {
388 wxLogDebug("MWM_DECOR_MINIMIZE");
389 hints.flags |= MWM_HINTS_DECORATIONS;
390 hints.decorations |= MWM_DECOR_MINIMIZE;
391 }
392
393 if (style & wxMAXIMIZE_BOX)
394 {
395 wxLogDebug("MWM_DECOR_MAXIMIZE");
396 hints.flags |= MWM_HINTS_DECORATIONS;
397 hints.decorations |= MWM_DECOR_MAXIMIZE;
398 }
399
400 XChangeProperty(wxGlobalDisplay(),
401 w,
402 mwm_wm_hints, mwm_wm_hints,
403 32, PropModeReplace,
404 (unsigned char *) &hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
405
406 return TRUE;
407 }
408
409 bool wxMWMIsRunning(Window w)
410 {
411 Display *dpy = (Display*)wxGetDisplay();
412 Atom motifWmInfo = XInternAtom(dpy, "_MOTIF_WM_INFO", False);
413
414 unsigned long length, bytesafter;
415 unsigned char value[20];
416 unsigned char *ptr = &value[0];
417 int ret, format;
418 Atom type;
419
420 type = format = length = 0;
421 value[0] = 0;
422
423 ret = XGetWindowProperty(wxGlobalDisplay(), w, motifWmInfo,
424 0L, 2, False, motifWmInfo,
425 &type, &format, &length, &bytesafter, &ptr);
426
427 return (ret == Success);
428 }
429
430 // For implementation purposes - sometimes decorations make the client area
431 // smaller
432 wxPoint wxTopLevelWindowX11::GetClientAreaOrigin() const
433 {
434 // In fact wxFrame::GetClientAreaOrigin
435 // does the required calculation already.
436 #if 0
437 if (this->IsKindOf(CLASSINFO(wxFrame)))
438 {
439 wxFrame* frame = (wxFrame*) this;
440 if (frame->GetMenuBar())
441 return wxPoint(0, frame->GetMenuBar()->GetSize().y);
442 }
443 #endif
444 return wxPoint(0, 0);
445 }
446
447 void wxTopLevelWindowX11::DoGetClientSize( int *width, int *height ) const
448 {
449 wxWindowX11::DoGetClientSize(width, height);
450 // Done by wxTopLevelWindow
451 #if 0
452 if (this->IsKindOf(CLASSINFO(wxFrame)))
453 {
454 wxFrame* frame = (wxFrame*) this;
455 if (frame->GetMenuBar())
456 (*height) -= frame->GetMenuBar()->GetSize().y;
457 if (frame->GetStatusBar())
458 (*height) -= frame->GetStatusBar()->GetSize().y;
459 }
460 #endif
461 }
462
463 void wxTopLevelWindowX11::DoSetClientSize(int width, int height)
464 {
465 wxWindowX11::DoSetClientSize(width, height);
466
467 #if 0
468 if (!GetMainWindow())
469 return;
470
471 XWindowChanges windowChanges;
472 int valueMask = 0;
473
474 if (width != -1)
475 {
476 windowChanges.width = width ;
477 valueMask |= CWWidth;
478 }
479 if (height != -1)
480 {
481 windowChanges.height = height ;
482 valueMask |= CWHeight;
483 }
484 XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(),
485 valueMask, & windowChanges);
486 #endif
487 }
488
489 void wxTopLevelWindowX11::DoSetSize(int x, int y, int width, int height, int sizeFlags)
490 {
491 wxString msg;
492 msg.Printf("Setting pos: %d, %d", x, y);
493 wxLogDebug(msg);
494 wxWindowX11::DoSetSize(x, y, width, height, sizeFlags);
495
496 wxPoint pt = GetPosition();
497 msg.Printf("After, pos: %d, %d", pt.x, pt.y);
498 wxLogDebug(msg);
499 #if 0
500 XSync(wxGlobalDisplay(), False);
501 int w, h;
502 GetSize(& w, & h);
503 wxString msg;
504 msg.Printf("Before setting size: %d, %d", w, h);
505 wxLogDebug(msg);
506 if (!GetMainWindow())
507 return;
508
509 XWindowChanges windowChanges;
510 int valueMask = 0;
511
512 if (x != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
513 {
514 int yy = 0;
515 AdjustForParentClientOrigin( x, yy, sizeFlags);
516 windowChanges.x = x;
517 valueMask |= CWX;
518 }
519 if (y != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
520 {
521 int xx = 0;
522 AdjustForParentClientOrigin( xx, y, sizeFlags);
523 windowChanges.y = y;
524 valueMask |= CWY;
525 }
526 if (width != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
527 {
528 windowChanges.width = width /* - m_borderSize*2 */;
529 valueMask |= CWWidth;
530 }
531 if (height != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
532 {
533 windowChanges.height = height /* -m_borderSize*2*/;
534 valueMask |= CWHeight;
535 }
536
537 XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(),
538 valueMask, & windowChanges);
539 XSync(wxGlobalDisplay(), False);
540 GetSize(& w, & h);
541 msg.Printf("Tried to set to %d, %d. After setting size: %d, %d", width, height, w, h);
542 wxLogDebug(msg);
543 #endif
544 }
545
546 void wxTopLevelWindowX11::DoGetPosition(int *x, int *y) const
547 {
548 XSync(wxGlobalDisplay(), False);
549 Window window = (Window) m_mainWidget;
550 if (window)
551 {
552 int offsetX = 0;
553 int offsetY = 0;
554
555 wxLogDebug("Translating...");
556 Window childWindow;
557 XTranslateCoordinates(wxGlobalDisplay(), window, XDefaultRootWindow(wxGlobalDisplay()),
558 0, 0, & offsetX, & offsetY, & childWindow);
559
560 wxString msg;
561 msg.Printf("Offset: %d, %d", offsetX, offsetY);
562 wxLogDebug(msg);
563
564 XWindowAttributes attr;
565 Status status = XGetWindowAttributes(wxGlobalDisplay(), window, & attr);
566 wxASSERT(status);
567
568 if (status)
569 {
570 *x = attr.x + offsetX;
571 *y = attr.y + offsetY;
572 }
573 }
574 }