1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/mdi.cpp
3 // Purpose: MDI classes for wxMSW
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin on 2008-11-04 to use the base classes
8 // Copyright: (c) 1998 Julian Smart
9 // (c) 2008-2009 Vadim Zeitlin
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ===========================================================================
15 // ===========================================================================
17 // ---------------------------------------------------------------------------
19 // ---------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
28 #if wxUSE_MDI && !defined(__WXUNIVERSAL__)
37 #include "wx/dialog.h"
38 #include "wx/statusbr.h"
39 #include "wx/settings.h"
42 #include "wx/toolbar.h"
45 #include "wx/stockitem.h"
46 #include "wx/msw/private.h"
50 // ---------------------------------------------------------------------------
52 // ---------------------------------------------------------------------------
54 extern wxMenu
*wxCurrentPopupMenu
;
56 extern void wxRemoveHandleAssociation(wxWindow
*win
);
61 // ---------------------------------------------------------------------------
63 // ---------------------------------------------------------------------------
65 // This range gives a maximum of 500 MDI children. Should be enough :-)
66 const int wxFIRST_MDI_CHILD
= 4100;
67 const int wxLAST_MDI_CHILD
= 4600;
69 // The MDI "Window" menu label
70 const char *WINDOW_MENU_LABEL
= gettext_noop("&Window");
72 // ---------------------------------------------------------------------------
74 // ---------------------------------------------------------------------------
76 // set the MDI menus (by sending the WM_MDISETMENU message) and update the menu
77 // of the parent of win (which is supposed to be the MDI client window)
78 void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
);
80 // insert the window menu (subMenu) into menu just before "Help" submenu or at
81 // the very end if not found
82 void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU hMenu
, HMENU subMenu
);
84 // Remove the window menu
85 void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU hMenu
);
87 // unpack the parameters of WM_MDIACTIVATE message
88 void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
89 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
);
91 // return the HMENU of the MDI menu
93 // this function works correctly even when we don't have a window menu and just
95 inline HMENU
GetMDIWindowMenu(wxMDIParentFrame
*frame
)
97 wxMenu
*menu
= frame
->GetWindowMenu();
98 return menu
? GetHmenuOf(menu
) : 0;
101 } // anonymous namespace
103 // ===========================================================================
105 // ===========================================================================
107 // ---------------------------------------------------------------------------
109 // ---------------------------------------------------------------------------
111 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxFrame
)
112 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxFrame
)
113 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxWindow
)
115 BEGIN_EVENT_TABLE(wxMDIParentFrame
, wxFrame
)
116 EVT_SIZE(wxMDIParentFrame::OnSize
)
117 EVT_ICONIZE(wxMDIParentFrame::OnIconized
)
118 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged
)
121 EVT_MENU_RANGE(wxFIRST_MDI_CHILD
, wxLAST_MDI_CHILD
,
122 wxMDIParentFrame::OnMDIChild
)
123 EVT_MENU_RANGE(wxID_MDI_WINDOW_FIRST
, wxID_MDI_WINDOW_LAST
,
124 wxMDIParentFrame::OnMDICommand
)
125 #endif // wxUSE_MENUS
128 BEGIN_EVENT_TABLE(wxMDIChildFrame
, wxFrame
)
129 EVT_IDLE(wxMDIChildFrame::OnIdle
)
132 BEGIN_EVENT_TABLE(wxMDIClientWindow
, wxWindow
)
133 EVT_SCROLL(wxMDIClientWindow::OnScroll
)
136 // ===========================================================================
137 // wxMDIParentFrame: the frame which contains the client window which manages
139 // ===========================================================================
141 bool wxMDIParentFrame::Create(wxWindow
*parent
,
143 const wxString
& title
,
147 const wxString
& name
)
149 // this style can be used to prevent a window from having the standard MDI
151 if ( !(style
& wxFRAME_NO_WINDOW_MENU
) )
153 // normal case: we have the window menu, so construct it
154 m_windowMenu
= new wxMenu
;
156 m_windowMenu
->Append(wxID_MDI_WINDOW_CASCADE
, _("&Cascade"));
157 m_windowMenu
->Append(wxID_MDI_WINDOW_TILE_HORZ
, _("Tile &Horizontally"));
158 m_windowMenu
->Append(wxID_MDI_WINDOW_TILE_VERT
, _("Tile &Vertically"));
159 m_windowMenu
->AppendSeparator();
160 m_windowMenu
->Append(wxID_MDI_WINDOW_ARRANGE_ICONS
, _("&Arrange Icons"));
161 m_windowMenu
->Append(wxID_MDI_WINDOW_NEXT
, _("&Next"));
162 m_windowMenu
->Append(wxID_MDI_WINDOW_PREV
, _("&Previous"));
165 #if wxUSE_MENUS && wxUSE_ACCEL
166 // the default menu doesn't have any accelerators (even if we have it)
167 m_accelWindowMenu
= NULL
;
168 #endif // wxUSE_MENUS && wxUSE_ACCEL
171 wxTopLevelWindows
.Append(this);
174 m_windowStyle
= style
;
177 parent
->AddChild(this);
179 if ( id
!= wxID_ANY
)
182 m_windowId
= NewControlId();
185 WXDWORD msflags
= MSWGetCreateWindowFlags(&exflags
);
186 msflags
&= ~WS_VSCROLL
;
187 msflags
&= ~WS_HSCROLL
;
189 if ( !wxWindow::MSWCreate(wxApp::GetRegisteredClassName(_T("wxMDIFrame")),
198 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
200 // unlike (almost?) all other windows, frames are created hidden
206 wxMDIParentFrame::~wxMDIParentFrame()
208 // see comment in ~wxMDIChildFrame
210 m_frameToolBar
= NULL
;
213 m_frameStatusBar
= NULL
;
214 #endif // wxUSE_STATUSBAR
216 #if wxUSE_MENUS && wxUSE_ACCEL
217 delete m_accelWindowMenu
;
218 #endif // wxUSE_MENUS && wxUSE_ACCEL
222 // the MDI frame menubar is not automatically deleted by Windows unlike for
225 ::DestroyMenu((HMENU
)m_hMenu
);
227 if ( m_clientWindow
)
229 if ( m_clientWindow
->MSWGetOldWndProc() )
230 m_clientWindow
->UnsubclassWin();
232 m_clientWindow
->SetHWND(0);
233 delete m_clientWindow
;
237 // ----------------------------------------------------------------------------
238 // wxMDIParentFrame child management
239 // ----------------------------------------------------------------------------
241 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
243 HWND hWnd
= (HWND
)::SendMessage(GetWinHwnd(GetClientWindow()),
244 WM_MDIGETACTIVE
, 0, 0L);
248 return static_cast<wxMDIChildFrame
*>(wxFindWinFromHandle(hWnd
));
251 int wxMDIParentFrame::GetChildFramesCount() const
254 for ( wxWindowList::const_iterator i
= GetChildren().begin();
255 i
!= GetChildren().end();
258 if ( wxDynamicCast(*i
, wxMDIChildFrame
) )
267 void wxMDIParentFrame::AddMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
269 switch ( GetChildFramesCount() )
272 // first MDI child was just added, we need to insert the window
273 // menu now if we have it
276 // and disable the items which can't be used until we have more
278 UpdateWindowMenu(false);
282 // second MDI child was added, enable the menu items which were
283 // disabled because they didn't make sense for a single window
284 UpdateWindowMenu(true);
289 void wxMDIParentFrame::RemoveMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
291 switch ( GetChildFramesCount() )
294 // last MDI child is being removed, remove the now unnecessary
298 // there is no need to call UpdateWindowMenu(true) here so this is
299 // not quite symmetric to AddMDIChild() above
303 // only one MDI child is going to remain, disable the menu commands
304 // which don't make sense for a single child window
305 UpdateWindowMenu(false);
310 // ----------------------------------------------------------------------------
311 // wxMDIParentFrame window menu handling
312 // ----------------------------------------------------------------------------
314 void wxMDIParentFrame::AddWindowMenu()
317 MDIInsertWindowMenu(GetClientWindow(), m_hMenu
, GetMDIWindowMenu(this));
320 void wxMDIParentFrame::RemoveWindowMenu()
323 MDIRemoveWindowMenu(GetClientWindow(), m_hMenu
);
326 void wxMDIParentFrame::UpdateWindowMenu(bool enable
)
330 m_windowMenu
->Enable(wxID_MDI_WINDOW_NEXT
, enable
);
331 m_windowMenu
->Enable(wxID_MDI_WINDOW_PREV
, enable
);
335 #if wxUSE_MENUS_NATIVE
337 void wxMDIParentFrame::InternalSetMenuBar()
339 if ( GetActiveChild() )
343 else // we don't have any MDI children yet
345 // wait until we do to add the window menu but do set the main menu for
346 // now (this is done by AddWindowMenu() as a side effect)
347 MDISetMenu(GetClientWindow(), (HMENU
)m_hMenu
, NULL
);
351 #endif // wxUSE_MENUS_NATIVE
353 void wxMDIParentFrame::SetWindowMenu(wxMenu
* menu
)
355 if ( menu
!= m_windowMenu
)
357 // notice that Remove/AddWindowMenu() are safe to call even when
358 // m_windowMenu is NULL
369 delete m_accelWindowMenu
;
370 m_accelWindowMenu
= NULL
;
372 if ( menu
&& menu
->HasAccels() )
373 m_accelWindowMenu
= menu
->CreateAccelTable();
374 #endif // wxUSE_ACCEL
377 // ----------------------------------------------------------------------------
378 // wxMDIParentFrame other menu-related stuff
379 // ----------------------------------------------------------------------------
381 void wxMDIParentFrame::DoMenuUpdates(wxMenu
* menu
)
383 wxMDIChildFrame
*child
= GetActiveChild();
386 wxEvtHandler
* source
= child
->GetEventHandler();
387 wxMenuBar
* bar
= child
->GetMenuBar();
391 menu
->UpdateUI(source
);
397 int nCount
= bar
->GetMenuCount();
398 for (int n
= 0; n
< nCount
; n
++)
399 bar
->GetMenu(n
)->UpdateUI(source
);
405 wxFrameBase::DoMenuUpdates(menu
);
409 wxMenuItem
*wxMDIParentFrame::FindItemInMenuBar(int menuId
) const
411 wxMenuItem
*item
= wxFrame::FindItemInMenuBar(menuId
);
412 if ( !item
&& GetActiveChild() )
414 item
= GetActiveChild()->FindItemInMenuBar(menuId
);
417 if ( !item
&& m_windowMenu
)
418 item
= m_windowMenu
->FindItem(menuId
);
423 WXHMENU
wxMDIParentFrame::MSWGetActiveMenu() const
425 wxMDIChildFrame
* const child
= GetActiveChild();
428 const WXHMENU hmenu
= child
->MSWGetActiveMenu();
433 return wxFrame::MSWGetActiveMenu();
436 #endif // wxUSE_MENUS
438 // ----------------------------------------------------------------------------
439 // wxMDIParentFrame event handling
440 // ----------------------------------------------------------------------------
442 void wxMDIParentFrame::UpdateClientSize()
444 if ( GetClientWindow() )
447 GetClientSize(&width
, &height
);
449 GetClientWindow()->SetSize(0, 0, width
, height
);
453 void wxMDIParentFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
457 // do not call event.Skip() here, it somehow messes up MDI client window
460 void wxMDIParentFrame::OnIconized(wxIconizeEvent
& event
)
464 if ( !event
.IsIconized() )
468 // Responds to colour changes, and passes event on to children.
469 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
471 if ( m_clientWindow
)
473 m_clientWindow
->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
474 m_clientWindow
->Refresh();
480 WXHICON
wxMDIParentFrame::GetDefaultIcon() const
482 // we don't have any standard icons (any more)
486 // ---------------------------------------------------------------------------
488 // ---------------------------------------------------------------------------
490 void wxMDIParentFrame::Cascade()
492 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE
, 0, 0);
495 void wxMDIParentFrame::Tile(wxOrientation orient
)
497 wxASSERT_MSG( orient
== wxHORIZONTAL
|| orient
== wxVERTICAL
,
498 _T("invalid orientation value") );
500 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE
,
501 orient
== wxHORIZONTAL
? MDITILE_HORIZONTAL
502 : MDITILE_VERTICAL
, 0);
505 void wxMDIParentFrame::ArrangeIcons()
507 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE
, 0, 0);
510 void wxMDIParentFrame::ActivateNext()
512 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 0);
515 void wxMDIParentFrame::ActivatePrevious()
517 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 1);
520 // ---------------------------------------------------------------------------
521 // the MDI parent frame window proc
522 // ---------------------------------------------------------------------------
524 WXLRESULT
wxMDIParentFrame::MSWWindowProc(WXUINT message
,
529 bool processed
= false;
535 WXWORD state
, minimized
;
537 UnpackActivate(wParam
, lParam
, &state
, &minimized
, &hwnd
);
539 processed
= HandleActivate(state
, minimized
!= 0, hwnd
);
547 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
549 (void)HandleCommand(id
, cmd
, hwnd
);
551 // even if the frame didn't process it, there is no need to try it
552 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
553 // so pretend we processed the message anyhow
557 // always pass this message DefFrameProc(), otherwise MDI menu
558 // commands (and sys commands - more surprisingly!) won't work
559 MSWDefWindowProc(message
, wParam
, lParam
);
563 m_clientWindow
= OnCreateClient();
564 // Uses own style for client style
565 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
567 wxLogMessage(_("Failed to create MDI parent frame."));
578 // we erase background ourselves
583 // though we don't (usually) resize the MDI client to exactly fit the
584 // client area we need to pass this one to DefFrameProc to allow the children to show
589 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
594 bool wxMDIParentFrame::HandleActivate(int state
, bool minimized
, WXHWND activate
)
596 bool processed
= false;
598 if ( wxWindow::HandleActivate(state
, minimized
, activate
) )
604 // If this window is an MDI parent, we must also send an OnActivate message
605 // to the current child.
606 if ( GetActiveChild() &&
607 ((state
== WA_ACTIVE
) || (state
== WA_CLICKACTIVE
)) )
609 wxActivateEvent
event(wxEVT_ACTIVATE
, true, GetActiveChild()->GetId());
610 event
.SetEventObject( GetActiveChild() );
611 if ( GetActiveChild()->HandleWindowEvent(event
) )
620 void wxMDIParentFrame::OnMDIChild(wxCommandEvent
& event
)
622 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
625 wxWindow
*child
= node
->GetData();
626 if ( child
->GetHWND() )
628 int childId
= wxGetWindowId(child
->GetHWND());
629 if ( childId
== event
.GetId() )
631 ::SendMessage( GetWinHwnd(GetClientWindow()),
633 (WPARAM
)child
->GetHWND(), 0);
638 node
= node
->GetNext();
641 wxFAIL_MSG( "unknown MDI child selected?" );
644 void wxMDIParentFrame::OnMDICommand(wxCommandEvent
& event
)
649 switch ( event
.GetId() )
651 case wxID_MDI_WINDOW_CASCADE
:
653 wParam
= MDITILE_SKIPDISABLED
;
656 case wxID_MDI_WINDOW_TILE_HORZ
:
657 wParam
|= MDITILE_HORIZONTAL
;
660 case wxID_MDI_WINDOW_TILE_VERT
:
662 wParam
= MDITILE_VERTICAL
;
664 wParam
|= MDITILE_SKIPDISABLED
;
667 case wxID_MDI_WINDOW_ARRANGE_ICONS
:
668 msg
= WM_MDIICONARRANGE
;
671 case wxID_MDI_WINDOW_NEXT
:
673 lParam
= 0; // next child
676 case wxID_MDI_WINDOW_PREV
:
678 lParam
= 1; // previous child
682 wxFAIL_MSG( "unknown MDI command" );
686 ::SendMessage(GetWinHwnd(GetClientWindow()), msg
, wParam
, lParam
);
689 #endif // wxUSE_MENUS
691 bool wxMDIParentFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND hwnd
)
693 wxMDIChildFrame
* const child
= GetActiveChild();
694 if ( child
&& child
->HandleCommand(id
, cmd
, hwnd
) )
697 return wxFrame::HandleCommand(id
, cmd
, hwnd
);
700 WXLRESULT
wxMDIParentFrame::MSWDefWindowProc(WXUINT message
,
705 if ( GetClientWindow() )
706 clientWnd
= GetClientWindow()->GetHWND();
710 return DefFrameProc(GetHwnd(), (HWND
)clientWnd
, message
, wParam
, lParam
);
713 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG
* msg
)
715 MSG
*pMsg
= (MSG
*)msg
;
717 // first let the current child get it
718 wxMDIChildFrame
* const child
= GetActiveChild();
719 if ( child
&& child
->MSWTranslateMessage(msg
) )
724 // then try out accelerator table (will also check the accelerators for the
725 // normal menu items)
726 if ( wxFrame::MSWTranslateMessage(msg
) )
731 #if wxUSE_MENUS && wxUSE_ACCEL
732 // but it doesn't check for the (custom) accelerators of the window menu
733 // items as it's not part of the menu bar as it's handled by Windows itself
734 // so we need to do this explicitly
735 if ( m_accelWindowMenu
&& m_accelWindowMenu
->Translate(this, msg
) )
737 #endif // wxUSE_MENUS && wxUSE_ACCEL
739 // finally, check for MDI specific built-in accelerators
740 if ( pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
742 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg
))
749 // ===========================================================================
751 // ===========================================================================
753 void wxMDIChildFrame::Init()
755 m_needsResize
= true;
756 m_needsInitialShow
= true;
759 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
761 const wxString
& title
,
765 const wxString
& name
)
767 m_mdiParent
= parent
;
771 if ( id
!= wxID_ANY
)
774 m_windowId
= NewControlId();
778 parent
->AddChild(this);
789 wxApp::GetRegisteredClassName(_T("wxMDIChildFrame"), COLOR_WINDOW
);
790 if ( !(style
& wxFULL_REPAINT_ON_RESIZE
) )
791 className
+= wxApp::GetNoRedrawClassSuffix();
793 mcs
.szClass
= className
.wx_str();
794 mcs
.szTitle
= title
.wx_str();
795 mcs
.hOwner
= wxGetInstance();
796 if (x
!= wxDefaultCoord
)
799 mcs
.x
= CW_USEDEFAULT
;
801 if (y
!= wxDefaultCoord
)
804 mcs
.y
= CW_USEDEFAULT
;
806 if (width
!= wxDefaultCoord
)
809 mcs
.cx
= CW_USEDEFAULT
;
811 if (height
!= wxDefaultCoord
)
814 mcs
.cy
= CW_USEDEFAULT
;
816 DWORD msflags
= WS_OVERLAPPED
| WS_CLIPCHILDREN
;
817 if (style
& wxMINIMIZE_BOX
)
818 msflags
|= WS_MINIMIZEBOX
;
819 if (style
& wxMAXIMIZE_BOX
)
820 msflags
|= WS_MAXIMIZEBOX
;
821 if (style
& wxRESIZE_BORDER
)
822 msflags
|= WS_THICKFRAME
;
823 if (style
& wxSYSTEM_MENU
)
824 msflags
|= WS_SYSMENU
;
825 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
826 msflags
|= WS_MINIMIZE
;
827 if (style
& wxMAXIMIZE
)
828 msflags
|= WS_MAXIMIZE
;
829 if (style
& wxCAPTION
)
830 msflags
|= WS_CAPTION
;
836 wxWindowCreationHook
hook(this);
838 m_hWnd
= (WXHWND
)::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
839 WM_MDICREATE
, 0, (LPARAM
)&mcs
);
843 wxLogLastError(_T("WM_MDICREATE"));
849 parent
->AddMDIChild(this);
854 wxMDIChildFrame::~wxMDIChildFrame()
856 // if we hadn't been created, there is nothing to destroy
860 GetMDIParent()->RemoveMDIChild(this);
862 // will be destroyed by DestroyChildren() but reset them before calling it
863 // to avoid using dangling pointers if a callback comes in the meanwhile
865 m_frameToolBar
= NULL
;
868 m_frameStatusBar
= NULL
;
869 #endif // wxUSE_STATUSBAR
873 MDIRemoveWindowMenu(NULL
, m_hMenu
);
878 bool wxMDIChildFrame::Show(bool show
)
880 m_needsInitialShow
= false;
882 if (!wxFrame::Show(show
))
885 // KH: Without this call, new MDI children do not become active.
886 // This was added here after the same BringWindowToTop call was
887 // removed from wxTopLevelWindow::Show (November 2005)
889 ::BringWindowToTop(GetHwnd());
891 // we need to refresh the MDI frame window menu to include (or exclude if
892 // we've been hidden) this frame
893 wxMDIParentFrame
* const parent
= GetMDIParent();
894 MDISetMenu(parent
->GetClientWindow(), NULL
, NULL
);
899 // Set the client size (i.e. leave the calculation of borders etc.
901 void wxMDIChildFrame::DoSetClientSize(int width
, int height
)
903 HWND hWnd
= GetHwnd();
906 ::GetClientRect(hWnd
, &rect
);
909 GetWindowRect(hWnd
, &rect2
);
911 // Find the difference between the entire window (title bar and all)
912 // and the client area; add this to the new client size to move the
914 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
915 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
918 if (GetStatusBar() && GetStatusBar()->IsShown())
921 GetStatusBar()->GetSize(&sx
, &sy
);
924 #endif // wxUSE_STATUSBAR
927 point
.x
= rect2
.left
;
930 // If there's an MDI parent, must subtract the parent's top left corner
931 // since MoveWindow moves relative to the parent
932 wxMDIParentFrame
* const mdiParent
= GetMDIParent();
933 ::ScreenToClient(GetHwndOf(mdiParent
->GetClientWindow()), &point
);
935 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)true);
937 wxSize
size(width
, height
);
938 wxSizeEvent
event(size
, m_windowId
);
939 event
.SetEventObject( this );
940 HandleWindowEvent(event
);
943 // Unlike other wxTopLevelWindowBase, the mdi child's "GetPosition" is not the
944 // same as its GetScreenPosition
945 void wxMDIChildFrame::DoGetScreenPosition(int *x
, int *y
) const
947 HWND hWnd
= GetHwnd();
950 ::GetWindowRect(hWnd
, &rect
);
958 void wxMDIChildFrame::DoGetPosition(int *x
, int *y
) const
961 GetWindowRect(GetHwnd(), &rect
);
966 // Since we now have the absolute screen coords,
967 // if there's a parent we must subtract its top left corner
968 wxMDIParentFrame
* const mdiParent
= GetMDIParent();
969 ::ScreenToClient(GetHwndOf(mdiParent
->GetClientWindow()), &point
);
977 void wxMDIChildFrame::InternalSetMenuBar()
979 wxMDIParentFrame
* const parent
= GetMDIParent();
981 MDIInsertWindowMenu(parent
->GetClientWindow(),
982 m_hMenu
, GetMDIWindowMenu(parent
));
985 void wxMDIChildFrame::DetachMenuBar()
987 MDIRemoveWindowMenu(NULL
, m_hMenu
);
988 wxFrame::DetachMenuBar();
991 WXHICON
wxMDIChildFrame::GetDefaultIcon() const
993 // we don't have any standard icons (any more)
997 // ---------------------------------------------------------------------------
999 // ---------------------------------------------------------------------------
1001 void wxMDIChildFrame::Maximize(bool maximize
)
1003 wxMDIParentFrame
* const parent
= GetMDIParent();
1004 if ( parent
&& parent
->GetClientWindow() )
1006 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
1007 maximize
? WM_MDIMAXIMIZE
: WM_MDIRESTORE
,
1008 (WPARAM
)GetHwnd(), 0);
1012 void wxMDIChildFrame::Restore()
1014 wxMDIParentFrame
* const parent
= GetMDIParent();
1015 if ( parent
&& parent
->GetClientWindow() )
1017 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIRESTORE
,
1018 (WPARAM
) GetHwnd(), 0);
1022 void wxMDIChildFrame::Activate()
1024 wxMDIParentFrame
* const parent
= GetMDIParent();
1025 if ( parent
&& parent
->GetClientWindow() )
1027 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIACTIVATE
,
1028 (WPARAM
) GetHwnd(), 0);
1032 // ---------------------------------------------------------------------------
1033 // MDI window proc and message handlers
1034 // ---------------------------------------------------------------------------
1036 WXLRESULT
wxMDIChildFrame::MSWWindowProc(WXUINT message
,
1041 bool processed
= false;
1045 case WM_GETMINMAXINFO
:
1046 processed
= HandleGetMinMaxInfo((MINMAXINFO
*)lParam
);
1049 case WM_MDIACTIVATE
:
1052 WXHWND hwndAct
, hwndDeact
;
1053 UnpackMDIActivate(wParam
, lParam
, &act
, &hwndAct
, &hwndDeact
);
1055 processed
= HandleMDIActivate(act
, hwndAct
, hwndDeact
);
1060 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
1061 // scrollbars if necessary
1066 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
1068 MSWDefWindowProc(message
, wParam
, lParam
);
1072 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
1073 // the message (the base class version does not)
1074 return MSWDefWindowProc(message
, wParam
, lParam
);
1076 case WM_WINDOWPOSCHANGING
:
1077 processed
= HandleWindowPosChanging((LPWINDOWPOS
)lParam
);
1082 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
1087 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate
),
1091 wxMDIParentFrame
* const parent
= GetMDIParent();
1093 WXHMENU hMenuToSet
= 0;
1097 if ( m_hWnd
== hwndAct
)
1100 parent
->SetActiveChild(this);
1102 WXHMENU hMenuChild
= m_hMenu
;
1104 hMenuToSet
= hMenuChild
;
1106 else if ( m_hWnd
== hwndDeact
)
1108 wxASSERT_MSG( parent
->GetActiveChild() == this,
1109 wxT("can't deactivate MDI child which wasn't active!") );
1112 parent
->SetActiveChild(NULL
);
1114 WXHMENU hMenuParent
= parent
->m_hMenu
;
1116 // activate the the parent menu only when there is no other child
1117 // that has been activated
1118 if ( hMenuParent
&& !hwndAct
)
1119 hMenuToSet
= hMenuParent
;
1123 // we have nothing to do with it
1129 MDISetMenu(parent
->GetClientWindow(),
1130 (HMENU
)hMenuToSet
, GetMDIWindowMenu(parent
));
1133 wxActivateEvent
event(wxEVT_ACTIVATE
, activated
, m_windowId
);
1134 event
.SetEventObject( this );
1136 ResetWindowStyle(NULL
);
1138 return HandleWindowEvent(event
);
1141 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos
)
1143 WINDOWPOS
*lpPos
= (WINDOWPOS
*)pos
;
1145 if (!(lpPos
->flags
& SWP_NOSIZE
))
1148 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1149 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1150 if (ResetWindowStyle((void *) & rectClient
) && (dwStyle
& WS_MAXIMIZE
))
1152 ::AdjustWindowRectEx(&rectClient
, dwStyle
, false, dwExStyle
);
1153 lpPos
->x
= rectClient
.left
;
1154 lpPos
->y
= rectClient
.top
;
1155 lpPos
->cx
= rectClient
.right
- rectClient
.left
;
1156 lpPos
->cy
= rectClient
.bottom
- rectClient
.top
;
1163 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo
)
1165 MINMAXINFO
*info
= (MINMAXINFO
*)mmInfo
;
1167 // let the default window proc calculate the size of MDI children
1168 // frames because it is based on the size of the MDI client window,
1169 // not on the values specified in wxWindow m_max variables
1170 bool processed
= MSWDefWindowProc(WM_GETMINMAXINFO
, 0, (LPARAM
)mmInfo
) != 0;
1172 int minWidth
= GetMinWidth(),
1173 minHeight
= GetMinHeight();
1175 // but allow GetSizeHints() to set the min size
1176 if ( minWidth
!= wxDefaultCoord
)
1178 info
->ptMinTrackSize
.x
= minWidth
;
1183 if ( minHeight
!= wxDefaultCoord
)
1185 info
->ptMinTrackSize
.y
= minHeight
;
1193 // ---------------------------------------------------------------------------
1194 // MDI specific message translation/preprocessing
1195 // ---------------------------------------------------------------------------
1197 WXLRESULT
wxMDIChildFrame::MSWDefWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1199 return DefMDIChildProc(GetHwnd(),
1200 (UINT
)message
, (WPARAM
)wParam
, (LPARAM
)lParam
);
1203 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG
* msg
)
1205 // we must pass the parent frame to ::TranslateAccelerator(), otherwise it
1206 // doesn't do its job correctly for MDI child menus
1207 return MSWDoTranslateMessage(GetMDIParent(), msg
);
1210 // ---------------------------------------------------------------------------
1212 // ---------------------------------------------------------------------------
1214 void wxMDIChildFrame::MSWDestroyWindow()
1216 wxMDIParentFrame
* const parent
= GetMDIParent();
1218 // Must make sure this handle is invalidated (set to NULL) since all sorts
1219 // of things could happen after the child client is destroyed, but before
1220 // the wxFrame is destroyed.
1222 HWND oldHandle
= (HWND
)GetHWND();
1223 SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIDESTROY
,
1224 (WPARAM
)oldHandle
, 0);
1226 if (parent
->GetActiveChild() == NULL
)
1227 ResetWindowStyle(NULL
);
1231 ::DestroyMenu((HMENU
) m_hMenu
);
1234 wxRemoveHandleAssociation(this);
1238 // Change the client window's extended style so we don't get a client edge
1239 // style when a child is maximised (a double border looks silly.)
1240 bool wxMDIChildFrame::ResetWindowStyle(void *vrect
)
1242 RECT
*rect
= (RECT
*)vrect
;
1243 wxMDIParentFrame
* const pFrameWnd
= GetMDIParent();
1244 wxMDIChildFrame
* pChild
= pFrameWnd
->GetActiveChild();
1246 if (!pChild
|| (pChild
== this))
1248 HWND hwndClient
= GetWinHwnd(pFrameWnd
->GetClientWindow());
1249 DWORD dwStyle
= ::GetWindowLong(hwndClient
, GWL_EXSTYLE
);
1251 // we want to test whether there is a maximized child, so just set
1252 // dwThisStyle to 0 if there is no child at all
1253 DWORD dwThisStyle
= pChild
1254 ? ::GetWindowLong(GetWinHwnd(pChild
), GWL_STYLE
) : 0;
1255 DWORD dwNewStyle
= dwStyle
;
1256 if ( dwThisStyle
& WS_MAXIMIZE
)
1257 dwNewStyle
&= ~(WS_EX_CLIENTEDGE
);
1259 dwNewStyle
|= WS_EX_CLIENTEDGE
;
1261 if (dwStyle
!= dwNewStyle
)
1263 // force update of everything
1264 ::RedrawWindow(hwndClient
, NULL
, NULL
,
1265 RDW_INVALIDATE
| RDW_ALLCHILDREN
);
1266 ::SetWindowLong(hwndClient
, GWL_EXSTYLE
, dwNewStyle
);
1267 ::SetWindowPos(hwndClient
, NULL
, 0, 0, 0, 0,
1268 SWP_FRAMECHANGED
| SWP_NOACTIVATE
|
1269 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1272 ::GetClientRect(hwndClient
, rect
);
1281 // ===========================================================================
1282 // wxMDIClientWindow: the window of predefined (by Windows) class which
1283 // contains the child frames
1284 // ===========================================================================
1286 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
1288 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1290 CLIENTCREATESTRUCT ccs
;
1291 m_windowStyle
= style
;
1294 ccs
.hWindowMenu
= GetMDIWindowMenu(parent
);
1295 ccs
.idFirstChild
= wxFIRST_MDI_CHILD
;
1297 DWORD msStyle
= MDIS_ALLCHILDSTYLES
| WS_VISIBLE
| WS_CHILD
|
1298 WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
;
1300 if ( style
& wxHSCROLL
)
1301 msStyle
|= WS_HSCROLL
;
1302 if ( style
& wxVSCROLL
)
1303 msStyle
|= WS_VSCROLL
;
1305 DWORD exStyle
= WS_EX_CLIENTEDGE
;
1307 wxWindowCreationHook
hook(this);
1308 m_hWnd
= (WXHWND
)::CreateWindowEx
1318 (LPSTR
)(LPCLIENTCREATESTRUCT
)&ccs
);
1321 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1326 SubclassWin(m_hWnd
);
1331 // Explicitly call default scroll behaviour
1332 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)
1334 // Note: for client windows, the scroll position is not set in
1335 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1336 // scroll position we're at.
1337 // This makes it hard to paint patterns or bitmaps in the background,
1338 // and have the client area scrollable as well.
1340 if ( event
.GetOrientation() == wxHORIZONTAL
)
1341 m_scrollX
= event
.GetPosition(); // Always returns zero!
1343 m_scrollY
= event
.GetPosition(); // Always returns zero!
1348 void wxMDIClientWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1350 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1351 // client area, you can end up with a non-refreshed portion in the client window
1352 // (see OGL studio sample). So check if the position is changed and if so,
1353 // redraw the MDI child frames.
1355 const wxPoint oldPos
= GetPosition();
1357 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
| wxSIZE_FORCE
);
1359 const wxPoint newPos
= GetPosition();
1361 if ((newPos
.x
!= oldPos
.x
) || (newPos
.y
!= oldPos
.y
))
1365 wxWindowList::compatibility_iterator node
= GetParent()->GetChildren().GetFirst();
1368 wxWindow
*child
= node
->GetData();
1369 if (child
->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
1371 ::RedrawWindow(GetHwndOf(child
),
1378 node
= node
->GetNext();
1384 void wxMDIChildFrame::OnIdle(wxIdleEvent
& event
)
1386 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1387 // in flicker e.g. when the frame contained controls with non-trivial
1388 // layout. Since 2.5.3, the frame is created hidden as all other top level
1389 // windows. In order to maintain backward compatibility, the frame is shown
1390 // in OnIdle, unless Show(false) was called by the programmer before.
1391 if ( m_needsInitialShow
)
1396 // MDI child frames get their WM_SIZE when they're constructed but at this
1397 // moment they don't have any children yet so all child windows will be
1398 // positioned incorrectly when they are added later - to fix this, we
1399 // generate an artificial size event here
1400 if ( m_needsResize
)
1402 m_needsResize
= false; // avoid any possibility of recursion
1410 // ---------------------------------------------------------------------------
1411 // private helper functions
1412 // ---------------------------------------------------------------------------
1417 void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
)
1419 if ( hmenuFrame
|| hmenuWindow
)
1421 if ( !::SendMessage(GetWinHwnd(win
),
1424 (LPARAM
)hmenuWindow
) )
1427 DWORD err
= ::GetLastError();
1429 wxLogApiError(_T("SendMessage(WM_MDISETMENU)"), err
);
1430 #endif // __WXDEBUG__
1434 // update menu bar of the parent window
1435 wxWindow
*parent
= win
->GetParent();
1436 wxCHECK_RET( parent
, wxT("MDI client without parent frame? weird...") );
1438 ::SendMessage(GetWinHwnd(win
), WM_MDIREFRESHMENU
, 0, 0L);
1440 ::DrawMenuBar(GetWinHwnd(parent
));
1443 void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU hMenu
, HMENU menuWin
)
1445 HMENU hmenu
= (HMENU
)hMenu
;
1449 // Try to insert Window menu in front of Help, otherwise append it.
1450 int N
= GetMenuItemCount(hmenu
);
1451 bool inserted
= false;
1452 for ( int i
= 0; i
< N
; i
++ )
1455 if ( !::GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1457 wxLogLastError(wxT("GetMenuString"));
1462 const wxString label
= wxStripMenuCodes(buf
);
1463 if ( label
== wxGetStockLabel(wxID_HELP
, wxSTOCK_NOFLAGS
) )
1466 ::InsertMenu(hmenu
, i
, MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1468 wxGetTranslation(WINDOW_MENU_LABEL
).wx_str());
1475 ::AppendMenu(hmenu
, MF_POPUP
,
1477 wxGetTranslation(WINDOW_MENU_LABEL
).wx_str());
1481 MDISetMenu(win
, hmenu
, menuWin
);
1484 void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU hMenu
)
1486 HMENU hmenu
= (HMENU
)hMenu
;
1492 int N
= ::GetMenuItemCount(hmenu
);
1493 for ( int i
= 0; i
< N
; i
++ )
1495 if ( !::GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1497 // Ignore successful read of menu string with length 0 which
1498 // occurs, for example, for a maximized MDI child system menu
1499 if ( ::GetLastError() != 0 )
1501 wxLogLastError(wxT("GetMenuString"));
1507 if ( wxStrcmp(buf
, wxGetTranslation(WINDOW_MENU_LABEL
)) == 0 )
1509 if ( !::RemoveMenu(hmenu
, i
, MF_BYPOSITION
) )
1511 wxLogLastError(wxT("RemoveMenu"));
1521 // we don't change the windows menu, but we update the main one
1522 MDISetMenu(win
, hmenu
, NULL
);
1526 void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
1527 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
)
1530 *hwndAct
= (WXHWND
)lParam
;
1531 *hwndDeact
= (WXHWND
)wParam
;
1534 } // anonymous namespace
1536 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)