File: GUIFile.py

package info (click to toggle)
forg 0.5.1-7.2
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 548 kB
  • ctags: 737
  • sloc: python: 5,407; xml: 85; makefile: 64
file content (131 lines) | stat: -rw-r--r-- 4,967 bytes parent folder | download | duplicates (5)
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
# GUIFile.py
# $Id: GUIFile.py,v 1.12 2001/07/05 17:16:53 s2mdalle Exp $
# Written by David Allen <mda@idatar.com>
#
# This is the class that describes how files behave when loaded into
# the FORG.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##############################################################################

from Tkinter import *
from gopher import *
from string import *
import Pmw
import re
import Options

import ContentFrame
import GopherResource
import GopherResponse

class GUIFile(ContentFrame.ContentFrame, Frame):
    verbose = None
    
    def __init__(self, parent_widget, parent_object, resp,
                 resource, filename):
        Frame.__init__(self, parent_widget)  # Superclass constructor
        self.resp = resp

        if self.useStatusLabels:
            labeltext = "%s:%d" % (resource.getHost(), int(resource.getPort()))

            if resource.getName() != '' and resource.getLocator() != '':
                label2 = "\"%s\" ID %s" % (resource.getName(),
                                           resource.getLocator())
            else:
                label2 = "    "

            if len(label2) > 50:
                label2 = label2[0:47] + "..."

            Label(self, text=labeltext).pack(expand=0, fill='x')
            Label(self, text=label2).pack(expand=0, fill='x')

        if resp.getTypeCode() != RESPONSE_FILE:
            Label(self, text="This file has been saved in:").pack()
            Label(self, text=filename).pack()
        else:
            self.textwid = Pmw.ScrolledText(self, hscrollmode='dynamic',
                                            vscrollmode='static')
            tw = self.textwid.component('text')
            tw.configure(background='#FFFFFF', foreground='#000000')
                
            self.textwid.component('text').bind('<Button-3>',
                                                parent_object.popupMenu)
            
            self.textwid.pack(expand=1, fill='both')
        return None

    def pack_content(self, *args):
        if Options.program_options.getOption('strip_carraige_returns'):
            # print "Stripping carriage returns..."
            data = replace(self.resp.getData(), "\r", "")
        else:
            data = self.resp.getData()

        if len(data) < 1024:
            self.textwid.settext(data)
        else:
            for index in range(0, len(data), 500):
                self.textwid.insert('end', data[index:index+500])
        return None
    
    def destroy(self, *args):
        self.pack_forget()
        self.textwid.destroy()
        Frame.destroy(self)
        return None

    def find(self, term, caseSensitive=None, lastIdentifier=None):
        """Overrides the function of the same type from ContentFrame"""
        try:
            # This will raise an exception if it's a 'save' type layout
            # where the data isn't displayed to the user.
            tw = self.textwid.component('text')
            print "Component is ", tw
        except:
            # Don't mess with this.  The user can read the entire label, all
            # big bad few lines of it.
            raise Exception, "This window is not searchable."

        if lastIdentifier is None:
            lastIdentifier = '0.0'

        # The variable caseSensitive is true if the search is case sensitive,
        # and false otherwise.  But since tkinter wants to know whether or not
        # the search is case INsensitive, we flip the boolean value, and use
        # it for the 'nocase' keyword arg to the search method.
        csflipped = (not caseSensitive)
        pos = tw.search(pattern=term, forwards=1,
                        nocase=csflipped, index=lastIdentifier,
                        stopindex=END)
        
        if pos:
            # Find the real index of the position returned.
            found_index = tw.index(pos)
        else:
            found_index = None

        print "Found index is \"%s\"" % found_index

        if found_index:
            tw.yview(found_index)
            ending_index = tw.index("%s + %d chars" % (found_index, len(term)))
            # Set the selection to highlight the given word.
            tw.tag_add(SEL, found_index, ending_index)
            return tw.index("%s + 1 chars" % found_index)

        return None