tutoral/example of gtk sourceview



I'm using GTKSourceView in a project of mine, and noticed there isn't to much of a tutorial/example online.
I wrote an example code for GTKSourceView 2.0

It shows the line numbers and highlights C source.  I guess any programmer can figure out how it works :p

Thanks for this widget!
Maarten


#include <gtk/gtk.h>
#include <gtksourceview/gtksourceview.h>
#include <gtksourceview/gtksourcebuffer.h>
#include <gtksourceview/gtksourcelanguage.h>
#include <gtksourceview/gtksourcelanguagemanager.h>
#include <gtksourceview/gtksourcelanguage.h>


int
main( int argc, char *argv[] )
{
  static GtkWidget *window, *scrollwin, *textview1;
  GtkSourceLanguageManager *lm;
  GtkSourceBuffer *sBuf;
  GtkSourceLanguage *language = NULL;

  /* Create window */
  gtk_init (&argc, &argv);
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW(window), 640, 480);
  gtk_window_set_title (GTK_WINDOW(window),"GTKSourceView Demonstration. Please type some C code");
  g_signal_connect (G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);

  /* Add GTKSourceView widget */
  scrollwin = gtk_scrolled_window_new (NULL, NULL);
  gtk_widget_show (scrollwin);

  lm = gtk_source_language_manager_new();
  sBuf = GTK_SOURCE_BUFFER (gtk_source_buffer_new (NULL));
  g_object_ref (lm);
  g_object_set_data_full ( G_OBJECT (sBuf), "languages-manager",
                           lm, (GDestroyNotify) g_object_unref);


  textview1 = gtk_source_view_new_with_buffer (sBuf);

  gtk_source_view_set_show_line_numbers (GTK_SOURCE_VIEW(textview1), TRUE);

  /* Add widgets to window */
  gtk_widget_show (textview1);
  gtk_container_add (GTK_CONTAINER (scrollwin), textview1);


  lm = g_object_get_data (G_OBJECT (sBuf), "languages-manager");

  /* Open C language, and check if its available */
  language = gtk_source_language_manager_get_language (lm,"c");

  if (language == NULL)
  {
   g_print ("No language found for mime type `%s'\n", "c");
   g_object_set (G_OBJECT (sBuf), "highlight", FALSE, NULL);
   }
   else
   {
    gtk_source_buffer_set_language (sBuf, language);
    g_object_set (G_OBJECT (sBuf), "highlight", TRUE, NULL);
   }

   gtk_container_add (GTK_CONTAINER (window), scrollwin);
 
   gtk_widget_show_all (window);
   gtk_main ();
   return 0;
}

 



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