1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 # See the COPYING file for license information.
17 # Copyright (c) 2006 Guillaume Chazarain <guichaz@yahoo.fr>
22 assert gtk
.pygtk_version
>= (2, 8)
25 def __init__(self
, x
, y
):
29 def _event_point(event
):
31 x
, y
, state
= event
.window
.get_pointer()
34 point
= _point(event
.x
, event
.y
)
37 class PysizeWidget_Mouse(object):
38 def __init__(self
, options
, args
):
39 self
.connect('motion-notify-event', type(self
)._motion
_notify
_event
)
40 self
.connect('button-press-event', type(self
)._button
_press
_event
)
41 self
.connect('button-release-event', type(self
)._button
_release
_event
)
42 self
.connect('building-tree-state-changed', type(self
)._reset
_mouse
)
43 self
.cursor_node
= None
44 self
.button_press_node
= None
45 self
.set_cursor_node(None)
47 def set_cursor_node(self
, cursor_node
):
48 self
.cursor_node
= cursor_node
49 self
.emit('hover-changed', cursor_node
)
51 def _reset_mouse(self
, unused_tree
):
52 self
.set_cursor_node(None)
53 self
.button_press_node
= None
55 def _get_node_here(self
, event
):
56 point
= _point(event
.x
, event
.y
)
57 for node
in self
.tree
.root
:
58 if node
.contains_point(point
):
61 def _motion_notify_event(self
, event
):
62 point
= _event_point(event
)
63 prev_selection
= new_selection
= self
.cursor_node
64 if not (self
.cursor_node
and self
.cursor_node
.contains_point(point
)):
65 # The cursor is no more in the same node
67 for node
in self
.tree
.root
:
68 if node
.contains_point(point
) != (node
== prev_selection
):
69 if node
== prev_selection
:
77 if prev_selection
!= new_selection
:
78 self
.set_cursor_node(new_selection
)
79 self
.queue_node_redraw(prev_selection
)
80 self
.queue_node_redraw(new_selection
)
83 def _handle_double_click(self
, node
):
85 self
.set_paths(node
.get_fullpaths())
86 return node
is not None
88 def _button_press_event(self
, event
):
90 node
= self
._get
_node
_here
(event
)
91 if event
.type == gtk
.gdk
._2BUTTON
_PRESS
:
92 return self
._handle
_double
_click
(node
)
93 if node
!= self
.button_press_node
:
94 self
.queue_node_redraw(self
.button_press_node
)
95 self
.queue_node_redraw(node
)
96 self
.button_press_node
= node
100 def _button_release_event(self
, event
):
101 if event
.button
== 1:
102 node
= self
._get
_node
_here
(event
)
103 if node
== self
.button_press_node
:
104 if event
.state
& gtk
.gdk
.CONTROL_MASK
:
106 node_fullpaths
= set(node
.get_fullpaths())
107 if node_fullpaths
<= self
.selected_paths
:
108 self
.selected_paths
-= node_fullpaths
110 self
.selected_paths |
= node_fullpaths
112 for n
in self
.get_selected_nodes():
113 self
.queue_node_redraw(n
)
115 self
.selected_paths
= set(node
.get_fullpaths())
117 self
.selected_paths
= set()
118 self
.queue_node_redraw(node
)
120 self
.queue_node_redraw(self
.button_press_node
)
121 self
.button_press_node
= None
124 def get_selected_nodes(self
):
126 cursor_fullpaths
= set(self
.cursor_node
.get_fullpaths())
128 cursor_fullpaths
= set()
129 sel
= self
.selected_paths | cursor_fullpaths
130 nodes
= [n
for n
in self
.tree
.root
if set(n
.get_fullpaths()) <= sel
]