Skip to content

Commit

Permalink
Add/implement word completion with gtk_source_completion and add conf…
Browse files Browse the repository at this point in the history
…igurable setting to enable/disable word completion in settings->file load/save->word completion. Protect includes to insure building without gtksourceview.
  • Loading branch information
David C. Rankin committed Jul 28, 2018
1 parent c35211d commit c3f9634
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 1 deletion.
8 changes: 8 additions & 0 deletions gtk_appdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ g_print ("app->exename : %s\n"
app->langname = NULL; /* language name (owned by mgr, do not free) */
// app->schememgr = gtk_source_style_scheme_manager_get_default();
// app->schemeids = gtk_source_style_scheme_manager_get_scheme_ids (app->schememgr);
app->completion = NULL; /* completion provider object */
app->enablecmplt = TRUE; /* enable word completion */
app->highlight = TRUE; /* show syntax highlight */
app->showmargin = TRUE; /* show margin at specific column */
app->marginwidth = 80; /* initial right margin to display */
Expand Down Expand Up @@ -318,6 +320,10 @@ static void context_read_keyfile (kwinst *app)
"highlight", &err);
if (chk_key_ok (&err)) app->highlight = bv;

bv = g_key_file_get_boolean (app->keyfile, "sourceview",
"enablecmplt", &err);
if (chk_key_ok (&err)) app->enablecmplt = bv;

bv = g_key_file_get_boolean (app->keyfile, "sourceview",
"lineno", &err);
if (chk_key_ok (&err)) app->lineno = bv;
Expand Down Expand Up @@ -430,6 +436,8 @@ static void context_write_keyfile (kwinst *app)
g_key_file_set_boolean (app->keyfile, "cleanup", "trimendws", app->trimendws);
#ifdef HAVESOURCEVIEW
g_key_file_set_boolean (app->keyfile, "sourceview", "highlight", app->highlight);
g_key_file_set_boolean (app->keyfile, "sourceview", "enablecmplt",
app->enablecmplt);
g_key_file_set_boolean (app->keyfile, "sourceview", "lineno", app->lineno);
g_key_file_set_boolean (app->keyfile, "sourceview", "linehghlt", app->linehghlt);
g_key_file_set_boolean (app->keyfile, "sourceview", "showmargin", app->showmargin);
Expand Down
4 changes: 3 additions & 1 deletion gtk_appdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#define HAVEMSWIN 1
#endif

