1
# Copyright (C) 2007-2008 www.stani.be
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation, either version 3 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program. If not, see http://www.gnu.org/licenses/
18
from compatible import SearchCtrl
20
def extract_tags(items):
29
class ContentMixin(object):
31
return self.GetParent()
34
return self.GetBrowser().filter
37
return self.GetBrowser().tag
40
self.GetBrowser().CheckEmpty()
43
return self.GetBrowser().empty
46
#check tag ctrl in parent
47
tag_ctrl = self.GetTag()
48
if tag_ctrl.GetStringSelection() != tag:
49
tag_ctrl.SetStringSelection(tag)
52
class Browser(paint.Mixin,wx.Panel):
53
"""ContentCtrl needs to be a class which implements these methods:
54
- content.SetTag <- browser.OnTag
55
- content.SetFilter <- browser.OnFilter"""
57
ContentCtrl = wx.Panel
59
paint_message = "nothing found"
60
paint_logo = None#"ART_TIP"
62
def __init__(self,parent,tags,content_ctrl_keyw,*args,**keyw):
63
"""At least four arguments should be passed:
64
Browser(['foo','bar'],TestContentCtrl,{},parent)"""
65
super(Browser,self).__init__(parent,*args,**keyw)
66
self._create_controls(tags,content_ctrl_keyw)
74
def _create_controls(self, tags,content_ctrl_keyw):
78
self.filter = SearchCtrl(self, -1, "")
80
self.tag = wx.Choice(self,-1,choices=tags)
82
self.empty = wx.Panel(self)
84
self.content = self.ContentCtrl(self,**content_ctrl_keyw)
88
main_sizer = wx.BoxSizer(wx.VERTICAL)
89
#horizontal browse sizer = search & tag control
90
browse_sizer = wx.BoxSizer(wx.HORIZONTAL)
91
browse_sizer.Add(self.filter, 1,
92
wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
93
browse_sizer.Add(self.tag, 0,
94
wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 4)
95
main_sizer.Add(browse_sizer, 0, wx.EXPAND, 0)
97
main_sizer.Add(self.content, 1, wx.ALL|wx.EXPAND, 4)
99
main_sizer.Add(self.empty, 1, wx.ALL|wx.EXPAND, 4)
101
self.SetSizer(main_sizer)
105
self.Bind(wx.EVT_TEXT, self.OnFilter, self.filter)
106
self.Bind(wx.EVT_CHOICE, self.OnTag, self.tag)
107
self.EnableBackgroundPainting(self.empty)
109
def OnTag(self,event):
110
self.content.SetTag(event.GetString())
112
def OnFilter(self,event):
113
self.content.SetFilter(self.filter.GetValue())
115
def GetItemTags(self,item):
116
"""Can be overwritten."""
119
def GetTags(self,items):
122
def GetContent(self):
125
def CheckEmpty(self):
126
is_empty = self.IsEmpty()
127
if self.is_empty != is_empty:
129
self.empty.Show(is_empty)
130
self.content.Show(not is_empty)
131
self.is_empty = is_empty
134
'Use fewer characters.'
138
return self.content.IsEmpty()
140
def OnSize(self,event):
145
def EnableResize(self,state=True,object=None):
147
object = wx.GetTopLevelParent(self)
149
object.Bind(wx.EVT_SIZE,self.OnSize)
151
object.Unbind(wx.EVT_SIZE)
153
class TestContentCtrl(ContentMixin,wx.TextCtrl):
154
def __init__(self,*args,**keyw):
155
super(TestContentCtrl,self).__init__(*args,**keyw)
158
def SetTag(self,tag):
159
self.SetValue('You selected tag: %s.'%tag)
161
def SetFilter(self,filter):
163
if not self.CheckEmpty():
164
self.SetValue('You selected filter: %s.'%filter)
167
return not self.filter
171
class TestBrowser(Browser):
172
ContentCtrl = TestContentCtrl
177
class TestFrame(wx.Frame):
178
def __init__(self, parent):
179
wx.Frame.__init__(self, parent, -1, "Test Tag Browser",
181
self.browser = TestBrowser(self,['foo','bar'],{})
182
self.browser.EnableResize()
187
sys.path.extend(['..'])
189
app = wx.PySimpleApp()
190
frame = TestFrame(None)
194
if __name__ == '__main__':