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