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