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 void wxMDIParentFrame::Init()
143 #if wxUSE_MENUS && wxUSE_ACCEL
144 // the default menu doesn't have any accelerators (even if we have it)
145 m_accelWindowMenu
= NULL
;
146 #endif // wxUSE_MENUS && wxUSE_ACCEL
149 bool wxMDIParentFrame::Create(wxWindow
*parent
,
151 const wxString
& title
,
155 const wxString
& name
)
157 // this style can be used to prevent a window from having the standard MDI
159 if ( !(style
& wxFRAME_NO_WINDOW_MENU
) )
161 // normal case: we have the window menu, so construct it
162 m_windowMenu
= new wxMenu
;
164 m_windowMenu
->Append(wxID_MDI_WINDOW_CASCADE
, _("&Cascade"));
165 m_windowMenu
->Append(wxID_MDI_WINDOW_TILE_HORZ
, _("Tile &Horizontally"));
166 m_windowMenu
->Append(wxID_MDI_WINDOW_TILE_VERT
, _("Tile &Vertically"));
167 m_windowMenu
->AppendSeparator();
168 m_windowMenu
->Append(wxID_MDI_WINDOW_ARRANGE_ICONS
, _("&Arrange Icons"));
169 m_windowMenu
->Append(wxID_MDI_WINDOW_NEXT
, _("&Next"));
170 m_windowMenu
->Append(wxID_MDI_WINDOW_PREV
, _("&Previous"));
174 wxTopLevelWindows
.Append(this);
177 m_windowStyle
= style
;
180 parent
->AddChild(this);
182 if ( id
!= wxID_ANY
)
185 m_windowId
= NewControlId();
188 WXDWORD msflags
= MSWGetCreateWindowFlags(&exflags
);
189 msflags
&= ~WS_VSCROLL
;
190 msflags
&= ~WS_HSCROLL
;
192 if ( !wxWindow::MSWCreate(wxApp::GetRegisteredClassName(wxT("wxMDIFrame")),
201 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
203 // unlike (almost?) all other windows, frames are created hidden
209 wxMDIParentFrame::~wxMDIParentFrame()
211 // see comment in ~wxMDIChildFrame
213 m_frameToolBar
= NULL
;
216 m_frameStatusBar
= NULL
;
217 #endif // wxUSE_STATUSBAR
219 #if wxUSE_MENUS && wxUSE_ACCEL
220 delete m_accelWindowMenu
;
221 #endif // wxUSE_MENUS && wxUSE_ACCEL
225 // the MDI frame menubar is not automatically deleted by Windows unlike for
228 ::DestroyMenu((HMENU
)m_hMenu
);
230 if ( m_clientWindow
)
232 if ( m_clientWindow
->MSWGetOldWndProc() )
233 m_clientWindow
->UnsubclassWin();
235 m_clientWindow
->SetHWND(0);
236 delete m_clientWindow
;
240 // ----------------------------------------------------------------------------
241 // wxMDIParentFrame child management
242 // ----------------------------------------------------------------------------
244 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
246 HWND hWnd
= (HWND
)::SendMessage(GetWinHwnd(GetClientWindow()),
247 WM_MDIGETACTIVE
, 0, 0L);
251 return static_cast<wxMDIChildFrame
*>(wxFindWinFromHandle(hWnd
));
254 int wxMDIParentFrame::GetChildFramesCount() const
257 for ( wxWindowList::const_iterator i
= GetChildren().begin();
258 i
!= GetChildren().end();
261 if ( wxDynamicCast(*i
, wxMDIChildFrame
) )
270 void wxMDIParentFrame::AddMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
272 switch ( GetChildFramesCount() )
275 // first MDI child was just added, we need to insert the window
276 // menu now if we have it
279 // and disable the items which can't be used until we have more
281 UpdateWindowMenu(false);
285 // second MDI child was added, enable the menu items which were
286 // disabled because they didn't make sense for a single window
287 UpdateWindowMenu(true);
292 void wxMDIParentFrame::RemoveMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
294 switch ( GetChildFramesCount() )
297 // last MDI child is being removed, remove the now unnecessary
301 // there is no need to call UpdateWindowMenu(true) here so this is
302 // not quite symmetric to AddMDIChild() above
306 // only one MDI child is going to remain, disable the menu commands
307 // which don't make sense for a single child window
308 UpdateWindowMenu(false);
313 // ----------------------------------------------------------------------------
314 // wxMDIParentFrame window menu handling
315 // ----------------------------------------------------------------------------
317 void wxMDIParentFrame::AddWindowMenu()
321 // For correct handling of the events from this menu we also must
322 // attach it to the menu bar.
323 m_windowMenu
->Attach(GetMenuBar());
325 MDIInsertWindowMenu(GetClientWindow(), m_hMenu
, GetMDIWindowMenu(this));
329 void wxMDIParentFrame::RemoveWindowMenu()
333 MDIRemoveWindowMenu(GetClientWindow(), m_hMenu
);
335 m_windowMenu
->Detach();
339 void wxMDIParentFrame::UpdateWindowMenu(bool enable
)
343 m_windowMenu
->Enable(wxID_MDI_WINDOW_NEXT
, enable
);
344 m_windowMenu
->Enable(wxID_MDI_WINDOW_PREV
, enable
);
348 #if wxUSE_MENUS_NATIVE
350 void wxMDIParentFrame::InternalSetMenuBar()
352 if ( GetActiveChild() )
356 else // we don't have any MDI children yet
358 // wait until we do to add the window menu but do set the main menu for
359 // now (this is done by AddWindowMenu() as a side effect)
360 MDISetMenu(GetClientWindow(), (HMENU
)m_hMenu
, NULL
);
364 #endif // wxUSE_MENUS_NATIVE
366 void wxMDIParentFrame::SetWindowMenu(wxMenu
* menu
)
368 if ( menu
!= m_windowMenu
)
370 // notice that Remove/AddWindowMenu() are safe to call even when
371 // m_windowMenu is NULL
382 wxDELETE(m_accelWindowMenu
);
384 if ( menu
&& menu
->HasAccels() )
385 m_accelWindowMenu
= menu
->CreateAccelTable();
386 #endif // wxUSE_ACCEL
389 // ----------------------------------------------------------------------------
390 // wxMDIParentFrame other menu-related stuff
391 // ----------------------------------------------------------------------------
393 void wxMDIParentFrame::DoMenuUpdates(wxMenu
* menu
)
395 wxMDIChildFrame
*child
= GetActiveChild();
398 wxEvtHandler
* source
= child
->GetEventHandler();
399 wxMenuBar
* bar
= child
->GetMenuBar();
403 menu
->UpdateUI(source
);
409 int nCount
= bar
->GetMenuCount();
410 for (int n
= 0; n
< nCount
; n
++)
411 bar
->GetMenu(n
)->UpdateUI(source
);
417 wxFrameBase::DoMenuUpdates(menu
);
421 wxMenuItem
*wxMDIParentFrame::FindItemInMenuBar(int menuId
) const
423 wxMenuItem
*item
= wxFrame::FindItemInMenuBar(menuId
);
424 if ( !item
&& GetActiveChild() )
426 item
= GetActiveChild()->FindItemInMenuBar(menuId
);
429 if ( !item
&& m_windowMenu
)
430 item
= m_windowMenu
->FindItem(menuId
);
435 WXHMENU
wxMDIParentFrame::MSWGetActiveMenu() const
437 wxMDIChildFrame
* const child
= GetActiveChild();
440 const WXHMENU hmenu
= child
->MSWGetActiveMenu();
445 return wxFrame::MSWGetActiveMenu();
448 #endif // wxUSE_MENUS
450 // ----------------------------------------------------------------------------
451 // wxMDIParentFrame event handling
452 // ----------------------------------------------------------------------------
454 void wxMDIParentFrame::UpdateClientSize()
456 if ( GetClientWindow() )
459 GetClientSize(&width
, &height
);
461 GetClientWindow()->SetSize(0, 0, width
, height
);
465 void wxMDIParentFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
469 // do not call event.Skip() here, it somehow messes up MDI client window
472 void wxMDIParentFrame::OnIconized(wxIconizeEvent
& event
)
476 if ( !event
.IsIconized() )
480 // Responds to colour changes, and passes event on to children.
481 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
483 if ( m_clientWindow
)
485 m_clientWindow
->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
486 m_clientWindow
->Refresh();
492 WXHICON
wxMDIParentFrame::GetDefaultIcon() const
494 // we don't have any standard icons (any more)
498 // ---------------------------------------------------------------------------
500 // ---------------------------------------------------------------------------
502 void wxMDIParentFrame::Cascade()
504 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE
, 0, 0);
507 void wxMDIParentFrame::Tile(wxOrientation orient
)
509 wxASSERT_MSG( orient
== wxHORIZONTAL
|| orient
== wxVERTICAL
,
510 wxT("invalid orientation value") );
512 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE
,
513 orient
== wxHORIZONTAL
? MDITILE_HORIZONTAL
514 : MDITILE_VERTICAL
, 0);
517 void wxMDIParentFrame::ArrangeIcons()
519 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE
, 0, 0);
522 void wxMDIParentFrame::ActivateNext()
524 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 0);
527 void wxMDIParentFrame::ActivatePrevious()
529 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 1);
532 // ---------------------------------------------------------------------------
533 // the MDI parent frame window proc
534 // ---------------------------------------------------------------------------
536 WXLRESULT
wxMDIParentFrame::MSWWindowProc(WXUINT message
,
541 bool processed
= false;
547 WXWORD state
, minimized
;
549 UnpackActivate(wParam
, lParam
, &state
, &minimized
, &hwnd
);
551 processed
= HandleActivate(state
, minimized
!= 0, hwnd
);
556 // system messages such as SC_CLOSE are sent as WM_COMMANDs to the
557 // parent MDI frame and we must let the DefFrameProc() have them
558 // for these commands to work (without it, closing the maximized
559 // MDI children doesn't work, for example)
563 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
565 if ( cmd
== 0 /* menu */ &&
566 id
>= SC_SIZE
/* first system menu command */ )
568 MSWDefWindowProc(message
, wParam
, lParam
);
575 m_clientWindow
= OnCreateClient();
576 // Uses own style for client style
577 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
579 wxLogMessage(_("Failed to create MDI parent frame."));
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 wxStaticCast(child
, wxMDIChildFrame
)->Activate();
636 node
= node
->GetNext();
639 wxFAIL_MSG( "unknown MDI child selected?" );
642 void wxMDIParentFrame::OnMDICommand(wxCommandEvent
& event
)
647 switch ( event
.GetId() )
649 case wxID_MDI_WINDOW_CASCADE
:
651 wParam
= MDITILE_SKIPDISABLED
;
654 case wxID_MDI_WINDOW_TILE_HORZ
:
655 wParam
|= MDITILE_HORIZONTAL
;
658 case wxID_MDI_WINDOW_TILE_VERT
:
660 wParam
= MDITILE_VERTICAL
;
662 wParam
|= MDITILE_SKIPDISABLED
;
665 case wxID_MDI_WINDOW_ARRANGE_ICONS
:
666 msg
= WM_MDIICONARRANGE
;
669 case wxID_MDI_WINDOW_NEXT
:
671 lParam
= 0; // next child
674 case wxID_MDI_WINDOW_PREV
:
676 lParam
= 1; // previous child
680 wxFAIL_MSG( "unknown MDI command" );
684 ::SendMessage(GetWinHwnd(GetClientWindow()), msg
, wParam
, lParam
);
687 #endif // wxUSE_MENUS
689 bool wxMDIParentFrame::TryBefore(wxEvent
& event
)
691 // menu (and toolbar) events should be sent to the active child frame
693 if ( event
.GetEventType() == wxEVT_COMMAND_MENU_SELECTED
)
695 wxMDIChildFrame
* const child
= GetActiveChild();
696 if ( child
&& child
->ProcessWindowEventLocally(event
) )
700 return wxMDIParentFrameBase::TryBefore(event
);
703 WXLRESULT
wxMDIParentFrame::MSWDefWindowProc(WXUINT message
,
708 if ( GetClientWindow() )
709 clientWnd
= GetClientWindow()->GetHWND();
713 return DefFrameProc(GetHwnd(), (HWND
)clientWnd
, message
, wParam
, lParam
);
716 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG
* msg
)
718 MSG
*pMsg
= (MSG
*)msg
;
720 // first let the current child get it
721 wxMDIChildFrame
* const child
= GetActiveChild();
722 if ( child
&& child
->MSWTranslateMessage(msg
) )
727 // then try out accelerator table (will also check the accelerators for the
728 // normal menu items)
729 if ( wxFrame::MSWTranslateMessage(msg
) )
734 #if wxUSE_MENUS && wxUSE_ACCEL
735 // but it doesn't check for the (custom) accelerators of the window menu
736 // items as it's not part of the menu bar as it's handled by Windows itself
737 // so we need to do this explicitly
738 if ( m_accelWindowMenu
&& m_accelWindowMenu
->Translate(this, msg
) )
740 #endif // wxUSE_MENUS && wxUSE_ACCEL
742 // finally, check for MDI specific built-in accelerators
743 if ( pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
745 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg
))
752 // ===========================================================================
754 // ===========================================================================
756 void wxMDIChildFrame::Init()
758 m_needsResize
= true;
759 m_needsInitialShow
= true;
762 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
764 const wxString
& title
,
768 const wxString
& name
)
770 m_mdiParent
= parent
;
774 if ( id
!= wxID_ANY
)
777 m_windowId
= NewControlId();
781 parent
->AddChild(this);
792 wxApp::GetRegisteredClassName(wxT("wxMDIChildFrame"), COLOR_WINDOW
);
793 if ( !(style
& wxFULL_REPAINT_ON_RESIZE
) )
794 className
+= wxApp::GetNoRedrawClassSuffix();
796 mcs
.szClass
= className
.t_str();
797 mcs
.szTitle
= title
.t_str();
798 mcs
.hOwner
= wxGetInstance();
799 if (x
!= wxDefaultCoord
)
802 mcs
.x
= CW_USEDEFAULT
;
804 if (y
!= wxDefaultCoord
)
807 mcs
.y
= CW_USEDEFAULT
;
809 if (width
!= wxDefaultCoord
)
812 mcs
.cx
= CW_USEDEFAULT
;
814 if (height
!= wxDefaultCoord
)
817 mcs
.cy
= CW_USEDEFAULT
;
819 DWORD msflags
= WS_OVERLAPPED
| WS_CLIPCHILDREN
;
820 if (style
& wxMINIMIZE_BOX
)
821 msflags
|= WS_MINIMIZEBOX
;
822 if (style
& wxMAXIMIZE_BOX
)
823 msflags
|= WS_MAXIMIZEBOX
;
824 if (style
& wxRESIZE_BORDER
)
825 msflags
|= WS_THICKFRAME
;
826 if (style
& wxSYSTEM_MENU
)
827 msflags
|= WS_SYSMENU
;
828 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
829 msflags
|= WS_MINIMIZE
;
830 if (style
& wxMAXIMIZE
)
831 msflags
|= WS_MAXIMIZE
;
832 if (style
& wxCAPTION
)
833 msflags
|= WS_CAPTION
;
839 wxWindowCreationHook
hook(this);
841 m_hWnd
= (WXHWND
)::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
842 WM_MDICREATE
, 0, (LPARAM
)&mcs
);
846 wxLogLastError(wxT("WM_MDICREATE"));
852 parent
->AddMDIChild(this);
857 wxMDIChildFrame::~wxMDIChildFrame()
859 // if we hadn't been created, there is nothing to destroy
863 GetMDIParent()->RemoveMDIChild(this);
865 // will be destroyed by DestroyChildren() but reset them before calling it
866 // to avoid using dangling pointers if a callback comes in the meanwhile
868 m_frameToolBar
= NULL
;
871 m_frameStatusBar
= NULL
;
872 #endif // wxUSE_STATUSBAR
876 MDIRemoveWindowMenu(NULL
, m_hMenu
);
881 bool wxMDIChildFrame::Show(bool show
)
883 m_needsInitialShow
= false;
885 if (!wxFrame::Show(show
))
888 // KH: Without this call, new MDI children do not become active.
889 // This was added here after the same BringWindowToTop call was
890 // removed from wxTopLevelWindow::Show (November 2005)
892 ::BringWindowToTop(GetHwnd());
894 // we need to refresh the MDI frame window menu to include (or exclude if
895 // we've been hidden) this frame
896 wxMDIParentFrame
* const parent
= GetMDIParent();
897 MDISetMenu(parent
->GetClientWindow(), NULL
, NULL
);
903 wxMDIChildFrame::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
905 // we need to disable client area origin adjustments used for the child
906 // windows for the frame itself
907 wxMDIChildFrameBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
910 // Set the client size (i.e. leave the calculation of borders etc.
912 void wxMDIChildFrame::DoSetClientSize(int width
, int height
)
914 HWND hWnd
= GetHwnd();
917 ::GetClientRect(hWnd
, &rect
);
920 GetWindowRect(hWnd
, &rect2
);
922 // Find the difference between the entire window (title bar and all)
923 // and the client area; add this to the new client size to move the
925 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
926 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
929 if (GetStatusBar() && GetStatusBar()->IsShown())
932 GetStatusBar()->GetSize(&sx
, &sy
);
935 #endif // wxUSE_STATUSBAR
938 point
.x
= rect2
.left
;
941 // If there's an MDI parent, must subtract the parent's top left corner
942 // since MoveWindow moves relative to the parent
943 wxMDIParentFrame
* const mdiParent
= GetMDIParent();
944 ::ScreenToClient(GetHwndOf(mdiParent
->GetClientWindow()), &point
);
946 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)true);
948 wxSize
size(width
, height
);
949 wxSizeEvent
event(size
, m_windowId
);
950 event
.SetEventObject( this );
951 HandleWindowEvent(event
);
954 // Unlike other wxTopLevelWindowBase, the mdi child's "GetPosition" is not the
955 // same as its GetScreenPosition
956 void wxMDIChildFrame::DoGetScreenPosition(int *x
, int *y
) const
958 HWND hWnd
= GetHwnd();
961 ::GetWindowRect(hWnd
, &rect
);
969 void wxMDIChildFrame::DoGetPosition(int *x
, int *y
) const
972 GetWindowRect(GetHwnd(), &rect
);
977 // Since we now have the absolute screen coords,
978 // if there's a parent we must subtract its top left corner
979 wxMDIParentFrame
* const mdiParent
= GetMDIParent();
980 ::ScreenToClient(GetHwndOf(mdiParent
->GetClientWindow()), &point
);
988 void wxMDIChildFrame::InternalSetMenuBar()
990 wxMDIParentFrame
* const parent
= GetMDIParent();
992 MDIInsertWindowMenu(parent
->GetClientWindow(),
993 m_hMenu
, GetMDIWindowMenu(parent
));
996 void wxMDIChildFrame::DetachMenuBar()
998 MDIRemoveWindowMenu(NULL
, m_hMenu
);
999 wxFrame::DetachMenuBar();
1002 WXHICON
wxMDIChildFrame::GetDefaultIcon() const
1004 // we don't have any standard icons (any more)
1008 // ---------------------------------------------------------------------------
1010 // ---------------------------------------------------------------------------
1012 void wxMDIChildFrame::Maximize(bool maximize
)
1014 wxMDIParentFrame
* const parent
= GetMDIParent();
1015 if ( parent
&& parent
->GetClientWindow() )
1017 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
1018 maximize
? WM_MDIMAXIMIZE
: WM_MDIRESTORE
,
1019 (WPARAM
)GetHwnd(), 0);
1023 void wxMDIChildFrame::Restore()
1025 wxMDIParentFrame
* const parent
= GetMDIParent();
1026 if ( parent
&& parent
->GetClientWindow() )
1028 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIRESTORE
,
1029 (WPARAM
) GetHwnd(), 0);
1033 void wxMDIChildFrame::Activate()
1035 wxMDIParentFrame
* const parent
= GetMDIParent();
1036 if ( parent
&& parent
->GetClientWindow() )
1038 // Activating an iconized MDI frame doesn't do anything, so restore it
1039 // first to really present it to the user.
1043 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIACTIVATE
,
1044 (WPARAM
) GetHwnd(), 0);
1048 // ---------------------------------------------------------------------------
1049 // MDI window proc and message handlers
1050 // ---------------------------------------------------------------------------
1052 WXLRESULT
wxMDIChildFrame::MSWWindowProc(WXUINT message
,
1057 bool processed
= false;
1061 case WM_GETMINMAXINFO
:
1062 processed
= HandleGetMinMaxInfo((MINMAXINFO
*)lParam
);
1065 case WM_MDIACTIVATE
:
1068 WXHWND hwndAct
, hwndDeact
;
1069 UnpackMDIActivate(wParam
, lParam
, &act
, &hwndAct
, &hwndDeact
);
1071 processed
= HandleMDIActivate(act
, hwndAct
, hwndDeact
);
1076 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
1077 // scrollbars if necessary
1082 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
1084 MSWDefWindowProc(message
, wParam
, lParam
);
1087 case WM_WINDOWPOSCHANGING
:
1088 processed
= HandleWindowPosChanging((LPWINDOWPOS
)lParam
);
1093 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
1098 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate
),
1102 wxMDIParentFrame
* const parent
= GetMDIParent();
1104 WXHMENU hMenuToSet
= 0;
1108 if ( m_hWnd
== hwndAct
)
1111 parent
->SetActiveChild(this);
1113 WXHMENU hMenuChild
= m_hMenu
;
1115 hMenuToSet
= hMenuChild
;
1117 else if ( m_hWnd
== hwndDeact
)
1119 wxASSERT_MSG( parent
->GetActiveChild() == this,
1120 wxT("can't deactivate MDI child which wasn't active!") );
1123 parent
->SetActiveChild(NULL
);
1125 WXHMENU hMenuParent
= parent
->m_hMenu
;
1127 // activate the parent menu only when there is no other child
1128 // that has been activated
1129 if ( hMenuParent
&& !hwndAct
)
1130 hMenuToSet
= hMenuParent
;
1134 // we have nothing to do with it
1140 MDISetMenu(parent
->GetClientWindow(),
1141 (HMENU
)hMenuToSet
, GetMDIWindowMenu(parent
));
1144 wxActivateEvent
event(wxEVT_ACTIVATE
, activated
, m_windowId
);
1145 event
.SetEventObject( this );
1147 ResetWindowStyle(NULL
);
1149 return HandleWindowEvent(event
);
1152 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos
)
1154 WINDOWPOS
*lpPos
= (WINDOWPOS
*)pos
;
1156 if (!(lpPos
->flags
& SWP_NOSIZE
))
1159 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1160 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1161 if (ResetWindowStyle((void *) & rectClient
) && (dwStyle
& WS_MAXIMIZE
))
1163 ::AdjustWindowRectEx(&rectClient
, dwStyle
, false, dwExStyle
);
1164 lpPos
->x
= rectClient
.left
;
1165 lpPos
->y
= rectClient
.top
;
1166 lpPos
->cx
= rectClient
.right
- rectClient
.left
;
1167 lpPos
->cy
= rectClient
.bottom
- rectClient
.top
;
1174 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo
)
1176 MINMAXINFO
*info
= (MINMAXINFO
*)mmInfo
;
1178 // let the default window proc calculate the size of MDI children
1179 // frames because it is based on the size of the MDI client window,
1180 // not on the values specified in wxWindow m_max variables
1181 bool processed
= MSWDefWindowProc(WM_GETMINMAXINFO
, 0, (LPARAM
)mmInfo
) != 0;
1183 int minWidth
= GetMinWidth(),
1184 minHeight
= GetMinHeight();
1186 // but allow GetSizeHints() to set the min size
1187 if ( minWidth
!= wxDefaultCoord
)
1189 info
->ptMinTrackSize
.x
= minWidth
;
1194 if ( minHeight
!= wxDefaultCoord
)
1196 info
->ptMinTrackSize
.y
= minHeight
;
1204 // ---------------------------------------------------------------------------
1205 // MDI specific message translation/preprocessing
1206 // ---------------------------------------------------------------------------
1208 WXLRESULT
wxMDIChildFrame::MSWDefWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1210 return DefMDIChildProc(GetHwnd(),
1211 (UINT
)message
, (WPARAM
)wParam
, (LPARAM
)lParam
);
1214 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG
* msg
)
1216 // we must pass the parent frame to ::TranslateAccelerator(), otherwise it
1217 // doesn't do its job correctly for MDI child menus
1218 return MSWDoTranslateMessage(GetMDIParent(), msg
);
1221 // ---------------------------------------------------------------------------
1223 // ---------------------------------------------------------------------------
1225 void wxMDIChildFrame::MSWDestroyWindow()
1227 wxMDIParentFrame
* const parent
= GetMDIParent();
1229 // Must make sure this handle is invalidated (set to NULL) since all sorts
1230 // of things could happen after the child client is destroyed, but before
1231 // the wxFrame is destroyed.
1233 HWND oldHandle
= (HWND
)GetHWND();
1234 SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIDESTROY
,
1235 (WPARAM
)oldHandle
, 0);
1237 if (parent
->GetActiveChild() == NULL
)
1238 ResetWindowStyle(NULL
);
1242 ::DestroyMenu((HMENU
) m_hMenu
);
1245 wxRemoveHandleAssociation(this);
1249 // Change the client window's extended style so we don't get a client edge
1250 // style when a child is maximised (a double border looks silly.)
1251 bool wxMDIChildFrame::ResetWindowStyle(void *vrect
)
1253 RECT
*rect
= (RECT
*)vrect
;
1254 wxMDIParentFrame
* const pFrameWnd
= GetMDIParent();
1255 wxMDIChildFrame
* pChild
= pFrameWnd
->GetActiveChild();
1257 if (!pChild
|| (pChild
== this))
1259 HWND hwndClient
= GetWinHwnd(pFrameWnd
->GetClientWindow());
1260 DWORD dwStyle
= ::GetWindowLong(hwndClient
, GWL_EXSTYLE
);
1262 // we want to test whether there is a maximized child, so just set
1263 // dwThisStyle to 0 if there is no child at all
1264 DWORD dwThisStyle
= pChild
1265 ? ::GetWindowLong(GetWinHwnd(pChild
), GWL_STYLE
) : 0;
1266 DWORD dwNewStyle
= dwStyle
;
1267 if ( dwThisStyle
& WS_MAXIMIZE
)
1268 dwNewStyle
&= ~(WS_EX_CLIENTEDGE
);
1270 dwNewStyle
|= WS_EX_CLIENTEDGE
;
1272 if (dwStyle
!= dwNewStyle
)
1274 // force update of everything
1275 ::RedrawWindow(hwndClient
, NULL
, NULL
,
1276 RDW_INVALIDATE
| RDW_ALLCHILDREN
);
1277 ::SetWindowLong(hwndClient
, GWL_EXSTYLE
, dwNewStyle
);
1278 ::SetWindowPos(hwndClient
, NULL
, 0, 0, 0, 0,
1279 SWP_FRAMECHANGED
| SWP_NOACTIVATE
|
1280 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1283 ::GetClientRect(hwndClient
, rect
);
1292 // ===========================================================================
1293 // wxMDIClientWindow: the window of predefined (by Windows) class which
1294 // contains the child frames
1295 // ===========================================================================
1297 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
1299 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1301 CLIENTCREATESTRUCT ccs
;
1302 m_windowStyle
= style
;
1305 ccs
.hWindowMenu
= GetMDIWindowMenu(parent
);
1306 ccs
.idFirstChild
= wxFIRST_MDI_CHILD
;
1308 DWORD msStyle
= MDIS_ALLCHILDSTYLES
| WS_VISIBLE
| WS_CHILD
|
1309 WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
;
1311 if ( style
& wxHSCROLL
)
1312 msStyle
|= WS_HSCROLL
;
1313 if ( style
& wxVSCROLL
)
1314 msStyle
|= WS_VSCROLL
;
1316 DWORD exStyle
= WS_EX_CLIENTEDGE
;
1318 wxWindowCreationHook
hook(this);
1319 m_hWnd
= (WXHWND
)::CreateWindowEx
1329 (LPSTR
)(LPCLIENTCREATESTRUCT
)&ccs
);
1332 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1337 SubclassWin(m_hWnd
);
1342 // Explicitly call default scroll behaviour
1343 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)
1345 // Note: for client windows, the scroll position is not set in
1346 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1347 // scroll position we're at.
1348 // This makes it hard to paint patterns or bitmaps in the background,
1349 // and have the client area scrollable as well.
1351 if ( event
.GetOrientation() == wxHORIZONTAL
)
1352 m_scrollX
= event
.GetPosition(); // Always returns zero!
1354 m_scrollY
= event
.GetPosition(); // Always returns zero!
1359 void wxMDIClientWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1361 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1362 // client area, you can end up with a non-refreshed portion in the client window
1363 // (see OGL studio sample). So check if the position is changed and if so,
1364 // redraw the MDI child frames.
1366 const wxPoint oldPos
= GetPosition();
1368 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
| wxSIZE_FORCE
);
1370 const wxPoint newPos
= GetPosition();
1372 if ((newPos
.x
!= oldPos
.x
) || (newPos
.y
!= oldPos
.y
))
1376 wxWindowList::compatibility_iterator node
= GetParent()->GetChildren().GetFirst();
1379 wxWindow
*child
= node
->GetData();
1380 if (wxDynamicCast(child
, wxMDIChildFrame
))
1382 ::RedrawWindow(GetHwndOf(child
),
1389 node
= node
->GetNext();
1395 void wxMDIChildFrame::OnIdle(wxIdleEvent
& event
)
1397 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1398 // in flicker e.g. when the frame contained controls with non-trivial
1399 // layout. Since 2.5.3, the frame is created hidden as all other top level
1400 // windows. In order to maintain backward compatibility, the frame is shown
1401 // in OnIdle, unless Show(false) was called by the programmer before.
1402 if ( m_needsInitialShow
)
1407 // MDI child frames get their WM_SIZE when they're constructed but at this
1408 // moment they don't have any children yet so all child windows will be
1409 // positioned incorrectly when they are added later - to fix this, we
1410 // generate an artificial size event here
1411 if ( m_needsResize
)
1413 m_needsResize
= false; // avoid any possibility of recursion
1421 // ---------------------------------------------------------------------------
1422 // private helper functions
1423 // ---------------------------------------------------------------------------
1428 void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
)
1430 if ( hmenuFrame
|| hmenuWindow
)
1432 if ( !::SendMessage(GetWinHwnd(win
),
1435 (LPARAM
)hmenuWindow
) )
1437 DWORD err
= ::GetLastError();
1440 wxLogApiError(wxT("SendMessage(WM_MDISETMENU)"), err
);
1445 // update menu bar of the parent window
1446 wxWindow
*parent
= win
->GetParent();
1447 wxCHECK_RET( parent
, wxT("MDI client without parent frame? weird...") );
1449 ::SendMessage(GetWinHwnd(win
), WM_MDIREFRESHMENU
, 0, 0L);
1451 ::DrawMenuBar(GetWinHwnd(parent
));
1454 void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU hMenu
, HMENU menuWin
)
1456 HMENU hmenu
= (HMENU
)hMenu
;
1460 // Try to insert Window menu in front of Help, otherwise append it.
1461 int N
= GetMenuItemCount(hmenu
);
1462 bool inserted
= false;
1463 for ( int i
= 0; i
< N
; i
++ )
1466 if ( !::GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1468 wxLogLastError(wxT("GetMenuString"));
1473 const wxString label
= wxStripMenuCodes(buf
);
1474 if ( label
== wxGetStockLabel(wxID_HELP
, wxSTOCK_NOFLAGS
) )
1477 ::InsertMenu(hmenu
, i
, MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1479 wxString(wxGetTranslation(WINDOW_MENU_LABEL
)).t_str());
1486 ::AppendMenu(hmenu
, MF_POPUP
,
1488 wxString(wxGetTranslation(WINDOW_MENU_LABEL
)).t_str());
1492 MDISetMenu(win
, hmenu
, menuWin
);
1495 void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU hMenu
)
1497 HMENU hmenu
= (HMENU
)hMenu
;
1503 int N
= ::GetMenuItemCount(hmenu
);
1504 for ( int i
= 0; i
< N
; i
++ )
1506 if ( !::GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1508 // Ignore successful read of menu string with length 0 which
1509 // occurs, for example, for a maximized MDI child system menu
1510 if ( ::GetLastError() != 0 )
1512 wxLogLastError(wxT("GetMenuString"));
1518 if ( wxStrcmp(buf
, wxGetTranslation(WINDOW_MENU_LABEL
)) == 0 )
1520 if ( !::RemoveMenu(hmenu
, i
, MF_BYPOSITION
) )
1522 wxLogLastError(wxT("RemoveMenu"));
1532 // we don't change the windows menu, but we update the main one
1533 MDISetMenu(win
, hmenu
, NULL
);
1537 void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
1538 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
)
1541 *hwndAct
= (WXHWND
)lParam
;
1542 *hwndDeact
= (WXHWND
)wParam
;
1545 } // anonymous namespace
1547 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)