disable the "Next" and "Previous" commands in the window menu if we have a single...
[wxWidgets.git] / src / msw / mdi.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/mdi.cpp
3 // Purpose: MDI classes for wxMSW
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin on 2008-11-04 to use the base classes
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Julian Smart
9 // (c) 2008-2009 Vadim Zeitlin
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 // ===========================================================================
14 // declarations
15 // ===========================================================================
16
17 // ---------------------------------------------------------------------------
18 // headers
19 // ---------------------------------------------------------------------------
20
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #if wxUSE_MDI && !defined(__WXUNIVERSAL__)
29
30 #include "wx/mdi.h"
31
32 #ifndef WX_PRECOMP
33 #include "wx/frame.h"
34 #include "wx/menu.h"
35 #include "wx/app.h"
36 #include "wx/utils.h"
37 #include "wx/dialog.h"
38 #include "wx/statusbr.h"
39 #include "wx/settings.h"
40 #include "wx/intl.h"
41 #include "wx/log.h"
42 #include "wx/toolbar.h"
43 #endif
44
45 #include "wx/stockitem.h"
46 #include "wx/msw/private.h"
47
48 #include <string.h>
49
50 // ---------------------------------------------------------------------------
51 // global variables
52 // ---------------------------------------------------------------------------
53
54 extern wxMenu *wxCurrentPopupMenu;
55
56 extern void wxRemoveHandleAssociation(wxWindow *win);
57
58 // ---------------------------------------------------------------------------
59 // constants
60 // ---------------------------------------------------------------------------
61
62 static const int IDM_WINDOWTILEHOR = 4001;
63 static const int IDM_WINDOWCASCADE = 4002;
64 static const int IDM_WINDOWICONS = 4003;
65 static const int IDM_WINDOWNEXT = 4004;
66 static const int IDM_WINDOWTILEVERT = 4005;
67 static const int IDM_WINDOWPREV = 4006;
68
69 // This range gives a maximum of 500 MDI children. Should be enough :-)
70 static const int wxFIRST_MDI_CHILD = 4100;
71 static const int wxLAST_MDI_CHILD = 4600;
72
73 // ---------------------------------------------------------------------------
74 // private functions
75 // ---------------------------------------------------------------------------
76
77 // set the MDI menus (by sending the WM_MDISETMENU message) and update the menu
78 // of the parent of win (which is supposed to be the MDI client window)
79 static void MDISetMenu(wxWindow *win, HMENU hmenuFrame, HMENU hmenuWindow);
80
81 // insert the window menu (subMenu) into menu just before "Help" submenu or at
82 // the very end if not found
83 static void MDIInsertWindowMenu(wxWindow *win, WXHMENU menu, HMENU subMenu);
84
85 // Remove the window menu
86 static void MDIRemoveWindowMenu(wxWindow *win, WXHMENU menu);
87
88 // is this an id of an MDI child?
89 inline bool IsMdiCommandId(int id)
90 {
91 return (id >= wxFIRST_MDI_CHILD) && (id <= wxLAST_MDI_CHILD);
92 }
93
94 // unpack the parameters of WM_MDIACTIVATE message
95 static void UnpackMDIActivate(WXWPARAM wParam, WXLPARAM lParam,
96 WXWORD *activate, WXHWND *hwndAct, WXHWND *hwndDeact);
97
98 // return the HMENU of the MDI menu
99 //
100 // this function works correctly even when we don't have a window menu and just
101 // returns 0 then
102 static inline HMENU GetMDIWindowMenu(wxMDIParentFrame *frame)
103 {
104 wxMenu *menu = frame->GetWindowMenu();
105 return menu ? GetHmenuOf(menu) : 0;
106 }
107
108 // ===========================================================================
109 // implementation
110 // ===========================================================================
111
112 // ---------------------------------------------------------------------------
113 // wxWin macros
114 // ---------------------------------------------------------------------------
115
116 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame, wxFrame)
117 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame, wxFrame)
118 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow, wxWindow)
119
120 BEGIN_EVENT_TABLE(wxMDIParentFrame, wxFrame)
121 EVT_SIZE(wxMDIParentFrame::OnSize)
122 EVT_ICONIZE(wxMDIParentFrame::OnIconized)
123 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged)
124 END_EVENT_TABLE()
125
126 BEGIN_EVENT_TABLE(wxMDIChildFrame, wxFrame)
127 EVT_IDLE(wxMDIChildFrame::OnIdle)
128 END_EVENT_TABLE()
129
130 BEGIN_EVENT_TABLE(wxMDIClientWindow, wxWindow)
131 EVT_SCROLL(wxMDIClientWindow::OnScroll)
132 END_EVENT_TABLE()
133
134 // ===========================================================================
135 // wxMDIParentFrame: the frame which contains the client window which manages
136 // the children
137 // ===========================================================================
138
139 wxMDIParentFrame::wxMDIParentFrame()
140 {
141 m_parentFrameActive = true;
142 }
143
144 bool wxMDIParentFrame::Create(wxWindow *parent,
145 wxWindowID id,
146 const wxString& title,
147 const wxPoint& pos,
148 const wxSize& size,
149 long style,
150 const wxString& name)
151 {
152 // this style can be used to prevent a window from having the standard MDI
153 // "Window" menu
154 if ( !(style & wxFRAME_NO_WINDOW_MENU) )
155 {
156 // normal case: we have the window menu, so construct it
157 m_windowMenu = new wxMenu;
158
159 m_windowMenu->Append(IDM_WINDOWCASCADE, _("&Cascade"));
160 m_windowMenu->Append(IDM_WINDOWTILEHOR, _("Tile &Horizontally"));
161 m_windowMenu->Append(IDM_WINDOWTILEVERT, _("Tile &Vertically"));
162 m_windowMenu->AppendSeparator();
163 m_windowMenu->Append(IDM_WINDOWICONS, _("&Arrange Icons"));
164 m_windowMenu->Append(IDM_WINDOWNEXT, _("&Next"));
165 m_windowMenu->Append(IDM_WINDOWPREV, _("&Previous"));
166 }
167
168 m_parentFrameActive = true;
169
170 if (!parent)
171 wxTopLevelWindows.Append(this);
172
173 SetName(name);
174 m_windowStyle = style;
175
176 if ( parent )
177 parent->AddChild(this);
178
179 if ( id != wxID_ANY )
180 m_windowId = id;
181 else
182 m_windowId = NewControlId();
183
184 WXDWORD exflags;
185 WXDWORD msflags = MSWGetCreateWindowFlags(&exflags);
186 msflags &= ~WS_VSCROLL;
187 msflags &= ~WS_HSCROLL;
188
189 if ( !wxWindow::MSWCreate(wxApp::GetRegisteredClassName(_T("wxMDIFrame")),
190 title.wx_str(),
191 pos, size,
192 msflags,
193 exflags) )
194 {
195 return false;
196 }
197
198 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
199
200 // unlike (almost?) all other windows, frames are created hidden
201 m_isShown = false;
202
203 return true;
204 }
205
206 wxMDIParentFrame::~wxMDIParentFrame()
207 {
208 // see comment in ~wxMDIChildFrame
209 #if wxUSE_TOOLBAR
210 m_frameToolBar = NULL;
211 #endif
212 #if wxUSE_STATUSBAR
213 m_frameStatusBar = NULL;
214 #endif // wxUSE_STATUSBAR
215
216 DestroyChildren();
217
218 // the MDI frame menubar is not automatically deleted by Windows unlike for
219 // the normal frames
220 if ( m_hMenu )
221 ::DestroyMenu((HMENU)m_hMenu);
222
223 if ( m_clientWindow )
224 {
225 if ( m_clientWindow->MSWGetOldWndProc() )
226 m_clientWindow->UnsubclassWin();
227
228 m_clientWindow->SetHWND(0);
229 delete m_clientWindow;
230 }
231 }
232
233 // ----------------------------------------------------------------------------
234 // wxMDIParentFrame child management
235 // ----------------------------------------------------------------------------
236
237 int wxMDIParentFrame::GetChildFramesCount() const
238 {
239 int count = 0;
240 for ( wxWindowList::const_iterator i = GetChildren().begin();
241 i != GetChildren().end();
242 ++i )
243 {
244 if ( wxDynamicCast(*i, wxMDIChildFrame) )
245 count++;
246 }
247
248 return count;
249 }
250
251 void wxMDIParentFrame::AddMDIChild(wxMDIChildFrame * WXUNUSED(child))
252 {
253 switch ( GetChildFramesCount() )
254 {
255 case 1:
256 // first MDI child was just added, we need to insert the window
257 // menu now if we have it
258 AddWindowMenu();
259
260 // and disable the items which can't be used until we have more
261 // than one child
262 UpdateWindowMenu(false);
263 break;
264
265 case 2:
266 // second MDI child was added, enable the menu items which were
267 // disabled because they didn't make sense for a single window
268 UpdateWindowMenu(true);
269 break;
270 }
271 }
272
273 void wxMDIParentFrame::RemoveMDIChild(wxMDIChildFrame * WXUNUSED(child))
274 {
275 switch ( GetChildFramesCount() )
276 {
277 case 1:
278 // last MDI child is being removed, remove the now unnecessary
279 // window menu too
280 RemoveWindowMenu();
281
282 // there is no need to call UpdateWindowMenu(true) here so this is
283 // not quite symmetric to AddMDIChild() above
284 break;
285
286 case 2:
287 // only one MDI child is going to remain, disable the menu commands
288 // which don't make sense for a single child window
289 UpdateWindowMenu(false);
290 break;
291 }
292 }
293
294 // ----------------------------------------------------------------------------
295 // wxMDIParentFrame window menu handling
296 // ----------------------------------------------------------------------------
297
298 void wxMDIParentFrame::AddWindowMenu()
299 {
300 if ( m_windowMenu )
301 MDIInsertWindowMenu(GetClientWindow(), m_hMenu, GetMDIWindowMenu(this));
302 }
303
304 void wxMDIParentFrame::RemoveWindowMenu()
305 {
306 if ( m_windowMenu )
307 MDIRemoveWindowMenu(GetClientWindow(), m_hMenu);
308 }
309
310 void wxMDIParentFrame::UpdateWindowMenu(bool enable)
311 {
312 if ( m_windowMenu )
313 {
314 m_windowMenu->Enable(IDM_WINDOWNEXT, enable);
315 m_windowMenu->Enable(IDM_WINDOWPREV, enable);
316 }
317 }
318
319 #if wxUSE_MENUS_NATIVE
320
321 void wxMDIParentFrame::InternalSetMenuBar()
322 {
323 m_parentFrameActive = true;
324
325 if ( GetActiveChild() )
326 {
327 AddWindowMenu();
328 }
329 else // we don't have any MDI children yet
330 {
331 // wait until we do to add the window menu but do set the main menu for
332 // now (this is done by AddWindowMenu() as a side effect)
333 MDISetMenu(GetClientWindow(), (HMENU)m_hMenu, NULL);
334 }
335 }
336
337 #endif // wxUSE_MENUS_NATIVE
338
339 void wxMDIParentFrame::SetWindowMenu(wxMenu* menu)
340 {
341 // notice that Remove/AddWindowMenu() are safe to call even when
342 // m_windowMenu is NULL
343 RemoveWindowMenu();
344
345 delete m_windowMenu;
346
347 m_windowMenu = menu;
348
349 AddWindowMenu();
350 }
351
352 void wxMDIParentFrame::DoMenuUpdates(wxMenu* menu)
353 {
354 wxMDIChildFrame *child = GetActiveChild();
355 if ( child )
356 {
357 wxEvtHandler* source = child->GetEventHandler();
358 wxMenuBar* bar = child->GetMenuBar();
359
360 if (menu)
361 {
362 menu->UpdateUI(source);
363 }
364 else
365 {
366 if ( bar != NULL )
367 {
368 int nCount = bar->GetMenuCount();
369 for (int n = 0; n < nCount; n++)
370 bar->GetMenu(n)->UpdateUI(source);
371 }
372 }
373 }
374 else
375 {
376 wxFrameBase::DoMenuUpdates(menu);
377 }
378 }
379
380 const wxMenuItem *wxMDIParentFrame::FindItemInMenuBar(int menuId) const
381 {
382 const wxMenuItem *item = wxFrame::FindItemInMenuBar(menuId);
383 if ( !item && m_currentChild )
384 {
385 item = m_currentChild->FindItemInMenuBar(menuId);
386 }
387
388 return item;
389 }
390
391 void wxMDIParentFrame::UpdateClientSize()
392 {
393 if ( GetClientWindow() )
394 {
395 int width, height;
396 GetClientSize(&width, &height);
397
398 GetClientWindow()->SetSize(0, 0, width, height);
399 }
400 }
401
402 void wxMDIParentFrame::OnSize(wxSizeEvent& WXUNUSED(event))
403 {
404 UpdateClientSize();
405
406 // do not call event.Skip() here, it somehow messes up MDI client window
407 }
408
409 void wxMDIParentFrame::OnIconized(wxIconizeEvent& event)
410 {
411 event.Skip();
412
413 if ( !event.IsIconized() )
414 UpdateClientSize();
415 }
416
417 // Returns the active MDI child window
418 wxMDIChildFrame *wxMDIParentFrame::GetActiveChild() const
419 {
420 HWND hWnd = (HWND)::SendMessage(GetWinHwnd(GetClientWindow()),
421 WM_MDIGETACTIVE, 0, 0L);
422 if ( hWnd == 0 )
423 return NULL;
424
425 return (wxMDIChildFrame *)wxFindWinFromHandle(hWnd);
426 }
427
428 // Responds to colour changes, and passes event on to children.
429 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
430 {
431 if ( m_clientWindow )
432 {
433 m_clientWindow->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
434 m_clientWindow->Refresh();
435 }
436
437 event.Skip();
438 }
439
440 WXHICON wxMDIParentFrame::GetDefaultIcon() const
441 {
442 // we don't have any standard icons (any more)
443 return (WXHICON)0;
444 }
445
446 // ---------------------------------------------------------------------------
447 // MDI operations
448 // ---------------------------------------------------------------------------
449
450 void wxMDIParentFrame::Cascade()
451 {
452 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE, 0, 0);
453 }
454
455 void wxMDIParentFrame::Tile(wxOrientation orient)
456 {
457 wxASSERT_MSG( orient == wxHORIZONTAL || orient == wxVERTICAL,
458 _T("invalid orientation value") );
459
460 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE,
461 orient == wxHORIZONTAL ? MDITILE_HORIZONTAL
462 : MDITILE_VERTICAL, 0);
463 }
464
465 void wxMDIParentFrame::ArrangeIcons()
466 {
467 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE, 0, 0);
468 }
469
470 void wxMDIParentFrame::ActivateNext()
471 {
472 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT, 0, 0);
473 }
474
475 void wxMDIParentFrame::ActivatePrevious()
476 {
477 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT, 0, 1);
478 }
479
480 // ---------------------------------------------------------------------------
481 // the MDI parent frame window proc
482 // ---------------------------------------------------------------------------
483
484 WXLRESULT wxMDIParentFrame::MSWWindowProc(WXUINT message,
485 WXWPARAM wParam,
486 WXLPARAM lParam)
487 {
488 WXLRESULT rc = 0;
489 bool processed = false;
490
491 switch ( message )
492 {
493 case WM_ACTIVATE:
494 {
495 WXWORD state, minimized;
496 WXHWND hwnd;
497 UnpackActivate(wParam, lParam, &state, &minimized, &hwnd);
498
499 processed = HandleActivate(state, minimized != 0, hwnd);
500 }
501 break;
502
503 case WM_COMMAND:
504 {
505 WXWORD id, cmd;
506 WXHWND hwnd;
507 UnpackCommand(wParam, lParam, &id, &hwnd, &cmd);
508
509 (void)HandleCommand(id, cmd, hwnd);
510
511 // even if the frame didn't process it, there is no need to try it
512 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
513 // so pretend we processed the message anyhow
514 processed = true;
515 }
516
517 // always pass this message DefFrameProc(), otherwise MDI menu
518 // commands (and sys commands - more surprisingly!) won't work
519 MSWDefWindowProc(message, wParam, lParam);
520 break;
521
522 case WM_CREATE:
523 m_clientWindow = OnCreateClient();
524 // Uses own style for client style
525 if ( !m_clientWindow->CreateClient(this, GetWindowStyleFlag()) )
526 {
527 wxLogMessage(_("Failed to create MDI parent frame."));
528
529 rc = -1;
530 }
531
532 processed = true;
533 break;
534
535 case WM_ERASEBKGND:
536 processed = true;
537
538 // we erase background ourselves
539 rc = true;
540 break;
541
542 case WM_SIZE:
543 // though we don't (usually) resize the MDI client to exactly fit the
544 // client area we need to pass this one to DefFrameProc to allow the children to show
545 break;
546 }
547
548 if ( !processed )
549 rc = wxFrame::MSWWindowProc(message, wParam, lParam);
550
551 return rc;
552 }
553
554 bool wxMDIParentFrame::HandleActivate(int state, bool minimized, WXHWND activate)
555 {
556 bool processed = false;
557
558 if ( wxWindow::HandleActivate(state, minimized, activate) )
559 {
560 // already processed
561 processed = true;
562 }
563
564 // If this window is an MDI parent, we must also send an OnActivate message
565 // to the current child.
566 if ( (m_currentChild != NULL) &&
567 ((state == WA_ACTIVE) || (state == WA_CLICKACTIVE)) )
568 {
569 wxActivateEvent event(wxEVT_ACTIVATE, true, m_currentChild->GetId());
570 event.SetEventObject( m_currentChild );
571 if ( m_currentChild->HandleWindowEvent(event) )
572 processed = true;
573 }
574
575 return processed;
576 }
577
578 bool wxMDIParentFrame::HandleCommand(WXWORD id_, WXWORD cmd, WXHWND hwnd)
579 {
580 // sign extend to int from short before comparing with the other int ids
581 int id = (signed short)id_;
582
583 // In case it's e.g. a toolbar.
584 if ( hwnd )
585 {
586 wxWindow *win = wxFindWinFromHandle(hwnd);
587 if ( win )
588 return win->MSWCommand(cmd, id);
589 }
590
591 if (wxCurrentPopupMenu)
592 {
593 wxMenu *popupMenu = wxCurrentPopupMenu;
594 wxCurrentPopupMenu = NULL;
595 if (popupMenu->MSWCommand(cmd, id))
596 return true;
597 }
598
599 // is it one of standard MDI commands?
600 WXWPARAM wParam = 0;
601 WXLPARAM lParam = 0;
602 int msg;
603 switch ( id )
604 {
605 case IDM_WINDOWCASCADE:
606 msg = WM_MDICASCADE;
607 wParam = MDITILE_SKIPDISABLED;
608 break;
609
610 case IDM_WINDOWTILEHOR:
611 wParam |= MDITILE_HORIZONTAL;
612 // fall through
613
614 case IDM_WINDOWTILEVERT:
615 if ( !wParam )
616 wParam = MDITILE_VERTICAL;
617 msg = WM_MDITILE;
618 wParam |= MDITILE_SKIPDISABLED;
619 break;
620
621 case IDM_WINDOWICONS:
622 msg = WM_MDIICONARRANGE;
623 break;
624
625 case IDM_WINDOWNEXT:
626 msg = WM_MDINEXT;
627 lParam = 0; // next child
628 break;
629
630 case IDM_WINDOWPREV:
631 msg = WM_MDINEXT;
632 lParam = 1; // previous child
633 break;
634
635 default:
636 msg = 0;
637 }
638
639 if ( msg )
640 {
641 ::SendMessage(GetWinHwnd(GetClientWindow()), msg, wParam, lParam);
642
643 return true;
644 }
645
646 // FIXME VZ: what does this test do??
647 if (id >= 0xF000)
648 {
649 return false; // Get WndProc to call default proc
650 }
651
652 if ( IsMdiCommandId(id) )
653 {
654 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
655 while ( node )
656 {
657 wxWindow *child = node->GetData();
658 if ( child->GetHWND() )
659 {
660 int childId = wxGetWindowId(child->GetHWND());
661 if ( childId == (signed short)id )
662 {
663 ::SendMessage( GetWinHwnd(GetClientWindow()),
664 WM_MDIACTIVATE,
665 (WPARAM)child->GetHWND(), 0);
666 return true;
667 }
668 }
669 node = node->GetNext();
670 }
671 }
672 else if ( m_parentFrameActive )
673 {
674 return ProcessCommand(id);
675 }
676 else if ( m_currentChild )
677 {
678 return m_currentChild->HandleCommand(id, cmd, hwnd);
679 }
680 else
681 {
682 // this shouldn't happen because it means that our messages are being
683 // lost (they're not sent to the parent frame nor to the children)
684 wxFAIL_MSG(wxT("MDI parent frame is not active, yet there is no active MDI child?"));
685 }
686
687 return false;
688 }
689
690 WXLRESULT wxMDIParentFrame::MSWDefWindowProc(WXUINT message,
691 WXWPARAM wParam,
692 WXLPARAM lParam)
693 {
694 WXHWND clientWnd;
695 if ( GetClientWindow() )
696 clientWnd = GetClientWindow()->GetHWND();
697 else
698 clientWnd = 0;
699
700 return DefFrameProc(GetHwnd(), (HWND)clientWnd, message, wParam, lParam);
701 }
702
703 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG* msg)
704 {
705 MSG *pMsg = (MSG *)msg;
706
707 // first let the current child get it
708 if ( m_currentChild && m_currentChild->GetHWND() &&
709 m_currentChild->MSWTranslateMessage(msg) )
710 {
711 return true;
712 }
713
714 // then try out accel table (will also check the menu accels)
715 if ( wxFrame::MSWTranslateMessage(msg) )
716 {
717 return true;
718 }
719
720 // finally, check for MDI specific built in accel keys
721 if ( pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN )
722 {
723 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg))
724 return true;
725 }
726
727 return false;
728 }
729
730 // ===========================================================================
731 // wxMDIChildFrame
732 // ===========================================================================
733
734 void wxMDIChildFrame::Init()
735 {
736 m_needsResize = true;
737 m_needsInitialShow = true;
738 }
739
740 bool wxMDIChildFrame::Create(wxMDIParentFrame *parent,
741 wxWindowID id,
742 const wxString& title,
743 const wxPoint& pos,
744 const wxSize& size,
745 long style,
746 const wxString& name)
747 {
748 m_mdiParent = parent;
749
750 SetName(name);
751
752 if ( id != wxID_ANY )
753 m_windowId = id;
754 else
755 m_windowId = NewControlId();
756
757 if ( parent )
758 {
759 parent->AddChild(this);
760 }
761
762 int x = pos.x;
763 int y = pos.y;
764 int width = size.x;
765 int height = size.y;
766
767 MDICREATESTRUCT mcs;
768
769 wxString className =
770 wxApp::GetRegisteredClassName(_T("wxMDIChildFrame"), COLOR_WINDOW);
771 if ( !(style & wxFULL_REPAINT_ON_RESIZE) )
772 className += wxApp::GetNoRedrawClassSuffix();
773
774 mcs.szClass = className.wx_str();
775 mcs.szTitle = title.wx_str();
776 mcs.hOwner = wxGetInstance();
777 if (x != wxDefaultCoord)
778 mcs.x = x;
779 else
780 mcs.x = CW_USEDEFAULT;
781
782 if (y != wxDefaultCoord)
783 mcs.y = y;
784 else
785 mcs.y = CW_USEDEFAULT;
786
787 if (width != wxDefaultCoord)
788 mcs.cx = width;
789 else
790 mcs.cx = CW_USEDEFAULT;
791
792 if (height != wxDefaultCoord)
793 mcs.cy = height;
794 else
795 mcs.cy = CW_USEDEFAULT;
796
797 DWORD msflags = WS_OVERLAPPED | WS_CLIPCHILDREN;
798 if (style & wxMINIMIZE_BOX)
799 msflags |= WS_MINIMIZEBOX;
800 if (style & wxMAXIMIZE_BOX)
801 msflags |= WS_MAXIMIZEBOX;
802 if (style & wxRESIZE_BORDER)
803 msflags |= WS_THICKFRAME;
804 if (style & wxSYSTEM_MENU)
805 msflags |= WS_SYSMENU;
806 if ((style & wxMINIMIZE) || (style & wxICONIZE))
807 msflags |= WS_MINIMIZE;
808 if (style & wxMAXIMIZE)
809 msflags |= WS_MAXIMIZE;
810 if (style & wxCAPTION)
811 msflags |= WS_CAPTION;
812
813 mcs.style = msflags;
814
815 mcs.lParam = 0;
816
817 wxWindowCreationHook hook(this);
818
819 m_hWnd = (WXHWND)::SendMessage(GetWinHwnd(parent->GetClientWindow()),
820 WM_MDICREATE, 0, (LPARAM)&mcs);
821
822 if ( !m_hWnd )
823 {
824 wxLogLastError(_T("WM_MDICREATE"));
825 return false;
826 }
827
828 SubclassWin(m_hWnd);
829
830 parent->AddMDIChild(this);
831
832 return true;
833 }
834
835 wxMDIChildFrame::~wxMDIChildFrame()
836 {
837 // if we hadn't been created, there is nothing to destroy
838 if ( !m_hWnd )
839 return;
840
841 GetMDIParent()->RemoveMDIChild(this);
842
843 // will be destroyed by DestroyChildren() but reset them before calling it
844 // to avoid using dangling pointers if a callback comes in the meanwhile
845 #if wxUSE_TOOLBAR
846 m_frameToolBar = NULL;
847 #endif
848 #if wxUSE_STATUSBAR
849 m_frameStatusBar = NULL;
850 #endif // wxUSE_STATUSBAR
851
852 DestroyChildren();
853
854 MDIRemoveWindowMenu(NULL, m_hMenu);
855
856 MSWDestroyWindow();
857 }
858
859 bool wxMDIChildFrame::Show(bool show)
860 {
861 m_needsInitialShow = false;
862
863 if (!wxFrame::Show(show))
864 return false;
865
866 // KH: Without this call, new MDI children do not become active.
867 // This was added here after the same BringWindowToTop call was
868 // removed from wxTopLevelWindow::Show (November 2005)
869 if ( show )
870 ::BringWindowToTop(GetHwnd());
871
872 // we need to refresh the MDI frame window menu to include (or exclude if
873 // we've been hidden) this frame
874 wxMDIParentFrame * const parent = GetMDIParent();
875 MDISetMenu(parent->GetClientWindow(), NULL, NULL);
876
877 return true;
878 }
879
880 // Set the client size (i.e. leave the calculation of borders etc.
881 // to wxWidgets)
882 void wxMDIChildFrame::DoSetClientSize(int width, int height)
883 {
884 HWND hWnd = GetHwnd();
885
886 RECT rect;
887 ::GetClientRect(hWnd, &rect);
888
889 RECT rect2;
890 GetWindowRect(hWnd, &rect2);
891
892 // Find the difference between the entire window (title bar and all)
893 // and the client area; add this to the new client size to move the
894 // window
895 int actual_width = rect2.right - rect2.left - rect.right + width;
896 int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
897
898 #if wxUSE_STATUSBAR
899 if (GetStatusBar() && GetStatusBar()->IsShown())
900 {
901 int sx, sy;
902 GetStatusBar()->GetSize(&sx, &sy);
903 actual_height += sy;
904 }
905 #endif // wxUSE_STATUSBAR
906
907 POINT point;
908 point.x = rect2.left;
909 point.y = rect2.top;
910
911 // If there's an MDI parent, must subtract the parent's top left corner
912 // since MoveWindow moves relative to the parent
913 wxMDIParentFrame * const mdiParent = GetMDIParent();
914 ::ScreenToClient(GetHwndOf(mdiParent->GetClientWindow()), &point);
915
916 MoveWindow(hWnd, point.x, point.y, actual_width, actual_height, (BOOL)true);
917
918 wxSize size(width, height);
919 wxSizeEvent event(size, m_windowId);
920 event.SetEventObject( this );
921 HandleWindowEvent(event);
922 }
923
924 // Unlike other wxTopLevelWindowBase, the mdi child's "GetPosition" is not the
925 // same as its GetScreenPosition
926 void wxMDIChildFrame::DoGetScreenPosition(int *x, int *y) const
927 {
928 HWND hWnd = GetHwnd();
929
930 RECT rect;
931 ::GetWindowRect(hWnd, &rect);
932 if (x)
933 *x = rect.left;
934 if (y)
935 *y = rect.top;
936 }
937
938
939 void wxMDIChildFrame::DoGetPosition(int *x, int *y) const
940 {
941 RECT rect;
942 GetWindowRect(GetHwnd(), &rect);
943 POINT point;
944 point.x = rect.left;
945 point.y = rect.top;
946
947 // Since we now have the absolute screen coords,
948 // if there's a parent we must subtract its top left corner
949 wxMDIParentFrame * const mdiParent = GetMDIParent();
950 ::ScreenToClient(GetHwndOf(mdiParent->GetClientWindow()), &point);
951
952 if (x)
953 *x = point.x;
954 if (y)
955 *y = point.y;
956 }
957
958 void wxMDIChildFrame::InternalSetMenuBar()
959 {
960 wxMDIParentFrame * const parent = GetMDIParent();
961
962 MDIInsertWindowMenu(parent->GetClientWindow(),
963 m_hMenu, GetMDIWindowMenu(parent));
964
965 parent->m_parentFrameActive = false;
966 }
967
968 void wxMDIChildFrame::DetachMenuBar()
969 {
970 MDIRemoveWindowMenu(NULL, m_hMenu);
971 wxFrame::DetachMenuBar();
972 }
973
974 WXHICON wxMDIChildFrame::GetDefaultIcon() const
975 {
976 // we don't have any standard icons (any more)
977 return (WXHICON)0;
978 }
979
980 // ---------------------------------------------------------------------------
981 // MDI operations
982 // ---------------------------------------------------------------------------
983
984 void wxMDIChildFrame::Maximize(bool maximize)
985 {
986 wxMDIParentFrame * const parent = GetMDIParent();
987 if ( parent && parent->GetClientWindow() )
988 {
989 ::SendMessage(GetWinHwnd(parent->GetClientWindow()),
990 maximize ? WM_MDIMAXIMIZE : WM_MDIRESTORE,
991 (WPARAM)GetHwnd(), 0);
992 }
993 }
994
995 void wxMDIChildFrame::Restore()
996 {
997 wxMDIParentFrame * const parent = GetMDIParent();
998 if ( parent && parent->GetClientWindow() )
999 {
1000 ::SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIRESTORE,
1001 (WPARAM) GetHwnd(), 0);
1002 }
1003 }
1004
1005 void wxMDIChildFrame::Activate()
1006 {
1007 wxMDIParentFrame * const parent = GetMDIParent();
1008 if ( parent && parent->GetClientWindow() )
1009 {
1010 ::SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIACTIVATE,
1011 (WPARAM) GetHwnd(), 0);
1012 }
1013 }
1014
1015 // ---------------------------------------------------------------------------
1016 // MDI window proc and message handlers
1017 // ---------------------------------------------------------------------------
1018
1019 WXLRESULT wxMDIChildFrame::MSWWindowProc(WXUINT message,
1020 WXWPARAM wParam,
1021 WXLPARAM lParam)
1022 {
1023 WXLRESULT rc = 0;
1024 bool processed = false;
1025
1026 switch ( message )
1027 {
1028 case WM_COMMAND:
1029 {
1030 WORD id, cmd;
1031 WXHWND hwnd;
1032 UnpackCommand((WXWPARAM)wParam, (WXLPARAM)lParam,
1033 &id, &hwnd, &cmd);
1034
1035 processed = HandleCommand(id, cmd, (WXHWND)hwnd);
1036 }
1037 break;
1038
1039 case WM_GETMINMAXINFO:
1040 processed = HandleGetMinMaxInfo((MINMAXINFO *)lParam);
1041 break;
1042
1043 case WM_MDIACTIVATE:
1044 {
1045 WXWORD act;
1046 WXHWND hwndAct, hwndDeact;
1047 UnpackMDIActivate(wParam, lParam, &act, &hwndAct, &hwndDeact);
1048
1049 processed = HandleMDIActivate(act, hwndAct, hwndDeact);
1050 }
1051 // fall through
1052
1053 case WM_MOVE:
1054 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
1055 // scrollbars if necessary
1056
1057 // fall through
1058
1059 case WM_SIZE:
1060 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
1061 // things happen
1062 MSWDefWindowProc(message, wParam, lParam);
1063 break;
1064
1065 case WM_SYSCOMMAND:
1066 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
1067 // the message (the base class version does not)
1068 return MSWDefWindowProc(message, wParam, lParam);
1069
1070 case WM_WINDOWPOSCHANGING:
1071 processed = HandleWindowPosChanging((LPWINDOWPOS)lParam);
1072 break;
1073 }
1074
1075 if ( !processed )
1076 rc = wxFrame::MSWWindowProc(message, wParam, lParam);
1077
1078 return rc;
1079 }
1080
1081 bool wxMDIChildFrame::HandleCommand(WXWORD id_, WXWORD cmd, WXHWND hwnd)
1082 {
1083 // sign extend to int from short before comparing with the other int ids
1084 int id = (signed short)id_;
1085
1086 // In case it's e.g. a toolbar.
1087 if ( hwnd )
1088 {
1089 wxWindow *win = wxFindWinFromHandle(hwnd);
1090 if (win)
1091 return win->MSWCommand(cmd, id);
1092 }
1093
1094 if (wxCurrentPopupMenu)
1095 {
1096 wxMenu *popupMenu = wxCurrentPopupMenu;
1097 wxCurrentPopupMenu = NULL;
1098 if (popupMenu->MSWCommand(cmd, id))
1099 return true;
1100 }
1101
1102 bool processed;
1103 if (GetMenuBar() && GetMenuBar()->FindItem(id))
1104 {
1105 processed = ProcessCommand(id);
1106 }
1107 else
1108 {
1109 processed = false;
1110 }
1111
1112 return processed;
1113 }
1114
1115 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate),
1116 WXHWND hwndAct,
1117 WXHWND hwndDeact)
1118 {
1119 wxMDIParentFrame * const parent = GetMDIParent();
1120
1121 HMENU menuToSet = 0;
1122
1123 bool activated;
1124
1125 if ( m_hWnd == hwndAct )
1126 {
1127 activated = true;
1128 parent->m_currentChild = this;
1129
1130 HMENU child_menu = (HMENU)GetWinMenu();
1131 if ( child_menu )
1132 {
1133 parent->m_parentFrameActive = false;
1134
1135 menuToSet = child_menu;
1136 }
1137 }
1138 else if ( m_hWnd == hwndDeact )
1139 {
1140 wxASSERT_MSG( parent->m_currentChild == this,
1141 wxT("can't deactivate MDI child which wasn't active!") );
1142
1143 activated = false;
1144 parent->m_currentChild = NULL;
1145
1146 HMENU parent_menu = (HMENU)parent->GetWinMenu();
1147
1148 // activate the the parent menu only when there is no other child
1149 // that has been activated
1150 if ( parent_menu && !hwndAct )
1151 {
1152 parent->m_parentFrameActive = true;
1153
1154 menuToSet = parent_menu;
1155 }
1156 }
1157 else
1158 {
1159 // we have nothing to do with it
1160 return false;
1161 }
1162
1163 if ( menuToSet )
1164 {
1165 MDISetMenu(parent->GetClientWindow(),
1166 menuToSet, GetMDIWindowMenu(parent));
1167 }
1168
1169 wxActivateEvent event(wxEVT_ACTIVATE, activated, m_windowId);
1170 event.SetEventObject( this );
1171
1172 ResetWindowStyle(NULL);
1173
1174 return HandleWindowEvent(event);
1175 }
1176
1177 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos)
1178 {
1179 WINDOWPOS *lpPos = (WINDOWPOS *)pos;
1180
1181 if (!(lpPos->flags & SWP_NOSIZE))
1182 {
1183 RECT rectClient;
1184 DWORD dwExStyle = ::GetWindowLong(GetHwnd(), GWL_EXSTYLE);
1185 DWORD dwStyle = ::GetWindowLong(GetHwnd(), GWL_STYLE);
1186 if (ResetWindowStyle((void *) & rectClient) && (dwStyle & WS_MAXIMIZE))
1187 {
1188 ::AdjustWindowRectEx(&rectClient, dwStyle, false, dwExStyle);
1189 lpPos->x = rectClient.left;
1190 lpPos->y = rectClient.top;
1191 lpPos->cx = rectClient.right - rectClient.left;
1192 lpPos->cy = rectClient.bottom - rectClient.top;
1193 }
1194 }
1195
1196 return false;
1197 }
1198
1199 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo)
1200 {
1201 MINMAXINFO *info = (MINMAXINFO *)mmInfo;
1202
1203 // let the default window proc calculate the size of MDI children
1204 // frames because it is based on the size of the MDI client window,
1205 // not on the values specified in wxWindow m_max variables
1206 bool processed = MSWDefWindowProc(WM_GETMINMAXINFO, 0, (LPARAM)mmInfo) != 0;
1207
1208 int minWidth = GetMinWidth(),
1209 minHeight = GetMinHeight();
1210
1211 // but allow GetSizeHints() to set the min size
1212 if ( minWidth != wxDefaultCoord )
1213 {
1214 info->ptMinTrackSize.x = minWidth;
1215
1216 processed = true;
1217 }
1218
1219 if ( minHeight != wxDefaultCoord )
1220 {
1221 info->ptMinTrackSize.y = minHeight;
1222
1223 processed = true;
1224 }
1225
1226 return processed;
1227 }
1228
1229 // ---------------------------------------------------------------------------
1230 // MDI specific message translation/preprocessing
1231 // ---------------------------------------------------------------------------
1232
1233 WXLRESULT wxMDIChildFrame::MSWDefWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
1234 {
1235 return DefMDIChildProc(GetHwnd(),
1236 (UINT)message, (WPARAM)wParam, (LPARAM)lParam);
1237 }
1238
1239 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG* msg)
1240 {
1241 // we must pass the parent frame to ::TranslateAccelerator(), otherwise it
1242 // doesn't do its job correctly for MDI child menus
1243 return MSWDoTranslateMessage(GetMDIParent(), msg);
1244 }
1245
1246 // ---------------------------------------------------------------------------
1247 // misc
1248 // ---------------------------------------------------------------------------
1249
1250 void wxMDIChildFrame::MSWDestroyWindow()
1251 {
1252 wxMDIParentFrame * const parent = GetMDIParent();
1253
1254 // Must make sure this handle is invalidated (set to NULL) since all sorts
1255 // of things could happen after the child client is destroyed, but before
1256 // the wxFrame is destroyed.
1257
1258 HWND oldHandle = (HWND)GetHWND();
1259 SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIDESTROY,
1260 (WPARAM)oldHandle, 0);
1261
1262 if (parent->GetActiveChild() == NULL)
1263 ResetWindowStyle(NULL);
1264
1265 if (m_hMenu)
1266 {
1267 ::DestroyMenu((HMENU) m_hMenu);
1268 m_hMenu = 0;
1269 }
1270 wxRemoveHandleAssociation(this);
1271 m_hWnd = 0;
1272 }
1273
1274 // Change the client window's extended style so we don't get a client edge
1275 // style when a child is maximised (a double border looks silly.)
1276 bool wxMDIChildFrame::ResetWindowStyle(void *vrect)
1277 {
1278 RECT *rect = (RECT *)vrect;
1279 wxMDIParentFrame * const pFrameWnd = GetMDIParent();
1280 wxMDIChildFrame* pChild = pFrameWnd->GetActiveChild();
1281
1282 if (!pChild || (pChild == this))
1283 {
1284 HWND hwndClient = GetWinHwnd(pFrameWnd->GetClientWindow());
1285 DWORD dwStyle = ::GetWindowLong(hwndClient, GWL_EXSTYLE);
1286
1287 // we want to test whether there is a maximized child, so just set
1288 // dwThisStyle to 0 if there is no child at all
1289 DWORD dwThisStyle = pChild
1290 ? ::GetWindowLong(GetWinHwnd(pChild), GWL_STYLE) : 0;
1291 DWORD dwNewStyle = dwStyle;
1292 if ( dwThisStyle & WS_MAXIMIZE )
1293 dwNewStyle &= ~(WS_EX_CLIENTEDGE);
1294 else
1295 dwNewStyle |= WS_EX_CLIENTEDGE;
1296
1297 if (dwStyle != dwNewStyle)
1298 {
1299 // force update of everything
1300 ::RedrawWindow(hwndClient, NULL, NULL,
1301 RDW_INVALIDATE | RDW_ALLCHILDREN);
1302 ::SetWindowLong(hwndClient, GWL_EXSTYLE, dwNewStyle);
1303 ::SetWindowPos(hwndClient, NULL, 0, 0, 0, 0,
1304 SWP_FRAMECHANGED | SWP_NOACTIVATE |
1305 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
1306 SWP_NOCOPYBITS);
1307 if (rect)
1308 ::GetClientRect(hwndClient, rect);
1309
1310 return true;
1311 }
1312 }
1313
1314 return false;
1315 }
1316
1317 // ===========================================================================
1318 // wxMDIClientWindow: the window of predefined (by Windows) class which
1319 // contains the child frames
1320 // ===========================================================================
1321
1322 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style)
1323 {
1324 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
1325
1326 CLIENTCREATESTRUCT ccs;
1327 m_windowStyle = style;
1328 m_parent = parent;
1329
1330 ccs.hWindowMenu = GetMDIWindowMenu(parent);
1331 ccs.idFirstChild = wxFIRST_MDI_CHILD;
1332
1333 DWORD msStyle = MDIS_ALLCHILDSTYLES | WS_VISIBLE | WS_CHILD |
1334 WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
1335
1336 if ( style & wxHSCROLL )
1337 msStyle |= WS_HSCROLL;
1338 if ( style & wxVSCROLL )
1339 msStyle |= WS_VSCROLL;
1340
1341 DWORD exStyle = WS_EX_CLIENTEDGE;
1342
1343 wxWindowCreationHook hook(this);
1344 m_hWnd = (WXHWND)::CreateWindowEx
1345 (
1346 exStyle,
1347 wxT("MDICLIENT"),
1348 NULL,
1349 msStyle,
1350 0, 0, 0, 0,
1351 GetWinHwnd(parent),
1352 NULL,
1353 wxGetInstance(),
1354 (LPSTR)(LPCLIENTCREATESTRUCT)&ccs);
1355 if ( !m_hWnd )
1356 {
1357 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1358
1359 return false;
1360 }
1361
1362 SubclassWin(m_hWnd);
1363
1364 return true;
1365 }
1366
1367 // Explicitly call default scroll behaviour
1368 void wxMDIClientWindow::OnScroll(wxScrollEvent& event)
1369 {
1370 // Note: for client windows, the scroll position is not set in
1371 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1372 // scroll position we're at.
1373 // This makes it hard to paint patterns or bitmaps in the background,
1374 // and have the client area scrollable as well.
1375
1376 if ( event.GetOrientation() == wxHORIZONTAL )
1377 m_scrollX = event.GetPosition(); // Always returns zero!
1378 else
1379 m_scrollY = event.GetPosition(); // Always returns zero!
1380
1381 event.Skip();
1382 }
1383
1384 void wxMDIClientWindow::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1385 {
1386 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1387 // client area, you can end up with a non-refreshed portion in the client window
1388 // (see OGL studio sample). So check if the position is changed and if so,
1389 // redraw the MDI child frames.
1390
1391 const wxPoint oldPos = GetPosition();
1392
1393 wxWindow::DoSetSize(x, y, width, height, sizeFlags | wxSIZE_FORCE);
1394
1395 const wxPoint newPos = GetPosition();
1396
1397 if ((newPos.x != oldPos.x) || (newPos.y != oldPos.y))
1398 {
1399 if (GetParent())
1400 {
1401 wxWindowList::compatibility_iterator node = GetParent()->GetChildren().GetFirst();
1402 while (node)
1403 {
1404 wxWindow *child = node->GetData();
1405 if (child->IsKindOf(CLASSINFO(wxMDIChildFrame)))
1406 {
1407 ::RedrawWindow(GetHwndOf(child),
1408 NULL,
1409 NULL,
1410 RDW_FRAME |
1411 RDW_ALLCHILDREN |
1412 RDW_INVALIDATE);
1413 }
1414 node = node->GetNext();
1415 }
1416 }
1417 }
1418 }
1419
1420 void wxMDIChildFrame::OnIdle(wxIdleEvent& event)
1421 {
1422 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1423 // in flicker e.g. when the frame contained controls with non-trivial
1424 // layout. Since 2.5.3, the frame is created hidden as all other top level
1425 // windows. In order to maintain backward compatibility, the frame is shown
1426 // in OnIdle, unless Show(false) was called by the programmer before.
1427 if ( m_needsInitialShow )
1428 {
1429 Show(true);
1430 }
1431
1432 // MDI child frames get their WM_SIZE when they're constructed but at this
1433 // moment they don't have any children yet so all child windows will be
1434 // positioned incorrectly when they are added later - to fix this, we
1435 // generate an artificial size event here
1436 if ( m_needsResize )
1437 {
1438 m_needsResize = false; // avoid any possibility of recursion
1439
1440 SendSizeEvent();
1441 }
1442
1443 event.Skip();
1444 }
1445
1446 // ---------------------------------------------------------------------------
1447 // non member functions
1448 // ---------------------------------------------------------------------------
1449
1450 static void MDISetMenu(wxWindow *win, HMENU hmenuFrame, HMENU hmenuWindow)
1451 {
1452 if ( hmenuFrame || hmenuWindow )
1453 {
1454 if ( !::SendMessage(GetWinHwnd(win),
1455 WM_MDISETMENU,
1456 (WPARAM)hmenuFrame,
1457 (LPARAM)hmenuWindow) )
1458 {
1459 #ifdef __WXDEBUG__
1460 DWORD err = ::GetLastError();
1461 if ( err )
1462 wxLogApiError(_T("SendMessage(WM_MDISETMENU)"), err);
1463 #endif // __WXDEBUG__
1464 }
1465 }
1466
1467 // update menu bar of the parent window
1468 wxWindow *parent = win->GetParent();
1469 wxCHECK_RET( parent, wxT("MDI client without parent frame? weird...") );
1470
1471 ::SendMessage(GetWinHwnd(win), WM_MDIREFRESHMENU, 0, 0L);
1472
1473 ::DrawMenuBar(GetWinHwnd(parent));
1474 }
1475
1476 static void MDIInsertWindowMenu(wxWindow *win, WXHMENU menu, HMENU subMenu)
1477 {
1478 // Try to insert Window menu in front of Help, otherwise append it.
1479 HMENU hmenu = (HMENU)menu;
1480
1481 if (subMenu)
1482 {
1483 int N = GetMenuItemCount(hmenu);
1484 bool success = false;
1485 for ( int i = 0; i < N; i++ )
1486 {
1487 wxChar buf[256];
1488 int chars = GetMenuString(hmenu, i, buf, WXSIZEOF(buf), MF_BYPOSITION);
1489 if ( chars == 0 )
1490 {
1491 wxLogLastError(wxT("GetMenuString"));
1492
1493 continue;
1494 }
1495
1496 wxString strBuf(buf);
1497 if ( wxStripMenuCodes(strBuf) == wxGetStockLabel(wxID_HELP,false) )
1498 {
1499 success = true;
1500 ::InsertMenu(hmenu, i, MF_BYPOSITION | MF_POPUP | MF_STRING,
1501 (UINT_PTR)subMenu, _("&Window").wx_str());
1502 break;
1503 }
1504 }
1505
1506 if ( !success )
1507 {
1508 ::AppendMenu(hmenu, MF_POPUP,
1509 (UINT_PTR)subMenu, _("&Window").wx_str());
1510 }
1511 }
1512
1513 MDISetMenu(win, hmenu, subMenu);
1514 }
1515
1516 static void MDIRemoveWindowMenu(wxWindow *win, WXHMENU menu)
1517 {
1518 HMENU hMenu = (HMENU)menu;
1519
1520 if ( hMenu )
1521 {
1522 wxChar buf[1024];
1523
1524 int N = ::GetMenuItemCount(hMenu);
1525 for ( int i = 0; i < N; i++ )
1526 {
1527 if ( !::GetMenuString(hMenu, i, buf, WXSIZEOF(buf), MF_BYPOSITION) )
1528 {
1529 // Ignore successful read of menu string with length 0 which
1530 // occurs, for example, for a maximized MDI childs system menu
1531 if ( ::GetLastError() != 0 )
1532 {
1533 wxLogLastError(wxT("GetMenuString"));
1534 }
1535
1536 continue;
1537 }
1538
1539 if ( wxStrcmp(buf, _("&Window")) == 0 )
1540 {
1541 if ( !::RemoveMenu(hMenu, i, MF_BYPOSITION) )
1542 {
1543 wxLogLastError(wxT("RemoveMenu"));
1544 }
1545
1546 break;
1547 }
1548 }
1549 }
1550
1551 if ( win )
1552 {
1553 // we don't change the windows menu, but we update the main one
1554 MDISetMenu(win, hMenu, NULL);
1555 }
1556 }
1557
1558 static void UnpackMDIActivate(WXWPARAM wParam, WXLPARAM lParam,
1559 WXWORD *activate, WXHWND *hwndAct, WXHWND *hwndDeact)
1560 {
1561 *activate = true;
1562 *hwndAct = (WXHWND)lParam;
1563 *hwndDeact = (WXHWND)wParam;
1564 }
1565
1566 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)