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