Changed GetForce -> !CanVeto
[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 - calls
786 // OnClose for backward compatibility.
787
788 void wxFrame::OnCloseWindow(wxCloseEvent& event)
789 {
790 // Compatibility
791 if ( GetEventHandler()->OnClose() || !event.CanVeto())
792 {
793 this->Destroy();
794 }
795 else
796 event.Veto(TRUE);
797 }
798
799 bool wxFrame::OnClose()
800 {
801 return TRUE;
802 }
803
804 // Destroy the window (delayed, if a managed window)
805 bool wxFrame::Destroy()
806 {
807 if (!wxPendingDelete.Member(this))
808 wxPendingDelete.Append(this);
809 return TRUE;
810 }
811
812 // Default menu selection behaviour - display a help string
813 void wxFrame::OnMenuHighlight(wxMenuEvent& event)
814 {
815 if (GetStatusBar())
816 {
817 if (event.GetMenuId() == -1)
818 SetStatusText("");
819 else
820 {
821 wxMenuBar *menuBar = GetMenuBar();
822 if (menuBar)
823 {
824 wxString helpString(menuBar->GetHelpString(event.GetMenuId()));
825 if (helpString != "")
826 SetStatusText(helpString);
827 }
828 }
829 }
830 }
831
832 wxMenuBar *wxFrame::GetMenuBar() const
833 {
834 return m_frameMenuBar;
835 }
836
837 void wxFrame::Centre(int direction)
838 {
839 int display_width, display_height, width, height, x, y;
840 wxDisplaySize(&display_width, &display_height);
841
842 GetSize(&width, &height);
843 GetPosition(&x, &y);
844
845 if (direction & wxHORIZONTAL)
846 x = (int)((display_width - width)/2);
847 if (direction & wxVERTICAL)
848 y = (int)((display_height - height)/2);
849
850 SetSize(x, y, width, height);
851 }
852
853 // Call this to simulate a menu command
854 void wxFrame::Command(int id)
855 {
856 ProcessCommand(id);
857 }
858
859 void wxFrame::ProcessCommand(int id)
860 {
861 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
862 commandEvent.SetInt( id );
863 commandEvent.SetEventObject( this );
864
865 wxMenuBar *bar = GetMenuBar() ;
866 if (!bar)
867 return;
868
869 /* TODO: check the menu item if required
870 wxMenuItem *item = bar->FindItemForId(id) ;
871 if (item && item->IsCheckable())
872 {
873 bar->Check(id,!bar->Checked(id)) ;
874 }
875 */
876
877 GetEventHandler()->ProcessEvent(commandEvent);
878 }
879
880 // Checks if there is a toolbar, and returns the first free client position
881 wxPoint wxFrame::GetClientAreaOrigin() const
882 {
883 wxPoint pt(0, 0);
884 #if wxUSE_TOOLBAR
885 if (GetToolBar())
886 {
887 int w, h;
888 GetToolBar()->GetSize(& w, & h);
889
890 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
891 {
892 pt.x += w;
893 }
894 else
895 {
896 pt.y += h;
897 }
898 }
899 #endif // wxUSE_TOOLBAR
900
901 return pt;
902 }
903
904 void wxFrame::ScreenToClient(int *x, int *y) const
905 {
906 wxWindow::ScreenToClient(x, y);
907
908 // We may be faking the client origin.
909 // So a window that's really at (0, 30) may appear
910 // (to wxWin apps) to be at (0, 0).
911 wxPoint pt(GetClientAreaOrigin());
912 *x -= pt.x;
913 *y -= pt.y;
914 }
915
916 void wxFrame::ClientToScreen(int *x, int *y) const
917 {
918 // We may be faking the client origin.
919 // So a window that's really at (0, 30) may appear
920 // (to wxWin apps) to be at (0, 0).
921 wxPoint pt1(GetClientAreaOrigin());
922 *x += pt1.x;
923 *y += pt1.y;
924
925 wxWindow::ClientToScreen(x, y);
926 }
927
928 #if wxUSE_TOOLBAR
929 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
930 {
931 wxCHECK_MSG( m_frameToolBar == NULL, FALSE,
932 "recreating toolbar in wxFrame" );
933
934 wxToolBar* toolBar = OnCreateToolBar(style, id, name);
935 if (toolBar)
936 {
937 SetToolBar(toolBar);
938 PositionToolBar();
939 return toolBar;
940 }
941 else
942 {
943 return NULL;
944 }
945 }
946
947 wxToolBar* wxFrame::OnCreateToolBar(long style, wxWindowID id, const wxString& name)
948 {
949 return new wxToolBar(this, id, wxPoint(0, 0), wxSize(100, 24), style, name);
950 }
951
952 void wxFrame::SetToolBar(wxToolBar *toolbar)
953 {
954 m_frameToolBar = toolbar;
955 }
956
957 wxToolBar *wxFrame::GetToolBar() const
958 {
959 return m_frameToolBar;
960 }
961
962 void wxFrame::PositionToolBar()
963 {
964 int cw, ch;
965
966 GetClientSize(& cw, &ch);
967
968 if (GetToolBar())
969 {
970 int tw, th;
971 GetToolBar()->GetSize(& tw, & th);
972
973 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
974 {
975 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
976 // means, pretend we don't have toolbar/status bar, so we
977 // have the original client size.
978 GetToolBar()->SetSize(0, 0, tw, ch + th, wxSIZE_NO_ADJUSTMENTS);
979 }
980 else
981 {
982 // Use the 'real' position
983 GetToolBar()->SetSize(0, 0, cw, th, wxSIZE_NO_ADJUSTMENTS);
984 }
985 }
986 }
987 #endif // wxUSE_TOOLBAR
988
989 void wxFrame::CaptureMouse()
990 {
991 if (m_winCaptured)
992 return;
993
994 if (GetMainWidget())
995 XtAddGrab((Widget) m_frameShell, TRUE, FALSE);
996 m_winCaptured = TRUE;
997 }
998
999 void wxFrame::ReleaseMouse()
1000 {
1001 if (!m_winCaptured)
1002 return;
1003
1004 if (GetMainWidget())
1005 XtRemoveGrab((Widget) m_frameShell);
1006 m_winCaptured = FALSE;
1007 }
1008
1009 void wxFrame::Raise(void)
1010 {
1011 Window parent_window = XtWindow((Widget) m_frameShell),
1012 next_parent = XtWindow((Widget) m_frameShell),
1013 root = RootWindowOfScreen(XtScreen((Widget) m_frameShell));
1014 // search for the parent that is child of ROOT, because the WM may
1015 // reparent twice and notify only the next parent (like FVWM)
1016 while (next_parent != root) {
1017 Window *theChildren; unsigned int n;
1018 parent_window = next_parent;
1019 XQueryTree(XtDisplay((Widget) m_frameShell), parent_window, &root,
1020 &next_parent, &theChildren, &n);
1021 XFree(theChildren); // not needed
1022 }
1023 XRaiseWindow(XtDisplay((Widget) m_frameShell), parent_window);
1024 }
1025
1026 void wxFrame::Lower(void)
1027 {
1028 Window parent_window = XtWindow((Widget) m_frameShell),
1029 next_parent = XtWindow((Widget) m_frameShell),
1030 root = RootWindowOfScreen(XtScreen((Widget) m_frameShell));
1031 // search for the parent that is child of ROOT, because the WM may
1032 // reparent twice and notify only the next parent (like FVWM)
1033 while (next_parent != root) {
1034 Window *theChildren; unsigned int n;
1035 parent_window = next_parent;
1036 XQueryTree(XtDisplay((Widget) m_frameShell), parent_window, &root,
1037 &next_parent, &theChildren, &n);
1038 XFree(theChildren); // not needed
1039 }
1040 XLowerWindow(XtDisplay((Widget) m_frameShell), parent_window);
1041 }
1042
1043 void wxFrameFocusProc(Widget workArea, XtPointer clientData,
1044 XmAnyCallbackStruct *cbs)
1045 {
1046 wxFrame *frame = (wxFrame *)clientData;
1047
1048 // wxDebugMsg("focus proc from frame %ld\n",(long)frame);
1049 // TODO
1050 // frame->GetEventHandler()->OnSetFocus();
1051 }
1052
1053 /* MATTEW: Used to insure that hide-&-show within an event cycle works */
1054 static void wxFrameMapProc(Widget frameShell, XtPointer clientData,
1055 XCrossingEvent * event)
1056 {
1057 wxFrame *frame = (wxFrame *)wxWidgetHashTable->Get((long)clientData);
1058
1059 if (frame) {
1060 XEvent *e = (XEvent *)event;
1061
1062 if (e->xany.type == MapNotify)
1063 {
1064 // Iconize fix
1065 XtVaSetValues(frameShell, XmNiconic, (Boolean)False, NULL);
1066 if (!frame->GetVisibleStatus())
1067 {
1068 /* We really wanted this to be hidden! */
1069 XtUnmapWidget((Widget) frame->GetShellWidget());
1070 }
1071 }
1072 else if (e->xany.type == UnmapNotify)
1073 // Iconize fix
1074 XtVaSetValues(frameShell, XmNiconic, (Boolean)True, NULL);
1075 }
1076 }
1077
1078 //// Motif-specific
1079 bool wxFrame::PreResize()
1080 {
1081 #if wxUSE_TOOLBAR
1082 PositionToolBar();
1083 #endif // wxUSE_TOOLBAR
1084 PositionStatusBar();
1085 return TRUE;
1086 }
1087
1088 WXWidget wxFrame::GetClientWidget() const
1089 {
1090 return m_clientArea;
1091 }
1092
1093 void wxFrame::ChangeFont(bool keepOriginalSize)
1094 {
1095 // TODO
1096 }
1097
1098 void wxFrame::ChangeBackgroundColour()
1099 {
1100 if (GetClientWidget())
1101 DoChangeBackgroundColour(GetClientWidget(), m_backgroundColour);
1102 }
1103
1104 void wxFrame::ChangeForegroundColour()
1105 {
1106 if (GetClientWidget())
1107 DoChangeForegroundColour(GetClientWidget(), m_foregroundColour);
1108 }
1109
1110 void wxCloseFrameCallback(Widget widget, XtPointer client_data, XmAnyCallbackStruct *cbs)
1111 {
1112 wxFrame *frame = (wxFrame *)client_data;
1113
1114 wxCloseEvent closeEvent(wxEVT_CLOSE_WINDOW, frame->GetId());
1115 closeEvent.SetEventObject(frame);
1116
1117 // May delete the frame (with delayed deletion)
1118 frame->GetEventHandler()->ProcessEvent(closeEvent);
1119 }
1120