Re: Invalid UTF-8 string passed to pango_layout_set_text()



On Fri, Dec 25, 2009 at 9:14 AM, Randy <randyqiuxy hotmail com> wrote:
> {
> const gchar *textLabel=gtk_label_get_text(GTK_LABEL(label));
> const gchar         *textWindow=gtk_window_get_title(GTK_WINDOW(window));
>
> if(keyEvent->keyval==GDK_Shift_L)
> {
>                 //surprise,if I skip(via "//") one of the two lines, another
>                 //will work fine.....
>        gtk_window_set_title(GTK_WINDOW(window),textLabel);
>        gtk_label_set_text(GTK_LABEL(label),textWindow);
> }
> return FALSE;
> }

This is a bit tricky :)  You have to note that textLabel and
textWindow both point to a string that is internal to the
GtkLabel/GtkWindow widgets.  This is why the string has to the const,
you are not allowed to modify that internal string directly.  The
point is that when you set another title or text, this internal string
will be replaced with the new string.  So as soon as you call
gtk_window_set_title(), the internal title string will be replaced and
after this call your textWindow pointer is no longer valid (because it
points to this internal string that has been replaced).  But you still
have to use it for the call to gtk_label_set_text().  Swapping the
calls will not help, because gtk_label_set_text() does the same and
textLabel will become invalid.

The easiest solution is probably to just duplicate the strings you get
from gtk_label_get_text() and gtk_window_get_title() using g_strdup()
(and free them at the end of the function with g_free() !).  This way
you will have a copy that will persist when the set_title() and
set_text() functions replace their internal strings.

Hope this helps.


regards,

-kris.


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