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