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