[cells-gtk-devel] UTF-8 support -- Patch

Peter Hildebrandt peter.hildebrandt at gmail.com
Fri Jan 18 12:56:48 UTC 2008



Peter Hildebrandt wrote:
> 
> I created a simple patch for gtk-ffi.lisp that adds a call to 
> sb-ext:string-to-octets to all gtk functions that take strings.  This 
> way all gtk functions transparently accept utf-8 characters.
> 
> Here's the patch
> http://www.washbear-network.de/peterblog/wp-content/uploads/2008/01/gtk-ffi-utf8-sbcl.patch 

And for the opposite direction:

http://www.washbear-network.de/peterblog/wp-content/uploads/2008/01/gtk-ffi-utf8-sbcl-input.patch
(you need both patches!)

This lets you input utf-8 characters (eg. German umlauts) into entry 
widgets.

With my threading patch you can test the following:

(start-win 'gtk-app
		 :kids (list
			(mk-vbox
			 :kids (list
				(mk-entry :md-name :entry)
				(mk-label :text (c? (print (md-value (fm-other :entry)))))))))

Whenever you press enter, the label will show the value entered, and the 
value is also printed to the console (=> it is properly accessible in 
gtk and lisp).

Without the threading patch, you'd need a defmodel

(defmodel test-utf8 (gtk-app)
   ()
   (:default-initargs :kids (list (mk-vbox
			 :kids (list
				(mk-entry :md-name :entry)
				(mk-label :text (c? (print (md-value (fm-other :entry)))))))))))

(start-app 'test-utf8)

Any questions, let me know.

Peter.

> 
> Ingo, let me know whether that solves your problem.
> 
> Or a version that works against my multithreaded [1] version
> http://www.washbear-network.de/peterblog/wp-content/uploads/2008/01/gtk-ffi-utf8-sbcl-threading.patch 
> 
> 
> The patch requires the latest cffi, that is 0.9.2.  Cells-gtk works fine 
> with this.
> 
> I have not looked at the other direction yet, that is, what happens if 
> the user enters utf-8 characters somewhere.
> 
> Any questions, let me know.
> 
> Peter
> -----
> [1] http://www.washbear-network.de/peterblog
> 
> Peter Hildebrandt wrote:
>>
>> Changing lisp-string-to-foreign in cffi/src/strings.lisp like this 
>> fixes the problem (at least for sbcl):
>>
>> (defun lisp-string-to-foreign (string ptr size)
>>   "Copy at most SIZE-1 characters from a Lisp STRING to PTR.
>> The foreign string will be null-terminated."
>>   (decf size)
>>   (loop with i = 0 for char across #-sbcl string #+sbcl 
>> (sb-ext:string-to-octets string :external-format :utf-8)
>>         while (< i #-sbcl size #+sbcl (length (sb-ext:string-to-octets 
>> string :external-format :utf-8)))
>>         do (%mem-set #-sbcl (char-code char)
>>                      #+sbcl char
>>                      ptr :unsigned-char (post-incf i))
>>         finally (%mem-set 0 ptr :unsigned-char i)))
>>
>> I will look at the newer cffi and see how that plays out.
>>
>>
>>
>> Dmitri Hrapof wrote:
>>> Peter Hildebrandt wrote:
>>>> Dmitri Hrapof wrote:
>>>>> Yes, now it's clear.
>>>>> MK-BUTTON expects a Lisp string, being decent Lisp function, not some
>>>>> FFI abomination ;)
>>>>> So the solution is to place (sb-ext:string-to-octets "foo äöüß bar"
>>>>> :external-format :utf-8) deeper, between MK-BUTTON and
>>>>> GTK-BUTTON-SET-LABEL.
>>>> Nope.  I think the problem is somewhere in cffi (maybe we're using
>>>> different versions?).  I'm doing:
>>> Stupid me! Just reread my answer and saw the error, but wasn't fast
>>> enough to correct myself befor you did :)
>>> You are right, the difference is in CFFI, in FOREIGN-STRING-ALLOC
>>> Use CFFI 0.9.2 instead of CFFI included in cells-gtk tarball.
>>> Also note that darcs version of CFFI is a new version, and is not very
>>> compatible with 0.9.2
>>> It may or it may not work with cells-gtk.
>>>
>>> Sincerely yours,
>>> Dmitri
>>
> 




More information about the cells-gtk-devel mailing list