[comp.windows.x] X/OLIT question

fm@neptune.iex.com (Mohammad Faroog) (03/05/91)

I have just started programming in X. I came accross a couple of problems. 
Is there anyone who can explain me why it is not doing what I think it suppose
to do.

I am using textfield widget of openlook intrinsics toolkit. What I want to do
when ever pointer is in the field and if user presses any key widget should call
application supplied function. As you know textfield widget doesn't offer 
any call back of that kind. So I tried to solve this problem by to different
methods. 

1- I used XtAddEventHandler()

1- I modified widget's translation table.

Method - 1 code example :

#include <stdio.h>
#include <errno.h>

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>

#include <Xol/OpenLook.h>
#include <Xol/ControlAre.h>
#include <Xol/TextField.h>
#include <Xol/Form.h>
#include <Xol/Caption.h>

myhandler()
{
	fprintf(stderr,"hello world");
}

void main(argc, argv)
unsigned int argc;
char *argv[];
{

	Widget w_toplevel, w_form, w_text, w_caption;

	w_toplevel = OlInitialize(
		NULL, 				/* app name - filled by Xt */
		"Event", 			/* app class */
		NULL,				/* option list */
		0,					/* # of options */
		&argc, 				/* addr of main argc */
		argv				/* main argv */
	);

	w_form = XtVaCreateWidget(	/* create form widget */
		"form",
		formWidgetClass,
		w_toplevel,
		XtNxResizable,FALSE, XtNyResizable, FALSE, NULL
	);

	w_caption = XtVaCreateManagedWidget(
       "fieldLabel",
       captionWidgetClass,
       w_form,
       XtNlabel,   "File Name:", 0
    );

	w_text = XtVaCreateManagedWidget(
		"textfield",
		textFieldWidgetClass,
		w_caption, 
		XtNwidth,    200,
		NULL
     );
	XtAddEventHandler(w_text,KeyPressMask,FALSE,myhandler,NULL);

	XtManageChild(w_form); 
	(void) XtRealizeWidget(w_toplevel);

	(void) XtMainLoop();
}

What I expected, while pointer is in the text field, when user presses any key
myhandler() will be called. Somehow it does not do anything like that. But If I
change KeyPressMask to StructureNotifyMask then it calls myhandler()( of course
when window is configured).

Method - 2 example code:

#include <stdio.h>
#include <errno.h>

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>

#include <Xol/OpenLook.h>
#include <Xol/TextField.h>
#include <Xol/Form.h>
#include <Xol/Caption.h>

XtActionProc Myfunc()
{
	fprintf(stderr,"hello world");
}

void main(argc, argv)
unsigned int argc;
char *argv[];
{
	static XtActionsRec new_actions[] = {
		{"myfunc",(XtActionProc) Myfunc}
		};
	Widget w_form, w_caption, w_text;

	XtAppContext app_context;
	Widget w_toplevel;

	w_toplevel = OlInitialize(
		NULL, 				/* app name - filled by Xt */
		"Trans",	 		/* app class */
		NULL,				/* option list */
		0,					/* # of options */
		&argc, 				/* addr of main argc */
		argv				/* main argv */
	);

	app_context = XtWidgetToApplicationContext(w_toplevel);
	XtAppAddActions(app_context,new_actions,XtNumber(new_actions));

	w_form = XtVaCreateWidget(	/* create form widget */
		"form",
		formWidgetClass,
		w_toplevel,
		XtNxResizable,FALSE, XtNyResizable, FALSE, NULL
	);

	w_caption = XtVaCreateWidget(
       "fieldLabel",
       captionWidgetClass,
       w_form,
	   NULL
    );

	w_text = XtVaCreateManagedWidget(
		"textfield",
		textFieldWidgetClass,
		w_caption, 
		XtNwidth,    200,
		NULL
     );

	XtManageChild(w_caption); 
	XtManageChild(w_form); 
	(void) XtRealizeWidget(w_toplevel);

	(void) XtMainLoop();
}

Resource table for 2nd method:

*label:File Name: 
*textfield.translations: #override\n\
<Key>:myfunc()

Every time I press any key I can see the text in the field, but myfunc() is 
never called. Am I missing something? I was reading openlook GUI RM it says 
in the core widget's resources that application should not change 
XtNtranslations. One thing surprise me that unlike motif, openlook toolkit 
documentation does not provide info. about the default translation table of
its widget set. Does it mean we should not modify wigdet's translation table?
I will appreciate if you point me to the right direction.

One other thing I noticed that you cannot change the cursor for textfield
window.

Mohammad Farooq.

address: fm@iex.com

grp@Unify.com (Greg Pasquariello) (03/06/91)

In article <fm.668135416@neptune>, fm@neptune.iex.com (Mohammad Faroog) writes:
> 
> I have just started programming in X. I came accross a couple of problems. 
> Is there anyone who can explain me why it is not doing what I think it
suppose
> to do.
> 
> I am using textfield widget of openlook intrinsics toolkit. What I want to do
> when ever pointer is in the field and if user presses any key widget should
call
> application supplied function. As you know textfield widget doesn't offer 
> any call back of that kind. So I tried to solve this problem by to different
> methods. 

Your problem is that you are installing the event handler on the wrong 
widget.  The TextField widget is made up of a sub-widget called the
TextEdit widget.  You need to get the id of that sub-widget, and install
the event handler on it.  The id of the sub-widget can be had by getting
the resource XtNtextEditWidget on the TextField.

I have modified your example slightly to demonstrate.  The modifications
are marked with comments.

