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