VListBox.py :  » GUI » wxPython » wxPython-src-2.8.11.0 » wxPython » demo » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » GUI » wxPython 
wxPython » wxPython src 2.8.11.0 » wxPython » demo » VListBox.py

import  wx

#----------------------------------------------------------------------

# The wx.VListBox is much like a regular wx.ListBox except you draw the
# items yourself and the items can vary in height.
class MyVListBox(wx.VListBox):

    # This method must be overridden.  When called it should draw the
    # n'th item on the dc within the rect.  How it is drawn, and what
    # is drawn is entirely up to you.
    def OnDrawItem(self, dc, rect, n):
        if self.GetSelection() == n:
            c = wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
        else:
            c = self.GetForegroundColour()
        dc.SetFont(self.GetFont())
        dc.SetTextForeground(c)
        dc.DrawLabel(self._getItemText(n), rect,
                     wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)

    # This method must be overridden.  It should return the height
    # required to draw the n'th item.
    def OnMeasureItem(self, n):
        height = 0
        for line in self._getItemText(n).split('\n'):
            w, h = self.GetTextExtent(line)
            height += h
        return height + 5


    # These are also overridable:
    #
    # OnDrawSeparator(dc, rect, n)
    #   Draw a separator between items.  Note that rect may be reduced
    #   in size if desired so OnDrawItem gets a smaller rect.
    #
    # OnDrawBackground(dc, rect, n)
    #   Draw the background and maybe a border if desired.


    def _getItemText(self, item):
        if item % 2 == 0:
            return "This is item# %d" % item
        else:
            return "This is item# %d\n with an extra line" % item

#----------------------------------------------------------------------

# The wx.HtmlListBox derives from wx.VListBox, but draws each item
# itself as a wx.HtmlCell.
class MyHtmlListBox(wx.HtmlListBox):

    def OnGetItem(self, n):
        if n % 2 == 0:
            return "This is item# <b>%d</b>" % n
        else:
            return "This is item# <b>%d</b> <br>Any <font color='RED'>HTML</font> is okay." % n

#----------------------------------------------------------------------

class TestPanel(wx.Panel):
    def __init__(self, parent, log):
        self.log = log
        wx.Panel.__init__(self, parent, -1)
        spacer = 50

        vlb = MyVListBox(self, -1, size=(150, 250), style=wx.BORDER_SUNKEN)
        vlb.SetItemCount(50)
        vlb.SetSelection(0)
        vlb.SetFocus()
        vlbSizer = wx.BoxSizer(wx.VERTICAL)
        vlbSizer.Add((spacer, spacer))
        vlbSizer.Add(wx.StaticText(self, -1, "wx.VListBox"), 0, 5, wx.ALL)
        vlbSizer.Add(vlb)

        hlb = MyHtmlListBox(self, -1, size=(150, 250), style=wx.BORDER_SUNKEN)
        hlb.SetItemCount(50)
        hlb.SetSelection(0)
        hlbSizer = wx.BoxSizer(wx.VERTICAL)
        hlbSizer.Add((spacer, spacer))
        hlbSizer.Add(wx.StaticText(self, -1, "wx.HtmlListBox"), 0, 5, wx.ALL)
        hlbSizer.Add(hlb)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add((spacer, spacer))
        sizer.Add(vlbSizer)
        sizer.Add((spacer, spacer))
        sizer.Add((spacer, spacer))
        sizer.Add(hlbSizer)

        self.SetSizer(sizer)


#----------------------------------------------------------------------

def runTest(frame, nb, log):
    win = TestPanel(nb, log)
    return win

#----------------------------------------------------------------------



overview = """<html><body>
<h2><center>wx.VListBox and wx.HtmlListBox</center></h2>
<hr>

The "V" in wxVListBox stands for both "virtual" because it can have an
unlimited number of items since it doesn't store them itself, and
"variable" since items can vary in height.  It has much the same
interface as wxListBox and also emits the same events so you can use
the same EVT_LISTBOX function to connect a handler.
<p>

The wx.HtmlListBox derives from wx.VListBox, but draws each item itself
as a wx.HtmlCell.  This means that you just need to provide a snippet
of HTML for each item when requested.

</body></html>
"""



if __name__ == '__main__':
    import sys,os
    import run
    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.