You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ovdCCommentEditorDialog.cpp 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "ovdCCommentEditorDialog.h"
  2. #include <cstring>
  3. namespace OpenViBE {
  4. namespace Designer {
  5. static void BoldSelectionCB(GtkButton* /*button*/, gpointer data) { static_cast<CCommentEditorDialog*>(data)->applyTagCB("<b>", "</b>"); }
  6. static void ItalicSelectionCB(GtkButton* /*button*/, gpointer data) { static_cast<CCommentEditorDialog*>(data)->applyTagCB("<i>", "</i>"); }
  7. static void UnderlineCB(GtkButton* /*button*/, gpointer data) { static_cast<CCommentEditorDialog*>(data)->applyTagCB("<u>", "</u>"); }
  8. static void StrikethroughCB(GtkButton* /*button*/, gpointer data) { static_cast<CCommentEditorDialog*>(data)->applyTagCB("<s>", "</s>"); }
  9. static void MonoCB(GtkButton* /*button*/, gpointer data) { static_cast<CCommentEditorDialog*>(data)->applyTagCB("<tt>", "</tt>"); }
  10. static void SubscriptCB(GtkButton* /*button*/, gpointer data) { static_cast<CCommentEditorDialog*>(data)->applyTagCB("<sub>", "</sub>"); }
  11. static void SuperscriptCB(GtkButton* /*button*/, gpointer data) { static_cast<CCommentEditorDialog*>(data)->applyTagCB("<sup>", "</sup>"); }
  12. static void BigCB(GtkButton* /*button*/, gpointer data) { static_cast<CCommentEditorDialog*>(data)->applyTagCB("<big>", "</big>"); }
  13. static void SmallCB(GtkButton* /*button*/, gpointer data) { static_cast<CCommentEditorDialog*>(data)->applyTagCB("<small>", "</small>"); }
  14. static void RedCB(GtkButton* /*button*/, gpointer data) { static_cast<CCommentEditorDialog*>(data)->applyTagCB("<span color=\"red\">", "</span>"); }
  15. static void GreenCB(GtkButton* /*button*/, gpointer data) { static_cast<CCommentEditorDialog*>(data)->applyTagCB("<span color=\"green\">", "</span>"); }
  16. static void BlueCB(GtkButton* /*button*/, gpointer data) { static_cast<CCommentEditorDialog*>(data)->applyTagCB("<span color=\"blue\">", "</span>"); }
  17. static void InfoCB(GtkButton* /*button*/, gpointer data) { static_cast<CCommentEditorDialog*>(data)->infoCB(); }
  18. bool CCommentEditorDialog::run()
  19. {
  20. bool res = false;
  21. m_interface = gtk_builder_new(); // glade_xml_new(m_guiFilename.toASCIIString(), "comment", nullptr);
  22. gtk_builder_add_from_file(m_interface, m_guiFilename.toASCIIString(), nullptr);
  23. gtk_builder_connect_signals(m_interface, nullptr);
  24. ::g_signal_connect(gtk_builder_get_object(m_interface, "comment_toolbutton_bold"), "clicked", G_CALLBACK(BoldSelectionCB), this);
  25. ::g_signal_connect(gtk_builder_get_object(m_interface, "comment_toolbutton_italic"), "clicked", G_CALLBACK(ItalicSelectionCB), this);
  26. ::g_signal_connect(gtk_builder_get_object(m_interface, "comment_toolbutton_underline"), "clicked", G_CALLBACK(UnderlineCB), this);
  27. ::g_signal_connect(gtk_builder_get_object(m_interface, "comment_toolbutton_strikethrough"), "clicked", G_CALLBACK(StrikethroughCB), this);
  28. ::g_signal_connect(gtk_builder_get_object(m_interface, "comment_toolbutton_mono"), "clicked", G_CALLBACK(MonoCB), this);
  29. ::g_signal_connect(gtk_builder_get_object(m_interface, "comment_toolbutton_subscript"), "clicked", G_CALLBACK(SubscriptCB), this);
  30. ::g_signal_connect(gtk_builder_get_object(m_interface, "comment_toolbutton_superscript"), "clicked", G_CALLBACK(SuperscriptCB), this);
  31. ::g_signal_connect(gtk_builder_get_object(m_interface, "comment_toolbutton_big"), "clicked", G_CALLBACK(BigCB), this);
  32. ::g_signal_connect(gtk_builder_get_object(m_interface, "comment_toolbutton_small"), "clicked", G_CALLBACK(SmallCB), this);
  33. ::g_signal_connect(gtk_builder_get_object(m_interface, "comment_toolbutton_red"), "clicked", G_CALLBACK(RedCB), this);
  34. ::g_signal_connect(gtk_builder_get_object(m_interface, "comment_toolbutton_green"), "clicked", G_CALLBACK(GreenCB), this);
  35. ::g_signal_connect(gtk_builder_get_object(m_interface, "comment_toolbutton_blue"), "clicked", G_CALLBACK(BlueCB), this);
  36. ::g_signal_connect(gtk_builder_get_object(m_interface, "comment_toolbutton_info"), "clicked", G_CALLBACK(InfoCB), this);
  37. m_dialog = GTK_WIDGET(gtk_builder_get_object(m_interface, "comment"));
  38. m_desc = GTK_WIDGET(gtk_builder_get_object(m_interface, "comment-textview_description"));
  39. m_infoDialog = GTK_WIDGET(gtk_builder_get_object(m_interface, "messagedialog_howto_comment"));
  40. ::g_signal_connect(m_infoDialog, "close", G_CALLBACK(gtk_widget_hide), nullptr);
  41. ::g_signal_connect(m_infoDialog, "delete-event", G_CALLBACK(gtk_widget_hide), nullptr);
  42. //::g_signal_connect(GTK_WIDGET(gtk_builder_get_object(m_interface, "messagedialog_howto_comment_button_close")), "clicked", G_CALLBACK(gtk_widget_hide), nullptr);
  43. g_object_unref(m_interface);
  44. m_buffer = gtk_text_buffer_new(nullptr);
  45. gtk_text_buffer_set_text(m_buffer, m_comment.getText().toASCIIString(), -1);
  46. gtk_text_view_set_buffer(GTK_TEXT_VIEW(m_desc), m_buffer);
  47. g_object_unref(m_buffer);
  48. gtk_widget_grab_focus(m_desc);
  49. const gint result = gtk_dialog_run(GTK_DIALOG(m_dialog));
  50. if (result == GTK_RESPONSE_APPLY)
  51. {
  52. res = true;
  53. GtkTextIter start, end;
  54. m_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(m_desc));
  55. gtk_text_buffer_get_start_iter(m_buffer, &start);
  56. gtk_text_buffer_get_end_iter(m_buffer, &end);
  57. m_comment.setText(gtk_text_buffer_get_text(m_buffer, &start, &end, TRUE));
  58. }
  59. gtk_widget_destroy(m_infoDialog);
  60. gtk_widget_destroy(m_dialog);
  61. return res;
  62. }
  63. //-----------------------------------------------------------------------------------
  64. void CCommentEditorDialog::applyTagCB(const char* in, const char* out) const
  65. {
  66. GtkTextIter start, end;
  67. if (gtk_text_buffer_get_has_selection(m_buffer))
  68. {
  69. gtk_text_buffer_get_selection_bounds(m_buffer, &start, &end);
  70. gtk_text_buffer_insert(m_buffer, &start, in, gint(strlen(in)));
  71. gtk_text_buffer_get_selection_bounds(m_buffer, &start, &end);
  72. gtk_text_buffer_insert(m_buffer, &end, out, gint(strlen(out)));
  73. // reset selection to the selected text, as the tagOut is now selected
  74. gtk_text_buffer_get_selection_bounds(m_buffer, &start, &end);
  75. gtk_text_iter_backward_chars(&end, gint(strlen(out)));
  76. gtk_text_buffer_select_range(m_buffer, &start, &end);
  77. }
  78. else
  79. {
  80. gtk_text_buffer_get_selection_bounds(m_buffer, &start, &end);
  81. const gint offset = gtk_text_iter_get_offset(&start);
  82. gtk_text_buffer_insert_at_cursor(m_buffer, in, gint(strlen(in)));
  83. gtk_text_buffer_insert_at_cursor(m_buffer, out, gint(strlen(out)));
  84. gtk_text_buffer_get_iter_at_offset(m_buffer, &start, gint(offset + strlen(in)));
  85. gtk_text_buffer_place_cursor(m_buffer, &start);
  86. }
  87. // set focus on the text, to get back in edition mode directly
  88. gtk_widget_grab_focus(m_desc);
  89. }
  90. } // namespace Designer
  91. } // namespace OpenViBE