]> git.saurik.com Git - wxWidgets.git/blame - src/msw/mdi.cpp
Compile fix.
[wxWidgets.git] / src / msw / mdi.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: mdi.cpp
3// Purpose: MDI classes
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"
38 #include "wx/statusbr.h"
39 #include "wx/settings.h"
2bda0e17
KB
40#endif
41
42#include "wx/mdi.h"
43#include "wx/msw/private.h"
44
47d67540 45#if wxUSE_NATIVE_STATUSBAR
a23fd0e1 46 #include <wx/msw/statbr95.h>
2bda0e17
KB
47#endif
48
49#include <string.h>
50
a23fd0e1
VZ
51// ---------------------------------------------------------------------------
52// global variables
53// ---------------------------------------------------------------------------
54
55extern wxWindowList wxModelessWindows; // from dialog.cpp
e1a6fc11 56extern wxMenu *wxCurrentPopupMenu;
2bda0e17 57
a23fd0e1
VZ
58extern char wxMDIFrameClassName[];
59extern char wxMDIChildFrameClassName[];
60extern wxWindow *wxWndHook; // from window.cpp
61
62extern wxList *wxWinHandleList;
63
42e69d6b
VZ
64static HWND invalidHandle = 0;
65
a23fd0e1
VZ
66// ---------------------------------------------------------------------------
67// constants
68// ---------------------------------------------------------------------------
69
70static const int IDM_WINDOWTILE = 4001;
42e69d6b 71static const int IDM_WINDOWTILEHOR = 4001;
a23fd0e1
VZ
72static const int IDM_WINDOWCASCADE = 4002;
73static const int IDM_WINDOWICONS = 4003;
74static const int IDM_WINDOWNEXT = 4004;
42e69d6b 75static const int IDM_WINDOWTILEVERT = 4005;
a23fd0e1
VZ
76
77// This range gives a maximum of 500 MDI children. Should be enough :-)
78static const int wxFIRST_MDI_CHILD = 4100;
79static const int wxLAST_MDI_CHILD = 4600;
2bda0e17
KB
80
81// Status border dimensions
a23fd0e1
VZ
82static const int wxTHICK_LINE_BORDER = 3;
83static const int wxTHICK_LINE_WIDTH = 1;
2bda0e17 84
42e69d6b
VZ
85// ---------------------------------------------------------------------------
86// private functions
87// ---------------------------------------------------------------------------
88
89// set the MDI menus (by sending the WM_MDISETMENU message) and update the menu
90// of the parent of win (which is supposed to be the MDI client window)
91static void MDISetMenu(wxWindow *win, HMENU hmenuFrame, HMENU hmenuWindow);
92
93// insert the window menu (subMenu) into menu just before "Help" submenu or at
94// the very end if not found
95static void InsertWindowMenu(wxWindow *win, WXHMENU menu, HMENU subMenu);
96
97// is this an id of an MDI child?
98inline bool IsMdiCommandId(int id)
99{
100 return (id >= wxFIRST_MDI_CHILD) && (id <= wxLAST_MDI_CHILD);
101}
102
2917e920
BM
103static void UnpackMDIActivate(WXWPARAM wParam, WXLPARAM lParam,
104 WXWORD *activate, WXHWND *hwndAct, WXHWND *hwndDeact);
42e69d6b 105
a23fd0e1
VZ
106// ===========================================================================
107// implementation
108// ===========================================================================
109
110// ---------------------------------------------------------------------------
111// wxWin macros
112// ---------------------------------------------------------------------------
2bda0e17
KB
113
114#if !USE_SHARED_LIBRARY
a23fd0e1
VZ
115 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame, wxFrame)
116 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame, wxFrame)
117 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow, wxWindow)
118#endif // USE_SHARED_LIBRARY
2bda0e17
KB
119
120BEGIN_EVENT_TABLE(wxMDIParentFrame, wxFrame)
a23fd0e1 121 EVT_SIZE(wxMDIParentFrame::OnSize)
a23fd0e1 122 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged)
2bda0e17
KB
123END_EVENT_TABLE()
124
125BEGIN_EVENT_TABLE(wxMDIClientWindow, wxWindow)
a23fd0e1 126 EVT_SCROLL(wxMDIClientWindow::OnScroll)
2bda0e17
KB
127END_EVENT_TABLE()
128
42e69d6b
VZ
129// ===========================================================================
130// wxMDIParentFrame: the frame which contains the client window which manages
131// the children
132// ===========================================================================
2bda0e17 133
c2dcfdef 134wxMDIParentFrame::wxMDIParentFrame()
2bda0e17
KB
135{
136 m_clientWindow = NULL;
137 m_currentChild = NULL;
138 m_windowMenu = 0;
139 m_parentFrameActive = TRUE;
2bda0e17
KB
140}
141
142bool wxMDIParentFrame::Create(wxWindow *parent,
a23fd0e1
VZ
143 wxWindowID id,
144 const wxString& title,
145 const wxPoint& pos,
146 const wxSize& size,
147 long style,
148 const wxString& name)
2bda0e17
KB
149{
150 m_defaultIcon = (WXHICON) (wxSTD_MDIPARENTFRAME_ICON ? wxSTD_MDIPARENTFRAME_ICON : wxDEFAULT_MDIPARENTFRAME_ICON);
151
2bda0e17
KB
152 m_clientWindow = NULL;
153 m_currentChild = NULL;
154 m_windowMenu = 0;
155 m_parentFrameActive = TRUE;
156
157 if (!parent)
158 wxTopLevelWindows.Append(this);
159
160 SetName(name);
161 m_windowStyle = style;
162
163 if (parent) parent->AddChild(this);
164
165 if ( id > -1 )
166 m_windowId = id;
167 else
168 m_windowId = (int)NewControlId();
169
170 int x = pos.x;
171 int y = pos.y;
172 int width = size.x;
173 int height = size.y;
174
175 m_windowMenu = (WXHMENU) ::LoadMenu(wxGetInstance(), "wxWindowMenu");
176
42e69d6b 177 DWORD msflags = WS_OVERLAPPED;
2bda0e17
KB
178 if (style & wxMINIMIZE_BOX)
179 msflags |= WS_MINIMIZEBOX;
180 if (style & wxMAXIMIZE_BOX)
181 msflags |= WS_MAXIMIZEBOX;
182 if (style & wxTHICK_FRAME)
183 msflags |= WS_THICKFRAME;
184 if (style & wxSYSTEM_MENU)
185 msflags |= WS_SYSMENU;
186 if ((style & wxMINIMIZE) || (style & wxICONIZE))
187 msflags |= WS_MINIMIZE;
188 if (style & wxMAXIMIZE)
189 msflags |= WS_MAXIMIZE;
190 if (style & wxCAPTION)
191 msflags |= WS_CAPTION;
46851318
JS
192
193 // Adding WS_CLIPCHILDREN causes children not to be properly
194 // drawn when first displaying them.
195// if (style & wxCLIP_CHILDREN)
196// msflags |= WS_CLIPCHILDREN;
2bda0e17
KB
197
198 wxWindow::MSWCreate(m_windowId, parent, wxMDIFrameClassName, this, title, x, y, width, height,
199 msflags);
200
201 wxModelessWindows.Append(this);
202
203 return TRUE;
204}
205
c2dcfdef 206wxMDIParentFrame::~wxMDIParentFrame()
2bda0e17 207{
42e69d6b 208 DestroyChildren();
2bda0e17 209
42e69d6b
VZ
210 ::DestroyMenu((HMENU)m_windowMenu);
211 m_windowMenu = 0;
2bda0e17 212
42e69d6b
VZ
213 if ( m_clientWindow )
214 {
215 if ( m_clientWindow->MSWGetOldWndProc() )
216 m_clientWindow->UnsubclassWin();
2bda0e17 217
42e69d6b
VZ
218 m_clientWindow->SetHWND(0);
219 delete m_clientWindow;
220 }
2bda0e17
KB
221}
222
42e69d6b 223void wxMDIParentFrame::InternalSetMenuBar()
2bda0e17 224{
42e69d6b 225 HMENU subMenu = GetSubMenu((HMENU) m_windowMenu, 0);
2bda0e17 226
42e69d6b 227 m_parentFrameActive = TRUE;
2bda0e17 228
42e69d6b 229 InsertWindowMenu(GetClientWindow(), m_hMenu, subMenu);
2bda0e17
KB
230}
231
232void wxMDIParentFrame::OnSize(wxSizeEvent& event)
233{
2bda0e17 234 if ( GetClientWindow() )
42e69d6b
VZ
235 {
236 int width, height;
237 GetClientSize(&width, &height);
2bda0e17 238
42e69d6b
VZ
239 GetClientWindow()->SetSize(0, 0, width, height);
240 }
2bda0e17
KB
241}
242
2bda0e17 243// Returns the active MDI child window
c2dcfdef 244wxMDIChildFrame *wxMDIParentFrame::GetActiveChild() const
2bda0e17 245{
a23fd0e1
VZ
246 HWND hWnd = (HWND)::SendMessage(GetWinHwnd(GetClientWindow()),
247 WM_MDIGETACTIVE, 0, 0L);
248 if ( hWnd == 0 )
249 return NULL;
250 else
251 return (wxMDIChildFrame *)wxFindWinFromHandle((WXHWND) hWnd);
2bda0e17
KB
252}
253
a23fd0e1
VZ
254// Create the client window class (don't Create the window, just return a new
255// class)
c2dcfdef 256wxMDIClientWindow *wxMDIParentFrame::OnCreateClient()
2bda0e17 257{
a23fd0e1 258 return new wxMDIClientWindow;
2bda0e17
KB
259}
260
261// Responds to colour changes, and passes event on to children.
262void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
263{
264 if ( m_clientWindow )
265 {
266 m_clientWindow->SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE));
267 m_clientWindow->Refresh();
268 }
2bda0e17 269
42e69d6b 270 event.Skip();
2bda0e17
KB
271}
272
42e69d6b 273// ---------------------------------------------------------------------------
2bda0e17 274// MDI operations
42e69d6b
VZ
275// ---------------------------------------------------------------------------
276
c2dcfdef 277void wxMDIParentFrame::Cascade()
2bda0e17 278{
a23fd0e1 279 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE, 0, 0);
2bda0e17
KB
280}
281
42e69d6b 282// TODO: add a direction argument (hor/vert)
c2dcfdef 283void wxMDIParentFrame::Tile()
2bda0e17 284{
a23fd0e1 285 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE, MDITILE_HORIZONTAL, 0);
2bda0e17
KB
286}
287
c2dcfdef 288void wxMDIParentFrame::ArrangeIcons()
2bda0e17 289{
a23fd0e1 290 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE, 0, 0);
2bda0e17
KB
291}
292
c2dcfdef 293void wxMDIParentFrame::ActivateNext()
2bda0e17 294{
a23fd0e1 295 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT, 0, 0);
2bda0e17
KB
296}
297
c2dcfdef 298void wxMDIParentFrame::ActivatePrevious()
2bda0e17 299{
a23fd0e1 300 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT, 0, 1);
2bda0e17
KB
301}
302
42e69d6b 303// ---------------------------------------------------------------------------
a23fd0e1 304// the MDI parent frame window proc
42e69d6b
VZ
305// ---------------------------------------------------------------------------
306
a23fd0e1
VZ
307long wxMDIParentFrame::MSWWindowProc(WXUINT message,
308 WXWPARAM wParam,
309 WXLPARAM lParam)
2bda0e17 310{
a23fd0e1
VZ
311 long rc = 0;
312 bool processed = FALSE;
2bda0e17 313
a23fd0e1
VZ
314 switch ( message )
315 {
42e69d6b
VZ
316 case WM_ACTIVATE:
317 {
318 WXWORD state, minimized;
319 WXHWND hwnd;
320 UnpackActivate(wParam, lParam, &state, &minimized, &hwnd);
321
322 processed = HandleActivate(state, minimized != 0, hwnd);
323 }
324 break;
325
326 case WM_COMMAND:
327 {
328 WXWORD id, cmd;
329 WXHWND hwnd;
330 UnpackCommand(wParam, lParam, &id, &hwnd, &cmd);
331
332 (void)HandleCommand(id, cmd, hwnd);
333
334 // even if the frame didn't process it, there is no need to try it
335 // once again (i.e. call wxFrame::HandleCommand()) - we just dud it,
336 // so pretend we processed the message anyhow
337 processed = TRUE;
338 }
339 break;
340
a23fd0e1
VZ
341 case WM_CREATE:
342 m_clientWindow = OnCreateClient();
343 // Uses own style for client style
344 if ( !m_clientWindow->CreateClient(this, GetWindowStyleFlag()) )
345 {
346 wxLogMessage(_("Failed to create MDI parent frame."));
2bda0e17 347
a23fd0e1
VZ
348 rc = -1;
349 }
2bda0e17 350
a23fd0e1
VZ
351 processed = TRUE;
352 break;
2bda0e17 353
a23fd0e1
VZ
354 case WM_ERASEBKGND:
355 processed = TRUE;
2bda0e17 356
a23fd0e1
VZ
357 // we erase background ourselves
358 rc = TRUE;
359 break;
360
361 case WM_MENUSELECT:
362 {
42e69d6b
VZ
363 WXWORD item, flags;
364 WXHMENU hmenu;
365 UnpackMenuSelect(wParam, lParam, &item, &flags, &hmenu);
366
a23fd0e1
VZ
367 if ( m_parentFrameActive )
368 {
42e69d6b 369 processed = HandleMenuSelect(item, flags, hmenu);
a23fd0e1
VZ
370 }
371 else if (m_currentChild)
372 {
373 processed = m_currentChild->
42e69d6b 374 HandleMenuSelect(item, flags, hmenu);
a23fd0e1
VZ
375 }
376 }
377 break;
378 }
2bda0e17 379
a23fd0e1
VZ
380 if ( !processed )
381 rc = wxFrame::MSWWindowProc(message, wParam, lParam);
2bda0e17 382
a23fd0e1 383 return rc;
2bda0e17
KB
384}
385
42e69d6b 386bool wxMDIParentFrame::HandleActivate(int state, bool minimized, WXHWND activate)
2bda0e17 387{
a23fd0e1
VZ
388 bool processed = FALSE;
389
42e69d6b 390 if ( wxWindow::HandleActivate(state, minimized, activate) )
a23fd0e1
VZ
391 {
392 // already processed
393 processed = TRUE;
394 }
2bda0e17
KB
395
396 // If this window is an MDI parent, we must also send an OnActivate message
397 // to the current child.
42e69d6b 398 if ( (m_currentChild != NULL) &&
a23fd0e1 399 ((state == WA_ACTIVE) || (state == WA_CLICKACTIVE)) )
c2dcfdef 400 {
debe6624
JS
401 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, m_currentChild->GetId());
402 event.SetEventObject( m_currentChild );
a23fd0e1
VZ
403 if ( m_currentChild->GetEventHandler()->ProcessEvent(event) )
404 processed = TRUE;
2bda0e17 405 }
a23fd0e1
VZ
406
407 return processed;
2bda0e17
KB
408}
409
42e69d6b 410bool wxMDIParentFrame::HandleCommand(WXWORD id, WXWORD cmd, WXHWND hwnd)
2bda0e17 411{
2bda0e17 412 // In case it's e.g. a toolbar.
42e69d6b 413 if ( hwnd )
e1a6fc11 414 {
42e69d6b
VZ
415 wxWindow *win = wxFindWinFromHandle(hwnd);
416 if ( win )
417 return win->MSWCommand(cmd, id);
e1a6fc11
JS
418 }
419
42e69d6b
VZ
420 // is it one of standard MDI commands?
421 WXWPARAM wParam = 0;
422 int msg;
423 switch ( id )
2bda0e17 424 {
42e69d6b
VZ
425 case IDM_WINDOWCASCADE:
426 msg = WM_MDICASCADE;
427 wParam = MDITILE_SKIPDISABLED;
428 break;
429
430 case IDM_WINDOWTILEHOR:
431 wParam |= MDITILE_HORIZONTAL;
432 // fall through
433
434 case IDM_WINDOWTILEVERT:
435 if ( !wParam )
436 wParam = MDITILE_VERTICAL;
437 msg = WM_MDITILE;
438 wParam |= MDITILE_SKIPDISABLED;
439 break;
440
441 case IDM_WINDOWICONS:
442 msg = WM_MDIICONARRANGE;
443 break;
444
445 case IDM_WINDOWNEXT:
446 msg = WM_MDINEXT;
447 break;
448
449 default:
450 msg = 0;
2bda0e17 451 }
c2dcfdef 452
42e69d6b 453 if ( msg )
2bda0e17 454 {
42e69d6b
VZ
455 ::SendMessage(GetWinHwnd(GetClientWindow()), msg, wParam, 0);
456
457 return TRUE;
2bda0e17 458 }
42e69d6b
VZ
459
460 // FIXME VZ: what does this test do??
461 if (id >= 0xF000)
2bda0e17 462 {
42e69d6b 463 return FALSE; // Get WndProc to call default proc
2bda0e17 464 }
42e69d6b
VZ
465
466 if ( IsMdiCommandId(id) )
2bda0e17 467 {
42e69d6b
VZ
468 wxWindowList::Node* node = GetChildren().GetFirst();
469 while ( node )
2bda0e17 470 {
42e69d6b
VZ
471 wxWindow* child = node->GetData();
472 if ( child->GetHWND() )
2bda0e17 473 {
42e69d6b
VZ
474 long childId = wxGetWindowId(child->GetHWND());
475 if (childId == id)
476 {
477 ::SendMessage( GetWinHwnd(GetClientWindow()),
478 WM_MDIACTIVATE,
479 (WPARAM)child->GetHWND(), 0);
480 return TRUE;
481 }
2bda0e17 482 }
42e69d6b 483 node = node->GetNext();
2bda0e17 484 }
2bda0e17 485 }
42e69d6b 486 else if ( m_parentFrameActive )
2bda0e17 487 {
42e69d6b
VZ
488 return ProcessCommand(id);
489 }
490 else if ( m_currentChild )
491 {
492 return m_currentChild->HandleCommand(id, cmd, hwnd);
493 }
494 else
495 {
496 // this shouldn't happen because it means that our messages are being
497 // lost (they're not sent to the parent frame nor to the children)
498 wxFAIL_MSG(_T("MDI parent frame is not active, "
499 "yet there is no active MDI child?"));
2bda0e17 500 }
2bda0e17 501
42e69d6b 502 return FALSE;
2bda0e17
KB
503}
504
42e69d6b
VZ
505long wxMDIParentFrame::MSWDefWindowProc(WXUINT message,
506 WXWPARAM wParam,
507 WXLPARAM lParam)
2bda0e17 508{
c2dcfdef
VZ
509 WXHWND clientWnd;
510 if ( GetClientWindow() )
511 clientWnd = GetClientWindow()->GetHWND();
512 else
513 clientWnd = 0;
2bda0e17 514
a23fd0e1 515 return DefFrameProc(GetHwnd(), (HWND)clientWnd, message, wParam, lParam);
2bda0e17
KB
516}
517
57a7b7c1
JS
518bool wxMDIParentFrame::MSWTranslateMessage(WXMSG* msg)
519{
a23fd0e1 520 MSG *pMsg = (MSG *)msg;
2bda0e17 521
a23fd0e1
VZ
522 if ( m_currentChild && m_currentChild->GetHWND() &&
523 m_currentChild->MSWTranslateMessage(msg) )
524 {
525 return TRUE;
526 }
2bda0e17 527
a23fd0e1
VZ
528 if ( m_acceleratorTable.Ok() &&
529 ::TranslateAccelerator(GetHwnd(),
42e69d6b 530 GetTableHaccel(m_acceleratorTable),
a23fd0e1
VZ
531 pMsg) )
532 {
533 return TRUE;
534 }
2bda0e17 535
a23fd0e1
VZ
536 if ( pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN )
537 {
538 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg))
539 return TRUE;
540 }
57a7b7c1 541
a23fd0e1 542 return FALSE;
2bda0e17
KB
543}
544
42e69d6b 545// ===========================================================================
a23fd0e1 546// wxMDIChildFrame
42e69d6b 547// ===========================================================================
2bda0e17 548
c2dcfdef 549wxMDIChildFrame::wxMDIChildFrame()
2bda0e17 550{
2bda0e17
KB
551}
552
553bool wxMDIChildFrame::Create(wxMDIParentFrame *parent,
a23fd0e1
VZ
554 wxWindowID id,
555 const wxString& title,
556 const wxPoint& pos,
557 const wxSize& size,
558 long style,
559 const wxString& name)
2bda0e17 560{
42e69d6b
VZ
561 m_defaultIcon = (WXHICON)(wxSTD_MDICHILDFRAME_ICON ? wxSTD_MDICHILDFRAME_ICON
562 : wxDEFAULT_MDICHILDFRAME_ICON);
2bda0e17
KB
563
564 SetName(name);
565
566 if ( id > -1 )
567 m_windowId = id;
568 else
569 m_windowId = (int)NewControlId();
570
42e69d6b
VZ
571 if ( parent )
572 {
573 parent->AddChild(this);
574 }
2bda0e17
KB
575
576 wxWndHook = this;
577
578 int x = pos.x;
579 int y = pos.y;
580 int width = size.x;
581 int height = size.y;
582
583 MDICREATESTRUCT mcs;
c2dcfdef 584
2bda0e17
KB
585 mcs.szClass = wxMDIChildFrameClassName;
586 mcs.szTitle = title;
587 mcs.hOwner = wxGetInstance();
42e69d6b
VZ
588 if (x > -1)
589 mcs.x = x;
590 else
591 mcs.x = CW_USEDEFAULT;
2bda0e17 592
42e69d6b
VZ
593 if (y > -1)
594 mcs.y = y;
595 else
596 mcs.y = CW_USEDEFAULT;
2bda0e17 597
42e69d6b
VZ
598 if (width > -1)
599 mcs.cx = width;
600 else
601 mcs.cx = CW_USEDEFAULT;
2bda0e17 602
42e69d6b
VZ
603 if (height > -1)
604 mcs.cy = height;
605 else
606 mcs.cy = CW_USEDEFAULT;
2bda0e17 607
42e69d6b 608 DWORD msflags = WS_OVERLAPPED | WS_CLIPCHILDREN;
2bda0e17
KB
609 if (style & wxMINIMIZE_BOX)
610 msflags |= WS_MINIMIZEBOX;
611 if (style & wxMAXIMIZE_BOX)
612 msflags |= WS_MAXIMIZEBOX;
613 if (style & wxTHICK_FRAME)
614 msflags |= WS_THICKFRAME;
615 if (style & wxSYSTEM_MENU)
616 msflags |= WS_SYSMENU;
617 if ((style & wxMINIMIZE) || (style & wxICONIZE))
618 msflags |= WS_MINIMIZE;
619 if (style & wxMAXIMIZE)
620 msflags |= WS_MAXIMIZE;
621 if (style & wxCAPTION)
622 msflags |= WS_CAPTION;
623
624 mcs.style = msflags;
625
626 mcs.lParam = 0;
627
628 DWORD Return = SendMessage((HWND) parent->GetClientWindow()->GetHWND(),
c2dcfdef 629 WM_MDICREATE, 0, (LONG)(LPSTR)&mcs);
2bda0e17
KB
630
631 //handle = (HWND)LOWORD(Return);
632 // Must be the DWORRD for WIN32. And in 16 bits, HIWORD=0 (says Microsoft)
633 m_hWnd = (WXHWND)Return;
634
635 // This gets reassigned so can't be stored
636// m_windowId = GetWindowLong((HWND) m_hWnd, GWL_ID);
637
638 wxWndHook = NULL;
639 wxWinHandleList->Append((long)GetHWND(), this);
640
641 SetWindowLong((HWND) GetHWND(), 0, (long)this);
642
643 wxModelessWindows.Append(this);
644 return TRUE;
645}
646
c2dcfdef 647wxMDIChildFrame::~wxMDIChildFrame()
2bda0e17 648{
c2dcfdef 649 MSWDestroyWindow();
2bda0e17
KB
650}
651
652// Set the client size (i.e. leave the calculation of borders etc.
653// to wxWindows)
cc2b7472 654void wxMDIChildFrame::DoSetClientSize(int width, int height)
2bda0e17
KB
655{
656 HWND hWnd = (HWND) GetHWND();
657
658 RECT rect;
2de8030d 659 ::GetClientRect(hWnd, &rect);
2bda0e17
KB
660
661 RECT rect2;
662 GetWindowRect(hWnd, &rect2);
663
664 // Find the difference between the entire window (title bar and all)
665 // and the client area; add this to the new client size to move the
666 // window
667 int actual_width = rect2.right - rect2.left - rect.right + width;
668 int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
669
670 if (GetStatusBar())
671 {
c2dcfdef
VZ
672 int sx, sy;
673 GetStatusBar()->GetSize(&sx, &sy);
2bda0e17
KB
674 actual_height += sy;
675 }
676
677 POINT point;
678 point.x = rect2.left;
679 point.y = rect2.top;
680
681 // If there's an MDI parent, must subtract the parent's top left corner
682 // since MoveWindow moves relative to the parent
683 wxMDIParentFrame *mdiParent = (wxMDIParentFrame *)GetParent();
684 ::ScreenToClient((HWND) mdiParent->GetClientWindow()->GetHWND(), &point);
685
686 MoveWindow(hWnd, point.x, point.y, actual_width, actual_height, (BOOL)TRUE);
debe6624 687
2bda0e17 688 wxSizeEvent event(wxSize(width, height), m_windowId);
debe6624 689 event.SetEventObject( this );
2bda0e17 690 GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
691}
692
cc2b7472 693void wxMDIChildFrame::DoGetPosition(int *x, int *y) const
2bda0e17
KB
694{
695 RECT rect;
696 GetWindowRect((HWND) GetHWND(), &rect);
697 POINT point;
698 point.x = rect.left;
699 point.y = rect.top;
700
701 // Since we now have the absolute screen coords,
702 // if there's a parent we must subtract its top left corner
703 wxMDIParentFrame *mdiParent = (wxMDIParentFrame *)GetParent();
704 ::ScreenToClient((HWND) mdiParent->GetClientWindow()->GetHWND(), &point);
705
706 *x = point.x;
707 *y = point.y;
708}
709
42e69d6b 710void wxMDIChildFrame::InternalSetMenuBar()
2bda0e17 711{
42e69d6b 712 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
2bda0e17 713
42e69d6b 714 HMENU subMenu = GetSubMenu((HMENU)parent->GetWindowMenu(), 0);
2bda0e17 715
42e69d6b 716 InsertWindowMenu(parent->GetClientWindow(), m_hMenu, subMenu);
2bda0e17 717
42e69d6b 718 parent->m_parentFrameActive = FALSE;
2bda0e17
KB
719}
720
42e69d6b 721// ---------------------------------------------------------------------------
2bda0e17 722// MDI operations
42e69d6b
VZ
723// ---------------------------------------------------------------------------
724
c2dcfdef 725void wxMDIChildFrame::Maximize()
2bda0e17
KB
726{
727 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
728 if ( parent && parent->GetClientWindow() )
729 ::SendMessage( (HWND) parent->GetClientWindow()->GetHWND(), WM_MDIMAXIMIZE, (WPARAM) (HWND) GetHWND(), 0);
730}
731
c2dcfdef 732void wxMDIChildFrame::Restore()
2bda0e17
KB
733{
734 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
735 if ( parent && parent->GetClientWindow() )
736 ::SendMessage( (HWND) parent->GetClientWindow()->GetHWND(), WM_MDIRESTORE, (WPARAM) (HWND) GetHWND(), 0);
737}
738
c2dcfdef 739void wxMDIChildFrame::Activate()
2bda0e17
KB
740{
741 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
742 if ( parent && parent->GetClientWindow() )
743 ::SendMessage( (HWND) parent->GetClientWindow()->GetHWND(), WM_MDIACTIVATE, (WPARAM) (HWND) GetHWND(), 0);
744}
745
42e69d6b
VZ
746// ---------------------------------------------------------------------------
747// MDI window proc and message handlers
748// ---------------------------------------------------------------------------
749
750long wxMDIChildFrame::MSWWindowProc(WXUINT message,
751 WXWPARAM wParam,
752 WXLPARAM lParam)
753{
754 long rc = 0;
755 bool processed = FALSE;
756
757 switch ( message )
758 {
759 case WM_COMMAND:
760 {
761 WORD id, cmd;
762 WXHWND hwnd;
763 UnpackCommand((WXWPARAM)wParam, (WXLPARAM)lParam,
764 &id, &hwnd, &cmd);
765
766 processed = HandleCommand(id, cmd, (WXHWND)hwnd);
767 }
768 break;
769
770 case WM_SIZE:
771 processed = HandleSize(LOWORD(lParam), HIWORD(lParam), wParam);
772 break;
773
774 case WM_GETMINMAXINFO:
775 // let the default window proc calculate the size of MDI children
776 // frames because it is based on the size of the MDI client window,
777 // not on the values specified in wxWindow m_min/max variables
778 return MSWDefWindowProc(message, wParam, lParam);
779
780 case WM_MDIACTIVATE:
781 {
782 WXWORD act;
783 WXHWND hwndAct, hwndDeact;
784 UnpackMDIActivate(wParam, lParam, &act, &hwndAct, &hwndDeact);
785
786 processed = HandleMDIActivate(act, hwndAct, hwndDeact);
787 }
788 break;
789
790 case WM_WINDOWPOSCHANGING:
791 processed = HandleWindowPosChanging((LPWINDOWPOS)lParam);
792 break;
793 }
794
795 if ( !processed )
796 rc = wxFrame::MSWWindowProc(message, wParam, lParam);
797
798 return rc;
799}
800
801bool wxMDIChildFrame::HandleSize(int x, int y, WXUINT id)
2bda0e17 802{
a23fd0e1 803 HWND hwnd = GetHwnd();
42e69d6b 804
a23fd0e1 805 if ( !hwnd || hwnd == invalidHandle )
2bda0e17 806 {
a23fd0e1
VZ
807 return FALSE;
808 }
42e69d6b 809
a23fd0e1
VZ
810 switch (id)
811 {
812 case SIZEFULLSCREEN:
813 case SIZENORMAL:
814 m_iconized = FALSE;
815 break;
816
817 case SIZEICONIC:
818 m_iconized = TRUE;
819 break;
2bda0e17 820 }
42e69d6b
VZ
821
822 if ( !m_iconized )
a23fd0e1
VZ
823 {
824 // forward WM_SIZE to status bar control
825#if wxUSE_NATIVE_STATUSBAR
826 if (m_frameStatusBar && m_frameStatusBar->IsKindOf(CLASSINFO(wxStatusBar95)))
827 {
828 wxSizeEvent event(wxSize(x, y), m_frameStatusBar->GetId());
829 event.SetEventObject( m_frameStatusBar );
42e69d6b 830
a23fd0e1
VZ
831 ((wxStatusBar95 *)m_frameStatusBar)->OnSize(event);
832 }
42e69d6b
VZ
833#endif // wxUSE_NATIVE_STATUSBAR
834
a23fd0e1
VZ
835 PositionStatusBar();
836 PositionToolBar();
42e69d6b
VZ
837
838 return wxWindow::HandleSize(x, y, id);
a23fd0e1
VZ
839 }
840 else
841 {
842 return FALSE;
843 }
2bda0e17
KB
844}
845
42e69d6b 846bool wxMDIChildFrame::HandleCommand(WXWORD id, WXWORD cmd, WXHWND hwnd)
2bda0e17 847{
2bda0e17 848 // In case it's e.g. a toolbar.
42e69d6b
VZ
849 if ( hwnd )
850 {
851 wxWindow *win = wxFindWinFromHandle(hwnd);
852 if (win)
853 return win->MSWCommand(cmd, id);
854 }
2bda0e17 855
e1a6fc11
JS
856 if (wxCurrentPopupMenu)
857 {
858 wxMenu *popupMenu = wxCurrentPopupMenu;
859 wxCurrentPopupMenu = NULL;
860 if (popupMenu->MSWCommand(cmd, id))
861 return TRUE;
862 }
863
2bda0e17
KB
864 if (GetMenuBar() && GetMenuBar()->FindItemForId(id))
865 {
42e69d6b
VZ
866 ProcessCommand(id);
867 return TRUE;
2bda0e17
KB
868 }
869 else
42e69d6b
VZ
870 return FALSE;
871
2bda0e17 872 return TRUE;
2bda0e17
KB
873}
874
42e69d6b
VZ
875bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate),
876 WXHWND hwndAct,
877 WXHWND hwndDeact)
2bda0e17 878{
42e69d6b 879 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
2bda0e17 880
42e69d6b 881 HMENU menuToSet = 0;
57a7b7c1 882
42e69d6b 883 bool activated;
57a7b7c1 884
42e69d6b
VZ
885 if ( m_hWnd == hwndAct )
886 {
887 activated = TRUE;
888 parent->m_currentChild = this;
2bda0e17 889
42e69d6b
VZ
890 HMENU child_menu = (HMENU)GetWinMenu();
891 if ( child_menu )
892 {
893 parent->m_parentFrameActive = FALSE;
2bda0e17 894
42e69d6b
VZ
895 menuToSet = child_menu;
896 }
897 }
898 else if ( m_hWnd == hwndDeact )
2bda0e17 899 {
42e69d6b
VZ
900 wxASSERT_MSG( parent->m_currentChild == this,
901 _T("can't deactivate MDI child which wasn't active!") );
902
903 activated = FALSE;
904 parent->m_currentChild = NULL;
2bda0e17 905
42e69d6b
VZ
906 HMENU parent_menu = (HMENU)parent->GetWinMenu();
907 if ( parent_menu )
908 {
909 parent->m_parentFrameActive = TRUE;
910
911 menuToSet = parent_menu;
912 }
913 }
914 else
915 {
916 // we have nothing to with it
917 return FALSE;
2bda0e17 918 }
debe6624 919
42e69d6b
VZ
920 if ( menuToSet )
921 {
922 HMENU subMenu = GetSubMenu((HMENU) parent->GetWindowMenu(), 0);
923
924 MDISetMenu(parent->GetClientWindow(), menuToSet, subMenu);
925 }
926
927 wxActivateEvent event(wxEVT_ACTIVATE, activated, m_windowId);
debe6624 928 event.SetEventObject( this );
2bda0e17 929
42e69d6b
VZ
930 return GetEventHandler()->ProcessEvent(event);
931}
932
933bool wxMDIChildFrame::HandleWindowPosChanging(void *pos)
934{
935 WINDOWPOS *lpPos = (WINDOWPOS *)pos;
936#if defined(__WIN95__)
937 if (!(lpPos->flags & SWP_NOSIZE))
2bda0e17 938 {
42e69d6b
VZ
939 RECT rectClient;
940 DWORD dwExStyle = ::GetWindowLong((HWND) GetHWND(), GWL_EXSTYLE);
941 DWORD dwStyle = ::GetWindowLong((HWND) GetHWND(), GWL_STYLE);
942 if (ResetWindowStyle((void *) & rectClient) && (dwStyle & WS_MAXIMIZE))
943 {
944 ::AdjustWindowRectEx(&rectClient, dwStyle, FALSE, dwExStyle);
945 lpPos->x = rectClient.left;
946 lpPos->y = rectClient.top;
947 lpPos->cx = rectClient.right - rectClient.left;
948 lpPos->cy = rectClient.bottom - rectClient.top;
949 }
950 wxMDIParentFrame* pFrameWnd = (wxMDIParentFrame *)GetParent();
951 if (pFrameWnd && pFrameWnd->GetToolBar())
952 {
953 pFrameWnd->GetToolBar()->Refresh();
954 }
955 }
956#endif // Win95
957
958 return FALSE;
959}
960
961// ---------------------------------------------------------------------------
962// MDI specific message translation/preprocessing
963// ---------------------------------------------------------------------------
2bda0e17 964
42e69d6b
VZ
965long wxMDIChildFrame::MSWDefWindowProc(WXUINT message, WXUINT wParam, WXLPARAM lParam)
966{
967 return DefMDIChildProc(GetHwnd(),
968 (UINT)message, (WPARAM)wParam, (LPARAM)lParam);
969}
970
971bool wxMDIChildFrame::MSWTranslateMessage(WXMSG* msg)
972{
973 MSG *pMsg = (MSG *)msg;
974 if ( m_acceleratorTable.Ok() )
975 {
976 return ::TranslateAccelerator(GetWinHwnd(GetParent()),
977 GetTableHaccel(m_acceleratorTable),
978 pMsg) != 0;
2bda0e17 979 }
42e69d6b
VZ
980
981 return FALSE;
2bda0e17
KB
982}
983
42e69d6b
VZ
984// ---------------------------------------------------------------------------
985// misc
986// ---------------------------------------------------------------------------
987
c2dcfdef 988void wxMDIChildFrame::MSWDestroyWindow()
2bda0e17 989{
42e69d6b
VZ
990 MSWDetachWindowMenu();
991 invalidHandle = (HWND) GetHWND();
2bda0e17 992
42e69d6b 993 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
2bda0e17 994
42e69d6b
VZ
995 // Must make sure this handle is invalidated (set to NULL) since all sorts
996 // of things could happen after the child client is destroyed, but before
997 // the wxFrame is destroyed.
2bda0e17 998
42e69d6b 999 HWND oldHandle = (HWND)GetHWND();
2bda0e17 1000#ifdef __WIN32__
42e69d6b 1001 SendMessage((HWND) parent->GetClientWindow()->GetHWND(), WM_MDIDESTROY, (WPARAM)oldHandle, (LPARAM)0);
2bda0e17 1002#else
42e69d6b 1003 SendMessage((HWND) parent->GetClientWindow()->GetHWND(), WM_MDIDESTROY, (HWND)oldHandle, 0);
2bda0e17 1004#endif
42e69d6b 1005 invalidHandle = 0;
2bda0e17 1006
42e69d6b
VZ
1007 if (m_hMenu)
1008 {
1009 ::DestroyMenu((HMENU) m_hMenu);
1010 m_hMenu = 0;
1011 }
1012 m_hWnd = 0;
2bda0e17
KB
1013}
1014
42e69d6b
VZ
1015// Change the client window's extended style so we don't get a client edge
1016// style when a child is maximised (a double border looks silly.)
2bda0e17
KB
1017bool wxMDIChildFrame::ResetWindowStyle(void *vrect)
1018{
1019#if defined(__WIN95__)
1020 RECT *rect = (RECT *)vrect;
c2dcfdef
VZ
1021 wxMDIParentFrame* pFrameWnd = (wxMDIParentFrame *)GetParent();
1022 wxMDIChildFrame* pChild = pFrameWnd->GetActiveChild();
1023 if (!pChild || (pChild == this))
1024 {
1025 DWORD dwStyle = ::GetWindowLong((HWND) pFrameWnd->GetClientWindow()->GetHWND(), GWL_EXSTYLE);
2bda0e17 1026 DWORD dwThisStyle = ::GetWindowLong((HWND) GetHWND(), GWL_STYLE);
c2dcfdef
VZ
1027 DWORD dwNewStyle = dwStyle;
1028 if (pChild != NULL && (dwThisStyle & WS_MAXIMIZE))
1029 dwNewStyle &= ~(WS_EX_CLIENTEDGE);
1030 else
1031 dwNewStyle |= WS_EX_CLIENTEDGE;
1032
1033 if (dwStyle != dwNewStyle)
1034 {
42e69d6b
VZ
1035 HWND hwnd = GetWinHwnd(pFrameWnd->GetClientWindow());
1036 ::RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ALLCHILDREN);
1037 ::SetWindowLong(hwnd, GWL_EXSTYLE, dwNewStyle);
1038 ::SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
1039 SWP_FRAMECHANGED | SWP_NOACTIVATE |
1040 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
1041 SWP_NOCOPYBITS);
c2dcfdef 1042 if (rect)
42e69d6b 1043 ::GetClientRect(hwnd, rect);
2bda0e17 1044
42e69d6b 1045 return TRUE;
2bda0e17 1046 }
c2dcfdef 1047 }
42e69d6b 1048#endif // Win95
a23fd0e1
VZ
1049
1050 return FALSE;
2bda0e17
KB
1051}
1052
42e69d6b
VZ
1053// ===========================================================================
1054// wxMDIClientWindow: the window of predefined (by Windows) class which
1055// contains the child frames
1056// ===========================================================================
2bda0e17 1057
debe6624 1058bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style)
2bda0e17 1059{
42e69d6b 1060 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE);
2bda0e17 1061
42e69d6b
VZ
1062 CLIENTCREATESTRUCT ccs;
1063 m_windowStyle = style;
1064 m_parent = parent;
c2dcfdef 1065
42e69d6b
VZ
1066 ccs.hWindowMenu = (HMENU)parent->GetWindowMenu();
1067 ccs.idFirstChild = wxFIRST_MDI_CHILD;
2bda0e17 1068
42e69d6b
VZ
1069 DWORD msStyle = WS_VISIBLE | WS_CHILD | WS_CLIPCHILDREN;
1070 if ( style & wxHSCROLL )
1071 msStyle |= WS_HSCROLL;
1072 if ( style & wxVSCROLL )
1073 msStyle |= WS_VSCROLL;
2bda0e17
KB
1074
1075#if defined(__WIN95__)
42e69d6b 1076 DWORD exStyle = WS_EX_CLIENTEDGE;
2bda0e17 1077#else
42e69d6b 1078 DWORD exStyle = 0;
2bda0e17
KB
1079#endif
1080
42e69d6b
VZ
1081 wxWndHook = this;
1082 m_hWnd = (WXHWND)::CreateWindowEx
1083 (
1084 exStyle,
1085 "MDICLIENT",
1086 NULL,
1087 msStyle,
1088 0, 0, 0, 0,
1089 GetWinHwnd(parent),
1090 NULL,
1091 wxGetInstance(),
1092 (LPSTR)(LPCLIENTCREATESTRUCT)&ccs);
1093 if ( !m_hWnd )
1094 {
1095 wxLogLastError("CreateWindowEx(MDI client)");
2bda0e17 1096
42e69d6b
VZ
1097 return FALSE;
1098 }
2bda0e17 1099
42e69d6b
VZ
1100 SubclassWin(m_hWnd);
1101 wxWndHook = NULL;
2bda0e17 1102
42e69d6b 1103 return TRUE;
2bda0e17
KB
1104}
1105
1106// Explicitly call default scroll behaviour
1107void wxMDIClientWindow::OnScroll(wxScrollEvent& event)
1108{
1109 // Note: for client windows, the scroll position is not set in
1110 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1111 // scroll position we're at.
1112 // This makes it hard to paint patterns or bitmaps in the background,
1113 // and have the client area scrollable as well.
1114
1115 if ( event.GetOrientation() == wxHORIZONTAL )
1116 m_scrollX = event.GetPosition(); // Always returns zero!
1117 else
1118 m_scrollY = event.GetPosition(); // Always returns zero!
1119
42e69d6b
VZ
1120 event.Skip();
1121}
1122
1123// ---------------------------------------------------------------------------
1124// non member functions
1125// ---------------------------------------------------------------------------
1126
1127static void MDISetMenu(wxWindow *win, HMENU hmenuFrame, HMENU hmenuWindow)
1128{
1129 ::SendMessage(GetWinHwnd(win), WM_MDISETMENU,
1130#ifdef __WIN32__
1131 (WPARAM)hmenuFrame, (LPARAM)hmenuWindow);
1132#else
1133 0, MAKELPARAM(hmenuFrame, hmenuWindow));
1134#endif
1135
1136 // update menu bar of the parent window
1137 wxWindow *parent = win->GetParent();
1138 wxCHECK_RET( parent, "MDI client without parent frame? weird..." );
1139
1140 ::DrawMenuBar(GetWinHwnd(parent));
1141}
1142
1143static void InsertWindowMenu(wxWindow *win, WXHMENU menu, HMENU subMenu)
1144{
1145 // Try to insert Window menu in front of Help, otherwise append it.
1146 HMENU hmenu = (HMENU)menu;
1147 int N = GetMenuItemCount(hmenu);
1148 bool success = FALSE;
1149 for ( int i = 0; i < N; i++ )
1150 {
1151 char buf[256];
1152 int chars = GetMenuString(hmenu, i, buf, WXSIZEOF(buf), MF_BYPOSITION);
1153 if ( chars == 0 )
1154 {
1155 wxLogLastError("GetMenuString");
1156
1157 continue;
1158 }
1159
1160 if ( wxStripMenuCodes(wxString(buf)).IsSameAs(_("Help")) )
1161 {
1162 success = TRUE;
1163 ::InsertMenu(hmenu, i, MF_BYPOSITION | MF_POPUP | MF_STRING,
1164 (UINT)subMenu, "&Window");
1165 break;
1166 }
1167 }
1168
1169 if ( !success )
1170 {
1171 ::AppendMenu(hmenu, MF_POPUP, (UINT)subMenu, _("&Window"));
1172 }
1173
1174 MDISetMenu(win, hmenu, subMenu);
1175}
1176
2917e920
BM
1177static void UnpackMDIActivate(WXWPARAM wParam, WXLPARAM lParam,
1178 WXWORD *activate, WXHWND *hwndAct, WXHWND *hwndDeact)
42e69d6b
VZ
1179{
1180#ifdef __WIN32__
1181 *activate = TRUE;
1182 *hwndAct = (WXHWND)lParam;
1183 *hwndDeact = (WXHWND)wParam;
1184#else // Win16
1185 *activate = (WXWORD)wParam;
1186 *hwndAct = (WXHWND)LOWORD(lParam);
1187 *hwndDeact = (WXHWND)HIWORD(lParam);
1188#endif // Win32/Win16
2bda0e17 1189}