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