1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/mdi.cpp
3 // Purpose: MDI classes for wxMSW
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
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 const wxChar
*wxMDIFrameClassName
; // from app.cpp
56 extern const wxChar
*wxMDIChildFrameClassName
;
57 extern const wxChar
*wxMDIChildFrameClassNameNoRedraw
;
58 extern void wxRemoveHandleAssociation(wxWindow
*win
);
60 // ---------------------------------------------------------------------------
62 // ---------------------------------------------------------------------------
64 static const int IDM_WINDOWTILEHOR
= 4001;
65 static const int IDM_WINDOWCASCADE
= 4002;
66 static const int IDM_WINDOWICONS
= 4003;
67 static const int IDM_WINDOWNEXT
= 4004;
68 static const int IDM_WINDOWTILEVERT
= 4005;
69 static const int IDM_WINDOWPREV
= 4006;
71 // This range gives a maximum of 500 MDI children. Should be enough :-)
72 static const int wxFIRST_MDI_CHILD
= 4100;
73 static const int wxLAST_MDI_CHILD
= 4600;
75 // ---------------------------------------------------------------------------
77 // ---------------------------------------------------------------------------
79 // set the MDI menus (by sending the WM_MDISETMENU message) and update the menu
80 // of the parent of win (which is supposed to be the MDI client window)
81 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
);
83 // insert the window menu (subMenu) into menu just before "Help" submenu or at
84 // the very end if not found
85 static void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
);
87 // Remove the window menu
88 static void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU menu
);
90 // is this an id of an MDI child?
91 inline bool IsMdiCommandId(int id
)
93 return (id
>= wxFIRST_MDI_CHILD
) && (id
<= wxLAST_MDI_CHILD
);
96 // unpack the parameters of WM_MDIACTIVATE message
97 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
98 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
);
100 // return the HMENU of the MDI menu
102 // this function works correctly even when we don't have a window menu and just
104 static inline HMENU
GetMDIWindowMenu(wxMDIParentFrame
*frame
)
106 wxMenu
*menu
= frame
->GetWindowMenu();
107 return menu
? GetHmenuOf(menu
) : 0;
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 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 wxMDIParentFrame::wxMDIParentFrame()
143 m_parentFrameActive
= true;
146 bool wxMDIParentFrame::Create(wxWindow
*parent
,
148 const wxString
& title
,
152 const wxString
& name
)
154 // this style can be used to prevent a window from having the standard MDI
156 if ( !(style
& wxFRAME_NO_WINDOW_MENU
) )
158 // normal case: we have the window menu, so construct it
159 m_windowMenu
= new wxMenu
;
161 m_windowMenu
->Append(IDM_WINDOWCASCADE
, _("&Cascade"));
162 m_windowMenu
->Append(IDM_WINDOWTILEHOR
, _("Tile &Horizontally"));
163 m_windowMenu
->Append(IDM_WINDOWTILEVERT
, _("Tile &Vertically"));
164 m_windowMenu
->AppendSeparator();
165 m_windowMenu
->Append(IDM_WINDOWICONS
, _("&Arrange Icons"));
166 m_windowMenu
->Append(IDM_WINDOWNEXT
, _("&Next"));
167 m_windowMenu
->Append(IDM_WINDOWPREV
, _("&Previous"));
170 m_parentFrameActive
= true;
173 wxTopLevelWindows
.Append(this);
176 m_windowStyle
= style
;
179 parent
->AddChild(this);
181 if ( id
!= wxID_ANY
)
184 m_windowId
= NewControlId();
187 WXDWORD msflags
= MSWGetCreateWindowFlags(&exflags
);
188 msflags
&= ~WS_VSCROLL
;
189 msflags
&= ~WS_HSCROLL
;
191 if ( !wxWindow::MSWCreate(wxMDIFrameClassName
,
200 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
202 // unlike (almost?) all other windows, frames are created hidden
208 wxMDIParentFrame::~wxMDIParentFrame()
210 // see comment in ~wxMDIChildFrame
212 m_frameToolBar
= NULL
;
215 m_frameStatusBar
= NULL
;
216 #endif // wxUSE_STATUSBAR
220 // the MDI frame menubar is not automatically deleted by Windows unlike for
223 ::DestroyMenu((HMENU
)m_hMenu
);
225 if ( m_clientWindow
)
227 if ( m_clientWindow
->MSWGetOldWndProc() )
228 m_clientWindow
->UnsubclassWin();
230 m_clientWindow
->SetHWND(0);
231 delete m_clientWindow
;
235 // ----------------------------------------------------------------------------
236 // wxMDIParentFrame child management
237 // ----------------------------------------------------------------------------
239 int wxMDIParentFrame::GetChildFramesCount() const
242 for ( wxWindowList::const_iterator i
= GetChildren().begin();
243 i
!= GetChildren().end();
246 if ( wxDynamicCast(*i
, wxMDIChildFrame
) )
253 void wxMDIParentFrame::AddMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
255 if ( GetChildFramesCount() == 1 )
257 // first MDI child added, we need to insert the window menu now if we
263 void wxMDIParentFrame::RemoveMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
265 if ( GetChildFramesCount() == 1 )
267 // last MDI child is being removed, remove the now unnecessary window
273 // ----------------------------------------------------------------------------
274 // wxMDIParentFrame window menu handling
275 // ----------------------------------------------------------------------------
277 void wxMDIParentFrame::AddWindowMenu()
280 MDIInsertWindowMenu(GetClientWindow(), m_hMenu
, GetMDIWindowMenu(this));
283 void wxMDIParentFrame::RemoveWindowMenu()
286 MDIRemoveWindowMenu(GetClientWindow(), m_hMenu
);
289 #if wxUSE_MENUS_NATIVE
291 void wxMDIParentFrame::InternalSetMenuBar()
293 m_parentFrameActive
= true;
295 if ( GetActiveChild() )
299 else // we don't have any MDI children yet
301 // wait until we do to add the window menu but do set the main menu for
302 // now (this is done by AddWindowMenu() as a side effect)
303 MDISetMenu(GetClientWindow(), (HMENU
)m_hMenu
, NULL
);
307 #endif // wxUSE_MENUS_NATIVE
309 void wxMDIParentFrame::SetWindowMenu(wxMenu
* menu
)
311 // notice that Remove/AddWindowMenu() are safe to call even when
312 // m_windowMenu is NULL
322 void wxMDIParentFrame::DoMenuUpdates(wxMenu
* menu
)
324 wxMDIChildFrame
*child
= GetActiveChild();
327 wxEvtHandler
* source
= child
->GetEventHandler();
328 wxMenuBar
* bar
= child
->GetMenuBar();
332 menu
->UpdateUI(source
);
338 int nCount
= bar
->GetMenuCount();
339 for (int n
= 0; n
< nCount
; n
++)
340 bar
->GetMenu(n
)->UpdateUI(source
);
346 wxFrameBase::DoMenuUpdates(menu
);
350 const wxMenuItem
*wxMDIParentFrame::FindItemInMenuBar(int menuId
) const
352 const wxMenuItem
*item
= wxFrame::FindItemInMenuBar(menuId
);
353 if ( !item
&& m_currentChild
)
355 item
= m_currentChild
->FindItemInMenuBar(menuId
);
361 void wxMDIParentFrame::UpdateClientSize()
363 if ( GetClientWindow() )
366 GetClientSize(&width
, &height
);
368 GetClientWindow()->SetSize(0, 0, width
, height
);
372 void wxMDIParentFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
376 // do not call event.Skip() here, it somehow messes up MDI client window
379 void wxMDIParentFrame::OnIconized(wxIconizeEvent
& event
)
383 if ( !event
.IsIconized() )
387 // Returns the active MDI child window
388 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
390 HWND hWnd
= (HWND
)::SendMessage(GetWinHwnd(GetClientWindow()),
391 WM_MDIGETACTIVE
, 0, 0L);
395 return (wxMDIChildFrame
*)wxFindWinFromHandle(hWnd
);
398 // Responds to colour changes, and passes event on to children.
399 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
401 if ( m_clientWindow
)
403 m_clientWindow
->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
404 m_clientWindow
->Refresh();
410 WXHICON
wxMDIParentFrame::GetDefaultIcon() const
412 // we don't have any standard icons (any more)
416 // ---------------------------------------------------------------------------
418 // ---------------------------------------------------------------------------
420 void wxMDIParentFrame::Cascade()
422 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE
, 0, 0);
425 void wxMDIParentFrame::Tile(wxOrientation orient
)
427 wxASSERT_MSG( orient
== wxHORIZONTAL
|| orient
== wxVERTICAL
,
428 _T("invalid orientation value") );
430 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE
,
431 orient
== wxHORIZONTAL
? MDITILE_HORIZONTAL
432 : MDITILE_VERTICAL
, 0);
435 void wxMDIParentFrame::ArrangeIcons()
437 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE
, 0, 0);
440 void wxMDIParentFrame::ActivateNext()
442 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 0);
445 void wxMDIParentFrame::ActivatePrevious()
447 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 1);
450 // ---------------------------------------------------------------------------
451 // the MDI parent frame window proc
452 // ---------------------------------------------------------------------------
454 WXLRESULT
wxMDIParentFrame::MSWWindowProc(WXUINT message
,
459 bool processed
= false;
465 WXWORD state
, minimized
;
467 UnpackActivate(wParam
, lParam
, &state
, &minimized
, &hwnd
);
469 processed
= HandleActivate(state
, minimized
!= 0, hwnd
);
477 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
479 (void)HandleCommand(id
, cmd
, hwnd
);
481 // even if the frame didn't process it, there is no need to try it
482 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
483 // so pretend we processed the message anyhow
487 // always pass this message DefFrameProc(), otherwise MDI menu
488 // commands (and sys commands - more surprisingly!) won't work
489 MSWDefWindowProc(message
, wParam
, lParam
);
493 m_clientWindow
= OnCreateClient();
494 // Uses own style for client style
495 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
497 wxLogMessage(_("Failed to create MDI parent frame."));
508 // we erase background ourselves
513 // though we don't (usually) resize the MDI client to exactly fit the
514 // client area we need to pass this one to DefFrameProc to allow the children to show
519 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
524 bool wxMDIParentFrame::HandleActivate(int state
, bool minimized
, WXHWND activate
)
526 bool processed
= false;
528 if ( wxWindow::HandleActivate(state
, minimized
, activate
) )
534 // If this window is an MDI parent, we must also send an OnActivate message
535 // to the current child.
536 if ( (m_currentChild
!= NULL
) &&
537 ((state
== WA_ACTIVE
) || (state
== WA_CLICKACTIVE
)) )
539 wxActivateEvent
event(wxEVT_ACTIVATE
, true, m_currentChild
->GetId());
540 event
.SetEventObject( m_currentChild
);
541 if ( m_currentChild
->HandleWindowEvent(event
) )
548 bool wxMDIParentFrame::HandleCommand(WXWORD id_
, WXWORD cmd
, WXHWND hwnd
)
550 // sign extend to int from short before comparing with the other int ids
551 int id
= (signed short)id_
;
553 // In case it's e.g. a toolbar.
556 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
558 return win
->MSWCommand(cmd
, id
);
561 if (wxCurrentPopupMenu
)
563 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
564 wxCurrentPopupMenu
= NULL
;
565 if (popupMenu
->MSWCommand(cmd
, id
))
569 // is it one of standard MDI commands?
575 case IDM_WINDOWCASCADE
:
577 wParam
= MDITILE_SKIPDISABLED
;
580 case IDM_WINDOWTILEHOR
:
581 wParam
|= MDITILE_HORIZONTAL
;
584 case IDM_WINDOWTILEVERT
:
586 wParam
= MDITILE_VERTICAL
;
588 wParam
|= MDITILE_SKIPDISABLED
;
591 case IDM_WINDOWICONS
:
592 msg
= WM_MDIICONARRANGE
;
597 lParam
= 0; // next child
602 lParam
= 1; // previous child
611 ::SendMessage(GetWinHwnd(GetClientWindow()), msg
, wParam
, lParam
);
616 // FIXME VZ: what does this test do??
619 return false; // Get WndProc to call default proc
622 if ( IsMdiCommandId(id
) )
624 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
627 wxWindow
*child
= node
->GetData();
628 if ( child
->GetHWND() )
630 int childId
= wxGetWindowId(child
->GetHWND());
631 if ( childId
== (signed short)id
)
633 ::SendMessage( GetWinHwnd(GetClientWindow()),
635 (WPARAM
)child
->GetHWND(), 0);
639 node
= node
->GetNext();
642 else if ( m_parentFrameActive
)
644 return ProcessCommand(id
);
646 else if ( m_currentChild
)
648 return m_currentChild
->HandleCommand(id
, cmd
, hwnd
);
652 // this shouldn't happen because it means that our messages are being
653 // lost (they're not sent to the parent frame nor to the children)
654 wxFAIL_MSG(wxT("MDI parent frame is not active, yet there is no active MDI child?"));
660 WXLRESULT
wxMDIParentFrame::MSWDefWindowProc(WXUINT message
,
665 if ( GetClientWindow() )
666 clientWnd
= GetClientWindow()->GetHWND();
670 return DefFrameProc(GetHwnd(), (HWND
)clientWnd
, message
, wParam
, lParam
);
673 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG
* msg
)
675 MSG
*pMsg
= (MSG
*)msg
;
677 // first let the current child get it
678 if ( m_currentChild
&& m_currentChild
->GetHWND() &&
679 m_currentChild
->MSWTranslateMessage(msg
) )
684 // then try out accel table (will also check the menu accels)
685 if ( wxFrame::MSWTranslateMessage(msg
) )
690 // finally, check for MDI specific built in accel keys
691 if ( pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
693 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg
))
700 // ===========================================================================
702 // ===========================================================================
704 void wxMDIChildFrame::Init()
706 m_needsResize
= true;
707 m_needsInitialShow
= true;
710 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
712 const wxString
& title
,
716 const wxString
& name
)
718 m_mdiParent
= parent
;
722 if ( id
!= wxID_ANY
)
725 m_windowId
= NewControlId();
729 parent
->AddChild(this);
739 mcs
.szClass
= style
& wxFULL_REPAINT_ON_RESIZE
740 ? wxMDIChildFrameClassName
741 : wxMDIChildFrameClassNameNoRedraw
;
742 mcs
.szTitle
= title
.wx_str();
743 mcs
.hOwner
= wxGetInstance();
744 if (x
!= wxDefaultCoord
)
747 mcs
.x
= CW_USEDEFAULT
;
749 if (y
!= wxDefaultCoord
)
752 mcs
.y
= CW_USEDEFAULT
;
754 if (width
!= wxDefaultCoord
)
757 mcs
.cx
= CW_USEDEFAULT
;
759 if (height
!= wxDefaultCoord
)
762 mcs
.cy
= CW_USEDEFAULT
;
764 DWORD msflags
= WS_OVERLAPPED
| WS_CLIPCHILDREN
;
765 if (style
& wxMINIMIZE_BOX
)
766 msflags
|= WS_MINIMIZEBOX
;
767 if (style
& wxMAXIMIZE_BOX
)
768 msflags
|= WS_MAXIMIZEBOX
;
769 if (style
& wxRESIZE_BORDER
)
770 msflags
|= WS_THICKFRAME
;
771 if (style
& wxSYSTEM_MENU
)
772 msflags
|= WS_SYSMENU
;
773 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
774 msflags
|= WS_MINIMIZE
;
775 if (style
& wxMAXIMIZE
)
776 msflags
|= WS_MAXIMIZE
;
777 if (style
& wxCAPTION
)
778 msflags
|= WS_CAPTION
;
784 wxWindowCreationHook
hook(this);
786 m_hWnd
= (WXHWND
)::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
787 WM_MDICREATE
, 0, (LPARAM
)&mcs
);
791 wxLogLastError(_T("WM_MDICREATE"));
797 parent
->AddMDIChild(this);
802 wxMDIChildFrame::~wxMDIChildFrame()
804 // if we hadn't been created, there is nothing to destroy
808 GetMDIParent()->RemoveMDIChild(this);
810 // will be destroyed by DestroyChildren() but reset them before calling it
811 // to avoid using dangling pointers if a callback comes in the meanwhile
813 m_frameToolBar
= NULL
;
816 m_frameStatusBar
= NULL
;
817 #endif // wxUSE_STATUSBAR
821 MDIRemoveWindowMenu(NULL
, m_hMenu
);
826 bool wxMDIChildFrame::Show(bool show
)
828 m_needsInitialShow
= false;
830 if (!wxFrame::Show(show
))
833 // KH: Without this call, new MDI children do not become active.
834 // This was added here after the same BringWindowToTop call was
835 // removed from wxTopLevelWindow::Show (November 2005)
837 ::BringWindowToTop(GetHwnd());
839 // we need to refresh the MDI frame window menu to include (or exclude if
840 // we've been hidden) this frame
841 wxMDIParentFrame
* const parent
= GetMDIParent();
842 MDISetMenu(parent
->GetClientWindow(), NULL
, NULL
);
847 // Set the client size (i.e. leave the calculation of borders etc.
849 void wxMDIChildFrame::DoSetClientSize(int width
, int height
)
851 HWND hWnd
= GetHwnd();
854 ::GetClientRect(hWnd
, &rect
);
857 GetWindowRect(hWnd
, &rect2
);
859 // Find the difference between the entire window (title bar and all)
860 // and the client area; add this to the new client size to move the
862 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
863 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
866 if (GetStatusBar() && GetStatusBar()->IsShown())
869 GetStatusBar()->GetSize(&sx
, &sy
);
872 #endif // wxUSE_STATUSBAR
875 point
.x
= rect2
.left
;
878 // If there's an MDI parent, must subtract the parent's top left corner
879 // since MoveWindow moves relative to the parent
880 wxMDIParentFrame
* const mdiParent
= GetMDIParent();
881 ::ScreenToClient(GetHwndOf(mdiParent
->GetClientWindow()), &point
);
883 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)true);
885 wxSize
size(width
, height
);
886 wxSizeEvent
event(size
, m_windowId
);
887 event
.SetEventObject( this );
888 HandleWindowEvent(event
);
891 // Unlike other wxTopLevelWindowBase, the mdi child's "GetPosition" is not the
892 // same as its GetScreenPosition
893 void wxMDIChildFrame::DoGetScreenPosition(int *x
, int *y
) const
895 HWND hWnd
= GetHwnd();
898 ::GetWindowRect(hWnd
, &rect
);
906 void wxMDIChildFrame::DoGetPosition(int *x
, int *y
) const
909 GetWindowRect(GetHwnd(), &rect
);
914 // Since we now have the absolute screen coords,
915 // if there's a parent we must subtract its top left corner
916 wxMDIParentFrame
* const mdiParent
= GetMDIParent();
917 ::ScreenToClient(GetHwndOf(mdiParent
->GetClientWindow()), &point
);
925 void wxMDIChildFrame::InternalSetMenuBar()
927 wxMDIParentFrame
* const parent
= GetMDIParent();
929 MDIInsertWindowMenu(parent
->GetClientWindow(),
930 m_hMenu
, GetMDIWindowMenu(parent
));
932 parent
->m_parentFrameActive
= false;
935 void wxMDIChildFrame::DetachMenuBar()
937 MDIRemoveWindowMenu(NULL
, m_hMenu
);
938 wxFrame::DetachMenuBar();
941 WXHICON
wxMDIChildFrame::GetDefaultIcon() const
943 // we don't have any standard icons (any more)
947 // ---------------------------------------------------------------------------
949 // ---------------------------------------------------------------------------
951 void wxMDIChildFrame::Maximize(bool maximize
)
953 wxMDIParentFrame
* const parent
= GetMDIParent();
954 if ( parent
&& parent
->GetClientWindow() )
956 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
957 maximize
? WM_MDIMAXIMIZE
: WM_MDIRESTORE
,
958 (WPARAM
)GetHwnd(), 0);
962 void wxMDIChildFrame::Restore()
964 wxMDIParentFrame
* const parent
= GetMDIParent();
965 if ( parent
&& parent
->GetClientWindow() )
967 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIRESTORE
,
968 (WPARAM
) GetHwnd(), 0);
972 void wxMDIChildFrame::Activate()
974 wxMDIParentFrame
* const parent
= GetMDIParent();
975 if ( parent
&& parent
->GetClientWindow() )
977 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIACTIVATE
,
978 (WPARAM
) GetHwnd(), 0);
982 // ---------------------------------------------------------------------------
983 // MDI window proc and message handlers
984 // ---------------------------------------------------------------------------
986 WXLRESULT
wxMDIChildFrame::MSWWindowProc(WXUINT message
,
991 bool processed
= false;
999 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
1002 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
1006 case WM_GETMINMAXINFO
:
1007 processed
= HandleGetMinMaxInfo((MINMAXINFO
*)lParam
);
1010 case WM_MDIACTIVATE
:
1013 WXHWND hwndAct
, hwndDeact
;
1014 UnpackMDIActivate(wParam
, lParam
, &act
, &hwndAct
, &hwndDeact
);
1016 processed
= HandleMDIActivate(act
, hwndAct
, hwndDeact
);
1021 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
1022 // scrollbars if necessary
1027 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
1029 MSWDefWindowProc(message
, wParam
, lParam
);
1033 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
1034 // the message (the base class version does not)
1035 return MSWDefWindowProc(message
, wParam
, lParam
);
1037 case WM_WINDOWPOSCHANGING
:
1038 processed
= HandleWindowPosChanging((LPWINDOWPOS
)lParam
);
1043 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
1048 bool wxMDIChildFrame::HandleCommand(WXWORD id_
, WXWORD cmd
, WXHWND hwnd
)
1050 // sign extend to int from short before comparing with the other int ids
1051 int id
= (signed short)id_
;
1053 // In case it's e.g. a toolbar.
1056 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
1058 return win
->MSWCommand(cmd
, id
);
1061 if (wxCurrentPopupMenu
)
1063 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
1064 wxCurrentPopupMenu
= NULL
;
1065 if (popupMenu
->MSWCommand(cmd
, id
))
1070 if (GetMenuBar() && GetMenuBar()->FindItem(id
))
1072 processed
= ProcessCommand(id
);
1082 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate
),
1086 wxMDIParentFrame
* const parent
= GetMDIParent();
1088 HMENU menuToSet
= 0;
1092 if ( m_hWnd
== hwndAct
)
1095 parent
->m_currentChild
= this;
1097 HMENU child_menu
= (HMENU
)GetWinMenu();
1100 parent
->m_parentFrameActive
= false;
1102 menuToSet
= child_menu
;
1105 else if ( m_hWnd
== hwndDeact
)
1107 wxASSERT_MSG( parent
->m_currentChild
== this,
1108 wxT("can't deactivate MDI child which wasn't active!") );
1111 parent
->m_currentChild
= NULL
;
1113 HMENU parent_menu
= (HMENU
)parent
->GetWinMenu();
1115 // activate the the parent menu only when there is no other child
1116 // that has been activated
1117 if ( parent_menu
&& !hwndAct
)
1119 parent
->m_parentFrameActive
= true;
1121 menuToSet
= parent_menu
;
1126 // we have nothing to do with it
1132 MDISetMenu(parent
->GetClientWindow(),
1133 menuToSet
, GetMDIWindowMenu(parent
));
1136 wxActivateEvent
event(wxEVT_ACTIVATE
, activated
, m_windowId
);
1137 event
.SetEventObject( this );
1139 ResetWindowStyle((void *)NULL
);
1141 return HandleWindowEvent(event
);
1144 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos
)
1146 WINDOWPOS
*lpPos
= (WINDOWPOS
*)pos
;
1148 if (!(lpPos
->flags
& SWP_NOSIZE
))
1151 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1152 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1153 if (ResetWindowStyle((void *) & rectClient
) && (dwStyle
& WS_MAXIMIZE
))
1155 ::AdjustWindowRectEx(&rectClient
, dwStyle
, false, dwExStyle
);
1156 lpPos
->x
= rectClient
.left
;
1157 lpPos
->y
= rectClient
.top
;
1158 lpPos
->cx
= rectClient
.right
- rectClient
.left
;
1159 lpPos
->cy
= rectClient
.bottom
- rectClient
.top
;
1166 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo
)
1168 MINMAXINFO
*info
= (MINMAXINFO
*)mmInfo
;
1170 // let the default window proc calculate the size of MDI children
1171 // frames because it is based on the size of the MDI client window,
1172 // not on the values specified in wxWindow m_max variables
1173 bool processed
= MSWDefWindowProc(WM_GETMINMAXINFO
, 0, (LPARAM
)mmInfo
) != 0;
1175 int minWidth
= GetMinWidth(),
1176 minHeight
= GetMinHeight();
1178 // but allow GetSizeHints() to set the min size
1179 if ( minWidth
!= wxDefaultCoord
)
1181 info
->ptMinTrackSize
.x
= minWidth
;
1186 if ( minHeight
!= wxDefaultCoord
)
1188 info
->ptMinTrackSize
.y
= minHeight
;
1196 // ---------------------------------------------------------------------------
1197 // MDI specific message translation/preprocessing
1198 // ---------------------------------------------------------------------------
1200 WXLRESULT
wxMDIChildFrame::MSWDefWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1202 return DefMDIChildProc(GetHwnd(),
1203 (UINT
)message
, (WPARAM
)wParam
, (LPARAM
)lParam
);
1206 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG
* msg
)
1208 // we must pass the parent frame to ::TranslateAccelerator(), otherwise it
1209 // doesn't do its job correctly for MDI child menus
1210 return MSWDoTranslateMessage(GetMDIParent(), msg
);
1213 // ---------------------------------------------------------------------------
1215 // ---------------------------------------------------------------------------
1217 void wxMDIChildFrame::MSWDestroyWindow()
1219 wxMDIParentFrame
* const parent
= GetMDIParent();
1221 // Must make sure this handle is invalidated (set to NULL) since all sorts
1222 // of things could happen after the child client is destroyed, but before
1223 // the wxFrame is destroyed.
1225 HWND oldHandle
= (HWND
)GetHWND();
1226 SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIDESTROY
,
1227 (WPARAM
)oldHandle
, 0);
1229 if (parent
->GetActiveChild() == NULL
)
1230 ResetWindowStyle(NULL
);
1234 ::DestroyMenu((HMENU
) m_hMenu
);
1237 wxRemoveHandleAssociation(this);
1241 // Change the client window's extended style so we don't get a client edge
1242 // style when a child is maximised (a double border looks silly.)
1243 bool wxMDIChildFrame::ResetWindowStyle(void *vrect
)
1245 RECT
*rect
= (RECT
*)vrect
;
1246 wxMDIParentFrame
* const pFrameWnd
= GetMDIParent();
1247 wxMDIChildFrame
* pChild
= pFrameWnd
->GetActiveChild();
1249 if (!pChild
|| (pChild
== this))
1251 HWND hwndClient
= GetWinHwnd(pFrameWnd
->GetClientWindow());
1252 DWORD dwStyle
= ::GetWindowLong(hwndClient
, GWL_EXSTYLE
);
1254 // we want to test whether there is a maximized child, so just set
1255 // dwThisStyle to 0 if there is no child at all
1256 DWORD dwThisStyle
= pChild
1257 ? ::GetWindowLong(GetWinHwnd(pChild
), GWL_STYLE
) : 0;
1258 DWORD dwNewStyle
= dwStyle
;
1259 if ( dwThisStyle
& WS_MAXIMIZE
)
1260 dwNewStyle
&= ~(WS_EX_CLIENTEDGE
);
1262 dwNewStyle
|= WS_EX_CLIENTEDGE
;
1264 if (dwStyle
!= dwNewStyle
)
1266 // force update of everything
1267 ::RedrawWindow(hwndClient
, NULL
, NULL
,
1268 RDW_INVALIDATE
| RDW_ALLCHILDREN
);
1269 ::SetWindowLong(hwndClient
, GWL_EXSTYLE
, dwNewStyle
);
1270 ::SetWindowPos(hwndClient
, NULL
, 0, 0, 0, 0,
1271 SWP_FRAMECHANGED
| SWP_NOACTIVATE
|
1272 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1275 ::GetClientRect(hwndClient
, rect
);
1284 // ===========================================================================
1285 // wxMDIClientWindow: the window of predefined (by Windows) class which
1286 // contains the child frames
1287 // ===========================================================================
1289 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
1291 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1293 CLIENTCREATESTRUCT ccs
;
1294 m_windowStyle
= style
;
1297 ccs
.hWindowMenu
= GetMDIWindowMenu(parent
);
1298 ccs
.idFirstChild
= wxFIRST_MDI_CHILD
;
1300 DWORD msStyle
= MDIS_ALLCHILDSTYLES
| WS_VISIBLE
| WS_CHILD
|
1301 WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
;
1303 if ( style
& wxHSCROLL
)
1304 msStyle
|= WS_HSCROLL
;
1305 if ( style
& wxVSCROLL
)
1306 msStyle
|= WS_VSCROLL
;
1308 DWORD exStyle
= WS_EX_CLIENTEDGE
;
1310 wxWindowCreationHook
hook(this);
1311 m_hWnd
= (WXHWND
)::CreateWindowEx
1321 (LPSTR
)(LPCLIENTCREATESTRUCT
)&ccs
);
1324 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1329 SubclassWin(m_hWnd
);
1334 // Explicitly call default scroll behaviour
1335 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)
1337 // Note: for client windows, the scroll position is not set in
1338 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1339 // scroll position we're at.
1340 // This makes it hard to paint patterns or bitmaps in the background,
1341 // and have the client area scrollable as well.
1343 if ( event
.GetOrientation() == wxHORIZONTAL
)
1344 m_scrollX
= event
.GetPosition(); // Always returns zero!
1346 m_scrollY
= event
.GetPosition(); // Always returns zero!
1351 void wxMDIClientWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1353 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1354 // client area, you can end up with a non-refreshed portion in the client window
1355 // (see OGL studio sample). So check if the position is changed and if so,
1356 // redraw the MDI child frames.
1358 const wxPoint oldPos
= GetPosition();
1360 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
| wxSIZE_FORCE
);
1362 const wxPoint newPos
= GetPosition();
1364 if ((newPos
.x
!= oldPos
.x
) || (newPos
.y
!= oldPos
.y
))
1368 wxWindowList::compatibility_iterator node
= GetParent()->GetChildren().GetFirst();
1371 wxWindow
*child
= node
->GetData();
1372 if (child
->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
1374 ::RedrawWindow(GetHwndOf(child
),
1381 node
= node
->GetNext();
1387 void wxMDIChildFrame::OnIdle(wxIdleEvent
& event
)
1389 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1390 // in flicker e.g. when the frame contained controls with non-trivial
1391 // layout. Since 2.5.3, the frame is created hidden as all other top level
1392 // windows. In order to maintain backward compatibility, the frame is shown
1393 // in OnIdle, unless Show(false) was called by the programmer before.
1394 if ( m_needsInitialShow
)
1399 // MDI child frames get their WM_SIZE when they're constructed but at this
1400 // moment they don't have any children yet so all child windows will be
1401 // positioned incorrectly when they are added later - to fix this, we
1402 // generate an artificial size event here
1403 if ( m_needsResize
)
1405 m_needsResize
= false; // avoid any possibility of recursion
1413 // ---------------------------------------------------------------------------
1414 // non member functions
1415 // ---------------------------------------------------------------------------
1417 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
)
1419 if ( hmenuFrame
|| hmenuWindow
)
1421 if ( !::SendMessage(GetWinHwnd(win
),
1424 (LPARAM
)hmenuWindow
) )
1427 DWORD err
= ::GetLastError();
1429 wxLogApiError(_T("SendMessage(WM_MDISETMENU)"), err
);
1430 #endif // __WXDEBUG__
1434 // update menu bar of the parent window
1435 wxWindow
*parent
= win
->GetParent();
1436 wxCHECK_RET( parent
, wxT("MDI client without parent frame? weird...") );
1438 ::SendMessage(GetWinHwnd(win
), WM_MDIREFRESHMENU
, 0, 0L);
1440 ::DrawMenuBar(GetWinHwnd(parent
));
1443 static void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
)
1445 // Try to insert Window menu in front of Help, otherwise append it.
1446 HMENU hmenu
= (HMENU
)menu
;
1450 int N
= GetMenuItemCount(hmenu
);
1451 bool success
= false;
1452 for ( int i
= 0; i
< N
; i
++ )
1455 int chars
= GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
);
1458 wxLogLastError(wxT("GetMenuString"));
1463 wxString
strBuf(buf
);
1464 if ( wxStripMenuCodes(strBuf
) == wxGetStockLabel(wxID_HELP
,false) )
1467 ::InsertMenu(hmenu
, i
, MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1468 (UINT_PTR
)subMenu
, _("&Window").wx_str());
1475 ::AppendMenu(hmenu
, MF_POPUP
,
1476 (UINT_PTR
)subMenu
, _("&Window").wx_str());
1480 MDISetMenu(win
, hmenu
, subMenu
);
1483 static void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU menu
)
1485 HMENU hMenu
= (HMENU
)menu
;
1491 int N
= ::GetMenuItemCount(hMenu
);
1492 for ( int i
= 0; i
< N
; i
++ )
1494 if ( !::GetMenuString(hMenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1496 // Ignore successful read of menu string with length 0 which
1497 // occurs, for example, for a maximized MDI childs system menu
1498 if ( ::GetLastError() != 0 )
1500 wxLogLastError(wxT("GetMenuString"));
1506 if ( wxStrcmp(buf
, _("&Window")) == 0 )
1508 if ( !::RemoveMenu(hMenu
, i
, MF_BYPOSITION
) )
1510 wxLogLastError(wxT("RemoveMenu"));
1520 // we don't change the windows menu, but we update the main one
1521 MDISetMenu(win
, hMenu
, NULL
);
1525 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
1526 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
)
1529 *hwndAct
= (WXHWND
)lParam
;
1530 *hwndDeact
= (WXHWND
)wParam
;
1533 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)