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