Ejemplo n.º 1
0
def combos(parent):
    uct = UberCombo(parent,
                    value='test',
                    skinkey='combobox',
                    typeable=True,
                    size=(100, 20))
    uct.AppendItem(SimpleMenuItem('Sample Item 1'))
    uct.AppendItem(SimpleMenuItem('Sample Item 2'))
    uct.AppendItem(SimpleMenuItem(id=-1))
    uct.AppendItem(SimpleMenuItem('Sample Item 3'))

    return [uct]
Ejemplo n.º 2
0
class F(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, size=(500, 55))

        events = [
            (wx.EVT_COMBOBOX, self.OnSelect),
            #            (wx.EVT_TEXT,self.OnType),
            #            (wx.EVT_TEXT_ENTER,self.OnPressEnter)
        ]
        do(self.Bind(event, method) for (event, method) in events)

        self.skin = 'combobox'
        self.uc = UberCombo(
            self,
            skinkey=self.skin,
            typeable=False,
            size=(100, 20),
            maxmenuheight=3,
            minmenuwidth=400,
        )  #selectioncallback=self.defaultmethodtest)
        items = [
            SimpleMenuItem([skincore.get("statusicons.typing"), 'Test1']),
            SimpleMenuItem([
                skincore.get("statusicons.typing"),
                skincore.get("statusicons.typing"), 'Test2'
            ]),
            SimpleMenuItem([
                skincore.get("statusicons.typing"),
                skincore.get("statusicons.typing"),
                skincore.get("statusicons.typing"),
                'Test3 followed by a long line of thext so I can see if truncating worked well or not, maybe?'
            ]),
            SimpleMenuItem(id=-1),
            SimpleMenuItem([
                skincore.get("statusicons.typing"),
                skincore.get("statusicons.typing"),
                skincore.get("statusicons.typing"), 'Test4 cause I can'
            ])
        ]
        self.uc.SetItems(items)

        self.ucf = UberCombo(self,
                             skinkey=self.skin,
                             typeable=False,
                             size=(100, 20),
                             maxmenuheight=10,
                             minmenuwidth=400)  #font method
        self.ucf.SetItems(self.DoFonts())

        self.uct = UberCombo(self,
                             value='test',
                             skinkey=self.skin,
                             typeable=True,
                             valuecallback=self.ValueTest,
                             size=(100, 20))
        self.uct.AppendItem(SimpleMenuItem('Sample Item 1'))
        self.uct.AppendItem(SimpleMenuItem('Sample Item 2'))
        self.uct.AppendItem(SimpleMenuItem(id=-1))
        self.uct.AppendItem(SimpleMenuItem('Sample Item 3'))

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.SetSizer(sizer)
        sizer.Add(self.uc, 1, wx.EXPAND | wx.ALL, 3)
        sizer.Add(self.ucf, 1, wx.EXPAND | wx.ALL, 3)
        sizer.Add(self.uct, 1, wx.EXPAND | wx.ALL, 3)

        self.Fit()

    def DoFonts(self):
        fontlist = GetFonts()
        fontitems = []
        for font in fontlist:
            wxfont = wx.Font(self.Font.GetPointSize(), wx.FONTFAMILY_DEFAULT,
                             wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False,
                             font)
            if font[0] != '@':
                fontitems.append(SimpleMenuItem([font], font=wxfont))

        return fontitems

    def defaultmethodtest(self, item):
        print item.id
        for i in item.content:
            if isinstance(i, basestring):
                print i
                break

    def DifferentMethodTest(self, item):
        print 'workage!!'

    def CycleTest(self, button):
        print 'yey!'
        button.Parent.display.TypeField()
        button.Parent.display.txtfld.SetSelection(-1, -1)

    def OnSelect(self, event):
        print 'OnSelect:', event.GetInt()

    def OnType(self, event):
        print 'OnType'

    def OnPressEnter(self, event):
        print 'OnPressEnter'

    def ValueTest(self, value):
        print 'valuecallback', value
