Package dtk :: Package ui :: Module paned

Source Code for Module dtk.ui.paned

  1  #! /usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  # Copyright (C) 2011 ~ 2012 Deepin, Inc. 
  5  #               2011 ~ 2012 Xia Bin 
  6  # 
  7  # Author:     Xia Bin <xiabin@gmail.com> 
  8  # Maintainer: Xia Bin <xiabin@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 utils import is_in_rect 
 24  import gobject 
 25  import gtk 
 26   
27 -class Paned(gtk.Paned):
28 ''' 29 Paned. 30 31 @undocumented: do_enter_notify_event 32 @undocumented: do_button_press_event 33 @undocumented: do_size_allocate 34 @undocumented: do_enter_notify_event 35 @undocumented: is_in_button 36 @undocumented: draw_handle 37 @undocumented: do_expose_event 38 39 gtk.Paned with custom better apperance. 40 '''
41 - def __init__(self):
42 ''' 43 Initialize Paned class. 44 ''' 45 gtk.Paned.__init__(self) 46 self.bheight = 60 #the button height or width; 47 self.saved_position = -1 48 self.handle_size = self.style_get_property('handle-size')
49
50 - def do_expose_event(self, e):
51 ''' 52 To intercept the default expose event and draw custom handle 53 after the **gtk.Container** expose evetn. 54 So the gtk.Paned's expose event callback is ignore. 55 ''' 56 #gtk.Paned.do_expose_event(self, e) 57 gtk.Container.do_expose_event(self, e) 58 self.draw_handle(e) 59 60 return False
61
62 - def draw_handle(self, e):
63 ''' 64 Draw the cusom handle apperance. 65 ''' 66 handle = self.get_handle_window() 67 line_width = 1 68 cr = handle.cairo_create() 69 cr.set_source_rgba(1, 0,0, 0.8) 70 (width, height) = handle.get_size() 71 if self.get_orientation() == gtk.ORIENTATION_HORIZONTAL: 72 #draw line 73 cr.rectangle(0, 0, line_width, height) 74 75 #draw_button 76 cr.rectangle(0, (height-self.bheight)/2, width, self.bheight) 77 else: 78 cr.rectangle(0, 0, height, line_width) 79 cr.rectangle((width-self.bheight)/2, 0, self.bheight, width) 80 81 cr.fill() 82 pass
83
84 - def is_in_button(self, x, y):
85 ''' 86 Detection of wheter the mouse pointer is in the handler's button. 87 ''' 88 handle = self.get_handle_window() 89 (width, height) = handle.get_size() 90 if self.get_orientation() == gtk.ORIENTATION_HORIZONTAL: 91 rect = (0, (height-self.bheight)/2, width, self.bheight) 92 else: 93 rect = ((width-self.bheight)/2, 0, self.bheight, height) 94 95 if is_in_rect((x, y), rect): 96 return True 97 else: 98 return False
99
100 - def do_enter_notify_event(self, e):
101 ''' 102 change the cursor style when move in handler 103 ''' 104 handle = self.get_handle_window() 105 (width, height) = handle.get_size() 106 if self.is_in_button(e.x, e.y): 107 handle.set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND1)) 108 else: 109 handle.set_cursor(self.cursor_type) 110 111 self.queue_draw()
112
113 - def do_button_press_event(self, e):
114 ''' 115 when press the handler's button change the position. 116 ''' 117 if self.is_in_button(e.x, e.y): 118 if self.saved_position == -1: 119 self.saved_position = self.get_position() 120 self.set_position(0) 121 else: 122 self.set_position(self.saved_position) 123 self.saved_position = -1 124 else: 125 gtk.Paned.do_button_press_event(self, e) 126 return True
127
128 - def do_size_allocate(self, e):
129 gtk.Paned.do_size_allocate(self, e) 130 131 c2 = self.get_child2() 132 133 if c2 == None: return 134 135 a2 = c2.allocation 136 137 if self.get_orientation() == gtk.ORIENTATION_HORIZONTAL: 138 a2.x -= self.handle_size 139 a2.width += self.handle_size 140 else: 141 a2.y -= self.handle_size 142 a2.height += self.handle_size 143 c2.size_allocate(a2)
144
145 -class HPaned(Paned):
146 - def __init__(self):
147 Paned.__init__(self) 148 self.set_orientation(gtk.ORIENTATION_HORIZONTAL) 149 self.cursor_type = gtk.gdk.Cursor(gtk.gdk.SB_H_DOUBLE_ARROW)
150
151 -class VPaned(Paned):
152 - def __init__(self):
153 Paned.__init__(self) 154 self.set_orientation(gtk.ORIENTATION_VERTICAL) 155 self.cursor_type = gtk.gdk.Cursor(gtk.gdk.SB_V_DOUBLE_ARROW)
156 157 gobject.type_register(Paned) 158 gobject.type_register(HPaned) 159 gobject.type_register(VPaned) 160 161 if __name__ == '__main__': 162 w = gtk.Window() 163 w.set_size_request(700, 400) 164 #w.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color('yellow')) 165 box = gtk.VBox() 166 167 p = VPaned() 168 c1 = gtk.Button("11111111111111111111111") 169 c1.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color('blue')) 170 c2 = gtk.Button("122222222222222222222222") 171 c1.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color('red')) 172 p.add1(c1) 173 p.add2(c2) 174 box.pack_start(p) 175 176 p = HPaned() 177 c1 = gtk.Button("11111111111111111111111") 178 c1.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color('blue')) 179 c2 = gtk.Button("122222222222222222222222") 180 c1.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color('red')) 181 p.add1(c1) 182 p.add2(c2) 183 box.pack_start(p) 184 185 w.add(box) 186 w.connect('destroy', gtk.main_quit) 187 w.show_all() 188 gtk.main() 189