test_sourceview.mod 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. MODULE test_sourceview ;
  2. (*
  3. m2-GTK4 test - GtkSourceView 5 (GtkSource namespace): buffer, view,
  4. language/style-scheme managers, marks, map, space drawer, search,
  5. region, snippets, completion and the print compositor.
  6. Requires a display.
  7. *)
  8. FROM GtkApplication IMPORT gtk_application_new;
  9. FROM Gio IMPORT g_application_run, g_application_quit;
  10. FROM GtkWindow IMPORT gtk_application_window_new, gtk_window_set_child,
  11. gtk_window_present;
  12. FROM GtkTextBuffer IMPORT gtk_text_buffer_get_start_iter;
  13. FROM GtkTextIter IMPORT GtkTextIter;
  14. FROM GtkUtils IMPORT Connect;
  15. FROM GtkSourceFunctions IMPORT gtk_source_init;
  16. FROM GtkSourceBuffer IMPORT gtk_source_buffer_new,
  17. gtk_source_buffer_new_with_language, gtk_source_buffer_set_language,
  18. gtk_source_buffer_get_language, gtk_source_buffer_set_highlight_syntax,
  19. gtk_source_buffer_get_highlight_syntax;
  20. FROM GtkSourceLanguageManager IMPORT gtk_source_language_manager_get_default,
  21. gtk_source_language_manager_get_language;
  22. FROM GtkSourceStyleSchemeManager IMPORT
  23. gtk_source_style_scheme_manager_get_default,
  24. gtk_source_style_scheme_manager_get_scheme_ids;
  25. FROM GtkSourceView IMPORT gtk_source_view_new_with_buffer,
  26. gtk_source_view_set_show_line_numbers,
  27. gtk_source_view_get_show_line_numbers, gtk_source_view_set_tab_width,
  28. gtk_source_view_get_tab_width, gtk_source_view_get_gutter,
  29. gtk_source_view_get_completion, gtk_source_view_get_space_drawer;
  30. FROM GtkSourceMark IMPORT gtk_source_mark_new;
  31. FROM GtkSourceGutter IMPORT gtk_source_gutter_insert;
  32. FROM GtkSourceGutterRendererText IMPORT
  33. gtk_source_gutter_renderer_text_new;
  34. FROM GtkSourceMap IMPORT gtk_source_map_new, gtk_source_map_set_view;
  35. FROM GtkSourceSpaceDrawer IMPORT gtk_source_space_drawer_set_types_for_locations,
  36. gtk_source_space_drawer_get_types_for_locations;
  37. FROM GtkSourceEnums IMPORT GtkSourceSpaceLocationAll,
  38. GtkSourceSpaceTypeAll;
  39. FROM GtkSourceSearchSettings IMPORT gtk_source_search_settings_new,
  40. gtk_source_search_settings_set_search_text;
  41. FROM GtkSourceSearchContext IMPORT gtk_source_search_context_new,
  42. gtk_source_search_context_forward;
  43. FROM GtkSourceRegion IMPORT gtk_source_region_new;
  44. FROM GtkSourceSnippet IMPORT gtk_source_snippet_new_parsed;
  45. FROM GtkSourceCompletionWords IMPORT gtk_source_completion_words_new;
  46. FROM GtkSourcePrintCompositor IMPORT gtk_source_print_compositor_new;
  47. FROM SYSTEM IMPORT ADDRESS, ADR;
  48. FROM libc IMPORT printf;
  49. CONST
  50. AppId = "org.example.m2gtk4.TestSourceView";
  51. VAR
  52. app: ADDRESS;
  53. passed: BOOLEAN;
  54. failures: INTEGER;
  55. PROCEDURE Check (cond: BOOLEAN; what: ARRAY OF CHAR);
  56. BEGIN
  57. IF cond THEN
  58. printf(" ok - %s\n", what)
  59. ELSE
  60. printf(" NOT OK - %s\n", what);
  61. INC(failures)
  62. END
  63. END Check;
  64. PROCEDURE OnActivate (application: ADDRESS; data: ADDRESS);
  65. VAR
  66. window, buffer, view, lang, gutter, renderer, map, drawer: ADDRESS;
  67. settings, search, region, snippet, words, compositor: ADDRESS;
  68. start, match_start, match_end: GtkTextIter;
  69. wrapped: INTEGER;
  70. found: INTEGER;
  71. BEGIN
  72. failures := 0;
  73. window := gtk_application_window_new(application);
  74. (* managers *)
  75. Check(gtk_source_language_manager_get_default() # NIL,
  76. "GtkSourceLanguageManager");
  77. Check(gtk_source_style_scheme_manager_get_default() # NIL,
  78. "GtkSourceStyleSchemeManager");
  79. buffer := gtk_source_buffer_new(NIL);
  80. Check(buffer # NIL, "GtkSourceBuffer");
  81. gtk_source_buffer_set_highlight_syntax(buffer, 1);
  82. Check(gtk_source_buffer_get_highlight_syntax(buffer) # 0,
  83. "highlight syntax");
  84. lang := gtk_source_language_manager_get_language
  85. (gtk_source_language_manager_get_default(), "c");
  86. IF lang # NIL THEN
  87. gtk_source_buffer_set_language(buffer, lang);
  88. Check(gtk_source_buffer_get_language(buffer) = lang, "set language")
  89. ELSE
  90. Check(TRUE, "language 'c' not installed (skipped)")
  91. END;
  92. view := gtk_source_view_new_with_buffer(buffer);
  93. Check(view # NIL, "GtkSourceView");
  94. gtk_source_view_set_show_line_numbers(view, 1);
  95. Check(gtk_source_view_get_show_line_numbers(view) # 0, "line numbers");
  96. gtk_source_view_set_tab_width(view, 4);
  97. Check(gtk_source_view_get_tab_width(view) = 4, "tab width");
  98. (* mark *)
  99. Check(gtk_source_mark_new("bookmark", "category") # NIL, "GtkSourceMark");
  100. (* gutter + renderer *)
  101. gutter := gtk_source_view_get_gutter(view, 3); (* GTK_TEXT_WINDOW_LEFT *)
  102. renderer := gtk_source_gutter_renderer_text_new();
  103. Check(gtk_source_gutter_insert(gutter, renderer, 0) # 0, "gutter renderer");
  104. (* map *)
  105. map := gtk_source_map_new();
  106. gtk_source_map_set_view(map, view);
  107. Check(map # NIL, "GtkSourceMap");
  108. (* space drawer *)
  109. drawer := gtk_source_view_get_space_drawer(view);
  110. gtk_source_space_drawer_set_types_for_locations(drawer,
  111. GtkSourceSpaceLocationAll, GtkSourceSpaceTypeAll);
  112. Check(gtk_source_space_drawer_get_types_for_locations(drawer,
  113. GtkSourceSpaceLocationAll) = GtkSourceSpaceTypeAll,
  114. "SpaceDrawer types");
  115. (* search *)
  116. settings := gtk_source_search_settings_new();
  117. gtk_source_search_settings_set_search_text(settings, "hello");
  118. search := gtk_source_search_context_new(buffer, settings);
  119. Check(search # NIL, "GtkSourceSearchContext");
  120. gtk_text_buffer_get_start_iter(buffer, start);
  121. found := gtk_source_search_context_forward(search, start, match_start,
  122. match_end, wrapped);
  123. Check(found = 0, "search finds nothing in empty buffer");
  124. (* region *)
  125. region := gtk_source_region_new(buffer);
  126. Check(region # NIL, "GtkSourceRegion");
  127. (* snippet *)
  128. snippet := gtk_source_snippet_new_parsed("Hello ${1:world}");
  129. Check(snippet # NIL, "GtkSourceSnippet");
  130. (* completion provider *)
  131. words := gtk_source_completion_words_new("Document Words");
  132. Check(words # NIL, "GtkSourceCompletionWords");
  133. (* print compositor *)
  134. compositor := gtk_source_print_compositor_new(buffer);
  135. Check(compositor # NIL, "GtkSourcePrintCompositor");
  136. gtk_window_set_child(window, view);
  137. gtk_window_present(window);
  138. passed := (failures = 0);
  139. g_application_quit(application)
  140. END OnActivate;
  141. VAR
  142. rc: INTEGER;
  143. BEGIN
  144. passed := FALSE;
  145. failures := 0;
  146. gtk_source_init();
  147. app := gtk_application_new(AppId, 0);
  148. IF app = NIL THEN
  149. printf("test_sourceview: FAIL (no app)\n");
  150. HALT(1)
  151. END;
  152. Connect(app, "activate", ADR(OnActivate), NIL);
  153. rc := g_application_run(app, 0, NIL);
  154. IF passed THEN
  155. printf("test_sourceview: PASS\n");
  156. HALT(0)
  157. ELSE
  158. printf("test_sourceview: FAIL\n");
  159. HALT(1)
  160. END
  161. END test_sourceview.