summaryrefslogtreecommitdiffstats
path: root/beos/Stepper.cpp
blob: 8f2733ac520a7817c1bd088773381737dc9e58d5 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <interface/Region.h>
#include <interface/TextControl.h>
#include <interface/Window.h>

#include "Stepper.h"

#include <stdio.h>

HBStepper::HBStepper( BRect rect, int step, int min, int max, int val,
                      BMessage * message )
    : BView( rect, NULL, B_FOLLOW_NONE, B_WILL_DRAW )
{
    fStep    = step;
    fMin     = min;
    fMax     = max;
    fMessage = message;

    BRect b = Bounds();

    fEnabled = true;

    fControl = new BTextControl( BRect( 0,1,b.Width()-14,b.Height()-1 ),
        NULL, NULL, "", new BMessage() );
    fControl->SetDivider( 0.0 );
    fControl->TextView()->MakeEditable( false );
    AddChild( fControl );

    SetValue( val );
}

void HBStepper::Draw( BRect rect )
{
    /* Why do we have to do this here!? */
    fControl->TextView()->MakeEditable( false );
    fControl->TextView()->MakeSelectable( false );

    BRect b = Bounds();
    BRegion region;

    SetHighColor( 128,128,128 ); /* Dark gray */
    region.MakeEmpty();
    region.Include( BRect(  3, 0,10, 0 ) );
    region.Include( BRect(  2, 1, 3, 1 ) );
    region.Include( BRect( 10, 1,11, 1 ) );
    region.Include( BRect(  1, 2, 2, 2 ) );
    region.Include( BRect( 11, 2,12, 2 ) );
    region.Include( BRect(  1, 2, 1,18 ) );
    region.Include( BRect(  1,10,12,10 ) );
    region.Include( BRect( 12, 2,12,18 ) );
    region.Include( BRect(  1,18, 2,18 ) );
    region.Include( BRect( 11,18,12,18 ) );
    region.Include( BRect(  2,19, 3,19 ) );
    region.Include( BRect( 10,19,11,19 ) );
    region.Include( BRect(  3,20,10,20 ) );
    region.OffsetBy( b.Width()-12,0 );
    FillRegion( &region );

    SetHighColor( 0,0,0 ); /* Black */
    region.MakeEmpty();
    region.Include( BRect(  6, 4, 7, 4 ) );
    region.Include( BRect(  5, 5, 8, 6 ) );
    region.Include( BRect(  5,14, 8,15 ) );
    region.Include( BRect(  6,16, 7,16 ) );
    region.OffsetBy( b.Width()-12,0 );
    FillRegion( &region );

    BView::Draw( rect );
}

void HBStepper::AttachedToWindow()
{
    if( Parent() )
    {
        SetViewColor( Parent()->ViewColor() );
    }
}

void HBStepper::MouseDown( BPoint point )
{
    BRect r, b = Bounds();

    if( !fEnabled )
    {
        return;
    }

    BMessenger messenger( Window() );

    r = BRect( 2,1,11,9 );
    r.OffsetBy( b.Width()-12,0 );
    if( r.Contains( point ) )
    {
        SetValue( fValue + fStep );
        messenger.SendMessage( fMessage );
    }
    r.OffsetBy( 0,10 );
    if( r.Contains( point ) )
    {
        SetValue( fValue - fStep );
        messenger.SendMessage( fMessage );
    }
}

void HBStepper::SetValue( int val )
{
    fValue = val;
    if( fValue < fMin )
    {
        fValue = fMin;
    }
    if( fValue > fMax )
    {
        fValue = fMax;
    }

    char text[16];
    snprintf( text, 16, "%d", fValue );
    fControl->SetText( text );
}

int HBStepper::Value()
{
    return fValue;
}

void HBStepper::SetEnabled( bool e )
{
    fEnabled = e;
    fControl->SetEnabled( fEnabled );
}