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