1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/mdi.cpp
3 // Purpose: MDI classes for wxMSW
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin on 2008-11-04 to use the base classes
8 // Copyright: (c) 1998 Julian Smart
9 // (c) 2008-2009 Vadim Zeitlin
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ===========================================================================
15 // ===========================================================================
17 // ---------------------------------------------------------------------------
19 // ---------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
28 #if wxUSE_MDI && !defined(__WXUNIVERSAL__)
37 #include "wx/dialog.h"
38 #include "wx/statusbr.h"
39 #include "wx/settings.h"
42 #include "wx/toolbar.h"
45 #include "wx/stockitem.h"
46 #include "wx/msw/private.h"
50 // ---------------------------------------------------------------------------
52 // ---------------------------------------------------------------------------
54 extern wxMenu
*wxCurrentPopupMenu
;
56 extern void wxRemoveHandleAssociation(wxWindow
*win
);
58 // ---------------------------------------------------------------------------
60 // ---------------------------------------------------------------------------
62 static const int IDM_WINDOWTILEHOR
= 4001;
63 static const int IDM_WINDOWCASCADE
= 4002;
64 static const int IDM_WINDOWICONS
= 4003;
65 static const int IDM_WINDOWNEXT
= 4004;
66 static const int IDM_WINDOWTILEVERT
= 4005;
67 static const int IDM_WINDOWPREV
= 4006;
69 // This range gives a maximum of 500 MDI children. Should be enough :-)
70 static const int wxFIRST_MDI_CHILD
= 4100;
71 static const int wxLAST_MDI_CHILD
= 4600;
73 // ---------------------------------------------------------------------------
75 // ---------------------------------------------------------------------------
77 // set the MDI menus (by sending the WM_MDISETMENU message) and update the menu
78 // of the parent of win (which is supposed to be the MDI client window)
79 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
);
81 // insert the window menu (subMenu) into menu just before "Help" submenu or at
82 // the very end if not found
83 static void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
);
85 // Remove the window menu
86 static void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU menu
);
88 // is this an id of an MDI child?
89 inline bool IsMdiCommandId(int id
)
91 return (id
>= wxFIRST_MDI_CHILD
) && (id
<= wxLAST_MDI_CHILD
);
94 // unpack the parameters of WM_MDIACTIVATE message
95 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
96 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
);
98 // return the HMENU of the MDI menu
100 // this function works correctly even when we don't have a window menu and just
102 static inline HMENU
GetMDIWindowMenu(wxMDIParentFrame
*frame
)
104 wxMenu
*menu
= frame
->GetWindowMenu();
105 return menu ?
GetHmenuOf(menu
) : 0;
108 // ===========================================================================
110 // ===========================================================================
112 // ---------------------------------------------------------------------------
114 // ---------------------------------------------------------------------------
116 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxFrame
)
117 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxFrame
)
118 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxWindow
)
120 BEGIN_EVENT_TABLE(wxMDIParentFrame
, wxFrame
)
121 EVT_SIZE(wxMDIParentFrame
::OnSize
)
122 EVT_ICONIZE(wxMDIParentFrame
::OnIconized
)
123 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame
::OnSysColourChanged
)
126 BEGIN_EVENT_TABLE(wxMDIChildFrame
, wxFrame
)
127 EVT_IDLE(wxMDIChildFrame
::OnIdle
)
130 BEGIN_EVENT_TABLE(wxMDIClientWindow
, wxWindow
)
131 EVT_SCROLL(wxMDIClientWindow
::OnScroll
)
134 // ===========================================================================
135 // wxMDIParentFrame: the frame which contains the client window which manages
137 // ===========================================================================
139 wxMDIParentFrame
::wxMDIParentFrame()
141 m_parentFrameActive
= true;
144 bool wxMDIParentFrame
::Create(wxWindow
*parent
,
146 const wxString
& title
,
150 const wxString
& name
)
152 // this style can be used to prevent a window from having the standard MDI
154 if ( !(style
& wxFRAME_NO_WINDOW_MENU
) )
156 // normal case: we have the window menu, so construct it
157 m_windowMenu
= new wxMenu
;
159 m_windowMenu
->Append(IDM_WINDOWCASCADE
, _("&Cascade"));
160 m_windowMenu
->Append(IDM_WINDOWTILEHOR
, _("Tile &Horizontally"));
161 m_windowMenu
->Append(IDM_WINDOWTILEVERT
, _("Tile &Vertically"));
162 m_windowMenu
->AppendSeparator();
163 m_windowMenu
->Append(IDM_WINDOWICONS
, _("&Arrange Icons"));
164 m_windowMenu
->Append(IDM_WINDOWNEXT
, _("&Next"));
165 m_windowMenu
->Append(IDM_WINDOWPREV
, _("&Previous"));
168 m_parentFrameActive
= true;
171 wxTopLevelWindows
.Append(this);
174 m_windowStyle
= style
;
177 parent
->AddChild(this);
179 if ( id
!= wxID_ANY
)
182 m_windowId
= NewControlId();
185 WXDWORD msflags
= MSWGetCreateWindowFlags(&exflags
);
186 msflags
&= ~WS_VSCROLL
;
187 msflags
&= ~WS_HSCROLL
;
189 if ( !wxWindow
::MSWCreate(wxApp
::GetRegisteredClassName(_T("wxMDIFrame")),
198 SetOwnBackgroundColour(wxSystemSettings
::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
200 // unlike (almost?) all other windows, frames are created hidden
206 wxMDIParentFrame
::~wxMDIParentFrame()
208 // see comment in ~wxMDIChildFrame
210 m_frameToolBar
= NULL
;
213 m_frameStatusBar
= NULL
;
214 #endif // wxUSE_STATUSBAR
218 // the MDI frame menubar is not automatically deleted by Windows unlike for
221 ::DestroyMenu((HMENU
)m_hMenu
);
223 if ( m_clientWindow
)
225 if ( m_clientWindow
->MSWGetOldWndProc() )
226 m_clientWindow
->UnsubclassWin();
228 m_clientWindow
->SetHWND(0);
229 delete m_clientWindow
;
233 // ----------------------------------------------------------------------------
234 // wxMDIParentFrame child management
235 // ----------------------------------------------------------------------------
237 int wxMDIParentFrame
::GetChildFramesCount() const
240 for ( wxWindowList
::const_iterator i
= GetChildren().begin();
241 i
!= GetChildren().end();
244 if ( wxDynamicCast(*i
, wxMDIChildFrame
) )
251 void wxMDIParentFrame
::AddMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
253 switch ( GetChildFramesCount() )
256 // first MDI child was just added, we need to insert the window
257 // menu now if we have it
260 // and disable the items which can't be used until we have more
262 UpdateWindowMenu(false);
266 // second MDI child was added, enable the menu items which were
267 // disabled because they didn't make sense for a single window
268 UpdateWindowMenu(true);
273 void wxMDIParentFrame
::RemoveMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
275 switch ( GetChildFramesCount() )
278 // last MDI child is being removed, remove the now unnecessary
282 // there is no need to call UpdateWindowMenu(true) here so this is
283 // not quite symmetric to AddMDIChild() above
287 // only one MDI child is going to remain, disable the menu commands
288 // which don't make sense for a single child window
289 UpdateWindowMenu(false);
294 // ----------------------------------------------------------------------------
295 // wxMDIParentFrame window menu handling
296 // ----------------------------------------------------------------------------
298 void wxMDIParentFrame
::AddWindowMenu()
301 MDIInsertWindowMenu(GetClientWindow(), m_hMenu
, GetMDIWindowMenu(this));
304 void wxMDIParentFrame
::RemoveWindowMenu()
307 MDIRemoveWindowMenu(GetClientWindow(), m_hMenu
);
310 void wxMDIParentFrame
::UpdateWindowMenu(bool enable
)
314 m_windowMenu
->Enable(IDM_WINDOWNEXT
, enable
);
315 m_windowMenu
->Enable(IDM_WINDOWPREV
, enable
);
319 #if wxUSE_MENUS_NATIVE
321 void wxMDIParentFrame
::InternalSetMenuBar()
323 m_parentFrameActive
= true;
325 if ( GetActiveChild() )
329 else // we don't have any MDI children yet
331 // wait until we do to add the window menu but do set the main menu for
332 // now (this is done by AddWindowMenu() as a side effect)
333 MDISetMenu(GetClientWindow(), (HMENU
)m_hMenu
, NULL
);
337 #endif // wxUSE_MENUS_NATIVE
339 void wxMDIParentFrame
::SetWindowMenu(wxMenu
* menu
)
341 // notice that Remove/AddWindowMenu() are safe to call even when
342 // m_windowMenu is NULL
352 void wxMDIParentFrame
::DoMenuUpdates(wxMenu
* menu
)
354 wxMDIChildFrame
*child
= GetActiveChild();
357 wxEvtHandler
* source
= child
->GetEventHandler();
358 wxMenuBar
* bar
= child
->GetMenuBar();
362 menu
->UpdateUI(source
);
368 int nCount
= bar
->GetMenuCount();
369 for (int n
= 0; n
< nCount
; n
++)
370 bar
->GetMenu(n
)->UpdateUI(source
);
376 wxFrameBase
::DoMenuUpdates(menu
);
380 const wxMenuItem
*wxMDIParentFrame
::FindItemInMenuBar(int menuId
) const
382 const wxMenuItem
*item
= wxFrame
::FindItemInMenuBar(menuId
);
383 if ( !item
&& m_currentChild
)
385 item
= m_currentChild
->FindItemInMenuBar(menuId
);
391 void wxMDIParentFrame
::UpdateClientSize()
393 if ( GetClientWindow() )
396 GetClientSize(&width
, &height
);
398 GetClientWindow()->SetSize(0, 0, width
, height
);
402 void wxMDIParentFrame
::OnSize(wxSizeEvent
& WXUNUSED(event
))
406 // do not call event.Skip() here, it somehow messes up MDI client window
409 void wxMDIParentFrame
::OnIconized(wxIconizeEvent
& event
)
413 if ( !event
.IsIconized() )
417 // Returns the active MDI child window
418 wxMDIChildFrame
*wxMDIParentFrame
::GetActiveChild() const
420 HWND hWnd
= (HWND
)::SendMessage(GetWinHwnd(GetClientWindow()),
421 WM_MDIGETACTIVE
, 0, 0L);
425 return (wxMDIChildFrame
*)wxFindWinFromHandle(hWnd
);
428 // Responds to colour changes, and passes event on to children.
429 void wxMDIParentFrame
::OnSysColourChanged(wxSysColourChangedEvent
& event
)
431 if ( m_clientWindow
)
433 m_clientWindow
->SetBackgroundColour(wxSystemSettings
::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
434 m_clientWindow
->Refresh();
440 WXHICON wxMDIParentFrame
::GetDefaultIcon() const
442 // we don't have any standard icons (any more)
446 // ---------------------------------------------------------------------------
448 // ---------------------------------------------------------------------------
450 void wxMDIParentFrame
::Cascade()
452 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE
, 0, 0);
455 void wxMDIParentFrame
::Tile(wxOrientation orient
)
457 wxASSERT_MSG( orient
== wxHORIZONTAL
|| orient
== wxVERTICAL
,
458 _T("invalid orientation value") );
460 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE
,
461 orient
== wxHORIZONTAL ? MDITILE_HORIZONTAL
462 : MDITILE_VERTICAL
, 0);
465 void wxMDIParentFrame
::ArrangeIcons()
467 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE
, 0, 0);
470 void wxMDIParentFrame
::ActivateNext()
472 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 0);
475 void wxMDIParentFrame
::ActivatePrevious()
477 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 1);
480 // ---------------------------------------------------------------------------
481 // the MDI parent frame window proc
482 // ---------------------------------------------------------------------------
484 WXLRESULT wxMDIParentFrame
::MSWWindowProc(WXUINT message
,
489 bool processed
= false;
495 WXWORD state
, minimized
;
497 UnpackActivate(wParam
, lParam
, &state
, &minimized
, &hwnd
);
499 processed
= HandleActivate(state
, minimized
!= 0, hwnd
);
507 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
509 (void)HandleCommand(id
, cmd
, hwnd
);
511 // even if the frame didn't process it, there is no need to try it
512 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
513 // so pretend we processed the message anyhow
517 // always pass this message DefFrameProc(), otherwise MDI menu
518 // commands (and sys commands - more surprisingly!) won't work
519 MSWDefWindowProc(message
, wParam
, lParam
);
523 m_clientWindow
= OnCreateClient();
524 // Uses own style for client style
525 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
527 wxLogMessage(_("Failed to create MDI parent frame."));
538 // we erase background ourselves
543 // though we don't (usually) resize the MDI client to exactly fit the
544 // client area we need to pass this one to DefFrameProc to allow the children to show
549 rc
= wxFrame
::MSWWindowProc(message
, wParam
, lParam
);
554 bool wxMDIParentFrame
::HandleActivate(int state
, bool minimized
, WXHWND activate
)
556 bool processed
= false;
558 if ( wxWindow
::HandleActivate(state
, minimized
, activate
) )
564 // If this window is an MDI parent, we must also send an OnActivate message
565 // to the current child.
566 if ( (m_currentChild
!= NULL
) &&
567 ((state
== WA_ACTIVE
) || (state
== WA_CLICKACTIVE
)) )
569 wxActivateEvent
event(wxEVT_ACTIVATE
, true, m_currentChild
->GetId());
570 event
.SetEventObject( m_currentChild
);
571 if ( m_currentChild
->HandleWindowEvent(event
) )
578 bool wxMDIParentFrame
::HandleCommand(WXWORD id_
, WXWORD cmd
, WXHWND hwnd
)
580 // sign extend to int from short before comparing with the other int ids
581 int id
= (signed short)id_
;
583 // In case it's e.g. a toolbar.
586 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
588 return win
->MSWCommand(cmd
, id
);
591 if (wxCurrentPopupMenu
)
593 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
594 wxCurrentPopupMenu
= NULL
;
595 if (popupMenu
->MSWCommand(cmd
, id
))
599 // is it one of standard MDI commands?
605 case IDM_WINDOWCASCADE
:
607 wParam
= MDITILE_SKIPDISABLED
;
610 case IDM_WINDOWTILEHOR
:
611 wParam
|= MDITILE_HORIZONTAL
;
614 case IDM_WINDOWTILEVERT
:
616 wParam
= MDITILE_VERTICAL
;
618 wParam
|= MDITILE_SKIPDISABLED
;
621 case IDM_WINDOWICONS
:
622 msg
= WM_MDIICONARRANGE
;
627 lParam
= 0; // next child
632 lParam
= 1; // previous child
641 ::SendMessage(GetWinHwnd(GetClientWindow()), msg
, wParam
, lParam
);
646 // FIXME VZ: what does this test do??
649 return false; // Get WndProc to call default proc
652 if ( IsMdiCommandId(id
) )
654 wxWindowList
::compatibility_iterator node
= GetChildren().GetFirst();
657 wxWindow
*child
= node
->GetData();
658 if ( child
->GetHWND() )
660 int childId
= wxGetWindowId(child
->GetHWND());
661 if ( childId
== (signed short)id
)
663 ::SendMessage( GetWinHwnd(GetClientWindow()),
665 (WPARAM
)child
->GetHWND(), 0);
669 node
= node
->GetNext();
672 else if ( m_parentFrameActive
)
674 return ProcessCommand(id
);
676 else if ( m_currentChild
)
678 return m_currentChild
->HandleCommand(id
, cmd
, hwnd
);
682 // this shouldn't happen because it means that our messages are being
683 // lost (they're not sent to the parent frame nor to the children)
684 wxFAIL_MSG(wxT("MDI parent frame is not active, yet there is no active MDI child?"));
690 WXLRESULT wxMDIParentFrame
::MSWDefWindowProc(WXUINT message
,
695 if ( GetClientWindow() )
696 clientWnd
= GetClientWindow()->GetHWND();
700 return DefFrameProc(GetHwnd(), (HWND
)clientWnd
, message
, wParam
, lParam
);
703 bool wxMDIParentFrame
::MSWTranslateMessage(WXMSG
* msg
)
705 MSG
*pMsg
= (MSG
*)msg
;
707 // first let the current child get it
708 if ( m_currentChild
&& m_currentChild
->GetHWND() &&
709 m_currentChild
->MSWTranslateMessage(msg
) )
714 // then try out accel table (will also check the menu accels)
715 if ( wxFrame
::MSWTranslateMessage(msg
) )
720 // finally, check for MDI specific built in accel keys
721 if ( pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
723 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg
))
730 // ===========================================================================
732 // ===========================================================================
734 void wxMDIChildFrame
::Init()
736 m_needsResize
= true;
737 m_needsInitialShow
= true;
740 bool wxMDIChildFrame
::Create(wxMDIParentFrame
*parent
,
742 const wxString
& title
,
746 const wxString
& name
)
748 m_mdiParent
= parent
;
752 if ( id
!= wxID_ANY
)
755 m_windowId
= NewControlId();
759 parent
->AddChild(this);
770 wxApp
::GetRegisteredClassName(_T("wxMDIChildFrame"), COLOR_WINDOW
);
771 if ( !(style
& wxFULL_REPAINT_ON_RESIZE
) )
772 className
+= wxApp
::GetNoRedrawClassSuffix();
774 mcs
.szClass
= className
.wx_str();
775 mcs
.szTitle
= title
.wx_str();
776 mcs
.hOwner
= wxGetInstance();
777 if (x
!= wxDefaultCoord
)
780 mcs
.x
= CW_USEDEFAULT
;
782 if (y
!= wxDefaultCoord
)
785 mcs
.y
= CW_USEDEFAULT
;
787 if (width
!= wxDefaultCoord
)
790 mcs
.cx
= CW_USEDEFAULT
;
792 if (height
!= wxDefaultCoord
)
795 mcs
.cy
= CW_USEDEFAULT
;
797 DWORD msflags
= WS_OVERLAPPED
| WS_CLIPCHILDREN
;
798 if (style
& wxMINIMIZE_BOX
)
799 msflags
|= WS_MINIMIZEBOX
;
800 if (style
& wxMAXIMIZE_BOX
)
801 msflags
|= WS_MAXIMIZEBOX
;
802 if (style
& wxRESIZE_BORDER
)
803 msflags
|= WS_THICKFRAME
;
804 if (style
& wxSYSTEM_MENU
)
805 msflags
|= WS_SYSMENU
;
806 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
807 msflags
|= WS_MINIMIZE
;
808 if (style
& wxMAXIMIZE
)
809 msflags
|= WS_MAXIMIZE
;
810 if (style
& wxCAPTION
)
811 msflags
|= WS_CAPTION
;
817 wxWindowCreationHook
hook(this);
819 m_hWnd
= (WXHWND
)::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
820 WM_MDICREATE
, 0, (LPARAM
)&mcs
);
824 wxLogLastError(_T("WM_MDICREATE"));
830 parent
->AddMDIChild(this);
835 wxMDIChildFrame
::~wxMDIChildFrame()
837 // if we hadn't been created, there is nothing to destroy
841 GetMDIParent()->RemoveMDIChild(this);
843 // will be destroyed by DestroyChildren() but reset them before calling it
844 // to avoid using dangling pointers if a callback comes in the meanwhile
846 m_frameToolBar
= NULL
;
849 m_frameStatusBar
= NULL
;
850 #endif // wxUSE_STATUSBAR
854 MDIRemoveWindowMenu(NULL
, m_hMenu
);
859 bool wxMDIChildFrame
::Show(bool show
)
861 m_needsInitialShow
= false;
863 if (!wxFrame
::Show(show
))
866 // KH: Without this call, new MDI children do not become active.
867 // This was added here after the same BringWindowToTop call was
868 // removed from wxTopLevelWindow::Show (November 2005)
870 ::BringWindowToTop(GetHwnd());
872 // we need to refresh the MDI frame window menu to include (or exclude if
873 // we've been hidden) this frame
874 wxMDIParentFrame
* const parent
= GetMDIParent();
875 MDISetMenu(parent
->GetClientWindow(), NULL
, NULL
);
880 // Set the client size (i.e. leave the calculation of borders etc.
882 void wxMDIChildFrame
::DoSetClientSize(int width
, int height
)
884 HWND hWnd
= GetHwnd();
887 ::GetClientRect(hWnd
, &rect
);
890 GetWindowRect(hWnd
, &rect2
);
892 // Find the difference between the entire window (title bar and all)
893 // and the client area; add this to the new client size to move the
895 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
896 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
899 if (GetStatusBar() && GetStatusBar()->IsShown())
902 GetStatusBar()->GetSize(&sx
, &sy
);
905 #endif // wxUSE_STATUSBAR
908 point
.x
= rect2
.left
;
911 // If there's an MDI parent, must subtract the parent's top left corner
912 // since MoveWindow moves relative to the parent
913 wxMDIParentFrame
* const mdiParent
= GetMDIParent();
914 ::ScreenToClient(GetHwndOf(mdiParent
->GetClientWindow()), &point
);
916 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)true);
918 wxSize
size(width
, height
);
919 wxSizeEvent
event(size
, m_windowId
);
920 event
.SetEventObject( this );
921 HandleWindowEvent(event
);
924 // Unlike other wxTopLevelWindowBase, the mdi child's "GetPosition" is not the
925 // same as its GetScreenPosition
926 void wxMDIChildFrame
::DoGetScreenPosition(int *x
, int *y
) const
928 HWND hWnd
= GetHwnd();
931 ::GetWindowRect(hWnd
, &rect
);
939 void wxMDIChildFrame
::DoGetPosition(int *x
, int *y
) const
942 GetWindowRect(GetHwnd(), &rect
);
947 // Since we now have the absolute screen coords,
948 // if there's a parent we must subtract its top left corner
949 wxMDIParentFrame
* const mdiParent
= GetMDIParent();
950 ::ScreenToClient(GetHwndOf(mdiParent
->GetClientWindow()), &point
);
958 void wxMDIChildFrame
::InternalSetMenuBar()
960 wxMDIParentFrame
* const parent
= GetMDIParent();
962 MDIInsertWindowMenu(parent
->GetClientWindow(),
963 m_hMenu
, GetMDIWindowMenu(parent
));
965 parent
->m_parentFrameActive
= false;
968 void wxMDIChildFrame
::DetachMenuBar()
970 MDIRemoveWindowMenu(NULL
, m_hMenu
);
971 wxFrame
::DetachMenuBar();
974 WXHICON wxMDIChildFrame
::GetDefaultIcon() const
976 // we don't have any standard icons (any more)
980 // ---------------------------------------------------------------------------
982 // ---------------------------------------------------------------------------
984 void wxMDIChildFrame
::Maximize(bool maximize
)
986 wxMDIParentFrame
* const parent
= GetMDIParent();
987 if ( parent
&& parent
->GetClientWindow() )
989 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
990 maximize ? WM_MDIMAXIMIZE
: WM_MDIRESTORE
,
991 (WPARAM
)GetHwnd(), 0);
995 void wxMDIChildFrame
::Restore()
997 wxMDIParentFrame
* const parent
= GetMDIParent();
998 if ( parent
&& parent
->GetClientWindow() )
1000 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIRESTORE
,
1001 (WPARAM
) GetHwnd(), 0);
1005 void wxMDIChildFrame
::Activate()
1007 wxMDIParentFrame
* const parent
= GetMDIParent();
1008 if ( parent
&& parent
->GetClientWindow() )
1010 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIACTIVATE
,
1011 (WPARAM
) GetHwnd(), 0);
1015 // ---------------------------------------------------------------------------
1016 // MDI window proc and message handlers
1017 // ---------------------------------------------------------------------------
1019 WXLRESULT wxMDIChildFrame
::MSWWindowProc(WXUINT message
,
1024 bool processed
= false;
1032 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
1035 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
1039 case WM_GETMINMAXINFO
:
1040 processed
= HandleGetMinMaxInfo((MINMAXINFO
*)lParam
);
1043 case WM_MDIACTIVATE
:
1046 WXHWND hwndAct
, hwndDeact
;
1047 UnpackMDIActivate(wParam
, lParam
, &act
, &hwndAct
, &hwndDeact
);
1049 processed
= HandleMDIActivate(act
, hwndAct
, hwndDeact
);
1054 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
1055 // scrollbars if necessary
1060 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
1062 MSWDefWindowProc(message
, wParam
, lParam
);
1066 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
1067 // the message (the base class version does not)
1068 return MSWDefWindowProc(message
, wParam
, lParam
);
1070 case WM_WINDOWPOSCHANGING
:
1071 processed
= HandleWindowPosChanging((LPWINDOWPOS
)lParam
);
1076 rc
= wxFrame
::MSWWindowProc(message
, wParam
, lParam
);
1081 bool wxMDIChildFrame
::HandleCommand(WXWORD id_
, WXWORD cmd
, WXHWND hwnd
)
1083 // sign extend to int from short before comparing with the other int ids
1084 int id
= (signed short)id_
;
1086 // In case it's e.g. a toolbar.
1089 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
1091 return win
->MSWCommand(cmd
, id
);
1094 if (wxCurrentPopupMenu
)
1096 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
1097 wxCurrentPopupMenu
= NULL
;
1098 if (popupMenu
->MSWCommand(cmd
, id
))
1103 if (GetMenuBar() && GetMenuBar()->FindItem(id
))
1105 processed
= ProcessCommand(id
);
1115 bool wxMDIChildFrame
::HandleMDIActivate(long WXUNUSED(activate
),
1119 wxMDIParentFrame
* const parent
= GetMDIParent();
1121 HMENU menuToSet
= 0;
1125 if ( m_hWnd
== hwndAct
)
1128 parent
->m_currentChild
= this;
1130 HMENU child_menu
= (HMENU
)GetWinMenu();
1133 parent
->m_parentFrameActive
= false;
1135 menuToSet
= child_menu
;
1138 else if ( m_hWnd
== hwndDeact
)
1140 wxASSERT_MSG( parent
->m_currentChild
== this,
1141 wxT("can't deactivate MDI child which wasn't active!") );
1144 parent
->m_currentChild
= NULL
;
1146 HMENU parent_menu
= (HMENU
)parent
->GetWinMenu();
1148 // activate the the parent menu only when there is no other child
1149 // that has been activated
1150 if ( parent_menu
&& !hwndAct
)
1152 parent
->m_parentFrameActive
= true;
1154 menuToSet
= parent_menu
;
1159 // we have nothing to do with it
1165 MDISetMenu(parent
->GetClientWindow(),
1166 menuToSet
, GetMDIWindowMenu(parent
));
1169 wxActivateEvent
event(wxEVT_ACTIVATE
, activated
, m_windowId
);
1170 event
.SetEventObject( this );
1172 ResetWindowStyle(NULL
);
1174 return HandleWindowEvent(event
);
1177 bool wxMDIChildFrame
::HandleWindowPosChanging(void *pos
)
1179 WINDOWPOS
*lpPos
= (WINDOWPOS
*)pos
;
1181 if (!(lpPos
->flags
& SWP_NOSIZE
))
1184 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1185 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1186 if (ResetWindowStyle((void *) & rectClient
) && (dwStyle
& WS_MAXIMIZE
))
1188 ::AdjustWindowRectEx(&rectClient
, dwStyle
, false, dwExStyle
);
1189 lpPos
->x
= rectClient
.left
;
1190 lpPos
->y
= rectClient
.top
;
1191 lpPos
->cx
= rectClient
.right
- rectClient
.left
;
1192 lpPos
->cy
= rectClient
.bottom
- rectClient
.top
;
1199 bool wxMDIChildFrame
::HandleGetMinMaxInfo(void *mmInfo
)
1201 MINMAXINFO
*info
= (MINMAXINFO
*)mmInfo
;
1203 // let the default window proc calculate the size of MDI children
1204 // frames because it is based on the size of the MDI client window,
1205 // not on the values specified in wxWindow m_max variables
1206 bool processed
= MSWDefWindowProc(WM_GETMINMAXINFO
, 0, (LPARAM
)mmInfo
) != 0;
1208 int minWidth
= GetMinWidth(),
1209 minHeight
= GetMinHeight();
1211 // but allow GetSizeHints() to set the min size
1212 if ( minWidth
!= wxDefaultCoord
)
1214 info
->ptMinTrackSize
.x
= minWidth
;
1219 if ( minHeight
!= wxDefaultCoord
)
1221 info
->ptMinTrackSize
.y
= minHeight
;
1229 // ---------------------------------------------------------------------------
1230 // MDI specific message translation/preprocessing
1231 // ---------------------------------------------------------------------------
1233 WXLRESULT wxMDIChildFrame
::MSWDefWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1235 return DefMDIChildProc(GetHwnd(),
1236 (UINT
)message
, (WPARAM
)wParam
, (LPARAM
)lParam
);
1239 bool wxMDIChildFrame
::MSWTranslateMessage(WXMSG
* msg
)
1241 // we must pass the parent frame to ::TranslateAccelerator(), otherwise it
1242 // doesn't do its job correctly for MDI child menus
1243 return MSWDoTranslateMessage(GetMDIParent(), msg
);
1246 // ---------------------------------------------------------------------------
1248 // ---------------------------------------------------------------------------
1250 void wxMDIChildFrame
::MSWDestroyWindow()
1252 wxMDIParentFrame
* const parent
= GetMDIParent();
1254 // Must make sure this handle is invalidated (set to NULL) since all sorts
1255 // of things could happen after the child client is destroyed, but before
1256 // the wxFrame is destroyed.
1258 HWND oldHandle
= (HWND
)GetHWND();
1259 SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIDESTROY
,
1260 (WPARAM
)oldHandle
, 0);
1262 if (parent
->GetActiveChild() == NULL
)
1263 ResetWindowStyle(NULL
);
1267 ::DestroyMenu((HMENU
) m_hMenu
);
1270 wxRemoveHandleAssociation(this);
1274 // Change the client window's extended style so we don't get a client edge
1275 // style when a child is maximised (a double border looks silly.)
1276 bool wxMDIChildFrame
::ResetWindowStyle(void *vrect
)
1278 RECT
*rect
= (RECT
*)vrect
;
1279 wxMDIParentFrame
* const pFrameWnd
= GetMDIParent();
1280 wxMDIChildFrame
* pChild
= pFrameWnd
->GetActiveChild();
1282 if (!pChild
|| (pChild
== this))
1284 HWND hwndClient
= GetWinHwnd(pFrameWnd
->GetClientWindow());
1285 DWORD dwStyle
= ::GetWindowLong(hwndClient
, GWL_EXSTYLE
);
1287 // we want to test whether there is a maximized child, so just set
1288 // dwThisStyle to 0 if there is no child at all
1289 DWORD dwThisStyle
= pChild
1290 ?
::GetWindowLong(GetWinHwnd(pChild
), GWL_STYLE
) : 0;
1291 DWORD dwNewStyle
= dwStyle
;
1292 if ( dwThisStyle
& WS_MAXIMIZE
)
1293 dwNewStyle
&= ~(WS_EX_CLIENTEDGE
);
1295 dwNewStyle
|= WS_EX_CLIENTEDGE
;
1297 if (dwStyle
!= dwNewStyle
)
1299 // force update of everything
1300 ::RedrawWindow(hwndClient
, NULL
, NULL
,
1301 RDW_INVALIDATE
| RDW_ALLCHILDREN
);
1302 ::SetWindowLong(hwndClient
, GWL_EXSTYLE
, dwNewStyle
);
1303 ::SetWindowPos(hwndClient
, NULL
, 0, 0, 0, 0,
1304 SWP_FRAMECHANGED
| SWP_NOACTIVATE
|
1305 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1308 ::GetClientRect(hwndClient
, rect
);
1317 // ===========================================================================
1318 // wxMDIClientWindow: the window of predefined (by Windows) class which
1319 // contains the child frames
1320 // ===========================================================================
1322 bool wxMDIClientWindow
::CreateClient(wxMDIParentFrame
*parent
, long style
)
1324 m_backgroundColour
= wxSystemSettings
::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1326 CLIENTCREATESTRUCT ccs
;
1327 m_windowStyle
= style
;
1330 ccs
.hWindowMenu
= GetMDIWindowMenu(parent
);
1331 ccs
.idFirstChild
= wxFIRST_MDI_CHILD
;
1333 DWORD msStyle
= MDIS_ALLCHILDSTYLES
| WS_VISIBLE
| WS_CHILD
|
1334 WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
;
1336 if ( style
& wxHSCROLL
)
1337 msStyle
|= WS_HSCROLL
;
1338 if ( style
& wxVSCROLL
)
1339 msStyle
|= WS_VSCROLL
;
1341 DWORD exStyle
= WS_EX_CLIENTEDGE
;
1343 wxWindowCreationHook
hook(this);
1344 m_hWnd
= (WXHWND
)::CreateWindowEx
1354 (LPSTR
)(LPCLIENTCREATESTRUCT
)&ccs
);
1357 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1362 SubclassWin(m_hWnd
);
1367 // Explicitly call default scroll behaviour
1368 void wxMDIClientWindow
::OnScroll(wxScrollEvent
& event
)
1370 // Note: for client windows, the scroll position is not set in
1371 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1372 // scroll position we're at.
1373 // This makes it hard to paint patterns or bitmaps in the background,
1374 // and have the client area scrollable as well.
1376 if ( event
.GetOrientation() == wxHORIZONTAL
)
1377 m_scrollX
= event
.GetPosition(); // Always returns zero!
1379 m_scrollY
= event
.GetPosition(); // Always returns zero!
1384 void wxMDIClientWindow
::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1386 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1387 // client area, you can end up with a non-refreshed portion in the client window
1388 // (see OGL studio sample). So check if the position is changed and if so,
1389 // redraw the MDI child frames.
1391 const wxPoint oldPos
= GetPosition();
1393 wxWindow
::DoSetSize(x
, y
, width
, height
, sizeFlags
| wxSIZE_FORCE
);
1395 const wxPoint newPos
= GetPosition();
1397 if ((newPos
.x
!= oldPos
.x
) || (newPos
.y
!= oldPos
.y
))
1401 wxWindowList
::compatibility_iterator node
= GetParent()->GetChildren().GetFirst();
1404 wxWindow
*child
= node
->GetData();
1405 if (child
->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
1407 ::RedrawWindow(GetHwndOf(child
),
1414 node
= node
->GetNext();
1420 void wxMDIChildFrame
::OnIdle(wxIdleEvent
& event
)
1422 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1423 // in flicker e.g. when the frame contained controls with non-trivial
1424 // layout. Since 2.5.3, the frame is created hidden as all other top level
1425 // windows. In order to maintain backward compatibility, the frame is shown
1426 // in OnIdle, unless Show(false) was called by the programmer before.
1427 if ( m_needsInitialShow
)
1432 // MDI child frames get their WM_SIZE when they're constructed but at this
1433 // moment they don't have any children yet so all child windows will be
1434 // positioned incorrectly when they are added later - to fix this, we
1435 // generate an artificial size event here
1436 if ( m_needsResize
)
1438 m_needsResize
= false; // avoid any possibility of recursion
1446 // ---------------------------------------------------------------------------
1447 // non member functions
1448 // ---------------------------------------------------------------------------
1450 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
)
1452 if ( hmenuFrame
|| hmenuWindow
)
1454 if ( !::SendMessage(GetWinHwnd(win
),
1457 (LPARAM
)hmenuWindow
) )
1460 DWORD err
= ::GetLastError();
1462 wxLogApiError(_T("SendMessage(WM_MDISETMENU)"), err
);
1463 #endif // __WXDEBUG__
1467 // update menu bar of the parent window
1468 wxWindow
*parent
= win
->GetParent();
1469 wxCHECK_RET( parent
, wxT("MDI client without parent frame? weird...") );
1471 ::SendMessage(GetWinHwnd(win
), WM_MDIREFRESHMENU
, 0, 0L);
1473 ::DrawMenuBar(GetWinHwnd(parent
));
1476 static void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
)
1478 // Try to insert Window menu in front of Help, otherwise append it.
1479 HMENU hmenu
= (HMENU
)menu
;
1483 int N
= GetMenuItemCount(hmenu
);
1484 bool success
= false;
1485 for ( int i
= 0; i
< N
; i
++ )
1488 int chars
= GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
);
1491 wxLogLastError(wxT("GetMenuString"));
1496 wxString
strBuf(buf
);
1497 if ( wxStripMenuCodes(strBuf
) == wxGetStockLabel(wxID_HELP
,false) )
1500 ::InsertMenu(hmenu
, i
, MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1501 (UINT_PTR
)subMenu
, _("&Window").wx_str());
1508 ::AppendMenu(hmenu
, MF_POPUP
,
1509 (UINT_PTR
)subMenu
, _("&Window").wx_str());
1513 MDISetMenu(win
, hmenu
, subMenu
);
1516 static void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU menu
)
1518 HMENU hMenu
= (HMENU
)menu
;
1524 int N
= ::GetMenuItemCount(hMenu
);
1525 for ( int i
= 0; i
< N
; i
++ )
1527 if ( !::GetMenuString(hMenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1529 // Ignore successful read of menu string with length 0 which
1530 // occurs, for example, for a maximized MDI childs system menu
1531 if ( ::GetLastError() != 0 )
1533 wxLogLastError(wxT("GetMenuString"));
1539 if ( wxStrcmp(buf
, _("&Window")) == 0 )
1541 if ( !::RemoveMenu(hMenu
, i
, MF_BYPOSITION
) )
1543 wxLogLastError(wxT("RemoveMenu"));
1553 // we don't change the windows menu, but we update the main one
1554 MDISetMenu(win
, hMenu
, NULL
);
1558 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
1559 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
)
1562 *hwndAct
= (WXHWND
)lParam
;
1563 *hwndDeact
= (WXHWND
)wParam
;
1566 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)