| 1 | /* StringsEditor.cpp |
| 2 | * |
| 3 | * Copyright (C) 2007-2012,2015-2022 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 | |
| 19 | #include "StringsEditor.h" |
| 20 | #include "EditorM.h" |
| 21 | #include "machine.h" |
| 22 | |
| 23 | Thing_implement (StringsEditor, Editor, 0); |
| 24 | |
| 25 | static void menu_cb_help (StringsEditor /* me */, EDITOR_ARGS) { |
| 26 | HELP (U"StringsEditor") |
| 27 | } |
| 28 | |
| 29 | void structStringsEditor :: v_createMenuItems_help (EditorMenu menu) { |
| 30 | StringsEditor_Parent :: v_createMenuItems_help (menu); |
| 31 | EditorMenu_addCommand (menu, U"StringsEditor help", U'?', menu_cb_help); |
| 32 | } |
| 33 | |
| 34 | static void updateList (StringsEditor me) { |
| 35 | GuiList_deleteAllItems (my list); |
| 36 | for (integer i = 1; i <= my strings() -> numberOfStrings; i ++) |
| 37 | GuiList_insertItem (my list, my strings() -> strings [i].get(), 0); |
| 38 | } |
| 39 | |
| 40 | static void gui_button_cb_insert (StringsEditor me, GuiButtonEvent /* event */) { |
| 41 | /* |
| 42 | Find the first selected item. |
| 43 | */ |
| 44 | autoINTVEC selected = GuiList_getSelectedPositions (my list); |
| 45 | integer position = ( selected.size >= 1 ? selected [1] : my strings() -> numberOfStrings + 1 ); |
| 46 | autostring32 text = GuiText_getString (my text); |
| 47 | /* |
| 48 | Change the data. |
| 49 | */ |
| 50 | Strings_insert (my 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 | |
| 63 | static void gui_button_cb_append (StringsEditor me, GuiButtonEvent /* event */) { |
| 64 | autostring32 text = GuiText_getString (my text); |
| 65 | /* |
| 66 | Change the data. |
| 67 | */ |
| 68 | Strings_insert (my 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, my strings() -> numberOfStrings); |
| 75 | /* |
| 76 | Clean up. |
| 77 | */ |
| 78 | Editor_broadcastDataChanged (me); |
| 79 | } |
| 80 | |
| 81 | static void gui_button_cb_remove (StringsEditor me, GuiButtonEvent /* event */) { |
| 82 | autoINTVEC selected = GuiList_getSelectedPositions (my list); |
| 83 | for (integer iselected = selected.size; iselected >= 1; iselected --) |
| 84 | Strings_remove (my strings(), selected [iselected]); |
| 85 | updateList (me); |
| 86 | Editor_broadcastDataChanged (me); |
| 87 | } |
| 88 | |
| 89 | static void gui_button_cb_replace (StringsEditor me, GuiButtonEvent /* event */) { |
| 90 | autoINTVEC selected = GuiList_getSelectedPositions (my list); |
| 91 | autostring32 text = GuiText_getString (my text); |
| 92 | for (integer iselected = 1; iselected <= selected.size; iselected ++) { |
| 93 | Strings_replace (my strings(), selected [iselected], text.get()); |
| 94 | GuiList_replaceItem (my list, text.get(), selected [iselected]); |
| 95 | } |
| 96 | Editor_broadcastDataChanged (me); |
| 97 | } |
| 98 | |
| 99 | static void gui_list_cb_doubleClick (StringsEditor me, GuiList_DoubleClickEvent /* event */) { |
| 100 | //if (event -> position <= my strings -> numberOfStrings) // FIXME |
| 101 | // GuiText_setString (my text, my strings -> strings [event -> position]); |
| 102 | } |
| 103 | |
| 104 | void structStringsEditor :: v_createChildren () { |
| 105 | list = GuiList_create (our windowForm, 1, 0, Machine_getMenuBarBottom (), -70, true, nullptr); |
| 106 | GuiList_setDoubleClickCallback (list, gui_list_cb_doubleClick, this); |
| 107 | GuiThing_show (list); |
| 108 | |
| 109 | text = GuiText_createShown (our windowForm, 0, 0, -40 - Gui_TEXTFIELD_HEIGHT, -40, 0); |
| 110 | GuiButton_createShown (our windowForm, 10, 100, -10 - Gui_PUSHBUTTON_HEIGHT, -10, U"Insert", gui_button_cb_insert, this, GuiButton_DEFAULT); |
| 111 | GuiButton_createShown (our windowForm, 110, 200, -10 - Gui_PUSHBUTTON_HEIGHT, -10, U"Append", gui_button_cb_append, this, 0); |
| 112 | GuiButton_createShown (our windowForm, 210, 300, -10 - Gui_PUSHBUTTON_HEIGHT, -10, U"Replace", gui_button_cb_replace, this, 0); |
| 113 | GuiButton_createShown (our windowForm, 310, 400, -10 - Gui_PUSHBUTTON_HEIGHT, -10, U"Remove", gui_button_cb_remove, this, 0); |
| 114 | } |
| 115 | |
| 116 | void structStringsEditor :: v1_dataChanged (Editor /* sender */) { |
| 117 | updateList (this); |
| 118 | } |
| 119 | |
| 120 | autoStringsEditor StringsEditor_create (conststring32 title, Strings strings) { |
| 121 | try { |
| 122 | autoStringsEditor me = Thing_new (StringsEditor); |
| 123 | Editor_init (me.get(), 20, 40, 600, 600, title, strings); |
| 124 | updateList (me.get()); |
| 125 | return me; |
| 126 | } catch (MelderError) { |
| 127 | Melder_throw (U"Strings window not created."); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* End of file StringsEditor.cpp */ |