StringsEditor.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* StringsEditor.cpp
  2. *
  3. * Copyright (C) 2007-2011,2015,2016,2017 Paul Boersma
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "StringsEditor.h"
  19. #include "EditorM.h"
  20. #include "machine.h"
  21. Thing_implement (StringsEditor, Editor, 0);
  22. void structStringsEditor :: v_destroy () noexcept {
  23. StringsEditor_Parent :: v_destroy ();
  24. }
  25. static void menu_cb_help (StringsEditor /* me */, EDITOR_ARGS_DIRECT) {
  26. Melder_help (U"StringsEditor");
  27. }
  28. void structStringsEditor :: v_createHelpMenuItems (EditorMenu menu) {
  29. StringsEditor_Parent :: v_createHelpMenuItems (menu);
  30. EditorMenu_addCommand (menu, U"StringsEditor help", U'?', menu_cb_help);
  31. }
  32. static void updateList (StringsEditor me) {
  33. Strings strings = (Strings) my data;
  34. GuiList_deleteAllItems (my list);
  35. for (integer i = 1; i <= strings -> numberOfStrings; i ++)
  36. GuiList_insertItem (my list, strings -> strings [i].get(), 0);
  37. }
  38. static void gui_button_cb_insert (StringsEditor me, GuiButtonEvent /* event */) {
  39. Strings strings = (Strings) my data;
  40. /*
  41. * Find the first selected item.
  42. */
  43. integer numberOfSelected, *selected = GuiList_getSelectedPositions (my list, & numberOfSelected);
  44. integer position = selected ? selected [1] : strings -> numberOfStrings + 1;
  45. NUMvector_free (selected, 1);
  46. autostring32 text = GuiText_getString (my text);
  47. /*
  48. Change the data.
  49. */
  50. Strings_insert (strings, position, text.get());
  51. /*
  52. Change the list.
  53. */
  54. GuiList_insertItem (my list, text.get(), position);
  55. GuiList_deselectAllItems (my list);
  56. GuiList_selectItem (my list, position);
  57. /*
  58. Clean up.
  59. */
  60. Editor_broadcastDataChanged (me);
  61. }
  62. static void gui_button_cb_append (StringsEditor me, GuiButtonEvent /* event */) {
  63. Strings strings = (Strings) my data;
  64. autostring32 text = GuiText_getString (my text);
  65. /*
  66. Change the data.
  67. */
  68. Strings_insert (strings, 0, text.get());
  69. /*
  70. Change the list.
  71. */
  72. GuiList_insertItem (my list, text.get(), 0);
  73. GuiList_deselectAllItems (my list);
  74. GuiList_selectItem (my list, strings -> numberOfStrings);
  75. /*
  76. Clean up.
  77. */
  78. Editor_broadcastDataChanged (me);
  79. }
  80. static void gui_button_cb_remove (StringsEditor me, GuiButtonEvent /* event */) {
  81. integer numberOfSelected, *selected = GuiList_getSelectedPositions (my list, & numberOfSelected);
  82. for (integer iselected = numberOfSelected; iselected >= 1; iselected --) {
  83. Strings_remove ((Strings) my data, selected [iselected]);
  84. }
  85. NUMvector_free (selected, 1);
  86. updateList (me);
  87. Editor_broadcastDataChanged (me);
  88. }
  89. static void gui_button_cb_replace (StringsEditor me, GuiButtonEvent /* event */) {
  90. Strings strings = (Strings) my data;
  91. integer numberOfSelected, *selected = GuiList_getSelectedPositions (my list, & numberOfSelected);
  92. autostring32 text = GuiText_getString (my text);
  93. for (integer iselected = 1; iselected <= numberOfSelected; iselected ++) {
  94. Strings_replace (strings, selected [iselected], text.get());
  95. GuiList_replaceItem (my list, text.get(), selected [iselected]);
  96. }
  97. Editor_broadcastDataChanged (me);
  98. }
  99. static void gui_list_cb_doubleClick (StringsEditor me, GuiList_DoubleClickEvent /* event */) {
  100. Strings strings = (Strings) my data;
  101. //if (event -> position <= strings -> numberOfStrings) // FIXME
  102. // GuiText_setString (my text, strings -> strings [event -> position]);
  103. }
  104. void structStringsEditor :: v_createChildren () {
  105. list = GuiList_create (our windowForm, 1, 0, Machine_getMenuBarHeight (), -70, true, nullptr);
  106. GuiList_setDoubleClickCallback (list, gui_list_cb_doubleClick, this);
  107. GuiThing_show (list);
  108. text = GuiText_createShown (our windowForm, 0, 0, -40 - Gui_TEXTFIELD_HEIGHT, -40, 0);
  109. GuiButton_createShown (our windowForm, 10, 100, -10 - Gui_PUSHBUTTON_HEIGHT, -10, U"Insert", gui_button_cb_insert, this, GuiButton_DEFAULT);
  110. GuiButton_createShown (our windowForm, 110, 200, -10 - Gui_PUSHBUTTON_HEIGHT, -10, U"Append", gui_button_cb_append, this, 0);
  111. GuiButton_createShown (our windowForm, 210, 300, -10 - Gui_PUSHBUTTON_HEIGHT, -10, U"Replace", gui_button_cb_replace, this, 0);
  112. GuiButton_createShown (our windowForm, 310, 400, -10 - Gui_PUSHBUTTON_HEIGHT, -10, U"Remove", gui_button_cb_remove, this, 0);
  113. }
  114. void structStringsEditor :: v_dataChanged () {
  115. updateList (this);
  116. }
  117. autoStringsEditor StringsEditor_create (conststring32 title, Strings data) {
  118. try {
  119. autoStringsEditor me = Thing_new (StringsEditor);
  120. Editor_init (me.get(), 20, 40, 600, 600, title, data);
  121. updateList (me.get());
  122. return me;
  123. } catch (MelderError) {
  124. Melder_throw (U"Strings window not created.");
  125. }
  126. }
  127. /* End of file StringsEditor.cpp */