vr-corelib 0.0.36

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/Demo.rb +41 -0
  3. data/doc/images/add.png +0 -0
  4. data/doc/images/brick.png +0 -0
  5. data/doc/images/brick_link.png +0 -0
  6. data/doc/images/bug.png +0 -0
  7. data/doc/images/bullet_black.png +0 -0
  8. data/doc/images/bullet_toggle_minus.png +0 -0
  9. data/doc/images/bullet_toggle_plus.png +0 -0
  10. data/doc/images/date.png +0 -0
  11. data/doc/images/delete.png +0 -0
  12. data/doc/images/find.png +0 -0
  13. data/doc/images/macFFBgHack.png +0 -0
  14. data/doc/images/package.png +0 -0
  15. data/doc/images/page_green.png +0 -0
  16. data/doc/images/page_white_text.png +0 -0
  17. data/doc/images/page_white_width.png +0 -0
  18. data/doc/images/plugin.png +0 -0
  19. data/doc/images/ruby.png +0 -0
  20. data/doc/images/tag_blue.png +0 -0
  21. data/doc/images/tag_green.png +0 -0
  22. data/doc/images/transparent.png +0 -0
  23. data/doc/images/wrench.png +0 -0
  24. data/doc/images/wrench_orange.png +0 -0
  25. data/doc/images/zoom.png +0 -0
  26. data/glade/Demo.glade +52 -0
  27. data/img/image-x-generic.png +0 -0
  28. data/lib/Dialog.rb +157 -0
  29. data/lib/GladeGUI.rb +358 -0
  30. data/lib/IconHash.rb +22 -0
  31. data/lib/SimpleComboBoxEntry.rb +14 -0
  32. data/lib/Thread.rb +59 -0
  33. data/lib/Widget.rb +37 -0
  34. data/lib/treeview/FileTreeView.rb +91 -0
  35. data/lib/treeview/IterMethods.rb +82 -0
  36. data/lib/treeview/ListView.rb +90 -0
  37. data/lib/treeview/ModelRow.rb +31 -0
  38. data/lib/treeview/TreeIter.rb +33 -0
  39. data/lib/treeview/TreeView.rb +43 -0
  40. data/lib/treeview/ViewCommon.rb +351 -0
  41. data/lib/treeview/columns/CalendarCol.rb +88 -0
  42. data/lib/treeview/columns/CellRendererCombo.rb +66 -0
  43. data/lib/treeview/columns/CellRendererDate.rb +45 -0
  44. data/lib/treeview/columns/CellRendererObject.rb +57 -0
  45. data/lib/treeview/columns/CellRendererPhone.rb +45 -0
  46. data/lib/treeview/columns/CellRendererPixbuf.rb +16 -0
  47. data/lib/treeview/columns/CellRendererProgress.rb +17 -0
  48. data/lib/treeview/columns/CellRendererSpin.rb +37 -0
  49. data/lib/treeview/columns/CellRendererText.rb +38 -0
  50. data/lib/treeview/columns/CellRendererToggle.rb +47 -0
  51. data/lib/treeview/columns/ComboCol.rb +43 -0
  52. data/lib/treeview/columns/CurrencyCol.rb +23 -0
  53. data/lib/treeview/columns/DateCol.rb +20 -0
  54. data/lib/treeview/columns/ImageCol.rb +30 -0
  55. data/lib/treeview/columns/ProgressCol.rb +27 -0
  56. data/lib/treeview/columns/SpinCol.rb +11 -0
  57. data/lib/treeview/columns/TextCol.rb +64 -0
  58. data/lib/treeview/columns/TreeViewColumn.rb +108 -0
  59. data/lib/treeview/columns/glade/CalendarCol.glade +133 -0
  60. data/lib/treeview/columns/glade/ImageCol.glade +16 -0
  61. data/lib/treeview/columns/glade/TextCol.glade +77 -0
  62. data/main.rb +3 -0
  63. data/vr-corelib.rb +7 -0
  64. data/vrlib.rb +7 -0
  65. metadata +138 -0
