fantasdic r375 - in trunk: . lib/fantasdic/ui



Author: mblondel
Date: Mon Jan  5 20:01:20 2009
New Revision: 375
URL: http://svn.gnome.org/viewvc/fantasdic?rev=375&view=rev

Log:
    * lib/fantasdic/ui/utils.rb: Added Gtk::TextBuffer#insert_pango_markup.
    This allows to insert text in pango markup (pseudo html). The markup
    will be displayed like normal text in case of wrong syntax or unknown tag.
    
    * lib/fantasdic/ui/result_text_view.rb: Use it. For now, it's always used
    but it may be better to activate it only when the source requires it. For
    example, Stardict dictionaries with "sametypesequence=g" use pango markup.


Modified:
   trunk/ChangeLog
   trunk/lib/fantasdic/ui/result_text_view.rb
   trunk/lib/fantasdic/ui/utils.rb

Modified: trunk/lib/fantasdic/ui/result_text_view.rb
==============================================================================
--- trunk/lib/fantasdic/ui/result_text_view.rb	(original)
+++ trunk/lib/fantasdic/ui/result_text_view.rb	Mon Jan  5 20:01:20 2009
@@ -127,7 +127,7 @@
 
         def insert_text(txt)
             @entries << [TEXT, txt]
-            insert(@iter, txt, "text")
+            insert_pango_markup(@iter, txt, "text")
         end
 
         def insert_link(word)

Modified: trunk/lib/fantasdic/ui/utils.rb
==============================================================================
--- trunk/lib/fantasdic/ui/utils.rb	(original)
+++ trunk/lib/fantasdic/ui/utils.rb	Mon Jan  5 20:01:20 2009
@@ -164,4 +164,68 @@
             nil
         end
     end
+end
+
+class Gtk::TextBuffer
+
+    # Displays text in pango markup (pseudo html)
+    # Will display plain text if unknown tags or wrong syntax
+    def insert_pango_markup(iter, markup_text, extratag=nil)
+        # based on work in C by Tim-Philipp MÃller
+        # see #59390 in GNOME's bugzilla
+
+        begin
+            attr_list, text, accel_char = Pango.parse_markup(markup_text)
+        rescue
+            insert(iter, markup_text)
+            return
+        end
+
+        if not attr_list or not text
+            insert(iter, markup_text)
+            return
+        end
+            
+        # create_mark(name, iter, left= true or right=false)
+        mark = create_mark(nil, iter, false)
+
+        paiter = attr_list.iterator
+
+        begin
+            start, end_ = paiter.range
+
+            tag = Gtk::TextTag.new
+
+            paiter.get.each do |attr|
+                # transform Pango::AttrStyle into style
+                key = attr.class.name.split("::").last
+                key = key.gsub("Attr", "").downcase
+
+                case key
+                    when "fontdescription"
+                        tag.font_desc = attr.value
+                    when "foreground", "background"
+                        col = attr.value
+                        col = Gdk::Color.new(col.red, col.green, col.blue)
+                        tag.send("#{key}_gdk=", col)
+                    else
+                        meth = "#{key}="
+                        tag.send(meth, attr.value) if tag.respond_to? meth
+                end
+            end
+
+            tags = [tag]
+            tags << extratag if extratag
+
+            self.tag_table.add(tag)
+
+            insert_with_tags(iter, text.slice(start...end_), *tags) 
+
+            iter = get_iter_at_mark(mark)
+
+        end while paiter.next!
+
+        delete_mark(mark)
+    end
+
 end
\ No newline at end of file



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]