]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: univ/window.cpp | |
3 | // Purpose: implementation of extra wxWindow methods for wxUniv port | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 06.08.00 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com) | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // =========================================================================== | |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "univwindow.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/app.h" | |
33 | #include "wx/window.h" | |
34 | #include "wx/dcclient.h" | |
35 | #include "wx/dcmemory.h" | |
36 | #include "wx/event.h" | |
37 | #include "wx/scrolbar.h" | |
38 | #include "wx/menu.h" | |
39 | #include "wx/frame.h" | |
40 | #endif // WX_PRECOMP | |
41 | ||
42 | #include "wx/univ/colschem.h" | |
43 | #include "wx/univ/renderer.h" | |
44 | #include "wx/univ/theme.h" | |
45 | ||
46 | #if wxUSE_CARET | |
47 | #include "wx/caret.h" | |
48 | #endif // wxUSE_CARET | |
49 | ||
50 | // turn Refresh() debugging on/off | |
51 | #define WXDEBUG_REFRESH | |
52 | ||
53 | #ifndef __WXDEBUG__ | |
54 | #undef WXDEBUG_REFRESH | |
55 | #endif | |
56 | ||
57 | #if defined(WXDEBUG_REFRESH) && defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
58 | #include "wx/msw/private.h" | |
59 | #endif | |
60 | ||
61 | // ============================================================================ | |
62 | // implementation | |
63 | // ============================================================================ | |
64 | ||
65 | // ---------------------------------------------------------------------------- | |
66 | // event tables | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | // we can't use wxWindowNative here as it won't be expanded inside the macro | |
70 | #if defined(__WXMSW__) | |
71 | IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowMSW) | |
72 | #elif defined(__WXGTK__) | |
73 | IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowGTK) | |
74 | #elif defined(__WXMGL__) | |
75 | IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowMGL) | |
76 | #endif | |
77 | ||
78 | BEGIN_EVENT_TABLE(wxWindow, wxWindowNative) | |
79 | EVT_SIZE(wxWindow::OnSize) | |
80 | ||
81 | #if wxUSE_ACCEL || wxUSE_MENUS | |
82 | EVT_KEY_DOWN(wxWindow::OnKeyDown) | |
83 | #endif // wxUSE_ACCEL | |
84 | ||
85 | #if wxUSE_MENUS | |
86 | EVT_CHAR(wxWindow::OnChar) | |
87 | EVT_KEY_UP(wxWindow::OnKeyUp) | |
88 | #endif // wxUSE_MENUS | |
89 | ||
90 | EVT_PAINT(wxWindow::OnPaint) | |
91 | EVT_NC_PAINT(wxWindow::OnNcPaint) | |
92 | EVT_ERASE_BACKGROUND(wxWindow::OnErase) | |
93 | END_EVENT_TABLE() | |
94 | ||
95 | // ---------------------------------------------------------------------------- | |
96 | // creation | |
97 | // ---------------------------------------------------------------------------- | |
98 | ||
99 | void wxWindow::Init() | |
100 | { | |
101 | m_scrollbarVert = | |
102 | m_scrollbarHorz = (wxScrollBar *)NULL; | |
103 | ||
104 | m_isCurrent = FALSE; | |
105 | ||
106 | m_renderer = wxTheme::Get()->GetRenderer(); | |
107 | } | |
108 | ||
109 | bool wxWindow::Create(wxWindow *parent, | |
110 | wxWindowID id, | |
111 | const wxPoint& pos, | |
112 | const wxSize& size, | |
113 | long style, | |
114 | const wxString& name) | |
115 | { | |
116 | // we add wxCLIP_CHILDREN to get the same ("natural") behaviour under MSW | |
117 | // as under the other platforms | |
118 | if ( !wxWindowNative::Create(parent, id, pos, size, | |
119 | style | wxCLIP_CHILDREN, | |
120 | name) ) | |
121 | { | |
122 | return FALSE; | |
123 | } | |
124 | ||
125 | // if we should always have the scrollbar, do show it | |
126 | if ( GetWindowStyle() & wxALWAYS_SHOW_SB ) | |
127 | { | |
128 | m_scrollbarVert = new wxScrollBar(this, -1, | |
129 | wxDefaultPosition, wxDefaultSize, | |
130 | wxSB_VERTICAL); | |
131 | ||
132 | // and position it | |
133 | PositionScrollbars(); | |
134 | } | |
135 | ||
136 | // the colours/fonts are default | |
137 | m_hasBgCol = | |
138 | m_hasFgCol = | |
139 | m_hasFont = FALSE; | |
140 | ||
141 | return TRUE; | |
142 | } | |
143 | ||
144 | // ---------------------------------------------------------------------------- | |
145 | // background pixmap | |
146 | // ---------------------------------------------------------------------------- | |
147 | ||
148 | void wxWindow::SetBackground(const wxBitmap& bitmap, | |
149 | int alignment, | |
150 | wxStretch stretch) | |
151 | { | |
152 | m_bitmapBg = bitmap; | |
153 | m_alignBgBitmap = alignment; | |
154 | m_stretchBgBitmap = stretch; | |
155 | } | |
156 | ||
157 | const wxBitmap& wxWindow::GetBackgroundBitmap(int *alignment, | |
158 | wxStretch *stretch) const | |
159 | { | |
160 | if ( m_bitmapBg.Ok() ) | |
161 | { | |
162 | if ( alignment ) | |
163 | *alignment = m_alignBgBitmap; | |
164 | if ( stretch ) | |
165 | *stretch = m_stretchBgBitmap; | |
166 | } | |
167 | ||
168 | return m_bitmapBg; | |
169 | } | |
170 | ||
171 | // ---------------------------------------------------------------------------- | |
172 | // painting | |
173 | // ---------------------------------------------------------------------------- | |
174 | ||
175 | // the event handler executed when the window background must be painted | |
176 | void wxWindow::OnErase(wxEraseEvent& event) | |
177 | { | |
178 | if ( !m_renderer ) | |
179 | { | |
180 | event.Skip(); | |
181 | ||
182 | return; | |
183 | } | |
184 | ||
185 | DoDrawBackground(*event.GetDC()); | |
186 | ||
187 | // if we have both scrollbars, we also have a square in the corner between | |
188 | // them which we must paint | |
189 | if ( m_scrollbarVert && m_scrollbarHorz ) | |
190 | { | |
191 | wxSize size = GetSize(); | |
192 | wxRect rectClient = GetClientRect(), | |
193 | rectBorder = m_renderer->GetBorderDimensions(GetBorder()); | |
194 | ||
195 | wxRect rectCorner; | |
196 | rectCorner.x = rectClient.GetRight() + 1; | |
197 | rectCorner.y = rectClient.GetBottom() + 1; | |
198 | rectCorner.SetRight(size.x - rectBorder.width); | |
199 | rectCorner.SetBottom(size.y - rectBorder.height); | |
200 | ||
201 | if ( GetUpdateRegion().Contains(rectCorner) ) | |
202 | { | |
203 | m_renderer->DrawScrollCorner(*event.GetDC(), rectCorner); | |
204 | } | |
205 | } | |
206 | } | |
207 | ||
208 | // the event handlers executed when the window must be repainted | |
209 | void wxWindow::OnNcPaint(wxPaintEvent& event) | |
210 | { | |
211 | if ( m_renderer ) | |
212 | { | |
213 | // get the window rect | |
214 | wxRect rect; | |
215 | wxSize size = GetSize(); | |
216 | rect.x = | |
217 | rect.y = 0; | |
218 | rect.width = size.x; | |
219 | rect.height = size.y; | |
220 | ||
221 | // if the scrollbars are outside the border, we must adjust the rect to | |
222 | // exclude them | |
223 | if ( !m_renderer->AreScrollbarsInsideBorder() ) | |
224 | { | |
225 | wxScrollBar *scrollbar = GetScrollbar(wxVERTICAL); | |
226 | if ( scrollbar ) | |
227 | rect.width -= scrollbar->GetSize().x; | |
228 | ||
229 | scrollbar = GetScrollbar(wxHORIZONTAL); | |
230 | if ( scrollbar ) | |
231 | rect.height -= scrollbar->GetSize().y; | |
232 | } | |
233 | ||
234 | // get the DC and draw the border on it | |
235 | wxWindowDC dc(this); | |
236 | DoDrawBorder(dc, rect); | |
237 | } | |
238 | } | |
239 | ||
240 | void wxWindow::OnPaint(wxPaintEvent& event) | |
241 | { | |
242 | if ( !m_renderer ) | |
243 | { | |
244 | // it is a native control which paints itself | |
245 | event.Skip(); | |
246 | } | |
247 | else | |
248 | { | |
249 | // get the DC to use and create renderer on it | |
250 | wxPaintDC dc(this); | |
251 | wxControlRenderer renderer(this, dc, m_renderer); | |
252 | ||
253 | // draw the control | |
254 | DoDraw(&renderer); | |
255 | } | |
256 | } | |
257 | ||
258 | bool wxWindow::DoDrawBackground(wxDC& dc) | |
259 | { | |
260 | // FIXME: leaving this code in leads to partial bg redraws sometimes under | |
261 | // MSW | |
262 | wxRect rect; | |
263 | #ifndef __WXMSW__ | |
264 | rect = GetUpdateRegion().GetBox(); | |
265 | if ( !rect.width && !rect.height ) | |
266 | #endif | |
267 | { | |
268 | wxSize size = GetSize(); | |
269 | rect.width = size.x; | |
270 | rect.height = size.y; | |
271 | } | |
272 | ||
273 | if ( GetBackgroundBitmap().Ok() ) | |
274 | { | |
275 | // get the bitmap and the flags | |
276 | int alignment; | |
277 | wxStretch stretch; | |
278 | wxBitmap bmp = GetBackgroundBitmap(&alignment, &stretch); | |
279 | wxControlRenderer::DrawBitmap(dc, bmp, rect, alignment, stretch); | |
280 | } | |
281 | else // just fill it with bg colour if no bitmap | |
282 | { | |
283 | m_renderer->DrawBackground(dc, wxTHEME_BG_COLOUR(this), | |
284 | rect, GetStateFlags()); | |
285 | } | |
286 | ||
287 | return TRUE; | |
288 | } | |
289 | ||
290 | void wxWindow::EraseBackground(wxDC& dc, const wxRect& rect) | |
291 | { | |
292 | // TODO: handle bg bitmaps here! | |
293 | ||
294 | m_renderer->DrawBackground(dc, wxTHEME_BG_COLOUR(this), rect, GetStateFlags()); | |
295 | } | |
296 | ||
297 | void wxWindow::DoDrawBorder(wxDC& dc, const wxRect& rect) | |
298 | { | |
299 | // draw outline unless the update region is enitrely inside it in which | |
300 | // case we don't need to do it | |
301 | #if 0 // doesn't seem to work, why? | |
302 | if ( wxRegion(rect).Contains(GetUpdateRegion().GetBox()) != wxInRegion ) | |
303 | #endif | |
304 | { | |
305 | m_renderer->DrawBorder(dc, GetBorder(), rect, GetStateFlags()); | |
306 | } | |
307 | } | |
308 | ||
309 | void wxWindow::DoDraw(wxControlRenderer *renderer) | |
310 | { | |
311 | } | |
312 | ||
313 | void wxWindow::Refresh(bool eraseBackground, const wxRect *rectClient) | |
314 | { | |
315 | wxRect rectWin; | |
316 | wxPoint pt = GetClientAreaOrigin(); | |
317 | ||
318 | wxSize size = GetClientSize(); | |
319 | ||
320 | if ( rectClient ) | |
321 | { | |
322 | rectWin = *rectClient; | |
323 | ||
324 | // don't refresh anything beyond the client area (scrollbars for | |
325 | // example) | |
326 | if ( rectWin.GetRight() > size.x ) | |
327 | rectWin.SetRight(size.x); | |
328 | if ( rectWin.GetBottom() > size.y ) | |
329 | rectWin.SetBottom(size.y); | |
330 | ||
331 | rectWin.Offset(pt); | |
332 | } | |
333 | else // refresh the entire client area | |
334 | { | |
335 | rectWin.x = pt.x; | |
336 | rectWin.y = pt.y; | |
337 | rectWin.width = size.x; | |
338 | rectWin.height = size.y; | |
339 | } | |
340 | ||
341 | // debugging helper | |
342 | #ifdef WXDEBUG_REFRESH | |
343 | static bool s_refreshDebug = FALSE; | |
344 | if ( s_refreshDebug ) | |
345 | { | |
346 | wxWindowDC dc(this); | |
347 | dc.SetBrush(*wxCYAN_BRUSH); | |
348 | dc.SetPen(*wxTRANSPARENT_PEN); | |
349 | dc.DrawRectangle(rectWin); | |
350 | ||
351 | // under Unix we use "--sync" X option for this | |
352 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
353 | ::GdiFlush(); | |
354 | ::Sleep(200); | |
355 | #endif // __WXMSW__ | |
356 | } | |
357 | #endif // WXDEBUG_REFRESH | |
358 | ||
359 | wxWindowNative::Refresh(eraseBackground, &rectWin); | |
360 | } | |
361 | ||
362 | // ---------------------------------------------------------------------------- | |
363 | // state flags | |
364 | // ---------------------------------------------------------------------------- | |
365 | ||
366 | bool wxWindow::Enable(bool enable) | |
367 | { | |
368 | if ( !wxWindowNative::Enable(enable) ) | |
369 | return FALSE; | |
370 | ||
371 | // disabled window can't keep focus | |
372 | if ( FindFocus() == this ) | |
373 | { | |
374 | GetParent()->SetFocus(); | |
375 | } | |
376 | ||
377 | if ( m_renderer ) | |
378 | { | |
379 | // a window with renderer is drawn by ourselves and it has to be | |
380 | // refreshed to reflect its new status | |
381 | Refresh(); | |
382 | } | |
383 | ||
384 | return TRUE; | |
385 | } | |
386 | ||
387 | bool wxWindow::IsFocused() const | |
388 | { | |
389 | wxWindow *self = wxConstCast(this, wxWindow); | |
390 | return self->FindFocus() == self; | |
391 | } | |
392 | ||
393 | bool wxWindow::IsPressed() const | |
394 | { | |
395 | return FALSE; | |
396 | } | |
397 | ||
398 | bool wxWindow::IsDefault() const | |
399 | { | |
400 | return FALSE; | |
401 | } | |
402 | ||
403 | bool wxWindow::IsCurrent() const | |
404 | { | |
405 | return m_isCurrent; | |
406 | } | |
407 | ||
408 | bool wxWindow::SetCurrent(bool doit) | |
409 | { | |
410 | if ( doit == m_isCurrent ) | |
411 | return FALSE; | |
412 | ||
413 | m_isCurrent = doit; | |
414 | ||
415 | if ( CanBeHighlighted() ) | |
416 | Refresh(); | |
417 | ||
418 | return TRUE; | |
419 | } | |
420 | ||
421 | int wxWindow::GetStateFlags() const | |
422 | { | |
423 | int flags = 0; | |
424 | if ( !IsEnabled() ) | |
425 | flags |= wxCONTROL_DISABLED; | |
426 | ||
427 | // the following states are only possible if our application is active - if | |
428 | // it is not, even our default/focused controls shouldn't appear as such | |
429 | if ( wxTheApp->IsActive() ) | |
430 | { | |
431 | if ( IsCurrent() ) | |
432 | flags |= wxCONTROL_CURRENT; | |
433 | if ( IsFocused() ) | |
434 | flags |= wxCONTROL_FOCUSED; | |
435 | if ( IsPressed() ) | |
436 | flags |= wxCONTROL_PRESSED; | |
437 | if ( IsDefault() ) | |
438 | flags |= wxCONTROL_ISDEFAULT; | |
439 | } | |
440 | ||
441 | return flags; | |
442 | } | |
443 | ||
444 | // ---------------------------------------------------------------------------- | |
445 | // size | |
446 | // ---------------------------------------------------------------------------- | |
447 | ||
448 | void wxWindow::OnSize(wxSizeEvent& event) | |
449 | { | |
450 | if ( m_scrollbarVert || m_scrollbarHorz ) | |
451 | { | |
452 | PositionScrollbars(); | |
453 | } | |
454 | ||
455 | event.Skip(); | |
456 | } | |
457 | ||
458 | wxSize wxWindow::DoGetBestSize() const | |
459 | { | |
460 | return AdjustSize(DoGetBestClientSize()); | |
461 | } | |
462 | ||
463 | wxSize wxWindow::DoGetBestClientSize() const | |
464 | { | |
465 | return wxWindowNative::DoGetBestSize(); | |
466 | } | |
467 | ||
468 | wxSize wxWindow::AdjustSize(const wxSize& size) const | |
469 | { | |
470 | wxSize sz = size; | |
471 | if ( m_renderer ) | |
472 | m_renderer->AdjustSize(&sz, this); | |
473 | return sz; | |
474 | } | |
475 | ||
476 | wxPoint wxWindow::GetClientAreaOrigin() const | |
477 | { | |
478 | wxPoint pt = wxWindowBase::GetClientAreaOrigin(); | |
479 | ||
480 | if ( m_renderer ) | |
481 | pt += m_renderer->GetBorderDimensions(GetBorder()).GetPosition(); | |
482 | ||
483 | return pt; | |
484 | } | |
485 | ||
486 | void wxWindow::DoGetClientSize(int *width, int *height) const | |
487 | { | |
488 | // if it is a native window, we assume it handles the scrollbars itself | |
489 | // too - and if it doesn't, there is not much we can do | |
490 | if ( !m_renderer ) | |
491 | { | |
492 | wxWindowNative::DoGetClientSize(width, height); | |
493 | ||
494 | return; | |
495 | } | |
496 | ||
497 | int w, h; | |
498 | wxWindowNative::DoGetClientSize(&w, &h); | |
499 | ||
500 | // we assume that the scrollbars are positioned correctly (by a previous | |
501 | // call to PositionScrollbars()) here | |
502 | ||
503 | wxRect rectBorder; | |
504 | if ( m_renderer ) | |
505 | rectBorder = m_renderer->GetBorderDimensions(GetBorder()); | |
506 | ||
507 | bool inside = m_renderer->AreScrollbarsInsideBorder(); | |
508 | ||
509 | if ( width ) | |
510 | { | |
511 | // in any case, take account of the scrollbar | |
512 | if ( m_scrollbarVert ) | |
513 | w -= m_scrollbarVert->GetSize().x; | |
514 | ||
515 | // if we don't have scrollbar or if it is outside the border (and not | |
516 | // blended into it), take account of the right border as well | |
517 | if ( !m_scrollbarVert || inside ) | |
518 | w -= rectBorder.width; | |
519 | ||
520 | // and always account for the left border | |
521 | *width = w - rectBorder.x; | |
522 | ||
523 | // we shouldn't return invalid width | |
524 | if ( *width < 0 ) | |
525 | *width = 0; | |
526 | } | |
527 | ||
528 | if ( height ) | |
529 | { | |
530 | if ( m_scrollbarHorz ) | |
531 | h -= m_scrollbarHorz->GetSize().y; | |
532 | ||
533 | if ( !m_scrollbarHorz || inside ) | |
534 | h -= rectBorder.height; | |
535 | ||
536 | *height = h - rectBorder.y; | |
537 | ||
538 | // we shouldn't return invalid height | |
539 | if ( *height < 0 ) | |
540 | *height = 0; | |
541 | } | |
542 | } | |
543 | ||
544 | void wxWindow::DoSetClientSize(int width, int height) | |
545 | { | |
546 | // take into account the borders | |
547 | wxRect rectBorder = m_renderer->GetBorderDimensions(GetBorder()); | |
548 | width += rectBorder.x; | |
549 | height += rectBorder.y; | |
550 | ||
551 | // and the scrollbars (as they may be offset into the border, use the | |
552 | // scrollbar position, not size - this supposes that PositionScrollbars() | |
553 | // had been called before) | |
554 | bool inside = m_renderer->AreScrollbarsInsideBorder(); | |
555 | wxSize size = GetSize(); | |
556 | if ( m_scrollbarVert ) | |
557 | width += size.x - m_scrollbarVert->GetPosition().x; | |
558 | if ( !m_scrollbarVert || inside ) | |
559 | width += rectBorder.width; | |
560 | ||
561 | if ( m_scrollbarHorz ) | |
562 | height += size.y - m_scrollbarHorz->GetPosition().y; | |
563 | if ( !m_scrollbarHorz || inside ) | |
564 | height += rectBorder.height; | |
565 | ||
566 | wxWindowNative::DoSetClientSize(width, height); | |
567 | } | |
568 | ||
569 | wxHitTest wxWindow::DoHitTest(wxCoord x, wxCoord y) const | |
570 | { | |
571 | wxHitTest ht = wxWindowNative::DoHitTest(x, y); | |
572 | if ( ht == wxHT_WINDOW_INSIDE ) | |
573 | { | |
574 | if ( m_scrollbarVert && x >= m_scrollbarVert->GetPosition().x ) | |
575 | { | |
576 | // it can still be changed below because it may also be the corner | |
577 | ht = wxHT_WINDOW_VERT_SCROLLBAR; | |
578 | } | |
579 | ||
580 | if ( m_scrollbarHorz && y >= m_scrollbarHorz->GetPosition().y ) | |
581 | { | |
582 | ht = ht == wxHT_WINDOW_VERT_SCROLLBAR ? wxHT_WINDOW_CORNER | |
583 | : wxHT_WINDOW_HORZ_SCROLLBAR; | |
584 | } | |
585 | } | |
586 | ||
587 | return ht; | |
588 | } | |
589 | ||
590 | // ---------------------------------------------------------------------------- | |
591 | // scrolling: we implement it entirely ourselves except for ScrollWindow() | |
592 | // function which is supposed to be (efficiently) implemented by the native | |
593 | // window class | |
594 | // ---------------------------------------------------------------------------- | |
595 | ||
596 | void wxWindow::RefreshScrollbars() | |
597 | { | |
598 | if ( m_scrollbarHorz ) | |
599 | m_scrollbarHorz->Refresh(); | |
600 | ||
601 | if ( m_scrollbarVert ) | |
602 | m_scrollbarVert->Refresh(); | |
603 | } | |
604 | ||
605 | void wxWindow::PositionScrollbars() | |
606 | { | |
607 | // do not use GetClientSize/Rect as it relies on the scrollbars being | |
608 | // correctly positioned | |
609 | ||
610 | wxSize size = GetSize(); | |
611 | wxBorder border = GetBorder(); | |
612 | wxRect rectBorder = m_renderer->GetBorderDimensions(border); | |
613 | bool inside = m_renderer->AreScrollbarsInsideBorder(); | |
614 | ||
615 | int height = m_scrollbarHorz ? m_scrollbarHorz->GetSize().y : 0; | |
616 | int width = m_scrollbarVert ? m_scrollbarVert->GetSize().x : 0; | |
617 | ||
618 | wxRect rectBar; | |
619 | if ( m_scrollbarVert ) | |
620 | { | |
621 | rectBar.x = size.x - width; | |
622 | if ( inside ) | |
623 | rectBar.x -= rectBorder.width; | |
624 | rectBar.width = width; | |
625 | rectBar.y = 0; | |
626 | if ( inside ) | |
627 | rectBar.y += rectBorder.y; | |
628 | rectBar.height = size.y - height; | |
629 | if ( inside ) | |
630 | rectBar.height -= rectBorder.y + rectBorder.height; | |
631 | ||
632 | m_scrollbarVert->SetSize(rectBar, wxSIZE_NO_ADJUSTMENTS); | |
633 | } | |
634 | ||
635 | if ( m_scrollbarHorz ) | |
636 | { | |
637 | rectBar.y = size.y - height; | |
638 | if ( inside ) | |
639 | rectBar.y -= rectBorder.height; | |
640 | rectBar.height = height; | |
641 | rectBar.x = 0; | |
642 | if ( inside ) | |
643 | rectBar.x += rectBorder.x; | |
644 | rectBar.width = size.x - width; | |
645 | if ( inside ) | |
646 | rectBar.width -= rectBorder.x + rectBorder.width; | |
647 | ||
648 | m_scrollbarHorz->SetSize(rectBar, wxSIZE_NO_ADJUSTMENTS); | |
649 | } | |
650 | ||
651 | RefreshScrollbars(); | |
652 | } | |
653 | ||
654 | void wxWindow::SetScrollbar(int orient, | |
655 | int pos, | |
656 | int pageSize, | |
657 | int range, | |
658 | bool refresh) | |
659 | { | |
660 | bool hasClientSizeChanged = FALSE; | |
661 | wxScrollBar *scrollbar = GetScrollbar(orient); | |
662 | if ( range ) | |
663 | { | |
664 | if ( !scrollbar ) | |
665 | { | |
666 | // create it | |
667 | scrollbar = new wxScrollBar(this, -1, | |
668 | wxDefaultPosition, wxDefaultSize, | |
669 | orient & wxVERTICAL ? wxSB_VERTICAL | |
670 | : wxSB_HORIZONTAL); | |
671 | if ( orient & wxVERTICAL ) | |
672 | m_scrollbarVert = scrollbar; | |
673 | else | |
674 | m_scrollbarHorz = scrollbar; | |
675 | ||
676 | // the client area diminished as we created a scrollbar | |
677 | hasClientSizeChanged = TRUE; | |
678 | ||
679 | PositionScrollbars(); | |
680 | } | |
681 | else if ( GetWindowStyle() & wxALWAYS_SHOW_SB ) | |
682 | { | |
683 | // we might have disabled it before | |
684 | scrollbar->Enable(); | |
685 | } | |
686 | ||
687 | scrollbar->SetScrollbar(pos, pageSize, range, pageSize, refresh); | |
688 | } | |
689 | else // no range means no scrollbar | |
690 | { | |
691 | if ( scrollbar ) | |
692 | { | |
693 | // wxALWAYS_SHOW_SB only applies to the vertical scrollbar | |
694 | if ( (orient & wxVERTICAL) && (GetWindowStyle() & wxALWAYS_SHOW_SB) ) | |
695 | { | |
696 | // just disable the scrollbar | |
697 | scrollbar->SetScrollbar(pos, pageSize, range, pageSize, refresh); | |
698 | scrollbar->Disable(); | |
699 | } | |
700 | else // really remove the scrollbar | |
701 | { | |
702 | delete scrollbar; | |
703 | ||
704 | if ( orient & wxVERTICAL ) | |
705 | m_scrollbarVert = NULL; | |
706 | else | |
707 | m_scrollbarHorz = NULL; | |
708 | ||
709 | // the client area increased as we removed a scrollbar | |
710 | hasClientSizeChanged = TRUE; | |
711 | ||
712 | // the size of the remaining scrollbar must be adjusted | |
713 | if ( m_scrollbarHorz || m_scrollbarVert ) | |
714 | { | |
715 | PositionScrollbars(); | |
716 | } | |
717 | } | |
718 | } | |
719 | } | |
720 | ||
721 | // give the window a chance to relayout | |
722 | if ( hasClientSizeChanged ) | |
723 | { | |
724 | wxSizeEvent event(GetSize()); | |
725 | (void)GetEventHandler()->ProcessEvent(event); | |
726 | } | |
727 | } | |
728 | ||
729 | void wxWindow::SetScrollPos(int orient, int pos, bool refresh) | |
730 | { | |
731 | wxScrollBar *scrollbar = GetScrollbar(orient); | |
732 | wxCHECK_RET( scrollbar, _T("no scrollbar to set position for") ); | |
733 | ||
734 | scrollbar->SetThumbPosition(pos); | |
735 | ||
736 | // VZ: I think we can safely ignore this as we always refresh it | |
737 | // automatically whenever the value chanegs | |
738 | #if 0 | |
739 | if ( refresh ) | |
740 | Refresh(); | |
741 | #endif | |
742 | } | |
743 | ||
744 | int wxWindow::GetScrollPos(int orient) const | |
745 | { | |
746 | wxScrollBar *scrollbar = GetScrollbar(orient); | |
747 | return scrollbar ? scrollbar->GetThumbPosition() : 0; | |
748 | } | |
749 | ||
750 | int wxWindow::GetScrollThumb(int orient) const | |
751 | { | |
752 | wxScrollBar *scrollbar = GetScrollbar(orient); | |
753 | return scrollbar ? scrollbar->GetThumbSize() : 0; | |
754 | } | |
755 | ||
756 | int wxWindow::GetScrollRange(int orient) const | |
757 | { | |
758 | wxScrollBar *scrollbar = GetScrollbar(orient); | |
759 | return scrollbar ? scrollbar->GetRange() : 0; | |
760 | } | |
761 | ||
762 | void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect) | |
763 | { | |
764 | // before scrolling it, ensure that we don't have any unpainted areas | |
765 | Update(); | |
766 | ||
767 | wxRect r; | |
768 | ||
769 | if ( dx ) | |
770 | { | |
771 | r = ScrollNoRefresh(dx, 0, rect); | |
772 | Refresh(TRUE /* erase bkgnd */, &r); | |
773 | } | |
774 | ||
775 | if ( dy ) | |
776 | { | |
777 | r = ScrollNoRefresh(0, dy, rect); | |
778 | Refresh(TRUE /* erase bkgnd */, &r); | |
779 | } | |
780 | } | |
781 | ||
782 | wxRect wxWindow::ScrollNoRefresh(int dx, int dy, const wxRect *rectTotal) | |
783 | { | |
784 | wxASSERT_MSG( !dx || !dy, _T("can't be used for diag scrolling") ); | |
785 | ||
786 | // the rect to refresh (which we will calculate) | |
787 | wxRect rect; | |
788 | ||
789 | if ( !dx && !dy ) | |
790 | { | |
791 | // nothing to do | |
792 | return rect; | |
793 | } | |
794 | ||
795 | // calculate the part of the window which we can just redraw in the new | |
796 | // location | |
797 | wxSize sizeTotal = rectTotal ? rectTotal->GetSize() : GetClientSize(); | |
798 | ||
799 | wxLogTrace(_T("scroll"), _T("rect is %dx%d, scroll by %d, %d"), | |
800 | sizeTotal.x, sizeTotal.y, dx, dy); | |
801 | ||
802 | // the initial and end point of the region we move in client coords | |
803 | wxPoint ptSource, ptDest; | |
804 | if ( rectTotal ) | |
805 | { | |
806 | ptSource = rectTotal->GetPosition(); | |
807 | ptDest = rectTotal->GetPosition(); | |
808 | } | |
809 | ||
810 | // the size of this region | |
811 | wxSize size; | |
812 | size.x = sizeTotal.x - abs(dx); | |
813 | size.y = sizeTotal.y - abs(dy); | |
814 | if ( size.x <= 0 || size.y <= 0 ) | |
815 | { | |
816 | // just redraw everything as nothing of the displayed image will stay | |
817 | wxLogTrace(_T("scroll"), _T("refreshing everything")); | |
818 | ||
819 | rect = rectTotal ? *rectTotal : wxRect(0, 0, sizeTotal.x, sizeTotal.y); | |
820 | } | |
821 | else // move the part which doesn't change to the new location | |
822 | { | |
823 | // note that when we scroll the canvas in some direction we move the | |
824 | // block which doesn't need to be refreshed in the opposite direction | |
825 | ||
826 | if ( dx < 0 ) | |
827 | { | |
828 | // scroll to the right, move to the left | |
829 | ptSource.x -= dx; | |
830 | } | |
831 | else | |
832 | { | |
833 | // scroll to the left, move to the right | |
834 | ptDest.x += dx; | |
835 | } | |
836 | ||
837 | if ( dy < 0 ) | |
838 | { | |
839 | // scroll down, move up | |
840 | ptSource.y -= dy; | |
841 | } | |
842 | else | |
843 | { | |
844 | // scroll up, move down | |
845 | ptDest.y += dy; | |
846 | } | |
847 | ||
848 | #if wxUSE_CARET | |
849 | // we need to hide the caret before moving or it will erase itself at | |
850 | // the wrong (old) location | |
851 | wxCaret *caret = GetCaret(); | |
852 | if ( caret ) | |
853 | caret->Hide(); | |
854 | #endif // wxUSE_CARET | |
855 | ||
856 | // do move | |
857 | wxClientDC dc(this); | |
858 | wxBitmap bmp(size.x, size.y); | |
859 | wxMemoryDC dcMem; | |
860 | dcMem.SelectObject(bmp); | |
861 | ||
862 | dcMem.Blit(wxPoint(0, 0), size, &dc, ptSource | |
863 | #if defined(__WXGTK__) && !defined(wxHAS_WORKING_GTK_DC_BLIT) | |
864 | + GetClientAreaOrigin() | |
865 | #endif // broken wxGTK wxDC::Blit | |
866 | ); | |
867 | dc.Blit(ptDest, size, &dcMem, wxPoint(0, 0)); | |
868 | ||
869 | wxLogTrace(_T("scroll"), | |
870 | _T("Blit: (%d, %d) of size %dx%d -> (%d, %d)"), | |
871 | ptSource.x, ptSource.y, | |
872 | size.x, size.y, | |
873 | ptDest.x, ptDest.y); | |
874 | ||
875 | // and now repaint the uncovered area | |
876 | ||
877 | // FIXME: We repaint the intersection of these rectangles twice - is | |
878 | // it bad? I don't think so as it is rare to scroll the window | |
879 | // diagonally anyhow and so adding extra logic to compute | |
880 | // rectangle intersection is probably not worth the effort | |
881 | ||
882 | rect.x = ptSource.x; | |
883 | rect.y = ptSource.y; | |
884 | ||
885 | if ( dx ) | |
886 | { | |
887 | if ( dx < 0 ) | |
888 | { | |
889 | // refresh the area along the right border | |
890 | rect.x += size.x + dx; | |
891 | rect.width = -dx; | |
892 | } | |
893 | else | |
894 | { | |
895 | // refresh the area along the left border | |
896 | rect.width = dx; | |
897 | } | |
898 | ||
899 | rect.height = sizeTotal.y; | |
900 | ||
901 | wxLogTrace(_T("scroll"), _T("refreshing (%d, %d)-(%d, %d)"), | |
902 | rect.x, rect.y, | |
903 | rect.GetRight() + 1, rect.GetBottom() + 1); | |
904 | } | |
905 | ||
906 | if ( dy ) | |
907 | { | |
908 | if ( dy < 0 ) | |
909 | { | |
910 | // refresh the area along the bottom border | |
911 | rect.y += size.y + dy; | |
912 | rect.height = -dy; | |
913 | } | |
914 | else | |
915 | { | |
916 | // refresh the area along the top border | |
917 | rect.height = dy; | |
918 | } | |
919 | ||
920 | rect.width = sizeTotal.x; | |
921 | ||
922 | wxLogTrace(_T("scroll"), _T("refreshing (%d, %d)-(%d, %d)"), | |
923 | rect.x, rect.y, | |
924 | rect.GetRight() + 1, rect.GetBottom() + 1); | |
925 | } | |
926 | ||
927 | #if wxUSE_CARET | |
928 | if ( caret ) | |
929 | caret->Show(); | |
930 | #endif // wxUSE_CARET | |
931 | } | |
932 | ||
933 | return rect; | |
934 | } | |
935 | ||
936 | // ---------------------------------------------------------------------------- | |
937 | // colours/fonts | |
938 | // ---------------------------------------------------------------------------- | |
939 | ||
940 | bool wxWindow::SetBackgroundColour(const wxColour& colour) | |
941 | { | |
942 | if ( !wxWindowNative::SetBackgroundColour(colour) ) | |
943 | return FALSE; | |
944 | ||
945 | m_hasBgCol = TRUE; | |
946 | ||
947 | return TRUE; | |
948 | } | |
949 | ||
950 | bool wxWindow::SetForegroundColour(const wxColour& colour) | |
951 | { | |
952 | if ( !wxWindowNative::SetForegroundColour(colour) ) | |
953 | return FALSE; | |
954 | ||
955 | m_hasFgCol = TRUE; | |
956 | ||
957 | return TRUE; | |
958 | } | |
959 | ||
960 | bool wxWindow::SetFont(const wxFont& font) | |
961 | { | |
962 | if ( !wxWindowNative::SetFont(font) ) | |
963 | return FALSE; | |
964 | ||
965 | m_hasFont = TRUE; | |
966 | ||
967 | return TRUE; | |
968 | } | |
969 | ||
970 | // ---------------------------------------------------------------------------- | |
971 | // mouse capture | |
972 | // ---------------------------------------------------------------------------- | |
973 | ||
974 | struct WXDLLEXPORT wxWindowNext | |
975 | { | |
976 | wxWindow *win; | |
977 | wxWindowNext *next; | |
978 | } *wxWindow::ms_winCaptureNext = NULL; | |
979 | ||
980 | void wxWindow::CaptureMouse() | |
981 | { | |
982 | wxLogTrace(_T("mousecapture"), _T("CaptureMouse(0x%08x)"), this); | |
983 | ||
984 | wxWindow *winOld = GetCapture(); | |
985 | if ( winOld ) | |
986 | { | |
987 | // save it on stack | |
988 | wxWindowNext *item = new wxWindowNext; | |
989 | item->win = winOld; | |
990 | item->next = ms_winCaptureNext; | |
991 | ms_winCaptureNext = item; | |
992 | } | |
993 | //else: no mouse capture to save | |
994 | ||
995 | wxWindowNative::CaptureMouse(); | |
996 | } | |
997 | ||
998 | void wxWindow::ReleaseMouse() | |
999 | { | |
1000 | wxWindowNative::ReleaseMouse(); | |
1001 | ||
1002 | if ( ms_winCaptureNext ) | |
1003 | { | |
1004 | ms_winCaptureNext->win->CaptureMouse(); | |
1005 | ||
1006 | wxWindowNext *item = ms_winCaptureNext; | |
1007 | ms_winCaptureNext = item->next; | |
1008 | delete item; | |
1009 | } | |
1010 | //else: stack is empty, no previous capture | |
1011 | ||
1012 | wxLogTrace(_T("mousecapture"), | |
1013 | _T("After ReleaseMouse() mouse is captured by 0x%08x"), | |
1014 | GetCapture()); | |
1015 | } | |
1016 | ||
1017 | // ---------------------------------------------------------------------------- | |
1018 | // accelerators and menu hot keys | |
1019 | // ---------------------------------------------------------------------------- | |
1020 | ||
1021 | #if wxUSE_MENUS | |
1022 | // the last window over which Alt was pressed (used by OnKeyUp) | |
1023 | wxWindow *wxWindow::ms_winLastAltPress = NULL; | |
1024 | #endif // wxUSE_MENUS | |
1025 | ||
1026 | #if wxUSE_ACCEL || wxUSE_MENUS | |
1027 | ||
1028 | void wxWindow::OnKeyDown(wxKeyEvent& event) | |
1029 | { | |
1030 | #if wxUSE_MENUS | |
1031 | int key = event.GetKeyCode(); | |
1032 | if ( !event.ControlDown() && (key == WXK_MENU || key == WXK_F10) ) | |
1033 | { | |
1034 | ms_winLastAltPress = this; | |
1035 | ||
1036 | // it can't be an accel anyhow | |
1037 | return; | |
1038 | } | |
1039 | ||
1040 | ms_winLastAltPress = NULL; | |
1041 | #endif // wxUSE_MENUS | |
1042 | ||
1043 | #if wxUSE_ACCEL | |
1044 | for ( wxWindow *win = this; win; win = win->GetParent() ) | |
1045 | { | |
1046 | int command = win->GetAcceleratorTable()->GetCommand(event); | |
1047 | if ( command != -1 ) | |
1048 | { | |
1049 | wxCommandEvent eventCmd(wxEVT_COMMAND_MENU_SELECTED, command); | |
1050 | if ( win->GetEventHandler()->ProcessEvent(eventCmd) ) | |
1051 | { | |
1052 | // skip "event.Skip()" below | |
1053 | return; | |
1054 | } | |
1055 | } | |
1056 | ||
1057 | if ( win->IsTopLevel() ) | |
1058 | { | |
1059 | // try the frame menu bar | |
1060 | #if wxUSE_MENUS | |
1061 | wxFrame *frame = wxDynamicCast(win, wxFrame); | |
1062 | if ( frame ) | |
1063 | { | |
1064 | wxMenuBar *menubar = frame->GetMenuBar(); | |
1065 | if ( menubar && menubar->ProcessAccelEvent(event) ) | |
1066 | { | |
1067 | // skip "event.Skip()" below | |
1068 | return; | |
1069 | } | |
1070 | } | |
1071 | #endif // wxUSE_MENUS | |
1072 | ||
1073 | // don't propagate accels from the child frame to the parent one | |
1074 | break; | |
1075 | } | |
1076 | } | |
1077 | #endif // wxUSE_ACCEL | |
1078 | ||
1079 | event.Skip(); | |
1080 | } | |
1081 | ||
1082 | #endif // wxUSE_ACCEL | |
1083 | ||
1084 | #if wxUSE_MENUS | |
1085 | ||
1086 | wxMenuBar *wxWindow::GetParentFrameMenuBar() const | |
1087 | { | |
1088 | for ( const wxWindow *win = this; win; win = win->GetParent() ) | |
1089 | { | |
1090 | if ( win->IsTopLevel() ) | |
1091 | { | |
1092 | wxFrame *frame = wxDynamicCast(win, wxFrame); | |
1093 | if ( frame ) | |
1094 | { | |
1095 | return frame->GetMenuBar(); | |
1096 | } | |
1097 | ||
1098 | // don't look further - we don't want to return the menubar of the | |
1099 | // parent frame | |
1100 | break; | |
1101 | } | |
1102 | } | |
1103 | ||
1104 | return NULL; | |
1105 | } | |
1106 | ||
1107 | void wxWindow::OnChar(wxKeyEvent& event) | |
1108 | { | |
1109 | if ( event.AltDown() && !event.ControlDown() ) | |
1110 | { | |
1111 | int key = event.GetKeyCode(); | |
1112 | ||
1113 | wxMenuBar *menubar = GetParentFrameMenuBar(); | |
1114 | if ( menubar ) | |
1115 | { | |
1116 | int item = menubar->FindNextItemForAccel(-1, key); | |
1117 | if ( item != -1 ) | |
1118 | { | |
1119 | menubar->PopupMenu((size_t)item); | |
1120 | ||
1121 | // skip "event.Skip()" below | |
1122 | return; | |
1123 | } | |
1124 | } | |
1125 | } | |
1126 | ||
1127 | event.Skip(); | |
1128 | } | |
1129 | ||
1130 | void wxWindow::OnKeyUp(wxKeyEvent& event) | |
1131 | { | |
1132 | int key = event.GetKeyCode(); | |
1133 | if ( !event.HasModifiers() && (key == WXK_MENU || key == WXK_F10) ) | |
1134 | { | |
1135 | // only process Alt release specially if there were no other key | |
1136 | // presses since Alt had been pressed and if both events happened in | |
1137 | // the same window | |
1138 | if ( ms_winLastAltPress == this ) | |
1139 | { | |
1140 | wxMenuBar *menubar = GetParentFrameMenuBar(); | |
1141 | if ( menubar && this != menubar ) | |
1142 | { | |
1143 | menubar->SelectMenu(0); | |
1144 | } | |
1145 | } | |
1146 | } | |
1147 | else | |
1148 | { | |
1149 | event.Skip(); | |
1150 | } | |
1151 | ||
1152 | // in any case reset it | |
1153 | ms_winLastAltPress = NULL; | |
1154 | } | |
1155 | ||
1156 | #endif // wxUSE_MENUS | |
1157 |