]> git.saurik.com Git - wxWidgets.git/blame - src/motif/frame.cpp
Solved wxMotif scrolling display problem; added wxImageModule;
[wxWidgets.git] / src / motif / frame.cpp
CommitLineData
4bb6408c
JS
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);
8704bf74 54void wxFrameFocusProc(Widget workArea, XtPointer clientData,
4bb6408c
JS
55 XmAnyCallbackStruct *cbs);
56static void wxFrameMapProc(Widget frameShell, XtPointer clientData,
2d120f83 57 XCrossingEvent * event);
4bb6408c
JS
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)
2d120f83
JS
68EVT_SIZE(wxFrame::OnSize)
69EVT_ACTIVATE(wxFrame::OnActivate)
70EVT_MENU_HIGHLIGHT_ALL(wxFrame::OnMenuHighlight)
71EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged)
72EVT_IDLE(wxFrame::OnIdle)
73EVT_CLOSE(wxFrame::OnCloseWindow)
4bb6408c
JS
74END_EVENT_TABLE()
75
76IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
77#endif
78
47d67540 79#if wxUSE_NATIVE_STATUSBAR
4bb6408c
JS
80bool wxFrame::m_useNativeStatusBar = TRUE;
81#else
82bool wxFrame::m_useNativeStatusBar = FALSE;
83#endif
84
85wxFrame::wxFrame()
86{
1ccbb61a 87#if wxUSE_TOOLBAR
4bb6408c 88 m_frameToolBar = NULL ;
1ccbb61a
VZ
89#endif // wxUSE_TOOLBAR
90
4bb6408c
JS
91 m_frameMenuBar = NULL;
92 m_frameStatusBar = NULL;
2d120f83 93
4bb6408c
JS
94 m_windowParent = NULL;
95 m_iconized = FALSE;
2d120f83 96
4bb6408c
JS
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,
2d120f83
JS
107 wxWindowID id,
108 const wxString& title,
109 const wxPoint& pos,
110 const wxSize& size,
111 long style,
112 const wxString& name)
4bb6408c 113{
2d120f83
JS
114 if (!parent)
115 wxTopLevelWindows.Append(this);
116
117 SetName(name);
118
119 m_windowStyle = style;
120 m_frameMenuBar = NULL;
1ccbb61a 121#if wxUSE_TOOLBAR
2d120f83 122 m_frameToolBar = NULL ;
1ccbb61a 123#endif // wxUSE_TOOLBAR
2d120f83
JS
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
4bb6408c 159 XtVaSetValues((Widget) m_frameShell,
2d120f83
JS
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);
4bb6408c 266#if (XmREVISION > 1 || XmVERSION > 1)
2d120f83 267 XmAddWMProtocolCallback((Widget) m_frameShell, WM_DELETE_WINDOW, (XtCallbackProc) wxCloseFrameCallback, (XtPointer)this);
4bb6408c
JS
268#else
269#if XmREVISION == 1
2d120f83 270 XmAddWMProtocolCallback((Widget) m_frameShell, WM_DELETE_WINDOW, (XtCallbackProc) wxCloseFrameCallback, (caddr_t)this);
4bb6408c 271#else
2d120f83 272 XmAddWMProtocolCallback((Widget) m_frameShell, WM_DELETE_WINDOW, (void (*)())wxCloseFrameCallback, (caddr_t)this);
4bb6408c
JS
273#endif
274#endif
2d120f83
JS
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;
4bb6408c
JS
286}
287
288wxFrame::~wxFrame()
289{
2d120f83
JS
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.
4bb6408c 298#if MOTIF_MENUBAR_DELETE_FIX
2d120f83 299 GetMenuBar()->SetMainWidget((WXWidget) NULL);
4bb6408c 300#endif
2d120f83
JS
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++)
4bb6408c 316 if (statusTextWidget[i])
2d120f83
JS
317 XtDestroyWidget (statusTextWidget[i]);
318
319 if (statusLineForm)
320 XtDestroyWidget (statusLineForm);
321
322 if (statusLineWidget)
323 XtDestroyWidget (statusLineWidget);
324 */
325
326 if (m_workArea)
4bb6408c 327 {
2d120f83
JS
328 wxDeleteWindowFromTable((Widget) m_workArea);
329
330 XtDestroyWidget ((Widget) m_workArea);
4bb6408c 331 }
2d120f83
JS
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
4bb6408c
JS
357}
358
359// Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
360void wxFrame::GetClientSize(int *x, int *y) const
361{
2d120f83
JS
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 }
1ccbb61a 371#if wxUSE_TOOLBAR
2d120f83
JS
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 }
1ccbb61a 381#endif // wxUSE_TOOLBAR
2d120f83
JS
382 /*
383 if (GetMenuBar() != (wxMenuBar*) NULL)
384 {
4bb6408c
JS
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;
c0ed460c 388 for(wxNode* node = GetChildren().First(); node; node = node->Next())
4bb6408c 389 {
2d120f83
JS
390 wxWindow *win = (wxWindow *)node->Data();
391 hasSubPanel = (win->IsKindOf(CLASSINFO(wxPanel)) && !win->IsKindOf(CLASSINFO(wxDialog)));
392
4bb6408c 393 if (hasSubPanel)
2d120f83
JS
394 break;
395 }
396 if (! hasSubPanel) {
4bb6408c
JS
397 Dimension ys;
398 XtVaGetValues((Widget) GetMenuBarWidget(), XmNheight, &ys, NULL);
399 yy -= ys;
2d120f83
JS
400 }
401 }
402 */
403
404 *x = xx; *y = yy;
4bb6408c
JS
405}
406
407// Set the client size (i.e. leave the calculation of borders etc.
408// to wxWindows)
409void wxFrame::SetClientSize(int width, int height)
410{
2d120f83
JS
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)
a4294b78 418 {
2d120f83
JS
419 if (m_frameStatusBar)
420 {
421 int sbw, sbh;
422 m_frameStatusBar->GetSize(& sbw, & sbh);
423 height += sbh;
424 }
1ccbb61a 425#if wxUSE_TOOLBAR
2d120f83
JS
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 }
1ccbb61a 435#endif // wxUSE_TOOLBAR
2d120f83
JS
436
437 XtVaSetValues((Widget) m_workArea, XmNheight, height, NULL);
a4294b78 438 }
2d120f83
JS
439 PreResize();
440
441 wxSizeEvent sizeEvent(wxSize(width, height), GetId());
442 sizeEvent.SetEventObject(this);
443
444 GetEventHandler()->ProcessEvent(sizeEvent);
445
4bb6408c
JS
446}
447
448void wxFrame::GetSize(int *width, int *height) const
449{
2d120f83
JS
450 Dimension xx, yy;
451 XtVaGetValues((Widget) m_frameShell, XmNwidth, &xx, XmNheight, &yy, NULL);
452 *width = xx; *height = yy;
4bb6408c
JS
453}
454
455void wxFrame::GetPosition(int *x, int *y) const
456{
457 Window parent_window = XtWindow((Widget) m_frameShell),
2d120f83
JS
458 next_parent = XtWindow((Widget) m_frameShell),
459 root = RootWindowOfScreen(XtScreen((Widget) m_frameShell));
460
4bb6408c
JS
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) {
2d120f83
JS
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
4bb6408c
JS
469 }
470 int xx, yy; unsigned int dummy;
471 XGetGeometry(XtDisplay((Widget) m_frameShell), parent_window, &root,
2d120f83 472 &xx, &yy, &dummy, &dummy, &dummy, &dummy);
4bb6408c
JS
473 if (x) *x = xx;
474 if (y) *y = yy;
475}
476
477void wxFrame::SetSize(int x, int y, int width, int height, int sizeFlags)
478{
2d120f83
JS
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 }
4bb6408c
JS
497}
498
499bool wxFrame::Show(bool show)
500{
2d120f83
JS
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;
4bb6408c
JS
515}
516
517void wxFrame::Iconize(bool iconize)
518{
2d120f83
JS
519 if (!iconize)
520 Show(TRUE);
521
522 if (m_frameShell)
523 XtVaSetValues((Widget) m_frameShell, XmNiconic, (Boolean)iconize, NULL);
4bb6408c
JS
524}
525
526// Equivalent to maximize/restore in Windows
527void wxFrame::Maximize(bool maximize)
528{
2d120f83
JS
529 Show(TRUE);
530
531 if (maximize && m_frameShell)
532 XtVaSetValues((Widget) m_frameShell, XmNiconic, FALSE, NULL);
4bb6408c
JS
533}
534
535bool wxFrame::IsIconized() const
536{
2d120f83
JS
537 if (!m_frameShell)
538 return FALSE;
539
540 Boolean iconic;
541 XtVaGetValues((Widget) m_frameShell, XmNiconic, &iconic, NULL);
542 return iconic;
4bb6408c
JS
543}
544
6f63ec3f
JS
545// Is it maximized?
546bool wxFrame::IsMaximized(void) const
547{
548 // No maximizing in Motif (?)
549 return FALSE;
550}
551
4bb6408c
JS
552void wxFrame::SetTitle(const wxString& title)
553{
2d120f83
JS
554 if (title == m_title)
555 return;
4bb6408c 556
2d120f83
JS
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);
4bb6408c
JS
564}
565
566void wxFrame::SetIcon(const wxIcon& icon)
567{
2d120f83
JS
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);
4bb6408c
JS
577}
578
579wxStatusBar *wxFrame::OnCreateStatusBar(int number, long style, wxWindowID id,
2d120f83 580 const wxString& name)
4bb6408c
JS
581{
582 wxStatusBar *statusBar = NULL;
2d120f83 583
4bb6408c
JS
584 statusBar = new wxStatusBar(this, id, wxPoint(0, 0), wxSize(100, 20),
585 style, name);
2d120f83 586
4bb6408c
JS
587 // Set the height according to the font and the border size
588 wxClientDC dc(statusBar);
c0ed460c 589 dc.SetFont(statusBar->GetFont());
2d120f83 590
4bb6408c
JS
591 long x, y;
592 dc.GetTextExtent("X", &x, &y);
2d120f83 593
4bb6408c 594 int height = (int)( (y * 1.1) + 2* statusBar->GetBorderY());
2d120f83 595
4bb6408c 596 statusBar->SetSize(-1, -1, 100, height);
2d120f83 597
4bb6408c
JS
598 statusBar->SetFieldsCount(number);
599 return statusBar;
600}
601
602wxStatusBar* wxFrame::CreateStatusBar(int number, long style, wxWindowID id,
2d120f83 603 const wxString& name)
4bb6408c 604{
2d120f83
JS
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;
4bb6408c
JS
618}
619
620void wxFrame::SetStatusText(const wxString& text, int number)
621{
2d120f83
JS
622 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set text for" );
623
624 m_frameStatusBar->SetStatusText(text, number);
4bb6408c
JS
625}
626
627void wxFrame::SetStatusWidths(int n, const int widths_field[])
628{
2d120f83
JS
629 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set widths for" );
630
631 m_frameStatusBar->SetStatusWidths(n, widths_field);
632 PositionStatusBar();
4bb6408c
JS
633}
634
635void wxFrame::PositionStatusBar()
636{
50414e24 637 if (!m_frameStatusBar)
2d120f83
JS
638 return;
639
4bb6408c
JS
640 int w, h;
641 GetClientSize(&w, &h);
642 int sw, sh;
643 m_frameStatusBar->GetSize(&sw, &sh);
2d120f83 644
4bb6408c
JS
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.
89c7e962 647 m_frameStatusBar->SetSize(0, h, w, sh);
4bb6408c
JS
648}
649
650WXWidget wxFrame::GetMenuBarWidget() const
651{
2d120f83
JS
652 if (GetMenuBar())
653 return GetMenuBar()->GetMainWidget();
654 else
655 return (WXWidget) NULL;
4bb6408c
JS
656}
657
658void wxFrame::SetMenuBar(wxMenuBar *menuBar)
659{
660 if (!menuBar)
661 {
662 m_frameMenuBar = NULL;
663 return;
664 }
2d120f83 665
4bb6408c 666 // Currently can't set it twice
2d120f83
JS
667 // wxASSERT_MSG( (m_frameMenuBar == (wxMenuBar*) NULL), "Cannot set the menubar more than once");
668
621793f4 669 if (m_frameMenuBar)
4bb6408c 670 {
621793f4
JS
671 m_frameMenuBar->DestroyMenuBar();
672 delete m_frameMenuBar;
4bb6408c 673 }
2d120f83 674
621793f4
JS
675 m_frameMenuBar = menuBar;
676 m_frameMenuBar->CreateMenuBar(this);
4bb6408c
JS
677}
678
679void wxFrame::Fit()
680{
2d120f83
JS
681 // Work out max. size
682 wxNode *node = GetChildren().First();
683 int max_width = 0;
684 int max_height = 0;
685 while (node)
4bb6408c 686 {
2d120f83
JS
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();
4bb6408c 704 }
2d120f83 705 SetClientSize(max_width, max_height);
4bb6408c
JS
706}
707
708// Responds to colour changes, and passes event on to children.
709void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
710{
711 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE));
712 Refresh();
2d120f83 713
4bb6408c
JS
714 if ( m_frameStatusBar )
715 {
716 wxSysColourChangedEvent event2;
717 event2.SetEventObject( m_frameStatusBar );
718 m_frameStatusBar->ProcessEvent(event2);
719 }
2d120f83 720
4bb6408c
JS
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
727void wxFrame::OnSize(wxSizeEvent& event)
728{
2d120f83
JS
729 // if we're using constraints - do use them
730#if wxUSE_CONSTRAINTS
4bb6408c 731 if ( GetAutoLayout() ) {
2d120f83
JS
732 Layout();
733 return;
4bb6408c 734 }
2d120f83
JS
735#endif
736
737 // do we have _exactly_ one child?
738 wxWindow *child = NULL;
739 for ( wxNode *node = GetChildren().First(); node; node = node->Next() )
4bb6408c 740 {
2d120f83
JS
741 wxWindow *win = (wxWindow *)node->Data();
742 if ( !win->IsKindOf(CLASSINFO(wxFrame)) &&
743 !win->IsKindOf(CLASSINFO(wxDialog)) &&
1ccbb61a
VZ
744 (win != GetStatusBar())
745#if wxUSE_TOOLBAR
746 && (win != GetToolBar())
747#endif // wxUSE_TOOLBAR
748 )
2d120f83
JS
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);
4bb6408c 765 }
4bb6408c
JS
766}
767
768// Default activation behaviour - set the focus for the first child
769// subwindow found.
770void wxFrame::OnActivate(wxActivateEvent& event)
771{
2d120f83 772 for(wxNode *node = GetChildren().First(); node; node = node->Next())
4bb6408c 773 {
2d120f83
JS
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 }
4bb6408c 782 }
4bb6408c
JS
783}
784
e3065973 785// The default implementation for the close window event.
4bb6408c
JS
786// OnClose for backward compatibility.
787
788void wxFrame::OnCloseWindow(wxCloseEvent& event)
789{
e3065973 790 this->Destroy();
4bb6408c
JS
791}
792
793// Destroy the window (delayed, if a managed window)
794bool wxFrame::Destroy()
795{
2d120f83
JS
796 if (!wxPendingDelete.Member(this))
797 wxPendingDelete.Append(this);
798 return TRUE;
4bb6408c
JS
799}
800
801// Default menu selection behaviour - display a help string
802void wxFrame::OnMenuHighlight(wxMenuEvent& event)
803{
2d120f83 804 if (GetStatusBar())
4bb6408c 805 {
2d120f83
JS
806 if (event.GetMenuId() == -1)
807 SetStatusText("");
808 else
809 {
810 wxMenuBar *menuBar = GetMenuBar();
811 if (menuBar)
812 {
813 wxString helpString(menuBar->GetHelpString(event.GetMenuId()));
814 if (helpString != "")
815 SetStatusText(helpString);
816 }
817 }
4bb6408c 818 }
4bb6408c
JS
819}
820
821wxMenuBar *wxFrame::GetMenuBar() const
822{
2d120f83 823 return m_frameMenuBar;
4bb6408c
JS
824}
825
826void wxFrame::Centre(int direction)
827{
2d120f83
JS
828 int display_width, display_height, width, height, x, y;
829 wxDisplaySize(&display_width, &display_height);
830
831 GetSize(&width, &height);
832 GetPosition(&x, &y);
833
834 if (direction & wxHORIZONTAL)
835 x = (int)((display_width - width)/2);
836 if (direction & wxVERTICAL)
837 y = (int)((display_height - height)/2);
838
839 SetSize(x, y, width, height);
4bb6408c
JS
840}
841
842// Call this to simulate a menu command
843void wxFrame::Command(int id)
844{
2d120f83 845 ProcessCommand(id);
4bb6408c
JS
846}
847
848void wxFrame::ProcessCommand(int id)
849{
c75e6695
VZ
850 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
851 commandEvent.SetInt( id );
852 commandEvent.SetEventObject( this );
853
854 wxMenuBar *bar = GetMenuBar() ;
855 if (!bar)
856 return;
857
858/* TODO: check the menu item if required
859 wxMenuItem *item = bar->FindItemForId(id) ;
860 if (item && item->IsCheckable())
861 {
862 bar->Check(id,!bar->Checked(id)) ;
863 }
864*/
865
866 GetEventHandler()->ProcessEvent(commandEvent);
4bb6408c
JS
867}
868
869// Checks if there is a toolbar, and returns the first free client position
870wxPoint wxFrame::GetClientAreaOrigin() const
871{
872 wxPoint pt(0, 0);
1ccbb61a 873#if wxUSE_TOOLBAR
4bb6408c
JS
874 if (GetToolBar())
875 {
876 int w, h;
877 GetToolBar()->GetSize(& w, & h);
2d120f83 878
4bb6408c
JS
879 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
880 {
881 pt.x += w;
882 }
883 else
884 {
885 pt.y += h;
886 }
887 }
1ccbb61a
VZ
888#endif // wxUSE_TOOLBAR
889
4bb6408c
JS
890 return pt;
891}
892
87d1e11f
JS
893void wxFrame::ScreenToClient(int *x, int *y) const
894{
895 wxWindow::ScreenToClient(x, y);
2d120f83 896
87d1e11f
JS
897 // We may be faking the client origin.
898 // So a window that's really at (0, 30) may appear
899 // (to wxWin apps) to be at (0, 0).
900 wxPoint pt(GetClientAreaOrigin());
901 *x -= pt.x;
902 *y -= pt.y;
903}
904
905void wxFrame::ClientToScreen(int *x, int *y) const
906{
907 // We may be faking the client origin.
908 // So a window that's really at (0, 30) may appear
909 // (to wxWin apps) to be at (0, 0).
910 wxPoint pt1(GetClientAreaOrigin());
911 *x += pt1.x;
912 *y += pt1.y;
2d120f83 913
87d1e11f
JS
914 wxWindow::ClientToScreen(x, y);
915}
916
1ccbb61a 917#if wxUSE_TOOLBAR
4bb6408c
JS
918wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
919{
920 wxCHECK_MSG( m_frameToolBar == NULL, FALSE,
2d120f83
JS
921 "recreating toolbar in wxFrame" );
922
4bb6408c
JS
923 wxToolBar* toolBar = OnCreateToolBar(style, id, name);
924 if (toolBar)
925 {
926 SetToolBar(toolBar);
927 PositionToolBar();
928 return toolBar;
929 }
930 else
931 {
932 return NULL;
933 }
934}
935
936wxToolBar* wxFrame::OnCreateToolBar(long style, wxWindowID id, const wxString& name)
937{
a4294b78 938 return new wxToolBar(this, id, wxPoint(0, 0), wxSize(100, 24), style, name);
4bb6408c
JS
939}
940
1ccbb61a
VZ
941void wxFrame::SetToolBar(wxToolBar *toolbar)
942{
943 m_frameToolBar = toolbar;
944}
945
946wxToolBar *wxFrame::GetToolBar() const
947{
948 return m_frameToolBar;
949}
950
4bb6408c
JS
951void wxFrame::PositionToolBar()
952{
953 int cw, ch;
2d120f83 954
4bb6408c 955 GetClientSize(& cw, &ch);
2d120f83 956
4bb6408c
JS
957 if (GetToolBar())
958 {
959 int tw, th;
960 GetToolBar()->GetSize(& tw, & th);
2d120f83 961
4bb6408c
JS
962 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
963 {
964 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
965 // means, pretend we don't have toolbar/status bar, so we
966 // have the original client size.
a4294b78 967 GetToolBar()->SetSize(0, 0, tw, ch + th, wxSIZE_NO_ADJUSTMENTS);
4bb6408c
JS
968 }
969 else
970 {
971 // Use the 'real' position
972 GetToolBar()->SetSize(0, 0, cw, th, wxSIZE_NO_ADJUSTMENTS);
973 }
974 }
975}
1ccbb61a 976#endif // wxUSE_TOOLBAR
4bb6408c
JS
977
978void wxFrame::CaptureMouse()
979{
2d120f83
JS
980 if (m_winCaptured)
981 return;
4bb6408c 982
2d120f83
JS
983 if (GetMainWidget())
984 XtAddGrab((Widget) m_frameShell, TRUE, FALSE);
985 m_winCaptured = TRUE;
4bb6408c
JS
986}
987
988void wxFrame::ReleaseMouse()
989{
2d120f83
JS
990 if (!m_winCaptured)
991 return;
4bb6408c 992
2d120f83
JS
993 if (GetMainWidget())
994 XtRemoveGrab((Widget) m_frameShell);
995 m_winCaptured = FALSE;
4bb6408c
JS
996}
997
998void wxFrame::Raise(void)
999{
1000 Window parent_window = XtWindow((Widget) m_frameShell),
2d120f83
JS
1001 next_parent = XtWindow((Widget) m_frameShell),
1002 root = RootWindowOfScreen(XtScreen((Widget) m_frameShell));
4bb6408c
JS
1003 // search for the parent that is child of ROOT, because the WM may
1004 // reparent twice and notify only the next parent (like FVWM)
1005 while (next_parent != root) {
2d120f83
JS
1006 Window *theChildren; unsigned int n;
1007 parent_window = next_parent;
1008 XQueryTree(XtDisplay((Widget) m_frameShell), parent_window, &root,
1009 &next_parent, &theChildren, &n);
1010 XFree(theChildren); // not needed
4bb6408c
JS
1011 }
1012 XRaiseWindow(XtDisplay((Widget) m_frameShell), parent_window);
1013}
1014
1015void wxFrame::Lower(void)
1016{
1017 Window parent_window = XtWindow((Widget) m_frameShell),
2d120f83
JS
1018 next_parent = XtWindow((Widget) m_frameShell),
1019 root = RootWindowOfScreen(XtScreen((Widget) m_frameShell));
4bb6408c
JS
1020 // search for the parent that is child of ROOT, because the WM may
1021 // reparent twice and notify only the next parent (like FVWM)
1022 while (next_parent != root) {
2d120f83
JS
1023 Window *theChildren; unsigned int n;
1024 parent_window = next_parent;
1025 XQueryTree(XtDisplay((Widget) m_frameShell), parent_window, &root,
1026 &next_parent, &theChildren, &n);
1027 XFree(theChildren); // not needed
4bb6408c
JS
1028 }
1029 XLowerWindow(XtDisplay((Widget) m_frameShell), parent_window);
1030}
1031
8704bf74 1032void wxFrameFocusProc(Widget workArea, XtPointer clientData,
4bb6408c
JS
1033 XmAnyCallbackStruct *cbs)
1034{
2d120f83
JS
1035 wxFrame *frame = (wxFrame *)clientData;
1036
1037 // wxDebugMsg("focus proc from frame %ld\n",(long)frame);
1038 // TODO
1039 // frame->GetEventHandler()->OnSetFocus();
4bb6408c
JS
1040}
1041
1042/* MATTEW: Used to insure that hide-&-show within an event cycle works */
1043static void wxFrameMapProc(Widget frameShell, XtPointer clientData,
2d120f83 1044 XCrossingEvent * event)
4bb6408c 1045{
2d120f83
JS
1046 wxFrame *frame = (wxFrame *)wxWidgetHashTable->Get((long)clientData);
1047
1048 if (frame) {
1049 XEvent *e = (XEvent *)event;
1050
1051 if (e->xany.type == MapNotify)
1052 {
1053 // Iconize fix
1054 XtVaSetValues(frameShell, XmNiconic, (Boolean)False, NULL);
1055 if (!frame->GetVisibleStatus())
1056 {
1057 /* We really wanted this to be hidden! */
1058 XtUnmapWidget((Widget) frame->GetShellWidget());
1059 }
1060 }
1061 else if (e->xany.type == UnmapNotify)
1062 // Iconize fix
1063 XtVaSetValues(frameShell, XmNiconic, (Boolean)True, NULL);
4bb6408c 1064 }
4bb6408c
JS
1065}
1066
1067//// Motif-specific
1068bool wxFrame::PreResize()
1069{
1ccbb61a 1070#if wxUSE_TOOLBAR
2d120f83 1071 PositionToolBar();
1ccbb61a 1072#endif // wxUSE_TOOLBAR
2d120f83
JS
1073 PositionStatusBar();
1074 return TRUE;
4bb6408c
JS
1075}
1076
50414e24
JS
1077WXWidget wxFrame::GetClientWidget() const
1078{
2d120f83 1079 return m_clientArea;
50414e24
JS
1080}
1081
4b5f3fe6 1082void wxFrame::ChangeFont(bool keepOriginalSize)
0d57be45
JS
1083{
1084 // TODO
1085}
1086
1087void wxFrame::ChangeBackgroundColour()
1088{
621793f4
JS
1089 if (GetClientWidget())
1090 DoChangeBackgroundColour(GetClientWidget(), m_backgroundColour);
0d57be45
JS
1091}
1092
1093void wxFrame::ChangeForegroundColour()
1094{
621793f4
JS
1095 if (GetClientWidget())
1096 DoChangeForegroundColour(GetClientWidget(), m_foregroundColour);
0d57be45
JS
1097}
1098
4bb6408c
JS
1099void wxCloseFrameCallback(Widget widget, XtPointer client_data, XmAnyCallbackStruct *cbs)
1100{
2d120f83
JS
1101 wxFrame *frame = (wxFrame *)client_data;
1102
1103 wxCloseEvent closeEvent(wxEVT_CLOSE_WINDOW, frame->GetId());
1104 closeEvent.SetEventObject(frame);
1105
1106 // May delete the frame (with delayed deletion)
1107 frame->GetEventHandler()->ProcessEvent(closeEvent);
4bb6408c 1108}
0d57be45 1109