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