summaryrefslogtreecommitdiff
path: root/iup/src/iup_zbox.c
blob: dd4a0e4bd1ae20780d72fe55f7acf53cec112ee4 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/** \file
 * \brief Zbox Control.
 *
 * See Copyright Notice in "iup.h"
 */

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

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

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


enum{IZBOX_ALIGN_NORTH, IZBOX_ALIGN_SOUTH, IZBOX_ALIGN_WEST, IZBOX_ALIGN_EAST,
     IZBOX_ALIGN_NE, IZBOX_ALIGN_SE, IZBOX_ALIGN_NW, IZBOX_ALIGN_SW,
     IZBOX_ALIGN_ACENTER};

struct _IcontrolData 
{
  int alignment;
  Ihandle* value_handle;
};

static int iZboxCreateMethod(Ihandle* ih, void** params)
{
  ih->data = iupALLOCCTRLDATA();

  ih->data->alignment = IZBOX_ALIGN_NW;

  if (params)
  {
    Ihandle** iparams = (Ihandle**)params;
    while (*iparams)
    {
      IupAppend(ih, *iparams);
      iparams++;
    }
  }

  return IUP_NOERROR;
}

static void iZboxChildAddedMethod(Ihandle* ih, Ihandle* child)
{
  if (!ih->data->value_handle)
  {
    IupSetAttribute(child, "VISIBLE", "YES");
    ih->data->value_handle = child;
  }
  else
    IupSetAttribute(child, "VISIBLE", "NO");
}

static void iZboxChildRemovedMethod(Ihandle* ih, Ihandle* child)
{
  if (child == ih->data->value_handle)
  {
    /* reset to the first child, even if it is NULL */
    if (ih->firstchild)
      IupSetAttribute(ih->firstchild, "VISIBLE", "YES");
    ih->data->value_handle = ih->firstchild;
  }
}

static int iZboxSetAlignmentAttrib(Ihandle* ih, const char* value)
{
  if (iupStrEqualNoCase(value, "NORTH") || iupStrEqualNoCase(value, "ATOP"))
    ih->data->alignment = IZBOX_ALIGN_NORTH;
  else if (iupStrEqualNoCase(value, "SOUTH") || iupStrEqualNoCase(value, "ABOTTOM"))
    ih->data->alignment = IZBOX_ALIGN_SOUTH;
  else if (iupStrEqualNoCase(value, "WEST") || iupStrEqualNoCase(value, "ALEFT"))
    ih->data->alignment = IZBOX_ALIGN_WEST;
  else if (iupStrEqualNoCase(value, "EAST") || iupStrEqualNoCase(value, "ARIGHT"))
    ih->data->alignment = IZBOX_ALIGN_EAST;
  else if (iupStrEqualNoCase(value, "NE"))
    ih->data->alignment = IZBOX_ALIGN_NE;
  else if (iupStrEqualNoCase(value, "SE"))
    ih->data->alignment = IZBOX_ALIGN_SE;
  else if (iupStrEqualNoCase(value, "NW"))
    ih->data->alignment = IZBOX_ALIGN_NW;
  else if (iupStrEqualNoCase(value, "SW"))
    ih->data->alignment = IZBOX_ALIGN_SW;
  else if (iupStrEqualNoCase(value, "ACENTER"))
    ih->data->alignment = IZBOX_ALIGN_ACENTER;
  return 0;
}

static char* iZboxGetAlignmentAttrib(Ihandle* ih)
{
  static char* align2str[9] = {"NORTH", "SOUTH", "WEST", "EAST",
                               "NE", "SE", "NW", "SW",
                               "ACENTER"};
  return align2str[ih->data->alignment];
}

static int iZboxSetValueHandleAttrib(Ihandle* ih, const char* value)
{
  Ihandle* old_handle, *new_handle, *child;

  new_handle = (Ihandle*)value;
  if (!iupObjectCheck(new_handle))
    return 0;

  old_handle = ih->data->value_handle;
  if (!iupObjectCheck(old_handle))
    old_handle = NULL;

  if (old_handle == new_handle)
    return 0;

  for (child = ih->firstchild; child; child = child->brother)
  {
    if (child == new_handle) /* found child */
    {
      if (old_handle && old_handle != new_handle)
        IupSetAttribute(old_handle, "VISIBLE", "NO");

      IupSetAttribute(new_handle, "VISIBLE", "YES");
      ih->data->value_handle = new_handle;
      return 0;
    }
  }
 
  return 0;
}

