Added "metal" theme.
[wxWidgets.git] / src / univ / winuniv.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: univ/window.cpp
3 // Purpose: implementation of extra wxWindow methods for wxUniv port
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 06.08.00
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "univwindow.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/app.h"
33 #include "wx/window.h"
34 #include "wx/dcclient.h"
35 #include "wx/dcmemory.h"
36 #include "wx/event.h"
37 #include "wx/scrolbar.h"
38 #include "wx/menu.h"
39 #include "wx/frame.h"
40 #endif // WX_PRECOMP
41
42 #include "wx/univ/colschem.h"
43 #include "wx/univ/renderer.h"
44 #include "wx/univ/theme.h"
45
46 #if wxUSE_CARET
47 #include "wx/caret.h"
48 #endif // wxUSE_CARET
49
50 // turn Refresh() debugging on/off
51 #define WXDEBUG_REFRESH
52
53 #ifndef __WXDEBUG__
54 #undef WXDEBUG_REFRESH
55 #endif
56
57 #if defined(WXDEBUG_REFRESH) && defined(__WXMSW__) && !defined(__WXMICROWIN__)
58 #include "wx/msw/private.h"
59 #endif
60
61 // ============================================================================
62 // implementation
63 // ============================================================================
64
65 // ----------------------------------------------------------------------------
66 // event tables
67 // ----------------------------------------------------------------------------
68
69 // we can't use wxWindowNative here as it won't be expanded inside the macro
70 #if defined(__WXMSW__)
71 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowMSW)
72 #elif defined(__WXGTK__)
73 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowGTK)
74 #elif defined(__WXMGL__)
75 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowMGL)
76 #elif defined(__WXX11__)
77 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowX11)
78 #elif defined(__WXPM__)
79 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowOS2)
80 #endif
81
82 BEGIN_EVENT_TABLE(wxWindow, wxWindowNative)
83 EVT_SIZE(wxWindow::OnSize)
84
85 #if wxUSE_ACCEL || wxUSE_MENUS
86 EVT_KEY_DOWN(wxWindow::OnKeyDown)
87 #endif // wxUSE_ACCEL
88
89 #if wxUSE_MENUS
90 EVT_CHAR(wxWindow::OnChar)
91 EVT_KEY_UP(wxWindow::OnKeyUp)
92 #endif // wxUSE_MENUS
93
94 EVT_PAINT(wxWindow::OnPaint)
95 EVT_NC_PAINT(wxWindow::OnNcPaint)
96 EVT_ERASE_BACKGROUND(wxWindow::OnErase)
97 END_EVENT_TABLE()
98
99 // ----------------------------------------------------------------------------
100 // creation
101 // ----------------------------------------------------------------------------
102
103 void wxWindow::Init()
104 {
105 m_scrollbarVert =
106 m_scrollbarHorz = (wxScrollBar *)NULL;
107
108 m_isCurrent = FALSE;
109
110 m_renderer = wxTheme::Get()->GetRenderer();
111
112 m_oldSize.x = -1;
113 m_oldSize.y = -1;
114
115 m_hasDialogBackground = FALSE;
116 }
117
118 bool wxWindow::Create(wxWindow *parent,
119 wxWindowID id,
120 const wxPoint& pos,
121 const wxSize& size,
122 long style,
123 const wxString& name)
124 {
125 // we add wxCLIP_CHILDREN to get the same ("natural") behaviour under MSW
126 // as under the other platforms
127 if ( !wxWindowNative::Create(parent, id, pos, size,
128 style | wxCLIP_CHILDREN,
129 name) )
130 {
131 return FALSE;
132 }
133
134 // if we should always have the scrollbar, do show it
135 if ( GetWindowStyle() & wxALWAYS_SHOW_SB )
136 {
137 #if wxUSE_TWO_WINDOWS
138 SetInsertIntoMain( TRUE );
139 #endif
140 m_scrollbarVert = new wxScrollBar(this, -1,
141 wxDefaultPosition, wxDefaultSize,
142 wxSB_VERTICAL);
143 #if wxUSE_TWO_WINDOWS
144 SetInsertIntoMain( FALSE );
145 #endif
146
147 // and position it
148 PositionScrollbars();
149 }
150
151 return TRUE;
152 }
153
154 // ----------------------------------------------------------------------------
155 // background pixmap
156 // ----------------------------------------------------------------------------
157
158 void wxWindow::SetBackground(const wxBitmap& bitmap,
159 int alignment,
160 wxStretch stretch)
161 {
162 m_bitmapBg = bitmap;
163 m_alignBgBitmap = alignment;
164 m_stretchBgBitmap = stretch;
165 }
166
167 const wxBitmap& wxWindow::GetBackgroundBitmap(int *alignment,
168 wxStretch *stretch) const
169 {
170 if ( m_bitmapBg.Ok() )
171 {
172 if ( alignment )
173 *alignment = m_alignBgBitmap;
174 if ( stretch )
175 *stretch = m_stretchBgBitmap;
176 }
177
178 return m_bitmapBg;
179 }
180
181 // ----------------------------------------------------------------------------
182 // painting
183 // ----------------------------------------------------------------------------
184
185 // the event handler executed when the window background must be painted
186 void wxWindow::OnErase(wxEraseEvent& event)
187 {
188 if ( !m_renderer )
189 {
190 event.Skip();
191
192 return;
193 }
194
195 DoDrawBackground(*event.GetDC());
196
197 // if we have both scrollbars, we also have a square in the corner between
198 // them which we must paint
199 if ( m_scrollbarVert && m_scrollbarHorz )
200 {
201 wxSize size = GetSize();
202 wxRect rectClient = GetClientRect(),
203 rectBorder = m_renderer->GetBorderDimensions(GetBorder());
204
205 wxRect rectCorner;
206 rectCorner.x = rectClient.GetRight() + 1;
207 rectCorner.y = rectClient.GetBottom() + 1;
208 rectCorner.SetRight(size.x - rectBorder.width);
209 rectCorner.SetBottom(size.y - rectBorder.height);
210
211 if ( GetUpdateRegion().Contains(rectCorner) )
212 {
213 m_renderer->DrawScrollCorner(*event.GetDC(), rectCorner);
214 }
215 }
216 }
217
218 // the event handlers executed when the window must be repainted
219 void wxWindow::OnNcPaint(wxPaintEvent& event)
220 {
221 if ( m_renderer )
222 {
223 // get the window rect
224 wxRect rect;
225 wxSize size = GetSize();
226 rect.x =
227 rect.y = 0;
228 rect.width = size.x;
229 rect.height = size.y;
230
231 // if the scrollbars are outside the border, we must adjust the rect to
232 // exclude them
233 if ( !m_renderer->AreScrollbarsInsideBorder() )
234 {
235 wxScrollBar *scrollbar = GetScrollbar(wxVERTICAL);
236 if ( scrollbar )
237 rect.width -= scrollbar->GetSize().x;
238
239 scrollbar = GetScrollbar(wxHORIZONTAL);
240 if ( scrollbar )
241 rect.height -= scrollbar->GetSize().y;
242 }
243
244 // get the DC and draw the border on it
245 wxWindowDC dc(this);
246 DoDrawBorder(dc, rect);
247 }
248 }
249
250 void wxWindow::OnPaint(wxPaintEvent& event)
251 {
252 if ( !m_renderer )
253 {
254 // it is a native control which paints itself
255 event.Skip();
256 }
257 else
258 {
259 // get the DC to use and create renderer on it
260 wxPaintDC dc(this);
261 wxControlRenderer renderer(this, dc, m_renderer);
262
263 // draw the control
264 DoDraw(&renderer);
265 }
266 }
267
268 bool wxWindow::DoDrawBackground(wxDC& dc)
269 {
270 // FIXME: Leaving this code in leads to partial bg redraws
271 // sometimes under MSW.
272 // The same happens under X11 because it has a clear
273 // region and an update region and these are sometimes
274 // different. RR.
275 wxRect rect;
276 // #ifndef __WXMSW__
277 #if 0
278 rect = GetUpdateRegion().GetBox();
279 if ( !rect.width && !rect.height )
280 #endif
281 {
282 wxSize size = GetSize();
283 rect.width = size.x;
284 rect.height = size.y;
285 }
286
287 if ( GetBackgroundBitmap().Ok() )
288 {
289 // get the bitmap and the flags
290 int alignment;
291 wxStretch stretch;
292 wxBitmap bmp = GetBackgroundBitmap(&alignment, &stretch);
293 wxControlRenderer::DrawBitmap(dc, bmp, rect, alignment, stretch);
294 }
295 else // just fill it with bg colour if no bitmap
296 {
297 m_renderer->DrawBackground(dc, wxTHEME_BG_COLOUR(this),
298 rect, GetStateFlags());
299 }
300
301 return TRUE;
302 }
303
304 void wxWindow::EraseBackground(wxDC& dc, const wxRect& rect)
305 {
306 // TODO: handle bg bitmaps here!
307
308 m_renderer->DrawBackground(dc, wxTHEME_BG_COLOUR(this), rect, GetStateFlags());
309 }
310
311 void wxWindow::DoDrawBorder(wxDC& dc, const wxRect& rect)
312 {
313 // draw outline unless the update region is enitrely inside it in which
314 // case we don't need to do it
315 #if 0 // doesn't seem to work, why?
316 if ( wxRegion(rect).Contains(GetUpdateRegion().GetBox()) != wxInRegion )
317 #endif
318 {
319 m_renderer->DrawBorder(dc, GetBorder(), rect, GetStateFlags());
320 }
321 }
322
323 void wxWindow::DoDraw(wxControlRenderer *renderer)
324 {
325 }
326
327 void wxWindow::Refresh(bool eraseBackground, const wxRect *rectClient)
328 {
329 wxRect rectWin;
330 wxPoint pt = GetClientAreaOrigin();
331
332 wxSize size = GetClientSize();
333
334 if ( rectClient )
335 {
336 rectWin = *rectClient;
337
338 // don't refresh anything beyond the client area (scrollbars for
339 // example)
340 if ( rectWin.GetRight() > size.x )
341 rectWin.SetRight(size.x);
342 if ( rectWin.GetBottom() > size.y )
343 rectWin.SetBottom(size.y);
344
345 rectWin.Offset(pt);
346 }
347 else // refresh the entire client area
348 {
349 rectWin.x = pt.x;
350 rectWin.y = pt.y;
351 rectWin.width = size.x;
352 rectWin.height = size.y;
353 }
354
355 // debugging helper
356 #ifdef WXDEBUG_REFRESH
357 static bool s_refreshDebug = FALSE;
358 if ( s_refreshDebug )
359 {
360 wxWindowDC dc(this);
361 dc.SetBrush(*wxCYAN_BRUSH);
362 dc.SetPen(*wxTRANSPARENT_PEN);
363 dc.DrawRectangle(rectWin);
364
365 // under Unix we use "--sync" X option for this
366 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
367 ::GdiFlush();
368 ::Sleep(200);
369 #endif // __WXMSW__
370 }
371 #endif // WXDEBUG_REFRESH
372
373 wxWindowNative::Refresh(eraseBackground, &rectWin);
374 }
375
376 // ----------------------------------------------------------------------------
377 // state flags
378 // ----------------------------------------------------------------------------
379
380 bool wxWindow::Enable(bool enable)
381 {
382 if ( !wxWindowNative::Enable(enable) )
383 return FALSE;
384
385 // disabled window can't keep focus
386 if ( FindFocus() == this && GetParent() != NULL )
387 {
388 GetParent()->SetFocus();
389 }
390
391 if ( m_renderer )
392 {
393 // a window with renderer is drawn by ourselves and it has to be
394 // refreshed to reflect its new status
395 Refresh();
396 }
397
398 return TRUE;
399 }
400
401 bool wxWindow::IsFocused() const
402 {
403 wxWindow *self = wxConstCast(this, wxWindow);
404 return self->FindFocus() == self;
405 }
406
407 bool wxWindow::IsPressed() const
408 {
409 return FALSE;
410 }
411
412 bool wxWindow::IsDefault() const
413 {
414 return FALSE;
415 }
416
417 bool wxWindow::IsCurrent() const
418 {
419 return m_isCurrent;
420 }
421
422 bool wxWindow::SetCurrent(bool doit)
423 {
424 if ( doit == m_isCurrent )
425 return FALSE;
426
427 m_isCurrent = doit;
428
429 if ( CanBeHighlighted() )
430 Refresh();
431
432 return TRUE;
433 }
434
435 int wxWindow::GetStateFlags() const
436 {
437 int flags = 0;
438 if ( !IsEnabled() )
439 flags |= wxCONTROL_DISABLED;
440
441 // the following states are only possible if our application is active - if
442 // it is not, even our default/focused controls shouldn't appear as such
443 if ( wxTheApp->IsActive() )
444 {
445 if ( IsCurrent() )
446 flags |= wxCONTROL_CURRENT;
447 if ( IsFocused() )
448 flags |= wxCONTROL_FOCUSED;
449 if ( IsPressed() )
450 flags |= wxCONTROL_PRESSED;
451 if ( IsDefault() )
452 flags |= wxCONTROL_ISDEFAULT;
453 }
454
455 return flags;
456 }
457
458 // ----------------------------------------------------------------------------
459 // size
460 // ----------------------------------------------------------------------------
461
462 void wxWindow::OnSize(wxSizeEvent& event)
463 {
464 event.Skip();
465
466 if ( m_scrollbarVert || m_scrollbarHorz )
467 {
468 PositionScrollbars();
469 }
470
471 #if 0 // ndef __WXMSW__
472 // Refresh the area (strip) previously occupied by the border
473
474 if (HasFlag( wxNO_FULL_REPAINT_ON_RESIZE ) && IsShown())
475 {
476 // This code assumes that wxSizeEvent.GetSize() returns
477 // the area of the entire window, not just the client
478 // area.
479 wxSize newSize = event.GetSize();
480
481 if (m_oldSize.x == -1 && m_oldSize.y == -1)
482 {
483 m_oldSize = newSize;
484 return;
485 }
486
487 if (HasFlag( wxSIMPLE_BORDER ))
488 {
489 if (newSize.y > m_oldSize.y)
490 {
491 wxRect rect;
492 rect.x = 0;
493 rect.width = m_oldSize.x;
494 rect.y = m_oldSize.y-2;
495 rect.height = 1;
496 Refresh( TRUE, &rect );
497 }
498 else if (newSize.y < m_oldSize.y)
499 {
500 wxRect rect;
501 rect.y = newSize.y;
502 rect.x = 0;
503 rect.height = 1;
504 rect.width = newSize.x;
505 wxWindowNative::Refresh( TRUE, &rect );
506 }
507
508 if (newSize.x > m_oldSize.x)
509 {
510 wxRect rect;
511 rect.y = 0;
512 rect.height = m_oldSize.y;
513 rect.x = m_oldSize.x-2;
514 rect.width = 1;
515 Refresh( TRUE, &rect );
516 }
517 else if (newSize.x < m_oldSize.x)
518 {
519 wxRect rect;
520 rect.x = newSize.x;
521 rect.y = 0;
522 rect.width = 1;
523 rect.height = newSize.y;
524 wxWindowNative::Refresh( TRUE, &rect );
525 }
526 }
527 else
528 if (HasFlag( wxSUNKEN_BORDER ) || HasFlag( wxRAISED_BORDER ))
529 {
530 if (newSize.y > m_oldSize.y)
531 {
532 wxRect rect;
533 rect.x = 0;
534 rect.width = m_oldSize.x;
535 rect.y = m_oldSize.y-4;
536 rect.height = 2;
537 Refresh( TRUE, &rect );
538 }
539 else if (newSize.y < m_oldSize.y)
540 {
541 wxRect rect;
542 rect.y = newSize.y;
543 rect.x = 0;
544 rect.height = 2;
545 rect.width = newSize.x;
546 wxWindowNative::Refresh( TRUE, &rect );
547 }
548
549 if (newSize.x > m_oldSize.x)
550 {
551 wxRect rect;
552 rect.y = 0;
553 rect.height = m_oldSize.y;
554 rect.x = m_oldSize.x-4;
555 rect.width = 2;
556 Refresh( TRUE, &rect );
557 }
558 else if (newSize.x < m_oldSize.x)
559 {
560 wxRect rect;
561 rect.x = newSize.x;
562 rect.y = 0;
563 rect.width = 2;
564 rect.height = newSize.y;
565 wxWindowNative::Refresh( TRUE, &rect );
566 }
567 }
568
569 m_oldSize = newSize;
570 }
571 #endif
572 }
573
574 wxSize wxWindow::DoGetBestSize() const
575 {
576 return AdjustSize(DoGetBestClientSize());
577 }
578
579 wxSize wxWindow::DoGetBestClientSize() const
580 {
581 return wxWindowNative::DoGetBestSize();
582 }
583
584 wxSize wxWindow::AdjustSize(const wxSize& size) const
585 {
586 wxSize sz = size;
587 if ( m_renderer )
588 m_renderer->AdjustSize(&sz, this);
589 return sz;
590 }
591
592 wxPoint wxWindow::GetClientAreaOrigin() const
593 {
594 wxPoint pt = wxWindowBase::GetClientAreaOrigin();
595
596 #if wxUSE_TWO_WINDOWS
597 #else
598 if ( m_renderer )
599 pt += m_renderer->GetBorderDimensions(GetBorder()).GetPosition();
600 #endif
601
602 return pt;
603 }
604
605 void wxWindow::DoGetClientSize(int *width, int *height) const
606 {
607 // if it is a native window, we assume it handles the scrollbars itself
608 // too - and if it doesn't, there is not much we can do
609 if ( !m_renderer )
610 {
611 wxWindowNative::DoGetClientSize(width, height);
612
613 return;
614 }
615
616 int w, h;
617 wxWindowNative::DoGetClientSize(&w, &h);
618
619 // we assume that the scrollbars are positioned correctly (by a previous
620 // call to PositionScrollbars()) here
621
622 wxRect rectBorder;
623 if ( m_renderer )
624 rectBorder = m_renderer->GetBorderDimensions(GetBorder());
625
626 bool inside = m_renderer->AreScrollbarsInsideBorder();
627
628 if ( width )
629 {
630 // in any case, take account of the scrollbar
631 if ( m_scrollbarVert )
632 w -= m_scrollbarVert->GetSize().x;
633
634 // if we don't have scrollbar or if it is outside the border (and not
635 // blended into it), take account of the right border as well
636 if ( !m_scrollbarVert || inside )
637 w -= rectBorder.width;
638
639 // and always account for the left border
640 *width = w - rectBorder.x;
641
642 // we shouldn't return invalid width
643 if ( *width < 0 )
644 *width = 0;
645 }
646
647 if ( height )
648 {
649 if ( m_scrollbarHorz )
650 h -= m_scrollbarHorz->GetSize().y;
651
652 if ( !m_scrollbarHorz || inside )
653 h -= rectBorder.height;
654
655 *height = h - rectBorder.y;
656
657 // we shouldn't return invalid height
658 if ( *height < 0 )
659 *height = 0;
660 }
661 }
662
663 void wxWindow::DoSetClientSize(int width, int height)
664 {
665 // take into account the borders
666 wxRect rectBorder = m_renderer->GetBorderDimensions(GetBorder());
667 width += rectBorder.x;
668 height += rectBorder.y;
669
670 // and the scrollbars (as they may be offset into the border, use the
671 // scrollbar position, not size - this supposes that PositionScrollbars()
672 // had been called before)
673 bool inside = m_renderer->AreScrollbarsInsideBorder();
674 wxSize size = GetSize();
675 if ( m_scrollbarVert )
676 width += size.x - m_scrollbarVert->GetPosition().x;
677 if ( !m_scrollbarVert || inside )
678 width += rectBorder.width;
679
680 if ( m_scrollbarHorz )
681 height += size.y - m_scrollbarHorz->GetPosition().y;
682 if ( !m_scrollbarHorz || inside )
683 height += rectBorder.height;
684
685 wxWindowNative::DoSetClientSize(width, height);
686 }
687
688 wxHitTest wxWindow::DoHitTest(wxCoord x, wxCoord y) const
689 {
690 wxHitTest ht = wxWindowNative::DoHitTest(x, y);
691 if ( ht == wxHT_WINDOW_INSIDE )
692 {
693 if ( m_scrollbarVert && x >= m_scrollbarVert->GetPosition().x )
694 {
695 // it can still be changed below because it may also be the corner
696 ht = wxHT_WINDOW_VERT_SCROLLBAR;
697 }
698
699 if ( m_scrollbarHorz && y >= m_scrollbarHorz->GetPosition().y )
700 {
701 ht = ht == wxHT_WINDOW_VERT_SCROLLBAR ? wxHT_WINDOW_CORNER
702 : wxHT_WINDOW_HORZ_SCROLLBAR;
703 }
704 }
705
706 return ht;
707 }
708
709 // ----------------------------------------------------------------------------
710 // scrolling: we implement it entirely ourselves except for ScrollWindow()
711 // function which is supposed to be (efficiently) implemented by the native
712 // window class
713 // ----------------------------------------------------------------------------
714
715 void wxWindow::RefreshScrollbars()
716 {
717 if ( m_scrollbarHorz )
718 m_scrollbarHorz->Refresh();
719
720 if ( m_scrollbarVert )
721 m_scrollbarVert->Refresh();
722 }
723
724 void wxWindow::PositionScrollbars()
725 {
726 // do not use GetClientSize/Rect as it relies on the scrollbars being
727 // correctly positioned
728
729 wxSize size = GetSize();
730 wxBorder border = GetBorder();
731 wxRect rectBorder = m_renderer->GetBorderDimensions(border);
732 bool inside = m_renderer->AreScrollbarsInsideBorder();
733
734 int height = m_scrollbarHorz ? m_scrollbarHorz->GetSize().y : 0;
735 int width = m_scrollbarVert ? m_scrollbarVert->GetSize().x : 0;
736
737 wxRect rectBar;
738 if ( m_scrollbarVert )
739 {
740 rectBar.x = size.x - width;
741 if ( inside )
742 rectBar.x -= rectBorder.width;
743 rectBar.width = width;
744 rectBar.y = 0;
745 if ( inside )
746 rectBar.y += rectBorder.y;
747 rectBar.height = size.y - height;
748 if ( inside )
749 rectBar.height -= rectBorder.y + rectBorder.height;
750
751 m_scrollbarVert->SetSize(rectBar, wxSIZE_NO_ADJUSTMENTS);
752 }
753
754 if ( m_scrollbarHorz )
755 {
756 rectBar.y = size.y - height;
757 if ( inside )
758 rectBar.y -= rectBorder.height;
759 rectBar.height = height;
760 rectBar.x = 0;
761 if ( inside )
762 rectBar.x += rectBorder.x;
763 rectBar.width = size.x - width;
764 if ( inside )
765 rectBar.width -= rectBorder.x + rectBorder.width;
766
767 m_scrollbarHorz->SetSize(rectBar, wxSIZE_NO_ADJUSTMENTS);
768 }
769
770 RefreshScrollbars();
771 }
772
773 void wxWindow::SetScrollbar(int orient,
774 int pos,
775 int pageSize,
776 int range,
777 bool refresh)
778 {
779 wxASSERT_MSG( pageSize <= range,
780 _T("page size can't be greater than range") );
781
782 bool hasClientSizeChanged = FALSE;
783 wxScrollBar *scrollbar = GetScrollbar(orient);
784 if ( range && (pageSize < range) )
785 {
786 if ( !scrollbar )
787 {
788 // create it
789 #if wxUSE_TWO_WINDOWS
790 SetInsertIntoMain( TRUE );
791 #endif
792 scrollbar = new wxScrollBar(this, -1,
793 wxDefaultPosition, wxDefaultSize,
794 orient & wxVERTICAL ? wxSB_VERTICAL
795 : wxSB_HORIZONTAL);
796 #if wxUSE_TWO_WINDOWS
797 SetInsertIntoMain( FALSE );
798 #endif
799 if ( orient & wxVERTICAL )
800 m_scrollbarVert = scrollbar;
801 else
802 m_scrollbarHorz = scrollbar;
803
804 // the client area diminished as we created a scrollbar
805 hasClientSizeChanged = TRUE;
806
807 PositionScrollbars();
808 }
809 else if ( GetWindowStyle() & wxALWAYS_SHOW_SB )
810 {
811 // we might have disabled it before
812 scrollbar->Enable();
813 }
814
815 scrollbar->SetScrollbar(pos, pageSize, range, pageSize, refresh);
816 }
817 else // no range means no scrollbar
818 {
819 if ( scrollbar )
820 {
821 // wxALWAYS_SHOW_SB only applies to the vertical scrollbar
822 if ( (orient & wxVERTICAL) && (GetWindowStyle() & wxALWAYS_SHOW_SB) )
823 {
824 // just disable the scrollbar
825 scrollbar->SetScrollbar(pos, pageSize, range, pageSize, refresh);
826 scrollbar->Disable();
827 }
828 else // really remove the scrollbar
829 {
830 delete scrollbar;
831
832 if ( orient & wxVERTICAL )
833 m_scrollbarVert = NULL;
834 else
835 m_scrollbarHorz = NULL;
836
837 // the client area increased as we removed a scrollbar
838 hasClientSizeChanged = TRUE;
839
840 // the size of the remaining scrollbar must be adjusted
841 if ( m_scrollbarHorz || m_scrollbarVert )
842 {
843 PositionScrollbars();
844 }
845 }
846 }
847 }
848
849 // give the window a chance to relayout
850 if ( hasClientSizeChanged )
851 {
852 #if wxUSE_TWO_WINDOWS
853 wxWindowNative::SetSize( GetSize() );
854 #else
855 wxSizeEvent event(GetSize());
856 (void)GetEventHandler()->ProcessEvent(event);
857 #endif
858 }
859 }
860
861 void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
862 {
863 wxScrollBar *scrollbar = GetScrollbar(orient);
864 wxCHECK_RET( scrollbar, _T("no scrollbar to set position for") );
865
866 scrollbar->SetThumbPosition(pos);
867
868 // VZ: I think we can safely ignore this as we always refresh it
869 // automatically whenever the value chanegs
870 #if 0
871 if ( refresh )
872 Refresh();
873 #endif
874 }
875
876 int wxWindow::GetScrollPos(int orient) const
877 {
878 wxScrollBar *scrollbar = GetScrollbar(orient);
879 return scrollbar ? scrollbar->GetThumbPosition() : 0;
880 }
881
882 int wxWindow::GetScrollThumb(int orient) const
883 {
884 wxScrollBar *scrollbar = GetScrollbar(orient);
885 return scrollbar ? scrollbar->GetThumbSize() : 0;
886 }
887
888 int wxWindow::GetScrollRange(int orient) const
889 {
890 wxScrollBar *scrollbar = GetScrollbar(orient);
891 return scrollbar ? scrollbar->GetRange() : 0;
892 }
893
894 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
895 {
896 // use native scrolling when available and do it in generic way
897 // otherwise:
898 #ifdef __WXX11__
899
900 wxWindowNative::ScrollWindow(dx, dy, rect);
901
902 #else
903
904 // before scrolling it, ensure that we don't have any unpainted areas
905 Update();
906
907 wxRect r;
908
909 if ( dx )
910 {
911 r = ScrollNoRefresh(dx, 0, rect);
912 Refresh(TRUE /* erase bkgnd */, &r);
913 }
914
915 if ( dy )
916 {
917 r = ScrollNoRefresh(0, dy, rect);
918 Refresh(TRUE /* erase bkgnd */, &r);
919 }
920
921 // scroll children accordingly:
922 wxPoint offset(dx, dy);
923
924 for (wxWindowList::Node *node = GetChildren().GetFirst();
925 node; node = node->GetNext())
926 {
927 wxWindow *child = node->GetData();
928 if ( child == m_scrollbarVert || child == m_scrollbarHorz )
929 continue;
930
931 // VS: Scrolling children has non-trivial semantics. If rect=NULL then
932 // it is easy: we scroll all children. Otherwise it gets
933 // complicated:
934 // 1. if scrolling in one direction only, scroll only
935 // those children that intersect shaft defined by the rectangle
936 // and scrolling direction
937 // 2. if scrolling in both axes, scroll all children
938
939 if ( rect && (dx * dy == 0 /* moving in only one of x, y axis */) )
940 {
941 wxRect childRect = child->GetRect();
942 if ( dx == 0 && (childRect.GetLeft() <= rect->GetRight() ||
943 childRect.GetRight() >= rect->GetLeft()) )
944 {
945 child->Move(child->GetPosition() + offset);
946 }
947 else if ( dy == 0 && (childRect.GetTop() <= rect->GetBottom() ||
948 childRect.GetBottom() >= rect->GetTop()) )
949 {
950 child->Move(child->GetPosition() + offset);
951 }
952 }
953 else
954 {
955 child->Move(child->GetPosition() + offset);
956 }
957 }
958 #endif
959 }
960
961 wxRect wxWindow::ScrollNoRefresh(int dx, int dy, const wxRect *rectTotal)
962 {
963 wxASSERT_MSG( !dx || !dy, _T("can't be used for diag scrolling") );
964
965 // the rect to refresh (which we will calculate)
966 wxRect rect;
967
968 if ( !dx && !dy )
969 {
970 // nothing to do
971 return rect;
972 }
973
974 // calculate the part of the window which we can just redraw in the new
975 // location
976 wxSize sizeTotal = rectTotal ? rectTotal->GetSize() : GetClientSize();
977
978 wxLogTrace(_T("scroll"), _T("rect is %dx%d, scroll by %d, %d"),
979 sizeTotal.x, sizeTotal.y, dx, dy);
980
981 // the initial and end point of the region we move in client coords
982 wxPoint ptSource, ptDest;
983 if ( rectTotal )
984 {
985 ptSource = rectTotal->GetPosition();
986 ptDest = rectTotal->GetPosition();
987 }
988
989 // the size of this region
990 wxSize size;
991 size.x = sizeTotal.x - abs(dx);
992 size.y = sizeTotal.y - abs(dy);
993 if ( size.x <= 0 || size.y <= 0 )
994 {
995 // just redraw everything as nothing of the displayed image will stay
996 wxLogTrace(_T("scroll"), _T("refreshing everything"));
997
998 rect = rectTotal ? *rectTotal : wxRect(0, 0, sizeTotal.x, sizeTotal.y);
999 }
1000 else // move the part which doesn't change to the new location
1001 {
1002 // note that when we scroll the canvas in some direction we move the
1003 // block which doesn't need to be refreshed in the opposite direction
1004
1005 if ( dx < 0 )
1006 {
1007 // scroll to the right, move to the left
1008 ptSource.x -= dx;
1009 }
1010 else
1011 {
1012 // scroll to the left, move to the right
1013 ptDest.x += dx;
1014 }
1015
1016 if ( dy < 0 )
1017 {
1018 // scroll down, move up
1019 ptSource.y -= dy;
1020 }
1021 else
1022 {
1023 // scroll up, move down
1024 ptDest.y += dy;
1025 }
1026
1027 #if wxUSE_CARET
1028 // we need to hide the caret before moving or it will erase itself at
1029 // the wrong (old) location
1030 wxCaret *caret = GetCaret();
1031 if ( caret )
1032 caret->Hide();
1033 #endif // wxUSE_CARET
1034
1035 // do move
1036 wxClientDC dc(this);
1037 wxBitmap bmp(size.x, size.y);
1038 wxMemoryDC dcMem;
1039 dcMem.SelectObject(bmp);
1040
1041 dcMem.Blit(wxPoint(0, 0), size, &dc, ptSource
1042 #if defined(__WXGTK__) && !defined(wxHAS_WORKING_GTK_DC_BLIT)
1043 + GetClientAreaOrigin()
1044 #endif // broken wxGTK wxDC::Blit
1045 );
1046 dc.Blit(ptDest, size, &dcMem, wxPoint(0, 0));
1047
1048 wxLogTrace(_T("scroll"),
1049 _T("Blit: (%d, %d) of size %dx%d -> (%d, %d)"),
1050 ptSource.x, ptSource.y,
1051 size.x, size.y,
1052 ptDest.x, ptDest.y);
1053
1054 // and now repaint the uncovered area
1055
1056 // FIXME: We repaint the intersection of these rectangles twice - is
1057 // it bad? I don't think so as it is rare to scroll the window
1058 // diagonally anyhow and so adding extra logic to compute
1059 // rectangle intersection is probably not worth the effort
1060
1061 rect.x = ptSource.x;
1062 rect.y = ptSource.y;
1063
1064 if ( dx )
1065 {
1066 if ( dx < 0 )
1067 {
1068 // refresh the area along the right border
1069 rect.x += size.x + dx;
1070 rect.width = -dx;
1071 }
1072 else
1073 {
1074 // refresh the area along the left border
1075 rect.width = dx;
1076 }
1077
1078 rect.height = sizeTotal.y;
1079
1080 wxLogTrace(_T("scroll"), _T("refreshing (%d, %d)-(%d, %d)"),
1081 rect.x, rect.y,
1082 rect.GetRight() + 1, rect.GetBottom() + 1);
1083 }
1084
1085 if ( dy )
1086 {
1087 if ( dy < 0 )
1088 {
1089 // refresh the area along the bottom border
1090 rect.y += size.y + dy;
1091 rect.height = -dy;
1092 }
1093 else
1094 {
1095 // refresh the area along the top border
1096 rect.height = dy;
1097 }
1098
1099 rect.width = sizeTotal.x;
1100
1101 wxLogTrace(_T("scroll"), _T("refreshing (%d, %d)-(%d, %d)"),
1102 rect.x, rect.y,
1103 rect.GetRight() + 1, rect.GetBottom() + 1);
1104 }
1105
1106 #if wxUSE_CARET
1107 if ( caret )
1108 caret->Show();
1109 #endif // wxUSE_CARET
1110 }
1111
1112 return rect;
1113 }
1114
1115 // ----------------------------------------------------------------------------
1116 // accelerators and menu hot keys
1117 // ----------------------------------------------------------------------------
1118
1119 #if wxUSE_MENUS
1120 // the last window over which Alt was pressed (used by OnKeyUp)
1121 wxWindow *wxWindow::ms_winLastAltPress = NULL;
1122 #endif // wxUSE_MENUS
1123
1124 #if wxUSE_ACCEL || wxUSE_MENUS
1125
1126 void wxWindow::OnKeyDown(wxKeyEvent& event)
1127 {
1128 #if wxUSE_MENUS
1129 int key = event.GetKeyCode();
1130 if ( !event.ControlDown() && (key == WXK_MENU || key == WXK_F10) )
1131 {
1132 ms_winLastAltPress = this;
1133
1134 // it can't be an accel anyhow
1135 return;
1136 }
1137
1138 ms_winLastAltPress = NULL;
1139 #endif // wxUSE_MENUS
1140
1141 #if wxUSE_ACCEL
1142 for ( wxWindow *win = this; win; win = win->GetParent() )
1143 {
1144 int command = win->GetAcceleratorTable()->GetCommand(event);
1145 if ( command != -1 )
1146 {
1147 wxCommandEvent eventCmd(wxEVT_COMMAND_MENU_SELECTED, command);
1148 if ( win->GetEventHandler()->ProcessEvent(eventCmd) )
1149 {
1150 // skip "event.Skip()" below
1151 return;
1152 }
1153 }
1154
1155 if ( win->IsTopLevel() )
1156 {
1157 // try the frame menu bar
1158 #if wxUSE_MENUS
1159 wxFrame *frame = wxDynamicCast(win, wxFrame);
1160 if ( frame )
1161 {
1162 wxMenuBar *menubar = frame->GetMenuBar();
1163 if ( menubar && menubar->ProcessAccelEvent(event) )
1164 {
1165 // skip "event.Skip()" below
1166 return;
1167 }
1168 }
1169 #endif // wxUSE_MENUS
1170
1171 // if it wasn't in a menu, try to find a button
1172 if ( command != -1 )
1173 {
1174 wxWindow* child = win->FindWindow(command);
1175 if ( child && wxDynamicCast(child, wxButton) )
1176 {
1177 wxCommandEvent eventCmd(wxEVT_COMMAND_BUTTON_CLICKED, command);
1178 eventCmd.SetEventObject(child);
1179 if ( child->GetEventHandler()->ProcessEvent(eventCmd) )
1180 {
1181 // skip "event.Skip()" below
1182 return;
1183 }
1184 }
1185 }
1186
1187 // don't propagate accels from the child frame to the parent one
1188 break;
1189 }
1190 }
1191 #endif // wxUSE_ACCEL
1192
1193 event.Skip();
1194 }
1195
1196 #endif // wxUSE_ACCEL
1197
1198 #if wxUSE_MENUS
1199
1200 wxMenuBar *wxWindow::GetParentFrameMenuBar() const
1201 {
1202 for ( const wxWindow *win = this; win; win = win->GetParent() )
1203 {
1204 if ( win->IsTopLevel() )
1205 {
1206 wxFrame *frame = wxDynamicCast(win, wxFrame);
1207 if ( frame )
1208 {
1209 return frame->GetMenuBar();
1210 }
1211
1212 // don't look further - we don't want to return the menubar of the
1213 // parent frame
1214 break;
1215 }
1216 }
1217
1218 return NULL;
1219 }
1220
1221 void wxWindow::OnChar(wxKeyEvent& event)
1222 {
1223 if ( event.AltDown() && !event.ControlDown() )
1224 {
1225 int key = event.GetKeyCode();
1226
1227 wxMenuBar *menubar = GetParentFrameMenuBar();
1228 if ( menubar )
1229 {
1230 int item = menubar->FindNextItemForAccel(-1, key);
1231 if ( item != -1 )
1232 {
1233 menubar->PopupMenu((size_t)item);
1234
1235 // skip "event.Skip()" below
1236 return;
1237 }
1238 }
1239 }
1240
1241 event.Skip();
1242 }
1243
1244 void wxWindow::OnKeyUp(wxKeyEvent& event)
1245 {
1246 int key = event.GetKeyCode();
1247 if ( !event.HasModifiers() && (key == WXK_MENU || key == WXK_F10) )
1248 {
1249 // only process Alt release specially if there were no other key
1250 // presses since Alt had been pressed and if both events happened in
1251 // the same window
1252 if ( ms_winLastAltPress == this )
1253 {
1254 wxMenuBar *menubar = GetParentFrameMenuBar();
1255 if ( menubar && this != menubar )
1256 {
1257 menubar->SelectMenu(0);
1258 }
1259 }
1260 }
1261 else
1262 {
1263 event.Skip();
1264 }
1265
1266 // in any case reset it
1267 ms_winLastAltPress = NULL;
1268 }
1269
1270 #endif // wxUSE_MENUS
1271
1272 // ----------------------------------------------------------------------------
1273 // MSW-specific section
1274 // ----------------------------------------------------------------------------
1275
1276 #ifdef __WXMSW__
1277
1278 #include "wx/msw/private.h"
1279
1280 long wxWindow::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
1281 {
1282 if ( message == WM_NCHITTEST )
1283 {
1284 // the windows which contain the other windows should let the mouse
1285 // events through, otherwise a window inside a static box would
1286 // never get any events at all
1287 if ( IsStaticBox() )
1288 {
1289 return HTTRANSPARENT;
1290 }
1291 }
1292
1293 return wxWindowNative::MSWWindowProc(message, wParam, lParam);
1294 }
1295
1296 #endif // __WXMSW__
1297