Add gtk107

This commit is contained in:
daleclack 2021-09-05 14:19:08 +08:00
parent 253dfce105
commit 334fb7681f
16 changed files with 601 additions and 0 deletions

View File

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

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,38 @@
<?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>
<item>
<attribute name="label" translatable="yes">Save</attribute>
<attribute name="action">win.save</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Save As</attribute>
<attribute name="action">win.save-as</attribute>
</item>
</section>
<section>
<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,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">prefs.ui</file>
</gresource>
</gresources>

View File

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="MyPrefs" parent="GtkDialog">
<property name="title" translatable="yes">Preferences</property>
<property name="resizable">0</property>
<property name="modal">1</property>
<child internal-child="content_area">
<object class="GtkBox" id="content_area">
<child>
<object class="GtkGrid" id="grid">
<property name="margin-start">12</property>
<property name="margin-end">12</property>
<property name="margin-top">12</property>
<property name="margin-bottom">12</property>
<property name="row-spacing">12</property>
<property name="column-spacing">12</property>
<child>
<object class="GtkLabel" id="fontlabel">
<property name="label">_Font:</property>
<property name="use-underline">1</property>
<property name="mnemonic-widget">font</property>
<property name="xalign">1</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkFontButton" id="font">
<layout>
<property name="column">1</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="transitionlabel">
<property name="label">_Transition:</property>
<property name="use-underline">1</property>
<property name="mnemonic-widget">transition</property>
<property name="xalign">1</property>
<layout>
<property name="column">0</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkComboBoxText" id="transition">
<items>
<item translatable="yes" id="none">None</item>
<item translatable="yes" id="crossfade">Fade</item>
<item translatable="yes" id="slide-left-right">Slide</item>
</items>
<layout>
<property name="column">1</property>
<property name="row">1</property>
</layout>
</object>
</child>
</object>
</child>
</object>
</child>
</template>
</interface>

View File

@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="MyWindow" parent="GtkApplicationWindow">
<property name="title" translatable="yes">Example Application</property>
<property name="default-width">600</property>
<property name="default-height">400</property>
<child type="titlebar">
<object class="GtkHeaderBar" id="header">
<child>
<object class="GtkLabel" id="lines_label">
<property name="visible">0</property>
<property name="label">Lines:</property>
</object>
</child>
<child>
<object class="GtkLabel" id="lines">
<property name="visible">0</property>
</object>
</child>
<child type="title">
<object class="GtkStackSwitcher" id="tabs">
<property name="stack">stack</property>
</object>
</child>
<child type="end">
<object class="GtkToggleButton" id="search">
<property name="sensitive">0</property>
<property name="icon-name">edit-find-symbolic</property>
</object>
</child>
<child type="end">
<object class="GtkMenuButton" id="gears">
<property name="direction">none</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox" id="content_box">
<property name="orientation">vertical</property>
<child>
<object class="GtkSearchBar" id="searchbar">
<child>
<object class="GtkSearchEntry" id="searchentry">
<signal name="search-changed" handler="search_text_changed"/>
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox" id="hbox">
<child>
<object class="GtkRevealer" id="sidebar">
<property name="transition-type">slide-right</property>
<child>
<object class="GtkScrolledWindow" id="sidebar-sw">
<property name="hscrollbar-policy">never</property>
<child>
<object class="GtkListBox" id="words">
<property name="selection-mode">none</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkStack" id="stack">
<signal name="notify::visible-child" handler="visible_child_changed"/>
</object>
</child>
</object>
</child>
</object>
</child>
</template>
</interface>

View File

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

View File

@ -0,0 +1,5 @@
#pragma once
#include "mywindow.h"
void my_dialog_new(MyWindow *window,const char *title,GtkFileChooserAction action);

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

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

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