Mike's Musings

Do you want code with that mongoose?

0

Syntax higlighting with gtksourceviewmm

Following on from yesterdays post, here is another task which I couldn’t find code for when I did a quick google search. This time around it’s setting the right syntax highlighting when using gtksourceviewmm, this example does use the function from the last post. For reference this code snippet is part of a function for loading a text file, which takes a Glib::ustring called file_name as a parameter.

m_uri = Gnome::Vfs::Uri::create(file_name);
Glib::ustring buf;

if(!read_file(m_uri, &buf))
	return false;

// Time to add the data to the sourceview
Glib::RefPtr buffer = m_text.get_source_buffer();

buffer->begin_not_undoable_action();
buffer->set_text(buf);
buffer->end_not_undoable_action();

// Set the syntax highlighting
Glib::RefPtr manager = gtksourceview::SourceLanguagesManager::create();
Glib::RefPtr language = manager->get_language_from_mime_type(m_uri->get_file_info()->get_mime_type());

if(language) {
	buffer->set_language(language);
	buffer->set_highlight(true);
}

Leave a Reply