summaryrefslogtreecommitdiff
path: root/iup/src/iup_names.c
diff options
context:
space:
mode:
Diffstat (limited to 'iup/src/iup_names.c')
-rwxr-xr-xiup/src/iup_names.c134
1 files changed, 90 insertions, 44 deletions
diff --git a/iup/src/iup_names.c b/iup/src/iup_names.c
index 9ec01a6..18ca3ef 100755
--- a/iup/src/iup_names.c
+++ b/iup/src/iup_names.c
@@ -14,11 +14,41 @@
#include "iup_object.h"
#include "iup_class.h"
#include "iup_assert.h"
+#include "iup_attrib.h"
#include "iup_str.h"
static Itable *inames_strtable = NULL; /* table indexed by name containing Ihandle* address */
-static Itable *inames_ihtable = NULL; /* table indexed by Ihandle* address containing names */
+
+void iupNamesInit(void)
+{
+ inames_strtable = iupTableCreate(IUPTABLE_STRINGINDEXED);
+}
+
+void iupNamesFinish(void)
+{
+ iupTableDestroy(inames_strtable);
+ inames_strtable = NULL;
+}
+
+static Ihandle* iNameGetTopParent(Ihandle* ih)
+{
+ Ihandle* parent = ih;
+ while (parent->parent)
+ parent = parent->parent;
+ return parent;
+}
+
+static int iNameCheckArray(Ihandle** ih_array, int count, Ihandle* ih)
+{
+ int i;
+ for (i = 0; i < count; i++)
+ {
+ if (ih_array[i] == ih)
+ return 0;
+ }
+ return 1;
+}
void iupNamesDestroyHandles(void)
{
@@ -37,10 +67,14 @@ void iupNamesDestroyHandles(void)
while (name)
{
ih = (Ihandle*)iupTableGetCurr(inames_strtable);
- if (iupObjectCheck(ih))
+ if (iupObjectCheck(ih)) /* here must be a handle */
{
- ih_array[i] = ih;
- i++;
+ ih = iNameGetTopParent(ih);
+ if (iNameCheckArray(ih_array, i, ih))
+ {
+ ih_array[i] = ih;
+ i++;
+ }
}
name = iupTableNext(inames_strtable);
}
@@ -48,44 +82,28 @@ void iupNamesDestroyHandles(void)
count = i;
for (i = 0; i < count; i++)
{
- if (iupObjectCheck(ih_array[i]))
+ if (iupObjectCheck(ih_array[i])) /* here must be a handle */
IupDestroy(ih_array[i]);
}
free(ih_array);
}
-void iupNamesInit(void)
-{
- inames_strtable = iupTableCreate(IUPTABLE_STRINGINDEXED);
- inames_ihtable = iupTableCreate(IUPTABLE_POINTERINDEXED);
-}
-
-void iupNamesFinish(void)
-{
- iupTableDestroy(inames_strtable);
- inames_strtable = NULL;
-
- iupTableDestroy(inames_ihtable);
- inames_ihtable = NULL;
-}
-
-void iupRemoveAllNames(Ihandle* ih)
+void iupRemoveNames(Ihandle* ih)
{
char *name;
- Ihandle *cur_ih;
- name = iupTableFirst(inames_strtable);
- while (name)
- {
- cur_ih = (Ihandle*)iupTableGetCurr(inames_strtable);
- if (iupObjectCheck(cur_ih) && cur_ih == ih)
- iupTableRemoveCurr(inames_strtable);
+ /* clear the cache */
+ name = iupAttribGet(ih, "_IUP_LASTHANDLENAME");
+ if (name)
+ iupTableRemove(inames_strtable, name);
- name = iupTableNext(inames_strtable);
- }
+ /* check for an internal name */
+ name = iupAttribGetHandleName(ih);
+ if (name)
+ iupTableRemove(inames_strtable, name);
- iupTableRemove(inames_ihtable, (char*)ih);
+ /* Do NOT search for other names */
}
Ihandle *IupGetHandle(const char *name)
@@ -104,22 +122,24 @@ Ihandle* IupSetHandle(const char *name, Ihandle *ih)
return NULL;
old_ih = iupTableGet(inames_strtable, name);
+
if (ih != NULL)
{
iupTableSet(inames_strtable, name, ih, IUPTABLE_POINTER);
- iupTableSet(inames_ihtable, (char*)ih, (char*)name, IUPTABLE_STRING); /* keep only the last name set */
+
+ /* save the name in the cache if it is a valid handle */
+ if (iupObjectCheck(ih))
+ iupAttribStoreStr(ih, "_IUP_LASTHANDLENAME", name);
}
else
{
- ih = iupTableGet(inames_strtable, name);
iupTableRemove(inames_strtable, name);
- if (ih)
- {
- char* cur_name = iupTableGet(inames_ihtable, (char*)ih);
- if (iupStrEqualNoCase(cur_name, name))
- iupTableRemove(inames_ihtable, (char*)ih);
- }
+
+ /* clear the name from the cache if it is a valid handle */
+ if (iupObjectCheck(old_ih))
+ iupAttribSetStr(old_ih, "_IUP_LASTHANDLENAME", NULL);
}
+
return old_ih;
}
@@ -151,7 +171,8 @@ static int iNamesCountDialogs(void)
while (name)
{
Ihandle* dlg = (Ihandle*)iupTableGetCurr(inames_strtable);
- if (iupObjectCheck(dlg) && dlg->iclass->nativetype == IUP_TYPEDIALOG)
+ if (iupObjectCheck(dlg) && /* here must be a handle */
+ dlg->iclass->nativetype == IUP_TYPEDIALOG)
i++;
name = iupTableNext(inames_strtable);
@@ -171,7 +192,8 @@ int IupGetAllDialogs(char** names, int n)
while (name)
{
Ihandle* dlg = (Ihandle*)iupTableGetCurr(inames_strtable);
- if (iupObjectCheck(dlg) && dlg->iclass->nativetype == IUP_TYPEDIALOG)
+ if (iupObjectCheck(dlg) && /* here must be a handle */
+ dlg->iclass->nativetype == IUP_TYPEDIALOG)
{
names[i] = name;
i++;
@@ -186,8 +208,32 @@ int IupGetAllDialogs(char** names, int n)
char* IupGetName(Ihandle* ih)
{
- iupASSERT(iupObjectCheck(ih));
- if (!iupObjectCheck(ih))
+ char *name;
+ if (!ih) /* no iupASSERT needed here */
return NULL;
- return iupTableGet(inames_ihtable, (char*)ih);
+
+ if (iupObjectCheck(ih))
+ {
+ /* check the cache first, but must be a handle */
+ name = iupAttribGet(ih, "_IUP_LASTHANDLENAME");
+ if (name)
+ return name;
+ }
+
+ /* check for an internal name */
+ name = iupAttribGetHandleName(ih);
+ if (name)
+ return name;
+
+ /* search for the name */
+ name = iupTableFirst(inames_strtable);
+ while (name)
+ {
+ if ((Ihandle*)iupTableGetCurr(inames_strtable) == ih)
+ return name;
+
+ name = iupTableNext(inames_strtable);
+ }
+
+ return NULL;
}