diff --git a/Gtk4/gtk107_text_gtk4/meson.build b/Gtk4/gtk107_text_gtk4/meson.build new file mode 100644 index 0000000..424a966 --- /dev/null +++ b/Gtk4/gtk107_text_gtk4/meson.build @@ -0,0 +1,35 @@ +#A Simple Project Test +project('gtk107', 'cpp', + default_options : ['c_std=c17', 'cpp_std=c++17']) + +#Initalize variants +gnome=import('gnome') + +#Compile Resource +gresources = gnome.compile_resources( + 'resources', 'res/gtk107.gresource.xml', + source_dir: 'res', + c_name: 'resources' +) + +#compile schemas +app_schemas = gnome.compile_schemas(depend_files: 'org.gtk.daleclack.gschema.xml') + +#The Gtkmm Library as a dependency +gtkdep = dependency('gtk4') + +#Additional include dirs +dir_include = include_directories('..') + +#source files +src = ['src/main.cpp', 'src/myapplication.cpp', 'src/mywindow.cpp', 'src/MyDialog.cpp', 'src/myprefs.cpp'] + +#Use Different Build Opinions in windows and Linux +if host_machine.system() == 'windows' + win=import('windows') + icon_res=win.compile_resources('../icon.rc') + executable('gtk107', icon_res, src, gresources, app_schemas, dependencies : gtkdep, + win_subsystem : 'windows', include_directories : dir_include) +else + executable('gtk107', src, gresources, app_schemas, dependencies : gtkdep, include_directories : dir_include) +endif diff --git a/Gtk4/gtk107_text_gtk4/org.gtk.daleclack.gschema.xml b/Gtk4/gtk107_text_gtk4/org.gtk.daleclack.gschema.xml new file mode 100644 index 0000000..83749ff --- /dev/null +++ b/Gtk4/gtk107_text_gtk4/org.gtk.daleclack.gschema.xml @@ -0,0 +1,25 @@ + + + + + 'Monospace 12' + Font + The font to be used for content. + + + + + + + + 'none' + Transition + The transition to use when switching tabs. + + + false + Show words + Whether to show a word list in the sidebar + + + diff --git a/Gtk4/gtk107_text_gtk4/res/app-menu.ui b/Gtk4/gtk107_text_gtk4/res/app-menu.ui new file mode 100644 index 0000000..68ede47 --- /dev/null +++ b/Gtk4/gtk107_text_gtk4/res/app-menu.ui @@ -0,0 +1,38 @@ + + + + +
+ + _Open File + win.open + + + Save + win.save + + + Save As + win.save-as + +
+
+ + _Lines + win.show-lines + +
+
+ + _Preferences + win.preferences + +
+
+ + _Quit + win.quit + +
+
+
diff --git a/Gtk4/gtk107_text_gtk4/res/gtk107.gresource.xml b/Gtk4/gtk107_text_gtk4/res/gtk107.gresource.xml new file mode 100644 index 0000000..da17115 --- /dev/null +++ b/Gtk4/gtk107_text_gtk4/res/gtk107.gresource.xml @@ -0,0 +1,8 @@ + + + + window.ui + app-menu.ui + prefs.ui + + diff --git a/Gtk4/gtk107_text_gtk4/res/prefs.ui b/Gtk4/gtk107_text_gtk4/res/prefs.ui new file mode 100644 index 0000000..c132d9a --- /dev/null +++ b/Gtk4/gtk107_text_gtk4/res/prefs.ui @@ -0,0 +1,67 @@ + + + + diff --git a/Gtk4/gtk107_text_gtk4/res/window.ui b/Gtk4/gtk107_text_gtk4/res/window.ui new file mode 100644 index 0000000..acb75cd --- /dev/null +++ b/Gtk4/gtk107_text_gtk4/res/window.ui @@ -0,0 +1,77 @@ + + + + diff --git a/Gtk4/gtk107_text_gtk4/src/MyDialog.cpp b/Gtk4/gtk107_text_gtk4/src/MyDialog.cpp new file mode 100644 index 0000000..c96cd05 --- /dev/null +++ b/Gtk4/gtk107_text_gtk4/src/MyDialog.cpp @@ -0,0 +1,32 @@ +#include "MyDialog.h" +#include "../text_types.h" + +static void my_dialog_response(GtkDialog *dialog,int response_id){ + GtkFileChooserAction action=gtk_file_chooser_get_action(GTK_FILE_CHOOSER(dialog)); + if(response_id == GTK_RESPONSE_OK){ + GtkWindow *win=gtk_window_get_transient_for(GTK_WINDOW(dialog)); + GFile *file=gtk_file_chooser_get_file(GTK_FILE_CHOOSER(dialog)); + switch(action){ + case GTK_FILE_CHOOSER_ACTION_OPEN: + my_window_open(MY_WINDOW(win),file); + break; + case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER: + break; + case GTK_FILE_CHOOSER_ACTION_SAVE: + break; + } + g_object_unref(file); + } + gtk_window_destroy(GTK_WINDOW(dialog)); +} + +void my_dialog_new(MyWindow *window,const char *title,GtkFileChooserAction action){ + //Initalize window + GtkWidget *dialog=gtk_file_chooser_dialog_new(title,GTK_WINDOW(window), + action,"OK",GTK_RESPONSE_OK,"Cancel",GTK_RESPONSE_CANCEL,NULL); + + g_signal_connect(dialog,"response",G_CALLBACK(my_dialog_response),NULL); + + gtk_window_present(GTK_WINDOW(dialog)); +} + diff --git a/Gtk4/gtk107_text_gtk4/src/MyDialog.h b/Gtk4/gtk107_text_gtk4/src/MyDialog.h new file mode 100644 index 0000000..06783b9 --- /dev/null +++ b/Gtk4/gtk107_text_gtk4/src/MyDialog.h @@ -0,0 +1,5 @@ +#pragma once + +#include "mywindow.h" + +void my_dialog_new(MyWindow *window,const char *title,GtkFileChooserAction action); diff --git a/Gtk4/gtk107_text_gtk4/src/gschemas.compiled b/Gtk4/gtk107_text_gtk4/src/gschemas.compiled new file mode 100644 index 0000000..c398aaf Binary files /dev/null and b/Gtk4/gtk107_text_gtk4/src/gschemas.compiled differ diff --git a/Gtk4/gtk107_text_gtk4/src/main.cpp b/Gtk4/gtk107_text_gtk4/src/main.cpp new file mode 100644 index 0000000..554167a --- /dev/null +++ b/Gtk4/gtk107_text_gtk4/src/main.cpp @@ -0,0 +1,8 @@ +#include "myapplication.h" + +int main(int argc,char **argv){ + //Set Schemas dir + g_setenv("GSETTINGS_SCHEMA_DIR",".",FALSE); + + return g_application_run(G_APPLICATION(my_application_new()),argc,argv); +} diff --git a/Gtk4/gtk107_text_gtk4/src/myapplication.cpp b/Gtk4/gtk107_text_gtk4/src/myapplication.cpp new file mode 100644 index 0000000..99e21bc --- /dev/null +++ b/Gtk4/gtk107_text_gtk4/src/myapplication.cpp @@ -0,0 +1,28 @@ +#include "myapplication.h" +#include "mywindow.h" + +struct _MyApplication{ + GtkApplication parent; +}; + +G_DEFINE_TYPE(MyApplication,my_application,GTK_TYPE_APPLICATION) + +static void my_application_activate(GApplication *app){ + MyWindow *win; + win=my_window_new(MY_APPLICATION(app)); + gtk_window_present(GTK_WINDOW(win)); +} + +static void my_application_init(MyApplication *app){ +} + +static void my_application_class_init(MyApplicationClass *appclass){ + G_APPLICATION_CLASS(appclass)->activate=my_application_activate; +} + +MyApplication * my_application_new(){ + return (MyApplication*)g_object_new(MY_APPLICATION_TYPE, + "application-id","org.gtk.daleclack", + "flags",G_APPLICATION_NON_UNIQUE, + NULL); +} diff --git a/Gtk4/gtk107_text_gtk4/src/myapplication.h b/Gtk4/gtk107_text_gtk4/src/myapplication.h new file mode 100644 index 0000000..a5b2da6 --- /dev/null +++ b/Gtk4/gtk107_text_gtk4/src/myapplication.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +#define MY_APPLICATION_TYPE (my_application_get_type()) +G_DECLARE_FINAL_TYPE(MyApplication,my_application,MY,APPLICATION,GtkApplication) + +MyApplication * my_application_new(); diff --git a/Gtk4/gtk107_text_gtk4/src/myprefs.cpp b/Gtk4/gtk107_text_gtk4/src/myprefs.cpp new file mode 100644 index 0000000..5bd0b9f --- /dev/null +++ b/Gtk4/gtk107_text_gtk4/src/myprefs.cpp @@ -0,0 +1,46 @@ +#include "myapplication.h" +#include "myprefs.h" + +struct _MyPrefs{ + GtkDialog parent; + GSettings *settings; + GtkWidget *font; + GtkWidget *transition; +}; + +G_DEFINE_TYPE(MyPrefs,my_prefs,GTK_TYPE_DIALOG) + +static void my_prefs_init(MyPrefs *dialog){ + gtk_widget_init_template(GTK_WIDGET(dialog)); + dialog->settings=g_settings_new("org.gtk.daleclack"); + + //Bind Proprties + g_settings_bind(dialog->settings,"font", + dialog->font,"font", + G_SETTINGS_BIND_DEFAULT); + + g_settings_bind(dialog->settings,"transition", + dialog->transition,"active-id", + G_SETTINGS_BIND_DEFAULT); +} + +static void my_prefs_dispose(GObject *object){ + //Free Memory + g_clear_object(&MY_PREFS(object)->settings); + + G_OBJECT_CLASS(my_prefs_parent_class)->dispose(object); +} + +static void my_prefs_class_init(MyPrefsClass *dlgclass){ + //Inintalize Dialog and get child + G_OBJECT_CLASS(dlgclass)->dispose=my_prefs_dispose; + + gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(dlgclass), + "/org/gtk/daleclack/prefs.ui"); + gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(dlgclass),MyPrefs,font); + gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(dlgclass),MyPrefs,transition); +} + +MyPrefs * my_prefs_new(MyWindow *win){ + return (MyPrefs*)g_object_new(MY_PREFS_TYPE, "transient-for", win, "use-header-bar", TRUE, NULL); +} diff --git a/Gtk4/gtk107_text_gtk4/src/myprefs.h b/Gtk4/gtk107_text_gtk4/src/myprefs.h new file mode 100644 index 0000000..154d0dc --- /dev/null +++ b/Gtk4/gtk107_text_gtk4/src/myprefs.h @@ -0,0 +1,11 @@ +#ifndef __MYPREFS_H_ +#define __MYPREFS_H_ + +#include "mywindow.h" + +#define MY_PREFS_TYPE (my_prefs_get_type()) +G_DECLARE_FINAL_TYPE(MyPrefs,my_prefs,MY,PREFS,GtkDialog) + +MyPrefs * my_prefs_new(MyWindow *win); + +#endif diff --git a/Gtk4/gtk107_text_gtk4/src/mywindow.cpp b/Gtk4/gtk107_text_gtk4/src/mywindow.cpp new file mode 100644 index 0000000..5cf400f --- /dev/null +++ b/Gtk4/gtk107_text_gtk4/src/mywindow.cpp @@ -0,0 +1,203 @@ +#include "mywindow.h" +#include "MyDialog.h" +#include "myprefs.h" + +struct _MyWindow{ + GtkApplicationWindow parent; + GtkWidget * stack; + GtkWidget * gears; + GSettings * settings; + GtkWidget * search; + GtkWidget * searchbar; + GtkWidget * lines; + GtkWidget * lines_label; +}; + +G_DEFINE_TYPE(MyWindow,my_window,GTK_TYPE_APPLICATION_WINDOW) + +static void search_text_changed(GtkEntry *entry,MyWindow *win){ + const char *text; + GtkWidget *tab,*view; + GtkTextBuffer *buffer; + GtkTextIter start,march_start,march_end; + + //text=gtk_entry_get_text(entry); + text=gtk_editable_get_text(GTK_EDITABLE(entry)); + if(text[0]=='\0'){ + return ; + } + + tab=gtk_stack_get_visible_child(GTK_STACK(win->stack)); + view=gtk_scrolled_window_get_child(GTK_SCROLLED_WINDOW(tab)); + buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(view)); + + /* Very simple-minded search implementation */ + gtk_text_buffer_get_start_iter(buffer,&start); + if(gtk_text_iter_forward_search(&start,text,GTK_TEXT_SEARCH_VISIBLE_ONLY + ,&march_start,&march_end,NULL)) + { + gtk_text_buffer_select_range(buffer,&march_start,&march_end); + gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(view),&start,0.0,FALSE,0.0,0.0); + } +} + +static void visible_child_changed(GObject *stack,GParamSpec *pspec,MyWindow *win){ + gtk_search_bar_set_search_mode(GTK_SEARCH_BAR(win->searchbar),FALSE); +} + +static void update_lines(MyWindow *window){ + GtkWidget *tab,*view; + GtkTextBuffer * buffer; + int count; + char *lines_str; + + tab=gtk_stack_get_visible_child(GTK_STACK(window->stack)); + + if(tab == NULL){return ;} + + view=gtk_scrolled_window_get_child(GTK_SCROLLED_WINDOW(tab)); + buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(view)); + + count=gtk_text_buffer_get_line_count(buffer); + lines_str=g_strdup_printf("%d",count); + + gtk_label_set_label(GTK_LABEL(window->lines),lines_str); + g_free(lines_str); +} + +static void open_dialog(GSimpleAction *action, + GVariant *parmeter, + gpointer data) //Open the file dialog +{ + my_dialog_new(MY_WINDOW(data),"Open a text file",GTK_FILE_CHOOSER_ACTION_OPEN); +} + +static void preferences_activated(GSimpleAction *action, + GVariant *parmeter, + gpointer data) +{ + MyPrefs * prefs; + prefs=my_prefs_new(MY_WINDOW(data)); + gtk_window_present(GTK_WINDOW(prefs)); +} + +static void quit_activated(GSimpleAction *action, + GVariant *parmeter, + gpointer data) +{ + gtk_window_destroy(GTK_WINDOW(data)); +} + +static void my_window_init(MyWindow *window){ + //Ininalize window + gtk_widget_init_template(GTK_WIDGET(window)); + gtk_window_set_icon_name(GTK_WINDOW(window),"org.gtk.daleclack"); + + //Add Actions + static GActionEntry entries[]={ + {"open",open_dialog,NULL,NULL,NULL}, + {"preferences",preferences_activated,NULL,NULL,NULL}, + {"quit",quit_activated,NULL,NULL,NULL} + }; + g_action_map_add_action_entries(G_ACTION_MAP(window),entries,G_N_ELEMENTS(entries),window); + + //Add Menu + GtkBuilder * builder; + GMenuModel * model; + GAction * action; + + builder=gtk_builder_new_from_resource("/org/gtk/daleclack/app-menu.ui"); + model=G_MENU_MODEL(gtk_builder_get_object(builder,"appmenu")); + gtk_menu_button_set_menu_model(GTK_MENU_BUTTON(window->gears),model); + + //Bind Settings + window->settings=g_settings_new("org.gtk.daleclack"); + g_settings_bind (window->settings, "transition", + window->stack, "transition-type", + G_SETTINGS_BIND_DEFAULT); + + g_object_bind_property(window->search,"active", + window->searchbar,"search-mode-enabled", + G_BINDING_BIDIRECTIONAL); + + action=(GAction*)g_property_action_new("show-lines",window->lines,"visible"); + g_action_map_add_action(G_ACTION_MAP(window),action); + g_object_unref(action); + + g_object_bind_property(window->lines,"visible", + window->lines_label,"visible", + G_BINDING_DEFAULT); +} + +static void my_window_dispose(GObject *object){ + + g_clear_object(&MY_WINDOW(object)->settings); + + G_OBJECT_CLASS(my_window_parent_class)->dispose(object); +} + +static void my_window_class_init(MyWindowClass *winclass){ + gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(winclass), + "/org/gtk/daleclack/window.ui"); + + G_OBJECT_CLASS(winclass)->dispose=my_window_dispose; + + //Get Child widgets + gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(winclass),MyWindow,stack); + gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(winclass),MyWindow,gears); + gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(winclass),MyWindow,search); + gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(winclass),MyWindow,searchbar); + gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(winclass),MyWindow,lines); + gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(winclass),MyWindow,lines_label); + + gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(winclass),search_text_changed); + gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(winclass),visible_child_changed); +} + +MyWindow * my_window_new(MyApplication *app){ + return (MyWindow*)g_object_new(MY_WINDOW_TYPE,"application",app,NULL); +} + +void my_window_open(MyWindow *window,GFile *file){ + char *basename; + GtkWidget *scrolled, *view; + char *contents; + gsize length; + GtkTextBuffer * buffer; + GtkTextIter start,end; + GtkTextTag * tag; + + //Get basename and stack + basename=g_file_get_basename(file); + + scrolled=gtk_scrolled_window_new(); + gtk_widget_show (scrolled); + gtk_widget_set_hexpand (scrolled, TRUE); + gtk_widget_set_vexpand (scrolled, TRUE); + view = gtk_text_view_new (); + gtk_text_view_set_editable (GTK_TEXT_VIEW (view), FALSE); + gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (view), FALSE); + gtk_widget_show (view); + gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scrolled),view); + gtk_stack_add_titled (GTK_STACK (window->stack), scrolled, basename, basename); + + if (g_file_load_contents (file, NULL, &contents, &length, NULL, NULL)) + { + buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)); + gtk_text_buffer_set_text (buffer, contents, length); + g_free (contents); + } + + //Apply Tag + tag=gtk_text_buffer_create_tag(buffer,NULL,NULL); + g_settings_bind(window->settings,"font",tag,"font",G_SETTINGS_BIND_DEFAULT); + gtk_text_buffer_get_start_iter(buffer,&start); + gtk_text_buffer_get_end_iter(buffer,&end); + gtk_text_buffer_apply_tag(buffer,tag,&start,&end); + + //Enable Search Button + gtk_widget_set_sensitive(window->search,TRUE); + + update_lines(window); + g_free (basename); +} diff --git a/Gtk4/gtk107_text_gtk4/src/mywindow.h b/Gtk4/gtk107_text_gtk4/src/mywindow.h new file mode 100644 index 0000000..51de19f --- /dev/null +++ b/Gtk4/gtk107_text_gtk4/src/mywindow.h @@ -0,0 +1,10 @@ +#pragma once + +#include "myapplication.h" + +#define MY_WINDOW_TYPE (my_window_get_type()) +G_DECLARE_FINAL_TYPE(MyWindow,my_window,MY,WINDOW,GtkApplicationWindow) + +MyWindow * my_window_new(MyApplication * app); + +void my_window_open(MyWindow *window,GFile *file);