gtk2, gtksourceview2 - Page 1
Forum section: electronics / general

author: DiTBho
author_rank: Super Contributor
timestamp: February 10, 2022, 03:43:14 pm
content: Code:#include <gtk/gtk.h>#include <gtksourceview/gtksourceview.h>#include <gtksourceview/gtksourcebuffer.h>#include <gtksourceview/gtksourcelanguage.h>#include <gtksourceview/gtksourcelanguagesmanager.h>static gboolean open_file (GtkSourceBuffer *sBuf, const gchar *filename);int main( int argc, char *argv[] ){static GtkWidget *window, *pScrollWin, *sView;PangoFontDescription *font_desc;GtkSourceLanguagesManager *lm;GtkSourceBuffer *sBuf;/* Create a Window. */gtk_init (&argc, &argv);window = gtk_window_new (GTK_WINDOW_TOPLEVEL);g_signal_connect (G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);gtk_container_set_border_width (GTK_CONTAINER (window), 10);gtk_window_set_default_size (GTK_WINDOW(window), 660, 500);gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);/* Create a Scrolled Window that will contain the GtkSourceView */pScrollWin = gtk_scrolled_window_new (NULL, NULL);gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (pScrollWin),GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);/* Now create a GtkSourceLanguagesManager */lm = gtk_source_languages_manager_new();/* and a GtkSourceBuffer to hold text (similar to GtkTextBuffer) */sBuf = GTK_SOURCE_BUFFER (gtk_source_buffer_new (NULL));g_object_ref (lm);g_object_set_data_full ( G_OBJECT (sBuf), "languages-manager",lm, (GDestroyNotify) g_object_unref);/* Create the GtkSourceView and associate it with the buffer */sView = gtk_source_view_new_with_buffer(sBuf);/* Set default Font name,size */font_desc = pango_font_description_from_string ("mono 8");gtk_widget_modify_font (sView, font_desc);pango_font_description_free (font_desc);/* Attach the GtkSourceView to the scrolled Window */gtk_container_add (GTK_CONTAINER (pScrollWin), GTK_WIDGET (sView));/* And the Scrolled Window to the main Window */gtk_container_add (GTK_CONTAINER (window), pScrollWin);gtk_widget_show_all (pScrollWin);/* Finally load an example file to see how it works */open_file (sBuf, "srcview.c");gtk_widget_show (window);gtk_main();return 0;}static gbooleanopen_file (GtkSourceBuffer *sBuf, const gchar *filename){GtkSourceLanguagesManager *lm;GtkSourceLanguage *language = NULL;GError *err = NULL;gboolean reading;GtkTextIter iter;GIOChannel *io;gchar *buffer;g_return_val_if_fail (sBuf != NULL, FALSE);g_return_val_if_fail (filename != NULL, FALSE);g_return_val_if_fail (GTK_IS_SOURCE_BUFFER (sBuf), FALSE);/* get the Language for C source mimetype */lm = g_object_get_data (G_OBJECT (sBuf), "languages-manager");language = gtk_source_languages_manager_get_language_from_mime_type (lm,"text/x-csrc");//g_print("Language: [%s]\n", gtk_source_language_get_name(language));if (language == NULL){g_print ("No language found for mime type `%s'\n", "text/x-csrc");g_object_set (G_OBJECT (sBuf), "highlight", FALSE, NULL);}else{gtk_source_buffer_set_language (sBuf, language);g_object_set (G_OBJECT (sBuf), "highlight", TRUE, NULL);}/* Now load the file from Disk */io = g_io_channel_new_file (filename, "r", &err);if (!io){g_print("error: %s %s\n", (err)->message, filename);return FALSE;}if (g_io_channel_set_encoding (io, "utf-8", &err) != G_IO_STATUS_NORMAL){g_print("err: Failed to set encoding:\n%s\n%s", filename, (err)->message);return FALSE;}gtk_source_buffer_begin_not_undoable_action (sBuf);//gtk_text_buffer_set_text (GTK_TEXT_BUFFER (sBuf), "", 0);buffer = g_malloc (4096);reading = TRUE;while (reading){gsize bytes_read;GIOStatus status;status = g_io_channel_read_chars (io, buffer, 4096, &bytes_read, &err);switch (status){case G_IO_STATUS_EOF: reading = FALSE;case G_IO_STATUS_NORMAL:if (bytes_read == 0) continue;gtk_text_buffer_get_end_iter ( GTK_TEXT_BUFFER (sBuf), &iter);gtk_text_buffer_insert (GTK_TEXT_BUFFER(sBuf),&iter,buffer,bytes_read);break;case G_IO_STATUS_AGAIN: continue;case G_IO_STATUS_ERROR:default:g_print("err (%s): %s", filename, (err)->message);/* because of error in input we clear already loaded text */gtk_text_buffer_set_text (GTK_TEXT_BUFFER (sBuf), "", 0);reading = FALSE;break;}}g_free (buffer);gtk_source_buffer_end_not_undoable_action (sBuf);g_io_channel_unref (io);if (err){g_error_free (err);return FALSE;}gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (sBuf), FALSE);/* move cursor to the beginning */gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (sBuf), &iter);gtk_text_buffer_place_cursor (GTK_TEXT_BUFFER (sBuf), &iter);g_object_set_data_full (G_OBJECT (sBuf),"filename", g_strdup (filename),(GDestroyNotify) g_free);return TRUE;}I am trying to compile this example, but there is an header missingCode:<gtksourceview/gtksourcelanguagesmanager.h>and I don't understand where it should come fromIt's not included with gtksourceview-2.10.5-r3

