1#
2# Demo: MailWasher
3#
4def demoMailWasher(t)
5  init_pics('*checked')
6
7  height = t.font.metrics(:linespace) + 2
8  height = 18 if height < 18
9
10  t.configure(:showroot=>false, :showrootbutton=>false, :showbuttons=>false,
11              :showlines=>false, :itemheight=>height, :selectmode=>:browse,
12              :xscrollincrement=>1)
13
14  pad = 4
15
16  if $Version_1_1_OrLater
17    t.column_create(:text=>'Delete', :textpadx=>pad, :tag=>'delete')
18    t.column_create(:text=>'Bounce', :textpadx=>pad, :tag=>'bounce')
19    t.column_create(:text=>'Status', :width=>80, :textpadx=>pad,
20                    :tag=>'status')
21    t.column_create(:text=>'Size', :width=>40, :textpadx=>pad,
22                    :justify=>:right, :tag=>'size')
23    t.column_create(:text=>'From', :width=>140, :textpadx=>pad, :tag=>'from')
24    t.column_create(:text=>'Subject', :width=>240, :textpadx=>pad,
25                    :tag=>'subject')
26    t.column_create(:text=>'Received', :textpadx=>pad, :arrow=>:up,
27                    :arrowpad=>[4,0], :tag=>'received')
28    t.column_create(:text=>'Attachments', :textpadx=>pad, :tag=>'attachments')
29
30    t.state_define('CHECK')
31
32    t.element_create('imgCheck', :image, :image=>[
33                       @images['checked'], ['CHECK'], @images['unchecked'], []
34                     ])
35
36  else # TreeCtrl 1.0
37    t.column_configure(0, :text=>'Delete', :textpadx=>pad, :tag=>'delete')
38    t.column_configure(1, :text=>'Bounce', :textpadx=>pad, :tag=>'bounce')
39    t.column_configure(2, :text=>'Status', :width=>80, :textpadx=>pad,
40                       :tag=>'status')
41    t.column_configure(3, :text=>'Size', :width=>40, :textpadx=>pad,
42                       :justify=>:right, :tag=>'size')
43    t.column_configure(4, :text=>'From', :width=>140, :textpadx=>pad,
44                       :tag=>'from')
45    t.column_configure(5, :text=>'Subject', :width=>240, :textpadx=>pad,
46                       :tag=>'subject')
47    t.column_configure(6, :text=>'Received', :textpadx=>pad, :arrow=>:up,
48                       :arrowpad=>[4,0], :tag=>'received')
49    t.column_configure(7, :text=>'Attachments', :textpadx=>pad,
50                       :tag=>'attachments')
51
52    t.element_create('imgOff', :image, :image=>@images['unchecked'])
53    t.element_create('imgOn', :image, :image=>@images['checked'])
54  end
55
56  t.element_create('border', :rect, :open=>:nw, :outline=>'gray',
57                   :outlinewidth=>1, :fill=>[@SystemHighlight, ['selected']])
58  t.element_create('txtAny', :text, :lines=>1,
59                   :fill=>[@SystemHighlightText, ['selected']])
60  t.element_create('txtNone', :text, :text=>'none', :lines=>1,
61                   :fill=>[@SystemHighlightText, ['selected']])
62  t.element_create('txtYes', :text, :text=>'yes', :lines=>1,
63                   :fill=>[@SystemHighlightText, ['selected']])
64  t.element_create('txtNormal', :text, :text=>'Normal', :lines=>1,
65                   :fill=>[@SystemHighlightText, ['selected'], '#006800', []])
66  t.element_create('txtPossSpam', :text, :text=>'Possible Spam', :lines=>1,
67                   :fill=>[@SystemHighlightText, ['selected'], '#787800', []])
68  t.element_create('txtProbSpam', :text, :text=>'Probably Spam', :lines=>1,
69                   :fill=>[@SystemHighlightText, ['selected'], '#FF9000', []])
70  t.element_create('txtBlacklist', :text, :text=>'Blacklisted', :lines=>1,
71                   :fill=>[@SystemHighlightText, ['selected'], '#FF5800', []])
72
73  if $Version_1_1_OrLater
74    s = t.style_create('styCheck')
75    t.style_elements(s, ['border', 'imgCheck'])
76    t.style_layout(s, 'border', :detach=>true, :iexpand=>:es)
77    t.style_layout(s, 'imgCheck', :expand=>:news)
78  else
79    ['Off', 'On'].each{|name|
80      s = t.style_create('sty' << name)
81      i = 'img' << name
82      t.style_elements(s, ['border', i])
83      t.style_layout(s, 'border', :detach=>true, :iexpand=>:es)
84      t.style_layout(s, i, :expand=>:news)
85    }
86  end
87
88  pad = 4
89
90  %w(Any None Yes Normal PossSpam ProbSpam Blacklist).each{|name|
91    s = t.style_create('sty' << name)
92    e = 'txt' << name
93    t.style_elements(s, ['border', e])
94    t.style_layout(s, 'border', :detach=>true, :iexpand=>:es)
95    t.style_layout(s, e, :padx=>pad, :squeeze=>:x, :expand=>:ns)
96  }
97
98  [
99    ['baldy@spammer.com',  "Your hair is thinning"],
100    ['flat@spammer.com', "Your breasts are too small"],
101    ['tiny@spammer.com',  "Your penis is too small"],
102    ['dumbass@spammer.com', "You are not very smart"],
103    ['bankrobber@spammer.com', "You need more money"],
104    ['loser@spammer.com', "You need better friends"],
105    ['gossip@spammer.com', "Find out what your coworkers think about you"],
106    ['whoami@spammer.com', "Find out what you think about yourself"],
107    ['downsized@spammer.com', "You need a better job"],
108    ['poorhouse@spammer.com', "Your mortgage is a joke"],
109    ['spam4ever@spammer.com', "You need more spam"]
110  ].each{|frm, subj|
111    item = t.item_create
112    status = ['styNormal','styPossSpam','styProbSpam','styBlacklist'][rand(4)]
113    attachments = ['styNone','styYes'][rand(2)]
114
115    if $Version_1_1_OrLater
116      delete = [false, true][rand(2)]
117      bounce = [false, true][rand(2)]
118      t.item_style_set(item,
119                       0, 'styCheck', 1, 'styCheck', 2, status, 3, 'styAny',
120                       4, 'styAny', 5, 'styAny', 6, 'styAny', 7, attachments)
121      t.item_state_forcolumn(item, 'delete', 'CHECK') if delete
122      t.item_state_forcolumn(item, 'bounce', 'CHECK') if bounce
123
124    else # TreeCtrl 1.0
125      delete = ['styOn', 'styOff'][rand(2)]
126      bounce = ['styOn', 'styOff'][rand(2)]
127      t.item_style_set(item,
128                       0, delete, 1, bounce, 2, status, 3, 'styAny',
129                       4, 'styAny', 5, 'styAny', 6, 'styAny', 7, attachments)
130    end
131
132    bytes = 512 + rand(1024 * 12)
133    size = "#{bytes / 1024 + 1}KB"
134    seconds = Tk::Clock.seconds - rand(100000)
135    received = Tk::Clock.format(seconds, '%d/%m/%y %I:%M %p')
136    t.item_text(item, 3, size, 4, frm, 5, subj, 6, received)
137    t.item_lastchild(:root, item)
138  }
139
140  sortColumn = 6
141  t.notify_bind(t, 'Header-invoke',
142                proc{|c, w|
143                  if c == sortColumn
144                    if w.column_cget(sortColumn, :arrow) == 'down'
145                      order = :increasing
146                      arrow = :up
147                    else
148                      order = :decreasing
149                      arrow = :down
150                    end
151                  else
152                    if w.column_cget(sortColumn, :arrow) == 'down'
153                      order = :decreasing
154                      arrow = :down
155                    else
156                      order = :increasing
157                      arrow = :up
158                    end
159                    w.column_configure(sortColumn, :arrow=>:none)
160                    sortColumn = c
161                  end
162                  w.column_configure(c, :arrow=>arrow)
163                  case w.column_cget(c, :tag)
164                  when 'bounce', 'delete'
165                    w.item_sort(:root, order,
166                                {
167                                  :column=>c,
168                                  :command=>proc{|item1, item2|
169                                    compareOnOff(w, c, item1, item2)
170                                  }
171                                },
172                                { :column=>'subject', :dictionary=>true })
173                  when 'status'
174                    w.item_sort(:root, order,
175                                { :column=>c, :dictionary=>true })
176                  when 'from'
177                    w.item_sort(:root, order,
178                                { :column=>c, :dictionary=>true },
179                                { :column=>'subject', :dictionary=>true })
180                  when 'subject'
181                    w.item_sort(:root, order,
182                                { :column=>c, :dictionary=>true })
183                  when 'size'
184                    w.item_sort(:root, order,
185                                { :column=>c, :dictionary=>true },
186                                { :column=>'subject', :dictionary=>true })
187                  when 'received'
188                    w.item_sort(:root, order,
189                                { :column=>c, :dictionary=>true },
190                                { :column=>'subject', :dictionary=>true })
191                  when 'attachments'
192                    w.item_sort(:root, order,
193                                { :column=>c, :dictionary=>true },
194                                { :column=>'subject', :dictionary=>true })
195                  end
196                }, '%C %T')
197
198  mailWasher = TkBindTag.new
199
200  if $Version_1_1_OrLater
201    mailWasher.bind('ButtonPress-1',
202                    proc{|w, x, y|
203                      id = w.identify(x, y)
204                      if id.empty?
205                      elsif id[0] == 'header'
206                      else
207                        what, item, where, arg1, arg2, arg3 = id
208                        if where == 'column'
209                          tag = w.column_cget(arg1, :tag)
210                          if tag == 'delete' || tag == 'bounce'
211                            w.item_state_forcolumn(item, arg1, '~CHECK')
212                          end
213                        end
214                      end
215                    }, '%W %x %y')
216  else # TreeCtrl 1.0
217    mailWasher.bind('ButtonPress-1',
218                    proc{|w, x, y|
219                      id = w.identify(x, y)
220                      if id.empty?
221                      elsif id[0] == 'header'
222                      else
223                        what, item, where, arg1, arg2, arg3 = id
224                        if where == 'column'
225                          tag = w.column_cget(arg1, :tag)
226                          if tag == 'delete' || tag == 'bounce'
227                            style = w.item_style_set(item, arg1)
228                            if style == 'styOn'
229                              style = 'styOff'
230                            else
231                              style = 'styOn'
232                            end
233                            w.item_style_set(item, arg1, style)
234                            @display_styles_in_item.call(item)
235                          end
236                        end
237                      end
238                    }, '%W %x %y')
239  end
240
241  t.bindtags = [t, mailWasher, Tk::TreeCtrl, t.winfo_toplevel, TkBindTag::ALL]
242end
243
244if $Version_1_1_OrLater
245  def compareOnOff(w, c, item1, item2)
246    s1 = w.item_state_forcolumn(item1, c)
247    s2 = w.item_state_forcolumn(item2, c)
248    if (s1 == s2)
249      0
250    elsif (s1 == 'CHECK')
251      -1
252    else
253      1
254    end
255  end
256
257else # TreeCtrl 1.0
258  def compareOnOff(w, c, item1, item2)
259    s1 = w.item_style_set(item1, c)
260    s2 = w.item_style_set(item2, c)
261    if (s1 == s2)
262      0
263    elsif (s1 == 'styOff')
264      -1
265    else
266      1
267    end
268  end
269end
270