1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: MDI classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "mdi.h"
18 #include "wx/settings.h"
21 #include <Xm/BulletinB.h>
24 #include <Xm/RowColumn.h>
25 #include <Xm/CascadeBG.h>
27 #include <Xm/PushBG.h>
28 #include <Xm/AtomMgr.h>
29 #include <Xm/Protocols.h>
31 #include "wx/motif/private.h"
33 extern wxList wxModelessWindows
;
35 // Implemented in frame.cpp
36 extern void wxFrameFocusProc(Widget workArea
, XtPointer clientData
,
37 XmAnyCallbackStruct
*cbs
);
39 #define wxID_NOTEBOOK_CLIENT_AREA wxID_HIGHEST + 100
41 #if !USE_SHARED_LIBRARY
42 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxFrame
)
43 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxFrame
)
44 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxNotebook
)
46 BEGIN_EVENT_TABLE(wxMDIParentFrame
, wxFrame
)
47 EVT_SIZE(wxMDIParentFrame::OnSize
)
48 EVT_ACTIVATE(wxMDIParentFrame::OnActivate
)
49 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged
)
52 BEGIN_EVENT_TABLE(wxMDIClientWindow
, wxNotebook
)
53 EVT_SCROLL(wxMDIClientWindow::OnScroll
)
54 EVT_NOTEBOOK_PAGE_CHANGED(wxID_NOTEBOOK_CLIENT_AREA
, wxMDIClientWindow::OnPageChanged
)
61 wxMDIParentFrame::wxMDIParentFrame()
63 m_clientWindow
= (wxMDIClientWindow
*) NULL
;
64 m_activeChild
= (wxMDIChildFrame
*) NULL
;
65 m_activeMenuBar
= (wxMenuBar
*) NULL
;
68 bool wxMDIParentFrame::Create(wxWindow
*parent
,
70 const wxString
& title
,
76 m_clientWindow
= (wxMDIClientWindow
*) NULL
;
77 m_activeChild
= (wxMDIChildFrame
*) NULL
;
78 m_activeMenuBar
= (wxMenuBar
*) NULL
;
80 bool success
= wxFrame::Create(parent
, id
, title
, pos
, size
, style
, name
);
83 // TODO: app cannot override OnCreateClient since
84 // wxMDIParentFrame::OnCreateClient will still be called
85 // (we're in the constructor). How to resolve?
87 m_clientWindow
= OnCreateClient();
89 // Uses own style for client style
90 m_clientWindow
->CreateClient(this, GetWindowStyleFlag());
93 GetClientSize(& w
, & h
);
94 m_clientWindow
->SetSize(0, 0, w
, h
);
101 wxMDIParentFrame::~wxMDIParentFrame()
103 // Make sure we delete the client window last of all
104 RemoveChild(m_clientWindow
);
108 delete m_clientWindow
;
109 m_clientWindow
= NULL
;
112 // Get size *available for subwindows* i.e. excluding menu bar.
113 void wxMDIParentFrame::GetClientSize(int *x
, int *y
) const
115 wxFrame::GetClientSize(x
, y
);
118 void wxMDIParentFrame::SetMenuBar(wxMenuBar
*menu_bar
)
120 m_frameMenuBar
= menu_bar
;
122 SetChildMenuBar((wxMDIChildFrame
*) NULL
);
125 void wxMDIParentFrame::OnSize(wxSizeEvent
& event
)
127 #if wxUSE_CONSTRAINTS
134 GetClientSize(&width
, &height
);
136 if ( GetClientWindow() )
137 GetClientWindow()->SetSize(x
, y
, width
, height
);
140 void wxMDIParentFrame::OnActivate(wxActivateEvent
& event
)
145 // Returns the active MDI child window
146 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
148 return m_activeChild
;
151 // Create the client window class (don't Create the window,
152 // just return a new class)
153 wxMDIClientWindow
*wxMDIParentFrame::OnCreateClient()
155 return new wxMDIClientWindow
;
158 // Set the child's menu into the parent frame
159 void wxMDIParentFrame::SetChildMenuBar(wxMDIChildFrame
* child
)
161 wxMenuBar
* oldMenuBar
= m_activeMenuBar
;
163 if (child
== (wxMDIChildFrame
*) NULL
) // No child: use parent frame
165 if (GetMenuBar() && (GetMenuBar() != m_activeMenuBar
))
167 // if (m_activeMenuBar)
168 // m_activeMenuBar->DestroyMenuBar();
170 m_activeMenuBar
= GetMenuBar();
171 m_activeMenuBar
->CreateMenuBar(this);
173 if (oldMenuBar && XtIsManaged((Widget) oldMenuBar->GetMainWidget()))
174 XtUnmanageChild((Widget) oldMenuBar->GetMainWidget());
176 if (oldMenuBar
&& oldMenuBar
->GetMainWidget())
177 XtUnmapWidget((Widget
) oldMenuBar
->GetMainWidget());
181 else if (child
->GetMenuBar() == (wxMenuBar
*) NULL
) // No child menu bar: use parent frame
183 if (GetMenuBar() && (GetMenuBar() != m_activeMenuBar
))
185 // if (m_activeMenuBar)
186 // m_activeMenuBar->DestroyMenuBar();
187 m_activeMenuBar
= GetMenuBar();
188 m_activeMenuBar
->CreateMenuBar(this);
190 if (oldMenuBar && XtIsManaged((Widget) oldMenuBar->GetMainWidget()))
191 XtUnmanageChild((Widget) oldMenuBar->GetMainWidget());
193 if (oldMenuBar
&& oldMenuBar
->GetMainWidget())
194 XtUnmapWidget((Widget
) oldMenuBar
->GetMainWidget());
197 else // The child has a menubar
199 if (child
->GetMenuBar() != m_activeMenuBar
)
201 // if (m_activeMenuBar)
202 // m_activeMenuBar->DestroyMenuBar();
204 m_activeMenuBar
= child
->GetMenuBar();
205 m_activeMenuBar
->CreateMenuBar(this);
207 if (oldMenuBar && XtIsManaged((Widget) oldMenuBar->GetMainWidget()))
208 XtUnmanageChild((Widget) oldMenuBar->GetMainWidget());
210 if (oldMenuBar
&& oldMenuBar
->GetMainWidget())
211 XtUnmapWidget((Widget
) oldMenuBar
->GetMainWidget());
216 // Redirect events to active child first
217 bool wxMDIParentFrame::ProcessEvent(wxEvent
& event
)
219 // Stops the same event being processed repeatedly
220 static wxEventType inEvent
= wxEVT_NULL
;
221 if (inEvent
== event
.GetEventType())
224 inEvent
= event
.GetEventType();
227 if (m_activeChild
&& event
.IsKindOf(CLASSINFO(wxCommandEvent
)))
229 res
= m_activeChild
->GetEventHandler()->ProcessEvent(event
);
233 res
= GetEventHandler()->wxEvtHandler::ProcessEvent(event
);
235 inEvent
= wxEVT_NULL
;
240 // Responds to colour changes, and passes event on to children.
241 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
245 // Propagate the event to the non-top-level children
246 wxFrame::OnSysColourChanged(event
);
250 void wxMDIParentFrame::Cascade()
255 void wxMDIParentFrame::Tile()
260 void wxMDIParentFrame::ArrangeIcons()
265 void wxMDIParentFrame::ActivateNext()
270 void wxMDIParentFrame::ActivatePrevious()
277 wxMDIChildFrame::wxMDIChildFrame()
279 m_mdiParentFrame
= (wxMDIParentFrame
*) NULL
;
282 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
284 const wxString
& title
,
288 const wxString
& name
)
292 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
);
293 m_foregroundColour
= *wxBLACK
;
294 m_windowFont
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
299 m_windowId
= (int)NewControlId();
301 wxMDIClientWindow
* clientWindow
= parent
->GetClientWindow();
303 wxASSERT_MSG( (clientWindow
!= (wxWindow
*) NULL
), "Missing MDI client window.");
305 if (clientWindow
) clientWindow
->AddChild(this);
307 SetMDIParentFrame(parent
);
309 int x
= pos
.x
; int y
= pos
.y
;
310 int width
= size
.x
; int height
= size
.y
;
312 width
= 200; // TODO: give reasonable default
314 height
= 200; // TODO: give reasonable default
316 // We're deactivating the old child
317 wxMDIChildFrame
* oldActiveChild
= parent
->GetActiveChild();
320 wxActivateEvent
event(wxEVT_ACTIVATE
, FALSE
, oldActiveChild
->GetId());
321 event
.SetEventObject( oldActiveChild
);
322 oldActiveChild
->GetEventHandler()->ProcessEvent(event
);
325 // This is the currently active child
326 parent
->SetActiveChild((wxMDIChildFrame
*) this);
328 // This time we'll try a bog-standard bulletin board for
329 // the 'frame'. A main window doesn't seem to work.
331 m_mainWidget
= (WXWidget
) XtVaCreateWidget("client",
332 xmBulletinBoardWidgetClass
, (Widget
) clientWindow
->GetTopWidget(),
336 XmNrightAttachment, XmATTACH_FORM,
337 XmNleftAttachment, XmATTACH_FORM,
338 XmNtopAttachment, XmATTACH_FORM,
339 XmNbottomAttachment, XmATTACH_FORM,
341 XmNresizePolicy
, XmRESIZE_NONE
,
344 SetCanAddEventHandler(TRUE
);
345 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
347 ChangeBackgroundColour();
352 m_frameWidget
= (WXWidget
) XtVaCreateManagedWidget("main_window",
353 xmMainWindowWidgetClass
, (Widget
) clientWindow
->GetTopWidget(),
354 XmNresizePolicy
, XmRESIZE_NONE
,
357 // TODO: make sure this doesn't cause problems.
358 // I think ~wxFrame will do the right thing since it deletes m_frameWidget,
359 // then sets the main widget to NULL.
360 m_mainWidget
= m_frameWidget
;
362 m_workArea
= (WXWidget
) XtVaCreateWidget("form",
363 xmFormWidgetClass
, (Widget
) m_frameWidget
,
364 XmNresizePolicy
, XmRESIZE_NONE
,
367 m_clientArea
= (WXWidget
) XtVaCreateWidget("client",
368 xmBulletinBoardWidgetClass
, (Widget
) m_workArea
,
371 XmNrightAttachment
, XmATTACH_FORM
,
372 XmNleftAttachment
, XmATTACH_FORM
,
373 XmNtopAttachment
, XmATTACH_FORM
,
374 XmNbottomAttachment
, XmATTACH_FORM
,
375 // XmNresizePolicy, XmRESIZE_ANY,
378 XtVaSetValues((Widget
) m_frameWidget
,
379 XmNworkWindow
, (Widget
) m_workArea
,
382 XtManageChild((Widget
) m_clientArea
);
383 XtManageChild((Widget
) m_workArea
);
385 wxASSERT_MSG ((wxWidgetHashTable
->Get((long)m_workArea
) == (wxObject
*) NULL
), "Widget table clash in frame.cpp") ;
387 wxAddWindowToTable((Widget
) m_workArea
, this);
391 XtOverrideTranslations((Widget
) m_workArea
,
392 ptr
= XtParseTranslationTable("<Configure>: resize()"));
396 XtAddCallback((Widget
) m_workArea
, XmNfocusCallback
,
397 (XtCallbackProc
)wxFrameFocusProc
, (XtPointer
)this);
399 XtManageChild((Widget
) m_mainWidget
);
402 XtVaSetValues((Widget
) m_mainWidget
, XmNx
, x
, NULL
);
404 XtVaSetValues((Widget
) m_mainWidget
, XmNy
, y
, NULL
);
406 XtVaSetValues((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
408 XtVaSetValues((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
412 XtManageChild((Widget
) m_mainWidget
);
416 clientWindow
->AddPage(this, title
, TRUE
);
417 clientWindow
->Refresh();
419 // Positions the toolbar and status bar -- but we don't have any.
422 wxModelessWindows
.Append(this);
427 wxMDIChildFrame::~wxMDIChildFrame()
429 if (GetMDIParentFrame())
431 wxMDIParentFrame
* parentFrame
= GetMDIParentFrame();
433 if (parentFrame
->GetActiveChild() == this)
434 parentFrame
->SetActiveChild((wxMDIChildFrame
*) NULL
);
435 wxMDIClientWindow
* clientWindow
= parentFrame
->GetClientWindow();
437 // Remove page if still there
438 if (clientWindow
->RemovePage(this))
439 clientWindow
->Refresh();
441 // Set the selection to the first remaining page
442 if (clientWindow
->GetPageCount() > 0)
444 wxMDIChildFrame
* child
= (wxMDIChildFrame
*) clientWindow
->GetPage(0);
445 parentFrame
->SetActiveChild(child
);
446 parentFrame
->SetChildMenuBar(child
);
450 parentFrame
->SetActiveChild((wxMDIChildFrame
*) NULL
);
451 parentFrame
->SetChildMenuBar((wxMDIChildFrame
*) NULL
);
457 // Implementation: intercept and act upon raise and lower commands.
458 void wxMDIChildFrame::OnRaise()
460 wxMDIParentFrame
* parentFrame
= (wxMDIParentFrame
*) GetParent() ;
461 wxMDIChildFrame
* oldActiveChild
= parentFrame
->GetActiveChild();
462 parentFrame
->SetActiveChild(this);
466 wxActivateEvent
event(wxEVT_ACTIVATE
, FALSE
, oldActiveChild
->GetId());
467 event
.SetEventObject( oldActiveChild
);
468 oldActiveChild
->GetEventHandler()->ProcessEvent(event
);
471 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, this->GetId());
472 event
.SetEventObject( this );
473 this->GetEventHandler()->ProcessEvent(event
);
476 void wxMDIChildFrame::OnLower()
478 wxMDIParentFrame
* parentFrame
= (wxMDIParentFrame
*) GetParent() ;
479 wxMDIChildFrame
* oldActiveChild
= parentFrame
->GetActiveChild();
481 if (oldActiveChild
== this)
483 wxActivateEvent
event(wxEVT_ACTIVATE
, FALSE
, oldActiveChild
->GetId());
484 event
.SetEventObject( oldActiveChild
);
485 oldActiveChild
->GetEventHandler()->ProcessEvent(event
);
487 // TODO: unfortunately we don't now know which is the top-most child,
488 // so make the active child NULL.
489 parentFrame
->SetActiveChild((wxMDIChildFrame
*) NULL
);
493 // Set the client size (i.e. leave the calculation of borders etc.
495 void wxMDIChildFrame::SetClientSize(int width
, int height
)
497 wxWindow::SetClientSize(width
, height
);
500 void wxMDIChildFrame::GetClientSize(int* width
, int* height
) const
502 wxWindow::GetSize(width
, height
);
505 void wxMDIChildFrame::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
507 wxWindow::SetSize(x
, y
, width
, height
, sizeFlags
);
510 void wxMDIChildFrame::GetSize(int* width
, int* height
) const
512 wxWindow::GetSize(width
, height
);
515 void wxMDIChildFrame::GetPosition(int *x
, int *y
) const
517 wxWindow::GetPosition(x
, y
);
520 bool wxMDIChildFrame::Show(bool show
)
522 m_visibleStatus
= show
; /* show-&-hide fix */
523 return wxWindow::Show(show
);
526 void wxMDIChildFrame::SetMenuBar(wxMenuBar
*menuBar
)
528 // Don't create the underlying menubar yet; need to recreate
529 // it every time the child is activated.
530 m_frameMenuBar
= menuBar
;
532 // We make the assumption that if you're setting the menubar,
533 // this is the currently active child.
534 GetMDIParentFrame()->SetChildMenuBar(this);
538 void wxMDIChildFrame::SetIcon(const wxIcon
& icon
)
543 /* TODO: doesn't work yet (crashes in XCopyArea)
544 Pixmap pixmap = (Pixmap) m_icon.GetPixmap();
545 m_mdiWindow->setPixmap(pixmap);
550 void wxMDIChildFrame::SetTitle(const wxString
& title
)
553 // TODO: set parent frame title
557 void wxMDIChildFrame::Maximize()
562 void wxMDIChildFrame::Iconize(bool iconize
)
567 bool wxMDIChildFrame::IsIconized() const
572 void wxMDIChildFrame::Restore()
577 void wxMDIChildFrame::Activate()
582 void wxMDIChildFrame::CaptureMouse()
584 wxWindow::CaptureMouse();
587 void wxMDIChildFrame::ReleaseMouse()
589 wxWindow::ReleaseMouse();
592 void wxMDIChildFrame::Raise()
597 void wxMDIChildFrame::Lower(void)
602 void wxMDIChildFrame::SetSizeHints(int WXUNUSED(minW
), int WXUNUSED(minH
), int WXUNUSED(maxW
), int WXUNUSED(maxH
), int WXUNUSED(incW
), int WXUNUSED(incH
))
608 wxMDIClientWindow::wxMDIClientWindow()
612 wxMDIClientWindow::~wxMDIClientWindow()
614 // By the time this destructor is called, the child frames will have been
615 // deleted and removed from the notebook/client window.
618 m_mainWidget
= (WXWidget
) 0;
621 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
623 // m_windowParent = parent;
624 // m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE);
626 return wxNotebook::Create(parent
, wxID_NOTEBOOK_CLIENT_AREA
, wxPoint(0, 0), wxSize(100, 100), 0);
629 void wxMDIClientWindow::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
631 wxWindow::SetSize(x
, y
, width
, height
, sizeFlags
);
634 void wxMDIClientWindow::SetClientSize(int width
, int height
)
636 wxWindow::SetClientSize(width
, height
);
639 void wxMDIClientWindow::GetClientSize(int *width
, int *height
) const
641 wxWindow::GetClientSize(width
, height
);
644 void wxMDIClientWindow::GetSize(int *width
, int *height
) const
646 wxWindow::GetSize(width
, height
);
649 void wxMDIClientWindow::GetPosition(int *x
, int *y
) const
651 wxWindow::GetPosition(x
, y
);
654 // Explicitly call default scroll behaviour
655 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)
657 Default(); // Default processing
660 void wxMDIClientWindow::OnPageChanged(wxNotebookEvent
& event
)
662 // Notify child that it has been activated
663 if (event
.GetOldSelection() != -1)
665 wxMDIChildFrame
* oldChild
= (wxMDIChildFrame
*) GetPage(event
.GetOldSelection());
668 wxActivateEvent
event(wxEVT_ACTIVATE
, FALSE
, oldChild
->GetId());
669 event
.SetEventObject( oldChild
);
670 oldChild
->GetEventHandler()->ProcessEvent(event
);
673 wxMDIChildFrame
* activeChild
= (wxMDIChildFrame
*) GetPage(event
.GetSelection());
676 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, activeChild
->GetId());
677 event
.SetEventObject( activeChild
);
678 activeChild
->GetEventHandler()->ProcessEvent(event
);
680 if (activeChild
->GetMDIParentFrame())
682 activeChild
->GetMDIParentFrame()->SetActiveChild(activeChild
);
683 activeChild
->GetMDIParentFrame()->SetChildMenuBar(activeChild
);