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