summaryrefslogtreecommitdiff
path: root/iup/src/iup_spin.c
blob: 514703f1a404a16283a100fa8aecf1ac2892abc7 (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
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
/** \file
 * \brief Spin control
 *
  * See Copyright Notice in "iup.h"
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "iup.h"
#include "iupcbs.h"
#include "iupkey.h"

#include "iup_object.h"
#include "iup_attrib.h"
#include "iup_str.h"
#include "iup_stdcontrols.h"
#include "iup_childtree.h"


static Ihandle* spin_timer = NULL;


static int iSpinCallCB(Ihandle* ih, int dub, int ten, int sign)
{
  IFni cb;

  /* get the callback on the spin or on the spinbox */
  Ihandle* spinbox = (Ihandle*)iupAttribGet(ih->parent, "_IUPSPIN_BOX");
  if (spinbox) 
    ih = spinbox;
  else
    ih = ih->parent;

  cb = (IFni) IupGetCallback(ih, "SPIN_CB");
  if (cb) 
  {
    return cb(ih, sign*(dub && ten ? 100 :
                               ten ?  10 : 
                               dub ?   2 : 1));
  }

  return IUP_DEFAULT;
}

static int iSpinTimerCB(Ihandle* ih)
{
  Ihandle* spin_button = (Ihandle*)iupAttribGet(ih, "_IUPSPIN_BUTTON");
  char* status   = iupAttribGet(ih, "_IUPSPIN_STATUS");
  int spin_dir   = iupAttribGetInt(ih, "_IUPSPIN_DIR");
  int count      = iupAttribGetInt(ih, "_IUPSPIN_COUNT");
  char* reconfig = NULL;

  if(count == 0) /* first time */
    reconfig = "50";
  else if(count == 14) /* 300 + 14*50 = 1000 (1 second) */
    reconfig = "25";
  else if(count == 34) /* 300 + 14*50 + 20*50 = 2000 (2 seconds) */
    reconfig = "10";

  if (reconfig)
  {
    IupSetAttribute(ih, "RUN", "NO");
    IupSetAttribute(ih, "TIME", reconfig);
    IupSetAttribute(ih, "RUN", "YES");
  }

  iupAttribSetInt(ih, "_IUPSPIN_COUNT", count + 1);

  return iSpinCallCB(spin_button, iup_isshift(status), iup_iscontrol(status), spin_dir);
}

static void iSpinPrepareTimer(Ihandle* ih, char* status, char* dir)
{
  (void)ih;

  iupAttribSetStr(spin_timer, "_IUPSPIN_BUTTON", (char*)ih);

  iupAttribStoreStr(spin_timer, "_IUPSPIN_STATUS", status);

  iupAttribSetStr(spin_timer, "_IUPSPIN_DIR",   dir);
  iupAttribSetStr(spin_timer, "_IUPSPIN_COUNT", "0");

  IupSetAttribute(spin_timer, "TIME",           "400");
  IupSetAttribute(spin_timer, "RUN",            "YES");
}

static int iSpinK_SP(Ihandle* ih)
{
  int dir = iupAttribGetInt(ih, "_IUPSPIN_DIR");
  
  return iSpinCallCB(ih, 0, 0, dir);
}

static int iSpinK_sSP(Ihandle* ih)
{
  int dir = iupAttribGetInt(ih, "_IUPSPIN_DIR");
  
  return iSpinCallCB(ih, 1, 0, dir);
}

static int iSpinK_cSP(Ihandle* ih)
{
  int dir = iupAttribGetInt(ih, "_IUPSPIN_DIR");
  
  return iSpinCallCB(ih, 0, 1, dir);
}

static int iSpinButtonCB(Ihandle* ih, int but, int pressed, int x, int y, char* status)
{
  (void)x;
  (void)y;
 
  if (pressed && but == IUP_BUTTON1)
  {
    int dir = iupAttribGetInt(ih, "_IUPSPIN_DIR");
    
    iSpinPrepareTimer(ih, status, iupAttribGet(ih, "_IUPSPIN_DIR"));
    
    return iSpinCallCB(ih, iup_isshift(status), iup_iscontrol(status), dir);
  }
  else if (!pressed && but == IUP_BUTTON1)
  {
    IupSetAttribute(spin_timer, "RUN", "NO");
  }
  
  return IUP_DEFAULT;
}

