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