|
From: Hans F. <fu...@us...> - 2004-06-10 02:33:25
|
Update of /cvsroot/neelix/neelix/view In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31942/view Modified Files: fox.rb Log Message: major refactoring of fox.rb, not complete. (e.g. the treelist is still broken) Index: fox.rb =================================================================== RCS file: /cvsroot/neelix/neelix/view/fox.rb,v retrieving revision 1.17 retrieving revision 1.18 diff -u -d -r1.17 -r1.18 --- fox.rb 5 May 2004 04:00:53 -0000 1.17 +++ fox.rb 10 Jun 2004 02:33:16 -0000 1.18 @@ -1,402 +1,137 @@ require 'fox' include Fox -require 'presenter/presenter' - -class CookbookForm < FXVerticalFrame - def initialize(parent,o) - super(parent) - @cookbook = o - - FXLabel.new(self,"Name") - @name = FXTextField.new(self, 80) - - @name.connect(SEL_COMMAND) do |sender,sel,data| - @cookbook.name = data - end - - populate - end - def populate - return if not @cookbook - @name.text = @cookbook.name - end - def cookbook=(o) - @cookbook = o - populate - end -end -class CategoryForm < FXVerticalFrame - def initialize(parent,o) - super(parent) - @category = o - - FXLabel.new(self,"Name") - @name = FXTextField.new(self,80) - - @name.connect(SEL_COMMAND) do |sender,sel,data| - @category.name = data - end - - populate - end - def populate - return if not @category - @name.text = @category.name - end - def category=(o) - @category = o - populate - end -end -class RecipeForm < FXVerticalFrame - def initialize(parent,r) - super(parent) - @recipe = r - - FXLabel.new(self,"Name") - @name = FXTextField.new(self, 80, nil, 0, LAYOUT_FILL_X|TEXTFIELD_NORMAL) - - FXLabel.new(self,"Author") - @author = FXTextField.new(self, 80, nil, 0, LAYOUT_FILL_X|TEXTFIELD_NORMAL) - - FXLabel.new(self,"Oven Temp.") - @temp = FXTextField.new(self, 80, nil, 0, LAYOUT_FILL_X|TEXTFIELD_NORMAL) - - FXLabel.new(self,"Total Time") - @tottime = FXTextField.new(self, 80, nil, 0, LAYOUT_FILL_X|TEXTFIELD_NORMAL) - - FXLabel.new(self,"Yields") - @yields = FXTextField.new(self, 80, nil, 0, LAYOUT_FILL_X|TEXTFIELD_NORMAL) - - FXLabel.new(self,"Ingredients") - frame = FXHorizontalFrame.new(self,LAYOUT_FILL_X, 0,0,0,0, 0,0,0,0) - ingredientFrame = FXVerticalFrame.new(frame, FRAME_NORMAL|LAYOUT_FILL_X, 0,0,0,0, 0,0,0,0) - @ingredientList = FXList.new(ingredientFrame,5,nil,0,LAYOUT_FILL_X|LIST_SINGLESELECT) - - frame = FXVerticalFrame.new(frame) - @add = FXButton.new(frame,"Add Ingredient") - @up = FXButton.new(frame,"Move Up") - @delete = FXButton.new(frame,"Delete") - @up.enabled = false - @delete.enabled = false - - FXLabel.new(self,"Directions") - frame = FXVerticalFrame.new(self, FRAME_NORMAL|LAYOUT_FILL_X, 0,0,0,0, 0,0,0,0) - @directions = FXText.new(frame, nil, 0, LAYOUT_FILL_X) - @directions.visCols=80 - - FXLabel.new(self,"Notes") - frame = FXVerticalFrame.new(self, FRAME_NORMAL|LAYOUT_FILL_X, 0,0,0,0, 0,0,0,0) - @notes = FXText.new(frame, nil, 0, LAYOUT_FILL_X) - @notes.visCols=80 - - # connections - @name.connect(SEL_COMMAND) do |sender,sel,data| - @recipe.name = data - end - - @author.connect(SEL_COMMAND) do |sender,sel,data| - @recipe.author = data - end - - @temp.connect(SEL_COMMAND) do |sender,sel,data| - @recipe.temp = data - end - - @tottime.connect(SEL_COMMAND) do |sender,sel,data| - @recipe.tottime = data - end - - @yields.connect(SEL_COMMAND) do |sender,sel,data| - @recipe.yields = data - end - - @ingredientList.connect(SEL_SELECTED) do |sender,sel,data| - @up.enabled = (data != 0) - @delete.enabled = true - @ingredientList.setCurrentItem(data) - end - - @ingredientList.connect(SEL_COMMAND) do |snd,sel,data| - @ingredientList.selectItem(data) - end - - @ingredientList.connect(SEL_DOUBLECLICKED) do |sender,sel,data| - ingredient = @recipe.ingredients.sort[data] - ingredientDialog = FXDialogBox.new(self,"Edit Ingredient") - - FXLabel.new(ingredientDialog,"Quantity") - quantity = FXTextField.new(ingredientDialog, 80, nil, 0, LAYOUT_FILL_X|TEXTFIELD_NORMAL) - quantity.text = ingredient.quantity.to_s - - FXLabel.new(ingredientDialog,"Measure") - measures = FXListBox.new(ingredientDialog, 5, nil, 0, FRAME_SUNKEN|FRAME_THICK|LISTBOX_NORMAL|LAYOUT_FILL_X) - $replicator.measures.each do |measure| - measures << measure.name - measures.currentItem = measures.numItems - 1 if measure === ingredient.measure - end - - FXLabel.new(ingredientDialog,"Food") - food = FXTextField.new(ingredientDialog, 80, nil, 0, LAYOUT_FILL_X|TEXTFIELD_NORMAL) - food.text = ingredient.food.name - - FXLabel.new(ingredientDialog,"Modifier") - modifier = FXTextField.new(ingredientDialog, 80, nil, 0, LAYOUT_FILL_X|TEXTFIELD_NORMAL) - modifier.text = ingredient.modifier - - FXButton.new(ingredientDialog,"Accept",nil,ingredientDialog,FXDialogBox::ID_ACCEPT) - FXButton.new(ingredientDialog,"Cancel",nil,ingredientDialog,FXDialogBox::ID_CANCEL) - if ingredientDialog.execute == 1 then - ingredient.quantity = quantity.text - ingredient.measure = $replicator.measures[measures.currentItem] - ingredient.modifier = (modifier.text == '' ? nil : modifier.text) - - new_food = food.text - food = $replicator.find_food(new_food) - food = $replicator.create('food', {'name' => new_food}) if food.nil? - ingredient.food = food - end - end - - @add.connect(SEL_COMMAND) do |sender,sel,data| - ingredientDialog = FXDialogBox.new(self,"Add Ingredient") - - FXLabel.new(ingredientDialog,"Quantity") - quantity = FXTextField.new(ingredientDialog, 80, nil, 0, LAYOUT_FILL_X|TEXTFIELD_NORMAL) - quantity.text = '0.0' - - FXLabel.new(ingredientDialog,"Measure") - measures = FXListBox.new(ingredientDialog, 5, nil, 0, FRAME_SUNKEN|FRAME_THICK|LISTBOX_NORMAL|LAYOUT_FILL_X) - $replicator.measures.each { |measure| measures << measure.name } - - FXLabel.new(ingredientDialog,"Food") - food = FXTextField.new(ingredientDialog, 80, nil, 0, LAYOUT_FILL_X|TEXTFIELD_NORMAL) +LAYOUT_FILL_XY = LAYOUT_FILL_X|LAYOUT_FILL_Y +# app +app = FXApp.new('Neelix', 'fugalh') +app.init(ARGV) - FXLabel.new(ingredientDialog,"Modifier") - modifier = FXTextField.new(ingredientDialog, 80, nil, 0, LAYOUT_FILL_X|TEXTFIELD_NORMAL) +# main window +mw = FXMainWindow.new(app, 'Neelix') +mw.resize(800,600) - FXButton.new(ingredientDialog,"Accept",nil,ingredientDialog,FXDialogBox::ID_ACCEPT) - FXButton.new(ingredientDialog,"Cancel",nil,ingredientDialog,FXDialogBox::ID_CANCEL) - if ingredientDialog.execute == 1 then - quantity = quantity.text - measure = $replicator.measures[measures.currentItem] - modifier = (modifier.text == '' ? nil : modifier.text) +# menu +menubar = FXMenubar.new(mw) - new_food = food.text - food = $replicator.find_food(new_food) - food = $replicator.create('food', {'name' => new_food}) if food.nil? +filemenu = FXMenuPane.new(mw) +FXMenuCommand.new(filemenu, "&Exit", nil, mw.getApp(), FXApp::ID_QUIT) +FXMenuTitle.new(menubar, "&File", nil, filemenu) - Presenter::addIngredient(@recipe, $replicator.create('ingredient',{ - 'recipe_id' => @recipe.id, - 'measure_id' => measure.id, - 'quantity' => quantity, - 'food_id' => food.id, - 'modifier' => modifier})) - end - end +actionmenu = FXMenuPane.new(mw) +actions={} +actions['add_cookbook'] = FXMenuCommand.new(actionmenu, "Add Cook&book", nil) +actions['add_cookbook'].enable +actions['add_category'] = FXMenuCommand.new(actionmenu, "Add &Category", nil) +actions['add_category'].disable +actions['add_recipe'] = FXMenuCommand.new(actionmenu, "Add &Recipe", nil) +actions['add_recipe'].disable +FXMenuTitle.new(menubar,"&Action",nil,actionmenu) +actions['add_cookbook'].connect(SEL_COMMAND) { + c = $replicator.create('cookbook',{'name'=>"New Cookbook"}) +} +actions['add_category'].connect(SEL_COMMAND) { puts "TODO: Add Category proc" } +actions['add_recipe'].connect(SEL_COMMAND) { puts "TODO: Add Recipe proc" } - @up.connect(SEL_COMMAND) do |sender,sel,data| - index = @ingredientList.currentItem - if index > 0 - Presenter::promoteIngredient(@recipe, index) - @ingredientList.selectItem(index - 1,true) - end - end +helpmenu = FXMenuPane.new(mw) +FXMenuCommand.new(helpmenu, "&About Neelix...").connect(SEL_COMMAND) { + FXMessageBox.information(mw, MBOX_OK, "About Neelix", + "Neelix is cool.\nhttp://sf.net/projects/neelix\n\nDedicated to Erin Fugal") +} +FXMenuTitle.new(menubar, "&Help", nil, helpmenu, LAYOUT_RIGHT) - @delete.connect(SEL_COMMAND) do |sender,sel,data| - selectedIndices = (0 .. @ingredientList.numItems-1).select do |i| - @ingredientList.itemSelected?(i) - end - selectedIndices.each do |index| - Presenter::deleteIngredient(@recipe, index) - end - end +# splitter +splitter = FXSplitter.new(mw,SPLITTER_HORIZONTAL|LAYOUT_FILL_XY) - @directions.connect(SEL_CHANGED) do |sender,sel,data| - @recipe.directions = @directions.text - end +# shelf +class Shelf < FXTreeList + def initialize(parent) + super(parent, 1, nil, 0, + TREELIST_SINGLESELECT| + TREELIST_SHOWS_LINES|TREELIST_SHOWS_BOXES|TREELIST_ROOT_BOXES| + LAYOUT_FILL_XY) + @items = {} + $replicator.cookbooks.add_observer { rebuild } + rebuild + end - @notes.connect(SEL_CHANGED) do |sender,sel,data| - @recipe.note = @notes.text + # rebuild the tree because something has changed + # This is the brute-force way. It may be good enough. + # discard the current tree + # walk the data: + # if the items hash has a matching node, bring it in + # else make a new node + def rebuild + self.clearItems + $replicator.cookbooks.each do |cookbook| + if not @items[cookbook] + @items[cookbook] = FXTreeItem.new(cookbook.name) + @items[cookbook].data = cookbook + cookbook.add_observer { @items[cookbook].text = cookbook.name; update } + end + self.addItemLast(nil, @items[cookbook]) + cookbook.categories.each do |category| + if not @items[category] + @items[category] = FXTreeItem.new(category.name) + @items[category].data = category + category.add_observer { @items[category].text = category.name; update } end - - populate - end - - def populate - return if not @recipe - @name.text = @recipe.name - @author.text = @recipe.author - @temp.text = @recipe.temp - @tottime.text = @recipe.tottime - @yields.text = @recipe.yields - - refreshIngredients - @recipe.ingredients.add_observer { refreshIngredients } - - @directions.text = @recipe.directions - @notes.text = @recipe.note - end - - def refreshIngredients - @ingredientList.clearItems - @recipe.ingredients.sort.each do |ingredient| - @ingredientList.appendItem(ingredient.to_s) + self.addItemLast(@items[cookbook], @items[category]) + category.recipes.each do |recipe| + if not @items[recipe] + @items[recipe] = FXTreeItem.new(recipe.name) + @items[recipe].data = recipe + recipe.add_observer { @items[recipe].text = recipe.name; update } + end + self.addItemLast(@items[category], @items[recipe]) end + end end - - def recipe=(r) - @recipe = r - populate - update - end + end end -class NeelixMainWindow < FXMainWindow - def initialize(app) - super(app, 'Neelix') - resize(800,600) - - # Menu - menubar = FXMenubar.new(self) - filemenu = FXMenuPane.new(self) - FXMenuCommand.new(filemenu, "&Exit", nil, getApp(), FXApp::ID_QUIT) - FXMenuTitle.new(menubar,"&File",nil,filemenu) - - actionmenu = FXMenuPane.new(self) - actions={} - actions['add_cookbook'] = FXMenuCommand.new(actionmenu, "Add Cook&book", nil) - actions['add_cookbook'].enable - actions['add_category'] = FXMenuCommand.new(actionmenu, "Add &Category", nil) - actions['add_category'].disable - actions['add_recipe'] = FXMenuCommand.new(actionmenu, "Add &Recipe", nil) - actions['add_recipe'].disable - FXMenuTitle.new(menubar,"&Action",nil,actionmenu) - - helpmenu = FXMenuPane.new(self) - FXMenuCommand.new(helpmenu, "&About Neelix...").connect(SEL_COMMAND) { - FXMessageBox.information(self, MBOX_OK, "About Neelix", - "Neelix is cool.\nhttp://sf.net/projects/neelix\n\nDedicated to Erin Fugal") - } - FXMenuTitle.new(menubar, "&Help", nil, helpmenu, LAYOUT_RIGHT) - - frame = FXHorizontalFrame.new(self, LAYOUT_FILL_X|LAYOUT_FILL_Y) - splitter = FXSplitter.new(frame,SPLITTER_HORIZONTAL|LAYOUT_FILL_X|LAYOUT_FILL_Y) - - # Splitter - shelf = FXGroupBox.new(splitter,"Shelf",FRAME_GROOVE) - shelf.width=200 - counter = FXGroupBox.new(splitter,"Counter",FRAME_GROOVE) - counterSwitcher = FXSwitcher.new(counter,LAYOUT_FILL_X|LAYOUT_FILL_Y) - FXWindow.new(counterSwitcher) - CookbookForm.new(counterSwitcher,nil) - CategoryForm.new(counterSwitcher,nil) - RecipeForm.new(counterSwitcher,nil) - - # Shelf - frame = FXVerticalFrame.new(shelf, FRAME_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_Y, 0,0,0,0, 0,0,0,0) - shelfTree = FXTreeList.new(frame,0,nil,0,TREELIST_SINGLESELECT|TREELIST_SHOWS_LINES|TREELIST_SHOWS_BOXES|TREELIST_ROOT_BOXES|LAYOUT_FILL_X|LAYOUT_FILL_Y) - $replicator.cookbooks.each do |cookbook| - cookbook_item = shelfTree.addItemLast(nil, FXTreeItem.new(cookbook.name)) - cookbook_item.data = cookbook - cookbook.add_observer { cookbook_item.text = cookbook.name; shelfTree.update } - cookbook.categories.each do |category| - category_item = shelfTree.addItemLast(cookbook_item, FXTreeItem.new(category.name)) - category_item.data = category - category.add_observer { category_item.text = category.name; shelfTree.update } - category.recipes.each do |recipe| - item = FXTreeItem.new(recipe.name) - item.data = recipe - shelfTree.addItemLast(category_item, item) - recipe.add_observer { item.text = recipe.name; shelfTree.update } - end - end - end - shelfTree.connect(SEL_SELECTED) do |sender,sel,ptr| - case ptr.data - when Cookbook - counterSwitcher.children[1].cookbook = ptr.data - counterSwitcher.current = 1 - actions['add_cookbook'].enable - actions['add_category'].enable - actions['add_recipe'].disable - when Category - counterSwitcher.children[2].category = ptr.data - counterSwitcher.current = 2 - actions['add_cookbook'].enable - actions['add_category'].disable - actions['add_recipe'].enable - when Recipe - counterSwitcher.children[3].recipe = ptr.data - counterSwitcher.current = 3 - actions['add_cookbook'].enable - actions['add_category'].disable - actions['add_recipe'].disable - else - counterSwitcher.current = 0 - actions['add_cookbook'].enable - actions['add_category'].disable - actions['add_recipe'].disable - end - end - - shelfTree.expandTree(shelfTree.firstItem) - shelfTree.expandTree(shelfTree.firstItem.first) - shelfTree.makeItemVisible(shelfTree.firstItem.first.first) - - # shelf observers - $replicator.cookbooks.add_observer { - ary = $replicator.cookbooks.to_a - data = ary.shift - shelfTree.each do |item| - if data == item.data - data = ary.shift - next - end +shelfbox = FXGroupBox.new(splitter, "Shelf", FRAME_GROOVE) +shelfbox.width = 200 +shelf = Shelf.new(shelfbox) - while data != nil and item.data != data - newitem = FXTreeItem.new(data.name) - newitem.data = data - data.add_observer { - newitem.text = data.name - } - shelfTree.addItemBefore(item,newitem) - shelfTree.selectItem(newitem,true) - data = ary.shift - end +shelf.connect(SEL_SELECTED) do |sender,sel,ptr| + case ptr.data + when Cookbook + #counterSwitcher.children[1].cookbook = ptr.data + #counterSwitcher.current = 1 + actions['add_cookbook'].enable + actions['add_category'].enable + actions['add_recipe'].disable + when Category + #counterSwitcher.children[2].category = ptr.data + #counterSwitcher.current = 2 + actions['add_cookbook'].enable + actions['add_category'].disable + actions['add_recipe'].enable + when Recipe + #counterSwitcher.children[3].recipe = ptr.data + #counterSwitcher.current = 3 + actions['add_cookbook'].enable + actions['add_category'].disable + actions['add_recipe'].disable + else + #counterSwitcher.current = 0 + actions['add_cookbook'].enable + actions['add_category'].disable + actions['add_recipe'].disable + end +end - shelfTree.removeItem(item) if data.nil? - end - ary << data - ary.each { |data| - newitem = FXTreeItem.new(data.name) - newitem.data = data - data.add_observer { - newitem.text = data.name - } - shelfTree.addItemLast(nil,newitem) - shelfTree.selectItem(newitem,true) - } - } - # TODO categories and recipes observers +shelf.expandTree(shelf.firstItem) +shelf.expandTree(shelf.firstItem.first) +shelf.makeItemVisible(shelf.firstItem.first.first) - # actions menu connections - actions['add_cookbook'].connect(SEL_COMMAND) { - c = $replicator.create('cookbook',{'name'=>"New Cookbook"}) - } - actions['add_category'].connect(SEL_COMMAND) { puts "TODO: Add Category proc" } - actions['add_recipe'].connect(SEL_COMMAND) { puts "TODO: Add Recipe proc" } - end -end +# counter +counter = FXGroupBox.new(splitter, "Counter", FRAME_GROOVE) -app = FXApp.new('Neelix', 'fugalh') -app.init(ARGV) -neelix = NeelixMainWindow.new(app) app.create -neelix.show +mw.show app.run -# vim:nowrap:ts=8:sw=4 +# vim:sw=2 |