~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/cursing/FileSelectPad.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This file is part of GNU Enterprise.
 
3
#
 
4
# GNU Enterprise is free software; you can redistribute it
 
5
# and/or modify it under the terms of the GNU General Public
 
6
# License as published by the Free Software Foundation; either
 
7
# version 2, or (at your option) any later version.
 
8
#
 
9
# GNU Enterprise is distributed in the hope that it will be
 
10
# useful, but WITHOUT ANY WARRANTY; without even the implied
 
11
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
12
# PURPOSE. See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public
 
15
# License along with program; see the file COPYING. If not,
 
16
# write to the Free Software Foundation, Inc., 59 Temple Place
 
17
# - Suite 330, Boston, MA 02111-1307, USA.
 
18
#
 
19
# Copyright 2002-2005 Free Software Foundation
 
20
#
 
21
# FILE:
 
22
# FileSelectPad.py
 
23
#
 
24
# DESCRIPTION:
 
25
#
 
26
# NOTES:
 
27
#
 
28
 
 
29
 
 
30
import os
 
31
import string
 
32
import curses
 
33
 
 
34
from constants import *
 
35
from Control import Control
 
36
from ScrollBar import ScrollBar
 
37
 
 
38
class FileSelectPad(Control):
 
39
  """
 
40
  """
 
41
  __prefix_width__ = 6
 
42
  __threshhold__   = 250
 
43
 
 
44
  def __init__(self, Root, Y, X,H, W, **properties):
 
45
    Control.__init__(self, Root, '', properties)
 
46
    self.Y = Y
 
47
    self.ROWS = H
 
48
    self.X = X
 
49
    self.H = H
 
50
    self.W = W
 
51
    self.PARENT = Root
 
52
    self.X = self.X
 
53
    self.Y = self.Y
 
54
    self._W = self.W
 
55
    self._H = self.H
 
56
    self.MYROOT = Root
 
57
    self.NAME = 'fileselectpad'
 
58
    self.SetMethod("SYSRUN",self.Run)
 
59
    self.SetMethod("GOTFOCUS",self.GotFocus)
 
60
    self.SetMethod("LOSTFOCUS",self.LostFocus)
 
61
#    self.SetMethod("SYSPAINT",self.Paint)
 
62
    self.SetMethod("PAINT",self.Paint)
 
63
    self.CANGETFOCUS = 1
 
64
    self.COLWIDTH = 25
 
65
    self.COLDISP = W / self.COLWIDTH
 
66
    self.pad = None
 
67
    self.hilit = 1
 
68
    self.xoffset = -1
 
69
    self.yoffset = 0
 
70
    self.SetColor(1)
 
71
    self.LoLight()
 
72
    self.dirlist = []
 
73
    self.sb = None
 
74
    self.changed = 1
 
75
    self.__first = 1
 
76
 
 
77
  def Run(self,v1,v2,v3):
 
78
    rows = self.ROWS
 
79
    if v1 :
 
80
      self.__SetHilit(v2,v3)
 
81
      if self.sb != None:
 
82
         self.sb.Set(self.hilit / rows)
 
83
      if self.dirlist[self.hilit-1][:1] != 'D':
 
84
        active = self.GetMethod("ACTIVE")
 
85
        if active != None and self.Verify(None,None,None):
 
86
          apply(active,(self,self.dirlist[self.hilit-1][self.__prefix_width__:],None))
 
87
      action = self.GetMethod("ACTION")
 
88
      if action != None and self.Verify(None,None,None):
 
89
        if self.path[-1:] != '/':
 
90
          self.path += '/'
 
91
        apply(action,(self,self.path + self.dirlist[self.hilit-1][self.__prefix_width__:],None))
 
92
      self.Paint(0,0,0)
 
93
    Container = self.SCREEN
 
94
    while 1:
 
95
      self.__DrawItem(self.hilit,1)
 
96
      if self.active != 1:
 
97
        return 0
 
98
      ch = Container.GetChar()
 
99
      if self.PARENT.BreakOrder(ch) :
 
100
        return 
 
101
      if ch in (Container.TokNextField,):
 
102
        return
 
103
      elif ch == Container.TokActivate:
 
104
        action = self.GetMethod("ACTION")
 
105
        if action != None and self.Verify(None,None,None):
 
106
          if self.path[-1:] != '/':
 
107
            self.path += '/'
 
108
          apply(action,(self,self.path + self.dirlist[self.hilit-1][self.__prefix_width__:], None))
 
109
      elif ch == Container.TokDownArrow:
 
110
        self.hilit += 1
 
111
        if self.Verify(None,None,None) and self.sb != None:
 
112
          col = (self.hilit) / self.ROWS
 
113
          self.sb.Set(col-1)
 
114
      elif ch == Container.TokUpArrow:
 
115
        self.hilit -= 1
 
116
        if self.Verify(None,None,None) and self.sb != None:
 
117
          col = (self.hilit) / self.ROWS
 
118
          self.sb.Set(col-1)
 
119
      elif ch == Container.TokLeftArrow:
 
120
        self.hilit -= rows
 
121
        if self.Verify(None,None,None) and self.sb != None:
 
