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

Revision 1118, 19.7 kB (checked in by kop-labs, 6 years ago)

Delete event at options added (lack do a subclass of Command

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    def voidplace(self):
224        '''
225        Construct a voidplace
226        '''
227       
228        self._xmp = background_xpm
229        win_tmp = self._entry.window
230        self._voidplace = gtk.gdk.Window(win_tmp,
231                             self._entry.allocation.width ,
232                             self._entry.allocation.height,
233                             gtk.gdk.WINDOW_CHILD,
234                             (gtk.gdk.ENTER_NOTIFY_MASK |
235                              gtk.gdk.LEAVE_NOTIFY_MASK),
236                             gtk.gdk.INPUT_OUTPUT,
237                             'voidplace',
238                             self._entry.allocation.x,self._entry.allocation.y,
239                             self._entry.get_visual(),
240                             self._entry.get_colormap(),
241                             gtk.gdk.Cursor(self._entry.get_display(), gdk.LEFT_PTR),
242                             '', '', True)
243        self._voidplace.set_user_data(self._entry)
244
245        temp_map = gdk.pixmap_colormap_create_from_xpm_d(self._voidplace,
246                                                         self._entry.get_colormap(),
247                                                         None,
248                                                         self._xmp)
249        self._voidplace.set_back_pixmap(temp_map[0], False)
250        c= self._voidplace.get_colormap()
251        color = c.alloc_color(0,65555,0)
252        self._draw_gc = gtk.gdk.GC(self._voidplace, 
253                                   line_width=6, 
254                                   foreground = color )
255        self._voidplace.draw_rectangle(self._draw_gc, True, self._entry.allocation.x, 
256                                   self._entry.allocation.y, self._entry.allocation.width,
257                                   self._entry.allocation.height)
258        self._voidplace.show()
259        self._vp = True     
260    def construct(self):
261        entry = self._entry
262        x,y,w,h = self._entry.allocation
263
264        #Left up
265        win = gtk.gdk.Window(entry.window,
266                             6,
267                             6,
268                             gtk.gdk.WINDOW_CHILD,
269                             (gtk.gdk.ENTER_NOTIFY_MASK |
270                              gtk.gdk.LEAVE_NOTIFY_MASK),
271                             gtk.gdk.INPUT_OUTPUT,
272                             'icon window',
273                             x, y,
274                             entry.get_visual(),
275                             entry.get_colormap(),
276                             gtk.gdk.Cursor(entry.get_display(), gdk.LEFT_PTR),
277                             '', '', True)
278        self._left_up_win = win
279        win.set_user_data(entry) 
280        c = win.get_colormap()
281        color = c.alloc_color(0,0,0)   
282        win.set_background(color)
283
284        #Left down
285        win2 = gtk.gdk.Window(entry.window,
286                             6, 
287                             6,
288                             gtk.gdk.WINDOW_CHILD,
289                             (gtk.gdk.ENTER_NOTIFY_MASK |
290                              gtk.gdk.LEAVE_NOTIFY_MASK),
291                             gtk.gdk.INPUT_OUTPUT,
292                             'icon window',
293                              x, 
294                               y+h-6,
295                             entry.get_visual(),
296                             entry.get_colormap(),
297                             gtk.gdk.Cursor(entry.get_display(), gdk.LEFT_PTR),
298                             '', '', True)
299        self._left_down_win = win2
300        win2.set_user_data(entry)
301        win2.set_background(color)
302
303        #Right up
304        win3 = gtk.gdk.Window(entry.window,
305                             6, 
306                             6,
307                             gtk.gdk.WINDOW_CHILD,
308                             (gtk.gdk.ENTER_NOTIFY_MASK |
309                              gtk.gdk.LEAVE_NOTIFY_MASK),
310                             gtk.gdk.INPUT_OUTPUT,
311                             'icon window',
312                              x+w-6, 
313                               y,
314                             entry.get_visual(),
315                             entry.get_colormap(),
316                             gtk.gdk.Cursor(entry.get_display(), gdk.LEFT_PTR),
317                             '', '', True)
318        self._right_up_win = win3
319        win3.set_user_data(entry)
320        win3.set_background(color)
321       
322        #Right down
323        win4 = gtk.gdk.Window(entry.window,
324                             6, 
325                             6,
326                             gtk.gdk.WINDOW_CHILD,
327                             (gtk.gdk.ENTER_NOTIFY_MASK |
328                              gtk.gdk.LEAVE_NOTIFY_MASK),
329                             gtk.gdk.INPUT_OUTPUT,
330                             'icon window',
331                              x+w-6, 
332                               y+h-6,
333                             entry.get_visual(),
334                             entry.get_colormap(),
335                             gtk.gdk.Cursor(entry.get_display(), gdk.LEFT_PTR),
336                             '', '', True)
337        self._right_down_win = win4
338        win4.set_user_data(entry)
339       
340        win4.set_background(color)
341       
342    def resize(self):
343       
344        x,y,w,h = self._entry.allocation
345        self._left_down_win.move(x, y+h-6)
346        self._left_up_win.move(x, y)
347        self._right_down_win.move(x+w-6, y)
348        self._right_up_win.move(x+w-6, y+h-6)
349       
350       
351    def draw(self):
352        self.resize()
353        x,y,w,h = self._entry.allocation
354        win = self._left_down_win
355        c = win.get_colormap()
356        color = c.alloc_color(0,0,0)   
357        color = gtk.gdk.GC(win, 
358                                   line_width=6, 
359                                   foreground = color )
360        win.draw_rectangle(color, True, 0, 0, 6, 6)
361        win.show()
362
363        win2 = self._left_up_win
364        c = win2.get_colormap()
365        color = c.alloc_color(0,0,0)   
366        color = gtk.gdk.GC(win2, line_width=6, foreground = color )
367        win2.draw_rectangle(color, True, 0, 0, 6, 6)
368        win2.show() 
369
370        win2 = self._right_down_win
371        c = win2.get_colormap()
372        color = c.alloc_color(0,0,0)   
373        color = gtk.gdk.GC(win2, line_width=6, foreground = color )
374        win2.draw_rectangle(color, True, 0, 0, 6, 6)
375        win2.show() 
376       
377        win2 = self._right_up_win
378        c = win2.get_colormap()
379        color = c.alloc_color(0,0,0)   
380        color = gtk.gdk.GC(win2, line_width=6, foreground = color )
381        win2.draw_rectangle(color, True, 0, 0, 6, 6)
382        win2.show() 
383       
384class SpecialEntry(gtk.Entry):
385    def __init__(self):
386        gtk.Entry.__init__(self)
387        self._icon = SpecialWrapperEntry(self)
388    def do_realize(self):
389        gtk.Entry.do_realize(self)
390        self._icon.construct() 
391    def do_expose_event(self,event):
392        gtk.Entry.do_expose_event(self, event)
393        self._icon.draw()
394       
395gobject.type_register(SpecialEntry)
396
397from Voidplace import Voidplace
398
399class SpecialVoidplace(Voidplace):
400    def __init__(self):
401        Voidplace.__init__(self)
402        self._icon = SpecialWrapperEntry(self)
403    def do_realize(self):
404        Voidplace.do_realize(self)
405        self._icon.construct() 
406    def do_expose_event(self,event):
407        Voidplace.do_expose_event(self, event)
408        self._icon.draw()
409       
410gobject.type_register(SpecialVoidplace)
411
412class SpecialHBox(gtk.HBox):
413    def __init__(self):
414        gtk.HBox.__init__(self)
415        self._icon = None
416        self._icon_exists = False 
417        self.selected = False
418    #Private API
419    def _button_press(self, widget, event, other):
420        if not self._icon_exists:
421           
422            self._icon.construct()
423       
424        #self._icon.draw()
425        self.emit('button-press-event', event)
426   
427    def _key_press(self, widget, event, other):
428        self.emit('key-press-event', event)
429   
430    #Public API
431    def do_realize(self):
432        gtk.HBox.do_realize(self)
433        self._icon = SpecialWrapperGeneric(self)
434        self._icon.construct() 
435        self._icon_exists= True 
436    def do_voidplace(self):
437        self._icon.voidplace()
438    def do_draw(self):
439        if self._icon!= None:
440            self._icon.draw()
441    def do_size_allocate(self, allocation):
442        gtk.HBox.do_size_allocate(self,allocation)
443    def do_expose_event(self,event):
444        gtk.HBox.do_expose_event(self, event)
445
446        #self._icon.draw()
447    def do_button_press_event(self, event):
448        if self._icon!= None and self._icon_exists:
449            self._icon.draw()
450   
451    def set_select(self, value):
452        self.selected = value
453        if not value:
454            self._icon.unconstruct()
455            self._icon_exists = False
456    def pack_start(self, child, expand=True, fill=True):
457        gtk.HBox.pack_start(self, child, expand, fill)
458        child.connect('button-press-event', self._button_press, None)
459        child.connect('key-press-event', self._key_press, None)
460       
461gobject.type_register(SpecialHBox)
462
463class NotebookLabel(gtk.EventBox):
464    '''
465    Label to Notebook with nodes and borders when selectable
466    '''
467    def __init__(self, title = ''):
468        gtk.EventBox.__init__(self)
469        self.label = gtk.Label(title)
470        self.add(self.label)
471        self._icon = SpecialWrapperEntry(self)
472        self._selected = None
473        self._vp = False 
474    def set_text(self, text):
475        self.label.set_text(text)
476    def get_text(self):
477        return self.label.get_text()
478   
479    def set_select(self, value):
480        self._selected = value
481   
482    def do_realize(self):
483
484       
485        gtk.EventBox.do_realize(self)
486        self.set_flags(self.flags() | gtk.REALIZED)
487        self._icon.construct()
488    def voidplace(self):
489        '''
490        Construct a voidplace
491        '''
492       
493        self._xmp = background_xpm
494        win_tmp = self.get_parent_window()
495        self._voidplace = gtk.gdk.Window(win_tmp,
496                             self.allocation.width ,
497                             self.allocation.height,
498                             gtk.gdk.WINDOW_CHILD,
499                             (gtk.gdk.ENTER_NOTIFY_MASK |
500                              gtk.gdk.LEAVE_NOTIFY_MASK),
501                             gtk.gdk.INPUT_OUTPUT,
502                             'voidplace',
503                             self.allocation.x,self.allocation.y,
504                             self.get_visual(),
505                             self.get_colormap(),
506                             gtk.gdk.Cursor(self.get_display(), gdk.LEFT_PTR),
507                             '', '', True)
508        self._voidplace.set_user_data(self)
509        self.style.attach(self.window)
510        temp_map = gdk.pixmap_colormap_create_from_xpm_d(self._voidplace,
511                                                         self.get_colormap(),
512                                                         None,
513                                                         self._xmp)
514        self._voidplace.set_back_pixmap(temp_map[0], False)
515        c= self._voidplace.get_colormap()
516        color = c.alloc_color(0,65555,0)
517        self._draw_gc = gtk.gdk.GC(self._voidplace, 
518                                   line_width=6, 
519                                   foreground = color )
520        self._voidplace.draw_rectangle(self._draw_gc, True, self.allocation.x, 
521                                   self.allocation.y, self.allocation.width,
522                                   self.allocation.height)
523        self._voidplace.show()
524        self._vp = True 
525    def unload(self):
526        if not self._icon.need_construct():
527            self._icon.unconstruct()
528        if self._vp:
529            #self._voidplace.set_background(self.style.base[gtk.STATE_NORMAL])
530            self._voidplace.set_user_data(None)
531            self._voidplace.destroy()
532            self._voidplace = None
533    def hide_voidplace(self):
534        '''
535        Hide voidplace
536        '''
537        self._voidplace.hide()
538    def show_voidplace(self):
539        '''
540        Show Voidplace
541        '''
542        self._voidplace.show()
543   
544    def unload_voidplace(self):
545        self._voidplace.set_user_data(None)
546        self._voidplace.destroy()
547        self._voidplace = None
548        self._vp=False
549        self._selected=True
550   
551    def is_voidplace(self):
552        return self._vp
553   
554    def do_expose_event(self, event):
555       
556        gtk.EventBox.do_expose_event(self,event)
557        if self._selected and not self._vp:
558            if self._icon.need_construct():
559                self._icon.construct()
560            self._icon.draw()
561        else:         
562            if not self._icon.need_construct():
563                self._icon.unconstruct()
564
565
566gobject.type_register(NotebookLabel)
Note: See TracBrowser for help on using the browser.