Gtksourceview-5.0 minimal working example

Hello all,

I migrated from gtk3 to gtk4, thus wanted to give a try to gtksourceview-5.0. I successfully compiled and installed it but having hard time figuring it out for my purposes. For example for gtksourceviewmm-3 there is ; https://gitlab.gnome.org/GNOME/gtksourceviewmm/-/blob/3.91.1/tests/basic/main.cc?ref_type=tags) this for a minimal example. Can anybody direct me to the API page and a minimal working example for gtksourceview-5 ? I can’t seem to find any useful resource when I checked around.

Thanks in advance,

Hi,

There is the official APIs documentation here, and a migration guide from gtk3+gtksourceview4 there.

I don’t know if C++ (gtksourceviewmm) APIs are available for the version 5, if not the standard C APIs should work from C++ code too.

Most of the APIs are unchanged, I remember the only big change on my side was to migrate my GtkSourceCompletion code to the new async APIs. There were also some small changes in the buffer history management, which is now inherited from GtkTextView.

Thank you for the links. After my attempts, I was able to get this far ;

#include <gtksourceview/gtksource.h>

static GMainLoop *main_loop;

int main (int argc, char *argv[])
{

GtkWidget *window;

GtkSourceView *view;
GtkSourceBuffer *buffer;

main_loop = g_main_loop_new (NULL, FALSE);

	gtk_init ();
	gtk_source_init ();

	window = gtk_window_new ();
	view = GTK_SOURCE_VIEW (gtk_source_view_new());

	buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));	
	
	gtk_source_view_set_show_line_marks(view, true);
	
	gtk_window_set_default_size (GTK_WINDOW (window), 900, 600);
	gtk_window_set_title (GTK_WINDOW (window), "GtkSourceView Test");

	g_signal_connect_swapped (window, "destroy", G_CALLBACK (g_main_loop_quit), main_loop);

	gtk_widget_set_visible(window, true);	

	g_main_loop_run (main_loop);

	gtk_source_finalize ();
	return 0;
}

window is appearing but source view is not available. What am I missing ?

Untested, but I don’t see where you add the widget to the window. Use gtk_window_set_child(GTK_WINDOW(window), GTK_WIDGET(view) for that. You probably also want to insert a GtkScrolledWindow in between for scrolling.

window

is a widget thus I can not add a child to it by gtk_window_set_child.

GtkWindow inherits from GtkWidget, so you can use the GTK_WINDOW(window) to cast window (which is definitively a window because you used gtk_window_new ()) to a GtkWindow type like you do for other calls. I’ve adjusted my post.

1 Like

Thank you. I modified as corresponding for the ScrollableWindow ;

GtkScrolledWindow *scroll;
scroll =  GTK_SCROLLED_WINDOW(gtk_scrolled_window_new ());
gtk_scrolled_window_set_child (scroll,  GTK_WIDGET(view));
gtk_window_set_child(GTK_WINDOW(window), GTK_WIDGET(view));
    

But result is not scrollable. Line numbering and the sizes are correct but could not find a way to implement the scroll

I figured it out. Thank you ! :slightly_smiling_face:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.