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