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