summaryrefslogtreecommitdiff
path: root/jni/ruby/ext/tk/sample/tkextlib/treectrl/mailwasher.rb
blob: 1b51bb9b218ffa18f150722470298fa3e025b3cd (plain)
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
#
# Demo: MailWasher
#
def demoMailWasher(t)
  init_pics('*checked')

  height = t.font.metrics(:linespace) + 2
  height = 18 if height < 18

  t.configure(:showroot=>false, :showrootbutton=>false, :showbuttons=>false,
              :showlines=>false, :itemheight=>height, :selectmode=>:browse,
              :xscrollincrement=>1)

  pad = 4

  if $Version_1_1_OrLater
    t.column_create(:text=>'Delete', :textpadx=>pad, :tag=>'delete')
    t.column_create(:text=>'Bounce', :textpadx=>pad, :tag=>'bounce')
    t.column_create(:text=>'Status', :width=>80, :textpadx=>pad,
                    :tag=>'status')
    t.column_create(:text=>'Size', :width=>40, :textpadx=>pad,
                    :justify=>:right, :tag=>'size')
    t.column_create(:text=>'From', :width=>140, :textpadx=>pad, :tag=>'from')
    t.column_create(:text=>'Subject', :width=>240, :textpadx=>pad,
                    :tag=>'subject')
    t.column_create(:text=>'Received', :textpadx=>pad, :arrow=>:up,
                    :arrowpad=>[4,0], :tag=>'received')
    t.column_create(:text=>'Attachments', :textpadx=>pad, :tag=>'attachments')

    t.state_define('CHECK')

    t.element_create('imgCheck', :image, :image=>[
                       @images['checked'], ['CHECK'], @images['unchecked'], []
                     ])

  else # TreeCtrl 1.0
    t.column_configure(0, :text=>'Delete', :textpadx=>pad, :tag=>'delete')
    t.column_configure(1, :text=>'Bounce', :textpadx=>pad, :tag=>'bounce')
    t.column_configure(2, :text=>'Status', :width=>80, :textpadx=>pad,
                       :tag=>'status')
    t.column_configure(3, :text=>'Size', :width=>40, :textpadx=>pad,
                       :justify=>:right, :tag=>'size')
    t.column_configure(4, :text=>'From', :width=>140, :textpadx=>pad,
                       :tag=>'from')
    t.column_configure(5, :text=>'Subject', :width=>240, :textpadx=>pad,
                       :tag=>'subject')
    t.column_configure(6, :text=>'Received', :textpadx=>pad, :arrow=>:up,
                       :arrowpad=>[4,0], :tag=>'received')
    t.column_configure(7, :text=>'Attachments', :textpadx=>pad,
                       :tag=>'attachments')

    t.element_create('imgOff', :image, :image=>@images['unchecked'])
    t.element_create('imgOn', :image, :image=>@images['checked'])
  end

  t.element_create('border', :rect, :open=>:nw, :outline=>'gray',
                   :outlinewidth=>1, :fill=>[@SystemHighlight, ['selected']])
  t.element_create('txtAny', :text, :lines=>1,
                   :fill=>[@SystemHighlightText, ['selected']])
  t.element_create('txtNone', :text, :text=>'none', :lines=>1,
                   :fill=>[@SystemHighlightText, ['selected']])
  t.element_create('txtYes', :text, :text=>'yes', :lines=>1,
                   :fill=>[@SystemHighlightText, ['selected']])
  t.element_create('txtNormal', :text, :text=>'Normal', :lines=>1,
                   :fill=>[@SystemHighlightText, ['selected'], '#006800', []])
  t.element_create('txtPossSpam', :text, :text=>'Possible Spam', :lines=>1,
                   :fill=>[@SystemHighlightText, ['selected'], '#787800', []])
  t.element_create('txtProbSpam', :text, :text=>'Probably Spam', :lines=>1,
                   :fill=>[@SystemHighlightText, ['selected'], '#FF9000', []])
  t.element_create('txtBlacklist', :text, :text=>'Blacklisted', :lines=>1,
                   :fill=>[@SystemHighlightText, ['selected'], '#FF5800', []])

  if $Version_1_1_OrLater
    s = t.style_create('styCheck')
    t.style_elements(s, ['border', 'imgCheck'])
    t.style_layout(s, 'border', :detach=>true, :iexpand=>:es)
    t.style_layout(s, 'imgCheck', :expand=>:news)
  else
    ['Off', 'On'].each{|name|
      s = t.style_create('sty' << name)
      i = 'img' << name
      t.style_elements(s, ['border', i])
      t.style_layout(s, 'border', :detach=>true, :iexpand=>:es)
      t.style_layout(s, i, :expand=>:news)
    }
  end

  pad = 4

  %w(Any None Yes Normal PossSpam ProbSpam Blacklist).each{|name|
    s = t.style_create('sty' << name)
    e = 'txt' << name
    t.style_elements(s, ['border', e])
    t.style_layout(s, 'border', :detach=>true, :iexpand=>:es)
    t.style_layout(s, e, :padx=>pad, :squeeze=>:x, :expand=>:ns)
  }

  [
    ['baldy@spammer.com',  "Your hair is thinning"],
    ['flat@spammer.com', "Your breasts are too small"],
    ['tiny@spammer.com',  "Your penis is too small"],
    ['dumbass@spammer.com', "You are not very smart"],
    ['bankrobber@spammer.com', "You need more money"],
    ['loser@spammer.com', "You need better friends"],
    ['gossip@spammer.com', "Find out what your coworkers think about you"],
    ['whoami@spammer.com', "Find out what you think about yourself"],
    ['downsized@spammer.com', "You need a better job"],
    ['poorhouse@spammer.com', "Your mortgage is a joke"],
    ['spam4ever@spammer.com', "You need more spam"]
  ].each{|frm, subj|
    item = t.item_create
    status = ['styNormal','styPossSpam','styProbSpam','styBlacklist'][rand(4)]
    attachments = ['styNone','styYes'][rand(2)]

    if $Version_1_1_OrLater
      delete = [false, true][rand(2)]
      bounce = [false, true][rand(2)]
      t.item_style_set(item,
                       0, 'styCheck', 1, 'styCheck', 2, status, 3, 'styAny',
                       4, 'styAny', 5, 'styAny', 6, 'styAny', 7, attachments)
      t.item_state_forcolumn(item, 'delete', 'CHECK') if delete
      t.item_state_forcolumn(item, 'bounce', 'CHECK') if bounce

    else # TreeCtrl 1.0
      delete = ['styOn', 'styOff'][rand(2)]
      bounce = ['styOn', 'styOff'][rand(2)]
      t.item_style_set(item,
                       0, delete, 1, bounce, 2, status, 3, 'styAny',
                       4, 'styAny', 5, 'styAny', 6, 'styAny', 7, attachments)
    end

    bytes = 512 + rand(1024 * 12)
    size = "#{bytes / 1024 + 1}KB"
    seconds = Tk::Clock.seconds - rand(100000)
    received = Tk::Clock.format(seconds, '%d/%m/%y %I:%M %p')
    t.item_text(item, 3, size, 4, frm, 5, subj, 6, received)
    t.item_lastchild(:root, item)
  }

  sortColumn = 6
  t.notify_bind(t, 'Header-invoke',
                proc{|c, w|
                  if c == sortColumn
                    if w.column_cget(sortColumn, :arrow) == 'down'
                      order = :increasing
                      arrow = :up
                    else
                      order = :decreasing
                      arrow = :down
                    end
                  else
                    if w.column_cget(sortColumn, :arrow) == 'down'
                      order = :decreasing
                      arrow = :down
                    else
                      order = :increasing
                      arrow = :up
                    end
                    w.column_configure(sortColumn, :arrow=>:none)
                    sortColumn = c
                  end
                  w.column_configure(c, :arrow=>arrow)
                  case w.column_cget(c, :tag)
                  when 'bounce', 'delete'
                    w.item_sort(:root, order,
                                {
                                  :column=>c,
                                  :command=>proc{|item1, item2|
                                    compareOnOff(w, c, item1, item2)
                                  }
                                },
                                { :column=>'subject', :dictionary=>true })
                  when 'status'
                    w.item_sort(:root, order,
                                { :column=>c, :dictionary=>true })
                  when 'from'
                    w.item_sort(:root, order,
                                { :column=>c, :dictionary=>true },
                                { :column=>'subject', :dictionary=>true })
                  when 'subject'
                    w.item_sort(:root, order,
                                { :column=>c, :dictionary=>true })
                  when 'size'
                    w.item_sort(:root, order,
                                { :column=>c, :dictionary=>true },
                                { :column=>'subject', :dictionary=>true })
                  when 'received'
                    w.item_sort(:root, order,
                                { :column=>c, :dictionary=>true },
                                { :column=>'subject', :dictionary=>true })
                  when 'attachments'
                    w.item_sort(:root, order,
                                { :column=>c, :dictionary=>true },
                                { :column=>'subject', :dictionary=>true })
                  end
                }, '%C %T')

  mailWasher = TkBindTag.new

  if $Version_1_1_OrLater
    mailWasher.bind('ButtonPress-1',
                    proc{|w, x, y|
                      id = w.identify(x, y)
                      if id.empty?
                      elsif id[0] == 'header'
                      else
                        what, item, where, arg1, arg2, arg3 = id
                        if where == 'column'
                          tag = w.column_cget(arg1, :tag)
                          if tag == 'delete' || tag == 'bounce'
                            w.item_state_forcolumn(item, arg1, '~CHECK')
                          end
                        end
                      end
                    }, '%W %x %y')
  else # TreeCtrl 1.0
    mailWasher.bind('ButtonPress-1',
                    proc{|w, x, y|
                      id = w.identify(x, y)
                      if id.empty?
                      elsif id[0] == 'header'
                      else
                        what, item, where, arg1, arg2, arg3 = id
                        if where == 'column'
                          tag = w.column_cget(arg1, :tag)
                          if tag == 'delete' || tag == 'bounce'
                            style = w.item_style_set(item, arg1)
                            if style == 'styOn'
                              style = 'styOff'
                            else
                              style = 'styOn'
                            end
                            w.item_style_set(item, arg1, style)
                            @display_styles_in_item.call(item)
                          end
                        end
                      end
                    }, '%W %x %y')
  end

  t.bindtags = [t, mailWasher, Tk::TreeCtrl, t.winfo_toplevel, TkBindTag::ALL]
end

if $Version_1_1_OrLater
  def compareOnOff(w, c, item1, item2)
    s1 = w.item_state_forcolumn(item1, c)
    s2 = w.item_state_forcolumn(item2, c)
    if (s1 == s2)
      0
    elsif (s1 == 'CHECK')
      -1
    else
      1
    end
  end

else # TreeCtrl 1.0
  def compareOnOff(w, c, item1, item2)
    s1 = w.item_style_set(item1, c)
    s2 = w.item_style_set(item2, c)
    if (s1 == s2)
      0
    elsif (s1 == 'styOff')
      -1
    else
      1
    end
  end
end