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"
47 #if wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR
48 #include "wx/msw/statbr95.h"
53 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
57 extern wxMenu
*wxCurrentPopupMenu
;
59 extern const wxChar
*wxMDIFrameClassName
; // from app.cpp
60 extern const wxChar
*wxMDIChildFrameClassName
;
61 extern const wxChar
*wxMDIChildFrameClassNameNoRedraw
;
62 extern void wxRemoveHandleAssociation(wxWindow
*win
);
64 // ---------------------------------------------------------------------------
66 // ---------------------------------------------------------------------------
68 static const int IDM_WINDOWTILEHOR
= 4001;
69 static const int IDM_WINDOWCASCADE
= 4002;
70 static const int IDM_WINDOWICONS
= 4003;
71 static const int IDM_WINDOWNEXT
= 4004;
72 static const int IDM_WINDOWTILEVERT
= 4005;
73 static const int IDM_WINDOWPREV
= 4006;
75 // This range gives a maximum of 500 MDI children. Should be enough :-)
76 static const int wxFIRST_MDI_CHILD
= 4100;
77 static const int wxLAST_MDI_CHILD
= 4600;
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 static 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 static void InsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
);
91 // Remove the window menu
92 static void RemoveWindowMenu(wxWindow
*win
, WXHMENU menu
);
94 // is this an id of an MDI child?
95 inline bool IsMdiCommandId(int id
)
97 return (id
>= wxFIRST_MDI_CHILD
) && (id
<= wxLAST_MDI_CHILD
);
100 // unpack the parameters of WM_MDIACTIVATE message
101 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
102 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
);
104 // return the HMENU of the MDI menu
105 static inline HMENU
GetMDIWindowMenu(wxMDIParentFrame
*frame
)
107 wxMenu
*menu
= frame
->GetWindowMenu();
108 return menu
? GetHmenuOf(menu
) : 0;
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 BEGIN_EVENT_TABLE(wxMDIChildFrame
, wxFrame
)
130 EVT_IDLE(wxMDIChildFrame::OnIdle
)
133 BEGIN_EVENT_TABLE(wxMDIClientWindow
, wxWindow
)
134 EVT_SCROLL(wxMDIClientWindow::OnScroll
)
137 // ===========================================================================
138 // wxMDIParentFrame: the frame which contains the client window which manages
140 // ===========================================================================
142 wxMDIParentFrame::wxMDIParentFrame()
144 m_clientWindow
= NULL
;
145 m_currentChild
= NULL
;
146 m_windowMenu
= (wxMenu
*) NULL
;
147 m_parentFrameActive
= true;
150 bool wxMDIParentFrame::Create(wxWindow
*parent
,
152 const wxString
& title
,
156 const wxString
& name
)
158 m_clientWindow
= NULL
;
159 m_currentChild
= NULL
;
161 // this style can be used to prevent a window from having the standard MDI
163 if ( style
& wxFRAME_NO_WINDOW_MENU
)
165 m_windowMenu
= (wxMenu
*)NULL
;
167 else // normal case: we have the window menu, so construct it
169 m_windowMenu
= new wxMenu
;
171 m_windowMenu
->Append(IDM_WINDOWCASCADE
, _("&Cascade"));
172 m_windowMenu
->Append(IDM_WINDOWTILEHOR
, _("Tile &Horizontally"));
173 m_windowMenu
->Append(IDM_WINDOWTILEVERT
, _("Tile &Vertically"));
174 m_windowMenu
->AppendSeparator();
175 m_windowMenu
->Append(IDM_WINDOWICONS
, _("&Arrange Icons"));
176 m_windowMenu
->Append(IDM_WINDOWNEXT
, _("&Next"));
177 m_windowMenu
->Append(IDM_WINDOWPREV
, _("&Previous"));
180 m_parentFrameActive
= true;
183 wxTopLevelWindows
.Append(this);
186 m_windowStyle
= style
;
189 parent
->AddChild(this);
191 if ( id
!= wxID_ANY
)
194 m_windowId
= NewControlId();
197 WXDWORD msflags
= MSWGetCreateWindowFlags(&exflags
);
198 msflags
&= ~WS_VSCROLL
;
199 msflags
&= ~WS_HSCROLL
;
201 if ( !wxWindow::MSWCreate(wxMDIFrameClassName
,
210 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
212 // unlike (almost?) all other windows, frames are created hidden
218 wxMDIParentFrame::~wxMDIParentFrame()
220 // see comment in ~wxMDIChildFrame
222 m_frameToolBar
= NULL
;
225 m_frameStatusBar
= NULL
;
226 #endif // wxUSE_STATUSBAR
233 m_windowMenu
= (wxMenu
*) NULL
;
236 // the MDI frame menubar is not automatically deleted by Windows unlike for
240 ::DestroyMenu((HMENU
)m_hMenu
);
241 m_hMenu
= (WXHMENU
)NULL
;
244 if ( m_clientWindow
)
246 if ( m_clientWindow
->MSWGetOldWndProc() )
247 m_clientWindow
->UnsubclassWin();
249 m_clientWindow
->SetHWND(0);
250 delete m_clientWindow
;
254 #if wxUSE_MENUS_NATIVE
256 void wxMDIParentFrame::InternalSetMenuBar()
258 m_parentFrameActive
= true;
260 InsertWindowMenu(GetClientWindow(), m_hMenu
, GetMDIWindowMenu(this));
263 #endif // wxUSE_MENUS_NATIVE
265 void wxMDIParentFrame::SetWindowMenu(wxMenu
* menu
)
271 // Remove old window menu
272 RemoveWindowMenu(GetClientWindow(), m_hMenu
);
276 m_windowMenu
= (wxMenu
*) NULL
;
284 InsertWindowMenu(GetClientWindow(), m_hMenu
,
285 GetHmenuOf(m_windowMenu
));
290 void wxMDIParentFrame::DoMenuUpdates(wxMenu
* menu
)
292 wxMDIChildFrame
*child
= GetActiveChild();
295 wxEvtHandler
* source
= child
->GetEventHandler();
296 wxMenuBar
* bar
= child
->GetMenuBar();
300 menu
->UpdateUI(source
);
306 int nCount
= bar
->GetMenuCount();
307 for (int n
= 0; n
< nCount
; n
++)
308 bar
->GetMenu(n
)->UpdateUI(source
);
314 wxFrameBase::DoMenuUpdates(menu
);
318 void wxMDIParentFrame::UpdateClientSize()
320 if ( GetClientWindow() )
323 GetClientSize(&width
, &height
);
325 GetClientWindow()->SetSize(0, 0, width
, height
);
329 void wxMDIParentFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
333 // do not call event.Skip() here, it somehow messes up MDI client window
336 void wxMDIParentFrame::OnIconized(wxIconizeEvent
& event
)
340 if ( !event
.Iconized() )
346 // Returns the active MDI child window
347 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
349 HWND hWnd
= (HWND
)::SendMessage(GetWinHwnd(GetClientWindow()),
350 WM_MDIGETACTIVE
, 0, 0L);
354 return (wxMDIChildFrame
*)wxFindWinFromHandle((WXHWND
) hWnd
);
357 // Create the client window class (don't Create the window, just return a new
359 wxMDIClientWindow
*wxMDIParentFrame::OnCreateClient()
361 return new wxMDIClientWindow
;
364 // Responds to colour changes, and passes event on to children.
365 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
367 if ( m_clientWindow
)
369 m_clientWindow
->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
370 m_clientWindow
->Refresh();
376 WXHICON
wxMDIParentFrame::GetDefaultIcon() const
378 // we don't have any standard icons (any more)
382 // ---------------------------------------------------------------------------
384 // ---------------------------------------------------------------------------
386 void wxMDIParentFrame::Cascade()
388 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE
, 0, 0);
391 void wxMDIParentFrame::Tile(wxOrientation orient
)
393 wxASSERT_MSG( orient
== wxHORIZONTAL
|| orient
== wxVERTICAL
,
394 _T("invalid orientation value") );
396 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE
,
397 orient
== wxHORIZONTAL
? MDITILE_HORIZONTAL
398 : MDITILE_VERTICAL
, 0);
401 void wxMDIParentFrame::ArrangeIcons()
403 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE
, 0, 0);
406 void wxMDIParentFrame::ActivateNext()
408 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 0);
411 void wxMDIParentFrame::ActivatePrevious()
413 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 1);
416 // ---------------------------------------------------------------------------
417 // the MDI parent frame window proc
418 // ---------------------------------------------------------------------------
420 WXLRESULT
wxMDIParentFrame::MSWWindowProc(WXUINT message
,
425 bool processed
= false;
431 WXWORD state
, minimized
;
433 UnpackActivate(wParam
, lParam
, &state
, &minimized
, &hwnd
);
435 processed
= HandleActivate(state
, minimized
!= 0, hwnd
);
443 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
445 (void)HandleCommand(id
, cmd
, hwnd
);
447 // even if the frame didn't process it, there is no need to try it
448 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
449 // so pretend we processed the message anyhow
453 // always pass this message DefFrameProc(), otherwise MDI menu
454 // commands (and sys commands - more surprisingly!) won't work
455 MSWDefWindowProc(message
, wParam
, lParam
);
459 m_clientWindow
= OnCreateClient();
460 // Uses own style for client style
461 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
463 wxLogMessage(_("Failed to create MDI parent frame."));
474 // we erase background ourselves
482 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
484 if ( m_parentFrameActive
)
486 processed
= HandleMenuSelect(item
, flags
, hmenu
);
488 else if (m_currentChild
)
490 processed
= m_currentChild
->
491 HandleMenuSelect(item
, flags
, hmenu
);
497 // though we don't (usually) resize the MDI client to exactly fit the
498 // client area we need to pass this one to DefFrameProc to allow the children to show
503 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
508 bool wxMDIParentFrame::HandleActivate(int state
, bool minimized
, WXHWND activate
)
510 bool processed
= false;
512 if ( wxWindow::HandleActivate(state
, minimized
, activate
) )
518 // If this window is an MDI parent, we must also send an OnActivate message
519 // to the current child.
520 if ( (m_currentChild
!= NULL
) &&
521 ((state
== WA_ACTIVE
) || (state
== WA_CLICKACTIVE
)) )
523 wxActivateEvent
event(wxEVT_ACTIVATE
, true, m_currentChild
->GetId());
524 event
.SetEventObject( m_currentChild
);
525 if ( m_currentChild
->GetEventHandler()->ProcessEvent(event
) )
532 bool wxMDIParentFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND hwnd
)
534 // In case it's e.g. a toolbar.
537 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
539 return win
->MSWCommand(cmd
, id
);
542 if (wxCurrentPopupMenu
)
544 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
545 wxCurrentPopupMenu
= NULL
;
546 if (popupMenu
->MSWCommand(cmd
, id
))
550 // is it one of standard MDI commands?
556 case IDM_WINDOWCASCADE
:
558 wParam
= MDITILE_SKIPDISABLED
;
561 case IDM_WINDOWTILEHOR
:
562 wParam
|= MDITILE_HORIZONTAL
;
565 case IDM_WINDOWTILEVERT
:
567 wParam
= MDITILE_VERTICAL
;
569 wParam
|= MDITILE_SKIPDISABLED
;
572 case IDM_WINDOWICONS
:
573 msg
= WM_MDIICONARRANGE
;
578 lParam
= 0; // next child
583 lParam
= 1; // previous child
592 ::SendMessage(GetWinHwnd(GetClientWindow()), msg
, wParam
, lParam
);
597 // FIXME VZ: what does this test do??
600 return false; // Get WndProc to call default proc
603 if ( IsMdiCommandId(id
) )
605 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
608 wxWindow
*child
= node
->GetData();
609 if ( child
->GetHWND() )
611 long childId
= wxGetWindowId(child
->GetHWND());
612 if (childId
== (long)id
)
614 ::SendMessage( GetWinHwnd(GetClientWindow()),
616 (WPARAM
)child
->GetHWND(), 0);
620 node
= node
->GetNext();
623 else if ( m_parentFrameActive
)
625 return ProcessCommand(id
);
627 else if ( m_currentChild
)
629 return m_currentChild
->HandleCommand(id
, cmd
, hwnd
);
633 // this shouldn't happen because it means that our messages are being
634 // lost (they're not sent to the parent frame nor to the children)
635 wxFAIL_MSG(wxT("MDI parent frame is not active, yet there is no active MDI child?"));
641 WXLRESULT
wxMDIParentFrame::MSWDefWindowProc(WXUINT message
,
646 if ( GetClientWindow() )
647 clientWnd
= GetClientWindow()->GetHWND();
651 return DefFrameProc(GetHwnd(), (HWND
)clientWnd
, message
, wParam
, lParam
);
654 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG
* msg
)
656 MSG
*pMsg
= (MSG
*)msg
;
658 // first let the current child get it
659 if ( m_currentChild
&& m_currentChild
->GetHWND() &&
660 m_currentChild
->MSWTranslateMessage(msg
) )
665 // then try out accel table (will also check the menu accels)
666 if ( wxFrame::MSWTranslateMessage(msg
) )
671 // finally, check for MDI specific built in accel keys
672 if ( pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
674 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg
))
681 // ===========================================================================
683 // ===========================================================================
685 void wxMDIChildFrame::Init()
687 m_needsResize
= true;
688 m_needsInitialShow
= true;
691 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
693 const wxString
& title
,
697 const wxString
& name
)
701 if ( id
!= wxID_ANY
)
704 m_windowId
= (int)NewControlId();
708 parent
->AddChild(this);
718 mcs
.szClass
= style
& wxFULL_REPAINT_ON_RESIZE
719 ? wxMDIChildFrameClassName
720 : wxMDIChildFrameClassNameNoRedraw
;
722 mcs
.hOwner
= wxGetInstance();
723 if (x
!= wxDefaultCoord
)
726 mcs
.x
= CW_USEDEFAULT
;
728 if (y
!= wxDefaultCoord
)
731 mcs
.y
= CW_USEDEFAULT
;
733 if (width
!= wxDefaultCoord
)
736 mcs
.cx
= CW_USEDEFAULT
;
738 if (height
!= wxDefaultCoord
)
741 mcs
.cy
= CW_USEDEFAULT
;
743 DWORD msflags
= WS_OVERLAPPED
| WS_CLIPCHILDREN
;
744 if (style
& wxMINIMIZE_BOX
)
745 msflags
|= WS_MINIMIZEBOX
;
746 if (style
& wxMAXIMIZE_BOX
)
747 msflags
|= WS_MAXIMIZEBOX
;
748 if (style
& wxRESIZE_BORDER
)
749 msflags
|= WS_THICKFRAME
;
750 if (style
& wxSYSTEM_MENU
)
751 msflags
|= WS_SYSMENU
;
752 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
753 msflags
|= WS_MINIMIZE
;
754 if (style
& wxMAXIMIZE
)
755 msflags
|= WS_MAXIMIZE
;
756 if (style
& wxCAPTION
)
757 msflags
|= WS_CAPTION
;
763 wxWindowCreationHook
hook(this);
765 m_hWnd
= (WXHWND
)::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
766 WM_MDICREATE
, 0, (LONG
)(LPSTR
)&mcs
);
770 wxLogLastError(_T("WM_MDICREATE"));
779 wxMDIChildFrame::~wxMDIChildFrame()
781 // will be destroyed by DestroyChildren() but reset them before calling it
782 // to avoid using dangling pointers if a callback comes in the meanwhile
784 m_frameToolBar
= NULL
;
787 m_frameStatusBar
= NULL
;
788 #endif // wxUSE_STATUSBAR
792 RemoveWindowMenu(NULL
, m_hMenu
);
797 bool wxMDIChildFrame::Show(bool show
)
799 m_needsInitialShow
= false;
801 if (!wxFrame::Show(show
))
804 // KH: Without this call, new MDI children do not become active.
805 // This was added here after the same BringWindowToTop call was
806 // removed from wxTopLevelWindow::Show (November 2005)
808 ::BringWindowToTop(GetHwnd());
810 // we need to refresh the MDI frame window menu to include (or exclude if
811 // we've been hidden) this frame
812 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
813 MDISetMenu(parent
->GetClientWindow(), NULL
, NULL
);
818 // Set the client size (i.e. leave the calculation of borders etc.
820 void wxMDIChildFrame::DoSetClientSize(int width
, int height
)
822 HWND hWnd
= GetHwnd();
825 ::GetClientRect(hWnd
, &rect
);
828 GetWindowRect(hWnd
, &rect2
);
830 // Find the difference between the entire window (title bar and all)
831 // and the client area; add this to the new client size to move the
833 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
834 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
837 if (GetStatusBar() && GetStatusBar()->IsShown())
840 GetStatusBar()->GetSize(&sx
, &sy
);
843 #endif // wxUSE_STATUSBAR
846 point
.x
= rect2
.left
;
849 // If there's an MDI parent, must subtract the parent's top left corner
850 // since MoveWindow moves relative to the parent
851 wxMDIParentFrame
*mdiParent
= (wxMDIParentFrame
*)GetParent();
852 ::ScreenToClient((HWND
) mdiParent
->GetClientWindow()->GetHWND(), &point
);
854 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)true);
856 wxSize
size(width
, height
);
857 wxSizeEvent
event(size
, m_windowId
);
858 event
.SetEventObject( this );
859 GetEventHandler()->ProcessEvent(event
);
862 // Unlike other wxTopLevelWindowBase, the mdi child's "GetPosition" is not the
863 // same as its GetScreenPosition
864 void wxMDIChildFrame::DoGetScreenPosition(int *x
, int *y
) const
866 HWND hWnd
= GetHwnd();
869 ::GetWindowRect(hWnd
, &rect
);
877 void wxMDIChildFrame::DoGetPosition(int *x
, int *y
) const
880 GetWindowRect(GetHwnd(), &rect
);
885 // Since we now have the absolute screen coords,
886 // if there's a parent we must subtract its top left corner
887 wxMDIParentFrame
*mdiParent
= (wxMDIParentFrame
*)GetParent();
888 ::ScreenToClient((HWND
) mdiParent
->GetClientWindow()->GetHWND(), &point
);
896 void wxMDIChildFrame::InternalSetMenuBar()
898 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
900 InsertWindowMenu(parent
->GetClientWindow(),
901 m_hMenu
, GetMDIWindowMenu(parent
));
903 parent
->m_parentFrameActive
= false;
906 void wxMDIChildFrame::DetachMenuBar()
908 RemoveWindowMenu(NULL
, m_hMenu
);
909 wxFrame::DetachMenuBar();
912 WXHICON
wxMDIChildFrame::GetDefaultIcon() const
914 // we don't have any standard icons (any more)
918 // ---------------------------------------------------------------------------
920 // ---------------------------------------------------------------------------
922 void wxMDIChildFrame::Maximize(bool maximize
)
924 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
925 if ( parent
&& parent
->GetClientWindow() )
927 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
928 maximize
? WM_MDIMAXIMIZE
: WM_MDIRESTORE
,
929 (WPARAM
)GetHwnd(), 0);
933 void wxMDIChildFrame::Restore()
935 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
936 if ( parent
&& parent
->GetClientWindow() )
938 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIRESTORE
,
939 (WPARAM
) GetHwnd(), 0);
943 void wxMDIChildFrame::Activate()
945 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
946 if ( parent
&& parent
->GetClientWindow() )
948 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIACTIVATE
,
949 (WPARAM
) GetHwnd(), 0);
953 // ---------------------------------------------------------------------------
954 // MDI window proc and message handlers
955 // ---------------------------------------------------------------------------
957 WXLRESULT
wxMDIChildFrame::MSWWindowProc(WXUINT message
,
962 bool processed
= false;
970 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
973 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
977 case WM_GETMINMAXINFO
:
978 processed
= HandleGetMinMaxInfo((MINMAXINFO
*)lParam
);
984 WXHWND hwndAct
, hwndDeact
;
985 UnpackMDIActivate(wParam
, lParam
, &act
, &hwndAct
, &hwndDeact
);
987 processed
= HandleMDIActivate(act
, hwndAct
, hwndDeact
);
992 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
993 // scrollbars if necessary
998 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
1000 MSWDefWindowProc(message
, wParam
, lParam
);
1004 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
1005 // the message (the base class version does not)
1006 return MSWDefWindowProc(message
, wParam
, lParam
);
1008 case WM_WINDOWPOSCHANGING
:
1009 processed
= HandleWindowPosChanging((LPWINDOWPOS
)lParam
);
1014 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
1019 bool wxMDIChildFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND hwnd
)
1021 // In case it's e.g. a toolbar.
1024 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
1026 return win
->MSWCommand(cmd
, id
);
1029 if (wxCurrentPopupMenu
)
1031 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
1032 wxCurrentPopupMenu
= NULL
;
1033 if (popupMenu
->MSWCommand(cmd
, id
))
1038 if (GetMenuBar() && GetMenuBar()->FindItem(id
))
1040 processed
= ProcessCommand(id
);
1050 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate
),
1054 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
1056 HMENU menuToSet
= 0;
1060 if ( m_hWnd
== hwndAct
)
1063 parent
->m_currentChild
= this;
1065 HMENU child_menu
= (HMENU
)GetWinMenu();
1068 parent
->m_parentFrameActive
= false;
1070 menuToSet
= child_menu
;
1073 else if ( m_hWnd
== hwndDeact
)
1075 wxASSERT_MSG( parent
->m_currentChild
== this,
1076 wxT("can't deactivate MDI child which wasn't active!") );
1079 parent
->m_currentChild
= NULL
;
1081 HMENU parent_menu
= (HMENU
)parent
->GetWinMenu();
1083 // activate the the parent menu only when there is no other child
1084 // that has been activated
1085 if ( parent_menu
&& !hwndAct
)
1087 parent
->m_parentFrameActive
= true;
1089 menuToSet
= parent_menu
;
1094 // we have nothing to do with it
1100 MDISetMenu(parent
->GetClientWindow(),
1101 menuToSet
, GetMDIWindowMenu(parent
));
1104 wxActivateEvent
event(wxEVT_ACTIVATE
, activated
, m_windowId
);
1105 event
.SetEventObject( this );
1107 ResetWindowStyle((void *)NULL
);
1109 return GetEventHandler()->ProcessEvent(event
);
1112 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos
)
1114 WINDOWPOS
*lpPos
= (WINDOWPOS
*)pos
;
1116 if (!(lpPos
->flags
& SWP_NOSIZE
))
1119 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1120 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1121 if (ResetWindowStyle((void *) & rectClient
) && (dwStyle
& WS_MAXIMIZE
))
1123 ::AdjustWindowRectEx(&rectClient
, dwStyle
, false, dwExStyle
);
1124 lpPos
->x
= rectClient
.left
;
1125 lpPos
->y
= rectClient
.top
;
1126 lpPos
->cx
= rectClient
.right
- rectClient
.left
;
1127 lpPos
->cy
= rectClient
.bottom
- rectClient
.top
;
1134 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo
)
1136 MINMAXINFO
*info
= (MINMAXINFO
*)mmInfo
;
1138 // let the default window proc calculate the size of MDI children
1139 // frames because it is based on the size of the MDI client window,
1140 // not on the values specified in wxWindow m_max variables
1141 bool processed
= MSWDefWindowProc(WM_GETMINMAXINFO
, 0, (LPARAM
)mmInfo
) != 0;
1143 int minWidth
= GetMinWidth(),
1144 minHeight
= GetMinHeight();
1146 // but allow GetSizeHints() to set the min size
1147 if ( minWidth
!= wxDefaultCoord
)
1149 info
->ptMinTrackSize
.x
= minWidth
;
1154 if ( minHeight
!= wxDefaultCoord
)
1156 info
->ptMinTrackSize
.y
= minHeight
;
1164 // ---------------------------------------------------------------------------
1165 // MDI specific message translation/preprocessing
1166 // ---------------------------------------------------------------------------
1168 WXLRESULT
wxMDIChildFrame::MSWDefWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1170 return DefMDIChildProc(GetHwnd(),
1171 (UINT
)message
, (WPARAM
)wParam
, (LPARAM
)lParam
);
1174 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG
* msg
)
1176 // we must pass the parent frame to ::TranslateAccelerator(), otherwise it
1177 // doesn't do its job correctly for MDI child menus
1178 return MSWDoTranslateMessage((wxMDIChildFrame
*)GetParent(), msg
);
1181 // ---------------------------------------------------------------------------
1183 // ---------------------------------------------------------------------------
1185 void wxMDIChildFrame::MSWDestroyWindow()
1187 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
1189 // Must make sure this handle is invalidated (set to NULL) since all sorts
1190 // of things could happen after the child client is destroyed, but before
1191 // the wxFrame is destroyed.
1193 HWND oldHandle
= (HWND
)GetHWND();
1194 SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIDESTROY
,
1195 (WPARAM
)oldHandle
, 0);
1197 if (parent
->GetActiveChild() == (wxMDIChildFrame
*) NULL
)
1198 ResetWindowStyle((void*) NULL
);
1202 ::DestroyMenu((HMENU
) m_hMenu
);
1205 wxRemoveHandleAssociation(this);
1209 // Change the client window's extended style so we don't get a client edge
1210 // style when a child is maximised (a double border looks silly.)
1211 bool wxMDIChildFrame::ResetWindowStyle(void *vrect
)
1213 RECT
*rect
= (RECT
*)vrect
;
1214 wxMDIParentFrame
* pFrameWnd
= (wxMDIParentFrame
*)GetParent();
1215 wxMDIChildFrame
* pChild
= pFrameWnd
->GetActiveChild();
1217 if (!pChild
|| (pChild
== this))
1219 HWND hwndClient
= GetWinHwnd(pFrameWnd
->GetClientWindow());
1220 DWORD dwStyle
= ::GetWindowLong(hwndClient
, GWL_EXSTYLE
);
1222 // we want to test whether there is a maximized child, so just set
1223 // dwThisStyle to 0 if there is no child at all
1224 DWORD dwThisStyle
= pChild
1225 ? ::GetWindowLong(GetWinHwnd(pChild
), GWL_STYLE
) : 0;
1226 DWORD dwNewStyle
= dwStyle
;
1227 if ( dwThisStyle
& WS_MAXIMIZE
)
1228 dwNewStyle
&= ~(WS_EX_CLIENTEDGE
);
1230 dwNewStyle
|= WS_EX_CLIENTEDGE
;
1232 if (dwStyle
!= dwNewStyle
)
1234 // force update of everything
1235 ::RedrawWindow(hwndClient
, NULL
, NULL
,
1236 RDW_INVALIDATE
| RDW_ALLCHILDREN
);
1237 ::SetWindowLong(hwndClient
, GWL_EXSTYLE
, dwNewStyle
);
1238 ::SetWindowPos(hwndClient
, NULL
, 0, 0, 0, 0,
1239 SWP_FRAMECHANGED
| SWP_NOACTIVATE
|
1240 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1243 ::GetClientRect(hwndClient
, rect
);
1252 // ===========================================================================
1253 // wxMDIClientWindow: the window of predefined (by Windows) class which
1254 // contains the child frames
1255 // ===========================================================================
1257 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
1259 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1261 CLIENTCREATESTRUCT ccs
;
1262 m_windowStyle
= style
;
1265 ccs
.hWindowMenu
= GetMDIWindowMenu(parent
);
1266 ccs
.idFirstChild
= wxFIRST_MDI_CHILD
;
1268 DWORD msStyle
= MDIS_ALLCHILDSTYLES
| WS_VISIBLE
| WS_CHILD
|
1269 WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
;
1271 if ( style
& wxHSCROLL
)
1272 msStyle
|= WS_HSCROLL
;
1273 if ( style
& wxVSCROLL
)
1274 msStyle
|= WS_VSCROLL
;
1276 DWORD exStyle
= WS_EX_CLIENTEDGE
;
1278 wxWindowCreationHook
hook(this);
1279 m_hWnd
= (WXHWND
)::CreateWindowEx
1289 (LPSTR
)(LPCLIENTCREATESTRUCT
)&ccs
);
1292 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1297 SubclassWin(m_hWnd
);
1302 // Explicitly call default scroll behaviour
1303 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)
1305 // Note: for client windows, the scroll position is not set in
1306 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1307 // scroll position we're at.
1308 // This makes it hard to paint patterns or bitmaps in the background,
1309 // and have the client area scrollable as well.
1311 if ( event
.GetOrientation() == wxHORIZONTAL
)
1312 m_scrollX
= event
.GetPosition(); // Always returns zero!
1314 m_scrollY
= event
.GetPosition(); // Always returns zero!
1319 void wxMDIClientWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1321 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1322 // client area, you can end up with a non-refreshed portion in the client window
1323 // (see OGL studio sample). So check if the position is changed and if so,
1324 // redraw the MDI child frames.
1326 const wxPoint oldPos
= GetPosition();
1328 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
| wxSIZE_FORCE
);
1330 const wxPoint newPos
= GetPosition();
1332 if ((newPos
.x
!= oldPos
.x
) || (newPos
.y
!= oldPos
.y
))
1336 wxWindowList::compatibility_iterator node
= GetParent()->GetChildren().GetFirst();
1339 wxWindow
*child
= node
->GetData();
1340 if (child
->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
1342 ::RedrawWindow(GetHwndOf(child
),
1349 node
= node
->GetNext();
1355 void wxMDIChildFrame::OnIdle(wxIdleEvent
& event
)
1357 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1358 // in flicker e.g. when the frame contained controls with non-trivial
1359 // layout. Since 2.5.3, the frame is created hidden as all other top level
1360 // windows. In order to maintain backward compatibility, the frame is shown
1361 // in OnIdle, unless Show(false) was called by the programmer before.
1362 if ( m_needsInitialShow
)
1367 // MDI child frames get their WM_SIZE when they're constructed but at this
1368 // moment they don't have any children yet so all child windows will be
1369 // positioned incorrectly when they are added later - to fix this, we
1370 // generate an artificial size event here
1371 if ( m_needsResize
)
1373 m_needsResize
= false; // avoid any possibility of recursion
1381 // ---------------------------------------------------------------------------
1382 // non member functions
1383 // ---------------------------------------------------------------------------
1385 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
)
1387 if ( hmenuFrame
|| hmenuWindow
)
1389 if ( !::SendMessage(GetWinHwnd(win
),
1392 (LPARAM
)hmenuWindow
) )
1394 wxLogLastError(_T("SendMessage(WM_MDISETMENU)"));
1398 // update menu bar of the parent window
1399 wxWindow
*parent
= win
->GetParent();
1400 wxCHECK_RET( parent
, wxT("MDI client without parent frame? weird...") );
1402 ::SendMessage(GetWinHwnd(win
), WM_MDIREFRESHMENU
, 0, 0L);
1404 ::DrawMenuBar(GetWinHwnd(parent
));
1407 static void InsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
)
1409 // Try to insert Window menu in front of Help, otherwise append it.
1410 HMENU hmenu
= (HMENU
)menu
;
1414 int N
= GetMenuItemCount(hmenu
);
1415 bool success
= false;
1416 for ( int i
= 0; i
< N
; i
++ )
1419 int chars
= GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
);
1422 wxLogLastError(wxT("GetMenuString"));
1427 wxString
strBuf(buf
);
1428 if ( wxStripMenuCodes(strBuf
) == wxGetStockLabel(wxID_HELP
,false) )
1431 ::InsertMenu(hmenu
, i
, MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1432 (UINT
)subMenu
, _("&Window"));
1439 ::AppendMenu(hmenu
, MF_POPUP
, (UINT
)subMenu
, _("&Window"));
1443 MDISetMenu(win
, hmenu
, subMenu
);
1446 static void RemoveWindowMenu(wxWindow
*win
, WXHMENU menu
)
1448 HMENU hMenu
= (HMENU
)menu
;
1454 int N
= ::GetMenuItemCount(hMenu
);
1455 for ( int i
= 0; i
< N
; i
++ )
1457 if ( !::GetMenuString(hMenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1459 // Ignore successful read of menu string with length 0 which
1460 // occurs, for example, for a maximized MDI childs system menu
1461 if ( ::GetLastError() != 0 )
1463 wxLogLastError(wxT("GetMenuString"));
1469 if ( wxStrcmp(buf
, _("&Window")) == 0 )
1471 if ( !::RemoveMenu(hMenu
, i
, MF_BYPOSITION
) )
1473 wxLogLastError(wxT("RemoveMenu"));
1483 // we don't change the windows menu, but we update the main one
1484 MDISetMenu(win
, hMenu
, NULL
);
1488 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
1489 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
)
1492 *hwndAct
= (WXHWND
)lParam
;
1493 *hwndDeact
= (WXHWND
)wParam
;
1496 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)