changeset 1785:c5ea64e8b436

Added UTF8/Wide conversion for GTK2/3. Also added some code in the test program to use it.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Wed, 25 Jul 2012 00:46:00 +0000
parents 86ace55df07b
children 9a32d4216f24
files dwtest.c gtk/dw.c gtk3/dw.c
diffstat 3 files changed, 100 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/dwtest.c	Tue Jul 24 23:33:50 2012 +0000
+++ b/dwtest.c	Wed Jul 25 00:46:00 2012 +0000
@@ -998,6 +998,8 @@
     unsigned long depth = dw_color_depth_get();
     HWND vscrollbox, hbox, button1, button2, label;
     int vscrollbarwidth, hscrollbarheight;
+    wchar_t widestring[100] = L"DWTest Wide";
+    char *utf8string = dw_wchar_to_utf8(widestring);
 
     /* create a box to pack into the notebook page */
     pagebox = dw_box_new(DW_HORZ, 2);
@@ -1092,7 +1094,7 @@
     if(image)
         dw_pixmap_set_transparent_color(image, DW_CLR_WHITE);
 
-    dw_messagebox("DWTest", DW_MB_OK|DW_MB_INFORMATION, "Width: %d Height: %d\n", font_width, font_height);
+    dw_messagebox(utf8string ? utf8string : "DWTest", DW_MB_OK|DW_MB_INFORMATION, "Width: %d Height: %d\n", font_width, font_height);
     dw_draw_rect(0, text1pm, DW_DRAW_FILL | DW_DRAW_NOAA, 0, 0, font_width*width1, font_height*rows);
     dw_draw_rect(0, text2pm, DW_DRAW_FILL | DW_DRAW_NOAA, 0, 0, font_width*cols, font_height*rows);
     dw_signal_connect(textbox1, DW_SIGNAL_BUTTON_PRESS, DW_SIGNAL_FUNC(context_menu_event), NULL);
--- a/gtk/dw.c	Tue Jul 24 23:33:50 2012 +0000
+++ b/gtk/dw.c	Wed Jul 25 00:46:00 2012 +0000
@@ -13652,3 +13652,52 @@
    }
    DW_MUTEX_UNLOCK;
 }
+
+/*
+ * Converts a UTF-8 encoded string into a wide string.
+ * Parameters:
+ *       utf8string: UTF-8 encoded source string.
+ * Returns:
+ *       Wide string that needs to be freed with dw_free()
+ *       or NULL on failure.
+ */
+wchar_t * API dw_utf8_to_wchar(char *utf8string)
+{
+   wchar_t *retval = NULL, *freeme;
+   
+   if(sizeof(wchar_t) == sizeof(gunichar))
+      freeme = retval = (wchar_t *)g_utf8_to_ucs4(utf8string, -1, NULL, NULL, NULL);
+   else if(sizeof(wchar_t) == sizeof(gunichar2))   
+      freeme = retval = (wchar_t *)g_utf8_to_utf16(utf8string, -1, NULL, NULL, NULL);
+   if(retval)
+   {
+      retval = wcsdup(retval);
+      g_free(freeme);
+   }
+   return retval;
+}
+
+/*
+ * Converts a wide string into a UTF-8 encoded string.
+ * Parameters:
+ *       wstring: Wide source string.
+ * Returns:
+ *       UTF-8 encoded string that needs to be freed with dw_free()
+ *       or NULL on failure.
+ */
+char * API dw_wchar_to_utf8(wchar_t *wstring)
+{
+   char *retval = NULL, *freeme;
+   
+   if(sizeof(wchar_t) == sizeof(gunichar))
+      freeme = retval = g_ucs4_to_utf8((gunichar *)wstring, -1, NULL, NULL, NULL);
+   else if(sizeof(wchar_t) == sizeof(gunichar2))
+      freeme = retval = g_utf16_to_utf8((gunichar2 *)wstring, -1, NULL, NULL, NULL);
+   if(retval)
+   {
+      retval = strdup(retval);
+      g_free(freeme);
+   }
+   return retval;    
+}
+
--- a/gtk3/dw.c	Tue Jul 24 23:33:50 2012 +0000
+++ b/gtk3/dw.c	Wed Jul 25 00:46:00 2012 +0000
@@ -11360,3 +11360,51 @@
    DW_MUTEX_UNLOCK;
 }
 
+/*
+ * Converts a UTF-8 encoded string into a wide string.
+ * Parameters:
+ *       utf8string: UTF-8 encoded source string.
+ * Returns:
+ *       Wide string that needs to be freed with dw_free()
+ *       or NULL on failure.
+ */
+wchar_t * API dw_utf8_to_wchar(char *utf8string)
+{
+   wchar_t *retval = NULL, *freeme;
+   
+   if(sizeof(wchar_t) == sizeof(gunichar))
+      freeme = retval = (wchar_t *)g_utf8_to_ucs4(utf8string, -1, NULL, NULL, NULL);
+   else if(sizeof(wchar_t) == sizeof(gunichar2))   
+      freeme = retval = (wchar_t *)g_utf8_to_utf16(utf8string, -1, NULL, NULL, NULL);
+   if(retval)
+   {
+      retval = wcsdup(retval);
+      g_free(freeme);
+   }
+   return retval;
+}
+
+/*
+ * Converts a wide string into a UTF-8 encoded string.
+ * Parameters:
+ *       wstring: Wide source string.
+ * Returns:
+ *       UTF-8 encoded string that needs to be freed with dw_free()
+ *       or NULL on failure.
+ */
+char * API dw_wchar_to_utf8(wchar_t *wstring)
+{
+   char *retval = NULL, *freeme;
+   
+   if(sizeof(wchar_t) == sizeof(gunichar))
+      freeme = retval = g_ucs4_to_utf8((gunichar *)wstring, -1, NULL, NULL, NULL);
+   else if(sizeof(wchar_t) == sizeof(gunichar2))
+      freeme = retval = g_utf16_to_utf8((gunichar2 *)wstring, -1, NULL, NULL, NULL);
+   if(retval)
+   {
+      retval = strdup(retval);
+      g_free(freeme);
+   }
+   return retval;    
+}
+