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