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