1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/mdi.cpp
3 // Purpose: MDI classes for wxMSW
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_MDI && !defined(__WXUNIVERSAL__)
36 #include "wx/dialog.h"
37 #include "wx/statusbr.h"
38 #include "wx/settings.h"
41 #include "wx/toolbar.h"
44 #include "wx/stockitem.h"
45 #include "wx/msw/private.h"
49 // ---------------------------------------------------------------------------
51 // ---------------------------------------------------------------------------
53 extern wxMenu
*wxCurrentPopupMenu
;
55 extern const wxChar
*wxMDIFrameClassName
; // from app.cpp
56 extern const wxChar
*wxMDIChildFrameClassName
;
57 extern const wxChar
*wxMDIChildFrameClassNameNoRedraw
;
58 extern void wxRemoveHandleAssociation(wxWindow
*win
);
60 // ---------------------------------------------------------------------------
62 // ---------------------------------------------------------------------------
64 static const int IDM_WINDOWTILEHOR
= 4001;
65 static const int IDM_WINDOWCASCADE
= 4002;
66 static const int IDM_WINDOWICONS
= 4003;
67 static const int IDM_WINDOWNEXT
= 4004;
68 static const int IDM_WINDOWTILEVERT
= 4005;
69 static const int IDM_WINDOWPREV
= 4006;
71 // This range gives a maximum of 500 MDI children. Should be enough :-)
72 static const int wxFIRST_MDI_CHILD
= 4100;
73 static const int wxLAST_MDI_CHILD
= 4600;
75 // ---------------------------------------------------------------------------
77 // ---------------------------------------------------------------------------
79 // set the MDI menus (by sending the WM_MDISETMENU message) and update the menu
80 // of the parent of win (which is supposed to be the MDI client window)
81 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
);
83 // insert the window menu (subMenu) into menu just before "Help" submenu or at
84 // the very end if not found
85 static void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
);
87 // Remove the window menu
88 static void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU menu
);
90 // is this an id of an MDI child?
91 inline bool IsMdiCommandId(int id
)
93 return (id
>= wxFIRST_MDI_CHILD
) && (id
<= wxLAST_MDI_CHILD
);
96 // unpack the parameters of WM_MDIACTIVATE message
97 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
98 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
);
100 // return the HMENU of the MDI menu
102 // this function works correctly even when we don't have a window menu and just
104 static inline HMENU
GetMDIWindowMenu(wxMDIParentFrame
*frame
)
106 wxMenu
*menu
= frame
->GetWindowMenu();
107 return menu
? GetHmenuOf(menu
) : 0;
110 // ===========================================================================
112 // ===========================================================================
114 // ---------------------------------------------------------------------------
116 // ---------------------------------------------------------------------------
118 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxFrame
)
119 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxFrame
)
120 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxWindow
)
122 BEGIN_EVENT_TABLE(wxMDIParentFrame
, wxFrame
)
123 EVT_SIZE(wxMDIParentFrame::OnSize
)
124 EVT_ICONIZE(wxMDIParentFrame::OnIconized
)
125 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged
)
128 BEGIN_EVENT_TABLE(wxMDIChildFrame
, wxFrame
)
129 EVT_IDLE(wxMDIChildFrame::OnIdle
)
132 BEGIN_EVENT_TABLE(wxMDIClientWindow
, wxWindow
)
133 EVT_SCROLL(wxMDIClientWindow::OnScroll
)
136 // ===========================================================================
137 // wxMDIParentFrame: the frame which contains the client window which manages
139 // ===========================================================================
141 wxMDIParentFrame::wxMDIParentFrame()
143 m_clientWindow
= NULL
;
144 m_currentChild
= NULL
;
146 m_parentFrameActive
= true;
149 bool wxMDIParentFrame::Create(wxWindow
*parent
,
151 const wxString
& title
,
155 const wxString
& name
)
157 m_clientWindow
= NULL
;
158 m_currentChild
= NULL
;
160 // this style can be used to prevent a window from having the standard MDI
162 if ( style
& wxFRAME_NO_WINDOW_MENU
)
166 else // normal case: we have the window menu, so construct it
168 m_windowMenu
= new wxMenu
;
170 m_windowMenu
->Append(IDM_WINDOWCASCADE
, _("&Cascade"));
171 m_windowMenu
->Append(IDM_WINDOWTILEHOR
, _("Tile &Horizontally"));
172 m_windowMenu
->Append(IDM_WINDOWTILEVERT
, _("Tile &Vertically"));
173 m_windowMenu
->AppendSeparator();
174 m_windowMenu
->Append(IDM_WINDOWICONS
, _("&Arrange Icons"));
175 m_windowMenu
->Append(IDM_WINDOWNEXT
, _("&Next"));
176 m_windowMenu
->Append(IDM_WINDOWPREV
, _("&Previous"));
179 m_parentFrameActive
= true;
182 wxTopLevelWindows
.Append(this);
185 m_windowStyle
= style
;
188 parent
->AddChild(this);
190 if ( id
!= wxID_ANY
)
193 m_windowId
= NewControlId();
196 WXDWORD msflags
= MSWGetCreateWindowFlags(&exflags
);
197 msflags
&= ~WS_VSCROLL
;
198 msflags
&= ~WS_HSCROLL
;
200 if ( !wxWindow::MSWCreate(wxMDIFrameClassName
,
209 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
211 // unlike (almost?) all other windows, frames are created hidden
217 wxMDIParentFrame::~wxMDIParentFrame()
219 // see comment in ~wxMDIChildFrame
221 m_frameToolBar
= NULL
;
224 m_frameStatusBar
= NULL
;
225 #endif // wxUSE_STATUSBAR
231 // the MDI frame menubar is not automatically deleted by Windows unlike for
234 ::DestroyMenu((HMENU
)m_hMenu
);
236 if ( m_clientWindow
)
238 if ( m_clientWindow
->MSWGetOldWndProc() )
239 m_clientWindow
->UnsubclassWin();
241 m_clientWindow
->SetHWND(0);
242 delete m_clientWindow
;
246 // ----------------------------------------------------------------------------
247 // wxMDIParentFrame child management
248 // ----------------------------------------------------------------------------
250 int wxMDIParentFrame::GetChildFramesCount() const
253 for ( wxWindowList::const_iterator i
= GetChildren().begin();
254 i
!= GetChildren().end();
257 if ( wxDynamicCast(*i
, wxMDIChildFrame
) )
264 void wxMDIParentFrame::AddMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
266 if ( GetChildFramesCount() == 1 )
268 // first MDI child added, we need to insert the window menu now if we
274 void wxMDIParentFrame::RemoveMDIChild(wxMDIChildFrame
* WXUNUSED(child
))
276 if ( GetChildFramesCount() == 1 )
278 // last MDI child is being removed, remove the now unnecessary window
284 // ----------------------------------------------------------------------------
285 // wxMDIParentFrame window menu handling
286 // ----------------------------------------------------------------------------
288 void wxMDIParentFrame::AddWindowMenu()
291 MDIInsertWindowMenu(GetClientWindow(), m_hMenu
, GetMDIWindowMenu(this));
294 void wxMDIParentFrame::RemoveWindowMenu()
297 MDIRemoveWindowMenu(GetClientWindow(), m_hMenu
);
300 #if wxUSE_MENUS_NATIVE
302 void wxMDIParentFrame::InternalSetMenuBar()
304 m_parentFrameActive
= true;
306 if ( GetActiveChild() )
310 else // we don't have any MDI children yet
312 // wait until we do to add the window menu but do set the main menu for
313 // now (this is done by AddWindowMenu() as a side effect)
314 MDISetMenu(GetClientWindow(), (HMENU
)m_hMenu
, NULL
);
318 #endif // wxUSE_MENUS_NATIVE
320 void wxMDIParentFrame::SetWindowMenu(wxMenu
* menu
)
338 void wxMDIParentFrame::DoMenuUpdates(wxMenu
* menu
)
340 wxMDIChildFrame
*child
= GetActiveChild();
343 wxEvtHandler
* source
= child
->GetEventHandler();
344 wxMenuBar
* bar
= child
->GetMenuBar();
348 menu
->UpdateUI(source
);
354 int nCount
= bar
->GetMenuCount();
355 for (int n
= 0; n
< nCount
; n
++)
356 bar
->GetMenu(n
)->UpdateUI(source
);
362 wxFrameBase::DoMenuUpdates(menu
);
366 const wxMenuItem
*wxMDIParentFrame::FindItemInMenuBar(int menuId
) const
368 const wxMenuItem
*item
= wxFrame::FindItemInMenuBar(menuId
);
369 if ( !item
&& m_currentChild
)
371 item
= m_currentChild
->FindItemInMenuBar(menuId
);
377 void wxMDIParentFrame::UpdateClientSize()
379 if ( GetClientWindow() )
382 GetClientSize(&width
, &height
);
384 GetClientWindow()->SetSize(0, 0, width
, height
);
388 void wxMDIParentFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
392 // do not call event.Skip() here, it somehow messes up MDI client window
395 void wxMDIParentFrame::OnIconized(wxIconizeEvent
& event
)
399 if ( !event
.IsIconized() )
403 // Returns the active MDI child window
404 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
406 HWND hWnd
= (HWND
)::SendMessage(GetWinHwnd(GetClientWindow()),
407 WM_MDIGETACTIVE
, 0, 0L);
411 return (wxMDIChildFrame
*)wxFindWinFromHandle(hWnd
);
414 // Create the client window class (don't Create the window, just return a new
416 wxMDIClientWindow
*wxMDIParentFrame::OnCreateClient()
418 return new wxMDIClientWindow
;
421 // Responds to colour changes, and passes event on to children.
422 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
424 if ( m_clientWindow
)
426 m_clientWindow
->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
427 m_clientWindow
->Refresh();
433 WXHICON
wxMDIParentFrame::GetDefaultIcon() const
435 // we don't have any standard icons (any more)
439 // ---------------------------------------------------------------------------
441 // ---------------------------------------------------------------------------
443 void wxMDIParentFrame::Cascade()
445 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE
, 0, 0);
448 void wxMDIParentFrame::Tile(wxOrientation orient
)
450 wxASSERT_MSG( orient
== wxHORIZONTAL
|| orient
== wxVERTICAL
,
451 _T("invalid orientation value") );
453 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE
,
454 orient
== wxHORIZONTAL
? MDITILE_HORIZONTAL
455 : MDITILE_VERTICAL
, 0);
458 void wxMDIParentFrame::ArrangeIcons()
460 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE
, 0, 0);
463 void wxMDIParentFrame::ActivateNext()
465 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 0);
468 void wxMDIParentFrame::ActivatePrevious()
470 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 1);
473 // ---------------------------------------------------------------------------
474 // the MDI parent frame window proc
475 // ---------------------------------------------------------------------------
477 WXLRESULT
wxMDIParentFrame::MSWWindowProc(WXUINT message
,
482 bool processed
= false;
488 WXWORD state
, minimized
;
490 UnpackActivate(wParam
, lParam
, &state
, &minimized
, &hwnd
);
492 processed
= HandleActivate(state
, minimized
!= 0, hwnd
);
500 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
502 (void)HandleCommand(id
, cmd
, hwnd
);
504 // even if the frame didn't process it, there is no need to try it
505 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
506 // so pretend we processed the message anyhow
510 // always pass this message DefFrameProc(), otherwise MDI menu
511 // commands (and sys commands - more surprisingly!) won't work
512 MSWDefWindowProc(message
, wParam
, lParam
);
516 m_clientWindow
= OnCreateClient();
517 // Uses own style for client style
518 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
520 wxLogMessage(_("Failed to create MDI parent frame."));
531 // we erase background ourselves
536 // though we don't (usually) resize the MDI client to exactly fit the
537 // client area we need to pass this one to DefFrameProc to allow the children to show
542 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
547 bool wxMDIParentFrame::HandleActivate(int state
, bool minimized
, WXHWND activate
)
549 bool processed
= false;
551 if ( wxWindow::HandleActivate(state
, minimized
, activate
) )
557 // If this window is an MDI parent, we must also send an OnActivate message
558 // to the current child.
559 if ( (m_currentChild
!= NULL
) &&
560 ((state
== WA_ACTIVE
) || (state
== WA_CLICKACTIVE
)) )
562 wxActivateEvent
event(wxEVT_ACTIVATE
, true, m_currentChild
->GetId());
563 event
.SetEventObject( m_currentChild
);
564 if ( m_currentChild
->HandleWindowEvent(event
) )
571 bool wxMDIParentFrame::HandleCommand(WXWORD id_
, WXWORD cmd
, WXHWND hwnd
)
573 // sign extend to int from short before comparing with the other int ids
574 int id
= (signed short)id_
;
576 // In case it's e.g. a toolbar.
579 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
581 return win
->MSWCommand(cmd
, id
);
584 if (wxCurrentPopupMenu
)
586 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
587 wxCurrentPopupMenu
= NULL
;
588 if (popupMenu
->MSWCommand(cmd
, id
))
592 // is it one of standard MDI commands?
598 case IDM_WINDOWCASCADE
:
600 wParam
= MDITILE_SKIPDISABLED
;
603 case IDM_WINDOWTILEHOR
:
604 wParam
|= MDITILE_HORIZONTAL
;
607 case IDM_WINDOWTILEVERT
:
609 wParam
= MDITILE_VERTICAL
;
611 wParam
|= MDITILE_SKIPDISABLED
;
614 case IDM_WINDOWICONS
:
615 msg
= WM_MDIICONARRANGE
;
620 lParam
= 0; // next child
625 lParam
= 1; // previous child
634 ::SendMessage(GetWinHwnd(GetClientWindow()), msg
, wParam
, lParam
);
639 // FIXME VZ: what does this test do??
642 return false; // Get WndProc to call default proc
645 if ( IsMdiCommandId(id
) )
647 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
650 wxWindow
*child
= node
->GetData();
651 if ( child
->GetHWND() )
653 int childId
= wxGetWindowId(child
->GetHWND());
654 if ( childId
== (signed short)id
)
656 ::SendMessage( GetWinHwnd(GetClientWindow()),
658 (WPARAM
)child
->GetHWND(), 0);
662 node
= node
->GetNext();
665 else if ( m_parentFrameActive
)
667 return ProcessCommand(id
);
669 else if ( m_currentChild
)
671 return m_currentChild
->HandleCommand(id
, cmd
, hwnd
);
675 // this shouldn't happen because it means that our messages are being
676 // lost (they're not sent to the parent frame nor to the children)
677 wxFAIL_MSG(wxT("MDI parent frame is not active, yet there is no active MDI child?"));
683 WXLRESULT
wxMDIParentFrame::MSWDefWindowProc(WXUINT message
,
688 if ( GetClientWindow() )
689 clientWnd
= GetClientWindow()->GetHWND();
693 return DefFrameProc(GetHwnd(), (HWND
)clientWnd
, message
, wParam
, lParam
);
696 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG
* msg
)
698 MSG
*pMsg
= (MSG
*)msg
;
700 // first let the current child get it
701 if ( m_currentChild
&& m_currentChild
->GetHWND() &&
702 m_currentChild
->MSWTranslateMessage(msg
) )
707 // then try out accel table (will also check the menu accels)
708 if ( wxFrame::MSWTranslateMessage(msg
) )
713 // finally, check for MDI specific built in accel keys
714 if ( pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
716 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg
))
723 // ===========================================================================
725 // ===========================================================================
727 void wxMDIChildFrame::Init()
729 m_needsResize
= true;
730 m_needsInitialShow
= true;
733 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
735 const wxString
& title
,
739 const wxString
& name
)
743 if ( id
!= wxID_ANY
)
746 m_windowId
= NewControlId();
750 parent
->AddChild(this);
760 mcs
.szClass
= style
& wxFULL_REPAINT_ON_RESIZE
761 ? wxMDIChildFrameClassName
762 : wxMDIChildFrameClassNameNoRedraw
;
763 mcs
.szTitle
= title
.wx_str();
764 mcs
.hOwner
= wxGetInstance();
765 if (x
!= wxDefaultCoord
)
768 mcs
.x
= CW_USEDEFAULT
;
770 if (y
!= wxDefaultCoord
)
773 mcs
.y
= CW_USEDEFAULT
;
775 if (width
!= wxDefaultCoord
)
778 mcs
.cx
= CW_USEDEFAULT
;
780 if (height
!= wxDefaultCoord
)
783 mcs
.cy
= CW_USEDEFAULT
;
785 DWORD msflags
= WS_OVERLAPPED
| WS_CLIPCHILDREN
;
786 if (style
& wxMINIMIZE_BOX
)
787 msflags
|= WS_MINIMIZEBOX
;
788 if (style
& wxMAXIMIZE_BOX
)
789 msflags
|= WS_MAXIMIZEBOX
;
790 if (style
& wxRESIZE_BORDER
)
791 msflags
|= WS_THICKFRAME
;
792 if (style
& wxSYSTEM_MENU
)
793 msflags
|= WS_SYSMENU
;
794 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
795 msflags
|= WS_MINIMIZE
;
796 if (style
& wxMAXIMIZE
)
797 msflags
|= WS_MAXIMIZE
;
798 if (style
& wxCAPTION
)
799 msflags
|= WS_CAPTION
;
805 wxWindowCreationHook
hook(this);
807 m_hWnd
= (WXHWND
)::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
808 WM_MDICREATE
, 0, (LPARAM
)&mcs
);
812 wxLogLastError(_T("WM_MDICREATE"));
818 parent
->AddMDIChild(this);
823 wxMDIChildFrame::~wxMDIChildFrame()
825 // if we hadn't been created, there is nothing to destroy
829 GetMDIParent()->RemoveMDIChild(this);
831 // will be destroyed by DestroyChildren() but reset them before calling it
832 // to avoid using dangling pointers if a callback comes in the meanwhile
834 m_frameToolBar
= NULL
;
837 m_frameStatusBar
= NULL
;
838 #endif // wxUSE_STATUSBAR
842 MDIRemoveWindowMenu(NULL
, m_hMenu
);
847 bool wxMDIChildFrame::Show(bool show
)
849 m_needsInitialShow
= false;
851 if (!wxFrame::Show(show
))
854 // KH: Without this call, new MDI children do not become active.
855 // This was added here after the same BringWindowToTop call was
856 // removed from wxTopLevelWindow::Show (November 2005)
858 ::BringWindowToTop(GetHwnd());
860 // we need to refresh the MDI frame window menu to include (or exclude if
861 // we've been hidden) this frame
862 wxMDIParentFrame
*parent
= GetMDIParent();
863 MDISetMenu(parent
->GetClientWindow(), NULL
, NULL
);
868 // Set the client size (i.e. leave the calculation of borders etc.
870 void wxMDIChildFrame::DoSetClientSize(int width
, int height
)
872 HWND hWnd
= GetHwnd();
875 ::GetClientRect(hWnd
, &rect
);
878 GetWindowRect(hWnd
, &rect2
);
880 // Find the difference between the entire window (title bar and all)
881 // and the client area; add this to the new client size to move the
883 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
884 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
887 if (GetStatusBar() && GetStatusBar()->IsShown())
890 GetStatusBar()->GetSize(&sx
, &sy
);
893 #endif // wxUSE_STATUSBAR
896 point
.x
= rect2
.left
;
899 // If there's an MDI parent, must subtract the parent's top left corner
900 // since MoveWindow moves relative to the parent
901 wxMDIParentFrame
*mdiParent
= GetMDIParent();
902 ::ScreenToClient((HWND
) mdiParent
->GetClientWindow()->GetHWND(), &point
);
904 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)true);
906 wxSize
size(width
, height
);
907 wxSizeEvent
event(size
, m_windowId
);
908 event
.SetEventObject( this );
909 HandleWindowEvent(event
);
912 // Unlike other wxTopLevelWindowBase, the mdi child's "GetPosition" is not the
913 // same as its GetScreenPosition
914 void wxMDIChildFrame::DoGetScreenPosition(int *x
, int *y
) const
916 HWND hWnd
= GetHwnd();
919 ::GetWindowRect(hWnd
, &rect
);
927 void wxMDIChildFrame::DoGetPosition(int *x
, int *y
) const
930 GetWindowRect(GetHwnd(), &rect
);
935 // Since we now have the absolute screen coords,
936 // if there's a parent we must subtract its top left corner
937 wxMDIParentFrame
*mdiParent
= GetMDIParent();
938 ::ScreenToClient((HWND
) mdiParent
->GetClientWindow()->GetHWND(), &point
);
946 void wxMDIChildFrame::InternalSetMenuBar()
948 wxMDIParentFrame
*parent
= GetMDIParent();
950 MDIInsertWindowMenu(parent
->GetClientWindow(),
951 m_hMenu
, GetMDIWindowMenu(parent
));
953 parent
->m_parentFrameActive
= false;
956 void wxMDIChildFrame::DetachMenuBar()
958 MDIRemoveWindowMenu(NULL
, m_hMenu
);
959 wxFrame::DetachMenuBar();
962 WXHICON
wxMDIChildFrame::GetDefaultIcon() const
964 // we don't have any standard icons (any more)
968 // ---------------------------------------------------------------------------
970 // ---------------------------------------------------------------------------
972 void wxMDIChildFrame::Maximize(bool maximize
)
974 wxMDIParentFrame
*parent
= GetMDIParent();
975 if ( parent
&& parent
->GetClientWindow() )
977 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
978 maximize
? WM_MDIMAXIMIZE
: WM_MDIRESTORE
,
979 (WPARAM
)GetHwnd(), 0);
983 void wxMDIChildFrame::Restore()
985 wxMDIParentFrame
*parent
= GetMDIParent();
986 if ( parent
&& parent
->GetClientWindow() )
988 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIRESTORE
,
989 (WPARAM
) GetHwnd(), 0);
993 void wxMDIChildFrame::Activate()
995 wxMDIParentFrame
*parent
= GetMDIParent();
996 if ( parent
&& parent
->GetClientWindow() )
998 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIACTIVATE
,
999 (WPARAM
) GetHwnd(), 0);
1003 // ---------------------------------------------------------------------------
1004 // MDI window proc and message handlers
1005 // ---------------------------------------------------------------------------
1007 WXLRESULT
wxMDIChildFrame::MSWWindowProc(WXUINT message
,
1012 bool processed
= false;
1020 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
1023 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
1027 case WM_GETMINMAXINFO
:
1028 processed
= HandleGetMinMaxInfo((MINMAXINFO
*)lParam
);
1031 case WM_MDIACTIVATE
:
1034 WXHWND hwndAct
, hwndDeact
;
1035 UnpackMDIActivate(wParam
, lParam
, &act
, &hwndAct
, &hwndDeact
);
1037 processed
= HandleMDIActivate(act
, hwndAct
, hwndDeact
);
1042 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
1043 // scrollbars if necessary
1048 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
1050 MSWDefWindowProc(message
, wParam
, lParam
);
1054 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
1055 // the message (the base class version does not)
1056 return MSWDefWindowProc(message
, wParam
, lParam
);
1058 case WM_WINDOWPOSCHANGING
:
1059 processed
= HandleWindowPosChanging((LPWINDOWPOS
)lParam
);
1064 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
1069 bool wxMDIChildFrame::HandleCommand(WXWORD id_
, WXWORD cmd
, WXHWND hwnd
)
1071 // sign extend to int from short before comparing with the other int ids
1072 int id
= (signed short)id_
;
1074 // In case it's e.g. a toolbar.
1077 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
1079 return win
->MSWCommand(cmd
, id
);
1082 if (wxCurrentPopupMenu
)
1084 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
1085 wxCurrentPopupMenu
= NULL
;
1086 if (popupMenu
->MSWCommand(cmd
, id
))
1091 if (GetMenuBar() && GetMenuBar()->FindItem(id
))
1093 processed
= ProcessCommand(id
);
1103 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate
),
1107 wxMDIParentFrame
*parent
= GetMDIParent();
1109 HMENU menuToSet
= 0;
1113 if ( m_hWnd
== hwndAct
)
1116 parent
->m_currentChild
= this;
1118 HMENU child_menu
= (HMENU
)GetWinMenu();
1121 parent
->m_parentFrameActive
= false;
1123 menuToSet
= child_menu
;
1126 else if ( m_hWnd
== hwndDeact
)
1128 wxASSERT_MSG( parent
->m_currentChild
== this,
1129 wxT("can't deactivate MDI child which wasn't active!") );
1132 parent
->m_currentChild
= NULL
;
1134 HMENU parent_menu
= (HMENU
)parent
->GetWinMenu();
1136 // activate the the parent menu only when there is no other child
1137 // that has been activated
1138 if ( parent_menu
&& !hwndAct
)
1140 parent
->m_parentFrameActive
= true;
1142 menuToSet
= parent_menu
;
1147 // we have nothing to do with it
1153 MDISetMenu(parent
->GetClientWindow(),
1154 menuToSet
, GetMDIWindowMenu(parent
));
1157 wxActivateEvent
event(wxEVT_ACTIVATE
, activated
, m_windowId
);
1158 event
.SetEventObject( this );
1160 ResetWindowStyle((void *)NULL
);
1162 return HandleWindowEvent(event
);
1165 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos
)
1167 WINDOWPOS
*lpPos
= (WINDOWPOS
*)pos
;
1169 if (!(lpPos
->flags
& SWP_NOSIZE
))
1172 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1173 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1174 if (ResetWindowStyle((void *) & rectClient
) && (dwStyle
& WS_MAXIMIZE
))
1176 ::AdjustWindowRectEx(&rectClient
, dwStyle
, false, dwExStyle
);
1177 lpPos
->x
= rectClient
.left
;
1178 lpPos
->y
= rectClient
.top
;
1179 lpPos
->cx
= rectClient
.right
- rectClient
.left
;
1180 lpPos
->cy
= rectClient
.bottom
- rectClient
.top
;
1187 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo
)
1189 MINMAXINFO
*info
= (MINMAXINFO
*)mmInfo
;
1191 // let the default window proc calculate the size of MDI children
1192 // frames because it is based on the size of the MDI client window,
1193 // not on the values specified in wxWindow m_max variables
1194 bool processed
= MSWDefWindowProc(WM_GETMINMAXINFO
, 0, (LPARAM
)mmInfo
) != 0;
1196 int minWidth
= GetMinWidth(),
1197 minHeight
= GetMinHeight();
1199 // but allow GetSizeHints() to set the min size
1200 if ( minWidth
!= wxDefaultCoord
)
1202 info
->ptMinTrackSize
.x
= minWidth
;
1207 if ( minHeight
!= wxDefaultCoord
)
1209 info
->ptMinTrackSize
.y
= minHeight
;
1217 // ---------------------------------------------------------------------------
1218 // MDI specific message translation/preprocessing
1219 // ---------------------------------------------------------------------------
1221 WXLRESULT
wxMDIChildFrame::MSWDefWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1223 return DefMDIChildProc(GetHwnd(),
1224 (UINT
)message
, (WPARAM
)wParam
, (LPARAM
)lParam
);
1227 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG
* msg
)
1229 // we must pass the parent frame to ::TranslateAccelerator(), otherwise it
1230 // doesn't do its job correctly for MDI child menus
1231 return MSWDoTranslateMessage(GetMDIParent(), msg
);
1234 // ---------------------------------------------------------------------------
1236 // ---------------------------------------------------------------------------
1238 void wxMDIChildFrame::MSWDestroyWindow()
1240 wxMDIParentFrame
*parent
= GetMDIParent();
1242 // Must make sure this handle is invalidated (set to NULL) since all sorts
1243 // of things could happen after the child client is destroyed, but before
1244 // the wxFrame is destroyed.
1246 HWND oldHandle
= (HWND
)GetHWND();
1247 SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIDESTROY
,
1248 (WPARAM
)oldHandle
, 0);
1250 if (parent
->GetActiveChild() == NULL
)
1251 ResetWindowStyle(NULL
);
1255 ::DestroyMenu((HMENU
) m_hMenu
);
1258 wxRemoveHandleAssociation(this);
1262 // Change the client window's extended style so we don't get a client edge
1263 // style when a child is maximised (a double border looks silly.)
1264 bool wxMDIChildFrame::ResetWindowStyle(void *vrect
)
1266 RECT
*rect
= (RECT
*)vrect
;
1267 wxMDIParentFrame
* pFrameWnd
= GetMDIParent();
1268 wxMDIChildFrame
* pChild
= pFrameWnd
->GetActiveChild();
1270 if (!pChild
|| (pChild
== this))
1272 HWND hwndClient
= GetWinHwnd(pFrameWnd
->GetClientWindow());
1273 DWORD dwStyle
= ::GetWindowLong(hwndClient
, GWL_EXSTYLE
);
1275 // we want to test whether there is a maximized child, so just set
1276 // dwThisStyle to 0 if there is no child at all
1277 DWORD dwThisStyle
= pChild
1278 ? ::GetWindowLong(GetWinHwnd(pChild
), GWL_STYLE
) : 0;
1279 DWORD dwNewStyle
= dwStyle
;
1280 if ( dwThisStyle
& WS_MAXIMIZE
)
1281 dwNewStyle
&= ~(WS_EX_CLIENTEDGE
);
1283 dwNewStyle
|= WS_EX_CLIENTEDGE
;
1285 if (dwStyle
!= dwNewStyle
)
1287 // force update of everything
1288 ::RedrawWindow(hwndClient
, NULL
, NULL
,
1289 RDW_INVALIDATE
| RDW_ALLCHILDREN
);
1290 ::SetWindowLong(hwndClient
, GWL_EXSTYLE
, dwNewStyle
);
1291 ::SetWindowPos(hwndClient
, NULL
, 0, 0, 0, 0,
1292 SWP_FRAMECHANGED
| SWP_NOACTIVATE
|
1293 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1296 ::GetClientRect(hwndClient
, rect
);
1305 // ===========================================================================
1306 // wxMDIClientWindow: the window of predefined (by Windows) class which
1307 // contains the child frames
1308 // ===========================================================================
1310 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
1312 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1314 CLIENTCREATESTRUCT ccs
;
1315 m_windowStyle
= style
;
1318 ccs
.hWindowMenu
= GetMDIWindowMenu(parent
);
1319 ccs
.idFirstChild
= wxFIRST_MDI_CHILD
;
1321 DWORD msStyle
= MDIS_ALLCHILDSTYLES
| WS_VISIBLE
| WS_CHILD
|
1322 WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
;
1324 if ( style
& wxHSCROLL
)
1325 msStyle
|= WS_HSCROLL
;
1326 if ( style
& wxVSCROLL
)
1327 msStyle
|= WS_VSCROLL
;
1329 DWORD exStyle
= WS_EX_CLIENTEDGE
;
1331 wxWindowCreationHook
hook(this);
1332 m_hWnd
= (WXHWND
)::CreateWindowEx
1342 (LPSTR
)(LPCLIENTCREATESTRUCT
)&ccs
);
1345 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1350 SubclassWin(m_hWnd
);
1355 // Explicitly call default scroll behaviour
1356 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)
1358 // Note: for client windows, the scroll position is not set in
1359 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1360 // scroll position we're at.
1361 // This makes it hard to paint patterns or bitmaps in the background,
1362 // and have the client area scrollable as well.
1364 if ( event
.GetOrientation() == wxHORIZONTAL
)
1365 m_scrollX
= event
.GetPosition(); // Always returns zero!
1367 m_scrollY
= event
.GetPosition(); // Always returns zero!
1372 void wxMDIClientWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1374 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1375 // client area, you can end up with a non-refreshed portion in the client window
1376 // (see OGL studio sample). So check if the position is changed and if so,
1377 // redraw the MDI child frames.
1379 const wxPoint oldPos
= GetPosition();
1381 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
| wxSIZE_FORCE
);
1383 const wxPoint newPos
= GetPosition();
1385 if ((newPos
.x
!= oldPos
.x
) || (newPos
.y
!= oldPos
.y
))
1389 wxWindowList::compatibility_iterator node
= GetParent()->GetChildren().GetFirst();
1392 wxWindow
*child
= node
->GetData();
1393 if (child
->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
1395 ::RedrawWindow(GetHwndOf(child
),
1402 node
= node
->GetNext();
1408 void wxMDIChildFrame::OnIdle(wxIdleEvent
& event
)
1410 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1411 // in flicker e.g. when the frame contained controls with non-trivial
1412 // layout. Since 2.5.3, the frame is created hidden as all other top level
1413 // windows. In order to maintain backward compatibility, the frame is shown
1414 // in OnIdle, unless Show(false) was called by the programmer before.
1415 if ( m_needsInitialShow
)
1420 // MDI child frames get their WM_SIZE when they're constructed but at this
1421 // moment they don't have any children yet so all child windows will be
1422 // positioned incorrectly when they are added later - to fix this, we
1423 // generate an artificial size event here
1424 if ( m_needsResize
)
1426 m_needsResize
= false; // avoid any possibility of recursion
1434 // ---------------------------------------------------------------------------
1435 // non member functions
1436 // ---------------------------------------------------------------------------
1438 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
)
1440 if ( hmenuFrame
|| hmenuWindow
)
1442 if ( !::SendMessage(GetWinHwnd(win
),
1445 (LPARAM
)hmenuWindow
) )
1448 DWORD err
= ::GetLastError();
1450 wxLogApiError(_T("SendMessage(WM_MDISETMENU)"), err
);
1451 #endif // __WXDEBUG__
1455 // update menu bar of the parent window
1456 wxWindow
*parent
= win
->GetParent();
1457 wxCHECK_RET( parent
, wxT("MDI client without parent frame? weird...") );
1459 ::SendMessage(GetWinHwnd(win
), WM_MDIREFRESHMENU
, 0, 0L);
1461 ::DrawMenuBar(GetWinHwnd(parent
));
1464 static void MDIInsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
)
1466 // Try to insert Window menu in front of Help, otherwise append it.
1467 HMENU hmenu
= (HMENU
)menu
;
1471 int N
= GetMenuItemCount(hmenu
);
1472 bool success
= false;
1473 for ( int i
= 0; i
< N
; i
++ )
1476 int chars
= GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
);
1479 wxLogLastError(wxT("GetMenuString"));
1484 wxString
strBuf(buf
);
1485 if ( wxStripMenuCodes(strBuf
) == wxGetStockLabel(wxID_HELP
,false) )
1488 ::InsertMenu(hmenu
, i
, MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1489 (UINT_PTR
)subMenu
, _("&Window").wx_str());
1496 ::AppendMenu(hmenu
, MF_POPUP
,
1497 (UINT_PTR
)subMenu
, _("&Window").wx_str());
1501 MDISetMenu(win
, hmenu
, subMenu
);
1504 static void MDIRemoveWindowMenu(wxWindow
*win
, WXHMENU menu
)
1506 HMENU hMenu
= (HMENU
)menu
;
1512 int N
= ::GetMenuItemCount(hMenu
);
1513 for ( int i
= 0; i
< N
; i
++ )
1515 if ( !::GetMenuString(hMenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1517 // Ignore successful read of menu string with length 0 which
1518 // occurs, for example, for a maximized MDI childs system menu
1519 if ( ::GetLastError() != 0 )
1521 wxLogLastError(wxT("GetMenuString"));
1527 if ( wxStrcmp(buf
, _("&Window")) == 0 )
1529 if ( !::RemoveMenu(hMenu
, i
, MF_BYPOSITION
) )
1531 wxLogLastError(wxT("RemoveMenu"));
1541 // we don't change the windows menu, but we update the main one
1542 MDISetMenu(win
, hMenu
, NULL
);
1546 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
1547 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
)
1550 *hwndAct
= (WXHWND
)lParam
;
1551 *hwndDeact
= (WXHWND
)wParam
;
1554 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)