]> git.saurik.com Git - wxWidgets.git/blob - src/motif/frame.cpp
Fixed event handling bug as suggested by Lech Wiktor Piotrowski.
[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 #include "wx/statusbr.h"
31 #include "wx/toolbar.h"
32 #include "wx/menu.h"
33 #include "wx/settings.h"
34 #include "wx/utils.h"
35 #include "wx/log.h"
36 #include "wx/app.h"
37 #include "wx/icon.h"
38
39 #ifdef __VMS__
40 #pragma message disable nosimpint
41 #endif
42
43 #if defined(__ultrix) || defined(__sgi)
44 #include <Xm/Frame.h>
45 #endif
46
47 #include <Xm/Xm.h>
48 #include <X11/Shell.h>
49 #include <X11/Core.h>
50 #if XmVersion >= 1002
51 #include <Xm/XmAll.h>
52 #else
53 #include <Xm/Frame.h>
54 #endif
55 #include <Xm/MwmUtil.h>
56 #include <Xm/BulletinB.h>
57 #include <Xm/Form.h>
58 #include <Xm/MainW.h>
59 #include <Xm/RowColumn.h>
60 #include <Xm/Label.h>
61 #include <Xm/AtomMgr.h>
62 #include <Xm/LabelG.h>
63 #include <Xm/Frame.h>
64 #if XmVersion > 1000
65 #include <Xm/Protocols.h>
66 #endif
67
68 #ifdef __VMS__
69 #pragma message enable nosimpint
70 #endif
71
72 #include "wx/motif/private.h"
73 #include "wx/unix/utilsx11.h"
74
75 // ----------------------------------------------------------------------------
76 // private functions
77 // ----------------------------------------------------------------------------
78
79 static void wxFrameMapProc(Widget frameShell, XtPointer clientData,
80 XCrossingEvent* event);
81
82 // ----------------------------------------------------------------------------
83 // globals
84 // ----------------------------------------------------------------------------
85
86 extern wxList wxModelessWindows;
87 extern wxList wxPendingDelete;
88
89 // ----------------------------------------------------------------------------
90 // wxWin macros
91 // ----------------------------------------------------------------------------
92
93 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
94 EVT_ACTIVATE(wxFrame::OnActivate)
95 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged)
96 END_EVENT_TABLE()
97
98 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
99
100 // ============================================================================
101 // implementation
102 // ============================================================================
103
104 // ----------------------------------------------------------------------------
105 // frame construction
106 // ----------------------------------------------------------------------------
107
108 void wxFrame::Init()
109 {
110 m_iconized = false;
111
112 //// Motif-specific
113 m_frameShell = (WXWidget) NULL;
114 m_mainWidget = (WXWidget) NULL;
115 m_workArea = (WXWidget) NULL;
116 m_clientArea = (WXWidget) NULL;
117 }
118
119 bool wxFrame::Create(wxWindow *parent,
120 wxWindowID id,
121 const wxString& title,
122 const wxPoint& pos,
123 const wxSize& size,
124 long style,
125 const wxString& name)
126 {
127 if( !wxTopLevelWindow::Create( parent, id, title, pos, size, style,
128 name ) )
129 return false;
130
131 m_backgroundColour =
132 wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
133 m_foregroundColour = *wxBLACK;
134 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
135
136 int x = pos.x, y = pos.y;
137 int width = size.x, height = size.y;
138
139 // Set reasonable values for position and size if defaults have been
140 // requested
141 //
142 // MB TODO: something better than these arbitrary values ?
143 // VZ should use X resources for this...
144 if ( width == -1 )
145 width = 400;
146 if ( height == -1 )
147 height = 400;
148
149 int displayW, displayH;
150 wxDisplaySize( &displayW, &displayH );
151
152 if ( x == -1 )
153 {
154 x = (displayW - width) / 2;
155 if (x < 10) x = 10;
156 }
157 if ( y == -1 )
158 {
159 y = (displayH - height) / 2;
160 if (y < 10) y = 10;
161 }
162
163 SetTitle( title );
164
165 wxLogTrace(wxTRACE_Messages,
166 "Created frame (0x%p) with work area 0x%p and client "
167 "area 0x%p", m_mainWidget, m_workArea, m_clientArea);
168
169 XtAddEventHandler((Widget) m_clientArea, ExposureMask,False,
170 wxUniversalRepaintProc, (XtPointer) this);
171
172 if (x > -1)
173 XtVaSetValues((Widget) m_frameShell, XmNx, x, NULL);
174 if (y > -1)
175 XtVaSetValues((Widget) m_frameShell, XmNy, y, NULL);
176 if (width > -1)
177 XtVaSetValues((Widget) m_frameShell, XmNwidth, width, NULL);
178 if (height > -1)
179 XtVaSetValues((Widget) m_frameShell, XmNheight, height, NULL);
180
181 ChangeFont(false);
182
183 ChangeBackgroundColour();
184
185 PreResize();
186
187 wxSize newSize(width, height);
188 wxSizeEvent sizeEvent(newSize, GetId());
189 sizeEvent.SetEventObject(this);
190
191 GetEventHandler()->ProcessEvent(sizeEvent);
192
193 return true;
194 }
195
196 bool wxFrame::XmDoCreateTLW(wxWindow* WXUNUSED(parent),
197 wxWindowID WXUNUSED(id),
198 const wxString& WXUNUSED(title),
199 const wxPoint& WXUNUSED(pos),
200 const wxSize& WXUNUSED(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 delete m_frameMenuBar;
283 m_frameMenuBar = NULL;
284 }
285
286 if (m_frameStatusBar)
287 {
288 delete m_frameStatusBar;
289 m_frameStatusBar = NULL;
290 }
291
292 PreDestroy();
293
294 Widget frameShell = (Widget)GetShellWidget();
295
296 if( frameShell )
297 XtRemoveEventHandler( frameShell, StructureNotifyMask,
298 False, (XtEventHandler)wxFrameMapProc,
299 (XtPointer)this );
300
301 if( m_clientArea )
302 {
303 wxDeleteWindowFromTable( (Widget)m_clientArea );
304 XtDestroyWidget( (Widget)m_clientArea );
305 }
306
307 if( m_workArea )
308 {
309 XtVaSetValues( (Widget)m_mainWidget,
310 XmNworkWindow, (Widget)NULL,
311 NULL );
312
313 wxDeleteWindowFromTable( (Widget)m_workArea );
314 XtDestroyWidget( (Widget)m_workArea );
315 }
316
317 if( m_mainWidget )
318 XtDestroyWidget( (Widget)m_mainWidget );
319
320 if( frameShell )
321 XtDestroyWidget( frameShell );
322 }
323
324 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
325 void wxFrame::DoGetClientSize(int *x, int *y) const
326 {
327 Dimension xx, yy;
328 XtVaGetValues((Widget) m_workArea, XmNwidth, &xx, XmNheight, &yy, NULL);
329
330 if (m_frameStatusBar)
331 {
332 int sbw, sbh;
333 m_frameStatusBar->GetSize(& sbw, & sbh);
334 yy = (Dimension)(yy - sbh);
335 }
336 #if wxUSE_TOOLBAR
337 if (m_frameToolBar)
338 {
339 int tbw, tbh;
340 m_frameToolBar->GetSize(& tbw, & tbh);
341 if (m_frameToolBar->GetWindowStyleFlag() & wxTB_VERTICAL)
342 xx = (Dimension)(xx - tbw);
343 else
344 yy = (Dimension)(yy - tbh);
345 }
346 #endif // wxUSE_TOOLBAR
347
348 //CE found a call here with NULL y pointer
349 if (x)
350 *x = xx;
351 if (y)
352 *y = yy;
353 }
354
355 // Set the client size (i.e. leave the calculation of borders etc.
356 // to wxWidgets)
357 void wxFrame::DoSetClientSize(int width, int height)
358 {
359 // Calculate how large the new main window should be
360 // by finding the difference between the client area and the
361 // main window area, and adding on to the new client area
362 if (width > -1)
363 XtVaSetValues((Widget) m_workArea, XmNwidth, width, NULL);
364
365 if (height > -1)
366 {
367 if (m_frameStatusBar)
368 {
369 int sbw, sbh;
370 m_frameStatusBar->GetSize(& sbw, & sbh);
371 height += sbh;
372 }
373 #if wxUSE_TOOLBAR
374 if (m_frameToolBar)
375 {
376 int tbw, tbh;
377 m_frameToolBar->GetSize(& tbw, & tbh);
378 if (m_frameToolBar->GetWindowStyleFlag() & wxTB_VERTICAL)
379 width += tbw;
380 else
381 height += tbh;
382 }
383 #endif // wxUSE_TOOLBAR
384
385 XtVaSetValues((Widget) m_workArea, XmNheight, height, NULL);
386 }
387 PreResize();
388
389 wxSize newSize(width, height);
390 wxSizeEvent sizeEvent(newSize, GetId());
391 sizeEvent.SetEventObject(this);
392
393 GetEventHandler()->ProcessEvent(sizeEvent);
394
395 }
396
397 void wxFrame::DoGetSize(int *width, int *height) const
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.c_str(),
456 XmNiconName, title.c_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 }