Is it possible to modify how Text swt widget wrap word?

梦想的初衷 提交于 2019-12-23 12:34:45

问题


The development platform is an Eclipse RCP 3.4 on Fedora (It's using Gtk native component). I need to avoid word wrapping.

i.e.: "Hello World" is displayed as case N°1 but I need the case N°2.

1|Hello    |    2|Hello Wor|
 |World    |     |ld       |

Is there a simple solution ?

I've seen that swt Text handle is build with OS.GTK_WRAP_WORD_CHAR wrap mode. I would like to test the behaviour with constant like OS.GTK_WRAP_WORD or OS.GTK_WRAP_NONE but I don't know how to perform this miracle ? Is it possible to modify platform specific configuration programmatically ?

EDIT 1 - There is a solution from one of my colleagues:

import org.eclipse.swt.SWT;
import org.eclipse.swt.internal.gtk.OS;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class SampleGrabExcess {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
      shell.setLayout(new GridLayout());
      shell.setSize(70, 70);

      GridData gridData = new GridData();
      Text addressText = new Text(shell, SWT.BORDER | SWT.MULTI) {
         @Override
         protected void checkSubclass() {}

         @Override
         protected void checkWidget() {
            OS.gtk_text_view_set_wrap_mode(handle, 1);
            super.checkWidget();
         }

      };
      gridData = new GridData();
      gridData.horizontalAlignment = SWT.FILL;
      gridData.grabExcessHorizontalSpace = true;
      gridData.verticalAlignment = SWT.FILL;
      gridData.grabExcessVerticalSpace = true;
      addressText.setLayoutData(gridData);
      addressText.setText("Hello World.");

      shell.open();

      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
   }
}

The result is that expected. I'm looking for a solution from http://dev.eclipse.org.


回答1:


No, I don't see how you could do that.

Btw, even if you could, setting the mode to OS.GTK_WRAP_WORD and OS.GTK_WRAP_NONE wouldn't get you the desired behaviour, since the first one breaks line in between words, just like OS.GTK_WRAP_WORD_CHAR, and the second one doesn't break lines at all. The mode you're looking for is GTK_WRAP_CHAR. However, the fact that a constant such as OS.GTK_WRAP_CHAR isn't even declared in org.eclipse.swt.internal.gtk.OS indicates that the creators of SWT were probably against allowing such behaviour, even in theory.



来源:https://stackoverflow.com/questions/5975046/is-it-possible-to-modify-how-text-swt-widget-wrap-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!