root/branch/k0p/umitInterfaceEditor/selectborder/WrapperWidgets.py @ 1116

Revision 1116, 17.7 kB (checked in by kop-labs, 6 years ago)

Select each item - only one

Line 
1# Copyright (C) 2005 Insecure.Com LLC.
2#
3# Author: Luis A. Bastiao Silva <luis.kop@gmail.com>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19
20import gobject
21import gtk
22from gtk import gdk
23
24#Voidplace
25from Voidplace import background_xpm
26
27
28'''
29Special widgets
30with a wrapper
31
32The trick is have some gdk.Window
33each node have one
34
35
36'''
37
38
39
40
41
42class SpecialWrapperEntry(object):
43    '''
44    It's a SpecialWrapper of any widget
45    using with inheritance
46    '''
47    def __init__(self, widget):
48        self._entry = widget
49        self._ctr = False
50       
51    def unconstruct(self):
52        self._left_down_win.set_user_data(None)
53        self._left_down_win.destroy()
54
55        self._left_up_win.set_user_data(None) 
56        self._left_up_win.destroy()
57
58        self._right_down_win.set_user_data(None)
59        self._right_down_win.destroy()
60       
61        self._right_up_win.set_user_data(None)
62        self._right_up_win.destroy()
63       
64        self._ctr  = True
65   
66   
67
68       
69    def construct(self):
70        entry = self._entry
71       
72        #Left up window
73        win = gtk.gdk.Window(entry.window,
74                             6 ,
75                             6,
76                             gtk.gdk.WINDOW_CHILD,
77                             (gtk.gdk.ENTER_NOTIFY_MASK |
78                              gtk.gdk.LEAVE_NOTIFY_MASK),
79                             gtk.gdk.INPUT_OUTPUT,
80                             'left up window',
81                             0, 0,
82                             entry.get_visual(),
83                             entry.get_colormap(),
84                             gtk.gdk.Cursor(entry.get_display(), gdk.LEFT_PTR),
85                             '', '', True)
86        self._left_up_win = win
87        win.set_user_data(entry)
88        c = win.get_colormap()
89        color = c.alloc_color(0,0,0) 
90        win.set_background(color)
91       
92        #Right up window
93        win2 = gtk.gdk.Window(entry.window,
94                             6, 
95                             6,
96                             gtk.gdk.WINDOW_CHILD,
97                             (gtk.gdk.ENTER_NOTIFY_MASK |
98                              gtk.gdk.LEAVE_NOTIFY_MASK),
99                             gtk.gdk.INPUT_OUTPUT,
100                             'right up window',
101                             self._entry.allocation.width-6, 
102                             0,
103                             entry.get_visual(),
104                             entry.get_colormap(),
105                             gtk.gdk.Cursor(entry.get_display(), gdk.LEFT_PTR),
106                             '', '', True)
107        self._right_up_win = win2
108        win2.set_user_data(entry)
109        #win2.set_background(entry.style.base[entry.state])
110        win2.set_background(color)
111        #Right Down window
112        win3 = gtk.gdk.Window(entry.window,
113                             6, 
114                             6,
115                             gtk.gdk.WINDOW_CHILD,
116                             (gtk.gdk.ENTER_NOTIFY_MASK |
117                              gtk.gdk.LEAVE_NOTIFY_MASK),
118                             gtk.gdk.INPUT_OUTPUT,
119                             'right down window',
120                             self._entry.allocation.width-6, 
121                             self._entry.allocation.height-6,
122                             entry.get_visual(),
123                             entry.get_colormap(),
124                             gtk.gdk.Cursor(entry.get_display(), gdk.LEFT_PTR),
125                             '', '', True)
126        self._right_down_win = win3
127        win3.set_user_data(entry)
128        win3.set_background(color)
129        #Left down window
130        win4 = gtk.gdk.Window(entry.window,
131                             6 ,
132                             6,
133                             gtk.gdk.WINDOW_CHILD,
134                             (gtk.gdk.ENTER_NOTIFY_MASK |
135                              gtk.gdk.LEAVE_NOTIFY_MASK),
136                             gtk.gdk.INPUT_OUTPUT,
137                             'left down window',
138                             0, self._entry.allocation.height-6,
139                             entry.get_visual(),
140                             entry.get_colormap(),
141                             gtk.gdk.Cursor(entry.get_display(), gdk.LEFT_PTR),
142                             '', '', True)
143        self._left_down_win = win4
144        win4.set_user_data(entry) 
145        win4.set_background(color)
146
147        self._ctr = False
148    def need_construct(self):
149        '''
150        @return : if need or not constuct
151        @rtype: bool
152       
153        '''
154        return self._ctr
155       
156    def resize(self):
157        self._right_up_win.move(0,0) 
158        self._right_up_win.move(self._entry.allocation.width-6,0) 
159        self._right_down_win.move(self._entry.allocation.width-6,
160                                  self._entry.allocation.height-6)       
161        self._left_down_win.move(0,self._entry.allocation.height-6) 
162    def draw(self):
163        self.resize()
164
165        x,y,w,h = self._entry.allocation
166       
167        win = self._left_up_win
168        c = win.get_colormap()
169        color = c.alloc_color(0,0,0)   
170        color = gtk.gdk.GC(win, 
171                                   line_width=6, 
172                                   foreground = color )
173        win.draw_rectangle(color, True, 0, 0, 6, 6)
174        win.show()
175
176        win2 = self._right_up_win
177        c = win2.get_colormap()
178        color = c.alloc_color(0,0,0)   
179        color = gtk.gdk.GC(win2, line_width=6, foreground = color )
180        win2.draw_rectangle(color, True, 0, 0, 
181                            6, 6)
182        win2.show() 
183       
184
185        win3 = self._right_down_win
186        c = win3.get_colormap()
187        color = c.alloc_color(0,0,0)   
188        color = gtk.gdk.GC(win3, line_width=6, foreground = color )
189        win3.draw_rectangle(color, True, 0, 0, 
190                            6, 6)
191        win3.show() 
192       
193        win4 = self._left_down_win
194        c = win4.get_colormap()
195        color = c.alloc_color(0,0,0)   
196        color = gtk.gdk.GC(win4, line_width=6, foreground = color )
197        win4.draw_rectangle(color, True, 0, 0, 
198                            6, 6)
199        win4.show() 
200       
201
202class SpecialWrapperGeneric(object):
203    '''
204    This wrapper is for box and contaires
205    '''
206    def __init__(self, widget):
207        self._entry = widget
208       
209    def unconstruct(self):
210        self._left_down_win.set_user_data(None)
211        self._left_down_win.destroy()
212
213        self._left_up_win.set_user_data(None) 
214        self._left_up_win.destroy()
215
216        self._right_down_win.set_user_data(None)
217        self._right_down_win.destroy()
218       
219        self._right_up_win.set_user_data(None)
220        self._right_up_win.destroy()
221       
222        self._ctr  = True
223   
224    def construct(self):
225        entry = self._entry
226        x,y,w,h = self._entry.allocation
227
228        #Left up
229        win = gtk.gdk.Window(entry.window,
230                             6,
231                             6,
232                             gtk.gdk.WINDOW_CHILD,
233                             (gtk.gdk.ENTER_NOTIFY_MASK |
234                              gtk.gdk.LEAVE_NOTIFY_MASK),
235                             gtk.gdk.INPUT_OUTPUT,
236                             'icon window',
237                             x, y,
238                             entry.get_visual(),
239                             entry.get_colormap(),
240                             gtk.gdk.Cursor(entry.get_display(), gdk.LEFT_PTR),
241                             '', '', True)
242        self._left_up_win = win
243        win.set_user_data(entry) 
244        c = win.get_colormap()
245        color = c.alloc_color(0,0,0)   
246        win.set_background(color)
247
248        #Left down
249        win2 = gtk.gdk.Window(entry.window,
250                             6, 
251                             6,
252                             gtk.gdk.WINDOW_CHILD,
253                             (gtk.gdk.ENTER_NOTIFY_MASK |
254                              gtk.gdk.LEAVE_NOTIFY_MASK),
255                             gtk.gdk.INPUT_OUTPUT,
256                             'icon window',
257                              x, 
258                               y+h-6,
259                             entry.get_visual(),
260                             entry.get_colormap(),
261                             gtk.gdk.Cursor(entry.get_display(), gdk.LEFT_PTR),
262                             '', '', True)
263        self._left_down_win = win2
264        win2.set_user_data(entry)
265        win2.set_background(color)
266
267        #Right up
268        win3 = gtk.gdk.Window(entry.window,
269                             6, 
270                             6,
271                             gtk.gdk.WINDOW_CHILD,
272                             (gtk.gdk.ENTER_NOTIFY_MASK |
273                              gtk.gdk.LEAVE_NOTIFY_MASK),
274                             gtk.gdk.INPUT_OUTPUT,
275                             'icon window',
276                              x+w-6, 
277                               y,
278                             entry.get_visual(),
279                             entry.get_colormap(),
280                             gtk.gdk.Cursor(entry.get_display(), gdk.LEFT_PTR),
281                             '', '', True)
282        self._right_up_win = win3
283        win3.set_user_data(entry)
284        win3.set_background(color)
285       
286        #Right down
287        win4 = gtk.gdk.Window(entry.window,
288                             6, 
289                             6,
290                             gtk.gdk.WINDOW_CHILD,
291                             (gtk.gdk.ENTER_NOTIFY_MASK |
292                              gtk.gdk.LEAVE_NOTIFY_MASK),
293                             gtk.gdk.INPUT_OUTPUT,
294                             'icon window',
295                              x+w-6, 
296                               y+h-6,
297                             entry.get_visual(),
298                             entry.get_colormap(),
299                             gtk.gdk.Cursor(entry.get_display(), gdk.LEFT_PTR),
300                             '', '', True)
301        self._right_down_win = win4
302        win4.set_user_data(entry)
303       
304        win4.set_background(color)
305       
306    def resize(self):
307       
308        x,y,w,h = self._entry.allocation
309        self._left_down_win.move(x, y+h-6)
310        self._left_up_win.move(x, y)
311        self._right_down_win.move(x+w-6, y)
312        self._right_up_win.move(x+w-6, y+h-6)
313       
314       
315    def draw(self):
316        self.resize()
317        x,y,w,h = self._entry.allocation
318        win = self._left_down_win
319        c = win.get_colormap()
320        color = c.alloc_color(0,0,0)   
321        color = gtk.gdk.GC(win, 
322                                   line_width=6, 
323                                   foreground = color )
324        win.draw_rectangle(color, True, 0, 0, 6, 6)
325        win.show()
326
327        win2 = self._left_up_win
328        c = win2.get_colormap()
329        color = c.alloc_color(0,0,0)   
330        color = gtk.gdk.GC(win2, line_width=6, foreground = color )
331        win2.draw_rectangle(color, True, 0, 0, 6, 6)
332        win2.show() 
333
334        win2 = self._right_down_win
335        c = win2.get_colormap()
336        color = c.alloc_color(0,0,0)   
337        color = gtk.gdk.GC(win2, line_width=6, foreground = color )
338        win2.draw_rectangle(color, True, 0, 0, 6, 6)
339        win2.show() 
340       
341        win2 = self._right_up_win
342        c = win2.get_colormap()
343        color = c.alloc_color(0,0,0)   
344        color = gtk.gdk.GC(win2, line_width=6, foreground = color )
345        win2.draw_rectangle(color, True, 0, 0, 6, 6)
346        win2.show() 
347       
348class SpecialEntry(gtk.Entry):
349    def __init__(self):
350        gtk.Entry.__init__(self)
351        self._icon = SpecialWrapperEntry(self)
352    def do_realize(self):
353        gtk.Entry.do_realize(self)
354        self._icon.construct() 
355    def do_expose_event(self,event):
356        gtk.Entry.do_expose_event(self, event)
357        self._icon.draw()
358       
359gobject.type_register(SpecialEntry)
360
361from Voidplace import Voidplace
362
363class SpecialVoidplace(Voidplace):
364    def __init__(self):
365        Voidplace.__init__(self)
366        self._icon = SpecialWrapperEntry(self)
367    def do_realize(self):
368        Voidplace.do_realize(self)
369        self._icon.construct() 
370    def do_expose_event(self,event):
371        Voidplace.do_expose_event(self, event)
372        self._icon.draw()
373       
374gobject.type_register(SpecialVoidplace)
375
376class SpecialHBox(gtk.HBox):
377    def __init__(self):
378        gtk.HBox.__init__(self)
379        self._icon = None
380        self._icon_exists = False 
381        self.selected = False
382    #Private API
383    def _button_press(self, widget, event, other):
384        if not self._icon_exists:
385           
386            self._icon.construct()
387       
388        self._icon.draw()
389        self.emit('button-press-event', event)
390   
391    #Public API
392    def do_realize(self):
393        gtk.HBox.do_realize(self)
394        self._icon = SpecialWrapperGeneric(self)
395        self._icon.construct() 
396        self._icon_exists= True 
397    def do_draw(self):
398        if self._icon!= None:
399            self._icon.draw()
400    def do_size_allocate(self, allocation):
401        gtk.HBox.do_size_allocate(self,allocation)
402    def do_expose_event(self,event):
403        gtk.HBox.do_expose_event(self, event)
404
405        #self._icon.draw()
406    def do_button_press_event(self, event):
407        if self._icon!= None and self._icon_exists:
408            self._icon.draw()
409   
410    def set_select(self, value):
411        self.selected = value
412        if not value:
413            self._icon.unconstruct()
414            self._icon_exists = False
415    def pack_start(self, child, expand=True, fill=True):
416        gtk.HBox.pack_start(self, child, expand, fill)
417        child.connect('button-press-event', self._button_press, None)
418       
419gobject.type_register(SpecialHBox)
420
421class NotebookLabel(gtk.EventBox):
422    '''
423    Label to Notebook with nodes and borders when selectable
424    '''
425    def __init__(self, title = ''):
426        gtk.EventBox.__init__(self)
427        self.label = gtk.Label(title)
428        self.add(self.label)
429        self._icon = SpecialWrapperEntry(self)
430        self._selected = None
431        self._vp = False 
432    def set_text(self, text):
433        self.label.set_text(text)
434    def get_text(self):
435        return self.label.get_text()
436   
437    def set_select(self, value):
438        self._selected = value
439   
440    def do_realize(self):
441
442       
443        gtk.EventBox.do_realize(self)
444        self.set_flags(self.flags() | gtk.REALIZED)
445        self._icon.construct()
446    def voidplace(self):
447        '''
448        Construct a voidplace
449        '''
450       
451        self._xmp = background_xpm
452        win_tmp = self.get_parent_window()
453        self._voidplace = gtk.gdk.Window(win_tmp,
454                             self.allocation.width ,
455                             self.allocation.height,
456                             gtk.gdk.WINDOW_CHILD,
457                             (gtk.gdk.ENTER_NOTIFY_MASK |
458                              gtk.gdk.LEAVE_NOTIFY_MASK),
459                             gtk.gdk.INPUT_OUTPUT,
460                             'voidplace',
461                             self.allocation.x,self.allocation.y,
462                             self.get_visual(),
463                             self.get_colormap(),
464                             gtk.gdk.Cursor(self.get_display(), gdk.LEFT_PTR),
465                             '', '', True)
466        self._voidplace.set_user_data(self)
467        self.style.attach(self.window)
468        temp_map = gdk.pixmap_colormap_create_from_xpm_d(self._voidplace,
469                                                         self.get_colormap(),
470                                                         None,
471                                                         self._xmp)
472        self._voidplace.set_back_pixmap(temp_map[0], False)
473        c= self._voidplace.get_colormap()
474        color = c.alloc_color(0,65555,0)
475        self._draw_gc = gtk.gdk.GC(self._voidplace, 
476                                   line_width=6, 
477                                   foreground = color )
478        self._voidplace.draw_rectangle(self._draw_gc, True, self.allocation.x, 
479                                   self.allocation.y, self.allocation.width,
480                                   self.allocation.height)
481        self._voidplace.show()
482        self._vp = True 
483    def unload(self):
484        if not self._icon.need_construct():
485            self._icon.unconstruct()
486        if self._vp:
487            #self._voidplace.set_background(self.style.base[gtk.STATE_NORMAL])
488            self._voidplace.set_user_data(None)
489            self._voidplace.destroy()
490            self._voidplace = None
491    def hide_voidplace(self):
492        '''
493        Hide voidplace
494        '''
495        self._voidplace.hide()
496    def show_voidplace(self):
497        '''
498        Show Voidplace
499        '''
500        self._voidplace.show()
501   
502    def unload_voidplace(self):
503        self._voidplace.set_user_data(None)
504        self._voidplace.destroy()
505        self._voidplace = None
506        self._vp=False
507        self._selected=True
508   
509    def is_voidplace(self):
510        return self._vp
511   
512    def do_expose_event(self, event):
513       
514        gtk.EventBox.do_expose_event(self,event)
515        if self._selected and not self._vp:
516            if self._icon.need_construct():
517                self._icon.construct()
518            self._icon.draw()
519        else:         
520            if not self._icon.need_construct():
521                self._icon.unconstruct()
522
523
524gobject.type_register(NotebookLabel)
Note: See TracBrowser for help on using the browser.