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