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