author: Nominal Animal
author_rank: Super Contributor
timestamp: February 10, 2022, 05:36:09 pm
content: If I downloadgtksourceview-2.10.5.tar.bz2and configure and build it using./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/varmakeand installing it under/tmp/gtksourceview2instead of root,make DESTDIR=/tmp/gtksourceview2 installdoes provide files/tmp/gtksourceview2/usr/include/gtksourceview-2.0/gtksourceview/gtksourcelanguage.h/tmp/gtksourceview2/usr/include/gtksourceview-2.0/gtksourceview/gtksourcelanguagemanager.hwhich indicates that they should be present. Note that if installed,pkg-config --cflags gtksourceview-2.0should include-I/usr/include/gtksourceview-2.0in its output, so that e.g.#include <gtksourceview/gtksourcelanguagemanager.h>includes/usr/include/gtksourceview-2.0/gtksourceview/gtksourcelanguagemanager.h.After sed'ings/languagesmanager/languagemanager/g; s/languages_manager/language_manager/g; s/languages-manager/language-manager/g; s/LanguagesManager/LanguageManager/gand changing lines 76-81 intolm = gtk_source_language_manager_get_default();language = gtk_source_language_manager_get_language(lm, "c");the example works. I usedgcc -Wall -O2 `pkg-config --cflags gtk+-2.0 gtksourceview-2.0` ex.c `pkg-config --libs gtk+-2.0 gtksourceview-2.0` -o exnoting that both gtk+ and gtksourceview do install the relevant pkg-config .pc files in/usr/lib/pkgconfig/if using the above configuration. A multilib installation like Debian derivatives prefer--libdir=/usr/lib/hardware-archinstead.

author: DiTBho
author_rank: Super Contributor
timestamp: February 10, 2022, 08:37:49 pm
content: That's what emerge fetches when I emerge x11-libs/gtksourceview:2 -> gtksourceview-2.10.5-r3.It emerges correctly, and applications like "xpad" work correctly with that dependency, but there is any "gtksourcelanguagemanager.h" installed.Listing files from "gtksourceview-2.10.5.tar.bz2" I can see the fileCode:tar -tjf gtksourceview-2.10.5.tar.bz2 | grep "gtksourcelanguagemanager.h"gtksourceview-2.10.5/gtksourceview/gtksourcelanguagemanager.hso it's in the source archive, but not installed.Code:find /usr/include/gtksourceview-2.0/gtksourceview/ | grep gtksourcelanguagemanager.hThat's a mystery ... or probably a bug?!?I am going to check with a 2022 portage, the one I am working with is a 2021 one.

