1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/mdi.cpp
3 // Purpose: MDI classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
20 #include "wx/settings.h"
24 #pragma message disable nosimpint
27 #include <Xm/BulletinB.h>
30 #include <Xm/RowColumn.h>
31 #include <Xm/CascadeBG.h>
33 #include <Xm/PushBG.h>
34 #include <Xm/AtomMgr.h>
35 #include <Xm/Protocols.h>
37 #pragma message enable nosimpint
40 #include "wx/motif/private.h"
42 extern wxList wxModelessWindows
;
44 // Implemented in frame.cpp
45 extern void wxFrameFocusProc(Widget workArea
, XtPointer clientData
,
46 XmAnyCallbackStruct
*cbs
);
48 #define wxID_NOTEBOOK_CLIENT_AREA wxID_HIGHEST + 100
50 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxFrame
)
51 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxFrame
)
52 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxNotebook
)
54 BEGIN_EVENT_TABLE(wxMDIParentFrame
, wxFrame
)
55 EVT_SIZE(wxMDIParentFrame::OnSize
)
56 EVT_ACTIVATE(wxMDIParentFrame::OnActivate
)
57 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged
)
58 EVT_MENU_HIGHLIGHT_ALL(wxMDIParentFrame::OnMenuHighlight
)
61 BEGIN_EVENT_TABLE(wxMDIClientWindow
, wxNotebook
)
62 EVT_NOTEBOOK_PAGE_CHANGED(wxID_NOTEBOOK_CLIENT_AREA
, wxMDIClientWindow::OnPageChanged
)
68 void wxMDIParentFrame::Init()
70 m_activeMenuBar
= NULL
;
73 bool wxMDIParentFrame::Create(wxWindow
*parent
,
75 const wxString
& title
,
81 if ( !wxFrame::Create(parent
, id
, title
, pos
, size
, style
, name
) )
84 m_clientWindow
= OnCreateClient();
85 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
89 GetClientSize(& w
, & h
);
90 m_clientWindow
->SetSize(0, 0, w
, h
);
94 wxMDIParentFrame::~wxMDIParentFrame()
96 // Make sure we delete the client window last of all
97 RemoveChild(m_clientWindow
);
101 delete m_clientWindow
;
102 m_clientWindow
= NULL
;
105 void wxMDIParentFrame::SetMenuBar(wxMenuBar
*menu_bar
)
107 m_frameMenuBar
= menu_bar
;
109 SetChildMenuBar(NULL
);
112 void wxMDIParentFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
114 #if wxUSE_CONSTRAINTS
121 GetClientSize(&width
, &height
);
123 if ( GetClientWindow() )
124 GetClientWindow()->SetSize(x
, y
, width
, height
);
127 void wxMDIParentFrame::OnActivate(wxActivateEvent
& WXUNUSED(event
))
132 // Set the child's menu into the parent frame
133 void wxMDIParentFrame::SetChildMenuBar(wxMDIChildFrame
* child
)
135 wxMenuBar
* oldMenuBar
= m_activeMenuBar
;
137 if (child
== NULL
) // No child: use parent frame
139 if (GetMenuBar() && (GetMenuBar() != m_activeMenuBar
))
141 // if (m_activeMenuBar)
142 // m_activeMenuBar->DestroyMenuBar();
144 m_activeMenuBar
= GetMenuBar();
145 m_activeMenuBar
->CreateMenuBar(this);
147 if (oldMenuBar && XtIsManaged((Widget) oldMenuBar->GetMainWidget()))
148 XtUnmanageChild((Widget) oldMenuBar->GetMainWidget());
150 if (oldMenuBar
&& oldMenuBar
->GetMainWidget())
151 XtUnmapWidget((Widget
) oldMenuBar
->GetMainWidget());
155 else if (child
->GetMenuBar() == NULL
) // No child menu bar: use parent frame
157 if (GetMenuBar() && (GetMenuBar() != m_activeMenuBar
))
159 // if (m_activeMenuBar)
160 // m_activeMenuBar->DestroyMenuBar();
161 m_activeMenuBar
= GetMenuBar();
162 m_activeMenuBar
->CreateMenuBar(this);
164 if (oldMenuBar && XtIsManaged((Widget) oldMenuBar->GetMainWidget()))
165 XtUnmanageChild((Widget) oldMenuBar->GetMainWidget());
167 if (oldMenuBar
&& oldMenuBar
->GetMainWidget())
168 XtUnmapWidget((Widget
) oldMenuBar
->GetMainWidget());
171 else // The child has a menubar
173 if (child
->GetMenuBar() != m_activeMenuBar
)
175 // if (m_activeMenuBar)
176 // m_activeMenuBar->DestroyMenuBar();
178 m_activeMenuBar
= child
->GetMenuBar();
179 m_activeMenuBar
->CreateMenuBar(this);
181 if (oldMenuBar && XtIsManaged((Widget) oldMenuBar->GetMainWidget()))
182 XtUnmanageChild((Widget) oldMenuBar->GetMainWidget());
184 if (oldMenuBar
&& oldMenuBar
->GetMainWidget())
185 XtUnmapWidget((Widget
) oldMenuBar
->GetMainWidget());
190 // Redirect events to active child first
191 bool wxMDIParentFrame::ProcessEvent(wxEvent
& event
)
193 // Stops the same event being processed repeatedly
194 static wxEventType inEvent
= wxEVT_NULL
;
195 if (inEvent
== event
.GetEventType())
198 inEvent
= event
.GetEventType();
201 if (m_currentChild
&& event
.IsKindOf(CLASSINFO(wxCommandEvent
)))
203 res
= m_currentChild
->HandleWindowEvent(event
);
207 res
= GetEventHandler()->wxEvtHandler::ProcessEvent(event
);
209 inEvent
= wxEVT_NULL
;
214 // Responds to colour changes, and passes event on to children.
215 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
219 // Propagate the event to the non-top-level children
220 wxFrame::OnSysColourChanged(event
);
223 // Default menu selection behaviour - display a help string
224 void wxMDIParentFrame::OnMenuHighlight(wxMenuEvent
& event
)
228 if (event
.GetMenuId() == -1)
229 SetStatusText(wxEmptyString
);
232 wxMenuBar
*menuBar
= NULL
;
233 if (GetActiveChild())
234 menuBar
= GetActiveChild()->GetMenuBar();
236 menuBar
= GetMenuBar();
239 wxString
helpString(menuBar
->GetHelpString(event
.GetMenuId()));
240 if (!helpString
.empty())
241 SetStatusText(helpString
);
249 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
251 const wxString
& title
,
255 const wxString
& name
)
258 SetWindowStyleFlag(style
);
263 m_windowId
= (int)NewControlId();
265 wxMDIClientWindow
* clientWindow
= parent
->GetClientWindow();
267 wxCHECK_MSG( clientWindow
, false, "Missing MDI client window." );
269 clientWindow
->AddChild(this);
271 m_mdiParent
= parent
;
277 width
= 200; // TODO: give reasonable default
279 height
= 200; // TODO: give reasonable default
281 // We're deactivating the old child
282 wxMDIChildFrame
* oldActiveChild
= parent
->GetActiveChild();
285 wxActivateEvent
event(wxEVT_ACTIVATE
, false, oldActiveChild
->GetId());
286 event
.SetEventObject( oldActiveChild
);
287 oldActiveChild
->HandleWindowEvent(event
);
290 // This is the currently active child
291 parent
->SetActiveChild((wxMDIChildFrame
*) this);
293 // This time we'll try a bog-standard bulletin board for
294 // the 'frame'. A main window doesn't seem to work.
296 m_mainWidget
= (WXWidget
) XtVaCreateWidget("client",
297 xmBulletinBoardWidgetClass
, (Widget
) clientWindow
->GetTopWidget(),
301 XmNrightAttachment, XmATTACH_FORM,
302 XmNleftAttachment, XmATTACH_FORM,
303 XmNtopAttachment, XmATTACH_FORM,
304 XmNbottomAttachment, XmATTACH_FORM,
306 XmNresizePolicy
, XmRESIZE_NONE
,
309 XtAddEventHandler((Widget
) m_mainWidget
, ExposureMask
,False
,
310 wxUniversalRepaintProc
, (XtPointer
) this);
313 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
315 XtManageChild((Widget
) m_mainWidget
);
319 clientWindow
->AddPage(this, title
, true);
320 clientWindow
->Refresh();
322 // Positions the toolbar and status bar -- but we don't have any.
325 wxModelessWindows
.Append(this);
330 wxMDIChildFrame::~wxMDIChildFrame()
333 XtRemoveEventHandler((Widget
) m_mainWidget
, ExposureMask
,False
,
334 wxUniversalRepaintProc
, (XtPointer
) this);
336 if (GetMDIParentFrame())
338 wxMDIParentFrame
* parentFrame
= GetMDIParentFrame();
340 if (parentFrame
->GetActiveChild() == this)
341 parentFrame
->SetActiveChild(NULL
);
342 wxMDIClientWindow
* clientWindow
= parentFrame
->GetClientWindow();
344 // Remove page if still there
346 int i
= clientWindow
->FindPage(this);
350 clientWindow
->RemovePage(i
);
351 clientWindow
->Refresh();
355 // Set the selection to the first remaining page
356 if (clientWindow
->GetPageCount() > 0)
358 wxMDIChildFrame
* child
= (wxMDIChildFrame
*) clientWindow
->GetPage(0);
359 parentFrame
->SetActiveChild(child
);
360 parentFrame
->SetChildMenuBar(child
);
364 parentFrame
->SetActiveChild(NULL
);
365 parentFrame
->SetChildMenuBar(NULL
);
370 bool wxMDIChildFrame::Show(bool show
)
372 SetVisibleStatus( show
);
373 return wxWindow::Show(show
);
376 void wxMDIChildFrame::SetMenuBar(wxMenuBar
*menuBar
)
378 // Don't create the underlying menubar yet; need to recreate
379 // it every time the child is activated.
380 m_frameMenuBar
= menuBar
;
382 // We make the assumption that if you're setting the menubar,
383 // this is the currently active child.
384 GetMDIParentFrame()->SetChildMenuBar(this);
387 void wxMDIChildFrame::SetTitle(const wxString
& title
)
390 wxMDIClientWindow
* clientWindow
= GetMDIParentFrame()->GetClientWindow();
392 int i
= clientWindow
->FindPage(this);
393 if (i
!= wxNOT_FOUND
)
394 clientWindow
->SetPageText(i
, title
);
397 void wxMDIChildFrame::Activate()
404 wxMDIClientWindow::~wxMDIClientWindow()
406 // By the time this destructor is called, the child frames will have been
407 // deleted and removed from the notebook/client window.
410 m_mainWidget
= (WXWidget
) 0;
413 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
415 SetWindowStyleFlag(style
);
417 if ( !wxNotebook::Create(parent
, wxID_NOTEBOOK_CLIENT_AREA
,
418 wxPoint(0, 0), wxSize(100, 100), 0) )
424 int wxMDIClientWindow::FindPage(const wxNotebookPage
* page
)
426 for (int i
= GetPageCount() - 1; i
>= 0; --i
)
428 if (GetPage(i
) == page
)
435 void wxMDIClientWindow::OnPageChanged(wxBookCtrlEvent
& event
)
437 // Notify child that it has been activated
438 if (event
.GetOldSelection() != -1)
440 wxMDIChildFrame
* oldChild
= (wxMDIChildFrame
*) GetPage(event
.GetOldSelection());
443 wxActivateEvent
event(wxEVT_ACTIVATE
, false, oldChild
->GetId());
444 event
.SetEventObject( oldChild
);
445 oldChild
->HandleWindowEvent(event
);
448 if (event
.GetSelection() != -1)
450 wxMDIChildFrame
* activeChild
= (wxMDIChildFrame
*) GetPage(event
.GetSelection());
453 wxActivateEvent
event(wxEVT_ACTIVATE
, true, activeChild
->GetId());
454 event
.SetEventObject( activeChild
);
455 activeChild
->HandleWindowEvent(event
);
457 if (activeChild
->GetMDIParentFrame())
459 activeChild
->GetMDIParentFrame()->SetActiveChild(activeChild
);
460 activeChild
->GetMDIParentFrame()->SetChildMenuBar(activeChild
);