@@ -0,0 +1,43 @@
1
+
2
+ module VR
3
+ #
4
+ # TreeView is almost identical to VR::ListView. The only difference
5
+ # is how rows are added, and that they use a different model class.
6
+ # See VR::ListView for in-depth docs. Also, see GtkTreeView for more.
7
+ #
8
+ # Also, most of the useful methods for VR::TreeView are found in VR::ViewCommon.
9
+ # All the methods in VR::ViewCommon work on VR::TreeViews and VR::ListViews.
10
+ #
11
+ class TreeView < Gtk::TreeView
12
+ unless defined? TREEVIEW_GTYPE
13
+ TREEVIEW_GTYPE = self.name.split(":").collect {|x| x.empty? ? nil : x}.compact.join("_")
14
+ type_register(TREEVIEW_GTYPE)
15
+ end
16
+
17
+ include ViewCommon
18
+
19
+ # See VR::ListView constructor. (exactly the same)
20
+
21
+ def initialize(cols)
22
+ super()
23
+ self.model = Gtk::TreeStore.new(*flatten_hash(cols).values)
24
+ load_columns(cols)
25
+ end
26
+ #
27
+ # Adds row to the model. This will return a "row" type iter that responds
28
+ # to column IDs (symbols). You need to provide a parent row (iter).
29
+ # See GtkTreeView#append for more.
30
+ #
31
+ # The iter is a GtkTreeIter object for the parent or nil for the root of the tree. The hash argument is a Hash of values to add:
32
+ #
33
+ # @view.add_row(iter, :name => "Chester", :email => "chester@chesterizer.com")
34
+ #
35
+ #
36
+ def add_row(parent, hash)
37
+ child = model.append(parent)
38
+ (0..args.size-1).each { |i| child[i] = args[i] }
39
+ return child
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,351 @@
1
+ module VR
2
+
3
+ # This module holds all the methods that both VR::ListView
4
+ # and VR::TreeView use. All the methods listed here can be called from a
5
+ # VR::ListView or a VR::TreeView.
6
+
7
+
8
+
9
+ #
10
+ # ==The col_<propery>, ren_<property>, col_attr, and ren_attr methods
11
+ #
12
+ # All of these methods do the same thing: set the properties of your VR::ListView.
13
+ #
14
+ # The way to configure a VR::ListView or VR::TreeView is to set the properties
15
+ # of its columns (and renderers). For example,
16
+ # setting the "title" property of a column will set the title that appears at the top.
17
+ #
18
+ # The normal Gtk approach to setting properties of a GtkTreeViewColumn, (or renderer) is to set
19
+ # them individually:
20
+ #
21
+ # col = Gtk::TreeViewColumn.new()
22
+ # col.title = "My Title"
23
+ #
24
+ # This is slow, and tedious. However, you can completely configure any VR::ListView or
25
+ # VR::TreeView object with only a few lines of code using the above methods. They can set many properties on many columns
26
+ # at once.
27
+ #
28
+ # The first thing to understand is that there are several ways to set properties. In fact all
29
+ # of these lines of code do the exact same thing:
30
+ #
31
+ # column(:name).title = "Person's Name"
32
+ # col_title(:name => "Person's Name")
33
+ # ren_title(:name => "Person's Name")
34
+ # col_attr(:name, :title => "Person's Name")
35
+ # ren_attr(:name, :title => "Person's Name")
36
+ #
37
+ # All the above lines of code simply set the title of the :name column. So why
38
+ # is one method better than another? Answer: because you can set multiple
39
+ # properties on multiple columns with one line of code. For example, you can set all the titles
40
+ # with this line:
41
+ #
42
+ # col_titles(:name => "Name", :email => "Address", :phone => "Phone Number")
43
+ #
44
+ # The col_<property> method is very good at setting <b>multiple columns</b>.
45
+ #
46
+ # Likewise, the VR::ListView#col_attr method is good at setting the same property on multiple columns:
47
+ #
48
+ # col_attr(:name, :email, :phone, :width => 200, :weight => 700) #bold
49
+ #
50
+ # Either way, you can set everything with one line of code. Also, if you want to set
51
+ # a propery for all the columns, simply omit any column IDs:
52
+ #
53
+ # col_width(200)
54
+ # ren_background("yellow")
55
+ # col_attr(:font => "Sans")
56
+ # ren_attr(:visible => true)
57
+ #
58
+ # There are many, many possibilities of properties you can set using these methods.
59
+ # You need to know the name of the property that you want to set from consulting
60
+ # the docs for GtkTreeViewColumn and the docs for the column's renderer type (listed on the left of this webpage)
61
+ #
62
+ # Any of the properties of these classes can be set using the above methods.
63
+ #
64
+ # If you consult, GtkCellRendererText for example, you'll see that it has a "background" property.
65
+ # Therefore you can use ren_background() to set the color of the background of that column.
66
+ #
67
+ # Method names like "col_title" are just one possibility of methods you can use.
68
+ # You could call any of these as well:
69
+ #
70
+ # col_width
71
+ # col_xalign # (0.00 to 1.00, 1 = right justify)
72
+ # col_weight # (100 to 900)
73
+ # col_background
74
+ # col_foreground
75
+ # col_font
76
+ # col_markup
77
+ # col_size
78
+ # col_spacing
79
+ #
80
+ # You'll notice that some of the methods listed above don't look valid. For example,
81
+ # col_background doesn't seem to make sense because the GtkTreeViewColumn doesn't
82
+ # have a <b>background</b> property. The <b>background</b> property belongs to the
83
+ # renderer, GtkCellRendererText. But, the col_<attribute> method is programmed
84
+ # to try to set the property on the column first, and if the column object doesn't
85
+ # support the propery, it will look in the renderer. If the renderer doesn't support it,
86
+ # it DOES NOTHING.
87
+ #
88
+ # Likewise, the ren_<property> method will look first in the renderer for the property, then
89
+ # the column. The only difference between the ren_<property> and col_<propery> methods
90
+ # is that the ren_<property> looks in the renderer object first. These two methods
91
+ # are almost interchangable. In fact, I always just use the col_<propery> method
92
+ # because it looks better. The only time I have a problem is when the column and the renderer
93
+ # have the same property (i.e. "visible" and "xalign"). Then, I simply substitute the ren_<property> method.
94
+ #
95
+ # ==Summary
96
+ #
97
+ # - To set the <b>same attribute value</b> on multiple columns use col_attr() or ren_attr()
98
+ # - To set <b>different attribute values</b> on multiple columns, use col_<property> or ren_<property>
99
+ # - The ren_<property> and col_<property> methods are almost interchangable.
100
+ # - The ren_attr() and col_attr() methods are almost interchangable.
101
+ # - To set the properties of <b>all</b> the columns, omit any column iDs
102
+ # - Consult the documentation for GtkTreeViewColumn to learn about properties to set.
103
+ # - Consult the Gtk docs for the renderer by locating the type on the left of this webpage.
104
+ #
105
+ # Some examples are:
106
+ # ren_background(:name => "yellow", :email => black) #renderer for :name bg = yellow
107
+ # col_editable(false) #makes all columns un-editable
108
+ # col_width(:name => 300, email => 200)
109
+ # ren_xalign(:email => 1) # right justify email
110
+ # ren_attr(:name, :email, :font => "Courier")
111
+ # ren_attr(:font => "Sans") # all columns now Sans font
112
+ # col_attr(:name, :email, :visible => true)
113
+ #
114
+
115
+ module ViewCommon
116
+
117
+
118
+
119
+ def load_columns(cols) # :nodoc:
120
+ model_col = 0
121
+ cols.each_pair do | sym, type|
122
+ col = VR::TreeViewColumn.new(self, model_col, sym, type)
123
+ model_col = model_col + (type.class == Hash ? type.size : 1)
124
+ self.append_column(col)
125
+ end
126
+ turn_on_comboboxes()
127
+ @column_keys = flatten_hash(cols).keys
128
+ end
129
+
130
+ def method_missing(meth, *args) # :nodoc:
131
+ unless m = /^(ren_|col_)(.+)$/.match(meth.to_s)
132
+ super
133
+ return
134
+ end
135
+ if args.first.is_a? Hash
136
+ args.first.each_pair { |key, val| method(m[1] + "attr").call(key, m[2] => val) }
137
+ else
138
+ method(m[1] + "attr").call(m[2] => args.first)
139
+ end
140
+ end
141
+
142
+ # Sets properties on renderers (and columns) See VR::ViewCommon#col_attr for more.
143
+
144
+ def ren_attr(*args)
145
+ cols = args.select { |arg| !arg.is_a? Hash }
146
+ return unless hash = args.detect { |arg| arg.is_a? Hash }
147
+ cols = @column_keys if cols.empty?
148
+ cols.each do |c|
149
+ hash.each_pair do | key, val |
150
+ if renderer(c).respond_to?(key.to_s + "=")
151
+ renderer(c).send(key.to_s + '=', val)
152
+ elsif column(c).respond_to?(key.to_s + "=")
153
+ column(c).send(key.to_s + '=', val)
154
+ end
155
+ end
156
+ end
157
+ end
158
+
159
+ # Sets properties on many columns at once. It can be called with many columns and many attributes.
160
+ # Also, if you don't specify any columns, it will set the properties on all of them. There are
161
+ # many ways to call this method, here are a few:
162
+ #
163
+ # @view.col_attr(:name, :date, :background => "yellow", foreground => "black")
164
+ # @view.col_attr(:background => "yellow", foreground => "black") #sets all columns
165
+ # @view.col_attr(:name, :date, :editable => true) # both editable now
166
+ # @view.col_attr(:editable => false) # turns off editing to all columns
167
+ #
168
+ # Also, if the column VR::TreeViewColumn object doesn't support the property, it will try to
169
+ # set the property on the renderer instead. In the above example, the col_attr() method tries to
170
+ # set teh "<b>background</b>" property of a VR:TreeViewColumn. However, that object doesn't
171
+ # support the "background" property, but the renderer for the column, VR::CellRendererText does.
172
+ # So it sets the renderer's property instead. So, in this example this line of code would do exactly the same
173
+ # thing:
174
+ #
175
+ # @view.ren_attr(:name, :date, :background => "yellow", foreground => "black")
176
+ #
177
+ # In the vast majority of cases, VR::ViewCommon#col_attr and VR::ViewCommon#ren_attr are interchangable.
178
+
179
+
180
+ def col_attr(*args)
181
+ cols = args.select { |arg| !arg.is_a? Hash }
182
+ return unless hash = args.detect { |arg| arg.is_a? Hash }
183
+ cols = @column_keys if cols.empty?
184
+ cols.each do |c|
185
+ hash.each_pair do | key, val |
186
+ if column(c).respond_to?(key.to_s + "=")
187
+ column(c).send(key.to_s + '=', val)
188
+ elsif renderer(c).respond_to?(key.to_s + "=")
189
+ renderer(c).send(key.to_s + '=', val)
190
+ end
191
+ end
192
+ end
193
+ end
194
+
195
+
196
+ # Returns an array of rows that are selected in the VR::TreeView or VR::ListView.
197
+ # If nothing is selected, it returns an empty array. If you've configured your
198
+ # listview to accept multiple selections, it will return all of them. In single
199
+ # selection mode, it will return an array with one row. These rows are
200
+ # able to respond to column IDs. They are the same types of rows as returned by
201
+ # VR::ViewCommon#vr_row.
202
+
203
+ def selected_rows()
204
+ rows = []
205
+ selection.selected_each do |model, path, iter|
206
+ rows << vr_row(iter)
207
+ end
208
+ rows
209
+ end
210
+
211
+ def turn_on_comboboxes() # :nodoc:
212
+ #detect if comboboxes are present:
213
+ found = false
214
+ self.each_renderer do |r|
215
+ if r.is_a? VR::CellRendererCombo
216
+ found = true
217
+ break
218
+ end
219
+ end
220
+ return unless found
221
+ self.signal_connect("cursor_changed") do |view|
222
+ next unless iter = view.selection.selected
223
+ view.each_renderer do |r|
224
+ r.set_model( iter[r.model_col] ) if r.is_a? VR::CellRendererCombo
225
+ end
226
+ end
227
+ end
228
+
229
+ def flatten_hash(hash) # :nodoc:
230
+ h = {}
231
+ hash.each do | k, v |
232
+ if v.class == Hash
233
+ v.each_pair { |key, val| h[key] = val }
234
+ else
235
+ h[k] = v
236
+ end
237
+ end
238
+ return h
239
+ end
240
+
241
+ # Enumerates each row in the model and returns an instance of GtkTreeIter.
242
+ # However, the iters returned have been converted into a "row" using VR::ViewCommon#vr_row
243
+ # so they will respond to colum IDs (symbols). Like this:
244
+ #
245
+ # @view.each_row { |row| puts row[:name] } # works!
246
+ #
247
+
248
+ def each_row
249
+ self.model.each { |mod, pth, itr| yield vr_row(itr) }
250
+ end
251
+
252
+ # Converts a normal GtkTreeIter to use VR's column IDs. You can use it like this:
253
+ #
254
+ # row = @view.vr_row(iter) # iter is a Gtk::TreeIter
255
+ # row[:name] = "Chester" # works!
256
+
257
+
258
+ def vr_row(iter)
259
+ iter.extend(VR::IterMethods)
260
+ iter.column_keys = @column_keys
261
+ return iter
262
+ end
263
+
264
+
265
+ # Returns a VR::TreeViewColumn object for the column. You can pass this method either
266
+ # a column ID (symbol) or the column number. Since, VR::TreeViewColumn is a subclass
267
+ # of Gtk::TreeViewColumn, you should consult Gtk::TreeViewColumn's documentation to
268
+ # see all the properties you can set on this object:
269
+ #
270
+ # @view.column(:name).title = "Person's Name"
271
+ # @view.column(:ok).visible = false
272
+ # @view.column(:name).class.name # => VR:TreeViewColumn
273
+ #
274
+ # Even though the above statements are valid, its usually easier to use the col_<property>
275
+ # methods instead. See VR::ViewCommon for more.
276
+
277
+
278
+ def column(id)
279
+ renderer(id).column
280
+ end
281
+
282
+ def each_renderer
283
+ self.columns.each do |c|
284
+ c.cell_renderers.each do |r|
285
+ yield r
286
+ end
287
+ end
288
+ end
289
+
290
+ #Returns the renderer for a given column ID.
291
+ #
292
+ #In VR::ListView#new (and VR::TreeView#new) method, a data model (VR::ListStore or VR::TreeStore) is automatically contstructed.
293
+ #Then the class will automatically assign a renderer to show it on the screen. These renderers
294
+ #simply convert a piece of data to something visual on the screen. For example, a column in the model
295
+ #might contain a value "true," but the renderer converts it to a GtkCheckButton which shows a check-mark.
296
+ #The VR::ListView class will
297
+ #automatically assign renderers to each column based on its type:
298
+ #
299
+ #String, Fixnum, Integer, Float => VR::CellRendererText
300
+ #TrueClass => VR::CellRendererToggle
301
+ #GdkPixbuf => VR::CellRendererPixbuf
302
+ #DateTime => VR::CellRendererDate
303
+ #VR::CalendarCol, VR::TextCol => VR::CellRendererObject
304
+ #VR::SpinCol => VR::CellRendererSpin
305
+ #VR::ProgressCol => VR::CellRendererProgress
306
+ #VR::ComboCol => VR::CellRendererCombo
307
+ #
308
+ #The renderer() method will return one of these renderers:
309
+ #
310
+ # ren = @view.renderer(:ok)
311
+ # puts ren.class.name # VR::CellRendererToggle (:ok column is a TrueClass)
312
+ # puts @view.renderer(:name).class.name # => VR::CellRendererText
313
+ #
314
+ #All the types of renderers are subclasses of Gtk renderers. For example, VR::CellRendererText
315
+ #is a subclass of Gtk::CellRendererText. So, you can use these objects just as you would use a
316
+ #normal Gtk renderer:
317
+ #
318
+ # @view.renderer(:name).width = 100
319
+ #
320
+ #This is perfectly valid even though there are better ways of setting these properties in visualruby.
321
+
322
+ def renderer(id)
323
+ each_renderer do |r|
324
+ return r if r.model_col == id(id)
325
+ end
326
+ return nil
327
+ end
328
+
329
+ # Returns the number of the given column ID. This is very useful when you're
330
+ # working with Gtk's methods because they require column numbers (not Column IDs)
331
+ # This method converts the column ID symbols from the VR::ListView#new constructor
332
+ # to Integers:
333
+ #
334
+ # @view = VR::ListView.new(:name => String, :date => VR::CalendarCol)
335
+ #
336
+ # Later in code...
337
+ #
338
+ # iter = get_iter(path)
339
+ # col_num = id(:date) # 1
340
+ # iter[col_num]
341
+ #
342
+ # You won't need to use this when adding rows with VR::ListView#add_row, and
343
+ # you also have the option of converting the whole iter to use column IDs (symbols)
344
+ # using VR::ViewCommon#vr_row.
345
+
346
+ def id(id)
347
+ return (id.is_a? Fixnum or id.is_a? Integer) ? id : @column_keys.index(id)
348
+ end
349
+
350
+ end
351
+ end
@@ -0,0 +1,88 @@
1
+
2
+ module VR
3
+
4
+ # The CalendarCol class is a simple calendar window where you can edit
5
+ # the date:
6
+ #
7
+ # http://visualruby.net/img/calendar.jpg
8
+ #
9
+ # This class is very useful when you want to display and edit dates
10
+ # in a VR::ListView. You can define a column with the type VR::CalendarCol
11
+ # and the column will display as a date, and when the user clicks on the
12
+ # date, a calendar window will appear so he/she can edit it:
13
+ #
14
+ # @view = VR::ListView.new(:name => String, :birthday => VR::CalendarCol)
15
+ # row = @view.add_row
16
+ # row[:name] = "Eric"
17
+ # row[:birthday] = VR::CalendarCol.new(DateTime.new(1966, 7, 14))
18
+ #
19
+ # See the example project, "listview_objects" for more.
20
+
21
+ class CalendarCol
22
+
23
+ include GladeGUI
24
+
25
+ attr_accessor :date, :date_format, :show_time, :show_calendar
26
+ #
27
+ # - datetime - Instance of DateTime class that holds the date value.
28
+ # - date_format - String that holds the Ruby date format. Default: "%d %b %Y %I:%M%p"
29
+ # - show_time - true/false If this is false, the time will not appear in the edit window.
30
+ # - show_calendar - true/false If this is false, Calendar will not appear. Only time will edit.
31
+ #
32
+ def initialize(datetime, date_format = nil, show_time = true, show_calendar = true)
33
+ @show_time = show_time
34
+ @show_calendar = show_calendar
35
+ @date_format = date_format ||= date_format = "%d %b %Y %I:%M%p"
36
+ @date = datetime
37
+ end
38
+
39
+ def show(show_time = true)
40
+ load_glade(__FILE__)
41
+ @hour = @date.strftime("%I").to_f
42
+ @minute = @date.min()
43
+ @am = (@date.hour < 12)
44
+ @pm = !@am
45
+ set_glade_variables()
46
+ @builder["hboxTime"].hide unless @show_time
47
+ @builder["date"].hide unless @show_calendar
48
+ show_window()
49
+ end
50
+
51
+ # displays time according to the @date_format instance variable. If you want to
52
+ # change the appearance of this object, assign a new vale to @date_format.
53
+
54
+ def to_s
55
+ @date.strftime(@date_format)
56
+ end
57
+
58
+ def am__toggled(*args) # :nodoc:
59
+ @builder["pm"].active = !@builder["am"].active?
60
+ end
61
+
62
+ def pm__toggled(*args) # :nodoc:
63
+ @builder["am"].active = !@builder["pm"].active?
64
+ end
65
+
66
+ def buttonSave__clicked(*args) # :nodoc:
67
+ get_glade_variables()
68
+ m = @builder["am"].active? ? "AM" : "PM"
69
+ t = DateTime.strptime("#{@hour.to_i.to_s} #{@minute.to_i.to_s} #{m}", "%I %M %p")
70
+ @date = DateTime.new(@date.year, @date.month, @date.day, t.hour, t.min, 0)
71
+ destroy_window()
72
+ end
73
+
74
+ def buttonCancel__clicked(*args) # :nodoc:
75
+ destroy_window()
76
+ end
77
+
78
+ def <=>(calendar)
79
+ return @date <=> calendar.date
80
+ end
81
+
82
+ def value
83
+ @date
84
+ end
85
+
86
+ end
87
+
88
+ end