author: DiTBho
author_rank: Super Contributor
timestamp: February 10, 2022, 08:49:00 pm
content: Just tried gtksourceview-2.10.5-r4Something starts moving: gtksourcelanguagesmanager.h is still not installed, but now there is a new file gtksourcelanguagemanager.hgtksourcelanguagesmanager.hgtksourcelanguagemanager.h

author: DiTBho
author_rank: Super Contributor
timestamp: February 10, 2022, 08:58:20 pm
content: Code:grep "gtk_source_languages_manager_get_language_from_mime_type" /usr/lib/libgtksourceview-*Not compiled, it's not in the library

author: DiTBho
author_rank: Super Contributor
timestamp: February 10, 2022, 09:05:36 pm
content: Code:strings /usr/lib/libgtksourceview-2.0.so | grep language_managergtk_source_language_manager_get_languagegtk_source_language_manager_get_typegtk_source_language_manager_newgtk_source_language_manager_get_defaultgtk_source_language_manager_set_search_pathgtk_source_language_manager_get_search_pathgtk_source_language_manager_get_language_idsgtk_source_language_manager_guess_language_gtk_source_language_get_language_managergtk_source_language_manager_guess_languagegtk_source_language_manager_get_languagegtk_source_language_manager_get_language_ids_gtk_source_language_manager_get_rng_filegtk_source_language_manager_get_search_pathgtk_source_language_manager_set_search_pathit looks a mess. OK

