]> git.saurik.com Git - wxWidgets.git/blob - src/msw/mdi.cpp
3624c580e9a037575c2fa516ef2e7a1cbde7caed
[wxWidgets.git] / src / msw / mdi.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/mdi.cpp
3 // Purpose: MDI classes for wxMSW
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "mdi.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/setup.h"
33 #include "wx/frame.h"
34 #include "wx/menu.h"
35 #include "wx/app.h"
36 #include "wx/utils.h"
37 #include "wx/dialog.h"
38 #if wxUSE_STATUSBAR
39 #include "wx/statusbr.h"
40 #endif
41 #include "wx/settings.h"
42 #include "wx/intl.h"
43 #include "wx/log.h"
44 #endif
45
46 #include "wx/mdi.h"
47 #include "wx/msw/private.h"
48
49 #if wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR
50 #include "wx/msw/statbr95.h"
51 #endif
52
53 #if wxUSE_TOOLBAR
54 #include "wx/toolbar.h"
55 #endif // wxUSE_TOOLBAR
56
57 #include <string.h>
58
59 // ---------------------------------------------------------------------------
60 // global variables
61 // ---------------------------------------------------------------------------
62
63 extern wxWindowList wxModelessWindows; // from dialog.cpp
64 extern wxMenu *wxCurrentPopupMenu;
65
66 extern const wxChar *wxMDIFrameClassName; // from app.cpp
67 extern const wxChar *wxMDIChildFrameClassName;
68 extern const wxChar *wxMDIChildFrameClassNameNoRedraw;
69
70 extern wxWindow *wxWndHook; // from window.cpp
71
72 extern void wxAssociateWinWithHandle(HWND hWnd, wxWindow *win);
73 extern void wxRemoveHandleAssociation(wxWindow *win);
74
75 static HWND invalidHandle = 0;
76
77 // ---------------------------------------------------------------------------
78 // constants
79 // ---------------------------------------------------------------------------
80
81 static const int IDM_WINDOWTILE = 4001;
82 static const int IDM_WINDOWTILEHOR = 4001;
83 static const int IDM_WINDOWCASCADE = 4002;
84 static const int IDM_WINDOWICONS = 4003;
85 static const int IDM_WINDOWNEXT = 4004;
86 static const int IDM_WINDOWTILEVERT = 4005;
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 static void UnpackMDIActivate(WXWPARAM wParam, WXLPARAM lParam,
118 WXWORD *activate, WXHWND *hwndAct, WXHWND *hwndDeact);
119
120 // ===========================================================================
121 // implementation
122 // ===========================================================================
123
124 // ---------------------------------------------------------------------------
125 // wxWin macros
126 // ---------------------------------------------------------------------------
127
128 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame, wxFrame)
129 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame, wxFrame)
130 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow, wxWindow)
131
132 BEGIN_EVENT_TABLE(wxMDIParentFrame, wxFrame)
133 EVT_SIZE(wxMDIParentFrame::OnSize)
134 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged)
135 END_EVENT_TABLE()
136
137 BEGIN_EVENT_TABLE(wxMDIChildFrame, wxFrame)
138 EVT_IDLE(wxMDIChildFrame::OnIdle)
139 END_EVENT_TABLE()
140
141 BEGIN_EVENT_TABLE(wxMDIClientWindow, wxWindow)
142 EVT_SCROLL(wxMDIClientWindow::OnScroll)
143 END_EVENT_TABLE()
144
145 // ===========================================================================
146 // wxMDIParentFrame: the frame which contains the client window which manages
147 // the children
148 // ===========================================================================
149
150 wxMDIParentFrame::wxMDIParentFrame()
151 {
152 m_clientWindow = NULL;
153 m_currentChild = NULL;
154 m_windowMenu = (wxMenu*) NULL;
155 m_parentFrameActive = TRUE;
156 }
157
158 bool wxMDIParentFrame::Create(wxWindow *parent,
159 wxWindowID id,
160 const wxString& title,
161 const wxPoint& pos,
162 const wxSize& size,
163 long style,
164 const wxString& name)
165 {
166 m_defaultIcon = (WXHICON) (wxSTD_MDIPARENTFRAME_ICON
167 ? wxSTD_MDIPARENTFRAME_ICON
168 : wxDEFAULT_MDIPARENTFRAME_ICON);
169
170 m_clientWindow = NULL;
171 m_currentChild = NULL;
172
173 // this style can be used to prevent a window from having the standard MDI
174 // "Window" menu
175 if ( style & wxFRAME_NO_WINDOW_MENU )
176 {
177 m_windowMenu = (wxMenu *)NULL;
178 }
179 else // normal case: we have the window menu, so construct it
180 {
181 m_windowMenu = new wxMenu;
182
183 m_windowMenu->Append(IDM_WINDOWCASCADE, _("&Cascade"));
184 m_windowMenu->Append(IDM_WINDOWTILEHOR, _("Tile &Horizontally"));
185 m_windowMenu->Append(IDM_WINDOWTILEVERT, _("Tile &Vertically"));
186 m_windowMenu->AppendSeparator();
187 m_windowMenu->Append(IDM_WINDOWICONS, _("&Arrange Icons"));
188 m_windowMenu->Append(IDM_WINDOWNEXT, _("&Next"));
189 }
190
191 m_parentFrameActive = TRUE;
192
193 if (!parent)
194 wxTopLevelWindows.Append(this);
195
196 SetName(name);
197 m_windowStyle = style;
198
199 if (parent) parent->AddChild(this);
200
201 if ( id > -1 )
202 m_windowId = id;
203 else
204 m_windowId = (int)NewControlId();
205
206 int x = pos.x;
207 int y = pos.y;
208 int width = size.x;
209 int height = size.y;
210
211 DWORD msflags = WS_OVERLAPPED;
212 if (style & wxMINIMIZE_BOX)
213 msflags |= WS_MINIMIZEBOX;
214 if (style & wxMAXIMIZE_BOX)
215 msflags |= WS_MAXIMIZEBOX;
216 if (style & wxTHICK_FRAME)
217 msflags |= WS_THICKFRAME;
218 if (style & wxSYSTEM_MENU)
219 msflags |= WS_SYSMENU;
220 if ((style & wxMINIMIZE) || (style & wxICONIZE))
221 msflags |= WS_MINIMIZE;
222 if (style & wxMAXIMIZE)
223 msflags |= WS_MAXIMIZE;
224 if (style & wxCAPTION)
225 msflags |= WS_CAPTION;
226
227 if (style & wxCLIP_CHILDREN)
228 msflags |= WS_CLIPCHILDREN;
229
230 if ( !wxWindow::MSWCreate(m_windowId,
231 parent,
232 wxMDIFrameClassName,
233 this,
234 title,
235 x, y, width, height,
236 msflags) )
237 {
238 return FALSE;
239 }
240
241 wxModelessWindows.Append(this);
242
243 // unlike (almost?) all other windows, frames are created hidden
244 m_isShown = FALSE;
245
246 return TRUE;
247 }
248
249 wxMDIParentFrame::~wxMDIParentFrame()
250 {
251 DestroyChildren();
252 // already delete by DestroyChildren()
253 m_frameToolBar = NULL;
254 m_frameStatusBar = NULL;
255
256 // ::DestroyMenu((HMENU)m_windowMenu);
257 if (m_windowMenu)
258 {
259 delete m_windowMenu;
260 m_windowMenu = (wxMenu*) NULL;
261 }
262
263 if ( m_clientWindow )
264 {
265 if ( m_clientWindow->MSWGetOldWndProc() )
266 m_clientWindow->UnsubclassWin();
267
268 m_clientWindow->SetHWND(0);
269 delete m_clientWindow;
270 }
271 }
272
273 void wxMDIParentFrame::InternalSetMenuBar()
274 {
275 // HMENU subMenu = GetSubMenu((HMENU) m_windowMenu, 0);
276
277 m_parentFrameActive = TRUE;
278
279 HMENU subMenu = (HMENU) 0;
280 if (GetWindowMenu())
281 subMenu = (HMENU) GetWindowMenu()->GetHMenu();
282
283 InsertWindowMenu(GetClientWindow(), m_hMenu, subMenu);
284 }
285
286 void wxMDIParentFrame::SetWindowMenu(wxMenu* menu)
287 {
288 if (m_windowMenu)
289 {
290 if (GetMenuBar())
291 {
292 // Remove old window menu
293 RemoveWindowMenu(GetClientWindow(), m_hMenu);
294 }
295
296 delete m_windowMenu;
297 m_windowMenu = (wxMenu*) NULL;
298 }
299 if (menu)
300 {
301 m_windowMenu = menu;
302 if (GetMenuBar())
303 {
304 InsertWindowMenu(GetClientWindow(), m_hMenu,
305 GetHMenuOf(m_windowMenu));
306 }
307 }
308 }
309
310 void wxMDIParentFrame::OnSize(wxSizeEvent&)
311 {
312 if ( GetClientWindow() )
313 {
314 int width, height;
315 GetClientSize(&width, &height);
316
317 GetClientWindow()->SetSize(0, 0, width, height);
318 }
319 }
320
321 // Returns the active MDI child window
322 wxMDIChildFrame *wxMDIParentFrame::GetActiveChild() const
323 {
324 HWND hWnd = (HWND)::SendMessage(GetWinHwnd(GetClientWindow()),
325 WM_MDIGETACTIVE, 0, 0L);
326 if ( hWnd == 0 )
327 return NULL;
328 else
329 return (wxMDIChildFrame *)wxFindWinFromHandle((WXHWND) hWnd);
330 }
331
332 // Create the client window class (don't Create the window, just return a new
333 // class)
334 wxMDIClientWindow *wxMDIParentFrame::OnCreateClient()
335 {
336 return new wxMDIClientWindow;
337 }
338
339 // Responds to colour changes, and passes event on to children.
340 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
341 {
342 if ( m_clientWindow )
343 {
344 m_clientWindow->SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE));
345 m_clientWindow->Refresh();
346 }
347
348 event.Skip();
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 long wxMDIParentFrame::MSWWindowProc(WXUINT message,
386 WXWPARAM wParam,
387 WXLPARAM lParam)
388 {
389 long 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 dud 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 surprizingly!) 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 int msg;
511 switch ( id )
512 {
513 case IDM_WINDOWCASCADE:
514 msg = WM_MDICASCADE;
515 wParam = MDITILE_SKIPDISABLED;
516 break;
517
518 case IDM_WINDOWTILEHOR:
519 wParam |= MDITILE_HORIZONTAL;
520 // fall through
521
522 case IDM_WINDOWTILEVERT:
523 if ( !wParam )
524 wParam = MDITILE_VERTICAL;
525 msg = WM_MDITILE;
526 wParam |= MDITILE_SKIPDISABLED;
527 break;
528
529 case IDM_WINDOWICONS:
530 msg = WM_MDIICONARRANGE;
531 break;
532
533 case IDM_WINDOWNEXT:
534 msg = WM_MDINEXT;
535 break;
536
537 default:
538 msg = 0;
539 }
540
541 if ( msg )
542 {
543 ::SendMessage(GetWinHwnd(GetClientWindow()), msg, wParam, 0);
544
545 return TRUE;
546 }
547
548 // FIXME VZ: what does this test do??
549 if (id >= 0xF000)
550 {
551 return FALSE; // Get WndProc to call default proc
552 }
553
554 if ( IsMdiCommandId(id) )
555 {
556 wxWindowList::Node* node = GetChildren().GetFirst();
557 while ( node )
558 {
559 wxWindow* child = node->GetData();
560 if ( child->GetHWND() )
561 {
562 long childId = wxGetWindowId(child->GetHWND());
563 if (childId == (long)id)
564 {
565 ::SendMessage( GetWinHwnd(GetClientWindow()),
566 WM_MDIACTIVATE,
567 (WPARAM)child->GetHWND(), 0);
568 return TRUE;
569 }
570 }
571 node = node->GetNext();
572 }
573 }
574 else if ( m_parentFrameActive )
575 {
576 return ProcessCommand(id);
577 }
578 else if ( m_currentChild )
579 {
580 return m_currentChild->HandleCommand(id, cmd, hwnd);
581 }
582 else
583 {
584 // this shouldn't happen because it means that our messages are being
585 // lost (they're not sent to the parent frame nor to the children)
586 wxFAIL_MSG(wxT("MDI parent frame is not active, yet there is no active MDI child?"));
587 }
588
589 return FALSE;
590 }
591
592 long wxMDIParentFrame::MSWDefWindowProc(WXUINT message,
593 WXWPARAM wParam,
594 WXLPARAM lParam)
595 {
596 WXHWND clientWnd;
597 if ( GetClientWindow() )
598 clientWnd = GetClientWindow()->GetHWND();
599 else
600 clientWnd = 0;
601
602 return DefFrameProc(GetHwnd(), (HWND)clientWnd, message, wParam, lParam);
603 }
604
605 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG* msg)
606 {
607 MSG *pMsg = (MSG *)msg;
608
609 // first let the current child get it
610 if ( m_currentChild && m_currentChild->GetHWND() &&
611 m_currentChild->MSWTranslateMessage(msg) )
612 {
613 return TRUE;
614 }
615
616 // then try out accel table (will also check the menu accels)
617 if ( wxFrame::MSWTranslateMessage(msg) )
618 {
619 return TRUE;
620 }
621
622 // finally, check for MDI specific built in accel keys
623 if ( pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN )
624 {
625 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg))
626 return TRUE;
627 }
628
629 return FALSE;
630 }
631
632 // ===========================================================================
633 // wxMDIChildFrame
634 // ===========================================================================
635
636 void wxMDIChildFrame::Init()
637 {
638 m_needsResize = TRUE;
639 }
640
641 bool wxMDIChildFrame::Create(wxMDIParentFrame *parent,
642 wxWindowID id,
643 const wxString& title,
644 const wxPoint& pos,
645 const wxSize& size,
646 long style,
647 const wxString& name)
648 {
649 m_defaultIcon = (WXHICON)(wxSTD_MDICHILDFRAME_ICON ? wxSTD_MDICHILDFRAME_ICON
650 : wxDEFAULT_MDICHILDFRAME_ICON);
651
652 SetName(name);
653
654 if ( id > -1 )
655 m_windowId = id;
656 else
657 m_windowId = (int)NewControlId();
658
659 if ( parent )
660 {
661 parent->AddChild(this);
662 }
663
664 wxWndHook = this;
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 & wxNO_FULL_REPAINT_ON_RESIZE
674 ? wxMDIChildFrameClassNameNoRedraw
675 : wxMDIChildFrameClassName;
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;
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 m_hWnd = (WXHWND)::SendMessage(GetWinHwnd(parent->GetClientWindow()),
719 WM_MDICREATE, 0, (LONG)(LPSTR)&mcs);
720
721 wxWndHook = NULL;
722 wxAssociateWinWithHandle((HWND) GetHWND(), this);
723
724 // VZ: what's this? an act of piracy?
725 //SetWindowLong(GetHwnd(), 0, (long)this);
726
727 wxModelessWindows.Append(this);
728
729 // Necessary to make ResetWindowStyle() work
730 // and will be called eventually anyway
731 Activate();
732
733 // Without this, a maximized child will still
734 // a double border around the child
735 ResetWindowStyle((void *)NULL); // Set the Client ExStyle right
736
737 return TRUE;
738 }
739
740 wxMDIChildFrame::~wxMDIChildFrame()
741 {
742 DestroyChildren();
743
744 // already delete by DestroyChildren()
745 m_frameToolBar = NULL;
746 m_frameStatusBar = NULL;
747
748 MSWDestroyWindow();
749 }
750
751 // Set the client size (i.e. leave the calculation of borders etc.
752 // to wxWindows)
753 void wxMDIChildFrame::DoSetClientSize(int width, int height)
754 {
755 HWND hWnd = GetHwnd();
756
757 RECT rect;
758 ::GetClientRect(hWnd, &rect);
759
760 RECT rect2;
761 GetWindowRect(hWnd, &rect2);
762
763 // Find the difference between the entire window (title bar and all)
764 // and the client area; add this to the new client size to move the
765 // window
766 int actual_width = rect2.right - rect2.left - rect.right + width;
767 int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
768
769 if (GetStatusBar() && GetStatusBar()->IsShown())
770 {
771 int sx, sy;
772 GetStatusBar()->GetSize(&sx, &sy);
773 actual_height += sy;
774 }
775
776 POINT point;
777 point.x = rect2.left;
778 point.y = rect2.top;
779
780 // If there's an MDI parent, must subtract the parent's top left corner
781 // since MoveWindow moves relative to the parent
782 wxMDIParentFrame *mdiParent = (wxMDIParentFrame *)GetParent();
783 ::ScreenToClient((HWND) mdiParent->GetClientWindow()->GetHWND(), &point);
784
785 MoveWindow(hWnd, point.x, point.y, actual_width, actual_height, (BOOL)TRUE);
786
787 wxSizeEvent event(wxSize(width, height), m_windowId);
788 event.SetEventObject( this );
789 GetEventHandler()->ProcessEvent(event);
790 }
791
792 void wxMDIChildFrame::DoGetPosition(int *x, int *y) const
793 {
794 RECT rect;
795 GetWindowRect(GetHwnd(), &rect);
796 POINT point;
797 point.x = rect.left;
798 point.y = rect.top;
799
800 // Since we now have the absolute screen coords,
801 // if there's a parent we must subtract its top left corner
802 wxMDIParentFrame *mdiParent = (wxMDIParentFrame *)GetParent();
803 ::ScreenToClient((HWND) mdiParent->GetClientWindow()->GetHWND(), &point);
804
805 *x = point.x;
806 *y = point.y;
807 }
808
809 void wxMDIChildFrame::InternalSetMenuBar()
810 {
811 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
812
813 // HMENU subMenu = GetSubMenu((HMENU)parent->GetWindowMenu(), 0);
814 HMENU subMenu = (HMENU) 0;
815 if (parent->GetWindowMenu())
816 subMenu = (HMENU) parent->GetWindowMenu()->GetHMenu();
817
818 InsertWindowMenu(parent->GetClientWindow(), m_hMenu, subMenu);
819
820 parent->m_parentFrameActive = FALSE;
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 long wxMDIChildFrame::MSWWindowProc(WXUINT message,
863 WXWPARAM wParam,
864 WXLPARAM lParam)
865 {
866 long 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 // let the default window proc calculate the size of MDI children
884 // frames because it is based on the size of the MDI client window,
885 // not on the values specified in wxWindow m_min/max variables
886 return MSWDefWindowProc(message, wParam, lParam);
887
888 case WM_MDIACTIVATE:
889 {
890 WXWORD act;
891 WXHWND hwndAct, hwndDeact;
892 UnpackMDIActivate(wParam, lParam, &act, &hwndAct, &hwndDeact);
893
894 processed = HandleMDIActivate(act, hwndAct, hwndDeact);
895 }
896 // fall through
897
898 case WM_MOVE:
899 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
900 // scrollbars if necessary
901
902 // fall through
903
904 case WM_SIZE:
905 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
906 // things happen
907 MSWDefWindowProc(message, wParam, lParam);
908 break;
909
910 case WM_SYSCOMMAND:
911 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
912 // the message (the base class version does not)
913 return MSWDefWindowProc(message, wParam, lParam);
914
915 case WM_WINDOWPOSCHANGING:
916 processed = HandleWindowPosChanging((LPWINDOWPOS)lParam);
917 break;
918 }
919
920 if ( !processed )
921 rc = wxFrame::MSWWindowProc(message, wParam, lParam);
922
923 return rc;
924 }
925
926 bool wxMDIChildFrame::HandleCommand(WXWORD id, WXWORD cmd, WXHWND hwnd)
927 {
928 // In case it's e.g. a toolbar.
929 if ( hwnd )
930 {
931 wxWindow *win = wxFindWinFromHandle(hwnd);
932 if (win)
933 return win->MSWCommand(cmd, id);
934 }
935
936 if (wxCurrentPopupMenu)
937 {
938 wxMenu *popupMenu = wxCurrentPopupMenu;
939 wxCurrentPopupMenu = NULL;
940 if (popupMenu->MSWCommand(cmd, id))
941 return TRUE;
942 }
943
944 bool processed;
945 if (GetMenuBar() && GetMenuBar()->FindItem(id))
946 {
947 processed = ProcessCommand(id);
948 }
949 else
950 {
951 processed = FALSE;
952 }
953
954 return processed;
955 }
956
957 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate),
958 WXHWND hwndAct,
959 WXHWND hwndDeact)
960 {
961 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
962
963 HMENU menuToSet = 0;
964
965 bool activated;
966
967 if ( m_hWnd == hwndAct )
968 {
969 activated = TRUE;
970 parent->m_currentChild = this;
971
972 HMENU child_menu = (HMENU)GetWinMenu();
973 if ( child_menu )
974 {
975 parent->m_parentFrameActive = FALSE;
976
977 menuToSet = child_menu;
978 }
979 }
980 else if ( m_hWnd == hwndDeact )
981 {
982 wxASSERT_MSG( parent->m_currentChild == this,
983 wxT("can't deactivate MDI child which wasn't active!") );
984
985 activated = FALSE;
986 parent->m_currentChild = NULL;
987
988 HMENU parent_menu = (HMENU)parent->GetWinMenu();
989 if ( parent_menu )
990 {
991 parent->m_parentFrameActive = TRUE;
992
993 menuToSet = parent_menu;
994 }
995 }
996 else
997 {
998 // we have nothing to do with it
999 return FALSE;
1000 }
1001
1002 if ( menuToSet )
1003 {
1004 HMENU subMenu = (HMENU) 0;
1005 if (parent->GetWindowMenu())
1006 subMenu = (HMENU) parent->GetWindowMenu()->GetHMenu();
1007
1008 MDISetMenu(parent->GetClientWindow(), menuToSet, subMenu);
1009 }
1010
1011 wxActivateEvent event(wxEVT_ACTIVATE, activated, m_windowId);
1012 event.SetEventObject( this );
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 wxMDIParentFrame* pFrameWnd = (wxMDIParentFrame *)GetParent();
1035 if (pFrameWnd && pFrameWnd->GetToolBar() && pFrameWnd->GetToolBar()->IsShown())
1036 {
1037 pFrameWnd->GetToolBar()->Refresh();
1038 }
1039 }
1040 #endif // Win95
1041
1042 return FALSE;
1043 }
1044
1045 // ---------------------------------------------------------------------------
1046 // MDI specific message translation/preprocessing
1047 // ---------------------------------------------------------------------------
1048
1049 long wxMDIChildFrame::MSWDefWindowProc(WXUINT message, WXUINT wParam, WXLPARAM lParam)
1050 {
1051 return DefMDIChildProc(GetHwnd(),
1052 (UINT)message, (WPARAM)wParam, (LPARAM)lParam);
1053 }
1054
1055 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG* msg)
1056 {
1057 return wxFrame::MSWTranslateMessage(msg);
1058 }
1059
1060 // ---------------------------------------------------------------------------
1061 // misc
1062 // ---------------------------------------------------------------------------
1063
1064 void wxMDIChildFrame::MSWDestroyWindow()
1065 {
1066 MSWDetachWindowMenu();
1067 invalidHandle = GetHwnd();
1068
1069 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
1070
1071 // Must make sure this handle is invalidated (set to NULL) since all sorts
1072 // of things could happen after the child client is destroyed, but before
1073 // the wxFrame is destroyed.
1074
1075 HWND oldHandle = (HWND)GetHWND();
1076 SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIDESTROY,
1077 (WPARAM)oldHandle, 0);
1078
1079 if (parent->GetActiveChild() == (wxMDIChildFrame*) NULL)
1080 ResetWindowStyle((void*) NULL);
1081
1082 invalidHandle = 0;
1083
1084 if (m_hMenu)
1085 {
1086 ::DestroyMenu((HMENU) m_hMenu);
1087 m_hMenu = 0;
1088 }
1089 wxRemoveHandleAssociation(this);
1090 m_hWnd = 0;
1091 }
1092
1093 // Change the client window's extended style so we don't get a client edge
1094 // style when a child is maximised (a double border looks silly.)
1095 bool wxMDIChildFrame::ResetWindowStyle(void *vrect)
1096 {
1097 #if defined(__WIN95__)
1098 RECT *rect = (RECT *)vrect;
1099 wxMDIParentFrame* pFrameWnd = (wxMDIParentFrame *)GetParent();
1100 wxMDIChildFrame* pChild = pFrameWnd->GetActiveChild();
1101 if (!pChild || (pChild == this))
1102 {
1103 HWND hwndClient = GetWinHwnd(pFrameWnd->GetClientWindow());
1104 DWORD dwStyle = ::GetWindowLong(hwndClient, GWL_EXSTYLE);
1105
1106 // we want to test whether there is a maximized child, so just set
1107 // dwThisStyle to 0 if there is no child at all
1108 DWORD dwThisStyle = pChild
1109 ? ::GetWindowLong(GetWinHwnd(pChild), GWL_STYLE) : 0;
1110 DWORD dwNewStyle = dwStyle;
1111 if ( dwThisStyle & WS_MAXIMIZE )
1112 dwNewStyle &= ~(WS_EX_CLIENTEDGE);
1113 else
1114 dwNewStyle |= WS_EX_CLIENTEDGE;
1115
1116 if (dwStyle != dwNewStyle)
1117 {
1118 // force update of everything
1119 ::RedrawWindow(hwndClient, NULL, NULL,
1120 RDW_INVALIDATE | RDW_ALLCHILDREN);
1121 ::SetWindowLong(hwndClient, GWL_EXSTYLE, dwNewStyle);
1122 ::SetWindowPos(hwndClient, NULL, 0, 0, 0, 0,
1123 SWP_FRAMECHANGED | SWP_NOACTIVATE |
1124 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
1125 SWP_NOCOPYBITS);
1126 if (rect)
1127 ::GetClientRect(hwndClient, rect);
1128
1129 return TRUE;
1130 }
1131 }
1132 #endif // Win95
1133
1134 return FALSE;
1135 }
1136
1137 // ===========================================================================
1138 // wxMDIClientWindow: the window of predefined (by Windows) class which
1139 // contains the child frames
1140 // ===========================================================================
1141
1142 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style)
1143 {
1144 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE);
1145
1146 CLIENTCREATESTRUCT ccs;
1147 m_windowStyle = style;
1148 m_parent = parent;
1149
1150 ccs.hWindowMenu = (HMENU) 0;
1151 if (parent->GetWindowMenu())
1152 ccs.hWindowMenu = (HMENU) parent->GetWindowMenu()->GetHMenu();
1153 ccs.idFirstChild = wxFIRST_MDI_CHILD;
1154
1155 DWORD msStyle = /* MDIS_ALLCHILDSTYLES | */ WS_VISIBLE | WS_CHILD | WS_CLIPCHILDREN;
1156 if ( style & wxHSCROLL )
1157 msStyle |= WS_HSCROLL;
1158 if ( style & wxVSCROLL )
1159 msStyle |= WS_VSCROLL;
1160
1161 #if defined(__WIN95__)
1162 DWORD exStyle = WS_EX_CLIENTEDGE;
1163 #else
1164 DWORD exStyle = 0;
1165 #endif
1166
1167 wxWndHook = this;
1168 m_hWnd = (WXHWND)::CreateWindowEx
1169 (
1170 exStyle,
1171 wxT("MDICLIENT"),
1172 NULL,
1173 msStyle,
1174 0, 0, 0, 0,
1175 GetWinHwnd(parent),
1176 NULL,
1177 wxGetInstance(),
1178 (LPSTR)(LPCLIENTCREATESTRUCT)&ccs);
1179 if ( !m_hWnd )
1180 {
1181 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1182
1183 return FALSE;
1184 }
1185
1186 SubclassWin(m_hWnd);
1187 wxWndHook = NULL;
1188
1189 return TRUE;
1190 }
1191
1192 // Explicitly call default scroll behaviour
1193 void wxMDIClientWindow::OnScroll(wxScrollEvent& event)
1194 {
1195 // Note: for client windows, the scroll position is not set in
1196 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1197 // scroll position we're at.
1198 // This makes it hard to paint patterns or bitmaps in the background,
1199 // and have the client area scrollable as well.
1200
1201 if ( event.GetOrientation() == wxHORIZONTAL )
1202 m_scrollX = event.GetPosition(); // Always returns zero!
1203 else
1204 m_scrollY = event.GetPosition(); // Always returns zero!
1205
1206 event.Skip();
1207 }
1208
1209 void wxMDIClientWindow::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1210 {
1211 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1212 // client area, you can end up with a non-refreshed portion in the client window
1213 // (see OGL studio sample). So check if the position is changed and if so,
1214 // redraw the MDI child frames.
1215
1216 wxPoint oldPos = GetPosition();
1217
1218 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
1219
1220 wxPoint newPos = GetPosition();
1221
1222 if ((newPos.x != oldPos.x) || (newPos.y != oldPos.y))
1223 {
1224 if (GetParent())
1225 {
1226 wxNode* node = GetParent()->GetChildren().First();
1227 while (node)
1228 {
1229 wxWindow* child = (wxWindow*) node->Data();
1230 if (child->IsKindOf(CLASSINFO(wxMDIChildFrame)))
1231 {
1232 HWND hWnd = (HWND) child->GetHWND();
1233 ::RedrawWindow(hWnd, NULL, NULL, RDW_FRAME|RDW_ALLCHILDREN|RDW_INVALIDATE );
1234 }
1235 node = node->Next();
1236 }
1237 }
1238 }
1239 }
1240
1241 void wxMDIChildFrame::OnIdle(wxIdleEvent& event)
1242 {
1243 // MDI child frames get their WM_SIZE when they're constructed but at this
1244 // moment they don't have any children yet so all child windows will be
1245 // positioned incorrectly when they are added later - to fix this, we
1246 // generate an artificial size event here
1247 if ( m_needsResize )
1248 {
1249 m_needsResize = FALSE; // avoid any possibility of recursion
1250
1251 SendSizeEvent();
1252 }
1253
1254 event.Skip();
1255 }
1256
1257 // ---------------------------------------------------------------------------
1258 // non member functions
1259 // ---------------------------------------------------------------------------
1260
1261 static void MDISetMenu(wxWindow *win, HMENU hmenuFrame, HMENU hmenuWindow)
1262 {
1263 ::SendMessage(GetWinHwnd(win), WM_MDISETMENU,
1264 #ifdef __WIN32__
1265 (WPARAM)hmenuFrame, (LPARAM)hmenuWindow
1266 #else
1267 0, MAKELPARAM(hmenuFrame, hmenuWindow)
1268 #endif
1269 );
1270
1271 // update menu bar of the parent window
1272 wxWindow *parent = win->GetParent();
1273 wxCHECK_RET( parent, wxT("MDI client without parent frame? weird...") );
1274
1275 ::SendMessage(GetWinHwnd(win), WM_MDIREFRESHMENU, 0, 0L);
1276 ::DrawMenuBar(GetWinHwnd(parent));
1277 }
1278
1279 static void InsertWindowMenu(wxWindow *win, WXHMENU menu, HMENU subMenu)
1280 {
1281 // Try to insert Window menu in front of Help, otherwise append it.
1282 HMENU hmenu = (HMENU)menu;
1283
1284 if (subMenu)
1285 {
1286 int N = GetMenuItemCount(hmenu);
1287 bool success = FALSE;
1288 for ( int i = 0; i < N; i++ )
1289 {
1290 wxChar buf[256];
1291 int chars = GetMenuString(hmenu, i, buf, WXSIZEOF(buf), MF_BYPOSITION);
1292 if ( chars == 0 )
1293 {
1294 wxLogLastError(wxT("GetMenuString"));
1295
1296 continue;
1297 }
1298
1299 if ( wxStripMenuCodes(wxString(buf)).IsSameAs(_("Help")) )
1300 {
1301 success = TRUE;
1302 ::InsertMenu(hmenu, i, MF_BYPOSITION | MF_POPUP | MF_STRING,
1303 (UINT)subMenu, _("&Window"));
1304 break;
1305 }
1306 }
1307
1308 if ( !success )
1309 {
1310 ::AppendMenu(hmenu, MF_POPUP, (UINT)subMenu, _("&Window"));
1311 }
1312 }
1313
1314 MDISetMenu(win, hmenu, subMenu);
1315 }
1316
1317 static void RemoveWindowMenu(wxWindow *win, WXHMENU menu)
1318 {
1319 // Try to insert Window menu in front of Help, otherwise append it.
1320 HMENU hmenu = (HMENU)menu;
1321 int N = GetMenuItemCount(hmenu);
1322 for ( int i = 0; i < N; i++ )
1323 {
1324 wxChar buf[256];
1325 int chars = GetMenuString(hmenu, i, buf, WXSIZEOF(buf), MF_BYPOSITION);
1326 if ( chars == 0 )
1327 {
1328 wxLogLastError(wxT("GetMenuString"));
1329
1330 continue;
1331 }
1332
1333 if ( wxStripMenuCodes(wxString(buf)).IsSameAs(_("Window")) )
1334 {
1335 ::RemoveMenu(hmenu, i, MF_BYPOSITION);
1336 break;
1337 }
1338 }
1339
1340 // Does passing 0 for the window menu really work with WM_MDISETMENU?
1341 MDISetMenu(win, hmenu, 0);
1342 }
1343
1344 static void UnpackMDIActivate(WXWPARAM wParam, WXLPARAM lParam,
1345 WXWORD *activate, WXHWND *hwndAct, WXHWND *hwndDeact)
1346 {
1347 #ifdef __WIN32__
1348 *activate = TRUE;
1349 *hwndAct = (WXHWND)lParam;
1350 *hwndDeact = (WXHWND)wParam;
1351 #else // Win16
1352 *activate = (WXWORD)wParam;
1353 *hwndAct = (WXHWND)LOWORD(lParam);
1354 *hwndDeact = (WXHWND)HIWORD(lParam);
1355 #endif // Win32/Win16
1356 }