> 
> 1- I used XtAddEventHandler()
> 
> 1- I modified widget's translation table.
> 
> Method - 1 code example :
> 
> #include <stdio.h>
> #include <errno.h>
> 
> #include <X11/Intrinsic.h>
> #include <X11/StringDefs.h>
> 
> #include <Xol/OpenLook.h>
> #include <Xol/ControlAre.h>
> #include <Xol/TextField.h>
> #include <Xol/Form.h>
> #include <Xol/Caption.h>
> 
> myhandler()
> {
> 	fprintf(stderr,"hello world");
> }
> 
> void main(argc, argv)
> unsigned int argc;
> char *argv[];
> {
> 
	Arg	warg;			/* MODIFICATION */
	Widget	subWid;			/* MODIFICATION */

> 	Widget w_toplevel, w_form, w_text, w_caption;
> 
> 	w_toplevel = OlInitialize(
> 		NULL, 				/* app name - filled by Xt */
> 		"Event", 			/* app class */
> 		NULL,				/* option list */
> 		0,					/* # of options */
> 		&argc, 				/* addr of main argc */
> 		argv				/* main argv */
> 	);
> 
> 	w_form = XtVaCreateWidget(	/* create form widget */
> 		"form",
> 		formWidgetClass,
> 		w_toplevel,
> 		XtNxResizable,FALSE, XtNyResizable, FALSE, NULL
> 	);
> 
> 	w_caption = XtVaCreateManagedWidget(
>        	"fieldLabel",
>        	captionWidgetClass,
>        	w_form,
>        	XtNlabel,   "File Name:", 0
>     	);
> 
> 	w_text = XtVaCreateManagedWidget(
> 		"textfield",
> 		textFieldWidgetClass,
> 		w_caption, 
> 		XtNwidth,    200,
> 		NULL
>      	);

	XtSetArg(wargs, XtNtextEditWidget, &subWid); 	/* MODIFICATION */
	XtGetValues(w_text, warg, 1);			/* MODIFICATION */
	
	XtAddEventHandler(subWid, KeyPressMask,		/* MODIFICATION */
			  FALSE, myhandler, NULL);

> 
> 	XtManageChild(w_form); 
> 	(void) XtRealizeWidget(w_toplevel);
> 
> 	(void) XtMainLoop();
> }
> 
> Mohammad Farooq.
> 
> address: fm@iex.com

--

---
Greg Pasquariello	grp@unify.com
Unify Corporation 	Be good and never poison people

mls@cbnewsm.att.com (mike.siemon) (03/07/91)

> I am using textfield widget of openlook intrinsics
> toolkit. What I want to do when ever pointer is in the field
> and if user presses any key widget should call application
> supplied function.

> As you know textfield widget doesn't offer any call back of
> that kind. So I tried to solve this problem by to different
> methods.

Yes, the TextField widget doesn't directly provide this
callback. However, it uses the TextEdit widget, and that widget
provides this callback (XtNkey). The TextField's XtNtextEditWidget
resource provides a handle to the TextEdit widget.
NOTE: This assumes you are using the OLIT toolkit from Sun's
OpenWindows 2.0 product (or later). If you are using an earlier
version of OLIT, the TextField widget uses the older Text widget,
which doesn't have the XtNkey resource. You'll have to use
the event handler or translation table method you tried
(see below).

> I used XtAddEventHandler() [on the TextField widget] ....

The TextField widget is a composite, as suggested in my paragraph
above. Thus the window of the widget on which the event handler
is registered will not be getting the events, rather the window
of the TextEdit widget (or Text widget, for older versions) will
get the events. Place the event handler on the child widget
and this should work. NOTE: If you are using the older toolkit,
you will have to fetch the Text widget ID by examining the
children list of the composite instance record...ugh!

We should have made the TextField a subclass of TextEdit,
not a composite that creates a TextEdit child. Sorry!

> What I expected, while pointer is in the text field, when
> user presses any key myhandler() will be called. Somehow it
> does not do anything like that.

Also, make sure you are operating in a focus-follows-pointer
mode. If not, you must first click in the window to move
focus to the widget. Without focus in the widget, keyboard
events will not go anywhere near the TextField widget (or its
child).

> 2- I modified widget's translation table.
> ....
> Resource table for 2nd method:
> *label:File Name: 
> *textfield.translations: #override\n\
> <Key>:myfunc()

Again, the child of the TextField widget is the one getting the
keyboard events, so it must have its translation table modified.
The name of this child is "Textedit" (another mistake, we didn't
follow widget naming conventions--should be "textedit" or "textEdit").

> I was reading openlook GUI RM it says in the core widget's
> resources that application should not change
> XtNtranslations. One thing surprise me that unlike motif,
> openlook toolkit documentation does not provide info. about
> the default translation table of its wiget set. Does it mean
> we should not modify wigdet's translation table?

For most widgets this is deliberate, because we are trying to
give application USERS a consistent interface across diverse
applications. Thus we provide high-level callbacks for widgets
like buttons, scrollbars, scrolling lists, etc. Even for text
widgets we provide high-level callbacks so that most
applications can simply fetch text from the user, thereby
providing a consistent text-input interface. However, text
input needs are also a highly variable, and we recognized that
we can't constrain application developers too tightly here.
Thus the XtNkey callback on the TextEdit widget.

It is a reasonable request that the translation tables be
advertized for at least the more important widgets, such as
the TextEdit (and TextField, if we ever make it a subclass
of TextEdit!)

Steve Humphrey
UNIX System Laboratories
merlyn@attunix.att.com