ruby-****@sourc*****
ruby-****@sourc*****
2012年 10月 8日 (月) 02:06:51 JST
------------------------- REMOTE_ADDR = 184.145.80.187 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-crs ------------------------- @@ -23,17 +23,23 @@ === Toggle Button Renderers -Displaying Boolean values as "TRUE" or "FALSE" with Gtk::CellRendererText is a bit tacky, and it takes up a large amount of valuable space in each row, especially when there are a lot of visible Boolean columns. You might be thinking that it would be nice if you could display a check button for Boolean values instead of text strings.It turns out that you can, with the help of a type of ell renderer called Gtk::CellRendererToggle. +Displaying Boolean values as "TRUE" or "FALSE" with Gtk::CellRendererText, especially in large tables with many Boolean columns, becomes soon very illegible and aesthetic, as well as it also takes up a too much valuable visible area. You would significantly improve lucidity of your Boolean tables if instead of text strings you displayed a check button for Boolean values. As it turns out you can do just that, with the help of the toggle button cell renderers called Gtk::CellRendererToggle. By default, they are drawn as check buttons, but can also be set up as radio buttons, however, that would also require you yourself implement the necessary radio button functionality. -By default, toggle button cell renderers are drawn as a check button, but you can also set them up to be drawn as radio buttons, however you will need to manage the radio button functionality yourself. +As with the editable text renderers, you have to manually apply the changes performed by the user. Otherwise, the button will not toggle visually on the screen. Because of this, Gtk::CellRendererToggle provides the((*toggle*))signal, which is emitted when the user presses the check button. -As with the editable text renderers, you have to manually apply the changes performed by the user. Otherwise, the button will not toggle visually on the screen. Because of this, Gtk::CellRendererToggle provides the ((*toggle*)) signal, which is emitted when the user presses the check button. + renderer.signal_connect('toggled') do |w, path| + iter = treeview.model.get_iter(path) + iter[ITEM_COLUMN] = !iter[ITEM_COLUMN] if (iter) + end +With the introduction of toggle button cell renderers you actually make the Boolean column editable, which means you can interactively modify its contents, i.e. presentation of its value in the tree view. If the Boolean value is in any way related to the contents of other columns in the tree view this dependency may have to be managed too. As you will discover shortly, our example program has such a dependency. Not to bury the mere mechanics of implementing toggle button cell renderers, we will approach the toggle button renderer itself and its data dependency related issues in two steps. First we will introduce the the toggle renderer without thinking how it may effect the presentation of other data in the tree view (toggle-rndr-1.rb), and in the second example (toggle-rndr-2.rb), immediately after that, we will tackle those dependency related issues. + {{image_right("treev-crs-01.png")}} + +As you can see from the image on the right we are continuing to use the same program example as in the introductory session to the tree store (1.3 ((<Using Gtk::ListStore|tut-gtk2-treev-parts#Using Gtk::ListStore>))). The example program there we called 'treestore.rb'. The only changes in the program here are in the ((*setup_tree_view(treeview)*)) method, where we had to change the type of our cell renderer. An important, often overlooked change is also in the column line, where we change the name of the attribute that used to be "text" to "active". Since we wish to provide our users with the ability to interactively change the status of "Buy" column, we need to make our toggle button clickable, which means that we also need to provide the callback proc (block), triggered by the((*toggled*))signal, which will set the toggle to the new value. -We are using the same program as in the introductory session to the tree store (1.3 ((<Using Gtk::ListStore|tut-gtk2-treev-parts#Using Gtk::ListStore>))), there called "treestore.rb". The only changes are in the ((*setup_tree_view(treeview)*)) method, where we had to change the type of our cell renderer. An important, often overlooked change is also in the column line, where we change the name of the attribute that used to be "text" to "active". Since we wish to provide our users with the ability to interactively change the status of "Buy" column, wee need to make our toggle button clickable, which means that we also need to provide the callback proc (block) which will set the toggle to the new value. +The listing 'toggle-rndr-1.rb' presents the Grocery List application with the just mentioned callback code block. In it the we alter the value in the model for the current row in the GItm::BUY_INDEX column, which subsequently effects the respective cell in the tree view as the toggle renderer renders it on the display. -The listing "treestore-toggle.rb" presents the Grocery List application with a toggled callback function. In it the $buy_index column is rendered with Gtk::CellRendererToggle. {{br}} ((*toggle-rndr.rb*))