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