wxMac and TextControl problem.

Hi,

I think there might be a problem with textctrl on the mac...

I have a need to be able to capture the line containing
the cursor when a <shift><return> key combo is pressed

I am using a PyValidator to watch for the shift return.

class CharValidator(wx.PyValidator):
     def __init__(self,textWin):
          wx.PyValidator.__init__(self)
          self.Bind(wx.EVT_CHAR, self.OnChar)
          self.textWin = textWin

     def Clone(self):
          """
          Note that every validator must implement the Clone() method.
          """
          return CharValidator(self.textWin)

     def Validate(self, win):
          return True

     def TransferToWindow(self):
          return True

     def TransferFromWindow(self):
          return True

     def OnChar(self, evt):
         if (evt.GetKeyCode() == wx.WXK_RETURN) and evt.ShiftDown() :
             #wx.MessageBox("Got it %d,%s" % (evt.GetKeyCode(),str(evt.ShiftDown())) )
             theCtl = self.GetWindow()
             #wx.MessageBox("*"+theCtl.GetStringSelection()+'*')
             theStr = theCtl.GetStringSelection()
             if theStr != '' :
                 self.textWin.ExecString(theStr)
             else:
                 # get the line containing the cursor
                 thePos = theCtl.GetInsertionPoint()
                 colRow = theCtl.PositionToXY(thePos)
                 theRow = colRow[1]
                 str = theCtl.GetLineText(theRow)
                 if re.search('\S',str)!=None: self.textWin.ExecString(str)
         else : evt.Skip()

If my text window can hold 5 lines and I enter numbers 1..10 (on separate lines )
and I "shift return" on any of the top lines. I will get the "right" line.

However if I scroll down past the first 5 lines I and do a shift return I get the
a line containing 1..5 depending on the relative position.

It appears that code to calculate theRow in the above proc does not account
for scrolling in the window.

The same code works *ok* on my linux box!

