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