Programaticly moving cursor in multi-line edit control

View: New views
5 Messages — Rating Filter:   Alert me  

Parent Message unknown Programaticly moving cursor in multi-line edit control

by Igor Kaplan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Alexandre,

  Thanks a lot for your reply. I'll try to attach the source file to this
message, however not sure if this mailing list allows attachments, so I have
uploaded it also to my ftp side:
ftp://ftp.uliy.com/pub/python/te.py

  I tried to run this file on the smartphone and on PocketPc, the same
result. Was playing with OnOpen method detaching the Edit control from the
frame, adding the text and attaching it again, still the same, cursor does
not move.

  Would so much appreciate any comments.

Igor.

From: Alexandre Delattre <alexandre.delattre@...>
Subject: Re: [PythonCE] Programaticly moving cursor in multi-line edit
        control
To: pythonce@...
Message-ID: <48A37C90.5020804@...>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Igor,

 From what you say, I don't see what you could have done wrong.
can you send me the full source code  so I can give a try ?

Alexandre


import ppygui as gui
import os
import rdebug

class MainFrame(gui.CeFrame):
# subclass to create our own main frame type
  keyFlag = 0
  filename = ''
  defaultFolder = ''
  startSelection = 0
  endSelection = 0

  def __init__(self):
    gui.CeFrame.__init__(self, title="Text edit", menu="menu", action=("Clear", self.onClear))

    self.MainMenu = gui.Menu()
    self.FileMenu = gui.PopupMenu()
    self.TextMenu = gui.PopupMenu()
    self.EditMenu = gui.PopupMenu()
    self.navigateMenu = gui.PopupMenu()
    self.FileMenu.append("New", self.OnNew)
    self.FileMenu.append("Open...", self.OnOpen)
    self.FileMenu.append("Save", self.OnSave)
    self.FileMenu.append("Save As...", self.OnSaveAs)
    self.SymbolsMenu = gui.PopupMenu()
    self.SymbolsMenu.append("''", self.OnApostrophi)
    self.SymbolsMenu.append('[]', self.OnBrackets)
    self.SymbolsMenu.append('<', self.OnLess)
    self.SymbolsMenu.append('>', self.OnGreater)
    self.SymbolsMenu.append('{}', self.OnBraces)
    self.SymbolsMenu.append('=', self.OnEquals)
    self.TextMenu.append_menu('Symbols', self.SymbolsMenu)
    self.TextMenu.append("Cursor pos", self.OnCursorPosition)
    self.EditMenu.append("Start Selection", self.OnStartSelection)
    self.EditMenu.append("End Selection", self.OnEndSelection)
    self.EditMenu.append("Select line", self.OnSelectLine)
    self.EditMenu.append("Copy", self.OnCopy)
    self.EditMenu.append("Paste", self.OnPaste)
    self.EditMenu.append("Delete", self.OnDeleteSelection)
    self.navigateMenu.append("Start of file", self.OnStartOfFile)
    self.navigateMenu.append("End of file", self.OnEndOfFile)
    self.navigateMenu.append("Home", self.OnHome)
    self.navigateMenu.append("End", self.OnEnd)
    self.cb_menu.append_menu("File", self.FileMenu)
    self.cb_menu.append_menu("Edit", self.EditMenu)
    self.cb_menu.append_menu("Navigate", self.navigateMenu)
    self.cb_menu.append_menu("Text", self.TextMenu)
    self.cb_menu.append("Exit", self.onExit)
    self.text_entry = gui.Edit(self, multiline=True)
    sizer = gui.VBox(border=(0,0,0,0), spacing=0)
    sizer.add(self.text_entry)
    self.sizer = sizer
    self.text_entry.text = ""
    self.text_entry.focus()
   
  def onClear(self, x):
    self.text_entry.text = ""
    self.text_entry.bringtofront()
   
  def onExit(self, x):
    self.close()

  def OnNew(self, x):
    self.text_entry.text = ""
    self.filename = 'untitled'
    print "Window title: "+self.texte
    self.text = U'Untitled'
   
  def OnOpen(self, x):
    self.filename = gui.FileDialog.open()
    self.text = 'Please wait...'
    fl = open(self.filename, 'r')
    l = fl.readlines()
    self.text_entry = None
    self.text_entry = gui.Edit(self, multiline=True)
    for i in l:
      self.text_entry.append(i)
    sizer = gui.VBox(border=(0,0,0,0), spacing=0)
    sizer.add(self.text_entry)
    self.sizer = sizer  
    fl.close()
    l = []
    self.defaultFolder = os.path.split(self.filename)[0]
    self.text_entry.selection = 0, 0
    self.text = self.filename
    self.text_entry.update()
    self.text_entry.focus()
   
  def OnSave(self, x):
    if self.filename == '':
      self.filename = 'unnamed.txt'
    if self.defaultFolder == '':
      self.defaultFolder = '.'
      self.filename = os.path.join(self.defaultFolder, self.filename)
    fl = open(self.filename, "w")
    fl.write(self.text_entry.text)
    fl.close()
    self.text_entry.focus()
   
  def OnSaveAs(self, x):
    self.filename = gui.FileDialog.save()
    fl = open(self.filename, 'w')
    fl.write(self.text_entry.text)
    fl.close()
    self.text_entry.focus()

  def OnApostrophi(self, x):
    selection =  self.text_entry.selection
    pos = selection[0]
    self.text_entry.text = self.text_entry.text[:pos] + "''" + self.text_entry.text[pos:]
    self.text_entry.selection = (selection[0]+2, selection[1]+2)
   
  def OnBrackets(self, x):
    selection =  self.text_entry.selection
    pos = selection[0]
    self.text_entry.text = self.text_entry.text[:pos] + "[]" + self.text_entry.text[pos:]
    self.text_entry.selection = (selection[0]+2, selection[1]+2)
   
  def OnBraces(self, x):
    selection =  self.text_entry.selection
    pos = selection[0]
    self.text_entry.text = self.text_entry.text[:pos] + "{}" + self.text_entry.text[pos:]
    self.text_entry.selection = (selection[0]+2, selection[1]+2)
   
  def OnLess(self, x):
    selection =  self.text_entry.selection
    pos = selection[0]
    self.text_entry.text = self.text_entry.text[:pos] + "<" + self.text_entry.text[pos:]
    self.text_entry.selection = (selection[0]+1, selection[1]+1)
   
  def OnGreater(self, x):
    selection =  self.text_entry.selection
    pos = selection[0]
    self.text_entry.text = self.text_entry.text[:pos] + ">" + self.text_entry.text[pos:]
    self.text_entry.selection = (selection[0]+1, selection[1]+1)
   
  def OnEquals(self, x):
    selection =  self.text_entry.selection
    pos = selection[0]
    self.text_entry.text = self.text_entry.text[:pos] + "=" + self.text_entry.text[pos:]
    self.text_entry.selection = (selection[0]+1, selection[1]+1)

  def OnStartSelection(self, x):
    self.startSelection = self.text_entry.selection[0]
    self.text_entry.focus()
   
  def OnEndSelection(self, x):
    self.endSelection = self.text_entry.selection[0]
    self.text_entry.selection = (self.startSelection, self.endSelection)
    self.text_entry.focus()
   
  def OnSelectLine(self, x):
    start = self.text_entry.line_index(-1)
    end = self.text_entry.line_index(-1) + self.text_entry.line_length(-1)
    self.text_entry.selection = (start, end)
    self.text_entry.focus()
   
  def OnCopy(self, x):
    self.text_entry.copy()
    self.text_entry.focus()
   
  def OnPaste(self, x):
    self.text_entry.paste()
    self.text_entry.focus()
   
  def OnDeleteSelection(self, x):
    self.text_entry.cut()
    self.text_entry.focus()
   
  def OnStartOfFile(self, x):
    self.text_entry.selection = (0, 0)
    self.text_entry.update()
    self.text_entry.focus()
   
  def OnEndOfFile(self, x):
    eof = len(self.text_entry.text)
    self.text_entry.selection = (eof, eof)
    self.text_entry.focus()
   
  def OnHome(self, x):
    i = self.text_entry.line_index(-1)
    self.text_entry.selection = (i, i)
    self.text_entry.focus()
   
  def OnEnd(self, x):
    i = self.text_entry.line_index(-1) + self.text_entry.line_length(-1)
    self.text_entry.selection = (i, i)
    self.text_entry.focus()
   
  def OnCursorPosition(self, x):
    line = self.text_entry.line_from_char(-1)+1
    col = self.text_entry.selection[0] - self.text_entry.line_index(-1)+1
    msg = "Line: "+str(line) + " Col: " + str(col)
    gui.Message.ok("Cursor position", msg)
    self.text_entry.focus()
   
