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 // This range gives a maximum of 500 MDI children. Should be enough :-)
63 static const int wxFIRST_MDI_CHILD
= 4100;
64 static const int wxLAST_MDI_CHILD
= 4600;
66 // ---------------------------------------------------------------------------
68 // ---------------------------------------------------------------------------
70 // set the MDI menus (by sending the WM_MDISETMENU message) and update the menu
71 // of the parent of win (which is supposed to be the MDI client window)
72 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
);
74 // insert the window menu (subMenu) into menu just before "Help" submenu or at
75 // the very end if not found
76 static void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
);
78 // Remove the window menu
79 static void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU menu
);
81 // unpack the parameters of WM_MDIACTIVATE message
82 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
83 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
);
85 // return the HMENU of the MDI menu
87 // this function works correctly even when we don't have a window menu and just
89 static inline HMENU
GetMDIWindowMenu(wxMDIParentFrame
*frame
)
91 wxMenu
*menu
= frame
->GetWindowMenu();
92 return menu
? GetHmenuOf(menu
) : 0;
95 // ===========================================================================
97 // ===========================================================================
99 // ---------------------------------------------------------------------------
101 // ---------------------------------------------------------------------------
103 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxFrame
)
104 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxFrame
)
105 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxWindow
)
107 BEGIN_EVENT_TABLE(wxMDIParentFrame
, wxFrame
)
108 EVT_SIZE(wxMDIParentFrame::OnSize
)
109 EVT_ICONIZE(wxMDIParentFrame::OnIconized
)
110 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged
)
113 EVT_MENU_RANGE(wxFIRST_MDI_CHILD
, wxLAST_MDI_CHILD
,
114 wxMDIParentFrame::OnMDIChild
)
115 EVT_MENU_RANGE(wxID_MDI_WINDOW_FIRST
, wxID_MDI_WINDOW_LAST
,
116 wxMDIParentFrame::OnMDICommand
)
117 #endif // wxUSE_MENUS
120 BEGIN_EVENT_TABLE(wxMDIChildFrame
, wxFrame
)
121 EVT_IDLE(wxMDIChildFrame::OnIdle
)
124 BEGIN_EVENT_TABLE(wxMDIClientWindow
, wxWindow
)
125 EVT_SCROLL(wxMDIClientWindow::OnScroll
)
128 // ===========================================================================
129 // wxMDIParentFrame: the frame which contains the client window which manages
131 // ===========================================================================
133 bool wxMDIParentFrame::Create(wxWindow
*parent
,
135 const wxString
& title
,
139 const wxString
& name
)
141 // this style can be used to prevent a window from having the standard MDI
143 if ( !(style
& wxFRAME_NO_WINDOW_MENU
) )
145 // normal case: we have the window menu, so construct it
146 m_windowMenu
= new wxMenu
;
148 m_windowMenu
->Append(wxID_MDI_WINDOW_CASCADE
, _("&Cascade"));
149 m_windowMenu
->Append(wxID_MDI_WINDOW_TILE_HORZ
, _("Tile &Horizontally"));
150 m_windowMenu
->Append(wxID_MDI_WINDOW_TILE_VERT
, _("Tile &Vertically"));
151 m_windowMenu
->AppendSeparator();
152 m_windowMenu
->Append(wxID_MDI_WINDOW_ARRANGE_ICONS
, _("&Arrange Icons"));
153 m_windowMenu
->Append(wxID_MDI_WINDOW_NEXT
, _("&Next"));
154 m_windowMenu
->Append(wxID_MDI_WINDOW_PREV
, _("&Previous"));
157 #if wxUSE_MENUS && wxUSE_ACCEL
158 // the default menu doesn't have any accelerators (even if we have it)
159 m_accelWindowMenu
= NULL
;
160 #endif // wxUSE_MENUS && wxUSE_ACCEL
163 wxTopLevelWindows
.Append(this);
166 m_windowStyle
= style
;
169 parent
->AddChild(this);
171 if ( id
!= wxID_ANY
)
174 m_windowId
= NewControlId();
177 WXDWORD msflags
= MSWGetCreateWindowFlags(&exflags
);
178 msflags
&= ~WS_VSCROLL
;
179 msflags
&= ~WS_HSCROLL
;
181 if ( !wxWindow::MSWCreate(wxApp::GetRegisteredClassName(_T("wxMDIFrame")),
190 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
192 // unlike (almost?) all other windows, frames are created hidden
198 wxMDIParentFrame::~wxMDIParentFrame()
200 // see comment in ~wxMDIChildFrame
202 m_frameToolBar
= NULL
;
205 m_frameStatusBar
= NULL
;
206 #endif // wxUSE_STATUSBAR
208 #if wxUSE_MENUS && wxUSE_ACCEL
209 delete m_accelWindowMenu
;
210 #endif // wxUSE_MENUS && wxUSE_ACCEL
214 // the MDI frame menubar is not automatically deleted by Windows unlike for
217 ::DestroyMenu((HMENU
)m_hMenu
);
219 if ( m_clientWindow
)
221 if ( m_clientWindow
->MSWGetOldWndProc() )
222 m_clientWindow
->UnsubclassWin();
224 m_clientWindow
->SetHWND(0);
225 delete m_clientWindow
;
229 // ----------------------------------------------------------------------------
230 // wxMDIParentFrame child management
231 // ----------------------------------------------------------------------------
233 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
235 HWND hWnd
= (HWND
)::SendMessage(GetWinHwnd(GetClientWindow()),
236 WM_MDIGETACTIVE
, 0, 0L);
240 return static_cast<wxMDIChildFrame
*>(wxFindWinFromHandle(hWnd
));
243 int wxMDIParentFrame::GetChildFramesCount() const
246 for ( wxWindowList::const_iterator i
= GetChildren().begin();
247 i
!= GetChildren().end();
250 if ( wxDynamicCast(*i
, wxMDIChildFrame
) )
259 void wxMDIParentFrame::AddMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
261 switch ( GetChildFramesCount() )
264 // first MDI child was just added, we need to insert the window
265 // menu now if we have it
268 // and disable the items which can't be used until we have more
270 UpdateWindowMenu(false);
274 // second MDI child was added, enable the menu items which were
275 // disabled because they didn't make sense for a single window
276 UpdateWindowMenu(true);
281 void wxMDIParentFrame::RemoveMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
283 switch ( GetChildFramesCount() )
286 // last MDI child is being removed, remove the now unnecessary
290 // there is no need to call UpdateWindowMenu(true) here so this is
291 // not quite symmetric to AddMDIChild() above
295 // only one MDI child is going to remain, disable the menu commands
296 // which don't make sense for a single child window
297 UpdateWindowMenu(false);
302 // ----------------------------------------------------------------------------
303 // wxMDIParentFrame window menu handling
304 // ----------------------------------------------------------------------------
306 void wxMDIParentFrame::AddWindowMenu()
309 MDIInsertWindowMenu(GetClientWindow(), m_hMenu
, GetMDIWindowMenu(this));
312 void wxMDIParentFrame::RemoveWindowMenu()
315 MDIRemoveWindowMenu(GetClientWindow(), m_hMenu
);
318 void wxMDIParentFrame::UpdateWindowMenu(bool enable
)
322 m_windowMenu
->Enable(wxID_MDI_WINDOW_NEXT
, enable
);
323 m_windowMenu
->Enable(wxID_MDI_WINDOW_PREV
, enable
);
327 #if wxUSE_MENUS_NATIVE
329 void wxMDIParentFrame::InternalSetMenuBar()
331 if ( GetActiveChild() )
335 else // we don't have any MDI children yet
337 // wait until we do to add the window menu but do set the main menu for
338 // now (this is done by AddWindowMenu() as a side effect)
339 MDISetMenu(GetClientWindow(), (HMENU
)m_hMenu
, NULL
);
343 #endif // wxUSE_MENUS_NATIVE
345 void wxMDIParentFrame::SetWindowMenu(wxMenu
* menu
)
347 if ( menu
!= m_windowMenu
)
349 // notice that Remove/AddWindowMenu() are safe to call even when
350 // m_windowMenu is NULL
361 delete m_accelWindowMenu
;
362 m_accelWindowMenu
= NULL
;
364 if ( menu
&& menu
->HasAccels() )
365 m_accelWindowMenu
= menu
->CreateAccelTable();
366 #endif // wxUSE_ACCEL
369 // ----------------------------------------------------------------------------
370 // wxMDIParentFrame other menu-related stuff
371 // ----------------------------------------------------------------------------
373 void wxMDIParentFrame::DoMenuUpdates(wxMenu
* menu
)
375 wxMDIChildFrame
*child
= GetActiveChild();
378 wxEvtHandler
* source
= child
->GetEventHandler();
379 wxMenuBar
* bar
= child
->GetMenuBar();
383 menu
->UpdateUI(source
);
389 int nCount
= bar
->GetMenuCount();
390 for (int n
= 0; n
< nCount
; n
++)
391 bar
->GetMenu(n
)->UpdateUI(source
);
397 wxFrameBase::DoMenuUpdates(menu
);
401 const wxMenuItem
*wxMDIParentFrame::FindItemInMenuBar(int menuId
) const
403 const wxMenuItem
*item
= wxFrame::FindItemInMenuBar(menuId
);
404 if ( !item
&& GetActiveChild() )
406 item
= GetActiveChild()->FindItemInMenuBar(menuId
);
412 WXHMENU
wxMDIParentFrame::MSWGetActiveMenu() const
414 wxMDIChildFrame
* const child
= GetActiveChild();
417 const WXHMENU hmenu
= child
->MSWGetActiveMenu();
422 return wxFrame::MSWGetActiveMenu();
425 #endif // wxUSE_MENUS
427 // ----------------------------------------------------------------------------
428 // wxMDIParentFrame event handling
429 // ----------------------------------------------------------------------------
431 void wxMDIParentFrame::UpdateClientSize()
433 if ( GetClientWindow() )
436 GetClientSize(&width
, &height
);
438 GetClientWindow()->SetSize(0, 0, width
, height
);
442 void wxMDIParentFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
446 // do not call event.Skip() here, it somehow messes up MDI client window
449 void wxMDIParentFrame::OnIconized(wxIconizeEvent
& event
)
453 if ( !event
.IsIconized() )
457 // Responds to colour changes, and passes event on to children.
458 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
460 if ( m_clientWindow
)
462 m_clientWindow
->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
463 m_clientWindow
->Refresh();
469 WXHICON
wxMDIParentFrame::GetDefaultIcon() const
471 // we don't have any standard icons (any more)
475 // ---------------------------------------------------------------------------
477 // ---------------------------------------------------------------------------
479 void wxMDIParentFrame::Cascade()
481 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE
, 0, 0);
484 void wxMDIParentFrame::Tile(wxOrientation orient
)
486 wxASSERT_MSG( orient
== wxHORIZONTAL
|| orient
== wxVERTICAL
,
487 _T("invalid orientation value") );
489 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE
,
490 orient
== wxHORIZONTAL
? MDITILE_HORIZONTAL
491 : MDITILE_VERTICAL
, 0);
494 void wxMDIParentFrame::ArrangeIcons()
496 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE
, 0, 0);
499 void wxMDIParentFrame::ActivateNext()
501 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 0);
504 void wxMDIParentFrame::ActivatePrevious()
506 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 1);
509 // ---------------------------------------------------------------------------
510 // the MDI parent frame window proc
511 // ---------------------------------------------------------------------------
513 WXLRESULT
wxMDIParentFrame::MSWWindowProc(WXUINT message
,
518 bool processed
= false;
524 WXWORD state
, minimized
;
526 UnpackActivate(wParam
, lParam
, &state
, &minimized
, &hwnd
);
528 processed
= HandleActivate(state
, minimized
!= 0, hwnd
);
536 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
538 (void)HandleCommand(id
, cmd
, hwnd
);
540 // even if the frame didn't process it, there is no need to try it
541 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
542 // so pretend we processed the message anyhow
546 // always pass this message DefFrameProc(), otherwise MDI menu
547 // commands (and sys commands - more surprisingly!) won't work
548 MSWDefWindowProc(message
, wParam
, lParam
);
552 m_clientWindow
= OnCreateClient();
553 // Uses own style for client style
554 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
556 wxLogMessage(_("Failed to create MDI parent frame."));
567 // we erase background ourselves
572 // though we don't (usually) resize the MDI client to exactly fit the
573 // client area we need to pass this one to DefFrameProc to allow the children to show
578 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
583 bool wxMDIParentFrame::HandleActivate(int state
, bool minimized
, WXHWND activate
)
585 bool processed
= false;
587 if ( wxWindow::HandleActivate(state
, minimized
, activate
) )
593 // If this window is an MDI parent, we must also send an OnActivate message
594 // to the current child.
595 if ( GetActiveChild() &&
596 ((state
== WA_ACTIVE
) || (state
== WA_CLICKACTIVE
)) )
598 wxActivateEvent
event(wxEVT_ACTIVATE
, true, GetActiveChild()->GetId());
599 event
.SetEventObject( GetActiveChild() );
600 if ( GetActiveChild()->HandleWindowEvent(event
) )
609 void wxMDIParentFrame::OnMDIChild(wxCommandEvent
& event
)
611 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
614 wxWindow
*child
= node
->GetData();
615 if ( child
->GetHWND() )
617 int childId
= wxGetWindowId(child
->GetHWND());
618 if ( childId
== event
.GetId() )
620 ::SendMessage( GetWinHwnd(GetClientWindow()),
622 (WPARAM
)child
->GetHWND(), 0);
627 node
= node
->GetNext();
630 wxFAIL_MSG( "unknown MDI child selected?" );
633 void wxMDIParentFrame::OnMDICommand(wxCommandEvent
& event
)
638 switch ( event
.GetId() )
640 case wxID_MDI_WINDOW_CASCADE
:
642 wParam
= MDITILE_SKIPDISABLED
;
645 case wxID_MDI_WINDOW_TILE_HORZ
:
646 wParam
|= MDITILE_HORIZONTAL
;
649 case wxID_MDI_WINDOW_TILE_VERT
:
651 wParam
= MDITILE_VERTICAL
;
653 wParam
|= MDITILE_SKIPDISABLED
;
656 case wxID_MDI_WINDOW_ARRANGE_ICONS
:
657 msg
= WM_MDIICONARRANGE
;
660 case wxID_MDI_WINDOW_NEXT
:
662 lParam
= 0; // next child
665 case wxID_MDI_WINDOW_PREV
:
667 lParam
= 1; // previous child
671 wxFAIL_MSG( "unknown MDI command" );
675 ::SendMessage(GetWinHwnd(GetClientWindow()), msg
, wParam
, lParam
);
678 wxMenuItem
*wxMDIParentFrame::MSWFindMenuBarItem(WXWORD id
)
680 wxMenuItem
*mitem
= wxFrame::MSWFindMenuBarItem(id
);
681 if ( !mitem
&& m_windowMenu
)
682 mitem
= m_windowMenu
->FindItem((signed short)id
);
687 #endif // wxUSE_MENUS
689 bool wxMDIParentFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND hwnd
)
691 wxMDIChildFrame
* const child
= GetActiveChild();
692 if ( child
&& child
->HandleCommand(id
, cmd
, hwnd
) )
695 return wxFrame::HandleCommand(id
, cmd
, hwnd
);
698 WXLRESULT
wxMDIParentFrame::MSWDefWindowProc(WXUINT message
,
703 if ( GetClientWindow() )
704 clientWnd
= GetClientWindow()->GetHWND();
708 return DefFrameProc(GetHwnd(), (HWND
)clientWnd
, message
, wParam
, lParam
);
711 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG
* msg
)
713 MSG
*pMsg
= (MSG
*)msg
;
715 // first let the current child get it
716 wxMDIChildFrame
* const child
= GetActiveChild();
717 if ( child
&& child
->MSWTranslateMessage(msg
) )
722 // then try out accelerator table (will also check the accelerators for the
723 // normal menu items)
724 if ( wxFrame::MSWTranslateMessage(msg
) )
729 #if wxUSE_MENUS && wxUSE_ACCEL
730 // but it doesn't check for the (custom) accelerators of the window menu
731 // items as it's not part of the menu bar as it's handled by Windows itself
732 // so we need to do this explicitly
733 if ( m_accelWindowMenu
->Translate(this, msg
) )
735 #endif // wxUSE_MENUS && wxUSE_ACCEL
737 // finally, check for MDI specific built-in accelerators
738 if ( pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
740 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg
))
747 // ===========================================================================
749 // ===========================================================================
751 void wxMDIChildFrame::Init()
753 m_needsResize
= true;
754 m_needsInitialShow
= true;
757 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
759 const wxString
& title
,
763 const wxString
& name
)
765 m_mdiParent
= parent
;
769 if ( id
!= wxID_ANY
)
772 m_windowId
= NewControlId();
776 parent
->AddChild(this);
787 wxApp::GetRegisteredClassName(_T("wxMDIChildFrame"), COLOR_WINDOW
);
788 if ( !(style
& wxFULL_REPAINT_ON_RESIZE
) )
789 className
+= wxApp::GetNoRedrawClassSuffix();
791 mcs
.szClass
= className
.wx_str();
792 mcs
.szTitle
= title
.wx_str();
793 mcs
.hOwner
= wxGetInstance();
794 if (x
!= wxDefaultCoord
)
797 mcs
.x
= CW_USEDEFAULT
;
799 if (y
!= wxDefaultCoord
)
802 mcs
.y
= CW_USEDEFAULT
;
804 if (width
!= wxDefaultCoord
)
807 mcs
.cx
= CW_USEDEFAULT
;
809 if (height
!= wxDefaultCoord
)
812 mcs
.cy
= CW_USEDEFAULT
;
814 DWORD msflags
= WS_OVERLAPPED
| WS_CLIPCHILDREN
;
815 if (style
& wxMINIMIZE_BOX
)
816 msflags
|= WS_MINIMIZEBOX
;
817 if (style
& wxMAXIMIZE_BOX
)
818 msflags
|= WS_MAXIMIZEBOX
;
819 if (style
& wxRESIZE_BORDER
)
820 msflags
|= WS_THICKFRAME
;
821 if (style
& wxSYSTEM_MENU
)
822 msflags
|= WS_SYSMENU
;
823 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
824 msflags
|= WS_MINIMIZE
;
825 if (style
& wxMAXIMIZE
)
826 msflags
|= WS_MAXIMIZE
;
827 if (style
& wxCAPTION
)
828 msflags
|= WS_CAPTION
;
834 wxWindowCreationHook
hook(this);
836 m_hWnd
= (WXHWND
)::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
837 WM_MDICREATE
, 0, (LPARAM
)&mcs
);
841 wxLogLastError(_T("WM_MDICREATE"));
847 parent
->AddMDIChild(this);
852 wxMDIChildFrame::~wxMDIChildFrame()
854 // if we hadn't been created, there is nothing to destroy
858 GetMDIParent()->RemoveMDIChild(this);
860 // will be destroyed by DestroyChildren() but reset them before calling it
861 // to avoid using dangling pointers if a callback comes in the meanwhile
863 m_frameToolBar
= NULL
;
866 m_frameStatusBar
= NULL
;
867 #endif // wxUSE_STATUSBAR
871 MDIRemoveWindowMenu(NULL
, m_hMenu
);
876 bool wxMDIChildFrame::Show(bool show
)
878 m_needsInitialShow
= false;
880 if (!wxFrame::Show(show
))
883 // KH: Without this call, new MDI children do not become active.
884 // This was added here after the same BringWindowToTop call was
885 // removed from wxTopLevelWindow::Show (November 2005)
887 ::BringWindowToTop(GetHwnd());
889 // we need to refresh the MDI frame window menu to include (or exclude if
890 // we've been hidden) this frame
891 wxMDIParentFrame
* const parent
= GetMDIParent();
892 MDISetMenu(parent
->GetClientWindow(), NULL
, NULL
);
897 // Set the client size (i.e. leave the calculation of borders etc.
899 void wxMDIChildFrame::DoSetClientSize(int width
, int height
)
901 HWND hWnd
= GetHwnd();
904 ::GetClientRect(hWnd
, &rect
);
907 GetWindowRect(hWnd
, &rect2
);
909 // Find the difference between the entire window (title bar and all)
910 // and the client area; add this to the new client size to move the
912 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
913 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
916 if (GetStatusBar() && GetStatusBar()->IsShown())
919 GetStatusBar()->GetSize(&sx
, &sy
);
922 #endif // wxUSE_STATUSBAR
925 point
.x
= rect2
.left
;
928 // If there's an MDI parent, must subtract the parent's top left corner
929 // since MoveWindow moves relative to the parent
930 wxMDIParentFrame
* const mdiParent
= GetMDIParent();
931 ::ScreenToClient(GetHwndOf(mdiParent
->GetClientWindow()), &point
);
933 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)true);
935 wxSize
size(width
, height
);
936 wxSizeEvent
event(size
, m_windowId
);
937 event
.SetEventObject( this );
938 HandleWindowEvent(event
);
941 // Unlike other wxTopLevelWindowBase, the mdi child's "GetPosition" is not the
942 // same as its GetScreenPosition
943 void wxMDIChildFrame::DoGetScreenPosition(int *x
, int *y
) const
945 HWND hWnd
= GetHwnd();
948 ::GetWindowRect(hWnd
, &rect
);
956 void wxMDIChildFrame::DoGetPosition(int *x
, int *y
) const
959 GetWindowRect(GetHwnd(), &rect
);
964 // Since we now have the absolute screen coords,
965 // if there's a parent we must subtract its top left corner
966 wxMDIParentFrame
* const mdiParent
= GetMDIParent();
967 ::ScreenToClient(GetHwndOf(mdiParent
->GetClientWindow()), &point
);
975 void wxMDIChildFrame::InternalSetMenuBar()
977 wxMDIParentFrame
* const parent
= GetMDIParent();
979 MDIInsertWindowMenu(parent
->GetClientWindow(),
980 m_hMenu
, GetMDIWindowMenu(parent
));
983 void wxMDIChildFrame::DetachMenuBar()
985 MDIRemoveWindowMenu(NULL
, m_hMenu
);
986 wxFrame::DetachMenuBar();
989 WXHICON
wxMDIChildFrame::GetDefaultIcon() const
991 // we don't have any standard icons (any more)
995 // ---------------------------------------------------------------------------
997 // ---------------------------------------------------------------------------
999 void wxMDIChildFrame::Maximize(bool maximize
)
1001 wxMDIParentFrame
* const parent
= GetMDIParent();
1002 if ( parent
&& parent
->GetClientWindow() )
1004 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
1005 maximize
? WM_MDIMAXIMIZE
: WM_MDIRESTORE
,
1006 (WPARAM
)GetHwnd(), 0);
1010 void wxMDIChildFrame::Restore()
1012 wxMDIParentFrame
* const parent
= GetMDIParent();
1013 if ( parent
&& parent
->GetClientWindow() )
1015 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIRESTORE
,
1016 (WPARAM
) GetHwnd(), 0);
1020 void wxMDIChildFrame::Activate()
1022 wxMDIParentFrame
* const parent
= GetMDIParent();
1023 if ( parent
&& parent
->GetClientWindow() )
1025 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIACTIVATE
,
1026 (WPARAM
) GetHwnd(), 0);
1030 // ---------------------------------------------------------------------------
1031 // MDI window proc and message handlers
1032 // ---------------------------------------------------------------------------
1034 WXLRESULT
wxMDIChildFrame::MSWWindowProc(WXUINT message
,
1039 bool processed
= false;
1043 case WM_GETMINMAXINFO
:
1044 processed
= HandleGetMinMaxInfo((MINMAXINFO
*)lParam
);
1047 case WM_MDIACTIVATE
:
1050 WXHWND hwndAct
, hwndDeact
;
1051 UnpackMDIActivate(wParam
, lParam
, &act
, &hwndAct
, &hwndDeact
);
1053 processed
= HandleMDIActivate(act
, hwndAct
, hwndDeact
);
1058 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
1059 // scrollbars if necessary
1064 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
1066 MSWDefWindowProc(message
, wParam
, lParam
);
1070 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
1071 // the message (the base class version does not)
1072 return MSWDefWindowProc(message
, wParam
, lParam
);
1074 case WM_WINDOWPOSCHANGING
:
1075 processed
= HandleWindowPosChanging((LPWINDOWPOS
)lParam
);
1080 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
1085 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate
),
1089 wxMDIParentFrame
* const parent
= GetMDIParent();
1091 WXHMENU hMenuToSet
= 0;
1095 if ( m_hWnd
== hwndAct
)
1098 parent
->SetActiveChild(this);
1100 WXHMENU hMenuChild
= m_hMenu
;
1102 hMenuToSet
= hMenuChild
;
1104 else if ( m_hWnd
== hwndDeact
)
1106 wxASSERT_MSG( parent
->GetActiveChild() == this,
1107 wxT("can't deactivate MDI child which wasn't active!") );
1110 parent
->SetActiveChild(NULL
);
1112 WXHMENU hMenuParent
= parent
->m_hMenu
;
1114 // activate the the parent menu only when there is no other child
1115 // that has been activated
1116 if ( hMenuParent
&& !hwndAct
)
1117 hMenuToSet
= hMenuParent
;
1121 // we have nothing to do with it
1127 MDISetMenu(parent
->GetClientWindow(),
1128 (HMENU
)hMenuToSet
, GetMDIWindowMenu(parent
));
1131 wxActivateEvent
event(wxEVT_ACTIVATE
, activated
, m_windowId
);
1132 event
.SetEventObject( this );
1134 ResetWindowStyle(NULL
);
1136 return HandleWindowEvent(event
);
1139 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos
)
1141 WINDOWPOS
*lpPos
= (WINDOWPOS
*)pos
;
1143 if (!(lpPos
->flags
& SWP_NOSIZE
))
1146 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1147 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1148 if (ResetWindowStyle((void *) & rectClient
) && (dwStyle
& WS_MAXIMIZE
))
1150 ::AdjustWindowRectEx(&rectClient
, dwStyle
, false, dwExStyle
);
1151 lpPos
->x
= rectClient
.left
;
1152 lpPos
->y
= rectClient
.top
;
1153 lpPos
->cx
= rectClient
.right
- rectClient
.left
;
1154 lpPos
->cy
= rectClient
.bottom
- rectClient
.top
;
1161 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo
)
1163 MINMAXINFO
*info
= (MINMAXINFO
*)mmInfo
;
1165 // let the default window proc calculate the size of MDI children
1166 // frames because it is based on the size of the MDI client window,
1167 // not on the values specified in wxWindow m_max variables
1168 bool processed
= MSWDefWindowProc(WM_GETMINMAXINFO
, 0, (LPARAM
)mmInfo
) != 0;
1170 int minWidth
= GetMinWidth(),
1171 minHeight
= GetMinHeight();
1173 // but allow GetSizeHints() to set the min size
1174 if ( minWidth
!= wxDefaultCoord
)
1176 info
->ptMinTrackSize
.x
= minWidth
;
1181 if ( minHeight
!= wxDefaultCoord
)
1183 info
->ptMinTrackSize
.y
= minHeight
;
1191 // ---------------------------------------------------------------------------
1192 // MDI specific message translation/preprocessing
1193 // ---------------------------------------------------------------------------
1195 WXLRESULT
wxMDIChildFrame::MSWDefWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1197 return DefMDIChildProc(GetHwnd(),
1198 (UINT
)message
, (WPARAM
)wParam
, (LPARAM
)lParam
);
1201 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG
* msg
)
1203 // we must pass the parent frame to ::TranslateAccelerator(), otherwise it
1204 // doesn't do its job correctly for MDI child menus
1205 return MSWDoTranslateMessage(GetMDIParent(), msg
);
1208 // ---------------------------------------------------------------------------
1210 // ---------------------------------------------------------------------------
1212 void wxMDIChildFrame::MSWDestroyWindow()
1214 wxMDIParentFrame
* const parent
= GetMDIParent();
1216 // Must make sure this handle is invalidated (set to NULL) since all sorts
1217 // of things could happen after the child client is destroyed, but before
1218 // the wxFrame is destroyed.
1220 HWND oldHandle
= (HWND
)GetHWND();
1221 SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIDESTROY
,
1222 (WPARAM
)oldHandle
, 0);
1224 if (parent
->GetActiveChild() == NULL
)
1225 ResetWindowStyle(NULL
);
1229 ::DestroyMenu((HMENU
) m_hMenu
);
1232 wxRemoveHandleAssociation(this);
1236 // Change the client window's extended style so we don't get a client edge
1237 // style when a child is maximised (a double border looks silly.)
1238 bool wxMDIChildFrame::ResetWindowStyle(void *vrect
)
1240 RECT
*rect
= (RECT
*)vrect
;
1241 wxMDIParentFrame
* const pFrameWnd
= GetMDIParent();
1242 wxMDIChildFrame
* pChild
= pFrameWnd
->GetActiveChild();
1244 if (!pChild
|| (pChild
== this))
1246 HWND hwndClient
= GetWinHwnd(pFrameWnd
->GetClientWindow());
1247 DWORD dwStyle
= ::GetWindowLong(hwndClient
, GWL_EXSTYLE
);
1249 // we want to test whether there is a maximized child, so just set
1250 // dwThisStyle to 0 if there is no child at all
1251 DWORD dwThisStyle
= pChild
1252 ? ::GetWindowLong(GetWinHwnd(pChild
), GWL_STYLE
) : 0;
1253 DWORD dwNewStyle
= dwStyle
;
1254 if ( dwThisStyle
& WS_MAXIMIZE
)
1255 dwNewStyle
&= ~(WS_EX_CLIENTEDGE
);
1257 dwNewStyle
|= WS_EX_CLIENTEDGE
;
1259 if (dwStyle
!= dwNewStyle
)
1261 // force update of everything
1262 ::RedrawWindow(hwndClient
, NULL
, NULL
,
1263 RDW_INVALIDATE
| RDW_ALLCHILDREN
);
1264 ::SetWindowLong(hwndClient
, GWL_EXSTYLE
, dwNewStyle
);
1265 ::SetWindowPos(hwndClient
, NULL
, 0, 0, 0, 0,
1266 SWP_FRAMECHANGED
| SWP_NOACTIVATE
|
1267 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1270 ::GetClientRect(hwndClient
, rect
);
1279 // ===========================================================================
1280 // wxMDIClientWindow: the window of predefined (by Windows) class which
1281 // contains the child frames
1282 // ===========================================================================
1284 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
1286 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1288 CLIENTCREATESTRUCT ccs
;
1289 m_windowStyle
= style
;
1292 ccs
.hWindowMenu
= GetMDIWindowMenu(parent
);
1293 ccs
.idFirstChild
= wxFIRST_MDI_CHILD
;
1295 DWORD msStyle
= MDIS_ALLCHILDSTYLES
| WS_VISIBLE
| WS_CHILD
|
1296 WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
;
1298 if ( style
& wxHSCROLL
)
1299 msStyle
|= WS_HSCROLL
;
1300 if ( style
& wxVSCROLL
)
1301 msStyle
|= WS_VSCROLL
;
1303 DWORD exStyle
= WS_EX_CLIENTEDGE
;
1305 wxWindowCreationHook
hook(this);
1306 m_hWnd
= (WXHWND
)::CreateWindowEx
1316 (LPSTR
)(LPCLIENTCREATESTRUCT
)&ccs
);
1319 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1324 SubclassWin(m_hWnd
);
1329 // Explicitly call default scroll behaviour
1330 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)
1332 // Note: for client windows, the scroll position is not set in
1333 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1334 // scroll position we're at.
1335 // This makes it hard to paint patterns or bitmaps in the background,
1336 // and have the client area scrollable as well.
1338 if ( event
.GetOrientation() == wxHORIZONTAL
)
1339 m_scrollX
= event
.GetPosition(); // Always returns zero!
1341 m_scrollY
= event
.GetPosition(); // Always returns zero!
1346 void wxMDIClientWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1348 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1349 // client area, you can end up with a non-refreshed portion in the client window
1350 // (see OGL studio sample). So check if the position is changed and if so,
1351 // redraw the MDI child frames.
1353 const wxPoint oldPos
= GetPosition();
1355 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
| wxSIZE_FORCE
);
1357 const wxPoint newPos
= GetPosition();
1359 if ((newPos
.x
!= oldPos
.x
) || (newPos
.y
!= oldPos
.y
))
1363 wxWindowList::compatibility_iterator node
= GetParent()->GetChildren().GetFirst();
1366 wxWindow
*child
= node
->GetData();
1367 if (child
->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
1369 ::RedrawWindow(GetHwndOf(child
),
1376 node
= node
->GetNext();
1382 void wxMDIChildFrame::OnIdle(wxIdleEvent
& event
)
1384 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1385 // in flicker e.g. when the frame contained controls with non-trivial
1386 // layout. Since 2.5.3, the frame is created hidden as all other top level
1387 // windows. In order to maintain backward compatibility, the frame is shown
1388 // in OnIdle, unless Show(false) was called by the programmer before.
1389 if ( m_needsInitialShow
)
1394 // MDI child frames get their WM_SIZE when they're constructed but at this
1395 // moment they don't have any children yet so all child windows will be
1396 // positioned incorrectly when they are added later - to fix this, we
1397 // generate an artificial size event here
1398 if ( m_needsResize
)
1400 m_needsResize
= false; // avoid any possibility of recursion
1408 // ---------------------------------------------------------------------------
1409 // non member functions
1410 // ---------------------------------------------------------------------------
1412 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
)
1414 if ( hmenuFrame
|| hmenuWindow
)
1416 if ( !::SendMessage(GetWinHwnd(win
),
1419 (LPARAM
)hmenuWindow
) )
1422 DWORD err
= ::GetLastError();
1424 wxLogApiError(_T("SendMessage(WM_MDISETMENU)"), err
);
1425 #endif // __WXDEBUG__
1429 // update menu bar of the parent window
1430 wxWindow
*parent
= win
->GetParent();
1431 wxCHECK_RET( parent
, wxT("MDI client without parent frame? weird...") );
1433 ::SendMessage(GetWinHwnd(win
), WM_MDIREFRESHMENU
, 0, 0L);
1435 ::DrawMenuBar(GetWinHwnd(parent
));
1438 static void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
)
1440 // Try to insert Window menu in front of Help, otherwise append it.
1441 HMENU hmenu
= (HMENU
)menu
;
1445 int N
= GetMenuItemCount(hmenu
);
1446 bool success
= false;
1447 for ( int i
= 0; i
< N
; i
++ )
1450 int chars
= GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
);
1453 wxLogLastError(wxT("GetMenuString"));
1458 wxString
strBuf(buf
);
1459 if ( wxStripMenuCodes(strBuf
) == wxGetStockLabel(wxID_HELP
,false) )
1462 ::InsertMenu(hmenu
, i
, MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1463 (UINT_PTR
)subMenu
, _("&Window").wx_str());
1470 ::AppendMenu(hmenu
, MF_POPUP
,
1471 (UINT_PTR
)subMenu
, _("&Window").wx_str());
1475 MDISetMenu(win
, hmenu
, subMenu
);
1478 static void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU menu
)
1480 HMENU hMenu
= (HMENU
)menu
;
1486 int N
= ::GetMenuItemCount(hMenu
);
1487 for ( int i
= 0; i
< N
; i
++ )
1489 if ( !::GetMenuString(hMenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1491 // Ignore successful read of menu string with length 0 which
1492 // occurs, for example, for a maximized MDI childs system menu
1493 if ( ::GetLastError() != 0 )
1495 wxLogLastError(wxT("GetMenuString"));
1501 if ( wxStrcmp(buf
, _("&Window")) == 0 )
1503 if ( !::RemoveMenu(hMenu
, i
, MF_BYPOSITION
) )
1505 wxLogLastError(wxT("RemoveMenu"));
1515 // we don't change the windows menu, but we update the main one
1516 MDISetMenu(win
, hMenu
, NULL
);
1520 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
1521 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
)
1524 *hwndAct
= (WXHWND
)lParam
;
1525 *hwndDeact
= (WXHWND
)wParam
;
1528 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)