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