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