[wxPython] Working wxCaret Example

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[wxPython] Working wxCaret Example

Andrew McCumiskey
Hello Everyone...

Well, after grinding my teeth a lot I was finally able to get the caret to
show in wxPython.  For anyone who is interested, here is a wxPython version of
the caret.cpp sample that comes with the wxWindows distribution.  (I hope I'm
not stepping on any licensing toes.)

I've added Delete key and Backspace key handling to the original example.
Also, I do not redefine the buffer array each time the frame is resized so the
contents are maintained.  HOWEVER, the maximum row and column size is 25 by 80
so don't size the window bigger than that without redefining the array size.

Please, also keep in mind this is the second program I've ever written in
Python.  I am still getting use to the language and having a hard time with
all of the variable and class attribute scoping rules.  If you find something
in here that could be done better... please let me know.

Here ya go!

from wxPython.wx import *
from array import *

class MyApp(wxApp):

    def OnInit(self):
        frame = MyFrame(None, -1, "Minimal wxWindows App",
wxPoint(50,50),wxSize(450,340))
        frame.Show(true)
        self.SetTopWindow(frame)
        return true

class MyCanvas(wxScrolledWindow):
    def __init__(self, parent):
        wxScrolledWindow.__init__(self, parent, -1, wxDefaultPosition,
wxDefaultSize, wxSUNKEN_BORDER)
        self.sb = parent
        self.SetBackgroundColour(wxNamedColour('white'))
        self.m_text = array('c',' '*25*80)
        self.m_font = wxNORMAL_FONT
        dc = wxClientDC(self)
        dc.SetFont(self.m_font)
        self.m_heightChar = dc.GetCharHeight()
        self.m_widthChar = dc.GetCharWidth()
        self.caret = wxCaret(self,
wxSize(self.m_widthChar,self.m_heightChar))
        self.SetCaret(self.caret)
        self.m_xCaret = self.m_yCaret = self.m_xChars = self.m_yChars = 0
        self.m_xMargin = self.m_yMargin = 5
        self.caret.MoveXY(self.m_xMargin, self.m_yMargin)
        self.caret.Show()

    def Home(self):
        self.m_xCaret = 0

    def End(self):
        self.m_xCaret = self.m_xChars - 1

    def FirstLine(self):
        self.m_yCaret = 0

    def LastLine(self):
        self.m_yCaret = self.m_yChars - 1

    def PrevChar(self):
        self.m_xCaret = self.m_xCaret - 1
        if self.m_xCaret < 0:
            self.End()
            self.PrevLine()

    def NextChar(self):
        self.m_xCaret = self.m_xCaret + 1
        if self.m_xCaret == self.m_xChars:
            self.Home()
            self.NextLine()

    def PrevLine(self):
        self.m_yCaret = self.m_yCaret - 1
        if self.m_yCaret < 0:
            self.LastLine()

    def NextLine(self):
        self.m_yCaret = self.m_yCaret + 1
        if self.m_yCaret == self.m_yChars:
            self.FirstLine()

    def CharAt(self, x, y):
        return (x + self.m_xChars * y)

    def OnPaint(self, event):
        dc = wxPaintDC(self)
        self.PrepareDC(dc)
        dc.SetFont(self.m_font)
        for y in range(0, self.m_yChars):
            line = ''
            for x in (range(0, self.m_xChars)):
                ch = self.m_text[self.CharAt(x,y)]
                if ch == ' ':
                    char = ' '
                line = line + ch
            dc.DrawText(line, self.m_xMargin, self.m_yMargin + y *
self.m_heightChar)

    def OnChar(self, event):

        if event.KeyCode() == WXK_LEFT:
            self.PrevChar()
        elif event.KeyCode() == WXK_RIGHT:
            self.NextChar()
        elif event.KeyCode() == WXK_UP:
            self.PrevLine()
        elif event.KeyCode() == WXK_DOWN:
            self.NextLine()
        elif event.KeyCode() == WXK_HOME:
            self.Home()
        elif event.KeyCode() == WXK_END:
            self.End()
        elif event.KeyCode() == WXK_RETURN:
            self.Home()
            self.NextLine()
        elif event.KeyCode() == WXK_DELETE:
            buflen = self.m_text.buffer_info()[1]
            for n in range(self.CharAt(self.m_xCaret, self.m_yCaret),buflen -
1):
                self.m_text[n] = self.m_text[n + 1]
        elif event.KeyCode() == WXK_BACK:
            buflen = self.m_text.buffer_info()[1]
            for n in range(self.CharAt(self.m_xCaret, self.m_yCaret),buflen -
1):
                self.m_text[n - 1] = self.m_text[n]
            self.PrevChar()
        else:
            self.m_text.insert(self.CharAt(self.m_xCaret,
self.m_yCaret),chr(event.KeyCode()))
            self.NextChar()

        self.caret.MoveXY(self.m_xMargin + self.m_xCaret * self.m_widthChar,
                          self.m_yMargin + self.m_yCaret * self.m_heightChar)

        self.Refresh()

    def OnSize(self, event):
        self.m_xChars = (event.GetSize().x - 2*self.m_xMargin) /
self.m_widthChar
        self.m_yChars = (event.GetSize().y - 2*self.m_yMargin) /
self.m_heightChar
        if self.m_xChars == 0:
            self.m_xChars = 1
        if self.m_yChars == 0:
            self.m_yChars = 1
        msg = 'Panel size is ' + str(self.m_xChars) + ', ' +
str(self.m_yChars)
        self.sb.SetStatusText(msg,1)
        event.Skip()

class MyFrame(wxFrame):
    def __init__(self, parent, id, title, pos, size):
        wxFrame.__init__(self, parent, id, title, pos, size)
        MyCanvas(self)
        self.CreateStatusBar(2)
        self.SetStatusText("Welcome to wxWindows!")

app = MyApp(0)
app.MainLoop()


____________________________________________________________________
Get your own FREE, personal Netscape WebMail account today at http://webmail.netscape.com.

_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users