author: DiTBho
author_rank: Super Contributor
timestamp: February 10, 2022, 09:31:31 pm
content: Code:#include <gtk/gtk.h>#include <gtksourceview/gtksourceview.h>#include <gtksourceview/gtksourcebuffer.h>#include <gtksourceview/gtksourcelanguage.h>#include <gtksourceview/gtksourcelanguagemanager.h>static gboolean open_file (GtkSourceBuffer *sBuf, const gchar *filename);void foo(){GtkSourceLanguageManager *manager;GtkSourceLanguage *language;GtkTextBuffer *buffer;char value[]="c";manager = gtk_source_language_manager_get_default();language = gtk_source_language_manager_get_language (manager, value);}int main( int argc, char *argv[] ){static GtkWidget *window, *pScrollWin, *sView;PangoFontDescription *font_desc;GtkSourceLanguageManager *manager;GtkSourceBuffer *sBuf;/* Create a Window. */gtk_init (&argc, &argv);window = gtk_window_new (GTK_WINDOW_TOPLEVEL);g_signal_connect (G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);gtk_container_set_border_width (GTK_CONTAINER (window), 10);gtk_window_set_default_size (GTK_WINDOW(window), 660, 500);gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);/* Create a Scrolled Window that will contain the GtkSourceView */pScrollWin = gtk_scrolled_window_new (NULL, NULL);gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (pScrollWin),GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);/* Now create a GtkSourceLanguageManager */manager = gtk_source_language_manager_new();/* and a GtkSourceBuffer to hold text (similar to GtkTextBuffer) */sBuf = GTK_SOURCE_BUFFER (gtk_source_buffer_new (NULL));g_object_ref (manager);g_object_set_data_full ( G_OBJECT (sBuf), "language-manager",manager, (GDestroyNotify) g_object_unref);/* Create the GtkSourceView and associate it with the buffer */sView = gtk_source_view_new_with_buffer(sBuf);/* Set default Font name,size */font_desc = pango_font_description_from_string ("mono 8");gtk_widget_modify_font (sView, font_desc);pango_font_description_free (font_desc);/* Attach the GtkSourceView to the scrolled Window */gtk_container_add (GTK_CONTAINER (pScrollWin), GTK_WIDGET (sView));/* And the Scrolled Window to the main Window */gtk_container_add (GTK_CONTAINER (window), pScrollWin);gtk_widget_show_all (pScrollWin);/* Finally load an example file to see how it works */open_file (sBuf, "guineapig.c");gtk_widget_show (window);gtk_main();return 0;}static gbooleanopen_file (GtkSourceBuffer *sBuf, const gchar *filename){GtkSourceLanguageManager *manager;GtkSourceLanguage *language = NULL;GError *err = NULL;gboolean reading;GtkTextIter iter;GIOChannel *io;gchar *buffer;g_return_val_if_fail (sBuf != NULL, FALSE);g_return_val_if_fail (filename != NULL, FALSE);g_return_val_if_fail (GTK_IS_SOURCE_BUFFER (sBuf), FALSE);/* get the Language for C source mimetype */manager = g_object_get_data (G_OBJECT (sBuf), "language-manager");language = gtk_source_language_manager_get_language (manager, "c");//language = gtk_source_language_manager_get_language_from_mime_type (manager, "text/x-csrc");//g_print("Language: [%s]\n", gtk_source_language_get_name(language));if (language == NULL){g_print ("No language found for mime type `%s'\n", "text/x-csrc");g_object_set (G_OBJECT (sBuf), "highlight", FALSE, NULL);}else{gtk_source_buffer_set_language (sBuf, language);g_object_set (G_OBJECT (sBuf), "highlight", TRUE, NULL);}/* Now load the file from Disk */io = g_io_channel_new_file (filename, "r", &err);if (!io){g_print("error: %s %s\n", (err)->message, filename);return FALSE;}if (g_io_channel_set_encoding (io, "utf-8", &err) != G_IO_STATUS_NORMAL){g_print("err: Failed to set encoding:\n%s\n%s", filename, (err)->message);return FALSE;}gtk_source_buffer_begin_not_undoable_action (sBuf);//gtk_text_buffer_set_text (GTK_TEXT_BUFFER (sBuf), "", 0);buffer = g_malloc (4096);reading = TRUE;while (reading){gsize bytes_read;GIOStatus status;status = g_io_channel_read_chars (io, buffer, 4096, &bytes_read, &err);switch (status){case G_IO_STATUS_EOF: reading = FALSE;case G_IO_STATUS_NORMAL:if (bytes_read == 0) continue;gtk_text_buffer_get_end_iter ( GTK_TEXT_BUFFER (sBuf), &iter);gtk_text_buffer_insert (GTK_TEXT_BUFFER(sBuf),&iter,buffer,bytes_read);break;case G_IO_STATUS_AGAIN: continue;case G_IO_STATUS_ERROR:default:g_print("err (%s): %s", filename, (err)->message);/* because of error in input we clear already loaded text */gtk_text_buffer_set_text (GTK_TEXT_BUFFER (sBuf), "", 0);reading = FALSE;break;}}g_free (buffer);gtk_source_buffer_end_not_undoable_action (sBuf);g_io_channel_unref (io);if (err){g_error_free (err);return FALSE;}gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (sBuf), FALSE);/* move cursor to the beginning */gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (sBuf), &iter);gtk_text_buffer_place_cursor (GTK_TEXT_BUFFER (sBuf), &iter);g_object_set_data_full (G_OBJECT (sBuf),"filename", g_strdup (filename),(GDestroyNotify) g_free);return TRUE;}Code:/usr/include/gtksourceview-2.0/usr/include/gtk-2.0/usr/lib/gtk-2.0/include/usr/include/pango-1.0/usr/include/glib-2.0/usr/lib/glib-2.0/include/usr/lib/libffi/include/usr/include/fribidi/usr/include/cairo/usr/include/pixman-1/usr/include/freetype2/usr/include/uuid/usr/include/libpng16/usr/include/harfbuzz/usr/include/gdk-pixbuf-2.0/usr/include/libmount/usr/include/blkid/usr/include/atk-1.0/usr/include/libxml2(dep headers)Code:gtksourceview-2.0gtk-x11-2.0gdk-x11-2.0pangocairo-1.0atk-1.0cairogdk_pixbuf-2.0gio-2.0pangoft2-1.0pango-1.0gobject-2.0glib-2.0fontconfigfreetype(dep libraries)This compiles and worksI renamed "languages" into "language", and changed this lineCode:language = gtk_source_language_manager_get_language (manager, "c");//language = gtk_source_language_manager_get_language_from_mime_type (manager, "text/x-csrc");I don't know what's wrong, it needs more investigation, but I have other priorities at the moment

