1 /////////////////////////////////////////////////////////////////////////////
2 // Name: motif/frame.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "frame.h"
25 #include "wx/statusbr.h"
26 #include "wx/toolbar.h"
27 #include "wx/menuitem.h"
29 #include "wx/dcclient.h"
30 #include "wx/dialog.h"
31 #include "wx/settings.h"
36 #pragma message disable nosimpint
39 #if defined(__ultrix) || defined(__sgi)
44 #include <X11/Shell.h>
50 #include <Xm/MwmUtil.h>
51 #include <Xm/BulletinB.h>
54 #include <Xm/RowColumn.h>
56 #include <Xm/AtomMgr.h>
57 #include <Xm/LabelG.h>
60 #include <Xm/Protocols.h>
64 #pragma message enable nosimpint
67 #include "wx/motif/private.h"
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 static void wxCloseFrameCallback(Widget
, XtPointer
, XmAnyCallbackStruct
*cbs
);
74 static void wxFrameFocusProc(Widget workArea
, XtPointer clientData
,
75 XmAnyCallbackStruct
*cbs
);
76 static void wxFrameMapProc(Widget frameShell
, XtPointer clientData
,
77 XCrossingEvent
* event
);
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 extern wxList wxModelessWindows
;
84 extern wxList wxPendingDelete
;
86 // TODO: this should be tidied so that any frame can be the
88 static bool wxTopLevelUsed
= FALSE
;
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 BEGIN_EVENT_TABLE(wxFrame
, wxFrameBase
)
95 EVT_ACTIVATE(wxFrame::OnActivate
)
96 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
)
99 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxWindow
)
101 // ============================================================================
103 // ============================================================================
105 // ----------------------------------------------------------------------------
106 // frame construction
107 // ----------------------------------------------------------------------------
114 m_frameShell
= (WXWidget
) NULL
;
115 m_frameWidget
= (WXWidget
) NULL
;;
116 m_workArea
= (WXWidget
) NULL
;;
117 m_clientArea
= (WXWidget
) NULL
;;
118 m_visibleStatus
= TRUE
;
121 bool wxFrame::Create(wxWindow
*parent
,
123 const wxString
& title
,
127 const wxString
& name
)
132 wxTopLevelWindows
.Append(this);
134 wxModelessWindows
.Append(this);
138 m_windowStyle
= style
;
140 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
);
141 m_foregroundColour
= *wxBLACK
;
142 m_font
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
147 m_windowId
= (int)NewControlId();
149 int x
= pos
.x
, y
= pos
.y
;
150 int width
= size
.x
, height
= size
.y
;
152 // Set reasonable values for position and size if defaults have been
155 // MB TODO: something better than these arbitrary values ?
156 // VZ should use X resources for this...
162 int displayW
, displayH
;
163 wxDisplaySize( &displayW
, &displayH
);
167 x
= (displayW
- width
) / 2;
172 y
= (displayH
- height
) / 2;
176 // VZ: what does this do??
179 // Change suggested by Matthew Flatt
180 m_frameShell
= (WXWidget
)XtAppCreateShell
183 wxTheApp
->GetClassName(),
184 topLevelShellWidgetClass
,
185 (Display
*) wxGetDisplay(),
192 m_frameShell
= wxTheApp
->GetTopLevelWidget();
193 wxTopLevelUsed
= TRUE
;
196 XtVaSetValues((Widget
) m_frameShell
,
197 // Allows menu to resize
198 XmNallowShellResize
, True
,
199 XmNdeleteResponse
, XmDO_NOTHING
,
200 XmNmappedWhenManaged
, False
,
201 XmNiconic
, (style
& wxICONIZE
) ? TRUE
: FALSE
,
204 if (!title
.IsEmpty())
205 XtVaSetValues((Widget
) m_frameShell
,
206 XmNtitle
, title
.c_str(),
209 m_frameWidget
= (WXWidget
) XtVaCreateManagedWidget("main_window",
210 xmMainWindowWidgetClass
, (Widget
) m_frameShell
,
211 XmNresizePolicy
, XmRESIZE_NONE
,
214 m_workArea
= (WXWidget
) XtVaCreateWidget("form",
215 xmFormWidgetClass
, (Widget
) m_frameWidget
,
216 XmNresizePolicy
, XmRESIZE_NONE
,
219 m_clientArea
= (WXWidget
) XtVaCreateWidget("client",
220 xmBulletinBoardWidgetClass
, (Widget
) m_workArea
,
223 XmNrightAttachment
, XmATTACH_FORM
,
224 XmNleftAttachment
, XmATTACH_FORM
,
225 XmNtopAttachment
, XmATTACH_FORM
,
226 XmNbottomAttachment
, XmATTACH_FORM
,
227 // XmNresizePolicy, XmRESIZE_ANY,
230 wxLogTrace(wxTRACE_Messages
,
231 "Created frame (0x%08x) with work area 0x%08x and client "
232 "area 0x%08x", m_frameWidget
, m_workArea
, m_clientArea
);
234 XtAddEventHandler((Widget
) m_clientArea
, ExposureMask
,FALSE
,
235 wxUniversalRepaintProc
, (XtPointer
) this);
237 XtVaSetValues((Widget
) m_frameWidget
,
238 XmNworkWindow
, (Widget
) m_workArea
,
241 XtManageChild((Widget
) m_clientArea
);
242 XtManageChild((Widget
) m_workArea
);
244 wxAddWindowToTable((Widget
) m_workArea
, this);
248 XtOverrideTranslations((Widget
) m_workArea
,
249 ptr
= XtParseTranslationTable("<Configure>: resize()"));
253 XtAddCallback((Widget
) m_workArea
, XmNfocusCallback
,
254 (XtCallbackProc
)wxFrameFocusProc
, (XtPointer
)this);
256 /* Part of show-&-hide fix */
257 XtAddEventHandler((Widget
) m_frameShell
, StructureNotifyMask
,
258 False
, (XtEventHandler
)wxFrameMapProc
,
259 (XtPointer
)m_workArea
);
262 XtVaSetValues((Widget
) m_frameShell
, XmNx
, x
, NULL
);
264 XtVaSetValues((Widget
) m_frameShell
, XmNy
, y
, NULL
);
266 XtVaSetValues((Widget
) m_frameShell
, XmNwidth
, width
, NULL
);
268 XtVaSetValues((Widget
) m_frameShell
, XmNheight
, height
, NULL
);
270 m_mainWidget
= m_frameWidget
;
274 // This patch comes from Torsten Liermann lier@lier1.muc.de
275 if (XmIsMotifWMRunning( (Widget
) m_frameShell
))
278 if (style
& wxRESIZE_BORDER
)
279 decor
|= MWM_DECOR_RESIZEH
;
280 if (style
& wxSYSTEM_MENU
)
281 decor
|= MWM_DECOR_MENU
;
282 if ((style
& wxCAPTION
) ||
283 (style
& wxTINY_CAPTION_HORIZ
) ||
284 (style
& wxTINY_CAPTION_VERT
))
285 decor
|= MWM_DECOR_TITLE
;
286 if (style
& wxTHICK_FRAME
)
287 decor
|= MWM_DECOR_BORDER
;
288 if (style
& wxTHICK_FRAME
)
289 decor
|= MWM_DECOR_BORDER
;
290 if (style
& wxMINIMIZE_BOX
)
291 decor
|= MWM_DECOR_MINIMIZE
;
292 if (style
& wxMAXIMIZE_BOX
)
293 decor
|= MWM_DECOR_MAXIMIZE
;
294 XtVaSetValues((Widget
) m_frameShell
,XmNmwmDecorations
,decor
,NULL
);
296 // This allows non-Motif window managers to support at least the
297 // no-decorations case.
301 XtVaSetValues((Widget
) m_frameShell
,XmNoverrideRedirect
,TRUE
,NULL
);
303 XtRealizeWidget((Widget
) m_frameShell
);
305 // Intercept CLOSE messages from the window manager
306 Atom WM_DELETE_WINDOW
= XmInternAtom(XtDisplay((Widget
) m_frameShell
), "WM_DELETE_WINDOW", False
);
307 #if (XmREVISION > 1 || XmVERSION > 1)
308 XmAddWMProtocolCallback((Widget
) m_frameShell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseFrameCallback
, (XtPointer
)this);
311 XmAddWMProtocolCallback((Widget
) m_frameShell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseFrameCallback
, (caddr_t
)this);
313 XmAddWMProtocolCallback((Widget
) m_frameShell
, WM_DELETE_WINDOW
, (void (*)())wxCloseFrameCallback
, (caddr_t
)this);
317 ChangeBackgroundColour();
321 wxSizeEvent
sizeEvent(wxSize(width
, height
), GetId());
322 sizeEvent
.SetEventObject(this);
324 GetEventHandler()->ProcessEvent(sizeEvent
);
331 m_isBeingDeleted
= TRUE
;
334 XtRemoveEventHandler((Widget
) m_clientArea
, ExposureMask
, FALSE
,
335 wxUniversalRepaintProc
, (XtPointer
) this);
342 m_frameMenuBar
->DestroyMenuBar();
344 // Hack to stop core dump on Ultrix, OSF, for some strange reason.
345 #if MOTIF_MENUBAR_DELETE_FIX
346 GetMenuBar()->SetMainWidget((WXWidget
) NULL
);
348 delete m_frameMenuBar
;
349 m_frameMenuBar
= NULL
;
352 wxTopLevelWindows
.DeleteObject(this);
353 wxModelessWindows
.DeleteObject(this);
355 if (m_frameStatusBar
)
357 delete m_frameStatusBar
;
358 m_frameStatusBar
= NULL
;
365 wxDeleteWindowFromTable((Widget
) m_workArea
);
367 XtDestroyWidget ((Widget
) m_workArea
);
372 wxDeleteWindowFromTable((Widget
) m_frameWidget
);
373 XtDestroyWidget ((Widget
) m_frameWidget
);
377 XtDestroyWidget ((Widget
) m_frameShell
);
379 SetMainWidget((WXWidget
) NULL
);
381 /* Check if it's the last top-level window */
383 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
385 wxTheApp
->SetTopWindow(NULL
);
387 if (wxTheApp
->GetExitOnFrameDelete())
389 // Signal to the app that we're going to close
390 wxTheApp
->ExitMainLoop();
395 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
396 void wxFrame::DoGetClientSize(int *x
, int *y
) const
399 XtVaGetValues((Widget
) m_workArea
, XmNwidth
, &xx
, XmNheight
, &yy
, NULL
);
401 if (m_frameStatusBar
)
404 m_frameStatusBar
->GetSize(& sbw
, & sbh
);
411 m_frameToolBar
->GetSize(& tbw
, & tbh
);
412 if (m_frameToolBar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
417 #endif // wxUSE_TOOLBAR
419 if (GetMenuBar() != (wxMenuBar*) NULL)
421 // it seems that if a frame holds a panel, the menu bar size
422 // gets automatically taken care of --- grano@cs.helsinki.fi 4.4.95
423 bool hasSubPanel = FALSE;
424 for(wxNode* node = GetChildren().First(); node; node = node->Next())
426 wxWindow *win = (wxWindow *)node->Data();
427 hasSubPanel = (win->IsKindOf(CLASSINFO(wxPanel)) && !win->IsKindOf(CLASSINFO(wxDialog)));
434 XtVaGetValues((Widget) GetMenuBarWidget(), XmNheight, &ys, NULL);
443 // Set the client size (i.e. leave the calculation of borders etc.
445 void wxFrame::DoSetClientSize(int width
, int height
)
447 // Calculate how large the new main window should be
448 // by finding the difference between the client area and the
449 // main window area, and adding on to the new client area
451 XtVaSetValues((Widget
) m_workArea
, XmNwidth
, width
, NULL
);
455 if (m_frameStatusBar
)
458 m_frameStatusBar
->GetSize(& sbw
, & sbh
);
465 m_frameToolBar
->GetSize(& tbw
, & tbh
);
466 if (m_frameToolBar
->GetWindowStyleFlag() & wxTB_VERTICAL
)
471 #endif // wxUSE_TOOLBAR
473 XtVaSetValues((Widget
) m_workArea
, XmNheight
, height
, NULL
);
477 wxSizeEvent
sizeEvent(wxSize(width
, height
), GetId());
478 sizeEvent
.SetEventObject(this);
480 GetEventHandler()->ProcessEvent(sizeEvent
);
484 void wxFrame::DoGetSize(int *width
, int *height
) const
487 XtVaGetValues((Widget
) m_frameShell
, XmNwidth
, &xx
, XmNheight
, &yy
, NULL
);
488 *width
= xx
; *height
= yy
;
491 void wxFrame::DoGetPosition(int *x
, int *y
) const
493 Window parent_window
= XtWindow((Widget
) m_frameShell
),
494 next_parent
= XtWindow((Widget
) m_frameShell
),
495 root
= RootWindowOfScreen(XtScreen((Widget
) m_frameShell
));
497 // search for the parent that is child of ROOT, because the WM may
498 // reparent twice and notify only the next parent (like FVWM)
499 while (next_parent
!= root
) {
500 Window
*theChildren
; unsigned int n
;
501 parent_window
= next_parent
;
502 XQueryTree(XtDisplay((Widget
) m_frameShell
), parent_window
, &root
,
503 &next_parent
, &theChildren
, &n
);
504 XFree(theChildren
); // not needed
506 int xx
, yy
; unsigned int dummy
;
507 XGetGeometry(XtDisplay((Widget
) m_frameShell
), parent_window
, &root
,
508 &xx
, &yy
, &dummy
, &dummy
, &dummy
, &dummy
);
513 void wxFrame::DoSetSize(int x
, int y
, int width
, int height
, int WXUNUSED(sizeFlags
))
516 XtVaSetValues((Widget
) m_frameShell
, XmNx
, x
, NULL
);
518 XtVaSetValues((Widget
) m_frameShell
, XmNy
, y
, NULL
);
520 XtVaSetValues((Widget
) m_frameWidget
, XmNwidth
, width
, NULL
);
522 XtVaSetValues((Widget
) m_frameWidget
, XmNheight
, height
, NULL
);
524 if (!(height
== -1 && width
== -1))
528 wxSizeEvent
sizeEvent(wxSize(width
, height
), GetId());
529 sizeEvent
.SetEventObject(this);
531 GetEventHandler()->ProcessEvent(sizeEvent
);
535 bool wxFrame::Show(bool show
)
538 return wxWindow::Show(show
);
540 m_visibleStatus
= show
; /* show-&-hide fix */
544 XtMapWidget((Widget
) m_frameShell
);
545 XRaiseWindow(XtDisplay((Widget
) m_frameShell
), XtWindow((Widget
) m_frameShell
));
547 XtUnmapWidget((Widget
) m_frameShell
);
548 // XmUpdateDisplay(wxTheApp->topLevel); // Experimental: may be responsible for crashes
553 void wxFrame::Iconize(bool iconize
)
559 XtVaSetValues((Widget
) m_frameShell
, XmNiconic
, (Boolean
)iconize
, NULL
);
562 void wxFrame::Restore()
565 XtVaSetValues((Widget
) m_frameShell
, XmNiconic
, FALSE
, NULL
);
568 void wxFrame::Maximize(bool maximize
)
576 bool wxFrame::IsIconized() const
582 XtVaGetValues((Widget
) m_frameShell
, XmNiconic
, &iconic
, NULL
);
587 bool wxFrame::IsMaximized() const
589 // No maximizing in Motif (?)
593 void wxFrame::SetTitle(const wxString
& title
)
595 if (title
== m_title
)
601 XtVaSetValues((Widget
) m_frameShell
,
602 XmNtitle
, title
.c_str(),
603 XmNiconName
, title
.c_str(),
607 void wxFrame::SetIcon(const wxIcon
& icon
)
614 if (!icon
.Ok() || !icon
.GetPixmap())
617 XtVaSetValues((Widget
) m_frameShell
, XtNiconPixmap
, icon
.GetPixmap(), NULL
);
620 void wxFrame::PositionStatusBar()
622 if (!m_frameStatusBar
)
626 GetClientSize(&w
, &h
);
628 m_frameStatusBar
->GetSize(&sw
, &sh
);
630 // Since we wish the status bar to be directly under the client area,
631 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
632 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
635 WXWidget
wxFrame::GetMenuBarWidget() const
638 return GetMenuBar()->GetMainWidget();
640 return (WXWidget
) NULL
;
643 void wxFrame::SetMenuBar(wxMenuBar
*menuBar
)
647 m_frameMenuBar
= NULL
;
651 // Currently can't set it twice
652 // wxASSERT_MSG( (m_frameMenuBar == (wxMenuBar*) NULL), "Cannot set the menubar more than once");
656 m_frameMenuBar
->DestroyMenuBar();
657 delete m_frameMenuBar
;
660 m_frameMenuBar
= menuBar
;
661 m_frameMenuBar
->CreateMenuBar(this);
664 // Responds to colour changes, and passes event on to children.
665 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
667 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
670 if ( m_frameStatusBar
)
672 wxSysColourChangedEvent event2
;
673 event2
.SetEventObject( m_frameStatusBar
);
674 m_frameStatusBar
->ProcessEvent(event2
);
677 // Propagate the event to the non-top-level children
678 wxWindow::OnSysColourChanged(event
);
681 // Default activation behaviour - set the focus for the first child
683 void wxFrame::OnActivate(wxActivateEvent
& event
)
685 if (!event
.GetActive())
688 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
690 // Find a child that's a subwindow, but not a dialog box.
691 wxWindow
*child
= (wxWindow
*)node
->Data();
692 if (!child
->IsKindOf(CLASSINFO(wxFrame
)) &&
693 !child
->IsKindOf(CLASSINFO(wxDialog
)))
703 wxToolBar
* wxFrame::CreateToolBar(long style
,
705 const wxString
& name
)
707 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
712 return m_frameToolBar
;
715 void wxFrame::PositionToolBar()
720 GetClientSize(& cw
, &ch
);
723 GetToolBar()->GetSize(& tw
, & th
);
725 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
727 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
728 // means, pretend we don't have toolbar/status bar, so we
729 // have the original client size.
730 GetToolBar()->SetSize(0, 0, tw
, ch
+ th
, wxSIZE_NO_ADJUSTMENTS
);
734 // Use the 'real' position
735 GetToolBar()->SetSize(0, 0, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);
739 #endif // wxUSE_TOOLBAR
741 void wxFrame::Raise()
743 Window parent_window
= XtWindow((Widget
) m_frameShell
),
744 next_parent
= XtWindow((Widget
) m_frameShell
),
745 root
= RootWindowOfScreen(XtScreen((Widget
) m_frameShell
));
746 // search for the parent that is child of ROOT, because the WM may
747 // reparent twice and notify only the next parent (like FVWM)
748 while (next_parent
!= root
) {
749 Window
*theChildren
; unsigned int n
;
750 parent_window
= next_parent
;
751 XQueryTree(XtDisplay((Widget
) m_frameShell
), parent_window
, &root
,
752 &next_parent
, &theChildren
, &n
);
753 XFree(theChildren
); // not needed
755 XRaiseWindow(XtDisplay((Widget
) m_frameShell
), parent_window
);
758 void wxFrame::Lower()
760 Window parent_window
= XtWindow((Widget
) m_frameShell
),
761 next_parent
= XtWindow((Widget
) m_frameShell
),
762 root
= RootWindowOfScreen(XtScreen((Widget
) m_frameShell
));
763 // search for the parent that is child of ROOT, because the WM may
764 // reparent twice and notify only the next parent (like FVWM)
765 while (next_parent
!= root
) {
766 Window
*theChildren
; unsigned int n
;
767 parent_window
= next_parent
;
768 XQueryTree(XtDisplay((Widget
) m_frameShell
), parent_window
, &root
,
769 &next_parent
, &theChildren
, &n
);
770 XFree(theChildren
); // not needed
772 XLowerWindow(XtDisplay((Widget
) m_frameShell
), parent_window
);
775 void wxFrameFocusProc(Widget
WXUNUSED(workArea
), XtPointer
WXUNUSED(clientData
),
776 XmAnyCallbackStruct
*WXUNUSED(cbs
))
778 // wxDebugMsg("focus proc from frame %ld\n",(long)frame);
780 // wxFrame *frame = (wxFrame *)clientData;
781 // frame->GetEventHandler()->OnSetFocus();
784 /* MATTEW: Used to insure that hide-&-show within an event cycle works */
785 static void wxFrameMapProc(Widget frameShell
, XtPointer clientData
,
786 XCrossingEvent
* event
)
788 wxFrame
*frame
= (wxFrame
*)wxGetWindowFromTable((Widget
)clientData
);
791 XEvent
*e
= (XEvent
*)event
;
793 if (e
->xany
.type
== MapNotify
)
796 XtVaSetValues(frameShell
, XmNiconic
, (Boolean
)False
, NULL
);
797 if (!frame
->GetVisibleStatus())
799 /* We really wanted this to be hidden! */
800 XtUnmapWidget((Widget
) frame
->GetShellWidget());
803 else if (e
->xany
.type
== UnmapNotify
)
805 XtVaSetValues(frameShell
, XmNiconic
, (Boolean
)True
, NULL
);
810 bool wxFrame::PreResize()
814 #endif // wxUSE_TOOLBAR
818 #endif // wxUSE_STATUSBAR
823 WXWidget
wxFrame::GetClientWidget() const
828 void wxFrame::ChangeFont(bool WXUNUSED(keepOriginalSize
))
833 void wxFrame::ChangeBackgroundColour()
835 if (GetClientWidget())
836 DoChangeBackgroundColour(GetClientWidget(), m_backgroundColour
);
839 void wxFrame::ChangeForegroundColour()
841 if (GetClientWidget())
842 DoChangeForegroundColour(GetClientWidget(), m_foregroundColour
);
845 void wxCloseFrameCallback(Widget
WXUNUSED(widget
), XtPointer client_data
, XmAnyCallbackStruct
*WXUNUSED(cbs
))
847 wxFrame
*frame
= (wxFrame
*)client_data
;
849 wxCloseEvent
closeEvent(wxEVT_CLOSE_WINDOW
, frame
->GetId());
850 closeEvent
.SetEventObject(frame
);
852 // May delete the frame (with delayed deletion)
853 frame
->GetEventHandler()->ProcessEvent(closeEvent
);