ruby-****@sourc*****
ruby-****@sourc*****
2012年 10月 2日 (火) 05:50:03 JST
------------------------- REMOTE_ADDR = 184.145.80.187 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-addrnhs ------------------------- @@ -814,36 +814,51 @@ The accumulative nature of the product prices is handled with the((*fix_parent_row_prices*))method, which is used for both additions and removal of products from our tree view. Programmer only needs to supply negative price argument to the method when removing a product. (Note, that the model is not effected by these price manipulation processes.) - elsif response == Gtk::Dialog::RESPONSE_DELETE_EVENT +:Check if children are removed along with the parent: - model = treeview.model - iter = model.get_iter(selected_row_path) - removed_price = -iter[PRICE_COLUMN] - puts "DELETING: #{iter[NAME_COLUMN]} ---> (#{removed_price})" - fix_parent_row_prices(treeview, selected_row_path, removed_price) + In the early days, when the API documentation was scarce, I had to check that no children dangle around, when their parents were removed from the tree. This is also a reasonable exercise for a beginner GTK+ developer. Here is the original removal code from our example program: - rmrow_refs = [] - if iter.has_child? - c_iter = iter.first_child - i = 0 - begin - i += 1 - puts "Child ##{i} #{c_iter[NAME_COLUMN]} to be removed" - rmrow_refs << Gtk::TreeRowReference.new(model, c_iter.path) - end while c_iter.next! - end + elsif response == Gtk::Dialog::RESPONSE_DELETE_EVENT + model = treeview.model + iter = model.get_iter(selected_row_path) + removed_price = -iter[PRICE_COLUMN] + puts "DELETING: #{iter[NAME_COLUMN]} ---> (#{removed_price})" + fix_parent_row_prices(treeview, selected_row_path, removed_price) + model.remove(iter) + end - model.remove(iter) + In order to check, if children are removed, we need to remember their position in the tree view. We learned that the proper way to do this is to store the references to the pertinent row paths. We will not check for more than the first generation of ancestors. Lets see how the above code is augmented in order to perform our test: + + elsif response == Gtk::Dialog::RESPONSE_DELETE_EVENT + + model = treeview.model + iter = model.get_iter(selected_row_path) + removed_price = -iter[PRICE_COLUMN] + puts "DELETING: #{iter[NAME_COLUMN]} ---> (#{removed_price})" + fix_parent_row_prices(treeview, selected_row_path, removed_price) + + rmrow_refs = [] + if iter.has_child? + c_iter = iter.first_child + i = 0 + begin + i += 1 + puts "Child ##{i} #{c_iter[NAME_COLUMN]} to be removed" + rmrow_refs << Gtk::TreeRowReference.new(model, c_iter.path) + end while c_iter.next! + end + + model.remove(iter) - rmrow_refs.each do |rowref| - if (rmrow_ref_path = rowref.path) - puts "#{model.get_iter(rmrow_ref_path)[NAME_COLUMN]}" - else - puts "After remove rowref.path=[#{rowref.path}] reference does not exist" - end - end + rmrow_refs.each do |rowref| + if (rmrow_ref_path = rowref.path) + puts "#{model.get_iter(rmrow_ref_path)[NAME_COLUMN]}" + else + puts "After remove rowref.path=[#{rowref.path}] reference does not exist" + end + end - end + end #### path = /(.+)(:\d+)/ =~ path ? $1 : $2 #### BUG? Gtk::TreePath#up! <<< doesn't work