]> git.saurik.com Git - wxWidgets.git/blob - src/generic/tbarsmpl.cpp
Add a little space so the wxHtmlCell doesn't overwrite the border of
[wxWidgets.git] / src / generic / tbarsmpl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/tbarsmpl.cpp
3 // Purpose: wxToolBarSimple
4 // Author: Julian Smart
5 // Modified by: VZ on 14.12.99 during wxToolBarSimple reorganization
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
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 "tbarsmpl.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 #if wxUSE_TOOLBAR && wxUSE_TOOLBAR_SIMPLE
32
33 #ifndef WX_PRECOMP
34 #include "wx/settings.h"
35 #include "wx/window.h"
36 #include "wx/dcclient.h"
37 #include "wx/dcmemory.h"
38 #endif
39
40 #include "wx/toolbar.h"
41 #include "wx/tbarsmpl.h"
42
43 // ----------------------------------------------------------------------------
44 // private classes
45 // ----------------------------------------------------------------------------
46
47 class WXDLLEXPORT wxToolBarToolSimple : public wxToolBarToolBase
48 {
49 public:
50 wxToolBarToolSimple(wxToolBarSimple *tbar,
51 int id,
52 const wxString& label,
53 const wxBitmap& bmpNormal,
54 const wxBitmap& bmpDisabled,
55 wxItemKind kind,
56 wxObject *clientData,
57 const wxString& shortHelp,
58 const wxString& longHelp)
59 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
60 clientData, shortHelp, longHelp)
61 {
62 }
63
64 wxToolBarToolSimple(wxToolBarSimple *tbar, wxControl *control)
65 : wxToolBarToolBase(tbar, control)
66 {
67 }
68
69 void SetSize(const wxSize& size)
70 {
71 m_width = size.x;
72 m_height = size.y;
73 }
74
75 wxCoord GetWidth() const { return m_width; }
76 wxCoord GetHeight() const { return m_height; }
77
78 wxCoord m_x;
79 wxCoord m_y;
80 wxCoord m_width;
81 wxCoord m_height;
82
83 DECLARE_NO_COPY_CLASS(wxToolBarToolSimple)
84 };
85
86 // ----------------------------------------------------------------------------
87 // wxWin macros
88 // ----------------------------------------------------------------------------
89
90 IMPLEMENT_DYNAMIC_CLASS(wxToolBarSimple, wxControl)
91
92 #if !wxUSE_TOOLBAR_NATIVE && !defined(__WXUNIVERSAL__)
93 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarSimple)
94 #endif
95
96 BEGIN_EVENT_TABLE(wxToolBarSimple, wxToolBarBase)
97 EVT_SIZE(wxToolBarSimple::OnSize)
98 EVT_SCROLL(wxToolBarSimple::OnScroll)
99 EVT_PAINT(wxToolBarSimple::OnPaint)
100 EVT_KILL_FOCUS(wxToolBarSimple::OnKillFocus)
101 EVT_MOUSE_EVENTS(wxToolBarSimple::OnMouseEvent)
102 END_EVENT_TABLE()
103
104 // ============================================================================
105 // implementation
106 // ============================================================================
107
108 // ----------------------------------------------------------------------------
109 // tool bar tools creation
110 // ----------------------------------------------------------------------------
111
112 wxToolBarToolBase *wxToolBarSimple::CreateTool(int id,
113 const wxString& label,
114 const wxBitmap& bmpNormal,
115 const wxBitmap& bmpDisabled,
116 wxItemKind kind,
117 wxObject *clientData,
118 const wxString& shortHelp,
119 const wxString& longHelp)
120 {
121 return new wxToolBarToolSimple(this, id, label, bmpNormal, bmpDisabled,
122 kind, clientData, shortHelp, longHelp);
123 }
124
125 wxToolBarToolBase *wxToolBarSimple::CreateTool(wxControl *control)
126 {
127 return new wxToolBarToolSimple(this, control);
128 }
129
130 // ----------------------------------------------------------------------------
131 // wxToolBarSimple creation
132 // ----------------------------------------------------------------------------
133
134 void wxToolBarSimple::Init()
135 {
136 m_currentRowsOrColumns = 0;
137
138 m_lastX =
139 m_lastY = 0;
140
141 m_maxWidth =
142 m_maxHeight = 0;
143
144 m_pressedTool =
145 m_currentTool = -1;
146
147 m_xPos =
148 m_yPos = -1;
149
150 m_toolPacking = 1;
151 m_toolSeparation = 5;
152
153 m_defaultWidth = 16;
154 m_defaultHeight = 15;
155
156 m_xScrollPixelsPerLine = 1;
157 m_yScrollPixelsPerLine = 1;
158 m_xScrollingEnabled = FALSE;
159 m_yScrollingEnabled = FALSE;
160 m_xScrollPosition = 0;
161 m_yScrollPosition = 0;
162 m_xScrollLines = 0;
163 m_yScrollLines = 0;
164 m_xScrollLinesPerPage = 0;
165 m_yScrollLinesPerPage = 0;
166 }
167
168 wxToolBarToolBase *wxToolBarSimple::DoAddTool(int id,
169 const wxString& label,
170 const wxBitmap& bitmap,
171 const wxBitmap& bmpDisabled,
172 wxItemKind kind,
173 const wxString& shortHelp,
174 const wxString& longHelp,
175 wxObject *clientData,
176 wxCoord xPos,
177 wxCoord yPos)
178 {
179 // rememeber the position for DoInsertTool()
180 m_xPos = xPos;
181 m_yPos = yPos;
182
183 return wxToolBarBase::DoAddTool(id, label, bitmap, bmpDisabled, kind,
184 shortHelp, longHelp,
185 clientData, xPos, yPos);
186 }
187
188 bool wxToolBarSimple::DoInsertTool(size_t WXUNUSED(pos),
189 wxToolBarToolBase *toolBase)
190 {
191 wxToolBarToolSimple *tool = (wxToolBarToolSimple *)toolBase;
192
193 wxCHECK_MSG( !tool->IsControl(), FALSE,
194 _T("generic wxToolBarSimple doesn't support controls") );
195
196 tool->m_x = m_xPos;
197 if ( tool->m_x == -1 )
198 tool->m_x = m_xMargin;
199
200 tool->m_y = m_yPos;
201 if ( tool->m_y == -1 )
202 tool->m_y = m_yMargin;
203
204 tool->SetSize(GetToolSize());
205
206 if ( tool->IsButton() )
207 {
208 // Calculate reasonable max size in case Layout() not called
209 if ((tool->m_x + tool->GetNormalBitmap().GetWidth() + m_xMargin) > m_maxWidth)
210 m_maxWidth = (wxCoord)((tool->m_x + tool->GetWidth() + m_xMargin));
211
212 if ((tool->m_y + tool->GetNormalBitmap().GetHeight() + m_yMargin) > m_maxHeight)
213 m_maxHeight = (wxCoord)((tool->m_y + tool->GetHeight() + m_yMargin));
214 }
215
216 return TRUE;
217 }
218
219 bool wxToolBarSimple::DoDeleteTool(size_t WXUNUSED(pos),
220 wxToolBarToolBase *tool)
221 {
222 // VZ: didn't test whether it works, but why not...
223 tool->Detach();
224
225 Refresh();
226
227 return TRUE;
228 }
229
230 bool wxToolBarSimple::Create(wxWindow *parent,
231 wxWindowID id,
232 const wxPoint& pos,
233 const wxSize& size,
234 long style,
235 const wxString& name)
236 {
237 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
238 return FALSE;
239
240 // Set it to grey (or other 3D face colour)
241 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
242
243 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
244 {
245 m_lastX = 7;
246 m_lastY = 3;
247
248 m_maxRows = 32000; // a lot
249 m_maxCols = 1;
250 }
251 else
252 {
253 m_lastX = 3;
254 m_lastY = 7;
255
256 m_maxRows = 1;
257 m_maxCols = 32000; // a lot
258 }
259
260 SetCursor(*wxSTANDARD_CURSOR);
261
262 return TRUE;
263 }
264
265 wxToolBarSimple::~wxToolBarSimple()
266 {
267 }
268
269 bool wxToolBarSimple::Realize()
270 {
271 m_currentRowsOrColumns = 0;
272 m_lastX = m_xMargin;
273 m_lastY = m_yMargin;
274 m_maxWidth = 0;
275 m_maxHeight = 0;
276
277 int maxToolWidth = 0;
278 int maxToolHeight = 0;
279
280 // Find the maximum tool width and height
281 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
282 while ( node )
283 {
284 wxToolBarToolSimple *tool = (wxToolBarToolSimple *)node->GetData();
285 if ( tool->GetWidth() > maxToolWidth )
286 maxToolWidth = tool->GetWidth();
287 if (tool->GetHeight() > maxToolHeight)
288 maxToolHeight = tool->GetHeight();
289
290 node = node->GetNext();
291 }
292
293 int separatorSize = m_toolSeparation;
294
295 node = m_tools.GetFirst();
296 while ( node )
297 {
298 wxToolBarToolSimple *tool = (wxToolBarToolSimple *)node->GetData();
299 if ( tool->IsSeparator() )
300 {
301 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
302 {
303 if (m_currentRowsOrColumns >= m_maxCols)
304 m_lastY += separatorSize;
305 else
306 m_lastX += separatorSize;
307 }
308 else
309 {
310 if (m_currentRowsOrColumns >= m_maxRows)
311 m_lastX += separatorSize;
312 else
313 m_lastY += separatorSize;
314 }
315 }
316 else if ( tool->IsButton() )
317 {
318 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
319 {
320 if (m_currentRowsOrColumns >= m_maxCols)
321 {
322 m_currentRowsOrColumns = 0;
323 m_lastX = m_xMargin;
324 m_lastY += maxToolHeight + m_toolPacking;
325 }
326 tool->m_x = (wxCoord)(m_lastX + (maxToolWidth - tool->GetWidth())/2.0);
327 tool->m_y = (wxCoord)(m_lastY + (maxToolHeight - tool->GetHeight())/2.0);
328
329 m_lastX += maxToolWidth + m_toolPacking;
330 }
331 else
332 {
333 if (m_currentRowsOrColumns >= m_maxRows)
334 {
335 m_currentRowsOrColumns = 0;
336 m_lastX += (maxToolWidth + m_toolPacking);
337 m_lastY = m_yMargin;
338 }
339 tool->m_x = (wxCoord)(m_lastX + (maxToolWidth - tool->GetWidth())/2.0);
340 tool->m_y = (wxCoord)(m_lastY + (maxToolHeight - tool->GetHeight())/2.0);
341
342 m_lastY += maxToolHeight + m_toolPacking;
343 }
344 m_currentRowsOrColumns ++;
345 }
346 else
347 {
348 // TODO: support the controls
349 }
350
351 if (m_lastX > m_maxWidth)
352 m_maxWidth = m_lastX;
353 if (m_lastY > m_maxHeight)
354 m_maxHeight = m_lastY;
355
356 node = node->GetNext();
357 }
358
359 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
360 m_maxHeight += maxToolHeight;
361 else
362 m_maxWidth += maxToolWidth;
363
364 m_maxWidth += m_xMargin;
365 m_maxHeight += m_yMargin;
366
367 SetSize(m_maxWidth, m_maxHeight);
368
369 return TRUE;
370 }
371
372 // ----------------------------------------------------------------------------
373 // event handlers
374 // ----------------------------------------------------------------------------
375
376 void wxToolBarSimple::OnPaint (wxPaintEvent& WXUNUSED(event))
377 {
378 wxPaintDC dc(this);
379 PrepareDC(dc);
380
381 static int count = 0;
382 // Prevent reentry of OnPaint which would cause wxMemoryDC errors.
383 if ( count > 0 )
384 return;
385 count++;
386
387 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
388 node;
389 node = node->GetNext() )
390 {
391 wxToolBarToolBase *tool = node->GetData();
392 if ( tool->IsButton() )
393 DrawTool(dc, tool);
394 }
395
396 count--;
397 }
398
399 void wxToolBarSimple::OnSize (wxSizeEvent& WXUNUSED(event))
400 {
401 #if wxUSE_CONSTRAINTS
402 if (GetAutoLayout())
403 Layout();
404 #endif
405
406 AdjustScrollbars();
407 }
408
409 void wxToolBarSimple::OnKillFocus(wxFocusEvent& WXUNUSED(event))
410 {
411 OnMouseEnter(m_pressedTool = m_currentTool = -1);
412 }
413
414 void wxToolBarSimple::OnMouseEvent(wxMouseEvent & event)
415 {
416 wxCoord x, y;
417 event.GetPosition(&x, &y);
418 wxToolBarToolSimple *tool = (wxToolBarToolSimple *)FindToolForPosition(x, y);
419
420 if (event.LeftDown())
421 {
422 CaptureMouse();
423 }
424 if (event.LeftUp())
425 {
426 ReleaseMouse();
427 }
428
429 if (!tool)
430 {
431 if (m_currentTool > -1)
432 {
433 if (event.LeftIsDown())
434 SpringUpButton(m_currentTool);
435 m_currentTool = -1;
436 OnMouseEnter(-1);
437 }
438 return;
439 }
440
441 if (!event.IsButton())
442 {
443 if ( tool->GetId() != m_currentTool )
444 {
445 // If the left button is kept down and moved over buttons,
446 // press those buttons.
447 if ( event.LeftIsDown() && tool->IsEnabled() )
448 {
449 SpringUpButton(m_currentTool);
450
451 if ( tool->CanBeToggled() )
452 {
453 tool->Toggle();
454 }
455
456 DrawTool(tool);
457 }
458
459 m_currentTool = tool->GetId();
460 OnMouseEnter(m_currentTool);
461 }
462 return;
463 }
464
465 // Left button pressed.
466 if ( event.LeftDown() && tool->IsEnabled() )
467 {
468 if ( tool->CanBeToggled() )
469 {
470 tool->Toggle();
471 }
472
473 DrawTool(tool);
474 }
475 else if (event.RightDown())
476 {
477 OnRightClick(tool->GetId(), x, y);
478 }
479
480 // Left Button Released. Only this action confirms selection.
481 // If the button is enabled and it is not a toggle tool and it is
482 // in the pressed state, then raise the button and call OnLeftClick.
483 //
484 if ( event.LeftUp() && tool->IsEnabled() )
485 {
486 // Pass the OnLeftClick event to tool
487 if ( !OnLeftClick(tool->GetId(), tool->IsToggled()) &&
488 tool->CanBeToggled() )
489 {
490 // If it was a toggle, and OnLeftClick says No Toggle allowed,
491 // then change it back
492 tool->Toggle();
493 }
494
495 DrawTool(tool);
496 }
497 }
498
499 // ----------------------------------------------------------------------------
500 // drawing
501 // ----------------------------------------------------------------------------
502
503 void wxToolBarSimple::DrawTool(wxToolBarToolBase *tool)
504 {
505 wxClientDC dc(this);
506 DrawTool(dc, tool);
507 }
508
509 void wxToolBarSimple::DrawTool(wxDC& dc, wxToolBarToolBase *toolBase)
510 {
511 wxToolBarToolSimple *tool = (wxToolBarToolSimple *)toolBase;
512
513 wxMemoryDC memDC;
514 PrepareDC(dc);
515
516 wxPen dark_grey_pen(wxColour( 85,85,85 ), 1, wxSOLID);
517 wxPen white_pen(wxT("WHITE"), 1, wxSOLID);
518 wxPen black_pen(wxT("BLACK"), 1, wxSOLID);
519
520 wxBitmap bitmap = tool->GetNormalBitmap();
521 if (!bitmap.Ok())
522 return;
523
524 if ( !tool->IsToggled() )
525 {
526 #if wxUSE_PALETTE
527 #ifndef __WXGTK__
528 if (bitmap.GetPalette())
529 memDC.SetPalette(*bitmap.GetPalette());
530 #endif
531 #endif // wxUSE_PALETTE
532
533 int ax = (int)tool->m_x,
534 ay = (int)tool->m_y,
535 bx = (int)(tool->m_x+tool->GetWidth()),
536 by = (int)(tool->m_y+tool->GetHeight());
537
538 memDC.SelectObject(bitmap);
539 if (m_windowStyle & wxTB_3DBUTTONS)
540 {
541 dc.SetClippingRegion(ax, ay, (bx-ax+1), (by-ay+1));
542 dc.Blit((ax+1), (ay+1), (bx-ax-2), (by-ay-2), &memDC, 0, 0);
543 wxPen * old_pen = & dc.GetPen();
544 dc.SetPen( white_pen );
545 dc.DrawLine(ax,(by-1),ax,ay);
546 dc.DrawLine(ax,ay,(bx-1),ay);
547 dc.SetPen( dark_grey_pen );
548 dc.DrawLine((bx-1),(ay+1),(bx-1),(by-1));
549 dc.DrawLine((bx-1),(by-1),(ax+1),(by-1));
550 dc.SetPen( black_pen );
551 dc.DrawLine(bx,ay,bx,by);
552 dc.DrawLine(bx,by,ax,by);
553 dc.SetPen( *old_pen );
554 dc.DestroyClippingRegion();
555 // Select bitmap out of the DC
556 }
557 else
558 {
559 dc.Blit(tool->m_x, tool->m_y,
560 bitmap.GetWidth(), bitmap.GetHeight(),
561 &memDC, 0, 0);
562 }
563 memDC.SelectObject(wxNullBitmap);
564
565 #if wxUSE_PALETTE
566 #ifndef __WXGTK__
567 if (bitmap.GetPalette())
568 memDC.SetPalette(wxNullPalette);
569 #endif
570 #endif // wxUSE_PALETTE
571 }
572 // No second bitmap, so draw a thick line around bitmap, or invert if mono
573 else if ( tool->IsToggled() )
574 {
575 bool drawBorder = FALSE;
576 #ifdef __X__ // X doesn't invert properly on colour
577 drawBorder = wxColourDisplay();
578 #else // Inversion works fine under Windows
579 drawBorder = FALSE;
580 #endif
581
582 if (!drawBorder)
583 {
584 memDC.SelectObject(tool->GetNormalBitmap());
585 dc.Blit(tool->m_x, tool->m_y, tool->GetWidth(), tool->GetHeight(),
586 &memDC, 0, 0, wxSRC_INVERT);
587 memDC.SelectObject(wxNullBitmap);
588 }
589 else
590 {
591 bitmap = tool->GetNormalBitmap();
592
593 if (m_windowStyle & wxTB_3DBUTTONS)
594 {
595 int ax = (int)tool->m_x,
596 ay = (int)tool->m_y,
597 bx = (int)(tool->m_x+tool->GetWidth()),
598 by = (int)(tool->m_y+tool->GetHeight());
599
600 memDC.SelectObject(bitmap);
601 dc.SetClippingRegion(ax, ay, (bx-ax+1), (by-ay+1));
602 dc.Blit((ax+2), (ay+2), (bx-ax-2), (by-ay-2), &memDC, 0, 0);
603 wxPen * old_pen = & dc.GetPen();
604 dc.SetPen( black_pen );
605 dc.DrawLine(ax,(by-1),ax,ay);
606 dc.DrawLine(ax,ay,(bx-1),ay);
607 dc.SetPen( dark_grey_pen );
608 dc.DrawLine((ax+1),(by-2),(ax+1),(ay+1));
609 dc.DrawLine((ax+1),(ay+1),(bx-2),(ay+1));
610 dc.SetPen( white_pen );
611 dc.DrawLine(bx,ay,bx,by);
612 dc.DrawLine(bx,by,ax,by);
613 dc.SetPen( *old_pen );
614 dc.DestroyClippingRegion();
615 memDC.SelectObject(wxNullBitmap);
616 }
617 else
618 {
619 wxCoord x = tool->m_x;
620 wxCoord y = tool->m_y;
621 wxCoord w = bitmap.GetWidth();
622 wxCoord h = bitmap.GetHeight();
623 wxPen thick_black_pen(wxT("BLACK"), 3, wxSOLID);
624
625 memDC.SelectObject(bitmap);
626 dc.SetClippingRegion(tool->m_x, tool->m_y, w, h);
627 dc.Blit(tool->m_x, tool->m_y, w, h,
628 &memDC, 0, 0);
629 dc.SetPen(thick_black_pen);
630 dc.SetBrush(*wxTRANSPARENT_BRUSH);
631 dc.DrawRectangle(x, y, w-1, h-1);
632 dc.DestroyClippingRegion();
633 memDC.SelectObject(wxNullBitmap);
634 }
635 }
636 }
637 }
638
639 // ----------------------------------------------------------------------------
640 // toolbar geometry
641 // ----------------------------------------------------------------------------
642
643 void wxToolBarSimple::SetRows(int nRows)
644 {
645 wxCHECK_RET( nRows != 0, _T("max number of rows must be > 0") );
646
647 m_maxCols = (GetToolsCount() + nRows - 1) / nRows;
648
649 AdjustScrollbars();
650 Refresh();
651 }
652
653 wxToolBarToolBase *wxToolBarSimple::FindToolForPosition(wxCoord x,
654 wxCoord y) const
655 {
656 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
657 while (node)
658 {
659 wxToolBarToolSimple *tool = (wxToolBarToolSimple *)node->GetData();
660 if ((x >= tool->m_x) && (y >= tool->m_y) &&
661 (x <= (tool->m_x + tool->GetWidth())) &&
662 (y <= (tool->m_y + tool->GetHeight())))
663 {
664 return tool;
665 }
666
667 node = node->GetNext();
668 }
669
670 return (wxToolBarToolBase *)NULL;
671 }
672
673 // ----------------------------------------------------------------------------
674 // tool state change handlers
675 // ----------------------------------------------------------------------------
676
677 void wxToolBarSimple::DoEnableTool(wxToolBarToolBase *tool,
678 bool WXUNUSED(enable))
679 {
680 DrawTool(tool);
681 }
682
683 void wxToolBarSimple::DoToggleTool(wxToolBarToolBase *tool,
684 bool WXUNUSED(toggle))
685 {
686 DrawTool(tool);
687 }
688
689 void wxToolBarSimple::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool),
690 bool WXUNUSED(toggle))
691 {
692 // nothing to do
693 }
694
695 // Okay, so we've left the tool we're in ... we must check if the tool we're
696 // leaving was a 'sprung push button' and if so, spring it back to the up
697 // state.
698 void wxToolBarSimple::SpringUpButton(int id)
699 {
700 wxToolBarToolBase *tool = FindById(id);
701
702 if ( tool && tool->CanBeToggled() )
703 {
704 if (tool->IsToggled())
705 tool->Toggle();
706
707 DrawTool(tool);
708 }
709 }
710
711 // ----------------------------------------------------------------------------
712 // scrolling implementation
713 // ----------------------------------------------------------------------------
714
715 /*
716 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
717 * noUnitsX/noUnitsY: : no. units per scrollbar
718 */
719 void wxToolBarSimple::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY,
720 int noUnitsX, int noUnitsY,
721 int xPos, int yPos)
722 {
723 m_xScrollPixelsPerLine = pixelsPerUnitX;
724 m_yScrollPixelsPerLine = pixelsPerUnitY;
725 m_xScrollLines = noUnitsX;
726 m_yScrollLines = noUnitsY;
727
728 int w, h;
729 GetSize(&w, &h);
730
731 // Recalculate scroll bar range and position
732 if (m_xScrollLines > 0)
733 {
734 m_xScrollPosition = xPos;
735 SetScrollPos (wxHORIZONTAL, m_xScrollPosition, TRUE);
736 }
737 else
738 {
739 SetScrollbar(wxHORIZONTAL, 0, 0, 0, FALSE);
740 m_xScrollPosition = 0;
741 }
742
743 if (m_yScrollLines > 0)
744 {
745 m_yScrollPosition = yPos;
746 SetScrollPos (wxVERTICAL, m_yScrollPosition, TRUE);
747 }
748 else
749 {
750 SetScrollbar(wxVERTICAL, 0, 0, 0, FALSE);
751 m_yScrollPosition = 0;
752 }
753 AdjustScrollbars();
754 Refresh();
755
756 #if 0 //def __WXMSW__
757 ::UpdateWindow ((HWND) GetHWND());
758 #endif
759 }
760
761 void wxToolBarSimple::OnScroll(wxScrollEvent& event)
762 {
763 int orient = event.GetOrientation();
764
765 int nScrollInc = CalcScrollInc(event);
766 if (nScrollInc == 0)
767 return;
768
769 if (orient == wxHORIZONTAL)
770 {
771 int newPos = m_xScrollPosition + nScrollInc;
772 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
773 }
774 else
775 {
776 int newPos = m_yScrollPosition + nScrollInc;
777 SetScrollPos(wxVERTICAL, newPos, TRUE );
778 }
779
780 if (orient == wxHORIZONTAL)
781 {
782 if (m_xScrollingEnabled)
783 ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, NULL);
784 else
785 Refresh();
786 }
787 else
788 {
789 if (m_yScrollingEnabled)
790 ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, NULL);
791 else
792 Refresh();
793 }
794
795 if (orient == wxHORIZONTAL)
796 {
797 m_xScrollPosition += nScrollInc;
798 }
799 else
800 {
801 m_yScrollPosition += nScrollInc;
802 }
803
804 }
805
806 int wxToolBarSimple::CalcScrollInc(wxScrollEvent& event)
807 {
808 int pos = event.GetPosition();
809 int orient = event.GetOrientation();
810
811 int nScrollInc = 0;
812 if (event.GetEventType() == wxEVT_SCROLL_TOP)
813 {
814 if (orient == wxHORIZONTAL)
815 nScrollInc = - m_xScrollPosition;
816 else
817 nScrollInc = - m_yScrollPosition;
818 } else
819 if (event.GetEventType() == wxEVT_SCROLL_BOTTOM)
820 {
821 if (orient == wxHORIZONTAL)
822 nScrollInc = m_xScrollLines - m_xScrollPosition;
823 else
824 nScrollInc = m_yScrollLines - m_yScrollPosition;
825 } else
826 if (event.GetEventType() == wxEVT_SCROLL_LINEUP)
827 {
828 nScrollInc = -1;
829 } else
830 if (event.GetEventType() == wxEVT_SCROLL_LINEDOWN)
831 {
832 nScrollInc = 1;
833 } else
834 if (event.GetEventType() == wxEVT_SCROLL_PAGEUP)
835 {
836 if (orient == wxHORIZONTAL)
837 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
838 else
839 nScrollInc = -GetScrollPageSize(wxVERTICAL);
840 } else
841 if (event.GetEventType() == wxEVT_SCROLL_PAGEDOWN)
842 {
843 if (orient == wxHORIZONTAL)
844 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
845 else
846 nScrollInc = GetScrollPageSize(wxVERTICAL);
847 } else
848 if ((event.GetEventType() == wxEVT_SCROLL_THUMBTRACK) ||
849 (event.GetEventType() == wxEVT_SCROLL_THUMBRELEASE))
850 {
851 if (orient == wxHORIZONTAL)
852 nScrollInc = pos - m_xScrollPosition;
853 else
854 nScrollInc = pos - m_yScrollPosition;
855 }
856
857 if (orient == wxHORIZONTAL)
858 {
859 int w, h;
860 GetClientSize(&w, &h);
861
862 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
863 int noPositions = (int) ( ((nMaxWidth - w)/(float)m_xScrollPixelsPerLine) + 0.5 );
864 if (noPositions < 0)
865 noPositions = 0;
866
867 if ( (m_xScrollPosition + nScrollInc) < 0 )
868 nScrollInc = -m_xScrollPosition; // As -ve as we can go
869 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
870 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
871
872 return nScrollInc;
873 }
874 else
875 {
876 int w, h;
877 GetClientSize(&w, &h);
878
879 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
880 int noPositions = (int) ( ((nMaxHeight - h)/(float)m_yScrollPixelsPerLine) + 0.5 );
881 if (noPositions < 0)
882 noPositions = 0;
883
884 if ( (m_yScrollPosition + nScrollInc) < 0 )
885 nScrollInc = -m_yScrollPosition; // As -ve as we can go
886 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
887 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
888
889 return nScrollInc;
890 }
891 }
892
893 // Adjust the scrollbars - new version.
894 void wxToolBarSimple::AdjustScrollbars()
895 {
896 int w, h;
897 GetClientSize(&w, &h);
898
899 // Recalculate scroll bar range and position
900 if (m_xScrollLines > 0)
901 {
902 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
903 int newRange = (int) ( ((nMaxWidth)/(float)m_xScrollPixelsPerLine) + 0.5 );
904 if (newRange < 0)
905 newRange = 0;
906
907 m_xScrollPosition = wxMin(newRange, m_xScrollPosition);
908
909 // Calculate page size i.e. number of scroll units you get on the
910 // current client window
911 int noPagePositions = (int) ( (w/(float)m_xScrollPixelsPerLine) + 0.5 );
912 if (noPagePositions < 1)
913 noPagePositions = 1;
914
915 SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, newRange);
916 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
917 }
918 if (m_yScrollLines > 0)
919 {
920 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
921 int newRange = (int) ( ((nMaxHeight)/(float)m_yScrollPixelsPerLine) + 0.5 );
922 if (newRange < 0)
923 newRange = 0;
924
925 m_yScrollPosition = wxMin(newRange, m_yScrollPosition);
926
927 // Calculate page size i.e. number of scroll units you get on the
928 // current client window
929 int noPagePositions = (int) ( (h/(float)m_yScrollPixelsPerLine) + 0.5 );
930 if (noPagePositions < 1)
931 noPagePositions = 1;
932
933 SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, newRange);
934 SetScrollPageSize(wxVERTICAL, noPagePositions);
935 }
936 }
937
938 // Prepare the DC by translating it according to the current scroll position
939 void wxToolBarSimple::PrepareDC(wxDC& dc)
940 {
941 dc.SetDeviceOrigin(- m_xScrollPosition * m_xScrollPixelsPerLine, - m_yScrollPosition * m_yScrollPixelsPerLine);
942 }
943
944 void wxToolBarSimple::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
945 {
946 *x_unit = m_xScrollPixelsPerLine;
947 *y_unit = m_yScrollPixelsPerLine;
948 }
949
950 int wxToolBarSimple::GetScrollPageSize(int orient) const
951 {
952 if ( orient == wxHORIZONTAL )
953 return m_xScrollLinesPerPage;
954 else
955 return m_yScrollLinesPerPage;
956 }
957
958 void wxToolBarSimple::SetScrollPageSize(int orient, int pageSize)
959 {
960 if ( orient == wxHORIZONTAL )
961 m_xScrollLinesPerPage = pageSize;
962 else
963 m_yScrollLinesPerPage = pageSize;
964 }
965
966 /*
967 * Scroll to given position (scroll position, not pixel position)
968 */
969 void wxToolBarSimple::Scroll (int x_pos, int y_pos)
970 {
971 int old_x, old_y;
972 ViewStart (&old_x, &old_y);
973 if (((x_pos == -1) || (x_pos == old_x)) && ((y_pos == -1) || (y_pos == old_y)))
974 return;
975
976 if (x_pos > -1)
977 {
978 m_xScrollPosition = x_pos;
979 SetScrollPos (wxHORIZONTAL, x_pos, TRUE);
980 }
981 if (y_pos > -1)
982 {
983 m_yScrollPosition = y_pos;
984 SetScrollPos (wxVERTICAL, y_pos, TRUE);
985 }
986 Refresh();
987
988 #if 0 //def __WXMSW__
989 UpdateWindow ((HWND) GetHWND());
990 #endif
991 }
992
993 void wxToolBarSimple::EnableScrolling (bool x_scroll, bool y_scroll)
994 {
995 m_xScrollingEnabled = x_scroll;
996 m_yScrollingEnabled = y_scroll;
997 }
998
999 void wxToolBarSimple::GetVirtualSize (int *x, int *y) const
1000 {
1001 *x = m_xScrollPixelsPerLine * m_xScrollLines;
1002 *y = m_yScrollPixelsPerLine * m_yScrollLines;
1003 }
1004
1005 // Where the current view starts from
1006 void wxToolBarSimple::ViewStart (int *x, int *y) const
1007 {
1008 *x = m_xScrollPosition;
1009 *y = m_yScrollPosition;
1010 }
1011
1012 #endif // wxUSE_TOOLBAR_SIMPLE