1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 from mplayer_window import MplayerWindow
24 from skin import SkinWindow
25 from skin_config import skin_config
26 from threads import post_gui
27 from titlebar import Titlebar
28 from utils import container_remove_all, place_center
29 from window import Window
30 import gtk
33 """
34 This is the base class of every program based on deepin-ui.
35 Every program should realize it.
36 """
37 - def __init__(self, app_support_colormap=True):
38 """
39 Initialize the Application class.
40
41 @param app_support_colormap: Set False if your program don't allow manipulate colormap, such as mplayer, otherwise you should keep this option as True.
42 """
43
44 self.app_support_colormap = app_support_colormap
45 self.close_callback = self.close_window
46
47
48 self.init()
49
51 """
52 This do the remain initialize step.
53
54 It Initializes the window and some important signal such as "destroy".
55 """
56
57 gtk.gdk.threads_init()
58
59
60 self.menu_button_callback = None
61
62
63 if self.app_support_colormap:
64 self.window = Window(True)
65 else:
66 self.window = MplayerWindow(True)
67 self.window.set_position(gtk.WIN_POS_CENTER)
68 self.window.connect("destroy", self.destroy)
69
70
71 self.main_box = self.window.window_frame
72
73
74 self.titlebar = None
75 self.titlebar_box = gtk.HBox()
76 self.main_box.pack_start(self.titlebar_box, False)
77
78 - def add_titlebar(self,
79 button_mask=["theme", "menu", "max", "min", "close"],
80 icon_dpixbuf=None, app_name=None, title=None, add_separator=False, show_title=True):
81 """
82 Add titlebar to the application.
83
84 Connect click signal of the standard button to default callback.
85
86 @param button_mask: A list of string, each of which stands for a standard button on top right of the window. By default, it's ["theme", "menu", "max", "min", "close"].
87 @param icon_dpixbuf: The icon pixbuf of type dtk.ui.theme.DynamicPixbuf. By default, it is None.
88 @param app_name: The name string of the application, which will be displayed just next to the icon_dpixbuf. By default, it is None.
89 @param title: The title string of the window, which will be displayed on the center of the titlebar. By default, it is None.
90 @param add_separator: If True, add a line between the titlebar and the body of the window. By default, it's False.
91 @param show_title: If False, the titlebar will not be displayed. By default, it's True.
92 """
93
94 self.titlebar = Titlebar(button_mask, icon_dpixbuf, app_name, title, add_separator, show_title=show_title)
95 if "theme" in button_mask:
96 self.titlebar.theme_button.connect("clicked", self.theme_callback)
97 if "menu" in button_mask:
98 self.titlebar.menu_button.connect("clicked", self.menu_callback)
99 if "min" in button_mask:
100 self.titlebar.min_button.connect("clicked", lambda w: self.window.min_window())
101 if "max" in button_mask:
102 self.titlebar.max_button.connect("clicked", lambda w: self.window.toggle_max_window())
103 if "close" in button_mask:
104 self.titlebar.close_button.connect("clicked", self.close_callback)
105 self.window.add_toggle_event(self.titlebar)
106 self.window.add_move_event(self.titlebar)
107
108
109 self.show_titlebar()
110
112 """
113 Close the window when the close button is clicked.
114
115 @param widget: A widget of Gtk.Widget. Passed by gtk.
116 """
117 self.window.close_window()
118
120 """
121 Show title bar of the window.
122
123 By default, it is invoked at the last step of add_titlebar.
124 """
125 if self.titlebar_box.get_children() == [] and self.titlebar != None:
126 self.titlebar_box.add(self.titlebar)
127
133
134 @post_gui
136 """
137 Raise the window to the top of the window stack.
138 """
139 self.window.present()
140
142 """
143 Set the application title.
144
145 @param title: The title string of the application.
146 """
147 self.titlebar.change_title(title)
148
150 """
151 Set the default size of the window.
152
153 @param default_width: Default width in pixels of the application, once set, application don't allow smaller than width.
154 @param default_height: Default height in pixels of the application, once set, application don't allow smaller than height.
155 """
156 self.window.set_default_size(default_width, default_height)
157 self.window.set_geometry_hints(
158 None,
159 default_width,
160 default_height
161 -1, -1, -1, -1, -1, -1, -1, -1
162 )
163
164
165 skin_config.set_application_window_size(default_width, default_height)
166
168 """
169 Set the icon of the application.
170
171 This icon is used by the window manager or the dock.
172
173 @param icon_dpixbuf: The icon pixbuf of dtk.ui.theme.DynamicPixbuf.
174 """
175 gtk.window_set_default_icon(icon_dpixbuf.get_pixbuf())
176
177 - def destroy(self, widget, data=None):
178 """
179 Destroy the window and quit the program.
180
181 @param widget: Not used.
182 @param data: Not used.
183 """
184 gtk.main_quit()
185
187 """
188 Show the window and start the mainloop.
189
190 You must use this function at last of program, otherwise program will run in loop too early that all code after application.run won't execute until program exit.
191 """
192
193 self.window.show_window()
194
195
196 gtk.main()
197
199 """
200 Set the skin preview of the application.
201
202 @note: The size of preview_pixbuf must be proportional to the size of program, otherwise adjust skin will got wrong coordinate.
203
204 @param preview_pixbuf: A pixbuf of type dtk.ui.theme.DynamicPixbuf.
205 """
206 self.skin_preview_pixbuf = preview_pixbuf
207
209 """
210 Invoked when the theme button is clicked.
211
212 @param widget: Not used.
213 @return: Always return False
214 """
215 skin_window = SkinWindow(self.skin_preview_pixbuf)
216 skin_window.show_all()
217 skin_window.connect("show", lambda w: place_center(self.window, w))
218
219 return False
220
222 """
223 Invoked when the menu button is clicked.
224
225 @param widget: Not used.
226 @return: Always return False
227 """
228 if self.menu_button_callback:
229 self.menu_button_callback(widget)
230
231 return False
232
234 """
235 Set the menu_button_callback function.
236
237 @param callback: A function which is invoked when the menu button is clicked.
238 """
239 self.menu_button_callback = callback
240