Submitted by Anonymous on Thu, 10/09/2003 - 08:48.
Dear reader,
I made a piece of program that generates a form with several textfields on it. These textfields are generated in a loop. This means that the all have the same widget name and a different naming. Could you tell me how to read the information that is in the textfields after pressing an button?
The code that I used to create these textfields is as follows.
void MDE(Widget w, XtPointer client_data, XtPointer call_data)
{
Widget toplevelMDE, mainform, subform, last_subform, label, text, btnOk,btnCancel;
Widget dialog, pane, rc, text_w, label_g, frame, frame2, frame3;
Widget CreateActionArea (Widget, ActionAreaItem *, int);
Widget action_area, work_area, check_area, widget;
XmString string;
Arg args[6];
char buf[32], *pCountTags, *pStart, *pEnd, cCurTag[SMALL_BUFFER +1];
char *pStart1, *pEnd1, cCurTag1[SMALL_BUFFER +1], cNew[SMALL_BUFFER +1], *value, *value1;
int n, i, x=0, y, iNmbrTags =0;
static ActionAreaItem action_items[] = {
{ "OK", Ok, NULL},
{ "Cancel", cancel, NULL },
};
TString *check_items;
TString *check_text;
TString *check_new;
/*first count the number of nested label tags*/
pCountTags = cLabelTag;
while (*pCountTags){
if (*pCountTags == `/`){iNmbrTags++;}
pCountTags++;
}
check_items = (TString *) malloc (iNmbrTags * sizeof (TString));
check_text = (TString *) malloc (iNmbrTags * sizeof (TString));
check_new = (TString *) malloc (iNmbrTags * sizeof (TString));
if (!check_items) {return;}
XtSetLanguageProc (NULL, NULL, NULL);
toplevelMDE = XmCreateDialogShell ( GetTopShell (w), "MDE", NULL, 0);
n = 0;
XtSetArg (args[n], XmNsashWidth, 1); n++;
XtSetArg (args[n], XmNsashHeight, 1); n++;
pane = XmCreatePanedWindow (toplevelMDE, "pane", args, n);
rc = XmCreateRowColumn (pane, "control_area", NULL, 0);
frame2 = XmCreateFrame (rc, "frame2", NULL,0);
frame3 = XmCreateFrame (rc, "frame3", NULL,0);
/* Create the check area.*/
mainform = XmCreateForm (frame2, "mainform", NULL, 0);
last_subform = NULL;
/*split the nested loop and make labels and text of it.*/
pStart = strchr (cLabelTag, `/`); pStart++;
pStart1 = strchr (cTextTag, `/`); pStart1++;
for (i = 0; i < iNmbrTags; i++) {
pEnd = strchr (pStart, `/`);
pEnd1 = strchr (pStart1, `/`);
if (!pEnd){
pEnd = pStart + strlen (pStart); /*for last nested tag.*/
}
if (!pEnd1){
pEnd1 = pStart1 + strlen (pStart1); /*for last nested tag.*/
}
CopyString (0, (pEnd - pStart), pStart, &cCurTag[0]);
strcpy((char *)check_items[i], cCurTag);
pStart = pEnd + 1;
CopyString (0, (pEnd1 - pStart1), pStart1, &cCurTag1[0]);
strcpy((char *)check_text[i], cCurTag1);
pStart1 = pEnd1 + 1;
subform = XmCreateForm (mainform, "subform", NULL, 0);
XtVaSetValues (subform,
XmNtopAttachment, last_subform ? XmATTACH_WIDGET XmATTACH_FORM,
XmNtopWidget, last_subform,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
NULL);
label = XmCreateLabelGadget (subform, cCurTag, NULL, 0);
XtVaSetValues (label, XmNtopAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNalignment, XmALIGNMENT_BEGINNING,
NULL);
XtManageChild (label);
XtSetArg (args[0], XmNvalue, cCurTag1);
sprintf(buff, "text_%d", i);
text = XmCreateText (subform, buff, args, 1);
XtVaSetValues (text, XmNtopAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_WIDGET,
XmNleftWidget, label,
NULL);
XtManageChild (text);
sprintf(cNew,"/%s",cCurTag1);
strcpy(cNew, (char *)check_new[i]);
XtManageChild (subform);
last_subform = subform;
}
XtManageChild (frame2);
/* Create the action area. */
action_area = XmCreateForm (frame3, "action_area", NULL, 0);
XtVaSetValues (action_area, XmNfractionBase, TIGHTNESS*3,
XmNleftOffset, 7,
XmNrightOffset, 7,
NULL);
btnOk = XmCreatePushButton (action_area, "Ok", NULL, 0);
XtVaSetValues (btnOk, XmNleftAttachment, XmATTACH_POSITION,
XmNleftPosition,2,
XmNtopAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_POSITION,
XmNrightPosition, 7,
XmNshowAsDefault, 0,
XmNdefaultButtonShadowThickness, 1,
NULL);
XtAddCallback (btnOk, XmNactivateCallback, cancel, (XtPointer) toplevelMDE);
XtManageChild (btnOk);
btnCancel = XmCreatePushButton (action_area, "Cancel", NULL, 0);
XtVaSetValues (btnCancel, XmNleftAttachment, XmATTACH_POSITION,
XmNleftPosition,8,
XmNtopAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_POSITION,
XmNrightPosition, 13,
XmNshowAsDefault, 0,
XmNdefaultButtonShadowThickness, 1,
NULL);
XtAddCallback (btnCancel, XmNactivateCallback, cancel, (XtPointer) toplevelMDE);
XtManageChild (btnCancel);
XtManageChild (frame3);
XtManageChild (rc);
XtManageChild (mainform);
XtManageChild (action_area);
XtManageChild (pane);
PositionDialog (pane);
free(check_items);
free(check_text);
}
Thu, 10/09/2003 - 09:24#1
Anonymous
HEEELP reading textfields
Use XmTextFieldGetString(the_widget) and don`t forget to XtFree the result afterwards.
Thu, 10/09/2003 - 09:52#2
Anonymous
HEEELP reading textfields
I tried that, but then, I will only get the information in the last textfield.
Thu, 10/09/2003 - 10:27#3
Anonymous
HEEELP reading textfields
You`ll need to store the widgets returned from the calls to XmCreateTextField(), otherwise you can`t easily access the widgets.
Either that, or name the widgets in the loop with something like
char name[64];
sprintf(name, "text_%d", loop_index)
XmCreateTextField(... name ..)
and search for them with
Widget textfield = XtNameToWidget(parent, "text_12");
char *str = XmTextFieldGetString(textfield);
...
XtFree(str);
Fri, 10/10/2003 - 09:23#4
Anonymous
HEEELP reading textfields
I tried it, but I get the next message
Error Object "" does not have windowed ancestor
I tried it like
Widget textfield
char buff[32], *str;
int i;
...
for (i=0; i {
...
sprintf(buff, "text_%d", i)
text = XmCreateTextField (subform, buff, args, 1);
...
}
...
textfield = XtNameToWidget(text, "text_12");
str = XmTextFieldGetString(textfield);
...
XtFree(str);
...
Fri, 10/10/2003 - 09:54#5
Anonymous
HEEELP reading textfields
This line is wrong...
textfield = XtNameToWidget(text, "text_12");
Read the man pages. XtNameToWidget will search the widget hierarchy starting from `first argument` for a widget called `second argument`.
What you should have is
textfield = XtNameToWidget(subform, "text_12");
Assuming, of course, that you have created at least 12 textfields (i.e. `x` > 12)
Fri, 10/10/2003 - 12:08#6
Anonymous
HEEELP reading textfields
I found it.
It is
textfield = XtNameToWidget(rc, "subform_12.text_12");
Now I have to find a way to use this after I pressed an "Ok" button.