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
| #include <stdlib.h>
#include <gtk/gtk.h>
void gsa_OpenFile(gpointer userDatas);
static void gsa_OpenedFile(const gchar *fileName,GtkTextView *textArea);
int main(int argc,char *argv[])
{
GtkWidget *window = NULL;
GtkWidget *generalContainer = NULL;
GtkWidget *textView = NULL;
GtkWidget *openButton = NULL;
GtkWidget *quitButton = NULL;
/* window */
{
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"Gabbec");
}
/* container */
{
generalContainer = gtk_hbutton_box_new();
gtk_container_add(GTK_CONTAINER(window),generalContainer);
}
/* Text */
{
textView = gtk_text_view_new();
gtk_box_pack_start(GTK_BOX(generalContainer),textView,TRUE,TRUE,0);
}
/* quit */
{
quitButton = gtk_button_new_from_stock(GTK_STOCK_QUIT);
g_signal_connect(G_OBJECT(quitButton),"clicked",G_CALLBACK(gtk_main_quit),NULL);
gtk_box_pack_start(GTK_BOX(generalContainer),quitButton,FALSE,FALSE,0);
}
/* openButton */
{
openButton = gtk_button_new_from_stock(GTK_STOCK_OPEN);
g_signal_connect(G_OBJECT(openButton),"clicked",G_CALLBACK(gsa_OpenFile),NULL);
gtk_box_pack_start(GTK_BOX(generalContainer),openButton,FALSE,FALSE,0);
}
g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
gtk_widget_show_all(window);
gtk_main();
return EXIT_SUCCESS;
}
void gsa_OpenFile(gpointer userDatas)
{
GtkWidget *dialog = NULL;
dialog = gtk_file_chooser_dialog_new("Ouvrir un fichier",NULL,
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,NULL);
if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
{
gchar fileName = NULL;
fileName = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
gsa_OpenedFile(fileName,GTK_TEXT_VIEW(userDatas));
g_free(fileName),fileName = NULL;
gtk_widget_destroy(dialog);
}
}
static void gsa_OpenedFile(const gchar *fileName,GtkTextView *textArea)
{
g_return_if_fail(fileName && textArea);
{
gchar *contents = NULL;
if(g_file_get_contents(fileName,&contents,NULL,NULL))
{
gchar *utf8 = NULL;
GtkTextIter iter;
GtkTextBuffer *textBuffer = NULL;
textBuffer = gtk_text_view_get_buffer(textArea);
gtk_text_buffer_get_iter_at_line(textBuffer,&iter,0);
utf8 = g_locale_to_utf8(contents,-1,NULL,NULL,NULL);
g_free (contents),contents = NULL;
gtk_text_buffer_insert(textBuffer,&iter,utf8,-1);
g_free (utf8),utf8 = NULL;
}
else
printf("ERROR -> Can't open %s",fileName);
}
} |