[ruby-gnome2-doc-cvs] [Ruby-GNOME2 Project Website] create - tut-gtk2-dnd-native-treev

Zurück zum Archiv-Index

ruby-****@sourc***** ruby-****@sourc*****
2012年 12月 9日 (日) 05:31:52 JST


-------------------------
REMOTE_ADDR = 184.145.81.223
REMOTE_HOST = 
        URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-dnd-native-treev
-------------------------
TITLE       = tut-gtk2-dnd-native-treev
KEYWORD     = 
= Drag And Drop
{{link "tut-gtk2-dnd-intro", "tut-gtk2-dnd", "tut-gtk", "tut-gtk2-dynui"}}


#######################################################################
#### Do not forget to update the return arrow in "tut-gtk2-dynui". ####
#######################################################################


== Sorry, still under construction


# (10.3)/(9a) [current file: tut-gtk2-dnd-native-treev]




# (10.3)
== DnD Widgets With Their Own Gdk::Window

# (10.3.1)
=== DnD Tree View Items

Some Gtk widgets have built in drag-and-drop mechanism, which require very little or no additional coding. Such a widget is Gtk::TreeView. However, sometimes you are not satisfied with the default dnd behaviour, and you may need to try to implement some customized dnd functionality. As you will see Gtk::TreeView provides a number of methods to help you accomplish this. We will investigate some of these issues in our example after the next one.


# (10.3.1.1)
=== Built-in DnD Tree View Mechanism


{{image_right("treeview-builtin-dnd-s1.png")}}

This example program will show that only with a line of code you can get the complete drag-and-drop behaviour for free. All you need is to set up the reorderable attribute to true, by using the 'Gtk::TreeView#reorderable=' method. The drag objects here are the tree view rows, regardless whether they are leaf or nodes with children. When you left-click, hold the mouse button down and drag the mouse simultaneously the cursor changes to the 'move-icon' and the row you are dragging appears underneath it. If you look carefully, when dragging, you will notice that a potential drop position is shown, namely, the items over which you are dragging the row get highlighted, and the when you are in-between i.e., above or below rows a line appears. If you are dropping the dragged row onto a highlighted row it will become it's child, but if you drop it when the line indicator is shown, the dragged item will be placed at that position.

# (10.3.1.1.1)
:Office Supplies Array Initialization Module

    In 'treeview-built-in-dnd.rb' example program we use auxiliary initialization application dependent module. If this module were useful also in many other tutorial example programs it would be wise to place it in a common directory or folder, along with our 'HikiGtk' module file (hiki-gtk.rb). However, we decided to place it in the same directory where also the example 'treeview-built-in-dnd.rb' program resides. This requires in the example program we tell Ruby interpreter to also include our current directory, from which we are going to run our example code, in the load path (look for the declarative statement ((*"$: << '.'"*)) at the beginning of the example program). Indeed, you could have as well specify the full path in the 'require' declarative instead, however, I wanted to repeat the idiom expanding the load path, so you get used to it, because it is very convenient, when you use module files in other directories.

    Following is the module file, which you should copy in your working directory along with the program example:

    ((*init-office-supp-arr.rb*))
    
     # This file is planed to be loaded from the same directory in which
     # the example program resides. 
     #
     # Its name should be: 'init-office-supp-arr.rb'
     module InitializeOfficeSuppliesFromASCItable
       # Tree View  Iinitialization  Array Of Arrays:
       # name,   qty,  Children
       # ---------------------------------------------
       INIT_ARRAY = [
         ['Stationery',  nil,  [
             ['Letter paper',       "500 sheets",  nil],
             ['Envelopes',          "200",         nil],
             ['Computer paper',  nil,    [
                 ['Legal',  "1 box",      nil],
                 ['Letter', "2 boxes",    nil],
               ]
             ]
           ]
         ],
         ['Notebooks & Writing Pads',  nil,  [
             ['Memo Book',   "10",  nil],
             ['Journals',    "5",  nil],
           ]
         ],
     
         ['Computer Accessories',  nil,  [
             ['Printer Tonner',  "2x C501AB",  nil],
             ['RWCDs&DVD',       "100x 4GB",  nil],
           ]
         ],
       ]
       class OfficeSupplies
         attr_accessor :name, :qty, :children
         def initialize(name, qty, children)
           @name, @qty, @children = name, qty, children
           arr = []
           if @children
             @children.each do |row|
               arr << OfficeSupplies.new(*row)
             end
             @children = arr
           end
         end
       end
     end





Let's look now at the example program:






