[gtksourceview/wip/chergert/vim] wire up basic command bar



commit d7f477ca2a9befc8efce084f20afc92f448393da
Author: Christian Hergert <chergert redhat com>
Date:   Fri Oct 22 11:55:56 2021 -0700

    wire up basic command bar

 gtksourceview/gtksourcevimimcontext.c          |  25 ++++++
 gtksourceview/vim/gtk-source-vim-command-bar.c | 104 ++++++++++++++++++++++++-
 gtksourceview/vim/gtk-source-vim-command-bar.h |   3 +-
 gtksourceview/vim/gtk-source-vim-normal.c      |  18 +----
 gtksourceview/vim/gtk-source-vim.c             |   4 +-
 5 files changed, 136 insertions(+), 18 deletions(-)
---
diff --git a/gtksourceview/gtksourcevimimcontext.c b/gtksourceview/gtksourcevimimcontext.c
index 8334188c..1cd48103 100644
--- a/gtksourceview/gtksourcevimimcontext.c
+++ b/gtksourceview/gtksourcevimimcontext.c
@@ -50,6 +50,25 @@ gtk_source_vim_im_context_new (void)
        return g_object_new (GTK_SOURCE_TYPE_VIM_IM_CONTEXT, NULL);
 }
 
+static void
+on_vim_notify_cb (GtkSourceVimIMContext *self,
+                  GParamSpec            *pspec,
+                  GtkSourceVim          *vim)
+{
+       g_assert (GTK_SOURCE_IS_VIM_IM_CONTEXT (self));
+       g_assert (GTK_SOURCE_IS_VIM (vim));
+
+       if (g_str_equal (pspec->name, "command-bar-text"))
+               pspec = properties[PROP_COMMAND_BAR_TEXT];
+       else if (g_str_equal (pspec->name, "command-text"))
+               pspec = properties[PROP_COMMAND_TEXT];
+       else
+               pspec = NULL;
+
+       if (pspec)
+               g_object_notify_by_pspec (G_OBJECT (self), pspec);
+}
+
 static void
 gtk_source_vim_im_context_set_client_widget (GtkIMContext *context,
                                              GtkWidget    *widget)
@@ -66,6 +85,12 @@ gtk_source_vim_im_context_set_client_widget (GtkIMContext *context,
 
        self->vim = gtk_source_vim_new (GTK_SOURCE_VIEW (widget));
 
+       g_signal_connect_object (self->vim,
+                                "notify",
+                                G_CALLBACK (on_vim_notify_cb),
+                                self,
+                                G_CONNECT_SWAPPED);
+
        g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_COMMAND_TEXT]);
        g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_COMMAND_BAR_TEXT]);
 }
diff --git a/gtksourceview/vim/gtk-source-vim-command-bar.c b/gtksourceview/vim/gtk-source-vim-command-bar.c
index cbb2385f..01c01dc1 100644
--- a/gtksourceview/vim/gtk-source-vim-command-bar.c
+++ b/gtksourceview/vim/gtk-source-vim-command-bar.c
@@ -21,16 +21,18 @@
 
 #include "config.h"
 
+#include "gtk-source-vim.h"
 #include "gtk-source-vim-command-bar.h"
 
 struct _GtkSourceVimCommandBar
 {
        GtkSourceVimState parent_instance;
+       GString *buffer;
 };
 
 G_DEFINE_TYPE (GtkSourceVimCommandBar, gtk_source_vim_command_bar, GTK_SOURCE_TYPE_VIM_STATE)
 
