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