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