Removed lots of OnClose functions; doc'ed OnCloseWindow better;
[wxWidgets.git] / src / motif / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
13 #pragma implementation "frame.h"
14 #endif
15
16 #include "wx/frame.h"
17 #include "wx/statusbr.h"
18 #include "wx/toolbar.h"
19 #include "wx/menuitem.h"
20 #include "wx/menu.h"
21 #include "wx/dcclient.h"
22 #include "wx/dialog.h"
23 #include "wx/settings.h"
24 #include "wx/app.h"
25 #include "wx/utils.h"
26
27 #if defined(__ultrix) || defined(__sgi)
28 #include <Xm/Frame.h>
29 #endif
30
31 #include <Xm/Xm.h>
32 #include <X11/Shell.h>
33 #if XmVersion >= 1002
34 #include <Xm/XmAll.h>
35 #else
36 #include <Xm/Frame.h>
37 #endif
38 #include <Xm/MwmUtil.h>
39 #include <Xm/BulletinB.h>
40 #include <Xm/Form.h>
41 #include <Xm/MainW.h>
42 #include <Xm/RowColumn.h>
43 #include <Xm/Label.h>
44 #include <Xm/AtomMgr.h>
45 #include <Xm/LabelG.h>
46 #include <Xm/Frame.h>
47 #if XmVersion > 1000
48 #include <Xm/Protocols.h>
49 #endif
50
51 #include "wx/motif/private.h"
52
53 void wxCloseFrameCallback(Widget, XtPointer, XmAnyCallbackStruct *cbs);
54 void wxFrameFocusProc(Widget workArea, XtPointer clientData,
55 XmAnyCallbackStruct *cbs);
56 static void wxFrameMapProc(Widget frameShell, XtPointer clientData,
57 XCrossingEvent * event);
58
59 extern wxList wxModelessWindows;
60 extern wxList wxPendingDelete;
61
62 // TODO: this should be tidied so that any frame can be the
63 // top frame
64 static bool wxTopLevelUsed = FALSE;
65
66 #if !USE_SHARED_LIBRARY
67 BEGIN_EVENT_TABLE(wxFrame, wxWindow)
68 EVT_SIZE(wxFrame::OnSize)
69 EVT_ACTIVATE(wxFrame::OnActivate)
70 EVT_MENU_HIGHLIGHT_ALL(wxFrame::OnMenuHighlight)
71 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged)
72 EVT_IDLE(wxFrame::OnIdle)
73 EVT_CLOSE(wxFrame::OnCloseWindow)
74 END_EVENT_TABLE()
75
76 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
77 #endif
78
79 #if wxUSE_NATIVE_STATUSBAR
80 bool wxFrame::m_useNativeStatusBar = TRUE;
81 #else
82 bool wxFrame::m_useNativeStatusBar = FALSE;
83 #endif
84
85 wxFrame::wxFrame()
86 {
87 #if wxUSE_TOOLBAR
88 m_frameToolBar = NULL ;
89 #endif // wxUSE_TOOLBAR
90
91 m_frameMenuBar = NULL;
92 m_frameStatusBar = NULL;
93
94 m_windowParent = NULL;
95 m_iconized = FALSE;
96
97 //// Motif-specific
98 m_frameShell = (WXWidget) NULL;
99 m_frameWidget = (WXWidget) NULL;;
100 m_workArea = (WXWidget) NULL;;
101 m_clientArea = (WXWidget) NULL;;
102 m_visibleStatus = TRUE;
103 m_title = "";
104 }
105
106 bool wxFrame::Create(wxWindow *parent,
107 wxWindowID id,
108 const wxString& title,
109 const wxPoint& pos,
110 const wxSize& size,
111 long style,
112 const wxString& name)
113 {
114 if (!parent)
115 wxTopLevelWindows.Append(this);
116
117 SetName(name);
118
119 m_windowStyle = style;
120 m_frameMenuBar = NULL;
121 #if wxUSE_TOOLBAR
122 m_frameToolBar = NULL ;
123 #endif // wxUSE_TOOLBAR
124 m_frameStatusBar = NULL;
125
126 //// Motif-specific
127 m_frameShell = (WXWidget) NULL;
128 m_frameWidget = (WXWidget) NULL;;
129 m_workArea = (WXWidget) NULL;;
130 m_clientArea = (WXWidget) NULL;;
131 m_visibleStatus = TRUE;
132 m_title = "";
133
134 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE);
135 m_foregroundColour = *wxBLACK;
136 m_windowFont = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT);
137
138 if ( id > -1 )
139 m_windowId = id;
140 else
141 m_windowId = (int)NewControlId();
142
143 if (parent) parent->AddChild(this);
144
145 wxModelessWindows.Append(this);
146
147 int x = pos.x; int y = pos.y;
148 int width = size.x; int height = size.y;
149
150 if (wxTopLevelUsed)
151 // Change suggested by Matthew Flatt
152 m_frameShell = (WXWidget) XtAppCreateShell(name, wxTheApp->GetClassName(), topLevelShellWidgetClass, (Display*) wxGetDisplay(), NULL, 0);
153 else
154 {
155 m_frameShell = wxTheApp->GetTopLevelWidget();
156 wxTopLevelUsed = TRUE;
157 }
158
159 XtVaSetValues((Widget) m_frameShell,
160 // Allows menu to resize
161 XmNallowShellResize, True,
162 XmNdeleteResponse, XmDO_NOTHING,
163 XmNmappedWhenManaged, False,
164 XmNiconic, (style & wxICONIZE) ? TRUE : FALSE,
165 NULL);
166
167 if (!title.IsNull())
168 XtVaSetValues((Widget) m_frameShell,
169 XmNtitle, (const char*) title,
170 NULL);
171
172 m_frameWidget = (WXWidget) XtVaCreateManagedWidget("main_window",
173 xmMainWindowWidgetClass, (Widget) m_frameShell,
174 XmNresizePolicy, XmRESIZE_NONE,
175 NULL);
176
177 m_workArea = (WXWidget) XtVaCreateWidget("form",
178 xmFormWidgetClass, (Widget) m_frameWidget,
179 XmNresizePolicy, XmRESIZE_NONE,
180 NULL);
181
182 m_clientArea = (WXWidget) XtVaCreateWidget("client",
183 xmBulletinBoardWidgetClass, (Widget) m_workArea,
184 XmNmarginWidth, 0,
185 XmNmarginHeight, 0,
186 XmNrightAttachment, XmATTACH_FORM,
187 XmNleftAttachment, XmATTACH_FORM,
188 XmNtopAttachment, XmATTACH_FORM,
189 XmNbottomAttachment, XmATTACH_FORM,
190 // XmNresizePolicy, XmRESIZE_ANY,
191 NULL);
192
193 XtVaSetValues((Widget) m_frameWidget,
194 XmNworkWindow, (Widget) m_workArea,
195 NULL);
196
197
198 XtManageChild((Widget) m_clientArea);
199 XtManageChild((Widget) m_workArea);
200
201 wxASSERT_MSG ((wxWidgetHashTable->Get((long)m_workArea) == (wxObject*) NULL), "Widget table clash in frame.cpp") ;
202
203 wxAddWindowToTable((Widget) m_workArea, this);
204
205 XtTranslations ptr ;
206
207 XtOverrideTranslations((Widget) m_workArea,
208 ptr = XtParseTranslationTable("<Configure>: resize()"));
209
210 XtFree((char *)ptr);
211
212 XtAddCallback((Widget) m_workArea, XmNfocusCallback,
213 (XtCallbackProc)wxFrameFocusProc, (XtPointer)this);
214
215 /* Part of show-&-hide fix */
216 XtAddEventHandler((Widget) m_frameShell, StructureNotifyMask,
217 False, (XtEventHandler)wxFrameMapProc,
218 (XtPointer)m_workArea);
219
220 if (x > -1)
221 XtVaSetValues((Widget) m_frameShell, XmNx, x, NULL);
222 if (y > -1)
223 XtVaSetValues((Widget) m_frameShell, XmNy, y, NULL);
224 if (width > -1)
225 XtVaSetValues((Widget) m_frameShell, XmNwidth, width, NULL);
226 if (height > -1)
227 XtVaSetValues((Widget) m_frameShell, XmNheight, height, NULL);
228
229 m_mainWidget = m_frameWidget;
230
231 ChangeFont(FALSE);
232
233 // This patch comes from Torsten Liermann lier@lier1.muc.de
234 if (XmIsMotifWMRunning( (Widget) m_frameShell ))
235 {
236 int decor = 0 ;
237 if (style & wxRESIZE_BORDER)
238 decor |= MWM_DECOR_RESIZEH ;
239 if (style & wxSYSTEM_MENU)
240 decor |= MWM_DECOR_MENU;
241 if ((style & wxCAPTION) ||
242 (style & wxTINY_CAPTION_HORIZ) ||
243 (style & wxTINY_CAPTION_VERT))
244 decor |= MWM_DECOR_TITLE;
245 if (style & wxTHICK_FRAME)
246 decor |= MWM_DECOR_BORDER;
247 if (style & wxTHICK_FRAME)
248 decor |= MWM_DECOR_BORDER;
249 if (style & wxMINIMIZE_BOX)
250 decor |= MWM_DECOR_MINIMIZE;
251 if (style & wxMAXIMIZE_BOX)
252 decor |= MWM_DECOR_MAXIMIZE;
253 XtVaSetValues((Widget) m_frameShell,XmNmwmDecorations,decor,NULL) ;
254 }
255 // This allows non-Motif window managers to support at least the
256 // no-decorations case.
257 else
258 {
259 if (style == 0)
260 XtVaSetValues((Widget) m_frameShell,XmNoverrideRedirect,TRUE,NULL);
261 }
262 XtRealizeWidget((Widget) m_frameShell);
263
264 // Intercept CLOSE messages from the window manager
265 Atom WM_DELETE_WINDOW = XmInternAtom(XtDisplay((Widget) m_frameShell), "WM_DELETE_WINDOW", False);
266 #if (XmREVISION > 1 || XmVERSION > 1)
267 XmAddWMProtocolCallback((Widget) m_frameShell, WM_DELETE_WINDOW, (XtCallbackProc) wxCloseFrameCallback, (XtPointer)this);
268 #else
269 #if XmREVISION == 1
270 XmAddWMProtocolCallback((Widget) m_frameShell, WM_DELETE_WINDOW, (XtCallbackProc) wxCloseFrameCallback, (caddr_t)this);
271 #else
272 XmAddWMProtocolCallback((Widget) m_frameShell, WM_DELETE_WINDOW, (void (*)())wxCloseFrameCallback, (caddr_t)this);
273 #endif
274 #endif
275
276 ChangeBackgroundColour();
277
278 PreResize();
279
280 wxSizeEvent sizeEvent(wxSize(width, height), GetId());
281 sizeEvent.SetEventObject(this);
282
283 GetEventHandler()->ProcessEvent(sizeEvent);
284
285 return TRUE;
286 }
287
288 wxFrame::~wxFrame()
289 {
290 if (GetMainWidget())
291 Show(FALSE);
292
293 if (m_frameMenuBar)
294 {
295 m_frameMenuBar->DestroyMenuBar();
296
297 // Hack to stop core dump on Ultrix, OSF, for some strange reason.
298 #if MOTIF_MENUBAR_DELETE_FIX
299 GetMenuBar()->SetMainWidget((WXWidget) NULL);
300 #endif
301 delete m_frameMenuBar;
302 m_frameMenuBar = NULL;
303 }
304
305 wxTopLevelWindows.DeleteObject(this);
306 wxModelessWindows.DeleteObject(this);
307
308 if (m_frameStatusBar)
309 delete m_frameStatusBar;
310
311 DestroyChildren();
312
313 /*
314 int i;
315 for (i = 0; i < wxMAX_STATUS; i++)
316 if (statusTextWidget[i])
317 XtDestroyWidget (statusTextWidget[i]);
318
319 if (statusLineForm)
320 XtDestroyWidget (statusLineForm);
321
322 if (statusLineWidget)
323 XtDestroyWidget (statusLineWidget);
324 */
325
326 if (m_workArea)
327 {
328 wxDeleteWindowFromTable((Widget) m_workArea);
329
330 XtDestroyWidget ((Widget) m_workArea);
331 }
332
333 if (m_frameWidget)
334 {
335 wxDeleteWindowFromTable((Widget) m_frameWidget);
336 XtDestroyWidget ((Widget) m_frameWidget);
337 }
338
339 if (m_frameShell)
340 XtDestroyWidget ((Widget) m_frameShell);
341
342 SetMainWidget((WXWidget) NULL);
343
344 /* Check if it's the last top-level window */
345
346 if (wxTheApp && (wxTopLevelWindows.Number() == 0))
347 {
348 wxTheApp->SetTopWindow(NULL);
349
350 if (wxTheApp->GetExitOnFrameDelete())
351 {
352 // Signal to the app that we're going to close
353 wxTheApp->ExitMainLoop();
354 }
355 }
356
357 }
358
359 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
360 void wxFrame::GetClientSize(int *x, int *y) const
361 {
362 Dimension xx, yy;
363 XtVaGetValues((Widget) m_workArea, XmNwidth, &xx, XmNheight, &yy, NULL);
364
365 if (m_frameStatusBar)
366 {
367 int sbw, sbh;
368 m_frameStatusBar->GetSize(& sbw, & sbh);
369 yy -= sbh;
370 }
371 #if wxUSE_TOOLBAR
372 if (m_frameToolBar)
373 {
374 int tbw, tbh;
375 m_frameToolBar->GetSize(& tbw, & tbh);
376 if (m_frameToolBar->GetWindowStyleFlag() & wxTB_VERTICAL)
377 xx -= tbw;
378 else
379 yy -= tbh;
380 }
381 #endif // wxUSE_TOOLBAR
382 /*
383 if (GetMenuBar() != (wxMenuBar*) NULL)
384 {
385 // it seems that if a frame holds a panel, the menu bar size
386 // gets automatically taken care of --- grano@cs.helsinki.fi 4.4.95
387 bool hasSubPanel = FALSE;
388 for(wxNode* node = GetChildren().First(); node; node = node->Next())
389 {
390 wxWindow *win = (wxWindow *)node->Data();
391 hasSubPanel = (win->IsKindOf(CLASSINFO(wxPanel)) && !win->IsKindOf(CLASSINFO(wxDialog)));
392
393 if (hasSubPanel)
394 break;
395 }
396 if (! hasSubPanel) {
397 Dimension ys;
398 XtVaGetValues((Widget) GetMenuBarWidget(), XmNheight, &ys, NULL);
399 yy -= ys;
400 }
401 }
402 */
403
404 *x = xx; *y = yy;
405 }
406
407 // Set the client size (i.e. leave the calculation of borders etc.
408 // to wxWindows)
409 void wxFrame::SetClientSize(int width, int height)
410 {
411 // Calculate how large the new main window should be
412 // by finding the difference between the client area and the
413 // main window area, and adding on to the new client area
414 if (width > -1)
415 XtVaSetValues((Widget) m_workArea, XmNwidth, width, NULL);
416
417 if (height > -1)
418 {
419 if (m_frameStatusBar)
420 {
421 int sbw, sbh;
422 m_frameStatusBar->GetSize(& sbw, & sbh);
423 height += sbh;
424 }
425 #if wxUSE_TOOLBAR
426 if (m_frameToolBar)
427 {
428 int tbw, tbh;
429 m_frameToolBar->GetSize(& tbw, & tbh);
430 if (m_frameToolBar->GetWindowStyleFlag() & wxTB_VERTICAL)
431 width += tbw;
432 else
433 height += tbh;
434 }
435 #endif // wxUSE_TOOLBAR
436
437 XtVaSetValues((Widget) m_workArea, XmNheight, height, NULL);
438 }
439 PreResize();
440
441 wxSizeEvent sizeEvent(wxSize(width, height), GetId());
442 sizeEvent.SetEventObject(this);
443
444 GetEventHandler()->ProcessEvent(sizeEvent);
445
446 }
447
448 void wxFrame::GetSize(int *width, int *height) const
449 {
450 Dimension xx, yy;
451 XtVaGetValues((Widget) m_frameShell, XmNwidth, &xx, XmNheight, &yy, NULL);
452 *width = xx; *height = yy;
453 }
454
455 void wxFrame::GetPosition(int *x, int *y) const
456 {
457 Window parent_window = XtWindow((Widget) m_frameShell),
458 next_parent = XtWindow((Widget) m_frameShell),
459 root = RootWindowOfScreen(XtScreen((Widget) m_frameShell));
460
461 // search for the parent that is child of ROOT, because the WM may
462 // reparent twice and notify only the next parent (like FVWM)
463 while (next_parent != root) {
464 Window *theChildren; unsigned int n;
465 parent_window = next_parent;
466 XQueryTree(XtDisplay((Widget) m_frameShell), parent_window, &root,
467 &next_parent, &theChildren, &n);
468 XFree(theChildren); // not needed
469 }
470 int xx, yy; unsigned int dummy;
471 XGetGeometry(XtDisplay((Widget) m_frameShell), parent_window, &root,
472 &xx, &yy, &dummy, &dummy, &dummy, &dummy);
473 if (x) *x = xx;
474 if (y) *y = yy;
475 }
476
477 void wxFrame::SetSize(int x, int y, int width, int height, int sizeFlags)
478 {
479 if (x > -1)
480 XtVaSetValues((Widget) m_frameShell, XmNx, x, NULL);
481 if (y > -1)
482 XtVaSetValues((Widget) m_frameShell, XmNy, y, NULL);
483 if (width > -1)
484 XtVaSetValues((Widget) m_frameWidget, XmNwidth, width, NULL);
485 if (height > -1)
486 XtVaSetValues((Widget) m_frameWidget, XmNheight, height, NULL);
487
488 if (!(height == -1 && width == -1))
489 {
490 PreResize();
491
492 wxSizeEvent sizeEvent(wxSize(width, height), GetId());
493 sizeEvent.SetEventObject(this);
494
495 GetEventHandler()->ProcessEvent(sizeEvent);
496 }
497 }
498
499 bool wxFrame::Show(bool show)
500 {
501 if (!m_frameShell)
502 return wxWindow::Show(show);
503
504 m_visibleStatus = show; /* show-&-hide fix */
505
506 m_isShown = show;
507 if (show) {
508 XtMapWidget((Widget) m_frameShell);
509 XRaiseWindow(XtDisplay((Widget) m_frameShell), XtWindow((Widget) m_frameShell));
510 } else {
511 XtUnmapWidget((Widget) m_frameShell);
512 // XmUpdateDisplay(wxTheApp->topLevel); // Experimental: may be responsible for crashes
513 }
514 return TRUE;
515 }
516
517 void wxFrame::Iconize(bool iconize)
518 {
519 if (!iconize)
520 Show(TRUE);
521
522 if (m_frameShell)
523 XtVaSetValues((Widget) m_frameShell, XmNiconic, (Boolean)iconize, NULL);
524 }
525
526 // Equivalent to maximize/restore in Windows
527 void wxFrame::Maximize(bool maximize)
528 {
529 Show(TRUE);
530
531 if (maximize && m_frameShell)
532 XtVaSetValues((Widget) m_frameShell, XmNiconic, FALSE, NULL);
533 }
534
535 bool wxFrame::IsIconized() const
536 {
537 if (!m_frameShell)
538 return FALSE;
539
540 Boolean iconic;
541 XtVaGetValues((Widget) m_frameShell, XmNiconic, &iconic, NULL);
542 return iconic;
543 }
544
545 // Is it maximized?
546 bool wxFrame::IsMaximized(void) const
547 {
548 // No maximizing in Motif (?)
549 return FALSE;
550 }
551
552 void wxFrame::SetTitle(const wxString& title)
553 {
554 if (title == m_title)
555 return;
556
557 m_title = title;
558
559 if (!title.IsNull())
560 XtVaSetValues((Widget) m_frameShell,
561 XmNtitle, (const char*) title,
562 XmNiconName, (const char*) title,
563 NULL);
564 }
565
566 void wxFrame::SetIcon(const wxIcon& icon)
567 {
568 m_icon = icon;
569
570 if (!m_frameShell)
571 return;
572
573 if (!icon.Ok() || !icon.GetPixmap())
574 return;
575
576 XtVaSetValues((Widget) m_frameShell, XtNiconPixmap, icon.GetPixmap(), NULL);
577 }
578
579 wxStatusBar *wxFrame::OnCreateStatusBar(int number, long style, wxWindowID id,
580 const wxString& name)
581 {
582 wxStatusBar *statusBar = NULL;
583
584 statusBar = new wxStatusBar(this, id, wxPoint(0, 0), wxSize(100, 20),
585 style, name);
586
587 // Set the height according to the font and the border size
588 wxClientDC dc(statusBar);
589 dc.SetFont(statusBar->GetFont());
590
591 long x, y;
592 dc.GetTextExtent("X", &x, &y);
593
594 int height = (int)( (y * 1.1) + 2* statusBar->GetBorderY());
595
596 statusBar->SetSize(-1, -1, 100, height);
597
598 statusBar->SetFieldsCount(number);
599 return statusBar;
600 }
601
602 wxStatusBar* wxFrame::CreateStatusBar(int number, long style, wxWindowID id,
603 const wxString& name)
604 {
605 // Calling CreateStatusBar twice is an error.
606 wxCHECK_MSG( m_frameStatusBar == NULL, FALSE,
607 "recreating status bar in wxFrame" );
608
609 m_frameStatusBar = OnCreateStatusBar(number, style, id,
610 name);
611 if ( m_frameStatusBar )
612 {
613 PositionStatusBar();
614 return m_frameStatusBar;
615 }
616 else
617 return NULL;
618 }
619
620 void wxFrame::SetStatusText(const wxString& text, int number)
621 {
622 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set text for" );
623
624 m_frameStatusBar->SetStatusText(text, number);
625 }
626
627 void wxFrame::SetStatusWidths(int n, const int widths_field[])
628 {
629 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set widths for" );
630
631 m_frameStatusBar->SetStatusWidths(n, widths_field);
632 PositionStatusBar();
633 }
634
635 void wxFrame::PositionStatusBar()
636 {
637 if (!m_frameStatusBar)
638 return;
639
640 int w, h;
641 GetClientSize(&w, &h);
642 int sw, sh;
643 m_frameStatusBar->GetSize(&sw, &sh);
644
645 // Since we wish the status bar to be directly under the client area,
646 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
647 m_frameStatusBar->SetSize(0, h, w, sh);
648 }
649
650 WXWidget wxFrame::GetMenuBarWidget() const
651 {
652 if (GetMenuBar())
653 return GetMenuBar()->GetMainWidget();
654 else
655 return (WXWidget) NULL;
656 }
657
658 void wxFrame::SetMenuBar(wxMenuBar *menuBar)
659 {
660 if (!menuBar)
661 {
662 m_frameMenuBar = NULL;
663 return;
664 }
665
666 // Currently can't set it twice
667 // wxASSERT_MSG( (m_frameMenuBar == (wxMenuBar*) NULL), "Cannot set the menubar more than once");
668
669 if (m_frameMenuBar)
670 {
671 m_frameMenuBar->DestroyMenuBar();
672 delete m_frameMenuBar;
673 }
674
675 m_frameMenuBar = menuBar;
676 m_frameMenuBar->CreateMenuBar(this);
677 }
678
679 void wxFrame::Fit()
680 {
681 // Work out max. size
682 wxNode *node = GetChildren().First();
683 int max_width = 0;
684 int max_height = 0;
685 while (node)
686 {
687 // Find a child that's a subwindow, but not a dialog box.
688 wxWindow *win = (wxWindow *)node->Data();
689
690 if (!win->IsKindOf(CLASSINFO(wxFrame)) &&
691 !win->IsKindOf(CLASSINFO(wxDialog)))
692 {
693 int width, height;
694 int x, y;
695 win->GetSize(&width, &height);
696 win->GetPosition(&x, &y);
697
698 if ((x + width) > max_width)
699 max_width = x + width;
700 if ((y + height) > max_height)
701 max_height = y + height;
702 }
703 node = node->Next();
704 }
705 SetClientSize(max_width, max_height);
706 }
707
708 // Responds to colour changes, and passes event on to children.
709 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
710 {
711 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE));
712 Refresh();
713
714 if ( m_frameStatusBar )
715 {
716 wxSysColourChangedEvent event2;
717 event2.SetEventObject( m_frameStatusBar );
718 m_frameStatusBar->ProcessEvent(event2);
719 }
720
721 // Propagate the event to the non-top-level children
722 wxWindow::OnSysColourChanged(event);
723 }
724
725 // Default resizing behaviour - if only ONE subwindow,
726 // resize to client rectangle size
727 void wxFrame::OnSize(wxSizeEvent& event)
728 {
729 // if we're using constraints - do use them
730 #if wxUSE_CONSTRAINTS
731 if ( GetAutoLayout() ) {
732 Layout();
733 return;
734 }
735 #endif
736
737 // do we have _exactly_ one child?
738 wxWindow *child = NULL;
739 for ( wxNode *node = GetChildren().First(); node; node = node->Next() )
740 {
741 wxWindow *win = (wxWindow *)node->Data();
742 if ( !win->IsKindOf(CLASSINFO(wxFrame)) &&
743 !win->IsKindOf(CLASSINFO(wxDialog)) &&
744 (win != GetStatusBar())
745 #if wxUSE_TOOLBAR
746 && (win != GetToolBar())
747 #endif // wxUSE_TOOLBAR
748 )
749 {
750 if ( child )
751 return; // it's our second subwindow - nothing to do
752 child = win;
753 }
754 }
755
756 if ( child ) {
757 // we have exactly one child - set it's size to fill the whole frame
758 int clientW, clientH;
759 GetClientSize(&clientW, &clientH);
760
761 int x = 0;
762 int y = 0;
763
764 child->SetSize(x, y, clientW, clientH);
765 }
766 }
767
768 // Default activation behaviour - set the focus for the first child
769 // subwindow found.
770 void wxFrame::OnActivate(wxActivateEvent& event)
771 {
772 for(wxNode *node = GetChildren().First(); node; node = node->Next())
773 {
774 // Find a child that's a subwindow, but not a dialog box.
775 wxWindow *child = (wxWindow *)node->Data();
776 if (!child->IsKindOf(CLASSINFO(wxFrame)) &&
777 !child->IsKindOf(CLASSINFO(wxDialog)))
778 {
779 child->SetFocus();
780 return;
781 }
782 }
783 }
784
785 // The default implementation for the close window event.
786 // OnClose for backward compatibility.
787
788 void wxFrame::OnCloseWindow(wxCloseEvent& event)
789 {
790 this->Destroy();
791 }
792
793 // Destroy the window (delayed, if a managed window)
794 bool wxFrame::Destroy()
795 {
796 if (!wxPendingDelete.Member(this))
797 wxPendingDelete.Append(this);
798 return TRUE;
799 }
800
801 // Default menu selection behaviour - display a help string
802 void wxFrame::OnMenuHighlight(wxMenuEvent& event)
803 {
804 if (GetStatusBar())
805 {
806 if (event.GetMenuId() == -1)
807 SetStatusText("");
808 else
809 {
810 wxMenuBar *menuBar = GetMenuBar();
811 if (menuBar)
812 {
813 wxString helpString(menuBar->GetHelpString(event.GetMenuId()));
814 if (helpString != "")
815 SetStatusText(helpString);
816 }
817 }
818 }
819 }
820
821 wxMenuBar *wxFrame::GetMenuBar() const
822 {
823 return m_frameMenuBar;
824 }
825
826 void wxFrame::Centre(int direction)
827 {
828 int display_width, display_height, width, height, x, y;
829 wxDisplaySize(&display_width, &display_height);
830
831 GetSize(&width, &height);
832 GetPosition(&x, &y);
833
834 if (direction & wxHORIZONTAL)
835 x = (int)((display_width - width)/2);
836 if (direction & wxVERTICAL)
837 y = (int)((display_height - height)/2);
838
839 SetSize(x, y, width, height);
840 }
841
842 // Call this to simulate a menu command
843 void wxFrame::Command(int id)
844 {
845 ProcessCommand(id);
846 }
847
848 void wxFrame::ProcessCommand(int id)
849 {
850 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
851 commandEvent.SetInt( id );
852 commandEvent.SetEventObject( this );
853
854 wxMenuBar *bar = GetMenuBar() ;
855 if (!bar)
856 return;
857
858 /* TODO: check the menu item if required
859 wxMenuItem *item = bar->FindItemForId(id) ;
860 if (item && item->IsCheckable())
861 {
862 bar->Check(id,!bar->Checked(id)) ;
863 }
864 */
865
866 GetEventHandler()->ProcessEvent(commandEvent);
867 }
868
869 // Checks if there is a toolbar, and returns the first free client position
870 wxPoint wxFrame::GetClientAreaOrigin() const
871 {
872 wxPoint pt(0, 0);
873 #if wxUSE_TOOLBAR
874 if (GetToolBar())
875 {
876 int w, h;
877 GetToolBar()->GetSize(& w, & h);
878
879 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
880 {
881 pt.x += w;
882 }
883 else
884 {
885 pt.y += h;
886 }
887 }
888 #endif // wxUSE_TOOLBAR
889
890 return pt;
891 }
892
893 void wxFrame::ScreenToClient(int *x, int *y) const
894 {
895 wxWindow::ScreenToClient(x, y);
896
897 // We may be faking the client origin.
898 // So a window that's really at (0, 30) may appear
899 // (to wxWin apps) to be at (0, 0).
900 wxPoint pt(GetClientAreaOrigin());
901 *x -= pt.x;
902 *y -= pt.y;
903 }
904
905 void wxFrame::ClientToScreen(int *x, int *y) const
906 {
907 // We may be faking the client origin.
908 // So a window that's really at (0, 30) may appear
909 // (to wxWin apps) to be at (0, 0).
910 wxPoint pt1(GetClientAreaOrigin());
911 *x += pt1.x;
912 *y += pt1.y;
913
914 wxWindow::ClientToScreen(x, y);
915 }
916
917 #if wxUSE_TOOLBAR
918 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
919 {
920 wxCHECK_MSG( m_frameToolBar == NULL, FALSE,
921 "recreating toolbar in wxFrame" );
922
923 wxToolBar* toolBar = OnCreateToolBar(style, id, name);
924 if (toolBar)
925 {
926 SetToolBar(toolBar);
927 PositionToolBar();
928 return toolBar;
929 }
930 else
931 {
932 return NULL;
933 }
934 }
935
936 wxToolBar* wxFrame::OnCreateToolBar(long style, wxWindowID id, const wxString& name)
937 {
938 return new wxToolBar(this, id, wxPoint(0, 0), wxSize(100, 24), style, name);
939 }
940
941 void wxFrame::SetToolBar(wxToolBar *toolbar)
942 {
943 m_frameToolBar = toolbar;
944 }
945
946 wxToolBar *wxFrame::GetToolBar() const
947 {
948 return m_frameToolBar;
949 }
950
951 void wxFrame::PositionToolBar()
952 {
953 int cw, ch;
954
955 GetClientSize(& cw, &ch);
956
957 if (GetToolBar())
958 {
959 int tw, th;
960 GetToolBar()->GetSize(& tw, & th);
961
962 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
963 {
964 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
965 // means, pretend we don't have toolbar/status bar, so we
966 // have the original client size.
967 GetToolBar()->SetSize(0, 0, tw, ch + th, wxSIZE_NO_ADJUSTMENTS);
968 }
969 else
970 {
971 // Use the 'real' position
972 GetToolBar()->SetSize(0, 0, cw, th, wxSIZE_NO_ADJUSTMENTS);
973 }
974 }
975 }
976 #endif // wxUSE_TOOLBAR
977
978 void wxFrame::CaptureMouse()
979 {
980 if (m_winCaptured)
981 return;
982
983 if (GetMainWidget())
984 XtAddGrab((Widget) m_frameShell, TRUE, FALSE);
985 m_winCaptured = TRUE;
986 }
987
988 void wxFrame::ReleaseMouse()
989 {
990 if (!m_winCaptured)
991 return;
992
993 if (GetMainWidget())
994 XtRemoveGrab((Widget) m_frameShell);
995 m_winCaptured = FALSE;
996 }
997
998 void wxFrame::Raise(void)
999 {
1000 Window parent_window = XtWindow((Widget) m_frameShell),
1001 next_parent = XtWindow((Widget) m_frameShell),
1002 root = RootWindowOfScreen(XtScreen((Widget) m_frameShell));
1003 // search for the parent that is child of ROOT, because the WM may
1004 // reparent twice and notify only the next parent (like FVWM)
1005 while (next_parent != root) {
1006 Window *theChildren; unsigned int n;
1007 parent_window = next_parent;
1008 XQueryTree(XtDisplay((Widget) m_frameShell), parent_window, &root,
1009 &next_parent, &theChildren, &n);
1010 XFree(theChildren); // not needed
1011 }
1012 XRaiseWindow(XtDisplay((Widget) m_frameShell), parent_window);
1013 }
1014
1015 void wxFrame::Lower(void)
1016 {
1017 Window parent_window = XtWindow((Widget) m_frameShell),
1018 next_parent = XtWindow((Widget) m_frameShell),
1019 root = RootWindowOfScreen(XtScreen((Widget) m_frameShell));
1020 // search for the parent that is child of ROOT, because the WM may
1021 // reparent twice and notify only the next parent (like FVWM)
1022 while (next_parent != root) {
1023 Window *theChildren; unsigned int n;
1024 parent_window = next_parent;
1025 XQueryTree(XtDisplay((Widget) m_frameShell), parent_window, &root,
1026 &next_parent, &theChildren, &n);
1027 XFree(theChildren); // not needed
1028 }
1029 XLowerWindow(XtDisplay((Widget) m_frameShell), parent_window);
1030 }
1031
1032 void wxFrameFocusProc(Widget workArea, XtPointer clientData,
1033 XmAnyCallbackStruct *cbs)
1034 {
1035 wxFrame *frame = (wxFrame *)clientData;
1036
1037 // wxDebugMsg("focus proc from frame %ld\n",(long)frame);
1038 // TODO
1039 // frame->GetEventHandler()->OnSetFocus();
1040 }
1041
1042 /* MATTEW: Used to insure that hide-&-show within an event cycle works */
1043 static void wxFrameMapProc(Widget frameShell, XtPointer clientData,
1044 XCrossingEvent * event)
1045 {
1046 wxFrame *frame = (wxFrame *)wxWidgetHashTable->Get((long)clientData);
1047
1048 if (frame) {
1049 XEvent *e = (XEvent *)event;
1050
1051 if (e->xany.type == MapNotify)
1052 {
1053 // Iconize fix
1054 XtVaSetValues(frameShell, XmNiconic, (Boolean)False, NULL);
1055 if (!frame->GetVisibleStatus())
1056 {
1057 /* We really wanted this to be hidden! */
1058 XtUnmapWidget((Widget) frame->GetShellWidget());
1059 }
1060 }
1061 else if (e->xany.type == UnmapNotify)
1062 // Iconize fix
1063 XtVaSetValues(frameShell, XmNiconic, (Boolean)True, NULL);
1064 }
1065 }
1066
1067 //// Motif-specific
1068 bool wxFrame::PreResize()
1069 {
1070 #if wxUSE_TOOLBAR
1071 PositionToolBar();
1072 #endif // wxUSE_TOOLBAR
1073 PositionStatusBar();
1074 return TRUE;
1075 }
1076
1077 WXWidget wxFrame::GetClientWidget() const
1078 {
1079 return m_clientArea;
1080 }
1081
1082 void wxFrame::ChangeFont(bool keepOriginalSize)
1083 {
1084 // TODO
1085 }
1086
1087 void wxFrame::ChangeBackgroundColour()
1088 {
1089 if (GetClientWidget())
1090 DoChangeBackgroundColour(GetClientWidget(), m_backgroundColour);
1091 }
1092
1093 void wxFrame::ChangeForegroundColour()
1094 {
1095 if (GetClientWidget())
1096 DoChangeForegroundColour(GetClientWidget(), m_foregroundColour);
1097 }
1098
1099 void wxCloseFrameCallback(Widget widget, XtPointer client_data, XmAnyCallbackStruct *cbs)
1100 {
1101 wxFrame *frame = (wxFrame *)client_data;
1102
1103 wxCloseEvent closeEvent(wxEVT_CLOSE_WINDOW, frame->GetId());
1104 closeEvent.SetEventObject(frame);
1105
1106 // May delete the frame (with delayed deletion)
1107 frame->GetEventHandler()->ProcessEvent(closeEvent);
1108 }
1109