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