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