#define VER "0.2.1"
#define VER "0.2.2"
#define SITE "https://www.rankinlawfirm.com"
#define LICENSE "gpl-2.0.txt"
#define CFGDIR "gtkwrite"
Expand Down Expand Up @@ -141,6 +141,8 @@ typedef struct {
GtkSourceBuffer *buffer;
GtkSourceLanguageManager *langmgr;
GtkSourceLanguage *language;
GtkSourceCompletion *completion;
gboolean enablecmplt; /* enable word-completion */
const gchar *langname; /* name of language from sourceview guess */
// GtkSourceStyleSchemeManager *schememgr;
// const gchar * const *schemeids;
Expand Down
99 changes: 99 additions & 0 deletions gtk_completionsv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
static const int stub;

#if defined (WGTKSOURCEVIEW2) || defined (WGTKSOURCEVIEW3) || defined (WGTKSOURCEVIEW4)

#include "gtk_completionsv.h"

typedef struct _cmplprovider cmplprovider;
typedef struct _cmplproviderClass cmplproviderClass;

struct _cmplprovider
{
GObject parent;

GList *proposals;
gint priority;
gchar *name;

GdkPixbuf *icon;
};

struct _cmplproviderClass
{
GObjectClass parent_class;
};

static void cmpl_provider_iface_init (GtkSourceCompletionProviderIface *iface);

G_DEFINE_TYPE_WITH_CODE (cmplprovider, cmpl_provider, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (GTK_TYPE_SOURCE_COMPLETION_PROVIDER,
cmpl_provider_iface_init))

static gchar *cmpl_provider_get_name (GtkSourceCompletionProvider *provider)
{
return g_strdup (((cmplprovider *)provider)->name);
}

static gint cmpl_provider_get_priority (GtkSourceCompletionProvider *provider)
{
return ((cmplprovider *)provider)->priority;
}

static gboolean cmpl_provider_match (GtkSourceCompletionProvider *provider,
GtkSourceCompletionContext *context)
{
return TRUE;

if (provider || context) {}
}

static void cmpl_provider_populate (GtkSourceCompletionProvider *provider,
GtkSourceCompletionContext *context)
{
gtk_source_completion_context_add_proposals (context, provider,
((cmplprovider *)provider)->proposals,
TRUE);
}

static void cmpl_provider_iface_init (GtkSourceCompletionProviderIface *iface)
{
iface->get_name = cmpl_provider_get_name;

iface->populate = cmpl_provider_populate;
iface->match = cmpl_provider_match;
iface->get_priority = cmpl_provider_get_priority;

// iface->get_icon = cmpl_provider_get_icon;
iface->get_icon = NULL;
}

static void cmpl_provider_class_init (cmplproviderClass *klass)
{
if (klass) {}
}

static void cmpl_provider_init (cmplprovider *self)
{
GList *proposals = NULL;
self->proposals = proposals;
}

void create_completion (kwinst *app)
{
GtkSourceCompletionWords *prov_words;

app->completion = gtk_source_view_get_completion (GTK_SOURCE_VIEW (app->view));

prov_words = gtk_source_completion_words_new (NULL, NULL);

gtk_source_completion_words_register (prov_words,
gtk_text_view_get_buffer (GTK_TEXT_VIEW (app->view)));

gtk_source_completion_add_provider (app->completion,
GTK_SOURCE_COMPLETION_PROVIDER (prov_words),
NULL);

// g_object_set (prov_words, "priority", 10, NULL);
}

#endif
19 changes: 19 additions & 0 deletions gtk_completionsv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef __completionsv_h__
#define __completionsv_h__ 1

#if defined (WGTKSOURCEVIEW2) || defined (WGTKSOURCEVIEW3) || defined (WGTKSOURCEVIEW4)

#include <gtksourceview/gtksourceview.h>
#include <gtksourceview/gtksourcecompletion.h>
#include <gtksourceview/gtksourcecompletioninfo.h>
#include <gtksourceview/gtksourcecompletionitem.h>
#include <gtksourceview/gtksourcelanguagemanager.h>
#include <gtksourceview/completion-providers/words/gtksourcecompletionwords.h>

#include "gtk_appdata.h"

void create_completion (kwinst *app);

#endif

#endif
41 changes: 41 additions & 0 deletions gtk_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ GtkWidget *create_settings_dlg (kwinst *app)
GtkWidget *chkshowmargin; /* checkbox - show right margin guide */
GtkObject *adjmarginwidth; /* adjustment - right marginwidth */
GtkWidget *spinmarginwidth; /* spinbutton - right marginwidth */
GtkWidget *chkenablecmplt; /* checkbox - enable word completion */
#endif

GtkObject *adjtab; /* adjustment - tab spinbutton */
Expand Down Expand Up @@ -552,6 +553,37 @@ GtkWidget *create_settings_dlg (kwinst *app)
/* pack frame into notebook vboxnb */
gtk_box_pack_start (GTK_BOX (vboxnb), frame, FALSE, FALSE, 0);

#ifdef HAVESOURCEVIEW
/* frame within page - cursor positon on open */
frame = gtk_frame_new (NULL);
gtk_frame_set_label (GTK_FRAME (frame), "Word Completion");
gtk_frame_set_label_align (GTK_FRAME (frame), 0.0, 0.5);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_OUT);
gtk_container_set_border_width (GTK_CONTAINER (frame), 5);
gtk_widget_show (frame);

