ruby-****@sourc*****
ruby-****@sourc*****
2009年 2月 19日 (木) 23:47:48 JST
------------------------- REMOTE_ADDR = 74.15.84.244 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-cbbr ------------------------- @@ -4,20 +4,155 @@ == Combo Box Renderers -{{image_left("treev-crs-01.png")}} - By this time I'd be pleasantly surprised if Combo Box Renderer would be working. - {{image_right("dialog-warning.png")}} Due to the problems with the past few renderer examples, I first implemented the combo box cell renderer functionality into C version of ((*treestore.c.*)) Indeed, it did not work. However, it behaved better without the error/warning messages, that we experienced with all earlier renderers that did not work. You can check this C GTK+ version of combo box renderer example by clicking on: ((<treestore-combob.c|tut-gtk2-treev-treev-combob-cgtk-01>)) +{{image_left("treev-combo-01.png")}} +{{image_right("dialog-warning.png")}} +Needless to say that our Ruby combo box renderer example also does not work. The image on the left is what we get on the screen. - +Following is the Ruby version of our combo box renderer program: {{br}} + + #!/usr/bin/env ruby + require 'gtk2' + + # Add three columns to the GtkTreeView. All three of the + # columns will be displayed as text, although one is a boolean + # value and another is an integer. + def setup_tree_view(treeview) + + # Create a Gtk::ListStore that will be used for the + # combo box renderer. + model = Gtk::ListStore.new(String) + iter = model.append + iter[0] = "None" + iter = model.append + iter[0] = "One" + iter = model.append + iter[0] = "Half a Dozen" + iter = model.append + iter[0] = "Dozen" + iter = model.append + iter[0] = "Two Dozen" + + renderer = Gtk::CellRendererText.new + column = Gtk::TreeViewColumn.new("Buy", renderer, "text" => $buy_index) + treeview.append_column(column) + + # Create the GtkCellRendererCombo and add the tree model. + # Then add the renderer to a new column and add the column + # to the GtkTreeView + + renderer = Gtk::CellRendererCombo.new + column = Gtk::TreeViewColumn.new("Count", renderer, "text" => $qty_index) + renderer.text_column = 0 + renderer.has_entry = true + renderer.editable = true + renderer.model = model + treeview.append_column(column) + + renderer.signal_connect('edited') do |w, path, new_text| + if path != "" + if (iter = treview.model.get_iter(path)) + iter[$qty_index] = new_text + end + end + end + + renderer = Gtk::CellRendererText.new + column = Gtk::TreeViewColumn.new("Product", renderer, "text" => $prod_index) + treeview.append_column(column) + end + + window = Gtk::Window.new(Gtk::Window::TOPLEVEL) + window.resizable = true + window.title = "Grocery List" + window.border_width = 10 + window.signal_connect('delete_event') { Gtk.main_quit } + window.set_size_request(275, 200) + + class GroceryItem + attr_accessor :product_type, :buy, :quantity, :product + def initialize(t,b,q,p) + @product_type, @buy, @quantity, @product = t, b, q, p + end + end + $buy_index = 0; $qty_index = 1; $prod_index = 2 + $p_category = 0; $p_child = 1 + + list = Array.new + list[0] = GroceryItem.new($p_category, true, 0, "Cleaning Supplies") + list[1] = GroceryItem.new($p_child, true, 1, "Paper Towels") + list[2] = GroceryItem.new($p_child, true, 3, "Toilet Paper") + list[3] = GroceryItem.new($p_category, true, 0, "Food") + list[4] = GroceryItem.new($p_child, true, 2, "Bread") + list[5] = GroceryItem.new($p_child, false, 1, "Butter") + list[6] = GroceryItem.new($p_child, true, 1, "Milk") + list[7] = GroceryItem.new($p_child, false, 3, "Chips") + list[8] = GroceryItem.new($p_child, true, 4, "Soda") + + treeview = Gtk::TreeView.new + setup_tree_view(treeview) + + # Create a new tree model with three columns, as Boolean, + # integer and string. + store = Gtk::TreeStore.new(TrueClass, Integer, String) + + # Avoid creation of iterators on every iterration, since they + # need to provide state information for all iterations. Hence: + # establish closure variables for iterators parent and child. + parent = child = nil + + # Add all of the products to the GtkListStore. + list.each_with_index do |e, i| + + # If the product type is a category, count the quantity + # of all of the products in the category that are going + # to be bought. + if (e.product_type == $p_category) + j = i + 1 + + # Calculate how many products will be bought in + # the category. + while j < list.size && list[j].product_type != $p_category + list[i].quantity += list[j].quantity if list[j].buy + j += 1 + end + + # Add the category as a new root (parent) row (element). + parent = store.append(nil) + # store.set_value(parent, $buy_index, list[i].buy) # <= same as below + parent[$buy_index] = list[i].buy + parent[$qty_index] = list[i].quantity + parent[$prod_index] = list[i].product + + # Otherwise, add the product as a child row of the category. + else + child = store.append(parent) + # store.set_value(child, $buy_index, list[i].buy) # <= same as below + child[$buy_index] = list[i].buy + child[$qty_index] = list[i].quantity + child[$prod_index] = list[i].product + end + end + + # Add the tree model to the tree view + treeview.model = store + treeview.expand_all + scrolled_win = Gtk::ScrolledWindow.new + scrolled_win.add(treeview) + scrolled_win.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC) + window.add(scrolled_win) + window.show_all + Gtk.main + + == Progress Bar Renderers