Styles and similar

You can treat the Scintilla editor as a normal widget. The constructor take no arguments:

>>> import gtkscintilla
>>> s = gtkscintilla.Scintilla()

The widget has a lot of features, however there’s a particular system to define the text elements (markers, text, margins, etc...).

Let’s take as an example the text style system.

Each charachter in the editor has an associated style_id, this is used internally to scintilla and substantially it represent a style definition.

It’s difficult to explain this concept in words:

>>> s = gtkscintilla.Scintilla()
>>> s.set_style(1, color = "blue", font = "!Monospace")

With this line of code you register the style 1 as a style that is blue and has a Monospace font (the “!” is used to force the editor to use pango descriptions, you have to use it everytime).

When you want to apply the style simply use:

>>> s.apply_style(1, 0, 3)

It applies the style that has the style_id 1, from position 0 to 3.


The concept is: first register (set), then apply.

The same rules (the id and so on) are also followed by for example margins, markers and indicators.

There are other elements that are set without an id, essentially they are unique elements, like the Caret ( the blinking cursor ) and the selection, there aren’t 2 selection types you just have to set your preferences:

>>> s.set_caret(period = 300,width = 3)

This sets the caret to blink every 300 millisecond and to have a width of 3 pixels.

Syntax Highlighting

The syntax highlighting is implemented by lexers, please refer to the Lexers section. However it’s simple as spelling a language name.

The immediate example is:

>>> s = Scintilla()
>>> s.set_lexer("python")

That’s it! in the Lexers section are explained ways to customize appearence of syntax elements.

Code Folding

To implement code folding you have to work on margins, to make them active and to show markers when there’s a folding point [1].

Quickly, there are 5 margins numbered 0 to 4 each margin has some properties, for code folding we are interested in the sensitive, type, mask properties. The sensitive property makes the margin to intercept mouse clicks, and to emit a signal, the margin-click. The other properties tell the margin to display symbols (not numbers or text) and the mask properties... ehm.. I don’t know.

So to implement code folding you have to:

  1. set the lexer that recognises the folding points
  2. choose a margin
  3. set the margin properties
  4. set markers to represent the folding states (open, close etc..)
  5. write a callback that intercepts the margin-click signal.

Set the Lexer

>>> s.set_lexer("python")

Make sure that it has the folding properties up:

>>> s.set_property("fold","1") # "1" is a string

Choose a margin

Well, usually the margin used for folding is the margin 1

Set the margin properties

With the gtkscintilla.Scintilla.set_margin() method: the first argument is the margin number that you have choose, other arguments are keyword arguments:

>>> s.set_margin(1, sensitive = True, type = SC_MARGIN_SYMBOL, mask = SC_MASK_FOLDERS )

Set the markers

They are defined by constants used by folding. You can import them from gtkscintilla module.

self.set_marker(SC_MARKNUM_FOLDER, type = SC_MARK_PLUS)
self.set_marker(SC_MARKNUM_FOLDEROPEN, type = SC_MARK_MINUS)
self.set_marker(SC_MARKNUM_FOLDEREND, type = SC_MARK_EMPTY)
self.set_marker(SC_MARKNUM_FOLDERTAIL, type = SC_MARK_EMPTY)
self.set_marker(SC_MARKNUM_FOLDERSUB, type = SC_MARK_EMPTY)

Write the callback

def onMarginClicked(self, w ,mod,position,margin):
    ln = self.line_from_position(position)
    if margin == FOLD_MARGIN:
[1]The lexer recognises the folding states, don’t care about it