]> git.saurik.com Git - wxWidgets.git/blame - src/univ/winuniv.cpp
1. fixed wxGTK notebook which was completely broken by the merge
[wxWidgets.git] / src / univ / winuniv.cpp
CommitLineData
1e6feb95
VZ
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"
8cb172b4 39 #include "wx/frame.h"
1e6feb95
VZ
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
74BEGIN_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)
89END_EVENT_TABLE()
90
91// ----------------------------------------------------------------------------
92// creation
93// ----------------------------------------------------------------------------
94
95void 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
105bool 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
145void 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
154const 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
173void 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
206void 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
237void 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
255bool 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
287void 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
294void 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
306void wxWindow::DoDraw(wxControlRenderer *renderer)
307{
308}
309
310void 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
8cb172b4 349 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1e6feb95
VZ
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
363bool 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
384bool wxWindow::IsFocused() const
385{
386 wxWindow *self = wxConstCast(this, wxWindow);
387 return self->FindFocus() == self;
388}
389
390bool wxWindow::IsPressed() const
391{
392 return FALSE;
393}
394
395bool wxWindow::IsDefault() const
396{
397 return FALSE;
398}
399
400bool wxWindow::IsCurrent() const
401{
402 return m_isCurrent;
403}
404
405bool 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
418int 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
445void wxWindow::OnSize(wxSizeEvent& event)
446{
447 if ( m_scrollbarVert || m_scrollbarHorz )
448 {
449 PositionScrollbars();
450 }
451
452 event.Skip();
453}
454
455wxSize wxWindow::DoGetBestSize() const
456{
457 return AdjustSize(DoGetBestClientSize());
458}
459
460wxSize wxWindow::DoGetBestClientSize() const
461{
462 return wxWindowNative::DoGetBestSize();
463}
464
465wxSize 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
473wxPoint 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
483void wxWindow::DoGetClientSize(int *width, int *height) const
484{
485 int w, h;
486 wxWindowNative::DoGetClientSize(&w, &h);
487
488 // we assume that the scrollbars are positioned correctly (by a previous
489 // call to PositionScrollbars()) here
490
491 wxRect rectBorder;
492 if ( m_renderer )
493 rectBorder = m_renderer->GetBorderDimensions(GetBorder());
494
495 bool inside = m_renderer->AreScrollbarsInsideBorder();
496
497 if ( width )
498 {
499 // in any case, take account of the scrollbar
500 if ( m_scrollbarVert )
501 w -= m_scrollbarVert->GetSize().x;
502
503 // if we don't have scrollbar or if it is outside the border (and not
504 // blended into it), take account of the right border as well
505 if ( !m_scrollbarVert || inside )
506 w -= rectBorder.width;
507
508 // and always account for the left border
509 *width = w - rectBorder.x;
510
511 // we shouldn't return invalid width
512 if ( *width < 0 )
513 *width = 0;
514 }
515
516 if ( height )
517 {
518 if ( m_scrollbarHorz )
519 h -= m_scrollbarHorz->GetSize().y;
520
521 if ( !m_scrollbarHorz || inside )
522 h -= rectBorder.height;
523
524 *height = h - rectBorder.y;
525
526 // we shouldn't return invalid height
527 if ( *height < 0 )
528 *height = 0;
529 }
530}
531
532void wxWindow::DoSetClientSize(int width, int height)
533{
534 // take into account the borders
535 wxRect rectBorder = m_renderer->GetBorderDimensions(GetBorder());
536 width += rectBorder.x;
537 height += rectBorder.y;
538
539 // and the scrollbars (as they may be offset into the border, use the
540 // scrollbar position, not size - this supposes that PositionScrollbars()
541 // had been called before)
542 bool inside = m_renderer->AreScrollbarsInsideBorder();
543 wxSize size = GetSize();
544 if ( m_scrollbarVert )
545 width += size.x - m_scrollbarVert->GetPosition().x;
546 if ( !m_scrollbarVert || inside )
547 width += rectBorder.width;
548
549 if ( m_scrollbarHorz )
550 height += size.y - m_scrollbarHorz->GetPosition().y;
551 if ( !m_scrollbarHorz || inside )
552 height += rectBorder.height;
553
554 wxWindowNative::DoSetClientSize(width, height);
555}
556
557wxHitTest wxWindow::DoHitTest(wxCoord x, wxCoord y) const
558{
559 wxHitTest ht = wxWindowNative::DoHitTest(x, y);
560 if ( ht == wxHT_WINDOW_INSIDE )
561 {
562 if ( m_scrollbarVert && x >= m_scrollbarVert->GetPosition().x )
563 {
564 // it can still be changed below because it may also be the corner
565 ht = wxHT_WINDOW_VERT_SCROLLBAR;
566 }
567
568 if ( m_scrollbarHorz && y >= m_scrollbarHorz->GetPosition().y )
569 {
570 ht = ht == wxHT_WINDOW_VERT_SCROLLBAR ? wxHT_WINDOW_CORNER
571 : wxHT_WINDOW_HORZ_SCROLLBAR;
572 }
573 }
574
575 return ht;
576}
577
578// ----------------------------------------------------------------------------
579// scrolling: we implement it entirely ourselves except for ScrollWindow()
580// function which is supposed to be (efficiently) implemented by the native
581// window class
582// ----------------------------------------------------------------------------
583
584void wxWindow::RefreshScrollbars()
585{
586 if ( m_scrollbarHorz )
587 m_scrollbarHorz->Refresh();
588
589 if ( m_scrollbarVert )
590 m_scrollbarVert->Refresh();
591}
592
593void wxWindow::PositionScrollbars()
594{
595 // do not use GetClientSize/Rect as it relies on the scrollbars being
596 // correctly positioned
597
598 wxSize size = GetSize();
599 wxBorder border = GetBorder();
600 wxRect rectBorder = m_renderer->GetBorderDimensions(border);
601 bool inside = m_renderer->AreScrollbarsInsideBorder();
602
603 int height = m_scrollbarHorz ? m_scrollbarHorz->GetSize().y : 0;
604 int width = m_scrollbarVert ? m_scrollbarVert->GetSize().x : 0;
605
606 wxRect rectBar;
607 if ( m_scrollbarVert )
608 {
609 rectBar.x = size.x - width;
610 if ( inside )
611 rectBar.x -= rectBorder.width;
612 rectBar.width = width;
613 rectBar.y = 0;
614 if ( inside )
615 rectBar.y += rectBorder.y;
616 rectBar.height = size.y - height;
617 if ( inside )
618 rectBar.height -= rectBorder.y + rectBorder.height;
619
620 m_scrollbarVert->SetSize(rectBar, wxSIZE_NO_ADJUSTMENTS);
621 }
622
623 if ( m_scrollbarHorz )
624 {
625 rectBar.y = size.y - height;
626 if ( inside )
627 rectBar.y -= rectBorder.height;
628 rectBar.height = height;
629 rectBar.x = 0;
630 if ( inside )
631 rectBar.x += rectBorder.x;
632 rectBar.width = size.x - width;
633 if ( inside )
634 rectBar.width -= rectBorder.x + rectBorder.width;
635
636 m_scrollbarHorz->SetSize(rectBar, wxSIZE_NO_ADJUSTMENTS);
637 }
638
639 RefreshScrollbars();
640}
641
642void wxWindow::SetScrollbar(int orient,
643 int pos,
644 int pageSize,
645 int range,
646 bool refresh)
647{
648 bool hasClientSizeChanged = FALSE;
649 wxScrollBar *scrollbar = GetScrollbar(orient);
650 if ( range )
651 {
652 if ( !scrollbar )
653 {
654 // create it
655 scrollbar = new wxScrollBar(this, -1,
656 wxDefaultPosition, wxDefaultSize,
657 orient & wxVERTICAL ? wxSB_VERTICAL
658 : wxSB_HORIZONTAL);
659 if ( orient & wxVERTICAL )
660 m_scrollbarVert = scrollbar;
661 else
662 m_scrollbarHorz = scrollbar;
663
664 // the client area diminished as we created a scrollbar
665 hasClientSizeChanged = TRUE;
666
667 PositionScrollbars();
668 }
669 else if ( GetWindowStyle() & wxALWAYS_SHOW_SB )
670 {
671 // we might have disabled it before
672 scrollbar->Enable();
673 }
674
675 scrollbar->SetScrollbar(pos, pageSize, range, pageSize, refresh);
676 }
677 else // no range means no scrollbar
678 {
679 if ( scrollbar )
680 {
681 // wxALWAYS_SHOW_SB only applies to the vertical scrollbar
682 if ( (orient & wxVERTICAL) && (GetWindowStyle() & wxALWAYS_SHOW_SB) )
683 {
684 // just disable the scrollbar
685 scrollbar->SetScrollbar(pos, pageSize, range, pageSize, refresh);
686 scrollbar->Disable();
687 }
688 else // really remove the scrollbar
689 {
690 delete scrollbar;
691
692 if ( orient & wxVERTICAL )
693 m_scrollbarVert = NULL;
694 else
695 m_scrollbarHorz = NULL;
696
697 // the client area increased as we removed a scrollbar
698 hasClientSizeChanged = TRUE;
699
700 // the size of the remaining scrollbar must be adjusted
701 if ( m_scrollbarHorz || m_scrollbarVert )
702 {
703 PositionScrollbars();
704 }
705 }
706 }
707 }
708
709 // give the window a chance to relayout
710 if ( hasClientSizeChanged )
711 {
712 wxSizeEvent event(GetSize());
713 (void)GetEventHandler()->ProcessEvent(event);
714 }
715}
716
717void wxWindow::SetScrollPos(int orient, int pos, bool refresh)
718{
719 wxScrollBar *scrollbar = GetScrollbar(orient);
720 wxCHECK_RET( scrollbar, _T("no scrollbar to set position for") );
721
722 scrollbar->SetThumbPosition(pos);
723 if ( refresh )
724 Refresh();
725}
726
727int wxWindow::GetScrollPos(int orient) const
728{
729 wxScrollBar *scrollbar = GetScrollbar(orient);
730 return scrollbar ? scrollbar->GetThumbPosition() : 0;
731}
732
733int wxWindow::GetScrollThumb(int orient) const
734{
735 wxScrollBar *scrollbar = GetScrollbar(orient);
736 return scrollbar ? scrollbar->GetThumbSize() : 0;
737}
738
739int wxWindow::GetScrollRange(int orient) const
740{
741 wxScrollBar *scrollbar = GetScrollbar(orient);
742 return scrollbar ? scrollbar->GetRange() : 0;
743}
744
745void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
746{
747 // before scrolling it, ensure that we don't have any unpainted areas
748 Update();
749
750 wxRect r;
751
752 if ( dx )
753 {
754 r = ScrollNoRefresh(dx, 0, rect);
755 Refresh(TRUE /* erase bkgnd */, &r);
756 }
757
758 if ( dy )
759 {
760 r = ScrollNoRefresh(0, dy, rect);
761 Refresh(TRUE /* erase bkgnd */, &r);
762 }
763}
764
765wxRect wxWindow::ScrollNoRefresh(int dx, int dy, const wxRect *rectTotal)
766{
767 wxASSERT_MSG( !dx || !dy, _T("can't be used for diag scrolling") );
768
769 // the rect to refresh (which we will calculate)
770 wxRect rect;
771
772 if ( !dx && !dy )
773 {
774 // nothing to do
775 return rect;
776 }
777
778 // calculate the part of the window which we can just redraw in the new
779 // location
780 wxSize sizeTotal = rectTotal ? rectTotal->GetSize() : GetClientSize();
781
782 wxLogTrace(_T("scroll"), _T("rect is %dx%d, scroll by %d, %d"),
783 sizeTotal.x, sizeTotal.y, dx, dy);
784
785 // the initial and end point of the region we move in client coords
786 wxPoint ptSource, ptDest;
787 if ( rectTotal )
788 {
789 ptSource = rectTotal->GetPosition();
790 ptDest = rectTotal->GetPosition();
791 }
792
793 // the size of this region
794 wxSize size;
795 size.x = sizeTotal.x - abs(dx);
796 size.y = sizeTotal.y - abs(dy);
797 if ( size.x <= 0 || size.y <= 0 )
798 {
799 // just redraw everything as nothing of the displayed image will stay
800 wxLogTrace(_T("scroll"), _T("refreshing everything"));
801
802 rect = rectTotal ? *rectTotal : wxRect(0, 0, sizeTotal.x, sizeTotal.y);
803 }
804 else // move the part which doesn't change to the new location
805 {
806 // note that when we scroll the canvas in some direction we move the
807 // block which doesn't need to be refreshed in the opposite direction
808
809 if ( dx < 0 )
810 {
811 // scroll to the right, move to the left
812 ptSource.x -= dx;
813 }
814 else
815 {
816 // scroll to the left, move to the right
817 ptDest.x += dx;
818 }
819
820 if ( dy < 0 )
821 {
822 // scroll down, move up
823 ptSource.y -= dy;
824 }
825 else
826 {
827 // scroll up, move down
828 ptDest.y += dy;
829 }
830
831#if wxUSE_CARET
832 // we need to hide the caret before moving or it will erase itself at
833 // the wrong (old) location
834 wxCaret *caret = GetCaret();
835 if ( caret )
836 caret->Hide();
837#endif // wxUSE_CARET
838
839 // do move
840 wxClientDC dc(this);
841 wxBitmap bmp(size.x, size.y);
842 wxMemoryDC dcMem;
843 dcMem.SelectObject(bmp);
844
845 dcMem.Blit(wxPoint(0, 0), size, &dc, ptSource
846#if defined(__WXGTK__) && !defined(__WX_DC_BLIT_FIXED__)
847 + GetClientAreaOrigin()
848#endif // broken wxGTK wxDC::Blit
849 );
850 dc.Blit(ptDest, size, &dcMem, wxPoint(0, 0));
851
852 wxLogTrace(_T("scroll"),
853 _T("Blit: (%d, %d) of size %dx%d -> (%d, %d)"),
854 ptSource.x, ptSource.y,
855 size.x, size.y,
856 ptDest.x, ptDest.y);
857
858 // and now repaint the uncovered area
859
860 // FIXME: We repaint the intersection of these rectangles twice - is
861 // it bad? I don't think so as it is rare to scroll the window
862 // diagonally anyhow and so adding extra logic to compute
863 // rectangle intersection is probably not worth the effort
864
865 rect.x = ptSource.x;
866 rect.y = ptSource.y;
867
868 if ( dx )
869 {
870 if ( dx < 0 )
871 {
872 // refresh the area along the right border
873 rect.x += size.x + dx;
874 rect.width = -dx;
875 }
876 else
877 {
878 // refresh the area along the left border
879 rect.width = dx;
880 }
881
882 rect.height = sizeTotal.y;
883
884 wxLogTrace(_T("scroll"), _T("refreshing (%d, %d)-(%d, %d)"),
885 rect.x, rect.y,
886 rect.GetRight() + 1, rect.GetBottom() + 1);
887 }
888
889 if ( dy )
890 {
891 if ( dy < 0 )
892 {
893 // refresh the area along the bottom border
894 rect.y += size.y + dy;
895 rect.height = -dy;
896 }
897 else
898 {
899 // refresh the area along the top border
900 rect.height = dy;
901 }
902
903 rect.width = sizeTotal.x;
904
905 wxLogTrace(_T("scroll"), _T("refreshing (%d, %d)-(%d, %d)"),
906 rect.x, rect.y,
907 rect.GetRight() + 1, rect.GetBottom() + 1);
908 }
909
910#if wxUSE_CARET
911 if ( caret )
912 caret->Show();
913#endif // wxUSE_CARET
914 }
915
916 return rect;
917}
918
919// ----------------------------------------------------------------------------
920// colours/fonts
921// ----------------------------------------------------------------------------
922
923bool wxWindow::SetBackgroundColour(const wxColour& colour)
924{
925 if ( !wxWindowNative::SetBackgroundColour(colour) )
926 return FALSE;
927
928 m_hasBgCol = TRUE;
929
930 return TRUE;
931}
932
933bool wxWindow::SetForegroundColour(const wxColour& colour)
934{
935 if ( !wxWindowNative::SetForegroundColour(colour) )
936 return FALSE;
937
938 m_hasFgCol = TRUE;
939
940 return TRUE;
941}
942
943bool wxWindow::SetFont(const wxFont& font)
944{
945 if ( !wxWindowNative::SetFont(font) )
946 return FALSE;
947
948 m_hasFont = TRUE;
949
950 return TRUE;
951}
952
953// ----------------------------------------------------------------------------
954// mouse capture
955// ----------------------------------------------------------------------------
956
957struct WXDLLEXPORT wxWindowNext
958{
959 wxWindow *win;
960 wxWindowNext *next;
961} *wxWindow::ms_winCaptureNext = NULL;
962
963void wxWindow::CaptureMouse()
964{
965 wxWindow *winOld = GetCapture();
966 if ( winOld )
967 {
968 // save it on stack
969 wxWindowNext *item = new wxWindowNext;
970 item->win = winOld;
971 item->next = ms_winCaptureNext;
972 ms_winCaptureNext = item;
973 }
974 //else: no mouse capture to save
975
976 wxWindowNative::CaptureMouse();
977}
978
979void wxWindow::ReleaseMouse()
980{
981 wxWindowNative::ReleaseMouse();
982
983 if ( ms_winCaptureNext )
984 {
985 ms_winCaptureNext->win->CaptureMouse();
986
987 wxWindowNext *item = ms_winCaptureNext;
988 ms_winCaptureNext = item->next;
989 delete item;
990 }
991 //else: stack is empty, no previous capture
992}
993
994// ----------------------------------------------------------------------------
995// accelerators and menu hot keys
996// ----------------------------------------------------------------------------
997
998#if wxUSE_MENUS
999 // the last window over which Alt was pressed (used by OnKeyUp)
1000 wxWindow *wxWindow::ms_winLastAltPress = NULL;
1001#endif // wxUSE_MENUS
1002
1003#if wxUSE_ACCEL || wxUSE_MENUS
1004
1005void wxWindow::OnKeyDown(wxKeyEvent& event)
1006{
1007#if wxUSE_MENUS
1008 int key = event.GetKeyCode();
1009 if ( !event.ControlDown() && (key == WXK_MENU || key == WXK_F10) )
1010 {
1011 ms_winLastAltPress = this;
1012
1013 // it can't be an accel anyhow
1014 return;
1015 }
1016
1017 ms_winLastAltPress = NULL;
1018#endif // wxUSE_MENUS
1019
1020#if wxUSE_ACCEL
1021 for ( wxWindow *win = this; win; win = win->GetParent() )
1022 {
1023 int command = win->GetAcceleratorTable()->GetCommand(event);
1024 if ( command != -1 )
1025 {
1026 wxCommandEvent eventCmd(wxEVT_COMMAND_MENU_SELECTED, command);
1027 if ( win->GetEventHandler()->ProcessEvent(eventCmd) )
1028 {
1029 // skip "event.Skip()" below
1030 return;
1031 }
1032 }
1033
1034 if ( win->IsTopLevel() )
1035 {
1036 // try the frame menu bar
1037#if wxUSE_MENUS
1038 wxFrame *frame = wxDynamicCast(win, wxFrame);
1039 if ( frame )
1040 {
1041 wxMenuBar *menubar = frame->GetMenuBar();
1042 if ( menubar && menubar->ProcessAccelEvent(event) )
1043 {
1044 // skip "event.Skip()" below
1045 return;
1046 }
1047 }
1048#endif // wxUSE_MENUS
1049
1050 // don't propagate accels from the child frame to the parent one
1051 break;
1052 }
1053 }
1054#endif // wxUSE_ACCEL
1055
1056 event.Skip();
1057}
1058
1059#endif // wxUSE_ACCEL
1060
1061#if wxUSE_MENUS
1062
1063wxMenuBar *wxWindow::GetParentFrameMenuBar() const
1064{
1065 for ( const wxWindow *win = this; win; win = win->GetParent() )
1066 {
1067 if ( win->IsTopLevel() )
1068 {
1069 wxFrame *frame = wxDynamicCast(win, wxFrame);
1070 if ( frame )
1071 {
1072 return frame->GetMenuBar();
1073 }
1074
1075 // don't look further - we don't want to return the menubar of the
1076 // parent frame
1077 break;
1078 }
1079 }
1080
1081 return NULL;
1082}
1083
1084void wxWindow::OnChar(wxKeyEvent& event)
1085{
1086 if ( event.AltDown() && !event.ControlDown() )
1087 {
1088 int key = event.GetKeyCode();
1089
1090 wxMenuBar *menubar = GetParentFrameMenuBar();
1091 if ( menubar )
1092 {
1093 int item = menubar->FindNextItemForAccel(-1, key);
1094 if ( item != -1 )
1095 {
1096 menubar->PopupMenu((size_t)item);
1097
1098 // skip "event.Skip()" below
1099 return;
1100 }
1101 }
1102 }
1103
1104 event.Skip();
1105}
1106
1107void wxWindow::OnKeyUp(wxKeyEvent& event)
1108{
1109 int key = event.GetKeyCode();
1110 if ( !event.HasModifiers() && (key == WXK_MENU || key == WXK_F10) )
1111 {
1112 // only process Alt release specially if there were no other key
1113 // presses since Alt had been pressed and if both events happened in
1114 // the same window
1115 if ( ms_winLastAltPress == this )
1116 {
1117 wxMenuBar *menubar = GetParentFrameMenuBar();
1118 if ( menubar && this != menubar )
1119 {
1120 menubar->SelectMenu(0);
1121 }
1122 }
1123 }
1124 else
1125 {
1126 event.Skip();
1127 }
1128
1129 // in any case reset it
1130 ms_winLastAltPress = NULL;
1131}
1132
1133#endif // wxUSE_MENUS
1134