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