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