if __name__ == '__main__':
  #rdebug.set_std('igor.txt', 'igor.err')
  app = gui.Application()
  app.mainframe = MainFrame()
  app.run()
  app.mainframe.text_entry.focus()

_______________________________________________
PythonCE mailing list
PythonCE@...
http://mail.python.org/mailman/listinfo/pythonce

Parent Message unknown Re: Programaticly moving cursor in multi-line edit control

by jabapyth :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The way to preserve linebreaks is tp replace "\n" with "\r\n"
so: self.text_entry.text = fl.read().replace("\n","\r\n")

On Wed, Aug 13, 2008 at 8:05 PM, Igor Kaplan <igor_kaplan@...> wrote:
Hello,

 I would like to ask a little advice. Already several days I am trying to
figure out the following problem using ppygui: How to move the cursor in the
edit field to the top of that field.
 In my code I create the frame and then Edit control:
Self.text_entry = gui.Edit(multiline=True)
   sizer = gui.VBox(border=(2,2,2,2), spacing=2)
   sizer.add(self.text_entry)
..

 Later I open the text file and copy it's context into that edit control:
   fl = open(self.filename, 'r')
   l = fl.readlines()
   self.text_entry.text = ""
   for i in l:
     self.text_entry.append(i)

 Only reason why I add the text such long way because if I just do:
