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