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