| 1 | /* CategoriesEditor.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2019 David Weenink, 2008,2011-2013,2015-2018,2022,2023,2025,2026 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 3 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 | /* |
| 20 | djmw 1995 |
| 21 | djmw 19980225 repaired a memory leak, caused by wrong inheritance for |
| 22 | CategoriesEditorInsert command. |
| 23 | djmw 20020408 GPL |
| 24 | djmw 20020408 Modified 'createMenus' |
| 25 | djmw 20060111 Replaced Resources.h with Preferences.h |
| 26 | djmw 20060328 Changed last argument to 0 in XtVaSetValues, XtVaGetValues and XtVaCreateManagedWidget |
| 27 | for 64-bit compatibility. |
| 28 | djmw 20070620 Latest modification. |
| 29 | pb 20080320 split off Help menu |
| 30 | pb 20080321 new Editor API |
| 31 | djmw 20090107 Removed a bug in update that caused editor to crash on replace |
| 32 | djmw 20090203 Removed potential crashes in CategoriesEditor<command>_create. |
| 33 | djmw 20110304 Thing_new |
| 34 | djmw 20111110 Use autostringvector |
| 35 | */ |
| 36 | |
| 37 | #include "CategoriesEditor.h" |
| 38 | #include "EditorM.h" |
| 39 | #include "NUM2.h" |
| 40 | |
| 41 | Thing_implement (CategoriesEditor, Editor, 0); |
| 42 | |
| 43 | static const conststring32 CategoriesEditor_EMPTYLABEL = U"(empty)"; |
| 44 | |
| 45 | static void menu_cb_help (CategoriesEditor /* me */, EDITOR_ARGS) { |
| 46 | Melder_help (U"CategoriesEditor"); |
| 47 | } |
| 48 | |
| 49 | #pragma mark - Collection extensions |
| 50 | |
| 51 | /* Preconditions: */ |
| 52 | /* 1 <= (position [i], newpos) <= size; */ |
| 53 | /* newpos <= position [1] || newpos >= position [npos] */ |
| 54 | static void Ordered_moveItems (Ordered me, constINTVEC position, integer newpos) { |
| 55 | if (position.size < 1) |
| 56 | return; |
| 57 | const integer min = NUMmin_e (position), max = NUMmax_e (position); |
| 58 | |
| 59 | Melder_assert (min >= 1 && max <= my size && (newpos <= min || newpos >= max)); |
| 60 | |
| 61 | autovector <Daata> tmp = newvectorzero <Daata> (position.size); |
| 62 | |
| 63 | /* |
| 64 | Move some data from `me` into `tmp`, in a different order. |
| 65 | */ |
| 66 | for (integer i = 1; i <= position.size; i ++) { |
| 67 | tmp [i] = (Daata) my at [position [i]]; // dangle |
| 68 | my at [position [i]] = nullptr; // undangle |
| 69 | } |
| 70 | |
| 71 | // create a contiguous 'hole' |
| 72 | |
| 73 | integer pos; |
| 74 | if (newpos <= min) { |
| 75 | pos = max; |
| 76 | for (integer i = max; i >= newpos; i --) { |
| 77 | if (my at [i]) |
| 78 | my at [pos --] = my at [i]; |
| 79 | } |
| 80 | pos = newpos; |
| 81 | } else { |
| 82 | pos = min; |
| 83 | for (integer i = min; i <= newpos; i ++) { |
| 84 | if (my at [i]) |
| 85 | my at [pos ++] = my at [i]; |
| 86 | } |
| 87 | pos = newpos - position.size + 1; |
| 88 | } |
| 89 | |
| 90 | // fill the 'hole' |
| 91 | |
| 92 | for (integer i = 1; i <= position.size; i ++) |
| 93 | my at [pos ++] = tmp [i]; |
| 94 | } |
| 95 | |
| 96 | /* Remove the item at position 'from' and insert it at position 'to'. */ |
| 97 | static void Ordered_moveItem (Ordered me, integer from, integer to) { |
| 98 | if (from < 1 || from > my size) |
| 99 | from = my size; |
| 100 | |
| 101 | if (to < 1 || to > my size) |
| 102 | to = my size; |
| 103 | |
| 104 | if (from == to) |
| 105 | return; |
| 106 | |
| 107 | Daata tmp = my at [from]; |
| 108 | if (from > to) { |
| 109 | for (integer i = from; i > to; i --) |
| 110 | my at [i] = my at [i - 1]; |
| 111 | } else { |
| 112 | for (integer i = from; i < to; i ++) |
| 113 | my at [i] = my at [i + 1]; |
| 114 | } |
| 115 | my at [to] = tmp; |
| 116 | } |
| 117 | |
| 118 | #pragma mark - Widget updates |
| 119 | |
| 120 | static void notifyNumberOfSelected (CategoriesEditor me) { |
| 121 | autoINTVEC posList = GuiList_getSelectedPositions (my list); |
| 122 | if (posList.size > 0) { |
| 123 | autoMelderString tmp; |
| 124 | MelderString_append (& tmp, posList.size, U" selection", ( posList.size > 1 ? U"s." : U"." )); |
| 125 | GuiLabel_setText (my outOfView, tmp.string); |
| 126 | } else |
| 127 | GuiLabel_setText (my outOfView, U""); |
| 128 | } |
| 129 | |
| 130 | static void updateWidgets (CategoriesEditor me) { // all buttons except undo & redo |
| 131 | const integer size = my categories()->size; |
| 132 | |
| 133 | autoINTVEC posList = GuiList_getSelectedPositions (my list); |
| 134 | |
| 135 | GuiThing_setSensitive (my insert, ( posList.size == 1 )); |
| 136 | GuiThing_setSensitive (my insertAtEnd, true); |
| 137 | GuiThing_setSensitive (my replace, ( posList.size > 0 )); |
| 138 | |
| 139 | const bool removeSensitive = ! (posList.size == 1 && size == 1 && |
| 140 | str32equ (CategoriesEditor_EMPTYLABEL, my categories()->at [1] -> string.get())); |
| 141 | GuiThing_setSensitive (my remove, removeSensitive); |
| 142 | |
| 143 | bool moveUpIsSensitive = false, moveDownIsSensitive = false; |
| 144 | if (posList.size > 0) { |
| 145 | const integer firstPos = posList [1], lastPos = posList [posList.size]; |
| 146 | const bool contiguous = ( lastPos - firstPos + 1 == posList.size ); |
| 147 | moveUpIsSensitive = contiguous && firstPos > 1; |
| 148 | moveDownIsSensitive = contiguous && lastPos < size; |
| 149 | my position = firstPos; |
| 150 | } |
| 151 | GuiThing_setSensitive (my moveUp, moveUpIsSensitive); |
| 152 | GuiThing_setSensitive (my moveDown, moveDownIsSensitive); |
| 153 | |
| 154 | if (my history) { |
| 155 | bool undoIsSensitive = true; |
| 156 | conststring32 commandName = CommandHistory_commandName (my history.get(), 0); |
| 157 | if (! commandName) { |
| 158 | GuiButton_setText (my undo, U"Cannot undo"); |
| 159 | undoIsSensitive = false; |
| 160 | } else |
| 161 | GuiButton_setText (my undo, Melder_cat (U"Undo ", U"\"", commandName, U"\"")); |
| 162 | GuiThing_setSensitive (my undo, undoIsSensitive); |
| 163 | |
| 164 | bool redoIsSensitive = true; |
| 165 | commandName = CommandHistory_commandName (my history.get(), 1); |
| 166 | if (! commandName) { |
| 167 | GuiButton_setText (my redo, U"Cannot redo"); |
| 168 | redoIsSensitive = false; |
| 169 | } else |
| 170 | GuiButton_setText (my redo, Melder_cat (U"Redo ", U"\"", commandName, U"\"")); |
| 171 | GuiThing_setSensitive (my redo, redoIsSensitive); |
| 172 | } |
| 173 | notifyNumberOfSelected (me); |
| 174 | } |
| 175 | |
| 176 | static void update (CategoriesEditor me, integer from, integer to, constINTVEC select, integer nSelect) { |
| 177 | const integer size = my categories()->size; |
| 178 | |
| 179 | if (size == 0) { |
| 180 | autoSimpleString str = SimpleString_create (CategoriesEditor_EMPTYLABEL); |
| 181 | my categories() -> addItem_move (str.move()); |
| 182 | update (me, 0, 0, select, 0); // was nullptr |
| 183 | return; |
| 184 | } |
| 185 | if (from == 0 && to == 0) { |
| 186 | from = 1; |
| 187 | to = size; |
| 188 | } |
| 189 | if (from < 1 || from > size) |
| 190 | from = size; |
| 191 | if (to < 1 || to > size) |
| 192 | to = size; |
| 193 | if (from > to) { |
| 194 | integer tmp = from; |
| 195 | from = to; |
| 196 | to = tmp; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | Begin optimization: add the items from a table instead of separately. |
| 201 | */ |
| 202 | try { |
| 203 | const integer offset = from - 1, numberOfElements = to - from + 1; |
| 204 | autoSTRVEC table (numberOfElements); |
| 205 | integer itemCount = GuiList_getNumberOfItems (my list); |
| 206 | for (integer i = from; i <= to; i ++) { |
| 207 | SimpleString category = my categories()->at [i]; |
| 208 | table [i - offset] = Melder_dup_f (Melder_cat (i, U" ", category -> string.get())); |
| 209 | } |
| 210 | if (itemCount > size) { // have any items been removed from the Categories? |
| 211 | for (integer j = itemCount; j > size; j --) |
| 212 | GuiList_deleteItem (my list, j); |
| 213 | itemCount = size; |
| 214 | } |
| 215 | if (to > itemCount) { |
| 216 | for (integer j = 1; j <= to - itemCount; j ++) |
| 217 | GuiList_insertItem (my list, table [itemCount + j - offset].get(), 0); |
| 218 | } |
| 219 | if (from <= itemCount) { |
| 220 | const integer n = ( to < itemCount ? to : itemCount ); |
| 221 | for (integer j = from; j <= n; j ++) |
| 222 | GuiList_replaceItem (my list, table [j - offset].get(), j); |
| 223 | } |
| 224 | } catch (MelderError) { |
| 225 | throw; |
| 226 | } |
| 227 | /* |
| 228 | End of optimization |
| 229 | */ |
| 230 | |
| 231 | // HIGHLIGHT |
| 232 | |
| 233 | GuiList_deselectAllItems (my list); |
| 234 | if (size == 1) { // the only item is always selected |
| 235 | SimpleString category = my categories()->at [1]; |
| 236 | GuiList_selectItem (my list, 1); |
| 237 | updateWidgets (me); // instead of "notify". BUG? |
| 238 | GuiText_setString (my text, category -> string.get()); |
| 239 | } else if (nSelect > 0) { |
| 240 | /* |
| 241 | Select, but postpone highlighting. |
| 242 | */ |
| 243 | for (integer i = 1; i <= nSelect; i ++) |
| 244 | GuiList_selectItem (my list, ( select [i] > size ? size : select [i] )); |
| 245 | } |
| 246 | |
| 247 | // VIEWPORT |
| 248 | |
| 249 | { |
| 250 | integer top = GuiList_getTopPosition (my list); |
| 251 | const integer bottom = GuiList_getBottomPosition (my list); |
| 252 | const integer visible = bottom - top + 1; |
| 253 | if (nSelect == 0) { |
| 254 | top = my position - visible / 2; |
| 255 | } else if (select [nSelect] < top) { |
| 256 | // selection above visible area |
| 257 | top = select [1]; |
| 258 | } else if (select [1] > bottom) { |
| 259 | // selection below visible area |
| 260 | top = select [nSelect] - visible + 1; |
| 261 | } else { |
| 262 | integer deltaTopPos = -1, nUpdate = to - from + 1; |
| 263 | if ((from == select [1] && to == select [nSelect]) || // replace |
| 264 | (nUpdate > 2 && nSelect == 1)) // insert |
| 265 | { |
| 266 | deltaTopPos = 0; |
| 267 | } else if (nUpdate == nSelect + 1 && select [1] == from + 1) { // down |
| 268 | deltaTopPos = 1; |
| 269 | } |
| 270 | top += deltaTopPos; |
| 271 | } |
| 272 | Melder_clip (1_integer, & top, size - visible + 1); |
| 273 | GuiList_setTopPosition (my list, top); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | #pragma mark - Commands for Undo and Redo |
| 278 | |
| 279 | Thing_define (CategoriesEditorCommand, Command) { |
| 280 | autoCategories categories; |
| 281 | autoINTVEC selection; |
| 282 | integer nSelected, newPos; |
| 283 | |
| 284 | void v_do () |
| 285 | override { }; |
| 286 | void v_undo () |
| 287 | override { }; |
| 288 | }; |
| 289 | |
| 290 | |
| 291 | Thing_implement (CategoriesEditorCommand, Command, 0); |
| 292 | |
| 293 | static void CategoriesEditorCommand_init (CategoriesEditorCommand me, conststring32 name, Thing boss, |
| 294 | integer /*nCategories*/, integer nSelected) |
| 295 | { |
| 296 | my nSelected = nSelected; |
| 297 | Command_init (me, name, boss); |
| 298 | my categories = Categories_create(); |
| 299 | my selection = zero_INTVEC (nSelected); |
| 300 | } |
| 301 | |
| 302 | #pragma mark Insert |
| 303 | |
| 304 | Thing_define (CategoriesEditorInsert, CategoriesEditorCommand) { |
| 305 | void v_do () { |
| 306 | CategoriesEditor editor = static_cast<CategoriesEditor> (boss); |
| 307 | autoSimpleString str = Data_copy (categories->at [1]); |
| 308 | editor -> categories() -> addItemAtPosition_move (str.move(), selection [1]); |
| 309 | update (editor, selection [1], 0, selection.get(), 1); |
| 310 | }; |
| 311 | |
| 312 | void v_undo () { |
| 313 | CategoriesEditor editor = static_cast<CategoriesEditor> (boss); |
| 314 | editor -> categories() -> removeItem (selection [1]); |
| 315 | update (editor, selection [1], 0, selection.get(), 1); |
| 316 | }; |
| 317 | }; |
| 318 | |
| 319 | Thing_implement (CategoriesEditorInsert, CategoriesEditorCommand, 0); |
| 320 | |
| 321 | static autoCategoriesEditorInsert CategoriesEditorInsert_create (Thing boss, autoSimpleString str, integer position) { |
| 322 | try { |
| 323 | autoCategoriesEditorInsert me = Thing_new (CategoriesEditorInsert); |
| 324 | CategoriesEditorCommand_init (me.get(), U"Insert", boss, 1, 1); |
| 325 | my selection [1] = position; |
| 326 | my categories -> addItem_move (str.move()); |
| 327 | return me; |
| 328 | } catch (MelderError) { |
| 329 | Melder_throw (U"CategoriesEditorInsert not created."); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | #pragma mark Remove |
| 334 | |
| 335 | Thing_define (CategoriesEditorRemove, CategoriesEditorCommand) { |
| 336 | void v_do () { |
| 337 | CategoriesEditor editor = static_cast<CategoriesEditor> (boss); |
| 338 | for (integer i = nSelected; i >= 1; i--) { |
| 339 | autoSimpleString item = Data_copy (editor -> categories()->at [selection [i]]); // FIXME this copy can probably be replaced with a move |
| 340 | categories -> addItemAtPosition_move (item.move(), 1); |
| 341 | editor -> categories() -> removeItem (selection [i]); |
| 342 | } |
| 343 | update (editor, selection [1], 0, selection.get(), 0); // was nullptr |
| 344 | } |
| 345 | |
| 346 | void v_undo () { |
| 347 | CategoriesEditor editor = (CategoriesEditor) boss; |
| 348 | for (integer i = 1; i <= nSelected; i ++) { |
| 349 | autoSimpleString item = Data_copy (categories->at [i]); |
| 350 | editor -> categories() -> addItemAtPosition_move (item.move(), selection [i]); |
| 351 | } |
| 352 | update (editor, selection [1], 0, selection.get(), nSelected); |
| 353 | } |
| 354 | }; |
| 355 | |
| 356 | Thing_implement (CategoriesEditorRemove, CategoriesEditorCommand, 0); |
| 357 | |
| 358 | static autoCategoriesEditorRemove CategoriesEditorRemove_create (Thing boss, constINTVEC posList) { |
| 359 | try { |
| 360 | autoCategoriesEditorRemove me = Thing_new (CategoriesEditorRemove); |
| 361 | CategoriesEditorCommand_init (me.get(), U"Remove", boss, posList.size, posList.size); |
| 362 | my selection.all() <<= posList; |
| 363 | return me; |
| 364 | } catch (MelderError) { |
| 365 | Melder_throw (U"CategoriesEditorRemove not created."); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | #pragma mark Replace |
| 370 | |
| 371 | Thing_define (CategoriesEditorReplace, CategoriesEditorCommand) { |
| 372 | void v_do () { |
| 373 | CategoriesEditor editor = static_cast<CategoriesEditor> (boss); |
| 374 | for (integer i = nSelected; i >= 1; i --) { |
| 375 | /* |
| 376 | Swap categories->at [1] with categories->at [my selection [i]] under ambiguous ownership. |
| 377 | */ |
| 378 | autoSimpleString tmp = Data_copy (categories->at [1]); |
| 379 | autoSimpleString other; |
| 380 | other. adoptFromAmbiguousOwner (editor -> categories()->at [selection [i]]); |
| 381 | categories -> addItemAtPosition_move (other.move(), 2); // YUCK |
| 382 | editor -> categories()->at [selection [i]] = tmp.releaseToAmbiguousOwner(); |
| 383 | } |
| 384 | update (editor, selection [1], selection [nSelected], selection.get(), nSelected); |
| 385 | } |
| 386 | |
| 387 | void v_undo () { |
| 388 | CategoriesEditor editor = static_cast<CategoriesEditor> (boss); |
| 389 | for (integer i = 1; i <= nSelected; i ++) { |
| 390 | autoSimpleString str = Data_copy (categories->at [i + 1]); |
| 391 | editor -> categories() -> replaceItem_move (str.move(), selection [i]); |
| 392 | } |
| 393 | update (editor, selection [1], selection [nSelected], selection.get(), nSelected); |
| 394 | } |
| 395 | }; |
| 396 | |
| 397 | Thing_implement (CategoriesEditorReplace, CategoriesEditorCommand, 0); |
| 398 | |
| 399 | static autoCategoriesEditorReplace CategoriesEditorReplace_create (Thing boss, autoSimpleString str, constINTVEC posList) { |
| 400 | try { |
| 401 | autoCategoriesEditorReplace me = Thing_new (CategoriesEditorReplace); |
| 402 | CategoriesEditorCommand_init (me.get(), U"Replace", boss, posList.size + 1, posList.size); |
| 403 | my selection.all() <<= posList; |
| 404 | my categories -> addItem_move (str.move()); |
| 405 | return me; |
| 406 | } catch (MelderError) { |
| 407 | Melder_throw (U"CategoriesEditorReplace not created."); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | #pragma mark MoveUp |
| 412 | |
| 413 | Thing_define (CategoriesEditorMoveUp, CategoriesEditorCommand) { |
| 414 | void v_do () { |
| 415 | CategoriesEditor editor = static_cast<CategoriesEditor> (boss); |
| 416 | Ordered_moveItems ((Ordered) editor -> categories(), selection.get(), newPos); // FIXME cast |
| 417 | autoINTVEC selectionUpdate = raw_INTVEC (nSelected); |
| 418 | for (integer i = 1; i <= nSelected; i ++) |
| 419 | selectionUpdate [i] = newPos + i - 1; |
| 420 | update (editor, newPos, selection [nSelected], selectionUpdate.get(), nSelected); |
| 421 | } |
| 422 | |
| 423 | void v_undo () { |
| 424 | CategoriesEditor editor = static_cast<CategoriesEditor> (boss); |
| 425 | for (integer i = 1; i <= nSelected; i ++) |
| 426 | Ordered_moveItem ((Ordered) editor -> categories(), newPos, selection [nSelected]); // FIXME cast |
| 427 | update (editor, newPos, selection [nSelected], selection.get(), nSelected); |
| 428 | } |
| 429 | }; |
| 430 | |
| 431 | Thing_implement (CategoriesEditorMoveUp, CategoriesEditorCommand, 0); |
| 432 | |
| 433 | static autoCategoriesEditorMoveUp CategoriesEditorMoveUp_create (Thing boss, constINTVEC posList, integer newPos) { |
| 434 | try { |
| 435 | autoCategoriesEditorMoveUp me = Thing_new (CategoriesEditorMoveUp); |
| 436 | CategoriesEditorCommand_init (me.get(), U"Move up", boss, 0, posList.size); |
| 437 | my selection.all() <<= posList; |
| 438 | my newPos = newPos; |
| 439 | return me; |
| 440 | } catch (MelderError) { |
| 441 | Melder_throw (U"CategoriesEditorMoveUp not created."); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | #pragma mark MoveDown |
| 446 | |
| 447 | Thing_define (CategoriesEditorMoveDown, CategoriesEditorCommand) { |
| 448 | |
| 449 | void v_do () { |
| 450 | CategoriesEditor editor = static_cast<CategoriesEditor> (boss); |
| 451 | Ordered_moveItems ((Ordered) editor -> categories(), selection.get(), newPos); // FIXME cast |
| 452 | autoINTVEC selectionUpdate = raw_INTVEC (nSelected); |
| 453 | for (integer i = 1; i <= nSelected; i ++) |
| 454 | selectionUpdate [i] = newPos - nSelected + i; |
| 455 | update (editor, selection [1], newPos, selectionUpdate.get(), nSelected); |
| 456 | } |
| 457 | |
| 458 | void v_undo () { |
| 459 | CategoriesEditor editor = static_cast<CategoriesEditor> (boss); |
| 460 | for (integer i = 1; i <= nSelected; i ++) |
| 461 | Ordered_moveItem ((Ordered) editor -> categories(), newPos, selection [1]); // TODO 1 or i ?? // FIXME cast |
| 462 | integer from = selection [1]; |
| 463 | update (editor, ( from > 1 ? from -- : from ), newPos, selection.get(), nSelected); |
| 464 | } |
| 465 | }; |
| 466 | |
| 467 | Thing_implement (CategoriesEditorMoveDown, CategoriesEditorCommand, 0); |
| 468 | |
| 469 | static autoCategoriesEditorMoveDown CategoriesEditorMoveDown_create (Thing boss, constINTVEC posList, integer newPos) { |
| 470 | try { |
| 471 | autoCategoriesEditorMoveDown me = Thing_new (CategoriesEditorMoveDown); |
| 472 | CategoriesEditorCommand_init (me.get(), U"Move down", boss, 0, posList.size); |
| 473 | my selection.all() <<= posList; |
| 474 | my newPos = newPos; |
| 475 | return me; |
| 476 | } catch (MelderError) { |
| 477 | Melder_throw (U"CategoriesEditorMoveDown not created."); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | #pragma mark - Callbacks |
| 482 | |
| 483 | static void gui_button_cb_remove (CategoriesEditor me, GuiButtonEvent /* event */) { |
| 484 | autoINTVEC posList = GuiList_getSelectedPositions (my list); |
| 485 | if (posList.size > 0) { |
| 486 | autoCategoriesEditorRemove command = CategoriesEditorRemove_create (me, posList.get()); |
| 487 | command -> v_do (); |
| 488 | if (my history) |
| 489 | CommandHistory_insertItem_move (my history.get(), command.move()); |
| 490 | updateWidgets (me); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | static void insert (CategoriesEditor me, integer position) { |
| 495 | autostring32 text = GuiText_getString (my text); |
| 496 | if (text && text [0] != U'\0') { |
| 497 | autoSimpleString str = SimpleString_create (text.get()); |
| 498 | autoCategoriesEditorInsert command = CategoriesEditorInsert_create (me, str.move(), position); |
| 499 | command -> v_do (); |
| 500 | if (my history) |
| 501 | CommandHistory_insertItem_move (my history.get(), command.move()); |
| 502 | updateWidgets (me); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | static void gui_button_cb_insert (CategoriesEditor me, GuiButtonEvent /* event */) { |
| 507 | insert (me, my position); |
| 508 | } |
| 509 | |
| 510 | static void gui_button_cb_insertAtEnd (CategoriesEditor me, GuiButtonEvent /* event */) { |
| 511 | insert (me, my categories()->size + 1); |
| 512 | my position = my categories()->size; |
| 513 | } |
| 514 | |
| 515 | static void gui_button_cb_replace (CategoriesEditor me, GuiButtonEvent /* event */) { |
| 516 | autoINTVEC posList = GuiList_getSelectedPositions (my list); |
| 517 | if (posList.size > 0) { |
| 518 | autostring32 text = GuiText_getString (my text); |
| 519 | if (text && text [0] != U'\0') { |
| 520 | autoSimpleString str = SimpleString_create (text.get()); |
| 521 | autoCategoriesEditorReplace command = CategoriesEditorReplace_create (me, str.move(), posList.get()); |
| 522 | command -> v_do (); |
| 523 | if (my history) |
| 524 | CommandHistory_insertItem_move (my history.get(), command.move()); |
| 525 | updateWidgets (me); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /* Precondition: contiguous selection */ |
| 531 | static void gui_button_cb_moveUp (CategoriesEditor me, GuiButtonEvent /* event */) { |
| 532 | autoINTVEC posList = GuiList_getSelectedPositions (my list); |
| 533 | if (posList.size > 0) { |
| 534 | autoCategoriesEditorMoveUp command = CategoriesEditorMoveUp_create (me, posList.get(), posList [1] - 1); |
| 535 | command -> v_do (); |
| 536 | if (my history) |
| 537 | CommandHistory_insertItem_move (my history.get(), command.move()); |
| 538 | updateWidgets (me); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /* Precondition: contiguous selection */ |
| 543 | static void gui_button_cb_moveDown (CategoriesEditor me, GuiButtonEvent /* event */) { |
| 544 | autoINTVEC posList = GuiList_getSelectedPositions (my list); |
| 545 | if (posList.size > 0) { |
| 546 | autoCategoriesEditorMoveDown command = CategoriesEditorMoveDown_create (me, posList.get(), posList [posList.size] + 1); |
| 547 | command -> v_do (); |
| 548 | if (my history) |
| 549 | CommandHistory_insertItem_move (my history.get(), command.move()); |
| 550 | updateWidgets (me); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | static void gui_list_cb_selectionChanged (CategoriesEditor me, GuiList_SelectionChangedEvent /* event */) { |
| 555 | updateWidgets (me); |
| 556 | } |
| 557 | |
| 558 | static void gui_list_cb_doubleClick (CategoriesEditor me, GuiList_DoubleClickEvent event) { |
| 559 | Melder_assert (event -> list == my list); |
| 560 | // `my position` should just have been updated by the selectionChanged callback. |
| 561 | |
| 562 | autoINTVEC posList = GuiList_getSelectedPositions (my list); |
| 563 | if (posList.size == 1 // often or even usually true when double-clicking? |
| 564 | && posList [1] == my position) // should be true, but we don't crash if it's false |
| 565 | { |
| 566 | SimpleString category = my categories()->at [my position]; |
| 567 | GuiText_setString (my text, ( category -> string ? category -> string.get() : U"" )); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | static void gui_list_cb_scroll (CategoriesEditor me, GuiList_ScrollEvent /* event */) { |
| 572 | notifyNumberOfSelected (me); |
| 573 | } |
| 574 | |
| 575 | static void gui_button_cb_undo (CategoriesEditor me, GuiButtonEvent /* event */) { |
| 576 | if (CommandHistory_isOffleft (my history.get())) |
| 577 | return; |
| 578 | Command command = CommandHistory_getItem (my history.get()); |
| 579 | command -> v_undo (); |
| 580 | CommandHistory_back (my history.get()); |
| 581 | updateWidgets (me); |
| 582 | } |
| 583 | |
| 584 | static void gui_button_cb_redo (CategoriesEditor me, GuiButtonEvent /* event */) { |
| 585 | CommandHistory_forth (my history.get()); |
| 586 | if (CommandHistory_isOffright (my history.get())) |
| 587 | return; |
| 588 | Command command = CommandHistory_getItem (my history.get()); |
| 589 | command -> v_do (); |
| 590 | updateWidgets (me); |
| 591 | } |
| 592 | |
| 593 | #pragma mark - Editor methods |
| 594 | |
| 595 | void structCategoriesEditor :: v_createMenuItems_help (EditorMenu menu) { |
| 596 | CategoriesEditor_Parent :: v_createMenuItems_help (menu); |
| 597 | EditorMenu_addCommand (menu, U"CategoriesEditor help", '?', menu_cb_help); |
| 598 | } |
| 599 | |
| 600 | // origin is at top left. |
| 601 | void structCategoriesEditor :: v_createChildren () { |
| 602 | constexpr int menuBarOffset { 40 }; |
| 603 | constexpr int button_width { 130 }, button_height { menuBarOffset }, list_width { 260 }, list_height { 420 }; |
| 604 | constexpr int delta_x { 15 }, delta_y { menuBarOffset / 2 }, text_button_height { button_height / 2 }; |
| 605 | |
| 606 | int left = 5, right = left + button_width, top = 3 + menuBarOffset, bottom = top + text_button_height; |
| 607 | GuiLabel_createShown (our windowForm, left, right, top, bottom, U"Positions:", 0); |
| 608 | left = right + delta_x ; |
| 609 | right = left + button_width; |
| 610 | GuiLabel_createShown (our windowForm, left, right, top, bottom, U"Values:", 0); |
| 611 | |
| 612 | left = 0; |
| 613 | right = left + list_width; |
| 614 | // int buttons_top = (top = bottom + delta_y); |
| 615 | int list_bottom = bottom = top + list_height; |
| 616 | list = GuiList_create (our windowForm, left, right, top, bottom, true, 0); |
| 617 | GuiList_setSelectionChangedCallback (list, gui_list_cb_selectionChanged, this); |
| 618 | GuiList_setDoubleClickCallback (list, gui_list_cb_doubleClick, this); |
| 619 | GuiList_setScrollCallback (list, gui_list_cb_scroll, this); |
| 620 | GuiThing_show (list); |
| 621 | |
| 622 | int buttons_left = left = right + 2 * delta_x; |
| 623 | right = left + button_width; |
| 624 | bottom = top + button_height; |
| 625 | GuiLabel_createShown (our windowForm, left, right, top, bottom, U"Value:", 0); |
| 626 | left = right + delta_x; |
| 627 | right = left + button_width; |
| 628 | text = GuiText_createShown (our windowForm, left, right, top, bottom, 0); |
| 629 | GuiText_setString (text, CategoriesEditor_EMPTYLABEL); |
| 630 | |
| 631 | left = buttons_left; |
| 632 | right = left + button_width; |
| 633 | top = bottom + delta_y; |
| 634 | bottom = top + button_height; |
| 635 | insert = GuiButton_createShown (our windowForm, left, right, top, bottom, U"Insert", gui_button_cb_insert, this, GuiButton_DEFAULT); |
| 636 | left = right + delta_x; |
| 637 | right = left + button_width; |
| 638 | replace = GuiButton_createShown (our windowForm, left, right, top, bottom, U"Replace", gui_button_cb_replace, this, 0); |
| 639 | left = buttons_left; |
| 640 | right = left + int (1.5 * button_width); |
| 641 | top = bottom + delta_y; |
| 642 | bottom = top + button_height; |
| 643 | insertAtEnd = GuiButton_createShown (our windowForm, left, right, top, bottom, U"Insert at end", gui_button_cb_insertAtEnd, this, 0); |
| 644 | top = bottom + delta_y; |
| 645 | bottom = top + button_height; |
| 646 | undo = GuiButton_createShown (our windowForm, left, right, top, bottom, U"Undo", gui_button_cb_undo, this, 0); |
| 647 | top = bottom + delta_y; |
| 648 | bottom = top + button_height; |
| 649 | redo = GuiButton_createShown (our windowForm, left, right, top, bottom, U"Redo", gui_button_cb_redo, this, 0); |
| 650 | top = bottom + delta_y; |
| 651 | bottom = top + button_height; |
| 652 | remove = GuiButton_createShown (our windowForm, left, right, top, bottom, U"Remove", gui_button_cb_remove, this, 0); |
| 653 | top = bottom + delta_y; |
| 654 | bottom = top + button_height; |
| 655 | moveUp = GuiButton_createShown (our windowForm, left, right, top, bottom, U"Move selection up", gui_button_cb_moveUp, this, 0); |
| 656 | top = bottom + delta_y; |
| 657 | bottom = top + button_height; |
| 658 | moveDown = GuiButton_createShown (our windowForm, left, right, top, bottom, U"Move selection down", gui_button_cb_moveDown, this, 0); |
| 659 | |
| 660 | top = list_bottom + delta_y; |
| 661 | bottom = top + button_height; |
| 662 | left = 5; |
| 663 | right = left + 200; |
| 664 | outOfView = GuiLabel_createShown (our windowForm, left, right, top, bottom, U"", 0); |
| 665 | } |
| 666 | |
| 667 | void structCategoriesEditor :: v1_dataChanged (Editor /* sender */) { |
| 668 | autoINTVEC select; |
| 669 | update (this, 0, 0, select.get(), 0); |
| 670 | updateWidgets (this); |
| 671 | } |
| 672 | |
| 673 | #pragma mark - |
| 674 | |
| 675 | autoCategoriesEditor CategoriesEditor_create (conststring32 title, Categories categories) { |
| 676 | try { |
| 677 | autoCategoriesEditor me = Thing_new (CategoriesEditor); |
| 678 | Editor_init (me.get(), 20, 40, 600, 600, title, categories); |
| 679 | my history = CommandHistory_create (); |
| 680 | autoINTVEC select; |
| 681 | update (me.get(), 0, 0, select.get(), 0); |
| 682 | updateWidgets (me.get()); |
| 683 | return me; |
| 684 | } catch (MelderError) { |
| 685 | Melder_throw (U"Categories window not created."); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | /* End of file CategoriesEditor.cpp */ |