SHARE
TWEET

Alvin Delagon

a guest Apr 8th, 2009 251 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * File: textedit.c
  3.  * Author: Alvin Delagon
  4.  * Summary: This is a shameful take to learn GTK+ programming in C
  5.  *
  6.  */
  7.  
  8. #include <gtk/gtk.h>
  9. #include <glade/glade.h>
  10. #include <gio/gio.h>
  11.  
  12. void on_save(GtkWidget *widget, GtkTextBuffer *txtbuffer)
  13. {
  14.   GtkWindow *window;
  15.   GtkWidget *dialog;
  16.   dialog = gtk_file_chooser_dialog_new("Save File",
  17.                                        window,
  18.                                        GTK_FILE_CHOOSER_ACTION_SAVE,
  19.                                        GTK_STOCK_CANCEL,
  20.                                        GTK_RESPONSE_CANCEL,
  21.                                        GTK_STOCK_SAVE,
  22.                                        GTK_RESPONSE_ACCEPT,
  23.                                        NULL);
  24.   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
  25.     {
  26.       GFile *file = NULL;
  27.       GFileOutputStream *stream = NULL;
  28.       GError *error;
  29.       GtkTextIter start;
  30.       GtkTextIter end;
  31.       GFileCreateFlags flags;
  32.       char *filename;
  33.       gchar *buffer;
  34.       gsize count;
  35.       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
  36.       file = g_file_new_for_path (filename);
  37.       stream = g_file_replace (file, NULL, TRUE, flags, NULL, &error);
  38.       gtk_text_buffer_get_start_iter (txtbuffer, &start);
  39.       gtk_text_buffer_get_end_iter (txtbuffer, &end);
  40.       buffer = gtk_text_buffer_get_text (txtbuffer,
  41.                                          &start,
  42.                                          &end,
  43.                                          TRUE);
  44.       count = gtk_text_buffer_get_char_count (txtbuffer);
  45.       g_output_stream_write (G_OUTPUT_STREAM (stream), buffer, count, NULL, &error);
  46.     }
  47.   gtk_widget_destroy (dialog);
  48. }
  49.                                        
  50.                                        
  51. void on_open(GtkWidget *widget, GtkTextBuffer *txtbuffer)
  52. {
  53.   GtkWindow *window;
  54.   GtkWidget *dialog;
  55.   dialog = gtk_file_chooser_dialog_new("Open File",
  56.                                        window,
  57.                                        GTK_FILE_CHOOSER_ACTION_OPEN,
  58.                                        GTK_STOCK_CANCEL,
  59.                                        GTK_RESPONSE_CANCEL,
  60.                                        GTK_STOCK_OPEN,
  61.                                        GTK_RESPONSE_ACCEPT,
  62.                                        NULL);
  63.   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
  64.     {
  65.       char *filename;
  66.       char *buffer;
  67.       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
  68.       GFile *file = NULL;
  69.       GFileInputStream *stream = NULL;
  70.       GError *error;
  71.       GtkTextIter iter;
  72.       gssize bytes_read;
  73.       gboolean is_reading = TRUE;
  74.       file = g_file_new_for_path (filename);
  75.       stream = g_file_read (file, NULL, &error);
  76.       buffer = g_malloc (1024);
  77.       gtk_text_buffer_set_text (txtbuffer, "", -1);
  78.       while (is_reading)
  79.         {
  80.           bytes_read = g_input_stream_read (G_INPUT_STREAM (stream), buffer, 1024, NULL, &error);
  81.           gtk_text_buffer_get_end_iter (txtbuffer, &iter);
  82.           gtk_text_buffer_insert (txtbuffer, &iter, buffer, bytes_read);
  83.           if (bytes_read == 0)
  84.             is_reading = FALSE;
  85.         }        
  86.     }
  87.   gtk_widget_destroy (dialog);
  88. }
  89.  
  90. void on_exit(GtkWidget *widget, gpointer gdata)
  91. {
  92.   gtk_main_quit();
  93. }
  94.  
  95. int main(int argc, char *argv[])
  96. {
  97.   GladeXML *main_window;
  98.   GtkWidget *widget;
  99.   GtkTextBuffer *txtbuffer;
  100.   gtk_init (&argc, &argv);
  101.   main_window = glade_xml_new ("data/textedit.glade", NULL, NULL);
  102.   widget = glade_xml_get_widget (main_window, "TextViewMain");
  103.   txtbuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
  104.  
  105.   /* Main Window Event Handlers */
  106.   widget = glade_xml_get_widget (main_window, "MainWindow");
  107.   g_signal_connect (G_OBJECT (widget),
  108.                     "delete_event",
  109.                     G_CALLBACK (on_exit),
  110.                     NULL);
  111.  
  112.   /* Menu Item Handlers */
  113.   widget = glade_xml_get_widget (main_window, "OpenItem");
  114.   g_signal_connect (G_OBJECT (widget),
  115.                     "activate",
  116.                     G_CALLBACK (on_open),
  117.                     txtbuffer);
  118.   widget = glade_xml_get_widget (main_window, "SaveItem");
  119.   g_signal_connect (G_OBJECT (widget),
  120.                     "activate",
  121.                     G_CALLBACK (on_save),
  122.                     txtbuffer);
  123.   widget = glade_xml_get_widget (main_window, "ExitItem");
  124.   g_signal_connect (G_OBJECT (widget),
  125.                     "activate",
  126.                     G_CALLBACK (on_exit),
  127.                     NULL);
  128.   gtk_main();
  129.   return 0;
  130. }
RAW Paste Data
Want to get better at C?
Learn to code C in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top