fixed recent modeless dialog breakage caused by removing wxDIALOG_MODAL (wxModelessWi...
[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 wxMenu *wxCurrentPopupMenu;
66
67 extern const wxChar *wxMDIFrameClassName; // from app.cpp
68 extern const wxChar *wxMDIChildFrameClassName;
69 extern const wxChar *wxMDIChildFrameClassNameNoRedraw;
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 msflags &= ~WS_VSCROLL;
214 msflags &= ~WS_HSCROLL;
215
216 if ( !wxWindow::MSWCreate(wxMDIFrameClassName,
217 title,
218 pos, size,
219 msflags,
220 exflags) )
221 {
222 return false;
223 }
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 // see comment in ~wxMDIChildFrame
234 #if wxUSE_TOOLBAR
235 m_frameToolBar = NULL;
236 #endif
237 m_frameStatusBar = NULL;
238
239 DestroyChildren();
240
241 if (m_windowMenu)
242 {
243 delete m_windowMenu;
244 m_windowMenu = (wxMenu*) NULL;
245 }
246
247 // the MDI frame menubar is not automatically deleted by Windows unlike for
248 // the normal frames
249 if ( m_hMenu )
250 {
251 ::DestroyMenu((HMENU)m_hMenu);
252 m_hMenu = (WXHMENU)NULL;
253 }
254
255 if ( m_clientWindow )
256 {
257 if ( m_clientWindow->MSWGetOldWndProc() )
258 m_clientWindow->UnsubclassWin();
259
260 m_clientWindow->SetHWND(0);
261 delete m_clientWindow;
262 }
263 }
264
265 #if wxUSE_MENUS_NATIVE
266
267 void wxMDIParentFrame::InternalSetMenuBar()
268 {
269 m_parentFrameActive = true;
270
271 InsertWindowMenu(GetClientWindow(), m_hMenu, GetMDIWindowMenu(this));
272 }
273
274 #endif // wxUSE_MENUS_NATIVE
275
276 void wxMDIParentFrame::SetWindowMenu(wxMenu* menu)
277 {
278 if (m_windowMenu)
279 {
280 if (GetMenuBar())
281 {
282 // Remove old window menu
283 RemoveWindowMenu(GetClientWindow(), m_hMenu);
284 }
285
286 delete m_windowMenu;
287 m_windowMenu = (wxMenu*) NULL;
288 }
289
290 if (menu)
291 {
292 m_windowMenu = menu;
293 if (GetMenuBar())
294 {
295 InsertWindowMenu(GetClientWindow(), m_hMenu,
296 GetHmenuOf(m_windowMenu));
297 }
298 }
299 }
300
301 void wxMDIParentFrame::OnSize(wxSizeEvent&)
302 {
303 if ( GetClientWindow() )
304 {
305 int width, height;
306 GetClientSize(&width, &height);
307
308 GetClientWindow()->SetSize(0, 0, width, height);
309 }
310 }
311
312 // Returns the active MDI child window
313 wxMDIChildFrame *wxMDIParentFrame::GetActiveChild() const
314 {
315 HWND hWnd = (HWND)::SendMessage(GetWinHwnd(GetClientWindow()),
316 WM_MDIGETACTIVE, 0, 0L);
317 if ( hWnd == 0 )
318 return NULL;
319 else
320 return (wxMDIChildFrame *)wxFindWinFromHandle((WXHWND) hWnd);
321 }
322
323 // Create the client window class (don't Create the window, just return a new
324 // class)
325 wxMDIClientWindow *wxMDIParentFrame::OnCreateClient()
326 {
327 return new wxMDIClientWindow;
328 }
329
330 // Responds to colour changes, and passes event on to children.
331 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
332 {
333 if ( m_clientWindow )
334 {
335 m_clientWindow->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
336 m_clientWindow->Refresh();
337 }
338
339 event.Skip();
340 }
341
342 WXHICON wxMDIParentFrame::GetDefaultIcon() const
343 {
344 // we don't have any standard icons (any more)
345 return (WXHICON)0;
346 }
347
348 // ---------------------------------------------------------------------------
349 // MDI operations
350 // ---------------------------------------------------------------------------
351
352 void wxMDIParentFrame::Cascade()
353 {
354 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE, 0, 0);
355 }
356
357 // TODO: add a direction argument (hor/vert)
358 void wxMDIParentFrame::Tile()
359 {
360 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE, MDITILE_HORIZONTAL, 0);
361 }
362
363 void wxMDIParentFrame::ArrangeIcons()
364 {
365 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE, 0, 0);
366 }
367
368 void wxMDIParentFrame::ActivateNext()
369 {
370 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT, 0, 0);
371 }
372
373 void wxMDIParentFrame::ActivatePrevious()
374 {
375 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT, 0, 1);
376 }
377
378 // ---------------------------------------------------------------------------
379 // the MDI parent frame window proc
380 // ---------------------------------------------------------------------------
381
382 WXLRESULT wxMDIParentFrame::MSWWindowProc(WXUINT message,
383 WXWPARAM wParam,
384 WXLPARAM lParam)
385 {
386 WXLRESULT rc = 0;
387 bool processed = false;
388
389 switch ( message )
390 {
391 case WM_ACTIVATE:
392 {
393 WXWORD state, minimized;
394 WXHWND hwnd;
395 UnpackActivate(wParam, lParam, &state, &minimized, &hwnd);
396
397 processed = HandleActivate(state, minimized != 0, hwnd);
398 }
399 break;
400
401 case WM_COMMAND:
402 {
403 WXWORD id, cmd;
404 WXHWND hwnd;
405 UnpackCommand(wParam, lParam, &id, &hwnd, &cmd);
406
407 (void)HandleCommand(id, cmd, hwnd);
408
409 // even if the frame didn't process it, there is no need to try it
410 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
411 // so pretend we processed the message anyhow
412 processed = true;
413 }
414
415 // always pass this message DefFrameProc(), otherwise MDI menu
416 // commands (and sys commands - more surprisingly!) won't work
417 MSWDefWindowProc(message, wParam, lParam);
418 break;
419
420 case WM_CREATE:
421 m_clientWindow = OnCreateClient();
422 // Uses own style for client style
423 if ( !m_clientWindow->CreateClient(this, GetWindowStyleFlag()) )
424 {
425 wxLogMessage(_("Failed to create MDI parent frame."));
426
427 rc = -1;
428 }
429
430 processed = true;
431 break;
432
433 case WM_ERASEBKGND:
434 processed = true;
435
436 // we erase background ourselves
437 rc = true;
438 break;
439
440 case WM_MENUSELECT:
441 {
442 WXWORD item, flags;
443 WXHMENU hmenu;
444 UnpackMenuSelect(wParam, lParam, &item, &flags, &hmenu);
445
446 if ( m_parentFrameActive )
447 {
448 processed = HandleMenuSelect(item, flags, hmenu);
449 }
450 else if (m_currentChild)
451 {
452 processed = m_currentChild->
453 HandleMenuSelect(item, flags, hmenu);
454 }
455 }
456 break;
457
458 case WM_SIZE:
459 // as we don't (usually) resize the MDI client to exactly fit the
460 // client area (we put it below the toolbar, above statusbar &c),
461 // we should not pass this one to DefFrameProc
462 break;
463 }
464
465 if ( !processed )
466 rc = wxFrame::MSWWindowProc(message, wParam, lParam);
467
468 return rc;
469 }
470
471 bool wxMDIParentFrame::HandleActivate(int state, bool minimized, WXHWND activate)
472 {
473 bool processed = false;
474
475 if ( wxWindow::HandleActivate(state, minimized, activate) )
476 {
477 // already processed
478 processed = true;
479 }
480
481 // If this window is an MDI parent, we must also send an OnActivate message
482 // to the current child.
483 if ( (m_currentChild != NULL) &&
484 ((state == WA_ACTIVE) || (state == WA_CLICKACTIVE)) )
485 {
486 wxActivateEvent event(wxEVT_ACTIVATE, true, m_currentChild->GetId());
487 event.SetEventObject( m_currentChild );
488 if ( m_currentChild->GetEventHandler()->ProcessEvent(event) )
489 processed = true;
490 }
491
492 return processed;
493 }
494
495 bool wxMDIParentFrame::HandleCommand(WXWORD id, WXWORD cmd, WXHWND hwnd)
496 {
497 // In case it's e.g. a toolbar.
498 if ( hwnd )
499 {
500 wxWindow *win = wxFindWinFromHandle(hwnd);
501 if ( win )
502 return win->MSWCommand(cmd, id);
503 }
504
505 // is it one of standard MDI commands?
506 WXWPARAM wParam = 0;
507 WXLPARAM lParam = 0;
508 int msg;
509 switch ( id )
510 {
511 case IDM_WINDOWCASCADE:
512 msg = WM_MDICASCADE;
513 wParam = MDITILE_SKIPDISABLED;
514 break;
515
516 case IDM_WINDOWTILEHOR:
517 wParam |= MDITILE_HORIZONTAL;
518 // fall through
519
520 case IDM_WINDOWTILEVERT:
521 if ( !wParam )
522 wParam = MDITILE_VERTICAL;
523 msg = WM_MDITILE;
524 wParam |= MDITILE_SKIPDISABLED;
525 break;
526
527 case IDM_WINDOWICONS:
528 msg = WM_MDIICONARRANGE;
529 break;
530
531 case IDM_WINDOWNEXT:
532 msg = WM_MDINEXT;
533 lParam = 0; // next child
534 break;
535
536 case IDM_WINDOWPREV:
537 msg = WM_MDINEXT;
538 lParam = 1; // previous child
539 break;
540
541 default:
542 msg = 0;
543 }
544
545 if ( msg )
546 {
547 ::SendMessage(GetWinHwnd(GetClientWindow()), msg, wParam, lParam);
548
549 return true;
550 }
551
552 // FIXME VZ: what does this test do??
553 if (id >= 0xF000)
554 {
555 return false; // Get WndProc to call default proc
556 }
557
558 if ( IsMdiCommandId(id) )
559 {
560 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
561 while ( node )
562 {
563 wxWindow *child = node->GetData();
564 if ( child->GetHWND() )
565 {
566 long childId = wxGetWindowId(child->GetHWND());
567 if (childId == (long)id)
568 {
569 ::SendMessage( GetWinHwnd(GetClientWindow()),
570 WM_MDIACTIVATE,
571 (WPARAM)child->GetHWND(), 0);
572 return true;
573 }
574 }
575 node = node->GetNext();
576 }
577 }
578 else if ( m_parentFrameActive )
579 {
580 return ProcessCommand(id);
581 }
582 else if ( m_currentChild )
583 {
584 return m_currentChild->HandleCommand(id, cmd, hwnd);
585 }
586 else
587 {
588 // this shouldn't happen because it means that our messages are being
589 // lost (they're not sent to the parent frame nor to the children)
590 wxFAIL_MSG(wxT("MDI parent frame is not active, yet there is no active MDI child?"));
591 }
592
593 return false;
594 }
595
596 WXLRESULT wxMDIParentFrame::MSWDefWindowProc(WXUINT message,
597 WXWPARAM wParam,
598 WXLPARAM lParam)
599 {
600 WXHWND clientWnd;
601 if ( GetClientWindow() )
602 clientWnd = GetClientWindow()->GetHWND();
603 else
604 clientWnd = 0;
605
606 return DefFrameProc(GetHwnd(), (HWND)clientWnd, message, wParam, lParam);
607 }
608
609 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG* msg)
610 {
611 MSG *pMsg = (MSG *)msg;
612
613 // first let the current child get it
614 if ( m_currentChild && m_currentChild->GetHWND() &&
615 m_currentChild->MSWTranslateMessage(msg) )
616 {
617 return true;
618 }
619
620 // then try out accel table (will also check the menu accels)
621 if ( wxFrame::MSWTranslateMessage(msg) )
622 {
623 return true;
624 }
625
626 // finally, check for MDI specific built in accel keys
627 if ( pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN )
628 {
629 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg))
630 return true;
631 }
632
633 return false;
634 }
635
636 // ===========================================================================
637 // wxMDIChildFrame
638 // ===========================================================================
639
640 void wxMDIChildFrame::Init()
641 {
642 m_needsResize = true;
643 }
644
645 bool wxMDIChildFrame::Create(wxMDIParentFrame *parent,
646 wxWindowID id,
647 const wxString& title,
648 const wxPoint& pos,
649 const wxSize& size,
650 long style,
651 const wxString& name)
652 {
653 SetName(name);
654 wxWindowBase::Show(true); // MDI child frame starts off shown
655
656 if ( id > -1 )
657 m_windowId = id;
658 else
659 m_windowId = (int)NewControlId();
660
661 if ( parent )
662 {
663 parent->AddChild(this);
664 }
665
666 int x = pos.x;
667 int y = pos.y;
668 int width = size.x;
669 int height = size.y;
670
671 MDICREATESTRUCT mcs;
672
673 mcs.szClass = style & wxFULL_REPAINT_ON_RESIZE
674 ? wxMDIChildFrameClassName
675 : wxMDIChildFrameClassNameNoRedraw;
676 mcs.szTitle = title;
677 mcs.hOwner = wxGetInstance();
678 if (x > -1)
679 mcs.x = x;
680 else
681 mcs.x = CW_USEDEFAULT;
682
683 if (y > -1)
684 mcs.y = y;
685 else
686 mcs.y = CW_USEDEFAULT;
687
688 if (width > -1)
689 mcs.cx = width;
690 else
691 mcs.cx = CW_USEDEFAULT;
692
693 if (height > -1)
694 mcs.cy = height;
695 else
696 mcs.cy = CW_USEDEFAULT;
697
698 DWORD msflags = WS_OVERLAPPED | WS_CLIPCHILDREN | WS_VISIBLE ;
699 if (style & wxMINIMIZE_BOX)
700 msflags |= WS_MINIMIZEBOX;
701 if (style & wxMAXIMIZE_BOX)
702 msflags |= WS_MAXIMIZEBOX;
703 if (style & wxTHICK_FRAME)
704 msflags |= WS_THICKFRAME;
705 if (style & wxSYSTEM_MENU)
706 msflags |= WS_SYSMENU;
707 if ((style & wxMINIMIZE) || (style & wxICONIZE))
708 msflags |= WS_MINIMIZE;
709 if (style & wxMAXIMIZE)
710 msflags |= WS_MAXIMIZE;
711 if (style & wxCAPTION)
712 msflags |= WS_CAPTION;
713
714 mcs.style = msflags;
715
716 mcs.lParam = 0;
717
718 wxWindowCreationHook hook(this);
719
720 m_hWnd = (WXHWND)::SendMessage(GetWinHwnd(parent->GetClientWindow()),
721 WM_MDICREATE, 0, (LONG)(LPSTR)&mcs);
722
723 wxAssociateWinWithHandle((HWND) GetHWND(), this);
724
725 return true;
726 }
727
728 wxMDIChildFrame::~wxMDIChildFrame()
729 {
730 // will be destroyed by DestroyChildren() but reset them before calling it
731 // to avoid using dangling pointers if a callback comes in the meanwhile
732 #if wxUSE_TOOLBAR
733 m_frameToolBar = NULL;
734 #endif
735 m_frameStatusBar = NULL;
736
737 DestroyChildren();
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 WXLRESULT wxMDIChildFrame::MSWWindowProc(WXUINT message,
858 WXWPARAM wParam,
859 WXLPARAM lParam)
860 {
861 WXLRESULT 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 #if wxUSE_TOOLBAR
1030 wxMDIParentFrame* pFrameWnd = (wxMDIParentFrame *)GetParent();
1031 if (pFrameWnd && pFrameWnd->GetToolBar() && pFrameWnd->GetToolBar()->IsShown())
1032 {
1033 pFrameWnd->GetToolBar()->Refresh();
1034 }
1035 #endif
1036 }
1037 #endif // Win95
1038
1039 return false;
1040 }
1041
1042 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo)
1043 {
1044 MINMAXINFO *info = (MINMAXINFO *)mmInfo;
1045
1046 // let the default window proc calculate the size of MDI children
1047 // frames because it is based on the size of the MDI client window,
1048 // not on the values specified in wxWindow m_max variables
1049 bool processed = MSWDefWindowProc(WM_GETMINMAXINFO, 0, (LPARAM)mmInfo) != 0;
1050
1051 int minWidth = GetMinWidth(),
1052 minHeight = GetMinHeight();
1053
1054 // but allow GetSizeHints() to set the min size
1055 if ( minWidth != -1 )
1056 {
1057 info->ptMinTrackSize.x = minWidth;
1058
1059 processed = true;
1060 }
1061
1062 if ( minHeight != -1 )
1063 {
1064 info->ptMinTrackSize.y = minHeight;
1065
1066 processed = true;
1067 }
1068
1069 return processed;
1070 }
1071
1072 // ---------------------------------------------------------------------------
1073 // MDI specific message translation/preprocessing
1074 // ---------------------------------------------------------------------------
1075
1076 WXLRESULT wxMDIChildFrame::MSWDefWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
1077 {
1078 return DefMDIChildProc(GetHwnd(),
1079 (UINT)message, (WPARAM)wParam, (LPARAM)lParam);
1080 }
1081
1082 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG* msg)
1083 {
1084 return wxFrame::MSWTranslateMessage(msg);
1085 }
1086
1087 // ---------------------------------------------------------------------------
1088 // misc
1089 // ---------------------------------------------------------------------------
1090
1091 void wxMDIChildFrame::MSWDestroyWindow()
1092 {
1093 invalidHandle = GetHwnd();
1094
1095 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
1096
1097 // Must make sure this handle is invalidated (set to NULL) since all sorts
1098 // of things could happen after the child client is destroyed, but before
1099 // the wxFrame is destroyed.
1100
1101 HWND oldHandle = (HWND)GetHWND();
1102 SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIDESTROY,
1103 (WPARAM)oldHandle, 0);
1104
1105 if (parent->GetActiveChild() == (wxMDIChildFrame*) NULL)
1106 ResetWindowStyle((void*) NULL);
1107
1108 invalidHandle = 0;
1109
1110 if (m_hMenu)
1111 {
1112 ::DestroyMenu((HMENU) m_hMenu);
1113 m_hMenu = 0;
1114 }
1115 wxRemoveHandleAssociation(this);
1116 m_hWnd = 0;
1117 }
1118
1119 // Change the client window's extended style so we don't get a client edge
1120 // style when a child is maximised (a double border looks silly.)
1121 bool wxMDIChildFrame::ResetWindowStyle(void *vrect)
1122 {
1123 #if defined(__WIN95__)
1124 RECT *rect = (RECT *)vrect;
1125 wxMDIParentFrame* pFrameWnd = (wxMDIParentFrame *)GetParent();
1126 wxMDIChildFrame* pChild = pFrameWnd->GetActiveChild();
1127 if (!pChild || (pChild == this))
1128 {
1129 HWND hwndClient = GetWinHwnd(pFrameWnd->GetClientWindow());
1130 DWORD dwStyle = ::GetWindowLong(hwndClient, GWL_EXSTYLE);
1131
1132 // we want to test whether there is a maximized child, so just set
1133 // dwThisStyle to 0 if there is no child at all
1134 DWORD dwThisStyle = pChild
1135 ? ::GetWindowLong(GetWinHwnd(pChild), GWL_STYLE) : 0;
1136 DWORD dwNewStyle = dwStyle;
1137 if ( dwThisStyle & WS_MAXIMIZE )
1138 dwNewStyle &= ~(WS_EX_CLIENTEDGE);
1139 else
1140 dwNewStyle |= WS_EX_CLIENTEDGE;
1141
1142 if (dwStyle != dwNewStyle)
1143 {
1144 // force update of everything
1145 ::RedrawWindow(hwndClient, NULL, NULL,
1146 RDW_INVALIDATE | RDW_ALLCHILDREN);
1147 ::SetWindowLong(hwndClient, GWL_EXSTYLE, dwNewStyle);
1148 ::SetWindowPos(hwndClient, NULL, 0, 0, 0, 0,
1149 SWP_FRAMECHANGED | SWP_NOACTIVATE |
1150 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
1151 SWP_NOCOPYBITS);
1152 if (rect)
1153 ::GetClientRect(hwndClient, rect);
1154
1155 return true;
1156 }
1157 }
1158 #endif // Win95
1159
1160 return false;
1161 }
1162
1163 // ===========================================================================
1164 // wxMDIClientWindow: the window of predefined (by Windows) class which
1165 // contains the child frames
1166 // ===========================================================================
1167
1168 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style)
1169 {
1170 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
1171
1172 CLIENTCREATESTRUCT ccs;
1173 m_windowStyle = style;
1174 m_parent = parent;
1175
1176 ccs.hWindowMenu = GetMDIWindowMenu(parent);
1177 ccs.idFirstChild = wxFIRST_MDI_CHILD;
1178
1179 DWORD msStyle = MDIS_ALLCHILDSTYLES | WS_VISIBLE | WS_CHILD |
1180 WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
1181
1182 if ( style & wxHSCROLL )
1183 msStyle |= WS_HSCROLL;
1184 if ( style & wxVSCROLL )
1185 msStyle |= WS_VSCROLL;
1186
1187 #if defined(__WIN95__)
1188 DWORD exStyle = WS_EX_CLIENTEDGE;
1189 #else
1190 DWORD exStyle = 0;
1191 #endif
1192
1193 wxWindowCreationHook hook(this);
1194 m_hWnd = (WXHWND)::CreateWindowEx
1195 (
1196 exStyle,
1197 wxT("MDICLIENT"),
1198 NULL,
1199 msStyle,
1200 0, 0, 0, 0,
1201 GetWinHwnd(parent),
1202 NULL,
1203 wxGetInstance(),
1204 (LPSTR)(LPCLIENTCREATESTRUCT)&ccs);
1205 if ( !m_hWnd )
1206 {
1207 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1208
1209 return false;
1210 }
1211
1212 SubclassWin(m_hWnd);
1213
1214 return true;
1215 }
1216
1217 // Explicitly call default scroll behaviour
1218 void wxMDIClientWindow::OnScroll(wxScrollEvent& event)
1219 {
1220 // Note: for client windows, the scroll position is not set in
1221 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1222 // scroll position we're at.
1223 // This makes it hard to paint patterns or bitmaps in the background,
1224 // and have the client area scrollable as well.
1225
1226 if ( event.GetOrientation() == wxHORIZONTAL )
1227 m_scrollX = event.GetPosition(); // Always returns zero!
1228 else
1229 m_scrollY = event.GetPosition(); // Always returns zero!
1230
1231 event.Skip();
1232 }
1233
1234 void wxMDIClientWindow::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1235 {
1236 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1237 // client area, you can end up with a non-refreshed portion in the client window
1238 // (see OGL studio sample). So check if the position is changed and if so,
1239 // redraw the MDI child frames.
1240
1241 wxPoint oldPos = GetPosition();
1242
1243 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
1244
1245 wxPoint newPos = GetPosition();
1246
1247 if ((newPos.x != oldPos.x) || (newPos.y != oldPos.y))
1248 {
1249 if (GetParent())
1250 {
1251 wxWindowList::compatibility_iterator node = GetParent()->GetChildren().GetFirst();
1252 while (node)
1253 {
1254 wxWindow *child = node->GetData();
1255 if (child->IsKindOf(CLASSINFO(wxMDIChildFrame)))
1256 {
1257 ::RedrawWindow(GetHwndOf(child),
1258 NULL,
1259 NULL,
1260 RDW_FRAME |
1261 RDW_ALLCHILDREN |
1262 RDW_INVALIDATE);
1263 }
1264 node = node->GetNext();
1265 }
1266 }
1267 }
1268 }
1269
1270 void wxMDIChildFrame::OnIdle(wxIdleEvent& event)
1271 {
1272 // MDI child frames get their WM_SIZE when they're constructed but at this
1273 // moment they don't have any children yet so all child windows will be
1274 // positioned incorrectly when they are added later - to fix this, we
1275 // generate an artificial size event here
1276 if ( m_needsResize )
1277 {
1278 m_needsResize = false; // avoid any possibility of recursion
1279
1280 SendSizeEvent();
1281 }
1282
1283 event.Skip();
1284 }
1285
1286 // ---------------------------------------------------------------------------
1287 // non member functions
1288 // ---------------------------------------------------------------------------
1289
1290 static void MDISetMenu(wxWindow *win, HMENU hmenuFrame, HMENU hmenuWindow)
1291 {
1292 ::SendMessage(GetWinHwnd(win), WM_MDISETMENU,
1293 #ifdef __WIN32__
1294 (WPARAM)hmenuFrame, (LPARAM)hmenuWindow
1295 #else
1296 0, MAKELPARAM(hmenuFrame, hmenuWindow)
1297 #endif
1298 );
1299
1300 // update menu bar of the parent window
1301 wxWindow *parent = win->GetParent();
1302 wxCHECK_RET( parent, wxT("MDI client without parent frame? weird...") );
1303
1304 ::SendMessage(GetWinHwnd(win), WM_MDIREFRESHMENU, 0, 0L);
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 *activate = true;
1393 *hwndAct = (WXHWND)lParam;
1394 *hwndDeact = (WXHWND)wParam;
1395 }
1396
1397 #endif
1398 // wxUSE_MDI_ARCHITECTURE && !defined(__WXUNIVERSAL__)
1399