Changeset 29212

Show
Ignore:
Timestamp:
01/29/09 11:19:47 (4 years ago)
Author:
isaisstillalive
Message:
  • 論理文字列、物理文字列を取得するメソッドをそれぞれ追加。
  • 論理文字数、物理文字数を取得するメソッドをそれぞれ追加。
  • ちょっと混乱してきたので、とにかくテストを通らせて一旦コミット。要整理。
Location:
lang/ruby/ruwin
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/ruwin/lib/ruwin/edit_field.rb

    r29149 r29212  
    66    module Base 
    77      include Const::EditField 
    8       STRING_CONVERTER = Proc.new{|string| string } 
     8      ENABLE_MULTILINE = false 
    99       
    1010      attr_reader :selection 
     
    1919       
    2020      def string 
    21         self.class::STRING_CONVERTER.call(caption.kconv(Kconv::UTF8, Kconv::SJIS)) || "" 
    22       end 
    23       alias to_s string 
     21        physical_string 
     22      end 
    2423       
    2524      def string= string 
    26         self.caption = self.class::STRING_CONVERTER.call(string).kconv(Kconv::SJIS, Kconv::UTF8) 
    27       end 
     25        self.caption = convert_string(string, "\r\n").kconv(Kconv::SJIS, Kconv::UTF8) 
     26      end 
     27       
     28      def length 
     29        physical_length 
     30      end 
     31       
     32       
     33      def physical_string 
     34        return "" if caption.empty? 
     35        convert_string(caption.kconv(Kconv::UTF8, Kconv::SJIS)) 
     36      end 
     37       
     38      def logical_string 
     39        return "" if caption.empty? 
     40        convert_string(caption.kconv(Kconv::UTF8, Kconv::SJIS), "\r") 
     41      end 
     42       
     43      def convert_string string, rs = "\r\n" 
     44        if self.class::ENABLE_MULTILINE 
     45          string.gsub(/\r\n|\r|\n/u, rs) 
     46        else 
     47          string.split(/\r\n|\r|\n/, 2)[0] 
     48        end 
     49      end 
     50       
     51      def physical_length 
     52        physical_string.length 
     53      end 
     54       
     55      def logical_length 
     56        logical_string.length 
     57      end 
     58       
    2859       
    2960      def substring logical_first, logical_last 
    30         to_physical_string(to_logical_string(physical_string).split(//u)[logical_first...logical_last].join("")) 
     61        to_physical_string(logical_string.split(//u)[logical_first...logical_last].join("")) 
    3162      end 
    3263       
     
    4475        physical_string[first...last] 
    4576      end 
    46        
    47        
    48       alias physical_string string 
    49       private :physical_string 
    5077       
    5178      def to_logical_string physical_string 
     
    151178    STYLE = Control::STYLE | ES_AUTOHSCROLL 
    152179    EXSTYLE = WS_EX_CLIENTEDGE 
    153     STRING_CONVERTER = Proc.new{|string| string.split(/\r\n|\r|\n/, 2)[0] } 
    154180     
    155181    property_accessor :writable => true 
     
    169195    class Multiline < EditField 
    170196      STYLE = Control::STYLE | ES_MULTILINE | ES_AUTOVSCROLL 
    171       STRING_CONVERTER = Proc.new{|string| string.gsub(/\r\n|\r|\n/u, "\r\n") } 
     197      ENABLE_MULTILINE = true 
    172198       
    173199      private 
    174       def return_count physical_first, physical_length 
     200      def count_rs physical_first, physical_length 
    175201        string[physical_first, physical_length].split("\r\n").size-1 
    176202      end 
     
    180206        len = logical_length 
    181207        until len == 0 
    182           return_count = return_count(pos, len) 
     208          count_rs = count_rs(pos, len) 
    183209          pos += len 
    184           len = return_count 
     210          len = count_rs 
    185211        end 
    186212        pos 
    187213      end 
    188214       
    189       def logical_length physical_first, physical_length 
    190         physical_length - return_count(physical_first, physical_length) 
     215      def logical_diff physical_first, physical_length 
     216        physical_length - count_rs(physical_first, physical_length) 
    191217      end 
    192218       
     
    203229        physical_length = physical_last-physical_first 
    204230         
    205         logical_length = logical_length(physical_first, physical_length) 
    206         logical_first  = logical_length(0,              physical_first) 
     231        logical_length = logical_diff(physical_first, physical_length) 
     232        logical_first  = logical_diff(0,              physical_first) 
    207233         
    208234        return logical_first, logical_first+logical_length 
  • lang/ruby/ruwin/lib/ruwin/edit_field/rich.rb

    r29150 r29212  
    2525        CLASS_NAME = "RICHEDIT" 
    2626      end 
    27       STRING_CONVERTER = Proc.new{|string| string } 
    2827       
    2928      property_accessor :background_color => nil, :color => nil 
     
    108107      class Multiline < Rich 
    109108        STYLE = EditField::Multiline::STYLE 
    110         RETURN_CODE = "\r\n" 
     109        ENABLE_MULTILINE = true 
    111110      end 
    112111    end 
  • lang/ruby/ruwin/test/ruwin/testm_edit_field.rb

    r29149 r29212  
    1313   
    1414   
     15  def test_string_setter 
     16    @control.string = "caption" 
     17    assert_equal "caption", @control.__send__(:caption) 
     18  end 
     19   
    1520  def test_string_setter_should_encode_utf8_to_sjis 
    1621    @control.string = "あいう" 
     
    1823  end 
    1924   
     25  def test_string_getter_empty 
     26    @control.__send__ :caption=, "" 
     27    assert_equal "", @control.string 
     28  end 
     29   
     30  def test_string_getter 
     31    @control.__send__ :caption=, "caption" 
     32    assert_equal "caption", @control.string 
     33  end 
     34   
    2035  def test_string_getter_should_encode_sjis_to_utf8 
    2136    @control.__send__ :caption=, "あいう".tosjis 
     
    2338  end 
    2439   
    25   def test_string_setter 
    26     @control.string = "caption" 
    27     assert_equal "caption", @control.__send__(:caption) 
    28   end 
    29    
    30   def test_string_getter_empty 
    31     @control.__send__ :caption=, "" 
    32     assert_equal "", @control.string 
    33   end 
    34    
    35   def test_string_getter 
    36     @control.__send__ :caption=, "caption" 
    37     assert_equal "caption", @control.string 
     40   
     41  def test_logical_string 
     42    @control.__send__ :caption=, "caption" 
     43    assert_equal "caption", @control.logical_string 
     44  end 
     45   
     46  def test_physical_string 
     47    @control.__send__ :caption=, "caption" 
     48    assert_equal "caption", @control.physical_string 
     49  end 
     50   
     51   
     52  def test_length 
     53    @control.__send__ :caption=, "caption" 
     54    assert_equal 7, @control.length 
     55  end 
     56   
     57  def test_logical_length 
     58    @control.__send__ :caption=, "caption" 
     59    assert_equal 7, @control.logical_length 
     60  end 
     61   
     62  def test_physical_length 
     63    @control.__send__ :caption=, "caption" 
     64    assert_equal 7, @control.physical_length 
    3865  end 
    3966   
     
    257284   
    258285  def test_string_setter_should_deny_return 
    259     klass = Class.new(self.class::TARGET_CLASS) 
    260     control = klass.new(@window) 
    261     control.string = "caption\r\ncaption\rcaption\ncaption" 
    262     assert_equal "caption", control.__send__(:caption) 
     286    @control.string = "caption\r\ncaption\rcaption\ncaption" 
     287    assert_equal "caption", @control.__send__(:caption) 
    263288  end 
    264289   
    265290  def test_string_getter_should_deny_return 
    266     klass = Class.new(self.class::TARGET_CLASS) 
    267     control = klass.new(@window) 
    268     control.__send__ :caption=, "caption\r\ncaption\rcaption\ncaption" 
    269     assert_equal "caption", control.string 
     291    @control.__send__ :caption=, "caption\r\ncaption\rcaption\ncaption" 
     292    assert_equal "caption", @control.string 
    270293  end 
    271294end 
     
    284307   
    285308  def test_string_setter_should_allow_return_and_convert_to_crlf 
    286     klass = Class.new(self.class::TARGET_CLASS) 
    287     control = klass.new(@window) 
    288     control.string = "caption\r\ncaption\rcaption\ncaption" 
    289     assert_equal "caption\r\ncaption\r\ncaption\r\ncaption", control.__send__(:caption) 
     309    @control.string = "caption\r\ncaption\rcaption\ncaption" 
     310    assert_equal "caption\r\ncaption\r\ncaption\r\ncaption", @control.__send__(:caption) 
    290311  end 
    291312   
    292313  def test_string_getter_should_allow_return_and_convert_to_crlf 
    293     klass = Class.new(self.class::TARGET_CLASS) 
    294     control = klass.new(@window) 
    295     control.__send__ :caption=, "caption\r\ncaption\rcaption\ncaption" 
    296     assert_equal "caption\r\ncaption\r\ncaption\r\ncaption", control.string 
     314    @control.__send__ :caption=, "caption\r\ncaption\rcaption\ncaption" 
     315    assert_equal "caption\r\ncaption\r\ncaption\r\ncaption", @control.string 
     316  end 
     317   
     318   
     319  def test_logical_string 
     320    @control.__send__ :caption=, "caption\r\ncaption\rcaption\ncaption" 
     321    assert_equal "caption\rcaption\rcaption\rcaption", @control.logical_string 
     322  end 
     323   
     324  def test_physical_string 
     325    @control.__send__ :caption=, "caption\r\ncaption\rcaption\ncaption" 
     326    assert_equal "caption\r\ncaption\r\ncaption\r\ncaption", @control.physical_string 
     327  end 
     328   
     329   
     330  def test_length 
     331    @control.__send__ :caption=, "caption\r\ncaption\rcaption\ncaption" 
     332    assert_equal 28+6, @control.length 
     333  end 
     334   
     335  def test_logical_length 
     336    @control.__send__ :caption=, "caption\r\ncaption\rcaption\ncaption" 
     337    assert_equal 28+3, @control.logical_length 
     338  end 
     339   
     340  def test_physical_length 
     341    @control.__send__ :caption=, "caption\r\ncaption\rcaption\ncaption" 
     342    assert_equal 28+6, @control.physical_length 
     343  end 
     344   
     345   
     346  def test_substring_multiline 
     347    @control.string = "caption\r\ncaption\rcaption\ncaption" 
     348    assert_equal "ion\r\ncap", @control.substring(4, 11) 
    297349  end 
    298350   
     
    303355    assert_equal "ion\r\ncap", @control.selection_string 
    304356  end 
    305    
    306   def test_substring_multiline 
    307     @control.string = "caption\r\ncaption\rcaption\ncaption" 
    308     assert_equal "ion\r\ncap", @control.substring(4, 11) 
    309   end 
    310357end