Replace direct calls to ProcessEvent() with
[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::SetIcons(const wxIconBundle& icons)
474 {
475 wxFrameBase::SetIcons( icons );
476
477 if (!m_frameShell)
478 return;
479
480 DoSetIcon( m_icons.GetIcon( -1 ) );
481 wxSetIconsX11(GetXDisplay(),
482 (WXWindow) XtWindow( (Widget) m_frameShell ), icons);
483 }
484
485 void wxFrame::PositionStatusBar()
486 {
487 if (!m_frameStatusBar)
488 return;
489
490 int w, h;
491 GetClientSize(&w, &h);
492 int sw, sh;
493 m_frameStatusBar->GetSize(&sw, &sh);
494
495 // Since we wish the status bar to be directly under the client area,
496 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
497 m_frameStatusBar->SetSize(0, h, w, sh);
498 }
499
500 WXWidget wxFrame::GetMenuBarWidget() const
501 {
502 if (GetMenuBar())
503 return GetMenuBar()->GetMainWidget();
504 else
505 return (WXWidget) NULL;
506 }
507
508 void wxFrame::SetMenuBar(wxMenuBar *menuBar)
509 {
510 if (!menuBar)
511 {
512 m_frameMenuBar = NULL;
513 return;
514 }
515
516 // Currently can't set it twice
517 // wxASSERT_MSG( (m_frameMenuBar == (wxMenuBar*) NULL), "Cannot set the menubar more than once");
518
519 if (m_frameMenuBar)
520 {
521 m_frameMenuBar->DestroyMenuBar();
522 delete m_frameMenuBar;
523 }
524
525 m_frameMenuBar = menuBar;
526 m_frameMenuBar->CreateMenuBar(this);
527 }
528
529 // Responds to colour changes, and passes event on to children.
530 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
531 {
532 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
533 Refresh();
534
535 if ( m_frameStatusBar )
536 {
537 wxSysColourChangedEvent event2;
538 event2.SetEventObject( m_frameStatusBar );
539 m_frameStatusBar->GetEventHandler()->ProcessEvent(event2);
540 }
541
542 // Propagate the event to the non-top-level children
543 wxWindow::OnSysColourChanged(event);
544 }
545
546 // Default activation behaviour - set the focus for the first child
547 // subwindow found.
548 void wxFrame::OnActivate(wxActivateEvent& event)
549 {
550 if (!event.GetActive())
551 return;
552
553 for(wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); node;
554 node = node->GetNext())
555 {
556 // Find a child that's a subwindow, but not a dialog box.
557 wxWindow *child = node->GetData();
558 if (!child->IsTopLevel())
559 {
560 child->SetFocus();
561 return;
562 }
563 }
564 }
565
566 void wxFrame::SendSizeEvent()
567 {
568 wxSizeEvent event(GetSize(), GetId());
569 event.SetEventObject(this);
570 GetEventHandler()->AddPendingEvent(event);
571 }
572
573 #if wxUSE_TOOLBAR
574
575 wxToolBar* wxFrame::CreateToolBar(long style,
576 wxWindowID id,
577 const wxString& name)
578 {
579 if ( wxFrameBase::CreateToolBar(style, id, name) )
580 {
581 PositionToolBar();
582 }
583
584 return m_frameToolBar;
585 }
586
587 void wxFrame::SetToolBar(wxToolBar *toolbar)
588 {
589 wxFrameBase::SetToolBar(toolbar);
590 SendSizeEvent();
591 }
592
593 void wxFrame::PositionToolBar()
594 {
595 wxToolBar* tb = GetToolBar();
596 if (tb)
597 {
598 int cw, ch;
599 GetClientSize(& cw, &ch);
600
601 int tw, th;
602 tb->GetSize(& tw, & th);
603
604 if (tb->GetWindowStyleFlag() & wxTB_VERTICAL)
605 {
606 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
607 // means, pretend we don't have toolbar/status bar, so we
608 // have the original client size.
609 th = ch + th;
610 }
611 else
612 {
613 // Use the 'real' position
614 tw = cw;
615 }
616
617 tb->SetSize(0, 0, -1, -1, wxSIZE_NO_ADJUSTMENTS);
618 }
619 }
620 #endif // wxUSE_TOOLBAR
621
622 //// Motif-specific
623 bool wxFrame::PreResize()
624 {
625 #if wxUSE_TOOLBAR
626 PositionToolBar();
627 #endif // wxUSE_TOOLBAR
628
629 #if wxUSE_STATUSBAR
630 PositionStatusBar();
631 #endif // wxUSE_STATUSBAR
632
633 return true;
634 }
635
636 WXWidget wxFrame::GetClientWidget() const
637 {
638 return m_clientArea;
639 }
640
641 void wxFrame::ChangeFont(bool WXUNUSED(keepOriginalSize))
642 {
643 // TODO
644 }
645
646 void wxFrame::ChangeBackgroundColour()
647 {
648 if (GetClientWidget())
649 wxDoChangeBackgroundColour(GetClientWidget(), m_backgroundColour);
650 }
651
652 void wxFrame::ChangeForegroundColour()
653 {
654 if (GetClientWidget())
655 wxDoChangeForegroundColour(GetClientWidget(), m_foregroundColour);
656 }
657
658 /* MATTEW: Used to insure that hide-&-show within an event cycle works */
659 static void wxFrameMapProc( Widget frameShell, XtPointer clientData,
660 XCrossingEvent* event )
661 {
662 wxFrame *tli = (wxFrame*)clientData;
663
664 XEvent *e = (XEvent *)event;
665
666 if( e->xany.type == MapNotify )
667 {
668 // Iconize fix
669 XtVaSetValues( frameShell, XmNiconic, (Boolean)False, NULL );
670 if( !tli->GetVisibleStatus() )
671 {
672 /* We really wanted this to be hidden! */
673 XtUnmapWidget( frameShell );
674 }
675 }
676 else if( e->xany.type == UnmapNotify )
677 // Iconize fix
678 XtVaSetValues( frameShell, XmNiconic, (Boolean)True, NULL );
679 }