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