]>
git.ipfire.org Git - thirdparty/newt.git/blob - whiptcl.c
4 #include "dialogboxes.h"
9 enum mode
{ MODE_NONE
, MODE_MSGBOX
, MODE_YESNO
, MODE_CHECKLIST
, MODE_INPUTBOX
,
10 MODE_RADIOLIST
, MODE_MENU
};
12 #define OPT_MSGBOX 1000
13 #define OPT_CHECKLIST 1001
14 #define OPT_YESNO 1002
15 #define OPT_INPUTBOX 1003
17 #define OPT_RADIOLIST 1006
19 static char * setBacktext(ClientData data
, Tcl_Interp
* interp
,
20 char * name1
, char * name2
, int flags
);
21 static char * setHelptext(ClientData data
, Tcl_Interp
* interp
,
22 char * name1
, char * name2
, int flags
);
23 static char * setFullButtons(ClientData data
, Tcl_Interp
* interp
,
24 char * name1
, char * name2
, int flags
);
26 static int wtFinish(ClientData clientData
, Tcl_Interp
* interp
, int argc
,
33 static int wtInit(ClientData clientData
, Tcl_Interp
* interp
, int argc
,
40 Tcl_TraceVar(interp
, "whiptcl_backtext",
41 TCL_TRACE_WRITES
| TCL_GLOBAL_ONLY
, setBacktext
, NULL
);
42 Tcl_TraceVar(interp
, "whiptcl_helpline",
43 TCL_TRACE_WRITES
| TCL_TRACE_UNSETS
| TCL_GLOBAL_ONLY
,
45 Tcl_TraceVar(interp
, "whiptcl_fullbuttons",
46 TCL_TRACE_WRITES
| TCL_TRACE_UNSETS
| TCL_GLOBAL_ONLY
,
47 setFullButtons
, NULL
);
49 Tcl_SetVar(interp
, "whiptcl_helpline", "", TCL_GLOBAL_ONLY
);
50 Tcl_SetVar(interp
, "whiptcl_fullbuttons", "1", TCL_GLOBAL_ONLY
);
55 static int wtCmd(ClientData clientData
, Tcl_Interp
* interp
, int argc
,
57 enum mode mode
= MODE_NONE
;
73 char ** selections
, ** next
;
75 struct poptOption optionsTable
[] = {
76 { "checklist", '\0', 0, 0, OPT_CHECKLIST
},
77 { "defaultno", '\0', 0, &defaultNo
, 0 },
78 { "inputbox", '\0', 0, 0, OPT_INPUTBOX
},
79 { "menu", '\0', 0, 0, OPT_MENU
},
80 { "msgbox", '\0', 0, 0, OPT_MSGBOX
},
81 { "nocancel", '\0', 0, &noCancel
, 0 },
82 { "noitem", '\0', 0, &noItem
, 0 },
83 { "radiolist", '\0', 0, 0, OPT_RADIOLIST
},
84 { "scrolltext", '\0', 0, &scrollText
, 0 },
85 { "title", '\0', POPT_ARG_STRING
, &title
, 0 },
86 { "yesno", '\0', 0, 0, OPT_YESNO
},
90 optCon
= poptGetContext("whiptcl", argc
, argv
, optionsTable
, 0);
92 while ((arg
= poptGetNextOpt(optCon
)) > 0) {
93 optArg
= poptGetOptArg(optCon
);
97 if (mode
!= MODE_NONE
) rc
= -1;
102 if (mode
!= MODE_NONE
) rc
= -1;
107 if (mode
!= MODE_NONE
) rc
= -1;
108 mode
= MODE_RADIOLIST
;
112 if (mode
!= MODE_NONE
) rc
= -1;
113 mode
= MODE_CHECKLIST
;
117 if (mode
!= MODE_NONE
) rc
= -1;
122 if (mode
!= MODE_NONE
) rc
= -1;
123 mode
= MODE_INPUTBOX
;
129 /* this could buffer oveflow, bug we're not setuid so I don't care */
130 interp
->result
= malloc(200);
131 interp
->freeProc
= TCL_DYNAMIC
;
132 sprintf(interp
->result
, "%s: %s\n",
133 poptBadOption(optCon
, POPT_BADOPTION_NOALIAS
),
139 if (mode
== MODE_NONE
) {
140 interp
->result
= "no dialog mode was specified";
143 interp
->result
= "multiple modes were specified";
147 if (!(text
= poptGetArg(optCon
))) {
148 interp
->result
= "missing text parameter";
152 if (!(nextArg
= poptGetArg(optCon
))) {
153 interp
->result
= "height missing";
156 height
= strtoul(nextArg
, &end
, 10);
158 interp
->result
= "height is not a number";
162 if (!(nextArg
= poptGetArg(optCon
))) {
163 interp
->result
= "width missing";
166 width
= strtoul(nextArg
, &end
, 10);
168 interp
->result
= "width is not a number";
174 newtOpenWindow((80 - width
) / 2, (24 - height
) / 2, width
, height
, title
);
176 if (noCancel
) flags
|= FLAG_NOCANCEL
;
177 if (noItem
) flags
|= FLAG_NOITEM
;
178 if (scrollText
) flags
|= FLAG_SCROLL_TEXT
;
179 if (defaultNo
) flags
|= FLAG_DEFAULT_NO
;
183 rc
= messageBox(text
, height
, width
, MSGBOX_MSG
, flags
);
187 rc
= messageBox(text
, height
, width
, MSGBOX_YESNO
, flags
);
189 interp
->result
= "yes";
191 interp
->result
= "no";
192 if (rc
== DLG_ERROR
) rc
= 0;
196 rc
= inputBox(text
, height
, width
, optCon
, flags
, &result
);
198 interp
->result
= strdup(result
);
199 interp
->freeProc
= TCL_DYNAMIC
;
204 rc
= listBox(text
, height
, width
, optCon
, flags
, &result
);
206 interp
->result
= strdup(result
);
207 interp
->freeProc
= TCL_DYNAMIC
;
212 rc
= checkList(text
, height
, width
, optCon
, 1, flags
, &selections
);
214 interp
->result
= strdup(selections
[0]);
215 interp
->freeProc
= TCL_DYNAMIC
;
220 rc
= checkList(text
, height
, width
, optCon
, 0, flags
, &selections
);
223 for (next
= selections
; *next
; next
++)
224 Tcl_AppendElement(interp
, *next
);
231 /* this can't happen */
236 if (rc
== DLG_ERROR
) {
237 interp
->result
= "bad paramter for whiptcl dialog box";
241 Tcl_SetVar(interp
, "whiptcl_canceled", (rc
== DLG_CANCEL
) ? "1" : "0",
247 static char * setBacktext(ClientData data
, Tcl_Interp
* interp
,
248 char * name1
, char * name2
, int flags
) {
249 static char blankLine
[81] = " "
252 newtDrawRootText(0, 0, blankLine
);
253 newtDrawRootText(0, 0, Tcl_GetVar(interp
, "whiptcl_backtext",
259 static char * setHelptext(ClientData data
, Tcl_Interp
* interp
,
260 char * name1
, char * name2
, int flags
) {
261 char * text
= Tcl_GetVar(interp
, "whiptcl_helpline", TCL_GLOBAL_ONLY
);
265 else if (!strlen(text
))
269 newtPushHelpLine(text
);
274 static char * setFullButtons(ClientData data
, Tcl_Interp
* interp
,
275 char * name1
, char * name2
, int flags
) {
276 char * val
= Tcl_GetVar(interp
, "whiptcl_fullbuttons", TCL_GLOBAL_ONLY
);
280 if ((rc
= Tcl_ExprBoolean(interp
, val
, &state
))) {
281 Tcl_FreeResult(interp
);
282 return "whiptcl_fullbuttons may only contain a boolean value";
285 useFullButtons(state
);
290 int Whiptcl_Init(Tcl_Interp
* interp
) {
291 Tcl_CreateCommand(interp
, "whiptcl_finish", wtFinish
, NULL
, NULL
);
292 Tcl_CreateCommand(interp
, "whiptcl_init", wtInit
, NULL
, NULL
);
293 Tcl_CreateCommand(interp
, "whiptcl_cmd", wtCmd
, NULL
, NULL
);