#!/usr/bin/env python
# -*- coding: utf-8 -*-

#-Imports.----------------------------------------------------------------------
#--Python Imports.
import sys

#--wxPython Imports.
import wx

class MyListBoxGroup(wx.ListBox):
    def __init__(self, parent, id=wx.ID_ANY,
                 pos=wx.DefaultPosition, size=wx.DefaultSize,
                 choices=[], validator=wx.DefaultValidator,
                 style=wx.BORDER_SUNKEN, name='listbox'):
        wx.ListBox.__init__(self, parent, id, pos, size, choices, style, validator, name)

        self.parent = parent

        self.SetItems(choices)
        self.SetSelection(0)

        self.Bind(wx.EVT_LISTBOX, self.OnListBox)

    def OnListBox(self, event=None):
        s = self.GetString(self.GetSelection())
        if 'Group' in s:
            self.parent.btn.Enable(False)
        else:
            self.parent.btn.Enable(True)
        print('EVT_LISTBOX: %s' % s)

class ListBoxGroupPanel(wx.Panel):
    def __init__(self, parent, id=wx.ID_ANY,
                 pos=wx.DefaultPosition, size=wx.DefaultSize,
                 style=wx.BORDER_SUNKEN, name='panel'):
        wx.Panel.__init__(self, parent, id, pos, size, style, name)

        self.lb = MyListBoxGroup(self, choices=['--- Group 1 ---', 'a', 'b', 'c', '--- Group 2 ---', 'd', 'e', 'f'])
        self.btn = wx.Button(self, -1, 'MyButton')
        self.btn.Bind(wx.EVT_BUTTON, self.OnButton)

        vbSizer = wx.BoxSizer(wx.VERTICAL)
        vbSizer.Add(self.lb, 1, wx.EXPAND | wx.ALL, 5)
        vbSizer.Add(self.btn, 0, wx.EXPAND | wx.ALL, 5)
        self.SetSizer(vbSizer)

        self.lb.OnListBox() # Update Enabled Status immediately.

    def OnButton(self, event):
        s = self.lb.GetString(self.lb.GetSelection())
        if 'Group' in s:
            wx.Bell() # Ding!!
            return
        print('Clicked: %s OnButton' % s)

class ListBoxGroupFrame(wx.Frame):
    def __init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString,
                 pos=wx.DefaultPosition, size=wx.DefaultSize,
                 style=wx.DEFAULT_FRAME_STYLE, name='frame'):
        wx.Frame.__init__(self, parent, id, title, pos, size, style, name)
        global gMainWin
        gMainWin = self
        panel = ListBoxGroupPanel(self)
        self.Bind(wx.EVT_CLOSE, self.OnDestroy)

    def OnDestroy(self, event):
        self.Destroy()

class ListBoxGroupApp(wx.App):
    def OnInit(self):
        self.SetClassName('ListBoxGroupApp')
        self.SetAppName('ListBoxGroupApp')
        gMainWin = ListBoxGroupFrame(None)
        gMainWin.SetTitle('ListBoxGroupFrame')
        self.SetTopWindow(gMainWin)
        gMainWin.Show()
        return True

if __name__ == '__main__':
    gApp = ListBoxGroupApp(redirect=False,
            filename=None,
            useBestVisual=False,
            clearSigInt=True)

    gApp.MainLoop()
