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 InsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
);
87 // Remove the window menu
88 static void RemoveWindowMenu(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
101 static inline HMENU
GetMDIWindowMenu(wxMDIParentFrame
*frame
)
103 wxMenu
*menu
= frame
->GetWindowMenu();
104 return menu
? GetHmenuOf(menu
) : 0;
107 // ===========================================================================
109 // ===========================================================================
111 // ---------------------------------------------------------------------------
113 // ---------------------------------------------------------------------------
115 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxFrame
)
116 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxFrame
)
117 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxWindow
)
119 BEGIN_EVENT_TABLE(wxMDIParentFrame
, wxFrame
)
120 EVT_SIZE(wxMDIParentFrame::OnSize
)
121 EVT_ICONIZE(wxMDIParentFrame::OnIconized
)
122 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged
)
125 BEGIN_EVENT_TABLE(wxMDIChildFrame
, wxFrame
)
126 EVT_IDLE(wxMDIChildFrame::OnIdle
)
129 BEGIN_EVENT_TABLE(wxMDIClientWindow
, wxWindow
)
130 EVT_SCROLL(wxMDIClientWindow::OnScroll
)
133 // ===========================================================================
134 // wxMDIParentFrame: the frame which contains the client window which manages
136 // ===========================================================================
138 wxMDIParentFrame::wxMDIParentFrame()
140 m_clientWindow
= NULL
;
141 m_currentChild
= NULL
;
142 m_windowMenu
= (wxMenu
*) NULL
;
143 m_parentFrameActive
= true;
146 bool wxMDIParentFrame::Create(wxWindow
*parent
,
148 const wxString
& title
,
152 const wxString
& name
)
154 m_clientWindow
= NULL
;
155 m_currentChild
= NULL
;
157 // this style can be used to prevent a window from having the standard MDI
159 if ( style
& wxFRAME_NO_WINDOW_MENU
)
161 m_windowMenu
= (wxMenu
*)NULL
;
163 else // normal case: we have the window menu, so construct it
165 m_windowMenu
= new wxMenu
;
167 m_windowMenu
->Append(IDM_WINDOWCASCADE
, _("&Cascade"));
168 m_windowMenu
->Append(IDM_WINDOWTILEHOR
, _("Tile &Horizontally"));
169 m_windowMenu
->Append(IDM_WINDOWTILEVERT
, _("Tile &Vertically"));
170 m_windowMenu
->AppendSeparator();
171 m_windowMenu
->Append(IDM_WINDOWICONS
, _("&Arrange Icons"));
172 m_windowMenu
->Append(IDM_WINDOWNEXT
, _("&Next"));
173 m_windowMenu
->Append(IDM_WINDOWPREV
, _("&Previous"));
176 m_parentFrameActive
= true;
179 wxTopLevelWindows
.Append(this);
182 m_windowStyle
= style
;
185 parent
->AddChild(this);
187 if ( id
!= wxID_ANY
)
190 m_windowId
= NewControlId();
193 WXDWORD msflags
= MSWGetCreateWindowFlags(&exflags
);
194 msflags
&= ~WS_VSCROLL
;
195 msflags
&= ~WS_HSCROLL
;
197 if ( !wxWindow::MSWCreate(wxMDIFrameClassName
,
206 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
208 // unlike (almost?) all other windows, frames are created hidden
214 wxMDIParentFrame::~wxMDIParentFrame()
216 // see comment in ~wxMDIChildFrame
218 m_frameToolBar
= NULL
;
221 m_frameStatusBar
= NULL
;
222 #endif // wxUSE_STATUSBAR
229 m_windowMenu
= (wxMenu
*) NULL
;
232 // the MDI frame menubar is not automatically deleted by Windows unlike for
236 ::DestroyMenu((HMENU
)m_hMenu
);
237 m_hMenu
= (WXHMENU
)NULL
;
240 if ( m_clientWindow
)
242 if ( m_clientWindow
->MSWGetOldWndProc() )
243 m_clientWindow
->UnsubclassWin();
245 m_clientWindow
->SetHWND(0);
246 delete m_clientWindow
;
250 #if wxUSE_MENUS_NATIVE
252 void wxMDIParentFrame::InternalSetMenuBar()
254 m_parentFrameActive
= true;
256 InsertWindowMenu(GetClientWindow(), m_hMenu
, GetMDIWindowMenu(this));
259 #endif // wxUSE_MENUS_NATIVE
261 void wxMDIParentFrame::SetWindowMenu(wxMenu
* menu
)
267 // Remove old window menu
268 RemoveWindowMenu(GetClientWindow(), m_hMenu
);
272 m_windowMenu
= (wxMenu
*) NULL
;
280 InsertWindowMenu(GetClientWindow(), m_hMenu
,
281 GetHmenuOf(m_windowMenu
));
286 void wxMDIParentFrame::DoMenuUpdates(wxMenu
* menu
)
288 wxMDIChildFrame
*child
= GetActiveChild();
291 wxEvtHandler
* source
= child
->GetEventHandler();
292 wxMenuBar
* bar
= child
->GetMenuBar();
296 menu
->UpdateUI(source
);
302 int nCount
= bar
->GetMenuCount();
303 for (int n
= 0; n
< nCount
; n
++)
304 bar
->GetMenu(n
)->UpdateUI(source
);
310 wxFrameBase::DoMenuUpdates(menu
);
314 const wxMenuItem
*wxMDIParentFrame::FindItemInMenuBar(int menuId
) const
316 const wxMenuItem
*item
= wxFrame::FindItemInMenuBar(menuId
);
317 if ( !item
&& m_currentChild
)
319 item
= m_currentChild
->FindItemInMenuBar(menuId
);
325 void wxMDIParentFrame::UpdateClientSize()
327 if ( GetClientWindow() )
330 GetClientSize(&width
, &height
);
332 GetClientWindow()->SetSize(0, 0, width
, height
);
336 void wxMDIParentFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
340 // do not call event.Skip() here, it somehow messes up MDI client window
343 void wxMDIParentFrame::OnIconized(wxIconizeEvent
& event
)
347 if ( !event
.Iconized() )
353 // Returns the active MDI child window
354 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
356 HWND hWnd
= (HWND
)::SendMessage(GetWinHwnd(GetClientWindow()),
357 WM_MDIGETACTIVE
, 0, 0L);
361 return (wxMDIChildFrame
*)wxFindWinFromHandle((WXHWND
) hWnd
);
364 // Create the client window class (don't Create the window, just return a new
366 wxMDIClientWindow
*wxMDIParentFrame::OnCreateClient()
368 return new wxMDIClientWindow
;
371 // Responds to colour changes, and passes event on to children.
372 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
374 if ( m_clientWindow
)
376 m_clientWindow
->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
377 m_clientWindow
->Refresh();
383 WXHICON
wxMDIParentFrame::GetDefaultIcon() const
385 // we don't have any standard icons (any more)
389 // ---------------------------------------------------------------------------
391 // ---------------------------------------------------------------------------
393 void wxMDIParentFrame::Cascade()
395 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE
, 0, 0);
398 void wxMDIParentFrame::Tile(wxOrientation orient
)
400 wxASSERT_MSG( orient
== wxHORIZONTAL
|| orient
== wxVERTICAL
,
401 _T("invalid orientation value") );
403 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE
,
404 orient
== wxHORIZONTAL
? MDITILE_HORIZONTAL
405 : MDITILE_VERTICAL
, 0);
408 void wxMDIParentFrame::ArrangeIcons()
410 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE
, 0, 0);
413 void wxMDIParentFrame::ActivateNext()
415 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 0);
418 void wxMDIParentFrame::ActivatePrevious()
420 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 1);
423 // ---------------------------------------------------------------------------
424 // the MDI parent frame window proc
425 // ---------------------------------------------------------------------------
427 WXLRESULT
wxMDIParentFrame::MSWWindowProc(WXUINT message
,
432 bool processed
= false;
438 WXWORD state
, minimized
;
440 UnpackActivate(wParam
, lParam
, &state
, &minimized
, &hwnd
);
442 processed
= HandleActivate(state
, minimized
!= 0, hwnd
);
450 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
452 (void)HandleCommand(id
, cmd
, hwnd
);
454 // even if the frame didn't process it, there is no need to try it
455 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
456 // so pretend we processed the message anyhow
460 // always pass this message DefFrameProc(), otherwise MDI menu
461 // commands (and sys commands - more surprisingly!) won't work
462 MSWDefWindowProc(message
, wParam
, lParam
);
466 m_clientWindow
= OnCreateClient();
467 // Uses own style for client style
468 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
470 wxLogMessage(_("Failed to create MDI parent frame."));
481 // we erase background ourselves
486 // though we don't (usually) resize the MDI client to exactly fit the
487 // client area we need to pass this one to DefFrameProc to allow the children to show
492 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
497 bool wxMDIParentFrame::HandleActivate(int state
, bool minimized
, WXHWND activate
)
499 bool processed
= false;
501 if ( wxWindow::HandleActivate(state
, minimized
, activate
) )
507 // If this window is an MDI parent, we must also send an OnActivate message
508 // to the current child.
509 if ( (m_currentChild
!= NULL
) &&
510 ((state
== WA_ACTIVE
) || (state
== WA_CLICKACTIVE
)) )
512 wxActivateEvent
event(wxEVT_ACTIVATE
, true, m_currentChild
->GetId());
513 event
.SetEventObject( m_currentChild
);
514 if ( m_currentChild
->HandleWindowEvent(event
) )
521 bool wxMDIParentFrame::HandleCommand(WXWORD id_
, WXWORD cmd
, WXHWND hwnd
)
523 // sign extend to int from short before comparing with the other int ids
524 int id
= (signed short)id_
;
526 // In case it's e.g. a toolbar.
529 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
531 return win
->MSWCommand(cmd
, id
);
534 if (wxCurrentPopupMenu
)
536 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
537 wxCurrentPopupMenu
= NULL
;
538 if (popupMenu
->MSWCommand(cmd
, id
))
542 // is it one of standard MDI commands?
548 case IDM_WINDOWCASCADE
:
550 wParam
= MDITILE_SKIPDISABLED
;
553 case IDM_WINDOWTILEHOR
:
554 wParam
|= MDITILE_HORIZONTAL
;
557 case IDM_WINDOWTILEVERT
:
559 wParam
= MDITILE_VERTICAL
;
561 wParam
|= MDITILE_SKIPDISABLED
;
564 case IDM_WINDOWICONS
:
565 msg
= WM_MDIICONARRANGE
;
570 lParam
= 0; // next child
575 lParam
= 1; // previous child
584 ::SendMessage(GetWinHwnd(GetClientWindow()), msg
, wParam
, lParam
);
589 // FIXME VZ: what does this test do??
592 return false; // Get WndProc to call default proc
595 if ( IsMdiCommandId(id
) )
597 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
600 wxWindow
*child
= node
->GetData();
601 if ( child
->GetHWND() )
603 int childId
= wxGetWindowId(child
->GetHWND());
604 if ( childId
== (signed short)id
)
606 ::SendMessage( GetWinHwnd(GetClientWindow()),
608 (WPARAM
)child
->GetHWND(), 0);
612 node
= node
->GetNext();
615 else if ( m_parentFrameActive
)
617 return ProcessCommand(id
);
619 else if ( m_currentChild
)
621 return m_currentChild
->HandleCommand(id
, cmd
, hwnd
);
625 // this shouldn't happen because it means that our messages are being
626 // lost (they're not sent to the parent frame nor to the children)
627 wxFAIL_MSG(wxT("MDI parent frame is not active, yet there is no active MDI child?"));
633 WXLRESULT
wxMDIParentFrame::MSWDefWindowProc(WXUINT message
,
638 if ( GetClientWindow() )
639 clientWnd
= GetClientWindow()->GetHWND();
643 return DefFrameProc(GetHwnd(), (HWND
)clientWnd
, message
, wParam
, lParam
);
646 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG
* msg
)
648 MSG
*pMsg
= (MSG
*)msg
;
650 // first let the current child get it
651 if ( m_currentChild
&& m_currentChild
->GetHWND() &&
652 m_currentChild
->MSWTranslateMessage(msg
) )
657 // then try out accel table (will also check the menu accels)
658 if ( wxFrame::MSWTranslateMessage(msg
) )
663 // finally, check for MDI specific built in accel keys
664 if ( pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
666 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg
))
673 // ===========================================================================
675 // ===========================================================================
677 void wxMDIChildFrame::Init()
679 m_needsResize
= true;
680 m_needsInitialShow
= true;
683 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
685 const wxString
& title
,
689 const wxString
& name
)
693 if ( id
!= wxID_ANY
)
696 m_windowId
= NewControlId();
700 parent
->AddChild(this);
710 mcs
.szClass
= style
& wxFULL_REPAINT_ON_RESIZE
711 ? wxMDIChildFrameClassName
712 : wxMDIChildFrameClassNameNoRedraw
;
713 mcs
.szTitle
= title
.wx_str();
714 mcs
.hOwner
= wxGetInstance();
715 if (x
!= wxDefaultCoord
)
718 mcs
.x
= CW_USEDEFAULT
;
720 if (y
!= wxDefaultCoord
)
723 mcs
.y
= CW_USEDEFAULT
;
725 if (width
!= wxDefaultCoord
)
728 mcs
.cx
= CW_USEDEFAULT
;
730 if (height
!= wxDefaultCoord
)
733 mcs
.cy
= CW_USEDEFAULT
;
735 DWORD msflags
= WS_OVERLAPPED
| WS_CLIPCHILDREN
;
736 if (style
& wxMINIMIZE_BOX
)
737 msflags
|= WS_MINIMIZEBOX
;
738 if (style
& wxMAXIMIZE_BOX
)
739 msflags
|= WS_MAXIMIZEBOX
;
740 if (style
& wxRESIZE_BORDER
)
741 msflags
|= WS_THICKFRAME
;
742 if (style
& wxSYSTEM_MENU
)
743 msflags
|= WS_SYSMENU
;
744 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
745 msflags
|= WS_MINIMIZE
;
746 if (style
& wxMAXIMIZE
)
747 msflags
|= WS_MAXIMIZE
;
748 if (style
& wxCAPTION
)
749 msflags
|= WS_CAPTION
;
755 wxWindowCreationHook
hook(this);
757 m_hWnd
= (WXHWND
)::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
758 WM_MDICREATE
, 0, (LPARAM
)&mcs
);
762 wxLogLastError(_T("WM_MDICREATE"));
771 wxMDIChildFrame::~wxMDIChildFrame()
773 // if we hadn't been created, there is nothing to destroy
777 // will be destroyed by DestroyChildren() but reset them before calling it
778 // to avoid using dangling pointers if a callback comes in the meanwhile
780 m_frameToolBar
= NULL
;
783 m_frameStatusBar
= NULL
;
784 #endif // wxUSE_STATUSBAR
788 RemoveWindowMenu(NULL
, m_hMenu
);
793 bool wxMDIChildFrame::Show(bool show
)
795 m_needsInitialShow
= false;
797 if (!wxFrame::Show(show
))
800 // KH: Without this call, new MDI children do not become active.
801 // This was added here after the same BringWindowToTop call was
802 // removed from wxTopLevelWindow::Show (November 2005)
804 ::BringWindowToTop(GetHwnd());
806 // we need to refresh the MDI frame window menu to include (or exclude if
807 // we've been hidden) this frame
808 wxMDIParentFrame
*parent
= GetMDIParent();
809 MDISetMenu(parent
->GetClientWindow(), NULL
, NULL
);
814 // Set the client size (i.e. leave the calculation of borders etc.
816 void wxMDIChildFrame::DoSetClientSize(int width
, int height
)
818 HWND hWnd
= GetHwnd();
821 ::GetClientRect(hWnd
, &rect
);
824 GetWindowRect(hWnd
, &rect2
);
826 // Find the difference between the entire window (title bar and all)
827 // and the client area; add this to the new client size to move the
829 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
830 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
833 if (GetStatusBar() && GetStatusBar()->IsShown())
836 GetStatusBar()->GetSize(&sx
, &sy
);
839 #endif // wxUSE_STATUSBAR
842 point
.x
= rect2
.left
;
845 // If there's an MDI parent, must subtract the parent's top left corner
846 // since MoveWindow moves relative to the parent
847 wxMDIParentFrame
*mdiParent
= GetMDIParent();
848 ::ScreenToClient((HWND
) mdiParent
->GetClientWindow()->GetHWND(), &point
);
850 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)true);
852 wxSize
size(width
, height
);
853 wxSizeEvent
event(size
, m_windowId
);
854 event
.SetEventObject( this );
855 HandleWindowEvent(event
);
858 // Unlike other wxTopLevelWindowBase, the mdi child's "GetPosition" is not the
859 // same as its GetScreenPosition
860 void wxMDIChildFrame::DoGetScreenPosition(int *x
, int *y
) const
862 HWND hWnd
= GetHwnd();
865 ::GetWindowRect(hWnd
, &rect
);
873 void wxMDIChildFrame::DoGetPosition(int *x
, int *y
) const
876 GetWindowRect(GetHwnd(), &rect
);
881 // Since we now have the absolute screen coords,
882 // if there's a parent we must subtract its top left corner
883 wxMDIParentFrame
*mdiParent
= GetMDIParent();
884 ::ScreenToClient((HWND
) mdiParent
->GetClientWindow()->GetHWND(), &point
);
892 void wxMDIChildFrame::InternalSetMenuBar()
894 wxMDIParentFrame
*parent
= GetMDIParent();
896 InsertWindowMenu(parent
->GetClientWindow(),
897 m_hMenu
, GetMDIWindowMenu(parent
));
899 parent
->m_parentFrameActive
= false;
902 void wxMDIChildFrame::DetachMenuBar()
904 RemoveWindowMenu(NULL
, m_hMenu
);
905 wxFrame::DetachMenuBar();
908 WXHICON
wxMDIChildFrame::GetDefaultIcon() const
910 // we don't have any standard icons (any more)
914 // ---------------------------------------------------------------------------
916 // ---------------------------------------------------------------------------
918 void wxMDIChildFrame::Maximize(bool maximize
)
920 wxMDIParentFrame
*parent
= GetMDIParent();
921 if ( parent
&& parent
->GetClientWindow() )
923 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
924 maximize
? WM_MDIMAXIMIZE
: WM_MDIRESTORE
,
925 (WPARAM
)GetHwnd(), 0);
929 void wxMDIChildFrame::Restore()
931 wxMDIParentFrame
*parent
= GetMDIParent();
932 if ( parent
&& parent
->GetClientWindow() )
934 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIRESTORE
,
935 (WPARAM
) GetHwnd(), 0);
939 void wxMDIChildFrame::Activate()
941 wxMDIParentFrame
*parent
= GetMDIParent();
942 if ( parent
&& parent
->GetClientWindow() )
944 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIACTIVATE
,
945 (WPARAM
) GetHwnd(), 0);
949 // ---------------------------------------------------------------------------
950 // MDI window proc and message handlers
951 // ---------------------------------------------------------------------------
953 WXLRESULT
wxMDIChildFrame::MSWWindowProc(WXUINT message
,
958 bool processed
= false;
966 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
969 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
973 case WM_GETMINMAXINFO
:
974 processed
= HandleGetMinMaxInfo((MINMAXINFO
*)lParam
);
980 WXHWND hwndAct
, hwndDeact
;
981 UnpackMDIActivate(wParam
, lParam
, &act
, &hwndAct
, &hwndDeact
);
983 processed
= HandleMDIActivate(act
, hwndAct
, hwndDeact
);
988 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
989 // scrollbars if necessary
994 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
996 MSWDefWindowProc(message
, wParam
, lParam
);
1000 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
1001 // the message (the base class version does not)
1002 return MSWDefWindowProc(message
, wParam
, lParam
);
1004 case WM_WINDOWPOSCHANGING
:
1005 processed
= HandleWindowPosChanging((LPWINDOWPOS
)lParam
);
1010 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
1015 bool wxMDIChildFrame::HandleCommand(WXWORD id_
, WXWORD cmd
, WXHWND hwnd
)
1017 // sign extend to int from short before comparing with the other int ids
1018 int id
= (signed short)id_
;
1020 // In case it's e.g. a toolbar.
1023 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
1025 return win
->MSWCommand(cmd
, id
);
1028 if (wxCurrentPopupMenu
)
1030 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
1031 wxCurrentPopupMenu
= NULL
;
1032 if (popupMenu
->MSWCommand(cmd
, id
))
1037 if (GetMenuBar() && GetMenuBar()->FindItem(id
))
1039 processed
= ProcessCommand(id
);
1049 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate
),
1053 wxMDIParentFrame
*parent
= GetMDIParent();
1055 HMENU menuToSet
= 0;
1059 if ( m_hWnd
== hwndAct
)
1062 parent
->m_currentChild
= this;
1064 HMENU child_menu
= (HMENU
)GetWinMenu();
1067 parent
->m_parentFrameActive
= false;
1069 menuToSet
= child_menu
;
1072 else if ( m_hWnd
== hwndDeact
)
1074 wxASSERT_MSG( parent
->m_currentChild
== this,
1075 wxT("can't deactivate MDI child which wasn't active!") );
1078 parent
->m_currentChild
= NULL
;
1080 HMENU parent_menu
= (HMENU
)parent
->GetWinMenu();
1082 // activate the the parent menu only when there is no other child
1083 // that has been activated
1084 if ( parent_menu
&& !hwndAct
)
1086 parent
->m_parentFrameActive
= true;
1088 menuToSet
= parent_menu
;
1093 // we have nothing to do with it
1099 MDISetMenu(parent
->GetClientWindow(),
1100 menuToSet
, GetMDIWindowMenu(parent
));
1103 wxActivateEvent
event(wxEVT_ACTIVATE
, activated
, m_windowId
);
1104 event
.SetEventObject( this );
1106 ResetWindowStyle((void *)NULL
);
1108 return HandleWindowEvent(event
);
1111 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos
)
1113 WINDOWPOS
*lpPos
= (WINDOWPOS
*)pos
;
1115 if (!(lpPos
->flags
& SWP_NOSIZE
))
1118 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1119 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1120 if (ResetWindowStyle((void *) & rectClient
) && (dwStyle
& WS_MAXIMIZE
))
1122 ::AdjustWindowRectEx(&rectClient
, dwStyle
, false, dwExStyle
);
1123 lpPos
->x
= rectClient
.left
;
1124 lpPos
->y
= rectClient
.top
;
1125 lpPos
->cx
= rectClient
.right
- rectClient
.left
;
1126 lpPos
->cy
= rectClient
.bottom
- rectClient
.top
;
1133 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo
)
1135 MINMAXINFO
*info
= (MINMAXINFO
*)mmInfo
;
1137 // let the default window proc calculate the size of MDI children
1138 // frames because it is based on the size of the MDI client window,
1139 // not on the values specified in wxWindow m_max variables
1140 bool processed
= MSWDefWindowProc(WM_GETMINMAXINFO
, 0, (LPARAM
)mmInfo
) != 0;
1142 int minWidth
= GetMinWidth(),
1143 minHeight
= GetMinHeight();
1145 // but allow GetSizeHints() to set the min size
1146 if ( minWidth
!= wxDefaultCoord
)
1148 info
->ptMinTrackSize
.x
= minWidth
;
1153 if ( minHeight
!= wxDefaultCoord
)
1155 info
->ptMinTrackSize
.y
= minHeight
;
1163 // ---------------------------------------------------------------------------
1164 // MDI specific message translation/preprocessing
1165 // ---------------------------------------------------------------------------
1167 WXLRESULT
wxMDIChildFrame::MSWDefWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1169 return DefMDIChildProc(GetHwnd(),
1170 (UINT
)message
, (WPARAM
)wParam
, (LPARAM
)lParam
);
1173 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG
* msg
)
1175 // we must pass the parent frame to ::TranslateAccelerator(), otherwise it
1176 // doesn't do its job correctly for MDI child menus
1177 return MSWDoTranslateMessage(GetMDIParent(), msg
);
1180 // ---------------------------------------------------------------------------
1182 // ---------------------------------------------------------------------------
1184 void wxMDIChildFrame::MSWDestroyWindow()
1186 wxMDIParentFrame
*parent
= GetMDIParent();
1188 // Must make sure this handle is invalidated (set to NULL) since all sorts
1189 // of things could happen after the child client is destroyed, but before
1190 // the wxFrame is destroyed.
1192 HWND oldHandle
= (HWND
)GetHWND();
1193 SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIDESTROY
,
1194 (WPARAM
)oldHandle
, 0);
1196 if (parent
->GetActiveChild() == (wxMDIChildFrame
*) NULL
)
1197 ResetWindowStyle((void*) NULL
);
1201 ::DestroyMenu((HMENU
) m_hMenu
);
1204 wxRemoveHandleAssociation(this);
1208 // Change the client window's extended style so we don't get a client edge
1209 // style when a child is maximised (a double border looks silly.)
1210 bool wxMDIChildFrame::ResetWindowStyle(void *vrect
)
1212 RECT
*rect
= (RECT
*)vrect
;
1213 wxMDIParentFrame
* pFrameWnd
= GetMDIParent();
1214 wxMDIChildFrame
* pChild
= pFrameWnd
->GetActiveChild();
1216 if (!pChild
|| (pChild
== this))
1218 HWND hwndClient
= GetWinHwnd(pFrameWnd
->GetClientWindow());
1219 DWORD dwStyle
= ::GetWindowLong(hwndClient
, GWL_EXSTYLE
);
1221 // we want to test whether there is a maximized child, so just set
1222 // dwThisStyle to 0 if there is no child at all
1223 DWORD dwThisStyle
= pChild
1224 ? ::GetWindowLong(GetWinHwnd(pChild
), GWL_STYLE
) : 0;
1225 DWORD dwNewStyle
= dwStyle
;
1226 if ( dwThisStyle
& WS_MAXIMIZE
)
1227 dwNewStyle
&= ~(WS_EX_CLIENTEDGE
);
1229 dwNewStyle
|= WS_EX_CLIENTEDGE
;
1231 if (dwStyle
!= dwNewStyle
)
1233 // force update of everything
1234 ::RedrawWindow(hwndClient
, NULL
, NULL
,
1235 RDW_INVALIDATE
| RDW_ALLCHILDREN
);
1236 ::SetWindowLong(hwndClient
, GWL_EXSTYLE
, dwNewStyle
);
1237 ::SetWindowPos(hwndClient
, NULL
, 0, 0, 0, 0,
1238 SWP_FRAMECHANGED
| SWP_NOACTIVATE
|
1239 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1242 ::GetClientRect(hwndClient
, rect
);
1251 // ===========================================================================
1252 // wxMDIClientWindow: the window of predefined (by Windows) class which
1253 // contains the child frames
1254 // ===========================================================================
1256 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
1258 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1260 CLIENTCREATESTRUCT ccs
;
1261 m_windowStyle
= style
;
1264 ccs
.hWindowMenu
= GetMDIWindowMenu(parent
);
1265 ccs
.idFirstChild
= wxFIRST_MDI_CHILD
;
1267 DWORD msStyle
= MDIS_ALLCHILDSTYLES
| WS_VISIBLE
| WS_CHILD
|
1268 WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
;
1270 if ( style
& wxHSCROLL
)
1271 msStyle
|= WS_HSCROLL
;
1272 if ( style
& wxVSCROLL
)
1273 msStyle
|= WS_VSCROLL
;
1275 DWORD exStyle
= WS_EX_CLIENTEDGE
;
1277 wxWindowCreationHook
hook(this);
1278 m_hWnd
= (WXHWND
)::CreateWindowEx
1288 (LPSTR
)(LPCLIENTCREATESTRUCT
)&ccs
);
1291 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1296 SubclassWin(m_hWnd
);
1301 // Explicitly call default scroll behaviour
1302 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)
1304 // Note: for client windows, the scroll position is not set in
1305 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1306 // scroll position we're at.
1307 // This makes it hard to paint patterns or bitmaps in the background,
1308 // and have the client area scrollable as well.
1310 if ( event
.GetOrientation() == wxHORIZONTAL
)
1311 m_scrollX
= event
.GetPosition(); // Always returns zero!
1313 m_scrollY
= event
.GetPosition(); // Always returns zero!
1318 void wxMDIClientWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1320 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1321 // client area, you can end up with a non-refreshed portion in the client window
1322 // (see OGL studio sample). So check if the position is changed and if so,
1323 // redraw the MDI child frames.
1325 const wxPoint oldPos
= GetPosition();
1327 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
| wxSIZE_FORCE
);
1329 const wxPoint newPos
= GetPosition();
1331 if ((newPos
.x
!= oldPos
.x
) || (newPos
.y
!= oldPos
.y
))
1335 wxWindowList::compatibility_iterator node
= GetParent()->GetChildren().GetFirst();
1338 wxWindow
*child
= node
->GetData();
1339 if (child
->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
1341 ::RedrawWindow(GetHwndOf(child
),
1348 node
= node
->GetNext();
1354 void wxMDIChildFrame::OnIdle(wxIdleEvent
& event
)
1356 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1357 // in flicker e.g. when the frame contained controls with non-trivial
1358 // layout. Since 2.5.3, the frame is created hidden as all other top level
1359 // windows. In order to maintain backward compatibility, the frame is shown
1360 // in OnIdle, unless Show(false) was called by the programmer before.
1361 if ( m_needsInitialShow
)
1366 // MDI child frames get their WM_SIZE when they're constructed but at this
1367 // moment they don't have any children yet so all child windows will be
1368 // positioned incorrectly when they are added later - to fix this, we
1369 // generate an artificial size event here
1370 if ( m_needsResize
)
1372 m_needsResize
= false; // avoid any possibility of recursion
1380 // ---------------------------------------------------------------------------
1381 // non member functions
1382 // ---------------------------------------------------------------------------
1384 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
)
1386 if ( hmenuFrame
|| hmenuWindow
)
1388 if ( !::SendMessage(GetWinHwnd(win
),
1391 (LPARAM
)hmenuWindow
) )
1394 DWORD err
= ::GetLastError();
1396 wxLogApiError(_T("SendMessage(WM_MDISETMENU)"), err
);
1397 #endif // __WXDEBUG__
1401 // update menu bar of the parent window
1402 wxWindow
*parent
= win
->GetParent();
1403 wxCHECK_RET( parent
, wxT("MDI client without parent frame? weird...") );
1405 ::SendMessage(GetWinHwnd(win
), WM_MDIREFRESHMENU
, 0, 0L);
1407 ::DrawMenuBar(GetWinHwnd(parent
));
1410 static void InsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
)
1412 // Try to insert Window menu in front of Help, otherwise append it.
1413 HMENU hmenu
= (HMENU
)menu
;
1417 int N
= GetMenuItemCount(hmenu
);
1418 bool success
= false;
1419 for ( int i
= 0; i
< N
; i
++ )
1422 int chars
= GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
);
1425 wxLogLastError(wxT("GetMenuString"));
1430 wxString
strBuf(buf
);
1431 if ( wxStripMenuCodes(strBuf
) == wxGetStockLabel(wxID_HELP
,false) )
1434 ::InsertMenu(hmenu
, i
, MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1435 (UINT_PTR
)subMenu
, _("&Window").wx_str());
1442 ::AppendMenu(hmenu
, MF_POPUP
,
1443 (UINT_PTR
)subMenu
, _("&Window").wx_str());
1447 MDISetMenu(win
, hmenu
, subMenu
);
1450 static void RemoveWindowMenu(wxWindow
*win
, WXHMENU menu
)
1452 HMENU hMenu
= (HMENU
)menu
;
1458 int N
= ::GetMenuItemCount(hMenu
);
1459 for ( int i
= 0; i
< N
; i
++ )
1461 if ( !::GetMenuString(hMenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1463 // Ignore successful read of menu string with length 0 which
1464 // occurs, for example, for a maximized MDI childs system menu
1465 if ( ::GetLastError() != 0 )
1467 wxLogLastError(wxT("GetMenuString"));
1473 if ( wxStrcmp(buf
, _("&Window")) == 0 )
1475 if ( !::RemoveMenu(hMenu
, i
, MF_BYPOSITION
) )
1477 wxLogLastError(wxT("RemoveMenu"));
1487 // we don't change the windows menu, but we update the main one
1488 MDISetMenu(win
, hMenu
, NULL
);
1492 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
1493 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
)
1496 *hwndAct
= (WXHWND
)lParam
;
1497 *hwndDeact
= (WXHWND
)wParam
;
1500 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)