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