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