2

I am trying to underline some specific words in single line wx.textctrl. I'm doing it by calculating position of a word in the control using GetTextExtent and drawing lines using wx.ClintDC.

My problem begins when text in TextCtrl is longer then size of the control, for example if user inputs ~100 chars he can see only 20 last characters if the control is not long enough.

My question is: how can I get a string which is actually displayed in the single line control? or how can I calculate the width of the text which is not displayed (on the left side) in pixels?

Single line text control has no scroll bar. The GetInsertionPoint returns position of caret in text, but it's impossible to translate it to the actual distance in pixels from the left border of control.

I don't want to use TE_RICH or TE_RICH2 styles not StyledTextCtrl class because they don't support right-to-left text.

Wujek Brulion
  • 61
  • 1
  • 5

2 Answers2

0

I think you have to use one of the TE_RICH style to underline text in a wx.TextCtrl. Have you tried the StyledText controls? They use scintilla which might give you more control.

There's also the ExpandoTextCtrl, which will expand as the user types.

Also if you set the locale, you should get the right-to-left behavior automagically: http://wiki.wxpython.org/Internationalization#Switching_between_Left_to_Right_and_Right_to_Left_Languages

I don't really see a method to just grab the part of the string that is visible. You'll have to calculate it using the font's width and probably using the Selection methods of the text control. You may want to cross-post to the wxPython mailing list too. They would probably have ideas too.

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
  • Thanks, but as I wrote, TE_RICH, TE_RICH2 and StyledTextCtrl class don't support RTL text AFAIK. Ans I don't want to change layout of my application but only to let them input RTL text in some controls. – Wujek Brulion Jul 15 '11 at 14:58
  • Did you try SetLayoutDirection() with the rich controls or StyledTextCtrl to see if it worked? You don't have to change your program. Just write a quick one-off to check. – Mike Driscoll Jul 15 '11 at 15:49
  • Scintilla currently has issues with RTL text display for LTR text and indicators (especially if RTL and LTR are combined). – Wujek Brulion Jul 19 '11 at 10:32
  • Sorry. I wasn't aware of that. I had asked Robin Dunn (creator of wxPython) about what to do and he thought that SetLayoutDirection() would work for you, which is why I mentioned it. – Mike Driscoll Jul 19 '11 at 12:52
0

Ok, the solution I've found is:

from win32api import SendMessage
result = SendMessage(self.GetHandle(), EM_POSFROMCHAR, 0, 0)

result is client coordinates of the first character in the control. The value is negative if the specified character is not displayed in the edit control's client area.

Wujek Brulion
  • 61
  • 1
  • 5