static int iZboxSetValuePosAttrib(Ihandle* ih, const char* value)
{
  Ihandle* child;
  int pos, i;

  if (!iupStrToInt(value, &pos))
    return 0;

  for (i=0, child=ih->firstchild; child; child = child->brother, i++)
  {
    if (i == pos) /* found child */
    {
      iZboxSetValueHandleAttrib(ih, (char*)child);
      return 0;
    }
  }
 
  return 0;
}

static char* iZboxGetValuePosAttrib(Ihandle* ih)
{
  Ihandle* child;
  int pos;

  if (!iupObjectCheck(ih->data->value_handle))
    return NULL;

  for (pos=0, child = ih->firstchild; child; child = child->brother, pos++)
  {
    if (child == ih->data->value_handle) /* found child */
    {
      char *str = iupStrGetMemory(50);
      sprintf(str, "%d", pos);
      return str;
    }
  }

  return NULL;
}

static int iZboxSetValueAttrib(Ihandle* ih, const char* value)
{
  Ihandle *new_handle;

  if (!value)
    return 0;

  new_handle = IupGetHandle(value);
  if (!new_handle)
    return 0;

  iZboxSetValueHandleAttrib(ih, (char*)new_handle);

  return 0;
}

static char* iZboxGetValueAttrib(Ihandle* ih)
{
  Ihandle* child;
  int pos;

  if (!iupObjectCheck(ih->data->value_handle))
    return NULL;

  for (pos=0, child = ih->firstchild; child; child = child->brother, pos++)
  {
    if (child == ih->data->value_handle) /* found child, just cheking */
      return IupGetName(ih->data->value_handle);
  }

  return NULL;
}

static int iZboxSetVisibleAttrib(Ihandle* ih, const char* value)
{
  if (iupObjectCheck(ih->data->value_handle))
    IupSetAttribute(ih->data->value_handle, "VISIBLE", (char*)value);
  return 1;  /* must be 1 to mark when set at the element */
}

static void iZboxComputeNaturalSizeMethod(Ihandle* ih, int *w, int *h, int *expand)
{
  Ihandle* child;
  int children_expand, 
      children_naturalwidth, children_naturalheight;

  /* calculate total children natural size (even for hidden children) */
  children_expand = 0;
  children_naturalwidth = 0;
  children_naturalheight = 0;

  for (child = ih->firstchild; child; child = child->brother)
  {
    /* update child natural size first */
    iupBaseComputeNaturalSize(child);

    if (!child->is_floating)
    {
      children_expand |= child->expand;
      children_naturalwidth = iupMAX(children_naturalwidth, child->naturalwidth);
      children_naturalheight = iupMAX(children_naturalheight, child->naturalheight);
    }
  }

  *expand = children_expand;
  *w = children_naturalwidth;
  *h = children_naturalheight;
}

static void iZboxSetChildrenCurrentSizeMethod(Ihandle* ih, int shrink)
{
  Ihandle* child;
  for (child = ih->firstchild; child; child = child->brother)
  {
    if (!child->is_floating)
      iupBaseSetCurrentSize(child, ih->currentwidth, ih->currentheight, shrink);
  }
}

