Package dtk :: Package ui :: Module application

Source Code for Module dtk.ui.application

  1  #! /usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  # Copyright (C) 2011 ~ 2012 Deepin, Inc. 
  5  #               2011 ~ 2012 Wang Yong 
  6  # 
  7  # Author:     Wang Yong <lazycat.manatee@gmail.com> 
  8  # Maintainer: Wang Yong <lazycat.manatee@gmail.com> 
  9  # 
 10  # This program is free software: you can redistribute it and/or modify 
 11  # it under the terms of the GNU General Public License as published by 
 12  # the Free Software Foundation, either version 3 of the License, or 
 13  # any later version. 
 14  # 
 15  # This program is distributed in the hope that it will be useful, 
 16  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 17  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 18  # GNU General Public License for more details. 
 19  # 
 20  # You should have received a copy of the GNU General Public License 
 21  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 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 
31 32 -class Application(object):
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 # Init. 44 self.app_support_colormap = app_support_colormap 45 self.close_callback = self.close_window 46 47 # Start application. 48 self.init()
49
50 - def init(self):
51 """ 52 This do the remain initialize step. 53 54 It Initializes the window and some important signal such as "destroy". 55 """ 56 # Init gdk threads, the integrant method for multi-thread GUI application. 57 gtk.gdk.threads_init() 58 59 # Init status. 60 self.menu_button_callback = None 61 62 # Init window. 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 # Init main box. 71 self.main_box = self.window.window_frame 72 73 # Add titlebar box. 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 # Init titlebar. 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 # Show titlebar. 109 self.show_titlebar()
110
111 - def close_window(self, widget):
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
119 - def show_titlebar(self):
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
128 - def hide_titlebar(self):
129 """ 130 Hide the title bar. 131 """ 132 container_remove_all(self.titlebar_box)
133 134 @post_gui
135 - def raise_to_top(self):
136 """ 137 Raise the window to the top of the window stack. 138 """ 139 self.window.present()
140
141 - def set_title(self, title):
142 """ 143 Set the application title. 144 145 @param title: The title string of the application. 146 """ 147 self.titlebar.change_title(title)
148
149 - def set_default_size(self, default_width, default_height):
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, # minimum width 160 default_height # minimum height 161 -1, -1, -1, -1, -1, -1, -1, -1 162 ) 163 164 # Pass application size to skin config. 165 skin_config.set_application_window_size(default_width, default_height)
166
167 - def set_icon(self, icon_dpixbuf):
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
186 - def run(self):
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 # Show window. 193 self.window.show_window() 194 195 # Run main loop. 196 gtk.main()
197
198 - def set_skin_preview(self, preview_pixbuf):
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
208 - def theme_callback(self, widget):
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
221 - def menu_callback(self, widget):
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
233 - def set_menu_callback(self, callback):
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