Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/infoslicer/widgets/Readonly_Textbox.py
blob: 958cfcd994c8b86bfc4fbdf59f83ac8a4661821f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Copyright (C) IBM Corporation 2008 
import pygtk
pygtk.require('2.0')
import gtk
import pango
import cPickle
from Textbox import Textbox

SELECT_SENTENCE, SELECT_PARAGRAPH, SELECT_SECTION, FULL_EDIT = range(4)

class Readonly_Textbox( Textbox ):    
    """
    Created by Jonathan Mace
    This class implements its own special code for dragging and selecting.
    It has an article class which provides the text buffer, and any modifications
    to the text buffer are done via the article class.
    This class is read-only, so it is not editable and will not act as a drag
    destination.
    """
    
    def __init__(self, use_as_drag_source = True):
        Textbox.__init__(self)
        self.selecting = False
        self.use_as_drag_source = use_as_drag_source
        self.set_mode(SELECT_SENTENCE)
        self.block = True
        self.modify_font(pango.FontDescription('arial 9'))
        
    
    def set_mode(self, mode):
        self.selectionmode = mode
        self.disconnect_handlers()
        if mode == SELECT_SENTENCE: self.__set_select_mode()
        elif mode == SELECT_PARAGRAPH: self.__set_select_mode()
        elif mode == SELECT_SECTION: self.__set_select_mode()
        else: pass
        
    def __set_select_mode(self):
        if self.use_as_drag_source == True:
            self.event_handlers.append(self.connect("button-press-event", self.clicked_event, None))
            self.event_handlers.append(self.connect("motion-notify-event", self.motion_notify, None))
            self.event_handlers.append(self.connect("move-cursor", self.move_cursor, None))
            self.event_handlers.append(self.connect("button-release-event", self.unclicked_event, None))
            self.event_handlers.append(self.connect("drag_data_get", self.drag_data_get_event, None))
        self.event_handlers.append(self.connect("drag-motion", self.drag_motion, None))
        
    def drag_motion(self, widget, context, x, y, timestamp, data):
        context.drag_status(gtk.gdk.ACTION_COPY, timestamp)
        return True
        
    def clicked_event(self, widget, event, data):
        if event.type == gtk.gdk._2BUTTON_PRESS or event.type == gtk.gdk._3BUTTON_PRESS:
            self.stop_emission("button_press_event")
            return
        if event.button == 3:
            self.stop_emission("button_press_event")
            return
        if not self.get_buffer().get_has_selection():
            result = self.do_button_press_event(widget, event)
            
            a = self.article
            loc_iter = self.get_mouse_iter(int(event.x), int(event.y))
            
            self.selecting = True
            self.selectionstart = loc_iter
            self.stop_emission("button-press-event")
            return result
        else:
            buf = self.get_buffer()
            bounds = buf.get_selection_bounds()
            if bounds == ():
                return
            start = bounds[0]
            end = bounds[1]
            if start.compare(end) == 1:
                start, end = end, start
            loc = self.get_mouse_iter(int(event.x), int(event.y))
            if start.compare(loc) == 1 or loc.compare(end) == 1:
                self.do_button_press_event(widget, event)
                a = self.article
                self.selecting = True
                self.selectionstart = loc
                self.stop_emission("button-press-event")
            
    def move_cursor(self, widget, stepsize, count, extend, data):
        if self.selecting:
            result = self.do_move_cursor(widget, event)
            self.stop_emission("move-cursor")
            return result
    
    def motion_notify(self, widget, event, data):
        if self.selecting:
            """ The following code implements the 'snapping' to sentences etc.
            
             The base class responds to motion notify events and does some unknown (to me)
             action which for some reason, must complete, otherwise on some platforms it will
             stop any more motion notify events.
            
             So what happens, is the first 'run through' of the motion notify responder emits another
             motion notify event, ignores it and lets the base class respond to it.  Then when control
             is given back to the first emission, we implement our code.  The order of events is:
            
            1) motion notify event 1 emitted naturally
            2)    our class responds to motion notify event 1
            3)        motion notify event 2 emitted by step 2)
            4)            our class ignores motion notify event 2
            5)            the default class acts upon motion notify event 2
            6)        motion notify event 2 finishes naturally
            7)    our class does its stuff
            8) motion notify event 1 finishes by our class stopping its emission
            
            """
            
            if self.block == True:
                self.stop_emission("motion-notify-event")
                self.block = False
                self.emit("motion-notify-event", event)
                
                buf = self.get_buffer()            
                mouseiter = self.get_mouse_iter(int(event.x), int(event.y))
                article = self.get_article()
                if mouseiter.compare(self.selectionstart) == 1:
                    if self.selectionmode == SELECT_SENTENCE: 
                        selectionstart = article.getSentence(self.selectionstart).getStart()
                        selectionend = article.getSentence(mouseiter).getEnd()
                    if self.selectionmode == SELECT_PARAGRAPH:
                        selectionstart = article.getParagraph(self.selectionstart).getStart()
                        selectionend = article.getParagraph(mouseiter).getEnd()
                    if self.selectionmode == SELECT_SECTION:
                        selectionstart = article.getSection(self.selectionstart).getStart()
                        selectionend = article.getSection(mouseiter).getEnd()
                else:
                    if self.selectionmode == SELECT_SENTENCE: 
                        selectionstart = article.getSentence(mouseiter).getStart()
                        selectionend = article.getSentence(self.selectionstart).getEnd()
                    if self.selectionmode == SELECT_PARAGRAPH:
                        selectionstart = article.getParagraph(mouseiter).getStart()
                        selectionend = article.getParagraph(self.selectionstart).getEnd()
                    if self.selectionmode == SELECT_SECTION:
                        selectionstart = article.getSection(mouseiter).getStart()
                        selectionend = article.getSection(self.selectionstart).getEnd()
                self.scroll_to_iter(mouseiter, 0)
                article.highlight(selectionstart, selectionend)                
                    
            else:
                self.block = True
            
    def unclicked_event(self, widget, event, data):
        self.article.clearArrow()
        self.do_button_release_event(widget, event)
        self.selecting = False
        self.stop_emission("button-release-event")
        
    def drag_data_get_event(self, widget, context, selection_data, info, time, data):
        
        a = self.article
        
        if self.selectionmode == SELECT_SENTENCE:
            atom = gtk.gdk.atom_intern("sentence")
        if self.selectionmode == SELECT_PARAGRAPH:
            atom = gtk.gdk.atom_intern("paragraph")
        if self.selectionmode == SELECT_SECTION:
            atom = gtk.gdk.atom_intern("section")
            
        string = cPickle.dumps(a.getSelection())
        selection_data.set(atom, 8, string)
        self.stop_emission("drag-data-get")
        self.set_editable(False)