diff --git a/Gtk3/gtk105_text/meson.build b/Gtk3/gtk105_text/meson.build new file mode 100644 index 0000000..42c4fae --- /dev/null +++ b/Gtk3/gtk105_text/meson.build @@ -0,0 +1,35 @@ +#A Simple Project Test +project('gtk105', 'cpp', + default_options : ['c_std=c17', 'cpp_std=c++17']) + +#Initalize variants +gnome=import('gnome') + +#Compile Resource +gresources = gnome.compile_resources( + 'resources', 'res/gtk105.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('gtk+-3.0') + +#Additional include dirs +dir_include = include_directories('..') + +#source files +src = ['src/main.cpp', 'src/myapp.cpp', 'src/mywindow.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('gtk105', icon_res, src, gresources, app_schemas, dependencies : gtkdep, + win_subsystem : 'windows', include_directories : dir_include) +else + executable('gtk105', src, gresources, app_schemas, dependencies : gtkdep, include_directories : dir_include) +endif diff --git a/Gtk3/gtk105_text/org.gtk.daleclack.gschema.xml b/Gtk3/gtk105_text/org.gtk.daleclack.gschema.xml new file mode 100644 index 0000000..83749ff --- /dev/null +++ b/Gtk3/gtk105_text/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/Gtk3/gtk105_text/res/app-menu.ui b/Gtk3/gtk105_text/res/app-menu.ui new file mode 100644 index 0000000..4b3fa39 --- /dev/null +++ b/Gtk3/gtk105_text/res/app-menu.ui @@ -0,0 +1,28 @@ + + + + +
+ + _Words + win.show-words + + + _Lines + win.show-lines + +
+
+ + _Preferences + app.preferences + +
+
+ + _Quit + app.quit + +
+
+
diff --git a/Gtk3/gtk105_text/res/compile-resource.sh b/Gtk3/gtk105_text/res/compile-resource.sh new file mode 100644 index 0000000..b65b4a7 --- /dev/null +++ b/Gtk3/gtk105_text/res/compile-resource.sh @@ -0,0 +1 @@ + glib-compile-resources gtk105.gresource.xml --target=../src/resources.cpp --generate-source diff --git a/Gtk3/gtk105_text/res/dialog.ui b/Gtk3/gtk105_text/res/dialog.ui new file mode 100644 index 0000000..d6dc721 --- /dev/null +++ b/Gtk3/gtk105_text/res/dialog.ui @@ -0,0 +1,111 @@ + + + + + + diff --git a/Gtk3/gtk105_text/res/gtk105.gresource.xml b/Gtk3/gtk105_text/res/gtk105.gresource.xml new file mode 100644 index 0000000..c976477 --- /dev/null +++ b/Gtk3/gtk105_text/res/gtk105.gresource.xml @@ -0,0 +1,8 @@ + + + + window.ui + app-menu.ui + dialog.ui + + diff --git a/Gtk3/gtk105_text/res/window.ui b/Gtk3/gtk105_text/res/window.ui new file mode 100644 index 0000000..a801382 --- /dev/null +++ b/Gtk3/gtk105_text/res/window.ui @@ -0,0 +1,154 @@ + + + + + + True + False + edit-find + + + diff --git a/Gtk3/gtk105_text/src/main.cpp b/Gtk3/gtk105_text/src/main.cpp new file mode 100644 index 0000000..b31d677 --- /dev/null +++ b/Gtk3/gtk105_text/src/main.cpp @@ -0,0 +1,13 @@ +#include "myapp.h" + +int main(int argc,char **argv){ + + /* Since this example is running uninstalled, + * we have to help it find its schema. This + * is *not* necessary in properly installed + * application. + */ + g_setenv ("GSETTINGS_SCHEMA_DIR", ".", FALSE); + + return g_application_run(G_APPLICATION(my_app_new()),argc,argv); +} diff --git a/Gtk3/gtk105_text/src/myapp.cpp b/Gtk3/gtk105_text/src/myapp.cpp new file mode 100644 index 0000000..658602d --- /dev/null +++ b/Gtk3/gtk105_text/src/myapp.cpp @@ -0,0 +1,94 @@ +#include "myapp.h" +#include "mywindow.h" +#include "myprefs.h" + +struct _MyApp{ + GtkApplication parent; +}; + +G_DEFINE_TYPE(MyApp,my_app,GTK_TYPE_APPLICATION) + +static void my_app_activate(GApplication *app){ + //When Application activate,create a window + MyWindow *win; + win=my_window_new(MY_APP(app)); + gtk_window_present(GTK_WINDOW(win)); +} + +static void my_app_open(GApplication *app, + GFile **files, + gint n_files, + const gchar *hint) +{ + GList *windows; + MyWindow *win; + + //Get Windows for application + windows=gtk_application_get_windows(GTK_APPLICATION(app)); + if(windows){ + win=MY_WINDOW(windows->data); + }else{ + win=my_window_new(MY_APP(app)); + } + + for(int i=0;iQ",NULL}; + + //Start The Application + G_APPLICATION_CLASS(my_app_parent_class)->startup(app); + + g_action_map_add_action_entries(G_ACTION_MAP(app),entries, + G_N_ELEMENTS(entries),app); + gtk_application_set_accels_for_action(GTK_APPLICATION(app),"app.quit",quit_accels); +} + +static void my_app_init(MyApp *app){ +} + +static void my_app_class_init(MyAppClass *class1){ + G_APPLICATION_CLASS(class1)->activate=my_app_activate; + G_APPLICATION_CLASS(class1)->open=my_app_open; + G_APPLICATION_CLASS(class1)->startup=my_app_startup; +} + +MyApp * my_app_new(){ + return (MyApp*)g_object_new(MY_APP_TYPE, + "application-id","org.gtk.daleclack", + "flags",G_APPLICATION_HANDLES_OPEN, + NULL); +} + diff --git a/Gtk3/gtk105_text/src/myapp.h b/Gtk3/gtk105_text/src/myapp.h new file mode 100644 index 0000000..1e0f831 --- /dev/null +++ b/Gtk3/gtk105_text/src/myapp.h @@ -0,0 +1,11 @@ +#ifndef __MYAPP_H_ +#define __MYAPP_H_ + +#include + +#define MY_APP_TYPE (my_app_get_type()) +G_DECLARE_FINAL_TYPE(MyApp,my_app,MY,APP,GtkApplication) + +MyApp * my_app_new(void); + +#endif diff --git a/Gtk3/gtk105_text/src/myprefs.cpp b/Gtk3/gtk105_text/src/myprefs.cpp new file mode 100644 index 0000000..ce7c4e2 --- /dev/null +++ b/Gtk3/gtk105_text/src/myprefs.cpp @@ -0,0 +1,57 @@ +#include "myapp.h" +#include "myprefs.h" + +struct _MyPrefs{ + GtkDialog parent; +}; + +typedef struct _MyPrefsPrivate MyPrefsPrivate; + +struct _MyPrefsPrivate{ + GSettings *settings; + GtkWidget *font; + GtkWidget *transition; +}; + +G_DEFINE_TYPE_WITH_PRIVATE(MyPrefs,my_prefs,GTK_TYPE_DIALOG) + +static void my_prefs_init(MyPrefs *dialog){ + MyPrefsPrivate * priv; + + priv = (MyPrefsPrivate*)my_prefs_get_instance_private(MY_PREFS(dialog)); + gtk_widget_init_template(GTK_WIDGET(dialog)); + priv->settings=g_settings_new("org.gtk.daleclack"); + + //Bind Proprties + g_settings_bind(priv->settings,"font", + priv->font,"font", + G_SETTINGS_BIND_DEFAULT); + + g_settings_bind(priv->settings,"transition", + priv->transition,"active-id", + G_SETTINGS_BIND_DEFAULT); +} + +static void my_prefs_dispose(GObject *object){ + //Free Memory + MyPrefsPrivate * priv; + + priv = (MyPrefsPrivate*)my_prefs_get_instance_private(MY_PREFS(object)); + g_clear_object(&priv->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/dialog.ui"); + gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(dlgclass),MyPrefs,font); + gtk_widget_class_bind_template_child_private(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/Gtk3/gtk105_text/src/myprefs.h b/Gtk3/gtk105_text/src/myprefs.h new file mode 100644 index 0000000..ef36ced --- /dev/null +++ b/Gtk3/gtk105_text/src/myprefs.h @@ -0,0 +1,12 @@ +#ifndef __MYPREFS_H_ +#define __MYPREFS_H_ + +#include +#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/Gtk3/gtk105_text/src/mywindow.cpp b/Gtk3/gtk105_text/src/mywindow.cpp new file mode 100644 index 0000000..26c35c9 --- /dev/null +++ b/Gtk3/gtk105_text/src/mywindow.cpp @@ -0,0 +1,318 @@ +#include "mywindow.h" + +struct _MyWindow{ + GtkApplicationWindow parent; +}; + +typedef struct _MyWindowPrivate MyWindowPrivate; + +struct _MyWindowPrivate{ + GSettings *settings; + GtkWidget *stack; + GtkWidget *search; + GtkWidget *searchbar; + GtkWidget *searchentry; + GtkWidget *gears; + GtkWidget *sidebar; + GtkWidget *words; + GtkWidget *lines; + GtkWidget *lines_label; +}; + +G_DEFINE_TYPE_WITH_PRIVATE(MyWindow,my_window,GTK_TYPE_APPLICATION_WINDOW) + +static void search_text_changed(GtkEntry *entry){ + MyWindow *win; + MyWindowPrivate *priv; + const gchar *text; + GtkWidget *tab; + GtkWidget *view; + GtkTextBuffer *buffer; + GtkTextIter start, match_start, match_end; + + text = gtk_entry_get_text (entry); + + if (text[0] == '\0') + return; + + win = MY_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (entry))); + priv = (MyWindowPrivate*)my_window_get_instance_private (win); + + tab = gtk_stack_get_visible_child (GTK_STACK (priv->stack)); + view = gtk_bin_get_child (GTK_BIN (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_CASE_INSENSITIVE, + &match_start, &match_end, NULL)) + { + gtk_text_buffer_select_range (buffer, &match_start, &match_end); + gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (view), &match_start, + 0.0, FALSE, 0.0, 0.0); + } + +} + +static void find_word(GtkButton *button,MyWindow *win) +{ + MyWindowPrivate *priv; + const gchar *word; + + priv = (MyWindowPrivate*)my_window_get_instance_private (win); + + word = gtk_button_get_label (button); + gtk_entry_set_text (GTK_ENTRY (priv->searchentry), word); +} + +static void update_words(MyWindow *win){ + MyWindowPrivate *priv; + GHashTable *strings; + GHashTableIter iter; + GtkWidget *tab, *view, *row; + GtkTextBuffer *buffer; + GtkTextIter start, end; + GList *children, *l; + gchar *word, *key; + + priv = (MyWindowPrivate*)my_window_get_instance_private (win); + + tab = gtk_stack_get_visible_child (GTK_STACK (priv->stack)); + + if (tab == NULL) + return; + + view = gtk_bin_get_child (GTK_BIN (tab)); + buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)); + + strings = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); + + gtk_text_buffer_get_start_iter (buffer, &start); + while (!gtk_text_iter_is_end (&start)) + { + while (!gtk_text_iter_starts_word (&start)) + { + if (!gtk_text_iter_forward_char (&start)) + goto done; + } + end = start; + if (!gtk_text_iter_forward_word_end (&end)) + goto done; + word = gtk_text_buffer_get_text (buffer, &start, &end, FALSE); + g_hash_table_add (strings, g_utf8_strdown (word, -1)); + g_free (word); + start = end; + } + +done: + children = gtk_container_get_children (GTK_CONTAINER (priv->words)); + for (l = children; l; l = l->next) + gtk_container_remove (GTK_CONTAINER (priv->words), GTK_WIDGET (l->data)); + g_list_free (children); + + g_hash_table_iter_init (&iter, strings); + while (g_hash_table_iter_next (&iter, (gpointer *)&key, NULL)) + { + row = gtk_button_new_with_label (key); + g_signal_connect (row, "clicked", + G_CALLBACK (find_word), win); + gtk_widget_show (row); + gtk_container_add (GTK_CONTAINER (priv->words), row); + } + + g_hash_table_unref (strings); +} + +static void update_lines(MyWindow *win){ + MyWindowPrivate *priv; + GtkWidget *tab,*view; + GtkTextBuffer *buffer; + GtkTextIter iter; + int count; + gchar *lines; + + //Get Private member + priv=(MyWindowPrivate*)my_window_get_instance_private(win); + + tab = gtk_stack_get_visible_child (GTK_STACK (priv->stack)); + + if(tab==NULL){ + return ; + } + + view = gtk_bin_get_child (GTK_BIN (tab)); + buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)); + + count=0; + + gtk_text_buffer_get_start_iter (buffer, &iter); + while (!gtk_text_iter_is_end (&iter)) + { + count++; + if (!gtk_text_iter_forward_line (&iter)) + break; + } + + lines=g_strdup_printf("%d",count); + gtk_label_set_label(GTK_LABEL(priv->lines),lines); + g_free(lines); +} + + +static void visible_child_changed(GObject *stack,GParamSpec *pspec){ + MyWindow *win; + MyWindowPrivate *priv; + + if(gtk_widget_in_destruction(GTK_WIDGET(stack))){ + return ; + } + + win=MY_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(stack))); + priv=(MyWindowPrivate*)my_window_get_instance_private(win); + gtk_search_bar_set_search_mode (GTK_SEARCH_BAR (priv->searchbar), FALSE); + update_words(win); + update_lines(win); +} + +static void word_changed(GObject *sidebar,GParamSpec *pspec,MyWindow *win){ + update_words(win); +} + +static void my_window_init(MyWindow *window){ + //Inintalize Window + GtkBuilder *builder; + GMenuModel *app_menu; + MyWindowPrivate * priv; + GAction *action; + + priv=(MyWindowPrivate*)my_window_get_instance_private(window); + gtk_widget_init_template(GTK_WIDGET(window)); + + //Add Menu + builder=gtk_builder_new_from_resource("/org/gtk/daleclack/app-menu.ui"); + app_menu=G_MENU_MODEL(gtk_builder_get_object(builder,"appmenu")); + gtk_menu_button_set_menu_model(GTK_MENU_BUTTON(priv->gears),app_menu); + g_object_unref(builder); + + //Bind Settings + priv->settings=g_settings_new("org.gtk.daleclack"); + + g_settings_bind (priv->settings, "transition", + priv->stack, "transition-type", + G_SETTINGS_BIND_DEFAULT); + + //Bind Search Button and searchbar + g_object_bind_property(priv->search, "active", + priv->searchbar, "search-mode-enabled", + G_BINDING_BIDIRECTIONAL); + + g_settings_bind (priv->settings, "show-words", + priv->sidebar, "reveal-child", + G_SETTINGS_BIND_DEFAULT); + + action = (GAction*) g_property_action_new ("show-lines", priv->lines, "visible"); + g_action_map_add_action (G_ACTION_MAP (window), action); + g_object_unref (action); + + g_object_bind_property (priv->lines, "visible", + priv->lines_label, "visible", + G_BINDING_DEFAULT); + + g_signal_connect(priv->sidebar,"notify::reveal-child", + G_CALLBACK(word_changed),window); + + action = g_settings_create_action (priv->settings, "show-words"); + g_action_map_add_action (G_ACTION_MAP (window), action); + g_object_unref (action); + + g_object_bind_property (priv->lines, "visible", + priv->lines_label, "visible", + G_BINDING_DEFAULT); +} + +static void my_window_dispose (GObject *object) +{ + //Free Memory for settings + MyWindowPrivate *priv; + MyWindow *win; + + win=MY_WINDOW(object); + priv = (MyWindowPrivate*)my_window_get_instance_private(win); + g_clear_object (&priv->settings); + + G_OBJECT_CLASS (my_window_parent_class)->dispose (object); +} + + +static void my_window_class_init(MyWindowClass *class1){ + //Inintalize Window + G_OBJECT_CLASS (class1)->dispose = my_window_dispose; + + + gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(class1), + "/org/gtk/daleclack/window.ui"); + gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(class1),MyWindow,stack); + gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(class1),MyWindow,search); + gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(class1),MyWindow,searchbar); + gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(class1),MyWindow,searchentry); + gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(class1),MyWindow,gears); + gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(class1),MyWindow,sidebar); + gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(class1),MyWindow,words); + gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(class1),MyWindow,lines); + gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(class1),MyWindow,lines_label); + + gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class1), search_text_changed); + gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class1), visible_child_changed); +} + +MyWindow * my_window_new(MyApp *app){ + return (MyWindow*)g_object_new(MY_WINDOW_TYPE,"application",app,NULL); +} + +void my_window_open(MyWindow *window,GFile *file){ + MyWindowPrivate * priv; + gchar * basename, * contents; + GtkWidget * scrolled, * view; + gsize length; + GtkTextBuffer *buffer; + GtkTextTag *tag; + GtkTextIter start_iter,end_iter; + + //Get Stack and basename of file + priv=(MyWindowPrivate*)my_window_get_instance_private(window); + basename=g_file_get_basename(file); + + //Create Text View + scrolled = gtk_scrolled_window_new (NULL, NULL); + 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_container_add (GTK_CONTAINER (scrolled), view); + gtk_stack_add_titled (GTK_STACK (priv->stack), scrolled, basename, basename); + + //buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(view)); + + //Load Contents and get buffer + 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); + } + + //Text Tag + tag=gtk_text_buffer_create_tag(buffer,NULL,NULL); + g_settings_bind(priv->settings,"font",tag,"font",G_SETTINGS_BIND_DEFAULT); + gtk_text_buffer_get_start_iter(buffer,&start_iter); + gtk_text_buffer_get_end_iter(buffer,&end_iter); + gtk_text_buffer_apply_tag(buffer,tag,&start_iter,&end_iter); + + g_free (basename); + gtk_widget_set_sensitive(priv->search,TRUE); + update_words (window); + update_lines (window); +} diff --git a/Gtk3/gtk105_text/src/mywindow.h b/Gtk3/gtk105_text/src/mywindow.h new file mode 100644 index 0000000..6cf3194 --- /dev/null +++ b/Gtk3/gtk105_text/src/mywindow.h @@ -0,0 +1,14 @@ +#ifndef __MYWINDOW_H_ +#define __MYWINDOW_H_ + +#include +#include "myapp.h" + +#define MY_WINDOW_TYPE (my_window_get_type()) +G_DECLARE_FINAL_TYPE(MyWindow,my_window,MY,WINDOW,GtkApplicationWindow) + +MyWindow * my_window_new(MyApp *app); + +void my_window_open(MyWindow *window,GFile *file); + +#endif