| 1 | /* ButtonEditor.cpp |
| 2 | * |
| 3 | * Copyright (C) 1996-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. |
| 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 "ButtonEditor.h" |
| 20 | #include "praatP.h" |
| 21 | #include "praat_script.h" |
| 22 | #include "EditorM.h" |
| 23 | #include "machine.h" |
| 24 | |
| 25 | Thing_implement (ButtonEditor, HyperPage, 0); |
| 26 | |
| 27 | #if defined (_WIN32) |
| 28 | #define BUTTON_WIDTH 72 |
| 29 | #elif defined (macintosh) |
| 30 | #define BUTTON_WIDTH 72 |
| 31 | #else |
| 32 | #define BUTTON_WIDTH 96 |
| 33 | #endif |
| 34 | |
| 35 | static void drawMenuCommand (ButtonEditor me, Praat_Command cmd, integer i) { |
| 36 | static MelderString text; |
| 37 | bool isAdded = cmd -> uniqueID != 0 || !! cmd -> script; |
| 38 | bool isHidden = cmd -> hidden; |
| 39 | bool isToggled = cmd -> toggled; |
| 40 | conststring32 clickText = isHidden ? (isToggled ? (isAdded ? (cmd -> uniqueID ? U"REMOVED" : U"HIDDEN") : U"HIDDEN") : U"hidden") : |
| 41 | (isToggled ? U"SHOWN" : (isAdded ? (cmd -> uniqueID ? U"ADDED" : U"START-UP") : U"shown")); |
| 42 | MelderString_empty (& text); |
| 43 | if (cmd -> unhidable) |
| 44 | MelderString_append (& text, U"#unhidable "); |
| 45 | else |
| 46 | MelderString_append (& text, U"@@m", i, U"|", clickText, U"@ "); |
| 47 | MelderString_append (& text, cmd -> window.get(), U": "); |
| 48 | if (cmd -> menu) |
| 49 | MelderString_append (& text, cmd -> menu.get(), U": "); |
| 50 | if (cmd -> title) { |
| 51 | if (cmd -> executable) |
| 52 | MelderString_append (& text, U"@@p", i, U"|", cmd -> title.get(), U"@"); |
| 53 | else |
| 54 | MelderString_append (& text, cmd -> title.get()); |
| 55 | } else { |
| 56 | MelderString_append (& text, U"---------"); |
| 57 | } |
| 58 | if (cmd -> after) |
| 59 | MelderString_append (& text, U", %%%%after \"", cmd -> after.get(), U"\"%%"); |
| 60 | if (cmd -> script) |
| 61 | MelderString_append (& text, U", script \"", Melder_peekExpandBackslashes (cmd -> script.get()), U"\""); |
| 62 | HyperPage_any (me, text.string, my instancePref_font(), my instancePref_fontSize(), cmd -> callback ? 0 : Graphics_ITALIC, 0.0, |
| 63 | cmd -> depth * 0.3, 0.4, 0.0, 0.0, 0); |
| 64 | } |
| 65 | |
| 66 | static void drawAction (ButtonEditor me, Praat_Command cmd, integer i) { |
| 67 | static MelderString text; |
| 68 | bool isAdded = cmd -> uniqueID != 0 || !! cmd -> script; |
| 69 | bool isHidden = cmd -> hidden, isToggled = cmd -> toggled; |
| 70 | conststring32 clickText = isHidden ? (isToggled ? (isAdded ? (cmd -> uniqueID ? U"REMOVED" : U"HIDDEN") : U"HIDDEN") : U"hidden") : |
| 71 | (isToggled ? U"SHOWN" : (isAdded ? (cmd -> uniqueID ? U"ADDED" : U"START-UP") : U"shown")); |
| 72 | integer n1 = cmd -> n1; |
| 73 | MelderString_empty (& text); |
| 74 | if (cmd -> class4) |
| 75 | MelderString_append (& text, U"#unhidable "); |
| 76 | else |
| 77 | MelderString_append (& text, U"@@a", i, U"|", clickText, U"@ "); |
| 78 | MelderString_append (& text, cmd -> class1 -> className); |
| 79 | if (n1) |
| 80 | MelderString_append (& text, U" (", n1, U")"); |
| 81 | if (cmd -> class2) { |
| 82 | integer n2 = cmd -> n2; |
| 83 | MelderString_append (& text, U" & ", cmd -> class2 -> className); |
| 84 | if (n2) |
| 85 | MelderString_append (& text, U" (", n2, U")"); |
| 86 | } |
| 87 | if (cmd -> class3) { |
| 88 | integer n3 = cmd -> n3; |
| 89 | MelderString_append (& text, U" & ", cmd -> class3 -> className); |
| 90 | if (n3) |
| 91 | MelderString_append (& text, U" (", n3, U")"); |
| 92 | } |
| 93 | if (cmd -> class4) { |
| 94 | integer n4 = cmd -> n4; |
| 95 | MelderString_append (& text, U" & ", cmd -> class4 -> className); |
| 96 | if (n4) |
| 97 | MelderString_append (& text, U" (", n4, U")"); |
| 98 | } |
| 99 | MelderString_append (& text, U": "); |
| 100 | if (cmd -> title) { |
| 101 | if (cmd -> executable) |
| 102 | MelderString_append (& text, U"@@e", i, U"|", cmd -> title.get(), U"@"); |
| 103 | else |
| 104 | MelderString_append (& text, cmd -> title.get()); |
| 105 | } else { |
| 106 | MelderString_append (& text, U"---------"); |
| 107 | } |
| 108 | if (cmd -> after) |
| 109 | MelderString_append (& text, U", %%%%after \"", cmd -> after.get(), U"\"%%"); |
| 110 | if (cmd -> script) |
| 111 | MelderString_append (& text, U", script \"", Melder_peekExpandBackslashes (cmd -> script.get()), U"\""); |
| 112 | HyperPage_any (me, text.string, my instancePref_font(), my instancePref_fontSize(), cmd -> callback ? 0 : Graphics_ITALIC, 0.0, |
| 113 | cmd -> depth * 0.3, 0.4, 0.0, 0.0, 0); |
| 114 | } |
| 115 | |
| 116 | void structButtonEditor :: v_draw () { |
| 117 | switch (show) { |
| 118 | case 1: |
| 119 | for (integer i = 1, n = praat_getNumberOfMenuCommands (); i <= n; i ++) { |
| 120 | Praat_Command cmd = praat_getMenuCommand (i); |
| 121 | if (str32equ (cmd -> window.get(), U"Objects")) |
| 122 | drawMenuCommand (this, praat_getMenuCommand (i), i); |
| 123 | } |
| 124 | break; |
| 125 | case 2: |
| 126 | for (integer i = 1, n = praat_getNumberOfMenuCommands (); i <= n; i ++) { |
| 127 | Praat_Command cmd = praat_getMenuCommand (i); |
| 128 | if (str32equ (cmd -> window.get(), U"Picture")) |
| 129 | drawMenuCommand (this, praat_getMenuCommand (i), i); |
| 130 | } |
| 131 | break; |
| 132 | case 3: |
| 133 | for (integer i = 1, n = praat_getNumberOfMenuCommands (); i <= n; i ++) { |
| 134 | Praat_Command cmd = praat_getMenuCommand (i); |
| 135 | if (! str32equ (cmd -> window.get(), U"Objects") && ! str32equ (cmd -> window.get(), U"Picture")) |
| 136 | drawMenuCommand (this, praat_getMenuCommand (i), i); |
| 137 | } |
| 138 | break; |
| 139 | case 4: |
| 140 | for (integer i = 1, n = praat_getNumberOfActions (); i <= n; i ++) { |
| 141 | Praat_Command cmd = praat_getAction (i); |
| 142 | conststring32 klas = cmd -> class1 -> className; |
| 143 | if (klas [0] >= U'A' && klas [0] <= U'D') |
| 144 | drawAction (this, praat_getAction (i), i); |
| 145 | } |
| 146 | break; |
| 147 | case 5: |
| 148 | for (integer i = 1, n = praat_getNumberOfActions (); i <= n; i ++) { |
| 149 | Praat_Command cmd = praat_getAction (i); |
| 150 | conststring32 klas = cmd -> class1 -> className; |
| 151 | if (klas [0] >= U'E' && klas [0] <= U'H') |
| 152 | drawAction (this, praat_getAction (i), i); |
| 153 | } |
| 154 | break; |
| 155 | case 6: |
| 156 | for (integer i = 1, n = praat_getNumberOfActions (); i <= n; i ++) { |
| 157 | Praat_Command cmd = praat_getAction (i); |
| 158 | conststring32 klas = cmd -> class1 -> className; |
| 159 | if (klas [0] >= U'I' && klas [0] <= U'L') |
| 160 | drawAction (this, praat_getAction (i), i); |
| 161 | } |
| 162 | break; |
| 163 | case 7: |
| 164 | for (integer i = 1, n = praat_getNumberOfActions (); i <= n; i ++) { |
| 165 | Praat_Command cmd = praat_getAction (i); |
| 166 | conststring32 klas = cmd -> class1 -> className; |
| 167 | if (klas [0] >= U'M' && klas [0] <= U'O') |
| 168 | drawAction (this, praat_getAction (i), i); |
| 169 | } |
| 170 | break; |
| 171 | case 8: |
| 172 | for (integer i = 1, n = praat_getNumberOfActions (); i <= n; i ++) { |
| 173 | Praat_Command cmd = praat_getAction (i); |
| 174 | conststring32 klas = cmd -> class1 -> className; |
| 175 | if (klas [0] >= U'P' && klas [0] <= U'S') |
| 176 | drawAction (this, praat_getAction (i), i); |
| 177 | } |
| 178 | break; |
| 179 | case 9: |
| 180 | for (integer i = 1, n = praat_getNumberOfActions (); i <= n; i ++) { |
| 181 | Praat_Command cmd = praat_getAction (i); |
| 182 | conststring32 klas = cmd -> class1 -> className; |
| 183 | if (klas [0] >= U'T' && klas [0] <= U'Z') |
| 184 | drawAction (this, praat_getAction (i), i); |
| 185 | } |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | int structButtonEditor :: v_goToPage (conststring32 title) { |
| 191 | if (! title || ! title [0]) |
| 192 | return 0; |
| 193 | if (str32equ (title, U"Buttons")) |
| 194 | return 1; |
| 195 | switch (title [0]) { |
| 196 | case 'a': { // toggle visibility of action |
| 197 | integer i = Melder_atoi (& title [1]); |
| 198 | Praat_Command action = praat_getAction (i); |
| 199 | if (! action) |
| 200 | return 0; |
| 201 | if (action -> hidden) |
| 202 | praat_showAction (action -> class1, action -> class2, action -> class3, action -> title.get()); |
| 203 | else |
| 204 | praat_hideAction (action -> class1, action -> class2, action -> class3, action -> title.get()); |
| 205 | } break; |
| 206 | case 'm': { // toggle visibility of menu command |
| 207 | integer i = Melder_atoi (& title [1]); |
| 208 | Praat_Command menuCommand = praat_getMenuCommand (i); |
| 209 | if (! menuCommand) |
| 210 | return 0; |
| 211 | if (menuCommand -> hidden) |
| 212 | praat_showMenuCommand (menuCommand -> window.get(), menuCommand -> menu.get(), menuCommand -> title.get()); |
| 213 | else |
| 214 | praat_hideMenuCommand (menuCommand -> window.get(), menuCommand -> menu.get(), menuCommand -> title.get()); |
| 215 | } break; |
| 216 | case 'e': { // execute action |
| 217 | integer i = Melder_atoi (& title [1]); |
| 218 | Praat_Command action = praat_getAction (i); |
| 219 | if (! action || ! action -> callback) |
| 220 | return 0; |
| 221 | if (action -> title) { |
| 222 | UiHistory_write (U"\n"); |
| 223 | UiHistory_write_colonize (action -> title.get()); |
| 224 | } |
| 225 | if (action -> script) { |
| 226 | try { |
| 227 | DO_RunTheScriptFromAnyAddedMenuCommand (nullptr, 0, nullptr, action -> script.get(), nullptr, nullptr, false, nullptr, nullptr); |
| 228 | } catch (MelderError) { |
| 229 | Melder_flushError (U"Command not executed."); |
| 230 | } |
| 231 | } else { |
| 232 | try { |
| 233 | action -> callback (nullptr, 0, nullptr, nullptr, nullptr, nullptr, false, nullptr, nullptr); |
| 234 | } catch (MelderError) { |
| 235 | Melder_flushError (U"Command not executed."); |
| 236 | } |
| 237 | } |
| 238 | praat_updateSelection (); |
| 239 | } break; |
| 240 | case 'p': { // perform menu command |
| 241 | integer i = Melder_atoi (& title [1]); |
| 242 | Praat_Command menuCommand = praat_getMenuCommand (i); |
| 243 | if (! menuCommand || ! menuCommand -> callback) |
| 244 | return 0; |
| 245 | if (menuCommand -> title) { |
| 246 | UiHistory_write (U"\n"); |
| 247 | UiHistory_write_colonize (menuCommand -> title.get()); |
| 248 | } |
| 249 | if (menuCommand -> script) { |
| 250 | try { |
| 251 | DO_RunTheScriptFromAnyAddedMenuCommand (nullptr, 0, nullptr, menuCommand -> script.get(), nullptr, nullptr, false, nullptr, nullptr); |
| 252 | } catch (MelderError) { |
| 253 | Melder_flushError (U"Command not executed."); |
| 254 | } |
| 255 | } else { |
| 256 | try { |
| 257 | menuCommand -> callback (nullptr, 0, nullptr, nullptr, nullptr, nullptr, false, nullptr, nullptr); |
| 258 | } catch (MelderError) { |
| 259 | Melder_flushError (U"Command not executed."); |
| 260 | } |
| 261 | } |
| 262 | praat_updateSelection (); |
| 263 | } break; |
| 264 | default: break; |
| 265 | } |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static void which (ButtonEditor me, int show) { |
| 270 | my show = show; |
| 271 | GuiRadioButton_set (show == 1 ? my button1 : show == 2 ? my button2 : show == 3 ? my button3 : |
| 272 | show == 4 ? my buttonAD : show == 5 ? my buttonEH : show == 6 ? my buttonIL : |
| 273 | show == 7 ? my buttonMO : show == 8 ? my buttonPS : my buttonTZ |
| 274 | ); |
| 275 | HyperPage_goToPage (me, U"Buttons"); |
| 276 | } |
| 277 | |
| 278 | static void gui_radiobutton_cb_objects (ButtonEditor me, GuiRadioButtonEvent /* event */) { which (me, 1); } |
| 279 | static void gui_radiobutton_cb_picture (ButtonEditor me, GuiRadioButtonEvent /* event */) { which (me, 2); } |
| 280 | static void gui_radiobutton_cb_editors (ButtonEditor me, GuiRadioButtonEvent /* event */) { which (me, 3); } |
| 281 | static void gui_radiobutton_cb_actionsAD (ButtonEditor me, GuiRadioButtonEvent /* event */) { which (me, 4); } |
| 282 | static void gui_radiobutton_cb_actionsEH (ButtonEditor me, GuiRadioButtonEvent /* event */) { which (me, 5); } |
| 283 | static void gui_radiobutton_cb_actionsIL (ButtonEditor me, GuiRadioButtonEvent /* event */) { which (me, 6); } |
| 284 | static void gui_radiobutton_cb_actionsMO (ButtonEditor me, GuiRadioButtonEvent /* event */) { which (me, 7); } |
| 285 | static void gui_radiobutton_cb_actionsPS (ButtonEditor me, GuiRadioButtonEvent /* event */) { which (me, 8); } |
| 286 | static void gui_radiobutton_cb_actionsTZ (ButtonEditor me, GuiRadioButtonEvent /* event */) { which (me, 9); } |
| 287 | |
| 288 | void structButtonEditor :: v_createChildren () { |
| 289 | ButtonEditor_Parent :: v_createChildren (); |
| 290 | int x = 3, y = Machine_getMenuBarBottom () + 4; |
| 291 | GuiRadioGroup_begin (); |
| 292 | constexpr int LETTER_BUTTON_WIDTH = BUTTON_WIDTH * 2 / 3; |
| 293 | button1 = GuiRadioButton_createShown (our windowForm, x, x + BUTTON_WIDTH, y, y + Gui_RADIOBUTTON_HEIGHT, |
| 294 | U"Objects", gui_radiobutton_cb_objects, this, GuiRadioButton_SET); |
| 295 | x += BUTTON_WIDTH + 5; |
| 296 | button2 = GuiRadioButton_createShown (our windowForm, x, x + BUTTON_WIDTH, y, y + Gui_RADIOBUTTON_HEIGHT, |
| 297 | U"Picture", gui_radiobutton_cb_picture, this, 0); |
| 298 | x += BUTTON_WIDTH + 5; |
| 299 | button3 = GuiRadioButton_createShown (our windowForm, x, x + BUTTON_WIDTH, y, y + Gui_RADIOBUTTON_HEIGHT, |
| 300 | U"Editors", gui_radiobutton_cb_editors, this, 0); |
| 301 | x += BUTTON_WIDTH + 5; |
| 302 | buttonAD = GuiRadioButton_createShown (our windowForm, x, x + BUTTON_WIDTH + 30, y, y + Gui_RADIOBUTTON_HEIGHT, |
| 303 | U"Actions: A–D", gui_radiobutton_cb_actionsAD, this, 0); |
| 304 | x += BUTTON_WIDTH + 35; |
| 305 | buttonEH = GuiRadioButton_createShown (our windowForm, x, x + LETTER_BUTTON_WIDTH, y, y + Gui_RADIOBUTTON_HEIGHT, |
| 306 | U"E–H", gui_radiobutton_cb_actionsEH, this, 0); |
| 307 | x += LETTER_BUTTON_WIDTH + 5; |
| 308 | buttonIL = GuiRadioButton_createShown (our windowForm, x, x + LETTER_BUTTON_WIDTH - 5, y, y + Gui_RADIOBUTTON_HEIGHT, |
| 309 | U"I–L", gui_radiobutton_cb_actionsIL, this, 0); |
| 310 | x += LETTER_BUTTON_WIDTH - 5 + 5; |
| 311 | buttonMO = GuiRadioButton_createShown (our windowForm, x, x + LETTER_BUTTON_WIDTH, y, y + Gui_RADIOBUTTON_HEIGHT, |
| 312 | U"M–O", gui_radiobutton_cb_actionsMO, this, 0); |
| 313 | x += LETTER_BUTTON_WIDTH + 5; |
| 314 | buttonPS = GuiRadioButton_createShown (our windowForm, x, x + LETTER_BUTTON_WIDTH, y, y + Gui_RADIOBUTTON_HEIGHT, |
| 315 | U"P–S", gui_radiobutton_cb_actionsPS, this, 0); |
| 316 | x += LETTER_BUTTON_WIDTH + 5; |
| 317 | buttonTZ = GuiRadioButton_createShown (our windowForm, x, x + LETTER_BUTTON_WIDTH, y, y + Gui_RADIOBUTTON_HEIGHT, |
| 318 | U"T–Z", gui_radiobutton_cb_actionsTZ, this, 0); |
| 319 | GuiRadioGroup_end (); |
| 320 | } |
| 321 | |
| 322 | static void menu_cb_ButtonEditorHelp (ButtonEditor /* me */, EDITOR_ARGS) { Melder_help (U"ButtonEditor"); } |
| 323 | |
| 324 | void structButtonEditor :: v_createMenuItems_help (EditorMenu menu) { |
| 325 | ButtonEditor_Parent :: v_createMenuItems_help (menu); |
| 326 | EditorMenu_addCommand (menu, U"ButtonEditor help", '?', menu_cb_ButtonEditorHelp); |
| 327 | } |
| 328 | |
| 329 | autoButtonEditor ButtonEditor_create () { |
| 330 | try { |
| 331 | autoButtonEditor me = Thing_new (ButtonEditor); |
| 332 | HyperPage_init (me.get(), U"Buttons", nullptr); |
| 333 | which (me.get(), 1); |
| 334 | return me; |
| 335 | } catch (MelderError) { |
| 336 | Melder_throw (U"Buttons window not created."); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /* End of file ButtonEditor.cpp */ |