[Fxruby-users] FXList and multiple selections
Status: Inactive
Brought to you by:
lyle
|
From: Matthew M. <nam...@na...> - 2004-02-25 23:31:08
|
Hello,
I'm working on learning FXRuby and to do that I'm writing a basic file
manager. My program uses FXList to display the file and directory names and I
have a question about some behavior I'm seeing. Since what I have done so far is
so few lines my program is included between the body and signature.
When selecting items in the FXList I see two different behaviors after starting
the program: When I select one item and press and hold Ctrl, and then select
other items and release Ctrl a message box shows that the correct number of
items are selected.
Conversely, before I select an item I press and hold Ctrl, and then start
selecting items. When Ctrl is released the message box shows that zero items
have been selected. My question is why I'm seeing this differing behavior based
on when the first item is selected.
Thanks for any help, Matthew.
#!/usr/local/bin/ruby -w
require 'fox'
include Fox
class FileListWindow < FXMainWindow
def initialize(app)
# Invoke the base class initialize first
super(app, "Ruby File Manager", nil, nil, DECOR_ALL, 0, 0, 300, 600)
@status = FXStatusbar.new(self, LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X);
frame = FXVerticalFrame.new(self, LAYOUT_FILL_X|LAYOUT_FILL_Y )
@list = FXList.new( frame, 0, nil, 0, LAYOUT_FILL_Y|LAYOUT_FILL_X|LIST_NORMAL )
font = FXFont.new( app, "courier", 9, FONTWEIGHT_MEDIUM )
@list.font = font
@list.connect( SEL_DOUBLECLICKED, method(:onDoubleClick))
@list.connect( SEL_KEYPRESS, method(:onKeyPress))
@list.connect( SEL_KEYRELEASE, method(:onKeyRelease))
@list.connect( SEL_SELECTED, method(:onSelect))
@list.connect( SEL_DESELECTED, method(:onDeselect))
# Variables for hold state information.
@selectedItems = Array.new
@selectedIndex = nil
@ctrlPressed = false
readDir('.')
end
def onDeselect( sender, sel, which )
if @ctrlPressed
@selectedItems.delete( which )
end
end
def onSelect( sender, sel, event )
@selectedIndex = event
if @ctrlPressed
@selectedItems << @selectedIndex
else
@selectedItems.clear
end
end
# Check to see if the keypress event is one we are interested in.
def onKeyPress( sender, sel, event )
case event.code
when KEY_Control_L, KEY_Control_R
@ctrlPressed = true
if ! @selectedIndex.nil? and
@selectedItems[@selectedItems.size-1] != @selectedIndex
@selectedItems << @selectedIndex
end
end
end
# Same here, check the release event to see if it one we watch for.
def onKeyRelease( sender, sel, event )
case event.code
when KEY_Control_L, KEY_Control_R
@ctrlPressed = false
FXMessageBox.question(self, MBOX_OK, 'Selected Items',
"The number of selected items is #{@selectedItems.size}" )
end
end
def readDir( path )
@list.clearItems( false )
dir = Dir.open( path )
begin
dir.each { |item|
filename = item.dup
stat = File.lstat( item )
if stat.directory?
item << '/'
elsif stat.executable?
item << '*'
end
@list.appendItem( item, nil, filename )
}
ensure
dir.close
end
end
# Take to proper action for the item that was double clicked.
def onDoubleClick( sender, sel, which )
name = @list.getItemData( which )
stat = File.lstat( name )
if stat.directory?
Dir.chdir( name )
readDir( '.' )
else # a file of some type, do something else.
#
end
end
# Start
def create
# Create window
super
# Show the main window
show(PLACEMENT_SCREEN)
end
end
# Make an application
application = FXApp.new("Ruby File Manager", "Matthew Miller")
# Open display
application.init(ARGV)
# Create main window
window = FileListWindow.new(application)
# Create the application
application.create
# Run
application.run
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." - Brian W. Kernighan
|