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
);
58 // ---------------------------------------------------------------------------
60 // ---------------------------------------------------------------------------
62 static const int IDM_WINDOWTILEHOR
= 4001;
63 static const int IDM_WINDOWCASCADE
= 4002;
64 static const int IDM_WINDOWICONS
= 4003;
65 static const int IDM_WINDOWNEXT
= 4004;
66 static const int IDM_WINDOWTILEVERT
= 4005;
67 static const int IDM_WINDOWPREV
= 4006;
69 // This range gives a maximum of 500 MDI children. Should be enough :-)
70 static const int wxFIRST_MDI_CHILD
= 4100;
71 static const int wxLAST_MDI_CHILD
= 4600;
73 // ---------------------------------------------------------------------------
75 // ---------------------------------------------------------------------------
77 // set the MDI menus (by sending the WM_MDISETMENU message) and update the menu
78 // of the parent of win (which is supposed to be the MDI client window)
79 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
);
81 // insert the window menu (subMenu) into menu just before "Help" submenu or at
82 // the very end if not found
83 static void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
);
85 // Remove the window menu
86 static void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU menu
);
88 // is this an id of an MDI child?
89 inline bool IsMdiCommandId(int id
)
91 return (id
>= wxFIRST_MDI_CHILD
) && (id
<= wxLAST_MDI_CHILD
);
94 // unpack the parameters of WM_MDIACTIVATE message
95 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
96 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
);
98 // return the HMENU of the MDI menu
100 // this function works correctly even when we don't have a window menu and just
102 static inline HMENU
GetMDIWindowMenu(wxMDIParentFrame
*frame
)
104 wxMenu
*menu
= frame
->GetWindowMenu();
105 return menu
? GetHmenuOf(menu
) : 0;
108 // ===========================================================================
110 // ===========================================================================
112 // ---------------------------------------------------------------------------
114 // ---------------------------------------------------------------------------
116 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxFrame
)
117 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxFrame
)
118 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxWindow
)
120 BEGIN_EVENT_TABLE(wxMDIParentFrame
, wxFrame
)
121 EVT_SIZE(wxMDIParentFrame::OnSize
)
122 EVT_ICONIZE(wxMDIParentFrame::OnIconized
)
123 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged
)
126 BEGIN_EVENT_TABLE(wxMDIChildFrame
, wxFrame
)
127 EVT_IDLE(wxMDIChildFrame::OnIdle
)
130 BEGIN_EVENT_TABLE(wxMDIClientWindow
, wxWindow
)
131 EVT_SCROLL(wxMDIClientWindow::OnScroll
)
134 // ===========================================================================
135 // wxMDIParentFrame: the frame which contains the client window which manages
137 // ===========================================================================
139 wxMDIParentFrame::wxMDIParentFrame()
141 m_parentFrameActive
= true;
144 bool wxMDIParentFrame::Create(wxWindow
*parent
,
146 const wxString
& title
,
150 const wxString
& name
)
152 // this style can be used to prevent a window from having the standard MDI
154 if ( !(style
& wxFRAME_NO_WINDOW_MENU
) )
156 // normal case: we have the window menu, so construct it
157 m_windowMenu
= new wxMenu
;
159 m_windowMenu
->Append(IDM_WINDOWCASCADE
, _("&Cascade"));
160 m_windowMenu
->Append(IDM_WINDOWTILEHOR
, _("Tile &Horizontally"));
161 m_windowMenu
->Append(IDM_WINDOWTILEVERT
, _("Tile &Vertically"));
162 m_windowMenu
->AppendSeparator();
163 m_windowMenu
->Append(IDM_WINDOWICONS
, _("&Arrange Icons"));
164 m_windowMenu
->Append(IDM_WINDOWNEXT
, _("&Next"));
165 m_windowMenu
->Append(IDM_WINDOWPREV
, _("&Previous"));
168 m_parentFrameActive
= true;
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
218 // the MDI frame menubar is not automatically deleted by Windows unlike for
221 ::DestroyMenu((HMENU
)m_hMenu
);
223 if ( m_clientWindow
)
225 if ( m_clientWindow
->MSWGetOldWndProc() )
226 m_clientWindow
->UnsubclassWin();
228 m_clientWindow
->SetHWND(0);
229 delete m_clientWindow
;
233 // ----------------------------------------------------------------------------
234 // wxMDIParentFrame child management
235 // ----------------------------------------------------------------------------
237 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
239 HWND hWnd
= (HWND
)::SendMessage(GetWinHwnd(GetClientWindow()),
240 WM_MDIGETACTIVE
, 0, 0L);
244 return (wxMDIChildFrame
*)wxFindWinFromHandle(hWnd
);
247 int wxMDIParentFrame::GetChildFramesCount() const
250 for ( wxWindowList::const_iterator i
= GetChildren().begin();
251 i
!= GetChildren().end();
254 if ( wxDynamicCast(*i
, wxMDIChildFrame
) )
263 void wxMDIParentFrame::AddMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
265 switch ( GetChildFramesCount() )
268 // first MDI child was just added, we need to insert the window
269 // menu now if we have it
272 // and disable the items which can't be used until we have more
274 UpdateWindowMenu(false);
278 // second MDI child was added, enable the menu items which were
279 // disabled because they didn't make sense for a single window
280 UpdateWindowMenu(true);
285 void wxMDIParentFrame::RemoveMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
287 switch ( GetChildFramesCount() )
290 // last MDI child is being removed, remove the now unnecessary
294 // there is no need to call UpdateWindowMenu(true) here so this is
295 // not quite symmetric to AddMDIChild() above
299 // only one MDI child is going to remain, disable the menu commands
300 // which don't make sense for a single child window
301 UpdateWindowMenu(false);
306 // ----------------------------------------------------------------------------
307 // wxMDIParentFrame window menu handling
308 // ----------------------------------------------------------------------------
310 void wxMDIParentFrame::AddWindowMenu()
313 MDIInsertWindowMenu(GetClientWindow(), m_hMenu
, GetMDIWindowMenu(this));
316 void wxMDIParentFrame::RemoveWindowMenu()
319 MDIRemoveWindowMenu(GetClientWindow(), m_hMenu
);
322 void wxMDIParentFrame::UpdateWindowMenu(bool enable
)
326 m_windowMenu
->Enable(IDM_WINDOWNEXT
, enable
);
327 m_windowMenu
->Enable(IDM_WINDOWPREV
, enable
);
331 #if wxUSE_MENUS_NATIVE
333 void wxMDIParentFrame::InternalSetMenuBar()
335 m_parentFrameActive
= true;
337 if ( GetActiveChild() )
341 else // we don't have any MDI children yet
343 // wait until we do to add the window menu but do set the main menu for
344 // now (this is done by AddWindowMenu() as a side effect)
345 MDISetMenu(GetClientWindow(), (HMENU
)m_hMenu
, NULL
);
349 #endif // wxUSE_MENUS_NATIVE
351 void wxMDIParentFrame::SetWindowMenu(wxMenu
* menu
)
353 // notice that Remove/AddWindowMenu() are safe to call even when
354 // m_windowMenu is NULL
364 // ----------------------------------------------------------------------------
365 // wxMDIParentFrame other menu-related stuff
366 // ----------------------------------------------------------------------------
368 void wxMDIParentFrame::DoMenuUpdates(wxMenu
* menu
)
370 wxMDIChildFrame
*child
= GetActiveChild();
373 wxEvtHandler
* source
= child
->GetEventHandler();
374 wxMenuBar
* bar
= child
->GetMenuBar();
378 menu
->UpdateUI(source
);
384 int nCount
= bar
->GetMenuCount();
385 for (int n
= 0; n
< nCount
; n
++)
386 bar
->GetMenu(n
)->UpdateUI(source
);
392 wxFrameBase::DoMenuUpdates(menu
);
396 const wxMenuItem
*wxMDIParentFrame::FindItemInMenuBar(int menuId
) const
398 const wxMenuItem
*item
= wxFrame::FindItemInMenuBar(menuId
);
399 if ( !item
&& m_currentChild
)
401 item
= m_currentChild
->FindItemInMenuBar(menuId
);
407 WXHMENU
wxMDIParentFrame::MSWGetActiveMenu() const
409 wxMDIChildFrame
* const child
= GetActiveChild();
412 const WXHMENU hmenu
= child
->MSWGetActiveMenu();
417 return wxFrame::MSWGetActiveMenu();
420 #endif // wxUSE_MENUS
422 // ----------------------------------------------------------------------------
423 // wxMDIParentFrame event handling
424 // ----------------------------------------------------------------------------
426 void wxMDIParentFrame::UpdateClientSize()
428 if ( GetClientWindow() )
431 GetClientSize(&width
, &height
);
433 GetClientWindow()->SetSize(0, 0, width
, height
);
437 void wxMDIParentFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
441 // do not call event.Skip() here, it somehow messes up MDI client window
444 void wxMDIParentFrame::OnIconized(wxIconizeEvent
& event
)
448 if ( !event
.IsIconized() )
452 // Responds to colour changes, and passes event on to children.
453 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
455 if ( m_clientWindow
)
457 m_clientWindow
->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
458 m_clientWindow
->Refresh();
464 WXHICON
wxMDIParentFrame::GetDefaultIcon() const
466 // we don't have any standard icons (any more)
470 // ---------------------------------------------------------------------------
472 // ---------------------------------------------------------------------------
474 void wxMDIParentFrame::Cascade()
476 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE
, 0, 0);
479 void wxMDIParentFrame::Tile(wxOrientation orient
)
481 wxASSERT_MSG( orient
== wxHORIZONTAL
|| orient
== wxVERTICAL
,
482 _T("invalid orientation value") );
484 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE
,
485 orient
== wxHORIZONTAL
? MDITILE_HORIZONTAL
486 : MDITILE_VERTICAL
, 0);
489 void wxMDIParentFrame::ArrangeIcons()
491 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE
, 0, 0);
494 void wxMDIParentFrame::ActivateNext()
496 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 0);
499 void wxMDIParentFrame::ActivatePrevious()
501 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 1);
504 // ---------------------------------------------------------------------------
505 // the MDI parent frame window proc
506 // ---------------------------------------------------------------------------
508 WXLRESULT
wxMDIParentFrame::MSWWindowProc(WXUINT message
,
513 bool processed
= false;
519 WXWORD state
, minimized
;
521 UnpackActivate(wParam
, lParam
, &state
, &minimized
, &hwnd
);
523 processed
= HandleActivate(state
, minimized
!= 0, hwnd
);
531 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
533 (void)HandleCommand(id
, cmd
, hwnd
);
535 // even if the frame didn't process it, there is no need to try it
536 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
537 // so pretend we processed the message anyhow
541 // always pass this message DefFrameProc(), otherwise MDI menu
542 // commands (and sys commands - more surprisingly!) won't work
543 MSWDefWindowProc(message
, wParam
, lParam
);
547 m_clientWindow
= OnCreateClient();
548 // Uses own style for client style
549 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
551 wxLogMessage(_("Failed to create MDI parent frame."));
562 // we erase background ourselves
567 // though we don't (usually) resize the MDI client to exactly fit the
568 // client area we need to pass this one to DefFrameProc to allow the children to show
573 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
578 bool wxMDIParentFrame::HandleActivate(int state
, bool minimized
, WXHWND activate
)
580 bool processed
= false;
582 if ( wxWindow::HandleActivate(state
, minimized
, activate
) )
588 // If this window is an MDI parent, we must also send an OnActivate message
589 // to the current child.
590 if ( (m_currentChild
!= NULL
) &&
591 ((state
== WA_ACTIVE
) || (state
== WA_CLICKACTIVE
)) )
593 wxActivateEvent
event(wxEVT_ACTIVATE
, true, m_currentChild
->GetId());
594 event
.SetEventObject( m_currentChild
);
595 if ( m_currentChild
->HandleWindowEvent(event
) )
602 bool wxMDIParentFrame::HandleCommand(WXWORD id_
, WXWORD cmd
, WXHWND hwnd
)
604 // sign extend to int from short before comparing with the other int ids
605 int id
= (signed short)id_
;
607 // In case it's e.g. a toolbar.
610 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
612 return win
->MSWCommand(cmd
, id
);
615 if (wxCurrentPopupMenu
)
617 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
618 wxCurrentPopupMenu
= NULL
;
619 if (popupMenu
->MSWCommand(cmd
, id
))
623 // is it one of standard MDI commands?
629 case IDM_WINDOWCASCADE
:
631 wParam
= MDITILE_SKIPDISABLED
;
634 case IDM_WINDOWTILEHOR
:
635 wParam
|= MDITILE_HORIZONTAL
;
638 case IDM_WINDOWTILEVERT
:
640 wParam
= MDITILE_VERTICAL
;
642 wParam
|= MDITILE_SKIPDISABLED
;
645 case IDM_WINDOWICONS
:
646 msg
= WM_MDIICONARRANGE
;
651 lParam
= 0; // next child
656 lParam
= 1; // previous child
665 ::SendMessage(GetWinHwnd(GetClientWindow()), msg
, wParam
, lParam
);
670 // FIXME VZ: what does this test do??
673 return false; // Get WndProc to call default proc
676 if ( IsMdiCommandId(id
) )
678 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
681 wxWindow
*child
= node
->GetData();
682 if ( child
->GetHWND() )
684 int childId
= wxGetWindowId(child
->GetHWND());
685 if ( childId
== (signed short)id
)
687 ::SendMessage( GetWinHwnd(GetClientWindow()),
689 (WPARAM
)child
->GetHWND(), 0);
693 node
= node
->GetNext();
696 else if ( m_parentFrameActive
)
698 return ProcessCommand(id
);
700 else if ( m_currentChild
)
702 return m_currentChild
->HandleCommand(id
, cmd
, hwnd
);
706 // this shouldn't happen because it means that our messages are being
707 // lost (they're not sent to the parent frame nor to the children)
708 wxFAIL_MSG(wxT("MDI parent frame is not active, yet there is no active MDI child?"));
714 WXLRESULT
wxMDIParentFrame::MSWDefWindowProc(WXUINT message
,
719 if ( GetClientWindow() )
720 clientWnd
= GetClientWindow()->GetHWND();
724 return DefFrameProc(GetHwnd(), (HWND
)clientWnd
, message
, wParam
, lParam
);
727 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG
* msg
)
729 MSG
*pMsg
= (MSG
*)msg
;
731 // first let the current child get it
732 if ( m_currentChild
&& m_currentChild
->GetHWND() &&
733 m_currentChild
->MSWTranslateMessage(msg
) )
738 // then try out accel table (will also check the menu accels)
739 if ( wxFrame::MSWTranslateMessage(msg
) )
744 // finally, check for MDI specific built in accel keys
745 if ( pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
747 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg
))
754 // ===========================================================================
756 // ===========================================================================
758 void wxMDIChildFrame::Init()
760 m_needsResize
= true;
761 m_needsInitialShow
= true;
764 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
766 const wxString
& title
,
770 const wxString
& name
)
772 m_mdiParent
= parent
;
776 if ( id
!= wxID_ANY
)
779 m_windowId
= NewControlId();
783 parent
->AddChild(this);
794 wxApp::GetRegisteredClassName(_T("wxMDIChildFrame"), COLOR_WINDOW
);
795 if ( !(style
& wxFULL_REPAINT_ON_RESIZE
) )
796 className
+= wxApp::GetNoRedrawClassSuffix();
798 mcs
.szClass
= className
.wx_str();
799 mcs
.szTitle
= title
.wx_str();
800 mcs
.hOwner
= wxGetInstance();
801 if (x
!= wxDefaultCoord
)
804 mcs
.x
= CW_USEDEFAULT
;
806 if (y
!= wxDefaultCoord
)
809 mcs
.y
= CW_USEDEFAULT
;
811 if (width
!= wxDefaultCoord
)
814 mcs
.cx
= CW_USEDEFAULT
;
816 if (height
!= wxDefaultCoord
)
819 mcs
.cy
= CW_USEDEFAULT
;
821 DWORD msflags
= WS_OVERLAPPED
| WS_CLIPCHILDREN
;
822 if (style
& wxMINIMIZE_BOX
)
823 msflags
|= WS_MINIMIZEBOX
;
824 if (style
& wxMAXIMIZE_BOX
)
825 msflags
|= WS_MAXIMIZEBOX
;
826 if (style
& wxRESIZE_BORDER
)
827 msflags
|= WS_THICKFRAME
;
828 if (style
& wxSYSTEM_MENU
)
829 msflags
|= WS_SYSMENU
;
830 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
831 msflags
|= WS_MINIMIZE
;
832 if (style
& wxMAXIMIZE
)
833 msflags
|= WS_MAXIMIZE
;
834 if (style
& wxCAPTION
)
835 msflags
|= WS_CAPTION
;
841 wxWindowCreationHook
hook(this);
843 m_hWnd
= (WXHWND
)::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
844 WM_MDICREATE
, 0, (LPARAM
)&mcs
);
848 wxLogLastError(_T("WM_MDICREATE"));
854 parent
->AddMDIChild(this);
859 wxMDIChildFrame::~wxMDIChildFrame()
861 // if we hadn't been created, there is nothing to destroy
865 GetMDIParent()->RemoveMDIChild(this);
867 // will be destroyed by DestroyChildren() but reset them before calling it
868 // to avoid using dangling pointers if a callback comes in the meanwhile
870 m_frameToolBar
= NULL
;
873 m_frameStatusBar
= NULL
;
874 #endif // wxUSE_STATUSBAR
878 MDIRemoveWindowMenu(NULL
, m_hMenu
);
883 bool wxMDIChildFrame::Show(bool show
)
885 m_needsInitialShow
= false;
887 if (!wxFrame::Show(show
))
890 // KH: Without this call, new MDI children do not become active.
891 // This was added here after the same BringWindowToTop call was
892 // removed from wxTopLevelWindow::Show (November 2005)
894 ::BringWindowToTop(GetHwnd());
896 // we need to refresh the MDI frame window menu to include (or exclude if
897 // we've been hidden) this frame
898 wxMDIParentFrame
* const parent
= GetMDIParent();
899 MDISetMenu(parent
->GetClientWindow(), NULL
, NULL
);
904 // Set the client size (i.e. leave the calculation of borders etc.
906 void wxMDIChildFrame::DoSetClientSize(int width
, int height
)
908 HWND hWnd
= GetHwnd();
911 ::GetClientRect(hWnd
, &rect
);
914 GetWindowRect(hWnd
, &rect2
);
916 // Find the difference between the entire window (title bar and all)
917 // and the client area; add this to the new client size to move the
919 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
920 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
923 if (GetStatusBar() && GetStatusBar()->IsShown())
926 GetStatusBar()->GetSize(&sx
, &sy
);
929 #endif // wxUSE_STATUSBAR
932 point
.x
= rect2
.left
;
935 // If there's an MDI parent, must subtract the parent's top left corner
936 // since MoveWindow moves relative to the parent
937 wxMDIParentFrame
* const mdiParent
= GetMDIParent();
938 ::ScreenToClient(GetHwndOf(mdiParent
->GetClientWindow()), &point
);
940 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)true);
942 wxSize
size(width
, height
);
943 wxSizeEvent
event(size
, m_windowId
);
944 event
.SetEventObject( this );
945 HandleWindowEvent(event
);
948 // Unlike other wxTopLevelWindowBase, the mdi child's "GetPosition" is not the
949 // same as its GetScreenPosition
950 void wxMDIChildFrame::DoGetScreenPosition(int *x
, int *y
) const
952 HWND hWnd
= GetHwnd();
955 ::GetWindowRect(hWnd
, &rect
);
963 void wxMDIChildFrame::DoGetPosition(int *x
, int *y
) const
966 GetWindowRect(GetHwnd(), &rect
);
971 // Since we now have the absolute screen coords,
972 // if there's a parent we must subtract its top left corner
973 wxMDIParentFrame
* const mdiParent
= GetMDIParent();
974 ::ScreenToClient(GetHwndOf(mdiParent
->GetClientWindow()), &point
);
982 void wxMDIChildFrame::InternalSetMenuBar()
984 wxMDIParentFrame
* const parent
= GetMDIParent();
986 MDIInsertWindowMenu(parent
->GetClientWindow(),
987 m_hMenu
, GetMDIWindowMenu(parent
));
989 parent
->m_parentFrameActive
= false;
992 void wxMDIChildFrame::DetachMenuBar()
994 MDIRemoveWindowMenu(NULL
, m_hMenu
);
995 wxFrame::DetachMenuBar();
998 WXHICON
wxMDIChildFrame::GetDefaultIcon() const
1000 // we don't have any standard icons (any more)
1004 // ---------------------------------------------------------------------------
1006 // ---------------------------------------------------------------------------
1008 void wxMDIChildFrame::Maximize(bool maximize
)
1010 wxMDIParentFrame
* const parent
= GetMDIParent();
1011 if ( parent
&& parent
->GetClientWindow() )
1013 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
1014 maximize
? WM_MDIMAXIMIZE
: WM_MDIRESTORE
,
1015 (WPARAM
)GetHwnd(), 0);
1019 void wxMDIChildFrame::Restore()
1021 wxMDIParentFrame
* const parent
= GetMDIParent();
1022 if ( parent
&& parent
->GetClientWindow() )
1024 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIRESTORE
,
1025 (WPARAM
) GetHwnd(), 0);
1029 void wxMDIChildFrame::Activate()
1031 wxMDIParentFrame
* const parent
= GetMDIParent();
1032 if ( parent
&& parent
->GetClientWindow() )
1034 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIACTIVATE
,
1035 (WPARAM
) GetHwnd(), 0);
1039 // ---------------------------------------------------------------------------
1040 // MDI window proc and message handlers
1041 // ---------------------------------------------------------------------------
1043 WXLRESULT
wxMDIChildFrame::MSWWindowProc(WXUINT message
,
1048 bool processed
= false;
1056 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
1059 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
1063 case WM_GETMINMAXINFO
:
1064 processed
= HandleGetMinMaxInfo((MINMAXINFO
*)lParam
);
1067 case WM_MDIACTIVATE
:
1070 WXHWND hwndAct
, hwndDeact
;
1071 UnpackMDIActivate(wParam
, lParam
, &act
, &hwndAct
, &hwndDeact
);
1073 processed
= HandleMDIActivate(act
, hwndAct
, hwndDeact
);
1078 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
1079 // scrollbars if necessary
1084 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
1086 MSWDefWindowProc(message
, wParam
, lParam
);
1090 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
1091 // the message (the base class version does not)
1092 return MSWDefWindowProc(message
, wParam
, lParam
);
1094 case WM_WINDOWPOSCHANGING
:
1095 processed
= HandleWindowPosChanging((LPWINDOWPOS
)lParam
);
1100 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
1105 bool wxMDIChildFrame::HandleCommand(WXWORD id_
, WXWORD cmd
, WXHWND hwnd
)
1107 // sign extend to int from short before comparing with the other int ids
1108 int id
= (signed short)id_
;
1110 // In case it's e.g. a toolbar.
1113 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
1115 return win
->MSWCommand(cmd
, id
);
1118 if (wxCurrentPopupMenu
)
1120 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
1121 wxCurrentPopupMenu
= NULL
;
1122 if (popupMenu
->MSWCommand(cmd
, id
))
1127 if (GetMenuBar() && GetMenuBar()->FindItem(id
))
1129 processed
= ProcessCommand(id
);
1139 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate
),
1143 wxMDIParentFrame
* const parent
= GetMDIParent();
1145 WXHMENU hMenuToSet
= 0;
1149 if ( m_hWnd
== hwndAct
)
1152 parent
->m_currentChild
= this;
1154 WXHMENU hMenuChild
= m_hMenu
;
1157 parent
->m_parentFrameActive
= false;
1159 hMenuToSet
= hMenuChild
;
1162 else if ( m_hWnd
== hwndDeact
)
1164 wxASSERT_MSG( parent
->m_currentChild
== this,
1165 wxT("can't deactivate MDI child which wasn't active!") );
1168 parent
->m_currentChild
= NULL
;
1170 WXHMENU hMenuParent
= parent
->m_hMenu
;
1172 // activate the the parent menu only when there is no other child
1173 // that has been activated
1174 if ( hMenuParent
&& !hwndAct
)
1176 parent
->m_parentFrameActive
= true;
1178 hMenuToSet
= hMenuParent
;
1183 // we have nothing to do with it
1189 MDISetMenu(parent
->GetClientWindow(),
1190 (HMENU
)hMenuToSet
, GetMDIWindowMenu(parent
));
1193 wxActivateEvent
event(wxEVT_ACTIVATE
, activated
, m_windowId
);
1194 event
.SetEventObject( this );
1196 ResetWindowStyle(NULL
);
1198 return HandleWindowEvent(event
);
1201 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos
)
1203 WINDOWPOS
*lpPos
= (WINDOWPOS
*)pos
;
1205 if (!(lpPos
->flags
& SWP_NOSIZE
))
1208 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1209 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1210 if (ResetWindowStyle((void *) & rectClient
) && (dwStyle
& WS_MAXIMIZE
))
1212 ::AdjustWindowRectEx(&rectClient
, dwStyle
, false, dwExStyle
);
1213 lpPos
->x
= rectClient
.left
;
1214 lpPos
->y
= rectClient
.top
;
1215 lpPos
->cx
= rectClient
.right
- rectClient
.left
;
1216 lpPos
->cy
= rectClient
.bottom
- rectClient
.top
;
1223 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo
)
1225 MINMAXINFO
*info
= (MINMAXINFO
*)mmInfo
;
1227 // let the default window proc calculate the size of MDI children
1228 // frames because it is based on the size of the MDI client window,
1229 // not on the values specified in wxWindow m_max variables
1230 bool processed
= MSWDefWindowProc(WM_GETMINMAXINFO
, 0, (LPARAM
)mmInfo
) != 0;
1232 int minWidth
= GetMinWidth(),
1233 minHeight
= GetMinHeight();
1235 // but allow GetSizeHints() to set the min size
1236 if ( minWidth
!= wxDefaultCoord
)
1238 info
->ptMinTrackSize
.x
= minWidth
;
1243 if ( minHeight
!= wxDefaultCoord
)
1245 info
->ptMinTrackSize
.y
= minHeight
;
1253 // ---------------------------------------------------------------------------
1254 // MDI specific message translation/preprocessing
1255 // ---------------------------------------------------------------------------
1257 WXLRESULT
wxMDIChildFrame::MSWDefWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1259 return DefMDIChildProc(GetHwnd(),
1260 (UINT
)message
, (WPARAM
)wParam
, (LPARAM
)lParam
);
1263 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG
* msg
)
1265 // we must pass the parent frame to ::TranslateAccelerator(), otherwise it
1266 // doesn't do its job correctly for MDI child menus
1267 return MSWDoTranslateMessage(GetMDIParent(), msg
);
1270 // ---------------------------------------------------------------------------
1272 // ---------------------------------------------------------------------------
1274 void wxMDIChildFrame::MSWDestroyWindow()
1276 wxMDIParentFrame
* const parent
= GetMDIParent();
1278 // Must make sure this handle is invalidated (set to NULL) since all sorts
1279 // of things could happen after the child client is destroyed, but before
1280 // the wxFrame is destroyed.
1282 HWND oldHandle
= (HWND
)GetHWND();
1283 SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIDESTROY
,
1284 (WPARAM
)oldHandle
, 0);
1286 if (parent
->GetActiveChild() == NULL
)
1287 ResetWindowStyle(NULL
);
1291 ::DestroyMenu((HMENU
) m_hMenu
);
1294 wxRemoveHandleAssociation(this);
1298 // Change the client window's extended style so we don't get a client edge
1299 // style when a child is maximised (a double border looks silly.)
1300 bool wxMDIChildFrame::ResetWindowStyle(void *vrect
)
1302 RECT
*rect
= (RECT
*)vrect
;
1303 wxMDIParentFrame
* const pFrameWnd
= GetMDIParent();
1304 wxMDIChildFrame
* pChild
= pFrameWnd
->GetActiveChild();
1306 if (!pChild
|| (pChild
== this))
1308 HWND hwndClient
= GetWinHwnd(pFrameWnd
->GetClientWindow());
1309 DWORD dwStyle
= ::GetWindowLong(hwndClient
, GWL_EXSTYLE
);
1311 // we want to test whether there is a maximized child, so just set
1312 // dwThisStyle to 0 if there is no child at all
1313 DWORD dwThisStyle
= pChild
1314 ? ::GetWindowLong(GetWinHwnd(pChild
), GWL_STYLE
) : 0;
1315 DWORD dwNewStyle
= dwStyle
;
1316 if ( dwThisStyle
& WS_MAXIMIZE
)
1317 dwNewStyle
&= ~(WS_EX_CLIENTEDGE
);
1319 dwNewStyle
|= WS_EX_CLIENTEDGE
;
1321 if (dwStyle
!= dwNewStyle
)
1323 // force update of everything
1324 ::RedrawWindow(hwndClient
, NULL
, NULL
,
1325 RDW_INVALIDATE
| RDW_ALLCHILDREN
);
1326 ::SetWindowLong(hwndClient
, GWL_EXSTYLE
, dwNewStyle
);
1327 ::SetWindowPos(hwndClient
, NULL
, 0, 0, 0, 0,
1328 SWP_FRAMECHANGED
| SWP_NOACTIVATE
|
1329 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1332 ::GetClientRect(hwndClient
, rect
);
1341 // ===========================================================================
1342 // wxMDIClientWindow: the window of predefined (by Windows) class which
1343 // contains the child frames
1344 // ===========================================================================
1346 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
1348 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1350 CLIENTCREATESTRUCT ccs
;
1351 m_windowStyle
= style
;
1354 ccs
.hWindowMenu
= GetMDIWindowMenu(parent
);
1355 ccs
.idFirstChild
= wxFIRST_MDI_CHILD
;
1357 DWORD msStyle
= MDIS_ALLCHILDSTYLES
| WS_VISIBLE
| WS_CHILD
|
1358 WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
;
1360 if ( style
& wxHSCROLL
)
1361 msStyle
|= WS_HSCROLL
;
1362 if ( style
& wxVSCROLL
)
1363 msStyle
|= WS_VSCROLL
;
1365 DWORD exStyle
= WS_EX_CLIENTEDGE
;
1367 wxWindowCreationHook
hook(this);
1368 m_hWnd
= (WXHWND
)::CreateWindowEx
1378 (LPSTR
)(LPCLIENTCREATESTRUCT
)&ccs
);
1381 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1386 SubclassWin(m_hWnd
);
1391 // Explicitly call default scroll behaviour
1392 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)
1394 // Note: for client windows, the scroll position is not set in
1395 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1396 // scroll position we're at.
1397 // This makes it hard to paint patterns or bitmaps in the background,
1398 // and have the client area scrollable as well.
1400 if ( event
.GetOrientation() == wxHORIZONTAL
)
1401 m_scrollX
= event
.GetPosition(); // Always returns zero!
1403 m_scrollY
= event
.GetPosition(); // Always returns zero!
1408 void wxMDIClientWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1410 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1411 // client area, you can end up with a non-refreshed portion in the client window
1412 // (see OGL studio sample). So check if the position is changed and if so,
1413 // redraw the MDI child frames.
1415 const wxPoint oldPos
= GetPosition();
1417 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
| wxSIZE_FORCE
);
1419 const wxPoint newPos
= GetPosition();
1421 if ((newPos
.x
!= oldPos
.x
) || (newPos
.y
!= oldPos
.y
))
1425 wxWindowList::compatibility_iterator node
= GetParent()->GetChildren().GetFirst();
1428 wxWindow
*child
= node
->GetData();
1429 if (child
->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
1431 ::RedrawWindow(GetHwndOf(child
),
1438 node
= node
->GetNext();
1444 void wxMDIChildFrame::OnIdle(wxIdleEvent
& event
)
1446 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1447 // in flicker e.g. when the frame contained controls with non-trivial
1448 // layout. Since 2.5.3, the frame is created hidden as all other top level
1449 // windows. In order to maintain backward compatibility, the frame is shown
1450 // in OnIdle, unless Show(false) was called by the programmer before.
1451 if ( m_needsInitialShow
)
1456 // MDI child frames get their WM_SIZE when they're constructed but at this
1457 // moment they don't have any children yet so all child windows will be
1458 // positioned incorrectly when they are added later - to fix this, we
1459 // generate an artificial size event here
1460 if ( m_needsResize
)
1462 m_needsResize
= false; // avoid any possibility of recursion
1470 // ---------------------------------------------------------------------------
1471 // non member functions
1472 // ---------------------------------------------------------------------------
1474 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
)
1476 if ( hmenuFrame
|| hmenuWindow
)
1478 if ( !::SendMessage(GetWinHwnd(win
),
1481 (LPARAM
)hmenuWindow
) )
1484 DWORD err
= ::GetLastError();
1486 wxLogApiError(_T("SendMessage(WM_MDISETMENU)"), err
);
1487 #endif // __WXDEBUG__
1491 // update menu bar of the parent window
1492 wxWindow
*parent
= win
->GetParent();
1493 wxCHECK_RET( parent
, wxT("MDI client without parent frame? weird...") );
1495 ::SendMessage(GetWinHwnd(win
), WM_MDIREFRESHMENU
, 0, 0L);
1497 ::DrawMenuBar(GetWinHwnd(parent
));
1500 static void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
)
1502 // Try to insert Window menu in front of Help, otherwise append it.
1503 HMENU hmenu
= (HMENU
)menu
;
1507 int N
= GetMenuItemCount(hmenu
);
1508 bool success
= false;
1509 for ( int i
= 0; i
< N
; i
++ )
1512 int chars
= GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
);
1515 wxLogLastError(wxT("GetMenuString"));
1520 wxString
strBuf(buf
);
1521 if ( wxStripMenuCodes(strBuf
) == wxGetStockLabel(wxID_HELP
,false) )
1524 ::InsertMenu(hmenu
, i
, MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1525 (UINT_PTR
)subMenu
, _("&Window").wx_str());
1532 ::AppendMenu(hmenu
, MF_POPUP
,
1533 (UINT_PTR
)subMenu
, _("&Window").wx_str());
1537 MDISetMenu(win
, hmenu
, subMenu
);
1540 static void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU menu
)
1542 HMENU hMenu
= (HMENU
)menu
;
1548 int N
= ::GetMenuItemCount(hMenu
);
1549 for ( int i
= 0; i
< N
; i
++ )
1551 if ( !::GetMenuString(hMenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1553 // Ignore successful read of menu string with length 0 which
1554 // occurs, for example, for a maximized MDI childs system menu
1555 if ( ::GetLastError() != 0 )
1557 wxLogLastError(wxT("GetMenuString"));
1563 if ( wxStrcmp(buf
, _("&Window")) == 0 )
1565 if ( !::RemoveMenu(hMenu
, i
, MF_BYPOSITION
) )
1567 wxLogLastError(wxT("RemoveMenu"));
1577 // we don't change the windows menu, but we update the main one
1578 MDISetMenu(win
, hMenu
, NULL
);
1582 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
1583 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
)
1586 *hwndAct
= (WXHWND
)lParam
;
1587 *hwndDeact
= (WXHWND
)wParam
;
1590 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)