Class: CDK::FSELECT

Inherits:
CDKOBJS show all
Defined in:
lib/cdk/fselect.rb

Instance Attribute Summary collapse

Attributes inherited from CDKOBJS

#BXAttr, #HZChar, #LLChar, #LRChar, #ULChar, #URChar, #VTChar, #accepts_focus, #binding_list, #border_size, #box, #exit_type, #has_focus, #is_visible, #screen, #screen_index

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CDKOBJS

#SCREEN_XPOS, #SCREEN_YPOS, #bind, #bindableObject, #checkBind, #cleanBindings, #cleanTitle, #drawTitle, #getBox, #getc, #getch, #isBind, #move_specific, #refreshData, #saveData, #setBXattr, #setBackgroundColor, #setBox, #setExitType, #setHZchar, #setLLchar, #setLRchar, #setPostProcess, #setPreProcess, #setTitle, #setULchar, #setURchar, #setVTchar, #unbind, #validCDKObject, #validObjType

Constructor Details

#initialize(cdkscreen, xplace, yplace, height, width, title, label, field_attribute, filler_char, highlight, d_attribute, f_attribute, l_attribute, s_attribute, box, shadow) ⇒ FSELECT

Returns a new instance of FSELECT.



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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/cdk/fselect.rb', line 10