static int iSpinCreateMethod(Ihandle* ih, void** params)
{
  Ihandle* bt_up;
  Ihandle* bt_down;
  (void)params;

  /* Button UP */
  bt_up = IupButton(NULL, NULL);
  
  IupSetAttribute(bt_up, "EXPAND", "NO");
  IupSetAttribute(bt_up, "IMAGE",  "IupSpinUpImage");
  IupSetAttribute(bt_up, "_IUPSPIN_DIR", "1");
  IupSetAttribute(bt_up, "CANFOCUS", "NO");

  IupSetCallback(bt_up, "BUTTON_CB", (Icallback) iSpinButtonCB);
  IupSetCallback(bt_up, "K_SP",      (Icallback) iSpinK_SP);
  IupSetCallback(bt_up, "K_sSP",     (Icallback) iSpinK_sSP);
  IupSetCallback(bt_up, "K_cSP",     (Icallback) iSpinK_cSP);

  /* Button DOWN */
  bt_down = IupButton(NULL, NULL);
  
  IupSetAttribute(bt_down, "EXPAND", "NO");
  IupSetAttribute(bt_down, "IMAGE",  "IupSpinDownImage");
  IupSetAttribute(bt_down, "_IUPSPIN_DIR", "-1");
  IupSetAttribute(bt_down, "CANFOCUS", "NO");

  IupSetCallback(bt_down, "BUTTON_CB", (Icallback) iSpinButtonCB);
  IupSetCallback(bt_down, "K_SP",      (Icallback) iSpinK_SP);
  IupSetCallback(bt_down, "K_sSP",     (Icallback) iSpinK_sSP);
  IupSetCallback(bt_down, "K_cSP",     (Icallback) iSpinK_cSP);

  /* manually add the buttons as a children */
  ih->firstchild = bt_up;
  bt_up->parent = ih;
  bt_up->brother = bt_down;
  bt_down->parent = ih;
  
  IupSetAttribute(ih, "GAP",    "0");
  IupSetAttribute(ih, "MARGIN", "0x0");

  return IUP_NOERROR;
}

static int iSpinboxCreateMethod(Ihandle* ih, void** params)
{
  Ihandle *spin;

  if (!params || !(params[0]))
    return IUP_ERROR;

  IupSetAttribute(ih, "GAP",       "0");
  IupSetAttribute(ih, "MARGIN",    "0x0");
  IupSetAttribute(ih, "ALIGNMENT", "ACENTER");

  /* IupText is already a child of Spinbox because of the IupHbox Create method */

  spin = IupSpin();
  iupChildTreeAppend(ih, spin);

  iupAttribSetStr(spin, "_IUPSPIN_BOX", (char*)ih);

  return IUP_NOERROR;
}

static void iSpinLoadImages(void)
{
  Ihandle* img;

  /* Spin UP image */
  unsigned char iupspin_up_img[] = {
    1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 0, 1, 1, 1, 1,
    1, 1, 1, 0, 0, 0, 1, 1, 1,
    1, 1, 0, 0, 0, 0, 0, 1, 1,
    1, 0, 0, 0, 0, 0, 0, 0, 1,
    0, 0, 0, 0, 0, 0, 0, 0, 0
  };

  /* Spin DOWN image */
  unsigned char iupspin_down_img[] = {
    0, 0, 0, 0, 0, 0, 0, 0, 0,
    1, 0, 0, 0, 0, 0, 0, 0, 1,
    1, 1, 0, 0, 0, 0, 0, 1, 1,
    1, 1, 1, 0, 0, 0, 1, 1, 1,
    1, 1, 1, 1, 0, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1
  };

  img = IupImage(9, 6, iupspin_up_img);
  IupSetAttribute(img, "0", "0 0 0"); 
  IupSetAttribute(img, "1", "BGCOLOR"); 
  IupSetHandle("IupSpinUpImage", img); 

  img = IupImage(9, 6, iupspin_down_img);
  IupSetAttribute(img, "0", "0 0 0"); 
  IupSetAttribute(img, "1", "BGCOLOR"); 
  IupSetHandle("IupSpinDownImage", img); 
}

static void iSpinReleaseMethod(Iclass* ic)
{
  (void)ic;

  if (spin_timer)
  {
    /* no need to destroy the images, they will be destroyed by IupClose automatically */
    IupDestroy(spin_timer);
    spin_timer = NULL;
  }
}

Iclass* iupSpinboxGetClass(void)
{
  Iclass* ic = iupClassNew(iupHboxGetClass());

  ic->name = "spinbox";
  ic->format = "h"; /* one Ihandle */
  ic->nativetype = IUP_TYPEVOID;
  ic->childtype = IUP_CHILD_ONE; /* fake value to define it as a container */
  ic->is_interactive = 0;

  iupClassRegisterCallback(ic, "SPIN_CB", "i");

  /* Class functions */
  ic->Create = iSpinboxCreateMethod;

  return ic;
}

Iclass* iupSpinGetClass(void)
{
  Iclass* ic = iupClassNew(iupVboxGetClass());

  ic->name = "spin";
  ic->format = NULL;  /* no parameters */
  ic->nativetype = IUP_TYPEVOID;
  ic->childtype = IUP_CHILD_ONE; /* fake value to define it as a container */
  ic->is_interactive = 0;

  /* Class functions */
  ic->Create = iSpinCreateMethod;
  ic->Release = iSpinReleaseMethod;

  iupClassRegisterCallback(ic, "SPIN_CB", "i");

  if (!spin_timer)
  {
    iSpinLoadImages();

    spin_timer = IupTimer();
    IupSetCallback(spin_timer, "ACTION_CB", (Icallback) iSpinTimerCB);
  }

  return ic;
}

Ihandle* IupSpin(void)
{
  return IupCreate("spin");
}

Ihandle* IupSpinbox(Ihandle* ctrl)
{
  void *params[2];
  params[0] = (void*)ctrl;
  params[1] = NULL; /* must add NULL because spinbox inherit from Hbox */
  return IupCreatev("spinbox", params);
}