Ejemplo n.º 3
0
class PrefPanel(SimplePanel):
    def __init__(self,
                 parent,
                 content=None,
                 title='',
                 buttonlabel='',
                 buttoncb=None,
                 titlemaker=None,
                 prefix=''):
        SimplePanel.__init__(self, parent, wx.FULL_REPAINT_ON_RESIZE)

        sizer = self.Sizer = BoxSizer(VERTICAL)
        self.headersizer = BoxSizer(HORIZONTAL)
        self.bodysizer = BoxSizer(VERTICAL)
        sizer.Add(self.headersizer, 0, EXPAND | TOP, space_over_header)
        sizer.Add(self.bodysizer, 1, EXPAND | TOP, space_under_header)

        self.title = None
        self.combo = None
        self.button = None
        self.content = None
        self.contents = {}
        self.titlemaker = titlemaker
        if wxMac:
            self.menuitems = {}

        if title and isinstance(title, basestring):
            self.title = wx.StaticText(self,
                                       -1,
                                       ' ' + title + ' ',
                                       style=wx.ALIGN_CENTER_VERTICAL)

            #need grey backgound behind label on mac to hide the line
            if wxMac:
                self.title.BackgroundColour = wx.Color(232, 232, 232)
            self.title.Font = self.HeaderFont
            self.headersizer.Add(self.title, 0, *header_sizer_flags)

        if callable(content):
            content = self.content = content(self, prefix)
            self.bodysizer.Add(self.content, 1, pref_sizer_style, 7)
        elif isinstance(content, wx.WindowClass):
            content.Reparent(self)
            self.content = content
            self.bodysizer.Add(self.content, 1, pref_sizer_style, 7)
        elif isinstance(content, list):
            self.SetContents(content)

        if buttoncb:
            self.SetButton(buttonlabel, buttoncb)

        Bind = self.Bind
        Bind(wx.EVT_PAINT, self.OnPaint)

        #darker border if mac so it is visible for now
        if not wxMac:
            self.pen = wx.Pen(wx.Colour(213, 213, 213))
        else:
            self.pen = wx.Pen(wx.Colour(155, 155, 155))

    def SetTitle(self, title):
        self.title.SetLabel(title)

    @property
    def HeaderFont(self):
        try:
            return self._headerfont
        except AttributeError:
            if not wxMac:
                PrefPanel._headerfont = makeFont('arial 8 bold')
            else:
                PrefPanel._headerfont = makeFont('9 bold')
            return self._headerfont

    _fg_brush = \
    _bg_brush = \
    _fg_pen = \
    _bg_pen = lambda self: None

    def get_bg_brush(self):
        return self._bg_brush() or wx.WHITE_BRUSH

    def get_fg_brush(self):
        return self._fg_brush() or wx.TRANSPARENT_BRUSH

    def get_bg_pen(self):
        return self._bg_pen() or wx.TRANSPARENT_PEN

    def get_fg_pen(self):
        return self._fg_pen() or self.pen

    bg_brush = property(get_bg_brush)
    fg_brush = property(get_fg_brush)
    bg_pen = property(get_bg_pen)
    fg_pen = property(get_fg_pen)

    def OnPaint(self, event):
        size = self.Size
        dc = AutoDC(self)

        if not wxMac:
            # Non mac: white background, rounded rectangle around controls
            rect = wx.RectS(size)
            dc.Brush = self.bg_brush  #background
            dc.Pen = self.bg_pen  #background border
            dc.DrawRectangleRect(rect)
            ypos = self.headersizer.Size.height // 2 + space_over_header
            gc = wx.GraphicsContext.Create(dc)
            gc.SetBrush(self.fg_brush)  #foreground
            gc.SetPen(self.fg_pen)  #foreground
            gc.DrawRoundedRectangle(0, ypos, size.width - 1,
                                    size.height - ypos - 1, 5)
        else:
            # Mac: normal grey background, horizontal line above controls
            ypos = self.headersizer.Size.height // 2 + space_over_header + 2
            dc.Pen = self.fg_pen
            button_width = 0 if self.button is None else (
                self.button.Size.width)
            dc.DrawLine(10, ypos,
                        self.headersizer.Size.width - 10 - button_width, ypos)

        content = self.content
        if isinstance(content, AnyList):  # TODO: don't special case
            crect = wx.Rect(*content.Rect)
            crect = crect.Inflate(1, 1)
            dc.SetBrush(wx.TRANSPARENT_BRUSH)
            dc.SetPen(self.pen)
            dc.DrawRectangleRect(crect)

    def ChangeShownContent(self, *a):
        if self.content:
            self.content.Show(False)

        if wxMac:
            menu_item = self.menuitems[self.combo.GetStringSelection()]
        else:
            menu_item = self.combo.Value

        self.content = self.contents[menu_item]
        self.content.Show(True)
        self.Layout()

    def SetButton(self, label, callback):
        if self.button:
            self.headersizer.Detach(self.button)
            self.button.Destroy()

        # native button on mac instead of the vista clone button
        if not wxMac:
            self.button = UberButton(self,
                                     -1,
                                     label,
                                     skin='AppDefaults.PrefButton')
        else:
            self.button = wx.Button(self, -1, label)
            self.button.SetWindowVariant(wx.WINDOW_VARIANT_SMALL)

        self.button.Bind(wx.EVT_BUTTON, lambda e: callback(self.button))

        self.headersizer.AddStretchSpacer(1)
        self.headersizer.Add(self.button, 0, wx.ALIGN_CENTER_VERTICAL | RIGHT,
                             7)

    @property
    def MenuItems(self):
        combo = self.combo
        if wxMac:
            return [combo.GetClientData(i) for i in xrange(combo.Count)]
        else:
            return combo.menu.spine.items

    def SetContents(self, content, destroyold=False):
        if destroyold:
            if not self.contents:
                self.content.Destroy()
            for content in self.contents.values():
                content.Destroy()

        # the currently showing pane in a multiple pane pref panel
        if self.content:
            self.content.Show(False)
            self.content = None

        self.bodysizer.Clear()
        contents = self.contents = {}

        titlemaker = self.titlemaker

        if self.combo is None:
            if not wxMac:
                self.combo = UberCombo(self,
                                       value='',
                                       skinkey='AppDefaults.PrefCombo',
                                       valuecallback=self.ChangeShownContent)
            else:
                # use a native ComboBox on mac
                self.combo = wx.ComboBox(self,
                                         style=wx.CB_DROPDOWN | wx.CB_READONLY)
                self.combo.Bind(wx.EVT_COMBOBOX, self.ChangeShownContent)
            newcombo = True
        else:
            self.combo.RemoveAllItems()
            newcombo = False

        for object in content:
            if isinstance(object, tuple):
                window, label = object
            elif isinstance(object, wx.WindowClass):
                window = object
                label = titlemaker(window) if titlemaker else object.Label

            window.Show(False)
            window.Reparent(self)
            assert window.Parent is self
            self.bodysizer.Add(window, 1, pref_sizer_style, 7)

            menuitem = SimpleMenuItem(label)
            contents[menuitem] = window

            if wxMac:
                itemname = menuitem.GetContentAsString()
                self.combo.Append(itemname)
                self.menuitems[itemname] = menuitem
            else:
                self.combo.AppendItem(menuitem)

        if self.combo:
            if wxMac:
                self.combo.SetSelection(0)
                self.ChangeShownContent()
            else:
                self.combo.Value = self.combo[0]

        if self.combo is not None and newcombo:
            self.headersizer.Add(self.combo, 1, *combo_sizer_flags)