summaryrefslogtreecommitdiff
path: root/iup/src/iup_class.c
blob: d2c4b02215bc39b6bf0737b2c0ff4f503abb06b3 (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
/** \file
 * \brief IUP Ihandle Class C Interface
 *
 * 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"



/*****************************************************************
                        Method Stubs
*****************************************************************/

static int iClassCreate(Iclass* ic, Ihandle* ih, void** params)
{
  int ret = IUP_NOERROR;
  if (ic->parent)
    ret = iClassCreate(ic->parent, ih, params);

  if (ret == IUP_NOERROR && ic->Create)
    ret = ic->Create(ih, params);

  return ret;
}

static int iClassMap(Iclass* ic, Ihandle* ih)
{
  int ret = IUP_NOERROR;
  if (ic->parent)
    ret = iClassMap(ic->parent, ih);

  if (ret == IUP_NOERROR && ic->Map)
    ret = ic->Map(ih);

  return ret;
}

static void iClassUnMap(Iclass* ic, Ihandle* ih)
{
  /* must be before the parent class */
  if (ic->UnMap)
    ic->UnMap(ih);

  if (ic->parent)
    iClassUnMap(ic->parent, ih);
}

static void iClassDestroy(Iclass* ic, Ihandle* ih)
{
  /* must destroy child class before the parent class */
  if (ic->Destroy)
    ic->Destroy(ih);

  if (ic->parent)
    iClassDestroy(ic->parent, ih);
}

static void iClassComputeNaturalSize(Iclass* ic, Ihandle* ih, int *w, int *h, int *expand)
{
  if (ic->parent)
    iClassComputeNaturalSize(ic->parent, ih, w, h, expand);

  if (ic->ComputeNaturalSize)
    ic->ComputeNaturalSize(ih, w, h, expand);
}

static void iClassSetChildrenCurrentSize(Iclass* ic, Ihandle* ih, int shrink)
{
  if (ic->parent)
    iClassSetChildrenCurrentSize(ic->parent, ih, shrink);

  if (ic->SetChildrenCurrentSize)
    ic->SetChildrenCurrentSize(ih, shrink);
}

static void iClassSetChildrenPosition(Iclass* ic, Ihandle* ih, int x, int y)
{
  if (ic->parent)
    iClassSetChildrenPosition(ic->parent, ih, x, y);

  if (ic->SetChildrenPosition)
    ic->SetChildrenPosition(ih, x, y);
}

static Ihandle* iClassGetInnerContainer(Iclass* ic, Ihandle* ih)
{
  Ihandle* ih_container = ih;

  if (ic->parent)
    ih_container = iClassGetInnerContainer(ic->parent, ih);

  /* if the class implements the function it will ignore the result of the parent class */

  if (ic->GetInnerContainer)
    ih_container = ic->GetInnerContainer(ih);

  return ih_container;
}

static void* iClassGetInnerNativeContainerHandle(Iclass* ic, Ihandle* ih, Ihandle* child)
{
  void* container_handle = ih->handle;

  if (ic->parent)
    container_handle = iClassGetInnerNativeContainerHandle(ic->parent, ih, child);

  /* if the class implements the function it will ignore the result of the parent class */

  if (ic->GetInnerNativeContainerHandle)
    container_handle = ic->GetInnerNativeContainerHandle(ih, child);

  return container_handle;
}

static void iClassObjectChildAdded(Iclass* ic, Ihandle* ih, Ihandle* child)
{
  if (ic->parent)
    iClassObjectChildAdded(ic->parent, ih, child);

  if (ic->ChildAdded)
    ic->ChildAdded(ih, child);
}

static void iClassObjectChildRemoved(Iclass* ic, Ihandle* ih, Ihandle* child)
{
  if (ic->parent)
    iClassObjectChildRemoved(ic->parent, ih, child);

  if (ic->ChildRemoved)
    ic->ChildRemoved(ih, child);
}

static void iClassLayoutUpdate(Iclass* ic, Ihandle *ih)
{
  if (ic->parent)
    iClassLayoutUpdate(ic->parent, ih);

  if (ic->LayoutUpdate)
    ic->LayoutUpdate(ih);
}

static int iClassDlgPopup(Iclass* ic, Ihandle* ih, int x, int y)
{
  int ret = IUP_INVALID;  /* IUP_INVALID means it is not implemented */
  if (ic->parent)
    ret = iClassDlgPopup(ic->parent, ih, x, y);

  if (ret != IUP_ERROR && ic->DlgPopup)
    ret = ic->DlgPopup(ih, x, y);

  return ret;
}

int iupClassObjectCreate(Ihandle* ih, void** params)
{
  return iClassCreate(ih->iclass, ih, params);
}

int iupClassObjectMap(Ihandle* ih)
{
  return iClassMap(ih->iclass, ih);
}

void iupClassObjectUnMap(Ihandle* ih)
{
  iClassUnMap(ih->iclass, ih);
}

void iupClassObjectDestroy(Ihandle* ih)
{
  iClassDestroy(ih->iclass, ih);
}

void iupClassObjectComputeNaturalSize(Ihandle* ih, int *w, int *h, int *expand)
{
  iClassComputeNaturalSize(ih->iclass, ih, w, h, expand);
}

void iupClassObjectSetChildrenCurrentSize(Ihandle* ih, int shrink)
{
  iClassSetChildrenCurrentSize(ih->iclass, ih, shrink);
}

void iupClassObjectSetChildrenPosition(Ihandle* ih, int x, int y)
{
  iClassSetChildrenPosition(ih->iclass, ih, x, y);
}

Ihandle* iupClassObjectGetInnerContainer(Ihandle* ih)
{
  return iClassGetInnerContainer(ih->iclass, ih);
}

void* iupClassObjectGetInnerNativeContainerHandle(Ihandle* ih, Ihandle* child)
{
  return iClassGetInnerNativeContainerHandle(ih->iclass, ih, child);
}

void iupClassObjectChildAdded(Ihandle* ih, Ihandle* child)
{
  iClassObjectChildAdded(ih->iclass, ih, child);
}

void iupClassObjectChildRemoved(Ihandle* ih, Ihandle* child)
{
  iClassObjectChildRemoved(ih->iclass, ih, child);
}

void iupClassObjectLayoutUpdate(Ihandle *ih)
{
  iClassLayoutUpdate(ih->iclass, ih);
}

int iupClassObjectDlgPopup(Ihandle* ih, int x, int y)
{
  return iClassDlgPopup(ih->iclass, ih, x, y);
}


/*****************************************************************
                        Class Definition
*****************************************************************/


static void iClassReleaseAttribFuncTable(Iclass* ic)
{
  char* name = iupTableFirst(ic->attrib_func);
  while (name)
  {
    void* afunc = iupTableGetCurr(ic->attrib_func);
    free(afunc);

    name = iupTableNext(ic->attrib_func);
  }

  iupTableDestroy(ic->attrib_func);
}

Iclass* iupClassNew(Iclass* parent)
{
  Iclass* ic = malloc(sizeof(Iclass));
  memset(ic, 0, sizeof(Iclass));

  if (parent)
    ic->attrib_func = parent->attrib_func;
  else
    ic->attrib_func = iupTableCreate(IUPTABLE_STRINGINDEXED);

  ic->parent = parent;

  return ic;
}

void iupClassRelease(Iclass* ic)
{
  Iclass* parent;

  /* must release only the actual class */
  if (ic->Release)
    ic->Release(ic);

  /* must free all classes, since a new instance is created when we inherit */
  parent = ic->parent;
  while (parent)
  {
    Iclass* tmp = parent;
    parent = parent->parent;
    free(tmp);
  }

  /* attributes functions table is released only once */
  iClassReleaseAttribFuncTable(ic);

  free(ic);
}


/*****************************************************************
                        Main API
*****************************************************************/


char* IupGetClassName(Ihandle *ih)
{
  iupASSERT(iupObjectCheck(ih));
  if (!iupObjectCheck(ih))
    return NULL;

  return ih->iclass->name;
}

char* IupGetClassType(Ihandle *ih)
{
  static char* type2str[] = {"void", "control", "canvas", "dialog", "image", "menu"};
  iupASSERT(iupObjectCheck(ih));
  if (!iupObjectCheck(ih))
    return NULL;

  return type2str[ih->iclass->nativetype];
}