122
          self.sb.Dec(None,None,None)
 
123
      elif ch == Container.TokRightArrow:
 
124
        self.hilit += rows
 
125
        if self.Verify(None,None,None) and self.sb != None:
 
126
          self.sb.Inc(None,None,None)
 
127
      if self.hilit > len(self.dirlist):
 
128
        self.hilit = len(self.dirlist)
 
129
      elif self.hilit <= 0:
 
130
        self.hilit = 1
 
131
      active = self.GetMethod("ACTIVE")
 
132
      if active != None and self.Verify(None,None,None):
 
133
        apply(active,(self,self.dirlist[self.hilit-1][self.__prefix_width__:],None))
 
134
      self.Paint(0,0,0)
 
135
 
 
136
  def GotFocus(self,v1,v2,v3):
 
137
    self.X = self.X
 
138
    self.Y = self.Y
 
139
    self._W = self.W
 
140
    self._H = self.H
 
141
    self.FOCUS = 1
 
142
    self.Paint(None,None,None)
 
143
    return 1
 
144
 
 
145
  def LostFocus(self,v1,v2,v3):
 
146
    self.FOCUS = 0
 
147
    self.Paint(None,None,None)
 
148
    return 1
 
149
 
 
150
  def Display(self, path):
 
151
    if self.pad != None:
 
152
      self.pad.erase()
 
153
      self.hilit = 1
 
154
#      self.Paint(0,0,0)
 
155
    if os.access(path, os.R_OK | os.X_OK) and os.path.isdir(path):
 
156
      self.path = path
 
157
      self.dirlist = os.listdir(path)
 
158
      self.__SortAndMarkDirList()
 
159
      count = len(self.dirlist)
 
160
      rows = self.H
 
161
      colwidth = self.COLWIDTH
 
162
      if (count) * colwidth > self.__area():
 
163
        rows -= 1
 
164
        setupSB = 1
 
165
      else:
 
166
        setupSB = 0
 
167
      self.ROWS = rows
 
168
      cols = ((count / rows) * colwidth)
 
169
      self.pad = curses.newpad(rows+1,cols+((self.COLDISP+1) * (colwidth)))
 
170
      self.__initScrollBar( setupSB, (count / rows) )
 
171
      if curses.has_colors():
 
172
        self.pad.bkgdset(' ',curses.color_pair(1))
 
173
      self.pad.erase()
 
174
      self.fillpad(1)
 
175
      self.Paint(None,None,None)
 
176
      self.SCREEN.Refresh()
 
177
      self.__first = 0
 
178
 
 
179
  def __initScrollBar(self, activate, cols):
 
180
    if activate :
 
181
      if self.sb != None:
 
182
        self.sb.Init(cols)
 
183
      else:
 
184
        self.sb = ScrollBar(self.PARENT,'filesb',self.H+3,2, self.W)
 
185
        self.PARENT.AddControl(self.sb)
 
186
        self._cols = cols
 
187
        self.sb.Init(cols)
 
188
        self.PARENT.Refresh(None,None,None)
 
189
        self.sb.SetMethod("CHANGED", self.SetCol)
 
190
    elif self.sb != None:
 
191
      self.PARENT.DelControl(self.sb)
 
192
      self.sb.__del__()
 
193
      self.PARENT.Refresh(None,None,None)
 
194
      self.sb = None
 
195
 
 
196
  def SetCol(self,newCol,MaxCol,arg3):
 
197
    self.__DrawItem(self.hilit,1)
 
198
    rows = self.ROWS
 
199
    if newCol <= self._cols and newCol >= 0:
 
200
      self.hilit = (rows * (newCol)) + self.yoffset + 1
 
201
      if self.hilit < 0:
 
202
        self.hilit = 0
 
203
      elif self.hilit > len(self.dirlist):
 
204
        self.hilit = len(self.dirlist)
 
205
      if self.dirlist[self.hilit-1][:1] != 'D':
 
206
        active = self.GetMethod("ACTIVE")
 
207
        if active != None and self.Verify(None,None,None):
 
208
          apply(active,(self,self.dirlist[self.hilit-1][self.__prefix_width__:],None))
 
209
      self.Paint(None, None,None)
 
210
      self.PARENT.Screen().Refresh()
 
211
 
 
212
  def __SortAndMarkDirList(self):
 
213
    path = self.path
 
214
    if path[-1:] != '/':
 
215
      path = self.path + '/'
 
216
    dirSort = [ [ not os.path.isdir(path + x), x ] for x in self.dirlist ]
 
217
    dirSort.sort
 
218
    dirs  = []
 
219
    files = []
 
220
    for i in dirSort:
 
221
      if i[0]==0:
 
222
        i[1] = string.ljust('D',self.__prefix_width__) + i[1]
 
223
        dirs.append(i[1])
 
224
      else:
 
225
        i[1] = string.ljust('',self.__prefix_width__)+i[1]
 
226
        files.append(i[1])
 
227
    dirs.sort()
 
228
    files.sort()
 
229
    dirs.extend(files)
 
230
    self.dirlist = dirs
 
231
    for i in range(0,len(self.dirlist)):
 