Self.text_entry.text = fl.read()
I looze all line breaks and all text goes into 1 line.

 After opening that file I do:
Self.text_entry.selection = 0, 0

 And that line does nothing. For some reasons the cursor always positions
at the same spot in the Edit box after opening the file, somewhere in the
middle and does not want to go to the beginning.

 I also have another event:
Def OnBegin(self, event):
 Self.text_entry.selection = 0, 0

 That event executes on menu item. And something interesting is going on.
Sometimes cursor goes to the beginning of the edit control as it should do,
sometimes moves to some line of the edit box, again, somewhere at the
middle, sometimes just stays where it was.

 I am not sure, if I am doing something incorrectly or positioning of the
cursor does not work in ppygui?
 Would greatly appreciate any advice!

 Many thanks.

  Igor.


_______________________________________________
PythonCE mailing list
PythonCE@...
http://mail.python.org/mailman/listinfo/pythonce


_______________________________________________
PythonCE mailing list
PythonCE@...
http://mail.python.org/mailman/listinfo/pythonce

Parent Message unknown Re: Programaticly moving cursor in multi-line edit control

by Alexandre Delattre :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Igor,

I've tested your app, and what happens really is that the cursor itself
is moved at the beginning but the edit isn't scrolled to make it visible
(if you press an hardware key or try to insert a few characters, you'll
be then scrolled at the beginning). This seems to be the normal
behaviour of windows ce.

There is at least 2 way to solve that:

1) Use the line proposed by Jared to make the conversion between unix
and windows line endings, so the problem isn't even ask. I recommand you
to use this in your case.

2) Make some "invisible" changes at the beginning of the Edit, so that
the view is scrolled to show the change, you can use this snippet:

self.text_entry.selection = 0, 1
self.text_entry.selected_text = self.text_entry.selected_text
self.text_entry.selection = 0, 0

I think that, later, I'll surely implement scrolling methods so that
kind of stuff could be done much more cleanly.

Hope it helps,
Alexandre
_______________________________________________
PythonCE mailing list
PythonCE@...
http://mail.python.org/mailman/listinfo/pythonce

Re: Programaticly moving cursor in multi-line edit control

by Igor Kaplan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

Hi Jared,

 

  Thanks so much for your advice, it solved the problem of loading file all into 1 line, so loading is so much faster now!

  I am still trying to figure out the cursor movement issues.

 

  All the best.

 


From: Jared Forsyth [mailto:jabapyth@...]
Sent: Thursday, August 14, 2008 7:41 AM
To: Igor Kaplan
Cc: pythonce@...
Subject: Re: [PythonCE] Programaticly moving cursor in multi-line edit control

 

The way to preserve linebreaks is tp replace "\n" with "\r\n"
so: self.text_entry.text = fl.read().replace("\n","\r\n")

On Wed, Aug 13, 2008 at 8:05 PM, Igor Kaplan <igor_kaplan@...> wrote:

Hello,

 I would like to ask a little advice. Already several days I am trying to
