ruby/ext/tk/sample/binstr_usage.rb
nagai 41b74c6e20 * ext/tk/extconf.rb: New strategy for searching Tcl/Tk libraries.
* ext/tk/*: Support new features of Tcl/Tk8.6b1 and minor bug fixes.
     ( [KNOWN BUG] Ruby/Tk on Ruby 1.9 will not work on Cygwin. )
* ext/tk/*: Unify sources between Ruby 1.8 & 1.9.
            Improve default_widget_set handling.
* ext/tk/*: Multi-TkInterpreter (multi-tk.rb) works on Ruby 1.8 & 1.9.
     ( [KNOWN BUG] On Ruby 1.8, join to a long term Thread on Tk
       callbacks may freeze. On Ruby 1.9, cannot create a second 
       master interpreter (creating slaves are OK); supported master
       interpreter is the default master interpreter only. )
* ext/tk/lib/tkextlib/*: Update supported versions of Tk extensions.
         Tcllib 1.8/Tklib 0.4.1  ==>  Tcllib 1.11.1/Tklib 0.5
         BWidgets 1.7            ==>  BWidgets 1.8
         TkTable 2.9             ==>  TkTable 2.10
         TkTreeCtrl 2005-12-02   ==>  TkTreeCtrl 2.2.9
         Tile 0.8.0/8.5.1        ==>  Tile 0.8.3/8.6b1
         IncrTcl 2005-02-14      ==>  IncrTcl 2008-12-15
         TclX 2005-02-07         ==>  TclX 2008-12-15
         Trofs 0.4.3             ==>  Trofs 0.4.4


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@24064 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-07-12 23:09:52 +00:00

45 lines
1.5 KiB
Ruby

#!/usr/bin/env ruby
require "tk"
TkMessage.new(:width=>360, :text=><<EOM).pack
This sample shows how to use a binary sequence between Ruby and Tk. \
This reads the image data from the file as the binary sequence.
To treat the difference of encodings between on Ruby and on Tk seamlessly, \
Ruby/Tk converts the encoding of string arguments automatically. \
I think it is comfortable for users on almost all situations. \
However, when treats a binary sequence, the convert process makes troubles.
Tk::BinaryString class (subclass of Tk::EncodedString class) is the class \
to avoid such troubles. Please see the source code of this sample. \
A Tk::BinaryString instance is used to create the image for the center button.
EOM
ImgFile=[File.dirname(__FILE__), 'images','tcllogo.gif'].join(File::Separator)
ph1 = TkPhotoImage.new(:file=>ImgFile)
p ph1.configinfo
b_str = Tk::BinaryString(IO.read(ImgFile))
p [b_str, b_str.encoding]
ph2 = TkPhotoImage.new(:data=>b_str)
p ph2.configinfo
p ph2.data(:grayscale=>true)
ph3 = TkPhotoImage.new(:palette=>256)
ph3.put(ph2.data)
ph4 = TkPhotoImage.new()
ph4.put(ph2.data(:grayscale=>true))
#p [b_str.encoding, b_str.rb_encoding]
f = TkFrame.new.pack
TkButton.new(:parent=>f, :image=>ph1, :command=>proc{exit}).pack(:side=>:left)
TkButton.new(:parent=>f, :image=>ph2, :command=>proc{exit}).pack(:side=>:left)
TkButton.new(:parent=>f, :image=>ph3, :command=>proc{exit}).pack(:side=>:left)
TkButton.new(:parent=>f, :image=>ph4, :command=>proc{exit}).pack(:side=>:left)
Tk.mainloop