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.

Details

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.
<?php
error_reporting
(E_ALL);

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

  function
__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

   
$this->set_show_line_numbers(true);
   
$this->set_show_line_markers(true);

   
$this->buffer->set_language($this->lang);
   
$this->buffer->set_highlight(true);
  }

  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))
      return
false;

   
$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;
  }

  function
split() {                               // note 9
   
$edit = new PhpEditWidget();
   
$edit->set_buffer($this->get_buffer());
    return
$edit;
  }
}
// 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();
   
$win->add($vbox);
     
$scrolled_win = new GtkScrolledWindow();
     
$scrolled_win->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
     
$vbox->add1($scrolled_win);
 
       
$php_edit = new PhpEditWidget();
       
$php_edit->load_file(__FILE__);
       
$scrolled_win->add_with_viewport($php_edit);    // note 1

      # test for a split-window like feature       // note 9
     
$scrolled_win = new GtkScrolledWindow();
     
$scrolled_win->set_policy(
       
Gtk::POLICY_AUTOMATIC,
       
Gtk::POLICY_AUTOMATIC);
 
     
$vbox->add2($scrolled_win);
       
$scrolled_win->add_with_viewport($php_edit->split());   // note 9

$win->set_size_request(400, 600);
$win->maximize();
$win->set_position(Gtk::WIN_POS_CENTER);

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

Gtk::main();
?>

To do

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

Links

AttachmentSize
phpEditWidget.php source example1.92 KB

Comments

GtkSourceView

GtkSourceView does work on windows there are links to the dll's on gnope.org 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.