File: label_field_slider.h

package info (click to toggle)
din 5.2.1-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,180 kB
  • ctags: 2,511
  • sloc: cpp: 9,369; sh: 6,563; ansic: 2,977; tcl: 1,770; makefile: 283
file content (144 lines) | stat: -rw-r--r-- 2,969 bytes parent folder | download | duplicates (3)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef __label_field_slider
#define __label_field_slider

#include "font.h"
#include "label.h"
#include "field.h"
#include "slider.h"
#include "filled_button.h"

#include <string>
#include <sstream>
#include <fstream>
#include <iostream>

extern font fnt;

template <typename T> struct val_handler {
  virtual void operator() (const T& t) = 0;
};

template <typename T> class label_field_slider : public widget, change_listener< slider<T> >, change_listener<field> {

  filled_button fbtn;
  label lbl;
  field fld;
  slider<T> sld;

  val_handler<T>& vhan;

  void advance_right (int& x, widget& w) {
    static const int XSPACING = 10;
    const box<int>& e = w.get_extents ();
    x += (e.width + XSPACING);
  }

  public:

    label_field_slider (int x, int y, const std::string& t, int wf, int ws, int hs, val_handler<T>& vh) : lbl (t), fld (wf), sld (ws, hs), vhan (vh) {

      set_pos (x, y);

      sld.add_listener (this);
      fld.set_listener (this);

      add_child (&lbl);
      lbl.add_child (&fbtn);
      lbl.add_child (&fld);
      lbl.add_child (&sld);
      lbl.set_moveable (1);

    }

    void update () {
      lbl.update ();
      fld.update ();
      int x, y; get_pos (x, y); set_pos (x, y);
    }

    void set_pos (int x, int y) {

      widget::set_pos (x, y);

      fbtn.set_pos (x, y + fnt.lift);
      advance_right (x, fbtn);

      lbl.set_pos (x, y);
      advance_right (x, lbl);

      sld.set_pos (x, y);
      advance_right (x, sld);

      fld.set_pos (x, y);

    }

    void update_pos () {
        int x1, y1; fbtn.get_pos (x1, y1);
        int x2, y2; lbl.get_pos (x2, y2);
        widget::set_pos (x1, y2);
    }

    void save (std::ofstream& file) {
      int x, y; get_pos (x, y);
      file << get_name () << ' ' << x << ' ' << y << std::endl;
    }

    int handle_input () {
      int t = fbtn.handle_input ();
      int l = lbl.handle_input ();
      if (l) update_pos ();
      t |= l;
      t |= fld.handle_input ();
      t |= sld.handle_input ();
      return t;
    }

    void draw () {
      fbtn.draw ();
      lbl.draw ();
      fld.draw ();
      sld.draw ();
    }

    void changed (slider<T>& s) {
      T v = s.get_val ();
      fld.set_text (v);
      vhan (v);
    }

    void changed (field& f) {
      T val = f;
      sld.set_val (val);
      vhan (sld.get_val ());
    }

    void set_color (float r, float g, float b) {
      fbtn.set_color (r, g, b);
      lbl.set_color (r, g, b);
      fld.set_color (r, g, b);
      sld.set_color (r, g, b);
    }

    const color& get_color () const {return fbtn.get_color ();}

    void set_limits (T lo, T hi) {
      sld.set_limits (lo, hi);
    }

    void get_limits (T& lo, T& hi) {
      sld.get_limits (lo, hi);
    }

    void set_val (const T& t) {
      sld.set_val (t);
    }

    void set_button_listener (click_listener* l) {
      fbtn.set_listener (l);
    }

    const std::string& get_text () const {return lbl.get_text();}
};

#endif