static void iZboxSetChildrenPositionMethod(Ihandle* ih, int x, int y)
{
  int dx = 0, dy = 0;
  Ihandle* child;

  for (child = ih->firstchild; child; child = child->brother)
  {
    if (!child->is_floating)
    {
      switch (ih->data->alignment)
      {
      case IZBOX_ALIGN_ACENTER:
        dx=(ih->currentwidth-child->currentwidth)/2;
        dy=(ih->currentheight-child->currentheight)/2;
        break;
      case IZBOX_ALIGN_NORTH:
        dx=(ih->currentwidth-child->currentwidth)/2;
        dy=0;
        break;
      case IZBOX_ALIGN_SOUTH:
        dx=(ih->currentwidth-child->currentwidth)/2;
        dy=ih->currentheight-child->currentheight;
        break;
      case IZBOX_ALIGN_WEST:
        dx=0;
        dy=(ih->currentheight-child->currentheight)/2;
        break;
      case IZBOX_ALIGN_EAST:
        dx=ih->currentwidth-child->currentwidth;
        dy=(ih->currentheight-child->currentheight)/2;
        break;
      case IZBOX_ALIGN_NE:
        dx=ih->currentwidth-child->currentwidth;
        dy=0;
        break;
      case IZBOX_ALIGN_SE:
        dx=ih->currentwidth-child->currentwidth;
        dy=ih->currentheight-child->currentheight;
        break;
      case IZBOX_ALIGN_SW:
        dx=0;
        dy=ih->currentheight-child->currentheight;
        break;
      case IZBOX_ALIGN_NW:
      default:
        dx=0;
        dy=0;
        break;
      }
      if (dx<0) dx = 0;
      if (dy<0) dy = 0;
                     
      /* update child */
      iupBaseSetPosition(child, x+dx, y+dy);
    }
  }
}


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


Ihandle *IupZboxv(Ihandle **children)
{
  return IupCreatev("zbox", (void**)children);
}

Ihandle *IupZbox(Ihandle* child, ...)
{
  Ihandle **children;
  Ihandle *ih;

  va_list arglist;
  va_start(arglist, child);
  children = (Ihandle **)iupObjectGetParamList(child, arglist);
  va_end(arglist);

  ih = IupCreatev("zbox", (void**)children);
  free(children);

  return ih;
}

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

  ic->name = "zbox";
  ic->format = "g"; /* array of Ihandle */
  ic->nativetype = IUP_TYPEVOID;
  ic->childtype = IUP_CHILDMANY;
  ic->is_interactive = 0;

  /* Class functions */
  ic->Create = iZboxCreateMethod;
  ic->Map = iupBaseTypeVoidMapMethod;
  ic->ChildAdded = iZboxChildAddedMethod;
  ic->ChildRemoved = iZboxChildRemovedMethod;

  ic->ComputeNaturalSize = iZboxComputeNaturalSizeMethod;
  ic->SetChildrenCurrentSize = iZboxSetChildrenCurrentSizeMethod;
  ic->SetChildrenPosition = iZboxSetChildrenPositionMethod;

  /* Common */
  iupBaseRegisterCommonAttrib(ic);

  /* Base Container */
  iupClassRegisterAttribute(ic, "EXPAND", iupBaseContainerGetExpandAttrib, NULL, IUPAF_SAMEASSYSTEM, "YES", IUPAF_NOT_MAPPED|IUPAF_NO_INHERIT);
  iupClassRegisterAttribute(ic, "CLIENTSIZE", iupBaseGetRasterSizeAttrib, NULL, NULL, NULL, IUPAF_READONLY|IUPAF_NOT_MAPPED|IUPAF_NO_INHERIT);

  /* Zbox only */
  iupClassRegisterAttribute(ic, "ALIGNMENT", iZboxGetAlignmentAttrib, iZboxSetAlignmentAttrib, IUPAF_SAMEASSYSTEM, "NW", IUPAF_NOT_MAPPED|IUPAF_NO_INHERIT);
  iupClassRegisterAttribute(ic, "VALUE", iZboxGetValueAttrib, iZboxSetValueAttrib, NULL, NULL, IUPAF_NOT_MAPPED|IUPAF_NO_INHERIT);
  iupClassRegisterAttribute(ic, "VALUEPOS", iZboxGetValuePosAttrib, iZboxSetValuePosAttrib, NULL, NULL, IUPAF_NOT_MAPPED|IUPAF_NO_INHERIT);
  iupClassRegisterAttribute(ic, "VALUE_HANDLE", NULL, iZboxSetValueHandleAttrib, NULL, NULL, IUPAF_NOT_MAPPED|IUPAF_NO_INHERIT|IUPAF_NO_STRING);

  /* Intercept VISIBLE since ZBOX works by showing and hidding its children */
  iupClassRegisterAttribute(ic, "VISIBLE", NULL, iZboxSetVisibleAttrib, IUPAF_SAMEASSYSTEM, "YES", IUPAF_NOT_MAPPED);

  return ic;
}