summaryrefslogtreecommitdiff
path: root/iup/src/iup_classattrib.c
blob: fc478bb923856921a1df320f2fe60d5013ee2fb0 (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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/** \file
 * \brief Ihandle Class Attribute Management
 *
 * See Copyright Notice in "iup.h"
 */

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

#include "iup.h"

#include "iup_object.h"
#include "iup_drv.h"
#include "iup_drvfont.h"
#include "iup_str.h"
#include "iup_attrib.h"
#include "iup_assert.h"
#include "iup_register.h"
#include "iup_globalattrib.h"


typedef struct _IattribFunc
{
  IattribGetFunc get;
  IattribSetFunc set;
  const char* default_value;
  const char* system_default;
  int call_global_default;
  int flags;
} IattribFunc;


static int iClassIsGlobalDefault(const char* name)
{
  if (iupStrEqual(name, "DEFAULTFONT"))
    return 1;
  if (iupStrEqual(name, "DLGBGCOLOR"))
    return 1;
  if (iupStrEqual(name, "DLGFGCOLOR"))
    return 1;
  if (iupStrEqual(name, "TXTBGCOLOR"))
    return 1;
  if (iupStrEqual(name, "TXTFGCOLOR"))
    return 1;
  if (iupStrEqual(name, "MENUBGCOLOR"))
    return 1;
  return 0;
}

static const char* iClassFindId(const char* name)
{
  while(*name)
  {
    if (*name >= '0' && *name <= '9')
      return name;
    if (*name == '*' || *name == ':' || *name == '-')
      return name;

    name++;
  }
  return NULL;
}

static const char* iClassCutNameId(const char* name, const char* name_id)
{
  char* str;
  int len = name_id - name;
  if (len == 0)
    return NULL;

  str = iupStrGetMemory(len+1);
  memcpy(str, name, len);
  str[len] = 0;
  return str;
}


static char* iClassGetDefaultValue(IattribFunc* afunc)
{
  if (afunc->call_global_default)
    return IupGetGlobal(afunc->default_value);
  else
    return (char*)afunc->default_value;
}

int iupClassObjectSetAttribute(Ihandle* ih, const char* name, const char * value, int *inherit)
{
  IattribFunc* afunc;

  if (ih->iclass->has_attrib_id)
  {
    const char* name_id = iClassFindId(name);
    if (name_id)
    {
      const char* partial_name = iClassCutNameId(name, name_id);
      if (!partial_name)
        partial_name = "IDVALUE";  /* pure numbers are used as attributes in IupList and IupMatrix, 
                                      translate them into IDVALUE. */
      afunc = (IattribFunc*)iupTableGet(ih->iclass->attrib_func, partial_name);
      if (afunc)
      {         
        *inherit = 0;       /* id numbered attributes are NON inheritable always */

        if (afunc->flags & IUPAF_READONLY)
        {
          if (afunc->flags & IUPAF_NO_STRING)
            return -1;  /* value is NOT a string, can NOT call iupAttribStoreStr */
          return 0;
        }

        if (afunc->set && (ih->handle || afunc->flags & IUPAF_NOT_MAPPED))
        {
          /* id numbered attributes have default value NULL always */
          IattribSetIdFunc id_set = (IattribSetIdFunc)afunc->set;
          return id_set(ih, name_id, value);
        }

        if (afunc->flags & IUPAF_NO_STRING)
          return -1; /* value is NOT a string, can NOT call iupAttribStoreStr */

        return 1; /* if the function exists, then must return here */
      }
    }
  }

  /* if not has_attrib_id, or not found an ID, or not found the partial name, check using the full name */

  afunc = (IattribFunc*)iupTableGet(ih->iclass->attrib_func, name);
  *inherit = 1; /* default is inheritable */
  if (afunc)
  {
    *inherit = !(afunc->flags & IUPAF_NO_INHERIT) &&   /* is inheritable */
               !(afunc->flags & IUPAF_NO_STRING);      /* is a string */

    if (afunc->flags & IUPAF_READONLY)
    {
      if (afunc->flags & IUPAF_NO_STRING)
        return -1;  /* value is NOT a string, can NOT call iupAttribStoreStr */
      return 0;
    }

    if (afunc->set && (ih->handle || afunc->flags & IUPAF_NOT_MAPPED))
    {
      int ret;
      if (!value)
      {
        /* inheritable attributes when reset must check the parent value */
        if (*inherit && ih->parent)   
          value = iupAttribGetInherit(ih->parent, name); 

        if (!value)
          value = iClassGetDefaultValue(afunc);
      }

      if (afunc->flags & IUPAF_HAS_ID)
      {
        IattribSetIdFunc id_set = (IattribSetIdFunc)afunc->set;
        return id_set(ih, "", value);  /* empty Id */
      }
      else
        ret = afunc->set(ih, value);

      if (ret == 1 && afunc->flags & IUPAF_NO_STRING)
        return -1;  /* value is NOT a string, can NOT call iupAttribStoreStr */

      if (*inherit)
        return 1;   /* inheritable attributes are always stored in the hash table, */
      else          /* to indicate that they are set at the control.               */
        return ret;
    }
  }

  return 1;
}

char* iupClassObjectGetAttribute(Ihandle* ih, const char* name, char* *def_value, int *inherit)
{
  IattribFunc* afunc;

  if (ih->iclass->has_attrib_id)
  {
    const char* name_id = iClassFindId(name);
    if (name_id)
    {
      IattribFunc* afunc;
      const char* partial_name = iClassCutNameId(name, name_id);
      if (!partial_name)
        partial_name = "IDVALUE";  /* pure numbers are used as attributes in IupList and IupMatrix, 
                                      translate them into IDVALUE. */
      afunc = (IattribFunc*)iupTableGet(ih->iclass->attrib_func, partial_name);
      if (afunc)
      {
        *def_value = NULL;  /* id numbered attributes have default value NULL always */
        *inherit = 0;       /* id numbered attributes are NON inheritable always */

        if (afunc->flags & IUPAF_WRITEONLY)
          return NULL;

        if (afunc->get && (ih->handle || afunc->flags & IUPAF_NOT_MAPPED))
        {
          IattribGetIdFunc id_get = (IattribGetIdFunc)afunc->get;
          return id_get(ih, name_id);
        }
        else
          return NULL;      /* if the function exists, then must return here */
      }
    }
  }

  /* if not has_attrib_id, or not found an ID, or not found the partial name, check using the full name */

  afunc = (IattribFunc*)iupTableGet(ih->iclass->attrib_func, name);
  *def_value = NULL;
  *inherit = 1; /* default is inheritable */
  if (afunc)
  {
    *def_value = iClassGetDefaultValue(afunc);
    *inherit = !(afunc->flags & IUPAF_NO_INHERIT) &&   /* is inheritable */
               !(afunc->flags & IUPAF_NO_STRING);      /* is a string */

    if (afunc->flags & IUPAF_WRITEONLY)
      return NULL;

    if (afunc->get && (ih->handle || afunc->flags & IUPAF_NOT_MAPPED))
    {
      if (afunc->flags & IUPAF_HAS_ID)
      {
        IattribGetIdFunc id_get = (IattribGetIdFunc)afunc->get;
        return id_get(ih, "");  /* empty Id */
      }
      else
        return afunc->get(ih);
    }
  }
  return NULL;
}

void iupClassObjectGetAttributeInfo(Ihandle* ih, const char* name, char* *def_value, int *inherit)
{
  IattribFunc* afunc;

  if (ih->iclass->has_attrib_id)
  {
    const char* name_id = iClassFindId(name);
    if (name_id)
    {
      IattribFunc* afunc;
      const char* partial_name = iClassCutNameId(name, name_id);
      if (!partial_name)
        partial_name = "IDVALUE";  /* pure numbers are used as attributes in IupList and IupMatrix, 
                                      translate them into IDVALUE. */
      afunc = (IattribFunc*)iupTableGet(ih->iclass->attrib_func, partial_name);
      if (afunc)
      {
        *def_value = NULL;  /* id numbered attributes have default value NULL always */
        *inherit = 0;       /* id numbered attributes are NON inheritable always */
         return;      /* if the function exists, then must return here */
      }
    }
  }

  /* if not has_attrib_id, or not found an ID, or not found the partial name, check using the full name */

  afunc = (IattribFunc*)iupTableGet(ih->iclass->attrib_func, name);
  *def_value = NULL;
  *inherit = 1; /* default is inheritable */
  if (afunc)
  {
    *def_value = iClassGetDefaultValue(afunc);
    *inherit = !(afunc->flags & IUPAF_NO_INHERIT) &&  /* is inheritable */
               !(afunc->flags & IUPAF_NO_STRING);     /* is a string */
  }
}

int iupClassObjectCurAttribIsInherit(Iclass* ic)
{
  IattribFunc* afunc = (IattribFunc*)iupTableGetCurr(ic->attrib_func);
  if (afunc && !(afunc->flags & IUPAF_NO_INHERIT))
    return 1;
  return 0;
}

int iupClassObjectAttribIsNotString(Ihandle* ih, const char* name)
{
  IattribFunc* afunc = (IattribFunc*)iupTableGet(ih->iclass->attrib_func, name);
  if (afunc && afunc->flags & IUPAF_NO_STRING)
    return 1;
  return 0;
}

void iupClassRegisterAttribute(Iclass* ic, const char* name, 
                               IattribGetFunc _get, IattribSetFunc _set, 
                               const char* _default_value, const char* _system_default, int _flags)
{
  IattribFunc* afunc = (IattribFunc*)iupTableGet(ic->attrib_func, name);
  if (afunc)
    free(afunc);  /* overwrite a previous registration */

  afunc = (IattribFunc*)malloc(sizeof(IattribFunc));
  afunc->get = _get;
  afunc->set = _set;
  if (_default_value == IUPAF_SAMEASSYSTEM)
    afunc->default_value = _system_default;
  else
    afunc->default_value = _default_value;
  afunc->system_default = _system_default;
  afunc->flags = _flags;

  if (iClassIsGlobalDefault(afunc->default_value))
    afunc->call_global_default = 1;
  else
    afunc->call_global_default = 0;

  iupTableSet(ic->attrib_func, name, (void*)afunc, IUPTABLE_POINTER);
}

void iupClassRegisterAttributeId(Iclass* ic, const char* name, 
                               IattribGetIdFunc _get, IattribSetIdFunc _set, 
                               int _flags)
{
  IattribFunc* afunc = (IattribFunc*)iupTableGet(ic->attrib_func, name);
  if (afunc)
    free(afunc);  /* overwrite a previous registration */

  afunc = (IattribFunc*)malloc(sizeof(IattribFunc));
  afunc->get = (IattribGetFunc)_get;
  afunc->set = (IattribSetFunc)_set;
  afunc->default_value = NULL;
  afunc->system_default = NULL;
  afunc->flags = _flags|IUPAF_HAS_ID|IUPAF_NO_INHERIT|IUPAF_NO_DEFAULTVALUE;
  afunc->call_global_default = 0;

  iupTableSet(ic->attrib_func, name, (void*)afunc, IUPTABLE_POINTER);
}

void iupClassRegisterGetAttribute(Iclass* ic, const char* name, 
                                  IattribGetFunc *_get, IattribSetFunc *_set, 
                                  const char* *_default_value, const char* *_system_default, int *_flags)
{
  IattribFunc* afunc = (IattribFunc*)iupTableGet(ic->attrib_func, name);
  if (afunc)
  {
    if (_get) *_get = afunc->get;
    if (_set) *_set = afunc->set;
    if (_default_value) *_default_value = afunc->default_value;
    if (_system_default) *_system_default = afunc->system_default;
    if (_flags) *_flags = afunc->flags;
  }
}

void iupClassRegisterCallback(Iclass* ic, const char* name, const char* format)
{
  /* Since attributes and callbacks do not conflict 
     we can use the same structure to store the callback format using the default_value. */
  iupClassRegisterAttribute(ic, name, NULL, NULL, format, NULL, IUPAF_NO_INHERIT);
}

char* iupClassCallbackGetFormat(Iclass* ic, const char* name)
{
  IattribFunc* afunc = (IattribFunc*)iupTableGet(ic->attrib_func, name);
  if (afunc)
    return (char*)afunc->default_value;
  return NULL;
}

int IupGetClassAttributes(const char* classname, char** names, int n)
{
  Iclass* ic;
  int i = 0;
  char* name;

  iupASSERT(classname!=NULL);
  if (!classname)
    return 0;

  ic = iupRegisterFindClass(classname);
  if (!ic)
    return -1;

  if (!names || !n)
    return iupTableCount(ic->attrib_func);

  name = iupTableFirst(ic->attrib_func);
  while (name)
  {
    names[i] = name;
    i++;
    if (i == n)
      break;

    name = iupTableNext(ic->attrib_func);
  }

  return i;
}

void IupSetClassDefaultAttribute(const char* classname, const char *name, const char* default_value)
{
  Iclass* ic;
  IattribFunc* afunc;

  iupASSERT(classname!=NULL);
  if (!classname)
    return;

  iupASSERT(name!=NULL);
  if (!name)
    return;

  ic = iupRegisterFindClass(name);
  if (!ic)
    return;

  afunc = (IattribFunc*)iupTableGet(ic->attrib_func, name);
  if (afunc && !(afunc->flags & IUPAF_NO_DEFAULTVALUE) &&   /* can has default */
               !(afunc->flags & IUPAF_NO_STRING) &&     /* is a string */
               !(afunc->flags & IUPAF_HAS_ID))
  {
    if (default_value == IUPAF_SAMEASSYSTEM)
      afunc->default_value = afunc->system_default;
    else
      afunc->default_value = default_value;

    if (iClassIsGlobalDefault(afunc->default_value))
      afunc->call_global_default = 1;
    else
      afunc->call_global_default = 0;
  }
  else if (!afunc && default_value)
    iupClassRegisterAttribute(ic, name, NULL, NULL, default_value, NULL, IUPAF_DEFAULT);
}

void IupSaveClassAttributes(Ihandle* ih)
{
  Iclass* ic;
  char *name;

  iupASSERT(iupObjectCheck(ih));
  if (!iupObjectCheck(ih))
    return;

  ic = ih->iclass;

  name = iupTableFirst(ic->attrib_func);
  while (name)
  {
    IattribFunc* afunc = (IattribFunc*)iupTableGet(ih->iclass->attrib_func, name);
    if (afunc && !(afunc->flags & IUPAF_NO_STRING) &&   /* is a string */
                 !(afunc->flags & IUPAF_HAS_ID))
    {
      int inherit;
      char *def_value;
      char *value = iupClassObjectGetAttribute(ih, name, &def_value, &inherit);
      if (value &&     /* NOT NULL */
          !iupStrEqualNoCase(value, iupAttribGet(ih, name)) &&     /* NOT already stored */
          !iupStrEqualNoCase(value, def_value))    /* NOT equal to the default value */
        iupAttribStoreStr(ih, name, value);
    }

    name = iupTableNext(ic->attrib_func);
  }
}

void iupClassObjectEnsureDefaultAttributes(Ihandle* ih)
{
  Iclass* ic;
  char *name;

  ic = ih->iclass;

  name = iupTableFirst(ic->attrib_func);
  while (name)
  {
    IattribFunc* afunc = (IattribFunc*)iupTableGetCurr(ic->attrib_func);
    if (afunc && afunc->set && 
        (afunc->default_value || afunc->system_default) &&
        !(afunc->flags & IUPAF_NO_DEFAULTVALUE) &&   /* can has default */
        !(afunc->flags & IUPAF_NO_STRING) &&       /* is a string */
        !(afunc->flags & IUPAF_HAS_ID))
    {
      if ((!iupStrEqualNoCase(afunc->default_value, afunc->system_default)) || 
          (afunc->call_global_default && iupGlobalDefaultColorChanged(afunc->default_value)))
      {
        if ((!ih->handle && (afunc->flags & IUPAF_NOT_MAPPED)) ||
            (ih->handle && !(afunc->flags & IUPAF_NOT_MAPPED)))
        {
          char* value = iupAttribGet(ih, name);
          if (!value)  /* if set will be updated later */
            afunc->set(ih, iClassGetDefaultValue(afunc));
        }
      }
    }

    name = iupTableNext(ic->attrib_func);
  }
}

char* iupClassGetDefaultAttribute(const char* classname, const char *attrib_name)
{
  Iclass* ic;
  IattribFunc* afunc;

  iupASSERT(classname!=NULL);
  if (!classname)
    return NULL;

  iupASSERT(attrib_name!=NULL);
  if (!attrib_name)
    return NULL;

  ic = iupRegisterFindClass(classname);
  if (!ic)
    return NULL;

  afunc = (IattribFunc*)iupTableGet(ic->attrib_func, attrib_name);
  if (afunc)
    return iClassGetDefaultValue(afunc);
  else
    return NULL;
}