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__)
34 #include "wx/dialog.h"
36 #include "wx/statusbr.h"
38 #include "wx/settings.h"
43 #include "wx/stockitem.h"
45 #include "wx/msw/private.h"
47 #if wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR
48 #include "wx/msw/statbr95.h"
52 #include "wx/toolbar.h"
53 #endif // wxUSE_TOOLBAR
57 // ---------------------------------------------------------------------------
59 // ---------------------------------------------------------------------------
61 extern wxMenu
*wxCurrentPopupMenu
;
63 extern const wxChar
*wxMDIFrameClassName
; // from app.cpp
64 extern const wxChar
*wxMDIChildFrameClassName
;
65 extern const wxChar
*wxMDIChildFrameClassNameNoRedraw
;
66 extern void wxAssociateWinWithHandle(HWND hWnd
, wxWindow
*win
);
67 extern void wxRemoveHandleAssociation(wxWindow
*win
);
69 static HWND invalidHandle
= 0;
71 // ---------------------------------------------------------------------------
73 // ---------------------------------------------------------------------------
75 static const int IDM_WINDOWTILEHOR
= 4001;
76 static const int IDM_WINDOWCASCADE
= 4002;
77 static const int IDM_WINDOWICONS
= 4003;
78 static const int IDM_WINDOWNEXT
= 4004;
79 static const int IDM_WINDOWTILEVERT
= 4005;
80 static const int IDM_WINDOWPREV
= 4006;
82 // This range gives a maximum of 500 MDI children. Should be enough :-)
83 static const int wxFIRST_MDI_CHILD
= 4100;
84 static const int wxLAST_MDI_CHILD
= 4600;
86 // ---------------------------------------------------------------------------
88 // ---------------------------------------------------------------------------
90 // set the MDI menus (by sending the WM_MDISETMENU message) and update the menu
91 // of the parent of win (which is supposed to be the MDI client window)
92 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
);
94 // insert the window menu (subMenu) into menu just before "Help" submenu or at
95 // the very end if not found
96 static void InsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
);
98 // Remove the window menu
99 static void RemoveWindowMenu(wxWindow
*win
, WXHMENU menu
);
101 // is this an id of an MDI child?
102 inline bool IsMdiCommandId(int id
)
104 return (id
>= wxFIRST_MDI_CHILD
) && (id
<= wxLAST_MDI_CHILD
);
107 // unpack the parameters of WM_MDIACTIVATE message
108 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
109 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
);
111 // return the HMENU of the MDI menu
112 static inline HMENU
GetMDIWindowMenu(wxMDIParentFrame
*frame
)
114 wxMenu
*menu
= frame
->GetWindowMenu();
115 return menu
? GetHmenuOf(menu
) : 0;
118 // ===========================================================================
120 // ===========================================================================
122 // ---------------------------------------------------------------------------
124 // ---------------------------------------------------------------------------
126 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxFrame
)
127 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxFrame
)
128 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxWindow
)
130 BEGIN_EVENT_TABLE(wxMDIParentFrame
, wxFrame
)
131 EVT_SIZE(wxMDIParentFrame::OnSize
)
132 EVT_ICONIZE(wxMDIParentFrame::OnIconized
)
133 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged
)
136 BEGIN_EVENT_TABLE(wxMDIChildFrame
, wxFrame
)
137 EVT_IDLE(wxMDIChildFrame::OnIdle
)
140 BEGIN_EVENT_TABLE(wxMDIClientWindow
, wxWindow
)
141 EVT_SCROLL(wxMDIClientWindow::OnScroll
)
144 // ===========================================================================
145 // wxMDIParentFrame: the frame which contains the client window which manages
147 // ===========================================================================
149 wxMDIParentFrame::wxMDIParentFrame()
151 m_clientWindow
= NULL
;
152 m_currentChild
= NULL
;
153 m_windowMenu
= (wxMenu
*) NULL
;
154 m_parentFrameActive
= true;
157 bool wxMDIParentFrame::Create(wxWindow
*parent
,
159 const wxString
& title
,
163 const wxString
& name
)
165 m_clientWindow
= NULL
;
166 m_currentChild
= NULL
;
168 // this style can be used to prevent a window from having the standard MDI
170 if ( style
& wxFRAME_NO_WINDOW_MENU
)
172 m_windowMenu
= (wxMenu
*)NULL
;
174 else // normal case: we have the window menu, so construct it
176 m_windowMenu
= new wxMenu
;
178 m_windowMenu
->Append(IDM_WINDOWCASCADE
, _("&Cascade"));
179 m_windowMenu
->Append(IDM_WINDOWTILEHOR
, _("Tile &Horizontally"));
180 m_windowMenu
->Append(IDM_WINDOWTILEVERT
, _("Tile &Vertically"));
181 m_windowMenu
->AppendSeparator();
182 m_windowMenu
->Append(IDM_WINDOWICONS
, _("&Arrange Icons"));
183 m_windowMenu
->Append(IDM_WINDOWNEXT
, _("&Next"));
184 m_windowMenu
->Append(IDM_WINDOWPREV
, _("&Previous"));
187 m_parentFrameActive
= true;
190 wxTopLevelWindows
.Append(this);
193 m_windowStyle
= style
;
196 parent
->AddChild(this);
198 if ( id
!= wxID_ANY
)
201 m_windowId
= NewControlId();
204 WXDWORD msflags
= MSWGetCreateWindowFlags(&exflags
);
205 msflags
&= ~WS_VSCROLL
;
206 msflags
&= ~WS_HSCROLL
;
208 if ( !wxWindow::MSWCreate(wxMDIFrameClassName
,
217 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
219 // unlike (almost?) all other windows, frames are created hidden
225 wxMDIParentFrame::~wxMDIParentFrame()
227 // see comment in ~wxMDIChildFrame
229 m_frameToolBar
= NULL
;
232 m_frameStatusBar
= NULL
;
233 #endif // wxUSE_STATUSBAR
240 m_windowMenu
= (wxMenu
*) NULL
;
243 // the MDI frame menubar is not automatically deleted by Windows unlike for
247 ::DestroyMenu((HMENU
)m_hMenu
);
248 m_hMenu
= (WXHMENU
)NULL
;
251 if ( m_clientWindow
)
253 if ( m_clientWindow
->MSWGetOldWndProc() )
254 m_clientWindow
->UnsubclassWin();
256 m_clientWindow
->SetHWND(0);
257 delete m_clientWindow
;
261 #if wxUSE_MENUS_NATIVE
263 void wxMDIParentFrame::InternalSetMenuBar()
265 m_parentFrameActive
= true;
267 InsertWindowMenu(GetClientWindow(), m_hMenu
, GetMDIWindowMenu(this));
270 #endif // wxUSE_MENUS_NATIVE
272 void wxMDIParentFrame::SetWindowMenu(wxMenu
* menu
)
278 // Remove old window menu
279 RemoveWindowMenu(GetClientWindow(), m_hMenu
);
283 m_windowMenu
= (wxMenu
*) NULL
;
291 InsertWindowMenu(GetClientWindow(), m_hMenu
,
292 GetHmenuOf(m_windowMenu
));
297 void wxMDIParentFrame::DoMenuUpdates(wxMenu
* menu
)
299 wxMDIChildFrame
*child
= GetActiveChild();
302 wxEvtHandler
* source
= child
->GetEventHandler();
303 wxMenuBar
* bar
= child
->GetMenuBar();
307 menu
->UpdateUI(source
);
313 int nCount
= bar
->GetMenuCount();
314 for (int n
= 0; n
< nCount
; n
++)
315 bar
->GetMenu(n
)->UpdateUI(source
);
321 wxFrameBase::DoMenuUpdates(menu
);
325 void wxMDIParentFrame::UpdateClientSize()
327 if ( GetClientWindow() )
330 GetClientSize(&width
, &height
);
332 GetClientWindow()->SetSize(0, 0, width
, height
);
336 void wxMDIParentFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
340 // do not call event.Skip() here, it somehow messes up MDI client window
343 void wxMDIParentFrame::OnIconized(wxIconizeEvent
& event
)
347 if ( !event
.Iconized() )
353 // Returns the active MDI child window
354 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
356 HWND hWnd
= (HWND
)::SendMessage(GetWinHwnd(GetClientWindow()),
357 WM_MDIGETACTIVE
, 0, 0L);
361 return (wxMDIChildFrame
*)wxFindWinFromHandle((WXHWND
) hWnd
);
364 // Create the client window class (don't Create the window, just return a new
366 wxMDIClientWindow
*wxMDIParentFrame::OnCreateClient()
368 return new wxMDIClientWindow
;
371 // Responds to colour changes, and passes event on to children.
372 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
374 if ( m_clientWindow
)
376 m_clientWindow
->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
377 m_clientWindow
->Refresh();
383 WXHICON
wxMDIParentFrame::GetDefaultIcon() const
385 // we don't have any standard icons (any more)
389 // ---------------------------------------------------------------------------
391 // ---------------------------------------------------------------------------
393 void wxMDIParentFrame::Cascade()
395 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE
, 0, 0);
398 void wxMDIParentFrame::Tile(wxOrientation orient
)
400 wxASSERT_MSG( orient
== wxHORIZONTAL
|| orient
== wxVERTICAL
,
401 _T("invalid orientation value") );
403 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE
,
404 orient
== wxHORIZONTAL
? MDITILE_HORIZONTAL
405 : MDITILE_VERTICAL
, 0);
408 void wxMDIParentFrame::ArrangeIcons()
410 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE
, 0, 0);
413 void wxMDIParentFrame::ActivateNext()
415 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 0);
418 void wxMDIParentFrame::ActivatePrevious()
420 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 1);
423 // ---------------------------------------------------------------------------
424 // the MDI parent frame window proc
425 // ---------------------------------------------------------------------------
427 WXLRESULT
wxMDIParentFrame::MSWWindowProc(WXUINT message
,
432 bool processed
= false;
438 WXWORD state
, minimized
;
440 UnpackActivate(wParam
, lParam
, &state
, &minimized
, &hwnd
);
442 processed
= HandleActivate(state
, minimized
!= 0, hwnd
);
450 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
452 (void)HandleCommand(id
, cmd
, hwnd
);
454 // even if the frame didn't process it, there is no need to try it
455 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
456 // so pretend we processed the message anyhow
460 // always pass this message DefFrameProc(), otherwise MDI menu
461 // commands (and sys commands - more surprisingly!) won't work
462 MSWDefWindowProc(message
, wParam
, lParam
);
466 m_clientWindow
= OnCreateClient();
467 // Uses own style for client style
468 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
470 wxLogMessage(_("Failed to create MDI parent frame."));
481 // we erase background ourselves
489 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
491 if ( m_parentFrameActive
)
493 processed
= HandleMenuSelect(item
, flags
, hmenu
);
495 else if (m_currentChild
)
497 processed
= m_currentChild
->
498 HandleMenuSelect(item
, flags
, hmenu
);
504 // though we don't (usually) resize the MDI client to exactly fit the
505 // client area we need to pass this one to DefFrameProc to allow the children to show
510 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
515 bool wxMDIParentFrame::HandleActivate(int state
, bool minimized
, WXHWND activate
)
517 bool processed
= false;
519 if ( wxWindow::HandleActivate(state
, minimized
, activate
) )
525 // If this window is an MDI parent, we must also send an OnActivate message
526 // to the current child.
527 if ( (m_currentChild
!= NULL
) &&
528 ((state
== WA_ACTIVE
) || (state
== WA_CLICKACTIVE
)) )
530 wxActivateEvent
event(wxEVT_ACTIVATE
, true, m_currentChild
->GetId());
531 event
.SetEventObject( m_currentChild
);
532 if ( m_currentChild
->GetEventHandler()->ProcessEvent(event
) )
539 bool wxMDIParentFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND hwnd
)
541 // In case it's e.g. a toolbar.
544 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
546 return win
->MSWCommand(cmd
, id
);
549 if (wxCurrentPopupMenu
)
551 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
552 wxCurrentPopupMenu
= NULL
;
553 if (popupMenu
->MSWCommand(cmd
, id
))
557 // is it one of standard MDI commands?
563 case IDM_WINDOWCASCADE
:
565 wParam
= MDITILE_SKIPDISABLED
;
568 case IDM_WINDOWTILEHOR
:
569 wParam
|= MDITILE_HORIZONTAL
;
572 case IDM_WINDOWTILEVERT
:
574 wParam
= MDITILE_VERTICAL
;
576 wParam
|= MDITILE_SKIPDISABLED
;
579 case IDM_WINDOWICONS
:
580 msg
= WM_MDIICONARRANGE
;
585 lParam
= 0; // next child
590 lParam
= 1; // previous child
599 ::SendMessage(GetWinHwnd(GetClientWindow()), msg
, wParam
, lParam
);
604 // FIXME VZ: what does this test do??
607 return false; // Get WndProc to call default proc
610 if ( IsMdiCommandId(id
) )
612 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
615 wxWindow
*child
= node
->GetData();
616 if ( child
->GetHWND() )
618 long childId
= wxGetWindowId(child
->GetHWND());
619 if (childId
== (long)id
)
621 ::SendMessage( GetWinHwnd(GetClientWindow()),
623 (WPARAM
)child
->GetHWND(), 0);
627 node
= node
->GetNext();
630 else if ( m_parentFrameActive
)
632 return ProcessCommand(id
);
634 else if ( m_currentChild
)
636 return m_currentChild
->HandleCommand(id
, cmd
, hwnd
);
640 // this shouldn't happen because it means that our messages are being
641 // lost (they're not sent to the parent frame nor to the children)
642 wxFAIL_MSG(wxT("MDI parent frame is not active, yet there is no active MDI child?"));
648 WXLRESULT
wxMDIParentFrame::MSWDefWindowProc(WXUINT message
,
653 if ( GetClientWindow() )
654 clientWnd
= GetClientWindow()->GetHWND();
658 return DefFrameProc(GetHwnd(), (HWND
)clientWnd
, message
, wParam
, lParam
);
661 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG
* msg
)
663 MSG
*pMsg
= (MSG
*)msg
;
665 // first let the current child get it
666 if ( m_currentChild
&& m_currentChild
->GetHWND() &&
667 m_currentChild
->MSWTranslateMessage(msg
) )
672 // then try out accel table (will also check the menu accels)
673 if ( wxFrame::MSWTranslateMessage(msg
) )
678 // finally, check for MDI specific built in accel keys
679 if ( pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
681 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg
))
688 // ===========================================================================
690 // ===========================================================================
692 void wxMDIChildFrame::Init()
694 m_needsResize
= true;
695 m_needsInitialShow
= true;
698 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
700 const wxString
& title
,
704 const wxString
& name
)
708 if ( id
!= wxID_ANY
)
711 m_windowId
= (int)NewControlId();
715 parent
->AddChild(this);
725 mcs
.szClass
= style
& wxFULL_REPAINT_ON_RESIZE
726 ? wxMDIChildFrameClassName
727 : wxMDIChildFrameClassNameNoRedraw
;
729 mcs
.hOwner
= wxGetInstance();
730 if (x
!= wxDefaultCoord
)
733 mcs
.x
= CW_USEDEFAULT
;
735 if (y
!= wxDefaultCoord
)
738 mcs
.y
= CW_USEDEFAULT
;
740 if (width
!= wxDefaultCoord
)
743 mcs
.cx
= CW_USEDEFAULT
;
745 if (height
!= wxDefaultCoord
)
748 mcs
.cy
= CW_USEDEFAULT
;
750 DWORD msflags
= WS_OVERLAPPED
| WS_CLIPCHILDREN
;
751 if (style
& wxMINIMIZE_BOX
)
752 msflags
|= WS_MINIMIZEBOX
;
753 if (style
& wxMAXIMIZE_BOX
)
754 msflags
|= WS_MAXIMIZEBOX
;
755 if (style
& wxRESIZE_BORDER
)
756 msflags
|= WS_THICKFRAME
;
757 if (style
& wxSYSTEM_MENU
)
758 msflags
|= WS_SYSMENU
;
759 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
760 msflags
|= WS_MINIMIZE
;
761 if (style
& wxMAXIMIZE
)
762 msflags
|= WS_MAXIMIZE
;
763 if (style
& wxCAPTION
)
764 msflags
|= WS_CAPTION
;
770 wxWindowCreationHook
hook(this);
772 m_hWnd
= (WXHWND
)::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
773 WM_MDICREATE
, 0, (LONG
)(LPSTR
)&mcs
);
775 wxAssociateWinWithHandle((HWND
) GetHWND(), this);
780 wxMDIChildFrame::~wxMDIChildFrame()
782 // will be destroyed by DestroyChildren() but reset them before calling it
783 // to avoid using dangling pointers if a callback comes in the meanwhile
785 m_frameToolBar
= NULL
;
788 m_frameStatusBar
= NULL
;
789 #endif // wxUSE_STATUSBAR
793 RemoveWindowMenu(NULL
, m_hMenu
);
798 bool wxMDIChildFrame::Show(bool show
)
800 m_needsInitialShow
= false;
802 if (!wxFrame::Show(show
))
805 // KH: Without this call, new MDI children do not become active.
806 // This was added here after the same BringWindowToTop call was
807 // removed from wxTopLevelWindow::Show (November 2005)
809 ::BringWindowToTop(GetHwnd());
811 // we need to refresh the MDI frame window menu to include (or exclude if
812 // we've been hidden) this frame
813 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
814 MDISetMenu(parent
->GetClientWindow(), NULL
, NULL
);
819 // Set the client size (i.e. leave the calculation of borders etc.
821 void wxMDIChildFrame::DoSetClientSize(int width
, int height
)
823 HWND hWnd
= GetHwnd();
826 ::GetClientRect(hWnd
, &rect
);
829 GetWindowRect(hWnd
, &rect2
);
831 // Find the difference between the entire window (title bar and all)
832 // and the client area; add this to the new client size to move the
834 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
835 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
838 if (GetStatusBar() && GetStatusBar()->IsShown())
841 GetStatusBar()->GetSize(&sx
, &sy
);
844 #endif // wxUSE_STATUSBAR
847 point
.x
= rect2
.left
;
850 // If there's an MDI parent, must subtract the parent's top left corner
851 // since MoveWindow moves relative to the parent
852 wxMDIParentFrame
*mdiParent
= (wxMDIParentFrame
*)GetParent();
853 ::ScreenToClient((HWND
) mdiParent
->GetClientWindow()->GetHWND(), &point
);
855 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)true);
857 wxSize
size(width
, height
);
858 wxSizeEvent
event(size
, m_windowId
);
859 event
.SetEventObject( this );
860 GetEventHandler()->ProcessEvent(event
);
863 void wxMDIChildFrame::DoGetPosition(int *x
, int *y
) const
866 GetWindowRect(GetHwnd(), &rect
);
871 // Since we now have the absolute screen coords,
872 // if there's a parent we must subtract its top left corner
873 wxMDIParentFrame
*mdiParent
= (wxMDIParentFrame
*)GetParent();
874 ::ScreenToClient((HWND
) mdiParent
->GetClientWindow()->GetHWND(), &point
);
882 void wxMDIChildFrame::InternalSetMenuBar()
884 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
886 InsertWindowMenu(parent
->GetClientWindow(),
887 m_hMenu
, GetMDIWindowMenu(parent
));
889 parent
->m_parentFrameActive
= false;
892 void wxMDIChildFrame::DetachMenuBar()
894 RemoveWindowMenu(NULL
, m_hMenu
);
895 wxFrame::DetachMenuBar();
898 WXHICON
wxMDIChildFrame::GetDefaultIcon() const
900 // we don't have any standard icons (any more)
904 // ---------------------------------------------------------------------------
906 // ---------------------------------------------------------------------------
908 void wxMDIChildFrame::Maximize(bool maximize
)
910 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
911 if ( parent
&& parent
->GetClientWindow() )
913 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
914 maximize
? WM_MDIMAXIMIZE
: WM_MDIRESTORE
,
915 (WPARAM
)GetHwnd(), 0);
919 void wxMDIChildFrame::Restore()
921 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
922 if ( parent
&& parent
->GetClientWindow() )
924 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIRESTORE
,
925 (WPARAM
) GetHwnd(), 0);
929 void wxMDIChildFrame::Activate()
931 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
932 if ( parent
&& parent
->GetClientWindow() )
934 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIACTIVATE
,
935 (WPARAM
) GetHwnd(), 0);
939 // ---------------------------------------------------------------------------
940 // MDI window proc and message handlers
941 // ---------------------------------------------------------------------------
943 WXLRESULT
wxMDIChildFrame::MSWWindowProc(WXUINT message
,
948 bool processed
= false;
956 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
959 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
963 case WM_GETMINMAXINFO
:
964 processed
= HandleGetMinMaxInfo((MINMAXINFO
*)lParam
);
970 WXHWND hwndAct
, hwndDeact
;
971 UnpackMDIActivate(wParam
, lParam
, &act
, &hwndAct
, &hwndDeact
);
973 processed
= HandleMDIActivate(act
, hwndAct
, hwndDeact
);
978 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
979 // scrollbars if necessary
984 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
986 MSWDefWindowProc(message
, wParam
, lParam
);
990 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
991 // the message (the base class version does not)
992 return MSWDefWindowProc(message
, wParam
, lParam
);
994 case WM_WINDOWPOSCHANGING
:
995 processed
= HandleWindowPosChanging((LPWINDOWPOS
)lParam
);
1000 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
1005 bool wxMDIChildFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND hwnd
)
1007 // In case it's e.g. a toolbar.
1010 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
1012 return win
->MSWCommand(cmd
, id
);
1015 if (wxCurrentPopupMenu
)
1017 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
1018 wxCurrentPopupMenu
= NULL
;
1019 if (popupMenu
->MSWCommand(cmd
, id
))
1024 if (GetMenuBar() && GetMenuBar()->FindItem(id
))
1026 processed
= ProcessCommand(id
);
1036 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate
),
1040 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
1042 HMENU menuToSet
= 0;
1046 if ( m_hWnd
== hwndAct
)
1049 parent
->m_currentChild
= this;
1051 HMENU child_menu
= (HMENU
)GetWinMenu();
1054 parent
->m_parentFrameActive
= false;
1056 menuToSet
= child_menu
;
1059 else if ( m_hWnd
== hwndDeact
)
1061 wxASSERT_MSG( parent
->m_currentChild
== this,
1062 wxT("can't deactivate MDI child which wasn't active!") );
1065 parent
->m_currentChild
= NULL
;
1067 HMENU parent_menu
= (HMENU
)parent
->GetWinMenu();
1069 // activate the the parent menu only when there is no other child
1070 // that has been activated
1071 if ( parent_menu
&& !hwndAct
)
1073 parent
->m_parentFrameActive
= true;
1075 menuToSet
= parent_menu
;
1080 // we have nothing to do with it
1086 MDISetMenu(parent
->GetClientWindow(),
1087 menuToSet
, GetMDIWindowMenu(parent
));
1090 wxActivateEvent
event(wxEVT_ACTIVATE
, activated
, m_windowId
);
1091 event
.SetEventObject( this );
1093 ResetWindowStyle((void *)NULL
);
1095 return GetEventHandler()->ProcessEvent(event
);
1098 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos
)
1100 WINDOWPOS
*lpPos
= (WINDOWPOS
*)pos
;
1102 if (!(lpPos
->flags
& SWP_NOSIZE
))
1105 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1106 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1107 if (ResetWindowStyle((void *) & rectClient
) && (dwStyle
& WS_MAXIMIZE
))
1109 ::AdjustWindowRectEx(&rectClient
, dwStyle
, false, dwExStyle
);
1110 lpPos
->x
= rectClient
.left
;
1111 lpPos
->y
= rectClient
.top
;
1112 lpPos
->cx
= rectClient
.right
- rectClient
.left
;
1113 lpPos
->cy
= rectClient
.bottom
- rectClient
.top
;
1120 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo
)
1122 MINMAXINFO
*info
= (MINMAXINFO
*)mmInfo
;
1124 // let the default window proc calculate the size of MDI children
1125 // frames because it is based on the size of the MDI client window,
1126 // not on the values specified in wxWindow m_max variables
1127 bool processed
= MSWDefWindowProc(WM_GETMINMAXINFO
, 0, (LPARAM
)mmInfo
) != 0;
1129 int minWidth
= GetMinWidth(),
1130 minHeight
= GetMinHeight();
1132 // but allow GetSizeHints() to set the min size
1133 if ( minWidth
!= wxDefaultCoord
)
1135 info
->ptMinTrackSize
.x
= minWidth
;
1140 if ( minHeight
!= wxDefaultCoord
)
1142 info
->ptMinTrackSize
.y
= minHeight
;
1150 // ---------------------------------------------------------------------------
1151 // MDI specific message translation/preprocessing
1152 // ---------------------------------------------------------------------------
1154 WXLRESULT
wxMDIChildFrame::MSWDefWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1156 return DefMDIChildProc(GetHwnd(),
1157 (UINT
)message
, (WPARAM
)wParam
, (LPARAM
)lParam
);
1160 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG
* msg
)
1162 // we must pass the parent frame to ::TranslateAccelerator(), otherwise it
1163 // doesn't do its job correctly for MDI child menus
1164 return MSWDoTranslateMessage((wxMDIChildFrame
*)GetParent(), msg
);
1167 // ---------------------------------------------------------------------------
1169 // ---------------------------------------------------------------------------
1171 void wxMDIChildFrame::MSWDestroyWindow()
1173 invalidHandle
= GetHwnd();
1175 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
1177 // Must make sure this handle is invalidated (set to NULL) since all sorts
1178 // of things could happen after the child client is destroyed, but before
1179 // the wxFrame is destroyed.
1181 HWND oldHandle
= (HWND
)GetHWND();
1182 SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIDESTROY
,
1183 (WPARAM
)oldHandle
, 0);
1185 if (parent
->GetActiveChild() == (wxMDIChildFrame
*) NULL
)
1186 ResetWindowStyle((void*) NULL
);
1192 ::DestroyMenu((HMENU
) m_hMenu
);
1195 wxRemoveHandleAssociation(this);
1199 // Change the client window's extended style so we don't get a client edge
1200 // style when a child is maximised (a double border looks silly.)
1201 bool wxMDIChildFrame::ResetWindowStyle(void *vrect
)
1203 RECT
*rect
= (RECT
*)vrect
;
1204 wxMDIParentFrame
* pFrameWnd
= (wxMDIParentFrame
*)GetParent();
1205 wxMDIChildFrame
* pChild
= pFrameWnd
->GetActiveChild();
1207 if (!pChild
|| (pChild
== this))
1209 HWND hwndClient
= GetWinHwnd(pFrameWnd
->GetClientWindow());
1210 DWORD dwStyle
= ::GetWindowLong(hwndClient
, GWL_EXSTYLE
);
1212 // we want to test whether there is a maximized child, so just set
1213 // dwThisStyle to 0 if there is no child at all
1214 DWORD dwThisStyle
= pChild
1215 ? ::GetWindowLong(GetWinHwnd(pChild
), GWL_STYLE
) : 0;
1216 DWORD dwNewStyle
= dwStyle
;
1217 if ( dwThisStyle
& WS_MAXIMIZE
)
1218 dwNewStyle
&= ~(WS_EX_CLIENTEDGE
);
1220 dwNewStyle
|= WS_EX_CLIENTEDGE
;
1222 if (dwStyle
!= dwNewStyle
)
1224 // force update of everything
1225 ::RedrawWindow(hwndClient
, NULL
, NULL
,
1226 RDW_INVALIDATE
| RDW_ALLCHILDREN
);
1227 ::SetWindowLong(hwndClient
, GWL_EXSTYLE
, dwNewStyle
);
1228 ::SetWindowPos(hwndClient
, NULL
, 0, 0, 0, 0,
1229 SWP_FRAMECHANGED
| SWP_NOACTIVATE
|
1230 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1233 ::GetClientRect(hwndClient
, rect
);
1242 // ===========================================================================
1243 // wxMDIClientWindow: the window of predefined (by Windows) class which
1244 // contains the child frames
1245 // ===========================================================================
1247 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
1249 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1251 CLIENTCREATESTRUCT ccs
;
1252 m_windowStyle
= style
;
1255 ccs
.hWindowMenu
= GetMDIWindowMenu(parent
);
1256 ccs
.idFirstChild
= wxFIRST_MDI_CHILD
;
1258 DWORD msStyle
= MDIS_ALLCHILDSTYLES
| WS_VISIBLE
| WS_CHILD
|
1259 WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
;
1261 if ( style
& wxHSCROLL
)
1262 msStyle
|= WS_HSCROLL
;
1263 if ( style
& wxVSCROLL
)
1264 msStyle
|= WS_VSCROLL
;
1266 DWORD exStyle
= WS_EX_CLIENTEDGE
;
1268 wxWindowCreationHook
hook(this);
1269 m_hWnd
= (WXHWND
)::CreateWindowEx
1279 (LPSTR
)(LPCLIENTCREATESTRUCT
)&ccs
);
1282 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1287 SubclassWin(m_hWnd
);
1292 // Explicitly call default scroll behaviour
1293 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)
1295 // Note: for client windows, the scroll position is not set in
1296 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1297 // scroll position we're at.
1298 // This makes it hard to paint patterns or bitmaps in the background,
1299 // and have the client area scrollable as well.
1301 if ( event
.GetOrientation() == wxHORIZONTAL
)
1302 m_scrollX
= event
.GetPosition(); // Always returns zero!
1304 m_scrollY
= event
.GetPosition(); // Always returns zero!
1309 void wxMDIClientWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1311 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1312 // client area, you can end up with a non-refreshed portion in the client window
1313 // (see OGL studio sample). So check if the position is changed and if so,
1314 // redraw the MDI child frames.
1316 const wxPoint oldPos
= GetPosition();
1318 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
| wxSIZE_FORCE
);
1320 const wxPoint newPos
= GetPosition();
1322 if ((newPos
.x
!= oldPos
.x
) || (newPos
.y
!= oldPos
.y
))
1326 wxWindowList::compatibility_iterator node
= GetParent()->GetChildren().GetFirst();
1329 wxWindow
*child
= node
->GetData();
1330 if (child
->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
1332 ::RedrawWindow(GetHwndOf(child
),
1339 node
= node
->GetNext();
1345 void wxMDIChildFrame::OnIdle(wxIdleEvent
& event
)
1347 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1348 // in flicker e.g. when the frame contained controls with non-trivial
1349 // layout. Since 2.5.3, the frame is created hidden as all other top level
1350 // windows. In order to maintain backward compatibility, the frame is shown
1351 // in OnIdle, unless Show(false) was called by the programmer before.
1352 if ( m_needsInitialShow
)
1357 // MDI child frames get their WM_SIZE when they're constructed but at this
1358 // moment they don't have any children yet so all child windows will be
1359 // positioned incorrectly when they are added later - to fix this, we
1360 // generate an artificial size event here
1361 if ( m_needsResize
)
1363 m_needsResize
= false; // avoid any possibility of recursion
1371 // ---------------------------------------------------------------------------
1372 // non member functions
1373 // ---------------------------------------------------------------------------
1375 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
)
1377 if ( hmenuFrame
|| hmenuWindow
)
1379 if ( !::SendMessage(GetWinHwnd(win
),
1382 (LPARAM
)hmenuWindow
) )
1384 wxLogLastError(_T("SendMessage(WM_MDISETMENU)"));
1388 // update menu bar of the parent window
1389 wxWindow
*parent
= win
->GetParent();
1390 wxCHECK_RET( parent
, wxT("MDI client without parent frame? weird...") );
1392 ::SendMessage(GetWinHwnd(win
), WM_MDIREFRESHMENU
, 0, 0L);
1394 ::DrawMenuBar(GetWinHwnd(parent
));
1397 static void InsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
)
1399 // Try to insert Window menu in front of Help, otherwise append it.
1400 HMENU hmenu
= (HMENU
)menu
;
1404 int N
= GetMenuItemCount(hmenu
);
1405 bool success
= false;
1406 for ( int i
= 0; i
< N
; i
++ )
1409 int chars
= GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
);
1412 wxLogLastError(wxT("GetMenuString"));
1417 wxString
strBuf(buf
);
1418 if ( wxStripMenuCodes(strBuf
) == wxGetStockLabel(wxID_HELP
,false) )
1421 ::InsertMenu(hmenu
, i
, MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1422 (UINT
)subMenu
, _("&Window"));
1429 ::AppendMenu(hmenu
, MF_POPUP
, (UINT
)subMenu
, _("&Window"));
1433 MDISetMenu(win
, hmenu
, subMenu
);
1436 static void RemoveWindowMenu(wxWindow
*win
, WXHMENU menu
)
1438 HMENU hMenu
= (HMENU
)menu
;
1444 int N
= ::GetMenuItemCount(hMenu
);
1445 for ( int i
= 0; i
< N
; i
++ )
1447 if ( !::GetMenuString(hMenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1449 // Ignore successful read of menu string with length 0 which
1450 // occurs, for example, for a maximized MDI childs system menu
1451 if ( ::GetLastError() != 0 )
1453 wxLogLastError(wxT("GetMenuString"));
1459 if ( wxStrcmp(buf
, _("&Window")) == 0 )
1461 if ( !::RemoveMenu(hMenu
, i
, MF_BYPOSITION
) )
1463 wxLogLastError(wxT("RemoveMenu"));
1473 // we don't change the windows menu, but we update the main one
1474 MDISetMenu(win
, hMenu
, NULL
);
1478 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
1479 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
)
1482 *hwndAct
= (WXHWND
)lParam
;
1483 *hwndDeact
= (WXHWND
)wParam
;
1486 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)