File: liststore.rb

package info (click to toggle)
ruby-gnome2 0.12.0-2sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,820 kB
  • ctags: 7,421
  • sloc: ansic: 61,387; ruby: 17,307; makefile: 85; xml: 35; sql: 13
file content (351 lines) | stat: -rw-r--r-- 8,860 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
=begin

  liststore.rb - a part of testgtk.c rewritten in Ruby/GTK2

  Copyright (C) 2002,2003 Ruby-GNOME2 Project Team

  $Id: liststore.rb,v 1.3 2003/02/01 16:46:23 mutoh Exp $

=end

require 'sample'

$gtk_mini_xpm = [
  "15 20 17 1",
  "       c None",
  ".      c #14121F",
  "+      c #278828",
  "@      c #9B3334",
  "#      c #284C72",
  "$      c #24692A",
  "%      c #69282E",
  "&      c #37C539",
  "*      c #1D2F4D",
  "=      c #6D7076",
  "-      c #7D8482",
  ";      c #E24A49",
  ">      c #515357",
  ",      c #9B9C9B",
  "'      c #2FA232",
  ")      c #3CE23D",
  "!      c #3B6CCB",
  "               ",
  "      ***>     ",
  "    >.*!!!*    ",
  "   ***....#*=  ",
  "  *!*.!!!**!!# ",
  " .!!#*!#*!!!!# ",
  " @%#!.##.*!!$& ",
  " @;%*!*.#!#')) ",
  " @;;@%!!*$&)'' ",
  " @%.%@%$'&)$+' ",
  " @;...@$'*'*)+ ",
  " @;%..@$+*.')$ ",
  " @;%%;;$+..$)# ",
  " @;%%;@$$$'.$# ",
  " %;@@;;$$+))&* ",
  "  %;;;@+$&)&*  ",
  "   %;;@'))+>   ",
  "    %;@'&#     ",
  "     >%$$      ",
  "      >=       " ]

