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