079. How to use GtkSourceView to display source code - Part 1?

Problem

You would like to display source codes with automatic numbering and syntax highlighting as shown below:

How to use GtkSourceView to display source code - Part 1?


Solution

php-gtk2 comes with Sourceview Classes that allow you display source codes, html scripts and SQL statements with automatic numbering and syntax highlighting.

Using the classes is simple and straightforward. The "challenging" part is how to get this up and running. As this is an extension to php-gtk2, the default Gnope Installer doesn't set this up for you.

Here are the steps to "turn on" the source view feature:

  1. gtksourceview.dll

    Assuming you have installed php-gtk2 in C:\php-gtk2. Make sure the file gtksourceview.dll there. The latest version of Gnope Installer puts it there. But I have an earlier version of Gnope Installer that puts the file in the C:\php-gtk2\ext directory. If it's in the ext directory, just make a copy in the php-gtk2 root folder.

  2. php_gtk_sourceview2.dll
    Go to the ext directory C:\php-gtk2\ext and make sure the file php_gtk_sourceview2.dll is there.
  3. php.ini

    Edit the php.ini located in the php-gtk2 root folder. Look for the following line:

    php-gtk.extensions = php_gtk_libglade2.dll

    and change it to:

    php-gtk.extensions = php_gtk_libglade2.dll, php_gtk_sourceview2.dll

    Note: If you have used the Gnope Installer to set up your php-gtk2, most likely you will see one big chunk of text near the end of the php.ini as shown below: (unless you are one of those people like me who enjoys using emacs on windows...:)

    ;;;;;;;;;;;;;;;;;;;;;; ; PHP-GTK extensions ; ;;;;;;;;;;;;;;;;;;;;;; ; Extensions written for PHP-GTK are in the format php_gtk_*.dll (Windows) or ; php_gtk_*.so (Unix), written here as a comma-separated list. The library ; files need to be in the same directory as the PHP-GTK library, along with ; any other PHP extensions you are using. php-gtk.extensions = php_gtk_libglade2.dll ;;;;;;;;;;;;; ; Code Page ; ;;;;;;;;;;;;; ; The string variables used for titles and other text values in GTK+ are ; encoded in UTF-8 internally. A code page is needed so that PHP-GTK 'knows' ; which character set is being used, and can convert it to UTF-8 as necessary. ; If your environment uses UTF-8 already, you can set the codepage directive ; to UTF-8 to skip the conversions. ; The default codepage setting in PHP-GTK 2 is ISO-8859-1, but you can also ; use either OEM (e.g. 850) or Windows Code Pages (e.g. CP1250) here, so ; long as the encoding format you choose is capable of iconv conversion. See ; http://www.microsoft.com/globaldev/reference/cphome.mspx for a list of ; the code pages and character sets that are supported on Windows systems. php-gtk.codepage = CP1250

    As you can see, the above line is "buried" in this chunk of text.

    To make this more readable, you could put in proper line breaks as follows:

    ;;;;;;;;;;;;;;;;;;;;;;
    ; PHP-GTK extensions ;
    ;;;;;;;;;;;;;;;;;;;;;;

    ; Extensions written for PHP-GTK are in the format php_gtk_*.dll (Windows) or
    ; php_gtk_*.so (Unix), written here as a comma-separated list. The library
    ; files need to be in the same directory as the PHP-GTK library, along with
    ; any other PHP extensions you are using.

    php-gtk.extensions = php_gtk_libglade2.dll, php_gtk_sourceview2.dll

    ;;;;;;;;;;;;;
    ; Code Page ;
    ;;;;;;;;;;;;;

    ; The string variables used for titles and other text values in GTK+ are
    ; encoded in UTF-8 internally. A code page is needed so that PHP-GTK 'knows'
    ; which character set is being used, and can convert it to UTF-8 as necessary.
    ; If your environment uses UTF-8 already, you can set the codepage directive
    ; to UTF-8 to skip the conversions.
    ; The default codepage setting in PHP-GTK 2 is ISO-8859-1, but you can also
    ; use either OEM (e.g. 850) or Windows Code Pages (e.g. CP1250) here, so
    ; long as the encoding format you choose is capable of iconv conversion. See
    ; http://www.microsoft.com/globaldev/reference/cphome.mspx for a list of
    ; the code pages and character sets that are supported on Windows systems.

    php-gtk.codepage = CP1250

  4.  
  5. Download a copy of language-specs
    (NOTE: You can skip this step if you are using PHP-GTK2 build by Elizabeth Smith.)
    You probably didn't expect this, but you need to download a copy of the language-specs from gtksourceview.sourceforge.net. Untar the file gtksourceview-1.6.0.tar.gz and you will find a folder inside "gtksourceview" called "language-specs".
  6.  
  7. Last and most important!!!
    (NOTE: You can skip this step if you are using PHP-GTK2 build by Elizabeth Smith.)

    This one took me one whole night to figure out!!! Where do you put this language-specs folder? Guess what? Here's the full path of the language-specs folder (replace <username> with your windows login name):

    C:\Documents and Settings\<username>\.gnome2\gtksourceview-1.0\language-specs

    Note: Windows Explorer won't allow you create a folder with name ".gnome2". You may use command prompt instead. Just go to the directory C:\Documents and Settings\<username> and type mkdir .gnome2.

