Compilation fixes for Nano-X
[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 m_focusWidget = NULL;
66 }
67
68 bool wxTopLevelWindowX11::Create(wxWindow *parent,
69 wxWindowID id,
70 const wxString& title,
71 const wxPoint& pos,
72 const wxSize& size,
73 long style,
74 const wxString& name)
75 {
76 // init our fields
77 Init();
78
79 m_windowStyle = style;
80 m_parent = parent;
81
82 SetName(name);
83
84 m_windowId = id == -1 ? NewControlId() : id;
85
86 if (parent)
87 parent->AddChild(this);
88
89 wxTopLevelWindows.Append(this);
90
91 Display *xdisplay = wxGlobalDisplay();
92 int xscreen = DefaultScreen( xdisplay );
93 Visual *xvisual = DefaultVisual( xdisplay, xscreen );
94 Window xparent = RootWindow( xdisplay, xscreen );
95 Colormap cm = DefaultColormap( xdisplay, xscreen );
96
97 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG)
98 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
99 else
100 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
101 m_backgroundColour.CalcPixel( (WXColormap) cm );
102 m_hasBgCol = TRUE;
103
104 #if !wxUSE_NANOX
105 XSetWindowAttributes xattributes;
106 XSizeHints size_hints;
107
108 long xattributes_mask =
109 CWOverrideRedirect |
110 CWBorderPixel | CWBackPixel;
111
112 xattributes.background_pixel = m_backgroundColour.GetPixel();
113 xattributes.border_pixel = BlackPixel( xdisplay, xscreen );
114
115 // TODO: if we want no border, caption etc.,
116 // I think we set this to True to remove decorations
117 // No. RR.
118 xattributes.override_redirect = False;
119 #endif
120
121 wxSize size2(size);
122 if (size2.x == -1)
123 size2.x = 100;
124 if (size2.y == -1)
125 size2.y = 100;
126
127 wxPoint pos2(pos);
128 if (pos2.x == -1)
129 pos2.x = 100;
130 if (pos2.y == -1)
131 pos2.y = 100;
132
133 #if wxUSE_NANOX
134 long backColor, foreColor;
135 backColor = GR_RGB(m_backgroundColour.Red(), m_backgroundColour.Green(), m_backgroundColour.Blue());
136 foreColor = GR_RGB(m_foregroundColour.Red(), m_foregroundColour.Green(), m_foregroundColour.Blue());
137
138 Window xwindow = XCreateWindowWithColor( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
139 0, 0, InputOutput, xvisual, backColor, foreColor);
140 #else
141 Window xwindow = XCreateWindow( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
142 0, DefaultDepth(xdisplay,xscreen), InputOutput, xvisual, xattributes_mask, &xattributes );
143 #endif
144 m_mainWidget = (WXWindow) xwindow;
145
146 int extraFlags = 0;
147 #if wxUSE_NANOX
148 extraFlags |= GR_EVENT_MASK_CLOSE_REQ;
149 #endif
150
151 #if wxUSE_NANOX
152 XSelectInput( xdisplay, xwindow,
153 extraFlags |
154 ExposureMask |
155 KeyPressMask |
156 KeyReleaseMask |
157 ButtonPressMask |
158 ButtonReleaseMask |
159 ButtonMotionMask |
160 EnterWindowMask |
161 LeaveWindowMask |
162 PointerMotionMask |
163 KeymapStateMask |
164 FocusChangeMask |
165 ColormapChangeMask |
166 StructureNotifyMask |
167 PropertyChangeMask
168 );
169 #else
170 XSelectInput( xdisplay, xwindow,
171 extraFlags |
172 ExposureMask |
173 KeyPressMask |
174 KeyReleaseMask |
175 ButtonPressMask |
176 ButtonReleaseMask |
177 ButtonMotionMask |
178 EnterWindowMask |
179 LeaveWindowMask |
180 PointerMotionMask |
181 KeymapStateMask |
182 FocusChangeMask |
183 ColormapChangeMask |
184 StructureNotifyMask |
185 PropertyChangeMask
186 );
187 #endif
188
189 wxAddWindowToTable( xwindow, (wxWindow*) this );
190
191 // Set background to None which will prevent X11 from clearing the
192 // background completely.
193 XSetWindowBackgroundPixmap( xdisplay, xwindow, None );
194
195 #if !wxUSE_NANOX
196 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG)
197 {
198 if (GetParent() && GetParent()->GetMainWindow())
199 {
200 Window xparentwindow = (Window) GetParent()->GetMainWindow();
201 XSetTransientForHint( xdisplay, xwindow, xparentwindow );
202 }
203 }
204
205 size_hints.flags = PSize | PPosition;
206 size_hints.x = pos2.x;
207 size_hints.y = pos2.y;
208 size_hints.width = size2.x;
209 size_hints.height = size2.y;
210 XSetWMNormalHints( xdisplay, xwindow, &size_hints);
211
212 XWMHints wm_hints;
213 wm_hints.flags = InputHint | StateHint /* | WindowGroupHint */;
214 wm_hints.input = True;
215 wm_hints.initial_state = NormalState;
216 XSetWMHints( xdisplay, xwindow, &wm_hints);
217
218 Atom wm_protocols[2];
219 wm_protocols[0] = XInternAtom( xdisplay, "WM_DELETE_WINDOW", False );
220 wm_protocols[1] = XInternAtom( xdisplay, "WM_TAKE_FOCUS", False );
221 XSetWMProtocols( xdisplay, xwindow, wm_protocols, 2);
222 #endif
223
224 #if 0 // wxUSE_NANOX
225 GR_WM_PROPERTIES props;
226 props.flags = GR_WM_FLAGS_TITLE;
227 props.title = (GR_CHAR*) "Hello";
228 GrSetWMProperties(xwindow, &props);
229 #else
230 wxSetWMDecorations( xwindow, style);
231 #endif
232
233 SetTitle(title);
234
235 return TRUE;
236 }
237
238 wxTopLevelWindowX11::~wxTopLevelWindowX11()
239 {
240 wxTopLevelWindows.DeleteObject(this);
241
242 // If this is the last top-level window, exit.
243 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
244 {
245 wxTheApp->SetTopWindow(NULL);
246
247 if (wxTheApp->GetExitOnFrameDelete())
248 {
249 // Signal to the app that we're going to close
250 wxTheApp->ExitMainLoop();
251 }
252 }
253 }
254
255 // ----------------------------------------------------------------------------
256 // wxTopLevelWindowX11 showing
257 // ----------------------------------------------------------------------------
258
259 bool wxTopLevelWindowX11::Show(bool show)
260 {
261 return wxWindowX11::Show(show);
262 }
263
264 // ----------------------------------------------------------------------------
265 // wxTopLevelWindowX11 maximize/minimize
266 // ----------------------------------------------------------------------------
267
268 void wxTopLevelWindowX11::Maximize(bool maximize)
269 {
270 // TODO
271 }
272
273 bool wxTopLevelWindowX11::IsMaximized() const
274 {
275 // TODO
276 return TRUE;
277 }
278
279 void wxTopLevelWindowX11::Iconize(bool iconize)
280 {
281 if (!m_iconized && GetMainWindow())
282 {
283 if (XIconifyWindow(wxGlobalDisplay(),
284 (Window) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
285 m_iconized = TRUE;
286 }
287 }
288
289 bool wxTopLevelWindowX11::IsIconized() const
290 {
291 return m_iconized;
292 }
293
294 void wxTopLevelWindowX11::Restore()
295 {
296 // This is the way to deiconify the window, according to the X FAQ
297 if (m_iconized && GetMainWindow())
298 {
299 XMapWindow(wxGlobalDisplay(), (Window) GetMainWindow());
300 m_iconized = FALSE;
301 }
302 }
303
304 // ----------------------------------------------------------------------------
305 // wxTopLevelWindowX11 fullscreen
306 // ----------------------------------------------------------------------------
307
308 bool wxTopLevelWindowX11::ShowFullScreen(bool show, long style)
309 {
310 if (show)
311 {
312 if (IsFullScreen())
313 return FALSE;
314
315 m_fsIsShowing = TRUE;
316 m_fsStyle = style;
317
318 // TODO
319
320 return TRUE;
321 }
322 else
323 {
324 if (!IsFullScreen())
325 return FALSE;
326
327 m_fsIsShowing = FALSE;
328
329 // TODO
330 return TRUE;
331 }
332 }
333
334 // ----------------------------------------------------------------------------
335 // wxTopLevelWindowX11 misc
336 // ----------------------------------------------------------------------------
337
338 void wxTopLevelWindowX11::SetIcon(const wxIcon& icon)
339 {
340 // this sets m_icon
341 wxTopLevelWindowBase::SetIcon(icon);
342
343 if (icon.Ok() && GetMainWindow())
344 {
345 #if wxUSE_NANOX
346 #else
347 XWMHints *wmHints = XAllocWMHints();
348 wmHints->icon_pixmap = (Pixmap) icon.GetPixmap();
349
350 wmHints->flags = IconPixmapHint;
351
352 if (icon.GetMask())
353 {
354 wmHints->flags |= IconMaskHint;
355 wmHints->icon_mask = (Pixmap) icon.GetMask()->GetBitmap();
356 }
357
358 XSetWMHints(wxGlobalDisplay(), (Window) GetMainWindow(), wmHints);
359 XFree(wmHints);
360 #endif
361 }
362 }
363
364 void wxTopLevelWindowX11::SetTitle(const wxString& title)
365 {
366 m_title = title;
367 if (GetMainWindow())
368 {
369 XStoreName(wxGlobalDisplay(), (Window) GetMainWindow(),
370 (const char*) title);
371 XSetIconName(wxGlobalDisplay(), (Window) GetMainWindow(),
372 (const char*) title);
373
374 // Use this if the platform doesn't supply the above functions.
375 #if 0
376 XTextProperty textProperty;
377 textProperty.value = (unsigned char*) title;
378 textProperty.encoding = XA_STRING;
379 textProperty.format = 8;
380 textProperty.nitems = 1;
381
382 XSetTextProperty(wxGlobalDisplay(), (Window) GetMainWindow(),
383 & textProperty, WM_NAME);
384 #endif
385 }
386 }
387
388 wxString wxTopLevelWindowX11::GetTitle() const
389 {
390 return m_title;
391 }
392
393 #ifndef MWM_DECOR_BORDER
394 /* bit definitions for MwmHints.flags */
395 #define MWM_HINTS_FUNCTIONS (1L << 0)
396 #define MWM_HINTS_DECORATIONS (1L << 1)
397 #define MWM_HINTS_INPUT_MODE (1L << 2)
398 #define MWM_HINTS_STATUS (1L << 3)
399
400 #define MWM_DECOR_ALL (1L << 0)
401 #define MWM_DECOR_BORDER (1L << 1)
402 #define MWM_DECOR_RESIZEH (1L << 2)
403 #define MWM_DECOR_TITLE (1L << 3)
404 #define MWM_DECOR_MENU (1L << 4)
405 #define MWM_DECOR_MINIMIZE (1L << 5)
406 #define MWM_DECOR_MAXIMIZE (1L << 6)
407 #endif
408
409 struct MwmHints {
410 long flags;
411 long functions;
412 long decorations;
413 long input_mode;
414 };
415
416 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
417
418 // Set the window manager decorations according to the
419 // given wxWindows style
420 bool wxSetWMDecorations(Window w, long style)
421 {
422 #if wxUSE_NANOX
423 GR_WM_PROPERTIES wmProp;
424
425 wmProp.flags = 0;
426
427 if (style & wxRESIZE_BORDER)
428 {
429 wmProp.props |= GR_WM_PROPS_APPFRAME ;
430 wmProp.flags |= GR_WM_FLAGS_PROPS ;
431 }
432
433 if (style & wxSYSTEM_MENU)
434 {
435 wmProp.props |= GR_WM_PROPS_CLOSEBOX ;
436 wmProp.flags |= GR_WM_FLAGS_PROPS ;
437 }
438
439 if ((style & wxCAPTION) ||
440 (style & wxTINY_CAPTION_HORIZ) ||
441 (style & wxTINY_CAPTION_VERT))
442 {
443 wmProp.props |= GR_WM_PROPS_CAPTION ;
444 wmProp.flags |= GR_WM_FLAGS_PROPS ;
445 }
446
447 if (style & wxTHICK_FRAME)
448 {
449 wmProp.props |= GR_WM_PROPS_APPFRAME ;
450 wmProp.flags |= GR_WM_FLAGS_PROPS ;
451 }
452
453 if (style & wxSIMPLE_BORDER)
454 {
455 wmProp.props |= GR_WM_PROPS_BORDER ;
456 wmProp.flags |= GR_WM_FLAGS_PROPS ;
457 }
458
459 if (style & wxMINIMIZE_BOX)
460 {
461 }
462
463 if (style & wxMAXIMIZE_BOX)
464 {
465 wmProp.props |= GR_WM_PROPS_MAXIMIZE ;
466 wmProp.flags |= GR_WM_FLAGS_PROPS ;
467 }
468
469 if (((style & wxBORDER) != wxBORDER) && ((style & wxTHICK_FRAME) != wxTHICK_FRAME)
470 && ((style & wxRESIZE_BORDER) != wxRESIZE_BORDER))
471 {
472 wmProp.props |= GR_WM_PROPS_NODECORATE ;
473 wmProp.flags |= GR_WM_FLAGS_PROPS ;
474 }
475
476 GrSetWMProperties(w, & wmProp);
477
478 #else
479 if (!wxMWMIsRunning(w))
480 return FALSE;
481
482 Atom mwm_wm_hints = XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False);
483 MwmHints hints;
484 hints.flags = 0;
485 hints.decorations = 0;
486
487 if (style & wxRESIZE_BORDER)
488 {
489 // wxLogDebug("MWM_DECOR_RESIZEH");
490 hints.flags |= MWM_HINTS_DECORATIONS;
491 hints.decorations |= MWM_DECOR_RESIZEH;
492 }
493
494 if (style & wxSYSTEM_MENU)
495 {
496 // wxLogDebug("MWM_DECOR_MENU");
497 hints.flags |= MWM_HINTS_DECORATIONS;
498 hints.decorations |= MWM_DECOR_MENU;
499 }
500
501 if ((style & wxCAPTION) ||
502 (style & wxTINY_CAPTION_HORIZ) ||
503 (style & wxTINY_CAPTION_VERT))
504 {
505 // wxLogDebug("MWM_DECOR_TITLE");
506 hints.flags |= MWM_HINTS_DECORATIONS;
507 hints.decorations |= MWM_DECOR_TITLE;
508 }
509
510 if ((style & wxTHICK_FRAME) || (style & wxSIMPLE_BORDER) || (style & wxCAPTION))
511 {
512 // wxLogDebug("MWM_DECOR_BORDER");
513 hints.flags |= MWM_HINTS_DECORATIONS;
514 hints.decorations |= MWM_DECOR_BORDER;
515 }
516
517 if (style & wxMINIMIZE_BOX)
518 {
519 // wxLogDebug("MWM_DECOR_MINIMIZE");
520 hints.flags |= MWM_HINTS_DECORATIONS;
521 hints.decorations |= MWM_DECOR_MINIMIZE;
522 }
523
524 if (style & wxMAXIMIZE_BOX)
525 {
526 // wxLogDebug("MWM_DECOR_MAXIMIZE");
527 hints.flags |= MWM_HINTS_DECORATIONS;
528 hints.decorations |= MWM_DECOR_MAXIMIZE;
529 }
530
531 XChangeProperty(wxGlobalDisplay(),
532 w,
533 mwm_wm_hints, mwm_wm_hints,
534 32, PropModeReplace,
535 (unsigned char *) &hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
536
537 #endif
538 return TRUE;
539 }
540
541 bool wxMWMIsRunning(Window w)
542 {
543 #if wxUSE_NANOX
544 return FALSE;
545 #else
546 Display *dpy = (Display*)wxGetDisplay();
547 Atom motifWmInfo = XInternAtom(dpy, "_MOTIF_WM_INFO", False);
548
549 unsigned long length, bytesafter;
550 unsigned char value[20];
551 unsigned char *ptr = &value[0];
552 int ret, format;
553 Atom type;
554
555 type = format = length = 0;
556 value[0] = 0;
557
558 ret = XGetWindowProperty(wxGlobalDisplay(), w, motifWmInfo,
559 0L, 2, False, motifWmInfo,
560 &type, &format, &length, &bytesafter, &ptr);
561
562 return (ret == Success);
563 #endif
564 }
565
566 // For implementation purposes - sometimes decorations make the client area
567 // smaller
568 wxPoint wxTopLevelWindowX11::GetClientAreaOrigin() const
569 {
570 // In fact wxFrame::GetClientAreaOrigin
571 // does the required calculation already.
572 #if 0
573 if (this->IsKindOf(CLASSINFO(wxFrame)))
574 {
575 wxFrame* frame = (wxFrame*) this;
576 if (frame->GetMenuBar())
577 return wxPoint(0, frame->GetMenuBar()->GetSize().y);
578 }
579 #endif
580 return wxPoint(0, 0);
581 }
582
583 void wxTopLevelWindowX11::DoGetClientSize( int *width, int *height ) const
584 {
585 wxWindowX11::DoGetClientSize(width, height);
586 // Done by wxTopLevelWindow
587 #if 0
588 if (this->IsKindOf(CLASSINFO(wxFrame)))
589 {
590 wxFrame* frame = (wxFrame*) this;
591 if (frame->GetMenuBar())
592 (*height) -= frame->GetMenuBar()->GetSize().y;
593 if (frame->GetStatusBar())
594 (*height) -= frame->GetStatusBar()->GetSize().y;
595 }
596 #endif
597 }
598
599 void wxTopLevelWindowX11::DoSetClientSize(int width, int height)
600 {
601 wxWindowX11::DoSetClientSize(width, height);
602
603 #if 0
604 if (!GetMainWindow())
605 return;
606
607 XWindowChanges windowChanges;
608 int valueMask = 0;
609
610 if (width != -1)
611 {
612 windowChanges.width = width ;
613 valueMask |= CWWidth;
614 }
615 if (height != -1)
616 {
617 windowChanges.height = height ;
618 valueMask |= CWHeight;
619 }
620 XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(),
621 valueMask, & windowChanges);
622 #endif
623 }
624
625 void wxTopLevelWindowX11::DoSetSize(int x, int y, int width, int height, int sizeFlags)
626 {
627 // wxLogDebug( "Setting pos: %d, %d", x, y );
628 wxWindowX11::DoSetSize(x, y, width, height, sizeFlags);
629
630 wxPoint pt = GetPosition();
631 // wxLogDebug( "After, pos: %d, %d", pt.x, pt.y );
632 #if 0
633 XSync(wxGlobalDisplay(), False);
634 int w, h;
635 GetSize(& w, & h);
636 wxString msg;
637 msg.Printf("Before setting size: %d, %d", w, h);
638 wxLogDebug(msg);
639 if (!GetMainWindow())
640 return;
641
642 XWindowChanges windowChanges;
643 int valueMask = 0;
644
645 if (x != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
646 {
647 int yy = 0;
648 AdjustForParentClientOrigin( x, yy, sizeFlags);
649 windowChanges.x = x;
650 valueMask |= CWX;
651 }
652 if (y != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
653 {
654 int xx = 0;
655 AdjustForParentClientOrigin( xx, y, sizeFlags);
656 windowChanges.y = y;
657 valueMask |= CWY;
658 }
659 if (width != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
660 {
661 windowChanges.width = width /* - m_borderSize*2 */;
662 valueMask |= CWWidth;
663 }
664 if (height != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
665 {
666 windowChanges.height = height /* -m_borderSize*2*/;
667 valueMask |= CWHeight;
668 }
669
670 XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(),
671 valueMask, & windowChanges);
672 XSync(wxGlobalDisplay(), False);
673 GetSize(& w, & h);
674 msg.Printf("Tried to set to %d, %d. After setting size: %d, %d", width, height, w, h);
675 wxLogDebug(msg);
676 #endif
677 }
678
679 void wxTopLevelWindowX11::DoGetPosition(int *x, int *y) const
680 {
681 XSync(wxGlobalDisplay(), False);
682 Window window = (Window) m_mainWidget;
683 if (window)
684 {
685 int offsetX = 0;
686 int offsetY = 0;
687
688 #if !wxUSE_NANOX
689 // wxLogDebug("Translating...");
690 Window childWindow;
691 XTranslateCoordinates(wxGlobalDisplay(), window, XDefaultRootWindow(wxGlobalDisplay()),
692 0, 0, & offsetX, & offsetY, & childWindow);
693
694 // wxLogDebug("Offset: %d, %d", offsetX, offsetY);
695 #endif
696
697 XWindowAttributes attr;
698 Status status = XGetWindowAttributes(wxGlobalDisplay(), window, & attr);
699 wxASSERT(status);
700
701 if (status)
702 {
703 *x = attr.x + offsetX;
704 *y = attr.y + offsetY;
705 }
706 }
707 }