Add gtk106

This commit is contained in:
daleclack 2021-09-03 12:56:59 +08:00
parent be0eb21590
commit 253dfce105
16 changed files with 691 additions and 0 deletions

View File

@ -0,0 +1,35 @@
#A Simple Project Test
project('gtk106', 'cpp',
default_options : ['c_std=c17', 'cpp_std=c++17'])
#Initalize variants
gnome=import('gnome')
#Compile Resource
gresources = gnome.compile_resources(
'resources', 'res/gtk106.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/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('gtk106', icon_res, src, gresources, app_schemas, dependencies : gtkdep,
win_subsystem : 'windows', include_directories : dir_include)
else
executable('gtk106', 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,34 @@
<?xml version="1.0"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<menu id="appmenu">
<section>
<item>
<attribute name="label" translatable="yes">_Open File</attribute>
<attribute name="action">win.open</attribute>
</item>
</section>
<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">win.preferences</attribute>
</item>
</section>
<section>
<item>
<attribute name="label" translatable="yes">_Quit</attribute>
<attribute name="action">win.quit</attribute>
</item>
</section>
</menu>
</interface>

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,133 @@
<?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">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="win_vbox">
<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">True</property>
<property name="primary-icon-name">edit-find-symbolic</property>
<property name="primary-icon-activatable">False</property>
<property name="primary-icon-sensitive">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="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>
</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>
<property name="stack">stack</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>
<child>
<object class="GtkImage" id="gears_image">
<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="GtkButton" id="btn_open">
<property name="label" translatable="yes">Open</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="action-name">win.open</property>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkSeparator" id="separator">
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
<packing>
<property name="position">2</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">3</property>
</packing>
</child>
<child>
<object class="GtkSeparator">
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
<packing>
<property name="pack-type">end</property>
<property name="position">4</property>
</packing>
</child>
</object>
</child>
</template>
</interface>

View File

@ -0,0 +1,32 @@
#include "MyDialog.h"
#include "../text_types.h"
struct _MyDialog{
GtkFileChooserDialog parent;
};
G_DEFINE_TYPE(MyDialog,my_dialog,GTK_TYPE_FILE_CHOOSER_DIALOG)
static void my_dialog_response(GtkDialog *dialog,int response_id){
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));
my_window_open(MY_WINDOW(win),file);
g_object_unref(file);
}
gtk_widget_destroy(GTK_WIDGET(dialog));
}
static void my_dialog_init(MyDialog * dialog){
//Initalize window
gtk_window_set_title(GTK_WINDOW(dialog),"Open a text file");
gtk_dialog_add_buttons(GTK_DIALOG(dialog),"OK",GTK_RESPONSE_OK,"Cancel",GTK_RESPONSE_CANCEL,NULL);
}
static void my_dialog_class_init(MyDialogClass *dlgclass){
GTK_DIALOG_CLASS(dlgclass)->response=my_dialog_response;
}
MyDialog * my_dialog_new(MyWindow * window){
return (MyDialog*)g_object_new(MY_DIALOG_TYPE,"transient-for",window,NULL);
}

View File

@ -0,0 +1,8 @@
#pragma once
#include "mywindow.h"
#define MY_DIALOG_TYPE (my_dialog_get_type())
G_DECLARE_FINAL_TYPE(MyDialog,my_dialog,MY,DIALOG,GtkFileChooserDialog)
MyDialog * my_dialog_new(MyWindow *window);

Binary file not shown.

View File

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

View File

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

View File

@ -0,0 +1,8 @@
#pragma once
#include <gtk/gtk.h>
#define MY_APPLICATION_TYPE (my_application_get_type())
G_DECLARE_FINAL_TYPE(MyApplication,my_application,MY,APPLICATION,GtkApplication)
MyApplication * my_application_new();

View File

@ -0,0 +1,57 @@
#include "myapplication.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,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

View File

@ -0,0 +1,183 @@
#include "mywindow.h"
#include "MyDialog.h"
#include "myprefs.h"
struct _MyWindow{
GtkApplicationWindow parent;
};
typedef struct _MyWindowPrivate MyWindowPrivate;
struct _MyWindowPrivate{
GtkWidget * stack;
GtkWidget * gears;
GSettings * settings;
GtkWidget * search;
GtkWidget * searchbar;
};
G_DEFINE_TYPE_WITH_PRIVATE(MyWindow,my_window,GTK_TYPE_APPLICATION_WINDOW)
static void search_text_changed(GtkEntry *entry){
MyWindow *win;
MyWindowPrivate *priv;
const char *text;
GtkWidget *tab,*view;
GtkTextBuffer *buffer;
GtkTextIter start,march_start,march_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_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;
MyWindowPrivate *priv;
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);
}
static void open_dialog(GSimpleAction *action,
GVariant *parmeter,
gpointer data) //Open the file dialog
{
MyDialog *dialog;
dialog=my_dialog_new(MY_WINDOW(data));
gtk_window_present(GTK_WINDOW(dialog));
}
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 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}
};
g_action_map_add_action_entries(G_ACTION_MAP(window),entries,G_N_ELEMENTS(entries),window);
//Get Private widgets
MyWindowPrivate *priv;
priv=(MyWindowPrivate*)my_window_get_instance_private(window);
//Add Menu
GtkBuilder * builder;
GMenuModel * model;
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(priv->gears),model);
//Bind Settings
priv->settings=g_settings_new("org.gtk.daleclack");
g_settings_bind (priv->settings, "transition",
priv->stack, "transition-type",
G_SETTINGS_BIND_DEFAULT);
g_object_bind_property(priv->search,"active",
priv->searchbar,"search-mode-enabled",
G_BINDING_BIDIRECTIONAL);
}
static void my_window_dispose(GObject *object){
MyWindowPrivate *priv;
priv=(MyWindowPrivate*)my_window_get_instance_private(MY_WINDOW(object));
g_clear_object(&priv->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_private(GTK_WIDGET_CLASS(winclass),MyWindow,stack);
gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(winclass),MyWindow,gears);
gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(winclass),MyWindow,search);
gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS(winclass),MyWindow,searchbar);
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){
MyWindowPrivate *priv;
char *basename;
GtkWidget *scrolled, *view;
char *contents;
gsize length;
GtkTextBuffer * buffer;
GtkTextIter start,end;
GtkTextTag * tag;
//Get basename and stack
priv=(MyWindowPrivate*)my_window_get_instance_private(window);
basename=g_file_get_basename(file);
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);
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(priv->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);
g_free (basename);
}

View File

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