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
|
#!/usr/bin/env ruby
#
# viewIcons.rb
#
# -- Display icons from icon library.
#
# -- Copy the clicked icon data (command string of creating
# a TkPhotoImage instance) to the clipboard.
#
require 'tk'
require 'tkextlib/ICONS'
class ViewIcons
#####################################
private
#####################################
def _create_controls
@controls = base = TkFrame.new
columns = TkFrame.new(base)
line1 = TkFrame.new(base, :height=>2, :borderwidth=>1, :relief=>:sunken)
line2 = TkFrame.new(base, :height=>2, :borderwidth=>1, :relief=>:sunken)
lbl_library = TkLabel.new(base, :font=>@boldfont, :text=>'Library')
lbl_groups = TkLabel.new(base, :font=>@boldfont, :text=>'Groups')
lbl_columns = TkLabel.new(base, :font=>@boldfont, :text=>'Columns')
ent_library = TkEntry.new(base, :width=>50, :textvariable=>@library)
ent_groups = TkEntry.new(base, :width=>50, :textvariable=>@groups)
btn_browse = TkButton.new(base, :text=>'Browse',
:command=>method(:select_icons))
btn_view = TkButton.new(base, :text=>'View',
:command=>method(:display_icons))
btn_exit = TkButton.new(base, :text=>'Exit', :command=>proc{exit})
@column_btns = {}
6.step(20, 2){|i|
@column_btns[i] = TkButton.new(columns,
:text=>i.to_s, :width=>2,
:command=>proc{set_columns(i)}
).pack(:side=>:left)
}
@column_btns[@columns][:relief] = :sunken
lbl_library.grid(:row=>0, :column=>0, :padx=>4)
ent_library.grid(:row=>0, :column=>1)
btn_browse.grid(:row=>0, :column=>2, :padx=>4, :pady=>2, :sticky=>:ew)
line1.grid(:row=>1, :column=>0, :pady=>2, :columnspan=>3, :sticky=>:ew)
lbl_groups.grid(:row=>2, :column=>0, :padx=>4)
ent_groups.grid(:row=>2, :column=>1)
btn_view.grid(:row=>2, :column=>2, :padx=>4, :pady=>2, :sticky=>:ew)
line1.grid(:row=>3, :column=>0, :pady=>2, :columnspan=>3, :sticky=>:ew)
lbl_columns.grid(:row=>4, :column=>0, :padx=>4)
columns.grid(:row=>4, :column=>1, :padx=>2, :sticky=>:ew)
btn_exit.grid(:row=>4, :column=>2, :padx=>4, :pady=>2, :sticky=>:ew)
base.pack
ent_library.bind('Return', method(:display_icons), '')
ent_groups.bind('Return', method(:display_icons), '')
end
def _create_display
base = TkFrame.new(:borderwidth=>2, :relief=>:sunken)
@icons_window = icons = TkCanvas.new(base)
xscr = icons.xscrollbar(TkScrollbar.new(base))
yscr = icons.yscrollbar(TkScrollbar.new(base))
icons.grid(:row=>0, :column=>0, :sticky=>:news)
yscr.grid(:row=>0, :column=>1, :sticky=>:ns)
xscr.grid(:row=>1, :column=>0, :sticky=>:ew)
base.grid_columnconfigure(0, :weight=>1)
base.grid_columnconfigure(1, :weight=>0)
base.grid_rowconfigure(0, :weight=>1)
base.grid_rowconfigure(1, :weight=>0)
# yscr.pack(:side=>:right, :fill=>:y)
# xscr.pack(:side=>:bottom, :fill=>:x)
# icons.pack(:side=>:left, :fill=>:both, :expand=>true)
@icons_layout = TkFrame.new(icons).pack
TkcWindow.create(icons, 0, 0, :anchor=>:nw, :window=>@icons_layout)
@icons_layout.bind('Configure', method(:layout_resize), '')
base.pack(:expand=>true, :fill=>:both)
end
def _create_info_window
@info_window = TkToplevel.new(:background=>'lightyellow', :borderwidth=>1,
:relief=>:solid){|w|
lbl_name = TkLabel.new(w, :text=>'Name', :background=>'lightyellow',
:font=>@boldfont, :justify=>:left)
lbl_grps = TkLabel.new(w, :text=>'Groups', :background=>'lightyellow',
:font=>@boldfont, :justify=>:left)
lbl_type = TkLabel.new(w, :text=>'Type', :background=>'lightyellow',
:font=>@boldfont, :justify=>:left)
lbl_size = TkLabel.new(w, :text=>'Size', :background=>'lightyellow',
:font=>@boldfont, :justify=>:left)
lbl_name.grid(:row=>0, :column=>0, :sticky=>:w)
lbl_grps.grid(:row=>1, :column=>0, :sticky=>:w)
lbl_type.grid(:row=>2, :column=>0, :sticky=>:w)
lbl_size.grid(:row=>3, :column=>0, :sticky=>:w)
@name = TkLabel.new(w, :background=>'lightyellow', :justify=>:left)
@grps = TkLabel.new(w, :background=>'lightyellow', :justify=>:left)
@type = TkLabel.new(w, :background=>'lightyellow', :justify=>:left)
@size = TkLabel.new(w, :background=>'lightyellow', :justify=>:left)
@name.grid(:row=>0, :column=>1, :sticky=>:w)
@grps.grid(:row=>1, :column=>1, :sticky=>:w)
@type.grid(:row=>2, :column=>1, :sticky=>:w)
@size.grid(:row=>3, :column=>1, :sticky=>:w)
def name(txt)
@name['text'] = txt
end
def groups(txt)
@grps['text'] = txt
end
def type(txt)
@type['text'] = txt
end
def size(txt)
@size['text'] = txt
end
overrideredirect(true)
withdraw
}
end
def initialize(init_path = Tk::LIBRARY)
init_path = Tk::LIBRARY unless init_path
init_path = File.expand_path(init_path)
if File.directory?(init_path)
@initial_dir = init_path
@initial_file = 'tkIcons'
else
@initial_dir = File.dirname(init_path)
@initial_file = File.basename(init_path)
end
if Tk::PLATFORM['platform'] == 'unix'
TkOption.add('*HighlightThickness', 0)
end
@columns = 14
@command = ""
@delay_timer = nil
dummy = TkLabel.new
@font = dummy.font
@boldfont = TkFont.new(@font, :weight=>:bold)
@icons = {}
@icon_name = {}
@icon_info = {}
@library = TkVariable.new(File.join(@initial_dir, @initial_file))
@groups = TkVariable.new('*')
_create_controls
_create_display
_create_info_window
Tk.root.title('viewIcons')
layout_resize
Tk.root.resizable(false, true)
display_icons
end
def init_info(item, name)
@icon_name[item] = name
item.bind('Button-1', method(:clip_info), '%W')
item.bind('Enter', method(:delay_info), '%W')
item.bind('Leave', method(:cancel_info), '')
end
def delay_info(item)
cancel_info
@delay_timer = TkTimer.new(200, 1, proc{ show_info(item) }).start
end
def cancel_info
if @delay_timer
@delay_timer.cancel
@delay_timer = nil
end
@info_window.withdraw
end
def show_info(item)
name, groups, type, size = @icon_info[@icon_name[item]]
@info_window.name(name)
@info_window.groups(groups)
@info_window.type(type)
@info_window.size(size)
info_x = item.winfo_rootx + 10
info_y = item.winfo_rooty + item.winfo_height
@info_window.geometry("+#{info_x}+#{info_y}")
@info_window.deiconify
@info_window.raise
@delay_timer = nil
end
def primary_transfer(offset, max_chars)
@command
end
def lost_selection
@command = ""
end
def clip_info(item)
name = @icon_name[item]
data_width = 60
cmd = "#{name} = TkPhotoImage.new(:data=><<'EOD')\n"
icon_data = Tk::ICONS.query(name, :file=>@library.value, :items=>'d')[0][0]
icon_data.scan(/.{1,#{data_width}}/m){|s| cmd << ' ' << s << "\n"}
cmd << "EOD\n"
@command = cmd
TkClipboard.clear
TkClipboard.append(@command)
if Tk::PLATFORM['platform'] == 'unix'
TkSelection.handle(Tk.root, method(:primary_transfer),
:selection=>'PRIMARY')
TkSelection.set_owner(Tk.root, :selection=>'PRIMARY',
:command=>method(:lost_selection))
end
Tk.bell
end
def layout_resize
Tk.update
bbox = @icons_window.bbox('all')
width = @controls.winfo_width - @icons_window.yscrollbar.winfo_width - 8
@icons_window.configure(:width=>width, :scrollregion=>bbox,
:xscrollincrement=>'0.1i',
:yscrollincrement=>'0.1i')
end
def select_icons
new_lib = Tk.getOpenFile(:initialdir=>@initial_dir,
:initialfile=>'tkIcons',
:title=>'Select Icon Library',
:filetypes=>[
['Icon Libraries', ['tkIcons*']],
['All Files', ['*']]
])
@library.value = new_lib if new_lib.length != 0
display_icons
end
def display_icons
column = 0
limit = @columns - 1
row = 0
unless File.exist?(@library.value)
Tk.messageBox(:icon=>'warning', :message=>'File does not exist',
:title=>'viewIcons')
return
end
cursor = Tk.root[:cursor]
Tk.root[:cursor] = 'watch'
Tk::ICONS.delete(@icons)
@icons_frame.destroy if @icons_frame
@icons_frame = TkFrame.new(@icons_layout).pack
@icons = Tk::ICONS.create(:file=>@library.value, :group=>@groups.value)
Tk::ICONS.query(:file=>@library.value, :group=>@groups.value).each{|inf|
name = inf[0]
@icon_info[name] = inf
lbl = TkLabel.new(@icons_frame, :image=>"::icon::#{name}")
lbl.grid(:column=>column, :row=>row, :padx=>3, :pady=>3)
# lbl.grid_columnconfigure column
init_info(lbl, name)
if column == limit
column = 0
row += 1
else
column += 1
end
}
Tk.root[:cursor] = cursor
end
def set_columns(columns)
@columns = columns
6.step(20, 2){|i| @column_btns[i][:relief] = :raised }
@column_btns[@columns][:relief] = :sunken
display_icons
end
end
ViewIcons.new(ARGV[0])
Tk.mainloop
|