SHARE
TWEET

Untitled

a guest Nov 20th, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <gtk/gtk.h>
  2. #include <gtksourceview/gtksource.h>
  3. #include <gdk/gdkkeysyms.h>
  4. #include <glib/gi18n.h>
  5. #include <errno.h>
  6.  
  7. void dialog_show(GtkWidget *textview);
  8.  
  9. int main (int argc,
  10. char *argv[])
  11. {
  12.     GtkWidget *window, *scrolled_win, *textview;
  13.     GtkWidget *vbox, *menubar, *fileMenu, *fileMi, *quit_item, *open_item;
  14.    
  15.     gtk_init (&argc, &argv);
  16.    
  17.     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  18.     gtk_window_maximize(GTK_WINDOW (window)); /* This is a cast: expand the windows to fullscreen*/
  19.     vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 12);
  20.     gtk_container_add(GTK_CONTAINER(window), vbox);
  21.    
  22.     gtk_window_set_title (GTK_WINDOW (window), "Text Views");
  23.     gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  24.    
  25.     textview = gtk_source_view_new();
  26.    
  27.     menubar = gtk_menu_bar_new();
  28.     fileMenu = gtk_menu_new();
  29.    
  30.     fileMi = gtk_menu_item_new_with_label("File");
  31.    
  32.     open_item = gtk_menu_item_new_with_label("Open");
  33.     quit_item = gtk_menu_item_new_with_label("Quit");
  34.    
  35.     /* CTRL-Q accelerator */
  36.     GtkAccelGroup *accel_group = NULL;
  37.     accel_group = gtk_accel_group_new();
  38.     gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
  39.    
  40.     gtk_widget_add_accelerator(quit_item, "activate", accel_group,
  41.       GDK_KEY_q, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE); 
  42.    
  43.     /*Expand the textbox to fullscreen */  
  44.     gtk_widget_set_hexpand(textview, TRUE);
  45.     gtk_widget_set_vexpand(textview, TRUE);
  46.    
  47.     GtkCssProvider *provider = gtk_css_provider_new ();
  48.     gtk_css_provider_load_from_path (provider,
  49.     "gtk-widgets.css", NULL);
  50.    
  51.     GtkStyleContext *context;
  52.    
  53.     context = gtk_widget_get_style_context(textview);
  54.    
  55.     gtk_style_context_add_provider (context,
  56.                                     GTK_STYLE_PROVIDER(provider),
  57.                                     GTK_STYLE_PROVIDER_PRIORITY_USER);
  58.      
  59.     gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileMi), fileMenu);
  60.     gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), open_item);
  61.     gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), quit_item);    
  62.    
  63.     gtk_menu_shell_append(GTK_MENU_SHELL(menubar), fileMi);
  64.     gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
  65.    
  66.     scrolled_win = gtk_scrolled_window_new (NULL, NULL);
  67.     gtk_container_add(GTK_CONTAINER(vbox), scrolled_win);
  68.     gtk_container_add (GTK_CONTAINER (scrolled_win), textview);
  69.    
  70.     gtk_source_view_set_show_line_numbers (GTK_SOURCE_VIEW(textview), TRUE); /*Show line numbers */
  71.  
  72.     gtk_widget_show_all (window);
  73.    
  74.     g_signal_connect(G_OBJECT(window), "destroy",
  75.         G_CALLBACK(gtk_main_quit), NULL);
  76.        
  77.     g_signal_connect(G_OBJECT(quit_item), "activate",
  78.         G_CALLBACK(gtk_main_quit), NULL);
  79.        
  80.     g_signal_connect(G_OBJECT(open_item), "activate",
  81.         G_CALLBACK(dialog_show), &textview);
  82.    
  83.     gtk_main();
  84.    
  85.     return 0;
  86. }
  87.  
  88. void dialog_show(GtkWidget *textview)
  89. {
  90.     FILE *fp;
  91.    
  92.     GtkWindow *parent_window = NULL;
  93.    
  94.     GtkFileChooserNative *native;
  95.     gint res;
  96.  
  97.     native = gtk_file_chooser_native_new ("Open File",
  98.                                       parent_window,
  99.                                       GTK_FILE_CHOOSER_ACTION_OPEN,
  100.                                        _("_Open"),
  101.                                        _("_Cancel"));                                  
  102.  
  103.     res = gtk_native_dialog_run (GTK_NATIVE_DIALOG (native));
  104.    
  105.     if (res == GTK_RESPONSE_ACCEPT)
  106.       {
  107.         char *filename;
  108.         GtkFileChooser *chooser = GTK_FILE_CHOOSER (native);
  109.         filename = gtk_file_chooser_get_filename (chooser);
  110.        
  111.         if(!(fp = fopen(filename, "r"))) /* if file cannot be opened */
  112.         {
  113.             g_printerr("%s: %s\n", filename, g_strerror(errno));
  114.             return;
  115.         }
  116.        
  117.         else {
  118.             char *buf;
  119.             GtkTextBuffer * buffer = gtk_text_view_get_buffer (textview);
  120.             fseek(fp, 0, SEEK_END);
  121.             long fsize = ftell(fp);
  122.             fseek(fp, 0, SEEK_SET);
  123.  
  124.             buf = malloc(fsize + 1);
  125.             fread(buf, fsize, 1, fp);
  126.             buf[fsize] = '\0';
  127.  
  128.             gtk_text_buffer_set_text(buffer, buf, -1);
  129.  
  130.             free(buf);
  131.             fclose(fp);
  132.         }
  133.        
  134.         g_free (filename);
  135.         fclose(fp);
  136.       }
  137.  
  138.     g_object_unref (native);
  139.  
  140. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top