class ListStoreSample < SampleWindow

  def initialize
    super("Gtk::ListStore")

    vbox = Gtk::VBox.new
    self.add(vbox)

    scrolled_win = Gtk::ScrolledWindow.new
    scrolled_win.border_width = 5
    scrolled_win.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)

    data = ["auto resize", "not resizeable", "max width 100", "min width 50",
	"hide column", "Title 5", "Title 6", "Title 7",
	"Title 8",  "Title 9",  "Title 10", "Title 11" ]
    @model = Gtk::ListStore.new(*data.collect{|label| label.class})
    treeview = Gtk::TreeView.new(@model)
    renderer = Gtk::CellRendererText.new
    data.each_index do |cnt|
      column = Gtk::TreeViewColumn.new(data[cnt], renderer, {:text => cnt})
      column.sort_column_id = cnt
      append_column(column)
    end
    scrolled_win.add(treeview)
    treeview.selection.signal_connect("changed") do |w, c| click_column(c) end

    # control buttons
    hbox = Gtk::HBox::new(false, 5)
    hbox.border_width = 5
    vbox.pack_start(hbox, false, false, 0)

    button = Gtk::Button::new("Insert Row")
    hbox.pack_start(button)
    button.signal_connect("clicked") do insert_row end

    button = Gtk::Button::new("Add 1,000 Rows With Pixmaps")
    hbox.pack_start(button)
    button.signal_connect("clicked") do add1000 end

    button = Gtk::Button::new("Add 10,000 Rows");
    hbox.pack_start(button)
    button.signal_connect("clicked") do add10000 end

    # second layer of buttons
    hbox = Gtk::HBox::new(false, 5)
    hbox.border_width = 5
    vbox.pack_start(hbox, false, false, 0);

    button = Gtk::Button::new("Clear List");
    hbox.pack_start(button);
    button.signal_connect("clicked") do clear end

    button = Gtk::Button::new("Remove Selection")
    hbox.pack_start(button)
    button.signal_connect("clicked") do remove_selection end

    undo_button = Gtk::Button::new("Undo Selection")
    hbox.pack_start(undo_button)
    undo_button.signal_connect("clicked") do undo_selection end

    button = Gtk::Button::new("Warning Test")
    hbox.pack_start(button)
    button.signal_connect("clicked") do warning_test end

    # third layer of buttons
    hbox = Gtk::HBox::new(false, 5)
    hbox.border_width = 5
    vbox.pack_start(hbox, false, false, 0);

    check = Gtk::CheckButton::new("Show Title Buttons")
    hbox.pack_start(check)
    check.signal_connect("clicked") do |w| toggle_title_buttons(w) end
    check.set_active(true);

    check = Gtk::CheckButton::new("Reorderable")
    hbox.pack_start(check)
    check.signal_connect("clicked") do |w| toggle_reorderable(w) end
    check.set_active(true)

    label = Gtk::Label::new("Selection Mode :")
    hbox.pack_start(label, false, true, 0);

    clist_omenu = build_option_menu(
      [ OptionMenuItem.new("Single",   proc { |w| toggle_sel_mode(w) }),
        OptionMenuItem.new("Browse",   proc { |w| toggle_sel_mode(w) }),
        OptionMenuItem.new("Multiple", proc { |w| toggle_sel_mode(w) }),
        OptionMenuItem.new("Extended", proc { |w| toggle_sel_mode(w) }) ],
	3)

    hbox.pack_start(clist_omenu, false, true, 0);

    vbox.pack_start(scrolled_win)
    @clist.set_row_height(18)
    @clist.set_usize(-1, 300)

    for i in 1 .. 11 do
      @clist.set_column_width(i, 80)
    end

    @clist.set_column_auto_resize(0, true)
    @clist.set_column_resizeable(1, false)
    @clist.set_column_max_width(2, 100)
    @clist.set_column_min_width(3, 50)
    @clist.set_selection_mode(Gtk::SELECTION_EXTENDED)
    @clist.set_column_justification(1, Gtk::JUSTIFY_RIGHT)
    @clist.set_column_justification(2, Gtk::JUSTIFY_CENTER)

    texts = []
    for i in 0 .. 11 do
      texts[i] = sprintf("Column %d", i)
    end
    texts[1, 2] = [ "Right", "Center" ]

    style = Gtk::Style::new()
    style.set_fg(Gtk::STATE_NORMAL, 56000, 0, 0);
    style.set_base(Gtk::STATE_NORMAL, 0, 56000, 32000);

    for i in 0 .. 9 do
      texts[0] = sprintf("CListRow %d", i)
      @clist.append(texts)

      case i % 4
      when 2 then
	@clist.set_row_style(i, style);
      else
        @clist.set_cell_style(i, i%4, style);
      end
    end

    separator = Gtk::HSeparator::new()
    vbox.pack_start(separator, false, true, 0)

    hbox = Gtk::HBox::new()
    vbox.pack_start(hbox, false, true, 0)

    button = Gtk::Button::new("close")
    button.border_width = 10
    hbox.pack_start(button)
    button.signal_connect("clicked") do destroy end

    button.set_flags(Gtk::Widget::CAN_DEFAULT)
    button.grab_default

    @add_remove = false
    @style1 = nil
    @style2 = nil
    @style3 = nil
  end

  private
  def toggle_sel_mode(rmitem)
    return unless rmitem.mapped?

    i = 3
    group = rmitem.group
    group.each do |g|
      break if g.active?
      i -= 1
    end
    @clist.set_selection_mode(i)
  end

  private
  def click_column(column)
    if column == 4 then
      @clist.set_column_visibility(column, false)
    elsif column == @clist.sort_column then
      if @clist.sort_type == Gtk::SORT_ASCENDING
        @clist.sort_type = Gtk::SORT_DESCENDING
      else
        @clist.sort_type = Gtk::SORT_ASCENDING
      end
    else
      @clist.sort_column = column
    end
    @clist.sort
  end

  private
  def add1000
    pixmap, mask = Gdk::Pixmap::create_from_xpm_d(window,
					    style.white,
					    $gtk_mini_xpm)
    texts = []
    for i in 0 .. 11 do
      texts[i] = sprintf("Column %d", i)
    end
    texts[3] = nil
    texts[1] = "Right"
    texts[2] = "Center"
    @clist.freeze
    for i in 0..999 do
      texts[0] = sprintf("CListRow %d", rand(9999))
      row = @clist.append(texts)
      @clist.set_pixtext(row, 3, "gtk+", 5, pixmap, mask)
    end
    @clist.thaw
  end

  private
  def add10000
    texts = []
    for i in 0 .. 11 do
      texts[i] = sprintf("Column %d", i)
    end
    texts[1] = "Right"
    texts[2] = "Center"
    @clist.freeze
    for i in 0..9999 do
      texts[0] = sprintf("CListRow %d", rand(9999))
      row = @clist.append(texts)
    end
    @clist.thaw
  end

  private
  def clear
    @clist.clear
  end

  private
  def remove_selection
    @clist.freeze
    @clist.each_selection do
      | row |
      @clist.remove_row(row)
    end
    if @clist.selection_mode == Gtk::SELECTION_EXTENDED and
       @clist.focus_row >= 0 then
      @clist.select_row(@clist.focus_row, -1)
    end
    @clist.thaw
  end

  private
  def toggle_title_buttons(cbutton)
    if cbutton.active? then
      @clist.column_titles_show
    else
      @clist.column_titles_hide
    end
  end

  private
  def toggle_reorderable(cbutton)
    @clist.set_reorderable(cbutton.active?)
  end

  private
  def insert_row
    texts = [ "This", "is an", "inserted", "row.",
	      "This", "is an", "inserted", "row.",
	      "This", "is an", "inserted", "row." ]

    if @clist.focus_row >= 0 then
      row = @clist.insert(@clist.focus_row, texts)
    else
      row = @clist.prepend(texts)
    end

    if @style1 == nil then

      @style1 = @clist.style.copy
      @style1.set_base(Gtk::STATE_NORMAL, 0, 56000, 0)
      @style1.set_base(Gtk::STATE_SELECTED, 32000, 0, 56000)

      @style2 = @clist.style.copy
      @style2.set_fg(Gtk::STATE_NORMAL, 0, 56000, 0)
      @style2.set_fg(Gtk::STATE_SELECTED, 32000, 0, 56000)

      @style3 = @clist.style.copy
      @style3.set_fg(Gtk::STATE_NORMAL, 0, 56000, 0)
      @style3.set_base(Gtk::STATE_NORMAL, 32000, 0, 56000)
    end

    @clist.set_cell_style(row, 3, @style1)
    @clist.set_cell_style(row, 4, @style2)
    @clist.set_cell_style(row, 0, @style3)
  end

  private
  def warning_test
    @add_remove = !@add_remove
    child = Gtk::Label::new("Test");
    if @add_remove then
      @clist.add(child)
    else
      child.set_parent(child)
      @clist.remove(child)
      child.set_parent(nil)
    end
  end

  private
  def undo_selection
    @clist.undo_selection
  end

end