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
7 // Copyright: (c) 1998 Julian Smart
8 // (c) 2008-2009 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_MDI && !defined(__WXUNIVERSAL__)
36 #include "wx/dialog.h"
37 #include "wx/statusbr.h"
38 #include "wx/settings.h"
41 #include "wx/toolbar.h"
44 #include "wx/stockitem.h"
45 #include "wx/msw/private.h"
49 // ---------------------------------------------------------------------------
51 // ---------------------------------------------------------------------------
53 extern wxMenu
*wxCurrentPopupMenu
;
55 extern void wxRemoveHandleAssociation(wxWindow
*win
);
60 // ---------------------------------------------------------------------------
62 // ---------------------------------------------------------------------------
64 // First ID for the MDI child menu item in the "Window" menu.
65 const int wxFIRST_MDI_CHILD
= 4100;
67 // There can be no more than 9 children in the "Window" menu as beginning with
68 // the tenth one they're not shown and "More windows..." menu item is used
70 const int wxLAST_MDI_CHILD
= wxFIRST_MDI_CHILD
+ 8;
72 // The ID of the "More windows..." menu item is the next one after the last
74 const int wxID_MDI_MORE_WINDOWS
= wxLAST_MDI_CHILD
+ 1;
76 // The MDI "Window" menu label
77 const char *WINDOW_MENU_LABEL
= gettext_noop("&Window");
79 // ---------------------------------------------------------------------------
81 // ---------------------------------------------------------------------------
83 // set the MDI menus (by sending the WM_MDISETMENU message) and update the menu
84 // of the parent of win (which is supposed to be the MDI client window)
85 void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
);
87 // insert the window menu (subMenu) into menu just before "Help" submenu or at
88 // the very end if not found
89 void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU hMenu
, HMENU subMenu
);
91 // Remove the window menu
92 void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU hMenu
);
94 // unpack the parameters of WM_MDIACTIVATE message
95 void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
96 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
);
98 // return the HMENU of the MDI menu
100 // this function works correctly even when we don't have a window menu and just
102 inline HMENU
GetMDIWindowMenu(wxMDIParentFrame
*frame
)
104 wxMenu
*menu
= frame
->GetWindowMenu();
105 return menu
? GetHmenuOf(menu
) : 0;
108 } // anonymous namespace
110 // ===========================================================================
112 // ===========================================================================
114 // ---------------------------------------------------------------------------
116 // ---------------------------------------------------------------------------
118 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxFrame
)
119 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxFrame
)
120 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxWindow
)
122 BEGIN_EVENT_TABLE(wxMDIParentFrame
, wxFrame
)
123 EVT_SIZE(wxMDIParentFrame::OnSize
)
124 EVT_ICONIZE(wxMDIParentFrame::OnIconized
)
125 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged
)
128 EVT_MENU_RANGE(wxFIRST_MDI_CHILD
, wxLAST_MDI_CHILD
,
129 wxMDIParentFrame::OnMDIChild
)
130 EVT_MENU_RANGE(wxID_MDI_WINDOW_FIRST
, wxID_MDI_WINDOW_LAST
,
131 wxMDIParentFrame::OnMDICommand
)
132 #endif // wxUSE_MENUS
135 BEGIN_EVENT_TABLE(wxMDIChildFrame
, wxFrame
)
136 EVT_IDLE(wxMDIChildFrame::OnIdle
)
139 BEGIN_EVENT_TABLE(wxMDIClientWindow
, wxWindow
)
140 EVT_SCROLL(wxMDIClientWindow::OnScroll
)
143 // ===========================================================================
144 // wxMDIParentFrame: the frame which contains the client window which manages
146 // ===========================================================================
148 void wxMDIParentFrame::Init()
150 #if wxUSE_MENUS && wxUSE_ACCEL
151 // the default menu doesn't have any accelerators (even if we have it)
152 m_accelWindowMenu
= NULL
;
153 #endif // wxUSE_MENUS && wxUSE_ACCEL
156 bool wxMDIParentFrame::Create(wxWindow
*parent
,
158 const wxString
& title
,
162 const wxString
& name
)
164 // this style can be used to prevent a window from having the standard MDI
166 if ( !(style
& wxFRAME_NO_WINDOW_MENU
) )
168 // normal case: we have the window menu, so construct it
169 m_windowMenu
= new wxMenu
;
171 m_windowMenu
->Append(wxID_MDI_WINDOW_CASCADE
, _("&Cascade"));
172 m_windowMenu
->Append(wxID_MDI_WINDOW_TILE_HORZ
, _("Tile &Horizontally"));
173 m_windowMenu
->Append(wxID_MDI_WINDOW_TILE_VERT
, _("Tile &Vertically"));
174 m_windowMenu
->AppendSeparator();
175 m_windowMenu
->Append(wxID_MDI_WINDOW_ARRANGE_ICONS
, _("&Arrange Icons"));
176 m_windowMenu
->Append(wxID_MDI_WINDOW_NEXT
, _("&Next"));
177 m_windowMenu
->Append(wxID_MDI_WINDOW_PREV
, _("&Previous"));
181 wxTopLevelWindows
.Append(this);
184 m_windowStyle
= style
;
187 parent
->AddChild(this);
189 if ( id
!= wxID_ANY
)
192 m_windowId
= NewControlId();
195 WXDWORD msflags
= MSWGetCreateWindowFlags(&exflags
);
196 msflags
&= ~WS_VSCROLL
;
197 msflags
&= ~WS_HSCROLL
;
199 if ( !wxWindow::MSWCreate(wxApp::GetRegisteredClassName(wxT("wxMDIFrame")),
208 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
210 // unlike (almost?) all other windows, frames are created hidden
216 wxMDIParentFrame::~wxMDIParentFrame()
218 // see comment in ~wxMDIChildFrame
220 m_frameToolBar
= NULL
;
223 m_frameStatusBar
= NULL
;
224 #endif // wxUSE_STATUSBAR
226 #if wxUSE_MENUS && wxUSE_ACCEL
227 delete m_accelWindowMenu
;
228 #endif // wxUSE_MENUS && wxUSE_ACCEL
232 // the MDI frame menubar is not automatically deleted by Windows unlike for
235 ::DestroyMenu((HMENU
)m_hMenu
);
237 if ( m_clientWindow
)
239 if ( m_clientWindow
->MSWGetOldWndProc() )
240 m_clientWindow
->UnsubclassWin();
242 m_clientWindow
->SetHWND(0);
243 delete m_clientWindow
;
247 // ----------------------------------------------------------------------------
248 // wxMDIParentFrame child management
249 // ----------------------------------------------------------------------------
251 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
253 HWND hWnd
= (HWND
)::SendMessage(GetWinHwnd(GetClientWindow()),
254 WM_MDIGETACTIVE
, 0, 0L);
258 return static_cast<wxMDIChildFrame
*>(wxFindWinFromHandle(hWnd
));
261 int wxMDIParentFrame::GetChildFramesCount() const
264 for ( wxWindowList::const_iterator i
= GetChildren().begin();
265 i
!= GetChildren().end();
268 if ( wxDynamicCast(*i
, wxMDIChildFrame
) )
277 void wxMDIParentFrame::AddMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
279 switch ( GetChildFramesCount() )
282 // first MDI child was just added, we need to insert the window
283 // menu now if we have it
286 // and disable the items which can't be used until we have more
288 UpdateWindowMenu(false);
292 // second MDI child was added, enable the menu items which were
293 // disabled because they didn't make sense for a single window
294 UpdateWindowMenu(true);
299 void wxMDIParentFrame::RemoveMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
301 switch ( GetChildFramesCount() )
304 // last MDI child is being removed, remove the now unnecessary
308 // there is no need to call UpdateWindowMenu(true) here so this is
309 // not quite symmetric to AddMDIChild() above
313 // only one MDI child is going to remain, disable the menu commands
314 // which don't make sense for a single child window
315 UpdateWindowMenu(false);
320 // ----------------------------------------------------------------------------
321 // wxMDIParentFrame window menu handling
322 // ----------------------------------------------------------------------------
324 void wxMDIParentFrame::AddWindowMenu()
328 // For correct handling of the events from this menu we also must
329 // attach it to the menu bar.
330 m_windowMenu
->Attach(GetMenuBar());
332 MDIInsertWindowMenu(GetClientWindow(), m_hMenu
, GetMDIWindowMenu(this));
336 void wxMDIParentFrame::RemoveWindowMenu()
340 MDIRemoveWindowMenu(GetClientWindow(), m_hMenu
);
342 m_windowMenu
->Detach();
346 void wxMDIParentFrame::UpdateWindowMenu(bool enable
)
350 m_windowMenu
->Enable(wxID_MDI_WINDOW_NEXT
, enable
);
351 m_windowMenu
->Enable(wxID_MDI_WINDOW_PREV
, enable
);
355 #if wxUSE_MENUS_NATIVE
357 void wxMDIParentFrame::InternalSetMenuBar()
359 if ( GetActiveChild() )
363 else // we don't have any MDI children yet
365 // wait until we do to add the window menu but do set the main menu for
366 // now (this is done by AddWindowMenu() as a side effect)
367 MDISetMenu(GetClientWindow(), (HMENU
)m_hMenu
, NULL
);
371 #endif // wxUSE_MENUS_NATIVE
373 void wxMDIParentFrame::SetWindowMenu(wxMenu
* menu
)
375 if ( menu
!= m_windowMenu
)
377 // notice that Remove/AddWindowMenu() are safe to call even when
378 // m_windowMenu is NULL
389 wxDELETE(m_accelWindowMenu
);
391 if ( menu
&& menu
->HasAccels() )
392 m_accelWindowMenu
= menu
->CreateAccelTable();
393 #endif // wxUSE_ACCEL
396 // ----------------------------------------------------------------------------
397 // wxMDIParentFrame other menu-related stuff
398 // ----------------------------------------------------------------------------
400 void wxMDIParentFrame::DoMenuUpdates(wxMenu
* menu
)
402 wxMDIChildFrame
*child
= GetActiveChild();
405 wxEvtHandler
* source
= child
->GetEventHandler();
406 wxMenuBar
* bar
= child
->GetMenuBar();
410 menu
->UpdateUI(source
);
416 int nCount
= bar
->GetMenuCount();
417 for (int n
= 0; n
< nCount
; n
++)
418 bar
->GetMenu(n
)->UpdateUI(source
);
424 wxFrameBase::DoMenuUpdates(menu
);
428 wxMenuItem
*wxMDIParentFrame::FindItemInMenuBar(int menuId
) const
430 // We must look in the child menu first: if it has an item with the same ID
431 // as in our own menu bar, the child item should be used to determine
432 // whether it's currently enabled.
433 wxMenuItem
*item
= GetActiveChild()
434 ? GetActiveChild()->FindItemInMenuBar(menuId
)
437 item
= wxFrame::FindItemInMenuBar(menuId
);
439 if ( !item
&& m_windowMenu
)
440 item
= m_windowMenu
->FindItem(menuId
);
445 WXHMENU
wxMDIParentFrame::MSWGetActiveMenu() const
447 wxMDIChildFrame
* const child
= GetActiveChild();
450 const WXHMENU hmenu
= child
->MSWGetActiveMenu();
455 return wxFrame::MSWGetActiveMenu();
458 #endif // wxUSE_MENUS
460 // ----------------------------------------------------------------------------
461 // wxMDIParentFrame event handling
462 // ----------------------------------------------------------------------------
464 void wxMDIParentFrame::UpdateClientSize()
466 if ( GetClientWindow() )
469 GetClientSize(&width
, &height
);
471 GetClientWindow()->SetSize(0, 0, width
, height
);
475 void wxMDIParentFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
479 // do not call event.Skip() here, it somehow messes up MDI client window
482 void wxMDIParentFrame::OnIconized(wxIconizeEvent
& event
)
486 if ( !event
.IsIconized() )
490 // Responds to colour changes, and passes event on to children.
491 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
493 if ( m_clientWindow
)
495 m_clientWindow
->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
496 m_clientWindow
->Refresh();
502 WXHICON
wxMDIParentFrame::GetDefaultIcon() const
504 // we don't have any standard icons (any more)
508 // ---------------------------------------------------------------------------
510 // ---------------------------------------------------------------------------
512 void wxMDIParentFrame::Cascade()
514 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE
, 0, 0);
517 void wxMDIParentFrame::Tile(wxOrientation orient
)
519 wxASSERT_MSG( orient
== wxHORIZONTAL
|| orient
== wxVERTICAL
,
520 wxT("invalid orientation value") );
522 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE
,
523 orient
== wxHORIZONTAL
? MDITILE_HORIZONTAL
524 : MDITILE_VERTICAL
, 0);
527 void wxMDIParentFrame::ArrangeIcons()
529 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE
, 0, 0);
532 void wxMDIParentFrame::ActivateNext()
534 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 0);
537 void wxMDIParentFrame::ActivatePrevious()
539 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 1);
542 // ---------------------------------------------------------------------------
543 // the MDI parent frame window proc
544 // ---------------------------------------------------------------------------
546 WXLRESULT
wxMDIParentFrame::MSWWindowProc(WXUINT message
,
551 bool processed
= false;
557 WXWORD state
, minimized
;
559 UnpackActivate(wParam
, lParam
, &state
, &minimized
, &hwnd
);
561 processed
= HandleActivate(state
, minimized
!= 0, hwnd
);
566 // system messages such as SC_CLOSE are sent as WM_COMMANDs to the
567 // parent MDI frame and we must let the DefFrameProc() have them
568 // for these commands to work (without it, closing the maximized
569 // MDI children doesn't work, for example)
573 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
575 if ( id
== wxID_MDI_MORE_WINDOWS
||
576 (cmd
== 0 /* menu */ &&
577 id
>= SC_SIZE
/* first system menu command */) )
579 MSWDefWindowProc(message
, wParam
, lParam
);
586 m_clientWindow
= OnCreateClient();
587 // Uses own style for client style
588 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
590 wxLogMessage(_("Failed to create MDI parent frame."));
600 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
605 bool wxMDIParentFrame::HandleActivate(int state
, bool minimized
, WXHWND activate
)
607 bool processed
= false;
609 if ( wxWindow::HandleActivate(state
, minimized
, activate
) )
615 // If this window is an MDI parent, we must also send an OnActivate message
616 // to the current child.
617 if ( GetActiveChild() &&
618 ((state
== WA_ACTIVE
) || (state
== WA_CLICKACTIVE
)) )
620 wxActivateEvent
event(wxEVT_ACTIVATE
, true, GetActiveChild()->GetId());
621 event
.SetEventObject( GetActiveChild() );
622 if ( GetActiveChild()->HandleWindowEvent(event
) )
631 void wxMDIParentFrame::OnMDIChild(wxCommandEvent
& event
)
633 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
636 wxWindow
*child
= node
->GetData();
637 if ( child
->GetHWND() )
639 int childId
= wxGetWindowId(child
->GetHWND());
640 if ( childId
== event
.GetId() )
642 wxStaticCast(child
, wxMDIChildFrame
)->Activate();
647 node
= node
->GetNext();
650 wxFAIL_MSG( "unknown MDI child selected?" );
653 void wxMDIParentFrame::OnMDICommand(wxCommandEvent
& event
)
658 switch ( event
.GetId() )
660 case wxID_MDI_WINDOW_CASCADE
:
662 wParam
= MDITILE_SKIPDISABLED
;
665 case wxID_MDI_WINDOW_TILE_HORZ
:
666 wParam
|= MDITILE_HORIZONTAL
;
669 case wxID_MDI_WINDOW_TILE_VERT
:
671 wParam
= MDITILE_VERTICAL
;
673 wParam
|= MDITILE_SKIPDISABLED
;
676 case wxID_MDI_WINDOW_ARRANGE_ICONS
:
677 msg
= WM_MDIICONARRANGE
;
680 case wxID_MDI_WINDOW_NEXT
:
682 lParam
= 0; // next child
685 case wxID_MDI_WINDOW_PREV
:
687 lParam
= 1; // previous child
691 wxFAIL_MSG( "unknown MDI command" );
695 ::SendMessage(GetWinHwnd(GetClientWindow()), msg
, wParam
, lParam
);
698 #endif // wxUSE_MENUS
700 WXLRESULT
wxMDIParentFrame::MSWDefWindowProc(WXUINT message
,
705 if ( GetClientWindow() )
706 clientWnd
= GetClientWindow()->GetHWND();
710 return DefFrameProc(GetHwnd(), (HWND
)clientWnd
, message
, wParam
, lParam
);
713 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG
* msg
)
715 MSG
*pMsg
= (MSG
*)msg
;
717 // first let the current child get it
718 wxMDIChildFrame
* const child
= GetActiveChild();
719 if ( child
&& child
->MSWTranslateMessage(msg
) )
724 // then try out accelerator table (will also check the accelerators for the
725 // normal menu items)
726 if ( wxFrame::MSWTranslateMessage(msg
) )
731 #if wxUSE_MENUS && wxUSE_ACCEL
732 // but it doesn't check for the (custom) accelerators of the window menu
733 // items as it's not part of the menu bar as it's handled by Windows itself
734 // so we need to do this explicitly
735 if ( m_accelWindowMenu
&& m_accelWindowMenu
->Translate(this, msg
) )
737 #endif // wxUSE_MENUS && wxUSE_ACCEL
739 // finally, check for MDI specific built-in accelerators
740 if ( pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
742 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg
))
749 // ===========================================================================
751 // ===========================================================================
753 void wxMDIChildFrame::Init()
755 m_needsResize
= true;
756 m_needsInitialShow
= true;
759 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
761 const wxString
& title
,
765 const wxString
& name
)
767 m_mdiParent
= parent
;
771 if ( id
!= wxID_ANY
)
774 m_windowId
= NewControlId();
778 parent
->AddChild(this);
789 wxApp::GetRegisteredClassName(wxT("wxMDIChildFrame"), COLOR_WINDOW
);
790 if ( !(style
& wxFULL_REPAINT_ON_RESIZE
) )
791 className
+= wxApp::GetNoRedrawClassSuffix();
793 mcs
.szClass
= className
.t_str();
794 mcs
.szTitle
= title
.t_str();
795 mcs
.hOwner
= wxGetInstance();
796 if (x
!= wxDefaultCoord
)
799 mcs
.x
= CW_USEDEFAULT
;
801 if (y
!= wxDefaultCoord
)
804 mcs
.y
= CW_USEDEFAULT
;
806 if (width
!= wxDefaultCoord
)
809 mcs
.cx
= CW_USEDEFAULT
;
811 if (height
!= wxDefaultCoord
)
814 mcs
.cy
= CW_USEDEFAULT
;
816 DWORD msflags
= WS_OVERLAPPED
| WS_CLIPCHILDREN
;
817 if (style
& wxMINIMIZE_BOX
)
818 msflags
|= WS_MINIMIZEBOX
;
819 if (style
& wxMAXIMIZE_BOX
)
820 msflags
|= WS_MAXIMIZEBOX
;
821 if (style
& wxRESIZE_BORDER
)
822 msflags
|= WS_THICKFRAME
;
823 if (style
& wxSYSTEM_MENU
)
824 msflags
|= WS_SYSMENU
;
825 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
826 msflags
|= WS_MINIMIZE
;
827 if (style
& wxMAXIMIZE
)
828 msflags
|= WS_MAXIMIZE
;
829 if (style
& wxCAPTION
)
830 msflags
|= WS_CAPTION
;
836 wxWindowCreationHook
hook(this);
838 m_hWnd
= (WXHWND
)::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
839 WM_MDICREATE
, 0, (LPARAM
)&mcs
);
843 wxLogLastError(wxT("WM_MDICREATE"));
849 parent
->AddMDIChild(this);
854 wxMDIChildFrame::~wxMDIChildFrame()
856 // if we hadn't been created, there is nothing to destroy
860 GetMDIParent()->RemoveMDIChild(this);
862 // will be destroyed by DestroyChildren() but reset them before calling it
863 // to avoid using dangling pointers if a callback comes in the meanwhile
865 m_frameToolBar
= NULL
;
868 m_frameStatusBar
= NULL
;
869 #endif // wxUSE_STATUSBAR
873 MDIRemoveWindowMenu(NULL
, m_hMenu
);
878 bool wxMDIChildFrame::Show(bool show
)
880 m_needsInitialShow
= false;
882 if (!wxFrame::Show(show
))
885 // KH: Without this call, new MDI children do not become active.
886 // This was added here after the same BringWindowToTop call was
887 // removed from wxTopLevelWindow::Show (November 2005)
889 ::BringWindowToTop(GetHwnd());
891 // we need to refresh the MDI frame window menu to include (or exclude if
892 // we've been hidden) this frame
893 wxMDIParentFrame
* const parent
= GetMDIParent();
894 MDISetMenu(parent
->GetClientWindow(), NULL
, NULL
);
900 wxMDIChildFrame::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
902 // we need to disable client area origin adjustments used for the child
903 // windows for the frame itself
904 wxMDIChildFrameBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
907 // Set the client size (i.e. leave the calculation of borders etc.
909 void wxMDIChildFrame::DoSetClientSize(int width
, int height
)
911 HWND hWnd
= GetHwnd();
914 ::GetClientRect(hWnd
, &rect
);
917 GetWindowRect(hWnd
, &rect2
);
919 // Find the difference between the entire window (title bar and all)
920 // and the client area; add this to the new client size to move the
922 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
923 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
926 if (GetStatusBar() && GetStatusBar()->IsShown())
929 GetStatusBar()->GetSize(&sx
, &sy
);
932 #endif // wxUSE_STATUSBAR
935 point
.x
= rect2
.left
;
938 // If there's an MDI parent, must subtract the parent's top left corner
939 // since MoveWindow moves relative to the parent
940 wxMDIParentFrame
* const mdiParent
= GetMDIParent();
941 ::ScreenToClient(GetHwndOf(mdiParent
->GetClientWindow()), &point
);
943 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)true);
945 wxSize
size(width
, height
);
946 wxSizeEvent
event(size
, m_windowId
);
947 event
.SetEventObject( this );
948 HandleWindowEvent(event
);
951 // Unlike other wxTopLevelWindowBase, the mdi child's "GetPosition" is not the
952 // same as its GetScreenPosition
953 void wxMDIChildFrame::DoGetScreenPosition(int *x
, int *y
) const
955 HWND hWnd
= GetHwnd();
958 ::GetWindowRect(hWnd
, &rect
);
966 void wxMDIChildFrame::DoGetPosition(int *x
, int *y
) const
969 GetWindowRect(GetHwnd(), &rect
);
974 // Since we now have the absolute screen coords,
975 // if there's a parent we must subtract its top left corner
976 wxMDIParentFrame
* const mdiParent
= GetMDIParent();
977 ::ScreenToClient(GetHwndOf(mdiParent
->GetClientWindow()), &point
);
985 void wxMDIChildFrame::InternalSetMenuBar()
987 wxMDIParentFrame
* const parent
= GetMDIParent();
989 MDIInsertWindowMenu(parent
->GetClientWindow(),
990 m_hMenu
, GetMDIWindowMenu(parent
));
993 void wxMDIChildFrame::DetachMenuBar()
995 MDIRemoveWindowMenu(NULL
, m_hMenu
);
996 wxFrame::DetachMenuBar();
999 WXHICON
wxMDIChildFrame::GetDefaultIcon() const
1001 // we don't have any standard icons (any more)
1005 // ---------------------------------------------------------------------------
1007 // ---------------------------------------------------------------------------
1009 void wxMDIChildFrame::Maximize(bool maximize
)
1011 wxMDIParentFrame
* const parent
= GetMDIParent();
1012 if ( parent
&& parent
->GetClientWindow() )
1014 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
1015 maximize
? WM_MDIMAXIMIZE
: WM_MDIRESTORE
,
1016 (WPARAM
)GetHwnd(), 0);
1020 void wxMDIChildFrame::Restore()
1022 wxMDIParentFrame
* const parent
= GetMDIParent();
1023 if ( parent
&& parent
->GetClientWindow() )
1025 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIRESTORE
,
1026 (WPARAM
) GetHwnd(), 0);
1030 void wxMDIChildFrame::Activate()
1032 wxMDIParentFrame
* const parent
= GetMDIParent();
1033 if ( parent
&& parent
->GetClientWindow() )
1035 // Activating an iconized MDI frame doesn't do anything, so restore it
1036 // first to really present it to the user.
1040 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIACTIVATE
,
1041 (WPARAM
) GetHwnd(), 0);
1045 // ---------------------------------------------------------------------------
1046 // MDI window proc and message handlers
1047 // ---------------------------------------------------------------------------
1049 WXLRESULT
wxMDIChildFrame::MSWWindowProc(WXUINT message
,
1054 bool processed
= false;
1058 case WM_GETMINMAXINFO
:
1059 processed
= HandleGetMinMaxInfo((MINMAXINFO
*)lParam
);
1062 case WM_MDIACTIVATE
:
1065 WXHWND hwndAct
, hwndDeact
;
1066 UnpackMDIActivate(wParam
, lParam
, &act
, &hwndAct
, &hwndDeact
);
1068 processed
= HandleMDIActivate(act
, hwndAct
, hwndDeact
);
1073 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
1074 // scrollbars if necessary
1079 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
1081 MSWDefWindowProc(message
, wParam
, lParam
);
1084 case WM_WINDOWPOSCHANGING
:
1085 processed
= HandleWindowPosChanging((LPWINDOWPOS
)lParam
);
1090 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
1095 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate
),
1099 wxMDIParentFrame
* const parent
= GetMDIParent();
1101 WXHMENU hMenuToSet
= 0;
1105 if ( m_hWnd
== hwndAct
)
1108 parent
->SetActiveChild(this);
1110 WXHMENU hMenuChild
= m_hMenu
;
1112 hMenuToSet
= hMenuChild
;
1114 else if ( m_hWnd
== hwndDeact
)
1116 wxASSERT_MSG( parent
->GetActiveChild() == this,
1117 wxT("can't deactivate MDI child which wasn't active!") );
1120 parent
->SetActiveChild(NULL
);
1122 WXHMENU hMenuParent
= parent
->m_hMenu
;
1124 // activate the parent menu only when there is no other child
1125 // that has been activated
1126 if ( hMenuParent
&& !hwndAct
)
1127 hMenuToSet
= hMenuParent
;
1131 // we have nothing to do with it
1137 MDISetMenu(parent
->GetClientWindow(),
1138 (HMENU
)hMenuToSet
, GetMDIWindowMenu(parent
));
1141 wxActivateEvent
event(wxEVT_ACTIVATE
, activated
, m_windowId
);
1142 event
.SetEventObject( this );
1144 ResetWindowStyle(NULL
);
1146 return HandleWindowEvent(event
);
1149 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos
)
1151 WINDOWPOS
*lpPos
= (WINDOWPOS
*)pos
;
1153 if (!(lpPos
->flags
& SWP_NOSIZE
))
1156 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1157 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1158 if (ResetWindowStyle((void *) & rectClient
) && (dwStyle
& WS_MAXIMIZE
))
1160 ::AdjustWindowRectEx(&rectClient
, dwStyle
, false, dwExStyle
);
1161 lpPos
->x
= rectClient
.left
;
1162 lpPos
->y
= rectClient
.top
;
1163 lpPos
->cx
= rectClient
.right
- rectClient
.left
;
1164 lpPos
->cy
= rectClient
.bottom
- rectClient
.top
;
1171 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo
)
1173 MINMAXINFO
*info
= (MINMAXINFO
*)mmInfo
;
1175 // let the default window proc calculate the size of MDI children
1176 // frames because it is based on the size of the MDI client window,
1177 // not on the values specified in wxWindow m_max variables
1178 bool processed
= MSWDefWindowProc(WM_GETMINMAXINFO
, 0, (LPARAM
)mmInfo
) != 0;
1180 int minWidth
= GetMinWidth(),
1181 minHeight
= GetMinHeight();
1183 // but allow GetSizeHints() to set the min size
1184 if ( minWidth
!= wxDefaultCoord
)
1186 info
->ptMinTrackSize
.x
= minWidth
;
1191 if ( minHeight
!= wxDefaultCoord
)
1193 info
->ptMinTrackSize
.y
= minHeight
;
1201 // ---------------------------------------------------------------------------
1202 // MDI specific message translation/preprocessing
1203 // ---------------------------------------------------------------------------
1205 WXLRESULT
wxMDIChildFrame::MSWDefWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1207 return DefMDIChildProc(GetHwnd(),
1208 (UINT
)message
, (WPARAM
)wParam
, (LPARAM
)lParam
);
1211 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG
* msg
)
1213 // we must pass the parent frame to ::TranslateAccelerator(), otherwise it
1214 // doesn't do its job correctly for MDI child menus
1215 return MSWDoTranslateMessage(GetMDIParent(), msg
);
1218 // ---------------------------------------------------------------------------
1220 // ---------------------------------------------------------------------------
1222 void wxMDIChildFrame::MSWDestroyWindow()
1224 wxMDIParentFrame
* const parent
= GetMDIParent();
1226 // Must make sure this handle is invalidated (set to NULL) since all sorts
1227 // of things could happen after the child client is destroyed, but before
1228 // the wxFrame is destroyed.
1230 HWND oldHandle
= (HWND
)GetHWND();
1231 SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIDESTROY
,
1232 (WPARAM
)oldHandle
, 0);
1234 if (parent
->GetActiveChild() == NULL
)
1235 ResetWindowStyle(NULL
);
1239 ::DestroyMenu((HMENU
) m_hMenu
);
1242 wxRemoveHandleAssociation(this);
1246 // Change the client window's extended style so we don't get a client edge
1247 // style when a child is maximised (a double border looks silly.)
1248 bool wxMDIChildFrame::ResetWindowStyle(void *vrect
)
1250 RECT
*rect
= (RECT
*)vrect
;
1251 wxMDIParentFrame
* const pFrameWnd
= GetMDIParent();
1252 wxMDIChildFrame
* pChild
= pFrameWnd
->GetActiveChild();
1254 if (!pChild
|| (pChild
== this))
1256 HWND hwndClient
= GetWinHwnd(pFrameWnd
->GetClientWindow());
1257 DWORD dwStyle
= ::GetWindowLong(hwndClient
, GWL_EXSTYLE
);
1259 // we want to test whether there is a maximized child, so just set
1260 // dwThisStyle to 0 if there is no child at all
1261 DWORD dwThisStyle
= pChild
1262 ? ::GetWindowLong(GetWinHwnd(pChild
), GWL_STYLE
) : 0;
1263 DWORD dwNewStyle
= dwStyle
;
1264 if ( dwThisStyle
& WS_MAXIMIZE
)
1265 dwNewStyle
&= ~(WS_EX_CLIENTEDGE
);
1267 dwNewStyle
|= WS_EX_CLIENTEDGE
;
1269 if (dwStyle
!= dwNewStyle
)
1271 // force update of everything
1272 ::RedrawWindow(hwndClient
, NULL
, NULL
,
1273 RDW_INVALIDATE
| RDW_ALLCHILDREN
);
1274 ::SetWindowLong(hwndClient
, GWL_EXSTYLE
, dwNewStyle
);
1275 ::SetWindowPos(hwndClient
, NULL
, 0, 0, 0, 0,
1276 SWP_FRAMECHANGED
| SWP_NOACTIVATE
|
1277 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1280 ::GetClientRect(hwndClient
, rect
);
1289 // ===========================================================================
1290 // wxMDIClientWindow: the window of predefined (by Windows) class which
1291 // contains the child frames
1292 // ===========================================================================
1294 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
1296 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1298 CLIENTCREATESTRUCT ccs
;
1299 m_windowStyle
= style
;
1302 ccs
.hWindowMenu
= GetMDIWindowMenu(parent
);
1303 ccs
.idFirstChild
= wxFIRST_MDI_CHILD
;
1305 DWORD msStyle
= MDIS_ALLCHILDSTYLES
| WS_VISIBLE
| WS_CHILD
|
1306 WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
;
1308 if ( style
& wxHSCROLL
)
1309 msStyle
|= WS_HSCROLL
;
1310 if ( style
& wxVSCROLL
)
1311 msStyle
|= WS_VSCROLL
;
1313 DWORD exStyle
= WS_EX_CLIENTEDGE
;
1315 wxWindowCreationHook
hook(this);
1316 m_hWnd
= (WXHWND
)::CreateWindowEx
1326 (LPSTR
)(LPCLIENTCREATESTRUCT
)&ccs
);
1329 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1334 SubclassWin(m_hWnd
);
1339 // Explicitly call default scroll behaviour
1340 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)
1342 // Note: for client windows, the scroll position is not set in
1343 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1344 // scroll position we're at.
1345 // This makes it hard to paint patterns or bitmaps in the background,
1346 // and have the client area scrollable as well.
1348 if ( event
.GetOrientation() == wxHORIZONTAL
)
1349 m_scrollX
= event
.GetPosition(); // Always returns zero!
1351 m_scrollY
= event
.GetPosition(); // Always returns zero!
1356 void wxMDIClientWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1358 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1359 // client area, you can end up with a non-refreshed portion in the client window
1360 // (see OGL studio sample). So check if the position is changed and if so,
1361 // redraw the MDI child frames.
1363 const wxPoint oldPos
= GetPosition();
1365 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
| wxSIZE_FORCE
);
1367 const wxPoint newPos
= GetPosition();
1369 if ((newPos
.x
!= oldPos
.x
) || (newPos
.y
!= oldPos
.y
))
1373 wxWindowList::compatibility_iterator node
= GetParent()->GetChildren().GetFirst();
1376 wxWindow
*child
= node
->GetData();
1377 if (wxDynamicCast(child
, wxMDIChildFrame
))
1379 ::RedrawWindow(GetHwndOf(child
),
1386 node
= node
->GetNext();
1392 void wxMDIChildFrame::OnIdle(wxIdleEvent
& event
)
1394 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1395 // in flicker e.g. when the frame contained controls with non-trivial
1396 // layout. Since 2.5.3, the frame is created hidden as all other top level
1397 // windows. In order to maintain backward compatibility, the frame is shown
1398 // in OnIdle, unless Show(false) was called by the programmer before.
1399 if ( m_needsInitialShow
)
1404 // MDI child frames get their WM_SIZE when they're constructed but at this
1405 // moment they don't have any children yet so all child windows will be
1406 // positioned incorrectly when they are added later - to fix this, we
1407 // generate an artificial size event here
1408 if ( m_needsResize
)
1410 m_needsResize
= false; // avoid any possibility of recursion
1418 // ---------------------------------------------------------------------------
1419 // private helper functions
1420 // ---------------------------------------------------------------------------
1425 void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
)
1427 if ( hmenuFrame
|| hmenuWindow
)
1429 if ( !::SendMessage(GetWinHwnd(win
),
1432 (LPARAM
)hmenuWindow
) )
1434 DWORD err
= ::GetLastError();
1437 wxLogApiError(wxT("SendMessage(WM_MDISETMENU)"), err
);
1442 // update menu bar of the parent window
1443 wxWindow
*parent
= win
->GetParent();
1444 wxCHECK_RET( parent
, wxT("MDI client without parent frame? weird...") );
1446 ::SendMessage(GetWinHwnd(win
), WM_MDIREFRESHMENU
, 0, 0L);
1448 ::DrawMenuBar(GetWinHwnd(parent
));
1451 void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU hMenu
, HMENU menuWin
)
1453 HMENU hmenu
= (HMENU
)hMenu
;
1457 // Try to insert Window menu in front of Help, otherwise append it.
1458 int N
= GetMenuItemCount(hmenu
);
1459 bool inserted
= false;
1460 for ( int i
= 0; i
< N
; i
++ )
1463 if ( !::GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1465 wxLogLastError(wxT("GetMenuString"));
1470 const wxString label
= wxStripMenuCodes(buf
);
1471 if ( label
== wxGetStockLabel(wxID_HELP
, wxSTOCK_NOFLAGS
) )
1474 ::InsertMenu(hmenu
, i
, MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1476 wxString(wxGetTranslation(WINDOW_MENU_LABEL
)).t_str());
1483 ::AppendMenu(hmenu
, MF_POPUP
,
1485 wxString(wxGetTranslation(WINDOW_MENU_LABEL
)).t_str());
1489 MDISetMenu(win
, hmenu
, menuWin
);
1492 void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU hMenu
)
1494 HMENU hmenu
= (HMENU
)hMenu
;
1500 int N
= ::GetMenuItemCount(hmenu
);
1501 for ( int i
= 0; i
< N
; i
++ )
1503 if ( !::GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1505 // Ignore successful read of menu string with length 0 which
1506 // occurs, for example, for a maximized MDI child system menu
1507 if ( ::GetLastError() != 0 )
1509 wxLogLastError(wxT("GetMenuString"));
1515 if ( wxStrcmp(buf
, wxGetTranslation(WINDOW_MENU_LABEL
)) == 0 )
1517 if ( !::RemoveMenu(hmenu
, i
, MF_BYPOSITION
) )
1519 wxLogLastError(wxT("RemoveMenu"));
1529 // we don't change the windows menu, but we update the main one
1530 MDISetMenu(win
, hmenu
, NULL
);
1534 void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
1535 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
)
1538 *hwndAct
= (WXHWND
)lParam
;
1539 *hwndDeact
= (WXHWND
)wParam
;
1542 } // anonymous namespace
1544 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)