parent
285e5022bf
commit
4318b883ac
|
@ -1,17 +1,36 @@
|
|||
#include <gtk/gtk.h>
|
||||
#include <cstdio>
|
||||
|
||||
static GtkWidget *infobar;
|
||||
static GtkTextBuffer *textbuffer;
|
||||
static int filemode;
|
||||
|
||||
void dialog_response(GtkWidget *widget,int response,gpointer data){
|
||||
static void dialog_response(GtkWidget *widget,int response,gpointer data){
|
||||
char *contents,*filename;
|
||||
gsize length;
|
||||
if(response==GTK_RESPONSE_OK){
|
||||
GFile *file=gtk_file_chooser_get_file((GtkFileChooser*)widget);
|
||||
filename=g_file_get_path(file);
|
||||
if(filemode==0){
|
||||
//Get File contents and show
|
||||
if(g_file_get_contents(filename,&contents,&length,NULL)){
|
||||
gtk_text_buffer_set_text(textbuffer,contents,length);
|
||||
}
|
||||
}else{
|
||||
//Get Start and End char of Text
|
||||
GtkTextIter start,end;
|
||||
gtk_text_buffer_get_start_iter(textbuffer,&start);
|
||||
gtk_text_buffer_get_end_iter(textbuffer,&end);
|
||||
//Get Contents
|
||||
contents=gtk_text_buffer_get_text(textbuffer,&start,&end,TRUE);
|
||||
//Create a file and write
|
||||
FILE *fp;
|
||||
fp=fopen(filename,"wt+");
|
||||
if(fp!=NULL){
|
||||
fputs(contents,fp);
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
g_object_unref(file);
|
||||
g_free(contents);
|
||||
g_free(filename);
|
||||
|
@ -19,7 +38,8 @@ void dialog_response(GtkWidget *widget,int response,gpointer data){
|
|||
gtk_widget_destroy(widget);
|
||||
}
|
||||
|
||||
void openfile_dialog(GtkWidget *widget,GtkWindow *parent){
|
||||
static void openfile_dialog(GtkWidget *widget,GtkWindow *parent){
|
||||
filemode=0;
|
||||
GtkFileChooserAction action=GTK_FILE_CHOOSER_ACTION_OPEN;
|
||||
GtkWidget *dialog=gtk_file_chooser_dialog_new("Open a text file",parent,
|
||||
action,"OK",GTK_RESPONSE_OK,"Cancel",GTK_RESPONSE_CANCEL,NULL);
|
||||
|
@ -40,12 +60,34 @@ void openfile_dialog(GtkWidget *widget,GtkWindow *parent){
|
|||
gtk_widget_show_all(dialog);
|
||||
}
|
||||
|
||||
void textbuffer_clear(GtkWidget *widget,GtkTextBuffer *buffer){
|
||||
static void savefile_dialog(GtkWidget *widget,GtkWindow *parent){
|
||||
filemode=1;
|
||||
GtkFileChooserAction action=GTK_FILE_CHOOSER_ACTION_SAVE;
|
||||
GtkWidget *dialog=gtk_file_chooser_dialog_new("Open a text file",parent,
|
||||
action,"OK",GTK_RESPONSE_OK,"Cancel",GTK_RESPONSE_CANCEL,NULL);
|
||||
|
||||
GtkFileFilter *filter=gtk_file_filter_new();
|
||||
gtk_file_filter_set_name(filter,"Text Files");
|
||||
gtk_file_filter_add_mime_type(filter,"text/*");
|
||||
gtk_file_chooser_add_filter((GtkFileChooser*)dialog,filter);
|
||||
//g_object_unref(filter);
|
||||
|
||||
filter=gtk_file_filter_new();
|
||||
gtk_file_filter_set_name(filter,"Any Files");
|
||||
gtk_file_filter_add_pattern(filter,"*");
|
||||
gtk_file_chooser_add_filter((GtkFileChooser*)dialog,filter);
|
||||
//g_object_unref(filter);
|
||||
|
||||
g_signal_connect(dialog,"response",G_CALLBACK(dialog_response),NULL);
|
||||
gtk_widget_show_all(dialog);
|
||||
}
|
||||
|
||||
static void textbuffer_clear(GtkWidget *widget,GtkTextBuffer *buffer){
|
||||
gtk_text_buffer_set_text(buffer,"",-1);
|
||||
gtk_widget_show(infobar);
|
||||
}
|
||||
|
||||
void infobar_response(GtkWidget *widget,gpointer data){
|
||||
static void infobar_response(GtkWidget *widget,gpointer data){
|
||||
gtk_widget_hide(widget);
|
||||
}
|
||||
|
||||
|
@ -81,15 +123,18 @@ static void gtkmain(GtkApplication *app,gpointer user_data){
|
|||
|
||||
//Add Buttons
|
||||
GtkWidget *btn_box=gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
|
||||
GtkWidget *btnopen,*btnclear,*btnexit;
|
||||
GtkWidget *btnopen,*btnsave,*btnclear,*btnexit;
|
||||
btnopen=gtk_button_new_with_label("Open File");
|
||||
g_signal_connect(btnopen,"clicked",G_CALLBACK(openfile_dialog),window);
|
||||
btnsave=gtk_button_new_with_label("Save File");
|
||||
g_signal_connect(btnsave,"clicked",G_CALLBACK(savefile_dialog),window);
|
||||
btnclear=gtk_button_new_with_label("Clear");
|
||||
g_signal_connect(btnclear,"clicked",G_CALLBACK(textbuffer_clear),textbuffer);
|
||||
btnexit=gtk_button_new_with_label("Exit");
|
||||
g_signal_connect_swapped(btnexit,"clicked",G_CALLBACK(gtk_widget_destroy),window);
|
||||
gtk_box_pack_end((GtkBox*)btn_box,btnexit,FALSE,FALSE,0);
|
||||
gtk_box_pack_end((GtkBox*)btn_box,btnclear,FALSE,FALSE,0);
|
||||
gtk_box_pack_end((GtkBox*)btn_box,btnsave,FALSE,FALSE,0);
|
||||
gtk_box_pack_end((GtkBox*)btn_box,btnopen,FALSE,FALSE,0);
|
||||
gtk_box_pack_end((GtkBox*)vbox,btn_box,TRUE,FALSE,0);
|
||||
|
||||
|
|
Loading…
Reference in New Issue