[wxPython] Changing font size in a wxStaticText control

I am trying to create a resizable static text control that will respond to
changes in window size by changing its font size. Initially I tried a
wxStaticText control, giving it an OnSize method responding to an
EVT_SIZE event. The OnSize method calculated a new font size, and
attempts to refresh the control. This effort fails, the wxStaticText
control refuses to recognize the change font size. All my attempts
to get it to do so (e.g. pretending to change the label) gave
unexepected results.

I then made a control that actually drew the text. This control worked
the way I expected, but unfortunately, I cannot get it to appear without
some kind of border drawn around it. It would seem to me, that if
a wxStaticText control is a wxControl is a wxWindow, and a wxStaticText
can appear without a border, then I too should be able to create a wxControl
or wxWindow, and have it appear without borders. Thats what object
oriented programming is about, isn't it?

Anyhow, If someone knows how to get a borderless window that I can
draw in, or can show me how to get a wxStaticText control to respond
to font size changes, I would appreciate your expertise.

- Parzival

···

-------------------------------
# This does not work:
class Widget:
    def PixPerPoint(self):
        return float(self.GetCharHeight())/self.GetFont().GetPointSize()
    def SetFontSize(self,pix):
        f = self.GetFont()
        pt = min(24, max(6,int(pix/self.PixPerPoint()+0.5)))
        #wxLogMessage("SetFontSize: pix:%d points:%d" % (pix,pt))
        f.SetPointSize(pt)

class StaticText2(wxStaticText,Widget):
    def __init__(self, parent, ID, text,
                 pos = wxDefaultPosition, size = (100,12),
                 style = 0, name=None):
        if not name: name = str(ID)
        wxStaticText.__init__(self, parent, ID, text, pos, size=size,
style=style, name=name)
        EVT_SIZE(self, self.OnSize)

    def OnSize(self,event):
        self.SetFontSize(event.GetSize().height)
        self.Refresh()

--------------------------------
# This does work, but I can't get rid of the border
AlignTop = 0
AlignMidRise = 1
AlignMidText = 2
AlignBot = 3
AlignLeft = 0
AlignCenter = 4
AlignRight = 8
class StaticText(wxControl,Widget):

    def __init__(self, parent, ID, text, align=0,
                 pos = wxDefaultPosition, size = (100,12),
                 style = 0, name=None):
        self.text = text
        self.align = align
        if style == 0:
            style = wxSTATIC_BORDER
        if not name: name = str(ID)
        wxControl.__init__(self, parent, ID, pos, size, style,
            validator = wxDefaultValidator, name=name)
        self.SetFontSize(12)
        EVT_SIZE(self, self.OnSize)
        EVT_PAINT(self, self.OnPaint)

    def OnPaint(self, event):
        self.Draw(wxPaintDC(self))

    def OnSize(self,event):
        self.SetFontSize(event.GetSize().height-2)
        pass # wxLogMessage("SetSize: " + str(event.GetSize()))

    def Draw(self,dc):
        w, h = self.GetClientSizeTuple()
        dc.BeginDrawing()
        dc.SetFont(self.GetFont())
        tw, th, td, tx = self.GetFullTextExtent(self.text)
        alignV = self.align & 3
        if alignV == AlignTop:
            y = 0
        elif alignV == AlignMidRise:
            y = (h - th + td)/2
        elif alignV == AlignMidText:
            y = (h - th)/2
        else: # alignV == AlignBot:
            y = h - th
        alignH = self.align & 12
        if alignH == AlignLeft:
            x = 0
        elif alignH == AlignCenter:
            x = (w - tw)/2
        else: # alignH == AlignRight:
            x = w - tw
        dc.DrawText(self.text, x, y)
        dc.EndDrawing()

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

I then made a control that actually drew the text. This control worked
the way I expected, but unfortunately, I cannot get it to appear without
some kind of border drawn around it.

Use style wxNO_BORDER.

···

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

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

Thanks Robin. I wish I could find these thing out more easily for myself:
the wxNO_BORDER style constant isn't listed under wxControl, or wxWindow
style options. A search for "wxNO_BORDER" found this constant under wxFrame
and wxToolbar. If I had encountered it there, I probably would have tried
it, but
logically, I would have expected that a style constant listed under a
specialized
window class would apply only to that class and its descendants. Is there a
principle here that I am missing?

Is there another place that all the style constants applicable to a wxWindow
are listed?

···

----- Original Message -----
From: "Robin Dunn" <robin@alldunn.com>
To: <wxpython-users@lists.sourceforge.net>
Sent: Monday, January 22, 2001 11:41 AM
Subject: Re: [wxPython] Changing font size in a wxStaticText control

Use style wxNO_BORDER.

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

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users