author: PKTKS
author_rank: Super Contributor
timestamp: February 11, 2022, 10:50:59 am
content: You have a bad crippled installed package...I will not be surprised if this kind of poisoned thing happens over here and there...More and more these common repos have been poisoned by 3rd part interestsand unreliable packed distros made to oversimplify deployments.Reason why I have been using my own trusted forked LFS builds.. more and more..From my own pack manager I can tell you that the HEADER is incorrectly installed..Probably the whole thing is bad..Get a fresh one as Nominal pointed you...build a sane safe from sources..Correct header tree attached, shot by my own pack manager toolthat tracks my LFS builds.. in PERL::CursesPaul

author: DiTBho
author_rank: Super Contributor
timestamp: February 11, 2022, 11:23:29 am
content: Quote from: PKTKS on February 11, 2022, 10:50:59 amGentoo has problems, we know, but the problem here seems only related to gtksourceview2.*every* package I tried to emerge with gtksourceview2 dependency worked out of the box.e.g.xpad needs gtksourceview2: it emerges, and it works, without any issue!geany needs gtksourceview2: it emerges, and it works, without any issue!The problem arose yesterday with an example (the source in my first post) I found in public gtk2 documentation.With gentoo, well ... there was an issue with the slot (gtksourceview2 -> slot2, gtksourceview2 -> slot3) which caused a collision between them; my portage is one 400 days old, I cannot sync because I have special support for Ada (my Overlays have dependencies with the current portage eclasses, a thing that is "updated" when you update the portage) and it would for sure break with a sync, so I sorted it out manually the defect.Talking about official portage, the slot-issue got sorted out one years ago and it's currently sorted out in current 2022 portagesSo, this issue is solved, but there is now something wrong with the function names of the libgtksourceview2 dynamic library. I don't understand why, the ebuild script looks correct.

author: PKTKS
author_rank: Super Contributor
timestamp: February 11, 2022, 11:46:55 am
content: if thesourcegot poisoned or crippled somehow it wont matter if you build or rebuild with any distro.. the bottom line is having a reliable source to build from...This has always been a problem.. eventually you can retrieve a proper build release..IMHO as far as reliable is concerned plain Debian repos have far more alternatives..which ultimately lead to being more difficult to poison or just screw the sources..they are spread over educational institutions and nor exclusive profit corps..(which not prevent rsync clones to spread poisoned.... but more difficult it is)And as more and more of theInternetis being privatized..and more and more "linux friends" corps (fill blanks) are taking over the code..buying assets and promoting their own products...you got the pointPaul

author: DiTBho
author_rank: Super Contributor
timestamp: February 11, 2022, 12:04:18 pm
content: gtksourceview/gtksourcelanguagesmanager.hgtksourceview/gtksourcelanguagemanager.h"languageS" instead of "language", why should it be plural? that's the first question

