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