[wxPython] Bitmap Text Buttons

Now that I’ve got bitmaps in my menus, is it possible to build bitmaped buttons ? I’m getting spoiled at those Microsoft interfaces.

The class wxBitmapButton would appear to be the answer, but is just a bitmap with no text label.

Anything else that would work? Is it possible to build something from these base classes ?

Lorne

The class wxBitmapButton would appear to be the answer, but
is just a bitmap with no text label.

Anything else that would work? Is it possible to build
something from these base classes ?

You could probably build a class that takes a bitmap and some text and makes
a new bitmap that combined the original bitmap and the text, and then used
the combined bitmap for the wxBitmapButton. Or you could proably do it with
a new generic button derived from one of the classes in
wxPython/lib/buttons.py

···

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

Following is a first crack at a "bitmap writer" that lets you write text
into your bitmaps fairly easily for use as bitmap buttons (or for whatever
reason). Demo code at the bottom shows usage (note that you'll need to
files named test.bmp and test2.bmp in the same directory to run that code).

Enjoy,
Mike

8<_________________ bitmapwrite.py ___________
from wxPython.wx import *
import string

MINIMUMFONTSIZE = 4

def writeCaption( text, bitmap, font=None, margins = (2,2), color=None ):
    """Write the given caption (text) into the bitmap
    using the font (or default if not given) with the
    margins given. Will try to make sure the text fits
    into the bitmap.
    """
    memory = wxMemoryDC( )
    font = font or memory.GetFont()
    textLines = string.split( text, '\n' )
    fit = 0
    while not fit:
        totalWidth=0
        totalHeight = 0
        setLines =
        for line in textLines:
            if line and line[-1] == '\r':
                line = line[:-1]
            width, height = extents = memory.GetTextExtent( line )
            totalWidth = max( totalWidth, width )
            totalHeight = totalHeight + height
            setLines.append( (line, extents))
        if (
            totalWidth > (bitmap.GetWidth()- 2*margins[0]) or
            totalHeight > (bitmap.GetHeight()- 2*margins[0])
        ):
            size = font.GetPointSize()-1
            if size < MINIMUMFONTSIZE:
                fit = 1 # will overdraw!!!
            else:
                font.SetPointSize( size )
                memory.SetFont( font )
        else:
            fit = 1
    if not setLines:
        return bitmap
    centreX, centreY = (bitmap.GetWidth()/2), (bitmap.GetHeight()/2)
    x, y = centreX-(totalWidth/2), centreY-(totalHeight/2)
    memory.SelectObject( bitmap )
    _setupContext( memory, font, color)
    for line, (deltaX, deltaY) in setLines:
        x = centreX - (deltaX/2)
        memory.DrawText( line, x, y,)
## x = x + deltaX
        y = y + deltaY
    memory.SelectObject( wxNullBitmap)
    return bitmap

def _setupContext( memory, font=None, color=None ):
    if font:
        memory.SetFont( font )
    else:
        memory.SetFont( wxNullFont )
    if color:
        memory.SetTextForeground( color )
    else:
        ## :frowning: following doesn't work on win32
        memory.SetLogicalFunction( wxINVERT )

def write( text, bitmap, pos=(0,0), font=None, color=None):
    """Simple write into a bitmap doesn't do any checking."""
    memory = wxMemoryDC( )
    _setupContext( memory, font, color )
    memory.SelectObject( bitmap )
    try:
        memory.DrawText( text, pos[0],pos[1],)
    finally:
        memory.SelectObject( wxNullBitmap)
    return bitmap

if __name__ == "__main__":
    class TestPanel( wxPanel ):
        def __init__( self, parent ):
            wxPanel.__init__( self, parent, -1 )
            sizer = wxBoxSizer( wxVERTICAL )
            for file, text, color in (
                ("test.bmp", "test", None),
                ("test.bmp", "test longer text here", None),
                ("test2.bmp", "test longer\nmultiline text", wxGREEN),
                ("test2.bmp", "a\nb\nc\nd\ne", wxRED),
            ):
                bitmap = wxBitmap( file, wxBITMAP_TYPE_BMP )
                bitmap = writeCaption( text, bitmap, color= color)
                button = wxBitmapButton(
                    self, -1, bitmap
                )
                sizer.Add(button )
            bitmap = wxBitmap( "test2.bmp", wxBITMAP_TYPE_BMP )
            bitmap = write( "hello", bitmap, pos=(0,0) )
            button = wxBitmapButton(
                self, -1, bitmap
            )
            sizer.Add(button )
            self.SetAutoLayout(true)
            self.SetSizer( sizer )
            self.Layout()
    class TestApp( wxPySimpleApp ):
        def OnInit( self ):
            self.frame = wxFrame( NULL, -1, "testbitmapwrite" )
            self.frame.panel = TestPanel( self.frame )
            self.frame.Show( true )

            return 1
    app = TestApp()
    app.MainLoop()

···

-----Original Message-----
From: wxpython-users-admin@lists.wxwindows.org
[mailto:wxpython-users-admin@lists.wxwindows.org]On Behalf Of Robin Dunn
Sent: June 20, 2001 11:43
To: wxpython-users@lists.wxwindows.org
Subject: Re: [wxPython] Bitmap Text Buttons

The class wxBitmapButton would appear to be the answer, but
is just a bitmap with no text label.

Anything else that would work? Is it possible to build
something from these base classes ?

You could probably build a class that takes a bitmap and some text and makes
a new bitmap that combined the original bitmap and the text, and then used
the combined bitmap for the wxBitmapButton. Or you could proably do it with
a new generic button derived from one of the classes in
wxPython/lib/buttons.py

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

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwindows.org
http://lists.wxwindows.org/mailman/listinfo/wxpython-users

Following is a first crack at a "bitmap writer" that lets you write text
into your bitmaps fairly easily for use as bitmap buttons (or for whatever
reason). Demo code at the bottom shows usage (note that you'll need to
files named test.bmp and test2.bmp in the same directory to run that

code).

Looks good Mike! I have a feature request for you though: It would be nice
if it could optionally create a bitmap that's larger than the original, with
the text placed either below or to the left of the source bitmap and with a
mask that will leave the area behind the text transparent.

···

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

Not really sure why you want this. I'll take a look at manipulating masks
next time I feel the wxPython urge :slight_smile: .

Enjoy,
Mike

···

-----Original Message-----
From: wxpython-users-admin@lists.wxwindows.org
[mailto:wxpython-users-admin@lists.wxwindows.org]On Behalf Of Robin Dunn
Sent: June 26, 2001 11:52
To: wxpython-users@lists.wxwindows.org
Subject: Re: [wxPython] Bitmap Text Buttons

Following is a first crack at a "bitmap writer" that lets you write text
into your bitmaps fairly easily for use as bitmap buttons (or for whatever
reason). Demo code at the bottom shows usage (note that you'll need to
files named test.bmp and test2.bmp in the same directory to run that

code).

Looks good Mike! I have a feature request for you though: It would be nice
if it could optionally create a bitmap that's larger than the original, with
the text placed either below or to the left of the source bitmap and with a
mask that will leave the area behind the text transparent.

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

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwindows.org
http://lists.wxwindows.org/mailman/listinfo/wxpython-users