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