Fix wxMotif compilation with g++ 4.4.
[wxWidgets.git] / src / motif / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/frame.cpp
3 // Purpose: wxFrame
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #include "wx/frame.h"
24
25 #ifndef WX_PRECOMP
26 #include "wx/log.h"
27 #include "wx/app.h"
28 #include "wx/utils.h"
29 #include "wx/menu.h"
30 #include "wx/icon.h"
31 #include "wx/settings.h"
32 #include "wx/toolbar.h"
33 #include "wx/statusbr.h"
34 #endif
35
36 #ifdef __VMS__
37 #pragma message disable nosimpint
38 #endif
39
40 #if defined(__ultrix) || defined(__sgi)
41 #include <Xm/Frame.h>
42 #endif
43
44 #include <Xm/Xm.h>
45 #include <X11/Shell.h>
46 #include <X11/Core.h>
47 #if XmVersion >= 1002
48 #include <Xm/XmAll.h>
49 #else
50 #include <Xm/Frame.h>
51 #endif
52 #include <Xm/MwmUtil.h>
53 #include <Xm/BulletinB.h>
54 #include <Xm/Form.h>
55 #include <Xm/MainW.h>
56 #include <Xm/RowColumn.h>
57 #include <Xm/Label.h>
58 #include <Xm/AtomMgr.h>
59 #include <Xm/LabelG.h>
60 #include <Xm/Frame.h>
61 #if XmVersion > 1000
62 #include <Xm/Protocols.h>
63 #endif
64
65 #ifdef __VMS__
66 #pragma message enable nosimpint
67 #endif
68
69 #include "wx/motif/private.h"
70 #include "wx/unix/utilsx11.h"
71
72 // ----------------------------------------------------------------------------
73 // private functions
74 // ----------------------------------------------------------------------------
75
76 static void wxFrameMapProc(Widget frameShell, XtPointer clientData,
77 XCrossingEvent* event);
78
79 // ----------------------------------------------------------------------------
80 // globals
81 // ----------------------------------------------------------------------------
82
83 extern wxList wxModelessWindows;
84
85 // ----------------------------------------------------------------------------
86 // wxWin macros
87 // ----------------------------------------------------------------------------
88
89 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
90 EVT_ACTIVATE(wxFrame::OnActivate)
91 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged)
92 END_EVENT_TABLE()
93
94 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
95
96 // ============================================================================
97 // implementation
98 // ============================================================================
99
100 // ----------------------------------------------------------------------------
101 // frame construction
102 // ----------------------------------------------------------------------------
103
104 void wxFrame::Init()
105 {
106 m_iconized = false;
107
108 //// Motif-specific
109 m_frameShell = (WXWidget) NULL;
110 m_mainWidget = (WXWidget) NULL;
111 m_workArea = (WXWidget) NULL;
112 m_clientArea = (WXWidget) NULL;
113 }
114
115 bool wxFrame::Create(wxWindow *parent,
116 wxWindowID id,
117 const wxString& title,
118 const wxPoint& pos,
119 const wxSize& size,
120 long style,
121 const wxString& name)
122 {
123 if( !wxTopLevelWindow::Create( parent, id, title, pos, size, style,
124 name ) )
125 return false;
126
127 int x = pos.x, y = pos.y;
128 int width = size.x, height = size.y;
129
130 // Set reasonable values for position and size if defaults have been
131 // requested
132 //
133 // MB TODO: something better than these arbitrary values ?
134 // VZ should use X resources for this...
135 if ( width == -1 )
136 width = 400;
137 if ( height == -1 )
138 height = 400;
139
140 int displayW, displayH;
141 wxDisplaySize( &displayW, &displayH );
142
143 if ( x == -1 )
144 {
145 x = (displayW - width) / 2;
146 if (x < 10) x = 10;
147 }
148 if ( y == -1 )
149 {
150 y = (displayH - height) / 2;
151 if (y < 10) y = 10;
152 }
153
154 SetTitle( title );
155
156 wxLogTrace(wxTRACE_Messages,
157 "Created frame (0x%p) with work area 0x%p and client "
158 "area 0x%p", m_mainWidget, m_workArea, m_clientArea);
159
160 XtAddEventHandler((Widget) m_clientArea, ExposureMask,False,
161 wxUniversalRepaintProc, (XtPointer) this);
162
163 if (x > -1)
164 XtVaSetValues((Widget) m_frameShell, XmNx, x, NULL);
165 if (y > -1)
166 XtVaSetValues((Widget) m_frameShell, XmNy, y, NULL);
167 if (width > -1)
168 XtVaSetValues((Widget) m_frameShell, XmNwidth, width, NULL);
169 if (height > -1)
170 XtVaSetValues((Widget) m_frameShell, XmNheight, height, NULL);
171
172 PostCreation();
173 PreResize();
174
175 wxSize newSize(width, height);
176 wxSizeEvent sizeEvent(newSize, GetId());
177 sizeEvent.SetEventObject(this);
178
179 HandleWindowEvent(sizeEvent);
180
181 return true;
182 }
183
184 bool wxFrame::XmDoCreateTLW(wxWindow* WXUNUSED(parent),
185 wxWindowID WXUNUSED(id),
186 const wxString& WXUNUSED(title),
187 const wxPoint& WXUNUSED(pos),
188 const wxSize& WXUNUSED(size),
189 long style,
190 const wxString& name)
191 {
192 Widget frameShell;
193
194 frameShell = XtCreatePopupShell( name, topLevelShellWidgetClass,
195 (Widget)wxTheApp->GetTopLevelWidget(),
196 NULL, 0 );
197
198 XtVaSetValues(frameShell,
199 // Allows menu to resize
200 XmNallowShellResize, True,
201 XmNdeleteResponse, XmDO_NOTHING,
202 XmNmappedWhenManaged, False,
203 XmNiconic, (style & wxICONIZE) ? True : False,
204 NULL);
205
206 m_frameShell = (WXWidget)frameShell;
207
208 m_mainWidget = (WXWidget) XtVaCreateManagedWidget("main_window",
209 xmMainWindowWidgetClass, frameShell,
210 XmNresizePolicy, XmRESIZE_NONE,
211 NULL);
212
213 m_workArea = (WXWidget) XtVaCreateWidget("form",
214 xmFormWidgetClass, (Widget) m_mainWidget,
215 XmNresizePolicy, XmRESIZE_NONE,
216 NULL);
217
218 m_clientArea = (WXWidget) XtVaCreateWidget("client",
219 xmBulletinBoardWidgetClass, (Widget) m_workArea,
220 XmNmarginWidth, 0,
221 XmNmarginHeight, 0,
222 XmNrightAttachment, XmATTACH_FORM,
223 XmNleftAttachment, XmATTACH_FORM,
224 XmNtopAttachment, XmATTACH_FORM,
225 XmNbottomAttachment, XmATTACH_FORM,
226 NULL);
227
228 XtVaSetValues((Widget) m_mainWidget,
229 XmNworkWindow, (Widget) m_workArea,
230 NULL);
231
232 XtManageChild((Widget) m_clientArea);
233 XtManageChild((Widget) m_workArea);
234
235 XtTranslations ptr = XtParseTranslationTable( "<Configure>: resize()" );
236 XtOverrideTranslations( (Widget) m_workArea, ptr );
237 XtFree( (char *)ptr );
238
239 /* Part of show-&-hide fix */
240 XtAddEventHandler( frameShell, StructureNotifyMask,
241 False, (XtEventHandler)wxFrameMapProc,
242 (XtPointer)this );
243
244 XtRealizeWidget(frameShell);
245
246 wxAddWindowToTable( (Widget)m_workArea, this);
247 wxAddWindowToTable( (Widget)m_clientArea, this);
248
249 wxModelessWindows.Append( this );
250
251 return true;
252 }
253
254 wxFrame::~wxFrame()
255 {
256 SendDestroyEvent();
257
258 if (m_clientArea)
259 {
260 XtRemoveEventHandler((Widget) m_clientArea, ExposureMask, False,
261 wxUniversalRepaintProc, (XtPointer) this);
262 }
263
264 if (GetMainWidget())
265 Show(false);
266
267 if (m_frameMenuBar)
268 {
269 m_frameMenuBar->DestroyMenuBar();
270 wxDELETE(m_frameMenuBar);
271 }
272
273 wxDELETE(m_frameStatusBar);
274
275 PreDestroy();
276
277 Widget frameShell = (Widget)GetShellWidget();
278
279 if( frameShell )
280 XtRemoveEventHandler( frameShell, StructureNotifyMask,
281 False, (XtEventHandler)wxFrameMapProc,
282 (XtPointer)this );
283
284 if( m_clientArea )
285 {
286 wxDeleteWindowFromTable( (Widget)m_clientArea );
287 XtDestroyWidget( (Widget)m_clientArea );
288 }
289
290 if( m_workArea )
291 {
292 XtVaSetValues( (Widget)m_mainWidget,
293 XmNworkWindow, (Widget)NULL,
294 NULL );
295
296 wxDeleteWindowFromTable( (Widget)m_workArea );
297 XtDestroyWidget( (Widget)m_workArea );
298 }
299
300 if( m_mainWidget )
301 XtDestroyWidget( (Widget)m_mainWidget );
302
303 if( frameShell )
304 XtDestroyWidget( frameShell );
305 }
306
307 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
308 void wxFrame::DoGetClientSize(int *x, int *y) const
309 {
310 Dimension xx, yy;
311 XtVaGetValues((Widget) m_workArea, XmNwidth, &xx, XmNheight, &yy, NULL);
312
313 if (m_frameStatusBar)
314 {
315 int sbw, sbh;
316 m_frameStatusBar->GetSize(& sbw, & sbh);
317 yy = (Dimension)(yy - sbh);
318 }
319 #if wxUSE_TOOLBAR
320 if (m_frameToolBar)
321 {
322 int tbw, tbh;
323 m_frameToolBar->GetSize(& tbw, & tbh);
324 if (m_frameToolBar->GetWindowStyleFlag() & wxTB_VERTICAL)
325 xx = (Dimension)(xx - tbw);
326 else
327 yy = (Dimension)(yy - tbh);
328 }
329 #endif // wxUSE_TOOLBAR
330
331 //CE found a call here with NULL y pointer
332 if (x)
333 *x = xx;
334 if (y)
335 *y = yy;
336 }
337
338 // Set the client size (i.e. leave the calculation of borders etc.
339 // to wxWidgets)
340 void wxFrame::DoSetClientSize(int width, int height)
341 {
342 // Calculate how large the new main window should be
343 // by finding the difference between the client area and the
344 // main window area, and adding on to the new client area
345 if (width > -1)
346 XtVaSetValues((Widget) m_workArea, XmNwidth, width, NULL);
347
348 if (height > -1)
349 {
350 if (m_frameStatusBar)
351 {
352 int sbw, sbh;
353 m_frameStatusBar->GetSize(& sbw, & sbh);
354 height += sbh;
355 }
356 #if wxUSE_TOOLBAR
357 if (m_frameToolBar)
358 {
359 int tbw, tbh;
360 m_frameToolBar->GetSize(& tbw, & tbh);
361 if (m_frameToolBar->GetWindowStyleFlag() & wxTB_VERTICAL)
362 width += tbw;
363 else
364 height += tbh;
365 }
366 #endif // wxUSE_TOOLBAR
367
368 XtVaSetValues((Widget) m_workArea, XmNheight, height, NULL);
369 }
370 PreResize();
371
372 wxSize newSize(width, height);
373 wxSizeEvent sizeEvent(newSize, GetId());
374 sizeEvent.SetEventObject(this);
375
376 HandleWindowEvent(sizeEvent);
377
378 }
379
380 void wxFrame::DoGetSize(int *width, int *height) const
381 {
382 if (!m_frameShell)
383 {
384 *width = -1; *height = -1;
385 return;
386 }
387
388 Dimension xx, yy;
389 XtVaGetValues((Widget) m_frameShell, XmNwidth, &xx, XmNheight, &yy, NULL);
390 *width = xx; *height = yy;
391 }
392
393 void wxFrame::DoSetSize(int x, int y, int width, int height, int WXUNUSED(sizeFlags))
394 {
395 if (x > -1)
396 XtVaSetValues((Widget) m_frameShell, XmNx, x, NULL);
397 if (y > -1)
398 XtVaSetValues((Widget) m_frameShell, XmNy, y, NULL);
399 if (width > -1)
400 XtVaSetValues((Widget) m_mainWidget, XmNwidth, width, NULL);
401 if (height > -1)
402 XtVaSetValues((Widget) m_mainWidget, XmNheight, height, NULL);
403
404 if (!(height == -1 && width == -1))
405 {
406 PreResize();
407 }
408 }
409
410 bool wxFrame::Show( bool show )
411 {
412 if( !wxWindowBase::Show( show ) )
413 return false;
414
415 m_isShown = show;
416
417 Widget shell = (Widget)GetShellWidget();
418 if (!shell)
419 return wxWindow::Show(show);
420
421 SetVisibleStatus(show);
422 if (show)
423 {
424 XtPopup(shell, XtGrabNone);
425 }
426 else
427 {
428 XtPopdown(shell);
429 }
430
431 return true;
432 }
433
434 void wxFrame::SetTitle(const wxString& title)
435 {
436 wxString oldTitle = GetTitle();
437 if( title == oldTitle )
438 return;
439
440 wxTopLevelWindow::SetTitle( title );
441
442 if( !title.empty() )
443 XtVaSetValues( (Widget)m_frameShell,
444 XmNtitle, (const char*)title.mb_str(),
445 XmNiconName, (const char*)title.mb_str(),
446 NULL );
447 }
448
449 void wxFrame::DoSetIcon(const wxIcon& icon)
450 {
451 if (!m_frameShell)
452 return;
453
454 if (!icon.Ok() || !icon.GetDrawable())
455 return;
456
457 XtVaSetValues((Widget) m_frameShell,
458 XtNiconPixmap, icon.GetDrawable(),
459 NULL);
460 }
461
462 void wxFrame::SetIcons(const wxIconBundle& icons)
463 {
464 wxFrameBase::SetIcons( icons );
465
466 if (!m_frameShell)
467 return;
468
469 DoSetIcon( m_icons.GetIcon( -1 ) );
470 wxSetIconsX11(GetXDisplay(),
471 (WXWindow) XtWindow( (Widget) m_frameShell ), icons);
472 }
473
474 void wxFrame::PositionStatusBar()
475 {
476 if (!m_frameStatusBar)
477 return;
478
479 int w, h;
480 GetClientSize(&w, &h);
481 int sw, sh;
482 m_frameStatusBar->GetSize(&sw, &sh);
483
484 // Since we wish the status bar to be directly under the client area,
485 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
486 m_frameStatusBar->SetSize(0, h, w, sh);
487 }
488
489 WXWidget wxFrame::GetMenuBarWidget() const
490 {
491 if (GetMenuBar())
492 return GetMenuBar()->GetMainWidget();
493 else
494 return (WXWidget) NULL;
495 }
496
497 void wxFrame::SetMenuBar(wxMenuBar *menuBar)
498 {
499 if (!menuBar)
500 {
501 m_frameMenuBar = NULL;
502 return;
503 }
504
505 // Currently can't set it twice
506 // wxASSERT_MSG( (m_frameMenuBar == NULL), "Cannot set the menubar more than once");
507
508 if (m_frameMenuBar)
509 {
510 m_frameMenuBar->DestroyMenuBar();
511 delete m_frameMenuBar;
512 }
513
514 m_frameMenuBar = menuBar;
515 m_frameMenuBar->CreateMenuBar(this);
516 }
517
518 // Responds to colour changes, and passes event on to children.
519 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
520 {
521 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
522 Refresh();
523
524 if ( m_frameStatusBar )
525 {
526 wxSysColourChangedEvent event2;
527 event2.SetEventObject( m_frameStatusBar );
528 m_frameStatusBar->HandleWindowEvent(event2);
529 }
530
531 // Propagate the event to the non-top-level children
532 wxWindow::OnSysColourChanged(event);
533 }
534
535 // Default activation behaviour - set the focus for the first child
536 // subwindow found.
537 void wxFrame::OnActivate(wxActivateEvent& event)
538 {
539 if (!event.GetActive())
540 return;
541
542 for(wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); node;
543 node = node->GetNext())
544 {
545 // Find a child that's a subwindow, but not a dialog box.
546 wxWindow *child = node->GetData();
547 if (!child->IsTopLevel())
548 {
549 child->SetFocus();
550 return;
551 }
552 }
553 }
554
555 #if wxUSE_TOOLBAR
556
557 wxToolBar* wxFrame::CreateToolBar(long style,
558 wxWindowID id,
559 const wxString& name)
560 {
561 if ( wxFrameBase::CreateToolBar(style, id, name) )
562 {
563 PositionToolBar();
564 }
565
566 return m_frameToolBar;
567 }
568
569 void wxFrame::SetToolBar(wxToolBar *toolbar)
570 {
571 wxFrameBase::SetToolBar(toolbar);
572 SendSizeEvent();
573 }
574
575 void wxFrame::PositionToolBar()
576 {
577 wxToolBar* tb = GetToolBar();
578 if (tb)
579 {
580 int cw, ch;
581 GetClientSize(& cw, &ch);
582
583 int tw, th;
584 tb->GetSize(& tw, & th);
585
586 if (tb->GetWindowStyleFlag() & wxTB_VERTICAL)
587 {
588 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
589 // means, pretend we don't have toolbar/status bar, so we
590 // have the original client size.
591 th = ch + th;
592 }
593 else
594 {
595 // Use the 'real' position
596 tw = cw;
597 }
598
599 tb->SetSize(0, 0, -1, -1, wxSIZE_NO_ADJUSTMENTS);
600 }
601 }
602 #endif // wxUSE_TOOLBAR
603
604 //// Motif-specific
605 bool wxFrame::PreResize()
606 {
607 #if wxUSE_TOOLBAR
608 PositionToolBar();
609 #endif // wxUSE_TOOLBAR
610
611 #if wxUSE_STATUSBAR
612 PositionStatusBar();
613 #endif // wxUSE_STATUSBAR
614
615 return true;
616 }
617
618 WXWidget wxFrame::GetClientWidget() const
619 {
620 return m_clientArea;
621 }
622
623 void wxFrame::ChangeFont(bool WXUNUSED(keepOriginalSize))
624 {
625 // TODO
626 }
627
628 void wxFrame::ChangeBackgroundColour()
629 {
630 if (GetClientWidget())
631 wxDoChangeBackgroundColour(GetClientWidget(), m_backgroundColour);
632 }
633
634 void wxFrame::ChangeForegroundColour()
635 {
636 if (GetClientWidget())
637 wxDoChangeForegroundColour(GetClientWidget(), m_foregroundColour);
638 }
639
640 /* MATTEW: Used to insure that hide-&-show within an event cycle works */
641 static void wxFrameMapProc( Widget frameShell, XtPointer clientData,
642 XCrossingEvent* event )
643 {
644 wxFrame *tli = (wxFrame*)clientData;
645
646 XEvent *e = (XEvent *)event;
647
648 if( e->xany.type == MapNotify )
649 {
650 // Iconize fix
651 XtVaSetValues( frameShell, XmNiconic, (Boolean)False, NULL );
652 if( !tli->GetVisibleStatus() )
653 {
654 /* We really wanted this to be hidden! */
655 XtUnmapWidget( frameShell );
656 }
657 }
658 else if( e->xany.type == UnmapNotify )
659 // Iconize fix
660 XtVaSetValues( frameShell, XmNiconic, (Boolean)True, NULL );
661 }