Is this a known problem with wxMac?
(OSX 10.latest/Python 2.5.1/wxPython 2.8.4

Thanks,

Jerry

Hi,

I think there might be a problem with textctrl on the mac...

I have a need to be able to capture the line containing
the cursor when a <shift><return> key combo is pressed

I am using a PyValidator to watch for the shift return.

class CharValidator(wx.PyValidator):
    def __init__(self,textWin):
         wx.PyValidator.__init__(self)
         self.Bind(wx.EVT_CHAR, self.OnChar)
         self.textWin = textWin

    def Clone(self):
         """
         Note that every validator must implement the Clone() method.
         """
         return CharValidator(self.textWin)

    def Validate(self, win):
         return True

    def TransferToWindow(self):
         return True

    def TransferFromWindow(self):
         return True

    def OnChar(self, evt):
        if (evt.GetKeyCode() == wx.WXK_RETURN) and evt.ShiftDown() :
            #wx.MessageBox("Got it %d,%s" % (evt.GetKeyCode(),str(evt.ShiftDown())) )
            theCtl = self.GetWindow()
            #wx.MessageBox("*"+theCtl.GetStringSelection()+'*')
            theStr = theCtl.GetStringSelection()
            if theStr != '' :
                self.textWin.ExecString(theStr)
            else:
                # get the line containing the cursor
                thePos = theCtl.GetInsertionPoint()
                colRow = theCtl.PositionToXY(thePos)
                theRow = colRow[1]
                str = theCtl.GetLineText(theRow)
                if re.search('\S',str)!=None: self.textWin.ExecString(str)
        else : evt.Skip()

If my text window can hold 5 lines and I enter numbers 1..10 (on separate lines )
and I "shift return" on any of the top lines. I will get the "right" line.

However if I scroll down past the first 5 lines I and do a shift return I get the
a line containing 1..5 depending on the relative position.

It appears that code to calculate theRow in the above proc does not account
for scrolling in the window.

The same code works *ok* on my linux box!

Is this a known problem with wxMac?
(OSX 10.latest/Python 2.5.1/wxPython 2.8.4

Thanks,

Jerry

More info:
    Doing some careful counting in the above procedure
        thePos = theCtl.GetInsertionPoint()
    Appears to be returning the correct offset into the string
    for the location of the cursor.

    The next statement
         colRow = theCtl.PositionToXY(thePos)
    does not properly compute the (col,row) properly when the
    window is scrolled.

Thanks,

Jerry

Jerry LeVan wrote:

It appears that code to calculate theRow in the above proc does not account
for scrolling in the window.

The same code works *ok* on my linux box!

Is this a known problem with wxMac?
(OSX 10.latest/Python 2.5.1/wxPython 2.8.4

Please enter a bug report about this with a category of "wxMac Specific".

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Well I tried several times but SourceForge is being very naughty this
morning, I cannot event browse the bug database...

Jerry

···

On May 31, 2007, at 1:14 AM, Robin Dunn wrote:

Jerry LeVan wrote:

It appears that code to calculate theRow in the above proc does not account
for scrolling in the window.
The same code works *ok* on my linux box!
Is this a known problem with wxMac?
(OSX 10.latest/Python 2.5.1/wxPython 2.8.4

Please enter a bug report about this with a category of "wxMac Specific".

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Hi,

I think there might be a problem with textctrl on the mac...

I have a need to be able to capture the line containing
the cursor when a <shift><return> key combo is pressed

I am using a PyValidator to watch for the shift return.

class CharValidator(wx.PyValidator):
    def __init__(self,textWin):
         wx.PyValidator.__init__(self)
         self.Bind(wx.EVT_CHAR, self.OnChar)
         self.textWin = textWin

    def Clone(self):
         """
         Note that every validator must implement the Clone() method.
         """
         return CharValidator(self.textWin)

    def Validate(self, win):
         return True

    def TransferToWindow(self):
         return True

    def TransferFromWindow(self):
         return True

    def OnChar(self, evt):
        if (evt.GetKeyCode() == wx.WXK_RETURN) and evt.ShiftDown() :
            #wx.MessageBox("Got it %d,%s" % (evt.GetKeyCode(),str(evt.ShiftDown())) )
            theCtl = self.GetWindow()
            #wx.MessageBox("*"+theCtl.GetStringSelection()+'*')
            theStr = theCtl.GetStringSelection()
            if theStr != '' :
                self.textWin.ExecString(theStr)
            else:
                # get the line containing the cursor
                thePos = theCtl.GetInsertionPoint()
                colRow = theCtl.PositionToXY(thePos)
                theRow = colRow[1]
                str = theCtl.GetLineText(theRow)
                if re.search('\S',str)!=None: self.textWin.ExecString(str)
        else : evt.Skip()

If my text window can hold 5 lines and I enter numbers 1..10 (on separate lines )
and I "shift return" on any of the top lines. I will get the "right" line.

However if I scroll down past the first 5 lines I and do a shift return I get the
a line containing 1..5 depending on the relative position.

It appears that code to calculate theRow in the above proc does not account
for scrolling in the window.

The same code works *ok* on my linux box!

Is this a known problem with wxMac?
(OSX 10.latest/Python 2.5.1/wxPython 2.8.4

Thanks,

Jerry

More info:
   Doing some careful counting in the above procedure
       thePos = theCtl.GetInsertionPoint()
   Appears to be returning the correct offset into the string
   for the location of the cursor.

   The next statement
        colRow = theCtl.PositionToXY(thePos)
   does not properly compute the (col,row) properly when the
   window is scrolled.

Thanks,

Jerry

I have half a workaround for the problem because I only need the contents
of the row at the position.

Fix:
Replace

            else:
                # get the line containing the cursor
                thePos = theCtl.GetInsertionPoint()
                colRow = theCtl.PositionToXY(thePos)
                theRow = colRow[1]
                str = theCtl.GetLineText(theRow)
                if re.search('\S',str)!=None: self.textWin.ExecString(str)
        else : evt.Skip()

by
             else:
                 # get the line containing the cursor
                 thePos = theCtl.GetInsertionPoint()
                 colRow = theCtl.PositionToXY(thePos)
                 if sys.platform == 'darwin' : theRow = self.textWin.MyPosToRow(thePos)
                 else: theRow = colRow[1]
                 str = theCtl.GetLineText(theRow)
                 if re.search('\S',str)!=None: self.textWin.ExecString(str)
         else : evt.Skip()

In the TextCtrl container add the following method:

     # calculate the row number from the position in the text
     def MyPosToRow(self,pos) :
         lines = self.theText.GetNumberOfLines()
         currPos = 0
         for row in range(lines):
             currPos = currPos + self.theText.GetLineLength(row)
             if currPos >= pos :
                 return row

where self.theText is the TextCtrl control.

Jerry