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 // First ID for the MDI child menu item in the "Window" menu.
66 const int wxFIRST_MDI_CHILD
= 4100;
68 // There can be no more than 9 children in the "Window" menu as beginning with
69 // the tenth one they're not shown and "More windows..." menu item is used
71 const int wxLAST_MDI_CHILD
= wxFIRST_MDI_CHILD
+ 8;
73 // The ID of the "More windows..." menu item is the next one after the last
75 const int wxID_MDI_MORE_WINDOWS
= wxLAST_MDI_CHILD
+ 1;
77 // The MDI "Window" menu label
78 const char *WINDOW_MENU_LABEL
= gettext_noop("&Window");
80 // ---------------------------------------------------------------------------
82 // ---------------------------------------------------------------------------
84 // set the MDI menus (by sending the WM_MDISETMENU message) and update the menu
85 // of the parent of win (which is supposed to be the MDI client window)
86 void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
);
88 // insert the window menu (subMenu) into menu just before "Help" submenu or at
89 // the very end if not found
90 void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU hMenu
, HMENU subMenu
);
92 // Remove the window menu
93 void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU hMenu
);
95 // unpack the parameters of WM_MDIACTIVATE message
96 void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
97 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
);
99 // return the HMENU of the MDI menu
101 // this function works correctly even when we don't have a window menu and just
103 inline HMENU
GetMDIWindowMenu(wxMDIParentFrame
*frame
)
105 wxMenu
*menu
= frame
->GetWindowMenu();
106 return menu
? GetHmenuOf(menu
) : 0;
109 } // anonymous namespace
111 // ===========================================================================
113 // ===========================================================================
115 // ---------------------------------------------------------------------------
117 // ---------------------------------------------------------------------------
119 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxFrame
)
120 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxFrame
)
121 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxWindow
)
123 BEGIN_EVENT_TABLE(wxMDIParentFrame
, wxFrame
)
124 EVT_SIZE(wxMDIParentFrame::OnSize
)
125 EVT_ICONIZE(wxMDIParentFrame::OnIconized
)
126 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged
)
129 EVT_MENU_RANGE(wxFIRST_MDI_CHILD
, wxLAST_MDI_CHILD
,
130 wxMDIParentFrame::OnMDIChild
)
131 EVT_MENU_RANGE(wxID_MDI_WINDOW_FIRST
, wxID_MDI_WINDOW_LAST
,
132 wxMDIParentFrame::OnMDICommand
)
133 #endif // wxUSE_MENUS
136 BEGIN_EVENT_TABLE(wxMDIChildFrame
, wxFrame
)
137 EVT_IDLE(wxMDIChildFrame::OnIdle
)
140 BEGIN_EVENT_TABLE(wxMDIClientWindow
, wxWindow
)
141 EVT_SCROLL(wxMDIClientWindow::OnScroll
)
144 // ===========================================================================
145 // wxMDIParentFrame: the frame which contains the client window which manages
147 // ===========================================================================
149 void wxMDIParentFrame::Init()
151 #if wxUSE_MENUS && wxUSE_ACCEL
152 // the default menu doesn't have any accelerators (even if we have it)
153 m_accelWindowMenu
= NULL
;
154 #endif // wxUSE_MENUS && wxUSE_ACCEL
157 bool wxMDIParentFrame::Create(wxWindow
*parent
,
159 const wxString
& title
,
163 const wxString
& name
)
165 // this style can be used to prevent a window from having the standard MDI
167 if ( !(style
& wxFRAME_NO_WINDOW_MENU
) )
169 // normal case: we have the window menu, so construct it
170 m_windowMenu
= new wxMenu
;
172 m_windowMenu
->Append(wxID_MDI_WINDOW_CASCADE
, _("&Cascade"));
173 m_windowMenu
->Append(wxID_MDI_WINDOW_TILE_HORZ
, _("Tile &Horizontally"));
174 m_windowMenu
->Append(wxID_MDI_WINDOW_TILE_VERT
, _("Tile &Vertically"));
175 m_windowMenu
->AppendSeparator();
176 m_windowMenu
->Append(wxID_MDI_WINDOW_ARRANGE_ICONS
, _("&Arrange Icons"));
177 m_windowMenu
->Append(wxID_MDI_WINDOW_NEXT
, _("&Next"));
178 m_windowMenu
->Append(wxID_MDI_WINDOW_PREV
, _("&Previous"));
182 wxTopLevelWindows
.Append(this);
185 m_windowStyle
= style
;
188 parent
->AddChild(this);
190 if ( id
!= wxID_ANY
)
193 m_windowId
= NewControlId();
196 WXDWORD msflags
= MSWGetCreateWindowFlags(&exflags
);
197 msflags
&= ~WS_VSCROLL
;
198 msflags
&= ~WS_HSCROLL
;
200 if ( !wxWindow::MSWCreate(wxApp::GetRegisteredClassName(wxT("wxMDIFrame")),
209 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
211 // unlike (almost?) all other windows, frames are created hidden
217 wxMDIParentFrame::~wxMDIParentFrame()
219 // see comment in ~wxMDIChildFrame
221 m_frameToolBar
= NULL
;
224 m_frameStatusBar
= NULL
;
225 #endif // wxUSE_STATUSBAR
227 #if wxUSE_MENUS && wxUSE_ACCEL
228 delete m_accelWindowMenu
;
229 #endif // wxUSE_MENUS && wxUSE_ACCEL
233 // the MDI frame menubar is not automatically deleted by Windows unlike for
236 ::DestroyMenu((HMENU
)m_hMenu
);
238 if ( m_clientWindow
)
240 if ( m_clientWindow
->MSWGetOldWndProc() )
241 m_clientWindow
->UnsubclassWin();
243 m_clientWindow
->SetHWND(0);
244 delete m_clientWindow
;
248 // ----------------------------------------------------------------------------
249 // wxMDIParentFrame child management
250 // ----------------------------------------------------------------------------
252 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
254 HWND hWnd
= (HWND
)::SendMessage(GetWinHwnd(GetClientWindow()),
255 WM_MDIGETACTIVE
, 0, 0L);
259 return static_cast<wxMDIChildFrame
*>(wxFindWinFromHandle(hWnd
));
262 int wxMDIParentFrame::GetChildFramesCount() const
265 for ( wxWindowList::const_iterator i
= GetChildren().begin();
266 i
!= GetChildren().end();
269 if ( wxDynamicCast(*i
, wxMDIChildFrame
) )
278 void wxMDIParentFrame::AddMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
280 switch ( GetChildFramesCount() )
283 // first MDI child was just added, we need to insert the window
284 // menu now if we have it
287 // and disable the items which can't be used until we have more
289 UpdateWindowMenu(false);
293 // second MDI child was added, enable the menu items which were
294 // disabled because they didn't make sense for a single window
295 UpdateWindowMenu(true);
300 void wxMDIParentFrame::RemoveMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
302 switch ( GetChildFramesCount() )
305 // last MDI child is being removed, remove the now unnecessary
309 // there is no need to call UpdateWindowMenu(true) here so this is
310 // not quite symmetric to AddMDIChild() above
314 // only one MDI child is going to remain, disable the menu commands
315 // which don't make sense for a single child window
316 UpdateWindowMenu(false);
321 // ----------------------------------------------------------------------------
322 // wxMDIParentFrame window menu handling
323 // ----------------------------------------------------------------------------
325 void wxMDIParentFrame::AddWindowMenu()
329 // For correct handling of the events from this menu we also must
330 // attach it to the menu bar.
331 m_windowMenu
->Attach(GetMenuBar());
333 MDIInsertWindowMenu(GetClientWindow(), m_hMenu
, GetMDIWindowMenu(this));
337 void wxMDIParentFrame::RemoveWindowMenu()
341 MDIRemoveWindowMenu(GetClientWindow(), m_hMenu
);
343 m_windowMenu
->Detach();
347 void wxMDIParentFrame::UpdateWindowMenu(bool enable
)
351 m_windowMenu
->Enable(wxID_MDI_WINDOW_NEXT
, enable
);
352 m_windowMenu
->Enable(wxID_MDI_WINDOW_PREV
, enable
);
356 #if wxUSE_MENUS_NATIVE
358 void wxMDIParentFrame::InternalSetMenuBar()
360 if ( GetActiveChild() )
364 else // we don't have any MDI children yet
366 // wait until we do to add the window menu but do set the main menu for
367 // now (this is done by AddWindowMenu() as a side effect)
368 MDISetMenu(GetClientWindow(), (HMENU
)m_hMenu
, NULL
);
372 #endif // wxUSE_MENUS_NATIVE
374 void wxMDIParentFrame::SetWindowMenu(wxMenu
* menu
)
376 if ( menu
!= m_windowMenu
)
378 // notice that Remove/AddWindowMenu() are safe to call even when
379 // m_windowMenu is NULL
390 wxDELETE(m_accelWindowMenu
);
392 if ( menu
&& menu
->HasAccels() )
393 m_accelWindowMenu
= menu
->CreateAccelTable();
394 #endif // wxUSE_ACCEL
397 // ----------------------------------------------------------------------------
398 // wxMDIParentFrame other menu-related stuff
399 // ----------------------------------------------------------------------------
401 void wxMDIParentFrame::DoMenuUpdates(wxMenu
* menu
)
403 wxMDIChildFrame
*child
= GetActiveChild();
406 wxEvtHandler
* source
= child
->GetEventHandler();
407 wxMenuBar
* bar
= child
->GetMenuBar();
411 menu
->UpdateUI(source
);
417 int nCount
= bar
->GetMenuCount();
418 for (int n
= 0; n
< nCount
; n
++)
419 bar
->GetMenu(n
)->UpdateUI(source
);
425 wxFrameBase::DoMenuUpdates(menu
);
429 wxMenuItem
*wxMDIParentFrame::FindItemInMenuBar(int menuId
) const
431 wxMenuItem
*item
= wxFrame::FindItemInMenuBar(menuId
);
432 if ( !item
&& GetActiveChild() )
434 item
= GetActiveChild()->FindItemInMenuBar(menuId
);
437 if ( !item
&& m_windowMenu
)
438 item
= m_windowMenu
->FindItem(menuId
);
443 WXHMENU
wxMDIParentFrame::MSWGetActiveMenu() const
445 wxMDIChildFrame
* const child
= GetActiveChild();
448 const WXHMENU hmenu
= child
->MSWGetActiveMenu();
453 return wxFrame::MSWGetActiveMenu();
456 #endif // wxUSE_MENUS
458 // ----------------------------------------------------------------------------
459 // wxMDIParentFrame event handling
460 // ----------------------------------------------------------------------------
462 void wxMDIParentFrame::UpdateClientSize()
464 if ( GetClientWindow() )
467 GetClientSize(&width
, &height
);
469 GetClientWindow()->SetSize(0, 0, width
, height
);
473 void wxMDIParentFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
477 // do not call event.Skip() here, it somehow messes up MDI client window
480 void wxMDIParentFrame::OnIconized(wxIconizeEvent
& event
)
484 if ( !event
.IsIconized() )
488 // Responds to colour changes, and passes event on to children.
489 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
491 if ( m_clientWindow
)
493 m_clientWindow
->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
494 m_clientWindow
->Refresh();
500 WXHICON
wxMDIParentFrame::GetDefaultIcon() const
502 // we don't have any standard icons (any more)
506 // ---------------------------------------------------------------------------
508 // ---------------------------------------------------------------------------
510 void wxMDIParentFrame::Cascade()
512 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE
, 0, 0);
515 void wxMDIParentFrame::Tile(wxOrientation orient
)
517 wxASSERT_MSG( orient
== wxHORIZONTAL
|| orient
== wxVERTICAL
,
518 wxT("invalid orientation value") );
520 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE
,
521 orient
== wxHORIZONTAL
? MDITILE_HORIZONTAL
522 : MDITILE_VERTICAL
, 0);
525 void wxMDIParentFrame::ArrangeIcons()
527 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE
, 0, 0);
530 void wxMDIParentFrame::ActivateNext()
532 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 0);
535 void wxMDIParentFrame::ActivatePrevious()
537 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 1);
540 // ---------------------------------------------------------------------------
541 // the MDI parent frame window proc
542 // ---------------------------------------------------------------------------
544 WXLRESULT
wxMDIParentFrame::MSWWindowProc(WXUINT message
,
549 bool processed
= false;
555 WXWORD state
, minimized
;
557 UnpackActivate(wParam
, lParam
, &state
, &minimized
, &hwnd
);
559 processed
= HandleActivate(state
, minimized
!= 0, hwnd
);
564 // system messages such as SC_CLOSE are sent as WM_COMMANDs to the
565 // parent MDI frame and we must let the DefFrameProc() have them
566 // for these commands to work (without it, closing the maximized
567 // MDI children doesn't work, for example)
571 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
573 if ( id
== wxID_MDI_MORE_WINDOWS
||
574 (cmd
== 0 /* menu */ &&
575 id
>= SC_SIZE
/* first system menu command */) )
577 MSWDefWindowProc(message
, wParam
, lParam
);
584 m_clientWindow
= OnCreateClient();
585 // Uses own style for client style
586 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
588 wxLogMessage(_("Failed to create MDI parent frame."));
598 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
603 bool wxMDIParentFrame::HandleActivate(int state
, bool minimized
, WXHWND activate
)
605 bool processed
= false;
607 if ( wxWindow::HandleActivate(state
, minimized
, activate
) )
613 // If this window is an MDI parent, we must also send an OnActivate message
614 // to the current child.
615 if ( GetActiveChild() &&
616 ((state
== WA_ACTIVE
) || (state
== WA_CLICKACTIVE
)) )
618 wxActivateEvent
event(wxEVT_ACTIVATE
, true, GetActiveChild()->GetId());
619 event
.SetEventObject( GetActiveChild() );
620 if ( GetActiveChild()->HandleWindowEvent(event
) )
629 void wxMDIParentFrame::OnMDIChild(wxCommandEvent
& event
)
631 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
634 wxWindow
*child
= node
->GetData();
635 if ( child
->GetHWND() )
637 int childId
= wxGetWindowId(child
->GetHWND());
638 if ( childId
== event
.GetId() )
640 wxStaticCast(child
, wxMDIChildFrame
)->Activate();
645 node
= node
->GetNext();
648 wxFAIL_MSG( "unknown MDI child selected?" );
651 void wxMDIParentFrame::OnMDICommand(wxCommandEvent
& event
)
656 switch ( event
.GetId() )
658 case wxID_MDI_WINDOW_CASCADE
:
660 wParam
= MDITILE_SKIPDISABLED
;
663 case wxID_MDI_WINDOW_TILE_HORZ
:
664 wParam
|= MDITILE_HORIZONTAL
;
667 case wxID_MDI_WINDOW_TILE_VERT
:
669 wParam
= MDITILE_VERTICAL
;
671 wParam
|= MDITILE_SKIPDISABLED
;
674 case wxID_MDI_WINDOW_ARRANGE_ICONS
:
675 msg
= WM_MDIICONARRANGE
;
678 case wxID_MDI_WINDOW_NEXT
:
680 lParam
= 0; // next child
683 case wxID_MDI_WINDOW_PREV
:
685 lParam
= 1; // previous child
689 wxFAIL_MSG( "unknown MDI command" );
693 ::SendMessage(GetWinHwnd(GetClientWindow()), msg
, wParam
, lParam
);
696 #endif // wxUSE_MENUS
698 bool wxMDIParentFrame::TryBefore(wxEvent
& event
)
700 // menu (and toolbar) events should be sent to the active child frame
702 if ( event
.GetEventType() == wxEVT_COMMAND_MENU_SELECTED
)
704 wxMDIChildFrame
* const child
= GetActiveChild();
705 if ( child
&& child
->ProcessWindowEventLocally(event
) )
709 return wxMDIParentFrameBase::TryBefore(event
);
712 WXLRESULT
wxMDIParentFrame::MSWDefWindowProc(WXUINT message
,
717 if ( GetClientWindow() )
718 clientWnd
= GetClientWindow()->GetHWND();
722 return DefFrameProc(GetHwnd(), (HWND
)clientWnd
, message
, wParam
, lParam
);
725 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG
* msg
)
727 MSG
*pMsg
= (MSG
*)msg
;
729 // first let the current child get it
730 wxMDIChildFrame
* const child
= GetActiveChild();
731 if ( child
&& child
->MSWTranslateMessage(msg
) )
736 // then try out accelerator table (will also check the accelerators for the
737 // normal menu items)
738 if ( wxFrame::MSWTranslateMessage(msg
) )
743 #if wxUSE_MENUS && wxUSE_ACCEL
744 // but it doesn't check for the (custom) accelerators of the window menu
745 // items as it's not part of the menu bar as it's handled by Windows itself
746 // so we need to do this explicitly
747 if ( m_accelWindowMenu
&& m_accelWindowMenu
->Translate(this, msg
) )
749 #endif // wxUSE_MENUS && wxUSE_ACCEL
751 // finally, check for MDI specific built-in accelerators
752 if ( pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
754 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg
))
761 // ===========================================================================
763 // ===========================================================================
765 void wxMDIChildFrame::Init()
767 m_needsResize
= true;
768 m_needsInitialShow
= true;
771 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
773 const wxString
& title
,
777 const wxString
& name
)
779 m_mdiParent
= parent
;
783 if ( id
!= wxID_ANY
)
786 m_windowId
= NewControlId();
790 parent
->AddChild(this);
801 wxApp::GetRegisteredClassName(wxT("wxMDIChildFrame"), COLOR_WINDOW
);
802 if ( !(style
& wxFULL_REPAINT_ON_RESIZE
) )
803 className
+= wxApp::GetNoRedrawClassSuffix();
805 mcs
.szClass
= className
.t_str();
806 mcs
.szTitle
= title
.t_str();
807 mcs
.hOwner
= wxGetInstance();
808 if (x
!= wxDefaultCoord
)
811 mcs
.x
= CW_USEDEFAULT
;
813 if (y
!= wxDefaultCoord
)
816 mcs
.y
= CW_USEDEFAULT
;
818 if (width
!= wxDefaultCoord
)
821 mcs
.cx
= CW_USEDEFAULT
;
823 if (height
!= wxDefaultCoord
)
826 mcs
.cy
= CW_USEDEFAULT
;
828 DWORD msflags
= WS_OVERLAPPED
| WS_CLIPCHILDREN
;
829 if (style
& wxMINIMIZE_BOX
)
830 msflags
|= WS_MINIMIZEBOX
;
831 if (style
& wxMAXIMIZE_BOX
)
832 msflags
|= WS_MAXIMIZEBOX
;
833 if (style
& wxRESIZE_BORDER
)
834 msflags
|= WS_THICKFRAME
;
835 if (style
& wxSYSTEM_MENU
)
836 msflags
|= WS_SYSMENU
;
837 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
838 msflags
|= WS_MINIMIZE
;
839 if (style
& wxMAXIMIZE
)
840 msflags
|= WS_MAXIMIZE
;
841 if (style
& wxCAPTION
)
842 msflags
|= WS_CAPTION
;
848 wxWindowCreationHook
hook(this);
850 m_hWnd
= (WXHWND
)::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
851 WM_MDICREATE
, 0, (LPARAM
)&mcs
);
855 wxLogLastError(wxT("WM_MDICREATE"));
861 parent
->AddMDIChild(this);
866 wxMDIChildFrame::~wxMDIChildFrame()
868 // if we hadn't been created, there is nothing to destroy
872 GetMDIParent()->RemoveMDIChild(this);
874 // will be destroyed by DestroyChildren() but reset them before calling it
875 // to avoid using dangling pointers if a callback comes in the meanwhile
877 m_frameToolBar
= NULL
;
880 m_frameStatusBar
= NULL
;
881 #endif // wxUSE_STATUSBAR
885 MDIRemoveWindowMenu(NULL
, m_hMenu
);
890 bool wxMDIChildFrame::Show(bool show
)
892 m_needsInitialShow
= false;
894 if (!wxFrame::Show(show
))
897 // KH: Without this call, new MDI children do not become active.
898 // This was added here after the same BringWindowToTop call was
899 // removed from wxTopLevelWindow::Show (November 2005)
901 ::BringWindowToTop(GetHwnd());
903 // we need to refresh the MDI frame window menu to include (or exclude if
904 // we've been hidden) this frame
905 wxMDIParentFrame
* const parent
= GetMDIParent();
906 MDISetMenu(parent
->GetClientWindow(), NULL
, NULL
);
912 wxMDIChildFrame::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
914 // we need to disable client area origin adjustments used for the child
915 // windows for the frame itself
916 wxMDIChildFrameBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
919 // Set the client size (i.e. leave the calculation of borders etc.
921 void wxMDIChildFrame::DoSetClientSize(int width
, int height
)
923 HWND hWnd
= GetHwnd();
926 ::GetClientRect(hWnd
, &rect
);
929 GetWindowRect(hWnd
, &rect2
);
931 // Find the difference between the entire window (title bar and all)
932 // and the client area; add this to the new client size to move the
934 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
935 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
938 if (GetStatusBar() && GetStatusBar()->IsShown())
941 GetStatusBar()->GetSize(&sx
, &sy
);
944 #endif // wxUSE_STATUSBAR
947 point
.x
= rect2
.left
;
950 // If there's an MDI parent, must subtract the parent's top left corner
951 // since MoveWindow moves relative to the parent
952 wxMDIParentFrame
* const mdiParent
= GetMDIParent();
953 ::ScreenToClient(GetHwndOf(mdiParent
->GetClientWindow()), &point
);
955 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)true);
957 wxSize
size(width
, height
);
958 wxSizeEvent
event(size
, m_windowId
);
959 event
.SetEventObject( this );
960 HandleWindowEvent(event
);
963 // Unlike other wxTopLevelWindowBase, the mdi child's "GetPosition" is not the
964 // same as its GetScreenPosition
965 void wxMDIChildFrame::DoGetScreenPosition(int *x
, int *y
) const
967 HWND hWnd
= GetHwnd();
970 ::GetWindowRect(hWnd
, &rect
);
978 void wxMDIChildFrame::DoGetPosition(int *x
, int *y
) const
981 GetWindowRect(GetHwnd(), &rect
);
986 // Since we now have the absolute screen coords,
987 // if there's a parent we must subtract its top left corner
988 wxMDIParentFrame
* const mdiParent
= GetMDIParent();
989 ::ScreenToClient(GetHwndOf(mdiParent
->GetClientWindow()), &point
);
997 void wxMDIChildFrame::InternalSetMenuBar()
999 wxMDIParentFrame
* const parent
= GetMDIParent();
1001 MDIInsertWindowMenu(parent
->GetClientWindow(),
1002 m_hMenu
, GetMDIWindowMenu(parent
));
1005 void wxMDIChildFrame::DetachMenuBar()
1007 MDIRemoveWindowMenu(NULL
, m_hMenu
);
1008 wxFrame::DetachMenuBar();
1011 WXHICON
wxMDIChildFrame::GetDefaultIcon() const
1013 // we don't have any standard icons (any more)
1017 // ---------------------------------------------------------------------------
1019 // ---------------------------------------------------------------------------
1021 void wxMDIChildFrame::Maximize(bool maximize
)
1023 wxMDIParentFrame
* const parent
= GetMDIParent();
1024 if ( parent
&& parent
->GetClientWindow() )
1026 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
1027 maximize
? WM_MDIMAXIMIZE
: WM_MDIRESTORE
,
1028 (WPARAM
)GetHwnd(), 0);
1032 void wxMDIChildFrame::Restore()
1034 wxMDIParentFrame
* const parent
= GetMDIParent();
1035 if ( parent
&& parent
->GetClientWindow() )
1037 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIRESTORE
,
1038 (WPARAM
) GetHwnd(), 0);
1042 void wxMDIChildFrame::Activate()
1044 wxMDIParentFrame
* const parent
= GetMDIParent();
1045 if ( parent
&& parent
->GetClientWindow() )
1047 // Activating an iconized MDI frame doesn't do anything, so restore it
1048 // first to really present it to the user.
1052 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIACTIVATE
,
1053 (WPARAM
) GetHwnd(), 0);
1057 // ---------------------------------------------------------------------------
1058 // MDI window proc and message handlers
1059 // ---------------------------------------------------------------------------
1061 WXLRESULT
wxMDIChildFrame::MSWWindowProc(WXUINT message
,
1066 bool processed
= false;
1070 case WM_GETMINMAXINFO
:
1071 processed
= HandleGetMinMaxInfo((MINMAXINFO
*)lParam
);
1074 case WM_MDIACTIVATE
:
1077 WXHWND hwndAct
, hwndDeact
;
1078 UnpackMDIActivate(wParam
, lParam
, &act
, &hwndAct
, &hwndDeact
);
1080 processed
= HandleMDIActivate(act
, hwndAct
, hwndDeact
);
1085 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
1086 // scrollbars if necessary
1091 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
1093 MSWDefWindowProc(message
, wParam
, lParam
);
1096 case WM_WINDOWPOSCHANGING
:
1097 processed
= HandleWindowPosChanging((LPWINDOWPOS
)lParam
);
1102 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
1107 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate
),
1111 wxMDIParentFrame
* const parent
= GetMDIParent();
1113 WXHMENU hMenuToSet
= 0;
1117 if ( m_hWnd
== hwndAct
)
1120 parent
->SetActiveChild(this);
1122 WXHMENU hMenuChild
= m_hMenu
;
1124 hMenuToSet
= hMenuChild
;
1126 else if ( m_hWnd
== hwndDeact
)
1128 wxASSERT_MSG( parent
->GetActiveChild() == this,
1129 wxT("can't deactivate MDI child which wasn't active!") );
1132 parent
->SetActiveChild(NULL
);
1134 WXHMENU hMenuParent
= parent
->m_hMenu
;
1136 // activate the parent menu only when there is no other child
1137 // that has been activated
1138 if ( hMenuParent
&& !hwndAct
)
1139 hMenuToSet
= hMenuParent
;
1143 // we have nothing to do with it
1149 MDISetMenu(parent
->GetClientWindow(),
1150 (HMENU
)hMenuToSet
, GetMDIWindowMenu(parent
));
1153 wxActivateEvent
event(wxEVT_ACTIVATE
, activated
, m_windowId
);
1154 event
.SetEventObject( this );
1156 ResetWindowStyle(NULL
);
1158 return HandleWindowEvent(event
);
1161 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos
)
1163 WINDOWPOS
*lpPos
= (WINDOWPOS
*)pos
;
1165 if (!(lpPos
->flags
& SWP_NOSIZE
))
1168 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1169 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1170 if (ResetWindowStyle((void *) & rectClient
) && (dwStyle
& WS_MAXIMIZE
))
1172 ::AdjustWindowRectEx(&rectClient
, dwStyle
, false, dwExStyle
);
1173 lpPos
->x
= rectClient
.left
;
1174 lpPos
->y
= rectClient
.top
;
1175 lpPos
->cx
= rectClient
.right
- rectClient
.left
;
1176 lpPos
->cy
= rectClient
.bottom
- rectClient
.top
;
1183 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo
)
1185 MINMAXINFO
*info
= (MINMAXINFO
*)mmInfo
;
1187 // let the default window proc calculate the size of MDI children
1188 // frames because it is based on the size of the MDI client window,
1189 // not on the values specified in wxWindow m_max variables
1190 bool processed
= MSWDefWindowProc(WM_GETMINMAXINFO
, 0, (LPARAM
)mmInfo
) != 0;
1192 int minWidth
= GetMinWidth(),
1193 minHeight
= GetMinHeight();
1195 // but allow GetSizeHints() to set the min size
1196 if ( minWidth
!= wxDefaultCoord
)
1198 info
->ptMinTrackSize
.x
= minWidth
;
1203 if ( minHeight
!= wxDefaultCoord
)
1205 info
->ptMinTrackSize
.y
= minHeight
;
1213 // ---------------------------------------------------------------------------
1214 // MDI specific message translation/preprocessing
1215 // ---------------------------------------------------------------------------
1217 WXLRESULT
wxMDIChildFrame::MSWDefWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1219 return DefMDIChildProc(GetHwnd(),
1220 (UINT
)message
, (WPARAM
)wParam
, (LPARAM
)lParam
);
1223 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG
* msg
)
1225 // we must pass the parent frame to ::TranslateAccelerator(), otherwise it
1226 // doesn't do its job correctly for MDI child menus
1227 return MSWDoTranslateMessage(GetMDIParent(), msg
);
1230 // ---------------------------------------------------------------------------
1232 // ---------------------------------------------------------------------------
1234 void wxMDIChildFrame::MSWDestroyWindow()
1236 wxMDIParentFrame
* const parent
= GetMDIParent();
1238 // Must make sure this handle is invalidated (set to NULL) since all sorts
1239 // of things could happen after the child client is destroyed, but before
1240 // the wxFrame is destroyed.
1242 HWND oldHandle
= (HWND
)GetHWND();
1243 SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIDESTROY
,
1244 (WPARAM
)oldHandle
, 0);
1246 if (parent
->GetActiveChild() == NULL
)
1247 ResetWindowStyle(NULL
);
1251 ::DestroyMenu((HMENU
) m_hMenu
);
1254 wxRemoveHandleAssociation(this);
1258 // Change the client window's extended style so we don't get a client edge
1259 // style when a child is maximised (a double border looks silly.)
1260 bool wxMDIChildFrame::ResetWindowStyle(void *vrect
)
1262 RECT
*rect
= (RECT
*)vrect
;
1263 wxMDIParentFrame
* const pFrameWnd
= GetMDIParent();
1264 wxMDIChildFrame
* pChild
= pFrameWnd
->GetActiveChild();
1266 if (!pChild
|| (pChild
== this))
1268 HWND hwndClient
= GetWinHwnd(pFrameWnd
->GetClientWindow());
1269 DWORD dwStyle
= ::GetWindowLong(hwndClient
, GWL_EXSTYLE
);
1271 // we want to test whether there is a maximized child, so just set
1272 // dwThisStyle to 0 if there is no child at all
1273 DWORD dwThisStyle
= pChild
1274 ? ::GetWindowLong(GetWinHwnd(pChild
), GWL_STYLE
) : 0;
1275 DWORD dwNewStyle
= dwStyle
;
1276 if ( dwThisStyle
& WS_MAXIMIZE
)
1277 dwNewStyle
&= ~(WS_EX_CLIENTEDGE
);
1279 dwNewStyle
|= WS_EX_CLIENTEDGE
;
1281 if (dwStyle
!= dwNewStyle
)
1283 // force update of everything
1284 ::RedrawWindow(hwndClient
, NULL
, NULL
,
1285 RDW_INVALIDATE
| RDW_ALLCHILDREN
);
1286 ::SetWindowLong(hwndClient
, GWL_EXSTYLE
, dwNewStyle
);
1287 ::SetWindowPos(hwndClient
, NULL
, 0, 0, 0, 0,
1288 SWP_FRAMECHANGED
| SWP_NOACTIVATE
|
1289 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1292 ::GetClientRect(hwndClient
, rect
);
1301 // ===========================================================================
1302 // wxMDIClientWindow: the window of predefined (by Windows) class which
1303 // contains the child frames
1304 // ===========================================================================
1306 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
1308 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1310 CLIENTCREATESTRUCT ccs
;
1311 m_windowStyle
= style
;
1314 ccs
.hWindowMenu
= GetMDIWindowMenu(parent
);
1315 ccs
.idFirstChild
= wxFIRST_MDI_CHILD
;
1317 DWORD msStyle
= MDIS_ALLCHILDSTYLES
| WS_VISIBLE
| WS_CHILD
|
1318 WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
;
1320 if ( style
& wxHSCROLL
)
1321 msStyle
|= WS_HSCROLL
;
1322 if ( style
& wxVSCROLL
)
1323 msStyle
|= WS_VSCROLL
;
1325 DWORD exStyle
= WS_EX_CLIENTEDGE
;
1327 wxWindowCreationHook
hook(this);
1328 m_hWnd
= (WXHWND
)::CreateWindowEx
1338 (LPSTR
)(LPCLIENTCREATESTRUCT
)&ccs
);
1341 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1346 SubclassWin(m_hWnd
);
1351 // Explicitly call default scroll behaviour
1352 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)
1354 // Note: for client windows, the scroll position is not set in
1355 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1356 // scroll position we're at.
1357 // This makes it hard to paint patterns or bitmaps in the background,
1358 // and have the client area scrollable as well.
1360 if ( event
.GetOrientation() == wxHORIZONTAL
)
1361 m_scrollX
= event
.GetPosition(); // Always returns zero!
1363 m_scrollY
= event
.GetPosition(); // Always returns zero!
1368 void wxMDIClientWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1370 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1371 // client area, you can end up with a non-refreshed portion in the client window
1372 // (see OGL studio sample). So check if the position is changed and if so,
1373 // redraw the MDI child frames.
1375 const wxPoint oldPos
= GetPosition();
1377 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
| wxSIZE_FORCE
);
1379 const wxPoint newPos
= GetPosition();
1381 if ((newPos
.x
!= oldPos
.x
) || (newPos
.y
!= oldPos
.y
))
1385 wxWindowList::compatibility_iterator node
= GetParent()->GetChildren().GetFirst();
1388 wxWindow
*child
= node
->GetData();
1389 if (wxDynamicCast(child
, wxMDIChildFrame
))
1391 ::RedrawWindow(GetHwndOf(child
),
1398 node
= node
->GetNext();
1404 void wxMDIChildFrame::OnIdle(wxIdleEvent
& event
)
1406 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1407 // in flicker e.g. when the frame contained controls with non-trivial
1408 // layout. Since 2.5.3, the frame is created hidden as all other top level
1409 // windows. In order to maintain backward compatibility, the frame is shown
1410 // in OnIdle, unless Show(false) was called by the programmer before.
1411 if ( m_needsInitialShow
)
1416 // MDI child frames get their WM_SIZE when they're constructed but at this
1417 // moment they don't have any children yet so all child windows will be
1418 // positioned incorrectly when they are added later - to fix this, we
1419 // generate an artificial size event here
1420 if ( m_needsResize
)
1422 m_needsResize
= false; // avoid any possibility of recursion
1430 // ---------------------------------------------------------------------------
1431 // private helper functions
1432 // ---------------------------------------------------------------------------
1437 void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
)
1439 if ( hmenuFrame
|| hmenuWindow
)
1441 if ( !::SendMessage(GetWinHwnd(win
),
1444 (LPARAM
)hmenuWindow
) )
1446 DWORD err
= ::GetLastError();
1449 wxLogApiError(wxT("SendMessage(WM_MDISETMENU)"), err
);
1454 // update menu bar of the parent window
1455 wxWindow
*parent
= win
->GetParent();
1456 wxCHECK_RET( parent
, wxT("MDI client without parent frame? weird...") );
1458 ::SendMessage(GetWinHwnd(win
), WM_MDIREFRESHMENU
, 0, 0L);
1460 ::DrawMenuBar(GetWinHwnd(parent
));
1463 void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU hMenu
, HMENU menuWin
)
1465 HMENU hmenu
= (HMENU
)hMenu
;
1469 // Try to insert Window menu in front of Help, otherwise append it.
1470 int N
= GetMenuItemCount(hmenu
);
1471 bool inserted
= false;
1472 for ( int i
= 0; i
< N
; i
++ )
1475 if ( !::GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1477 wxLogLastError(wxT("GetMenuString"));
1482 const wxString label
= wxStripMenuCodes(buf
);
1483 if ( label
== wxGetStockLabel(wxID_HELP
, wxSTOCK_NOFLAGS
) )
1486 ::InsertMenu(hmenu
, i
, MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1488 wxString(wxGetTranslation(WINDOW_MENU_LABEL
)).t_str());
1495 ::AppendMenu(hmenu
, MF_POPUP
,
1497 wxString(wxGetTranslation(WINDOW_MENU_LABEL
)).t_str());
1501 MDISetMenu(win
, hmenu
, menuWin
);
1504 void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU hMenu
)
1506 HMENU hmenu
= (HMENU
)hMenu
;
1512 int N
= ::GetMenuItemCount(hmenu
);
1513 for ( int i
= 0; i
< N
; i
++ )
1515 if ( !::GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1517 // Ignore successful read of menu string with length 0 which
1518 // occurs, for example, for a maximized MDI child system menu
1519 if ( ::GetLastError() != 0 )
1521 wxLogLastError(wxT("GetMenuString"));
1527 if ( wxStrcmp(buf
, wxGetTranslation(WINDOW_MENU_LABEL
)) == 0 )
1529 if ( !::RemoveMenu(hmenu
, i
, MF_BYPOSITION
) )
1531 wxLogLastError(wxT("RemoveMenu"));
1541 // we don't change the windows menu, but we update the main one
1542 MDISetMenu(win
, hmenu
, NULL
);
1546 void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
1547 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
)
1550 *hwndAct
= (WXHWND
)lParam
;
1551 *hwndDeact
= (WXHWND
)wParam
;
1554 } // anonymous namespace
1556 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)