]> git.saurik.com Git - wxWidgets.git/blame - src/msw/mdi.cpp
pointer returned by GetNativeFontInfo() is now const and must not be deleted (replace...
[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{
42e69d6b 236 DestroyChildren();
4e152a23 237
f048e32f
VZ
238 // already delete by DestroyChildren()
239 m_frameToolBar = NULL;
c0bcc480 240 m_frameStatusBar = NULL;
f048e32f 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
5c44cd05 699 DWORD msflags = WS_OVERLAPPED | WS_CLIPCHILDREN | WS_THICKFRAME | 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{
627a3091
JS
733 DestroyChildren();
734
4e152a23 735 // already deleted by DestroyChildren()
627a3091
JS
736 m_frameToolBar = NULL;
737 m_frameStatusBar = NULL;
738
4e152a23
VZ
739 RemoveWindowMenu(NULL, m_hMenu);
740
c2dcfdef 741 MSWDestroyWindow();
2bda0e17
KB
742}
743
744// Set the client size (i.e. leave the calculation of borders etc.
745// to wxWindows)
cc2b7472 746void wxMDIChildFrame::DoSetClientSize(int width, int height)
2bda0e17 747{
b3818fbe 748 HWND hWnd = GetHwnd();
2bda0e17
KB
749
750 RECT rect;
2de8030d 751 ::GetClientRect(hWnd, &rect);
2bda0e17
KB
752
753 RECT rect2;
754 GetWindowRect(hWnd, &rect2);
755
756 // Find the difference between the entire window (title bar and all)
757 // and the client area; add this to the new client size to move the
758 // window
759 int actual_width = rect2.right - rect2.left - rect.right + width;
760 int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
761
a2327a9f 762 if (GetStatusBar() && GetStatusBar()->IsShown())
2bda0e17 763 {
c2dcfdef
VZ
764 int sx, sy;
765 GetStatusBar()->GetSize(&sx, &sy);
2bda0e17
KB
766 actual_height += sy;
767 }
768
769 POINT point;
770 point.x = rect2.left;
771 point.y = rect2.top;
772
773 // If there's an MDI parent, must subtract the parent's top left corner
774 // since MoveWindow moves relative to the parent
775 wxMDIParentFrame *mdiParent = (wxMDIParentFrame *)GetParent();
776 ::ScreenToClient((HWND) mdiParent->GetClientWindow()->GetHWND(), &point);
777
4f8090e0 778 MoveWindow(hWnd, point.x, point.y, actual_width, actual_height, (BOOL)true);
debe6624 779
2bda0e17 780 wxSizeEvent event(wxSize(width, height), m_windowId);
debe6624 781 event.SetEventObject( this );
2bda0e17 782 GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
783}
784
cc2b7472 785void wxMDIChildFrame::DoGetPosition(int *x, int *y) const
2bda0e17
KB
786{
787 RECT rect;
b3818fbe 788 GetWindowRect(GetHwnd(), &rect);
2bda0e17
KB
789 POINT point;
790 point.x = rect.left;
791 point.y = rect.top;
792
793 // Since we now have the absolute screen coords,
794 // if there's a parent we must subtract its top left corner
795 wxMDIParentFrame *mdiParent = (wxMDIParentFrame *)GetParent();
796 ::ScreenToClient((HWND) mdiParent->GetClientWindow()->GetHWND(), &point);
797
798 *x = point.x;
799 *y = point.y;
800}
801
42e69d6b 802void wxMDIChildFrame::InternalSetMenuBar()
2bda0e17 803{
42e69d6b 804 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
2bda0e17 805
4e152a23
VZ
806 InsertWindowMenu(parent->GetClientWindow(),
807 m_hMenu, GetMDIWindowMenu(parent));
2bda0e17 808
4f8090e0 809 parent->m_parentFrameActive = false;
2bda0e17
KB
810}
811
82c9f85c
VZ
812WXHICON wxMDIChildFrame::GetDefaultIcon() const
813{
94826170
VZ
814 // we don't have any standard icons (any more)
815 return (WXHICON)0;
82c9f85c
VZ
816}
817
42e69d6b 818// ---------------------------------------------------------------------------
2bda0e17 819// MDI operations
42e69d6b
VZ
820// ---------------------------------------------------------------------------
821
9b73db3c 822void wxMDIChildFrame::Maximize(bool maximize)
2bda0e17
KB
823{
824 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
825 if ( parent && parent->GetClientWindow() )
9b73db3c
VZ
826 {
827 ::SendMessage(GetWinHwnd(parent->GetClientWindow()),
828 maximize ? WM_MDIMAXIMIZE : WM_MDIRESTORE,
829 (WPARAM)GetHwnd(), 0);
830 }
2bda0e17
KB
831}
832
c2dcfdef 833void wxMDIChildFrame::Restore()
2bda0e17
KB
834{
835 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
836 if ( parent && parent->GetClientWindow() )
9b73db3c
VZ
837 {
838 ::SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIRESTORE,
839 (WPARAM) GetHwnd(), 0);
840 }
2bda0e17
KB
841}
842
c2dcfdef 843void wxMDIChildFrame::Activate()
2bda0e17
KB
844{
845 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
846 if ( parent && parent->GetClientWindow() )
9b73db3c
VZ
847 {
848 ::SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIACTIVATE,
849 (WPARAM) GetHwnd(), 0);
850 }
2bda0e17
KB
851}
852
42e69d6b
VZ
853// ---------------------------------------------------------------------------
854// MDI window proc and message handlers
855// ---------------------------------------------------------------------------
856
857long wxMDIChildFrame::MSWWindowProc(WXUINT message,
858 WXWPARAM wParam,
859 WXLPARAM lParam)
860{
861 long rc = 0;
4f8090e0 862 bool processed = false;
42e69d6b
VZ
863
864 switch ( message )
865 {
866 case WM_COMMAND:
867 {
868 WORD id, cmd;
869 WXHWND hwnd;
870 UnpackCommand((WXWPARAM)wParam, (WXLPARAM)lParam,
871 &id, &hwnd, &cmd);
872
873 processed = HandleCommand(id, cmd, (WXHWND)hwnd);
874 }
875 break;
876
42e69d6b 877 case WM_GETMINMAXINFO:
3ebcfb76
VZ
878 processed = HandleGetMinMaxInfo((MINMAXINFO *)lParam);
879 break;
42e69d6b
VZ
880
881 case WM_MDIACTIVATE:
882 {
883 WXWORD act;
884 WXHWND hwndAct, hwndDeact;
885 UnpackMDIActivate(wParam, lParam, &act, &hwndAct, &hwndDeact);
886
887 processed = HandleMDIActivate(act, hwndAct, hwndDeact);
888 }
b3818fbe
VZ
889 // fall through
890
891 case WM_MOVE:
892 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
893 // scrollbars if necessary
894
895 // fall through
896
897 case WM_SIZE:
898 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
899 // things happen
900 MSWDefWindowProc(message, wParam, lParam);
42e69d6b
VZ
901 break;
902
b3818fbe
VZ
903 case WM_SYSCOMMAND:
904 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
905 // the message (the base class version does not)
906 return MSWDefWindowProc(message, wParam, lParam);
907
42e69d6b
VZ
908 case WM_WINDOWPOSCHANGING:
909 processed = HandleWindowPosChanging((LPWINDOWPOS)lParam);
910 break;
911 }
912
913 if ( !processed )
914 rc = wxFrame::MSWWindowProc(message, wParam, lParam);
915
916 return rc;
917}
918
42e69d6b 919bool wxMDIChildFrame::HandleCommand(WXWORD id, WXWORD cmd, WXHWND hwnd)
2bda0e17 920{
2bda0e17 921 // In case it's e.g. a toolbar.
42e69d6b
VZ
922 if ( hwnd )
923 {
924 wxWindow *win = wxFindWinFromHandle(hwnd);
925 if (win)
926 return win->MSWCommand(cmd, id);
927 }
2bda0e17 928
e1a6fc11
JS
929 if (wxCurrentPopupMenu)
930 {
931 wxMenu *popupMenu = wxCurrentPopupMenu;
932 wxCurrentPopupMenu = NULL;
933 if (popupMenu->MSWCommand(cmd, id))
4f8090e0 934 return true;
e1a6fc11
JS
935 }
936
3ca6a5f0 937 bool processed;
dd60b9ec 938 if (GetMenuBar() && GetMenuBar()->FindItem(id))
2bda0e17 939 {
3ca6a5f0 940 processed = ProcessCommand(id);
2bda0e17
KB
941 }
942 else
3ca6a5f0 943 {
4f8090e0 944 processed = false;
3ca6a5f0 945 }
42e69d6b 946
3ca6a5f0 947 return processed;
2bda0e17
KB
948}
949
42e69d6b
VZ
950bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate),
951 WXHWND hwndAct,
952 WXHWND hwndDeact)
2bda0e17 953{
42e69d6b 954 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
2bda0e17 955
42e69d6b 956 HMENU menuToSet = 0;
57a7b7c1 957
42e69d6b 958 bool activated;
57a7b7c1 959
42e69d6b
VZ
960 if ( m_hWnd == hwndAct )
961 {
4f8090e0 962 activated = true;
42e69d6b 963 parent->m_currentChild = this;
2bda0e17 964
42e69d6b
VZ
965 HMENU child_menu = (HMENU)GetWinMenu();
966 if ( child_menu )
967 {
4f8090e0 968 parent->m_parentFrameActive = false;
2bda0e17 969
42e69d6b
VZ
970 menuToSet = child_menu;
971 }
972 }
973 else if ( m_hWnd == hwndDeact )
2bda0e17 974 {
42e69d6b 975 wxASSERT_MSG( parent->m_currentChild == this,
223d09f6 976 wxT("can't deactivate MDI child which wasn't active!") );
42e69d6b 977
4f8090e0 978 activated = false;
42e69d6b 979 parent->m_currentChild = NULL;
2bda0e17 980
42e69d6b 981 HMENU parent_menu = (HMENU)parent->GetWinMenu();
f4075804
VZ
982
983 // activate the the parent menu only when there is no other child
984 // that has been activated
985 if ( parent_menu && !hwndAct )
42e69d6b 986 {
4f8090e0 987 parent->m_parentFrameActive = true;
42e69d6b
VZ
988
989 menuToSet = parent_menu;
990 }
991 }
992 else
993 {
18d2e170 994 // we have nothing to do with it
4f8090e0 995 return false;
2bda0e17 996 }
debe6624 997
42e69d6b
VZ
998 if ( menuToSet )
999 {
4e152a23
VZ
1000 MDISetMenu(parent->GetClientWindow(),
1001 menuToSet, GetMDIWindowMenu(parent));
42e69d6b
VZ
1002 }
1003
1004 wxActivateEvent event(wxEVT_ACTIVATE, activated, m_windowId);
debe6624 1005 event.SetEventObject( this );
2bda0e17 1006
c03b48d7
GT
1007 ResetWindowStyle((void *)NULL);
1008
42e69d6b
VZ
1009 return GetEventHandler()->ProcessEvent(event);
1010}
1011
1012bool wxMDIChildFrame::HandleWindowPosChanging(void *pos)
1013{
1014 WINDOWPOS *lpPos = (WINDOWPOS *)pos;
1015#if defined(__WIN95__)
1016 if (!(lpPos->flags & SWP_NOSIZE))
2bda0e17 1017 {
42e69d6b 1018 RECT rectClient;
b3818fbe
VZ
1019 DWORD dwExStyle = ::GetWindowLong(GetHwnd(), GWL_EXSTYLE);
1020 DWORD dwStyle = ::GetWindowLong(GetHwnd(), GWL_STYLE);
42e69d6b
VZ
1021 if (ResetWindowStyle((void *) & rectClient) && (dwStyle & WS_MAXIMIZE))
1022 {
4f8090e0 1023 ::AdjustWindowRectEx(&rectClient, dwStyle, false, dwExStyle);
42e69d6b
VZ
1024 lpPos->x = rectClient.left;
1025 lpPos->y = rectClient.top;
1026 lpPos->cx = rectClient.right - rectClient.left;
1027 lpPos->cy = rectClient.bottom - rectClient.top;
1028 }
1029 wxMDIParentFrame* pFrameWnd = (wxMDIParentFrame *)GetParent();
a2327a9f 1030 if (pFrameWnd && pFrameWnd->GetToolBar() && pFrameWnd->GetToolBar()->IsShown())
42e69d6b
VZ
1031 {
1032 pFrameWnd->GetToolBar()->Refresh();
1033 }
1034 }
1035#endif // Win95
1036
4f8090e0 1037 return false;
42e69d6b
VZ
1038}
1039
3ebcfb76
VZ
1040bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo)
1041{
1042 MINMAXINFO *info = (MINMAXINFO *)mmInfo;
1043
1044 // let the default window proc calculate the size of MDI children
1045 // frames because it is based on the size of the MDI client window,
1046 // not on the values specified in wxWindow m_max variables
d9f9aa2d 1047 bool processed = MSWDefWindowProc(WM_GETMINMAXINFO, 0, (LPARAM)mmInfo) != 0;
3ebcfb76 1048
e7dda1ff
VS
1049 int minWidth = GetMinWidth(),
1050 minHeight = GetMinHeight();
1051
3ebcfb76 1052 // but allow GetSizeHints() to set the min size
e7dda1ff 1053 if ( minWidth != -1 )
3ebcfb76 1054 {
e7dda1ff 1055 info->ptMinTrackSize.x = minWidth;
3ebcfb76 1056
4f8090e0 1057 processed = true;
3ebcfb76
VZ
1058 }
1059
e7dda1ff 1060 if ( minHeight != -1 )
3ebcfb76 1061 {
e7dda1ff 1062 info->ptMinTrackSize.y = minHeight;
3ebcfb76 1063
4f8090e0 1064 processed = true;
3ebcfb76
VZ
1065 }
1066
4c9d78a4 1067 return processed;
3ebcfb76
VZ
1068}
1069
42e69d6b
VZ
1070// ---------------------------------------------------------------------------
1071// MDI specific message translation/preprocessing
1072// ---------------------------------------------------------------------------
2bda0e17 1073
42e69d6b
VZ
1074long wxMDIChildFrame::MSWDefWindowProc(WXUINT message, WXUINT wParam, WXLPARAM lParam)
1075{
1076 return DefMDIChildProc(GetHwnd(),
1077 (UINT)message, (WPARAM)wParam, (LPARAM)lParam);
1078}
1079
1080bool wxMDIChildFrame::MSWTranslateMessage(WXMSG* msg)
1081{
5a2762f0 1082 return wxFrame::MSWTranslateMessage(msg);
2bda0e17
KB
1083}
1084
42e69d6b
VZ
1085// ---------------------------------------------------------------------------
1086// misc
1087// ---------------------------------------------------------------------------
1088
c2dcfdef 1089void wxMDIChildFrame::MSWDestroyWindow()
2bda0e17 1090{
b3818fbe 1091 invalidHandle = GetHwnd();
2bda0e17 1092
42e69d6b 1093 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
2bda0e17 1094
42e69d6b
VZ
1095 // Must make sure this handle is invalidated (set to NULL) since all sorts
1096 // of things could happen after the child client is destroyed, but before
1097 // the wxFrame is destroyed.
2bda0e17 1098
42e69d6b 1099 HWND oldHandle = (HWND)GetHWND();
b3818fbe
VZ
1100 SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIDESTROY,
1101 (WPARAM)oldHandle, 0);
18d2e170
JS
1102
1103 if (parent->GetActiveChild() == (wxMDIChildFrame*) NULL)
1104 ResetWindowStyle((void*) NULL);
1105
42e69d6b 1106 invalidHandle = 0;
2bda0e17 1107
42e69d6b
VZ
1108 if (m_hMenu)
1109 {
1110 ::DestroyMenu((HMENU) m_hMenu);
1111 m_hMenu = 0;
1112 }
ac6482e0 1113 wxRemoveHandleAssociation(this);
42e69d6b 1114 m_hWnd = 0;
2bda0e17
KB
1115}
1116
42e69d6b
VZ
1117// Change the client window's extended style so we don't get a client edge
1118// style when a child is maximised (a double border looks silly.)
2bda0e17
KB
1119bool wxMDIChildFrame::ResetWindowStyle(void *vrect)
1120{
1121#if defined(__WIN95__)
1122 RECT *rect = (RECT *)vrect;
c2dcfdef
VZ
1123 wxMDIParentFrame* pFrameWnd = (wxMDIParentFrame *)GetParent();
1124 wxMDIChildFrame* pChild = pFrameWnd->GetActiveChild();
1125 if (!pChild || (pChild == this))
1126 {
47ca6bfb 1127 HWND hwndClient = GetWinHwnd(pFrameWnd->GetClientWindow());
8082a771
VZ
1128 DWORD dwStyle = ::GetWindowLong(hwndClient, GWL_EXSTYLE);
1129
1130 // we want to test whether there is a maximized child, so just set
1131 // dwThisStyle to 0 if there is no child at all
1132 DWORD dwThisStyle = pChild
1f3943e0 1133 ? ::GetWindowLong(GetWinHwnd(pChild), GWL_STYLE) : 0;
c2dcfdef 1134 DWORD dwNewStyle = dwStyle;
8082a771 1135 if ( dwThisStyle & WS_MAXIMIZE )
c2dcfdef
VZ
1136 dwNewStyle &= ~(WS_EX_CLIENTEDGE);
1137 else
1138 dwNewStyle |= WS_EX_CLIENTEDGE;
1139
1140 if (dwStyle != dwNewStyle)
1141 {
47ca6bfb
VZ
1142 // force update of everything
1143 ::RedrawWindow(hwndClient, NULL, NULL,
1144 RDW_INVALIDATE | RDW_ALLCHILDREN);
1145 ::SetWindowLong(hwndClient, GWL_EXSTYLE, dwNewStyle);
1146 ::SetWindowPos(hwndClient, NULL, 0, 0, 0, 0,
42e69d6b
VZ
1147 SWP_FRAMECHANGED | SWP_NOACTIVATE |
1148 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
1149 SWP_NOCOPYBITS);
c2dcfdef 1150 if (rect)
47ca6bfb 1151 ::GetClientRect(hwndClient, rect);
2bda0e17 1152
4f8090e0 1153 return true;
2bda0e17 1154 }
c2dcfdef 1155 }
42e69d6b 1156#endif // Win95
a23fd0e1 1157
4f8090e0 1158 return false;
2bda0e17
KB
1159}
1160
42e69d6b
VZ
1161// ===========================================================================
1162// wxMDIClientWindow: the window of predefined (by Windows) class which
1163// contains the child frames
1164// ===========================================================================
2bda0e17 1165
debe6624 1166bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style)
2bda0e17 1167{
a756f210 1168 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
2bda0e17 1169
42e69d6b
VZ
1170 CLIENTCREATESTRUCT ccs;
1171 m_windowStyle = style;
1172 m_parent = parent;
c2dcfdef 1173
4e152a23 1174 ccs.hWindowMenu = GetMDIWindowMenu(parent);
42e69d6b 1175 ccs.idFirstChild = wxFIRST_MDI_CHILD;
2bda0e17 1176
5c44cd05 1177 DWORD msStyle = MDIS_ALLCHILDSTYLES | WS_VISIBLE | WS_CHILD |
b0766406
JS
1178 WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
1179
42e69d6b
VZ
1180 if ( style & wxHSCROLL )
1181 msStyle |= WS_HSCROLL;
1182 if ( style & wxVSCROLL )
1183 msStyle |= WS_VSCROLL;
2bda0e17
KB
1184
1185#if defined(__WIN95__)
42e69d6b 1186 DWORD exStyle = WS_EX_CLIENTEDGE;
2bda0e17 1187#else
42e69d6b 1188 DWORD exStyle = 0;
2bda0e17
KB
1189#endif
1190
b225f659 1191 wxWindowCreationHook hook(this);
42e69d6b
VZ
1192 m_hWnd = (WXHWND)::CreateWindowEx
1193 (
1194 exStyle,
223d09f6 1195 wxT("MDICLIENT"),
42e69d6b
VZ
1196 NULL,
1197 msStyle,
1198 0, 0, 0, 0,
1199 GetWinHwnd(parent),
1200 NULL,
1201 wxGetInstance(),
1202 (LPSTR)(LPCLIENTCREATESTRUCT)&ccs);
1203 if ( !m_hWnd )
1204 {
f6bcfd97 1205 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
2bda0e17 1206
4f8090e0 1207 return false;
42e69d6b 1208 }
2bda0e17 1209
42e69d6b 1210 SubclassWin(m_hWnd);
2bda0e17 1211
4f8090e0 1212 return true;
2bda0e17
KB
1213}
1214
1215// Explicitly call default scroll behaviour
1216void wxMDIClientWindow::OnScroll(wxScrollEvent& event)
1217{
1218 // Note: for client windows, the scroll position is not set in
1219 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1220 // scroll position we're at.
1221 // This makes it hard to paint patterns or bitmaps in the background,
1222 // and have the client area scrollable as well.
1223
1224 if ( event.GetOrientation() == wxHORIZONTAL )
1225 m_scrollX = event.GetPosition(); // Always returns zero!
1226 else
1227 m_scrollY = event.GetPosition(); // Always returns zero!
1228
42e69d6b
VZ
1229 event.Skip();
1230}
1231
ec06b234
JS
1232void wxMDIClientWindow::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1233{
1234 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1235 // client area, you can end up with a non-refreshed portion in the client window
1236 // (see OGL studio sample). So check if the position is changed and if so,
1237 // redraw the MDI child frames.
1238
1239 wxPoint oldPos = GetPosition();
1240
1241 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
1242
1243 wxPoint newPos = GetPosition();
1244
1245 if ((newPos.x != oldPos.x) || (newPos.y != oldPos.y))
1246 {
1247 if (GetParent())
1248 {
222ed1d6 1249 wxWindowList::compatibility_iterator node = GetParent()->GetChildren().GetFirst();
ec06b234
JS
1250 while (node)
1251 {
d162a7ee 1252 wxWindow *child = node->GetData();
ec06b234
JS
1253 if (child->IsKindOf(CLASSINFO(wxMDIChildFrame)))
1254 {
d162a7ee
VZ
1255 ::RedrawWindow(GetHwndOf(child),
1256 NULL,
1257 NULL,
1258 RDW_FRAME |
1259 RDW_ALLCHILDREN |
1260 RDW_INVALIDATE);
ec06b234 1261 }
4f8090e0 1262 node = node->GetNext();
ec06b234
JS
1263 }
1264 }
1265 }
1266}
1267
f6bcfd97
BP
1268void wxMDIChildFrame::OnIdle(wxIdleEvent& event)
1269{
1270 // MDI child frames get their WM_SIZE when they're constructed but at this
1271 // moment they don't have any children yet so all child windows will be
1272 // positioned incorrectly when they are added later - to fix this, we
1273 // generate an artificial size event here
1274 if ( m_needsResize )
1275 {
4f8090e0 1276 m_needsResize = false; // avoid any possibility of recursion
f6bcfd97
BP
1277
1278 SendSizeEvent();
1279 }
1280
1281 event.Skip();
1282}
1283
42e69d6b
VZ
1284// ---------------------------------------------------------------------------
1285// non member functions
1286// ---------------------------------------------------------------------------
1287
1288static void MDISetMenu(wxWindow *win, HMENU hmenuFrame, HMENU hmenuWindow)
1289{
1290 ::SendMessage(GetWinHwnd(win), WM_MDISETMENU,
1291#ifdef __WIN32__
f6bcfd97 1292 (WPARAM)hmenuFrame, (LPARAM)hmenuWindow
42e69d6b 1293#else
f6bcfd97 1294 0, MAKELPARAM(hmenuFrame, hmenuWindow)
42e69d6b 1295#endif
f6bcfd97 1296 );
42e69d6b
VZ
1297
1298 // update menu bar of the parent window
1299 wxWindow *parent = win->GetParent();
223d09f6 1300 wxCHECK_RET( parent, wxT("MDI client without parent frame? weird...") );
42e69d6b 1301
788722ac 1302#ifndef __WIN16__
a967ef9d 1303 ::SendMessage(GetWinHwnd(win), WM_MDIREFRESHMENU, 0, 0L);
788722ac
JS
1304#endif
1305
42e69d6b
VZ
1306 ::DrawMenuBar(GetWinHwnd(parent));
1307}
1308
1309static void InsertWindowMenu(wxWindow *win, WXHMENU menu, HMENU subMenu)
1310{
1311 // Try to insert Window menu in front of Help, otherwise append it.
1312 HMENU hmenu = (HMENU)menu;
df61c009
JS
1313
1314 if (subMenu)
1315 {
4e152a23 1316 int N = GetMenuItemCount(hmenu);
4f8090e0 1317 bool success = false;
4e152a23 1318 for ( int i = 0; i < N; i++ )
42e69d6b 1319 {
4e152a23
VZ
1320 wxChar buf[256];
1321 int chars = GetMenuString(hmenu, i, buf, WXSIZEOF(buf), MF_BYPOSITION);
1322 if ( chars == 0 )
1323 {
1324 wxLogLastError(wxT("GetMenuString"));
42e69d6b 1325
4e152a23
VZ
1326 continue;
1327 }
1328
1329 if ( wxStripMenuCodes(wxString(buf)).IsSameAs(_("Help")) )
1330 {
4f8090e0 1331 success = true;
4e152a23
VZ
1332 ::InsertMenu(hmenu, i, MF_BYPOSITION | MF_POPUP | MF_STRING,
1333 (UINT)subMenu, _("&Window"));
1334 break;
1335 }
42e69d6b
VZ
1336 }
1337
4e152a23 1338 if ( !success )
42e69d6b 1339 {
4e152a23 1340 ::AppendMenu(hmenu, MF_POPUP, (UINT)subMenu, _("&Window"));
42e69d6b
VZ
1341 }
1342 }
1343
42e69d6b
VZ
1344 MDISetMenu(win, hmenu, subMenu);
1345}
1346
df61c009
JS
1347static void RemoveWindowMenu(wxWindow *win, WXHMENU menu)
1348{
4e152a23
VZ
1349 HMENU hMenu = (HMENU)menu;
1350
1351 if ( hMenu )
df61c009 1352 {
4e152a23
VZ
1353 wxChar buf[1024];
1354
1355 int N = ::GetMenuItemCount(hMenu);
1356 for ( int i = 0; i < N; i++ )
df61c009 1357 {
4e152a23
VZ
1358 if ( !::GetMenuString(hMenu, i, buf, WXSIZEOF(buf), MF_BYPOSITION) )
1359 {
2c62b3c3
VZ
1360 // Ignore successful read of menu string with length 0 which
1361 // occurs, for example, for a maximized MDI childs system menu
1362 if ( ::GetLastError() != 0 )
1363 {
1364 wxLogLastError(wxT("GetMenuString"));
1365 }
df61c009 1366
4e152a23
VZ
1367 continue;
1368 }
df61c009 1369
4e152a23
VZ
1370 if ( wxStrcmp(buf, _("&Window")) == 0 )
1371 {
1372 if ( !::RemoveMenu(hMenu, i, MF_BYPOSITION) )
1373 {
1374 wxLogLastError(wxT("RemoveMenu"));
1375 }
1376
1377 break;
1378 }
df61c009
JS
1379 }
1380 }
1381
4e152a23
VZ
1382 if ( win )
1383 {
1384 // we don't change the windows menu, but we update the main one
1385 MDISetMenu(win, hMenu, NULL);
1386 }
df61c009
JS
1387}
1388
2917e920
BM
1389static void UnpackMDIActivate(WXWPARAM wParam, WXLPARAM lParam,
1390 WXWORD *activate, WXHWND *hwndAct, WXHWND *hwndDeact)
42e69d6b
VZ
1391{
1392#ifdef __WIN32__
4f8090e0 1393 *activate = true;
42e69d6b
VZ
1394 *hwndAct = (WXHWND)lParam;
1395 *hwndDeact = (WXHWND)wParam;
1396#else // Win16
1397 *activate = (WXWORD)wParam;
1398 *hwndAct = (WXHWND)LOWORD(lParam);
1399 *hwndDeact = (WXHWND)HIWORD(lParam);
1400#endif // Win32/Win16
2bda0e17 1401}
b9f933ab
JS
1402
1403#endif
1404// wxUSE_MDI_ARCHITECTURE && !defined(__WXUNIVERSAL__)
1405