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