Using GtkSourceview to build a php editor with syntax highlighting

a simple php editor using GtkSourceView Here is a quick way to build a PHP editor with syntax highlighting. This widget is nearly usable as a real php-gtk IDE. It extends GtkSourceView widget and internally manages both a text buffer and language object classes.


To use a GtkSourceView widget, you need to manage :

  • a text buffer containing edited data,
  • a view : a GtkSourceWiew widget or subclass,
  • for syntax highlighting, there is some classes ; in the class source below, you can see how to use them.

GtkSourceView widgets are extensions of a GtkTextWidget model. So you can use regular methods from this framework (see GtkTextView, GtkTextBuffer classes).

Supported Features

a simple php editor using GtkSourceView GtksourceView has builtin features :

  • multiple undo (redo?),
  • syntaxe highlighting (php, C, C++,bash ...),
  • line numbering
  • marker management
  • brackets highlighting

Source code

here is some details :

  1. PhpEditWidget is a widget (extending GtkSourceView) ; so you can use it just like a regular widget,
  2. lets parent widget build what need to be done,
  3. we want to create a php editor with php syntax highlighting ; just feed php mime type to GtkSourceLanguages* classes
  4. text editing with Gtk widget respect MVC partern model (Model, View, Controler); GtkSourceBuffer is the Model part, GtkSourceView is the View part, the code you are writing is the Controler part. Text editing, changes are done with Buffer part (Model), the View part is responsible for display part (line numbering, syntax highlighting, user interactions), Controler part should be a php class overriding PhpEditWidget widget ; this widget is keeped as simple as possible for this tutorial.
  5. This is an all-in-one widget ; the MVC part is directly managed by PhpEditWidget widget ; in simple usage, you can ignore text buffer existance, you will only need to use a ready to use widget. If you need to write more complexe usage, GtkSourceBuffer is still available as a protected attribute for experimentated developpers only
  6. File loading is made by a conveniant public method
  7. php source text is appended to text buffer (should be cleared before)
  8. text code should be protected from "undo" feature
  9. some IDE offer window spliting ; this feature is make quickly with this split method; we need to create a new PhpEditor widget and share text buffer between widgets.

PhpEditWidget extends GtkSourceView // Note 1
protected $buffer;                        // note 5
protected $lang;

__construct() {
parent::__construct();  // note 2

    # default lang
$this->mime_lang = 'text/x-php-source';    // note 3
$this->set_lang($this->mime_lang);        // note 3

$this->buffer = new GtkSourceBuffer();     // note 4, note 5
$this->set_buffer($this->buffer);              // note 4



  protected function
set_lang($mime_type) {      // note 3
$lang_manager = new GtkSourceLanguagesManager();
$this->lang = $lang_manager->get_language_from_mime_type($mime_type);

  public function
load_file($file) {    // note 6
if (!file_exists($file))

$lines = file_get_contents($file);

$this->buffer->begin_not_undoable_action();  // note 8
$this->buffer->set_text($lines);   // note 7
$this->buffer->end_not_undoable_action();   // note 8
return true;

split() {                               // note 9
$edit = new PhpEditWidget();
// end of class PhpEditWidget

* In this part of the code, indenting matches the
* widgets containment, not the code structure
$win = new GtkWindow();
$vbox = new GtkVPaned();
$scrolled_win = new GtkScrolledWindow();
$scrolled_win->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
$php_edit = new PhpEditWidget();
$scrolled_win->add_with_viewport($php_edit);    // note 1

      # test for a split-window like feature       // note 9
$scrolled_win = new GtkScrolledWindow();
$scrolled_win->add_with_viewport($php_edit->split());   // note 9

$win->set_size_request(400, 600);

$win->set_title("PhpEditWidget - demo");
$win->connect_simple("destroy", array("Gtk", "main_quit"));


To do

  • manage fonts,
  • make same widget demo with Scintilla widget


phpEditWidget.php source example1.92 KB



GtkSourceView does work on windows there are links to the dll's on forum.

gtksourceview on windows

gtksourceview works on windows - there's a version of gnope that ships with it and it is included in the latest beta

However, the mime-type listed above is incorrect for newer versions of gtksourceview (the windows beta uses gtksourceview 2.8) - the php language mime type is application/x-php. Language spec files are just XML, so feel free to open one up in a text editor, the second line will have the mimetype you want

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.