summaryrefslogtreecommitdiff
path: root/iup/src/iup_list.c
diff options
context:
space:
mode:
Diffstat (limited to 'iup/src/iup_list.c')
-rwxr-xr-xiup/src/iup_list.c59
1 files changed, 52 insertions, 7 deletions
diff --git a/iup/src/iup_list.c b/iup/src/iup_list.c
index 5965665..1077d98 100755
--- a/iup/src/iup_list.c
+++ b/iup/src/iup_list.c
@@ -53,6 +53,37 @@ static void iListCallActionCallback(Ihandle* ih, IFnsii cb, int pos, int state)
IupExitLoop();
}
+void iupListUpdateOldValue(Ihandle* ih, int pos, int removed)
+{
+ if (!ih->data->has_editbox)
+ {
+ char* old_value = iupAttribGet(ih, "_IUPLIST_OLDVALUE");
+ if (old_value)
+ {
+ int old_pos = atoi(old_value)-1; /* was in IUP reference, starting at 1 */
+ if (ih->data->is_dropdown || !ih->data->is_multiple)
+ {
+ if (old_pos >= pos)
+ {
+ if (removed && old_pos == pos)
+ {
+ /* when the current item is removed nothing remains selected */
+ iupAttribSetStr(ih, "_IUPLIST_OLDVALUE", NULL);
+ }
+ else
+ iupAttribSetInt(ih, "_IUPLIST_OLDVALUE", removed? old_pos-1: old_pos+1);
+ }
+ }
+ else
+ {
+ /* multiple selection on a non drop-down list. */
+ char* value = IupGetAttribute(ih, "VALUE");
+ iupAttribStoreStr(ih, "_IUPLIST_OLDVALUE", value);
+ }
+ }
+ }
+}
+
void iupListSingleCallActionCallback(Ihandle* ih, IFnsii cb, int pos)
{
char* old_str = iupAttribGet(ih, "_IUPLIST_OLDVALUE");
@@ -77,7 +108,7 @@ void iupListMultipleCallActionCallback(Ihandle* ih, IFnsii cb, IFns multi_cb, in
char* old_str = iupAttribGet(ih, "_IUPLIST_OLDVALUE");
int old_count = old_str? strlen(old_str): 0;
- char* str = iupStrGetMemory(count+1);
+ char* str = malloc(count+1);
memset(str, '-', count);
str[count]=0;
for (i=0; i<sel_count; i++)
@@ -92,6 +123,7 @@ void iupListMultipleCallActionCallback(Ihandle* ih, IFnsii cb, IFns multi_cb, in
if (multi_cb)
{
int unchanged = 1;
+
for (i=0; i<count && old_str; i++)
{
if (str[i] == old_str[i])
@@ -101,7 +133,10 @@ void iupListMultipleCallActionCallback(Ihandle* ih, IFnsii cb, IFns multi_cb, in
}
if (old_str && unchanged)
+ {
+ free(str);
return;
+ }
if (multi_cb(ih, str) == IUP_CLOSE)
IupExitLoop();
@@ -133,6 +168,7 @@ void iupListMultipleCallActionCallback(Ihandle* ih, IFnsii cb, IFns multi_cb, in
}
iupAttribStoreStr(ih, "_IUPLIST_OLDVALUE", str);
+ free(str);
}
int iupListGetPos(Ihandle* ih, const char* name_id)
@@ -145,7 +181,8 @@ int iupListGetPos(Ihandle* ih, const char* name_id)
pos--; /* IUP items start at 1 */
if (pos < 0) return -1;
- if (pos > count-1) return -1;
+ if (pos == count) return -2;
+ if (pos > count) return -1;
return pos;
}
@@ -217,7 +254,10 @@ int iupListSetIdValueAttrib(Ihandle* ih, const char* name_id, const char* value)
if (pos >= 0 && pos <= count-1)
{
if (pos == 0)
+ {
iupdrvListRemoveAllItems(ih);
+ iupAttribSetStr(ih, "_IUPLIST_OLDVALUE", NULL);
+ }
else
{
int i = pos;
@@ -245,7 +285,7 @@ int iupListSetIdValueAttrib(Ihandle* ih, const char* name_id, const char* value)
static int iListSetAppendItemAttrib(Ihandle* ih, const char* value)
{
- if (!ih->handle) /* do not store the action before map */
+ if (!ih->handle) /* do not do the action before map */
return 0;
if (value)
iupdrvListAppendItem(ih, value);
@@ -254,27 +294,32 @@ static int iListSetAppendItemAttrib(Ihandle* ih, const char* value)
static int iListSetInsertItemAttrib(Ihandle* ih, const char* name_id, const char* value)
{
- if (!ih->handle) /* do not store the action before map */
+ if (!ih->handle) /* do not do the action before map */
return 0;
if (value)
{
int pos = iupListGetPos(ih, name_id);
- if (pos!=-1)
+ if (pos >= 0)
iupdrvListInsertItem(ih, pos, value);
+ else if (pos == -2)
+ iupdrvListAppendItem(ih, value);
}
return 0;
}
static int iListSetRemoveItemAttrib(Ihandle* ih, const char* value)
{
- if (!ih->handle) /* do not store the action before map */
+ if (!ih->handle) /* do not do the action before map */
return 0;
if (!value)
+ {
iupdrvListRemoveAllItems(ih);
+ iupAttribSetStr(ih, "_IUPLIST_OLDVALUE", NULL);
+ }
else
{
int pos = iupListGetPos(ih, value);
- if (pos!=-1)
+ if (pos >= 0)
iupdrvListRemoveItem(ih, pos);
}
return 0;