Motif files added.
[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 USE_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) wxTheApp->GetTopLevelWidget()))
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 // TODO 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 /* TODO
346 if (status_line_exists)
347 {
348 Dimension ys;
349 XtVaGetValues(statusLineWidget, XmNheight, &ys, NULL);
350 yy -= ys;
351 }
352 */
353
354 if (GetMenuBar() != (wxMenuBar*) NULL)
355 {
356 // it seems that if a frame holds a panel, the menu bar size
357 // gets automatically taken care of --- grano@cs.helsinki.fi 4.4.95
358 bool hasSubPanel = FALSE;
359 for(wxNode* node = GetChildren()->First(); node; node = node->Next())
360 {
361 wxWindow *win = (wxWindow *)node->Data();
362 hasSubPanel = (win->IsKindOf(CLASSINFO(wxPanel)) && !win->IsKindOf(CLASSINFO(wxDialog)));
363
364 if (hasSubPanel)
365 break;
366 }
367 if (! hasSubPanel) {
368 Dimension ys;
369 XtVaGetValues((Widget) GetMenuBarWidget(), XmNheight, &ys, NULL);
370 yy -= ys;
371 }
372 }
373
374 *x = xx; *y = yy;
375 }
376
377 // Set the client size (i.e. leave the calculation of borders etc.
378 // to wxWindows)
379 void wxFrame::SetClientSize(int width, int height)
380 {
381 // Calculate how large the new main window should be
382 // by finding the difference between the client area and the
383 // main window area, and adding on to the new client area
384 if (width > -1)
385 XtVaSetValues((Widget) m_workArea, XmNwidth, width, NULL);
386
387 if (height > -1)
388 {
389 /* TODO
390 if (status_line_exists)
391 {
392 Dimension ys;
393 XtVaGetValues(statusLineWidget, XmNheight, &ys, NULL);
394 height += ys;
395 }
396 */
397 XtVaSetValues((Widget) m_workArea, XmNheight, height, NULL);
398 }
399 PreResize();
400
401 wxSizeEvent sizeEvent(wxSize(width, height), GetId());
402 sizeEvent.SetEventObject(this);
403
404 GetEventHandler()->ProcessEvent(sizeEvent);
405
406 }
407
408 void wxFrame::GetSize(int *width, int *height) const
409 {
410 Dimension xx, yy;
411 XtVaGetValues((Widget) m_frameShell, XmNwidth, &xx, XmNheight, &yy, NULL);
412 *width = xx; *height = yy;
413 }
414
415 void wxFrame::GetPosition(int *x, int *y) const
416 {
417 Window parent_window = XtWindow((Widget) m_frameShell),
418 next_parent = XtWindow((Widget) m_frameShell),
419 root = RootWindowOfScreen(XtScreen((Widget) m_frameShell));
420
421 // search for the parent that is child of ROOT, because the WM may
422 // reparent twice and notify only the next parent (like FVWM)
423 while (next_parent != root) {
424 Window *theChildren; unsigned int n;
425 parent_window = next_parent;
426 XQueryTree(XtDisplay((Widget) m_frameShell), parent_window, &root,
427 &next_parent, &theChildren, &n);
428 XFree(theChildren); // not needed
429 }
430 int xx, yy; unsigned int dummy;
431 XGetGeometry(XtDisplay((Widget) m_frameShell), parent_window, &root,
432 &xx, &yy, &dummy, &dummy, &dummy, &dummy);
433 if (x) *x = xx;
434 if (y) *y = yy;
435 }
436
437 void wxFrame::SetSize(int x, int y, int width, int height, int sizeFlags)
438 {
439 if (x > -1)
440 XtVaSetValues((Widget) m_frameShell, XmNx, x, NULL);
441 if (y > -1)
442 XtVaSetValues((Widget) m_frameShell, XmNy, y, NULL);
443 if (width > -1)
444 XtVaSetValues((Widget) m_frameWidget, XmNwidth, width, NULL);
445 if (height > -1)
446 XtVaSetValues((Widget) m_frameWidget, XmNheight, height, NULL);
447
448 if (!(height == -1 && width == -1))
449 {
450 PreResize();
451 wxSizeEvent sizeEvent(wxSize(width, height), GetId());
452 sizeEvent.SetEventObject(this);
453
454 GetEventHandler()->ProcessEvent(sizeEvent);
455 }
456 }
457
458 bool wxFrame::Show(bool show)
459 {
460 m_visibleStatus = show; /* show-&-hide fix */
461
462 m_isShown = show;
463 if (show) {
464 XtMapWidget((Widget) m_frameShell);
465 XRaiseWindow(XtDisplay((Widget) m_frameShell), XtWindow((Widget) m_frameShell));
466 } else {
467 XtUnmapWidget((Widget) m_frameShell);
468 // XmUpdateDisplay(wxTheApp->topLevel); // Experimental: may be responsible for crashes
469 }
470 return TRUE;
471 }
472
473 void wxFrame::Iconize(bool iconize)
474 {
475 if (!iconize)
476 Show(TRUE);
477
478 XtVaSetValues((Widget) m_frameShell, XmNiconic, (Boolean)iconize, NULL);
479 }
480
481 // Equivalent to maximize/restore in Windows
482 void wxFrame::Maximize(bool maximize)
483 {
484 Show(TRUE);
485
486 if (maximize)
487 XtVaSetValues((Widget) m_frameShell, XmNiconic, FALSE, NULL);
488 }
489
490 bool wxFrame::IsIconized() const
491 {
492 Boolean iconic;
493 XtVaGetValues((Widget) m_frameShell, XmNiconic, &iconic, NULL);
494 return iconic;
495 }
496
497 void wxFrame::SetTitle(const wxString& title)
498 {
499 if (title == m_title)
500 return;
501
502 m_title = title;
503
504 if (!title.IsNull())
505 XtVaSetValues((Widget) m_frameShell,
506 XmNtitle, (const char*) title,
507 XmNiconName, (const char*) title,
508 NULL);
509 }
510
511 void wxFrame::SetIcon(const wxIcon& icon)
512 {
513 m_icon = icon;
514
515 // TODO
516 /*
517 if (!icon.Ok() || !icon.GetPixmap())
518 return;
519
520 XtVaSetValues((Widget) m_frameShell, XtNiconPixmap, icon->.GetPixmap(), NULL);
521 */
522 }
523
524 wxStatusBar *wxFrame::OnCreateStatusBar(int number, long style, wxWindowID id,
525 const wxString& name)
526 {
527 wxStatusBar *statusBar = NULL;
528
529 statusBar = new wxStatusBar(this, id, wxPoint(0, 0), wxSize(100, 20),
530 style, name);
531
532 // Set the height according to the font and the border size
533 wxClientDC dc(statusBar);
534 dc.SetFont(* statusBar->GetFont());
535
536 long x, y;
537 dc.GetTextExtent("X", &x, &y);
538
539 int height = (int)( (y * 1.1) + 2* statusBar->GetBorderY());
540
541 statusBar->SetSize(-1, -1, 100, height);
542
543 statusBar->SetFieldsCount(number);
544 return statusBar;
545 }
546
547 wxStatusBar* wxFrame::CreateStatusBar(int number, long style, wxWindowID id,
548 const wxString& name)
549 {
550 // Calling CreateStatusBar twice is an error.
551 wxCHECK_MSG( m_frameStatusBar == NULL, FALSE,
552 "recreating status bar in wxFrame" );
553
554 m_frameStatusBar = OnCreateStatusBar(number, style, id,
555 name);
556 if ( m_frameStatusBar )
557 {
558 PositionStatusBar();
559 return m_frameStatusBar;
560 }
561 else
562 return NULL;
563 }
564
565 void wxFrame::SetStatusText(const wxString& text, int number)
566 {
567 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set text for" );
568
569 m_frameStatusBar->SetStatusText(text, number);
570 }
571
572 void wxFrame::SetStatusWidths(int n, const int widths_field[])
573 {
574 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set widths for" );
575
576 m_frameStatusBar->SetStatusWidths(n, widths_field);
577 PositionStatusBar();
578 }
579
580 void wxFrame::PositionStatusBar()
581 {
582 int w, h;
583 GetClientSize(&w, &h);
584 int sw, sh;
585 m_frameStatusBar->GetSize(&sw, &sh);
586
587 // Since we wish the status bar to be directly under the client area,
588 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
589 m_frameStatusBar->SetSize(0, h, w, sh);
590 }
591
592 WXWidget wxFrame::GetMenuBarWidget() const
593 {
594 /* TODO
595 if (GetMenuBar())
596 return GetMenuBar()->GetMainWidget();
597 else
598 */
599 return (WXWidget) NULL;
600 }
601
602 void wxFrame::SetMenuBar(wxMenuBar *menuBar)
603 {
604 if (!menuBar)
605 {
606 m_frameMenuBar = NULL;
607 return;
608 }
609
610 // Currently can't set it twice
611 wxASSERT_MSG( (m_frameMenuBar == (wxMenuBar*) NULL), "Cannot set the menubar more than once");
612
613 m_frameMenuBar = menuBar;
614
615 // TODO
616 #if 0
617 Widget menuBarW = XmCreateMenuBar ((Widget) m_frameWidget, "MenuBar", NULL, 0);
618 m_frameMenuBar->SetMainWidget( (WXWidget) menuBarW);
619
620 int i;
621 for (i = 0; i < menuBar->GetMenuCount(); i++)
622 {
623 wxMenu *menu = menuBar->GetMenu(i);
624 menu->SetButtonWidget(menu->CreateMenu (menuBar, menuBarW, menu, menuBar->m_titles[i], TRUE);
625
626 /*
627 * COMMENT THIS OUT IF YOU DON'T LIKE A RIGHT-JUSTIFIED HELP MENU
628 */
629 wxStripMenuCodes (menuBar->m_titles[i], wxBuffer);
630
631 if (strcmp (wxBuffer, "Help") == 0)
632 XtVaSetValues ((Widget) menuBarW, XmNmenuHelpWidget, (Widget) menu->GetButtonWidget(), NULL);
633 }
634
635 XtRealizeWidget ((Widget) menuBarW);
636 XtManageChild ((Widget) menuBarW);
637 menuBar->SetMenuBarFrame(this);
638 #endif
639 }
640
641 void wxFrame::Fit()
642 {
643 // Work out max. size
644 wxNode *node = GetChildren()->First();
645 int max_width = 0;
646 int max_height = 0;
647 while (node)
648 {
649 // Find a child that's a subwindow, but not a dialog box.
650 wxWindow *win = (wxWindow *)node->Data();
651
652 if (!win->IsKindOf(CLASSINFO(wxFrame)) &&
653 !win->IsKindOf(CLASSINFO(wxDialog)))
654 {
655 int width, height;
656 int x, y;
657 win->GetSize(&width, &height);
658 win->GetPosition(&x, &y);
659
660 if ((x + width) > max_width)
661 max_width = x + width;
662 if ((y + height) > max_height)
663 max_height = y + height;
664 }
665 node = node->Next();
666 }
667 SetClientSize(max_width, max_height);
668 }
669
670 // Responds to colour changes, and passes event on to children.
671 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
672 {
673 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE));
674 Refresh();
675
676 if ( m_frameStatusBar )
677 {
678 wxSysColourChangedEvent event2;
679 event2.SetEventObject( m_frameStatusBar );
680 m_frameStatusBar->ProcessEvent(event2);
681 }
682
683 // Propagate the event to the non-top-level children
684 wxWindow::OnSysColourChanged(event);
685 }
686
687 // Default resizing behaviour - if only ONE subwindow,
688 // resize to client rectangle size
689 void wxFrame::OnSize(wxSizeEvent& event)
690 {
691 // if we're using constraints - do use them
692 #if USE_CONSTRAINTS
693 if ( GetAutoLayout() ) {
694 Layout();
695 return;
696 }
697 #endif
698
699 // do we have _exactly_ one child?
700 wxWindow *child = NULL;
701 for ( wxNode *node = GetChildren()->First(); node; node = node->Next() )
702 {
703 wxWindow *win = (wxWindow *)node->Data();
704 if ( !win->IsKindOf(CLASSINFO(wxFrame)) &&
705 !win->IsKindOf(CLASSINFO(wxDialog)) &&
706 (win != GetStatusBar()) &&
707 (win != GetToolBar()) )
708 {
709 if ( child )
710 return; // it's our second subwindow - nothing to do
711 child = win;
712 }
713 }
714
715 if ( child ) {
716 // we have exactly one child - set it's size to fill the whole frame
717 int clientW, clientH;
718 GetClientSize(&clientW, &clientH);
719
720 int x = 0;
721 int y = 0;
722
723 child->SetSize(x, y, clientW, clientH);
724 }
725 }
726
727 // Default activation behaviour - set the focus for the first child
728 // subwindow found.
729 void wxFrame::OnActivate(wxActivateEvent& event)
730 {
731 for(wxNode *node = GetChildren()->First(); node; node = node->Next())
732 {
733 // Find a child that's a subwindow, but not a dialog box.
734 wxWindow *child = (wxWindow *)node->Data();
735 if (!child->IsKindOf(CLASSINFO(wxFrame)) &&
736 !child->IsKindOf(CLASSINFO(wxDialog)))
737 {
738 #if WXDEBUG > 1
739 wxDebugMsg("wxFrame::OnActivate: about to set the child's focus.\n");
740 #endif
741 child->SetFocus();
742 return;
743 }
744 }
745 }
746
747 // The default implementation for the close window event - calls
748 // OnClose for backward compatibility.
749
750 void wxFrame::OnCloseWindow(wxCloseEvent& event)
751 {
752 // Compatibility
753 if ( GetEventHandler()->OnClose() || event.GetForce())
754 {
755 this->Destroy();
756 }
757 }
758
759 bool wxFrame::OnClose()
760 {
761 return TRUE;
762 }
763
764 // Destroy the window (delayed, if a managed window)
765 bool wxFrame::Destroy()
766 {
767 if (!wxPendingDelete.Member(this))
768 wxPendingDelete.Append(this);
769 return TRUE;
770 }
771
772 // Default menu selection behaviour - display a help string
773 void wxFrame::OnMenuHighlight(wxMenuEvent& event)
774 {
775 if (GetStatusBar())
776 {
777 if (event.GetMenuId() == -1)
778 SetStatusText("");
779 else
780 {
781 wxMenuBar *menuBar = GetMenuBar();
782 if (menuBar)
783 {
784 wxString helpString(menuBar->GetHelpString(event.GetMenuId()));
785 if (helpString != "")
786 SetStatusText(helpString);
787 }
788 }
789 }
790 }
791
792 wxMenuBar *wxFrame::GetMenuBar() const
793 {
794 return m_frameMenuBar;
795 }
796
797 void wxFrame::Centre(int direction)
798 {
799 int display_width, display_height, width, height, x, y;
800 wxDisplaySize(&display_width, &display_height);
801
802 GetSize(&width, &height);
803 GetPosition(&x, &y);
804
805 if (direction & wxHORIZONTAL)
806 x = (int)((display_width - width)/2);
807 if (direction & wxVERTICAL)
808 y = (int)((display_height - height)/2);
809
810 SetSize(x, y, width, height);
811 }
812
813 // Call this to simulate a menu command
814 void wxFrame::Command(int id)
815 {
816 ProcessCommand(id);
817 }
818
819 void wxFrame::ProcessCommand(int id)
820 {
821 wxCommandEvent commandEvent(wxEVENT_TYPE_MENU_COMMAND, id);
822 commandEvent.SetInt( id );
823 commandEvent.SetEventObject( this );
824
825 wxMenuBar *bar = GetMenuBar() ;
826 if (!bar)
827 return;
828
829 /* TODO: check the menu item if required
830 wxMenuItem *item = bar->FindItemForId(id) ;
831 if (item && item->IsCheckable())
832 {
833 bar->Check(id,!bar->Checked(id)) ;
834 }
835 */
836
837 GetEventHandler()->ProcessEvent(commandEvent);
838 }
839
840 // Checks if there is a toolbar, and returns the first free client position
841 wxPoint wxFrame::GetClientAreaOrigin() const
842 {
843 wxPoint pt(0, 0);
844 if (GetToolBar())
845 {
846 int w, h;
847 GetToolBar()->GetSize(& w, & h);
848
849 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
850 {
851 pt.x += w;
852 }
853 else
854 {
855 pt.y += h;
856 }
857 }
858 return pt;
859 }
860
861 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
862 {
863 wxCHECK_MSG( m_frameToolBar == NULL, FALSE,
864 "recreating toolbar in wxFrame" );
865
866 wxToolBar* toolBar = OnCreateToolBar(style, id, name);
867 if (toolBar)
868 {
869 SetToolBar(toolBar);
870 PositionToolBar();
871 return toolBar;
872 }
873 else
874 {
875 return NULL;
876 }
877 }
878
879 wxToolBar* wxFrame::OnCreateToolBar(long style, wxWindowID id, const wxString& name)
880 {
881 return new wxToolBar(this, id, wxDefaultPosition, wxDefaultSize, style, name);
882 }
883
884 void wxFrame::PositionToolBar()
885 {
886 int cw, ch;
887
888 // TODO: we actually need to use the low-level client size, before
889 // the toolbar/status bar were added.
890 // So DEFINITELY replace the line below with something appropriate.
891
892 GetClientSize(& cw, &ch);
893
894 if ( GetStatusBar() )
895 {
896 int statusX, statusY;
897 GetStatusBar()->GetClientSize(&statusX, &statusY);
898 ch -= statusY;
899 }
900
901 if (GetToolBar())
902 {
903 int tw, th;
904 GetToolBar()->GetSize(& tw, & th);
905
906 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
907 {
908 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
909 // means, pretend we don't have toolbar/status bar, so we
910 // have the original client size.
911 GetToolBar()->SetSize(0, 0, tw, ch, wxSIZE_NO_ADJUSTMENTS);
912 }
913 else
914 {
915 // Use the 'real' position
916 GetToolBar()->SetSize(0, 0, cw, th, wxSIZE_NO_ADJUSTMENTS);
917 }
918 }
919 }
920
921 void wxFrame::CaptureMouse()
922 {
923 if (m_winCaptured)
924 return;
925
926 if (GetMainWidget())
927 XtAddGrab((Widget) m_frameShell, TRUE, FALSE);
928 m_winCaptured = TRUE;
929 }
930
931 void wxFrame::ReleaseMouse()
932 {
933 if (!m_winCaptured)
934 return;
935
936 if (GetMainWidget())
937 XtRemoveGrab((Widget) m_frameShell);
938 m_winCaptured = FALSE;
939 }
940
941 void wxFrame::Raise(void)
942 {
943 Window parent_window = XtWindow((Widget) m_frameShell),
944 next_parent = XtWindow((Widget) m_frameShell),
945 root = RootWindowOfScreen(XtScreen((Widget) m_frameShell));
946 // search for the parent that is child of ROOT, because the WM may
947 // reparent twice and notify only the next parent (like FVWM)
948 while (next_parent != root) {
949 Window *theChildren; unsigned int n;
950 parent_window = next_parent;
951 XQueryTree(XtDisplay((Widget) m_frameShell), parent_window, &root,
952 &next_parent, &theChildren, &n);
953 XFree(theChildren); // not needed
954 }
955 XRaiseWindow(XtDisplay((Widget) m_frameShell), parent_window);
956 }
957
958 void wxFrame::Lower(void)
959 {
960 Window parent_window = XtWindow((Widget) m_frameShell),
961 next_parent = XtWindow((Widget) m_frameShell),
962 root = RootWindowOfScreen(XtScreen((Widget) m_frameShell));
963 // search for the parent that is child of ROOT, because the WM may
964 // reparent twice and notify only the next parent (like FVWM)
965 while (next_parent != root) {
966 Window *theChildren; unsigned int n;
967 parent_window = next_parent;
968 XQueryTree(XtDisplay((Widget) m_frameShell), parent_window, &root,
969 &next_parent, &theChildren, &n);
970 XFree(theChildren); // not needed
971 }
972 XLowerWindow(XtDisplay((Widget) m_frameShell), parent_window);
973 }
974
975 void wxFrame::SetToolBar(wxToolBar *toolbar)
976 { m_frameToolBar = toolbar; }
977
978 wxToolBar *wxFrame::GetToolBar() const
979 { return m_frameToolBar; }
980
981 static void wxFrameFocusProc(Widget workArea, XtPointer clientData,
982 XmAnyCallbackStruct *cbs)
983 {
984 wxFrame *frame = (wxFrame *)clientData;
985
986 // wxDebugMsg("focus proc from frame %ld\n",(long)frame);
987 // TODO
988 // frame->GetEventHandler()->OnSetFocus();
989 }
990
991 /* MATTEW: Used to insure that hide-&-show within an event cycle works */
992 static void wxFrameMapProc(Widget frameShell, XtPointer clientData,
993 XCrossingEvent * event)
994 {
995 wxFrame *frame = (wxFrame *)wxWidgetHashTable->Get((long)clientData);
996
997 if (frame) {
998 XEvent *e = (XEvent *)event;
999
1000 if (e->xany.type == MapNotify)
1001 {
1002 // Iconize fix
1003 XtVaSetValues(frameShell, XmNiconic, (Boolean)False, NULL);
1004 if (!frame->GetVisibleStatus())
1005 {
1006 /* We really wanted this to be hidden! */
1007 XtUnmapWidget((Widget) frame->GetShellWidget());
1008 }
1009 }
1010 else if (e->xany.type == UnmapNotify)
1011 // Iconize fix
1012 XtVaSetValues(frameShell, XmNiconic, (Boolean)True, NULL);
1013 }
1014 }
1015
1016 //// Motif-specific
1017 bool wxFrame::PreResize()
1018 {
1019 /* TODO
1020 // Set status line, if any
1021 if (status_line_exists)
1022 {
1023 Dimension clientW, clientH;
1024 XtVaGetValues(clientArea, XmNwidth, &clientW, XmNheight, &clientH, NULL);
1025 Dimension xx, yy;
1026 XtVaGetValues(statusLineWidget, XmNwidth, &xx, XmNheight, &yy, NULL);
1027
1028 XtUnmanageChild(statusLineWidget);
1029 XtVaSetValues(statusLineWidget, XmNx, 0, XmNy, clientH - yy, XmNwidth, clientW, NULL);
1030
1031 if (statusLineForm)
1032 XtVaSetValues(statusLineForm, XmNwidth, clientW, NULL);
1033
1034 XtManageChild(statusLineWidget);
1035 }
1036
1037 int width, height;
1038 GetSize(&width, &height);
1039
1040 if (width == lastWidth && height == lastHeight)
1041 return FALSE;
1042 else
1043 {
1044 return TRUE;
1045 }
1046 */
1047 return TRUE;
1048 }
1049
1050 void wxCloseFrameCallback(Widget widget, XtPointer client_data, XmAnyCallbackStruct *cbs)
1051 {
1052 wxFrame *frame = (wxFrame *)client_data;
1053
1054 wxCloseEvent closeEvent(wxEVT_CLOSE_WINDOW, frame->GetId());
1055 closeEvent.SetEventObject(frame);
1056
1057 // May delete the frame (with delayed deletion)
1058 frame->GetEventHandler()->ProcessEvent(closeEvent);
1059 }