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