1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 from box import EventBox
24 from constant import DEFAULT_FONT_SIZE, BUTTON_PRESS, BUTTON_NORMAL, BUTTON_HOVER
25 from draw import draw_vlinear, draw_pixbuf, draw_text, expose_linear_background
26 from theme import ui_theme
27 from utils import get_content_size, propagate_expose
28 import gobject
29 import gtk
30
32 '''
33 Categorybar.
34 '''
35
42 '''
43 Initialize Categorybar class.
44
45 @param items: A list of category item, format: (icon_dpixbuf, content, click_callback)
46 '''
47
48 super(Categorybar, self).__init__()
49 self.category_index = 0
50 self.connect(
51 "expose-event",
52 lambda w, e:
53 expose_linear_background(w, e, ui_theme.get_shadow_color("categorybar_background").get_color_info()))
54
55
56 self.category_item_box = gtk.VBox()
57 self.add(self.category_item_box)
58
59
60 if items:
61 icon_width = self.get_icon_width(items)
62 for (index, item) in enumerate(items):
63 category_item = CategoryItem(item, index, font_size, icon_width, padding_left, padding_middle, padding_right,
64 self.set_index, self.get_index)
65 self.category_item_box.pack_start(category_item)
66
67
68 self.show_all()
69
71 '''
72 Set selected item index.
73
74 @param index: Index of selected item.
75 '''
76 self.category_item_box.queue_draw()
77 self.category_index = index
78
80 '''
81 Get selected index.
82
83 @return: Return selected item index.
84 '''
85 return self.category_index
86
88 '''
89 Get icon width.
90
91 @param items: A list of category item, format: (icon_dpixbuf, content, click_callback)
92 '''
93 icon_width = 0
94 for (icon_dpixbuf, content, _) in items:
95 if icon_dpixbuf:
96 icon_width = icon_dpixbuf.get_pixbuf().get_width()
97 break
98
99 return icon_width
100
101 gobject.type_register(Categorybar)
102
104 '''
105 CategoryItem class for use in CategoryBar.
106
107 @undocumented: wrap_category_item_clicked_action
108 @undocumented: expose_category_item
109 '''
110
111 - def __init__(self,
112 item,
113 index,
114 font_size,
115 icon_width,
116 padding_left,
117 padding_middle,
118 padding_right,
119 set_index,
120 get_index):
121 '''
122 Initialize CategoryItem class.
123
124 @param item: Category item, format: (item_dpixbuf, content, click_callback)
125 @param index: Category item index.
126 @param font_size: Font size.
127 @param icon_width: Icon width.
128 @param padding_left: Padding at left of item.
129 @param padding_middle: Padding between icon and font.
130 @param padding_right: Padding at right of item.
131 @param set_index: Set index callback.
132 @param get_index: Get index callback.
133 '''
134
135 gtk.Button.__init__(self)
136 self.font_size = font_size
137 self.index = index
138 self.set_index = set_index
139 self.get_index = get_index
140 self.padding_left = padding_left
141 self.padding_right = padding_right
142 (self.icon_dpixbuf, self.content, self.clicked_callback) = item
143 (content_width, font_height) = get_content_size(self.content, self.font_size)
144
145
146 self.font_offset = 0
147 if icon_width == 0:
148 self.font_offset = 0
149 else:
150 self.font_offset = padding_middle + icon_width
151 self.set_size_request(
152 padding_left + self.font_offset + content_width + padding_right,
153 -1
154 )
155
156 self.connect("expose-event", self.expose_category_item)
157 self.connect("clicked", lambda w: self.wrap_category_item_clicked_action())
158
160 '''
161 Internal function, wrap clicked action.
162 '''
163 if self.clicked_callback:
164 self.clicked_callback()
165 self.set_index(self.index)
166
168 '''
169 Internal function, callback for `expose-event` signal.
170
171 @param widget: Gtk.Widget instance.
172 @param event: Expose event.
173 @return: Always return True.
174 '''
175
176 cr = widget.window.cairo_create()
177 rect = widget.allocation
178 select_index = self.get_index()
179 font_color = ui_theme.get_color("category_item").get_color()
180
181
182 if widget.state == gtk.STATE_NORMAL:
183 if select_index == self.index:
184 select_status = BUTTON_PRESS
185 else:
186 select_status = BUTTON_NORMAL
187 elif widget.state == gtk.STATE_PRELIGHT:
188 if select_index == self.index:
189 select_status = BUTTON_PRESS
190 else:
191 select_status = BUTTON_HOVER
192 elif widget.state == gtk.STATE_ACTIVE:
193 select_status = BUTTON_PRESS
194
195 if select_status == BUTTON_PRESS:
196 draw_vlinear(cr, rect.x, rect.y, rect.width, rect.height,
197 ui_theme.get_shadow_color("category_item_press").get_color_info())
198
199 font_color = ui_theme.get_color("category_select_item").get_color()
200 elif select_status == BUTTON_HOVER:
201 draw_vlinear(cr, rect.x, rect.y, rect.width, rect.height,
202 ui_theme.get_shadow_color("category_item_hover").get_color_info())
203
204 font_color = ui_theme.get_color("category_select_item").get_color()
205
206
207 category_item_pixbuf = self.icon_dpixbuf.get_pixbuf()
208 draw_pixbuf(
209 cr, category_item_pixbuf,
210 rect.x + self.padding_left,
211 rect.y + (rect.height - category_item_pixbuf.get_height()) / 2
212 )
213
214
215 draw_text(cr, self.content,
216 rect.x + self.padding_left + self.font_offset,
217 rect.y,
218 rect.width - self.padding_left - self.font_offset - self.padding_right,
219 rect.height,
220 self.font_size,
221 font_color,
222 )
223
224
225 propagate_expose(widget, event)
226
227 return True
228
229 gobject.type_register(CategoryItem)
230