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