File: ASlider.cpp

package info (click to toggle)
audacity 0.98-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,896 kB
  • ctags: 4,089
  • sloc: cpp: 26,099; ansic: 4,961; sh: 2,465; makefile: 156; perl: 23
file content (114 lines) | stat: -rw-r--r-- 2,789 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**********************************************************************

  Audacity: A Digital Audio Editor

  ASlider.cpp

  Dominic Mazzoni

  This class is a custom slider (currently used just for the volume
  control).  It is not very customizable in the sense that the
  background image must be exactly the size you want it, but it does
  allow for a slicker look and feel by allowing you to use images
  for the slider background and the thumb.

**********************************************************************/

#include <wx/dcclient.h>
#include <wx/dcmemory.h>
#include <wx/image.h>

#include "AButton.h"
#include "ASlider.h"

BEGIN_EVENT_TABLE(ASlider, wxWindow)
    EVT_MOUSE_EVENTS(ASlider::OnMouseEvent)
    EVT_PAINT(ASlider::OnPaint)
    END_EVENT_TABLE()

ASlider::ASlider(wxWindow * parent, wxWindowID id,
                     const wxPoint & pos,
                     const wxSize & size,
                     char **sliderXPM,
                     char **thumbXPM,
                     int max):wxWindow(parent, id, pos, size)
{
   mMax = max;
   mValue = 0;

   mIsDragging = false;

   mBitmap = new wxBitmap((const char **) sliderXPM);

   mThumbBitmap = new wxBitmap((const char **) thumbXPM);

   mThumbWidth = mThumbBitmap->GetWidth();
   mThumbHeight = mThumbBitmap->GetHeight();

   GetSize(&mWidth, &mHeight);
}

ASlider::~ASlider()
{
   delete mBitmap;
   delete mThumbBitmap;
}

void ASlider::OnPaint(wxPaintEvent & event)
{
   wxPaintDC dc(this);
   int thumbPos = mValue * (mWidth - mThumbWidth) / mMax;
   int thumbY = (mHeight - mThumbHeight) / 2;

#if defined(__WXMAC__) || defined(__WXMSW__)
   dc.DrawBitmap(*mBitmap, 0, 0);
   dc.DrawBitmap(*mThumbBitmap, thumbPos, thumbY);
#else
   wxMemoryDC memDC;
   memDC.SelectObject(*mBitmap);
   dc.Blit(0, 0, mWidth, mHeight, &memDC, 0, 0, wxCOPY, FALSE);
   memDC.SelectObject(*mThumbBitmap);
   dc.Blit(thumbPos, thumbY, mThumbWidth, mThumbHeight,
           &memDC, 0, 0, wxCOPY, FALSE);
#endif
}

void ASlider::OnMouseEvent(wxMouseEvent & event)
{
   if (event.ButtonDown()) {
      mIsDragging = true;
      CaptureMouse();
      mInitialX = event.m_x;
      mInitialY = event.m_y;
      mInitialPos = mValue * (mWidth - mThumbWidth) / mMax;
   }

   if (event.ButtonUp()) {
      mIsDragging = false;
      ReleaseMouse();
   }

   if (mIsDragging) {
      int newPos = mInitialPos + (event.m_x - mInitialX);
      int newValue = newPos * mMax / (mWidth - mThumbWidth);
      if (newValue < 0)
         newValue = 0;
      if (newValue > mMax)
         newValue = mMax;
      if (newValue != mValue) {
         mValue = newValue;
         this->Refresh(false);
      }
   }
}

int ASlider::Get()
{
   return mValue;
}

void ASlider::Set(int value)
{
   mValue = value;
   this->Refresh(false);
}