author: PKTKS
author_rank: Super Contributor
timestamp: February 11, 2022, 12:06:41 pm
content: Quote from: DiTBho on February 11, 2022, 12:04:18 pmObviously some smart ass crippled the source...for fun for just the sake of messing everybody else's life ...whatever..the world is full of irrational ones..do not expect otherwisePaul

author: Nominal Animal
author_rank: Super Contributor
timestamp: February 11, 2022, 01:29:06 pm
content: Quote from: DiTBho on February 11, 2022, 12:04:18 pmDoing a deep dive inofficial gtksourceview archives, the singular name has been valid sincegtksourceview-2.0.0(2007).Before that, gtksourceview-0.x and gtksourceview-1.x used the plural.So, if you see<gtksourceview/gtksourcelanguagesmanager.h>, you're looking at code written for GtkSourceView-0 or GtkSourceView-1 (at least fourteen years ago), and not for GtkSourceView-2 (which works for Gtk+ version 2.0).(There are also GtkSourceView-3 for Gtk+ 3, and GtkSourceView-4 for Gtk+ 4. These major versions are not compatible with each other; you must pair Gtk+ major version with same GtkSourceView version.)(I find the old documentation (developer-old.gnome.org) better than the new one (developer.gnome.org); seeGtk+ 2,GtkSourceView-2,Gtk+ 3,GtkSourceView-3.22/3.24,Gtk+ 4,GtkSourceView-4.2, for example.)No nefarious business afoot, just an old, old API change in GtkSourceView.

author: PKTKS
author_rank: Super Contributor
timestamp: February 11, 2022, 01:51:29 pm
content: lets hope for the best...prepare for the nastiest...WTF a 14yo piece of code is doing there....Paul

author: DiTBho
author_rank: Super Contributor
timestamp: February 11, 2022, 02:24:01 pm
content: I'm not a fan of GnomeToolKit in general, nor do I like the KDE alternative.Somehow I like Motif because it is simpler and runs on my old TekX terminal.Anyway, Scintilla and GTKSourceView are really powerful and do a great jobI've never seriously programmed anything with them, so I have these kind of silly doubts and made the mistake of reading the wrong manual.Also, GTK, GTK2, GTK3 are a bit confusing about the portage, but this is the Gentoo way to handle them.x11-libs/gtksourceview:2 ->2.10.5-r4 -> { /usr/lib/libgtk-2.so, /usr/include/gtksourceview-2.0 }x11-libs/gtksourceview:3 ->3.24.11 -> { /usr/lib/libgtk-3.so, /usr/include/gtksourceview-3.0 }x11-libs/gtksourceview:4 ->4.6.1-r1 -> { /usr/lib/libgtk-4.so, /usr/include/gtksourceview-4 }If I were them, I would have supported gtk, gtk2 and gtk3 in three dedicated ebuilds, one for eache.g.x11-libs/gtksourceview2x11-libs/gtksourceview3x11-libs/gtksourceview4instead of grouping them all in the same (x11-libs/gtksourceview) ebuild with a SLOT to select.I spent three days on this type of problem because it was buggy and (also due to my ignorance with gtk) I didn't understand what was going on under the shell.

author: Nominal Animal
author_rank: Super Contributor
timestamp: February 11, 2022, 04:30:44 pm
content: Quote from: DiTBho on February 11, 2022, 02:24:01 pmExactly. That also lets them co-exist simultaneously, like they do on e.g. Debian, which usesgtksourceview2,gtksourceview3, andgtksourceview4for the source packages. This is important for compatibility, since the major versions are not downwards (binary or API) compatible.Quote from: DiTBho on February 11, 2022, 02:24:01 pmI wasn't aware of it either, until I started looking into it; I didn't even catch the plural at first.When I realized the plural issue, I did some web searching (dammit, DuckDuckGo no longer does verbatim searches!), and noticed both singular and plural exist on the web.The dates on the pages hinted that perhaps the plural was old, and singular current; an API change.Based on that, I started looking at the gtksourceview release tarballs, and discovered what I posted above.