summaryrefslogtreecommitdiff
path: root/iup/src/iup_val.c
blob: 521e35afcf0460ae9a0159cd9a48883044b9c9d7 (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
/** \file
 * \brief Valuator 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 "iupcontrols.h"

#include "iup_object.h"
#include "iup_attrib.h"
#include "iup_str.h"
#include "iup_drv.h"
#include "iup_stdcontrols.h"
#include "iup_layout.h"
#include "iup_val.h"


void iupValCropValue(Ihandle* ih)
{
  if (ih->data->val > ih->data->vmax) 
    ih->data->val = ih->data->vmax;
  else if (ih->data->val < ih->data->vmin) 
    ih->data->val = ih->data->vmin;
}

char* iupValGetShowTicksAttrib(Ihandle* ih)
{
  char* str = iupStrGetMemory(30);
  sprintf(str, "%d", ih->data->show_ticks);
  return str;
}

char* iupValGetValueAttrib(Ihandle* ih)
{
  char* str = iupStrGetMemory(30);
  sprintf(str, "%g", ih->data->val);
  return str;
}

char* iupValGetStepAttrib(Ihandle* ih)
{
  char* str = iupStrGetMemory(30);
  sprintf(str, "%g", ih->data->step);
  return str;
}

char* iupValGetPageStepAttrib(Ihandle* ih)
{
  char* str = iupStrGetMemory(30);
  sprintf(str, "%g", ih->data->pagestep);
  return str;
}

static int iValSetMaxAttrib(Ihandle* ih, const char* value)
{
  ih->data->vmax = atof(value);
  iupValCropValue(ih);
  return 0; /* do not store value in hash table */
}

static char* iValGetMaxAttrib(Ihandle* ih)
{
  char* str = iupStrGetMemory(30);
  sprintf(str, "%g", ih->data->vmax);
  return str;
}

static int iValSetMinAttrib(Ihandle* ih, const char* value)
{
  ih->data->vmin = atof(value);
  iupValCropValue(ih);
  return 0; /* do not store value in hash table */
}

static char* iValGetMinAttrib(Ihandle* ih)
{
  char* str = iupStrGetMemory(30);
  sprintf(str, "%g", ih->data->vmin);
  return str;
}

static int iValSetTypeAttrib(Ihandle* ih, const char *value)
{
  int min_w, min_h;

  /* valid only before map */
  if (ih->handle)
    return 0;

  iupdrvValGetMinSize(ih, &min_w, &min_h);

  if (iupStrEqualNoCase(value, "VERTICAL"))
  {
    /* val natural vertical size is MinWx100 */
    IupSetfAttribute(ih, "RASTERSIZE", "%dx%d", min_w, 100);
    ih->data->type = IVAL_VERTICAL;
  }
  else /* "HORIZONTAL" */
  {
    /* val natural horizontal size is 100xMinH */
    IupSetfAttribute(ih, "RASTERSIZE", "%dx%d", 100, min_h);
    ih->data->type = IVAL_HORIZONTAL;
  }

  return 0; /* do not store value in hash table */
}

static char* iValGetTypeAttrib(Ihandle* ih)
{
  if (ih->data->type == IVAL_HORIZONTAL)
    return "HORIZONTAL";
  else /* (ih->data->type == IVAL_VERTICAL) */
    return "VERTICAL";
}

static int iValSetInvertedAttrib(Ihandle* ih, const char *value)
{
  /* valid only before map */
  if (ih->handle)
    return 0;

  if (iupStrBoolean(value))
    ih->data->inverted = 1;
  else
    ih->data->inverted = 0;

  return 0; /* do not store value in hash table */
}

static char* iValGetInvertedAttrib(Ihandle* ih)
{
  if (ih->data->inverted)
    return "YES";
  else
    return "NO";
}

static int iValCreateMethod(Ihandle* ih, void **params)
{
  char* type = "HORIZONTAL";
  if (params && params[0])
    type = params[0];

  ih->data = iupALLOCCTRLDATA();

  iValSetTypeAttrib(ih, type);
  if (ih->data->type == IVAL_VERTICAL)
    ih->data->inverted = 1;  /* default is YES when vertical */

  ih->data->vmax = 1.00;
  ih->data->step = 0.01;
  ih->data->pagestep = 0.10;

  return IUP_NOERROR; 
}

Iclass* iupValGetClass(void)
{
  Iclass* ic = iupClassNew(NULL);

  ic->name = "val";
  ic->format = "S"; /* one optional string */
  ic->nativetype = IUP_TYPECONTROL;
  ic->childtype = IUP_CHILDNONE;
  ic->is_interactive = 1;

  /* Class functions */
  ic->Create  = iValCreateMethod;
  ic->LayoutUpdate = iupdrvBaseLayoutUpdateMethod;
  ic->UnMap = iupdrvBaseUnMapMethod;

  /* Callbacks */
  iupClassRegisterCallback(ic, "VALUECHANGED_CB", "");

  /* Common Callbacks */
  iupBaseRegisterCommonCallbacks(ic);

  /* Common */
  iupBaseRegisterCommonAttrib(ic);

  /* Visual */
  iupBaseRegisterVisualAttrib(ic);

  /* IupVal only */
  iupClassRegisterAttribute(ic, "MAX", iValGetMaxAttrib, iValSetMaxAttrib, IUPAF_SAMEASSYSTEM, "1.0", IUPAF_NOT_MAPPED);
  iupClassRegisterAttribute(ic, "MIN", iValGetMinAttrib, iValSetMinAttrib, IUPAF_SAMEASSYSTEM, "0.0", IUPAF_NOT_MAPPED);
  iupClassRegisterAttribute(ic, "TYPE", iValGetTypeAttrib, iValSetTypeAttrib, IUPAF_SAMEASSYSTEM, "HORIZONTAL", IUPAF_NOT_MAPPED|IUPAF_NO_INHERIT);
  iupClassRegisterAttribute(ic, "INVERTED", iValGetInvertedAttrib, iValSetInvertedAttrib, NULL, NULL, IUPAF_NOT_MAPPED|IUPAF_NO_INHERIT);

  iupdrvValInitClass(ic);

  return ic;
}

Ihandle *IupVal(const char *type)
{
  void *params[2];
  params[0] = (void*)type;
  params[1] = NULL;
  return IupCreatev("val", params);
}