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