Problem
You would like to display source codes with automatic numbering and syntax highlighting as shown below:
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:
- 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.
- php_gtk_sourceview2.dll
Go to the ext directoryC:\php-gtk2\ext
and make sure the file php_gtk_sourceview2.dll is there. - php.ini
Edit the php.ini located in the php-gtk2 root folder. Look for the following line:
php-gtk.extensions = php_gtk_libglade2.dlland change it to:
php-gtk.extensions = php_gtk_libglade2.dll, php_gtk_sourceview2.dllNote: 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 = CP1250As 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
- 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". - 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-specsNote: 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 typemkdir .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.
- First create a buffer with GtkSourceBuffer::new_with_language(language)
- Display the buffer with GtkSourceView
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
- Set the language using MIME type. Here we set it to php. For other languages, please see notes below.
- Create the buffer.
- Create the view.
- Create some sample text in buffer.
- If you wish to read from external file, you can uncomment this line.
- Turn on automatic line numbering.
- 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:
Language | MIME type |
---|---|
PHP | application/x-php |
HTML | text/html |
Perl | application/x-perl |
Ada | text/x-ada |
C | text/x-csrc;text/x-chdr |
ChangeLog | text/x-changelog |
C++ | text/x-c++src;text/x-c++hdr |
C# | text/x-csharp |
CSS | text/css |
.desktop | application/x-desktop |
Diff | text/x-patch |
Fortran 95 | text/x-fortran |
GtkRC | text/x-gtkrc |
Haskell | text/x-haskell |
IDL | text/x-idl |
.ini | text/x-ini-file |
Java | text/x-java |
JavaScript | application/x-javascript |
LaTeX | text/x-tex |
Lua | text/x-lua |
Makefile | text/x-makefile |
MSIL | text/x-msil |
Nemerle | text/x-nemerle |
Octave | text/x-octave;text/x-matlab |
Pascal | text/x-pascal |
gettext translation | text/x-gettext-translation |
Python | text/x-python |
R | text/x-R |
Ruby | application/x-ruby |
Scheme | text/x-scheme |
sh | application/x-shellscript |
SQL | text/x-sql |
Tcl | text/x-tcl |
Texinfo | text/x-texinfo |
VB.NET | text/x-vbnet;text/x-vb |
Verilog | text/x-verilog-src |
VHDL | text/x-vhdl |
XML | application/xml |
Read more...