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