]> gitweb.factorcode.org Git - factor.git/commitdiff
Add support for rendering selected text to ui.text
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sun, 8 Feb 2009 00:05:11 +0000 (18:05 -0600)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sun, 8 Feb 2009 00:05:11 +0000 (18:05 -0600)
basis/core-text/core-text.factor
basis/fonts/fonts.factor
basis/ui/text/text.factor

index 20f2a9e803c311d6e3811a156ae326691c6d7cdf..0aa3bce2828d5037b0c84c0c99a8893d55c9d286 100644 (file)
@@ -34,6 +34,7 @@ FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) ;
 
 : <CTLine> ( string open-font color -- line )
     [
+        [ dup selection? [ string>> ] when ] 2dip
         [
             kCTForegroundColorAttributeName set
             kCTFontAttributeName set
@@ -53,6 +54,25 @@ TUPLE: line font line metrics dim bitmap age refs disposed ;
     [ ceiling >fixnum ]
     bi@ 2array ;
 
+: fill-background ( context font dim -- )
+    [ background>> >rgba-components CGContextSetRGBFillColor ]
+    [ [ 0 0 ] dip first2 <CGRect> CGContextFillRect ]
+    bi-curry* bi ;
+
+: selection-rect ( dim line selection -- rect )
+    [ start>> ] [ end>> ] bi
+    [ f CTLineGetOffsetForStringIndex ] bi-curry@ bi
+    [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
+
+:: fill-selection-background ( context dim line string -- )
+    string selection? [
+        context string color>> >rgba-components CGContextSetRGBFillColor
+        context dim line string selection-rect CGContextFillRect
+    ] when ;
+
+: set-text-position ( context metrics -- )
+    [ 0 ] dip descent>> ceiling CGContextSetTextPosition ;
+
 :: <line> ( font string -- line )
     [
         [let* | open-font [ font cache-font CFRetain |CFRelease ]
@@ -61,10 +81,10 @@ TUPLE: line font line metrics dim bitmap age refs disposed ;
                 dim [ metrics bounds>dim ] |
             dim [
                 {
-                    [ font background>> >rgba-components CGContextSetRGBFillColor ]
-                    [ 0 0 dim first2 <CGRect> CGContextFillRect ]
-                    [ 0 metrics descent>> ceiling CGContextSetTextPosition ]
-                    [ line swap CTLineDraw ]
+                    [ font dim fill-background ]
+                    [ dim line string fill-selection-background ]
+                    [ metrics set-text-position ]
+                    [ [ line ] dip CTLineDraw ]
                 } cleave
             ] with-bitmap-context
             [ open-font line metrics dim ] dip 0 0 f
index 3e6541283d4e0f2b345d0b3829228efd21de3352..4f6fcac27b77db7c206289ff30380d23cfdc1d55 100644 (file)
@@ -55,4 +55,8 @@ italic?
         "monospace" >>name
         12 >>size ;
 
-TUPLE: metrics width ascent descent leading ;
\ No newline at end of file
+TUPLE: metrics width ascent descent leading ;
+
+TUPLE: selection string start end color ;
+
+C: <selection> selection
\ No newline at end of file
index 4acf892ae0a2d678820532b147a89f29771a1a79..87e6dc0862bde30e36c2608be30a6c348ef566f3 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: kernel arrays sequences math math.order opengl opengl.gl
-strings fonts colors ;
+strings fonts colors accessors ;
 IN: ui.text
 
 <PRIVATE
@@ -56,6 +56,8 @@ GENERIC# draw-text 1 ( font text loc -- )
 
 M: string draw-text draw-string ;
 
+M: selection draw-text draw-string ;
+
 M: sequence draw-text
     [
         [