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, ImageBox
24 from button import ThemeButton, MenuButton, MinButton, MaxButton, CloseButton
25 from draw import draw_line
26 from label import Label
27 from locales import _
28 import tooltip as Tooltip
29 from utils import window_is_max
30 import gobject
31 import gtk
32 import pango
33
35 """
36 Titlebar defines every thing of a title bar of a application based on deepin ui.
37
38 @undocumented: expose_titlebar_separator
39 """
40 - def __init__(self,
41 button_mask=["theme", "menu", "max", "min", "close"],
42 icon_dpixbuf=None,
43 app_name=None,
44 title=None,
45 add_separator=False,
46 height=26,
47 show_title=True,
48 ):
49 """
50 Initialize the title bar.
51
52 @param button_mask: A string list. Each item of it indicates that there is a corresponding button on the title bar. By default, it's ["theme", "menu", "max", "min", "close"], which means theme button, menu button, max button, min button and close button, respectively.
53 @param icon_dpixbuf: A pixbuf of type dtk.ui.theme.DynamicPixbuf. It will be displayed at the top left of the window. By default, it's None.
54 @param app_name: Application name string. It will be displayed just next to the icon_dpixbuf. By default, it's None.
55 @param title: Title string of the application. It will be displayed on the center of the title bar. By default, it's None.
56 @param add_separator: If True, add a separation line between the title bar and the body of the window. By default, it's False.
57 @param height: The hight of the title bar. By default, it's 26 pixels.
58 @param show_title: If False, the title bar will not be displayed. By default, it's True.
59 """
60
61 EventBox.__init__(self)
62 self.set_size_request(-1, height)
63 self.v_layout_box = gtk.VBox()
64 self.h_layout_box = gtk.HBox()
65 self.add(self.v_layout_box)
66 self.v_layout_box.pack_start(self.h_layout_box, True, True)
67
68
69 if add_separator:
70 self.separator = gtk.HBox()
71 self.separator.set_size_request(-1, 1)
72 self.separator.connect("expose-event", self.expose_titlebar_separator)
73 self.v_layout_box.pack_start(self.separator, True, True)
74
75
76 self.drag_box = EventBox()
77 self.h_layout_box.pack_start(self.drag_box, True, True)
78
79
80 self.left_box = gtk.HBox()
81 self.drag_box.add(self.left_box)
82
83 if show_title:
84
85 if icon_dpixbuf != None:
86 self.icon_image_box = ImageBox(icon_dpixbuf)
87 self.icon_align = gtk.Alignment()
88 self.icon_align.set(0.5, 0.5, 0.0, 0.0)
89 self.icon_align.set_padding(5, 5, 5, 0)
90 self.icon_align.add(self.icon_image_box)
91 self.left_box.pack_start(self.icon_align, False, False)
92
93
94 if app_name != None:
95 self.app_name_box = Label(app_name, enable_gaussian=True)
96 self.app_name_align = gtk.Alignment()
97 self.app_name_align.set(0.5, 0.5, 0.0, 0.0)
98 self.app_name_align.set_padding(2, 0, 5, 0)
99 self.app_name_align.add(self.app_name_box)
100 self.left_box.pack_start(self.app_name_align, False, False)
101
102
103 if title != None:
104 self.title_box = Label(title, enable_gaussian=True, text_x_align=pango.ALIGN_CENTER)
105 self.title_align = gtk.Alignment()
106 self.title_align.set(0.5, 0.5, 0.0, 0.0)
107 self.title_align.set_padding(2, 0, 30, 30)
108 self.title_align.add(self.title_box)
109 self.left_box.pack_start(self.title_align, True, True)
110
111
112 self.button_box = gtk.HBox()
113 self.button_align = gtk.Alignment()
114 self.button_align.set(1.0, 0.0, 0.0, 0.0)
115 self.button_align.set_padding(0, 0, 0, 0)
116 self.button_align.add(self.button_box)
117 self.h_layout_box.pack_start(self.button_align, False, False)
118
119
120 if "theme" in button_mask:
121 self.theme_button = ThemeButton()
122 self.button_box.pack_start(self.theme_button, False, False, 1)
123 Tooltip.text(self.theme_button, _("Change skin")).show_delay(self.theme_button, 2000)
124
125
126 if "menu" in button_mask:
127 self.menu_button = MenuButton()
128 self.button_box.pack_start(self.menu_button, False, False, 1)
129 Tooltip.text(self.menu_button, _("Main menu")).show_delay(self.menu_button, 2000)
130
131
132 if "min" in button_mask:
133 self.min_button = MinButton()
134 self.button_box.pack_start(self.min_button, False, False, 1)
135 Tooltip.text(self.min_button, _("Minimum")).show_delay(self.min_button, 2000)
136
137
138 if "max" in button_mask:
139 self.max_button = MaxButton()
140 self.button_box.pack_start(self.max_button, False, False, 1)
141 Tooltip.text(self.max_button, _("Maximize")).show_delay(self.max_button, 2000)
142
143
144 if "close" in button_mask:
145 self.close_button = CloseButton()
146 self.button_box.pack_start(self.close_button, False, False)
147 Tooltip.text(self.close_button, _("Close")).show_delay(self.close_button, 2000)
148
149
150 self.show_all()
151
153 """
154 Expose the separation line between the titlebar and the body of the window.
155
156 @param widget: A widget of type Gtk.Widget.
157 @param event: Not used.
158 @return: Always return True.
159 """
160
161 cr = widget.window.cairo_create()
162 rect = widget.allocation
163
164
165 cr.set_source_rgba(1, 1, 1, 0.5)
166 draw_line(cr, rect.x + 1, rect.y + 2, rect.x + rect.width - 1, rect.y + 1)
167
168 return True
169
171 """
172 Change the title of the application, which is diplayed on the center of the title bar.
173
174 @param title: New title string that want to set.
175 """
176 self.title_box.set_text(title)
177
178 gobject.type_register(Titlebar)
179
180 if __name__ == "__main__":
181
183 if window_is_max(w):
184 win.unmaximize()
185 print "min"
186 else:
187 win.maximize()
188 print "max"
189
190 win = gtk.Window(gtk.WINDOW_TOPLEVEL)
191 win.connect("destroy", gtk.main_quit)
192 tit = Titlebar()
193 tit.max_button.connect("clicked", max_signal)
194 win.add(tit.box)
195 win.show_all()
196
197 gtk.main()
198