test_dnd.mod 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. MODULE test_dnd ;
  2. (*
  3. m2-GTK4 test - drag and drop controllers and the clipboard.
  4. Constructs a GtkDropTarget and a GtkDragSource, adds them to a widget,
  5. then round-trips text through the clipboard asynchronously.
  6. Requires a display.
  7. *)
  8. FROM Gio IMPORT g_application_run, g_application_quit;
  9. FROM GtkApplication IMPORT gtk_application_new;
  10. FROM GtkWindow IMPORT gtk_application_window_new, gtk_window_set_child,
  11. gtk_window_present;
  12. FROM GtkBox IMPORT gtk_box_new, gtk_box_append;
  13. FROM GtkEnums IMPORT GtkVertical;
  14. FROM GtkLabel IMPORT gtk_label_new;
  15. FROM GtkWidget IMPORT gtk_widget_add_controller;
  16. FROM GtkDropTarget IMPORT gtk_drop_target_new, gtk_drop_target_set_actions,
  17. gtk_drop_target_get_actions;
  18. FROM GtkDragSource IMPORT gtk_drag_source_new, gtk_drag_source_set_actions,
  19. gtk_drag_source_get_actions;
  20. FROM GObject IMPORT g_type_from_name;
  21. FROM Gdk IMPORT gdk_display_get_default, gdk_display_get_clipboard,
  22. GdkActionCopy;
  23. FROM GdkClipboard IMPORT gdk_clipboard_set_text,
  24. gdk_clipboard_read_text_async, gdk_clipboard_read_text_finish;
  25. FROM GError IMPORT GError;
  26. FROM GtkUtils IMPORT Connect, CStrToM2, Free, ClearError;
  27. FROM GLib IMPORT g_timeout_add, GSourceRemove;
  28. FROM SYSTEM IMPORT ADDRESS, ADR;
  29. FROM libc IMPORT printf;
  30. CONST
  31. AppId = "org.example.m2gtk4.TestDnd";
  32. Expected = "m2-clipboard";
  33. VAR
  34. app, clipboard: ADDRESS;
  35. passed, gotText: BOOLEAN;
  36. buf: ARRAY [0..63] OF CHAR;
  37. (* void on_read(GObject *source, GAsyncResult *result, gpointer user) *)
  38. PROCEDURE OnRead (source: ADDRESS; result: ADDRESS; user: ADDRESS);
  39. VAR
  40. s: ADDRESS;
  41. err: GError;
  42. BEGIN
  43. err := NIL;
  44. s := gdk_clipboard_read_text_finish(clipboard, result, err);
  45. IF s = NIL THEN
  46. printf("clipboard read failed\n");
  47. ClearError(err);
  48. passed := FALSE
  49. ELSE
  50. CStrToM2(s, buf);
  51. gotText := TRUE;
  52. IF (buf[0] # 'm') OR (buf[2] # '-') THEN
  53. printf("clipboard text wrong: [%s]\n", buf);
  54. passed := FALSE
  55. END;
  56. Free(s)
  57. END;
  58. g_application_quit(app)
  59. END OnRead;
  60. PROCEDURE Fallback (data: ADDRESS) : INTEGER;
  61. BEGIN
  62. IF NOT gotText THEN
  63. printf("clipboard callback did not fire\n");
  64. passed := FALSE
  65. END;
  66. g_application_quit(app);
  67. RETURN GSourceRemove
  68. END Fallback;
  69. PROCEDURE OnActivate (application: ADDRESS; data: ADDRESS);
  70. VAR
  71. window, box, drop, drag, label: ADDRESS;
  72. gtype: LONGCARD;
  73. ok: BOOLEAN;
  74. BEGIN
  75. ok := TRUE;
  76. gotText := FALSE;
  77. gtype := g_type_from_name("gchararray");
  78. drop := gtk_drop_target_new(gtype, GdkActionCopy);
  79. IF drop = NIL THEN printf("drop target NIL\n"); ok := FALSE END;
  80. gtk_drop_target_set_actions(drop, GdkActionCopy);
  81. IF gtk_drop_target_get_actions(drop) # GdkActionCopy THEN
  82. printf("drop actions wrong\n"); ok := FALSE
  83. END;
  84. drag := gtk_drag_source_new();
  85. gtk_drag_source_set_actions(drag, GdkActionCopy);
  86. IF gtk_drag_source_get_actions(drag) # GdkActionCopy THEN
  87. printf("drag actions wrong\n"); ok := FALSE
  88. END;
  89. label := gtk_label_new("drag/drop target");
  90. gtk_widget_add_controller(label, drop);
  91. gtk_widget_add_controller(label, drag);
  92. clipboard := gdk_display_get_clipboard(gdk_display_get_default());
  93. gdk_clipboard_set_text(clipboard, Expected);
  94. gdk_clipboard_read_text_async(clipboard, NIL, ADR(OnRead), NIL);
  95. box := gtk_box_new(GtkVertical, 4);
  96. gtk_box_append(box, label);
  97. window := gtk_application_window_new(application);
  98. gtk_window_set_child(window, box);
  99. gtk_window_present(window);
  100. passed := ok;
  101. g_timeout_add(2000, Fallback, NIL)
  102. END OnActivate;
  103. VAR
  104. rc: INTEGER;
  105. BEGIN
  106. passed := FALSE;
  107. app := gtk_application_new(AppId, 0);
  108. IF app = NIL THEN
  109. printf("test_dnd: FAIL (no app)\n");
  110. HALT(1)
  111. END;
  112. Connect(app, "activate", ADR(OnActivate), NIL);
  113. rc := g_application_run(app, 0, NIL);
  114. IF passed THEN
  115. printf("test_dnd: PASS\n");
  116. HALT(0)
  117. ELSE
  118. printf("test_dnd: FAIL\n");
  119. HALT(1)
  120. END
  121. END test_dnd.