Add gtk105

This commit is contained in:
daleclack 2021-08-28 12:40:37 +08:00
parent a2ca39dddf
commit d6b621dde2
14 changed files with 881 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<schemalist>
<schema path="/org/gtk/daleclack/" id="org.gtk.daleclack">
<key name="font" type="s">
<default>'Monospace 12'</default>
<summary>Font</summary>
<description>The font to be used for content.</description>
</key>
<key name="transition" type="s">
<choices>
<choice value='none'/>
<choice value='crossfade'/>
<choice value='slide-left-right'/>
</choices>
<default>'none'</default>
<summary>Transition</summary>
<description>The transition to use when switching tabs.</description>
</key>
<key name="show-words" type="b">
<default>false</default>
<summary>Show words</summary>
<description>Whether to show a word list in the sidebar</description>
</key>
</schema>
</schemalist>

View File

@ -0,0 +1,28 @@
<?xml version="1.0"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<menu id="appmenu">
<section>
<item>
<attribute name="label" translatable="yes">_Words</attribute>
<attribute name="action">win.show-words</attribute>
</item>
<item>
<attribute name="label" translatable="yes">_Lines</attribute>
<attribute name="action">win.show-lines</attribute>
</item>
</section>
<section>
<item>
<attribute name="label" translatable="yes">_Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
<section>
<item>
<attribute name="label" translatable="yes">_Quit</attribute>
<attribute name="action">app.quit</attribute>
</item>
</section>
</menu>
</interface>

View File

@ -0,0 +1 @@
glib-compile-resources gtk105.gresource.xml --target=../src/resources.cpp --generate-source

View File

@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<template class="MyPrefs" parent="GtkDialog">
<property name="can-focus">False</property>
<property name="title" translatable="yes">Preferences</property>
<property name="resizable">False</property>
<property name="modal">True</property>
<property name="type-hint">normal</property>
<child internal-child="vbox">
<object class="GtkBox" id="vbox">
<property name="can-focus">False</property>
<child internal-child="action_area">
<object class="GtkButtonBox">
<property name="can-focus">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<!-- n-columns=3 n-rows=3 -->
<object class="GtkGrid" id="grid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="row-spacing">12</property>
<property name="column-spacing">6</property>
<child>
<object class="GtkLabel" id="fontlabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label">_Font:</property>
<property name="use-underline">True</property>
<property name="mnemonic-widget">font</property>
<property name="xalign">1</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkFontButton" id="font">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="receives-default">False</property>
<property name="font">Sans 12</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="transitionlabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label">_Transition:</property>
<property name="use-underline">True</property>
<property name="mnemonic-widget">transition</property>
<property name="xalign">1</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
<object class="GtkComboBoxText" id="transition">
<property name="visible">True</property>
<property name="can-focus">False</property>
<items>
<item id="none" translatable="yes">None</item>
<item id="crossfade" translatable="yes">Fade</item>
<item id="slide-left-right" translatable="yes">Slide</item>
</items>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</template>
</interface>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="org/gtk/daleclack">
<file preprocess="xml-stripblanks">window.ui</file>
<file preprocess="xml-stripblanks">app-menu.ui</file>
<file preprocess="xml-stripblanks">dialog.ui</file>
</gresource>
</gresources>

View File

@ -0,0 +1,154 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkImage" id="image1">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="icon-name">edit-find</property>
</object>
<template class="MyWindow" parent="GtkApplicationWindow">
<property name="can-focus">False</property>
<property name="default-width">600</property>
<property name="default-height">400</property>
<child>
<object class="GtkBox" id="content_box">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkSearchBar" id="searchbar">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkSearchEntry" id="searchentry">
<property name="visible">True</property>
<property name="can-focus">False</property>
<signal name="search-changed" handler="search_text_changed" swapped="no"/>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox" id="hbox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkRevealer" id="sidebar">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="transition-type">slide-right</property>
<child>
<object class="GtkScrolledWindow" id="sidebar-sw">
<property name="visible">True</property>
<property name="hscrollbar-policy">never</property>
<property name="vscrollbar-policy">automatic</property>
<child>
<object class="GtkListBox" id="words">
<property name="visible">True</property>
<property name="selection-mode">none</property>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkStack" id="stack">
<property name="visible">True</property>
<property name="can-focus">False</property>
<signal name="notify::visible-child" handler="visible_child_changed" swapped="no"/>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
<child type="titlebar">
<object class="GtkHeaderBar" id="header">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="show-close-button">True</property>
<child type="title">
<object class="GtkStackSwitcher" id="tabs">
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
</child>
<child>
<object class="GtkMenuButton" id="gears">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="focus-on-click">False</property>
<property name="receives-default">True</property>
<property name="direction">up</property>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="icon-name">open-menu-symbolic</property>
</object>
</child>
</object>
<packing>
<property name="pack-type">end</property>
</packing>
</child>
<child>
<object class="GtkToggleButton" id="search">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image">image1</property>
</object>
<packing>
<property name="pack-type">end</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="lines_label">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Lines:</property>
</object>
<packing>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="lines">
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
<packing>
<property name="position">3</property>
</packing>
</child>
</object>
</child>
</template>
</interface>

View File

@ -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);
}

View File

@ -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;i<n_files;i++){
my_window_open(win,files[i]);
}
//Show the window
gtk_window_present(GTK_WINDOW(win));
}
static void preference_activated(GSimpleAction *action,
GVariant *parameter,
gpointer app)
{
//g_print("Preference Activated\n");
MyPrefs * prefs;
GtkWindow * win;
win=gtk_application_get_active_window(GTK_APPLICATION(app));
prefs=my_prefs_new(MY_WINDOW(win));
gtk_window_present(GTK_WINDOW(prefs));
}
static void quit_activated(GSimpleAction *action,
GVariant *parameter,
gpointer app)
{
g_application_quit(G_APPLICATION(app));
}
static GActionEntry entries[]={
{"preferences",preference_activated,NULL,NULL,NULL},
{"quit",quit_activated,NULL,NULL,NULL}
};
static void my_app_startup(GApplication *app){
const gchar * quit_accels[2]={"<Ctrl>Q",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);
}

View File

@ -0,0 +1,11 @@
#ifndef __MYAPP_H_
#define __MYAPP_H_
#include <gtk/gtk.h>
#define MY_APP_TYPE (my_app_get_type())
G_DECLARE_FINAL_TYPE(MyApp,my_app,MY,APP,GtkApplication)
MyApp * my_app_new(void);
#endif

View File

@ -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);
}

View File

@ -0,0 +1,12 @@
#ifndef __MYPREFS_H_
#define __MYPREFS_H_
#include <gtk/gtk.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

View File

@ -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);
}

View File

@ -0,0 +1,14 @@
#ifndef __MYWINDOW_H_
#define __MYWINDOW_H_
#include <gtk/gtk.h>
#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