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