[gnome-latex: 14/205] syntaxic color with GtkSourceView



commit cb3bb6658ec99a67819a2fa20a529822c1af3b57
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Wed Aug 5 17:15:31 2009 +0200

    syntaxic color with GtkSourceView

 TODO            |  2 +-
 src/Makefile    |  4 ++--
 src/callbacks.c | 45 ++++++++++++++++++++++++++++++++++++---------
 src/main.c      |  8 +++++++-
 src/main.h      |  4 +++-
 5 files changed, 49 insertions(+), 14 deletions(-)
---
diff --git a/TODO b/TODO
index b9c8445..8fbbc3f 100644
--- a/TODO
+++ b/TODO
@@ -15,7 +15,7 @@ Jul 31, 2009 to Aug 7, 2009
        - tabs with close buttons
 
 [-] GtkSourceView
-       - syntaxic color
+       x syntaxic color
        - show/hide line numbers
        - undo/redo
 
diff --git a/src/Makefile b/src/Makefile
index 6a008f5..559b26b 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,6 +1,6 @@
 CC = gcc
-CFLAGS = -g -Wall -std=c99 $(shell pkg-config --cflags gtk+-2.0) -DGTK_DISABLE_DEPRECATED=1
-LDFLAGS = $(shell pkg-config --libs gtk+-2.0)
+CFLAGS = -g -Wall -std=c99 $(shell pkg-config --cflags gtk+-2.0 gtksourceview-2.0) -DGTK_DISABLE_DEPRECATED=1
+LDFLAGS = $(shell pkg-config --libs gtk+-2.0 gtksourceview-2.0)
 OBJ = main.o callbacks.o error.o
 
 .PHONY: clean
diff --git a/src/callbacks.c b/src/callbacks.c
index 93b6e95..4ef7292 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -3,6 +3,9 @@
 #include <locale.h>
 #include <libintl.h>
 #include <gtk/gtk.h>
+#include <gtksourceview/gtksourceview.h>
+#include <gtksourceview/gtksourcelanguage.h>
+#include <gtksourceview/gtksourcelanguagemanager.h>
 
 #include "main.h"
 #include "callbacks.h"
@@ -143,7 +146,11 @@ cb_close (void)
 
                /* close the tab */
                docs.all = g_list_remove (docs.all, docs.active);
-               g_free (docs.active->path);
+               if (docs.active->path != NULL)
+               {
+                       print_info ("close the file \"%s\"", docs.active->path);
+                       g_free (docs.active->path);
+               }
                g_free (docs.active);
                gtk_notebook_remove_page (docs.notebook, gtk_notebook_get_current_page (docs.notebook));
                if (gtk_notebook_get_n_pages (docs.notebook) > 0)
@@ -235,26 +242,46 @@ create_document_in_new_tab (gchar *path, gchar *text, GtkWidget *label)
        // if path = NULL, this is a new document
        // else, the user opened a document
        document_t *new_doc = g_malloc (sizeof (document_t));
-       new_doc->path = path;
-       new_doc->saved = (path == NULL) ? FALSE : TRUE;
-       new_doc->text_view = GTK_TEXT_VIEW (gtk_text_view_new ());
+       new_doc->path = g_strdup (path);
+       //new_doc->saved = (path == NULL) ? FALSE : TRUE;
+       new_doc->saved = TRUE;
+       new_doc->source_buffer = gtk_source_buffer_new (NULL);
+       new_doc->source_view = gtk_source_view_new_with_buffer (new_doc->source_buffer);
 
        docs.all = g_list_append (docs.all, new_doc);
        docs.active = new_doc;
 
+       // set the language for the syntaxic color
+       if (path != NULL)
+       {
+               GtkSourceLanguage *lang = gtk_source_language_manager_guess_language (
+                               docs.lm, path, NULL);
+               if (lang != NULL)
+                       gtk_source_buffer_set_language (new_doc->source_buffer, lang);
+               else
+                       print_info ("language of the file unknown ==> no syntaxic color");
+       }
+
+       // the default file is a LaTeX document
+       else
+       {
+               GtkSourceLanguage *lang = gtk_source_language_manager_get_language (
+                               docs.lm, "latex");
+               gtk_source_buffer_set_language (new_doc->source_buffer, lang);
+       }
+
        // put the text into the buffer
-       GtkTextBuffer *text_buffer = gtk_text_view_get_buffer (new_doc->text_view);
-       gtk_text_buffer_set_text (text_buffer, text, -1);
+       gtk_text_buffer_set_text (GTK_TEXT_BUFFER (new_doc->source_buffer), text, -1);
 
        // when the text is modified
-       g_signal_connect (G_OBJECT (text_buffer), "changed",
+       g_signal_connect (G_OBJECT (new_doc->source_buffer), "changed",
                        G_CALLBACK (cb_text_changed), NULL);
 
        // with a scrollbar
        GtkWidget *sw = gtk_scrolled_window_new (NULL, NULL);
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
-       gtk_container_add (GTK_CONTAINER (sw), GTK_WIDGET (new_doc->text_view));
+       gtk_container_add (GTK_CONTAINER (sw), GTK_WIDGET (new_doc->source_view));
        gtk_widget_show_all (sw);
 
        // add the new document in a new tab
@@ -290,7 +317,7 @@ file_save (void)
                FILE* file = fopen (docs.active->path, "w");
                if (file != NULL)
                {
-                       GtkTextBuffer *text_buffer = gtk_text_view_get_buffer (docs.active->text_view);
+                       GtkTextBuffer *text_buffer = GTK_TEXT_BUFFER (docs.active->source_buffer);
                        GtkTextIter start;
                        GtkTextIter end;
                        gtk_text_buffer_get_bounds (text_buffer, &start, &end);
diff --git a/src/main.c b/src/main.c
index 92c8084..0741316 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,12 +3,15 @@
 #include <locale.h>
 #include <libintl.h>
 #include <gtk/gtk.h>
+#include <gtksourceview/gtksourceview.h>
+#include <gtksourceview/gtksourcelanguage.h>
+#include <gtksourceview/gtksourcelanguagemanager.h>
 
 #include "main.h"
 #include "callbacks.h"
 #include "error.h"
 
-docs_t docs = {NULL, NULL, NULL, NULL}; 
+docs_t docs = {NULL, NULL, NULL, NULL, NULL}; 
 
 int
 main (int argc, char *argv[])
@@ -119,6 +122,9 @@ main (int argc, char *argv[])
        gtk_paned_pack1 (GTK_PANED (vpaned), notebook, TRUE, TRUE);
        docs.notebook = GTK_NOTEBOOK (notebook);
 
+       GtkSourceLanguageManager *lm = gtk_source_language_manager_get_default ();
+       docs.lm = lm;
+
        /* log zone */
        //TODO set a default height
        GtkWidget *log_view = gtk_text_view_new ();
diff --git a/src/main.h b/src/main.h
index b1f7d00..beb5a41 100644
--- a/src/main.h
+++ b/src/main.h
@@ -11,7 +11,8 @@ typedef struct
 {
        gchar *path;
        gboolean saved;
-       GtkTextView *text_view;
+       GtkWidget *source_view;
+       GtkSourceBuffer *source_buffer;
 } document_t;
 
 typedef struct
@@ -20,6 +21,7 @@ typedef struct
        document_t *active;
        GtkWindow *main_window;
        GtkNotebook *notebook;
+       GtkSourceLanguageManager *lm;
 } docs_t;
 
 // all the documents are accessible by the docs variable


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