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