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