richtext.mod 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. MODULE richtext ;
  2. (*
  3. m2-GTK4 example - text extras.
  4. A GtkTextView whose buffer is filled with Pango markup (bold/colour),
  5. with a named mark used to scroll back to the top, plus a GtkEntry with
  6. an attached GtkEntryCompletion.
  7. Build: make examples
  8. Run: ./build/examples/richtext
  9. *)
  10. FROM Gio IMPORT g_application_run;
  11. FROM GtkApplication IMPORT gtk_application_new;
  12. FROM GtkWindow IMPORT gtk_application_window_new, gtk_window_set_title,
  13. gtk_window_set_default_size, gtk_window_set_child, gtk_window_present;
  14. FROM GtkBox IMPORT gtk_box_new, gtk_box_append, gtk_box_set_spacing;
  15. FROM GtkEnums IMPORT GtkVertical, GtkWrapWordChar;
  16. FROM GtkScrolledWindow IMPORT gtk_scrolled_window_new,
  17. gtk_scrolled_window_set_child;
  18. FROM GtkTextView IMPORT gtk_text_view_new, gtk_text_view_get_buffer,
  19. gtk_text_view_set_wrap_mode, gtk_text_view_set_monospace,
  20. gtk_text_view_scroll_to_mark;
  21. FROM GtkTextBuffer IMPORT gtk_text_buffer_get_start_iter,
  22. gtk_text_buffer_create_mark, gtk_text_buffer_insert_markup;
  23. FROM GtkTextIter IMPORT GtkTextIter;
  24. FROM GtkEntry IMPORT gtk_entry_new, gtk_entry_set_completion;
  25. FROM GtkEntryCompletion IMPORT gtk_entry_completion_new,
  26. gtk_entry_completion_set_text_column,
  27. gtk_entry_completion_set_minimum_key_length,
  28. gtk_entry_completion_set_popup_completion;
  29. FROM GtkWidget IMPORT gtk_widget_set_vexpand;
  30. FROM GtkClosures IMPORT Connect2;
  31. FROM SYSTEM IMPORT ADDRESS, ADR;
  32. FROM libc IMPORT printf;
  33. CONST
  34. AppId = "org.example.m2gtk4.RichText";
  35. Markup = "<b>Rich text</b> from GNU Modula-2 - <span foreground='#e01b24'>red</span>, <i>italic</i> and <tt>monospace</tt>.";
  36. PROCEDURE OnActivate (application: ADDRESS; data: ADDRESS);
  37. VAR
  38. window, box, sw, view, buffer, entry, completion, mark: ADDRESS;
  39. start: GtkTextIter;
  40. BEGIN
  41. view := gtk_text_view_new();
  42. gtk_text_view_set_wrap_mode(view, GtkWrapWordChar);
  43. gtk_text_view_set_monospace(view, 0);
  44. buffer := gtk_text_view_get_buffer(view);
  45. gtk_text_buffer_get_start_iter(buffer, start);
  46. gtk_text_buffer_insert_markup(buffer, start, Markup, -1);
  47. mark := gtk_text_buffer_create_mark(buffer, "top", start, 1);
  48. gtk_text_view_scroll_to_mark(view, mark, 0.0, 0, 0.0, 0.0);
  49. entry := gtk_entry_new();
  50. completion := gtk_entry_completion_new();
  51. gtk_entry_completion_set_text_column(completion, 0);
  52. gtk_entry_completion_set_minimum_key_length(completion, 2);
  53. gtk_entry_completion_set_popup_completion(completion, 1);
  54. gtk_entry_set_completion(entry, completion);
  55. sw := gtk_scrolled_window_new();
  56. gtk_scrolled_window_set_child(sw, view);
  57. gtk_widget_set_vexpand(sw, 1);
  58. box := gtk_box_new(GtkVertical, 8);
  59. gtk_box_set_spacing(box, 8);
  60. gtk_box_append(box, entry);
  61. gtk_box_append(box, sw);
  62. window := gtk_application_window_new(application);
  63. gtk_window_set_title(window, "m2-GTK4 Rich Text");
  64. gtk_window_set_default_size(window, 460, 260);
  65. gtk_window_set_child(window, box);
  66. gtk_window_present(window)
  67. END OnActivate;
  68. VAR
  69. app: ADDRESS;
  70. BEGIN
  71. app := gtk_application_new(AppId, 0);
  72. IF app = NIL THEN
  73. printf("gtk_application_new failed\n");
  74. HALT(1)
  75. END;
  76. Connect2(app, "activate", OnActivate, NIL);
  77. g_application_run(app, 0, NIL)
  78. END richtext.