((*treeview-built-in-dnd.rb*))

 #!/usr/bin/env ruby
 $: << '~/work/HikiLib'
 require 'hiki-gtk.rb'
 include HikiGtk
 $: << '.'
 require 'init-office-supp-arr.rb'
 include InitializeOfficeSuppliesFromASCItable
 
 class DnDWindow < BasicWindow
   NAME_COLUMN, QTY_COLUMN = 0, 1
   def initialize(title)
     super
     self.set_size_request(400, 350)
     office_supply_list = []
     INIT_ARRAY.each_with_index do |row, i|
       office_supply_list[i] = OfficeSupplies.new(*row)
     end
     @treeview = Gtk::TreeView.new(store = Gtk::TreeStore.new(String, String))
     load_office_supplies_into_tree_view(store, office_supply_list, nil)
     @treeview.expand_all        # only works after treview is loaded
     setup_tree_view
     ## -- Enables automatic Drag-And-Drop in the tree view -------
     @treeview.reorderable = true
     scrolled_win = Gtk::ScrolledWindow.new
     scrolled_win.add(@treeview)
     scrolled_win.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
     self.add(scrolled_win)
     self.show_all
     Gtk.main
   end
 
   private
   def setup_tree_view
     renderer = Gtk::CellRendererText.new
     column   = Gtk::TreeViewColumn.new("Name", renderer, :text => NAME_COLUMN)
     @treeview.append_column(column)
     renderer = Gtk::CellRendererText.new
     column   = Gtk::TreeViewColumn.new("Quantity", renderer, :text => QTY_COLUMN)
     @treeview.append_column(column)
   end
   def load_office_supplies_into_tree_view(store, office_supply_list, parent)
     # Add all of the names to the GtkTreeStore.
     office_supply_list.each do |supplies_obj|
       # If the supplies object has children it's a parent
       if (supplies_obj.children)
         # Add the parent as a new root row (element).
         child_parent = store.append(parent)
         child_parent[NAME_COLUMN] = supplies_obj.name
         child_parent[QTY_COLUMN]  = supplies_obj.qty
         load_office_supplies_into_tree_view(store, supplies_obj.children, child_parent)
       # Otherwise, add the leaf item as a child row.
       else
         child = store.append(parent)
         child[NAME_COLUMN] = supplies_obj.name
         child[QTY_COLUMN]  = supplies_obj.qty
       end
     end
   end
 end
 DnDWindow.new("Built-in Drag-n-Drop Within a Tree View")


{{br}}


We start the above program with a helper module used to initialize our tree view from the asci array. We have discussed this technique in chapter 8 in section 8.2.3.1 (((<Tedious Job Of Loading Multidimensional Tree Store|tut-gtk2-treev-trees#Tedious Job Of Loading Multidimensional Tree Store>))). The tree view itself is rather simple, and you should be fairly familiar with its code. The only piece of code that deserves our attention is the one that enables the dnd functionality:

 ## -- Enables automatic Drag-And-Drop in the tree view -------
 @treeview.reorderable = true

Also note that we do not need to set up any signal handlers for the dnd to function.




##  10 Drag And Drop (this page)
##    10.1 DnD Introduction
##       10.1.1 Identifying the DnD Objects
##       10.1.2 The DnD Source And Destination Objects
##          10.1.2.1 Why Do the DnD Widgets Need a Gdk::Window Of Their Own
##          10.1.2.2 There Are Three Different Source And Destination DnD Set-Up Scenarios
##
##    10.2 Setting DnD For Widgets Without Their Own Gdk::Window
##       10.2.1 Dragging a Button Onto a Label
##         10.2.1.1 Time To Start Using Object-Oriented Programming Paradigm
##     [(10.2.2) 9.6.2.2 Dragging a Toolbar Within a Window]
##
##    10.3 DnD Widgets With Their Own Gdk::Window
##       10.3.1 DnD Tree View Items
##          10.3.1.1 Built-in DnD Tree View Mechanism
##          10.3.1.2 Custom Made DnD In Tree View
##
##       10.3.2 DnD In Text View Widget
##
##       10.3.3 DnD Icon View Items
##
##       10.3.4 Dragging And Dropping Onto Drawable Widgets
##          10.3.4.1 DnD Onto Gtk::Gtk::DrawingArea
##          10.3.4.2 DnD And Cairo







# (10.3.1.2)
=== Custom Made DnD In Tree View




# (10.3.2)
=== DnD In Text View Widget




ruby-gnome2-cvs メーリングリストの案内
Zurück zum Archiv-Index