232
      if os.path.islink(path + self.dirlist[i][self.__prefix_width__:]):
 
233
        inter = self.dirlist[i][:1] + 'l' + self.dirlist[i][2:]
 
234
        self.dirlist[i] = inter
 
235
      mod = os.R_OK
 
236
      if  os.access( path + self.dirlist[i][self.__prefix_width__:], mod ):
 
237
        self.dirlist[i] = self.dirlist[i][:2] + 'r' + self.dirlist[i][3:]
 
238
      else:
 
239
        self.dirlist[i] = self.dirlist[i][:2] + '-' + self.dirlist[i][3:]
 
240
      mod = os.W_OK
 
241
      if  os.access( path + self.dirlist[i][self.__prefix_width__:], mod ):
 
242
        self.dirlist[i] = self.dirlist[i][:3] + 'w' + self.dirlist[i][4:]
 
243
      else:
 
244
        self.dirlist[i] = self.dirlist[i][:3] + '-' + self.dirlist[i][4:]
 
245
      mod |= os.X_OK
 
246
      if  os.access( path + self.dirlist[i][self.__prefix_width__:], mod ):
 
247
        self.dirlist[i] = self.dirlist[i][:4] + 'x' + self.dirlist[i][5:]
 
248
      else:
 
249
        self.dirlist[i] = self.dirlist[i][:4] + '-' + self.dirlist[i][5:]
 
250
  def __area(self):
 
251
    return self.H* self.W
 
252
 
 
253
  def Paint(self,arg1,arg2,arg3):
 
254
    if self.pad != None and self.__first == 0:
 
255
      self.__DrawItem(self.hilit,3)
 
256
      rows = self.ROWS
 
257
      colwidth = self.COLWIDTH
 
258
      self.yoffset = (self.hilit-1) % rows
 
259
      newXoffset = ((self.hilit-1) / rows ) * colwidth
 
260
      self.xoffset = newXoffset
 
261
      self.pad.refresh(0, self.xoffset,self.Y,self.X,
 
262
        self.Y + self._H,self.X + self._W)
 
263
    
 
264
  def fillpad(self, showProgress = 0):
 
265
    rows = self.ROWS
 
266
    xoffset = 0
 
267
    yoffset = 0
 
268
    step = 0
 
269
    pb = None
 
270
    if showProgress :
 
271
      pb = self.PROGRESS
 
272
    if pb != None and len(self.dirlist) > self.__threshhold__:
 
273
      pb.DeActivate(1)
 
274
      pb.Init(len(self.dirlist))
 
275
      step = 1
 
276
    for i in range(1,len(self.dirlist)+1):
 
277
      color = 1
 
278
      if step :
 
279
        pb.Step()
 
280
      if i == self.hilit and self.FOCUS:
 
281
        color = 3
 
282
      elif self.dirlist[i-1][2] != 'r':
 
283
        color = 4
 
284
      self.__DrawItem(i,color)
 
285
      if i % rows == 0:
 
286
        xoffset += self.COLWIDTH
 
287
        yoffset = 0
 
288
      else:
 
289
        yoffset += 1
 
290
    if step :
 
291
      pb.Init(1)
 
292
      pb.DeActivate(0)
 
293
    self.changed = 1
 
294
 
 
295
  def __DrawItem(self,id,col):
 
296
    if self.pad != None and len(self.dirlist)>0:
 
297
      rows = self.ROWS
 
298
      hilitCol = ((id-1) / rows) * self.COLWIDTH
 
299
      hilitRow = (id-1) % rows
 
300
      mods = 0
 
301
      self.pad.addstr(hilitRow,hilitCol,'')
 
302
      String = str(self.dirlist[id-1][:self.COLWIDTH-2])
 
303
      if String[2]!='r' and col == 1:
 
304
        col = 4
 
305
      if curses.has_colors():
 
306
        mods = curses.color_pair(col)
 
307
      for j in range(0,len(String)):
 
308
        if ord(String[j]) == tiHLINE:
 
309
          self.pad.addch(curses.ACS_HLINE, mods)
 
310
        elif ord(String[j]) == tiVLINE:
 
311
          self.pad.addch(curses.ACS_VLINE, mods)
 
312
        elif ord(String[j]) == tiBOX:
 
313
          self.pad.addch(curses.ACS_BOARD, mods)
 
314
        else:
 
315
          self.pad.addstr(str(String[j]), mods)
 
316
  def __SetHilit(self, YPOS, XPOS):
 
317
    self.__DrawItem(self.hilit,1)
 
318
    colNo = (self.xoffset + (XPOS-self.X)) / self.COLWIDTH
 
319
    self.hilit = ( colNo * self.ROWS) + (YPOS-self.Y) + 1
 
320
    if self.hilit < 1:
 
321
      self.hilit = 1
 
322
    if self.hilit > len(self.dirlist):
 
323
      self.hilit = len(self.dirlist)
 
324
    self.__DrawItem(self.hilit,3)
 
325
 
 
326
  def Verify(self, arg1, arg2, arg3):
 
327
    return self.hilit > 0 and self.hilit <= len(self.dirlist)
 
328
 
 
329