diff options
author | nagai <nagai@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-10-16 07:45:51 +0000 |
---|---|---|
committer | nagai <nagai@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-10-16 07:45:51 +0000 |
commit | 98a3a9db9718d8a54a66ae5f5e4da080c57cba5c (patch) | |
tree | ebca1fb8cbbd8d33e46c7c8267fc26c5d67a5c61 /ext/tk | |
parent | e45c4cd1ac8523f83b1d72b068ba009e3d9fe6db (diff) | |
download | ruby-98a3a9db9718d8a54a66ae5f5e4da080c57cba5c.tar.gz |
Add Tk::EncodedString and Tk::UTF8_String class to support characters
using the \uXXXX escape to the UNICODE string.
sample/{demos-en,demos-jp}/unicodeout.rb are samples of Tk::UTF8_String.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4784 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/tk')
-rw-r--r-- | ext/tk/lib/tk.rb | 58 | ||||
-rw-r--r-- | ext/tk/sample/demos-en/unicodeout.rb | 100 | ||||
-rw-r--r-- | ext/tk/sample/demos-en/widget | 22 | ||||
-rw-r--r-- | ext/tk/sample/demos-jp/entry3.rb | 14 | ||||
-rw-r--r-- | ext/tk/sample/demos-jp/unicodeout.rb | 102 | ||||
-rw-r--r-- | ext/tk/sample/demos-jp/widget | 22 |
6 files changed, 289 insertions, 29 deletions
diff --git a/ext/tk/lib/tk.rb b/ext/tk/lib/tk.rb index fcf9d11593..e904278b25 100644 --- a/ext/tk/lib/tk.rb +++ b/ext/tk/lib/tk.rb @@ -1525,6 +1525,50 @@ module Tk end ########################################### +# string with Tcl's encoding +########################################### +module Tk + class EncodedString < String + @@enc_buf = '__rb_encoding_buffer__' + + def self.tk_escape(str) + s = '"' + str.gsub(/[\[\]$"]/, '\\\\\&') + '"' + TkCore::INTERP.__eval(format('global %s; set %s %s', + @@enc_buf, @@enc_buf, s)) + end + + def self.new(str, enc = Tk.encoding_system) + obj = super(self.tk_escape(str)) + obj.instance_eval{@enc = enc} + obj + end + + def self.new_without_escape(str, enc = Tk.encoding_system) + obj = super(str) + obj.instance_eval{@enc = enc} + obj + end + + def encoding + @enc + end + end + def Tk.EncodedString(str, enc = Tk.encoding_system) + Tk::EncodedString.new(str, enc) + end + + class UTF8_String < EncodedString + def self.new(str) + super(str, 'utf-8') + end + end + def Tk.UTF8_String(str) + Tk::UTF8_String.new(str) + end +end + + +########################################### # convert kanji string to/from utf-8 ########################################### if /^8\.[1-9]/ =~ Tk::TCL_VERSION && !Tk::JAPANIZED_TK @@ -1537,7 +1581,11 @@ if /^8\.[1-9]/ =~ Tk::TCL_VERSION && !Tk::JAPANIZED_TK def _eval(cmd) if defined? @encoding - _fromUTF8(__eval(_toUTF8(cmd, @encoding)), @encoding) + if cmd.kind_of?(Tk::EncodedString) + _fromUTF8(__eval(_toUTF8(cmd, cmd.encoding)), @encoding) + else + _fromUTF8(__eval(_toUTF8(cmd, @encoding)), @encoding) + end else __eval(cmd) end @@ -1545,7 +1593,13 @@ if /^8\.[1-9]/ =~ Tk::TCL_VERSION && !Tk::JAPANIZED_TK def _invoke(*cmds) if defined? @encoding - cmds = cmds.collect{|cmd| _toUTF8(cmd, @encoding)} + cmds = cmds.collect{|cmd| + if cmd.kind_of?(Tk::EncodedString) + _toUTF8(cmd, cmd.encoding) + else + _toUTF8(cmd, @encoding) + end + } _fromUTF8(__invoke(*cmds), @encoding) else __invoke(*cmds) diff --git a/ext/tk/sample/demos-en/unicodeout.rb b/ext/tk/sample/demos-en/unicodeout.rb new file mode 100644 index 0000000000..9e69628e7e --- /dev/null +++ b/ext/tk/sample/demos-en/unicodeout.rb @@ -0,0 +1,100 @@ +# unicodeout.rb -- +# +# This demonstration script shows how you can produce output (in label +# widgets) using many different alphabets. +# +# based on Tcl/Tk8.4.4 widget demos + +if defined?($unicodeout_demo) && $unicodeout_demo + $unicodeout_demo.destroy + $unicodeout_demo = nil +end + +$unicodeout_demo = TkToplevel.new {|w| + title("Unicode Label Demonstration") + iconname("unicodeout") + positionWindow(w) +} + +TkLabel.new($unicodeout_demo, + :font=>$font, :wraplength=>'4i', :justify=>:left, + :text=><<EOL).pack(:side=>:top) +This is a sample of Tk's support for languages that use non-Western \ +character sets. However, what you will actually see below depends \ +largely on what character sets you have installed, and what you see \ +for characters that are not present varies greatly between platforms \ +as well. The strings are written in Tcl using UNICODE characters \ +using the \\uXXXX escape so as to do so in a portable fashion. + +ATTENTION: +The strings are converted to the encoded string objects \ +(completed to rewrite Tcl's escapes) by Tk::UTF8_String method. \ +And the Tk::UTF8_String objects are passed to the label widgets. +EOL + +TkFrame.new($unicodeout_demo){|f| + pack(:side=>:bottom, :fill=>:x, :pady=>'2m') + + TkButton.new(f, :text=>'Dismiss', :width=>15, :command=>proc{ + $unicodeout_demo.destroy + $unicodeout_demo = nil + }).pack(:side=>:left, :expand=>true) + + TkButton.new(f, :text=>'See Code', :width=>15, :command=>proc{ + showCode 'unicodeout' + }).pack(:side=>:left, :expand=>true) +} + +wait_msg = TkLabel.new($unicodeout_demo, + :text=>"Please wait while loading fonts...", + :font=>"Helvetica 12 italic").pack + +class Unicodeout_SampleFrame < TkFrame + @@font = $font + + def initialize() + super($unicodeout_demo) + grid_columnconfig(1, :weight=>1) + end + + def add_sample(lang, *args) + sample_txt = Tk::UTF8_String(args.join('')) + l = TkLabel.new(self, :font=>@@font, :text=>lang+':', + :anchor=>:nw, :pady=>0) + s = TkLabel.new(self, :font=>TkFont.new(@@font), :text=>sample_txt, + :anchor=>:nw, :width=>30, :pady=>0) + Tk.grid(l, s, :sticky=>:ew, :pady=>0) + l.grid_config(:padx, '1m') + end +end +f = Unicodeout_SampleFrame.new +f.pack(:expand=>true, :fill=>:both, :padx=>'2m', :pady=>'1m') + +# Processing when some characters are missing might take a while, so make +# sure we're displaying something in the meantime... + +oldCursor = $unicodeout_demo.cursor +$unicodeout_demo.cursor('watch') +Tk.update + +f.add_sample('Arabic', + '\uFE94\uFEF4\uFE91\uFEAE\uFECC\uFEDF\uFE8D\uFE94', + '\uFEE4\uFEE0\uFEDC\uFEDF\uFE8D') +f.add_sample('Trad. Chinese', '\u4E2D\u570B\u7684\u6F22\u5B57') +f.add_sample('Simpl. Chinese', '\u6C49\u8BED') +f.add_sample('Greek', + '\u0395\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA\u03AE ', + '\u03B3\u03BB\u03CE\u03C3\u03C3\u03B1') +f.add_sample('Hebrew', + '\u05DD\u05D9\u05DC\u05E9\u05D5\u05E8\u05D9 ', + '\u05DC\u05D9\u05D0\u05E8\u05E9\u05D9') +f.add_sample('Japanese', + '\u65E5\u672C\u8A9E\u306E\u3072\u3089\u304C\u306A, ', + '\u6F22\u5B57\u3068\u30AB\u30BF\u30AB\u30CA') +f.add_sample('Korean', '\uB300\uD55C\uBBFC\uAD6D\uC758 \uD55C\uAE00') +f.add_sample('Russian', + '\u0420\u0443\u0441\u0441\u043A\u0438\u0439 ', + '\u044F\u0437\u044B\u043A') + +wait_msg.destroy +$unicodeout_demo.cursor(oldCursor) diff --git a/ext/tk/sample/demos-en/widget b/ext/tk/sample/demos-en/widget index 2c13a5e493..aa702883de 100644 --- a/ext/tk/sample/demos-en/widget +++ b/ext/tk/sample/demos-en/widget @@ -200,25 +200,27 @@ txt.insert('end', "Labels, buttons, checkbuttons, and radiobuttons.\n", tag_titl txt.insert('end', " \n ", tag_demospace) txt.insert('end', "1. Labels (text and bitmaps).\n", tag_demo, "demo-label") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "2. Buttons.\n", tag_demo, "demo-button") +txt.insert('end', "2. Labels and UNICODE text. (if supported)\n", tag_demo, "demo-unicodeout") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "3. Checkbuttons (select any of a group).\n", tag_demo, "demo-check") +txt.insert('end', "3. Buttons.\n", tag_demo, "demo-button") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "4. Radiobuttons (select one of a group).\n", tag_demo, "demo-radio") +txt.insert('end', "4. Checkbuttons (select any of a group).\n", tag_demo, "demo-check") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "5. Radiobuttons (if supported 'compound' option).\n", tag_demo, "demo-radio2") +txt.insert('end', "5. Radiobuttons (select one of a group).\n", tag_demo, "demo-radio") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "6. A 15-puzzle game made out of buttons.\n", tag_demo, "demo-puzzle") +txt.insert('end', "6. Radiobuttons (if supported 'compound' option).\n", tag_demo, "demo-radio2") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "7. Iconic buttons that use bitmaps.\n", tag_demo, "demo-icon") +txt.insert('end', "7. A 15-puzzle game made out of buttons.\n", tag_demo, "demo-puzzle") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "8. Two labels displaying images.\n", tag_demo, "demo-image1") +txt.insert('end', "8. Iconic buttons that use bitmaps.\n", tag_demo, "demo-icon") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "9. A simple user interface for viewing images.\n", tag_demo, "demo-image2") +txt.insert('end', "9. Two labels displaying images.\n", tag_demo, "demo-image1") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "10. A simple user interface for viewing images. (if supported)\n", tag_demo, "demo-image3") +txt.insert('end', "10. A simple user interface for viewing images.\n", tag_demo, "demo-image2") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "11. Labelled frames (if supported)\n", tag_demo, "demo-labelframe") +txt.insert('end', "11. A simple user interface for viewing images. (if supported)\n", tag_demo, "demo-image3") +txt.insert('end', " \n ", tag_demospace) +txt.insert('end', "12. Labelled frames (if supported)\n", tag_demo, "demo-labelframe") txt.insert('end', " \n ", tag_demospace) txt.insert('end', "\n") diff --git a/ext/tk/sample/demos-jp/entry3.rb b/ext/tk/sample/demos-jp/entry3.rb index 4c0b24e28c..9bf3f62097 100644 --- a/ext/tk/sample/demos-jp/entry3.rb +++ b/ext/tk/sample/demos-jp/entry3.rb @@ -29,7 +29,7 @@ TkLabel.new($entry3_demo, 二つめのエントリボックスは,入力された文字列の長さが\ 10文字未満の場合だけを受け付け,制限を越えて書き込もうとしたときには\ ベルを鳴らして知らせます.\ -三つめはアメリカの電話番号を受け付けるエントリボックスです.\ +三つめは米国の電話番号を受け付けるエントリボックスです.\ アルファベットは,電話機のダイヤル上で対応づけられている数字に変換されます.\ 不適切な文字が入力されたり数字以外の文字の位置に数字を入力しようとしたり\ した場合には警告のベルが鳴ります.\ @@ -41,12 +41,12 @@ EOL TkFrame.new($entry3_demo){|f| pack(:side=>:bottom, :fill=>:x, :pady=>'2m') - TkButton.new(f, :text=>'Dismiss', :width=>15, :command=>proc{ + TkButton.new(f, :text=>'了解', :width=>15, :command=>proc{ $entry3_demo.destroy $entry3_demo = nil }).pack(:side=>:left, :expand=>true) - TkButton.new(f, :text=>'See Code', :width=>15, :command=>proc{ + TkButton.new(f, :text=>'コード参照', :width=>15, :command=>proc{ showCode 'entry3' }).pack(:side=>:left, :expand=>true) } @@ -71,7 +71,7 @@ def focusAndFlash(widget, fg, bg, count=5) widget.focus(true) end -l1 = TkLabelFrame.new($entry3_demo, :text=>"Integer Entry") +l1 = TkLabelFrame.new($entry3_demo, :text=>"整数エントリ") TkEntry.new(l1, :validate=>:focus, :vcmd=>[ proc{|s| s == '' || /^[+-]?\d+$/ =~ s }, '%P' @@ -80,7 +80,7 @@ TkEntry.new(l1, :validate=>:focus, pack(:fill=>:x, :expand=>true, :padx=>'1m', :pady=>'1m') } -l2 = TkLabelFrame.new($entry3_demo, :text=>"Length-Constrained Entry") +l2 = TkLabelFrame.new($entry3_demo, :text=>"長さ制約付きエントリ") TkEntry.new(l2, :validate=>:key, :invcmd=>proc{Tk.bell}, :vcmd=>[proc{|s| s.length < 10}, '%P'] ).pack(:fill=>:x, :expand=>true, :padx=>'1m', :pady=>'1m') @@ -167,7 +167,7 @@ def validatePhoneChange(widget, vmode, idx, char) end -l3 = TkLabelFrame.new($entry3_demo, :text=>"US Phone-Number Entry") +l3 = TkLabelFrame.new($entry3_demo, :text=>"米国電話番号エントリ") TkEntry.new(l3, :validate=>:key, :invcmd=>proc{Tk.bell}, :textvariable=>entry3content, :vcmd=>[ @@ -186,7 +186,7 @@ TkEntry.new(l3, :validate=>:key, :invcmd=>proc{Tk.bell}, pack(:fill=>:x, :expand=>true, :padx=>'1m', :pady=>'1m') } -l4 = TkLabelFrame.new($entry3_demo, :text=>"Password Entry") +l4 = TkLabelFrame.new($entry3_demo, :text=>"パスワードエントリ") TkEntry.new(l4, :validate=>:key, :show=>'*', :vcmd=>[ proc{|s| s.length <= 8}, diff --git a/ext/tk/sample/demos-jp/unicodeout.rb b/ext/tk/sample/demos-jp/unicodeout.rb new file mode 100644 index 0000000000..fcc0a21875 --- /dev/null +++ b/ext/tk/sample/demos-jp/unicodeout.rb @@ -0,0 +1,102 @@ +# unicodeout.rb -- +# +# This demonstration script shows how you can produce output (in label +# widgets) using many different alphabets. +# +# based on Tcl/Tk8.4.4 widget demos + +if defined?($unicodeout_demo) && $unicodeout_demo + $unicodeout_demo.destroy + $unicodeout_demo = nil +end + +$unicodeout_demo = TkToplevel.new {|w| + title("Unicode Label Demonstration") + iconname("unicodeout") + positionWindow(w) +} + +TkLabel.new($unicodeout_demo, + :font=>$font, :wraplength=>'4i', :justify=>:left, + :text=><<EOL).pack(:side=>:top) +これは,Tkにおける非欧米文字集合を用いる言語に対するサポートについての\ +サンプルです.ただし,下の表示においてあなたが実際にどのような表示を\ +目にするかは,あなたの環境にどのような文字集合がインストールされているかに\ +大きく依存します.また,対象となる文字集合がインストールされていない場合に\ +どのような表示がなされるかもあなたの環境次第です.\ +スクリプトが可搬性を持つように,文字列は\\uXXXXという\ +Tclのエスケープ表現を用いたUNICODE文字列で書かれています. +文字列はTk::UTF8_StringメソッドによってUTF8形式の文字列であるという\ +エンコード情報付きの文字列オブジェクト\ +(Tclのエスケープ表現の変換済み)に変換して\ +ラベルウィジェットに渡している点に注意してください. +EOL +#' + +TkFrame.new($unicodeout_demo){|f| + pack(:side=>:bottom, :fill=>:x, :pady=>'2m') + + TkButton.new(f, :text=>'了解', :width=>15, :command=>proc{ + $unicodeout_demo.destroy + $unicodeout_demo = nil + }).pack(:side=>:left, :expand=>true) + + TkButton.new(f, :text=>'コード参照', :width=>15, :command=>proc{ + showCode 'unicodeout' + }).pack(:side=>:left, :expand=>true) +} + +wait_msg = TkLabel.new($unicodeout_demo, + :text=>"フォント読み込みの完了まで" + + "しばらくお待ち下さい...", + :font=>"Helvetica 12 italic").pack + +class Unicodeout_SampleFrame < TkFrame + @@font = $font + + def initialize() + super($unicodeout_demo) + grid_columnconfig(1, :weight=>1) + end + + def add_sample(lang, *args) + sample_txt = Tk::UTF8_String(args.join('')) + l = TkLabel.new(self, :font=>@@font, :text=>lang+':', + :anchor=>:nw, :pady=>0) + s = TkLabel.new(self, :font=>TkFont.new(@@font), :text=>sample_txt, + :anchor=>:nw, :width=>30, :pady=>0) + Tk.grid(l, s, :sticky=>:ew, :pady=>0) + l.grid_config(:padx, '1m') + end +end +f = Unicodeout_SampleFrame.new +f.pack(:expand=>true, :fill=>:both, :padx=>'2m', :pady=>'1m') + +# Processing when some characters are missing might take a while, so make +# sure we're displaying something in the meantime... + +oldCursor = $unicodeout_demo.cursor +$unicodeout_demo.cursor('watch') +Tk.update + +f.add_sample('Arabic', + '\uFE94\uFEF4\uFE91\uFEAE\uFECC\uFEDF\uFE8D\uFE94', + '\uFEE4\uFEE0\uFEDC\uFEDF\uFE8D') +f.add_sample('Trad. Chinese', '\u4E2D\u570B\u7684\u6F22\u5B57') +f.add_sample('Simpl. Chinese', '\u6C49\u8BED') +f.add_sample('Greek', + '\u0395\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA\u03AE ', + '\u03B3\u03BB\u03CE\u03C3\u03C3\u03B1') +f.add_sample('Hebrew', + '\u05DD\u05D9\u05DC\u05E9\u05D5\u05E8\u05D9 ', + '\u05DC\u05D9\u05D0\u05E8\u05E9\u05D9') +f.add_sample('Japanese', + '\u65E5\u672C\u8A9E\u306E\u3072\u3089\u304C\u306A, ', + '\u6F22\u5B57\u3068\u30AB\u30BF\u30AB\u30CA') +f.add_sample('Korean', '\uB300\uD55C\uBBFC\uAD6D\uC758 \uD55C\uAE00') +f.add_sample('Russian', + '\u0420\u0443\u0441\u0441\u043A\u0438\u0439 ', + '\u044F\u0437\u044B\u043A') + +wait_msg.destroy +$unicodeout_demo.cursor(oldCursor) diff --git a/ext/tk/sample/demos-jp/widget b/ext/tk/sample/demos-jp/widget index 3dd285a489..696a9073cc 100644 --- a/ext/tk/sample/demos-jp/widget +++ b/ext/tk/sample/demos-jp/widget @@ -211,33 +211,35 @@ txt.insert('end', " \n ", tag_demospace) txt.insert('end', "1. ラベル (テキスト, ビットマップ)\n", tag_demo, "demo-label") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "2. ボタン \n", tag_demo, "demo-button") +txt.insert('end', "2. ラベルとUNICODEテキスト (機能に対応したバージョンのTkが必要)\n", tag_demo, "demo-unicodeout") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "3. チェックボタン (複数を選択可能)\n", +txt.insert('end', "3. ボタン \n", tag_demo, "demo-button") +txt.insert('end', " \n ", tag_demospace) +txt.insert('end', "4. チェックボタン (複数を選択可能)\n", tag_demo, "demo-check") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "4. ラジオボタン (任意の一つを選択可能)\n", +txt.insert('end', "5. ラジオボタン (任意の一つを選択可能)\n", tag_demo, "demo-radio") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "5. ラジオボタン (機能に対応したバージョンのTkが必要)\n", +txt.insert('end', "6. ラジオボタン (機能に対応したバージョンのTkが必要)\n", tag_demo, "demo-radio2") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "6. ボタンで作られた15-パズルゲーム\n", +txt.insert('end', "7. ボタンで作られた15-パズルゲーム\n", tag_demo, "demo-puzzle") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "7. ビットマップを使用したアイコンボタン\n", +txt.insert('end', "8. ビットマップを使用したアイコンボタン\n", tag_demo, "demo-icon") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "8. 画像を表示する二つのラベル\n", +txt.insert('end', "9. 画像を表示する二つのラベル\n", tag_demo, "demo-image1") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "9. 画像を見るための簡単なユーザインターフェース\n", +txt.insert('end', "10. 画像を見るための簡単なユーザインターフェース\n", tag_demo, "demo-image2") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "10. 画像を見るための簡単なユーザインターフェース (機能に対応したバージョンのTkが必要)\n", +txt.insert('end', "11. 画像を見るための簡単なユーザインターフェース (機能に対応したバージョンのTkが必要)\n", tag_demo, "demo-image3") txt.insert('end', " \n ", tag_demospace) -txt.insert('end', "11. ラベル付きフレーム (機能に対応したバージョンのTkが必要)\n", +txt.insert('end', "12. ラベル付きフレーム (機能に対応したバージョンのTkが必要)\n", tag_demo, "demo-labelframe") txt.insert('end', " \n ", tag_demospace) |