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