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__)
35 #include "wx/dialog.h"
37 #include "wx/statusbr.h"
39 #include "wx/settings.h"
44 #include "wx/stockitem.h"
46 #include "wx/msw/private.h"
48 #if wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR
49 #include "wx/msw/statbr95.h"
53 #include "wx/toolbar.h"
54 #endif // wxUSE_TOOLBAR
58 // ---------------------------------------------------------------------------
60 // ---------------------------------------------------------------------------
62 extern wxMenu
*wxCurrentPopupMenu
;
64 extern const wxChar
*wxMDIFrameClassName
; // from app.cpp
65 extern const wxChar
*wxMDIChildFrameClassName
;
66 extern const wxChar
*wxMDIChildFrameClassNameNoRedraw
;
67 extern void wxAssociateWinWithHandle(HWND hWnd
, wxWindow
*win
);
68 extern void wxRemoveHandleAssociation(wxWindow
*win
);
70 static HWND invalidHandle
= 0;
72 // ---------------------------------------------------------------------------
74 // ---------------------------------------------------------------------------
76 static const int IDM_WINDOWTILEHOR
= 4001;
77 static const int IDM_WINDOWCASCADE
= 4002;
78 static const int IDM_WINDOWICONS
= 4003;
79 static const int IDM_WINDOWNEXT
= 4004;
80 static const int IDM_WINDOWTILEVERT
= 4005;
81 static const int IDM_WINDOWPREV
= 4006;
83 // This range gives a maximum of 500 MDI children. Should be enough :-)
84 static const int wxFIRST_MDI_CHILD
= 4100;
85 static const int wxLAST_MDI_CHILD
= 4600;
87 // ---------------------------------------------------------------------------
89 // ---------------------------------------------------------------------------
91 // set the MDI menus (by sending the WM_MDISETMENU message) and update the menu
92 // of the parent of win (which is supposed to be the MDI client window)
93 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
);
95 // insert the window menu (subMenu) into menu just before "Help" submenu or at
96 // the very end if not found
97 static void InsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
);
99 // Remove the window menu
100 static void RemoveWindowMenu(wxWindow
*win
, WXHMENU menu
);
102 // is this an id of an MDI child?
103 inline bool IsMdiCommandId(int id
)
105 return (id
>= wxFIRST_MDI_CHILD
) && (id
<= wxLAST_MDI_CHILD
);
108 // unpack the parameters of WM_MDIACTIVATE message
109 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
110 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
);
112 // return the HMENU of the MDI menu
113 static inline HMENU
GetMDIWindowMenu(wxMDIParentFrame
*frame
)
115 wxMenu
*menu
= frame
->GetWindowMenu();
116 return menu
? GetHmenuOf(menu
) : 0;
119 // ===========================================================================
121 // ===========================================================================
123 // ---------------------------------------------------------------------------
125 // ---------------------------------------------------------------------------
127 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame
, wxFrame
)
128 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame
, wxFrame
)
129 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow
, wxWindow
)
131 BEGIN_EVENT_TABLE(wxMDIParentFrame
, wxFrame
)
132 EVT_SIZE(wxMDIParentFrame::OnSize
)
133 EVT_ICONIZE(wxMDIParentFrame::OnIconized
)
134 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged
)
137 BEGIN_EVENT_TABLE(wxMDIChildFrame
, wxFrame
)
138 EVT_IDLE(wxMDIChildFrame::OnIdle
)
141 BEGIN_EVENT_TABLE(wxMDIClientWindow
, wxWindow
)
142 EVT_SCROLL(wxMDIClientWindow::OnScroll
)
145 // ===========================================================================
146 // wxMDIParentFrame: the frame which contains the client window which manages
148 // ===========================================================================
150 wxMDIParentFrame::wxMDIParentFrame()
152 m_clientWindow
= NULL
;
153 m_currentChild
= NULL
;
154 m_windowMenu
= (wxMenu
*) NULL
;
155 m_parentFrameActive
= true;
158 bool wxMDIParentFrame::Create(wxWindow
*parent
,
160 const wxString
& title
,
164 const wxString
& name
)
166 m_clientWindow
= NULL
;
167 m_currentChild
= NULL
;
169 // this style can be used to prevent a window from having the standard MDI
171 if ( style
& wxFRAME_NO_WINDOW_MENU
)
173 m_windowMenu
= (wxMenu
*)NULL
;
175 else // normal case: we have the window menu, so construct it
177 m_windowMenu
= new wxMenu
;
179 m_windowMenu
->Append(IDM_WINDOWCASCADE
, _("&Cascade"));
180 m_windowMenu
->Append(IDM_WINDOWTILEHOR
, _("Tile &Horizontally"));
181 m_windowMenu
->Append(IDM_WINDOWTILEVERT
, _("Tile &Vertically"));
182 m_windowMenu
->AppendSeparator();
183 m_windowMenu
->Append(IDM_WINDOWICONS
, _("&Arrange Icons"));
184 m_windowMenu
->Append(IDM_WINDOWNEXT
, _("&Next"));
185 m_windowMenu
->Append(IDM_WINDOWPREV
, _("&Previous"));
188 m_parentFrameActive
= true;
191 wxTopLevelWindows
.Append(this);
194 m_windowStyle
= style
;
197 parent
->AddChild(this);
199 if ( id
!= wxID_ANY
)
202 m_windowId
= NewControlId();
205 WXDWORD msflags
= MSWGetCreateWindowFlags(&exflags
);
206 msflags
&= ~WS_VSCROLL
;
207 msflags
&= ~WS_HSCROLL
;
209 if ( !wxWindow::MSWCreate(wxMDIFrameClassName
,
218 // unlike (almost?) all other windows, frames are created hidden
224 wxMDIParentFrame::~wxMDIParentFrame()
226 // see comment in ~wxMDIChildFrame
228 m_frameToolBar
= NULL
;
231 m_frameStatusBar
= NULL
;
232 #endif // wxUSE_STATUSBAR
239 m_windowMenu
= (wxMenu
*) NULL
;
242 // the MDI frame menubar is not automatically deleted by Windows unlike for
246 ::DestroyMenu((HMENU
)m_hMenu
);
247 m_hMenu
= (WXHMENU
)NULL
;
250 if ( m_clientWindow
)
252 if ( m_clientWindow
->MSWGetOldWndProc() )
253 m_clientWindow
->UnsubclassWin();
255 m_clientWindow
->SetHWND(0);
256 delete m_clientWindow
;
260 #if wxUSE_MENUS_NATIVE
262 void wxMDIParentFrame::InternalSetMenuBar()
264 m_parentFrameActive
= true;
266 InsertWindowMenu(GetClientWindow(), m_hMenu
, GetMDIWindowMenu(this));
269 #endif // wxUSE_MENUS_NATIVE
271 void wxMDIParentFrame::SetWindowMenu(wxMenu
* menu
)
277 // Remove old window menu
278 RemoveWindowMenu(GetClientWindow(), m_hMenu
);
282 m_windowMenu
= (wxMenu
*) NULL
;
290 InsertWindowMenu(GetClientWindow(), m_hMenu
,
291 GetHmenuOf(m_windowMenu
));
296 void wxMDIParentFrame::DoMenuUpdates(wxMenu
* menu
)
298 wxMDIChildFrame
*child
= GetActiveChild();
301 wxEvtHandler
* source
= child
->GetEventHandler();
302 wxMenuBar
* bar
= child
->GetMenuBar();
306 menu
->UpdateUI(source
);
312 int nCount
= bar
->GetMenuCount();
313 for (int n
= 0; n
< nCount
; n
++)
314 bar
->GetMenu(n
)->UpdateUI(source
);
320 wxFrameBase::DoMenuUpdates(menu
);
324 void wxMDIParentFrame::UpdateClientSize()
326 if ( GetClientWindow() )
329 GetClientSize(&width
, &height
);
331 GetClientWindow()->SetSize(0, 0, width
, height
);
335 void wxMDIParentFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
339 // do not call event.Skip() here, it somehow messes up MDI client window
342 void wxMDIParentFrame::OnIconized(wxIconizeEvent
& event
)
346 if ( !event
.Iconized() )
352 // Returns the active MDI child window
353 wxMDIChildFrame
*wxMDIParentFrame::GetActiveChild() const
355 HWND hWnd
= (HWND
)::SendMessage(GetWinHwnd(GetClientWindow()),
356 WM_MDIGETACTIVE
, 0, 0L);
360 return (wxMDIChildFrame
*)wxFindWinFromHandle((WXHWND
) hWnd
);
363 // Create the client window class (don't Create the window, just return a new
365 wxMDIClientWindow
*wxMDIParentFrame::OnCreateClient()
367 return new wxMDIClientWindow
;
370 // Responds to colour changes, and passes event on to children.
371 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
373 if ( m_clientWindow
)
375 m_clientWindow
->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
));
376 m_clientWindow
->Refresh();
382 WXHICON
wxMDIParentFrame::GetDefaultIcon() const
384 // we don't have any standard icons (any more)
388 // ---------------------------------------------------------------------------
390 // ---------------------------------------------------------------------------
392 void wxMDIParentFrame::Cascade()
394 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE
, 0, 0);
397 void wxMDIParentFrame::Tile(wxOrientation orient
)
399 wxASSERT_MSG( orient
== wxHORIZONTAL
|| orient
== wxVERTICAL
,
400 _T("invalid orientation value") );
402 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE
,
403 orient
== wxHORIZONTAL
? MDITILE_HORIZONTAL
404 : MDITILE_VERTICAL
, 0);
407 void wxMDIParentFrame::ArrangeIcons()
409 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE
, 0, 0);
412 void wxMDIParentFrame::ActivateNext()
414 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 0);
417 void wxMDIParentFrame::ActivatePrevious()
419 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT
, 0, 1);
422 // ---------------------------------------------------------------------------
423 // the MDI parent frame window proc
424 // ---------------------------------------------------------------------------
426 WXLRESULT
wxMDIParentFrame::MSWWindowProc(WXUINT message
,
431 bool processed
= false;
437 WXWORD state
, minimized
;
439 UnpackActivate(wParam
, lParam
, &state
, &minimized
, &hwnd
);
441 processed
= HandleActivate(state
, minimized
!= 0, hwnd
);
449 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
451 (void)HandleCommand(id
, cmd
, hwnd
);
453 // even if the frame didn't process it, there is no need to try it
454 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
455 // so pretend we processed the message anyhow
459 // always pass this message DefFrameProc(), otherwise MDI menu
460 // commands (and sys commands - more surprisingly!) won't work
461 MSWDefWindowProc(message
, wParam
, lParam
);
465 m_clientWindow
= OnCreateClient();
466 // Uses own style for client style
467 if ( !m_clientWindow
->CreateClient(this, GetWindowStyleFlag()) )
469 wxLogMessage(_("Failed to create MDI parent frame."));
480 // we erase background ourselves
488 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
490 if ( m_parentFrameActive
)
492 processed
= HandleMenuSelect(item
, flags
, hmenu
);
494 else if (m_currentChild
)
496 processed
= m_currentChild
->
497 HandleMenuSelect(item
, flags
, hmenu
);
503 // though we don't (usually) resize the MDI client to exactly fit the
504 // client area we need to pass this one to DefFrameProc to allow the children to show
509 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
514 bool wxMDIParentFrame::HandleActivate(int state
, bool minimized
, WXHWND activate
)
516 bool processed
= false;
518 if ( wxWindow::HandleActivate(state
, minimized
, activate
) )
524 // If this window is an MDI parent, we must also send an OnActivate message
525 // to the current child.
526 if ( (m_currentChild
!= NULL
) &&
527 ((state
== WA_ACTIVE
) || (state
== WA_CLICKACTIVE
)) )
529 wxActivateEvent
event(wxEVT_ACTIVATE
, true, m_currentChild
->GetId());
530 event
.SetEventObject( m_currentChild
);
531 if ( m_currentChild
->GetEventHandler()->ProcessEvent(event
) )
538 bool wxMDIParentFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND hwnd
)
540 // In case it's e.g. a toolbar.
543 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
545 return win
->MSWCommand(cmd
, id
);
548 // is it one of standard MDI commands?
554 case IDM_WINDOWCASCADE
:
556 wParam
= MDITILE_SKIPDISABLED
;
559 case IDM_WINDOWTILEHOR
:
560 wParam
|= MDITILE_HORIZONTAL
;
563 case IDM_WINDOWTILEVERT
:
565 wParam
= MDITILE_VERTICAL
;
567 wParam
|= MDITILE_SKIPDISABLED
;
570 case IDM_WINDOWICONS
:
571 msg
= WM_MDIICONARRANGE
;
576 lParam
= 0; // next child
581 lParam
= 1; // previous child
590 ::SendMessage(GetWinHwnd(GetClientWindow()), msg
, wParam
, lParam
);
595 // FIXME VZ: what does this test do??
598 return false; // Get WndProc to call default proc
601 if ( IsMdiCommandId(id
) )
603 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
606 wxWindow
*child
= node
->GetData();
607 if ( child
->GetHWND() )
609 long childId
= wxGetWindowId(child
->GetHWND());
610 if (childId
== (long)id
)
612 ::SendMessage( GetWinHwnd(GetClientWindow()),
614 (WPARAM
)child
->GetHWND(), 0);
618 node
= node
->GetNext();
621 else if ( m_parentFrameActive
)
623 return ProcessCommand(id
);
625 else if ( m_currentChild
)
627 return m_currentChild
->HandleCommand(id
, cmd
, hwnd
);
631 // this shouldn't happen because it means that our messages are being
632 // lost (they're not sent to the parent frame nor to the children)
633 wxFAIL_MSG(wxT("MDI parent frame is not active, yet there is no active MDI child?"));
639 WXLRESULT
wxMDIParentFrame::MSWDefWindowProc(WXUINT message
,
644 if ( GetClientWindow() )
645 clientWnd
= GetClientWindow()->GetHWND();
649 return DefFrameProc(GetHwnd(), (HWND
)clientWnd
, message
, wParam
, lParam
);
652 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG
* msg
)
654 MSG
*pMsg
= (MSG
*)msg
;
656 // first let the current child get it
657 if ( m_currentChild
&& m_currentChild
->GetHWND() &&
658 m_currentChild
->MSWTranslateMessage(msg
) )
663 // then try out accel table (will also check the menu accels)
664 if ( wxFrame::MSWTranslateMessage(msg
) )
669 // finally, check for MDI specific built in accel keys
670 if ( pMsg
->message
== WM_KEYDOWN
|| pMsg
->message
== WM_SYSKEYDOWN
)
672 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg
))
679 // ===========================================================================
681 // ===========================================================================
683 void wxMDIChildFrame::Init()
685 m_needsResize
= true;
686 m_needsInitialShow
= true;
689 bool wxMDIChildFrame::Create(wxMDIParentFrame
*parent
,
691 const wxString
& title
,
695 const wxString
& name
)
699 if ( id
!= wxID_ANY
)
702 m_windowId
= (int)NewControlId();
706 parent
->AddChild(this);
716 mcs
.szClass
= style
& wxFULL_REPAINT_ON_RESIZE
717 ? wxMDIChildFrameClassName
718 : wxMDIChildFrameClassNameNoRedraw
;
720 mcs
.hOwner
= wxGetInstance();
721 if (x
!= wxDefaultCoord
)
724 mcs
.x
= CW_USEDEFAULT
;
726 if (y
!= wxDefaultCoord
)
729 mcs
.y
= CW_USEDEFAULT
;
731 if (width
!= wxDefaultCoord
)
734 mcs
.cx
= CW_USEDEFAULT
;
736 if (height
!= wxDefaultCoord
)
739 mcs
.cy
= CW_USEDEFAULT
;
741 DWORD msflags
= WS_OVERLAPPED
| WS_CLIPCHILDREN
;
742 if (style
& wxMINIMIZE_BOX
)
743 msflags
|= WS_MINIMIZEBOX
;
744 if (style
& wxMAXIMIZE_BOX
)
745 msflags
|= WS_MAXIMIZEBOX
;
746 if (style
& wxTHICK_FRAME
)
747 msflags
|= WS_THICKFRAME
;
748 if (style
& wxSYSTEM_MENU
)
749 msflags
|= WS_SYSMENU
;
750 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
751 msflags
|= WS_MINIMIZE
;
752 if (style
& wxMAXIMIZE
)
753 msflags
|= WS_MAXIMIZE
;
754 if (style
& wxCAPTION
)
755 msflags
|= WS_CAPTION
;
761 wxWindowCreationHook
hook(this);
763 m_hWnd
= (WXHWND
)::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
764 WM_MDICREATE
, 0, (LONG
)(LPSTR
)&mcs
);
766 wxAssociateWinWithHandle((HWND
) GetHWND(), this);
771 wxMDIChildFrame::~wxMDIChildFrame()
773 // will be destroyed by DestroyChildren() but reset them before calling it
774 // to avoid using dangling pointers if a callback comes in the meanwhile
776 m_frameToolBar
= NULL
;
779 m_frameStatusBar
= NULL
;
780 #endif // wxUSE_STATUSBAR
784 RemoveWindowMenu(NULL
, m_hMenu
);
789 bool wxMDIChildFrame::Show(bool show
)
791 m_needsInitialShow
= false;
793 if (!wxFrame::Show(show
))
796 // KH: Without this call, new MDI children do not become active.
797 // This was added here after the same BringWindowToTop call was
798 // removed from wxTopLevelWindow::Show (November 2005)
800 ::BringWindowToTop(GetHwnd());
805 // Set the client size (i.e. leave the calculation of borders etc.
807 void wxMDIChildFrame::DoSetClientSize(int width
, int height
)
809 HWND hWnd
= GetHwnd();
812 ::GetClientRect(hWnd
, &rect
);
815 GetWindowRect(hWnd
, &rect2
);
817 // Find the difference between the entire window (title bar and all)
818 // and the client area; add this to the new client size to move the
820 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
821 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
824 if (GetStatusBar() && GetStatusBar()->IsShown())
827 GetStatusBar()->GetSize(&sx
, &sy
);
830 #endif // wxUSE_STATUSBAR
833 point
.x
= rect2
.left
;
836 // If there's an MDI parent, must subtract the parent's top left corner
837 // since MoveWindow moves relative to the parent
838 wxMDIParentFrame
*mdiParent
= (wxMDIParentFrame
*)GetParent();
839 ::ScreenToClient((HWND
) mdiParent
->GetClientWindow()->GetHWND(), &point
);
841 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)true);
843 wxSize
size(width
, height
);
844 wxSizeEvent
event(size
, m_windowId
);
845 event
.SetEventObject( this );
846 GetEventHandler()->ProcessEvent(event
);
849 void wxMDIChildFrame::DoGetPosition(int *x
, int *y
) const
852 GetWindowRect(GetHwnd(), &rect
);
857 // Since we now have the absolute screen coords,
858 // if there's a parent we must subtract its top left corner
859 wxMDIParentFrame
*mdiParent
= (wxMDIParentFrame
*)GetParent();
860 ::ScreenToClient((HWND
) mdiParent
->GetClientWindow()->GetHWND(), &point
);
868 void wxMDIChildFrame::InternalSetMenuBar()
870 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
872 InsertWindowMenu(parent
->GetClientWindow(),
873 m_hMenu
, GetMDIWindowMenu(parent
));
875 parent
->m_parentFrameActive
= false;
878 void wxMDIChildFrame::DetachMenuBar()
880 RemoveWindowMenu(NULL
, m_hMenu
);
881 wxFrame::DetachMenuBar();
884 WXHICON
wxMDIChildFrame::GetDefaultIcon() const
886 // we don't have any standard icons (any more)
890 // ---------------------------------------------------------------------------
892 // ---------------------------------------------------------------------------
894 void wxMDIChildFrame::Maximize(bool maximize
)
896 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
897 if ( parent
&& parent
->GetClientWindow() )
899 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()),
900 maximize
? WM_MDIMAXIMIZE
: WM_MDIRESTORE
,
901 (WPARAM
)GetHwnd(), 0);
905 void wxMDIChildFrame::Restore()
907 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
908 if ( parent
&& parent
->GetClientWindow() )
910 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIRESTORE
,
911 (WPARAM
) GetHwnd(), 0);
915 void wxMDIChildFrame::Activate()
917 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
918 if ( parent
&& parent
->GetClientWindow() )
920 ::SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIACTIVATE
,
921 (WPARAM
) GetHwnd(), 0);
925 // ---------------------------------------------------------------------------
926 // MDI window proc and message handlers
927 // ---------------------------------------------------------------------------
929 WXLRESULT
wxMDIChildFrame::MSWWindowProc(WXUINT message
,
934 bool processed
= false;
942 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
945 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
949 case WM_GETMINMAXINFO
:
950 processed
= HandleGetMinMaxInfo((MINMAXINFO
*)lParam
);
956 WXHWND hwndAct
, hwndDeact
;
957 UnpackMDIActivate(wParam
, lParam
, &act
, &hwndAct
, &hwndDeact
);
959 processed
= HandleMDIActivate(act
, hwndAct
, hwndDeact
);
964 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
965 // scrollbars if necessary
970 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
972 MSWDefWindowProc(message
, wParam
, lParam
);
976 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
977 // the message (the base class version does not)
978 return MSWDefWindowProc(message
, wParam
, lParam
);
980 case WM_WINDOWPOSCHANGING
:
981 processed
= HandleWindowPosChanging((LPWINDOWPOS
)lParam
);
986 rc
= wxFrame::MSWWindowProc(message
, wParam
, lParam
);
991 bool wxMDIChildFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND hwnd
)
993 // In case it's e.g. a toolbar.
996 wxWindow
*win
= wxFindWinFromHandle(hwnd
);
998 return win
->MSWCommand(cmd
, id
);
1001 if (wxCurrentPopupMenu
)
1003 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
1004 wxCurrentPopupMenu
= NULL
;
1005 if (popupMenu
->MSWCommand(cmd
, id
))
1010 if (GetMenuBar() && GetMenuBar()->FindItem(id
))
1012 processed
= ProcessCommand(id
);
1022 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate
),
1026 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
1028 HMENU menuToSet
= 0;
1032 if ( m_hWnd
== hwndAct
)
1035 parent
->m_currentChild
= this;
1037 HMENU child_menu
= (HMENU
)GetWinMenu();
1040 parent
->m_parentFrameActive
= false;
1042 menuToSet
= child_menu
;
1045 else if ( m_hWnd
== hwndDeact
)
1047 wxASSERT_MSG( parent
->m_currentChild
== this,
1048 wxT("can't deactivate MDI child which wasn't active!") );
1051 parent
->m_currentChild
= NULL
;
1053 HMENU parent_menu
= (HMENU
)parent
->GetWinMenu();
1055 // activate the the parent menu only when there is no other child
1056 // that has been activated
1057 if ( parent_menu
&& !hwndAct
)
1059 parent
->m_parentFrameActive
= true;
1061 menuToSet
= parent_menu
;
1066 // we have nothing to do with it
1072 MDISetMenu(parent
->GetClientWindow(),
1073 menuToSet
, GetMDIWindowMenu(parent
));
1076 wxActivateEvent
event(wxEVT_ACTIVATE
, activated
, m_windowId
);
1077 event
.SetEventObject( this );
1079 ResetWindowStyle((void *)NULL
);
1081 return GetEventHandler()->ProcessEvent(event
);
1084 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos
)
1086 WINDOWPOS
*lpPos
= (WINDOWPOS
*)pos
;
1087 #if defined(__WIN95__)
1088 if (!(lpPos
->flags
& SWP_NOSIZE
))
1091 DWORD dwExStyle
= ::GetWindowLong(GetHwnd(), GWL_EXSTYLE
);
1092 DWORD dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1093 if (ResetWindowStyle((void *) & rectClient
) && (dwStyle
& WS_MAXIMIZE
))
1095 ::AdjustWindowRectEx(&rectClient
, dwStyle
, false, dwExStyle
);
1096 lpPos
->x
= rectClient
.left
;
1097 lpPos
->y
= rectClient
.top
;
1098 lpPos
->cx
= rectClient
.right
- rectClient
.left
;
1099 lpPos
->cy
= rectClient
.bottom
- rectClient
.top
;
1102 wxMDIParentFrame
* pFrameWnd
= (wxMDIParentFrame
*)GetParent();
1103 if (pFrameWnd
&& pFrameWnd
->GetToolBar() && pFrameWnd
->GetToolBar()->IsShown())
1105 pFrameWnd
->GetToolBar()->Refresh();
1114 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo
)
1116 MINMAXINFO
*info
= (MINMAXINFO
*)mmInfo
;
1118 // let the default window proc calculate the size of MDI children
1119 // frames because it is based on the size of the MDI client window,
1120 // not on the values specified in wxWindow m_max variables
1121 bool processed
= MSWDefWindowProc(WM_GETMINMAXINFO
, 0, (LPARAM
)mmInfo
) != 0;
1123 int minWidth
= GetMinWidth(),
1124 minHeight
= GetMinHeight();
1126 // but allow GetSizeHints() to set the min size
1127 if ( minWidth
!= -1 )
1129 info
->ptMinTrackSize
.x
= minWidth
;
1134 if ( minHeight
!= -1 )
1136 info
->ptMinTrackSize
.y
= minHeight
;
1144 // ---------------------------------------------------------------------------
1145 // MDI specific message translation/preprocessing
1146 // ---------------------------------------------------------------------------
1148 WXLRESULT
wxMDIChildFrame::MSWDefWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1150 return DefMDIChildProc(GetHwnd(),
1151 (UINT
)message
, (WPARAM
)wParam
, (LPARAM
)lParam
);
1154 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG
* msg
)
1156 return wxFrame::MSWTranslateMessage(msg
);
1159 // ---------------------------------------------------------------------------
1161 // ---------------------------------------------------------------------------
1163 void wxMDIChildFrame::MSWDestroyWindow()
1165 invalidHandle
= GetHwnd();
1167 wxMDIParentFrame
*parent
= (wxMDIParentFrame
*)GetParent();
1169 // Must make sure this handle is invalidated (set to NULL) since all sorts
1170 // of things could happen after the child client is destroyed, but before
1171 // the wxFrame is destroyed.
1173 HWND oldHandle
= (HWND
)GetHWND();
1174 SendMessage(GetWinHwnd(parent
->GetClientWindow()), WM_MDIDESTROY
,
1175 (WPARAM
)oldHandle
, 0);
1177 if (parent
->GetActiveChild() == (wxMDIChildFrame
*) NULL
)
1178 ResetWindowStyle((void*) NULL
);
1184 ::DestroyMenu((HMENU
) m_hMenu
);
1187 wxRemoveHandleAssociation(this);
1191 // Change the client window's extended style so we don't get a client edge
1192 // style when a child is maximised (a double border looks silly.)
1193 bool wxMDIChildFrame::ResetWindowStyle(void *vrect
)
1195 #if defined(__WIN95__)
1196 RECT
*rect
= (RECT
*)vrect
;
1197 wxMDIParentFrame
* pFrameWnd
= (wxMDIParentFrame
*)GetParent();
1198 wxMDIChildFrame
* pChild
= pFrameWnd
->GetActiveChild();
1199 if (!pChild
|| (pChild
== this))
1201 HWND hwndClient
= GetWinHwnd(pFrameWnd
->GetClientWindow());
1202 DWORD dwStyle
= ::GetWindowLong(hwndClient
, GWL_EXSTYLE
);
1204 // we want to test whether there is a maximized child, so just set
1205 // dwThisStyle to 0 if there is no child at all
1206 DWORD dwThisStyle
= pChild
1207 ? ::GetWindowLong(GetWinHwnd(pChild
), GWL_STYLE
) : 0;
1208 DWORD dwNewStyle
= dwStyle
;
1209 if ( dwThisStyle
& WS_MAXIMIZE
)
1210 dwNewStyle
&= ~(WS_EX_CLIENTEDGE
);
1212 dwNewStyle
|= WS_EX_CLIENTEDGE
;
1214 if (dwStyle
!= dwNewStyle
)
1216 // force update of everything
1217 ::RedrawWindow(hwndClient
, NULL
, NULL
,
1218 RDW_INVALIDATE
| RDW_ALLCHILDREN
);
1219 ::SetWindowLong(hwndClient
, GWL_EXSTYLE
, dwNewStyle
);
1220 ::SetWindowPos(hwndClient
, NULL
, 0, 0, 0, 0,
1221 SWP_FRAMECHANGED
| SWP_NOACTIVATE
|
1222 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1225 ::GetClientRect(hwndClient
, rect
);
1235 // ===========================================================================
1236 // wxMDIClientWindow: the window of predefined (by Windows) class which
1237 // contains the child frames
1238 // ===========================================================================
1240 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame
*parent
, long style
)
1242 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1244 CLIENTCREATESTRUCT ccs
;
1245 m_windowStyle
= style
;
1248 ccs
.hWindowMenu
= GetMDIWindowMenu(parent
);
1249 ccs
.idFirstChild
= wxFIRST_MDI_CHILD
;
1251 DWORD msStyle
= MDIS_ALLCHILDSTYLES
| WS_VISIBLE
| WS_CHILD
|
1252 WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
;
1254 if ( style
& wxHSCROLL
)
1255 msStyle
|= WS_HSCROLL
;
1256 if ( style
& wxVSCROLL
)
1257 msStyle
|= WS_VSCROLL
;
1259 #if defined(__WIN95__)
1260 DWORD exStyle
= WS_EX_CLIENTEDGE
;
1265 wxWindowCreationHook
hook(this);
1266 m_hWnd
= (WXHWND
)::CreateWindowEx
1276 (LPSTR
)(LPCLIENTCREATESTRUCT
)&ccs
);
1279 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1284 SubclassWin(m_hWnd
);
1289 // Explicitly call default scroll behaviour
1290 void wxMDIClientWindow::OnScroll(wxScrollEvent
& event
)
1292 // Note: for client windows, the scroll position is not set in
1293 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1294 // scroll position we're at.
1295 // This makes it hard to paint patterns or bitmaps in the background,
1296 // and have the client area scrollable as well.
1298 if ( event
.GetOrientation() == wxHORIZONTAL
)
1299 m_scrollX
= event
.GetPosition(); // Always returns zero!
1301 m_scrollY
= event
.GetPosition(); // Always returns zero!
1306 void wxMDIClientWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1308 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1309 // client area, you can end up with a non-refreshed portion in the client window
1310 // (see OGL studio sample). So check if the position is changed and if so,
1311 // redraw the MDI child frames.
1313 const wxPoint oldPos
= GetPosition();
1315 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
| wxSIZE_FORCE
);
1317 const wxPoint newPos
= GetPosition();
1319 if ((newPos
.x
!= oldPos
.x
) || (newPos
.y
!= oldPos
.y
))
1323 wxWindowList::compatibility_iterator node
= GetParent()->GetChildren().GetFirst();
1326 wxWindow
*child
= node
->GetData();
1327 if (child
->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
1329 ::RedrawWindow(GetHwndOf(child
),
1336 node
= node
->GetNext();
1342 void wxMDIChildFrame::OnIdle(wxIdleEvent
& event
)
1344 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1345 // in flicker e.g. when the frame contained controls with non-trivial
1346 // layout. Since 2.5.3, the frame is created hidden as all other top level
1347 // windows. In order to maintain backward compatibility, the frame is shown
1348 // in OnIdle, unless Show(false) was called by the programmer before.
1349 if ( m_needsInitialShow
)
1354 // MDI child frames get their WM_SIZE when they're constructed but at this
1355 // moment they don't have any children yet so all child windows will be
1356 // positioned incorrectly when they are added later - to fix this, we
1357 // generate an artificial size event here
1358 if ( m_needsResize
)
1360 m_needsResize
= false; // avoid any possibility of recursion
1368 // ---------------------------------------------------------------------------
1369 // non member functions
1370 // ---------------------------------------------------------------------------
1372 static void MDISetMenu(wxWindow
*win
, HMENU hmenuFrame
, HMENU hmenuWindow
)
1374 ::SendMessage(GetWinHwnd(win
), WM_MDISETMENU
,
1376 (WPARAM
)hmenuFrame
, (LPARAM
)hmenuWindow
1378 0, MAKELPARAM(hmenuFrame
, hmenuWindow
)
1382 // update menu bar of the parent window
1383 wxWindow
*parent
= win
->GetParent();
1384 wxCHECK_RET( parent
, wxT("MDI client without parent frame? weird...") );
1386 ::SendMessage(GetWinHwnd(win
), WM_MDIREFRESHMENU
, 0, 0L);
1388 ::DrawMenuBar(GetWinHwnd(parent
));
1391 static void InsertWindowMenu(wxWindow
*win
, WXHMENU menu
, HMENU subMenu
)
1393 // Try to insert Window menu in front of Help, otherwise append it.
1394 HMENU hmenu
= (HMENU
)menu
;
1398 int N
= GetMenuItemCount(hmenu
);
1399 bool success
= false;
1400 for ( int i
= 0; i
< N
; i
++ )
1403 int chars
= GetMenuString(hmenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
);
1406 wxLogLastError(wxT("GetMenuString"));
1411 wxString
strBuf(buf
);
1412 if ( wxStripMenuCodes(strBuf
) == wxGetStockLabel(wxID_HELP
,false) )
1415 ::InsertMenu(hmenu
, i
, MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1416 (UINT
)subMenu
, _("&Window"));
1423 ::AppendMenu(hmenu
, MF_POPUP
, (UINT
)subMenu
, _("&Window"));
1427 MDISetMenu(win
, hmenu
, subMenu
);
1430 static void RemoveWindowMenu(wxWindow
*win
, WXHMENU menu
)
1432 HMENU hMenu
= (HMENU
)menu
;
1438 int N
= ::GetMenuItemCount(hMenu
);
1439 for ( int i
= 0; i
< N
; i
++ )
1441 if ( !::GetMenuString(hMenu
, i
, buf
, WXSIZEOF(buf
), MF_BYPOSITION
) )
1443 // Ignore successful read of menu string with length 0 which
1444 // occurs, for example, for a maximized MDI childs system menu
1445 if ( ::GetLastError() != 0 )
1447 wxLogLastError(wxT("GetMenuString"));
1453 if ( wxStrcmp(buf
, _("&Window")) == 0 )
1455 if ( !::RemoveMenu(hMenu
, i
, MF_BYPOSITION
) )
1457 wxLogLastError(wxT("RemoveMenu"));
1467 // we don't change the windows menu, but we update the main one
1468 MDISetMenu(win
, hMenu
, NULL
);
1472 static void UnpackMDIActivate(WXWPARAM wParam
, WXLPARAM lParam
,
1473 WXWORD
*activate
, WXHWND
*hwndAct
, WXHWND
*hwndDeact
)
1476 *hwndAct
= (WXHWND
)lParam
;
1477 *hwndDeact
= (WXHWND
)wParam
;
1480 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)