Package dtk :: Package ui :: Module panel

Source Code for Module dtk.ui.panel

  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 skin_config import skin_config 
 24  from theme import ui_theme 
 25  from utils import remove_timeout_id 
 26  import cairo 
 27  import gobject 
 28  import gtk 
 29   
 30  __all__ = ["Panel"] 
 31   
32 -class Panel(gtk.Window):
33 ''' 34 Panel. 35 36 @undocumented: stop_render 37 @undocumented: start_show 38 @undocumented: start_hide 39 @undocumented: render_show 40 @undocumented: render_hide 41 @undocumented: shape_panel 42 ''' 43
44 - def __init__(self, 45 width, 46 height, 47 window_type=gtk.WINDOW_TOPLEVEL):
48 ''' 49 Initialize Panel class. 50 51 @param width: Initialize panel width. 52 @param height: Initialize panel height. 53 @param window_type: Window type, default is gtk.WINDOW_TOPLEVEL. 54 ''' 55 # Init. 56 gtk.Window.__init__(self, window_type) 57 skin_config.wrap_skin_window(self) 58 self.set_decorated(False) 59 self.set_colormap(gtk.gdk.Screen().get_rgba_colormap()) 60 self.add_events(gtk.gdk.ALL_EVENTS_MASK) 61 self.set_skip_taskbar_hint(True) 62 self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG) # make panel window don't switch in window manager 63 self.start_show_id = None 64 self.start_hide_id = None 65 self.delay = 50 # milliseconds 66 self.show_inc_opacity = 0.1 67 self.hide_dec_opacity = 0.05 68 self.width = width 69 self.height = height 70 self.set_size_request(self.width, self.height) 71 72 # Change shape. 73 self.connect("size-allocate", self.shape_panel)
74
75 - def stop_render(self):
76 ''' 77 Internal function to stop render. 78 ''' 79 # Stop callback. 80 remove_timeout_id(self.start_show_id) 81 remove_timeout_id(self.start_hide_id)
82
83 - def show_panel(self):
84 ''' 85 Show panel. 86 ''' 87 self.set_opacity(1) 88 self.show_all()
89
90 - def hide_panel(self):
91 ''' 92 Hide panel. 93 ''' 94 self.set_opacity(0) 95 self.hide_all()
96
97 - def start_show(self):
98 ''' 99 Internal function to start show. 100 ''' 101 if self.start_show_id == None and self.get_opacity() != 1: 102 self.stop_render() 103 self.start_show_id = gtk.timeout_add(self.delay, self.render_show) 104 self.show_all()
105
106 - def start_hide(self):
107 ''' 108 Internal function to start hide. 109 ''' 110 if self.start_hide_id == None and self.get_opacity() != 0: 111 self.stop_render() 112 self.start_hide_id = gtk.timeout_add(self.delay, self.render_hide)
113
114 - def render_show(self):
115 ''' 116 Internal function to render show effect. 117 ''' 118 self.set_opacity(min(self.get_opacity() + self.show_inc_opacity, 1)) 119 120 if self.get_opacity() >= 1: 121 self.stop_render() 122 return False 123 else: 124 return True
125
126 - def render_hide(self):
127 ''' 128 Internal function to render hide effect. 129 ''' 130 self.set_opacity(max(self.get_opacity() - self.hide_dec_opacity, 0)) 131 132 if self.get_opacity() <= 0: 133 self.stop_render() 134 self.hide_panel() 135 return False 136 else: 137 return True
138
139 - def resize_panel(self, w, h):
140 ''' 141 Resize panel. 142 143 @param w: Resize width. 144 @param h: Resize height. 145 ''' 146 self.width = w 147 self.height = h 148 self.set_size_request(self.width, self.height) 149 self.shape_panel(self, self.get_allocation())
150
151 - def shape_panel(self, widget, rect):
152 ''' 153 Internal callback for `size-allocate` signal. 154 155 @param widget: Panel widget. 156 @param rect: Size allocation. 157 ''' 158 if widget.window != None and widget.get_has_window() and rect.width > 0 and rect.height > 0: 159 # Init. 160 x, y, w, h = rect.x, rect.y, rect.width, rect.height 161 bitmap = gtk.gdk.Pixmap(None, w, h, 1) 162 cr = bitmap.cairo_create() 163 164 # Clear the bitmap 165 cr.set_source_rgb(0.0, 0.0, 0.0) 166 cr.set_operator(cairo.OPERATOR_CLEAR) 167 cr.paint() 168 169 # Draw our shape into the bitmap using cairo. 170 cr.set_source_rgb(1.0, 1.0, 1.0) 171 cr.set_operator(cairo.OPERATOR_OVER) 172 cr.rectangle(0, 0, w, self.height) 173 cr.fill() 174 175 # Shape with given mask. 176 widget.shape_combine_mask(bitmap, 0, 0)
177 178 gobject.type_register(Panel) 179
180 -class TestWidget(object):
181 '''class docs''' 182
183 - def __init__(self, panel):
184 '''init docs''' 185 self.panel = panel 186 self.test_hide_id = None
187
188 - def show_panel(self):
189 '''docs''' 190 remove_timeout_id(self.test_hide_id) 191 192 self.panel.start_show() 193 self.test_hide_id = gtk.timeout_add(5000, self.panel.start_hide)
194
195 - def enter_notify_callback(self):
196 '''docs''' 197 remove_timeout_id(self.test_hide_id) 198 199 self.panel.start_show()
200
201 - def leave_notify_callback(self):
202 '''docs''' 203 self.test_hide_id = gtk.timeout_add(5000, self.panel.start_hide)
204 205 if __name__ == "__main__": 206 # Init window. 207 window = gtk.Window() 208 window.set_decorated(False) 209 window.add_events(gtk.gdk.ALL_EVENTS_MASK) 210 window.connect("destroy", lambda w: gtk.main_quit()) 211 window.move(100, 100) 212 window.add(gtk.image_new_from_pixbuf(ui_theme.get_pixbuf("background12.png").get_pixbuf())) 213 214 window.show_all() 215 216 panel = Panel() 217 panel.move(100, 592) 218 panel.add(gtk.image_new_from_pixbuf(ui_theme.get_pixbuf("background13.png").get_pixbuf())) 219 panel.hide_panel() 220 221 test_widget = TestWidget(panel) 222 window.connect("motion-notify-event", lambda w, e: test_widget.show_panel()) 223 panel.connect("enter-notify-event", lambda w, e: test_widget.enter_notify_callback()) 224 panel.connect("leave-notify-event", lambda w, e: test_widget.leave_notify_callback()) 225 226 gtk.main() 227