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