summaryrefslogtreecommitdiff
path: root/iup/src/iup_object.c
blob: f0d5dc98fb094418fd58f1e93c555408423ec635 (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
/** \file
 * \brief Ihandle management
 *
 * See Copyright Notice in "iup.h"
 */

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

#include "iup.h"

#include "iup_object.h"
#include "iup_assert.h"
#include "iup_register.h"
#include "iup_names.h"


static Ihandle* iHandleCreate(void)
{
  Ihandle *ih = (Ihandle*)malloc(sizeof(Ihandle));
  memset(ih, 0, sizeof(Ihandle));

  ih->sig[0] = 'I';
  ih->sig[1] = 'U';
  ih->sig[2] = 'P';
  ih->sig[3] = 0;

  ih->serial = -1;

  ih->attrib = iupTableCreate(IUPTABLE_STRINGINDEXED);

  return ih;
}

static void iHandleDestroy(Ihandle* ih)
{
  iupTableDestroy(ih->attrib);
  memset(ih, 0, sizeof(Ihandle));
  free(ih);
}

int iupObjectCheck(Ihandle* ih)
{
  char* sig = (char*)ih;

  if (!ih) return 0;  

  if (sig[0] != 'I' ||
      sig[1] != 'U' ||
      sig[2] != 'P' ||
      sig[3] != 0)
    return 0;

  return 1;
}

Ihandle* iupObjectCreate(Iclass* iclass, void** params)
{
  /* create the base handle structure */
  Ihandle* ih = iHandleCreate();

  ih->iclass = iclass;

  /* create the element */
  if (iupClassObjectCreate(ih, params) == IUP_ERROR)
  {
    iupERROR1("IUP object creation failed (%s).", iclass->name);
    iHandleDestroy(ih);
    return NULL;
  }

  /* ensure attributes default values, at this time only the ones that can be set before map */
  iupClassObjectEnsureDefaultAttributes(ih);

  return ih;
}

void** iupObjectGetParamList(void* first, va_list arglist)
{
  const int INITIAL_NUMBER = 50;
  void **params;
  void *param;
  int max_count = INITIAL_NUMBER, count = 0;

  params = (void **) malloc (sizeof (void *) * INITIAL_NUMBER);

  param = first;

  while (param != NULL)
  {
    params[count] = param;
    count++;

    /* verifica se precisa realocar memoria */
    if (count >= max_count)
    {
      void **new_params = NULL;

      max_count += INITIAL_NUMBER;

      new_params = (void **) realloc (params, sizeof (void *) * max_count);

      params = new_params;
    }

    param = va_arg (arglist, void*);
  }
  params[count] = NULL;

  return params;
}

Ihandle* IupCreatev(const char *name, void **params)
{
  Iclass *ic;
  iupASSERT(name!=NULL);
  ic = iupRegisterFindClass(name);
  if (ic)
    return iupObjectCreate(ic, params);
  else
    return NULL;
}

Ihandle *IupCreatep(const char *name, void* first, ...)
{
  va_list arglist;
  void **params;
  Ihandle *ih;
  iupASSERT(name!=NULL);

  va_start(arglist, first);
  params = iupObjectGetParamList(first, arglist);
  va_end(arglist);

  ih = IupCreatev(name, params);
  free(params);

  return ih;
}

Ihandle* IupCreate(const char *name)
{
  iupASSERT(name!=NULL);
  return IupCreatev(name, NULL);
}

void IupDestroy(Ihandle *ih)
{
  Icallback cb;

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

  /* Hide before destroy to avoid children redraw */
  if (ih->iclass->nativetype == IUP_TYPEDIALOG)
    IupHide(ih);

  cb = IupGetCallback(ih, "DESTROY_CB");
  if (cb) cb(ih);

  /* Destroy all its children.
     Just need to remove the first child,
     IupDetach will update firstchild. */
  while (ih->firstchild)
    IupDestroy(ih->firstchild);

  /* unmap if mapped and remove from its parent child list */
  IupDetach(ih);

  /* removes names associated with the element */
  iupRemoveNames(ih);

  /* destroy the element */
  iupClassObjectDestroy(ih);

  /* destroy the private data */
  if (ih->data)
    free(ih->data);

  /* destroy the base handle structure */
  iHandleDestroy(ih);
}