/* table inside frame */
table = gtk_table_new (1, 2, TRUE);
gtk_table_set_row_spacings (GTK_TABLE (table), 5);
gtk_table_set_col_spacings (GTK_TABLE (table), 3);
gtk_container_set_border_width (GTK_CONTAINER (table), 5);
gtk_container_add (GTK_CONTAINER (frame), table);
gtk_widget_show (table);

/* options checkboxs */
chkenablecmplt = gtk_check_button_new_with_mnemonic ("Enable _Word Completion");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chkenablecmplt),
app->enablecmplt);
gtk_table_attach_defaults (GTK_TABLE (table), chkenablecmplt, 0, 1, 0, 1);
gtk_widget_show (chkenablecmplt);
label = gtk_label_new ("(enabled/disabled on next editor use)");
gtk_table_attach_defaults (GTK_TABLE (table), label, 1, 2, 0, 1);
gtk_widget_show (label);

/* pack frame into notebook vboxnb */
gtk_box_pack_start (GTK_BOX (vboxnb), frame, FALSE, FALSE, 0);
#endif

/* frame within page */
frame = gtk_frame_new (NULL);
gtk_frame_set_label (GTK_FRAME (frame), "End-of-Line Handling/Selection");
Expand Down Expand Up @@ -732,6 +764,9 @@ GtkWidget *create_settings_dlg (kwinst *app)
g_signal_connect (spinmarginwidth, "value-changed",
G_CALLBACK (spinmarginwidth_changed), app);

g_signal_connect (chkenablecmplt, "toggled",
G_CALLBACK (chkenablecmplt_toggled), app);

#endif
g_signal_connect (chkwinrestore, "toggled",
G_CALLBACK (chkwinrestore_toggled), app);
Expand Down Expand Up @@ -893,6 +928,12 @@ void spinmarginwidth_changed (GtkWidget *widget, kwinst *app)
app->marginwidth = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(widget));
}

void chkenablecmplt_toggled (GtkWidget *widget, kwinst *app)
{
app->enablecmplt = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
gtk_source_view_set_highlight_current_line (GTK_SOURCE_VIEW(app->view),
app->enablecmplt);
}
#endif

void chkwinrestore_toggled (GtkWidget *widget, kwinst *app)
Expand Down
1 change: 1 addition & 0 deletions gtk_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void spinrecent_changed (GtkWidget *widget, kwinst *app);
void chklinehghlt_toggled (GtkWidget *widget, kwinst *app);
void chkshowmargin_toggled (GtkWidget *widget, kwinst *app);
void spinmarginwidth_changed (GtkWidget *widget, kwinst *app);
void chkenablecmplt_toggled (GtkWidget *widget, kwinst *app);
#endif
// gboolean fov (GtkWidget *widget, GdkEvent *event, gpointer user_data);

Expand Down
4 changes: 4 additions & 0 deletions gtk_textview.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ GtkWidget *create_textview_scrolledwindow (kwinst *app)
// gtk_source_view_set_smart_backspace (GTK_SOURCE_VIEW(app->view), TRUE);
gtk_source_view_set_smart_home_end (GTK_SOURCE_VIEW(app->view),
GTK_SOURCE_SMART_HOME_END_BEFORE);

/* create the sourceview completion object */
if (app->enablecmplt)
create_completion (app);
#else
/* create buffer for text_view, init cursor and iter, line & col */
app->buffer = gtk_text_buffer_new (NULL);
Expand Down
1 change: 1 addition & 0 deletions gtk_textview.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <gtk/gtk.h>

#include "gtk_appdata.h"
#include "gtk_completionsv.h"

GtkWidget *create_textview_scrolledwindow (kwinst *app);
void set_tab_size (PangoFontDescription *font_desc, kwinst *app, gint sz);
Expand Down

0 comments on commit c3f9634

Please sign in to comment.