summaryrefslogtreecommitdiff
path: root/iup/src/win/iupwin_val.c
blob: 5c956d9a2dedd36e37286270a964d1b645cece58 (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
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
326
/** \file
 * \brief Valuator Control
 *
 * See Copyright Notice in "iup.h"
 */

#include <windows.h>
#include <commctrl.h>

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

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

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

#include "iupwin_drv.h"
#include "iupwin_handle.h"
#include "iupwin_draw.h"


#ifndef SHRT_MAX
#define SHRT_MAX 32767
#endif

void iupdrvValGetMinSize(Ihandle* ih, int *w, int *h)
{
  int ticks_size = 0;
  if (iupAttribGetInt(ih, "SHOWTICKS"))
  {
    char* tickspos = iupAttribGetStr(ih, "TICKSPOS");
    if(iupStrEqualNoCase(tickspos, "BOTH"))
      ticks_size = 2*8;
    else 
      ticks_size = 8;
  }

  if (ih->data->type == IVAL_HORIZONTAL)
  {
    *w = 35;
    *h = 30+ticks_size;
  }
  else
  {
    *w = 30+ticks_size;
    *h = 35;
  }
}

static int winValSetBgColorAttrib(Ihandle *ih, const char *value)
{
  (void)value;
  iupdrvPostRedraw(ih);
  return 1;
}

static int winValSetStepAttrib(Ihandle* ih, const char* value)
{
  int linesize;
  ih->data->step = atof(value);
  linesize = (int)(ih->data->step*SHRT_MAX);
  SendMessage(ih->handle, TBM_SETLINESIZE, 0, linesize);
  return 0; /* do not store value in hash table */
}

static int winValSetPageStepAttrib(Ihandle* ih, const char* value)
{
  int pagesize;
  ih->data->pagestep = atof(value);
  pagesize = (int)(ih->data->pagestep*SHRT_MAX);
  SendMessage(ih->handle, TBM_SETPAGESIZE, 0, pagesize);
  return 0; /* do not store value in hash table */
}

static int winValSetShowTicksAttrib(Ihandle* ih, const char* value)
{
  int tick_freq, show_ticks;

  if (!ih->data->show_ticks)  /* can only set if already not zero */
    return 0;

  show_ticks = atoi(value);
  if (show_ticks<2) show_ticks=2;
  ih->data->show_ticks = show_ticks;

  /* Defines the interval frequency for tick marks */
  tick_freq = SHRT_MAX/(show_ticks-1);
  SendMessage(ih->handle, TBM_SETTICFREQ, tick_freq, 0);
  return 0;
}

static int winValSetValueAttrib(Ihandle* ih, const char* value)
{
  int ival;

  ih->data->val = atof(value);
  iupValCropValue(ih);

  ival = (int)(((ih->data->val-ih->data->vmin)/(ih->data->vmax - ih->data->vmin))*SHRT_MAX);
  if (ih->data->inverted)
    ival = SHRT_MAX-ival;

  SendMessage(ih->handle, TBM_SETPOS, TRUE, ival);
  return 0; /* do not store value in hash table */
}


/*********************************************************************************************/


static int winValCustomScroll(Ihandle* ih, int msg)
{
  double old_val = ih->data->val;
  int ival;
  IFn cb;

  ival = (int)SendMessage(ih->handle, TBM_GETPOS, 0, 0);
  if (ih->data->inverted)
    ival = SHRT_MAX-ival;

  ih->data->val = (((double)ival/(double)SHRT_MAX)*(ih->data->vmax - ih->data->vmin)) + ih->data->vmin;
  iupValCropValue(ih);

  cb = (IFn)IupGetCallback(ih, "VALUECHANGED_CB");
  if (cb)
  {
    if (ih->data->val == old_val)
      return 0;

    cb(ih);
  }
  else
  {
    IFnd cb_old = NULL;
    switch (msg)
    {
      case TB_BOTTOM:
      case TB_TOP:
      case TB_LINEDOWN:
      case TB_LINEUP:
      case TB_PAGEDOWN:
      case TB_PAGEUP:
      {
        cb_old = (IFnd) IupGetCallback(ih, "BUTTON_PRESS_CB");
        break;
      }
      case TB_THUMBPOSITION:
      {
        cb_old = (IFnd) IupGetCallback(ih, "BUTTON_RELEASE_CB");
        break;
      }
      case TB_THUMBTRACK:
      {
        cb_old = (IFnd) IupGetCallback(ih, "MOUSEMOVE_CB");
        break;
      }
    }
    if (cb_old)
      cb_old(ih, ih->data->val);
  }

  return 0; /* not used */
}

static void winValIncPageValue(Ihandle *ih, int dir)
{
  int pagesize, ival;
  pagesize = (int)(ih->data->pagestep*SHRT_MAX);

  ival = (int)SendMessage(ih->handle, TBM_GETPOS, 0, 0);
  ival += dir*pagesize;
  if (ival < 0) ival = 0;
  if (ival > SHRT_MAX) ival = SHRT_MAX;
  SendMessage(ih->handle, TBM_SETPOS, TRUE, ival);

  winValCustomScroll(ih, 0);
}

static int winValCtlColor(Ihandle* ih, HDC hdc, LRESULT *result)
{
  COLORREF cr;
  if (iupwinGetParentBgColor(ih, &cr))
  {
    SetBkColor(hdc, cr);
    SetDCBrushColor(hdc, cr);
    *result = (LRESULT)GetStockObject(DC_BRUSH);
    return 1;
  }
  return 0;
}

static int winValProc(Ihandle* ih, UINT msg, WPARAM wp, LPARAM lp, LRESULT *result)
{
  (void)lp;

  switch (msg)
  {
  case WM_ERASEBKGND:
    {
      RECT rect;
      HDC hDC = (HDC)wp;
      GetClientRect(ih->handle, &rect); 
      iupwinDrawParentBackground(ih, hDC, &rect);
      /* return non zero value */
      *result = 1;
      return 1;
    }
  case WM_KEYDOWN:
  case WM_SYSKEYDOWN:
    {
      if (iupwinBaseProc(ih, msg, wp, lp, result)==1)
        return 1;

      if (GetKeyState(VK_CONTROL) & 0x8000)  /* handle Ctrl+Arrows */
      {
        if (wp == VK_UP || wp == VK_LEFT)
        {
          winValIncPageValue(ih, -1);
          *result = 0;
          return 1;
        }
        if (wp == VK_RIGHT || wp == VK_DOWN)
        {
          winValIncPageValue(ih, 1);
          *result = 0;
          return 1;
        }
      }
      return 0;
    }
  }

  return iupwinBaseProc(ih, msg, wp, lp, result);
}


/*********************************************************************************************/


static int winValMapMethod(Ihandle* ih)
{
  DWORD dwStyle = WS_CHILD | WS_CLIPSIBLINGS | TBS_AUTOTICKS;
  int show_ticks;

  if (!ih->parent)
    return IUP_ERROR;

  /* Track bar Orientation */
  if (ih->data->type == IVAL_HORIZONTAL)
    dwStyle |= TBS_HORZ;
  else
    dwStyle |= TBS_VERT;

  if (iupAttribGetBoolean(ih, "CANFOCUS"))
    dwStyle |= WS_TABSTOP;

  /* Track bar Ticks */
  show_ticks = iupAttribGetInt(ih, "SHOWTICKS");
  if (!show_ticks)
  {
    dwStyle |= TBS_NOTICKS;    /* No show_ticks */
  }
  else
  {
    char* tickspos;

    if (show_ticks<2) show_ticks=2;
    ih->data->show_ticks = show_ticks; /* non zero value, can be changed later, but not to zero */

    /* Defines the position of tick marks */
    tickspos = iupAttribGetStr(ih, "TICKSPOS");
    if(iupStrEqualNoCase(tickspos, "BOTH"))
      dwStyle |= TBS_BOTH;
    else if(iupStrEqualNoCase(tickspos, "REVERSE"))
      dwStyle |= TBS_BOTTOM;  /* same as TBS_RIGHT */
    else /* NORMAL */
      dwStyle |= TBS_TOP;     /* same as TBS_LEFT  */
  }

  if (!iupwinCreateWindowEx(ih, TRACKBAR_CLASS, 0, dwStyle))
    return IUP_ERROR;

  /* Process Keyboard */
  IupSetCallback(ih, "_IUPWIN_CTRLPROC_CB", (Icallback)winValProc);

  /* Process Val Scroll commands */
  IupSetCallback(ih, "_IUPWIN_CUSTOMSCROLL_CB", (Icallback)winValCustomScroll);

  /* Process background color */
  IupSetCallback(ih, "_IUPWIN_CTLCOLOR_CB", (Icallback)winValCtlColor);

  /* configure the native range */
  SendMessage(ih->handle, TBM_SETRANGEMIN, FALSE, 0);
  SendMessage(ih->handle, TBM_SETRANGEMAX, FALSE, SHRT_MAX);

  if (ih->data->inverted)
    SendMessage(ih->handle, TBM_SETPOS, FALSE, SHRT_MAX);  /* default initial position is at MIN */

  return IUP_NOERROR;
}

void iupdrvValInitClass(Iclass* ic)
{
  /* Driver Dependent Class functions */
  ic->Map = winValMapMethod;

  /* IupVal only */
  iupClassRegisterAttribute(ic, "VALUE", iupValGetValueAttrib, winValSetValueAttrib, NULL, NULL, IUPAF_NO_DEFAULTVALUE|IUPAF_NO_INHERIT);  
  iupClassRegisterAttribute(ic, "SHOWTICKS", iupValGetShowTicksAttrib, winValSetShowTicksAttrib, IUPAF_SAMEASSYSTEM, "0", IUPAF_DEFAULT);
  iupClassRegisterAttribute(ic, "PAGESTEP", iupValGetPageStepAttrib, winValSetPageStepAttrib, "0.1", NULL, IUPAF_NO_INHERIT);  /* force new default value */
  iupClassRegisterAttribute(ic, "STEP", iupValGetStepAttrib, winValSetStepAttrib, "0.01", NULL, IUPAF_NO_INHERIT);   /* force new default value */

  iupClassRegisterAttribute(ic, "TICKSPOS", NULL, NULL, "NORMAL", NULL, IUPAF_NOT_MAPPED);

  /* Visual */
  iupClassRegisterAttribute(ic, "BGCOLOR", NULL, winValSetBgColorAttrib, IUPAF_SAMEASSYSTEM, "DLGBGCOLOR", IUPAF_DEFAULT);
}