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