File: dial.hc

package info (click to toggle)
craft 3.5-10
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 18,000 kB
  • ctags: 1,602
  • sloc: cpp: 3,794; makefile: 2,319; ansic: 857; sh: 385
file content (325 lines) | stat: -rw-r--r-- 7,513 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*======================================================================*/
/*= CHANGES AND UPDATES                                                =*/
/*======================================================================*/
/*= date   person file       subject                                   =*/
/*=--------------------------------------------------------------------=*/
/*=                                                                    =*/
/*= 291193 hua    dial.hc    created                                   =*/
/*=                                                                    =*/
/*======================================================================*/

#include "dial.h"

#define button_dx 12
#define button_dy 8
 
#define val_dy    20

dial::dial (win  *p_w,
            char p_title [],
            int  p_title_dx, 
            int  p_x,
            int  p_y,
            int  p_v_min,
            int  p_val,
            int  p_v_max,
            int  p_incr,
            bool with_hist,
            int  values_dx,
            bool may_edit,
            int  p_incr2,
            bool auto_repeat)

  {store_params;
   show_buttons;
   show_msg;
   init_history;
   w->tick ();

.  store_params
     {w         = p_w;
      x         = p_x;
      y         = p_y;
      v_min     = p_v_min;
      v_max     = p_v_max;
      incr      = p_incr;
      incr2     = p_incr2;
      val_dx    = values_dx;
      title_dx  = p_title_dx;
      is_bool   = false;
      is_hist   = with_hist; 
      with_edit = may_edit;
      strcpy  (title, p_title);
      sprintf (s_value, "%d", p_val);
     }.

.  show_msg
     {w->write (x, y + val_dy - 5, title);
     }.

.  show_buttons
     {g_value = new Getline ("dial", w, s_value,
                             x + title_dx, y,
                             val_dx, val_dy); 
      up   = new button (w, "/dial.up", 
                             x + title_dx + val_dx + 2,
                             y, auto_repeat, by_default, by_default,1,1);
      down = new button (w, "/dial.down",
                             x + title_dx + val_dx + 2,
                             y+button_dy+2, auto_repeat,
                             by_default, by_default,1,1);
      if (incr2 != 0)
         {up2   = new button (w, "/dial.up2", 
                                 x + title_dx + val_dx + 18,
                                 y, auto_repeat, by_default, by_default,1,1);
          down2 = new button (w, "/dial.down2",
                                 x + title_dx + val_dx + 18,
                                 y+button_dy+2,auto_repeat,
                                 by_default,by_default,1,1);
         };
     }.

.  init_history
     {if (is_hist)
         open_history;
     }.

.  open_history
     {hist_button = new button  (w, 
                                 "/hist.open",
                                 x + title_dx + val_dx + 20, y + button_dy/2);
      hist        = new history (title, hist_button);
     }.

  }


dial::dial (win  *p_w,
            char p_title [],
            int  p_title_dx, 
            int  p_x,
            int  p_y,
            bool p_val)

  {store_params;
   show_buttons;
   show_msg;
   w->tick ();

.  store_params
     {w        = p_w;
      x        = p_x;
      y        = p_y;
      title_dx = p_title_dx;
      is_bool  = true;
      strcpy  (title, p_title);
      b_val    = p_val;
      is_hist  = false;
     }.

.  show_msg
     {w->write (x, y + val_dy - 10, title);
     }.

.  show_buttons
     {if   (b_val)
           show_on
      else show_off;
     }.
 
.  show_off
     {up   = new button (w, "/dial.bool.off", 
                            x + title_dx + 2,
                            y, false, by_default, by_default,0,0);
      up->press (b_val);
     }.

.  show_on
     {up   = new button (w, "/dial.bool.on", 
                            x + title_dx + 2,
                            y, false, by_default, by_default,0,0);
      up->press (b_val);
     }.

  }

dial::~dial ()
  {delete (up);
   if (! is_bool)
      {delete (g_value);
       delete (down);
       if (incr2 != 0)
          {delete (up2);
           delete (down2);
          };
      };
   if (is_hist)
      {delete (hist);
       delete (hist_button);
      };
  }

void dial::set (int val)
  {if   (is_bool)
        perform_bool_set
   else perform_int_set;

.  perform_bool_set
     {b_val = val;
      if   (b_val)
           up->write ("/dial.bool.on");
      else up->write ("/dial.bool.off");
      }.

.  perform_int_set
     {sprintf          (s_value, "%d", val);
      g_value->refresh ();
     }.

  }

bool dial::eval (int &val)
  {bool any_change = false;

   if   (is_bool)
        perform_bool_eval
   else perform_int_eval;
   return any_change;

.  perform_bool_eval
     {if (up->eval ())
         handle_bool_event;
     }.

.  handle_bool_event
     {any_change = true;
      b_val = ! b_val;
      show_bool_button;
      up->press (b_val);
      val = b_val;
      any_change = true;
     }.

.  show_bool_button
      {if   (b_val)
            up->write ("/dial.bool.on");
       else up->write ("/dial.bool.off");
      }.

.  perform_int_eval
     {set_value;
      check_up;
      check_down;
      if (incr2 != 0) 
         {check_up2;
          check_down2;
         };
      check_hist_button;
      show_value;
      return any_change;
     }.

.  add_to_hist
     {char v [128];

      sprintf (v, "%d", val);
      if (any_change && is_hist)
         hist->push (v);
     }.

.  set_value
     {sprintf (s_value, "%d", val);
     }.

.  show_value
     {set_value;
      if (with_edit && g_value->get ())
         {val        = atoi (s_value);
          val        = i_min (v_max, i_max (v_min, val));
          any_change = true;
          set_value;
          g_value->refresh ();
          add_to_hist;
         };
     }.  
 
.  check_hist_button
     {if (is_hist && hist_button->eval ())
         handle_hist;
     }.

.  handle_hist
     {char hist_line [256];

      hist_button->press (true);
      strcpy             (hist_line, hist->select (by_fix,by_fix,val_dx,140));
      hist_button->press (false);
      if (strlen (hist_line) > 0)
         use_hist_line;
     }.

.  use_hist_line
     {strcpy           (s_value, hist_line);
      g_value->refresh ();
      val        = atoi (s_value);
      any_change = true;
     }.

.  check_up
     {if (up->eval ())
        handle_up;
     }.

.  handle_up
     {up->press (true);
      any_change = true;
      val = i_min (v_max, val + incr);
      sprintf          (s_value, "%d", val);
      g_value->refresh ();
      up->press (false);
     }.

.  check_down
     {if (down->eval ())
         handle_down;
     }.

.  handle_down
     {down->press (true);
      any_change = true;
      val = i_max (v_min, val - incr);
      sprintf          (s_value, "%d", val);
      g_value->refresh ();
      down->press (false);
     }.
   
.  check_up2
     {if (up2->eval ())
        handle_up2;
     }.

.  handle_up2
     {up2->press (true);
      any_change = true;
      val = i_min (v_max, val + incr2);
      sprintf          (s_value, "%d", val);
      g_value->refresh ();
      up2->press (false);
     }.

.  check_down2
     {if (down2->eval ())
         handle_down2;
     }.

.  handle_down2
     {down2->press (true);
      any_change = true;
      val = i_max (v_min, val - incr2);
      sprintf          (s_value, "%d", val);
      g_value->refresh ();
      down2->press (false);
     }.
   
  }