Top | ![]() |
![]() |
![]() |
![]() |
GtkSourceView is the main class of the GtkSourceView library. Use a GtkSourceBuffer to display text with a GtkSourceView.
This class provides:
Show the line numbers;
Show a right margin;
Highlight the current line;
Indentation settings;
Configuration for the Home and End keyboard keys;
Configure and show line marks;
A way to visualize white spaces (by drawing symbols);
And a few other things.
An easy way to test all these features is to use the test-widget mini-program provided in the GtkSourceView repository, in the tests/ directory.
The GtkSourceView implementation of the GtkBuildable interface exposes the “completion” object with the internal-child "completion".
An example of a UI definition fragment with GtkSourceView:
1 2 3 4 5 6 7 8 9 |
<object class="GtkSourceView" id="source_view"> <property name="tab_width">4</property> <property name="auto_indent">True</property> <child internal-child="completion"> <object class="GtkSourceCompletion"> <property name="select_on_show">False</property> </object> </child> </object> |
GtkWidget *
gtk_source_view_new (void
);
Creates a new GtkSourceView. An empty default GtkSourceBuffer will be
created for you and can be retrieved with gtk_text_view_get_buffer()
. If you
want to specify your own buffer, consider gtk_source_view_new_with_buffer()
.
GtkWidget *
gtk_source_view_new_with_buffer (GtkSourceBuffer *buffer
);
Creates a new GtkSourceView widget displaying the buffer
buffer
. One buffer can be shared among many widgets.
void gtk_source_view_set_show_line_numbers (GtkSourceView *view
,gboolean show
);
If TRUE
line numbers will be displayed beside the text.
gboolean
gtk_source_view_get_show_line_numbers (GtkSourceView *view
);
Returns whether line numbers are displayed beside the text.
void gtk_source_view_set_show_right_margin (GtkSourceView *view
,gboolean show
);
If TRUE
a right margin is displayed.
gboolean
gtk_source_view_get_show_right_margin (GtkSourceView *view
);
Returns whether a right margin is displayed.
void gtk_source_view_set_right_margin_position (GtkSourceView *view
,guint pos
);
Sets the position of the right margin in the given view
.
guint
gtk_source_view_get_right_margin_position
(GtkSourceView *view
);
Gets the position of the right margin in the given view
.
void gtk_source_view_set_highlight_current_line (GtkSourceView *view
,gboolean highlight
);
If highlight
is TRUE
the current line will be highlighted.
gboolean
gtk_source_view_get_highlight_current_line
(GtkSourceView *view
);
Returns whether the current line is highlighted.
void gtk_source_view_set_auto_indent (GtkSourceView *view
,gboolean enable
);
If TRUE
auto-indentation of text is enabled.
When Enter is pressed to create a new line, the auto-indentation inserts the same indentation as the previous line. This is not a "smart indentation" where an indentation level is added or removed depending on the context.
gboolean
gtk_source_view_get_auto_indent (GtkSourceView *view
);
Returns whether auto-indentation of text is enabled.
void gtk_source_view_set_indent_on_tab (GtkSourceView *view
,gboolean enable
);
If TRUE
, when the tab key is pressed when several lines are selected, the
selected lines are indented of one level instead of being replaced with a \t
character. Shift+Tab unindents the selection.
If the first or last line is not selected completely, it is also indented or unindented.
When the selection doesn't span several lines, the tab key always replaces the selection with a normal \t character.
gboolean
gtk_source_view_get_indent_on_tab (GtkSourceView *view
);
Returns whether when the tab key is pressed the current selection should get indented instead of replaced with the \t character.
void gtk_source_view_set_tab_width (GtkSourceView *view
,guint width
);
Sets the width of tabulation in characters. The GtkTextBuffer still contains \t characters, but they can take a different visual width in a GtkSourceView widget.
guint
gtk_source_view_get_tab_width (GtkSourceView *view
);
Returns the width of tabulation in characters.
void gtk_source_view_set_indent_width (GtkSourceView *view
,gint width
);
Sets the number of spaces to use for each step of indent when the tab key is
pressed. If width
is -1, the value of the “tab-width” property
will be used.
The “indent-width” interacts with the
“insert-spaces-instead-of-tabs” property and
“tab-width”. An example will be clearer: if the
“indent-width” is 4 and
“tab-width” is 8 and
“insert-spaces-instead-of-tabs” is FALSE
, then pressing the tab
key at the beginning of a line will insert 4 spaces. So far so good. Pressing
the tab key a second time will remove the 4 spaces and insert a \t character
instead (since “tab-width” is 8). On the other hand, if
“insert-spaces-instead-of-tabs” is TRUE
, the second tab key
pressed will insert 4 more spaces for a total of 8 spaces in the
GtkTextBuffer.
The test-widget program (available in the GtkSourceView repository) may be useful to better understand the indentation settings (enable the space drawing!).
gint
gtk_source_view_get_indent_width (GtkSourceView *view
);
Returns the number of spaces to use for each step of indent.
See gtk_source_view_set_indent_width()
for details.
void gtk_source_view_set_insert_spaces_instead_of_tabs (GtkSourceView *view
,gboolean enable
);
If TRUE
a tab key pressed is replaced by a group of space characters. Of
course it is still possible to insert a real \t programmatically with the
GtkTextBuffer API.
gboolean
gtk_source_view_get_insert_spaces_instead_of_tabs
(GtkSourceView *view
);
Returns whether when inserting a tabulator character it should be replaced by a group of space characters.
void gtk_source_view_indent_lines (GtkSourceView *view
,GtkTextIter *start
,GtkTextIter *end
);
Insert one indentation level at the beginning of the specified lines.
view |
||
start |
GtkTextIter of the first line to indent |
|
end |
GtkTextIter of the last line to indent |
Since: 3.16
void gtk_source_view_unindent_lines (GtkSourceView *view
,GtkTextIter *start
,GtkTextIter *end
);
Removes one indentation level at the beginning of the specified lines.
view |
||
start |
GtkTextIter of the first line to indent |
|
end |
GtkTextIter of the last line to indent |
Since: 3.16
guint gtk_source_view_get_visual_column (GtkSourceView *view
,const GtkTextIter *iter
);
Determines the visual column at iter
taking into consideration the
“tab-width” of view
.
void gtk_source_view_set_smart_backspace (GtkSourceView *view
,gboolean smart_backspace
);
When set to TRUE
, pressing the Backspace key will try to delete spaces
up to the previous tab stop.
Since: 3.18
gboolean
gtk_source_view_get_smart_backspace (GtkSourceView *view
);
Returns TRUE
if pressing the Backspace key will try to delete spaces
up to the previous tab stop.
Since: 3.18
void gtk_source_view_set_smart_home_end (GtkSourceView *view
,GtkSourceSmartHomeEndType smart_home_end
);
Set the desired movement of the cursor when HOME and END keys are pressed.
GtkSourceSmartHomeEndType
gtk_source_view_get_smart_home_end (GtkSourceView *view
);
Returns a GtkSourceSmartHomeEndType end value specifying how the cursor will move when HOME and END keys are pressed.
void gtk_source_view_set_mark_attributes (GtkSourceView *view
,const gchar *category
,GtkSourceMarkAttributes *attributes
,gint priority
);
Sets attributes and priority for the category
.
GtkSourceMarkAttributes * gtk_source_view_get_mark_attributes (GtkSourceView *view
,const gchar *category
,gint *priority
);
Gets attributes and priority for the category
.
view |
||
category |
the category. |
|
priority |
place where priority of the category will be stored. |
GtkSourceMarkAttributes for the category
.
The object belongs to view
, so it must not be unreffed.
[transfer none]
void gtk_source_view_set_show_line_marks (GtkSourceView *view
,gboolean show
);
If TRUE
line marks will be displayed beside the text.
Since: 2.2
gboolean
gtk_source_view_get_show_line_marks (GtkSourceView *view
);
Returns whether line marks are displayed beside the text.
Since: 2.2
void gtk_source_view_set_draw_spaces (GtkSourceView *view
,GtkSourceDrawSpacesFlags flags
);
Set if and how the spaces should be visualized. Specifying flags
as 0 will
disable display of spaces.
For a finer-grained method, there is also the GtkSourceTag's “draw-spaces” property.
GtkSourceDrawSpacesFlags
gtk_source_view_get_draw_spaces (GtkSourceView *view
);
Returns the GtkSourceDrawSpacesFlags specifying if and how spaces
should be displayed for this view
.
GtkSourceCompletion *
gtk_source_view_get_completion (GtkSourceView *view
);
Gets the GtkSourceCompletion associated with view
.
GtkSourceGutter * gtk_source_view_get_gutter (GtkSourceView *view
,GtkTextWindowType window_type
);
Returns the GtkSourceGutter object associated with window_type
for view
.
Only GTK_TEXT_WINDOW_LEFT and GTK_TEXT_WINDOW_RIGHT are supported,
respectively corresponding to the left and right gutter. The line numbers
and mark category icons are rendered in the left gutter.
Since: 2.8
void gtk_source_view_set_background_pattern (GtkSourceView *view
,GtkSourceBackgroundPatternType background_pattern
);
Set if and how the background pattern should be displayed.
Since: 3.16
GtkSourceBackgroundPatternType
gtk_source_view_get_background_pattern
(GtkSourceView *view
);
Returns the GtkSourceBackgroundPatternType specifying if and how
the background pattern should be displayed for this view
.
Since: 3.16
smart-home-end disabled. |
||
move to the first/last non-whitespace character on the first press of the HOME/END keys and to the beginning/end of the line on the second press. |
||
move to the beginning/end of the line on the first press of the HOME/END keys and to the first/last non-whitespace character on the second press. |
||
always move to the first/last non-whitespace character when the HOME/END keys are pressed. |
GtkSourceDrawSpacesFlags determine what kind of spaces whould be drawn. If none of GTK_SOURCE_DRAW_SPACES_LEADING, GTK_SOURCE_DRAW_SPACES_TEXT or GTK_SOURCE_DRAW_SPACES_TRAILING is specified, whitespaces at any position in the line will be drawn (i.e. it has the same effect as specifying all of them).
whether the space character should be drawn. |
||
whether the tab character should be drawn. |
||
whether the line breaks should be drawn. |
||
whether the non-breaking whitespaces should be drawn. |
||
whether leading whitespaces should be drawn. |
||
whether whitespaces inside text should be drawn. |
||
whether trailing whitespaces should be drawn. |
||
wheter all kind of spaces should be drawn. |