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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "mdi.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #if wxUSE_MDI && !defined(__WXUNIVERSAL__)
39 #include "wx/dialog.h"
41 #include "wx/statusbr.h"
43 #include "wx/settings.h"
48 #include "wx/stockitem.h"
50 #include "wx/msw/private.h"
52 #if wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR
53 #include "wx/msw/statbr95.h"
57 #include "wx/toolbar.h"
58 #endif // wxUSE_TOOLBAR
62 // ---------------------------------------------------------------------------
64 // ---------------------------------------------------------------------------
66 extern wxMenu
*wxCurrentPopupMenu
;
68 extern const wxChar
*wxMDIFrameClassName
; // from app.cpp
69 extern const wxChar
*wxMDIChildFrameClassName
;
70 extern const wxChar
*wxMDIChildFrameClassNameNoRedraw
;
71 extern void wxAssociateWinWithHandle(HWND hWnd
, wxWindow
*win
);
72 extern void wxRemoveHandleAssociation(wxWindow
*win
);
74 static HWND invalidHandle
= 0;
76 // ---------------------------------------------------------------------------
78 // ---------------------------------------------------------------------------
80 static const int IDM_WINDOWTILEHOR
= 4001;
81 static const int IDM_WINDOWCASCADE
= 4002;
82 static const int IDM_WINDOWICONS
= 4003;
83 static const int IDM_WINDOWNEXT
= 4004;
84 static const int IDM_WINDOWTILEVERT
= 4005;
85 static const int IDM_WINDOWPREV
= 4006;
87 // This range gives a maximum of 500 MDI children. Should be enough :-)
88 static const int wxFIRST_MDI_CHILD
= 4100;
89 static const int wxLAST_MDI_CHILD
= 4600;
91 // ---------------------------------------------------------------------------
93 // ---------------------------------------------------------------------------
95 // set the MDI menus (by sending the WM_MDISETMENU message) and update the menu
96 // of the parent of win (which is supposed to be the MDI client window)
97 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
);
99 // insert the window menu (subMenu) into menu just before "Help" submenu or at
100 // the very end if not found
101 static void InsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
);
103 // Remove the window menu
104 static void RemoveWindowMenu(wxWindow
*win
, WXHMENU menu
);
106 // is this an id of an MDI child?
107 inline bool IsMdiCommandId(int id
)
109 return (id
>= wxFIRST_MDI_CHILD
) && (id
<= wxLAST_MDI_CHILD
);
112 // unpack the parameters of WM_MDIACTIVATE message
113 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
114 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
);
116 // return the HMENU of the MDI menu
117 static inline HMENU
GetMDIWindowMenu(wxMDIParentFrame
*frame
)
119 wxMenu
*menu
= frame
->GetWindowMenu();
120 return menu
? GetHmenuOf(menu
) : 0;
123 // ===========================================================================
125 // ===========================================================================
127 // ---------------------------------------------------------------------------
129 // ---------------------------------------------------------------------------
131 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxFrame
)
132 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxFrame
)
133 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxWindow
)
135 BEGIN_EVENT_TABLE(wxMDIParentFrame
, wxFrame
)
136 EVT_SIZE(wxMDIParentFrame::OnSize
)
137 EVT_ICONIZE(wxMDIParentFrame::OnIconized
)
138 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged
)
141 BEGIN_EVENT_TABLE(wxMDIChildFrame
, wxFrame
)
142 EVT_IDLE(wxMDIChildFrame::OnIdle
)
145 BEGIN_EVENT_TABLE(wxMDIClientWindow
, wxWindow
)
146 EVT_SCROLL(wxMDIClientWindow::OnScroll
)
149 // ===========================================================================
150 // wxMDIParentFrame: the frame which contains the client window which manages
152 // ===========================================================================
154 wxMDIParentFrame::wxMDIParentFrame()
156 m_clientWindow
= NULL
;
157 m_currentChild
= NULL
;
158 m_windowMenu
= (wxMenu
*) NULL
;
159 m_parentFrameActive
= true;
162 bool wxMDIParentFrame::Create(wxWindow
*parent
,
164 const wxString
& title
,
168 const wxString
& name
)
170 m_clientWindow
= NULL
;
171 m_currentChild
= NULL
;
173 // this style can be used to prevent a window from having the standard MDI
175 if ( style
& wxFRAME_NO_WINDOW_MENU
)
177 m_windowMenu
= (wxMenu
*)NULL
;
179 else // normal case: we have the window menu, so construct it
181 m_windowMenu
= new wxMenu
;
183 m_windowMenu
->Append(IDM_WINDOWCASCADE
, _("&Cascade"));
184 m_windowMenu
->Append(IDM_WINDOWTILEHOR
, _("Tile &Horizontally"));
185 m_windowMenu
->Append(IDM_WINDOWTILEVERT
, _("Tile &Vertically"));
186 m_windowMenu
->AppendSeparator();
187 m_windowMenu
->Append(IDM_WINDOWICONS
, _("&Arrange Icons"));
188 m_windowMenu
->Append(IDM_WINDOWNEXT
, _("&Next"));
189 m_windowMenu
->Append(IDM_WINDOWPREV
, _("&Previous"));
192 m_parentFrameActive
= true;
195 wxTopLevelWindows
.Append(this);
198 m_windowStyle
= style
;
201 parent
->AddChild(this);
203 if ( id
!= wxID_ANY
)
206 m_windowId
= NewControlId();
209 WXDWORD msflags
= MSWGetCreateWindowFlags(&exflags
);
210 msflags
&= ~WS_VSCROLL
;
211 msflags
&= ~WS_HSCROLL
;
213 if ( !wxWindow::MSWCreate(wxMDIFrameClassName
,
222 // unlike (almost?) all other windows, frames are created hidden
228 wxMDIParentFrame::~wxMDIParentFrame()
230 // see comment in ~wxMDIChildFrame
232 m_frameToolBar
= NULL
;
235 m_frameStatusBar
= NULL
;
236 #endif // wxUSE_STATUSBAR
243 m_windowMenu
= (wxMenu
*) NULL
;
246 // the MDI frame menubar is not automatically deleted by Windows unlike for
250 ::DestroyMenu((HMENU
)m_hMenu
);
251 m_hMenu
= (WXHMENU
)NULL
;
254 if ( m_clientWindow
)
256 if ( m_clientWindow
->MSWGetOldWndProc() )
257 m_clientWindow
->UnsubclassWin();
259 m_clientWindow
->SetHWND(0);
260 delete m_clientWindow
;
264 #if wxUSE_MENUS_NATIVE
266 void wxMDIParentFrame::InternalSetMenuBar()
268 m_parentFrameActive
= true;
270 InsertWindowMenu(GetClientWindow(), m_hMenu
, GetMDIWindowMenu(this));
273 #endif // wxUSE_MENUS_NATIVE
275 void wxMDIParentFrame::SetWindowMenu(wxMenu
* menu
)
281 // Remove old window menu
282 RemoveWindowMenu(GetClientWindow(), m_hMenu
);
286 m_windowMenu
= (wxMenu
*) NULL
;
294 InsertWindowMenu(GetClientWindow(), m_hMenu
,
295 GetHmenuOf(m_windowMenu
));
300 void wxMDIParentFrame::DoMenuUpdates(wxMenu
* menu
)
302 wxMDIChildFrame
*child
= GetActiveChild();
305 wxEvtHandler
* source
= child
->GetEventHandler();
306 wxMenuBar
* bar
= child
->GetMenuBar();
310 menu
->UpdateUI(source
);
316 int nCount
= bar
->GetMenuCount();
317 for (int n
= 0; n
< nCount
; n
++)
318 bar
->GetMenu(n
)->UpdateUI(source
);
324 wxFrameBase::DoMenuUpdates(menu
);
328 void wxMDIParentFrame::UpdateClientSize()
330 if ( GetClientWindow() )
333 GetClientSize(&width
, &height
);
335 GetClientWindow()->SetSize(0, 0, width
, height
);
339 void wxMDIParentFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
343 // do not call event.Skip() here, it somehow messes up MDI client window
346 void wxMDIParentFrame::OnIconized(wxIconizeEvent
& event
)
350 if ( !event
.Iconized() )
356 // Returns the active MDI child window
357 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
359 HWND hWnd
= (HWND
)::SendMessage(GetWinHwnd(GetClientWindow()),
360 WM_MDIGETACTIVE
, 0, 0L);
364 return (wxMDIChildFrame
*)wxFindWinFromHandle((WXHWND
) hWnd
);
367 // Create the client window class (don't Create the window, just return a new
369 wxMDIClientWindow
*wxMDIParentFrame::OnCreateClient()
371 return new wxMDIClientWindow
;
374 // Responds to colour changes, and passes event on to children.
375 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
377 if ( m_clientWindow
)
379 m_clientWindow
->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
380 m_clientWindow
->Refresh();
386 WXHICON
wxMDIParentFrame::GetDefaultIcon() const
388 // we don't have any standard icons (any more)
392 // ---------------------------------------------------------------------------
394 // ---------------------------------------------------------------------------
396 void wxMDIParentFrame::Cascade()
398 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE
, 0, 0);
401 void wxMDIParentFrame::Tile(wxOrientation orient
)
403 wxASSERT_MSG( orient
== wxHORIZONTAL
|| orient
== wxVERTICAL
,
404 _T("invalid orientation value") );
406 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE
,
407 orient
== wxHORIZONTAL
? MDITILE_HORIZONTAL
408 : MDITILE_VERTICAL
, 0);
411 void wxMDIParentFrame::ArrangeIcons()
413 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE
, 0, 0);
416 void wxMDIParentFrame::ActivateNext()
418 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 0);
421 void wxMDIParentFrame::ActivatePrevious()
423 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 1);
426 // ---------------------------------------------------------------------------
427 // the MDI parent frame window proc
428 // ---------------------------------------------------------------------------
430 WXLRESULT
wxMDIParentFrame::MSWWindowProc(WXUINT message
,
435 bool processed
= false;
441 WXWORD state
, minimized
;
443 UnpackActivate(wParam
, lParam
, &state
, &minimized
, &hwnd
);
445 processed
= HandleActivate(state
, minimized
!= 0, hwnd
);
453 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
455 (void)HandleCommand(id
, cmd
, hwnd
);
457 // even if the frame didn't process it, there is no need to try it
458 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
459 // so pretend we processed the message anyhow
463 // always pass this message DefFrameProc(), otherwise MDI menu
464 // commands (and sys commands - more surprisingly!) won't work
465 MSWDefWindowProc(message
, wParam
, lParam
);
469 m_clientWindow
= OnCreateClient();
470 // Uses own style for client style
471 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
473 wxLogMessage(_("Failed to create MDI parent frame."));
484 // we erase background ourselves
492 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
494 if ( m_parentFrameActive
)
496 processed
= HandleMenuSelect(item
, flags
, hmenu
);
498 else if (m_currentChild
)
500 processed
= m_currentChild
->
501 HandleMenuSelect(item
, flags
, hmenu
);
507 // though we don't (usually) resize the MDI client to exactly fit the
508 // client area we need to pass this one to DefFrameProc to allow the children to show
513 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
518 bool wxMDIParentFrame::HandleActivate(int state
, bool minimized
, WXHWND activate
)
520 bool processed
= false;
522 if ( wxWindow::HandleActivate(state
, minimized
, activate
) )
528 // If this window is an MDI parent, we must also send an OnActivate message
529 // to the current child.
530 if ( (m_currentChild
!= NULL
) &&
531 ((state
== WA_ACTIVE
) || (state
== WA_CLICKACTIVE
)) )
533 wxActivateEvent
event(wxEVT_ACTIVATE
, true, m_currentChild
->GetId());
534 event
.SetEventObject( m_currentChild
);
535 if ( m_currentChild
->GetEventHandler()->ProcessEvent(event
) )
542 bool wxMDIParentFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND hwnd
)
544 // In case it's e.g. a toolbar.
547 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
549 return win
->MSWCommand(cmd
, id
);
552 // is it one of standard MDI commands?
558 case IDM_WINDOWCASCADE
:
560 wParam
= MDITILE_SKIPDISABLED
;
563 case IDM_WINDOWTILEHOR
:
564 wParam
|= MDITILE_HORIZONTAL
;
567 case IDM_WINDOWTILEVERT
:
569 wParam
= MDITILE_VERTICAL
;
571 wParam
|= MDITILE_SKIPDISABLED
;
574 case IDM_WINDOWICONS
:
575 msg
= WM_MDIICONARRANGE
;
580 lParam
= 0; // next child
585 lParam
= 1; // previous child
594 ::SendMessage(GetWinHwnd(GetClientWindow()), msg
, wParam
, lParam
);
599 // FIXME VZ: what does this test do??
602 return false; // Get WndProc to call default proc
605 if ( IsMdiCommandId(id
) )
607 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
610 wxWindow
*child
= node
->GetData();
611 if ( child
->GetHWND() )
613 long childId
= wxGetWindowId(child
->GetHWND());
614 if (childId
== (long)id
)
616 ::SendMessage( GetWinHwnd(GetClientWindow()),
618 (WPARAM
)child
->GetHWND(), 0);
622 node
= node
->GetNext();
625 else if ( m_parentFrameActive
)
627 return ProcessCommand(id
);
629 else if ( m_currentChild
)
631 return m_currentChild
->HandleCommand(id
, cmd
, hwnd
);
635 // this shouldn't happen because it means that our messages are being
636 // lost (they're not sent to the parent frame nor to the children)
637 wxFAIL_MSG(wxT("MDI parent frame is not active, yet there is no active MDI child?"));
643 WXLRESULT
wxMDIParentFrame::MSWDefWindowProc(WXUINT message
,
648 if ( GetClientWindow() )
649 clientWnd
= GetClientWindow()->GetHWND();
653 return DefFrameProc(GetHwnd(), (HWND
)clientWnd
, message
, wParam
, lParam
);
656 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG
* msg
)
658 MSG
*pMsg
= (MSG
*)msg
;
660 // first let the current child get it
661 if ( m_currentChild
&& m_currentChild
->GetHWND() &&
662 m_currentChild
->MSWTranslateMessage(msg
) )
667 // then try out accel table (will also check the menu accels)
668 if ( wxFrame::MSWTranslateMessage(msg
) )
673 // finally, check for MDI specific built in accel keys
674 if ( pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
676 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg
))
683 // ===========================================================================
685 // ===========================================================================
687 void wxMDIChildFrame::Init()
689 m_needsResize
= true;
690 m_needsInitialShow
= true;
693 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
695 const wxString
& title
,
699 const wxString
& name
)
703 if ( id
!= wxID_ANY
)
706 m_windowId
= (int)NewControlId();
710 parent
->AddChild(this);
720 mcs
.szClass
= style
& wxFULL_REPAINT_ON_RESIZE
721 ? wxMDIChildFrameClassName
722 : wxMDIChildFrameClassNameNoRedraw
;
724 mcs
.hOwner
= wxGetInstance();
725 if (x
!= wxDefaultCoord
)
728 mcs
.x
= CW_USEDEFAULT
;
730 if (y
!= wxDefaultCoord
)
733 mcs
.y
= CW_USEDEFAULT
;
735 if (width
!= wxDefaultCoord
)
738 mcs
.cx
= CW_USEDEFAULT
;
740 if (height
!= wxDefaultCoord
)
743 mcs
.cy
= CW_USEDEFAULT
;
745 DWORD msflags
= WS_OVERLAPPED
| WS_CLIPCHILDREN
;
746 if (style
& wxMINIMIZE_BOX
)
747 msflags
|= WS_MINIMIZEBOX
;
748 if (style
& wxMAXIMIZE_BOX
)
749 msflags
|= WS_MAXIMIZEBOX
;
750 if (style
& wxTHICK_FRAME
)
751 msflags
|= WS_THICKFRAME
;
752 if (style
& wxSYSTEM_MENU
)
753 msflags
|= WS_SYSMENU
;
754 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
755 msflags
|= WS_MINIMIZE
;
756 if (style
& wxMAXIMIZE
)
757 msflags
|= WS_MAXIMIZE
;
758 if (style
& wxCAPTION
)
759 msflags
|= WS_CAPTION
;
765 wxWindowCreationHook
hook(this);
767 m_hWnd
= (WXHWND
)::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
768 WM_MDICREATE
, 0, (LONG
)(LPSTR
)&mcs
);
770 wxAssociateWinWithHandle((HWND
) GetHWND(), this);
775 wxMDIChildFrame::~wxMDIChildFrame()
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;
796 return wxFrame::Show(show
);
799 // Set the client size (i.e. leave the calculation of borders etc.
801 void wxMDIChildFrame::DoSetClientSize(int width
, int height
)
803 HWND hWnd
= GetHwnd();
806 ::GetClientRect(hWnd
, &rect
);
809 GetWindowRect(hWnd
, &rect2
);
811 // Find the difference between the entire window (title bar and all)
812 // and the client area; add this to the new client size to move the
814 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
815 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
818 if (GetStatusBar() && GetStatusBar()->IsShown())
821 GetStatusBar()->GetSize(&sx
, &sy
);
824 #endif // wxUSE_STATUSBAR
827 point
.x
= rect2
.left
;
830 // If there's an MDI parent, must subtract the parent's top left corner
831 // since MoveWindow moves relative to the parent
832 wxMDIParentFrame
*mdiParent
= (wxMDIParentFrame
*)GetParent();
833 ::ScreenToClient((HWND
) mdiParent
->GetClientWindow()->GetHWND(), &point
);
835 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)true);
837 wxSize
size(width
, height
);
838 wxSizeEvent
event(size
, m_windowId
);
839 event
.SetEventObject( this );
840 GetEventHandler()->ProcessEvent(event
);
843 void wxMDIChildFrame::DoGetPosition(int *x
, int *y
) const
846 GetWindowRect(GetHwnd(), &rect
);
851 // Since we now have the absolute screen coords,
852 // if there's a parent we must subtract its top left corner
853 wxMDIParentFrame
*mdiParent
= (wxMDIParentFrame
*)GetParent();
854 ::ScreenToClient((HWND
) mdiParent
->GetClientWindow()->GetHWND(), &point
);
862 void wxMDIChildFrame::InternalSetMenuBar()
864 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
866 InsertWindowMenu(parent
->GetClientWindow(),
867 m_hMenu
, GetMDIWindowMenu(parent
));
869 parent
->m_parentFrameActive
= false;
872 void wxMDIChildFrame::DetachMenuBar()
874 RemoveWindowMenu(NULL
, m_hMenu
);
875 wxFrame::DetachMenuBar();
878 WXHICON
wxMDIChildFrame::GetDefaultIcon() const
880 // we don't have any standard icons (any more)
884 // ---------------------------------------------------------------------------
886 // ---------------------------------------------------------------------------
888 void wxMDIChildFrame::Maximize(bool maximize
)
890 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
891 if ( parent
&& parent
->GetClientWindow() )
893 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
894 maximize
? WM_MDIMAXIMIZE
: WM_MDIRESTORE
,
895 (WPARAM
)GetHwnd(), 0);
899 void wxMDIChildFrame::Restore()
901 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
902 if ( parent
&& parent
->GetClientWindow() )
904 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIRESTORE
,
905 (WPARAM
) GetHwnd(), 0);
909 void wxMDIChildFrame::Activate()
911 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
912 if ( parent
&& parent
->GetClientWindow() )
914 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIACTIVATE
,
915 (WPARAM
) GetHwnd(), 0);
919 // ---------------------------------------------------------------------------
920 // MDI window proc and message handlers
921 // ---------------------------------------------------------------------------
923 WXLRESULT
wxMDIChildFrame::MSWWindowProc(WXUINT message
,
928 bool processed
= false;
936 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
939 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
943 case WM_GETMINMAXINFO
:
944 processed
= HandleGetMinMaxInfo((MINMAXINFO
*)lParam
);
950 WXHWND hwndAct
, hwndDeact
;
951 UnpackMDIActivate(wParam
, lParam
, &act
, &hwndAct
, &hwndDeact
);
953 processed
= HandleMDIActivate(act
, hwndAct
, hwndDeact
);
958 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
959 // scrollbars if necessary
964 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
966 MSWDefWindowProc(message
, wParam
, lParam
);
970 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
971 // the message (the base class version does not)
972 return MSWDefWindowProc(message
, wParam
, lParam
);
974 case WM_WINDOWPOSCHANGING
:
975 processed
= HandleWindowPosChanging((LPWINDOWPOS
)lParam
);
980 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
985 bool wxMDIChildFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND hwnd
)
987 // In case it's e.g. a toolbar.
990 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
992 return win
->MSWCommand(cmd
, id
);
995 if (wxCurrentPopupMenu
)
997 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
998 wxCurrentPopupMenu
= NULL
;
999 if (popupMenu
->MSWCommand(cmd
, id
))
1004 if (GetMenuBar() && GetMenuBar()->FindItem(id
))
1006 processed
= ProcessCommand(id
);
1016 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate
),
1020 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
1022 HMENU menuToSet
= 0;
1026 if ( m_hWnd
== hwndAct
)
1029 parent
->m_currentChild
= this;
1031 HMENU child_menu
= (HMENU
)GetWinMenu();
1034 parent
->m_parentFrameActive
= false;
1036 menuToSet
= child_menu
;
1039 else if ( m_hWnd
== hwndDeact
)
1041 wxASSERT_MSG( parent
->m_currentChild
== this,
1042 wxT("can't deactivate MDI child which wasn't active!") );
1045 parent
->m_currentChild
= NULL
;
1047 HMENU parent_menu
= (HMENU
)parent
->GetWinMenu();
1049 // activate the the parent menu only when there is no other child
1050 // that has been activated
1051 if ( parent_menu
&& !hwndAct
)
1053 parent
->m_parentFrameActive
= true;
1055 menuToSet
= parent_menu
;
1060 // we have nothing to do with it
1066 MDISetMenu(parent
->GetClientWindow(),
1067 menuToSet
, GetMDIWindowMenu(parent
));
1070 wxActivateEvent
event(wxEVT_ACTIVATE
, activated
, m_windowId
);
1071 event
.SetEventObject( this );
1073 ResetWindowStyle((void *)NULL
);
1075 return GetEventHandler()->ProcessEvent(event
);
1078 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos
)
1080 WINDOWPOS
*lpPos
= (WINDOWPOS
*)pos
;
1081 #if defined(__WIN95__)
1082 if (!(lpPos
->flags
& SWP_NOSIZE
))
1085 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1086 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1087 if (ResetWindowStyle((void *) & rectClient
) && (dwStyle
& WS_MAXIMIZE
))
1089 ::AdjustWindowRectEx(&rectClient
, dwStyle
, false, dwExStyle
);
1090 lpPos
->x
= rectClient
.left
;
1091 lpPos
->y
= rectClient
.top
;
1092 lpPos
->cx
= rectClient
.right
- rectClient
.left
;
1093 lpPos
->cy
= rectClient
.bottom
- rectClient
.top
;
1096 wxMDIParentFrame
* pFrameWnd
= (wxMDIParentFrame
*)GetParent();
1097 if (pFrameWnd
&& pFrameWnd
->GetToolBar() && pFrameWnd
->GetToolBar()->IsShown())
1099 pFrameWnd
->GetToolBar()->Refresh();
1108 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo
)
1110 MINMAXINFO
*info
= (MINMAXINFO
*)mmInfo
;
1112 // let the default window proc calculate the size of MDI children
1113 // frames because it is based on the size of the MDI client window,
1114 // not on the values specified in wxWindow m_max variables
1115 bool processed
= MSWDefWindowProc(WM_GETMINMAXINFO
, 0, (LPARAM
)mmInfo
) != 0;
1117 int minWidth
= GetMinWidth(),
1118 minHeight
= GetMinHeight();
1120 // but allow GetSizeHints() to set the min size
1121 if ( minWidth
!= -1 )
1123 info
->ptMinTrackSize
.x
= minWidth
;
1128 if ( minHeight
!= -1 )
1130 info
->ptMinTrackSize
.y
= minHeight
;
1138 // ---------------------------------------------------------------------------
1139 // MDI specific message translation/preprocessing
1140 // ---------------------------------------------------------------------------
1142 WXLRESULT
wxMDIChildFrame::MSWDefWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1144 return DefMDIChildProc(GetHwnd(),
1145 (UINT
)message
, (WPARAM
)wParam
, (LPARAM
)lParam
);
1148 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG
* msg
)
1150 return wxFrame::MSWTranslateMessage(msg
);
1153 // ---------------------------------------------------------------------------
1155 // ---------------------------------------------------------------------------
1157 void wxMDIChildFrame::MSWDestroyWindow()
1159 invalidHandle
= GetHwnd();
1161 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
1163 // Must make sure this handle is invalidated (set to NULL) since all sorts
1164 // of things could happen after the child client is destroyed, but before
1165 // the wxFrame is destroyed.
1167 HWND oldHandle
= (HWND
)GetHWND();
1168 SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIDESTROY
,
1169 (WPARAM
)oldHandle
, 0);
1171 if (parent
->GetActiveChild() == (wxMDIChildFrame
*) NULL
)
1172 ResetWindowStyle((void*) NULL
);
1178 ::DestroyMenu((HMENU
) m_hMenu
);
1181 wxRemoveHandleAssociation(this);
1185 // Change the client window's extended style so we don't get a client edge
1186 // style when a child is maximised (a double border looks silly.)
1187 bool wxMDIChildFrame::ResetWindowStyle(void *vrect
)
1189 #if defined(__WIN95__)
1190 RECT
*rect
= (RECT
*)vrect
;
1191 wxMDIParentFrame
* pFrameWnd
= (wxMDIParentFrame
*)GetParent();
1192 wxMDIChildFrame
* pChild
= pFrameWnd
->GetActiveChild();
1193 if (!pChild
|| (pChild
== this))
1195 HWND hwndClient
= GetWinHwnd(pFrameWnd
->GetClientWindow());
1196 DWORD dwStyle
= ::GetWindowLong(hwndClient
, GWL_EXSTYLE
);
1198 // we want to test whether there is a maximized child, so just set
1199 // dwThisStyle to 0 if there is no child at all
1200 DWORD dwThisStyle
= pChild
1201 ? ::GetWindowLong(GetWinHwnd(pChild
), GWL_STYLE
) : 0;
1202 DWORD dwNewStyle
= dwStyle
;
1203 if ( dwThisStyle
& WS_MAXIMIZE
)
1204 dwNewStyle
&= ~(WS_EX_CLIENTEDGE
);
1206 dwNewStyle
|= WS_EX_CLIENTEDGE
;
1208 if (dwStyle
!= dwNewStyle
)
1210 // force update of everything
1211 ::RedrawWindow(hwndClient
, NULL
, NULL
,
1212 RDW_INVALIDATE
| RDW_ALLCHILDREN
);
1213 ::SetWindowLong(hwndClient
, GWL_EXSTYLE
, dwNewStyle
);
1214 ::SetWindowPos(hwndClient
, NULL
, 0, 0, 0, 0,
1215 SWP_FRAMECHANGED
| SWP_NOACTIVATE
|
1216 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1219 ::GetClientRect(hwndClient
, rect
);
1229 // ===========================================================================
1230 // wxMDIClientWindow: the window of predefined (by Windows) class which
1231 // contains the child frames
1232 // ===========================================================================
1234 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
1236 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1238 CLIENTCREATESTRUCT ccs
;
1239 m_windowStyle
= style
;
1242 ccs
.hWindowMenu
= GetMDIWindowMenu(parent
);
1243 ccs
.idFirstChild
= wxFIRST_MDI_CHILD
;
1245 DWORD msStyle
= MDIS_ALLCHILDSTYLES
| WS_VISIBLE
| WS_CHILD
|
1246 WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
;
1248 if ( style
& wxHSCROLL
)
1249 msStyle
|= WS_HSCROLL
;
1250 if ( style
& wxVSCROLL
)
1251 msStyle
|= WS_VSCROLL
;
1253 #if defined(__WIN95__)
1254 DWORD exStyle
= WS_EX_CLIENTEDGE
;
1259 wxWindowCreationHook
hook(this);
1260 m_hWnd
= (WXHWND
)::CreateWindowEx
1270 (LPSTR
)(LPCLIENTCREATESTRUCT
)&ccs
);
1273 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1278 SubclassWin(m_hWnd
);
1283 // Explicitly call default scroll behaviour
1284 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)
1286 // Note: for client windows, the scroll position is not set in
1287 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1288 // scroll position we're at.
1289 // This makes it hard to paint patterns or bitmaps in the background,
1290 // and have the client area scrollable as well.
1292 if ( event
.GetOrientation() == wxHORIZONTAL
)
1293 m_scrollX
= event
.GetPosition(); // Always returns zero!
1295 m_scrollY
= event
.GetPosition(); // Always returns zero!
1300 void wxMDIClientWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1302 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1303 // client area, you can end up with a non-refreshed portion in the client window
1304 // (see OGL studio sample). So check if the position is changed and if so,
1305 // redraw the MDI child frames.
1307 const wxPoint oldPos
= GetPosition();
1309 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
| wxSIZE_FORCE
);
1311 const wxPoint newPos
= GetPosition();
1313 if ((newPos
.x
!= oldPos
.x
) || (newPos
.y
!= oldPos
.y
))
1317 wxWindowList::compatibility_iterator node
= GetParent()->GetChildren().GetFirst();
1320 wxWindow
*child
= node
->GetData();
1321 if (child
->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
1323 ::RedrawWindow(GetHwndOf(child
),
1330 node
= node
->GetNext();
1336 void wxMDIChildFrame::OnIdle(wxIdleEvent
& event
)
1338 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1339 // in flicker e.g. when the frame contained controls with non-trivial
1340 // layout. Since 2.5.3, the frame is created hidden as all other top level
1341 // windows. In order to maintain backward compatibility, the frame is shown
1342 // in OnIdle, unless Show(false) was called by the programmer before.
1343 if ( m_needsInitialShow
)
1348 // MDI child frames get their WM_SIZE when they're constructed but at this
1349 // moment they don't have any children yet so all child windows will be
1350 // positioned incorrectly when they are added later - to fix this, we
1351 // generate an artificial size event here
1352 if ( m_needsResize
)
1354 m_needsResize
= false; // avoid any possibility of recursion
1362 // ---------------------------------------------------------------------------
1363 // non member functions
1364 // ---------------------------------------------------------------------------
1366 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
)
1368 ::SendMessage(GetWinHwnd(win
), WM_MDISETMENU
,
1370 (WPARAM
)hmenuFrame
, (LPARAM
)hmenuWindow
1372 0, MAKELPARAM(hmenuFrame
, hmenuWindow
)
1376 // update menu bar of the parent window
1377 wxWindow
*parent
= win
->GetParent();
1378 wxCHECK_RET( parent
, wxT("MDI client without parent frame? weird...") );
1380 ::SendMessage(GetWinHwnd(win
), WM_MDIREFRESHMENU
, 0, 0L);
1382 ::DrawMenuBar(GetWinHwnd(parent
));
1385 static void InsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
)
1387 // Try to insert Window menu in front of Help, otherwise append it.
1388 HMENU hmenu
= (HMENU
)menu
;
1392 int N
= GetMenuItemCount(hmenu
);
1393 bool success
= false;
1394 for ( int i
= 0; i
< N
; i
++ )
1397 int chars
= GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
);
1400 wxLogLastError(wxT("GetMenuString"));
1405 wxString
strBuf(buf
);
1406 if ( wxStripMenuCodes(strBuf
) == wxGetStockLabel(wxID_HELP
,false) )
1409 ::InsertMenu(hmenu
, i
, MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1410 (UINT
)subMenu
, _("&Window"));
1417 ::AppendMenu(hmenu
, MF_POPUP
, (UINT
)subMenu
, _("&Window"));
1421 MDISetMenu(win
, hmenu
, subMenu
);
1424 static void RemoveWindowMenu(wxWindow
*win
, WXHMENU menu
)
1426 HMENU hMenu
= (HMENU
)menu
;
1432 int N
= ::GetMenuItemCount(hMenu
);
1433 for ( int i
= 0; i
< N
; i
++ )
1435 if ( !::GetMenuString(hMenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1437 // Ignore successful read of menu string with length 0 which
1438 // occurs, for example, for a maximized MDI childs system menu
1439 if ( ::GetLastError() != 0 )
1441 wxLogLastError(wxT("GetMenuString"));
1447 if ( wxStrcmp(buf
, _("&Window")) == 0 )
1449 if ( !::RemoveMenu(hMenu
, i
, MF_BYPOSITION
) )
1451 wxLogLastError(wxT("RemoveMenu"));
1461 // we don't change the windows menu, but we update the main one
1462 MDISetMenu(win
, hMenu
, NULL
);
1466 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
1467 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
)
1470 *hwndAct
= (WXHWND
)lParam
;
1471 *hwndDeact
= (WXHWND
)wParam
;
1474 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)