MODULE test_sourceview ; (* m2-GTK4 test - GtkSourceView 5 (GtkSource namespace): buffer, view, language/style-scheme managers, marks, map, space drawer, search, region, snippets, completion and the print compositor. Requires a display. *) FROM GtkApplication IMPORT gtk_application_new; FROM Gio IMPORT g_application_run, g_application_quit; FROM GtkWindow IMPORT gtk_application_window_new, gtk_window_set_child, gtk_window_present; FROM GtkTextBuffer IMPORT gtk_text_buffer_get_start_iter; FROM GtkTextIter IMPORT GtkTextIter; FROM GtkUtils IMPORT Connect; FROM GtkSourceFunctions IMPORT gtk_source_init; FROM GtkSourceBuffer IMPORT gtk_source_buffer_new, gtk_source_buffer_new_with_language, gtk_source_buffer_set_language, gtk_source_buffer_get_language, gtk_source_buffer_set_highlight_syntax, gtk_source_buffer_get_highlight_syntax; FROM GtkSourceLanguageManager IMPORT gtk_source_language_manager_get_default, gtk_source_language_manager_get_language; FROM GtkSourceStyleSchemeManager IMPORT gtk_source_style_scheme_manager_get_default, gtk_source_style_scheme_manager_get_scheme_ids; FROM GtkSourceView IMPORT gtk_source_view_new_with_buffer, gtk_source_view_set_show_line_numbers, gtk_source_view_get_show_line_numbers, gtk_source_view_set_tab_width, gtk_source_view_get_tab_width, gtk_source_view_get_gutter, gtk_source_view_get_completion, gtk_source_view_get_space_drawer; FROM GtkSourceMark IMPORT gtk_source_mark_new; FROM GtkSourceGutter IMPORT gtk_source_gutter_insert; FROM GtkSourceGutterRendererText IMPORT gtk_source_gutter_renderer_text_new; FROM GtkSourceMap IMPORT gtk_source_map_new, gtk_source_map_set_view; FROM GtkSourceSpaceDrawer IMPORT gtk_source_space_drawer_set_types_for_locations, gtk_source_space_drawer_get_types_for_locations; FROM GtkSourceEnums IMPORT GtkSourceSpaceLocationAll, GtkSourceSpaceTypeAll; FROM GtkSourceSearchSettings IMPORT gtk_source_search_settings_new, gtk_source_search_settings_set_search_text; FROM GtkSourceSearchContext IMPORT gtk_source_search_context_new, gtk_source_search_context_forward; FROM GtkSourceRegion IMPORT gtk_source_region_new; FROM GtkSourceSnippet IMPORT gtk_source_snippet_new_parsed; FROM GtkSourceCompletionWords IMPORT gtk_source_completion_words_new; FROM GtkSourcePrintCompositor IMPORT gtk_source_print_compositor_new; FROM SYSTEM IMPORT ADDRESS, ADR; FROM libc IMPORT printf; CONST AppId = "org.example.m2gtk4.TestSourceView"; VAR app: ADDRESS; passed: BOOLEAN; failures: INTEGER; PROCEDURE Check (cond: BOOLEAN; what: ARRAY OF CHAR); BEGIN IF cond THEN printf(" ok - %s\n", what) ELSE printf(" NOT OK - %s\n", what); INC(failures) END END Check; PROCEDURE OnActivate (application: ADDRESS; data: ADDRESS); VAR window, buffer, view, lang, gutter, renderer, map, drawer: ADDRESS; settings, search, region, snippet, words, compositor: ADDRESS; start, match_start, match_end: GtkTextIter; wrapped: INTEGER; found: INTEGER; BEGIN failures := 0; window := gtk_application_window_new(application); (* managers *) Check(gtk_source_language_manager_get_default() # NIL, "GtkSourceLanguageManager"); Check(gtk_source_style_scheme_manager_get_default() # NIL, "GtkSourceStyleSchemeManager"); buffer := gtk_source_buffer_new(NIL); Check(buffer # NIL, "GtkSourceBuffer"); gtk_source_buffer_set_highlight_syntax(buffer, 1); Check(gtk_source_buffer_get_highlight_syntax(buffer) # 0, "highlight syntax"); lang := gtk_source_language_manager_get_language (gtk_source_language_manager_get_default(), "c"); IF lang # NIL THEN gtk_source_buffer_set_language(buffer, lang); Check(gtk_source_buffer_get_language(buffer) = lang, "set language") ELSE Check(TRUE, "language 'c' not installed (skipped)") END; view := gtk_source_view_new_with_buffer(buffer); Check(view # NIL, "GtkSourceView"); gtk_source_view_set_show_line_numbers(view, 1); Check(gtk_source_view_get_show_line_numbers(view) # 0, "line numbers"); gtk_source_view_set_tab_width(view, 4); Check(gtk_source_view_get_tab_width(view) = 4, "tab width"); (* mark *) Check(gtk_source_mark_new("bookmark", "category") # NIL, "GtkSourceMark"); (* gutter + renderer *) gutter := gtk_source_view_get_gutter(view, 3); (* GTK_TEXT_WINDOW_LEFT *) renderer := gtk_source_gutter_renderer_text_new(); Check(gtk_source_gutter_insert(gutter, renderer, 0) # 0, "gutter renderer"); (* map *) map := gtk_source_map_new(); gtk_source_map_set_view(map, view); Check(map # NIL, "GtkSourceMap"); (* space drawer *) drawer := gtk_source_view_get_space_drawer(view); gtk_source_space_drawer_set_types_for_locations(drawer, GtkSourceSpaceLocationAll, GtkSourceSpaceTypeAll); Check(gtk_source_space_drawer_get_types_for_locations(drawer, GtkSourceSpaceLocationAll) = GtkSourceSpaceTypeAll, "SpaceDrawer types"); (* search *) settings := gtk_source_search_settings_new(); gtk_source_search_settings_set_search_text(settings, "hello"); search := gtk_source_search_context_new(buffer, settings); Check(search # NIL, "GtkSourceSearchContext"); gtk_text_buffer_get_start_iter(buffer, start); found := gtk_source_search_context_forward(search, start, match_start, match_end, wrapped); Check(found = 0, "search finds nothing in empty buffer"); (* region *) region := gtk_source_region_new(buffer); Check(region # NIL, "GtkSourceRegion"); (* snippet *) snippet := gtk_source_snippet_new_parsed("Hello ${1:world}"); Check(snippet # NIL, "GtkSourceSnippet"); (* completion provider *) words := gtk_source_completion_words_new("Document Words"); Check(words # NIL, "GtkSourceCompletionWords"); (* print compositor *) compositor := gtk_source_print_compositor_new(buffer); Check(compositor # NIL, "GtkSourcePrintCompositor"); gtk_window_set_child(window, view); gtk_window_present(window); passed := (failures = 0); g_application_quit(application) END OnActivate; VAR rc: INTEGER; BEGIN passed := FALSE; failures := 0; gtk_source_init(); app := gtk_application_new(AppId, 0); IF app = NIL THEN printf("test_sourceview: FAIL (no app)\n"); HALT(1) END; Connect(app, "activate", ADR(OnActivate), NIL); rc := g_application_run(app, 0, NIL); IF passed THEN printf("test_sourceview: PASS\n"); HALT(0) ELSE printf("test_sourceview: FAIL\n"); HALT(1) END END test_sourceview.