-GtkSourceVimCommandBar *
+GtkSourceVimState *
 gtk_source_vim_command_bar_new (void)
 {
        return g_object_new (GTK_SOURCE_TYPE_VIM_COMMAND_BAR, NULL);
@@ -39,18 +41,118 @@ gtk_source_vim_command_bar_new (void)
 static void
 gtk_source_vim_command_bar_dispose (GObject *object)
 {
+       GtkSourceVimCommandBar *self = (GtkSourceVimCommandBar *)object;
+
+       if (self->buffer == NULL)
+       {
+               g_string_free (self->buffer, TRUE);
+               self->buffer = NULL;
+       }
+
        G_OBJECT_CLASS (gtk_source_vim_command_bar_parent_class)->dispose (object);
 }
 
+static void
+do_notify (GtkSourceVimCommandBar *self)
+{
+       GtkSourceVimState *root;
+
+       g_assert (GTK_SOURCE_IS_VIM_COMMAND_BAR (self));
+
+       root = gtk_source_vim_state_get_root (GTK_SOURCE_VIM_STATE (self));
+
+       if (GTK_SOURCE_IS_VIM (root))
+       {
+               g_object_notify (G_OBJECT (root), "command-bar-text");
+       }
+}
+
+static gboolean
+gtk_source_vim_command_bar_handle_keypress (GtkSourceVimState *state,
+                                            guint              keyval,
+                                            guint              keycode,
+                                            GdkModifierType    mods,
+                                            const char        *str)
+{
+       GtkSourceVimCommandBar *self = (GtkSourceVimCommandBar *)state;
+
+       g_assert (GTK_SOURCE_IS_VIM_COMMAND_BAR (self));
+
+       /* TODO: It would be nice to reuse the insert-literal stuff for
+        * this if we can (which might mean using a temporary text view).
+        */
+
+       if (gtk_source_vim_state_is_escape (keyval, mods))
+       {
+               g_string_truncate (self->buffer, 0);
+               do_notify (self);
+               gtk_source_vim_state_pop (state);
+               return TRUE;
+       }
+
+       if (str[0])
+       {
+               g_string_append (self->buffer, str);
+               do_notify (self);
+       }
+
+       return TRUE;
+}
+
+static void
+gtk_source_vim_command_bar_enter (GtkSourceVimState *state)
+{
+       GtkSourceVimCommandBar *self = (GtkSourceVimCommandBar *)state;
+       GtkSourceView *view;
+
+       g_assert (GTK_SOURCE_VIM_STATE (self));
+
+       g_string_truncate (self->buffer, 0);
+       g_string_append_c (self->buffer, ':');
+       do_notify (self);
+
+       view = gtk_source_vim_state_get_view (GTK_SOURCE_VIM_STATE (self));
+       gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (view), FALSE);
+}
+
+static void
+gtk_source_vim_command_bar_leave (GtkSourceVimState *state)
+{
+       GtkSourceVimCommandBar *self = (GtkSourceVimCommandBar *)state;
+       GtkSourceView *view;
+
+       g_assert (GTK_SOURCE_VIM_STATE (self));
+
+       g_string_truncate (self->buffer, 0);
+       do_notify (self);
+
+       view = gtk_source_vim_state_get_view (GTK_SOURCE_VIM_STATE (self));
+       gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (view), TRUE);
+}
+
 static void
 gtk_source_vim_command_bar_class_init (GtkSourceVimCommandBarClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       GtkSourceVimStateClass *state_class = GTK_SOURCE_VIM_STATE_CLASS (klass);
 
        object_class->dispose = gtk_source_vim_command_bar_dispose;
+
+       state_class->enter = gtk_source_vim_command_bar_enter;
+       state_class->leave = gtk_source_vim_command_bar_leave;
+       state_class->handle_keypress = gtk_source_vim_command_bar_handle_keypress;
 }
 
 static void
 gtk_source_vim_command_bar_init (GtkSourceVimCommandBar *self)
 {
+       self->buffer = g_string_new (NULL);
+}
+
+const char *
+gtk_source_vim_command_bar_get_text (GtkSourceVimCommandBar *self)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_VIM_COMMAND_BAR (self), NULL);
+
+       return self->buffer->str;
 }
diff --git a/gtksourceview/vim/gtk-source-vim-command-bar.h b/gtksourceview/vim/gtk-source-vim-command-bar.h
index bf212128..2495fa8a 100644
--- a/gtksourceview/vim/gtk-source-vim-command-bar.h
+++ b/gtksourceview/vim/gtk-source-vim-command-bar.h
@@ -29,6 +29,7 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GtkSourceVimCommandBar, gtk_source_vim_command_bar, GTK_SOURCE, VIM_COMMAND_BAR, 
GtkSourceVimState)
 
-GtkSourceVimCommandBar *gtk_source_vim_command_bar_new (void);
+GtkSourceVimState *gtk_source_vim_command_bar_new      (void);
+const char         *gtk_source_vim_command_bar_get_text (GtkSourceVimCommandBar *self);
 
 G_END_DECLS
diff --git a/gtksourceview/vim/gtk-source-vim-normal.c b/gtksourceview/vim/gtk-source-vim-normal.c
index eee71207..e8fc3336 100644
--- a/gtksourceview/vim/gtk-source-vim-normal.c
+++ b/gtksourceview/vim/gtk-source-vim-normal.c
@@ -21,6 +21,7 @@
 
 #include "config.h"
 
+#include "gtk-source-vim-command-bar.h"
 #include "gtk-source-vim-insert.h"
 #include "gtk-source-vim-normal.h"
 #include "gtk-source-vim-replace.h"
@@ -232,18 +233,6 @@ key_handler_search (GtkSourceVimNormal *self,
        return TRUE;
 }
 
-static gboolean
-key_handler_command_bar (GtkSourceVimNormal *self,
-                         guint               keyval,
-                         guint               keycode,
-                         GdkModifierType     mods,
-                         const char         *string)
-{
-       g_assert (GTK_SOURCE_IS_VIM_NORMAL (self));
-
-       return TRUE;
-}
-
 static gboolean
 key_handler_yank (GtkSourceVimNormal *self,
                   guint               keyval,
@@ -466,8 +455,9 @@ key_handler_initial (GtkSourceVimNormal *self,
                                break;
 
                        case GDK_KEY_colon:
-                               self->handler = key_handler_command_bar;
-                               break;
+                               gtk_source_vim_state_push (GTK_SOURCE_VIM_STATE (self),
+                                                          gtk_source_vim_command_bar_new ());
+                               return TRUE;
 
                        case GDK_KEY_v:
                        case GDK_KEY_V:
diff --git a/gtksourceview/vim/gtk-source-vim.c b/gtksourceview/vim/gtk-source-vim.c
index b7f29637..d080fa47 100644
--- a/gtksourceview/vim/gtk-source-vim.c
+++ b/gtksourceview/vim/gtk-source-vim.c
@@ -190,11 +190,11 @@ gtk_source_vim_get_command_bar_text (GtkSourceVim *self)
 
        g_return_val_if_fail (GTK_SOURCE_IS_VIM (self), NULL);
 
-       child = gtk_source_vim_state_get_child (GTK_SOURCE_VIM_STATE (self));
+       child = gtk_source_vim_state_get_current (GTK_SOURCE_VIM_STATE (self));
 
        if (GTK_SOURCE_IS_VIM_COMMAND_BAR (child))
        {
-               /* TODO */
+               return gtk_source_vim_command_bar_get_text (GTK_SOURCE_VIM_COMMAND_BAR (child));
        }
 
        return "";


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]