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