IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

GTK+ avec C & C++ Discussion :

[GTK+] Invalid utf-8 sur Windows


Sujet :

GTK+ avec C & C++

  1. #1
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Juin 2009
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Portugal

    Informations professionnelles :
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2009
    Messages : 27
    Par défaut [GTK+] Invalid utf-8 sur Windows
    Bon jour,

    J'ai fait une petite code pour apprendre a utiliser une GtkComboBox. Cependant, j'avai des problemes avec le retour de la string dans la function dialog_get_string (). La console me montre le error [Invalid utf-8] et le texte est en hexa. Je besoin d'aide s'il vous plaît.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
     
    #include <gtk/gtk.h>
    #include <stdlib.h>
     
    #define TITLE "GtkComboBox..."
    #define WIDTH 300
    #define HEIGHT 200
     
    typedef struct _WindowData WindowData;
    struct _WindowData
    {
    	GtkWidget *window;
    	GtkWidget *in_item;
    	GtkWidget *cb_item;
    };
     
    gchar *dialog_get_string (gchar *message_string, WindowData *data);
    void on_add_item_clicked (GtkWidget *widget, WindowData *data);
    void on_edit_item_clicked (GtkWidget *widget, WindowData *data);
     
    int main (int argc, char **argv)
    {
    	GtkWidget *vbox;
    	GtkWidget *hbox_frm_in;
    	GtkWidget *vbox_in, *hbox_lbl, *hbox_in, *hbox_op;
    	GtkWidget *frm_in;
    	GtkWidget *lbl_item, *lbl_item_in;
    	GtkWidget *btn_add, *btn_edit, *btn_remove;
    	GtkTreeIter iter;
    	GtkListStore *store;
    	GtkCellRenderer *cell;
     
    	_WindowData win;
     
    	gtk_init (&argc, &argv);
     
    	win.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    	gtk_window_set_title (GTK_WINDOW(win.window), TITLE);
    	gtk_window_set_default_size (GTK_WINDOW(win.window), WIDTH, HEIGHT);
    	//gtk_window_set_resizable(GTK_WINDOW(win.window), FALSE);
    	gtk_window_set_position (GTK_WINDOW(win.window), GTK_WIN_POS_CENTER);
    	g_signal_connect (G_OBJECT(win.window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
     
    	vbox = gtk_vbox_new (FALSE, 0); /* Create a vbox to contain all the GUI */
    	gtk_container_add (GTK_CONTAINER(win.window), vbox); /* Adds vbox to container */
     
    	hbox_frm_in = gtk_hbox_new (FALSE, 0); /* Create a vbox to attach the frame */
    	gtk_box_pack_start (GTK_BOX(vbox), hbox_frm_in, FALSE, FALSE, 0);
    	frm_in = gtk_frame_new ("Input Data...");
    	gtk_box_pack_start (GTK_BOX(hbox_frm_in), frm_in, TRUE, TRUE, 5);
     
    	vbox_in = gtk_vbox_new (FALSE, 0);
    	gtk_container_add (GTK_CONTAINER(frm_in), vbox_in);
     
    	hbox_lbl = gtk_hbox_new (TRUE, 0);
    	gtk_box_pack_start (GTK_BOX(vbox_in), hbox_lbl, FALSE, FALSE, 0);
     
    	lbl_item = gtk_label_new ("Select an Item");
    	gtk_box_pack_start (GTK_BOX(hbox_lbl), lbl_item, TRUE, FALSE, 0);
     
    	lbl_item_in = gtk_label_new ("Input a new item");
    	gtk_box_pack_start (GTK_BOX(hbox_lbl), lbl_item_in, TRUE, FALSE, 0);
     
    	hbox_in = gtk_hbox_new (TRUE, 0);
    	gtk_box_pack_start (GTK_BOX(vbox_in), hbox_in, FALSE, FALSE, 0);
     
    	/**
            * Create a GtkListStore to store items
            * The GtkListSore consists in one column
            * and one text field
            */
    	store = gtk_list_store_new (1, G_TYPE_STRING);
    	/**
            * gtk_list_store_set (GtkListStore *list_store, GtkTreeIter *iter, ...);
            * For example, to set column 0 with type G_TYPE_STRING to "Foo",
            * you would write gtk_list_store_set (store, iter, 0, "Foo", -1).
            */
    	gtk_list_store_append (store, &iter);
    	gtk_list_store_set (store, &iter, 0, "Linux", -1);
    	gtk_list_store_append (store, &iter);
    	gtk_list_store_set (store, &iter, 0, "Mac OS X", -1);
    	gtk_list_store_append (store, &iter);
    	gtk_list_store_set (store, &iter, 0, "Windows 7", -1);
     
    	win.cb_item = gtk_combo_box_new_with_model (GTK_TREE_MODEL(store));
    	gtk_box_pack_start (GTK_BOX(hbox_in), win.cb_item, TRUE, FALSE, 0);
     
    	/* Remove our reference from store to avoid memory leak. */
    	g_object_unref (G_OBJECT(store));
     
    	cell = gtk_cell_renderer_text_new (); /* Create a cell renderer */
    	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT(win.cb_item), cell, TRUE); /* Pack it into the combo box */
    	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT(win.cb_item), cell, "text", 0, NULL);
     
    	gtk_combo_box_set_active (GTK_COMBO_BOX(win.cb_item), 0);
     
     
    	win.in_item = gtk_entry_new ();
    	gtk_entry_set_max_length (GTK_ENTRY(win.in_item), 6);
    	gtk_box_pack_start (GTK_BOX(hbox_in), win.in_item, TRUE, FALSE, 0);
     
    	hbox_op = gtk_hbox_new (TRUE, 0);
    	gtk_box_pack_start (GTK_BOX(vbox_in), hbox_op, FALSE, FALSE, 0);
     
    	btn_add = gtk_button_new_with_label ("Add item");
    	gtk_box_pack_start (GTK_BOX(hbox_op), btn_add, TRUE, TRUE, 0);
    	g_signal_connect (G_OBJECT(btn_add), "clicked", G_CALLBACK(on_add_item_clicked), &win);
     
    	btn_edit = gtk_button_new_with_label ("Edit item");
    	gtk_box_pack_start (GTK_BOX(hbox_op), btn_edit, TRUE, TRUE, 0);
    	g_signal_connect (G_OBJECT(btn_edit), "clicked", G_CALLBACK(on_edit_item_clicked), &win);
     
    	btn_remove = gtk_button_new_with_label ("Remove item");
    	gtk_box_pack_start (GTK_BOX(hbox_op), btn_remove, TRUE, TRUE, 0);
     
     
    	gtk_widget_show_all (win.window);
     
    	gtk_main ();
     
    	return EXIT_SUCCESS;
     
    }
     
    gchar *dialog_get_string (gchar *message_string, WindowData *data)
    {
    	GtkWidget *dialog;
    	GtkWidget *in_str;
    	gchar *string = NULL;
     
    	dialog = gtk_dialog_new_with_buttons (message_string,
    		GTK_WINDOW(data->window),
    		GTK_DIALOG_MODAL,
    		GTK_STOCK_OK,
    		GTK_RESPONSE_OK,
    		GTK_STOCK_CANCEL,
    		GTK_RESPONSE_CANCEL,
    		NULL);
     
    	in_str = gtk_entry_new ();
    	gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), in_str, TRUE, FALSE, 0);
     
    	gtk_widget_show_all (GTK_DIALOG(dialog)->vbox);
     
    	switch (gtk_dialog_run(GTK_DIALOG(dialog)))
    	{
    		case GTK_RESPONSE_OK:
    			string = (gchar *) gtk_entry_get_text(GTK_ENTRY(in_str));
    			break;
     
    		case GTK_RESPONSE_CANCEL:
    		case GTK_RESPONSE_NONE:
    		default:
    			break;
     
    	}
     
    	gtk_widget_destroy (dialog);
     
    	return string;
    }
     
    void on_add_item_clicked (GtkWidget *widget, WindowData *data)
    {
    	GtkTreeIter iter;
    	GtkListStore *store;
    	gchar *string = NULL;
     
    	string = (gchar *) gtk_entry_get_text (GTK_ENTRY(data->in_item));
    	store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX(data->cb_item)));
    	gtk_list_store_append (store, &iter);
    	gtk_list_store_set (store, &iter, 0, string, -1);
     
    	g_print ("\nItem %s added...", string);
     
    	gtk_editable_delete_text (GTK_EDITABLE(data->in_item), 0, -1);
     
    	//Don't work in windows7
    	//if (string) /* Free string (if not NULL) */
    	//	g_free (string);
    }
     
    void on_edit_item_clicked (GtkWidget *widget, WindowData *data)
    {
    	GtkTreeIter iter;
    	GtkListStore *store;
    	gchar *string = NULL;
     
    	if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX(data->cb_item), &iter))
    	{
    		//store = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(data->cb_item)));
    		string = dialog_get_string ("Input the new item!", data);
    		gtk_entry_set_text (GTK_ENTRY(data->in_item), string);
    		g_print ("\nNew item: %s", string);
    	}
     
    }

  2. #2
    Expert confirmé
    Avatar de gerald3d
    Homme Profil pro
    Conducteur de train
    Inscrit en
    Février 2008
    Messages
    2 308
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Conducteur de train
    Secteur : Transports

    Informations forums :
    Inscription : Février 2008
    Messages : 2 308
    Billets dans le blog
    5
    Par défaut
    A tout hasard sauve tes fichiers sources en utf8 et pas dans la variable locale de Windows.

  3. #3
    Modérateur

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2009
    Messages
    1 395
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2009
    Messages : 1 395
    Par défaut
    Citation Envoyé par gerald3d Voir le message
    pas dans la variable locale de Windows.
    ?

    http://en.wikipedia.org/wiki/Locale

  4. #4
    Expert confirmé
    Avatar de gerald3d
    Homme Profil pro
    Conducteur de train
    Inscrit en
    Février 2008
    Messages
    2 308
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Conducteur de train
    Secteur : Transports

    Informations forums :
    Inscription : Février 2008
    Messages : 2 308
    Billets dans le blog
    5
    Par défaut
    Mon clavier à fourché .

    Je voulais dire "...pas dans la norme ISO 8859-15". Maintenant je suis loin d'être un spécialiste du codage de caractère. Peut être que Windows est en UTF8 par défaut?

  5. #5
    Modérateur

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2009
    Messages
    1 395
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2009
    Messages : 1 395
    Par défaut
    L'erreur se trouve à la ligne 147. gtk_entry_get_text renvoie un const char*, mais toi tu fais un cast pour convertir la valeur en char * ! En gros le compilateur a dû se plaindre, et plutôt que de comprendre ce qu'il te disait tu l'as fait taire violemment... La valeur renvoyée est un const car elle est gérée en interne par le GtkEntry. Si tu veux la conserver plus longtemps que la durée de vie du contrôle, il te faut la copier, avec g_strdup.

    Tu peux donc corriger ton bug en remplaçant la ligne:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    string = (gchar *) gtk_entry_get_text(GTK_ENTRY(in_str));
    par:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    string = g_strdup (gtk_entry_get_text(GTK_ENTRY(in_str)));

  6. #6
    Modérateur

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2009
    Messages
    1 395
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2009
    Messages : 1 395
    Par défaut
    Citation Envoyé par gerald3d Voir le message
    Peut être que Windows est en UTF8 par défaut?
    Les distributions Linux utilisent majoritairement UTF-8, et c'est le codage utilisé par la GLib et GTK+. En revanche, sous Windows en NTFS les noms de fichiers sont en UTF-16.

  7. #7
    Expert confirmé
    Avatar de gerald3d
    Homme Profil pro
    Conducteur de train
    Inscrit en
    Février 2008
    Messages
    2 308
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Conducteur de train
    Secteur : Transports

    Informations forums :
    Inscription : Février 2008
    Messages : 2 308
    Billets dans le blog
    5
    Par défaut
    Finalement Windows a pris de l'avance...

  8. #8
    Modérateur

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2009
    Messages
    1 395
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2009
    Messages : 1 395
    Par défaut
    Ext3 et ext4 gère tout Unicode pour les noms de fichiers, comme NTFS.

    C'est la GLib qui utilise UTF-8 en interne:
    http://library.gnome.org/devel/glib/...name-encodings

    Mais c'est plus un choix qu'une limitation... En UTF-16, n'importe quelle caractère prend 2 octets minimum. En UTF-8, c'est un octet minimum. UTF-8 avantage les langues occidentales, UTF-16 les langues orientales...

  9. #9
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Juin 2009
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Portugal

    Informations professionnelles :
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2009
    Messages : 27
    Par défaut
    Citation Envoyé par liberforce Voir le message
    L'erreur se trouve à la ligne 147. gtk_entry_get_text renvoie un const char*, mais toi tu fais un cast pour convertir la valeur en char * ! En gros le compilateur a dû se plaindre, et plutôt que de comprendre ce qu'il te disait tu l'as fait taire violemment... La valeur renvoyée est un const car elle est gérée en interne par le GtkEntry. Si tu veux la conserver plus longtemps que la durée de vie du contrôle, il te faut la copier, avec g_strdup.

    Tu peux donc corriger ton bug en remplaçant la ligne:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    string = (gchar *) gtk_entry_get_text(GTK_ENTRY(in_str));
    par:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    string = g_strdup (gtk_entry_get_text(GTK_ENTRY(in_str)));
    Merci liberforce, ça marche! Je n'ai pas regarder le prototype de la function.

  10. #10
    Expert confirmé
    Avatar de gerald3d
    Homme Profil pro
    Conducteur de train
    Inscrit en
    Février 2008
    Messages
    2 308
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Conducteur de train
    Secteur : Transports

    Informations forums :
    Inscription : Février 2008
    Messages : 2 308
    Billets dans le blog
    5
    Par défaut
    Citation Envoyé par liberforce Voir le message
    Ext3 et ext4 gère tout Unicode pour les noms de fichiers, comme NTFS.

    C'est la GLib qui utilise UTF-8 en interne:
    http://library.gnome.org/devel/glib/...name-encodings

    Mais c'est plus un choix qu'une limitation... En UTF-16, n'importe quelle caractère prend 2 octets minimum. En UTF-8, c'est un octet minimum. UTF-8 avantage les langues occidentales, UTF-16 les langues orientales...
    Merci pour la précision Liberforce.

  11. #11
    Expert confirmé
    Avatar de gerald3d
    Homme Profil pro
    Conducteur de train
    Inscrit en
    Février 2008
    Messages
    2 308
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Conducteur de train
    Secteur : Transports

    Informations forums :
    Inscription : Février 2008
    Messages : 2 308
    Billets dans le blog
    5
    Par défaut
    Citation Envoyé par 0xpoint Voir le message
    Merci liberforce, ça marche! Je n'ai pas regarder le prototype de la fonction.
    J'en profite pour ajouter une petite information. g_strdup(); alloue dans le tas. Il faudra donc penser à libérer string avec un g_free(); une fois son utilisation devenue inutile.

Discussions similaires

  1. Installer GTK+ sur windows
    Par Soporythmique dans le forum GTK+ avec C & C++
    Réponses: 1
    Dernier message: 07/06/2012, 10h41
  2. Exporter un .exe qui utilise GTK+ sur windows NT
    Par suzan_ dans le forum GTK+ avec C & C++
    Réponses: 6
    Dernier message: 31/08/2010, 14h43
  3. utiliser un .exe sur Windows NT qui utilise GTK+
    Par suzan_ dans le forum Windows 2000/Me/98/95
    Réponses: 0
    Dernier message: 30/08/2010, 14h59
  4. pb d'installation d'Oracle 9 sur windows 2000
    Par condor dans le forum Oracle
    Réponses: 1
    Dernier message: 14/12/2006, 11h40
  5. pb d'installation d'Oracle 9 sur windows 2000
    Par condor dans le forum Oracle
    Réponses: 1
    Dernier message: 14/06/2006, 14h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo