From: kcrews@bbn.com (Keith Crews)
Newsgroups: comp.windows.x.motif
Subject: Problem with Motif text selection
Date: 25 Aug 1992 18:28:58 GMT
NNTP-Posting-Host: bbn.com

I hope that someone can help me with the problem I am having with
selecting text in a Motif text widget.  In the description below I use
the term Motif to refer to the combination Motif/Xt.  I am using Motif
1.1.1 from DEC.  

Please respond via email to keith@belmont.com.  I am a guest on this
system and do not read news here.  I will summarize if there is
sufficient interest.

I have a Motif program that does the following in response to a key
press.  It puts a new value in a text widget with XtSetValues.  It then
does an XmTextSetSelection on the text widget specifying the range 0 to
strlen(newstring) and uses CurrentTime for time.  The text widget has
focus.  This works fine when running to a DEC station 5000 server but
not when running to an HP 710 server.  When connected to the HP server
every other time it ends up not being selected and every other time it
is selected.  When I use the time in the last event (the key press)
rather than CurrentTime a different problem occurs where Motif shows the
text as highlighted (and thus apparently selected) each time but in
fact it only acts selected every other time.  In the first
(CurrentTime) case the server thinks that the text widget has the
selection after each XmTextSetSelection.  In the other case the server
only thinks the text widget has the selection half the time.  In the
second case, the problem occurs on both the HP and DEC.  In all cases
program is running on the DEC station.  I have observed the same
problem when running an HP version of the client but have not
debugged it in that environment.

The server difference that causes the first problem to occur on
the HP server is that if a client owns the primary selection and does
an XSetSelectionOwner with an owner argument of None, the client
receives a SelectionClear event from the server.  This event is not
sent by the DEC server.  Motif makes this call in response to the
XtSetValues that puts the new string in the text widget.  When I do the
XmTextSetSelection before returning to the event loop, the selection
clear event is processed after the XmTextSetSelection and the selection
is cleared again.  It appears that this is a bug in the DEC server -
see XSetSelectionOwner in Vol. 2 of O'Reilly or page 96 of X Window
System by Scheifler and Gettys.  However, it is when connected to the
HP server that the problem occurs.

I believe that the sequence of events inside Motif that causes the
problem when connected to  the HP is that when XmTextSetSelection is called
with CurrentTime Motif remembers that as the time of the last selection.
This selection succeeds.  Then when the selection clear event is
subsequently processed it has a non-zero time and Motif decides it
doesn't have the selection anymore.  However, the server has processed
the set selection after generating the selection clear and knows that
this is the order of events so it thinks that the selection is owned by
the text widget.

I wrote a program that returns the server's idea of the current owner
of the primary selection.  In all the relevant cases the server thinks
it's the text widget, even when it is not highlighted.  I wrote another
program that gets and releases the primary selection and watches for
events coming back from the server. When connected to the HP you get
selection clear events and when connected to a DEC server you don't.
These are both simple X lib level programs so are Xt and Motif
independent.  I also hooked up xscope and monitored what was being sent
between the client and server and there were no selection related
requests or events except those that resulted from the XtSetValues and
XmTextSetSelection.

I tried to work around this problem by various hacks, such as setting the
selection twice, explicitly clearing it before selecting it, setting
the selection to another widget and then setting it to the one we
really want, etc. and none of them has any effect, except some of them
cause annoying extra flashing.

I finally got around this by adding code between the XtSetValues and
the XmTextSetSelection.  This code does an XSync and then processes any
selection clear events.  The program now works on both DEC and HP
platforms.

There is a different problem if you use the time of the last key press
on the XmTextSetSelection call rather than CurrentTime.  This problem
seems to be server independent.  In this case you get text alternately
selected and not selected but it is always highlighted in the text
widget.  I think this is because when the widget decides to give up the
selection it does so with a call to XSetSelectionOwner with owner None
and time CurrentTime.  The server substitutes the current server time
for CurrentTime.  Then when the subsequent set selection is done using
the keypress time this is an earlier time than the time in the previous
call and the set selection fails.  It seems that despite this Motif
highlights the text.  One possible way around this would be to send a
request before the set selection that makes a round trip to the server
and returns the server time, which should then be later than the time
that the server used when the selection was relinquished.

However, even if this were done it is not clear that things would work
correctly because there are times when clicking and typing in the text
widget do not remove the highlighting.  Whether this is a result of the
previous problem or some different problem I don't know.

I guess my questions are:  1) Is my analysis correct?  2) Is there a
better way around the problem than using CurrentTime, doing an XSync
and checking the event queue?  3) Isn't this all really a bug in the
Motif selection support?  4) If the answer to 1) is no, where did I go
wrong? 5) Is this in any way related to Motif bug 2252 which has the
description, "Text selection isn't always selected?"?

Remember:  Please respond to keith@belmont.com

Keith Crews
Belmont Research
84 Sherman St.
Cambridge, Mass. 02140
617-868-6878


Newsgroups: comp.windows.x.motif