foxygit / doom Log in
commit e10af52152ba793d983497cd86d7ccc10d32a13d
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Sun Sep 8 18:57:20 2013 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Sun Sep 8 18:57:20 2013 +0000

    Fix file select widget to emit "changed" signal properly. Reset variable
    value to empty string rather than NULL if cancel is pressed in dialog
    (thanks Alexandre Xavier).

    Subversion-branch: /trunk/chocolate-doom
    Subversion-revision: 2638
---
 textscreen/txt_fileselect.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/textscreen/txt_fileselect.c b/textscreen/txt_fileselect.c
index 6e492734..52b0dc1c 100644
--- a/textscreen/txt_fileselect.c
+++ b/textscreen/txt_fileselect.c
@@ -591,6 +591,16 @@ static int DoSelectFile(txt_fileselect_t *fileselect)
     {
         path = TXT_SelectFile(fileselect->prompt,
                               fileselect->extensions);
+
+        // Update inputbox variable.
+        // If cancel was pressed (ie. NULL was returned by TXT_SelectFile)
+        // then reset to empty string, not NULL).
+
+        if (path == NULL)
+        {
+            path = strdup("");
+        }
+
         var = fileselect->inputbox->value;
         free(*var);
         *var = path;
@@ -658,6 +668,16 @@ txt_widget_class_t txt_fileselect_class =
     TXT_FileSelectFocused,
 };

+// If the (inner) inputbox widget is changed, emit a change to the
+// outer (fileselect) widget.
+
+static void InputBoxChanged(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(fileselect))
+{
+    TXT_CAST_ARG(txt_fileselect_t, fileselect);
+
+    TXT_EmitSignal(&fileselect->widget, "changed");
+}
+
 txt_fileselect_t *TXT_NewFileSelector(char **variable, int size,
                                       char *prompt, char **extensions)
 {
@@ -671,6 +691,9 @@ txt_fileselect_t *TXT_NewFileSelector(char **variable, int size,
     fileselect->prompt = prompt;
     fileselect->extensions = extensions;

+    TXT_SignalConnect(fileselect->inputbox, "changed",
+                      InputBoxChanged, fileselect);
+
     return fileselect;
 }