| 1 | /* ArtwordEditor.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2013,2015-2024 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. |
| 13 | * See the GNU 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 "ArtwordEditor.h" |
| 20 | #include "machine.h" |
| 21 | |
| 22 | Thing_implement (ArtwordEditor, Editor, 0); |
| 23 | |
| 24 | static void updateList (ArtwordEditor me) { |
| 25 | ArtwordData a = & my artword -> data [(int) my muscle]; |
| 26 | GuiList_deleteAllItems (my list); |
| 27 | for (int16 i = 1; i <= a -> numberOfTargets; i ++) { |
| 28 | GuiList_insertItem (my list, |
| 29 | Melder_cat (Melder_single (a -> times [i]), U" ", Melder_single (a -> targets [i])), |
| 30 | i |
| 31 | ); |
| 32 | } |
| 33 | Graphics_updateWs (my graphics.get()); |
| 34 | } |
| 35 | |
| 36 | static void gui_button_cb_removeTarget (ArtwordEditor me, GuiButtonEvent /* event */) { |
| 37 | autoINTVEC selectedPositions = GuiList_getSelectedPositions (my list); |
| 38 | for (integer ipos = selectedPositions.size; ipos > 0; ipos --) { |
| 39 | integer position = selectedPositions [ipos]; |
| 40 | Melder_assert (position >= 1 && position <= INT16_MAX); |
| 41 | Artword_removeTarget (my artword, my muscle, (int16) position); // guarded conversion |
| 42 | } |
| 43 | updateList (me); |
| 44 | Editor_broadcastDataChanged (me); |
| 45 | } |
| 46 | |
| 47 | static void gui_button_cb_addTarget (ArtwordEditor me, GuiButtonEvent /* event */) { |
| 48 | autostring32 timeText = GuiText_getString (my time); |
| 49 | double tim = Melder_atof (timeText.get()); |
| 50 | if (isundef (tim)) |
| 51 | tim = 0.0; |
| 52 | autostring32 valueText = GuiText_getString (my value); |
| 53 | const double value = Melder_atof (valueText.get()); |
| 54 | ArtwordData a = & my artword -> data [(int) my muscle]; |
| 55 | int i = 1, oldCount = a -> numberOfTargets; |
| 56 | Artword_setTarget (my artword, my muscle, tim, value); |
| 57 | |
| 58 | /* Optimization instead of "updateList (me)". */ |
| 59 | |
| 60 | Melder_clip (0.0, & tim, my artword -> totalTime); |
| 61 | while (tim != a -> times [i]) { |
| 62 | i ++; |
| 63 | Melder_assert (i <= a -> numberOfTargets); // can fail if tim is in an extended precision register |
| 64 | } |
| 65 | const conststring32 itemText = Melder_cat (Melder_single (tim), U" ", Melder_single (value)); |
| 66 | if (a -> numberOfTargets == oldCount) |
| 67 | GuiList_replaceItem (my list, itemText, i); |
| 68 | else |
| 69 | GuiList_insertItem (my list, itemText, i); |
| 70 | Graphics_updateWs (my graphics.get()); |
| 71 | Editor_broadcastDataChanged (me); |
| 72 | } |
| 73 | |
| 74 | static void gui_radiobutton_cb_toggle (ArtwordEditor me, GuiRadioButtonEvent event) { |
| 75 | my muscle = (kArt_muscle) event -> position; |
| 76 | Melder_assert ((int) my muscle > 0); |
| 77 | Melder_assert (my muscle <= kArt_muscle::MAX); |
| 78 | updateList (me); |
| 79 | } |
| 80 | |
| 81 | static void gui_drawingarea_cb_expose (ArtwordEditor me, GuiDrawingArea_ExposeEvent /* event */) { |
| 82 | if (! my graphics) |
| 83 | return; |
| 84 | Graphics_clearWs (my graphics.get()); |
| 85 | Artword_draw (my artword, my graphics.get(), my muscle, true); |
| 86 | } |
| 87 | |
| 88 | static void gui_drawingarea_cb_mouse (ArtwordEditor me, GuiDrawingArea_MouseEvent event) { |
| 89 | if (! my graphics) |
| 90 | return; |
| 91 | Graphics_setWindow (my graphics.get(), 0, my artword -> totalTime, -1.0, 1.0); |
| 92 | Graphics_setInner (my graphics.get()); |
| 93 | double xWC, yWC; |
| 94 | Graphics_DCtoWC (my graphics.get(), event -> x, event -> y, & xWC, & yWC); |
| 95 | Graphics_unsetInner (my graphics.get()); |
| 96 | GuiText_setString (my time, Melder_fixed (xWC, 6)); |
| 97 | GuiText_setString (my value, Melder_fixed (yWC, 6)); |
| 98 | } |
| 99 | |
| 100 | void structArtwordEditor :: v1_dataChanged (Editor /* sender */) { |
| 101 | updateList (this); |
| 102 | Graphics_updateWs (graphics.get()); |
| 103 | } |
| 104 | |
| 105 | void structArtwordEditor :: v_createChildren () { |
| 106 | int dy = Machine_getMenuBarBottom (); |
| 107 | GuiLabel_createShown (our windowForm, 40, 100, dy + 3, dy + 3 + Gui_LABEL_HEIGHT, U"Targets:", 0); |
| 108 | GuiLabel_createShown (our windowForm, 5, 65, dy + 20, dy + 20 + Gui_LABEL_HEIGHT, U"Times:", 0); |
| 109 | GuiLabel_createShown (our windowForm, 80, 140, dy + 20, dy + 20 + Gui_LABEL_HEIGHT, U"Values:", 0); |
| 110 | our list = GuiList_createShown (our windowForm, 0, 140, dy + 40, dy + 340, true, nullptr); |
| 111 | |
| 112 | GuiButton_createShown (our windowForm, 10, 130, dy + 410, dy + 410 + Gui_PUSHBUTTON_HEIGHT, U"Remove target", gui_button_cb_removeTarget, this, 0); |
| 113 | |
| 114 | our drawingArea = GuiDrawingArea_createShown (our windowForm, 170, 470, dy + 10, dy + 310, |
| 115 | gui_drawingarea_cb_expose, gui_drawingarea_cb_mouse, |
| 116 | nullptr, nullptr, nullptr, this, 0 |
| 117 | ); |
| 118 | |
| 119 | GuiLabel_createShown (our windowForm, 220, 270, dy + 340, dy + 340 + Gui_LABEL_HEIGHT, U"Time:", 0); |
| 120 | our time = GuiText_createShown (our windowForm, 270, 370, dy + 340, dy + 340 + Gui_TEXTFIELD_HEIGHT, 0); |
| 121 | |
| 122 | GuiLabel_createShown (our windowForm, 220, 270, dy + 370, dy + 370 + Gui_LABEL_HEIGHT, U"Value:", 0); |
| 123 | our value = GuiText_createShown (our windowForm, 270, 370, dy + 370, dy + 370 + Gui_TEXTFIELD_HEIGHT, 0); |
| 124 | |
| 125 | GuiButton_createShown (our windowForm, 240, 360, dy + 410, dy + 410 + Gui_PUSHBUTTON_HEIGHT, U"Add target", gui_button_cb_addTarget, this, GuiButton_DEFAULT); |
| 126 | |
| 127 | dy = Machine_getMenuBarBottom (); |
| 128 | GuiRadioGroup_begin (); |
| 129 | for (int i = 1; i <= (int) kArt_muscle::MAX; i ++) { |
| 130 | button [i] = GuiRadioButton_createShown (our windowForm, |
| 131 | 480, 0, dy, dy + Gui_RADIOBUTTON_HEIGHT, |
| 132 | kArt_muscle_getText ((kArt_muscle) i), gui_radiobutton_cb_toggle, this, 0 |
| 133 | ); |
| 134 | dy += Gui_RADIOBUTTON_HEIGHT + Gui_RADIOBUTTON_SPACING - 2; |
| 135 | } |
| 136 | GuiRadioGroup_end (); |
| 137 | muscle = (kArt_muscle) 1; |
| 138 | GuiRadioButton_set (button [(int) muscle]); |
| 139 | } |
| 140 | |
| 141 | autoArtwordEditor ArtwordEditor_create (conststring32 title, Artword artword) { |
| 142 | try { |
| 143 | autoArtwordEditor me = Thing_new (ArtwordEditor); |
| 144 | my artword = artword; |
| 145 | Editor_init (me.get(), 20, 40, 650, 600, title, artword); |
| 146 | //XtUnmanageChild (my menuBar); |
| 147 | my graphics = Graphics_create_xmdrawingarea (my drawingArea); |
| 148 | updateList (me.get()); |
| 149 | return me; |
| 150 | } catch (MelderError) { |
| 151 | Melder_throw (U"Artword window not created."); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /* End of file ArtwordEditor.cpp */ |