Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [platform-swt-dev] The styledText's offset issue

The right way to get the line delimiter length is:
StyledText#getLineDelimiter().length().

It is equals, in the current implementation, to 
System.getProperty("line.separator").

Felipe




"Arunachalam" <arunachalaml@xxxxxxxxxxxxxxxxx> 
Sent by: platform-swt-dev-admin@xxxxxxxxxxx
07/24/2004 01:31 AM
Please respond to
platform-swt-dev


To
<platform-swt-dev@xxxxxxxxxxx>
cc

Subject
RE: [platform-swt-dev] The styledText's offset issue






Hi Felipe,
                 Thanks for your detailed response. Actually I made the 
things complex. Your
sample is really simple. But can you tell one thing. How the line 
delimeter
gets increased? How long it can be? It would be great to have a answer for
this.

Thanks
Arun

-----Original Message-----
From: platform-swt-dev-admin@xxxxxxxxxxx
[mailto:platform-swt-dev-admin@xxxxxxxxxxx]On Behalf Of Felipe Heidrich
Sent: Thursday, July 22, 2004 3:38 AM
To: platform-swt-dev@xxxxxxxxxxx
Subject: Re: [platform-swt-dev] The styledText's offset issue


Hi,

I don't know why your code is failing, it is kind of hard to tell if your
code is doing the rigth thing or not, you have too many special cases and
you also assume that the line delimiter length is always 1.

I wrote this:
DropTarget dropTarget = new DropTarget (text, DND.DROP_MOVE |
DND.DROP_COPY | DND.DROP_LINK);
dropTarget.setTransfer(new Transfer[] {TextTransfer.getInstance()});
dropTarget.addDropListener(new DropTargetAdapter() {
        public void drop(DropTargetEvent event) {
                if (event.data == null) return;
                Point point = display.map(null, text, event.x, event.y);
                int offset;
                try {
                        offset = text.getOffsetAtLocation(point);
                } catch (IllegalArgumentException e) {
                        int lineIndex = (text.getTopIndex() + point.y) /
text.getLineHeight();
                        lineIndex = Math.min(text.getLineCount() - 1,
lineIndex);
                        int lineOffset = text.getOffsetAtLine(lineIndex);
                        String line =
text.getContent().getLine(lineIndex);
                        offset = lineOffset + line.length();
                }
                text.setCaretOffset(offset);
                text.insert((String)event.data);
        }
});

It does the same work, I didn't test a lot but I expect it to work for
normal cases, should work if you have scrollbars as well, fails if the
widget is wrap (not sure I how to fix it for wrap).

If you think your code is right and there is something wrong with
StyledText please open a problem report against SWT
(https://bugs.eclipse.org/bugs/)
To ask user-question the SWT newsgroup is the best place
(news://news.eclipse.org/eclipse.platform.swt)

Thanks
Felipe





"Arunachalam" <arunachalaml@xxxxxxxxxxxxxxxxx>
Sent by: platform-swt-dev-admin@xxxxxxxxxxx
07/16/2004 02:31 AM
Please respond to
platform-swt-dev


To
<platform-swt-dev@xxxxxxxxxxx>
cc

Subject
[platform-swt-dev] The styledText's offset issue






Hi,
   I was trying to implement the drag and drop in styledtext widget.  When
I
dragged one item from view and dropped at empty line of the styled text
widget, I need to insert a string (which is relative to the dragged item)
at
that location relative to the mouse cursor position. At that time I found
one behavior is that the offset of the StyledText is getting reduced by
one.
It was throwing RuntimeException(IllegalArgument)

                                 try {
 insertText(text,startNextLineOffset -1);
                                 } catch (RuntimeException e1) {
 insertText(text,startNextLineOffset -2);
                                 }
So I need to reduce one further. Sometimes the try block works. Once it
reaches the catch block, from the next time onwards only the catch block
is
getting executed. This code works fine. But it would be nice to know the
reason.

Thanks and regards,
ArunachalaM

********************************************************************

FYI, the code snippet is below, the problematic code is italicized.

      StyledText text = MyEditor.getTextWidget();
      Point location =
Display.getDefault().map(null,text,event.x,event.y);//new Point(event.x,
event.y);
                 Point maxLocation =
text.getLocationAtOffset(text.getCharCount());
                 int maxOffset = text.getCharCount();
                 if (location.y >= maxLocation.y + text.getLineHeight()) {
                   insertText(text, maxOffset);
                   return;
                 }

                 int startLineOffset= text.getOffsetAtLocation(new
Point(0,
location.y));
                 int line = text.getLineAtOffset(startLineOffset);
                 if (line == text.getLineCount() - 1) {
                                 if (location.x > maxLocation.x +
text.getLocation().x) {
 insertText(text,maxOffset);
                                 } else {
                                                 int offset =
text.getOffsetAtLocation(location);
                                                 insertText(text,offset);
                                 }
                                 return;
      }
      int startNextLineOffset = text.getOffsetAtLine(line+1);
                 Point lineEnd =
text.getLocationAtOffset(startNextLineOffset - 1);
                 if (location.x > lineEnd.x) {
                                 try {
 insertText(text,startNextLineOffset -1);
                                 } catch (RuntimeException e1) {
 insertText(text,startNextLineOffset -2);
                                 }
                 } else {
                    int offset = text.getOffsetAtLocation(location);
                    insertText(text,offset);
         return;
                 }

                 private void insertText(StyledText text, int offset) {
                     text.setCaretOffset(offset);
                     text.insert(fqnName);
                 }



_______________________________________________
platform-swt-dev mailing list
platform-swt-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/platform-swt-dev




Back to the top