Class: TkImg_demo

Inherits:
Object show all
Defined in:
sample/tkextlib/tkimg/demo.rb

Overview

Make the Image format available.

Instance Method Summary collapse

Constructor Details

#initializeTkImg_demo

Returns a new instance of TkImg_demo.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'sample/tkextlib/tkimg/demo.rb', line 16

def initialize
  img_version = Tk::Img.package_version

  @typeFrame = Hash.new
  @imgPriv = Hash.new

  root = TkRoot.new(:title=>'Tests for available image formats')

  root.winfo_children.each{|w| w.destroy}
  TkImage.names{|img| img.delete}

  f = TkFrame.new
  TkButton.new(f, :text=>'Dismiss', :command=>proc{exit}).pack(:side=>:left)
  f.pack(:side=>:top, :expand=>:y, :fill=>:both)

  TkMessage.new(:aspect=>900, :text=>format('This page shows the available image formats of the Img extension (Img version %s, using Tcl/Tk %s)', img_version, Tk::TK_PATCHLEVEL)).pack(:side=>:top, :expand=>:y, :fill=>:both)
end

Instance Method Details

#show_animated_gif(keys) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'sample/tkextlib/tkimg/demo.rb', line 76

def show_animated_gif(keys)
  w = TkLabel.new
  begin
    im = TkPhotoImage.new(keys.merge(:format=>[:gif, {:index=>0}]))
    im.configure(:data=>'', :file=>'', :format=>'') #free storage
    w.image(im)
    @imgPriv[w] ||= Hash.new
    @imgPriv[w][0] = im
    @imgPriv[w][:args] = keys
    Tk.update_idletasks
    Tk.after(20, proc{update_animated_gif(w, :image, 0)})
  rescue => e
    w.configure(:text=>"error displaying animated gif:\n#{e.message}",
                :image=>'', :relief=>:ridge)
  end
  w.pack
end

#show_image(fmt, type, data) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'sample/tkextlib/tkimg/demo.rb', line 94

def show_image(fmt, type, data)
  fmt = fmt.to_s.capitalize
  unless @typeFrame[fmt]
    @typeFrame[fmt] = TkFrame.new.pack(:side=>:top, :expand=>true, :fill=>:x)
    TkLabel.new(@typeFrame[fmt], :text=>"#{fmt} :  ").pack(:side=>:left)
  end
  begin
    f = TkFrame.new(@typeFrame[fmt],
                    :borderwidth=>2, :relief=>:ridge).pack(:side=>:left)
    im = TkPhotoImage.new(:data=>data)
    im['data'] = ''
    TkLabel.new(f, :image=>im).pack
    TkLabel.new(f, :text=>type, :borderwidth=>0, :pady=>0, :padx=>2,
                :font=>'Helvetica 8').pack
  rescue => e
    TkMessage.new(f, :text=>"error displaying #{type} image: #{e.message}",
                  :aspect=>250).pack
  end
  Tk.update
end

#update_animated_gif(w, method, num) ⇒ Object



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
# File 'sample/tkextlib/tkimg/demo.rb', line 36

def update_animated_gif(w, method, num)
  return unless @imgPriv[w]

  if @imgPriv[w][:args]
    im = TkPhotoImage.new
    im.copy(@imgPriv[w][num])
    num += 1
    begin
      im.configure(@imgPriv[w][:args].merge(:format=>[:gif, {:index=>num}]))
      im.configure(:data=>'', :file=>'') #free storage
      @imgPriv[w][num] = im
    rescue
      @imgPriv[w].delete(:args)
      if num > 1
        num = 0
        im = @imgPriv[w][num]
      else
        # this is not an animated GIF; just stop
        @imgPriv[w].delete(0)
        return
      end
    end
  else
    num += 1
    num = 0 unless @imgPriv[w][num]
    im = @imgPriv[w][num]
  end
  begin
    w.__send__(method, im)
    Tk.update_idletasks
    Tk.after(20, proc{update_animated_gif(w, method, num)})
  rescue
    @imgPriv[w].delete(:args)
    @imgPriv[w].each{|im|
      @im.delete
      @imgPriv.delete(im)
    }
  end
end