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