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