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 // unlike (almost?) all other windows, frames are created hidden
223 wxMDIParentFrame::~wxMDIParentFrame()
225 // see comment in ~wxMDIChildFrame
227 m_frameToolBar
= NULL
;
230 m_frameStatusBar
= NULL
;
231 #endif // wxUSE_STATUSBAR
238 m_windowMenu
= (wxMenu
*) NULL
;
241 // the MDI frame menubar is not automatically deleted by Windows unlike for
245 ::DestroyMenu((HMENU
)m_hMenu
);
246 m_hMenu
= (WXHMENU
)NULL
;
249 if ( m_clientWindow
)
251 if ( m_clientWindow
->MSWGetOldWndProc() )
252 m_clientWindow
->UnsubclassWin();
254 m_clientWindow
->SetHWND(0);
255 delete m_clientWindow
;
259 #if wxUSE_MENUS_NATIVE
261 void wxMDIParentFrame::InternalSetMenuBar()
263 m_parentFrameActive
= true;
265 InsertWindowMenu(GetClientWindow(), m_hMenu
, GetMDIWindowMenu(this));
268 #endif // wxUSE_MENUS_NATIVE
270 void wxMDIParentFrame::SetWindowMenu(wxMenu
* menu
)
276 // Remove old window menu
277 RemoveWindowMenu(GetClientWindow(), m_hMenu
);
281 m_windowMenu
= (wxMenu
*) NULL
;
289 InsertWindowMenu(GetClientWindow(), m_hMenu
,
290 GetHmenuOf(m_windowMenu
));
295 void wxMDIParentFrame::DoMenuUpdates(wxMenu
* menu
)
297 wxMDIChildFrame
*child
= GetActiveChild();
300 wxEvtHandler
* source
= child
->GetEventHandler();
301 wxMenuBar
* bar
= child
->GetMenuBar();
305 menu
->UpdateUI(source
);
311 int nCount
= bar
->GetMenuCount();
312 for (int n
= 0; n
< nCount
; n
++)
313 bar
->GetMenu(n
)->UpdateUI(source
);
319 wxFrameBase::DoMenuUpdates(menu
);
323 void wxMDIParentFrame::UpdateClientSize()
325 if ( GetClientWindow() )
328 GetClientSize(&width
, &height
);
330 GetClientWindow()->SetSize(0, 0, width
, height
);
334 void wxMDIParentFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
338 // do not call event.Skip() here, it somehow messes up MDI client window
341 void wxMDIParentFrame::OnIconized(wxIconizeEvent
& event
)
345 if ( !event
.Iconized() )
351 // Returns the active MDI child window
352 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
354 HWND hWnd
= (HWND
)::SendMessage(GetWinHwnd(GetClientWindow()),
355 WM_MDIGETACTIVE
, 0, 0L);
359 return (wxMDIChildFrame
*)wxFindWinFromHandle((WXHWND
) hWnd
);
362 // Create the client window class (don't Create the window, just return a new
364 wxMDIClientWindow
*wxMDIParentFrame::OnCreateClient()
366 return new wxMDIClientWindow
;
369 // Responds to colour changes, and passes event on to children.
370 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
372 if ( m_clientWindow
)
374 m_clientWindow
->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
375 m_clientWindow
->Refresh();
381 WXHICON
wxMDIParentFrame::GetDefaultIcon() const
383 // we don't have any standard icons (any more)
387 // ---------------------------------------------------------------------------
389 // ---------------------------------------------------------------------------
391 void wxMDIParentFrame::Cascade()
393 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE
, 0, 0);
396 void wxMDIParentFrame::Tile(wxOrientation orient
)
398 wxASSERT_MSG( orient
== wxHORIZONTAL
|| orient
== wxVERTICAL
,
399 _T("invalid orientation value") );
401 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE
,
402 orient
== wxHORIZONTAL
? MDITILE_HORIZONTAL
403 : MDITILE_VERTICAL
, 0);
406 void wxMDIParentFrame::ArrangeIcons()
408 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE
, 0, 0);
411 void wxMDIParentFrame::ActivateNext()
413 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 0);
416 void wxMDIParentFrame::ActivatePrevious()
418 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 1);
421 // ---------------------------------------------------------------------------
422 // the MDI parent frame window proc
423 // ---------------------------------------------------------------------------
425 WXLRESULT
wxMDIParentFrame::MSWWindowProc(WXUINT message
,
430 bool processed
= false;
436 WXWORD state
, minimized
;
438 UnpackActivate(wParam
, lParam
, &state
, &minimized
, &hwnd
);
440 processed
= HandleActivate(state
, minimized
!= 0, hwnd
);
448 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
450 (void)HandleCommand(id
, cmd
, hwnd
);
452 // even if the frame didn't process it, there is no need to try it
453 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
454 // so pretend we processed the message anyhow
458 // always pass this message DefFrameProc(), otherwise MDI menu
459 // commands (and sys commands - more surprisingly!) won't work
460 MSWDefWindowProc(message
, wParam
, lParam
);
464 m_clientWindow
= OnCreateClient();
465 // Uses own style for client style
466 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
468 wxLogMessage(_("Failed to create MDI parent frame."));
479 // we erase background ourselves
487 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
489 if ( m_parentFrameActive
)
491 processed
= HandleMenuSelect(item
, flags
, hmenu
);
493 else if (m_currentChild
)
495 processed
= m_currentChild
->
496 HandleMenuSelect(item
, flags
, hmenu
);
502 // though we don't (usually) resize the MDI client to exactly fit the
503 // client area we need to pass this one to DefFrameProc to allow the children to show
508 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
513 bool wxMDIParentFrame::HandleActivate(int state
, bool minimized
, WXHWND activate
)
515 bool processed
= false;
517 if ( wxWindow::HandleActivate(state
, minimized
, activate
) )
523 // If this window is an MDI parent, we must also send an OnActivate message
524 // to the current child.
525 if ( (m_currentChild
!= NULL
) &&
526 ((state
== WA_ACTIVE
) || (state
== WA_CLICKACTIVE
)) )
528 wxActivateEvent
event(wxEVT_ACTIVATE
, true, m_currentChild
->GetId());
529 event
.SetEventObject( m_currentChild
);
530 if ( m_currentChild
->GetEventHandler()->ProcessEvent(event
) )
537 bool wxMDIParentFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND hwnd
)
539 // In case it's e.g. a toolbar.
542 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
544 return win
->MSWCommand(cmd
, id
);
547 if (wxCurrentPopupMenu
)
549 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
550 wxCurrentPopupMenu
= NULL
;
551 if (popupMenu
->MSWCommand(cmd
, id
))
555 // is it one of standard MDI commands?
561 case IDM_WINDOWCASCADE
:
563 wParam
= MDITILE_SKIPDISABLED
;
566 case IDM_WINDOWTILEHOR
:
567 wParam
|= MDITILE_HORIZONTAL
;
570 case IDM_WINDOWTILEVERT
:
572 wParam
= MDITILE_VERTICAL
;
574 wParam
|= MDITILE_SKIPDISABLED
;
577 case IDM_WINDOWICONS
:
578 msg
= WM_MDIICONARRANGE
;
583 lParam
= 0; // next child
588 lParam
= 1; // previous child
597 ::SendMessage(GetWinHwnd(GetClientWindow()), msg
, wParam
, lParam
);
602 // FIXME VZ: what does this test do??
605 return false; // Get WndProc to call default proc
608 if ( IsMdiCommandId(id
) )
610 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
613 wxWindow
*child
= node
->GetData();
614 if ( child
->GetHWND() )
616 long childId
= wxGetWindowId(child
->GetHWND());
617 if (childId
== (long)id
)
619 ::SendMessage( GetWinHwnd(GetClientWindow()),
621 (WPARAM
)child
->GetHWND(), 0);
625 node
= node
->GetNext();
628 else if ( m_parentFrameActive
)
630 return ProcessCommand(id
);
632 else if ( m_currentChild
)
634 return m_currentChild
->HandleCommand(id
, cmd
, hwnd
);
638 // this shouldn't happen because it means that our messages are being
639 // lost (they're not sent to the parent frame nor to the children)
640 wxFAIL_MSG(wxT("MDI parent frame is not active, yet there is no active MDI child?"));
646 WXLRESULT
wxMDIParentFrame::MSWDefWindowProc(WXUINT message
,
651 if ( GetClientWindow() )
652 clientWnd
= GetClientWindow()->GetHWND();
656 return DefFrameProc(GetHwnd(), (HWND
)clientWnd
, message
, wParam
, lParam
);
659 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG
* msg
)
661 MSG
*pMsg
= (MSG
*)msg
;
663 // first let the current child get it
664 if ( m_currentChild
&& m_currentChild
->GetHWND() &&
665 m_currentChild
->MSWTranslateMessage(msg
) )
670 // then try out accel table (will also check the menu accels)
671 if ( wxFrame::MSWTranslateMessage(msg
) )
676 // finally, check for MDI specific built in accel keys
677 if ( pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
679 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg
))
686 // ===========================================================================
688 // ===========================================================================
690 void wxMDIChildFrame::Init()
692 m_needsResize
= true;
693 m_needsInitialShow
= true;
696 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
698 const wxString
& title
,
702 const wxString
& name
)
706 if ( id
!= wxID_ANY
)
709 m_windowId
= (int)NewControlId();
713 parent
->AddChild(this);
723 mcs
.szClass
= style
& wxFULL_REPAINT_ON_RESIZE
724 ? wxMDIChildFrameClassName
725 : wxMDIChildFrameClassNameNoRedraw
;
727 mcs
.hOwner
= wxGetInstance();
728 if (x
!= wxDefaultCoord
)
731 mcs
.x
= CW_USEDEFAULT
;
733 if (y
!= wxDefaultCoord
)
736 mcs
.y
= CW_USEDEFAULT
;
738 if (width
!= wxDefaultCoord
)
741 mcs
.cx
= CW_USEDEFAULT
;
743 if (height
!= wxDefaultCoord
)
746 mcs
.cy
= CW_USEDEFAULT
;
748 DWORD msflags
= WS_OVERLAPPED
| WS_CLIPCHILDREN
;
749 if (style
& wxMINIMIZE_BOX
)
750 msflags
|= WS_MINIMIZEBOX
;
751 if (style
& wxMAXIMIZE_BOX
)
752 msflags
|= WS_MAXIMIZEBOX
;
753 if (style
& wxTHICK_FRAME
)
754 msflags
|= WS_THICKFRAME
;
755 if (style
& wxSYSTEM_MENU
)
756 msflags
|= WS_SYSMENU
;
757 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
758 msflags
|= WS_MINIMIZE
;
759 if (style
& wxMAXIMIZE
)
760 msflags
|= WS_MAXIMIZE
;
761 if (style
& wxCAPTION
)
762 msflags
|= WS_CAPTION
;
768 wxWindowCreationHook
hook(this);
770 m_hWnd
= (WXHWND
)::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
771 WM_MDICREATE
, 0, (LONG
)(LPSTR
)&mcs
);
773 wxAssociateWinWithHandle((HWND
) GetHWND(), this);
778 wxMDIChildFrame::~wxMDIChildFrame()
780 // will be destroyed by DestroyChildren() but reset them before calling it
781 // to avoid using dangling pointers if a callback comes in the meanwhile
783 m_frameToolBar
= NULL
;
786 m_frameStatusBar
= NULL
;
787 #endif // wxUSE_STATUSBAR
791 RemoveWindowMenu(NULL
, m_hMenu
);
796 bool wxMDIChildFrame::Show(bool show
)
798 m_needsInitialShow
= false;
800 if (!wxFrame::Show(show
))
803 // KH: Without this call, new MDI children do not become active.
804 // This was added here after the same BringWindowToTop call was
805 // removed from wxTopLevelWindow::Show (November 2005)
807 ::BringWindowToTop(GetHwnd());
809 // we need to refresh the MDI frame window menu to include (or exclude if
810 // we've been hidden) this frame
811 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
812 MDISetMenu(parent
->GetClientWindow(), NULL
, NULL
);
817 // Set the client size (i.e. leave the calculation of borders etc.
819 void wxMDIChildFrame::DoSetClientSize(int width
, int height
)
821 HWND hWnd
= GetHwnd();
824 ::GetClientRect(hWnd
, &rect
);
827 GetWindowRect(hWnd
, &rect2
);
829 // Find the difference between the entire window (title bar and all)
830 // and the client area; add this to the new client size to move the
832 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
833 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
836 if (GetStatusBar() && GetStatusBar()->IsShown())
839 GetStatusBar()->GetSize(&sx
, &sy
);
842 #endif // wxUSE_STATUSBAR
845 point
.x
= rect2
.left
;
848 // If there's an MDI parent, must subtract the parent's top left corner
849 // since MoveWindow moves relative to the parent
850 wxMDIParentFrame
*mdiParent
= (wxMDIParentFrame
*)GetParent();
851 ::ScreenToClient((HWND
) mdiParent
->GetClientWindow()->GetHWND(), &point
);
853 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)true);
855 wxSize
size(width
, height
);
856 wxSizeEvent
event(size
, m_windowId
);
857 event
.SetEventObject( this );
858 GetEventHandler()->ProcessEvent(event
);
861 void wxMDIChildFrame::DoGetPosition(int *x
, int *y
) const
864 GetWindowRect(GetHwnd(), &rect
);
869 // Since we now have the absolute screen coords,
870 // if there's a parent we must subtract its top left corner
871 wxMDIParentFrame
*mdiParent
= (wxMDIParentFrame
*)GetParent();
872 ::ScreenToClient((HWND
) mdiParent
->GetClientWindow()->GetHWND(), &point
);
880 void wxMDIChildFrame::InternalSetMenuBar()
882 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
884 InsertWindowMenu(parent
->GetClientWindow(),
885 m_hMenu
, GetMDIWindowMenu(parent
));
887 parent
->m_parentFrameActive
= false;
890 void wxMDIChildFrame::DetachMenuBar()
892 RemoveWindowMenu(NULL
, m_hMenu
);
893 wxFrame::DetachMenuBar();
896 WXHICON
wxMDIChildFrame::GetDefaultIcon() const
898 // we don't have any standard icons (any more)
902 // ---------------------------------------------------------------------------
904 // ---------------------------------------------------------------------------
906 void wxMDIChildFrame::Maximize(bool maximize
)
908 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
909 if ( parent
&& parent
->GetClientWindow() )
911 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
912 maximize
? WM_MDIMAXIMIZE
: WM_MDIRESTORE
,
913 (WPARAM
)GetHwnd(), 0);
917 void wxMDIChildFrame::Restore()
919 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
920 if ( parent
&& parent
->GetClientWindow() )
922 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIRESTORE
,
923 (WPARAM
) GetHwnd(), 0);
927 void wxMDIChildFrame::Activate()
929 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
930 if ( parent
&& parent
->GetClientWindow() )
932 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIACTIVATE
,
933 (WPARAM
) GetHwnd(), 0);
937 // ---------------------------------------------------------------------------
938 // MDI window proc and message handlers
939 // ---------------------------------------------------------------------------
941 WXLRESULT
wxMDIChildFrame::MSWWindowProc(WXUINT message
,
946 bool processed
= false;
954 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
957 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
961 case WM_GETMINMAXINFO
:
962 processed
= HandleGetMinMaxInfo((MINMAXINFO
*)lParam
);
968 WXHWND hwndAct
, hwndDeact
;
969 UnpackMDIActivate(wParam
, lParam
, &act
, &hwndAct
, &hwndDeact
);
971 processed
= HandleMDIActivate(act
, hwndAct
, hwndDeact
);
976 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
977 // scrollbars if necessary
982 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
984 MSWDefWindowProc(message
, wParam
, lParam
);
988 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
989 // the message (the base class version does not)
990 return MSWDefWindowProc(message
, wParam
, lParam
);
992 case WM_WINDOWPOSCHANGING
:
993 processed
= HandleWindowPosChanging((LPWINDOWPOS
)lParam
);
998 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
1003 bool wxMDIChildFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND hwnd
)
1005 // In case it's e.g. a toolbar.
1008 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
1010 return win
->MSWCommand(cmd
, id
);
1013 if (wxCurrentPopupMenu
)
1015 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
1016 wxCurrentPopupMenu
= NULL
;
1017 if (popupMenu
->MSWCommand(cmd
, id
))
1022 if (GetMenuBar() && GetMenuBar()->FindItem(id
))
1024 processed
= ProcessCommand(id
);
1034 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate
),
1038 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
1040 HMENU menuToSet
= 0;
1044 if ( m_hWnd
== hwndAct
)
1047 parent
->m_currentChild
= this;
1049 HMENU child_menu
= (HMENU
)GetWinMenu();
1052 parent
->m_parentFrameActive
= false;
1054 menuToSet
= child_menu
;
1057 else if ( m_hWnd
== hwndDeact
)
1059 wxASSERT_MSG( parent
->m_currentChild
== this,
1060 wxT("can't deactivate MDI child which wasn't active!") );
1063 parent
->m_currentChild
= NULL
;
1065 HMENU parent_menu
= (HMENU
)parent
->GetWinMenu();
1067 // activate the the parent menu only when there is no other child
1068 // that has been activated
1069 if ( parent_menu
&& !hwndAct
)
1071 parent
->m_parentFrameActive
= true;
1073 menuToSet
= parent_menu
;
1078 // we have nothing to do with it
1084 MDISetMenu(parent
->GetClientWindow(),
1085 menuToSet
, GetMDIWindowMenu(parent
));
1088 wxActivateEvent
event(wxEVT_ACTIVATE
, activated
, m_windowId
);
1089 event
.SetEventObject( this );
1091 ResetWindowStyle((void *)NULL
);
1093 return GetEventHandler()->ProcessEvent(event
);
1096 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos
)
1098 WINDOWPOS
*lpPos
= (WINDOWPOS
*)pos
;
1100 if (!(lpPos
->flags
& SWP_NOSIZE
))
1103 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1104 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1105 if (ResetWindowStyle((void *) & rectClient
) && (dwStyle
& WS_MAXIMIZE
))
1107 ::AdjustWindowRectEx(&rectClient
, dwStyle
, false, dwExStyle
);
1108 lpPos
->x
= rectClient
.left
;
1109 lpPos
->y
= rectClient
.top
;
1110 lpPos
->cx
= rectClient
.right
- rectClient
.left
;
1111 lpPos
->cy
= rectClient
.bottom
- rectClient
.top
;
1114 wxMDIParentFrame
* pFrameWnd
= (wxMDIParentFrame
*)GetParent();
1115 if (pFrameWnd
&& pFrameWnd
->GetToolBar() && pFrameWnd
->GetToolBar()->IsShown())
1117 pFrameWnd
->GetToolBar()->Refresh();
1125 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo
)
1127 MINMAXINFO
*info
= (MINMAXINFO
*)mmInfo
;
1129 // let the default window proc calculate the size of MDI children
1130 // frames because it is based on the size of the MDI client window,
1131 // not on the values specified in wxWindow m_max variables
1132 bool processed
= MSWDefWindowProc(WM_GETMINMAXINFO
, 0, (LPARAM
)mmInfo
) != 0;
1134 int minWidth
= GetMinWidth(),
1135 minHeight
= GetMinHeight();
1137 // but allow GetSizeHints() to set the min size
1138 if ( minWidth
!= wxDefaultCoord
)
1140 info
->ptMinTrackSize
.x
= minWidth
;
1145 if ( minHeight
!= wxDefaultCoord
)
1147 info
->ptMinTrackSize
.y
= minHeight
;
1155 // ---------------------------------------------------------------------------
1156 // MDI specific message translation/preprocessing
1157 // ---------------------------------------------------------------------------
1159 WXLRESULT
wxMDIChildFrame::MSWDefWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1161 return DefMDIChildProc(GetHwnd(),
1162 (UINT
)message
, (WPARAM
)wParam
, (LPARAM
)lParam
);
1165 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG
* msg
)
1167 // we must pass the parent frame to ::TranslateAccelerator(), otherwise it
1168 // doesn't do its job correctly for MDI child menus
1169 return MSWDoTranslateMessage((wxMDIChildFrame
*)GetParent(), msg
);
1172 // ---------------------------------------------------------------------------
1174 // ---------------------------------------------------------------------------
1176 void wxMDIChildFrame::MSWDestroyWindow()
1178 invalidHandle
= GetHwnd();
1180 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
1182 // Must make sure this handle is invalidated (set to NULL) since all sorts
1183 // of things could happen after the child client is destroyed, but before
1184 // the wxFrame is destroyed.
1186 HWND oldHandle
= (HWND
)GetHWND();
1187 SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIDESTROY
,
1188 (WPARAM
)oldHandle
, 0);
1190 if (parent
->GetActiveChild() == (wxMDIChildFrame
*) NULL
)
1191 ResetWindowStyle((void*) NULL
);
1197 ::DestroyMenu((HMENU
) m_hMenu
);
1200 wxRemoveHandleAssociation(this);
1204 // Change the client window's extended style so we don't get a client edge
1205 // style when a child is maximised (a double border looks silly.)
1206 bool wxMDIChildFrame::ResetWindowStyle(void *vrect
)
1208 RECT
*rect
= (RECT
*)vrect
;
1209 wxMDIParentFrame
* pFrameWnd
= (wxMDIParentFrame
*)GetParent();
1210 wxMDIChildFrame
* pChild
= pFrameWnd
->GetActiveChild();
1212 if (!pChild
|| (pChild
== this))
1214 HWND hwndClient
= GetWinHwnd(pFrameWnd
->GetClientWindow());
1215 DWORD dwStyle
= ::GetWindowLong(hwndClient
, GWL_EXSTYLE
);
1217 // we want to test whether there is a maximized child, so just set
1218 // dwThisStyle to 0 if there is no child at all
1219 DWORD dwThisStyle
= pChild
1220 ? ::GetWindowLong(GetWinHwnd(pChild
), GWL_STYLE
) : 0;
1221 DWORD dwNewStyle
= dwStyle
;
1222 if ( dwThisStyle
& WS_MAXIMIZE
)
1223 dwNewStyle
&= ~(WS_EX_CLIENTEDGE
);
1225 dwNewStyle
|= WS_EX_CLIENTEDGE
;
1227 if (dwStyle
!= dwNewStyle
)
1229 // force update of everything
1230 ::RedrawWindow(hwndClient
, NULL
, NULL
,
1231 RDW_INVALIDATE
| RDW_ALLCHILDREN
);
1232 ::SetWindowLong(hwndClient
, GWL_EXSTYLE
, dwNewStyle
);
1233 ::SetWindowPos(hwndClient
, NULL
, 0, 0, 0, 0,
1234 SWP_FRAMECHANGED
| SWP_NOACTIVATE
|
1235 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1238 ::GetClientRect(hwndClient
, rect
);
1247 // ===========================================================================
1248 // wxMDIClientWindow: the window of predefined (by Windows) class which
1249 // contains the child frames
1250 // ===========================================================================
1252 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
1254 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1256 CLIENTCREATESTRUCT ccs
;
1257 m_windowStyle
= style
;
1260 ccs
.hWindowMenu
= GetMDIWindowMenu(parent
);
1261 ccs
.idFirstChild
= wxFIRST_MDI_CHILD
;
1263 DWORD msStyle
= MDIS_ALLCHILDSTYLES
| WS_VISIBLE
| WS_CHILD
|
1264 WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
;
1266 if ( style
& wxHSCROLL
)
1267 msStyle
|= WS_HSCROLL
;
1268 if ( style
& wxVSCROLL
)
1269 msStyle
|= WS_VSCROLL
;
1271 DWORD exStyle
= WS_EX_CLIENTEDGE
;
1273 wxWindowCreationHook
hook(this);
1274 m_hWnd
= (WXHWND
)::CreateWindowEx
1284 (LPSTR
)(LPCLIENTCREATESTRUCT
)&ccs
);
1287 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1292 SubclassWin(m_hWnd
);
1297 // Explicitly call default scroll behaviour
1298 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)
1300 // Note: for client windows, the scroll position is not set in
1301 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1302 // scroll position we're at.
1303 // This makes it hard to paint patterns or bitmaps in the background,
1304 // and have the client area scrollable as well.
1306 if ( event
.GetOrientation() == wxHORIZONTAL
)
1307 m_scrollX
= event
.GetPosition(); // Always returns zero!
1309 m_scrollY
= event
.GetPosition(); // Always returns zero!
1314 void wxMDIClientWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1316 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1317 // client area, you can end up with a non-refreshed portion in the client window
1318 // (see OGL studio sample). So check if the position is changed and if so,
1319 // redraw the MDI child frames.
1321 const wxPoint oldPos
= GetPosition();
1323 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
| wxSIZE_FORCE
);
1325 const wxPoint newPos
= GetPosition();
1327 if ((newPos
.x
!= oldPos
.x
) || (newPos
.y
!= oldPos
.y
))
1331 wxWindowList::compatibility_iterator node
= GetParent()->GetChildren().GetFirst();
1334 wxWindow
*child
= node
->GetData();
1335 if (child
->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
1337 ::RedrawWindow(GetHwndOf(child
),
1344 node
= node
->GetNext();
1350 void wxMDIChildFrame::OnIdle(wxIdleEvent
& event
)
1352 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1353 // in flicker e.g. when the frame contained controls with non-trivial
1354 // layout. Since 2.5.3, the frame is created hidden as all other top level
1355 // windows. In order to maintain backward compatibility, the frame is shown
1356 // in OnIdle, unless Show(false) was called by the programmer before.
1357 if ( m_needsInitialShow
)
1362 // MDI child frames get their WM_SIZE when they're constructed but at this
1363 // moment they don't have any children yet so all child windows will be
1364 // positioned incorrectly when they are added later - to fix this, we
1365 // generate an artificial size event here
1366 if ( m_needsResize
)
1368 m_needsResize
= false; // avoid any possibility of recursion
1376 // ---------------------------------------------------------------------------
1377 // non member functions
1378 // ---------------------------------------------------------------------------
1380 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
)
1382 if ( hmenuFrame
|| hmenuWindow
)
1384 if ( !::SendMessage(GetWinHwnd(win
),
1387 (LPARAM
)hmenuWindow
) )
1389 wxLogLastError(_T("SendMessage(WM_MDISETMENU)"));
1393 // update menu bar of the parent window
1394 wxWindow
*parent
= win
->GetParent();
1395 wxCHECK_RET( parent
, wxT("MDI client without parent frame? weird...") );
1397 ::SendMessage(GetWinHwnd(win
), WM_MDIREFRESHMENU
, 0, 0L);
1399 ::DrawMenuBar(GetWinHwnd(parent
));
1402 static void InsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
)
1404 // Try to insert Window menu in front of Help, otherwise append it.
1405 HMENU hmenu
= (HMENU
)menu
;
1409 int N
= GetMenuItemCount(hmenu
);
1410 bool success
= false;
1411 for ( int i
= 0; i
< N
; i
++ )
1414 int chars
= GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
);
1417 wxLogLastError(wxT("GetMenuString"));
1422 wxString
strBuf(buf
);
1423 if ( wxStripMenuCodes(strBuf
) == wxGetStockLabel(wxID_HELP
,false) )
1426 ::InsertMenu(hmenu
, i
, MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1427 (UINT
)subMenu
, _("&Window"));
1434 ::AppendMenu(hmenu
, MF_POPUP
, (UINT
)subMenu
, _("&Window"));
1438 MDISetMenu(win
, hmenu
, subMenu
);
1441 static void RemoveWindowMenu(wxWindow
*win
, WXHMENU menu
)
1443 HMENU hMenu
= (HMENU
)menu
;
1449 int N
= ::GetMenuItemCount(hMenu
);
1450 for ( int i
= 0; i
< N
; i
++ )
1452 if ( !::GetMenuString(hMenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1454 // Ignore successful read of menu string with length 0 which
1455 // occurs, for example, for a maximized MDI childs system menu
1456 if ( ::GetLastError() != 0 )
1458 wxLogLastError(wxT("GetMenuString"));
1464 if ( wxStrcmp(buf
, _("&Window")) == 0 )
1466 if ( !::RemoveMenu(hMenu
, i
, MF_BYPOSITION
) )
1468 wxLogLastError(wxT("RemoveMenu"));
1478 // we don't change the windows menu, but we update the main one
1479 MDISetMenu(win
, hMenu
, NULL
);
1483 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
1484 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
)
1487 *hwndAct
= (WXHWND
)lParam
;
1488 *hwndDeact
= (WXHWND
)wParam
;
1491 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)