]> git.saurik.com Git - wxWidgets.git/blob - src/msw/frame.cpp
corrected conditional compilation
[wxWidgets.git] / src / msw / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/frame.cpp
3 // Purpose: wxFrameMSW
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "frame.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/frame.h"
33 #include "wx/app.h"
34 #include "wx/menu.h"
35 #include "wx/utils.h"
36 #include "wx/dialog.h"
37 #include "wx/settings.h"
38 #include "wx/dcclient.h"
39 #include "wx/mdi.h"
40 #include "wx/panel.h"
41 #endif // WX_PRECOMP
42
43 #include "wx/msw/private.h"
44
45 #if wxUSE_STATUSBAR
46 #include "wx/statusbr.h"
47 #include "wx/generic/statusbr.h"
48 #endif // wxUSE_STATUSBAR
49
50 #if wxUSE_TOOLBAR
51 #include "wx/toolbar.h"
52 #endif // wxUSE_TOOLBAR
53
54 #include "wx/menuitem.h"
55 #include "wx/log.h"
56
57 #ifdef __WXUNIVERSAL__
58 #include "wx/univ/theme.h"
59 #include "wx/univ/colschem.h"
60 #endif // __WXUNIVERSAL__
61
62 // ----------------------------------------------------------------------------
63 // globals
64 // ----------------------------------------------------------------------------
65
66 extern wxWindowList wxModelessWindows;
67 extern wxList WXDLLEXPORT wxPendingDelete;
68 extern const wxChar *wxFrameClassName;
69
70 #if wxUSE_MENUS_NATIVE
71 extern wxMenu *wxCurrentPopupMenu;
72 #endif // wxUSE_MENUS_NATIVE
73
74 // ----------------------------------------------------------------------------
75 // event tables
76 // ----------------------------------------------------------------------------
77
78 BEGIN_EVENT_TABLE(wxFrameMSW, wxFrameBase)
79 EVT_ACTIVATE(wxFrameMSW::OnActivate)
80 EVT_SYS_COLOUR_CHANGED(wxFrameMSW::OnSysColourChanged)
81 END_EVENT_TABLE()
82
83 IMPLEMENT_DYNAMIC_CLASS(wxFrameMSW, wxWindow)
84 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxFrameMSW)
85
86 // ============================================================================
87 // implementation
88 // ============================================================================
89
90 // ----------------------------------------------------------------------------
91 // static class members
92 // ----------------------------------------------------------------------------
93
94 #if wxUSE_STATUSBAR
95 #if wxUSE_NATIVE_STATUSBAR
96 bool wxFrameMSW::m_useNativeStatusBar = TRUE;
97 #else
98 bool wxFrameMSW::m_useNativeStatusBar = FALSE;
99 #endif
100 #endif // wxUSE_NATIVE_STATUSBAR
101
102 // ----------------------------------------------------------------------------
103 // creation/destruction
104 // ----------------------------------------------------------------------------
105
106 void wxFrameMSW::Init()
107 {
108 m_iconized =
109 m_maximizeOnShow = FALSE;
110
111 #if wxUSE_TOOLTIPS
112 m_hwndToolTip = 0;
113 #endif
114
115 // Data to save/restore when calling ShowFullScreen
116 m_fsStyle = 0;
117 m_fsOldWindowStyle = 0;
118 m_fsStatusBarFields = 0;
119 m_fsStatusBarHeight = 0;
120 m_fsToolBarHeight = 0;
121 // m_fsMenu = 0;
122 m_fsIsMaximized = FALSE;
123 m_fsIsShowing = FALSE;
124
125 m_winLastFocused = (wxWindow *)NULL;
126
127 // unlike (almost?) all other windows, frames are created hidden
128 m_isShown = FALSE;
129 }
130
131 bool wxFrameMSW::Create(wxWindow *parent,
132 wxWindowID id,
133 const wxString& title,
134 const wxPoint& pos,
135 const wxSize& size,
136 long style,
137 const wxString& name)
138 {
139 SetName(name);
140 m_windowStyle = style;
141 #if wxUSE_MENUS
142 m_frameMenuBar = NULL;
143 #endif // wxUSE_MENUS
144 #if wxUSE_TOOLBAR
145 m_frameToolBar = NULL;
146 #endif // wxUSE_TOOLBAR
147 #if wxUSE_STATUSBAR
148 m_frameStatusBar = NULL;
149 #endif // wxUSE_STATUSBAR
150
151 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE));
152
153 if ( id > -1 )
154 m_windowId = id;
155 else
156 m_windowId = (int)NewControlId();
157
158 if (parent) parent->AddChild(this);
159
160 int x = pos.x;
161 int y = pos.y;
162 int width = size.x;
163 int height = size.y;
164
165 m_iconized = FALSE;
166
167 wxTopLevelWindows.Append(this);
168
169 MSWCreate(m_windowId, parent, wxFrameClassName, this, title,
170 x, y, width, height, style);
171
172 wxModelessWindows.Append(this);
173
174 return TRUE;
175 }
176
177 wxFrameMSW::~wxFrameMSW()
178 {
179 m_isBeingDeleted = TRUE;
180 wxTopLevelWindows.DeleteObject(this);
181
182 // the ~wxToolBar() code relies on the previous line to be executed before
183 // this one, i.e. the frame should remove itself from wxTopLevelWindows
184 // before destorying its toolbar
185 DeleteAllBars();
186
187 if (wxTheApp && (wxTopLevelWindows.Number() == 0))
188 {
189 wxTheApp->SetTopWindow(NULL);
190
191 if (wxTheApp->GetExitOnFrameDelete())
192 {
193 PostQuitMessage(0);
194 }
195 }
196
197 wxModelessWindows.DeleteObject(this);
198
199 // For some reason, wxWindows can activate another task altogether
200 // when a frame is destroyed after a modal dialog has been invoked.
201 // Try to bring the parent to the top.
202 // MT:Only do this if this frame is currently the active window, else weird
203 // things start to happen
204 if ( wxGetActiveWindow() == this )
205 if (GetParent() && GetParent()->GetHWND())
206 ::BringWindowToTop((HWND) GetParent()->GetHWND());
207 }
208
209 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
210 void wxFrameMSW::DoGetClientSize(int *x, int *y) const
211 {
212 RECT rect;
213 ::GetClientRect(GetHwnd(), &rect);
214
215 #if wxUSE_STATUSBAR
216 if ( GetStatusBar() && GetStatusBar()->IsShown() )
217 {
218 int statusX, statusY;
219 GetStatusBar()->GetClientSize(&statusX, &statusY);
220 rect.bottom -= statusY;
221 }
222 #endif // wxUSE_STATUSBAR
223
224 wxPoint pt(GetClientAreaOrigin());
225 rect.bottom -= pt.y;
226 rect.right -= pt.x;
227
228 if ( x )
229 *x = rect.right;
230 if ( y )
231 *y = rect.bottom;
232 }
233
234 // Set the client size (i.e. leave the calculation of borders etc.
235 // to wxWindows)
236 void wxFrameMSW::DoSetClientSize(int width, int height)
237 {
238 HWND hWnd = GetHwnd();
239
240 RECT rectClient;
241 ::GetClientRect(hWnd, &rectClient);
242
243 RECT rectTotal;
244 ::GetWindowRect(hWnd, &rectTotal);
245
246 // Find the difference between the entire window (title bar and all)
247 // and the client area; add this to the new client size to move the
248 // window
249 width += rectTotal.right - rectTotal.left - rectClient.right;
250 height += rectTotal.bottom - rectTotal.top - rectClient.bottom;
251
252 #if wxUSE_STATUSBAR
253 wxStatusBar *statbar = GetStatusBar();
254 if ( statbar && statbar->IsShown() )
255 {
256 // leave enough space for the status bar
257 height += statbar->GetSize().y;
258 }
259 #endif // wxUSE_STATUSBAR
260
261 // note that this takes the toolbar into account
262 wxPoint pt = GetClientAreaOrigin();
263 width += pt.x;
264 height += pt.y;
265
266 if ( !::MoveWindow(hWnd, rectTotal.left, rectTotal.top,
267 width, height, TRUE /* redraw */) )
268 {
269 wxLogLastError(_T("MoveWindow"));
270 }
271
272 wxSizeEvent event(wxSize(width, height), m_windowId);
273 event.SetEventObject(this);
274 GetEventHandler()->ProcessEvent(event);
275 }
276
277 void wxFrameMSW::DoGetSize(int *width, int *height) const
278 {
279 RECT rect;
280 ::GetWindowRect(GetHwnd(), &rect);
281
282 *width = rect.right - rect.left;
283 *height = rect.bottom - rect.top;
284 }
285
286 void wxFrameMSW::DoGetPosition(int *x, int *y) const
287 {
288 RECT rect;
289 ::GetWindowRect(GetHwnd(), &rect);
290
291 *x = rect.left;
292 *y = rect.top;
293 }
294
295 // ----------------------------------------------------------------------------
296 // variations around ::ShowWindow()
297 // ----------------------------------------------------------------------------
298
299 void wxFrameMSW::DoShowWindow(int nShowCmd)
300 {
301 ::ShowWindow(GetHwnd(), nShowCmd);
302
303 m_iconized = nShowCmd == SW_MINIMIZE;
304 }
305
306 bool wxFrameMSW::Show(bool show)
307 {
308 // don't use wxWindow version as we want to call DoShowWindow()
309 if ( !wxWindowBase::Show(show) )
310 return FALSE;
311
312 int nShowCmd;
313 if ( show )
314 {
315 if ( m_maximizeOnShow )
316 {
317 // show and maximize
318 nShowCmd = SW_MAXIMIZE;
319
320 m_maximizeOnShow = FALSE;
321 }
322 else // just show
323 {
324 nShowCmd = SW_SHOW;
325 }
326 }
327 else // hide
328 {
329 nShowCmd = SW_HIDE;
330 }
331
332 DoShowWindow(nShowCmd);
333
334 if ( show )
335 {
336 ::BringWindowToTop(GetHwnd());
337
338 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, m_windowId);
339 event.SetEventObject( this );
340 GetEventHandler()->ProcessEvent(event);
341 }
342 else // hide
343 {
344 // Try to highlight the correct window (the parent)
345 if ( GetParent() )
346 {
347 HWND hWndParent = GetHwndOf(GetParent());
348 if (hWndParent)
349 ::BringWindowToTop(hWndParent);
350 }
351 }
352
353 return TRUE;
354 }
355
356 void wxFrameMSW::Iconize(bool iconize)
357 {
358 DoShowWindow(iconize ? SW_MINIMIZE : SW_RESTORE);
359 }
360
361 void wxFrameMSW::Maximize(bool maximize)
362 {
363 if ( IsShown() )
364 {
365 // just maximize it directly
366 DoShowWindow(maximize ? SW_MAXIMIZE : SW_RESTORE);
367 }
368 else // hidden
369 {
370 // we can't maximize the hidden frame because it shows it as well, so
371 // just remember that we should do it later in this case
372 m_maximizeOnShow = TRUE;
373 }
374 }
375
376 void wxFrameMSW::Restore()
377 {
378 DoShowWindow(SW_RESTORE);
379 }
380
381 bool wxFrameMSW::IsIconized() const
382 {
383 ((wxFrameMSW *)this)->m_iconized = (::IsIconic(GetHwnd()) != 0);
384 return m_iconized;
385 }
386
387 // Is it maximized?
388 bool wxFrameMSW::IsMaximized() const
389 {
390 return (::IsZoomed(GetHwnd()) != 0);
391 }
392
393 void wxFrameMSW::SetIcon(const wxIcon& icon)
394 {
395 wxFrameBase::SetIcon(icon);
396
397 #if defined(__WIN95__)
398 if ( m_icon.Ok() )
399 {
400 SendMessage(GetHwnd(), WM_SETICON,
401 (WPARAM)TRUE, (LPARAM)(HICON) m_icon.GetHICON());
402 }
403 #endif // __WIN95__
404 }
405
406 // generate an artificial resize event
407 void wxFrameMSW::SendSizeEvent()
408 {
409 RECT r;
410 #ifdef __WIN16__
411 ::GetWindowRect(GetHwnd(), &r);
412 #else
413 if ( !::GetWindowRect(GetHwnd(), &r) )
414 {
415 wxLogLastError(_T("GetWindowRect"));
416 }
417 #endif
418
419 if ( !m_iconized )
420 {
421 (void)::PostMessage(GetHwnd(), WM_SIZE,
422 IsMaximized() ? SIZE_MAXIMIZED : SIZE_RESTORED,
423 MAKELPARAM(r.right - r.left, r.bottom - r.top));
424 }
425 }
426
427 #if wxUSE_STATUSBAR
428 wxStatusBar *wxFrameMSW::OnCreateStatusBar(int number,
429 long style,
430 wxWindowID id,
431 const wxString& name)
432 {
433 wxStatusBar *statusBar = NULL;
434
435 #if wxUSE_NATIVE_STATUSBAR
436 if ( !UsesNativeStatusBar() )
437 {
438 statusBar = (wxStatusBar *)new wxStatusBarGeneric(this, id, style);
439 }
440 else
441 #endif
442 {
443 statusBar = new wxStatusBar(this, id, style, name);
444 }
445
446 // Set the height according to the font and the border size
447 wxClientDC dc(statusBar);
448 dc.SetFont(statusBar->GetFont());
449
450 wxCoord y;
451 dc.GetTextExtent(_T("X"), NULL, &y );
452
453 int height = (int)( (11*y)/10 + 2*statusBar->GetBorderY());
454
455 statusBar->SetSize(-1, -1, -1, height);
456
457 statusBar->SetFieldsCount(number);
458
459 return statusBar;
460 }
461
462 void wxFrameMSW::PositionStatusBar()
463 {
464 if ( !m_frameStatusBar )
465 return;
466
467 int w, h;
468 GetClientSize(&w, &h);
469 int sw, sh;
470 m_frameStatusBar->GetSize(&sw, &sh);
471
472 // Since we wish the status bar to be directly under the client area,
473 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
474 m_frameStatusBar->SetSize(0, h, w, sh);
475 }
476 #endif // wxUSE_STATUSBAR
477
478 void wxFrameMSW::DetachMenuBar()
479 {
480 #if wxUSE_MENUS
481 if ( m_frameMenuBar )
482 {
483 m_frameMenuBar->Detach();
484 m_frameMenuBar = NULL;
485 }
486 #endif // wxUSE_MENUS
487 }
488
489 void wxFrameMSW::SetMenuBar(wxMenuBar *menubar)
490 {
491 #if wxUSE_MENUS
492 // detach the old menu bar in any case
493 DetachMenuBar();
494
495 #if wxUSE_MENUS_NATIVE
496 if ( !menubar )
497 {
498 // actually remove the menu from the frame
499 m_hMenu = (WXHMENU)0;
500 InternalSetMenuBar();
501 }
502 else // set new non NULL menu bar
503 {
504 // Can set a menubar several times.
505 if ( menubar->GetHMenu() )
506 {
507 m_hMenu = menubar->GetHMenu();
508 }
509 else
510 {
511 if (menubar->IsAttached())
512 menubar->Detach();
513
514 m_hMenu = menubar->Create();
515
516 if ( !m_hMenu )
517 return;
518 }
519
520 InternalSetMenuBar();
521 }
522 #endif // wxUSE_MENUS_NATIVE
523
524 if ( menubar )
525 {
526 m_frameMenuBar = menubar;
527 menubar->Attach((wxFrame *)this);
528 }
529 #endif // wxUSE_MENUS
530 }
531
532 #if wxUSE_MENUS_NATIVE
533
534 void wxFrameMSW::InternalSetMenuBar()
535 {
536 if ( !::SetMenu(GetHwnd(), (HMENU)m_hMenu) )
537 {
538 wxLogLastError(wxT("SetMenu"));
539 }
540 }
541
542 #endif // wxUSE_MENUS_NATIVE
543
544 // Responds to colour changes, and passes event on to children.
545 void wxFrameMSW::OnSysColourChanged(wxSysColourChangedEvent& event)
546 {
547 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE));
548 Refresh();
549
550 #if wxUSE_STATUSBAR
551 if ( m_frameStatusBar )
552 {
553 wxSysColourChangedEvent event2;
554 event2.SetEventObject( m_frameStatusBar );
555 m_frameStatusBar->GetEventHandler()->ProcessEvent(event2);
556 }
557 #endif // wxUSE_STATUSBAR
558
559 // Propagate the event to the non-top-level children
560 wxWindow::OnSysColourChanged(event);
561 }
562
563 // Pass TRUE to show full screen, FALSE to restore.
564 bool wxFrameMSW::ShowFullScreen(bool show, long style)
565 {
566 if (show)
567 {
568 if (IsFullScreen())
569 return FALSE;
570
571 m_fsIsShowing = TRUE;
572 m_fsStyle = style;
573
574 #if wxUSE_TOOLBAR
575 wxToolBar *theToolBar = GetToolBar();
576 if (theToolBar)
577 theToolBar->GetSize(NULL, &m_fsToolBarHeight);
578
579 // zap the toolbar, menubar, and statusbar
580
581 if ((style & wxFULLSCREEN_NOTOOLBAR) && theToolBar)
582 {
583 theToolBar->SetSize(-1,0);
584 theToolBar->Show(FALSE);
585 }
586 #endif // wxUSE_TOOLBAR
587
588 if (style & wxFULLSCREEN_NOMENUBAR)
589 SetMenu((HWND)GetHWND(), (HMENU) NULL);
590
591 #if wxUSE_STATUSBAR
592 wxStatusBar *theStatusBar = GetStatusBar();
593 if (theStatusBar)
594 theStatusBar->GetSize(NULL, &m_fsStatusBarHeight);
595
596 // Save the number of fields in the statusbar
597 if ((style & wxFULLSCREEN_NOSTATUSBAR) && theStatusBar)
598 {
599 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
600 //SetStatusBar((wxStatusBar*) NULL);
601 //delete theStatusBar;
602 theStatusBar->Show(FALSE);
603 }
604 else
605 m_fsStatusBarFields = 0;
606 #endif // wxUSE_STATUSBAR
607
608 // zap the frame borders
609
610 // save the 'normal' window style
611 m_fsOldWindowStyle = GetWindowLong((HWND)GetHWND(), GWL_STYLE);
612
613 // save the old position, width & height, maximize state
614 m_fsOldSize = GetRect();
615 m_fsIsMaximized = IsMaximized();
616
617 // decide which window style flags to turn off
618 LONG newStyle = m_fsOldWindowStyle;
619 LONG offFlags = 0;
620
621 if (style & wxFULLSCREEN_NOBORDER)
622 offFlags |= WS_BORDER;
623 if (style & wxFULLSCREEN_NOCAPTION)
624 offFlags |= (WS_CAPTION | WS_SYSMENU);
625
626 newStyle &= (~offFlags);
627
628 // change our window style to be compatible with full-screen mode
629 SetWindowLong((HWND)GetHWND(), GWL_STYLE, newStyle);
630
631 // resize to the size of the desktop
632 int width, height;
633
634 RECT rect;
635 ::GetWindowRect(GetDesktopWindow(), &rect);
636 width = rect.right - rect.left;
637 height = rect.bottom - rect.top;
638
639 SetSize(width, height);
640
641 // now flush the window style cache and actually go full-screen
642 SetWindowPos((HWND)GetHWND(), HWND_TOP, 0, 0, width, height, SWP_FRAMECHANGED);
643
644 wxSizeEvent event(wxSize(width, height), GetId());
645 GetEventHandler()->ProcessEvent(event);
646
647 return TRUE;
648 }
649 else
650 {
651 if (!IsFullScreen())
652 return FALSE;
653
654 m_fsIsShowing = FALSE;
655
656 #if wxUSE_TOOLBAR
657 wxToolBar *theToolBar = GetToolBar();
658
659 // restore the toolbar, menubar, and statusbar
660 if (theToolBar && (m_fsStyle & wxFULLSCREEN_NOTOOLBAR))
661 {
662 theToolBar->SetSize(-1, m_fsToolBarHeight);
663 theToolBar->Show(TRUE);
664 }
665 #endif // wxUSE_TOOLBAR
666
667 #if wxUSE_STATUSBAR
668 if ( m_fsStyle & wxFULLSCREEN_NOSTATUSBAR )
669 {
670 //CreateStatusBar(m_fsStatusBarFields);
671 if (GetStatusBar())
672 {
673 GetStatusBar()->Show(TRUE);
674 PositionStatusBar();
675 }
676 }
677 #endif // wxUSE_STATUSBAR
678
679 if ((m_fsStyle & wxFULLSCREEN_NOMENUBAR) && (m_hMenu != 0))
680 SetMenu((HWND)GetHWND(), (HMENU)m_hMenu);
681
682 Maximize(m_fsIsMaximized);
683 SetWindowLong((HWND)GetHWND(),GWL_STYLE, m_fsOldWindowStyle);
684 SetWindowPos((HWND)GetHWND(),HWND_TOP,m_fsOldSize.x, m_fsOldSize.y,
685 m_fsOldSize.width, m_fsOldSize.height, SWP_FRAMECHANGED);
686
687 return TRUE;
688 }
689 }
690
691 /*
692 * Frame window
693 *
694 */
695
696 bool wxFrameMSW::MSWCreate(int id, wxWindow *parent, const wxChar *wclass, wxWindow *wx_win, const wxChar *title,
697 int x, int y, int width, int height, long style)
698
699 {
700 m_defaultIcon = (WXHICON) (wxSTD_FRAME_ICON ? wxSTD_FRAME_ICON : wxDEFAULT_FRAME_ICON);
701
702 // If child windows aren't properly drawn initially, WS_CLIPCHILDREN
703 // could be the culprit. But without it, you can get a lot of flicker.
704
705 DWORD msflags = 0;
706 if ( style & wxCAPTION )
707 {
708 if ( style & wxFRAME_TOOL_WINDOW )
709 msflags |= WS_POPUPWINDOW;
710 else
711 msflags |= WS_OVERLAPPED;
712 }
713 else
714 {
715 msflags |= WS_POPUP;
716 }
717
718 if (style & wxMINIMIZE_BOX)
719 msflags |= WS_MINIMIZEBOX;
720 if (style & wxMAXIMIZE_BOX)
721 msflags |= WS_MAXIMIZEBOX;
722 if (style & wxTHICK_FRAME)
723 msflags |= WS_THICKFRAME;
724 if (style & wxSYSTEM_MENU)
725 msflags |= WS_SYSMENU;
726 if ( style & wxMINIMIZE )
727 msflags |= WS_MINIMIZE;
728 if (style & wxMAXIMIZE)
729 msflags |= WS_MAXIMIZE;
730 if (style & wxCAPTION)
731 msflags |= WS_CAPTION;
732 if (style & wxCLIP_CHILDREN)
733 msflags |= WS_CLIPCHILDREN;
734
735 // Keep this in wxFrameMSW because it saves recoding this function
736 // in wxTinyFrame
737 #if wxUSE_ITSY_BITSY && !defined(__WIN32__)
738 if (style & wxTINY_CAPTION_VERT)
739 msflags |= IBS_VERTCAPTION;
740 if (style & wxTINY_CAPTION_HORIZ)
741 msflags |= IBS_HORZCAPTION;
742 #else
743 if (style & wxTINY_CAPTION_VERT)
744 msflags |= WS_CAPTION;
745 if (style & wxTINY_CAPTION_HORIZ)
746 msflags |= WS_CAPTION;
747 #endif
748 if ((style & wxTHICK_FRAME) == 0)
749 msflags |= WS_BORDER;
750
751 WXDWORD extendedStyle = MakeExtendedStyle(style);
752
753 // make all frames appear in the win9x shell taskbar unless
754 // wxFRAME_TOOL_WINDOW or wxFRAME_NO_TASKBAR is given - without giving them
755 // WS_EX_APPWINDOW style, the child (i.e. owned) frames wouldn't appear in it
756 #if !defined(__WIN16__) && !defined(__SC__)
757 if ( (style & wxFRAME_TOOL_WINDOW) ||
758 (style & wxFRAME_NO_TASKBAR) )
759 extendedStyle |= WS_EX_TOOLWINDOW;
760 else if ( !(style & wxFRAME_NO_TASKBAR) )
761 extendedStyle |= WS_EX_APPWINDOW;
762 #endif
763
764 if (style & wxSTAY_ON_TOP)
765 extendedStyle |= WS_EX_TOPMOST;
766
767 #ifndef __WIN16__
768 if (m_exStyle & wxFRAME_EX_CONTEXTHELP)
769 extendedStyle |= WS_EX_CONTEXTHELP;
770 #endif
771
772 m_iconized = FALSE;
773 if ( !wxWindow::MSWCreate(id, parent, wclass, wx_win, title, x, y, width, height,
774 msflags, NULL, extendedStyle) )
775 return FALSE;
776
777 // Seems to be necessary if we use WS_POPUP
778 // style instead of WS_OVERLAPPED
779 if (width > -1 && height > -1)
780 ::PostMessage(GetHwnd(), WM_SIZE, SIZE_RESTORED, MAKELPARAM(width, height));
781
782 return TRUE;
783 }
784
785 // Default activation behaviour - set the focus for the first child
786 // subwindow found.
787 void wxFrameMSW::OnActivate(wxActivateEvent& event)
788 {
789 if ( event.GetActive() )
790 {
791 // restore focus to the child which was last focused
792 wxLogTrace(_T("focus"), _T("wxFrameMSW %08x activated."), m_hWnd);
793
794 wxWindow *parent = m_winLastFocused ? m_winLastFocused->GetParent()
795 : NULL;
796 if ( !parent )
797 {
798 parent = this;
799 }
800
801 wxSetFocusToChild(parent, &m_winLastFocused);
802 }
803 else // deactivating
804 {
805 // remember the last focused child if it is our child
806 m_winLastFocused = FindFocus();
807
808 // so we NULL it out if it's a child from some other frame
809 wxWindow *win = m_winLastFocused;
810 while ( win )
811 {
812 if ( win->IsTopLevel() )
813 {
814 if ( win != this )
815 {
816 m_winLastFocused = NULL;
817 }
818
819 break;
820 }
821
822 win = win->GetParent();
823 }
824
825 wxLogTrace(_T("focus"),
826 _T("wxFrameMSW %08x deactivated, last focused: %08x."),
827 m_hWnd,
828 m_winLastFocused ? GetHwndOf(m_winLastFocused)
829 : NULL);
830
831 event.Skip();
832 }
833 }
834
835 // ----------------------------------------------------------------------------
836 // tool/status bar stuff
837 // ----------------------------------------------------------------------------
838
839 #if wxUSE_TOOLBAR
840
841 wxToolBar* wxFrameMSW::CreateToolBar(long style, wxWindowID id, const wxString& name)
842 {
843 if ( wxFrameBase::CreateToolBar(style, id, name) )
844 {
845 PositionToolBar();
846 }
847
848 return m_frameToolBar;
849 }
850
851 void wxFrameMSW::PositionToolBar()
852 {
853 RECT rect;
854 ::GetClientRect(GetHwnd(), &rect);
855
856 #if wxUSE_STATUSBAR
857 if ( GetStatusBar() )
858 {
859 int statusX, statusY;
860 GetStatusBar()->GetClientSize(&statusX, &statusY);
861 rect.bottom -= statusY;
862 }
863 #endif // wxUSE_STATUSBAR
864
865 if ( GetToolBar() && GetToolBar()->IsShown() )
866 {
867 int tw, th;
868 GetToolBar()->GetSize(&tw, &th);
869
870 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL )
871 {
872 th = rect.bottom;
873 }
874 else
875 {
876 tw = rect.right;
877 }
878
879 // Use the 'real' MSW position here
880 GetToolBar()->SetSize(0, 0, tw, th, wxSIZE_NO_ADJUSTMENTS);
881 }
882 }
883 #endif // wxUSE_TOOLBAR
884
885 // ----------------------------------------------------------------------------
886 // frame state (iconized/maximized/...)
887 // ----------------------------------------------------------------------------
888
889 // propagate our state change to all child frames: this allows us to emulate X
890 // Windows behaviour where child frames float independently of the parent one
891 // on the desktop, but are iconized/restored with it
892 void wxFrameMSW::IconizeChildFrames(bool bIconize)
893 {
894 for ( wxWindowList::Node *node = GetChildren().GetFirst();
895 node;
896 node = node->GetNext() )
897 {
898 wxWindow *win = node->GetData();
899
900 // iconizing the frames with this style under Win95 shell puts them at
901 // the bottom of the screen (as the MDI children) instead of making
902 // them appear in the taskbar because they are, by virtue of this
903 // style, not managed by the taskbar - instead leave Windows take care
904 // of them
905 #ifdef __WIN95__
906 if ( win->GetWindowStyle() & wxFRAME_TOOL_WINDOW )
907 continue;
908 #endif // Win95
909
910 // the child MDI frames are a special case and should not be touched by
911 // the parent frame - instead, they are managed by the user
912 wxFrameMSW *frame = wxDynamicCast(win, wxFrameMSW);
913 if ( frame
914 #if wxUSE_MDI_ARCHITECTURE
915 && !wxDynamicCast(frame, wxMDIChildFrame)
916 #endif // wxUSE_MDI_ARCHITECTURE
917 )
918 {
919 frame->Iconize(bIconize);
920 }
921 }
922 }
923
924 // ===========================================================================
925 // message processing
926 // ===========================================================================
927
928 // ---------------------------------------------------------------------------
929 // preprocessing
930 // ---------------------------------------------------------------------------
931
932 bool wxFrameMSW::MSWTranslateMessage(WXMSG* pMsg)
933 {
934 if ( wxWindow::MSWTranslateMessage(pMsg) )
935 return TRUE;
936
937 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
938 // try the menu bar accels
939 wxMenuBar *menuBar = GetMenuBar();
940 if ( !menuBar )
941 return FALSE;
942
943 const wxAcceleratorTable& acceleratorTable = menuBar->GetAccelTable();
944 return acceleratorTable.Translate(this, pMsg);
945 #else
946 return FALSE;
947 #endif // wxUSE_MENUS && wxUSE_ACCEL
948 }
949
950 // ---------------------------------------------------------------------------
951 // our private (non virtual) message handlers
952 // ---------------------------------------------------------------------------
953
954 bool wxFrameMSW::HandlePaint()
955 {
956 RECT rect;
957 if ( GetUpdateRect(GetHwnd(), &rect, FALSE) )
958 {
959 if ( m_iconized )
960 {
961 HICON hIcon = m_icon.Ok() ? GetHiconOf(m_icon)
962 : (HICON)m_defaultIcon;
963
964 // Hold a pointer to the dc so long as the OnPaint() message
965 // is being processed
966 PAINTSTRUCT ps;
967 HDC hdc = ::BeginPaint(GetHwnd(), &ps);
968
969 // Erase background before painting or we get white background
970 MSWDefWindowProc(WM_ICONERASEBKGND, (WORD)(LONG)ps.hdc, 0L);
971
972 if ( hIcon )
973 {
974 RECT rect;
975 ::GetClientRect(GetHwnd(), &rect);
976
977 // FIXME: why hardcoded?
978 static const int icon_width = 32;
979 static const int icon_height = 32;
980
981 int icon_x = (int)((rect.right - icon_width)/2);
982 int icon_y = (int)((rect.bottom - icon_height)/2);
983
984 ::DrawIcon(hdc, icon_x, icon_y, hIcon);
985 }
986
987 ::EndPaint(GetHwnd(), &ps);
988
989 return TRUE;
990 }
991 else
992 {
993 return wxWindow::HandlePaint();
994 }
995 }
996 else
997 {
998 // nothing to paint - processed
999 return TRUE;
1000 }
1001 }
1002
1003 bool wxFrameMSW::HandleSize(int x, int y, WXUINT id)
1004 {
1005 bool processed = FALSE;
1006
1007 switch ( id )
1008 {
1009 case SIZENORMAL:
1010 // only do it it if we were iconized before, otherwise resizing the
1011 // parent frame has a curious side effect of bringing it under it's
1012 // children
1013 if ( !m_iconized )
1014 break;
1015
1016 // restore all child frames too
1017 IconizeChildFrames(FALSE);
1018
1019 (void)SendIconizeEvent(FALSE);
1020
1021 // fall through
1022
1023 case SIZEFULLSCREEN:
1024 m_iconized = FALSE;
1025 break;
1026
1027 case SIZEICONIC:
1028 // iconize all child frames too
1029 IconizeChildFrames(TRUE);
1030
1031 (void)SendIconizeEvent();
1032
1033 m_iconized = TRUE;
1034 break;
1035 }
1036
1037 if ( !m_iconized )
1038 {
1039 #if wxUSE_STATUSBAR
1040 PositionStatusBar();
1041 #endif // wxUSE_STATUSBAR
1042
1043 #if wxUSE_TOOLBAR
1044 PositionToolBar();
1045 #endif // wxUSE_TOOLBAR
1046
1047 wxSizeEvent event(wxSize(x, y), m_windowId);
1048 event.SetEventObject( this );
1049 processed = GetEventHandler()->ProcessEvent(event);
1050 }
1051
1052 return processed;
1053 }
1054
1055 bool wxFrameMSW::HandleCommand(WXWORD id, WXWORD cmd, WXHWND control)
1056 {
1057 if ( control )
1058 {
1059 // In case it's e.g. a toolbar.
1060 wxWindow *win = wxFindWinFromHandle(control);
1061 if ( win )
1062 return win->MSWCommand(cmd, id);
1063 }
1064
1065 // handle here commands from menus and accelerators
1066 if ( cmd == 0 || cmd == 1 )
1067 {
1068 #if wxUSE_MENUS_NATIVE
1069 if ( wxCurrentPopupMenu )
1070 {
1071 wxMenu *popupMenu = wxCurrentPopupMenu;
1072 wxCurrentPopupMenu = NULL;
1073
1074 return popupMenu->MSWCommand(cmd, id);
1075 }
1076 #endif // wxUSE_MENUS_NATIVE
1077
1078 if ( ProcessCommand(id) )
1079 {
1080 return TRUE;
1081 }
1082 }
1083
1084 return FALSE;
1085 }
1086
1087 bool wxFrameMSW::HandleMenuSelect(WXWORD nItem, WXWORD flags, WXHMENU hMenu)
1088 {
1089 int item;
1090 if ( flags == 0xFFFF && hMenu == 0 )
1091 {
1092 // menu was removed from screen
1093 item = -1;
1094 }
1095 else if ( !(flags & MF_POPUP) && !(flags & MF_SEPARATOR) )
1096 {
1097 item = nItem;
1098 }
1099 else
1100 {
1101 #if wxUSE_STATUSBAR
1102 // don't give hints for separators (doesn't make sense) nor for the
1103 // items opening popup menus (they don't have them anyhow) but do clear
1104 // the status line - otherwise, we would be left with the help message
1105 // for the previous item which doesn't apply any more
1106 wxStatusBar *statbar = GetStatusBar();
1107 if ( statbar )
1108 {
1109 statbar->SetStatusText(wxEmptyString);
1110 }
1111 #endif // wxUSE_STATUSBAR
1112
1113 return FALSE;
1114 }
1115
1116 wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, item);
1117 event.SetEventObject( this );
1118
1119 return GetEventHandler()->ProcessEvent(event);
1120 }
1121
1122 // ---------------------------------------------------------------------------
1123 // the window proc for wxFrameMSW
1124 // ---------------------------------------------------------------------------
1125
1126 long wxFrameMSW::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
1127 {
1128 long rc = 0;
1129 bool processed = FALSE;
1130
1131 switch ( message )
1132 {
1133 case WM_CLOSE:
1134 // if we can't close, tell the system that we processed the
1135 // message - otherwise it would close us
1136 processed = !Close();
1137 break;
1138
1139 case WM_COMMAND:
1140 {
1141 WORD id, cmd;
1142 WXHWND hwnd;
1143 UnpackCommand((WXWPARAM)wParam, (WXLPARAM)lParam,
1144 &id, &hwnd, &cmd);
1145
1146 processed = HandleCommand(id, cmd, (WXHWND)hwnd);
1147 }
1148 break;
1149
1150 case WM_MENUSELECT:
1151 {
1152 WXWORD item, flags;
1153 WXHMENU hmenu;
1154 UnpackMenuSelect(wParam, lParam, &item, &flags, &hmenu);
1155
1156 processed = HandleMenuSelect(item, flags, hmenu);
1157 }
1158 break;
1159
1160 case WM_PAINT:
1161 processed = HandlePaint();
1162 break;
1163
1164 case WM_QUERYDRAGICON:
1165 {
1166 HICON hIcon = m_icon.Ok() ? GetHiconOf(m_icon)
1167 : (HICON)(m_defaultIcon);
1168 rc = (long)hIcon;
1169 processed = rc != 0;
1170 }
1171 break;
1172
1173 case WM_SIZE:
1174 processed = HandleSize(LOWORD(lParam), HIWORD(lParam), wParam);
1175 break;
1176 }
1177
1178 if ( !processed )
1179 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
1180
1181 return rc;
1182 }
1183