figure out the following problem using ppygui: How to move the cursor in the
edit field to the top of that field.
 In my code I create the frame and then Edit control:
Self.text_entry = gui.Edit(multiline=True)
   sizer = gui.VBox(border=(2,2,2,2), spacing=2)
   sizer.add(self.text_entry)
.

 Later I open the text file and copy it's context into that edit control:
   fl = open(self.filename, 'r')
   l = fl.readlines()
   self.text_entry.text = ""
   for i in l:
     self.text_entry.append(i)

 Only reason why I add the text such long way because if I just do:
Self.text_entry.text = fl.read()
I looze all line breaks and all text goes into 1 line.

 After opening that file I do:
Self.text_entry.selection = 0, 0

 And that line does nothing. For some reasons the cursor always positions
at the same spot in the Edit box after opening the file, somewhere in the
middle and does not want to go to the beginning.

 I also have another event:
Def OnBegin(self, event):
 Self.text_entry.selection = 0, 0

 That event executes on menu item. And something interesting is going on.
Sometimes cursor goes to the beginning of the edit control as it should do,
sometimes moves to some line of the edit box, again, somewhere at the
middle, sometimes just stays where it was.

 I am not sure, if I am doing something incorrectly or positioning of the
cursor does not work in ppygui?
 Would greatly appreciate any advice!

 Many thanks.

  Igor.


_______________________________________________
PythonCE mailing list
PythonCE@...
http://mail.python.org/mailman/listinfo/pythonce

 


_______________________________________________
PythonCE mailing list
PythonCE@...
http://mail.python.org/mailman/listinfo/pythonce

Parent Message unknown Re: Programaticly moving cursor in multi-line edit control

by jabapyth :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Have you played around with different values for selection? like self.text_edit.selection = 1,1.....etc.
If they all fail, you might want to try going in and firing off the actual underlying win32 message

Good luck

On Thu, Aug 14, 2008 at 7:56 PM, Igor Kaplan <igor_kaplan@...> wrote:

Hi Jared,

 

  Thanks so much for your advice, it solved the problem of loading file all into 1 line, so loading is so much faster now!

  I am still trying to figure out the cursor movement issues.

 

  All the best.

 


From: Jared Forsyth [mailto:jabapyth@...]
Sent: Thursday, August 14, 2008 7:41 AM
To: Igor Kaplan
Cc: pythonce@...
Subject: Re: [PythonCE] Programaticly moving cursor in multi-line edit control

 

The way to preserve linebreaks is tp replace "\n" with "\r\n"
so: self.text_entry.text = fl.read().replace("\n","\r\n")

On Wed, Aug 13, 2008 at 8:05 PM, Igor Kaplan <igor_kaplan@...> wrote:

Hello,

 I would like to ask a little advice. Already several days I am trying to
figure out the following problem using ppygui: How to move the cursor in the
edit field to the top of that field.
 In my code I create the frame and then Edit control:
Self.text_entry = gui.Edit(multiline=True)
   sizer = gui.VBox(border=(2,2,2,2), spacing=2)
   sizer.add(self.text_entry)
.

 Later I open the text file and copy it's context into that edit control:
   fl = open(self.filename, 'r')
   l = fl.readlines()
   self.text_entry.text = ""
   for i in l:
     self.text_entry.append(i)

 Only reason why I add the text such long way because if I just do:
Self.text_entry.text = fl.read()
I looze all line breaks and all text goes into 1 line.

 After opening that file I do:
Self.text_entry.selection = 0, 0

 And that line does nothing. For some reasons the cursor always positions
at the same spot in the Edit box after opening the file, somewhere in the
middle and does not want to go to the beginning.

 I also have another event:
Def OnBegin(self, event):
 Self.text_entry.selection = 0, 0

 That event executes on menu item. And something interesting is going on.
Sometimes cursor goes to the beginning of the edit control as it should do,
sometimes moves to some line of the edit box, again, somewhere at the
middle, sometimes just stays where it was.

 I am not sure, if I am doing something incorrectly or positioning of the
cursor does not work in ppygui?
 Would greatly appreciate any advice!

 Many thanks.

  Igor.


_______________________________________________
PythonCE mailing list
PythonCE@...
http://mail.python.org/mailman/listinfo/pythonce

 



_______________________________________________
PythonCE mailing list
PythonCE@...
http://mail.python.org/mailman/listinfo/pythonce