Lots of fixes for OS/2
[wxWidgets.git] / src / os2 / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: frame.cpp
3 // Purpose: wxFrame
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/27/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/defs.h"
17 #include "wx/object.h"
18 #include "wx/dynarray.h"
19 #include "wx/list.h"
20 #include "wx/hash.h"
21 #include "wx/string.h"
22 #include "wx/intl.h"
23 #include "wx/log.h"
24 #include "wx/event.h"
25 #include "wx/setup.h"
26 #include "wx/frame.h"
27 #include "wx/menu.h"
28 #include "wx/app.h"
29 #include "wx/utils.h"
30 #include "wx/dialog.h"
31 #include "wx/settings.h"
32 #include "wx/dcclient.h"
33 #include "wx/mdi.h"
34 #endif // WX_PRECOMP
35
36 #include "wx/os2/private.h"
37
38 #if wxUSE_STATUSBAR
39 #include "wx/statusbr.h"
40 #include "wx/generic/statusbr.h"
41 #endif // wxUSE_STATUSBAR
42
43 #if wxUSE_TOOLBAR
44 #include "wx/toolbar.h"
45 #endif // wxUSE_TOOLBAR
46
47 #include "wx/menuitem.h"
48 #include "wx/log.h"
49
50 // ----------------------------------------------------------------------------
51 // globals
52 // ----------------------------------------------------------------------------
53
54 extern wxWindowList wxModelessWindows;
55 extern wxList WXDLLEXPORT wxPendingDelete;
56
57 #if wxUSE_MENUS_NATIVE
58 extern wxMenu *wxCurrentPopupMenu;
59 #endif
60
61 extern void wxAssociateWinWithHandle( HWND hWnd
62 ,wxWindowOS2* pWin
63 );
64
65 // ----------------------------------------------------------------------------
66 // event tables
67 // ----------------------------------------------------------------------------
68
69 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
70 EVT_ACTIVATE(wxFrame::OnActivate)
71 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged)
72 END_EVENT_TABLE()
73
74 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
75
76 // ============================================================================
77 // implementation
78 // ============================================================================
79
80 // ----------------------------------------------------------------------------
81 // static class members
82 // ----------------------------------------------------------------------------
83 #if wxUSE_STATUSBAR
84
85 #if wxUSE_NATIVE_STATUSBAR
86 bool wxFrame::m_bUseNativeStatusBar = TRUE;
87 #else
88 bool wxFrame::m_bUseNativeStatusBar = FALSE;
89 #endif
90
91 #endif //wxUSE_STATUSBAR
92
93 // ----------------------------------------------------------------------------
94 // creation/destruction
95 // ----------------------------------------------------------------------------
96
97 void wxFrame::Init()
98 {
99 m_nFsStatusBarFields = 0;
100 m_nFsStatusBarHeight = 0;
101 m_nFsToolBarHeight = 0;
102 m_hWndToolTip = 0L;
103 m_bWasMinimized = FALSE;
104 m_pWinLastFocused = NULL;
105
106
107 m_frameMenuBar = NULL;
108 m_frameToolBar = NULL;
109 m_frameStatusBar = NULL;
110
111 m_hTitleBar = NULLHANDLE;
112 m_hHScroll = NULLHANDLE;
113 m_hVScroll = NULLHANDLE;
114
115 //
116 // Initialize SWP's
117 //
118 memset(&m_vSwpTitleBar, 0, sizeof(SWP));
119 memset(&m_vSwpMenuBar, 0, sizeof(SWP));
120 memset(&m_vSwpHScroll, 0, sizeof(SWP));
121 memset(&m_vSwpVScroll, 0, sizeof(SWP));
122 memset(&m_vSwpStatusBar, 0, sizeof(SWP));
123 memset(&m_vSwpToolBar, 0, sizeof(SWP));
124 m_bIconized = FALSE;
125
126 } // end of wxFrame::Init
127
128 bool wxFrame::Create(
129 wxWindow* pParent
130 , wxWindowID vId
131 , const wxString& rsTitle
132 , const wxPoint& rPos
133 , const wxSize& rSize
134 , long lStyle
135 , const wxString& rsName
136 )
137 {
138 if (!wxTopLevelWindow::Create( pParent
139 ,vId
140 ,rsTitle
141 ,rPos
142 ,rSize
143 ,lStyle
144 ,rsName
145 ))
146 return FALSE;
147 wxModelessWindows.Append(this);
148 return TRUE;
149 } // end of wxFrame::Create
150
151 wxFrame::~wxFrame()
152 {
153 m_isBeingDeleted = TRUE;
154 DeleteAllBars();
155 } // end of wxFrame::~wxFrame
156
157 //
158 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
159 //
160 void wxFrame::DoGetClientSize(
161 int* pX
162 , int* pY
163 ) const
164 {
165 wxTopLevelWindow::DoGetClientSize( pX
166 ,pY
167 );
168 //
169 // No need to use statusbar code as in WIN32 as the FORMATFRAME
170 // window procedure ensures PM knows about the new frame client
171 // size internally. A ::WinQueryWindowRect (that is called in
172 // wxWindow's GetClient size from above) is all that is needed!
173 //
174 } // end of wxFrame::DoGetClientSize
175
176 //
177 // Set the client size (i.e. leave the calculation of borders etc.
178 // to wxWindows)
179 //
180 void wxFrame::DoSetClientSize(
181 int nWidth
182 , int nHeight
183 )
184 {
185 wxStatusBar* pStatusBar = GetStatusBar();
186
187 //
188 // Statusbars are not part of the OS/2 Client but parent frame
189 // so no statusbar consideration
190 //
191 wxTopLevelWindow::DoSetClientSize( nWidth
192 ,nHeight
193 );
194 } // end of wxFrame::DoSetClientSize
195
196 // ----------------------------------------------------------------------------
197 // wxFrame: various geometry-related functions
198 // ----------------------------------------------------------------------------
199
200 void wxFrame::Raise()
201 {
202 wxFrameBase::Raise();
203 ::WinSetWindowPos( (HWND) GetParent()->GetHWND()
204 ,HWND_TOP
205 ,0
206 ,0
207 ,0
208 ,0
209 ,SWP_ZORDER
210 );
211 }
212
213 // generate an artificial resize event
214 void wxFrame::SendSizeEvent()
215 {
216 if (!m_bIconized)
217 {
218 RECTL vRect = wxGetWindowRect(GetHwnd());
219
220 (void)::WinPostMsg( m_hFrame
221 ,WM_SIZE
222 ,MPFROM2SHORT(vRect.xRight - vRect.xLeft, vRect.yTop - vRect.yBottom)
223 ,MPFROM2SHORT(vRect.xRight - vRect.xLeft, vRect.yTop - vRect.yBottom)
224 );
225 }
226 }
227
228 #if wxUSE_STATUSBAR
229 wxStatusBar* wxFrame::OnCreateStatusBar(
230 int nNumber
231 , long lulStyle
232 , wxWindowID vId
233 , const wxString& rName
234 )
235 {
236 wxStatusBar* pStatusBar = NULL;
237 SWP vSwp;
238 ERRORID vError;
239 wxString sError;
240
241 pStatusBar = wxFrameBase::OnCreateStatusBar( nNumber
242 ,lulStyle
243 ,vId
244 ,rName
245 );
246
247 if( !pStatusBar )
248 return NULL;
249
250 wxClientDC vDC(pStatusBar);
251 int nY;
252
253 //
254 // Set the height according to the font and the border size
255 //
256 vDC.SetFont(pStatusBar->GetFont()); // Screws up the menues for some reason
257 vDC.GetTextExtent( "X"
258 ,NULL
259 ,&nY
260 );
261
262 int nHeight = ((11 * nY) / 10 + 2 * pStatusBar->GetBorderY());
263
264 pStatusBar->SetSize( -1
265 ,-1
266 ,-1
267 ,nHeight
268 );
269
270 ::WinSetParent( pStatusBar->GetHWND()
271 ,m_hFrame
272 ,FALSE
273 );
274 ::WinSetOwner( pStatusBar->GetHWND()
275 ,m_hFrame
276 );
277 //
278 // to show statusbar
279 //
280 if(::WinIsWindowShowing(m_hFrame))
281 ::WinSendMsg(m_hFrame, WM_UPDATEFRAME, (MPARAM)~0, 0);
282
283 return pStatusBar;
284 } // end of wxFrame::OnCreateStatusBar
285
286 void wxFrame::PositionStatusBar()
287 {
288 SWP vSwp;
289 ERRORID vError;
290 wxString sError;
291
292 //
293 // Native status bar positions itself
294 //
295 if (m_frameStatusBar)
296 {
297 int nWidth;
298 int nY;
299 int nStatbarWidth;
300 int nStatbarHeight;
301 HWND hWndClient;
302 RECTL vRect;
303 RECTL vFRect;
304
305 ::WinQueryWindowRect(m_hFrame, &vRect);
306 nY = vRect.yTop;
307 ::WinMapWindowPoints(m_hFrame, HWND_DESKTOP, (PPOINTL)&vRect, 2);
308 vFRect = vRect;
309 ::WinCalcFrameRect(m_hFrame, &vRect, TRUE);
310 nWidth = vRect.xRight - vRect.xLeft;
311 nY = nY - (vRect.yBottom - vFRect.yBottom);
312
313 m_frameStatusBar->GetSize( &nStatbarWidth
314 ,&nStatbarHeight
315 );
316
317 nY= nY - nStatbarHeight;
318 //
319 // Since we wish the status bar to be directly under the client area,
320 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
321 //
322 m_frameStatusBar->SetSize( vRect.xLeft - vFRect.xLeft
323 ,nY
324 ,nWidth
325 ,nStatbarHeight
326 );
327 if (!::WinQueryWindowPos(m_frameStatusBar->GetHWND(), &vSwp))
328 {
329 vError = ::WinGetLastError(vHabmain);
330 sError = wxPMErrorToStr(vError);
331 wxLogError("Error setting parent for StautsBar. Error: %s\n", sError);
332 return;
333 }
334 }
335 } // end of wxFrame::PositionStatusBar
336 #endif // wxUSE_STATUSBAR
337
338 #if wxUSE_MENUS_NATIVE
339 void wxFrame::DetachMenuBar()
340 {
341 if (m_frameMenuBar)
342 {
343 m_frameMenuBar->Detach();
344 m_frameMenuBar = NULL;
345 }
346 } // end of wxFrame::DetachMenuBar
347
348 void wxFrame::SetMenuBar(
349 wxMenuBar* pMenuBar
350 )
351 {
352 ERRORID vError;
353 wxString sError;
354 HWND hTitlebar = NULLHANDLE;
355 HWND hHScroll = NULLHANDLE;
356 HWND hVScroll = NULLHANDLE;
357 HWND hMenuBar = NULLHANDLE;
358 SWP vSwp;
359 SWP vSwpTitlebar;
360 SWP vSwpVScroll;
361 SWP vSwpHScroll;
362 SWP vSwpMenu;
363
364 if (!pMenuBar)
365 {
366 DetachMenuBar();
367
368 //
369 // Actually remove the menu from the frame
370 //
371 m_hMenu = (WXHMENU)0;
372 InternalSetMenuBar();
373 }
374 else // set new non NULL menu bar
375 {
376 m_frameMenuBar = NULL;
377
378 //
379 // Can set a menubar several times.
380 // TODO: how to prevent a memory leak if you have a currently-unattached
381 // menubar? wxWindows assumes that the frame will delete the menu (otherwise
382 // there are problems for MDI).
383 //
384 if (pMenuBar->GetHMenu())
385 {
386 m_hMenu = pMenuBar->GetHMenu();
387 }
388 else
389 {
390 pMenuBar->Detach();
391 m_hMenu = pMenuBar->Create();
392 if (!m_hMenu)
393 return;
394 }
395 InternalSetMenuBar();
396 m_frameMenuBar = pMenuBar;
397 pMenuBar->Attach((wxFrame*)this);
398 }
399 } // end of wxFrame::SetMenuBar
400
401 void wxFrame::AttachMenuBar(
402 wxMenuBar* pMenubar
403 )
404 {
405 wxFrameBase::AttachMenuBar(pMenubar);
406
407 m_frameMenuBar = pMenubar;
408
409 if (!pMenubar)
410 {
411 //
412 // Actually remove the menu from the frame
413 //
414 m_hMenu = (WXHMENU)0;
415 InternalSetMenuBar();
416 }
417 else // Set new non NULL menu bar
418 {
419 //
420 // Can set a menubar several times.
421 //
422 if (pMenubar->GetHMenu())
423 {
424 m_hMenu = pMenubar->GetHMenu();
425 }
426 else
427 {
428 if (pMenubar->IsAttached())
429 pMenubar->Detach();
430
431 m_hMenu = pMenubar->Create();
432
433 if (!m_hMenu)
434 return;
435 }
436 InternalSetMenuBar();
437 }
438 } // end of wxFrame::AttachMenuBar
439
440 void wxFrame::InternalSetMenuBar()
441 {
442 ERRORID vError;
443 wxString sError;
444 //
445 // Set the parent and owner of the menubar to be the frame
446 //
447 if (!::WinSetParent(m_hMenu, m_hFrame, FALSE))
448 {
449 vError = ::WinGetLastError(vHabmain);
450 sError = wxPMErrorToStr(vError);
451 wxLogError("Error setting parent for submenu. Error: %s\n", sError);
452 }
453
454 if (!::WinSetOwner(m_hMenu, m_hFrame))
455 {
456 vError = ::WinGetLastError(vHabmain);
457 sError = wxPMErrorToStr(vError);
458 wxLogError("Error setting parent for submenu. Error: %s\n", sError);
459 }
460 ::WinSendMsg(m_hFrame, WM_UPDATEFRAME, (MPARAM)FCF_MENU, (MPARAM)0);
461 } // end of wxFrame::InternalSetMenuBar
462 #endif // wxUSE_MENUS_NATIVE
463
464 //
465 // Responds to colour changes, and passes event on to children
466 //
467 void wxFrame::OnSysColourChanged(
468 wxSysColourChangedEvent& rEvent
469 )
470 {
471 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
472 Refresh();
473
474 #if wxUSE_STATUSBAR
475 if (m_frameStatusBar)
476 {
477 wxSysColourChangedEvent vEvent2;
478
479 vEvent2.SetEventObject(m_frameStatusBar);
480 m_frameStatusBar->GetEventHandler()->ProcessEvent(vEvent2);
481 }
482 #endif //wxUSE_STATUSBAR
483
484 //
485 // Propagate the event to the non-top-level children
486 //
487 wxWindow::OnSysColourChanged(rEvent);
488 } // end of wxFrame::OnSysColourChanged
489
490 // Pass TRUE to show full screen, FALSE to restore.
491 bool wxFrame::ShowFullScreen(
492 bool bShow
493 , long lStyle
494 )
495 {
496 if (bShow)
497 {
498 if (IsFullScreen())
499 return FALSE;
500
501 m_bFsIsShowing = TRUE;
502 m_lFsStyle = lStyle;
503
504 #if wxUSE_TOOLBAR
505 wxToolBar* pTheToolBar = GetToolBar();
506 #endif //wxUSE_TOOLBAR
507
508 #if wxUSE_STATUSBAR
509 wxStatusBar* pTheStatusBar = GetStatusBar();
510 #endif //wxUSE_STATUSBAR
511
512 int nDummyWidth;
513
514 #if wxUSE_TOOLBAR
515 if (pTheToolBar)
516 pTheToolBar->GetSize(&nDummyWidth, &m_nFsToolBarHeight);
517 #endif //wxUSE_TOOLBAR
518
519 #if wxUSE_STATUSBAR
520 if (pTheStatusBar)
521 pTheStatusBar->GetSize(&nDummyWidth, &m_nFsStatusBarHeight);
522 #endif //wxUSE_STATUSBAR
523
524 #if wxUSE_TOOLBAR
525 //
526 // Zap the toolbar, menubar, and statusbar
527 //
528 if ((lStyle & wxFULLSCREEN_NOTOOLBAR) && pTheToolBar)
529 {
530 pTheToolBar->SetSize(-1,0);
531 pTheToolBar->Show(FALSE);
532 }
533 #endif //wxUSE_TOOLBAR
534
535 if (lStyle & wxFULLSCREEN_NOMENUBAR)
536 {
537 ::WinSetParent(m_hMenu, m_hFrame, FALSE);
538 ::WinSetOwner(m_hMenu, m_hFrame);
539 ::WinSendMsg((HWND)m_hFrame, WM_UPDATEFRAME, (MPARAM)FCF_MENU, (MPARAM)0);
540 }
541
542 #if wxUSE_STATUSBAR
543 //
544 // Save the number of fields in the statusbar
545 //
546 if ((lStyle & wxFULLSCREEN_NOSTATUSBAR) && pTheStatusBar)
547 {
548 m_nFsStatusBarFields = pTheStatusBar->GetFieldsCount();
549 SetStatusBar((wxStatusBar*) NULL);
550 delete pTheStatusBar;
551 }
552 else
553 m_nFsStatusBarFields = 0;
554 #endif //wxUSE_STATUSBAR
555
556 //
557 // Zap the frame borders
558 //
559
560 //
561 // Save the 'normal' window style
562 //
563 m_lFsOldWindowStyle = ::WinQueryWindowULong(m_hFrame, QWL_STYLE);
564
565 //
566 // Save the old position, width & height, maximize state
567 //
568 m_vFsOldSize = GetRect();
569 m_bFsIsMaximized = IsMaximized();
570
571 //
572 // Decide which window style flags to turn off
573 //
574 LONG lNewStyle = m_lFsOldWindowStyle;
575 LONG lOffFlags = 0;
576
577 if (lStyle & wxFULLSCREEN_NOBORDER)
578 lOffFlags |= FCF_BORDER;
579 if (lStyle & wxFULLSCREEN_NOCAPTION)
580 lOffFlags |= (FCF_TASKLIST | FCF_SYSMENU);
581
582 lNewStyle &= (~lOffFlags);
583
584 //
585 // Change our window style to be compatible with full-screen mode
586 //
587 ::WinSetWindowULong((HWND)m_hFrame, QWL_STYLE, (ULONG)lNewStyle);
588
589 //
590 // Resize to the size of the desktop
591 int nWidth;
592 int nHeight;
593
594 RECTL vRect;
595
596 ::WinQueryWindowRect(HWND_DESKTOP, &vRect);
597 nWidth = vRect.xRight - vRect.xLeft;
598 //
599 // Rmember OS/2 is backwards!
600 //
601 nHeight = vRect.yTop - vRect.yBottom;
602
603 SetSize( nWidth
604 ,nHeight
605 );
606
607 //
608 // Now flush the window style cache and actually go full-screen
609 //
610 ::WinSetWindowPos( (HWND) GetParent()->GetHWND()
611 ,HWND_TOP
612 ,0
613 ,0
614 ,nWidth
615 ,nHeight
616 ,SWP_SIZE | SWP_SHOW
617 );
618
619 wxSizeEvent vEvent( wxSize( nWidth
620 ,nHeight
621 )
622 ,GetId()
623 );
624
625 GetEventHandler()->ProcessEvent(vEvent);
626 return TRUE;
627 }
628 else
629 {
630 if (!IsFullScreen())
631 return FALSE;
632
633 m_bFsIsShowing = FALSE;
634
635 #if wxUSE_TOOLBAR
636 wxToolBar* pTheToolBar = GetToolBar();
637
638 //
639 // Restore the toolbar, menubar, and statusbar
640 //
641 if (pTheToolBar && (m_lFsStyle & wxFULLSCREEN_NOTOOLBAR))
642 {
643 pTheToolBar->SetSize(-1, m_nFsToolBarHeight);
644 pTheToolBar->Show(TRUE);
645 }
646 #endif //wxUSE_TOOLBAR
647
648 #if wxUSE_STATUSBAR
649 if ((m_lFsStyle & wxFULLSCREEN_NOSTATUSBAR) && (m_nFsStatusBarFields > 0))
650 {
651 CreateStatusBar(m_nFsStatusBarFields);
652 // PositionStatusBar();
653 }
654 #endif //wxUSE_STATUSBAR
655
656 if ((m_lFsStyle & wxFULLSCREEN_NOMENUBAR) && (m_hMenu != 0))
657 {
658 ::WinSetParent(m_hMenu, m_hFrame, FALSE);
659 ::WinSetOwner(m_hMenu, m_hFrame);
660 ::WinSendMsg(m_hFrame, WM_UPDATEFRAME, (MPARAM)FCF_MENU, (MPARAM)0);
661 }
662 Maximize(m_bFsIsMaximized);
663
664 ::WinSetWindowULong( m_hFrame
665 ,QWL_STYLE
666 ,(ULONG)m_lFsOldWindowStyle
667 );
668 ::WinSetWindowPos( (HWND) GetParent()->GetHWND()
669 ,HWND_TOP
670 ,m_vFsOldSize.x
671 ,m_vFsOldSize.y
672 ,m_vFsOldSize.width
673 ,m_vFsOldSize.height
674 ,SWP_SIZE | SWP_SHOW
675 );
676 return TRUE;
677 }
678 } // end of wxFrame::ShowFullScreen
679
680 //
681 // Frame window
682 //
683 //
684 // Default activation behaviour - set the focus for the first child
685 // subwindow found.
686 //
687 void wxFrame::OnActivate(
688 wxActivateEvent& rEvent
689 )
690 {
691 if ( rEvent.GetActive() )
692 {
693 // restore focus to the child which was last focused
694 wxLogTrace(_T("focus"), _T("wxFrame %08x activated."), m_hWnd);
695
696 wxWindow* pParent = m_pWinLastFocused ? m_pWinLastFocused->GetParent()
697 : NULL;
698 if (!pParent)
699 {
700 pParent = this;
701 }
702
703 wxSetFocusToChild( pParent
704 ,&m_pWinLastFocused
705 );
706 }
707 else // deactivating
708 {
709 //
710 // Remember the last focused child if it is our child
711 //
712 m_pWinLastFocused = FindFocus();
713
714 for (wxWindowList::Node* pNode = GetChildren().GetFirst();
715 pNode;
716 pNode = pNode->GetNext())
717 {
718 // FIXME all this is totally bogus - we need to do the same as wxPanel,
719 // but how to do it without duplicating the code?
720
721 // restore focus
722 wxWindow* pChild = pNode->GetData();
723
724 if (!pChild->IsTopLevel()
725 #if wxUSE_TOOLBAR
726 && !wxDynamicCast(pChild, wxToolBar)
727 #endif // wxUSE_TOOLBAR
728 #if wxUSE_STATUSBAR
729 && !wxDynamicCast(pChild, wxStatusBar)
730 #endif // wxUSE_STATUSBAR
731 )
732 {
733 pChild->SetFocus();
734 return;
735 }
736 }
737 }
738 } // end of wxFrame::OnActivate
739
740 // ----------------------------------------------------------------------------
741 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
742 // from the client area, so the client area is what's really available for the
743 // frame contents
744 // ----------------------------------------------------------------------------
745
746 // Checks if there is a toolbar, and returns the first free client position
747 wxPoint wxFrame::GetClientAreaOrigin() const
748 {
749 wxPoint vPoint(0, 0);
750
751 #if wxUSE_TOOLBAR
752 if (GetToolBar())
753 {
754 int nWidth;
755 int nHeight;
756
757 GetToolBar()->GetSize( &nWidth
758 ,&nHeight
759 );
760
761 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
762 {
763 vPoint.x += nWidth;
764 }
765 else
766 {
767 // PM is backwards from windows
768 vPoint.y += nHeight;
769 }
770 }
771 #endif //wxUSE_TOOLBAR
772 return vPoint;
773 } // end of wxFrame::GetClientAreaOrigin
774
775 // ----------------------------------------------------------------------------
776 // tool/status bar stuff
777 // ----------------------------------------------------------------------------
778
779 #if wxUSE_TOOLBAR
780
781 wxToolBar* wxFrame::CreateToolBar(
782 long lStyle
783 , wxWindowID vId
784 , const wxString& rName
785 )
786 {
787 if (wxFrameBase::CreateToolBar( lStyle
788 ,vId
789 ,rName
790 ))
791 {
792 PositionToolBar();
793 }
794 return m_frameToolBar;
795 } // end of wxFrame::CreateToolBar
796
797 void wxFrame::PositionToolBar()
798 {
799 HWND hWndClient;
800 RECTL vRect;
801
802 ::WinQueryWindowRect(GetHwnd(), &vRect);
803
804 #if wxUSE_STATUSBAR
805 if (GetStatusBar())
806 {
807 int nStatusX;
808 int nStatusY;
809
810 GetStatusBar()->GetClientSize( &nStatusX
811 ,&nStatusY
812 );
813 // PM is backwards from windows
814 vRect.yBottom += nStatusY;
815 }
816 #endif // wxUSE_STATUSBAR
817
818 if ( m_frameToolBar )
819 {
820 int nToolbarWidth;
821 int nToolbarHeight;
822
823 m_frameToolBar->GetSize( &nToolbarWidth
824 ,&nToolbarHeight
825 );
826
827 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
828 {
829 nToolbarHeight = vRect.yBottom;
830 }
831 else
832 {
833 nToolbarWidth = vRect.xRight;
834 }
835
836 //
837 // Use the 'real' PM position here
838 //
839 GetToolBar()->SetSize( 0
840 ,0
841 ,nToolbarWidth
842 ,nToolbarHeight
843 ,wxSIZE_NO_ADJUSTMENTS
844 );
845 }
846 } // end of wxFrame::PositionToolBar
847 #endif // wxUSE_TOOLBAR
848
849 // ----------------------------------------------------------------------------
850 // frame state (iconized/maximized/...)
851 // ----------------------------------------------------------------------------
852
853 //
854 // propagate our state change to all child frames: this allows us to emulate X
855 // Windows behaviour where child frames float independently of the parent one
856 // on the desktop, but are iconized/restored with it
857 //
858 void wxFrame::IconizeChildFrames(
859 bool bIconize
860 )
861 {
862 for (wxWindowList::Node* pNode = GetChildren().GetFirst();
863 pNode;
864 pNode = pNode->GetNext() )
865 {
866 wxWindow* pWin = pNode->GetData();
867 wxFrame* pFrame = wxDynamicCast(pWin, wxFrame);
868
869 if ( pFrame
870 #if wxUSE_MDI_ARCHITECTURE
871 && !wxDynamicCast(pFrame, wxMDIChildFrame)
872 #endif // wxUSE_MDI_ARCHITECTURE
873 )
874 {
875 //
876 // We don't want to restore the child frames which had been
877 // iconized even before we were iconized, so save the child frame
878 // status when iconizing the parent frame and check it when
879 // restoring it.
880 //
881 if (bIconize)
882 {
883 pFrame->m_bWasMinimized = pFrame->IsIconized();
884 }
885
886 //
887 // This test works for both iconizing and restoring
888 //
889 if (!pFrame->m_bWasMinimized)
890 pFrame->Iconize(bIconize);
891 }
892 }
893 } // end of wxFrame::IconizeChildFrames
894
895 WXHICON wxFrame::GetDefaultIcon() const
896 {
897 return (WXHICON)(wxSTD_FRAME_ICON ? wxSTD_FRAME_ICON
898 : wxDEFAULT_FRAME_ICON);
899 }
900 // ===========================================================================
901 // message processing
902 // ===========================================================================
903
904 // ---------------------------------------------------------------------------
905 // preprocessing
906 // ---------------------------------------------------------------------------
907 bool wxFrame::OS2TranslateMessage(
908 WXMSG* pMsg
909 )
910 {
911 //
912 // try the menu bar accels
913 //
914 wxMenuBar* pMenuBar = GetMenuBar();
915
916 if (!pMenuBar)
917 return FALSE;
918
919 #if wxUSE_ACCEL && wxUSE_MENUS_NATIVE
920 const wxAcceleratorTable& rAcceleratorTable = pMenuBar->GetAccelTable();
921 return rAcceleratorTable.Translate(GetHWND(), pMsg);
922 #else
923 return FALSE;
924 #endif //wxUSE_ACCEL
925 } // end of wxFrame::OS2TranslateMessage
926
927 // ---------------------------------------------------------------------------
928 // our private (non virtual) message handlers
929 // ---------------------------------------------------------------------------
930 bool wxFrame::HandlePaint()
931 {
932 RECTL vRect;
933
934 if (::WinQueryUpdateRect(GetHWND(), &vRect))
935 {
936 if (m_bIconized)
937 {
938 //
939 // Icons in PM are the same as "pointers"
940 //
941 HPOINTER hIcon;
942
943 if (m_icon.Ok())
944 hIcon = (HPOINTER)::WinSendMsg(m_hFrame, WM_QUERYICON, 0L, 0L);
945 else
946 hIcon = (HPOINTER)m_hDefaultIcon;
947
948 //
949 // Hold a pointer to the dc so long as the OnPaint() message
950 // is being processed
951 //
952 RECTL vRect2;
953 HPS hPs = ::WinBeginPaint(GetHwnd(), NULLHANDLE, &vRect2);
954
955 //
956 // Erase background before painting or we get white background
957 //
958 OS2DefWindowProc(WM_ERASEBACKGROUND, (MPARAM)hPs, (MPARAM)&vRect2);
959
960 if (hIcon)
961 {
962 HWND hWndClient;
963 RECTL vRect3;
964
965 ::WinQueryWindowRect(GetHwnd(), &vRect3);
966
967 static const int nIconWidth = 32;
968 static const int nIconHeight = 32;
969 int nIconX = (int)((vRect3.xRight - nIconWidth)/2);
970 int nIconY = (int)((vRect3.yBottom + nIconHeight)/2);
971
972 ::WinDrawPointer(hPs, nIconX, nIconY, hIcon, DP_NORMAL);
973 }
974 ::WinEndPaint(hPs);
975 return TRUE;
976 }
977 else
978 {
979 if (!wxWindow::HandlePaint())
980 {
981 HPS hPS;
982 RECTL vRect;
983
984 hPS = ::WinBeginPaint( GetHwnd()
985 ,NULLHANDLE
986 ,&vRect
987 );
988 if(hPS)
989 {
990 ::GpiCreateLogColorTable( hPS
991 ,0L
992 ,LCOLF_CONSECRGB
993 ,0L
994 ,(LONG)wxTheColourDatabase->m_nSize
995 ,(PLONG)wxTheColourDatabase->m_palTable
996 );
997 ::GpiCreateLogColorTable( hPS
998 ,0L
999 ,LCOLF_RGB
1000 ,0L
1001 ,0L
1002 ,NULL
1003 );
1004
1005 ::WinFillRect( hPS
1006 ,&vRect
1007 ,GetBackgroundColour().GetPixel()
1008 );
1009 ::WinEndPaint(hPS);
1010 }
1011 }
1012 return TRUE;
1013 }
1014 }
1015 else
1016 {
1017 // nothing to paint - processed
1018 return TRUE;
1019 }
1020 return FALSE;
1021 } // end of wxFrame::HandlePaint
1022
1023 bool wxFrame::HandleSize(
1024 int nX
1025 , int nY
1026 , WXUINT nId
1027 )
1028 {
1029 bool bProcessed = FALSE;
1030
1031 switch (nId)
1032 {
1033 case kSizeNormal:
1034 //
1035 // Only do it it if we were iconized before, otherwise resizing the
1036 // parent frame has a curious side effect of bringing it under it's
1037 // children
1038 if (!m_bIconized )
1039 break;
1040
1041 //
1042 // restore all child frames too
1043 //
1044 IconizeChildFrames(FALSE);
1045 (void)SendIconizeEvent(FALSE);
1046
1047 //
1048 // fall through
1049 //
1050
1051 case kSizeMax:
1052 m_bIconized = FALSE;
1053 break;
1054
1055 case kSizeMin:
1056 //
1057 // Iconize all child frames too
1058 //
1059 IconizeChildFrames(TRUE);
1060 (void)SendIconizeEvent();
1061 m_bIconized = TRUE;
1062 break;
1063 }
1064
1065 if (!m_bIconized)
1066 {
1067 //
1068 // forward WM_SIZE to status bar control
1069 //
1070 #if wxUSE_NATIVE_STATUSBAR
1071 if (m_frameStatusBar && m_frameStatusBar->IsKindOf(CLASSINFO(wxStatusBar95)))
1072 {
1073 wxSizeEvent vEvent( wxSize( nX
1074 ,nY
1075 )
1076 ,m_frameStatusBar->GetId()
1077 );
1078
1079 vEvent.SetEventObject(m_frameStatusBar);
1080 m_frameStatusBar->OnSize(vEvent);
1081 }
1082 #endif // wxUSE_NATIVE_STATUSBAR
1083
1084 PositionStatusBar();
1085 #if wxUSE_TOOLBAR
1086 PositionToolBar();
1087 #endif // wxUSE_TOOLBAR
1088
1089 wxSizeEvent vEvent( wxSize( nX
1090 ,nY
1091 )
1092 ,m_windowId
1093 );
1094
1095 vEvent.SetEventObject(this);
1096 bProcessed = GetEventHandler()->ProcessEvent(vEvent);
1097 AlterChildPos();
1098 }
1099 return bProcessed;
1100 } // end of wxFrame::HandleSize
1101
1102 bool wxFrame::HandleCommand(
1103 WXWORD nId
1104 , WXWORD nCmd
1105 , WXHWND hControl
1106 )
1107 {
1108 if (hControl)
1109 {
1110 //
1111 // In case it's e.g. a toolbar.
1112 //
1113 wxWindow* pWin = wxFindWinFromHandle(hControl);
1114
1115 if (pWin)
1116 return pWin->OS2Command( nCmd
1117 ,nId
1118 );
1119 }
1120
1121 //
1122 // Handle here commands from menus and accelerators
1123 //
1124 if (nCmd == CMDSRC_MENU || nCmd == CMDSRC_ACCELERATOR)
1125 {
1126 #if wxUSE_MENUS_NATIVE
1127 if (wxCurrentPopupMenu)
1128 {
1129 wxMenu* pPopupMenu = wxCurrentPopupMenu;
1130
1131 wxCurrentPopupMenu = NULL;
1132
1133 return pPopupMenu->OS2Command( nCmd
1134 ,nId
1135 );
1136 return TRUE;
1137 }
1138 #endif
1139
1140 if (ProcessCommand(nId))
1141 {
1142 return TRUE;
1143 }
1144 }
1145 return FALSE;
1146 } // end of wxFrame::HandleCommand
1147
1148 bool wxFrame::HandleMenuSelect(
1149 WXWORD nItem
1150 , WXWORD nFlags
1151 , WXHMENU hMenu
1152 )
1153 {
1154 if( !nFlags )
1155 {
1156 MENUITEM mItem;
1157 MRESULT rc;
1158
1159 rc = ::WinSendMsg(hMenu, MM_QUERYITEM, MPFROM2SHORT(nItem, TRUE), (MPARAM)&mItem);
1160
1161 if(rc && !(mItem.afStyle & (MIS_SUBMENU | MIS_SEPARATOR)))
1162 {
1163 wxMenuEvent vEvent(wxEVT_MENU_HIGHLIGHT, nItem);
1164
1165 vEvent.SetEventObject(this);
1166 GetEventHandler()->ProcessEvent(vEvent); // return value would be ignored by PM
1167 }
1168 }
1169 return TRUE;
1170 } // end of wxFrame::HandleMenuSelect
1171
1172 // ---------------------------------------------------------------------------
1173 // Main Frame window proc
1174 // ---------------------------------------------------------------------------
1175 MRESULT EXPENTRY wxFrameMainWndProc(
1176 HWND hWnd
1177 , ULONG ulMsg
1178 , MPARAM wParam
1179 , MPARAM lParam
1180 )
1181 {
1182 MRESULT rc = (MRESULT)0;
1183 bool bProcessed = FALSE;
1184 wxFrame* pWnd = NULL;
1185
1186 pWnd = (wxFrame*) wxFindWinFromHandle((WXHWND) hWnd);
1187 switch (ulMsg)
1188 {
1189 case WM_QUERYFRAMECTLCOUNT:
1190 if(pWnd && pWnd->m_fnOldWndProc)
1191 {
1192 USHORT uItemCount = SHORT1FROMMR(pWnd->m_fnOldWndProc(hWnd, ulMsg, wParam, lParam));
1193
1194 rc = MRFROMSHORT(uItemCount);
1195 }
1196 break;
1197
1198 case WM_FORMATFRAME:
1199 /////////////////////////////////////////////////////////////////////////////////
1200 // Applications that subclass frame controls may find that the frame is already
1201 // subclassed the number of frame controls is variable.
1202 // The WM_FORMATFRAME and WM_QUERYFRAMECTLCOUNT messages must always be
1203 // subclassed by calling the previous window procedure and modifying its result.
1204 ////////////////////////////////////////////////////////////////////////////////
1205 {
1206 int nItemCount;
1207 int i;
1208 PSWP pSWP = NULL;
1209 SWP vSwpStb;
1210 RECTL vRectl;
1211 RECTL vRstb;
1212 int nHeight=0;
1213
1214 pSWP = (PSWP)PVOIDFROMMP(wParam);
1215 nItemCount = SHORT1FROMMR(pWnd->m_fnOldWndProc(hWnd, ulMsg, wParam, lParam));
1216 if(pWnd->m_frameStatusBar)
1217 {
1218 ::WinQueryWindowRect(pWnd->m_frameStatusBar->GetHWND(), &vRstb);
1219 pWnd->m_frameStatusBar->GetSize(NULL, &nHeight);
1220 ::WinQueryWindowRect(pWnd->m_hFrame, &vRectl);
1221 ::WinMapWindowPoints(pWnd->m_hFrame, HWND_DESKTOP, (PPOINTL)&vRectl, 2);
1222 vRstb = vRectl;
1223 ::WinCalcFrameRect(pWnd->m_hFrame, &vRectl, TRUE);
1224
1225 vSwpStb.x = vRectl.xLeft - vRstb.xLeft;
1226 vSwpStb.y = vRectl.yBottom - vRstb.yBottom;
1227 vSwpStb.cx = vRectl.xRight - vRectl.xLeft - 1; //?? -1 ??
1228 vSwpStb.cy = nHeight;
1229 vSwpStb.fl = SWP_SIZE |SWP_MOVE | SWP_SHOW;
1230 vSwpStb.hwnd = pWnd->m_frameStatusBar->GetHWND();
1231 vSwpStb.hwndInsertBehind = HWND_TOP;
1232 }
1233 ::WinQueryWindowRect(pWnd->m_hFrame, &vRectl);
1234 ::WinMapWindowPoints(pWnd->m_hFrame, HWND_DESKTOP, (PPOINTL)&vRectl, 2);
1235 ::WinCalcFrameRect(pWnd->m_hFrame, &vRectl, TRUE);
1236 ::WinMapWindowPoints(HWND_DESKTOP, pWnd->m_hFrame, (PPOINTL)&vRectl, 2);
1237 for(i = 0; i < nItemCount; i++)
1238 {
1239 if(pWnd->m_hWnd && pSWP[i].hwnd == pWnd->m_hWnd)
1240 {
1241 pSWP[i].x = vRectl.xLeft;
1242 pSWP[i].y = vRectl.yBottom + nHeight;
1243 pSWP[i].cx = vRectl.xRight - vRectl.xLeft;
1244 pSWP[i].cy = vRectl.yTop - vRectl.yBottom - nHeight;
1245 pSWP[i].fl = SWP_SIZE | SWP_MOVE | SWP_SHOW;
1246 pSWP[i].hwndInsertBehind = HWND_TOP;
1247 }
1248 }
1249 bProcessed = TRUE;
1250 rc = MRFROMSHORT(nItemCount);
1251 }
1252 break;
1253
1254 default:
1255 if(pWnd && pWnd->m_fnOldWndProc)
1256 rc = pWnd->m_fnOldWndProc(hWnd, ulMsg, wParam, lParam);
1257 else
1258 rc = ::WinDefWindowProc(hWnd, ulMsg, wParam, lParam);
1259 }
1260 return rc;
1261 } // end of wxFrameMainWndProc
1262
1263 MRESULT EXPENTRY wxFrameWndProc(
1264 HWND hWnd
1265 , ULONG ulMsg
1266 , MPARAM wParam
1267 , MPARAM lParam
1268 )
1269 {
1270 //
1271 // Trace all ulMsgs - useful for the debugging
1272 //
1273 HWND parentHwnd;
1274 wxFrame* pWnd = NULL;
1275
1276 parentHwnd = WinQueryWindow(hWnd,QW_PARENT);
1277 pWnd = (wxFrame*) wxFindWinFromHandle((WXHWND) hWnd);
1278
1279 //
1280 // When we get the first message for the HWND we just created, we associate
1281 // it with wxWindow stored in wxWndHook
1282 //
1283
1284 MRESULT rc = (MRESULT)0;
1285 bool bProcessed = FALSE;
1286
1287 //
1288 // Stop right here if we don't have a valid handle in our wxWindow object.
1289 //
1290 if (pWnd && !pWnd->GetHWND())
1291 {
1292 pWnd->SetHWND((WXHWND) hWnd);
1293 rc = pWnd->OS2DefWindowProc(ulMsg, wParam, lParam );
1294 pWnd->SetHWND(0);
1295 }
1296 else
1297 {
1298 if (pWnd)
1299 rc = pWnd->OS2WindowProc(ulMsg, wParam, lParam);
1300 else
1301 rc = ::WinDefWindowProc(hWnd, ulMsg, wParam, lParam);
1302 }
1303 return rc;
1304 } // end of wxFrameWndProc
1305
1306 MRESULT wxFrame::OS2WindowProc(
1307 WXUINT uMessage
1308 , WXWPARAM wParam
1309 , WXLPARAM lParam
1310 )
1311 {
1312 MRESULT mRc = 0L;
1313 bool bProcessed = FALSE;
1314
1315 switch (uMessage)
1316 {
1317 case WM_CLOSE:
1318 //
1319 // If we can't close, tell the system that we processed the
1320 // message - otherwise it would close us
1321 //
1322 bProcessed = !Close();
1323 break;
1324
1325 case WM_PAINT:
1326 bProcessed = HandlePaint();
1327 mRc = (MRESULT)FALSE;
1328 break;
1329
1330 case WM_ERASEBACKGROUND:
1331 //
1332 // Returning TRUE to requests PM to paint the window background
1333 // in SYSCLR_WINDOW. We capture this here because the PS returned
1334 // in Frames is the PS for the whole frame, which we can't really
1335 // use at all. If you want to paint a different background, do it
1336 // in an OnPaint using a wxPaintDC.
1337 //
1338 mRc = (MRESULT)(TRUE);
1339 break;
1340
1341 case WM_COMMAND:
1342 {
1343 WORD wId;
1344 WORD wCmd;
1345 WXHWND hWnd;
1346
1347 UnpackCommand( (WXWPARAM)wParam
1348 ,(WXLPARAM)lParam
1349 ,&wId
1350 ,&hWnd
1351 ,&wCmd
1352 );
1353
1354 bProcessed = HandleCommand( wId
1355 ,wCmd
1356 ,(WXHWND)hWnd
1357 );
1358 }
1359 break;
1360
1361 case WM_MENUSELECT:
1362 {
1363 WXWORD wItem;
1364 WXWORD wFlags;
1365 WXHMENU hMenu;
1366
1367 UnpackMenuSelect( wParam
1368 ,lParam
1369 ,&wItem
1370 ,&wFlags
1371 ,&hMenu
1372 );
1373 bProcessed = HandleMenuSelect( wItem
1374 ,wFlags
1375 ,hMenu
1376 );
1377 mRc = (MRESULT)TRUE;
1378 }
1379 break;
1380
1381 case WM_SIZE:
1382 {
1383 SHORT nScxold = SHORT1FROMMP(wParam); // Old horizontal size.
1384 SHORT nScyold = SHORT2FROMMP(wParam); // Old vertical size.
1385 SHORT nScxnew = SHORT1FROMMP(lParam); // New horizontal size.
1386 SHORT nScynew = SHORT2FROMMP(lParam); // New vertical size.
1387
1388 lParam = MRFROM2SHORT( nScxnew - 20
1389 ,nScynew - 30
1390 );
1391 }
1392 bProcessed = HandleSize(LOWORD(lParam), HIWORD(lParam), (WXUINT)wParam);
1393 mRc = (MRESULT)FALSE;
1394 break;
1395
1396 case CM_QUERYDRAGIMAGE:
1397 {
1398 HPOINTER hIcon;
1399
1400 if (m_icon.Ok())
1401 hIcon = (HPOINTER)::WinSendMsg(GetHWND(), WM_QUERYICON, 0L, 0L);
1402 else
1403 hIcon = (HPOINTER)m_hDefaultIcon;
1404 mRc = (MRESULT)hIcon;
1405 bProcessed = mRc != 0;
1406 }
1407 break;
1408 }
1409
1410 if (!bProcessed )
1411 mRc = wxWindow::OS2WindowProc( uMessage
1412 ,wParam
1413 ,lParam
1414 );
1415 return (MRESULT)mRc;
1416 } // wxFrame::OS2WindowProc
1417
1418 void wxFrame::SetClient(WXHWND c_Hwnd)
1419 {
1420 // Duh...nothing to do under OS/2
1421 }
1422
1423 void wxFrame::SetClient(
1424 wxWindow* pWindow
1425 )
1426 {
1427 wxWindow* pOldClient = this->GetClient();
1428 bool bClientHasFocus = pOldClient && (pOldClient == wxWindow::FindFocus());
1429
1430 if(pOldClient == pWindow) // nothing to do
1431 return;
1432 if(pWindow == NULL) // just need to remove old client
1433 {
1434 if(pOldClient == NULL) // nothing to do
1435 return;
1436
1437 if(bClientHasFocus )
1438 this->SetFocus();
1439
1440 pOldClient->Enable( FALSE );
1441 pOldClient->Show( FALSE );
1442 ::WinSetWindowUShort(pOldClient->GetHWND(), QWS_ID, (USHORT)pOldClient->GetId());
1443 // to avoid OS/2 bug need to update frame
1444 ::WinSendMsg((HWND)this->GetFrame(), WM_UPDATEFRAME, (MPARAM)~0, 0);
1445 return;
1446 }
1447
1448 //
1449 // Else need to change client
1450 //
1451 if(bClientHasFocus)
1452 this->SetFocus();
1453
1454 ::WinEnableWindowUpdate((HWND)GetHWND(), FALSE);
1455 if(pOldClient)
1456 {
1457 pOldClient->Enable(FALSE);
1458 pOldClient->Show(FALSE);
1459 ::WinSetWindowUShort(pOldClient->GetHWND(), QWS_ID, (USHORT)pOldClient->GetId());
1460 }
1461 pWindow->Reparent(this);
1462 ::WinSetWindowUShort(pWindow->GetHWND(), QWS_ID, FID_CLIENT);
1463 ::WinEnableWindowUpdate((HWND)GetHWND(), TRUE);
1464 pWindow->Enable();
1465 pWindow->Show(); // ensure client is showing
1466 if( this->IsShown() )
1467 {
1468 this->Show();
1469 ::WinSendMsg(m_hFrame, WM_UPDATEFRAME, (MPARAM)~0, 0);
1470 }
1471 }
1472
1473 wxWindow* wxFrame::GetClient()
1474 {
1475 return wxFindWinFromHandle((WXHWND)::WinWindowFromID(m_hFrame, FID_CLIENT));
1476 }