def initialize(cdkscreen, xplace, yplace, height, width, title, label,
    field_attribute, filler_char, highlight, d_attribute, f_attribute,
    l_attribute, s_attribute, box, shadow)
  super()
  parent_width = cdkscreen.window.getmaxx
  parent_height = cdkscreen.window.getmaxy
  bindings = {
      CDK::BACKCHAR => Ncurses::KEY_PPAGE,
      CDK::FORCHAR  => Ncurses::KEY_NPAGE,
  }

  self.setBox(box)

  # If the height is a negative value the height will be ROWS-height,
  # otherwise the height will be the given height
  box_height = CDK.setWidgetDimension(parent_height, height, 0)

  # If the width is a negative value, the width will be COLS-width,
  # otherwise the width will be the given width.
  box_width = CDK.setWidgetDimension(parent_width, width, 0)

  # Rejustify the x and y positions if we need to.
  xtmp = [xplace]
  ytmp = [yplace]
  CDK.alignxy(cdkscreen.window, xtmp, ytmp, box_width, box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Make sure the box isn't too small.
  box_width = [box_width, 15].max
  box_height = [box_height, 6].max

  # Make the file selector window.
  @win = Ncurses::WINDOW.new(box_height, box_width, ypos, xpos)

  # is the window nil?
  if @win.nil?
    fselect.destroy
    return nil
  end
  @win.keypad(true)

  # Set some variables.
  @screen = cdkscreen
  @parent = cdkscreen.window
  @dir_attribute = d_attribute.clone
  @file_attribute = f_attribute.clone
  @link_attribute = l_attribute.clone
  @sock_attribute = s_attribute.clone
  @highlight = highlight
  @filler_character = filler_char
  @field_attribute = field_attribute
  @box_height = box_height
  @box_width = box_width
  @file_counter = 0
  @pwd = ''
  @input_window = @win
  @shadow = shadow
  @shadow_win = nil

  # Get the present working directory.
  # XXX need error handling (set to '.' on error)
  @pwd = Dir.getwd

  # Get the contents of the current directory
  self.setDirContents

  # Create the entry field in the selector
  label_len = []
  CDK.char2Chtype(label, label_len, [])
  label_len = label_len[0]

  temp_width = if CDK::FSELECT.isFullWidth(width)
               then CDK::FULL
               else box_width - 2 - label_len
               end
  @entry_field = CDK::ENTRY.new(cdkscreen, @win.getbegx, @win.getbegy,
      title, label, field_attribute, filler_char, :MIXED, temp_width,
      0, 512, box, false)

  # Make sure the widget was created.
  if @entry_field.nil?
    self.destroy
    return nil
  end

  # Set the lower left/right characters of the entry field.
  @entry_field.setLLchar(Ncurses::ACS_LTEE)
  @entry_field.setLRchar(Ncurses::ACS_RTEE)

  # This is a callback to the scrolling list which displays information
  # about the current file.  (and the whole directory as well)
  display_file_info_cb = lambda do |object_type, entry, fselect, key|
    # Get the file name.
    filename = fselect.entry_field.info

    # Get specific information about the files.
    # lstat (filename, &fileStat);
    file_stat = File.stat(filename)

    # Determine the file type
    filetype = case
               when file_stat.symlink?
                 'Symbolic Link'
               when file_stat.socket?
                 'Socket'
               when file_stat.file?
                 'Regular File'
               when file_stat.directory?
                 'Directory'
               when file_stat.chardev?
                 'Character Device'
               when file_stat.blockdev?
                 'Block Device'
               when file_stat.ftype == 'fif'
                 'FIFO Device'
               else
                 'Unknown'
               end

    # Get the user name and group name.
    pw_ent = Etc.getpwuid(file_stat.uid)
    gr_ent = Etc.getgrgid(file_stat.gid)

    # Convert the mode to both string and int
    # intMode = mode2Char (stringMode, fileStat.st_mode);

    # Create the message.
    mesg = [
        'Directory  : </U>%s' % [fselect.pwd],
        'Filename   : </U>%s' % [filename],
        'Owner      : </U>%s<!U> (%d)' % [pw_ent.name, file_stat.uid],
        'Group      : </U>%s<!U> (%d)' % [gr_ent.name, file_stat.gid],
        'Permissions: </U>%s<!U> (%o)' % [string_mode, int_mode],
        'Size       : </U>%ld<!U> bytes' % [file_stat.size],
        'Last Access: </U>%s' % [file_stat.atime],
        'Last Change: </U>%s' % [file_stat.ctime],
        'File Type  : </U>%s' % [filetype]
    ]

    # Create the pop up label.
    info_label = CDK::LABEL.new(entry.screen, CDK::CENTER, CDK::CENTER,
        mesg, 9, true, false)
    info_label.draw(true)
    info_label.getch([])

    info_label.destroy

    # Redraw the file selector.
    fselect.draw(fselect.box)
    return true
  end

  # This tries to complete the filename
  complete_filename_cb = lambda do |object_type, object, fselect, key|
    scrollp = fselect.scroll_field
    entry = fselect.entry_field
    filename = entry.info.clone
    mydirname = CDK.dirName(filename)
    current_index = 0
    
    # Make sure the filename is not nil/empty.
    if filename.nil? || filename.size == 0
      CDK.Beep
      return true
    end

    # Try to expand the filename if it starts with a ~
    unless (new_filename = CDK::FSELECT.expandTilde(filename)).nil?
      filename = new_filename
      entry.setValue(filename)
      entry.draw(entry.box)
    end

    # Make sure we can change into the directory.
    is_directory = Dir.exists?(filename)
    # if (chdir (fselect->pwd) != 0)
    #    return FALSE;
    #Dir.chdir(fselect.pwd)

    # XXX original: isDirectory ? mydirname : filename
    fselect.set(if is_directory then filename else mydirname end,
        fselect.field_attribute, fselect.filler_character,
        fselect.highlight, fselect.dir_attribute, fselect.file_attribute,
        fselect.link_attribute, fselect.sock_attribute, fselect.box)

    # If we can, change into the directory.
    # XXX original: if isDirectory (with 0 as success result)
    if is_directory
      entry.setValue(filename)
      entry.draw(entry.box)
    end

    # Create the file list.
    list = []
    (0...fselect.file_counter).each do |x|
      list << fselect.contentToPath(fselect.dir_contents[x])
    end

    # Look for a unique filename match.
    index = CDK.searchList(list, fselect.file_counter, filename)

    # If the index is less than zero, return we didn't find a match.
    if index < 0
      CDK.Beep
    else
      # Move to the current item in the scrolling list.
      # difference = Index - scrollp->currentItem;
      # absoluteDifference = abs (difference);
      # if (difference < 0)
      # {
      #    for (x = 0; x < absoluteDifference; x++)
      #    {
      #       injectMyScroller (fselect, KEY_UP);
      #    }
      # }
      # else if (difference > 0)
      # {
      #    for (x = 0; x < absoluteDifferene; x++)
      #    {
      #       injectMyScroller (fselect, KEY_DOWN);
      #    }
      # }
      scrollp.setPosition(index)
      fselect.drawMyScroller

      # Ok, we found a match, is the next item similar?
      if index + 1 < fselect.file_counter && index + 1 < list.size &&
          list[index + 1][0..([filename.size, list[index + 1].size].min)] ==
          filename
        current_index = index
        base_chars = filename.size
        matches = 0

        # Determine the number of files which match.
        while current_index < fselect.file_counter
          if current_index + 1 < list.size
            if list[current_index][0..(
                [filename.size, list[current_index].size].max)] == filename
              matches += 1
            end
          end
          current_index += 1
        end

        # Start looking for the common base characters.
        while true
          secondary_matches = 0
          (index...index + matches).each do |x|
            if list[index][base_chars] == list[x][base_chars]
              secondary_matches += 1
            end
          end

          if secondary_matches != matches
            CDK.Beep
            break
          end

          # Inject the character into the entry field.
          fselect.entry_field.inject(list[index][base_chars])
          base_chars += 1
        end
      else
        # Set the entry field with the found item.
        entry.setValue(list[index])
        entry.draw(entry.box)
      end
    end

    return true
  end

  # This allows the user to delete a file.
  delete_file_cb = lambda do |object_type, fscroll, fselect|
    buttons = ['No', 'Yes']

    # Get the filename which is to be deleted.
    filename = CDK.chtype2Char(fscroll.item[fscroll.current_item])
    filename = filename[0...-1]

    # Create the dialog message.
    mesg = [
        '<C>Are you sure you want to delete the file:',
        '<C></U>"%s"?' % [filename]
    ]

    # Create the dialog box.
    question = CDK::DIALOG.new(fselect.screen, CDK::CENTER, CDK::CENTER,
        mesg, 2, buttons, 2, Ncurses::A_REVERSE, true, true, false)

    # If the said yes then try to nuke it.
    if question.activate([]) == 1
      # If we were successful, reload the scrolling list.
      if File.unlink(filename) == 0
        # Set the file selector information.
        fselect.set(fselect.pwd, fselect.field_attribute,
            fselect.filler_character, fselect.highlight,
            fselect.dir_attribute, fselect.file_attribute,
            fselect.link_attribute, fselect.sock_attribute, fselect.box)
      else
        # Pop up a message.
        # mesg[0] = copyChar (errorMessage ("<C>Cannot delete file: %s"));
        # mesg[1] = copyChar (" ");
        # mesg[2] = copyChar("<C>Press any key to continue.");
        # popupLabel(ScreenOf (fselect), (CDK_CSTRING2) mesg, 3);
        # freeCharList (mesg, 3);
      end
    end

    # Clean up.
    question.destroy

    # Redraw the file seoector.
    fselect.draw(fselect.box)
  end

  # Start of callback functions.
  adjust_scroll_cb = lambda do |object_type, object, fselect, key|
    scrollp = fselect.scroll_field
    entry = fselect.entry_field

    if scrollp.list_size > 0
      # Move the scrolling list.
      fselect.injectMyScroller(key)

      # Get the currently highlighted filename.
      current = CDK.chtype2Char(scrollp.item[scrollp.current_item])
      #current = CDK.chtype2String(scrollp.item[scrollp.current_item])
      current = current[0...-1]

      temp = CDK::FSELECT.make_pathname(fselect.pwd, current)

      # Set the value in the entry field.
      entry.setValue(temp)
      entry.draw(entry.box)

      return true
    end
    CDK.Beep
    return false
  end

  # Define the callbacks for the entry field.
  @entry_field.bind(:ENTRY, Ncurses::KEY_UP, adjust_scroll_cb, self)
  @entry_field.bind(:ENTRY, Ncurses::KEY_PPAGE, adjust_scroll_cb, self)
  @entry_field.bind(:ENTRY, Ncurses::KEY_DOWN, adjust_scroll_cb, self)
  @entry_field.bind(:ENTRY, Ncurses::KEY_NPAGE, adjust_scroll_cb, self)
  @entry_field.bind(:ENTRY, CDK::KEY_TAB, complete_filename_cb, self)
  @entry_field.bind(:ENTRY, CDK.CTRL('^'), display_file_info_cb, self)

  # Put the current working directory in the entry field.
  @entry_field.setValue(@pwd)

  # Create the scrolling list in the selector.
  temp_height = @entry_field.win.getmaxy - @border_size
  temp_width = if CDK::FSELECT.isFullWidth(width)
               then CDK::FULL
               else box_width - 1
               end
  @scroll_field = CDK::SCROLL.new(cdkscreen,
      @win.getbegx, @win.getbegy + temp_height, CDK::RIGHT,
      box_height - temp_height, temp_width, '', @dir_contents,
      @file_counter, false, @highlight, box, false)

  # Set the lower left/right characters of the entry field.
  @scroll_field.setULchar(Ncurses::ACS_LTEE)
  @scroll_field.setURchar(Ncurses::ACS_RTEE)

  # Do we want a shadow?
  if shadow
    @shadow_win = Ncurses::WINDOW.new(box_height, box_width,
        ypos + 1, xpos + 1)
  end

  # Setup the key bindings
  bindings.each do |from, to|
    self.bind(:FSELECT, from, :getc, to)
  end

  cdkscreen.register(:FSELECT, self)
end

Instance Attribute Details

#dir_attributeObject (readonly)

Returns the value of attribute dir_attribute.



6
7
8
# File 'lib/cdk/fselect.rb', line 6

def dir_attribute
  @dir_attribute
end

#dir_contentsObject (readonly)

Returns the value of attribute dir_contents.



8
9
10
# File 'lib/cdk/fselect.rb', line 8

def dir_contents
  @dir_contents
end

#entry_fieldObject (readonly)

Returns the value of attribute entry_field.



5
6
7
# File 'lib/cdk/fselect.rb', line 5

def entry_field
  @entry_field
end

#field_attributeObject (readonly)

Returns the value of attribute field_attribute.



7
8
9
# File 'lib/cdk/fselect.rb', line 7

def field_attribute
  @field_attribute
end

#file_attributeObject (readonly)

Returns the value of attribute file_attribute.



6
7
8
# File 'lib/cdk/fselect.rb', line 6

def file_attribute
  @file_attribute
end

#file_counterObject (readonly)

Returns the value of attribute file_counter.



8
9
10
# File 'lib/cdk/fselect.rb', line 8

def file_counter
  @file_counter
end

#filler_characterObject (readonly)

Returns the value of attribute filler_character.



7
8
9
# File 'lib/cdk/fselect.rb', line 7

def filler_character
  @filler_character
end

#highlightObject (readonly)

Returns the value of attribute highlight.



6
7
8
# File 'lib/cdk/fselect.rb', line 6

def highlight
  @highlight
end

Returns the value of attribute link_attribute.



6
7
8
# File 'lib/cdk/fselect.rb', line 6

def link_attribute
  @link_attribute
end

#pathnameObject (readonly)

Returns the value of attribute pathname.



8
9
10
# File 'lib/cdk/fselect.rb', line 8

def pathname
  @pathname
end

#pwdObject (readonly)

Returns the value of attribute pwd.



8
9
10
# File 'lib/cdk/fselect.rb', line 8

def pwd
  @pwd
end

#scroll_fieldObject (readonly)

Returns the value of attribute scroll_field.



5
6
7
# File 'lib/cdk/fselect.rb', line 5

def scroll_field
  @scroll_field
end

#sock_attributeObject (readonly)

Returns the value of attribute sock_attribute.



7
8
9
# File 'lib/cdk/fselect.rb', line 7

def sock_attribute
  @sock_attribute
end

Class Method Details

.expandTilde(filename) ⇒ Object

Currently a wrapper for File.expand_path



883
884
885
# File 'lib/cdk/fselect.rb', line 883

def self.expandTilde(filename)
  return File.expand_path(filename)
end

.isFullWidth(width) ⇒ Object



928
929
930
# File 'lib/cdk/fselect.rb', line 928

def self.isFullWidth(width)
  width == CDK::FULL || (Ncurses.COLS != 0 && width >= Ncurses.COLS)
end

.make_pathname(directory, filename) ⇒ Object

Currently a wrapper for File.expand_path



861
862
863
864
865
866
867
# File 'lib/cdk/fselect.rb', line 861

def self.make_pathname(directory, filename)
  if filename == '..'
    return File.expand_path(directory) + '/..'
  else
    return File.expand_path(filename, directory)
  end
end

Instance Method Details

#activate(actions) ⇒ Object

This means you want to use the given file selector. It takes input from the keyboard and when it’s done it fills the entry info element of the structure with what was typed.



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/cdk/fselect.rb', line 454

def activate(actions)
  input = 0
  ret = 0

  # Draw the widget.
  self.draw(@box)

  if actions.nil? || actions.size == 0
    while true
      input = @entry_field.getch([])

      # Inject the character into the widget.
      ret = self.inject(input)
      if @exit_type != :EARLY_EXIT
        return ret
      end
    end
  else
    # Inject each character one at a time.
    actions.each do |action|
      ret = self.inject(action)
      if @exit_type != :EARLY_EXIT
        return ret
      end
    end
  end

  # Set the exit type and exit.
  self.setExitType(0)
  return 0
end

#contentToPath(content) ⇒ Object

Return the plain string that corresponds to an item in dir_contents



870
871
872
873
874
875
876
877
878
879
880
# File 'lib/cdk/fselect.rb', line 870

def contentToPath(content)
  # XXX direct translation of original but might be redundant
  temp_chtype = CDK.char2Chtype(content, [], [])
  temp_char = CDK.chtype2Char(temp_chtype)
  temp_char = temp_char[0..-1]

  # Create the pathname.
  result = CDK::FSELECT.make_pathname(@pwd, temp_char)

  return result
end

#createList(list, list_size) ⇒ Object



892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
# File 'lib/cdk/fselect.rb', line 892

def createList(list, list_size)
  status = false

  if list_size >= 0
    newlist = []

    # Copy in the new information
    status = true
    (0...list_size).each do |x|
      newlist << list[x]
      if newlist[x] == 0
        status = false
        break
      end
    end

    if status
      self.destroyInfo
      @file_counter = list_size
      @dir_contents = newlist
    end
  else
    self.destroyInfo
    status = true
  end
  return status
end

#destroyObject

This destroys the file selector.



844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
# File 'lib/cdk/fselect.rb', line 844

def destroy
  self.cleanBindings(:FSELECT)

  # Destroy the other CDK objects
  @scroll_field.destroy
  @entry_field.destroy

  # Free up the windows
  CDK.deleteCursesWindow(@shadow_win)
  CDK.deleteCursesWindow(@win)

  # Clean the key bindings.
  # Unregister the object.
  CDK::SCREEN.unregister(:FSELECT, self)
end

#destroyInfoObject



887
888
889
890
# File 'lib/cdk/fselect.rb', line 887

def destroyInfo
  @dir_contents = []
  @file_counter = 0
end

#draw(box) ⇒ Object

This draws the file selector widget.



438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/cdk/fselect.rb', line 438

def draw(box)
  # Draw in the shadow if we need to.
  unless @shadow_win.nil?
    Draw.drawShadow(@shadow_win)
  end

  # Draw in the entry field.
  @entry_field.draw(@entry_field.box)

  # Draw in the scroll field.
  self.drawMyScroller
end

#drawMyScrollerObject



425
426
427
428
429
# File 'lib/cdk/fselect.rb', line 425

def drawMyScroller
  self.saveFocus
  @scroll_field.draw(@scroll_field.box)
  self.restoreFocus
end

#eraseObject

This erases the file selector from the screen.



394
395
396
397
398
399
400
# File 'lib/cdk/fselect.rb', line 394

def erase
  if self.validCDKObject
    @scroll_field.erase
    @entry_field.erase
    CDK.eraseCursesWindow(@win)
  end
end

#focusObject



920
921
922
# File 'lib/cdk/fselect.rb', line 920

def focus
  @entry_field.focus
end

#getContents(size) ⇒ Object



786
787
788
789
# File 'lib/cdk/fselect.rb', line 786

def getContents(size)
  size << @file_counter
  return @dir_contents
end

#getCurrentItemObject

Get/set the current position in the scroll wiget.



792
793
794
# File 'lib/cdk/fselect.rb', line 792

def getCurrentItem
  return @scroll_field.getCurrent
end

#getDirAttributeObject



721
722
723
# File 'lib/cdk/fselect.rb', line 721

def getDirAttribute
  return @dir_attribute
end

#getDirContents(count) ⇒ Object



652
653
654
655
# File 'lib/cdk/fselect.rb', line 652

def getDirContents(count)
  count << @file_counter
  return @dir_contents
end

#getDirectoryObject



687
688
689
# File 'lib/cdk/fselect.rb', line 687

def getDirectory
  return @pwd
end

#getFileAttributeObject



760
761
762
# File 'lib/cdk/fselect.rb', line 760

def getFileAttribute
  return @file_attribute
end

#getFillerCharObject



697
698
699
# File 'lib/cdk/fselect.rb', line 697

def getFillerChar
  return @filler_character
end

#getHighlightObject



707
708
709
# File 'lib/cdk/fselect.rb', line 707

def getHighlight
  return @highlight
end

#getLinkAttributeObject



734
735
736
# File 'lib/cdk/fselect.rb', line 734

def getLinkAttribute
  return @link_attribute
end

#getSocketAttributeObject



747
748
749
# File 'lib/cdk/fselect.rb', line 747

def getSocketAttribute
  return @sock_attribute
end

#inject(input) ⇒ Object

This injects a single character into the file selector.



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/cdk/fselect.rb', line 487

def inject(input)
  ret = -1
  complete = false

  # Let the user play.
  filename = @entry_field.inject(input)

  # Copy the entry field exit_type to the file selector.
  @exit_type = @entry_field.exit_type

  # If we exited early, make sure we don't interpret it as a file.
  if @exit_type == :EARLY_EXIT
    return 0
  end

  # Can we change into the directory
  #file = Dir.chdir(filename)
  #if Dir.chdir(@pwd) != 0
  #  return 0
  #end

  # If it's not a directory, return the filename.
  if !Dir.exists?(filename)
    # It's a regular file, create the full path
    @pathname = filename.clone

    # Return the complete pathname.
    ret = @pathname
    complete = true
  else
    # Set the file selector information.
    self.set(filename, @field_attribute, @filler_character, @highlight,
        @dir_attribute, @file_attribute, @link_attribute, @sock_attribute,
        @box)

    # Redraw the scrolling list.
    self.drawMyScroller
  end

  if !complete
    self.setExitType(0)
  end

  @result_data = ret
  return ret
end

#injectMyScroller(key) ⇒ Object



431
432
433
434
435
# File 'lib/cdk/fselect.rb', line 431

def injectMyScroller(key)
  self.saveFocus
  @scroll_field.inject(key)
  self.restoreFocus
end

#move(xplace, yplace, relative, refresh_flag) ⇒ Object

This moves the fselect field to the given location.



403
404
405
406
407
408
409
# File 'lib/cdk/fselect.rb', line 403

def move(xplace, yplace, relative, refresh_flag)
  windows = [@win, @shadow_win]
  subwidgets = [@entry_field, @scroll_field]

  self.move_specific(xplace, yplace, relative, refresh_flag,
      windows, subwidgets)
end

#object_typeObject



936
937
938
# File 'lib/cdk/fselect.rb', line 936

def object_type
  :FSELECT
end

#positionObject



932
933
934
# File 'lib/cdk/fselect.rb', line 932

def position
  super(@win)
end

#restoreFocusObject



421
422
423
# File 'lib/cdk/fselect.rb', line 421

def restoreFocus
  @scroll_field.has_focus = @save
end

#saveFocusObject

The fselect’s focus resides in the entry widget. But the scroll widget will not draw items highlighted unless it has focus. Temporarily adjust the focus of the scroll widget when drawing on it to get the right highlighting.



416
417
418
419
# File 'lib/cdk/fselect.rb', line 416

def saveFocus
  @save = @scroll_field.has_focus
  @scroll_field.has_focus = @entry_field.has_focus
end

#set(directory, field_attrib, filler, highlight, dir_attribute, file_attribute, link_attribute, sock_attribute, box) ⇒ Object

This function sets the information inside the file selector.



535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/cdk/fselect.rb', line 535

def set(directory, field_attrib, filler, highlight, dir_attribute,
    file_attribute, link_attribute, sock_attribute, box)
  fscroll = @scroll_field
  fentry = @entry_field
  new_directory = ''

  # keep the info sent to us.
  @field_attribute = field_attrib
  @filler_character = filler
  @highlight = highlight

  # Set the attributes of the entry field/scrolling list.
  self.setFillerChar(filler)
  self.setHighlight(highlight)

  # Only do the directory stuff if the directory is not nil.
  if !(directory.nil?) && directory.size > 0
    # Try to expand the directory if it starts with a ~
    if (temp_dir = CDK::FSELECT.expandTilde(directory)).size > 0
      new_directory = temp_dir
    else
      new_directory = directory.clone
    end

    # Change directories.
    if Dir.chdir(new_directory) != 0
      CDK.Beep

      # Could not get into the directory, pop up a little message.
      mesg = [
          '<C>Could not change into %s' % [new_directory],
          '<C></U>%s' % ['Unknown reason.'],  # errorMessage(format)
          ' ',
          '<C>Press Any Key To Continue.'
      ]

      # Pop up a message.
      @screen.popupLabel(mesg, 4)

      # Get out of here.
      self.erase
      self.draw(@box)
      return
    end
  end

  # if the information coming in is the same as the information
  # that is already there, there is no need to destroy it.
  if @pwd != directory
    @pwd = Dir.getwd
  end

  @file_attribute = file_attribute.clone
  @dir_attribute = dir_attribute.clone
  @link_attribute = link_attribute.clone
  @sock_attribute = sock_attribute.clone

  # Set the contents of the entry field.
  fentry.setValue(@pwd)
  fentry.draw(fentry.box)

  # Get the directory contents.
  unless self.setDirContents
    CDK.Beep
    return
  end

  # Set the values in the scrolling list.
  fscroll.setItems(@dir_contents, @file_counter, false)
end

#setBKattr(attrib) ⇒ Object

This sets the background attribute of the widget.



838
839
840
841
# File 'lib/cdk/fselect.rb', line 838

def setBKattr(attrib)
  @entry_field.setBKattr(attrib)
  @scroll_field.setBKattr(attrib)
end

#setContents(list, list_size) ⇒ Object

this sets the contents of the widget



765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
# File 'lib/cdk/fselect.rb', line 765

def setContents(list, list_size)
  scrollp = @scroll_field
  entry = @entry_field

  if !self.createList(list, list_size)
    return
  end

  # Set the information in the scrolling list.
  scrollp.set(@dir_contents, @file_counter, false, scrollp.highlight,
      scrollp.box)

  # Clean out the entry field.
  self.setCurrentItem(0)
  entry.clean

  # Redraw the widget.
  self.erase
  self.draw(@box)
end

#setCurrentItem(item) ⇒ Object



796
797
798
799
800
801
802
803
# File 'lib/cdk/fselect.rb', line 796

def setCurrentItem(item)
  if @file_counter != 0
    @scroll_field.setCurrent(item)

    data = self.contentToPath(@dir_contents[@scroll_field.getCurrentItem])
    @entry_field.setValue(data)
  end
end

#setDirAttribute(attribute) ⇒ Object

This sets the attribute of the directory attribute in the scrolling list.



713
714
715
716
717
718
719
# File 'lib/cdk/fselect.rb', line 713

def setDirAttribute(attribute)
  # Make sure they are not the same.
  if @dir_attribute != attribute
    @dir_attribute = attribute
    self.setDirContents
  end
end

#setDirContentsObject

This creates a list of the files in the current directory.



607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/cdk/fselect.rb', line 607

def setDirContents
  dir_list = []

  # Get the directory contents
  file_count = CDK.getDirectoryContents(@pwd, dir_list)
  if file_count <= 0
    # We couldn't read the directory. Return.
    return false
  end

  @dir_contents = dir_list
  @file_counter = file_count

  # Set the properties of the files.
  (0...@file_counter).each do |x|
    attr = ''
    mode = '?'

    # FIXME(original): access() would give a more correct answer
    # TODO: add error handling
    file_stat = File.stat(dir_list[x])
    if file_stat.executable?
      mode = '*'
    else
      mode = ' '
    end

    case
    when file_stat.symlink?
      attr = @link_attribute
      mode = '@'
    when file_stat.socket?
      attr = @sock_attribute
      mode = '&'
    when file_stat.file?
      attr = @file_attribute
    when file_stat.directory?
      attr = @dir_attribute
      mode = '/'
    end
    @dir_contents[x] = '%s%s%s' % [attr, dir_list[x], mode]
  end
  return true
end

#setDirectory(directory) ⇒ Object

This sets the current directory of the file selector.



658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'lib/cdk/fselect.rb', line 658

def setDirectory(directory)
  fentry = @entry_field
  fscroll = @scroll_field
  result = 1

  # If the directory supplied is the same as what is already there, return.
  if @pwd != directory
    # Try to chdir into the given directory.
    if Dir.chdir(directory) != 0
      result = 0
    else
      @pwd = Dir.getwd

      # Set the contents of the entry field.
      fentry.setValue(@pwd)
      fentry.draw(fentry.box)

      # Get the directory contents.
      if self.setDirContents
        # Set the values in the scrolling list.
        fscroll.setItems(@dir_contents, @file_counter, false)
      else
        result = 0
      end
    end
  end
  return result
end

#setFileAttribute(attribute) ⇒ Object

This sets the attribute of the file attribute in the scrolling list.



752
753
754
755
756
757
758
# File 'lib/cdk/fselect.rb', line 752

def setFileAttribute(attribute)
  # Make sure they are not the same.
  if @file_attribute != attribute
    @file_attribute = attribute
    self.setDirContents
  end
end

#setFillerChar(filler) ⇒ Object

This sets the filler character of the entry field.



692
693
694
695
# File 'lib/cdk/fselect.rb', line 692

def setFillerChar(filler)
  @filler_character = filler
  @entry_field.setFillerChar(filler)
end

#setHighlight(highlight) ⇒ Object

This sets the highlight bar of the scrolling list.



702
703
704
705
# File 'lib/cdk/fselect.rb', line 702

def setHighlight(highlight)
  @highlight = highlight
  @scroll_field.setHighlight(highlight)
end

#setLinkAttribute(attribute) ⇒ Object

This sets the attribute of the link attribute in the scrolling list.



726
727
728
729
730
731
732
# File 'lib/cdk/fselect.rb', line 726

def setLinkAttribute(attribute)
  # Make sure they are not the same.
  if @link_attribute != attribute
    @link_attribute = attribute
    self.setDirContents
  end
end

#setMyBXattr(character) ⇒ Object



832
833
834
835
# File 'lib/cdk/fselect.rb', line 832

def setMyBXattr(character)
  @entry_field.setBXattr(character)
  @scroll_field.setBXattr(character)
end

#setMyHZchar(character) ⇒ Object



827
828
829
830
# File 'lib/cdk/fselect.rb', line 827

def setMyHZchar(character)
  @entry_field.setHZchar(character)
  @scroll_field.setHZchar(character)
end

#setMyLLchar(character) ⇒ Object



814
815
816
# File 'lib/cdk/fselect.rb', line 814

def setMyLLchar(character)
  @scroll_field.setLLchar(character)
end

#setMyLRchar(character) ⇒ Object



818
819
820
# File 'lib/cdk/fselect.rb', line 818

def setMyLRchar(character)
  @scroll_field.setLRchar(character)
end

#setMyULchar(character) ⇒ Object

These functions set the draw characters of the widget.



806
807
808
# File 'lib/cdk/fselect.rb', line 806

def setMyULchar(character)
  @entry_field.setULchar(character)
end

#setMyURchar(character) ⇒ Object



810
811
812
# File 'lib/cdk/fselect.rb', line 810

def setMyURchar(character)
  @entry_field.setURchar(character)
end

#setMyVTchar(character) ⇒ Object



822
823
824
825
# File 'lib/cdk/fselect.rb', line 822

def setMyVTchar(character)
  @entry_field.setVTchar(character)
  @scroll_field.setVTchar(character)
end

#setSocketAttribute(attribute) ⇒ Object

This sets the attribute of the socket attribute in the scrolling list.



739
740
741
742
743
744
745
# File 'lib/cdk/fselect.rb', line 739

def setSocketAttribute(attribute)
  # Make sure they are not the same.
  if @sock_attribute != attribute
    @sock_attribute = attribute
    self.setDirContents
  end
end

#unfocusObject



924
925
926
# File 'lib/cdk/fselect.rb', line 924

def unfocus
  @entry_field.unfocus
end