Index: /lang/ruby/ruwin/test/ruwin/edit_field/test_rich.rb
===================================================================
--- /lang/ruby/ruwin/test/ruwin/edit_field/test_rich.rb (revision 29128)
+++ /lang/ruby/ruwin/test/ruwin/edit_field/test_rich.rb (revision 29145)
@@ -139,3 +139,19 @@
     assert_equal "RICHEDIT20A", @control.classname
   end
+  
+  
+  def test_set_selection_should_counting_return_code_1_char
+    @control.string = "caption\r\ncaption\r\ncaption"
+    @control.set_selection 12, 19
+    
+    sel = @control.sendMessage(EM_GETSEL, 0, 0)
+    assert_equal 12, Ruwin::LOWORD(sel)
+    assert_equal 19, Ruwin::HIWORD(sel)
+  end
+  
+  def test_get_selection_should_counting_return_code_1_char
+    @control.string = "caption\r\ncaption\r\ncaption"
+    @control.sendMessage(EM_SETSEL, 12, 19)
+    assert_equal [12, 19], @control.get_selection
+  end
 end
Index: /lang/ruby/ruwin/test/ruwin/test_edit_field.rb
===================================================================
--- /lang/ruby/ruwin/test/ruwin/test_edit_field.rb (revision 28963)
+++ /lang/ruby/ruwin/test/ruwin/test_edit_field.rb (revision 29145)
@@ -14,3 +14,19 @@
   include Ruwin::Const::EditField
   TARGET_CLASS = Ruwin::EditField::Multiline
+  
+  
+  def test_set_selection_should_counting_return_code_1_char
+    @control.string = "caption\r\ncaption\r\ncaption"
+    @control.set_selection 12, 19
+    
+    sel = @control.sendMessage(EM_GETSEL, 0, 0)
+    assert_equal 13, Ruwin::LOWORD(sel)
+    assert_equal 21, Ruwin::HIWORD(sel)
+  end
+  
+  def test_get_selection_should_counting_return_code_1_char
+    @control.string = "caption\r\ncaption\r\ncaption"
+    @control.sendMessage(EM_SETSEL, 13, 21)
+    assert_equal [12, 19], @control.get_selection
+  end
 end
Index: /lang/ruby/ruwin/test/ruwin/testm_edit_field.rb
===================================================================
--- /lang/ruby/ruwin/test/ruwin/testm_edit_field.rb (revision 29128)
+++ /lang/ruby/ruwin/test/ruwin/testm_edit_field.rb (revision 29145)
@@ -42,5 +42,7 @@
     @control.string = "caption"
     @control.set_selection 1, 4
-    assert_equal 1|4<<16, @control.sendMessage(EM_GETSEL, 0, 0)
+    sel = @control.sendMessage(EM_GETSEL, 0, 0)
+    assert_equal 1, Ruwin::LOWORD(sel)
+    assert_equal 4, Ruwin::HIWORD(sel)
   end
   
@@ -50,4 +52,13 @@
     assert_equal [1, 4], @control.get_selection
   end
+  
+  
+  def test_selection_string_getter
+    @control.string = "caption"
+    @control.set_selection 1, 4
+    assert_equal "apt", @control.selection_string
+  end
+  
+  
   
   
@@ -274,3 +285,10 @@
     assert_equal "caption\r\ncaption\r\ncaption\r\ncaption", control.string
   end
+  
+  
+  def test_selection_string_getter_multiline
+    @control.string = "caption\r\ncaption"
+    @control.set_selection 4, 11
+    assert_equal "ion\r\ncap", @control.selection_string
+  end
 end
Index: /lang/ruby/ruwin/lib/ruwin/edit_field/rich.rb
===================================================================
--- /lang/ruby/ruwin/lib/ruwin/edit_field/rich.rb (revision 29128)
+++ /lang/ruby/ruwin/lib/ruwin/edit_field/rich.rb (revision 29145)
@@ -39,4 +39,12 @@
         @default = Format::Char.new self, SCF_DEFAULT
         @default.color = @proparty[:color] if @proparty[:color]
+      end
+      
+      
+      def selection_string
+        range = getsel
+        result = "\0"*(range.last-range.first)
+        sendMessage EM_GETSELTEXT, 0, result
+        result.gsub("\r", "\r\n")
       end
       
Index: /lang/ruby/ruwin/lib/ruwin/edit_field.rb
===================================================================
--- /lang/ruby/ruwin/lib/ruwin/edit_field.rb (revision 29128)
+++ /lang/ruby/ruwin/lib/ruwin/edit_field.rb (revision 29145)
@@ -37,19 +37,18 @@
       
       def get_selection
+        return getsel
+      end
+      
+      
+      def selection_string
+        first, last = getsel
+        string[first...last]
+      end
+      
+      def getsel
         ranges = sendMessage EM_GETSEL, 0, 0
         return LOWORD(ranges), HIWORD(ranges)
       end
-      
-      
-      
-      
-      
-      
-      
-      
-      
-      
-      
-      
+      private :getsel
       
       
@@ -147,4 +146,44 @@
       STYLE = Control::STYLE | ES_MULTILINE | ES_AUTOVSCROLL
       STRING_CONVERTER = Proc.new{|string| string.gsub(/\r\n|\r|\n/u, "\r\n") }
+      
+      def set_selection logical_first, logical_last
+        logical_length = logical_last-logical_first
+        
+        physical_first = physical_add(0,              logical_first)
+        physical_last  = physical_add(physical_first, logical_length)
+        
+        super physical_first, physical_last
+      end
+      
+      def get_selection
+        physical_first, physical_last = super
+        
+        physical_length = physical_last-physical_first
+        
+        logical_length = logical_length(physical_first, physical_length)
+        logical_first  = logical_length(0,              physical_first)
+        
+        return logical_first, logical_first+logical_length
+      end
+      
+      private
+      def return_count physical_first, physical_length
+        string[physical_first, physical_length].split("\r\n").size-1
+      end
+      
+      def physical_add physical_first, logical_length
+        pos = physical_first
+        len = logical_length
+        until len == 0
+          return_count = return_count(pos, len)
+          pos += len
+          len = return_count
+        end
+        pos
+      end
+      
+      def logical_length physical_first, physical_length
+        physical_length - return_count(physical_first, physical_length)
+      end
     end
   end
