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