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