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