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