Once you have set up the above, you can start to use the sourceview classes. Note that GtkSourceBuffer is a direct subclass of and GtkTextBuffer. Similarly, GtkSourceView is a direct subclass of GtkTextView. So using GtkSourceBuffer/GtkTextView is exactly the same as using GtkTextBuffer/GtkTextView.


Sample Code

1   
2   
3   
4   
5   
6   
8   
9   
11   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
36   
37   
38   
42   
43   
44   
45   
46   
<?php
$window = new GtkWindow();
$window->set_size_request(480, 240);
$window->connect_simple('destroy', array('Gtk','main_quit'));

$lang_mgr = new GtkSourceLanguagesManager();
$lang = $lang_mgr->get_language_from_mime_type("application/x-php"); // note 1
$buffer = GtkSourceBuffer::new_with_language($lang); // note 2
$view = GtkSourceView::new_with_buffer($buffer); // note 3

$buffer->set_text('<?php
    $window = new GtkWindow();
    $window->set_size_request(480, 240);
    $window->connect_simple("destroy", array("Gtk","main_quit"));

    $lang_mgr = new GtkSourceLanguagesManager();
    $lang = $lang_mgr->get_language_from_mime_type("text/x-php");
    $buffer = GtkSourceBuffer::new_with_language($lang);
    $view = GtkSourceView::new_with_buffer($buffer);
    $view->set_show_line_numbers(1);
    $buffer->set_highlight(1);

    $window->add($view);
    $window->show_all();
    Gtk::main();
?>');

// if you want to read a file
// $buffer->set_text(file_get_contents('file.php')); note 5

$view->set_show_line_numbers(1); // note 6
$buffer->set_highlight(1); // note 7

$window->add($view);
$window->show_all();
Gtk::main();
?>

Output

 

Explanation

  1. Set the language using MIME type. Here we set it to php. For other languages, please see notes below.
  2. Create the buffer.
  3. Create the view.
  4. Create some sample text in buffer.
  5. If you wish to read from external file, you can uncomment this line.
  6. Turn on automatic line numbering.
  7. Turn on syntax highlighting.

Linux

If you already have a compiled php-gtk2 running and wish to "turn on" the GtkSourceView feature, please refer to: here.

If you are compiling everything from scratch, please refer to the article: How to install php gtk2 on linux?.

Note

The language is specified using MIME type. If you have downloaded the language-specs as outlined above, below are the languages supported and the corresponding MIME type:

LanguageMIME type
PHPapplication/x-php
HTMLtext/html
Perlapplication/x-perl
Adatext/x-ada
Ctext/x-csrc;text/x-chdr
ChangeLogtext/x-changelog
C++text/x-c++src;text/x-c++hdr
C#text/x-csharp
CSStext/css
.desktopapplication/x-desktop
Difftext/x-patch
Fortran 95text/x-fortran
GtkRCtext/x-gtkrc
Haskelltext/x-haskell
IDLtext/x-idl
.initext/x-ini-file
Javatext/x-java
JavaScriptapplication/x-javascript
LaTeXtext/x-tex
Luatext/x-lua
Makefiletext/x-makefile
MSILtext/x-msil
Nemerletext/x-nemerle
Octavetext/x-octave;text/x-matlab
Pascaltext/x-pascal
gettext translationtext/x-gettext-translation
Pythontext/x-python
Rtext/x-R
Rubyapplication/x-ruby
Schemetext/x-scheme
shapplication/x-shellscript
SQLtext/x-sql
Tcltext/x-tcl
Texinfotext/x-texinfo
VB.NETtext/x-vbnet;text/x-vb
Verilogtext/x-verilog-src
VHDLtext/x-vhdl
XMLapplication/xml

Add comment


Security code
Refresh