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 const wxMenuItem
*wxMDIParentFrame::FindItemInMenuBar(int menuId
) const
320 const wxMenuItem
*item
= wxFrame::FindItemInMenuBar(menuId
);
321 if ( !item
&& m_currentChild
)
323 item
= m_currentChild
->FindItemInMenuBar(menuId
);
329 void wxMDIParentFrame::UpdateClientSize()
331 if ( GetClientWindow() )
334 GetClientSize(&width
, &height
);
336 GetClientWindow()->SetSize(0, 0, width
, height
);
340 void wxMDIParentFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
344 // do not call event.Skip() here, it somehow messes up MDI client window
347 void wxMDIParentFrame::OnIconized(wxIconizeEvent
& event
)
351 if ( !event
.Iconized() )
357 // Returns the active MDI child window
358 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
360 HWND hWnd
= (HWND
)::SendMessage(GetWinHwnd(GetClientWindow()),
361 WM_MDIGETACTIVE
, 0, 0L);
365 return (wxMDIChildFrame
*)wxFindWinFromHandle((WXHWND
) hWnd
);
368 // Create the client window class (don't Create the window, just return a new
370 wxMDIClientWindow
*wxMDIParentFrame::OnCreateClient()
372 return new wxMDIClientWindow
;
375 // Responds to colour changes, and passes event on to children.
376 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
378 if ( m_clientWindow
)
380 m_clientWindow
->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
381 m_clientWindow
->Refresh();
387 WXHICON
wxMDIParentFrame::GetDefaultIcon() const
389 // we don't have any standard icons (any more)
393 // ---------------------------------------------------------------------------
395 // ---------------------------------------------------------------------------
397 void wxMDIParentFrame::Cascade()
399 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE
, 0, 0);
402 void wxMDIParentFrame::Tile(wxOrientation orient
)
404 wxASSERT_MSG( orient
== wxHORIZONTAL
|| orient
== wxVERTICAL
,
405 _T("invalid orientation value") );
407 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE
,
408 orient
== wxHORIZONTAL
? MDITILE_HORIZONTAL
409 : MDITILE_VERTICAL
, 0);
412 void wxMDIParentFrame::ArrangeIcons()
414 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE
, 0, 0);
417 void wxMDIParentFrame::ActivateNext()
419 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 0);
422 void wxMDIParentFrame::ActivatePrevious()
424 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 1);
427 // ---------------------------------------------------------------------------
428 // the MDI parent frame window proc
429 // ---------------------------------------------------------------------------
431 WXLRESULT
wxMDIParentFrame::MSWWindowProc(WXUINT message
,
436 bool processed
= false;
442 WXWORD state
, minimized
;
444 UnpackActivate(wParam
, lParam
, &state
, &minimized
, &hwnd
);
446 processed
= HandleActivate(state
, minimized
!= 0, hwnd
);
454 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
456 (void)HandleCommand(id
, cmd
, hwnd
);
458 // even if the frame didn't process it, there is no need to try it
459 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
460 // so pretend we processed the message anyhow
464 // always pass this message DefFrameProc(), otherwise MDI menu
465 // commands (and sys commands - more surprisingly!) won't work
466 MSWDefWindowProc(message
, wParam
, lParam
);
470 m_clientWindow
= OnCreateClient();
471 // Uses own style for client style
472 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
474 wxLogMessage(_("Failed to create MDI parent frame."));
485 // we erase background ourselves
490 // though we don't (usually) resize the MDI client to exactly fit the
491 // client area we need to pass this one to DefFrameProc to allow the children to show
496 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
501 bool wxMDIParentFrame::HandleActivate(int state
, bool minimized
, WXHWND activate
)
503 bool processed
= false;
505 if ( wxWindow::HandleActivate(state
, minimized
, activate
) )
511 // If this window is an MDI parent, we must also send an OnActivate message
512 // to the current child.
513 if ( (m_currentChild
!= NULL
) &&
514 ((state
== WA_ACTIVE
) || (state
== WA_CLICKACTIVE
)) )
516 wxActivateEvent
event(wxEVT_ACTIVATE
, true, m_currentChild
->GetId());
517 event
.SetEventObject( m_currentChild
);
518 if ( m_currentChild
->GetEventHandler()->ProcessEvent(event
) )
525 bool wxMDIParentFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND hwnd
)
527 // In case it's e.g. a toolbar.
530 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
532 return win
->MSWCommand(cmd
, id
);
535 if (wxCurrentPopupMenu
)
537 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
538 wxCurrentPopupMenu
= NULL
;
539 if (popupMenu
->MSWCommand(cmd
, id
))
543 // is it one of standard MDI commands?
549 case IDM_WINDOWCASCADE
:
551 wParam
= MDITILE_SKIPDISABLED
;
554 case IDM_WINDOWTILEHOR
:
555 wParam
|= MDITILE_HORIZONTAL
;
558 case IDM_WINDOWTILEVERT
:
560 wParam
= MDITILE_VERTICAL
;
562 wParam
|= MDITILE_SKIPDISABLED
;
565 case IDM_WINDOWICONS
:
566 msg
= WM_MDIICONARRANGE
;
571 lParam
= 0; // next child
576 lParam
= 1; // previous child
585 ::SendMessage(GetWinHwnd(GetClientWindow()), msg
, wParam
, lParam
);
590 // FIXME VZ: what does this test do??
593 return false; // Get WndProc to call default proc
596 if ( IsMdiCommandId(id
) )
598 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
601 wxWindow
*child
= node
->GetData();
602 if ( child
->GetHWND() )
604 long childId
= wxGetWindowId(child
->GetHWND());
605 if (childId
== (long)id
)
607 ::SendMessage( GetWinHwnd(GetClientWindow()),
609 (WPARAM
)child
->GetHWND(), 0);
613 node
= node
->GetNext();
616 else if ( m_parentFrameActive
)
618 return ProcessCommand(id
);
620 else if ( m_currentChild
)
622 return m_currentChild
->HandleCommand(id
, cmd
, hwnd
);
626 // this shouldn't happen because it means that our messages are being
627 // lost (they're not sent to the parent frame nor to the children)
628 wxFAIL_MSG(wxT("MDI parent frame is not active, yet there is no active MDI child?"));
634 WXLRESULT
wxMDIParentFrame::MSWDefWindowProc(WXUINT message
,
639 if ( GetClientWindow() )
640 clientWnd
= GetClientWindow()->GetHWND();
644 return DefFrameProc(GetHwnd(), (HWND
)clientWnd
, message
, wParam
, lParam
);
647 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG
* msg
)
649 MSG
*pMsg
= (MSG
*)msg
;
651 // first let the current child get it
652 if ( m_currentChild
&& m_currentChild
->GetHWND() &&
653 m_currentChild
->MSWTranslateMessage(msg
) )
658 // then try out accel table (will also check the menu accels)
659 if ( wxFrame::MSWTranslateMessage(msg
) )
664 // finally, check for MDI specific built in accel keys
665 if ( pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
667 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg
))
674 // ===========================================================================
676 // ===========================================================================
678 void wxMDIChildFrame::Init()
680 m_needsResize
= true;
681 m_needsInitialShow
= true;
684 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
686 const wxString
& title
,
690 const wxString
& name
)
694 if ( id
!= wxID_ANY
)
697 m_windowId
= (int)NewControlId();
701 parent
->AddChild(this);
711 mcs
.szClass
= style
& wxFULL_REPAINT_ON_RESIZE
712 ? wxMDIChildFrameClassName
713 : wxMDIChildFrameClassNameNoRedraw
;
715 mcs
.hOwner
= wxGetInstance();
716 if (x
!= wxDefaultCoord
)
719 mcs
.x
= CW_USEDEFAULT
;
721 if (y
!= wxDefaultCoord
)
724 mcs
.y
= CW_USEDEFAULT
;
726 if (width
!= wxDefaultCoord
)
729 mcs
.cx
= CW_USEDEFAULT
;
731 if (height
!= wxDefaultCoord
)
734 mcs
.cy
= CW_USEDEFAULT
;
736 DWORD msflags
= WS_OVERLAPPED
| WS_CLIPCHILDREN
;
737 if (style
& wxMINIMIZE_BOX
)
738 msflags
|= WS_MINIMIZEBOX
;
739 if (style
& wxMAXIMIZE_BOX
)
740 msflags
|= WS_MAXIMIZEBOX
;
741 if (style
& wxRESIZE_BORDER
)
742 msflags
|= WS_THICKFRAME
;
743 if (style
& wxSYSTEM_MENU
)
744 msflags
|= WS_SYSMENU
;
745 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
746 msflags
|= WS_MINIMIZE
;
747 if (style
& wxMAXIMIZE
)
748 msflags
|= WS_MAXIMIZE
;
749 if (style
& wxCAPTION
)
750 msflags
|= WS_CAPTION
;
756 wxWindowCreationHook
hook(this);
758 m_hWnd
= (WXHWND
)::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
759 WM_MDICREATE
, 0, (LONG
)(LPSTR
)&mcs
);
763 wxLogLastError(_T("WM_MDICREATE"));
772 wxMDIChildFrame::~wxMDIChildFrame()
774 // if we hadn't been created, there is nothing to destroy
778 // will be destroyed by DestroyChildren() but reset them before calling it
779 // to avoid using dangling pointers if a callback comes in the meanwhile
781 m_frameToolBar
= NULL
;
784 m_frameStatusBar
= NULL
;
785 #endif // wxUSE_STATUSBAR
789 RemoveWindowMenu(NULL
, m_hMenu
);
794 bool wxMDIChildFrame::Show(bool show
)
796 m_needsInitialShow
= false;
798 if (!wxFrame::Show(show
))
801 // KH: Without this call, new MDI children do not become active.
802 // This was added here after the same BringWindowToTop call was
803 // removed from wxTopLevelWindow::Show (November 2005)
805 ::BringWindowToTop(GetHwnd());
807 // we need to refresh the MDI frame window menu to include (or exclude if
808 // we've been hidden) this frame
809 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
810 MDISetMenu(parent
->GetClientWindow(), NULL
, NULL
);
815 // Set the client size (i.e. leave the calculation of borders etc.
817 void wxMDIChildFrame::DoSetClientSize(int width
, int height
)
819 HWND hWnd
= GetHwnd();
822 ::GetClientRect(hWnd
, &rect
);
825 GetWindowRect(hWnd
, &rect2
);
827 // Find the difference between the entire window (title bar and all)
828 // and the client area; add this to the new client size to move the
830 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
831 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
834 if (GetStatusBar() && GetStatusBar()->IsShown())
837 GetStatusBar()->GetSize(&sx
, &sy
);
840 #endif // wxUSE_STATUSBAR
843 point
.x
= rect2
.left
;
846 // If there's an MDI parent, must subtract the parent's top left corner
847 // since MoveWindow moves relative to the parent
848 wxMDIParentFrame
*mdiParent
= (wxMDIParentFrame
*)GetParent();
849 ::ScreenToClient((HWND
) mdiParent
->GetClientWindow()->GetHWND(), &point
);
851 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)true);
853 wxSize
size(width
, height
);
854 wxSizeEvent
event(size
, m_windowId
);
855 event
.SetEventObject( this );
856 GetEventHandler()->ProcessEvent(event
);
859 // Unlike other wxTopLevelWindowBase, the mdi child's "GetPosition" is not the
860 // same as its GetScreenPosition
861 void wxMDIChildFrame::DoGetScreenPosition(int *x
, int *y
) const
863 HWND hWnd
= GetHwnd();
866 ::GetWindowRect(hWnd
, &rect
);
874 void wxMDIChildFrame::DoGetPosition(int *x
, int *y
) const
877 GetWindowRect(GetHwnd(), &rect
);
882 // Since we now have the absolute screen coords,
883 // if there's a parent we must subtract its top left corner
884 wxMDIParentFrame
*mdiParent
= (wxMDIParentFrame
*)GetParent();
885 ::ScreenToClient((HWND
) mdiParent
->GetClientWindow()->GetHWND(), &point
);
893 void wxMDIChildFrame::InternalSetMenuBar()
895 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
897 InsertWindowMenu(parent
->GetClientWindow(),
898 m_hMenu
, GetMDIWindowMenu(parent
));
900 parent
->m_parentFrameActive
= false;
903 void wxMDIChildFrame::DetachMenuBar()
905 RemoveWindowMenu(NULL
, m_hMenu
);
906 wxFrame::DetachMenuBar();
909 WXHICON
wxMDIChildFrame::GetDefaultIcon() const
911 // we don't have any standard icons (any more)
915 // ---------------------------------------------------------------------------
917 // ---------------------------------------------------------------------------
919 void wxMDIChildFrame::Maximize(bool maximize
)
921 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
922 if ( parent
&& parent
->GetClientWindow() )
924 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
925 maximize
? WM_MDIMAXIMIZE
: WM_MDIRESTORE
,
926 (WPARAM
)GetHwnd(), 0);
930 void wxMDIChildFrame::Restore()
932 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
933 if ( parent
&& parent
->GetClientWindow() )
935 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIRESTORE
,
936 (WPARAM
) GetHwnd(), 0);
940 void wxMDIChildFrame::Activate()
942 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
943 if ( parent
&& parent
->GetClientWindow() )
945 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIACTIVATE
,
946 (WPARAM
) GetHwnd(), 0);
950 // ---------------------------------------------------------------------------
951 // MDI window proc and message handlers
952 // ---------------------------------------------------------------------------
954 WXLRESULT
wxMDIChildFrame::MSWWindowProc(WXUINT message
,
959 bool processed
= false;
967 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
970 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
974 case WM_GETMINMAXINFO
:
975 processed
= HandleGetMinMaxInfo((MINMAXINFO
*)lParam
);
981 WXHWND hwndAct
, hwndDeact
;
982 UnpackMDIActivate(wParam
, lParam
, &act
, &hwndAct
, &hwndDeact
);
984 processed
= HandleMDIActivate(act
, hwndAct
, hwndDeact
);
989 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
990 // scrollbars if necessary
995 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
997 MSWDefWindowProc(message
, wParam
, lParam
);
1001 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
1002 // the message (the base class version does not)
1003 return MSWDefWindowProc(message
, wParam
, lParam
);
1005 case WM_WINDOWPOSCHANGING
:
1006 processed
= HandleWindowPosChanging((LPWINDOWPOS
)lParam
);
1011 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
1016 bool wxMDIChildFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND hwnd
)
1018 // In case it's e.g. a toolbar.
1021 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
1023 return win
->MSWCommand(cmd
, id
);
1026 if (wxCurrentPopupMenu
)
1028 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
1029 wxCurrentPopupMenu
= NULL
;
1030 if (popupMenu
->MSWCommand(cmd
, id
))
1035 if (GetMenuBar() && GetMenuBar()->FindItem(id
))
1037 processed
= ProcessCommand(id
);
1047 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate
),
1051 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
1053 HMENU menuToSet
= 0;
1057 if ( m_hWnd
== hwndAct
)
1060 parent
->m_currentChild
= this;
1062 HMENU child_menu
= (HMENU
)GetWinMenu();
1065 parent
->m_parentFrameActive
= false;
1067 menuToSet
= child_menu
;
1070 else if ( m_hWnd
== hwndDeact
)
1072 wxASSERT_MSG( parent
->m_currentChild
== this,
1073 wxT("can't deactivate MDI child which wasn't active!") );
1076 parent
->m_currentChild
= NULL
;
1078 HMENU parent_menu
= (HMENU
)parent
->GetWinMenu();
1080 // activate the the parent menu only when there is no other child
1081 // that has been activated
1082 if ( parent_menu
&& !hwndAct
)
1084 parent
->m_parentFrameActive
= true;
1086 menuToSet
= parent_menu
;
1091 // we have nothing to do with it
1097 MDISetMenu(parent
->GetClientWindow(),
1098 menuToSet
, GetMDIWindowMenu(parent
));
1101 wxActivateEvent
event(wxEVT_ACTIVATE
, activated
, m_windowId
);
1102 event
.SetEventObject( this );
1104 ResetWindowStyle((void *)NULL
);
1106 return GetEventHandler()->ProcessEvent(event
);
1109 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos
)
1111 WINDOWPOS
*lpPos
= (WINDOWPOS
*)pos
;
1113 if (!(lpPos
->flags
& SWP_NOSIZE
))
1116 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1117 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1118 if (ResetWindowStyle((void *) & rectClient
) && (dwStyle
& WS_MAXIMIZE
))
1120 ::AdjustWindowRectEx(&rectClient
, dwStyle
, false, dwExStyle
);
1121 lpPos
->x
= rectClient
.left
;
1122 lpPos
->y
= rectClient
.top
;
1123 lpPos
->cx
= rectClient
.right
- rectClient
.left
;
1124 lpPos
->cy
= rectClient
.bottom
- rectClient
.top
;
1131 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo
)
1133 MINMAXINFO
*info
= (MINMAXINFO
*)mmInfo
;
1135 // let the default window proc calculate the size of MDI children
1136 // frames because it is based on the size of the MDI client window,
1137 // not on the values specified in wxWindow m_max variables
1138 bool processed
= MSWDefWindowProc(WM_GETMINMAXINFO
, 0, (LPARAM
)mmInfo
) != 0;
1140 int minWidth
= GetMinWidth(),
1141 minHeight
= GetMinHeight();
1143 // but allow GetSizeHints() to set the min size
1144 if ( minWidth
!= wxDefaultCoord
)
1146 info
->ptMinTrackSize
.x
= minWidth
;
1151 if ( minHeight
!= wxDefaultCoord
)
1153 info
->ptMinTrackSize
.y
= minHeight
;
1161 // ---------------------------------------------------------------------------
1162 // MDI specific message translation/preprocessing
1163 // ---------------------------------------------------------------------------
1165 WXLRESULT
wxMDIChildFrame::MSWDefWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1167 return DefMDIChildProc(GetHwnd(),
1168 (UINT
)message
, (WPARAM
)wParam
, (LPARAM
)lParam
);
1171 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG
* msg
)
1173 // we must pass the parent frame to ::TranslateAccelerator(), otherwise it
1174 // doesn't do its job correctly for MDI child menus
1175 return MSWDoTranslateMessage((wxMDIChildFrame
*)GetParent(), msg
);
1178 // ---------------------------------------------------------------------------
1180 // ---------------------------------------------------------------------------
1182 void wxMDIChildFrame::MSWDestroyWindow()
1184 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
1186 // Must make sure this handle is invalidated (set to NULL) since all sorts
1187 // of things could happen after the child client is destroyed, but before
1188 // the wxFrame is destroyed.
1190 HWND oldHandle
= (HWND
)GetHWND();
1191 SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIDESTROY
,
1192 (WPARAM
)oldHandle
, 0);
1194 if (parent
->GetActiveChild() == (wxMDIChildFrame
*) NULL
)
1195 ResetWindowStyle((void*) NULL
);
1199 ::DestroyMenu((HMENU
) m_hMenu
);
1202 wxRemoveHandleAssociation(this);
1206 // Change the client window's extended style so we don't get a client edge
1207 // style when a child is maximised (a double border looks silly.)
1208 bool wxMDIChildFrame::ResetWindowStyle(void *vrect
)
1210 RECT
*rect
= (RECT
*)vrect
;
1211 wxMDIParentFrame
* pFrameWnd
= (wxMDIParentFrame
*)GetParent();
1212 wxMDIChildFrame
* pChild
= pFrameWnd
->GetActiveChild();
1214 if (!pChild
|| (pChild
== this))
1216 HWND hwndClient
= GetWinHwnd(pFrameWnd
->GetClientWindow());
1217 DWORD dwStyle
= ::GetWindowLong(hwndClient
, GWL_EXSTYLE
);
1219 // we want to test whether there is a maximized child, so just set
1220 // dwThisStyle to 0 if there is no child at all
1221 DWORD dwThisStyle
= pChild
1222 ? ::GetWindowLong(GetWinHwnd(pChild
), GWL_STYLE
) : 0;
1223 DWORD dwNewStyle
= dwStyle
;
1224 if ( dwThisStyle
& WS_MAXIMIZE
)
1225 dwNewStyle
&= ~(WS_EX_CLIENTEDGE
);
1227 dwNewStyle
|= WS_EX_CLIENTEDGE
;
1229 if (dwStyle
!= dwNewStyle
)
1231 // force update of everything
1232 ::RedrawWindow(hwndClient
, NULL
, NULL
,
1233 RDW_INVALIDATE
| RDW_ALLCHILDREN
);
1234 ::SetWindowLong(hwndClient
, GWL_EXSTYLE
, dwNewStyle
);
1235 ::SetWindowPos(hwndClient
, NULL
, 0, 0, 0, 0,
1236 SWP_FRAMECHANGED
| SWP_NOACTIVATE
|
1237 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1240 ::GetClientRect(hwndClient
, rect
);
1249 // ===========================================================================
1250 // wxMDIClientWindow: the window of predefined (by Windows) class which
1251 // contains the child frames
1252 // ===========================================================================
1254 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
1256 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1258 CLIENTCREATESTRUCT ccs
;
1259 m_windowStyle
= style
;
1262 ccs
.hWindowMenu
= GetMDIWindowMenu(parent
);
1263 ccs
.idFirstChild
= wxFIRST_MDI_CHILD
;
1265 DWORD msStyle
= MDIS_ALLCHILDSTYLES
| WS_VISIBLE
| WS_CHILD
|
1266 WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
;
1268 if ( style
& wxHSCROLL
)
1269 msStyle
|= WS_HSCROLL
;
1270 if ( style
& wxVSCROLL
)
1271 msStyle
|= WS_VSCROLL
;
1273 DWORD exStyle
= WS_EX_CLIENTEDGE
;
1275 wxWindowCreationHook
hook(this);
1276 m_hWnd
= (WXHWND
)::CreateWindowEx
1286 (LPSTR
)(LPCLIENTCREATESTRUCT
)&ccs
);
1289 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1294 SubclassWin(m_hWnd
);
1299 // Explicitly call default scroll behaviour
1300 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)
1302 // Note: for client windows, the scroll position is not set in
1303 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1304 // scroll position we're at.
1305 // This makes it hard to paint patterns or bitmaps in the background,
1306 // and have the client area scrollable as well.
1308 if ( event
.GetOrientation() == wxHORIZONTAL
)
1309 m_scrollX
= event
.GetPosition(); // Always returns zero!
1311 m_scrollY
= event
.GetPosition(); // Always returns zero!
1316 void wxMDIClientWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1318 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1319 // client area, you can end up with a non-refreshed portion in the client window
1320 // (see OGL studio sample). So check if the position is changed and if so,
1321 // redraw the MDI child frames.
1323 const wxPoint oldPos
= GetPosition();
1325 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
| wxSIZE_FORCE
);
1327 const wxPoint newPos
= GetPosition();
1329 if ((newPos
.x
!= oldPos
.x
) || (newPos
.y
!= oldPos
.y
))
1333 wxWindowList::compatibility_iterator node
= GetParent()->GetChildren().GetFirst();
1336 wxWindow
*child
= node
->GetData();
1337 if (child
->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
1339 ::RedrawWindow(GetHwndOf(child
),
1346 node
= node
->GetNext();
1352 void wxMDIChildFrame::OnIdle(wxIdleEvent
& event
)
1354 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1355 // in flicker e.g. when the frame contained controls with non-trivial
1356 // layout. Since 2.5.3, the frame is created hidden as all other top level
1357 // windows. In order to maintain backward compatibility, the frame is shown
1358 // in OnIdle, unless Show(false) was called by the programmer before.
1359 if ( m_needsInitialShow
)
1364 // MDI child frames get their WM_SIZE when they're constructed but at this
1365 // moment they don't have any children yet so all child windows will be
1366 // positioned incorrectly when they are added later - to fix this, we
1367 // generate an artificial size event here
1368 if ( m_needsResize
)
1370 m_needsResize
= false; // avoid any possibility of recursion
1378 // ---------------------------------------------------------------------------
1379 // non member functions
1380 // ---------------------------------------------------------------------------
1382 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
)
1384 if ( hmenuFrame
|| hmenuWindow
)
1386 if ( !::SendMessage(GetWinHwnd(win
),
1389 (LPARAM
)hmenuWindow
) )
1392 DWORD err
= ::GetLastError();
1394 wxLogApiError(_T("SendMessage(WM_MDISETMENU)"), err
);
1395 #endif // __WXDEBUG__
1399 // update menu bar of the parent window
1400 wxWindow
*parent
= win
->GetParent();
1401 wxCHECK_RET( parent
, wxT("MDI client without parent frame? weird...") );
1403 ::SendMessage(GetWinHwnd(win
), WM_MDIREFRESHMENU
, 0, 0L);
1405 ::DrawMenuBar(GetWinHwnd(parent
));
1408 static void InsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
)
1410 // Try to insert Window menu in front of Help, otherwise append it.
1411 HMENU hmenu
= (HMENU
)menu
;
1415 int N
= GetMenuItemCount(hmenu
);
1416 bool success
= false;
1417 for ( int i
= 0; i
< N
; i
++ )
1420 int chars
= GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
);
1423 wxLogLastError(wxT("GetMenuString"));
1428 wxString
strBuf(buf
);
1429 if ( wxStripMenuCodes(strBuf
) == wxGetStockLabel(wxID_HELP
,false) )
1432 ::InsertMenu(hmenu
, i
, MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1433 (UINT
)subMenu
, _("&Window"));
1440 ::AppendMenu(hmenu
, MF_POPUP
, (UINT
)subMenu
, _("&Window"));
1444 MDISetMenu(win
, hmenu
, subMenu
);
1447 static void RemoveWindowMenu(wxWindow
*win
, WXHMENU menu
)
1449 HMENU hMenu
= (HMENU
)menu
;
1455 int N
= ::GetMenuItemCount(hMenu
);
1456 for ( int i
= 0; i
< N
; i
++ )
1458 if ( !::GetMenuString(hMenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1460 // Ignore successful read of menu string with length 0 which
1461 // occurs, for example, for a maximized MDI childs system menu
1462 if ( ::GetLastError() != 0 )
1464 wxLogLastError(wxT("GetMenuString"));
1470 if ( wxStrcmp(buf
, _("&Window")) == 0 )
1472 if ( !::RemoveMenu(hMenu
, i
, MF_BYPOSITION
) )
1474 wxLogLastError(wxT("RemoveMenu"));
1484 // we don't change the windows menu, but we update the main one
1485 MDISetMenu(win
, hMenu
, NULL
);
1489 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
1490 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
)
1493 *hwndAct
= (WXHWND
)lParam
;
1494 *hwndDeact
= (WXHWND
)wParam
;
1497 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)