require 'fox'

include Fox

#
# This subclass of FXIconItem overrides the default comparison
# operator <=> to sort items differently according to the current
# "sorting method" (i.e. by name, by title or by age).
#
class MyIconItem < FXIconItem
  def initialize(*args)
    super(*args)
    @@sorting_method = 0 # sort by name
  end

  def MyIconItem.sorting_method=(s)
    @@sorting_method = s
  end

  def MyIconItem.sorting_method
    @@sorting_method
  end

  def name
    fields = text.split("\t")
    fields[0]
  end

  def title
    fields = text.split("\t")
    fields[1]
  end

  def age
    fields = text.split("\t")
    fields[2].to_i
  end

  def <=>(otherItem)
    case @@sorting_method
      when 0
        name <=> otherItem.name
      when 1
        title <=> otherItem.title
      else
        age <=> otherItem.age
    end
  end
end

#
# This subclass of FXIconList listens for SEL_COMMAND messages from its
# embedded FXHeader widget.
#
class MyIconList < FXIconList

  include Responder
  
  def initialize(*args)
    super(*args)
    FXMAPFUNC(SEL_COMMAND, FXIconList::ID_HEADER_CHANGE, :onCmdHeader)
  end
  
  def onCmdHeader(sender, sel, headerItemIndex)
    puts "sender=#{sender}, sel=#{sel}, headerItemIndex=#{headerItemIndex}"
    MyIconItem.sorting_method = headerItemIndex
    sortItems
    return 1
  end
end

class IconListWindow < FXMainWindow
  # Load the named PNG icon from a file
  def loadIcon(filename)
    begin
      filename = File.join("icons", filename)
      icon = nil
      File.open(filename, "rb") { |f|
        icon = FXPNGIcon.new(getApp(), f.read)
      }
      icon
    rescue
      raise RuntimeError, "Couldn't load icon: #{filename}"
    end
  end

  # Main window constructor
  def initialize(app)
    # Initialize base class first
    super(app, "Icon List Test", nil, nil, DECOR_ALL, 0, 0, 800, 600)
    
    # Menu bar
    menubar = FXMenubar.new(self, LAYOUT_SIDE_TOP|LAYOUT_FILL_X)
    
    # File menu
    filemenu = FXMenuPane.new(self)
    FXMenuCommand.new(filemenu, "&Quit\tCtl-Q", nil, getApp(), FXApp::ID_QUIT)
    FXMenuTitle.new(menubar, "&File", nil, filemenu)
      
    # Status bar
    status = FXStatusbar.new(self, LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X|STATUSBAR_WITH_DRAGCORNER)
    
    # Main window interior
    group = FXVerticalFrame.new(self, LAYOUT_FILL_X|LAYOUT_FILL_Y, 0, 0, 0, 0, 0, 0, 0, 0)

    # Files
    FXLabel.new(group, "Icon List Widget", nil, LAYOUT_TOP|LAYOUT_FILL_X|FRAME_SUNKEN)
    subgroup = FXVerticalFrame.new(group, FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_X|LAYOUT_FILL_Y, 0, 0, 0, 0, 0, 0, 0, 0)
  
    # Icon list on the right
    iconlist = MyIconList.new(subgroup, nil, 0, LAYOUT_FILL_X|LAYOUT_FILL_Y|ICONLIST_BIG_ICONS|ICONLIST_EXTENDEDSELECT)
    
    iconlist.appendHeader("Name", nil, 200)
    iconlist.appendHeader("Title", nil, 100)
    iconlist.appendHeader("Age", nil, 60)
  
    big_folder = loadIcon("bigfolder.png")
    mini_folder = loadIcon("minifolder.png")

    iconlist.appendItem(MyIconItem.new("Oscar\tCat\t7", big_folder, mini_folder))
    iconlist.appendItem(MyIconItem.new("Lyle\tSoftware Engineer\t33", big_folder, mini_folder))
    iconlist.appendItem(MyIconItem.new("Alfred\tButler\t17", big_folder, mini_folder))

    iconlist.currentItem = iconlist.numItems - 1

    # Arrange menu
    FXMenuPane.new(self) do |menuPane|
      FXMenuCommand.new(menuPane, "&Details", nil, iconlist, FXIconList::ID_SHOW_DETAILS)
      FXMenuCommand.new(menuPane, "&Small Icons", nil, iconlist, FXIconList::ID_SHOW_MINI_ICONS)
      FXMenuCommand.new(menuPane, "&Big Icons", nil, iconlist, FXIconList::ID_SHOW_BIG_ICONS)
      FXMenuCommand.new(menuPane, "&Rows", nil, iconlist, FXIconList::ID_ARRANGE_BY_ROWS)
      FXMenuCommand.new(menuPane, "&Columns", nil, iconlist, FXIconList::ID_ARRANGE_BY_COLUMNS)
      FXMenuTitle.new(menubar, "&Arrange", nil, menuPane)
    end
  
    # Let's see a tooltip
    FXTooltip.new(getApp())
  end
  
  # Overrides base class version
  def create
    super
    show(PLACEMENT_SCREEN)
  end
end

if __FILE__ == $0
  FXApp.new("IconList", "FXRuby") do |theApp|
    IconListWindow.new(theApp)
    theApp.create
    theApp.run
  end
end

