| 1 | /* TextGridArea.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-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. |
| 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 "TextGridArea.h" |
| 20 | #include "TextGrid_Sound.h" |
| 21 | #include "SpeechSynthesizer_and_TextGrid.h" |
| 22 | #include "LongSoundArea.h" // for drawing TextGrid and Sound, or for aligning TextGrid to Sound |
| 23 | #include "SoundAnalysisArea.h" // for drawing TextGrid and Pitch |
| 24 | #include "EditorM.h" |
| 25 | #include "SpeechRecognizer.h" |
| 26 | |
| 27 | Thing_implement (TextGridArea, FunctionArea, 0); |
| 28 | |
| 29 | #include "enums_getText.h" |
| 30 | #include "TextGridArea_enums.h" |
| 31 | #include "enums_getValue.h" |
| 32 | #include "TextGridArea_enums.h" |
| 33 | |
| 34 | #include "Prefs_define.h" |
| 35 | #include "TextGridArea_prefs.h" |
| 36 | #include "Prefs_install.h" |
| 37 | #include "TextGridArea_prefs.h" |
| 38 | #include "Prefs_copyToInstance.h" |
| 39 | #include "TextGridArea_prefs.h" |
| 40 | |
| 41 | /* |
| 42 | * The main invariant of the TextGridArea is that the selected interval |
| 43 | * always has the cursor in it, and that the cursor always selects an interval |
| 44 | * if the selected tier is an interval tier. |
| 45 | */ |
| 46 | |
| 47 | static void checkTierSelection (constTextGridArea me, conststring32 verbPhrase) { |
| 48 | if (my selectedTier < 1 || my selectedTier > my textGrid() -> tiers->size) |
| 49 | Melder_throw (U"To ", verbPhrase, U", first select a tier by clicking anywhere inside it."); |
| 50 | } |
| 51 | static integer getSelectedInterval (constTextGridArea me) { |
| 52 | Melder_assert (my selectedTier >= 1 || my selectedTier <= my textGrid() -> tiers->size); |
| 53 | const IntervalTier tier = (IntervalTier) my textGrid() -> tiers->at [my selectedTier]; |
| 54 | Melder_assert (tier -> classInfo == classIntervalTier); |
| 55 | return IntervalTier_timeToIndex (tier, my startSelection()); |
| 56 | } |
| 57 | |
| 58 | static integer getSelectedLeftBoundary (constTextGridArea me) { |
| 59 | Melder_assert (my selectedTier >= 1 || my selectedTier <= my textGrid() -> tiers->size); |
| 60 | const IntervalTier tier = (IntervalTier) my textGrid() -> tiers->at [my selectedTier]; |
| 61 | Melder_assert (tier -> classInfo == classIntervalTier); |
| 62 | return IntervalTier_hasBoundary (tier, my startSelection()); |
| 63 | } |
| 64 | static integer getSelectedPoint (constTextGridArea me) { |
| 65 | Melder_assert (my selectedTier >= 1 || my selectedTier <= my textGrid() -> tiers->size); |
| 66 | const TextTier tier = (TextTier) my textGrid() -> tiers->at [my selectedTier]; |
| 67 | Melder_assert (tier -> classInfo == classTextTier); |
| 68 | Melder_assert (isdefined (my startSelection())); |
| 69 | return AnyTier_hasPoint (tier->asAnyTier(), my startSelection()); |
| 70 | } |
| 71 | |
| 72 | static void timeToInterval (TextGridArea me, double t, integer tierNumber, |
| 73 | double *out_tmin, double *out_tmax) |
| 74 | { |
| 75 | Melder_assert (isdefined (t)); |
| 76 | const Function tier = my textGrid() -> tiers->at [tierNumber]; |
| 77 | IntervalTier intervalTier; |
| 78 | TextTier textTier; |
| 79 | AnyTextGridTier_identifyClass (tier, & intervalTier, & textTier); |
| 80 | if (intervalTier) { |
| 81 | integer iinterval = IntervalTier_timeToIndex (intervalTier, t); |
| 82 | if (iinterval == 0) { |
| 83 | if (t < my tmin()) { |
| 84 | iinterval = 1; |
| 85 | } else { |
| 86 | iinterval = intervalTier -> intervals.size; |
| 87 | } |
| 88 | } |
| 89 | Melder_assert (iinterval >= 1); |
| 90 | Melder_assert (iinterval <= intervalTier -> intervals.size); |
| 91 | const TextInterval interval = intervalTier -> intervals.at [iinterval]; |
| 92 | *out_tmin = interval -> xmin; |
| 93 | *out_tmax = interval -> xmax; |
| 94 | } else { |
| 95 | const integer n = textTier -> points.size; |
| 96 | if (n == 0) { |
| 97 | *out_tmin = my tmin(); |
| 98 | *out_tmax = my tmax(); |
| 99 | } else { |
| 100 | integer ipointleft = AnyTier_timeToLowIndex (textTier->asAnyTier(), t); |
| 101 | *out_tmin = ( ipointleft == 0 ? my tmin() : textTier -> points.at [ipointleft] -> number ); |
| 102 | *out_tmax = ( ipointleft == n ? my tmax() : textTier -> points.at [ipointleft + 1] -> number ); |
| 103 | } |
| 104 | } |
| 105 | Melder_clipLeft (my tmin(), out_tmin); |
| 106 | Melder_clipRight (out_tmax, my tmax()); |
| 107 | } |
| 108 | |
| 109 | static void insertBoundaryOrPoint (TextGridArea me, integer itier, double t1, double t2, bool insertSecond) { |
| 110 | const integer numberOfTiers = my textGrid() -> tiers->size; |
| 111 | if (itier < 1 || itier > numberOfTiers) |
| 112 | Melder_throw (U"No tier ", itier, U"."); |
| 113 | IntervalTier intervalTier; |
| 114 | TextTier textTier; |
| 115 | AnyTextGridTier_identifyClass (my textGrid() -> tiers->at [itier], & intervalTier, & textTier); |
| 116 | Melder_assert (t1 <= t2); |
| 117 | |
| 118 | if (intervalTier) { |
| 119 | autoTextInterval rightNewInterval, midNewInterval; |
| 120 | const bool t1IsABoundary = IntervalTier_hasTime (intervalTier, t1); |
| 121 | const bool t2IsABoundary = IntervalTier_hasTime (intervalTier, t2); |
| 122 | if (t1 == t2 && t1IsABoundary) |
| 123 | Melder_throw (U"Cannot add a boundary at ", Melder_fixed (t1, 6), U" seconds, because there is already a boundary there."); |
| 124 | if (t1IsABoundary && t2IsABoundary) |
| 125 | Melder_throw (U"Cannot add boundaries at ", Melder_fixed (t1, 6), U" and ", Melder_fixed (t2, 6), U" seconds, because there are already boundaries there."); |
| 126 | const integer iinterval = IntervalTier_timeToIndex (intervalTier, t1); |
| 127 | const integer iinterval2 = t1 == t2 ? iinterval : IntervalTier_timeToIndex (intervalTier, t2); |
| 128 | if (iinterval == 0 || iinterval2 == 0) |
| 129 | Melder_throw (U"The selection is outside the time domain of the intervals."); |
| 130 | const integer correctedIinterval2 = ( t2IsABoundary && iinterval2 == intervalTier -> intervals.size ? iinterval2 + 1 : iinterval2 ); |
| 131 | if (correctedIinterval2 > iinterval + 1 || (correctedIinterval2 > iinterval && ! t2IsABoundary)) |
| 132 | Melder_throw (U"The selection straddles a boundary."); |
| 133 | const TextInterval interval = intervalTier -> intervals.at [iinterval]; |
| 134 | |
| 135 | if (t1 == t2) { |
| 136 | FunctionArea_save (me, U"Add boundary"); |
| 137 | } else { |
| 138 | FunctionArea_save (me, U"Add interval"); |
| 139 | } |
| 140 | |
| 141 | if (itier == my selectedTier) { |
| 142 | /* |
| 143 | Divide up the label text into left, mid and right, depending on where the text selection is. |
| 144 | */ |
| 145 | integer left, right; |
| 146 | autostring32 text = GuiText_getStringAndSelectionPosition (my functionEditor() -> textArea, & left, & right); |
| 147 | const bool wholeTextIsSelected = ( right - left == Melder_length (text.get()) ); |
| 148 | rightNewInterval = TextInterval_create (t2, interval -> xmax, text.get() + right); |
| 149 | text [right] = U'\0'; |
| 150 | midNewInterval = TextInterval_create (t1, t2, text.get() + left); |
| 151 | if (! wholeTextIsSelected || t1 != t2) |
| 152 | text [left] = U'\0'; |
| 153 | TextInterval_setText (interval, text.get()); |
| 154 | } else { |
| 155 | /* |
| 156 | Move the text to the left of the boundary. |
| 157 | */ |
| 158 | rightNewInterval = TextInterval_create (t2, interval -> xmax, U""); |
| 159 | midNewInterval = TextInterval_create (t1, t2, U""); |
| 160 | } |
| 161 | if (t1IsABoundary) { |
| 162 | /* |
| 163 | Merge mid with left interval. |
| 164 | */ |
| 165 | if (interval -> xmin != t1) |
| 166 | Melder_crash (U"Boundary unequal: ", interval -> xmin, U" versus ", t1, U"."); |
| 167 | interval -> xmax = t2; |
| 168 | TextInterval_setText (interval, Melder_cat (interval -> text.get(), midNewInterval -> text.get())); |
| 169 | } else if (t2IsABoundary) { |
| 170 | /* |
| 171 | Merge mid and right interval. |
| 172 | */ |
| 173 | if (interval -> xmax != t2) |
| 174 | Melder_crash (U"Boundary unequal: ", interval -> xmax, U" versus ", t2, U"."); |
| 175 | interval -> xmax = t1; |
| 176 | Melder_assert (rightNewInterval -> xmin == t2); |
| 177 | Melder_assert (rightNewInterval -> xmax == t2); |
| 178 | rightNewInterval -> xmin = t1; |
| 179 | TextInterval_setText (rightNewInterval.get(), Melder_cat (midNewInterval -> text.get(), rightNewInterval -> text.get())); |
| 180 | } else { |
| 181 | interval -> xmax = t1; |
| 182 | if (t1 != t2) |
| 183 | intervalTier -> intervals. addItem_move (midNewInterval.move()); |
| 184 | } |
| 185 | intervalTier -> intervals. addItem_move (rightNewInterval.move()); |
| 186 | if (insertSecond && numberOfTiers >= 2 && t1 == t2) { |
| 187 | /* |
| 188 | Find the last time before t on another tier. |
| 189 | */ |
| 190 | double tlast = interval -> xmin; |
| 191 | for (integer jtier = 1; jtier <= numberOfTiers; jtier ++) { |
| 192 | if (jtier != itier) { |
| 193 | double startInterval, endInterval; |
| 194 | timeToInterval (me, t1, jtier, & startInterval, & endInterval); |
| 195 | if (startInterval > tlast) |
| 196 | tlast = startInterval; |
| 197 | } |
| 198 | } |
| 199 | if (tlast > interval -> xmin && tlast < t1) { |
| 200 | autoTextInterval newInterval = TextInterval_create (tlast, t1, U""); |
| 201 | interval -> xmax = tlast; |
| 202 | intervalTier -> intervals. addItem_move (newInterval.move()); |
| 203 | } |
| 204 | } |
| 205 | } else { |
| 206 | Melder_assert (isdefined (t1)); |
| 207 | if (AnyTier_hasPoint (textTier->asAnyTier(), t1)) |
| 208 | Melder_throw (U"Cannot add a point at ", Melder_fixed (t1, 6), U" seconds, because there is already a point there."); |
| 209 | |
| 210 | FunctionArea_save (me, U"Add point"); |
| 211 | |
| 212 | autoTextPoint newPoint = TextPoint_create (t1, U""); |
| 213 | textTier -> points. addItem_move (newPoint.move()); |
| 214 | } |
| 215 | my setSelection (t1, t1); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | #pragma mark - TextGridArea info |
| 220 | |
| 221 | void structTextGridArea :: v1_info () { |
| 222 | MelderInfo_writeLine (U"Selected tier: ", our selectedTier); |
| 223 | MelderInfo_writeLine (U"TextGrid uses text styles: ", our instancePref_useTextStyles()); |
| 224 | MelderInfo_writeLine (U"TextGrid font size: ", our instancePref_fontSize()); |
| 225 | MelderInfo_writeLine (U"TextGrid alignment: ", kGraphics_horizontalAlignment_getText (our instancePref_alignment())); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | #pragma mark - TextGridArea drawing |
| 230 | |
| 231 | void structTextGridArea :: v_specializedHighlightBackground () const { |
| 232 | Melder_assert (our textGrid()); |
| 233 | const integer numberOfTiers = our textGrid() -> tiers->size; |
| 234 | for (integer itier = 1; itier <= numberOfTiers; itier ++) { |
| 235 | const Function anyTier = our textGrid() -> tiers->at [itier]; |
| 236 | if (anyTier -> classInfo != classIntervalTier) |
| 237 | continue; |
| 238 | IntervalTier tier = (IntervalTier) anyTier; |
| 239 | /* |
| 240 | Highlight interval: yellow (selected) or green (matching label). |
| 241 | */ |
| 242 | const integer selectedInterval = ( itier == our selectedTier ? getSelectedInterval (this) : 0 ); |
| 243 | const integer numberOfIntervals = tier -> intervals.size; |
| 244 | for (integer iinterval = 1; iinterval <= numberOfIntervals; iinterval ++) { |
| 245 | const TextInterval interval = tier -> intervals.at [iinterval]; |
| 246 | /* mutable clip */ double startInterval = interval -> xmin, endInterval = interval -> xmax; |
| 247 | if (endInterval > our startWindow() && startInterval < our endWindow()) { // interval visible? |
| 248 | const bool intervalIsSelected = ( iinterval == selectedInterval ); |
| 249 | const bool labelDoesMatch = Melder_stringMatchesCriterion (interval -> text.get(), |
| 250 | our instancePref_greenMethod(), our instancePref_greenString(), true); |
| 251 | Melder_clipLeft (our startWindow(), & startInterval); |
| 252 | Melder_clipRight (& endInterval, our endWindow()); |
| 253 | const double bottom = 1.0 - double (itier) / numberOfTiers; |
| 254 | const double top = 1.0 - double (itier - 1) / numberOfTiers; |
| 255 | if (labelDoesMatch) { |
| 256 | Graphics_setColour (our graphics(), Melder_LIME); |
| 257 | Graphics_fillRectangle (our graphics(), startInterval, endInterval, bottom, top); |
| 258 | } |
| 259 | if (intervalIsSelected) { |
| 260 | Graphics_setColour (our graphics(), Melder_YELLOW); |
| 261 | Graphics_fillRectangle (our graphics(), |
| 262 | labelDoesMatch ? 0.85 * startInterval + 0.15 * endInterval : startInterval, |
| 263 | labelDoesMatch ? 0.15 * startInterval + 0.85 * endInterval : endInterval, |
| 264 | labelDoesMatch ? 0.85 * bottom + 0.15 * top : bottom, |
| 265 | labelDoesMatch ? 0.15 * bottom + 0.85 * top : top |
| 266 | ); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | Graphics_setColour (our graphics(), Melder_BLACK); |
| 272 | } |
| 273 | static void do_drawIntervalTier (TextGridArea me, IntervalTier tier, integer itier) { |
| 274 | #if gtk || defined (macintosh) |
| 275 | constexpr bool platformUsesAntiAliasing = true; |
| 276 | #else |
| 277 | constexpr bool platformUsesAntiAliasing = false; |
| 278 | #endif |
| 279 | integer x1DC, x2DC, yDC; |
| 280 | Graphics_WCtoDC (my graphics(), my startWindow(), 0.0, & x1DC, & yDC); |
| 281 | Graphics_WCtoDC (my graphics(), my endWindow(), 0.0, & x2DC, & yDC); |
| 282 | Graphics_setPercentSignIsItalic (my graphics(), my instancePref_useTextStyles()); |
| 283 | Graphics_setNumberSignIsBold (my graphics(), my instancePref_useTextStyles()); |
| 284 | Graphics_setCircumflexIsSuperscript (my graphics(), my instancePref_useTextStyles()); |
| 285 | Graphics_setUnderscoreIsSubscript (my graphics(), my instancePref_useTextStyles()); |
| 286 | |
| 287 | const integer selectedInterval = ( itier == my selectedTier ? getSelectedInterval (me) : 0 ); |
| 288 | const integer numberOfIntervals = tier -> intervals.size; |
| 289 | |
| 290 | /* |
| 291 | Draw a grey bar and a selection button at the cursor position. |
| 292 | */ |
| 293 | if (my startSelection() == my endSelection() && my startSelection() >= my startWindow() && my startSelection() <= my endWindow()) { |
| 294 | /* mutable search */ bool cursorAtBoundary = false; |
| 295 | for (integer iinterval = 2; iinterval <= numberOfIntervals; iinterval ++) { |
| 296 | const TextInterval interval = tier -> intervals.at [iinterval]; |
| 297 | if (interval -> xmin == my startSelection()) |
| 298 | cursorAtBoundary = true; |
| 299 | } |
| 300 | if (! cursorAtBoundary) { |
| 301 | const double dy = Graphics_dyMMtoWC (my graphics(), 1.5); |
| 302 | Graphics_setGrey (my graphics(), 0.8); |
| 303 | Graphics_setLineWidth (my graphics(), platformUsesAntiAliasing ? 6.0 : 5.0); |
| 304 | Graphics_line (my graphics(), my startSelection(), 0.0, my startSelection(), 1.0); |
| 305 | Graphics_setLineWidth (my graphics(), 1.0); |
| 306 | if (my editable()) { |
| 307 | Graphics_setColour (my graphics(), Melder_BLUE); |
| 308 | Graphics_circle_mm (my graphics(), my startSelection(), 1.0 - dy, 3.0); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | Graphics_setTextAlignment (my graphics(), my instancePref_alignment(), Graphics_HALF); |
| 314 | for (integer iinterval = 1; iinterval <= numberOfIntervals; iinterval ++) { |
| 315 | const TextInterval interval = tier -> intervals.at [iinterval]; |
| 316 | /* mutable clip */ double startInterval = interval -> xmin, endInterval = interval -> xmax; |
| 317 | Melder_clipLeft (my tmin(), & startInterval); |
| 318 | Melder_clipRight (& endInterval, my tmax()); |
| 319 | if (startInterval >= endInterval) |
| 320 | continue; |
| 321 | const bool intervalIsSelected = ( selectedInterval == iinterval ); |
| 322 | |
| 323 | /* |
| 324 | Draw left boundary. |
| 325 | */ |
| 326 | if (startInterval >= my startWindow() && startInterval <= my endWindow() && iinterval > 1) { |
| 327 | const bool boundaryIsSelected = ( my selectedTier == itier && startInterval == my startSelection() ); |
| 328 | Graphics_setColour (my graphics(), DataGui_defaultForegroundColour (me, boundaryIsSelected)); |
| 329 | Graphics_setLineWidth (my graphics(), platformUsesAntiAliasing ? 6.0 : 5.0); |
| 330 | Graphics_line (my graphics(), startInterval, 0.0, startInterval, 1.0); |
| 331 | |
| 332 | /* |
| 333 | Show alignment with cursor. |
| 334 | */ |
| 335 | if (startInterval == my startSelection()) { |
| 336 | Graphics_setColour (my graphics(), Melder_YELLOW); |
| 337 | Graphics_setLineWidth (my graphics(), platformUsesAntiAliasing ? 2.0 : 1.0); |
| 338 | Graphics_line (my graphics(), startInterval, 0.0, startInterval, 1.0); |
| 339 | } |
| 340 | } |
| 341 | Graphics_setLineWidth (my graphics(), 1.0); |
| 342 | |
| 343 | /* |
| 344 | Draw label text. |
| 345 | */ |
| 346 | if (interval -> text && endInterval >= my startWindow() && startInterval <= my endWindow()) { |
| 347 | const double t1 = std::max (my startWindow(), startInterval); |
| 348 | const double t2 = std::min (my endWindow(), endInterval); |
| 349 | Graphics_setColour (my graphics(), DataGui_defaultForegroundColour (me, intervalIsSelected)); |
| 350 | Graphics_rectangleText_wrapAndTruncate (my graphics(), t1, t2, 0.0, 1.0, interval -> text.get()); |
| 351 | Graphics_setColour (my graphics(), Melder_BLACK); |
| 352 | } |
| 353 | |
| 354 | } |
| 355 | Graphics_setPercentSignIsItalic (my graphics(), true); |
| 356 | Graphics_setNumberSignIsBold (my graphics(), true); |
| 357 | Graphics_setCircumflexIsSuperscript (my graphics(), true); |
| 358 | Graphics_setUnderscoreIsSubscript (my graphics(), true); |
| 359 | } |
| 360 | |
| 361 | static void do_drawTextTier (TextGridArea me, TextTier tier, integer itier) { |
| 362 | #if gtk || defined (macintosh) |
| 363 | constexpr bool platformUsesAntiAliasing = true; |
| 364 | #else |
| 365 | constexpr bool platformUsesAntiAliasing = false; |
| 366 | #endif |
| 367 | const integer numberOfPoints = tier -> points.size; |
| 368 | Graphics_setPercentSignIsItalic (my graphics(), my instancePref_useTextStyles()); |
| 369 | Graphics_setNumberSignIsBold (my graphics(), my instancePref_useTextStyles()); |
| 370 | Graphics_setCircumflexIsSuperscript (my graphics(), my instancePref_useTextStyles()); |
| 371 | Graphics_setUnderscoreIsSubscript (my graphics(), my instancePref_useTextStyles()); |
| 372 | |
| 373 | /* |
| 374 | Draw a grey bar and a selection button at the cursor position. |
| 375 | */ |
| 376 | if (my startSelection() == my endSelection() && my startSelection() >= my startWindow() && my startSelection() <= my endWindow()) { |
| 377 | bool cursorAtPoint = false; |
| 378 | for (integer ipoint = 1; ipoint <= numberOfPoints; ipoint ++) { |
| 379 | const TextPoint point = tier -> points.at [ipoint]; |
| 380 | if (point -> number == my startSelection()) |
| 381 | cursorAtPoint = true; |
| 382 | } |
| 383 | if (! cursorAtPoint) { |
| 384 | const double dy = Graphics_dyMMtoWC (my graphics(), 1.5); |
| 385 | Graphics_setGrey (my graphics(), 0.8); |
| 386 | Graphics_setLineWidth (my graphics(), platformUsesAntiAliasing ? 6.0 : 5.0); |
| 387 | Graphics_line (my graphics(), my startSelection(), 0.0, my startSelection(), 1.0); |
| 388 | Graphics_setLineWidth (my graphics(), 1.0); |
| 389 | if (my editable()) { |
| 390 | Graphics_setColour (my graphics(), DataGuiColour_EDITABLE); |
| 391 | Graphics_circle_mm (my graphics(), my startSelection(), 1.0 - dy, 3.0); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | Graphics_setTextAlignment (my graphics(), Graphics_CENTRE, Graphics_HALF); |
| 397 | for (integer ipoint = 1; ipoint <= numberOfPoints; ipoint ++) { |
| 398 | const TextPoint point = tier -> points.at [ipoint]; |
| 399 | const double t = point -> number; |
| 400 | if (t >= my startWindow() && t <= my endWindow()) { |
| 401 | const bool pointIsSelected = ( itier == my selectedTier && t == my startSelection() ); |
| 402 | Graphics_setColour (my graphics(), DataGui_defaultForegroundColour (me, pointIsSelected)); |
| 403 | Graphics_setLineWidth (my graphics(), platformUsesAntiAliasing ? 6.0 : 5.0); |
| 404 | Graphics_line (my graphics(), t, 0.0, t, 0.2); |
| 405 | Graphics_line (my graphics(), t, 0.8, t, 1); |
| 406 | Graphics_setLineWidth (my graphics(), 1.0); |
| 407 | |
| 408 | /* |
| 409 | Wipe out the cursor where the text is going to be. |
| 410 | */ |
| 411 | if (my startSelection() == t || my endSelection() == t) { |
| 412 | Graphics_setLineWidth (my graphics(), 1.0); |
| 413 | Graphics_setColour (my graphics(), DataGuiColour_AREA_BACKGROUND); |
| 414 | Graphics_line (my graphics(), t, 0.2, t, 0.8); |
| 415 | Graphics_setLineWidth (my graphics(), 1.0); |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | Show alignment with cursor. |
| 420 | */ |
| 421 | if (my startSelection() == my endSelection() && t == my startSelection()) { |
| 422 | Graphics_setColour (my graphics(), Melder_YELLOW); |
| 423 | Graphics_setLineWidth (my graphics(), platformUsesAntiAliasing ? 2.0 : 1.0); |
| 424 | Graphics_line (my graphics(), t, 0.0, t, 0.2); |
| 425 | Graphics_line (my graphics(), t, 0.8, t, 1.0); |
| 426 | Graphics_setLineWidth (my graphics(), 1.0); |
| 427 | } |
| 428 | Graphics_setColour (my graphics(), DataGui_defaultForegroundColour (me, pointIsSelected)); |
| 429 | if (point -> mark) |
| 430 | Graphics_text (my graphics(), t, 0.5, point -> mark.get()); |
| 431 | } |
| 432 | } |
| 433 | Graphics_setPercentSignIsItalic (my graphics(), true); |
| 434 | Graphics_setNumberSignIsBold (my graphics(), true); |
| 435 | Graphics_setCircumflexIsSuperscript (my graphics(), true); |
| 436 | Graphics_setUnderscoreIsSubscript (my graphics(), true); |
| 437 | } |
| 438 | void structTextGridArea :: v_drawInside () { |
| 439 | Graphics_Viewport vp2; |
| 440 | const integer numberOfTiers = our textGrid() -> tiers->size; |
| 441 | const enum kGraphics_font oldFont = Graphics_inqFont (our graphics()); |
| 442 | const double oldFontSize = Graphics_inqFontSize (our graphics()); |
| 443 | Graphics_setWindow (our graphics(), our startWindow(), our endWindow(), 0.0, 1.0); |
| 444 | const double xright = our functionViewerRight_WC(); |
| 445 | for (integer itier = 1; itier <= numberOfTiers; itier ++) { |
| 446 | const Function anyTier = our textGrid() -> tiers->at [itier]; |
| 447 | const bool tierIsSelected = ( itier == our selectedTier ); |
| 448 | const bool isIntervalTier = ( anyTier -> classInfo == classIntervalTier ); |
| 449 | vp2 = Graphics_insetViewport (our graphics(), 0.0, 1.0, |
| 450 | 1.0 - (double) itier / (double) numberOfTiers, |
| 451 | 1.0 - (double) (itier - 1) / (double) numberOfTiers |
| 452 | ); |
| 453 | Graphics_setColour (our graphics(), Melder_BLACK); |
| 454 | if (itier != 1) |
| 455 | Graphics_line (our graphics(), our startWindow(), 1.0, our endWindow(), 1.0); |
| 456 | |
| 457 | /* |
| 458 | Show the number and the name of the tier. |
| 459 | */ |
| 460 | Graphics_setColour (our graphics(), DataGui_defaultForegroundColour (this, tierIsSelected)); |
| 461 | Graphics_setFont (our graphics(), oldFont); |
| 462 | Graphics_setFontSize (our graphics(), 14); |
| 463 | Graphics_setTextAlignment (our graphics(), Graphics_RIGHT, Graphics_HALF); |
| 464 | Graphics_text (our graphics(), our startWindow(), 0.5, tierIsSelected ? U"☞ " : U"", itier); |
| 465 | Graphics_setFontSize (our graphics(), oldFontSize); |
| 466 | if (anyTier -> name && anyTier -> name [0]) { |
| 467 | Graphics_setTextAlignment (our graphics(), Graphics_LEFT, Graphics_HALF); |
| 468 | Graphics_rectangleText_maximalFit (our graphics(), our endWindow(), xright, 0.05, 0.5, |
| 469 | our instancePref_showNumberOf() == kTextGridArea_showNumberOf::NOTHING ? 0.0 : 0.5, 1.0, 0.01, 0.1, anyTier -> name.get()); |
| 470 | } |
| 471 | if (our instancePref_showNumberOf() != kTextGridArea_showNumberOf::NOTHING) { |
| 472 | Graphics_setTextAlignment (our graphics(), Graphics_LEFT, Graphics_HALF); |
| 473 | if (our instancePref_showNumberOf() == kTextGridArea_showNumberOf::INTERVALS_OR_POINTS) { |
| 474 | const integer count = ( isIntervalTier ? ((IntervalTier) anyTier) -> intervals.size : ((TextTier) anyTier) -> points.size ); |
| 475 | const integer position = ( itier == our selectedTier ? ( isIntervalTier ? getSelectedInterval (this) : getSelectedPoint (this) ) : 0 ); |
| 476 | if (position) |
| 477 | Graphics_rectangleText_maximalFit (our graphics(), our endWindow(), xright, 0.05, 0.5, |
| 478 | 0.0, 0.5, 0.01, 0.1, Melder_cat (U"(", position, U"/", count, U")")); |
| 479 | else |
| 480 | Graphics_rectangleText_maximalFit (our graphics(), our endWindow(), xright, 0.05, 0.5, |
| 481 | 0.0, 0.5, 0.01, 0.1, Melder_cat (U"(", count, U")")); |
| 482 | } else { |
| 483 | Melder_assert (our instancePref_showNumberOf() == kTextGridArea_showNumberOf::NONEMPTY_INTERVALS_OR_POINTS); |
| 484 | integer count = 0; |
| 485 | if (isIntervalTier) { |
| 486 | const IntervalTier tier = (IntervalTier) anyTier; |
| 487 | const integer numberOfIntervals = tier -> intervals.size; |
| 488 | for (integer iinterval = 1; iinterval <= numberOfIntervals; iinterval ++) { |
| 489 | const TextInterval interval = tier -> intervals.at [iinterval]; |
| 490 | if (interval -> text && interval -> text [0] != U'\0') |
| 491 | count ++; |
| 492 | } |
| 493 | } else { |
| 494 | const TextTier tier = (TextTier) anyTier; |
| 495 | const integer numberOfPoints = tier -> points.size; |
| 496 | for (integer ipoint = 1; ipoint <= numberOfPoints; ipoint ++) { |
| 497 | const TextPoint point = tier -> points.at [ipoint]; |
| 498 | if (point -> mark && point -> mark [0] != U'\0') |
| 499 | count ++; |
| 500 | } |
| 501 | } |
| 502 | Graphics_rectangleText_maximalFit (our graphics(), our endWindow(), xright, 0.05, 0.5, |
| 503 | 0.0, 0.5, 0.01, 0.1, Melder_cat (U"(##", count, U"#)")); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | Graphics_setColour (our graphics(), Melder_BLACK); |
| 508 | Graphics_setFont (our graphics(), kGraphics_font::TIMES); |
| 509 | Graphics_setFontSize (our graphics(), our instancePref_fontSize()); |
| 510 | if (isIntervalTier) |
| 511 | do_drawIntervalTier (this, (IntervalTier) anyTier, itier); |
| 512 | else |
| 513 | do_drawTextTier (this, (TextTier) anyTier, itier); |
| 514 | Graphics_resetViewport (our graphics(), vp2); |
| 515 | } |
| 516 | Graphics_setColour (our graphics(), Melder_BLACK); |
| 517 | Graphics_setFont (our graphics(), oldFont); |
| 518 | Graphics_setFontSize (our graphics(), oldFontSize); |
| 519 | |
| 520 | if (isdefined (our draggingTime) && hasBeenDraggedBeyondVicinityRadiusAtLeastOnce) { |
| 521 | Graphics_xorOn (our graphics(), Melder_MAROON); |
| 522 | for (integer itier = 1; itier <= numberOfTiers; itier ++) { |
| 523 | if (our draggingTiers [itier]) { |
| 524 | const double ymin = 1.0 - (double) itier / numberOfTiers; |
| 525 | const double ymax = 1.0 - (double) (itier - 1) / numberOfTiers; |
| 526 | Graphics_setLineWidth (our graphics(), 7.0); |
| 527 | Graphics_line (our graphics(), our draggingTime, ymin, our draggingTime, ymax); |
| 528 | } |
| 529 | } |
| 530 | our functionEditor() -> viewDataAsWorldByFraction (); |
| 531 | Graphics_setLineWidth (our graphics(), 1.0); |
| 532 | Graphics_line (our graphics(), our draggingTime, 0.0, our draggingTime, 1.0); |
| 533 | Graphics_text (our graphics(), our draggingTime, 1.0, Melder_fixed (our draggingTime, 6)); |
| 534 | Graphics_xorOff (our graphics()); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | |
| 539 | #pragma mark - TextGridArea tracking |
| 540 | |
| 541 | static integer localY_fraction_toTier (TextGridArea me, double localY_fraction) { |
| 542 | const integer numberOfTiers = my textGrid() -> tiers->size; |
| 543 | integer tierNumber = numberOfTiers - Melder_ifloor (localY_fraction * (double) numberOfTiers); |
| 544 | Melder_clip (1_integer, & tierNumber, numberOfTiers); |
| 545 | return tierNumber; |
| 546 | } |
| 547 | |
| 548 | bool structTextGridArea :: v_mouse (GuiDrawingArea_MouseEvent event, double x_world, double localY_fraction) { |
| 549 | const integer numberOfTiers = our textGrid() -> tiers->size; |
| 550 | const integer oldSelectedTier = our selectedTier; |
| 551 | |
| 552 | constexpr double clickingVicinityRadius_mm = 1.0; |
| 553 | constexpr double draggingVicinityRadius_mm = clickingVicinityRadius_mm + 0.2; // must be greater than `clickingVicinityRadius_mm` |
| 554 | constexpr double droppingVicinityRadius_mm = 1.5; |
| 555 | |
| 556 | const integer mouseTier = localY_fraction_toTier (this, localY_fraction); |
| 557 | |
| 558 | our draggingTime = undefined; // information to next expose event |
| 559 | if (event -> isClick()) { |
| 560 | if (isdefined (our anchorTime)) // sanity check for the fixed order click-drag-drop |
| 561 | return false; |
| 562 | Melder_assert (our clickedLeftBoundary == 0); |
| 563 | Melder_assert (! our hasBeenDraggedBeyondVicinityRadiusAtLeastOnce); |
| 564 | our draggingTiers. reset(); |
| 565 | /* |
| 566 | The user clicked in the grid part. |
| 567 | We select the tier in which they clicked. |
| 568 | */ |
| 569 | our selectedTier = mouseTier; |
| 570 | double startInterval, endInterval; |
| 571 | timeToInterval (this, x_world, our selectedTier, & startInterval, & endInterval); |
| 572 | |
| 573 | if (event -> optionKeyPressed != event -> commandKeyPressed) { |
| 574 | if (event -> isLeftBottomFunctionKeyPressed()) { |
| 575 | our setSelection (x_world - startInterval < endInterval - x_world ? startInterval : endInterval, our endSelection()); // to nearest boundary |
| 576 | return FunctionEditor_UPDATE_NEEDED; |
| 577 | } |
| 578 | if (event -> isRightBottomFunctionKeyPressed()) { |
| 579 | our setSelection (our startSelection(), x_world - startInterval < endInterval - x_world ? startInterval : endInterval); |
| 580 | return FunctionEditor_UPDATE_NEEDED; |
| 581 | } |
| 582 | } |
| 583 | Melder_assert (! our functionEditor() -> clickWasModifiedByOptionKey && ! our functionEditor() -> clickWasModifiedByCommandKey || |
| 584 | our functionEditor() -> clickWasModifiedByOptionKey && our functionEditor() -> clickWasModifiedByCommandKey); // either neither or both |
| 585 | |
| 586 | IntervalTier selectedIntervalTier; |
| 587 | TextTier selectedTextTier; |
| 588 | AnyTextGridTier_identifyClass (our textGrid() -> tiers->at [our selectedTier], & selectedIntervalTier, & selectedTextTier); |
| 589 | |
| 590 | if (x_world <= our startWindow() || x_world >= our endWindow()) |
| 591 | return FunctionEditor_UPDATE_NEEDED; |
| 592 | |
| 593 | /* |
| 594 | Get the time of the nearest boundary or point. |
| 595 | */ |
| 596 | if (selectedIntervalTier) { |
| 597 | const integer clickedIntervalNumber = IntervalTier_timeToIndex (selectedIntervalTier, x_world); |
| 598 | const bool theyClickedOutsidetheTimeDomainOfTheIntervals = ( clickedIntervalNumber == 0 ); |
| 599 | if (theyClickedOutsidetheTimeDomainOfTheIntervals) |
| 600 | return FunctionEditor_UPDATE_NEEDED; |
| 601 | const TextInterval interval = selectedIntervalTier -> intervals.at [clickedIntervalNumber]; |
| 602 | if (x_world > 0.5 * (interval -> xmin + interval -> xmax)) { |
| 603 | our anchorTime = interval -> xmax; |
| 604 | our clickedLeftBoundary = clickedIntervalNumber + 1; |
| 605 | } else { |
| 606 | our anchorTime = interval -> xmin; |
| 607 | our clickedLeftBoundary = clickedIntervalNumber; |
| 608 | } |
| 609 | } else { |
| 610 | const integer clickedPointNumber = AnyTier_timeToNearestIndex (selectedTextTier->asAnyTier(), x_world); |
| 611 | if (clickedPointNumber != 0) { |
| 612 | const TextPoint point = selectedTextTier -> points.at [clickedPointNumber]; |
| 613 | our anchorTime = point -> number; |
| 614 | } |
| 615 | } |
| 616 | Melder_assert (! (selectedIntervalTier && our clickedLeftBoundary == 0)); |
| 617 | |
| 618 | const bool nearBoundaryOrPoint = ( isdefined (our anchorTime) && |
| 619 | fabs (Graphics_dxWCtoMM (our graphics(), x_world - our anchorTime)) < 1.5 ); |
| 620 | Graphics_setWindow (our graphics(), our startWindow(), our endWindow(), 0.0, 1.0); |
| 621 | const double distanceToCursorCircle = ( our startSelection() != our endSelection() ? undefined : |
| 622 | Graphics_distanceWCtoMM (our graphics(), x_world, localY_fraction, |
| 623 | our startSelection(), |
| 624 | (numberOfTiers + 1 - our selectedTier) / double (numberOfTiers) - |
| 625 | Graphics_dyMMtoWC (our graphics(), 1.5)) |
| 626 | ); |
| 627 | trace (localY_fraction, U" ", distanceToCursorCircle); |
| 628 | const bool nearCursorCircle = ( distanceToCursorCircle < 1.5 ); |
| 629 | |
| 630 | if (nearBoundaryOrPoint) { |
| 631 | /* |
| 632 | Possibility 1: the user clicked near a boundary or point. |
| 633 | Perhaps drag it. |
| 634 | */ |
| 635 | bool boundaryOrPointIsMovable = true; |
| 636 | if (selectedIntervalTier) { |
| 637 | const bool isLeftEdgeOfFirstInterval = ( our clickedLeftBoundary <= 1 ); |
| 638 | const bool isRightEdgeOfLastInterval = ( our clickedLeftBoundary > selectedIntervalTier -> intervals.size ); |
| 639 | boundaryOrPointIsMovable = ! isLeftEdgeOfFirstInterval && ! isRightEdgeOfLastInterval; |
| 640 | } |
| 641 | /* |
| 642 | If the user clicked on an unselected boundary or point, we extend or shrink the selection to it. |
| 643 | */ |
| 644 | const bool rubberBanding = our functionEditor() -> clickWasModifiedByOptionKey && |
| 645 | our functionEditor() -> clickWasModifiedByCommandKey; |
| 646 | if (event -> shiftKeyPressed && ! rubberBanding) { |
| 647 | if (our anchorTime > 0.5 * (our startSelection() + our endSelection())) |
| 648 | our setSelection (our startSelection(), our anchorTime); |
| 649 | else |
| 650 | our setSelection (our anchorTime, our endSelection()); |
| 651 | } |
| 652 | if (! boundaryOrPointIsMovable) { |
| 653 | our draggingTime = undefined; |
| 654 | our hasBeenDraggedBeyondVicinityRadiusAtLeastOnce = false; |
| 655 | our anchorTime = undefined; |
| 656 | our clickedLeftBoundary = 0; |
| 657 | return FunctionEditor_UPDATE_NEEDED; |
| 658 | } |
| 659 | /* |
| 660 | Determine the set of selected boundaries and points, and the dragging range. |
| 661 | */ |
| 662 | our draggingTiers = zero_BOOLVEC (numberOfTiers); |
| 663 | if (rubberBanding) { |
| 664 | our leftDraggingBoundary = our startSelection(); |
| 665 | our rightDraggingBoundary = our endSelection(); |
| 666 | } else { |
| 667 | our leftDraggingBoundary = our tmin(); |
| 668 | our rightDraggingBoundary = our tmax(); |
| 669 | } |
| 670 | for (int itier = 1; itier <= numberOfTiers; itier ++) { |
| 671 | /* |
| 672 | If the user has pressed the shift key, let her drag all the boundaries and points at this time. |
| 673 | Otherwise, let her drag only the boundary or point on the clicked tier. |
| 674 | */ |
| 675 | if (itier == mouseTier || our functionEditor() -> clickWasModifiedByShiftKey == our instancePref_shiftDragMultiple()) { |
| 676 | IntervalTier intervalTier; |
| 677 | TextTier textTier; |
| 678 | AnyTextGridTier_identifyClass (our textGrid() -> tiers->at [itier], & intervalTier, & textTier); |
| 679 | if (intervalTier) { |
| 680 | const integer ibound = IntervalTier_hasBoundary (intervalTier, our anchorTime); |
| 681 | if (ibound) { |
| 682 | TextInterval leftInterval = intervalTier -> intervals.at [ibound - 1]; |
| 683 | TextInterval rightInterval = intervalTier -> intervals.at [ibound]; |
| 684 | our draggingTiers [itier] = true; |
| 685 | if (! rubberBanding) { |
| 686 | /* |
| 687 | Prevent the user from dragging the boundary past its left or right neighbours on the same tier. |
| 688 | */ |
| 689 | Melder_clipLeft (leftInterval -> xmin, & our leftDraggingBoundary); |
| 690 | Melder_clipRight (& our rightDraggingBoundary, rightInterval -> xmax); |
| 691 | } |
| 692 | } |
| 693 | } else { |
| 694 | Melder_assert (isdefined (our anchorTime)); |
| 695 | if (AnyTier_hasPoint (textTier->asAnyTier(), our anchorTime)) { |
| 696 | /* |
| 697 | Other than with boundaries on interval tiers, |
| 698 | points on text tiers can be dragged past their neighbours. |
| 699 | */ |
| 700 | our draggingTiers [itier] = true; |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | } else if (nearCursorCircle && our editable()) { |
| 706 | /* |
| 707 | Possibility 2: the user clicked near the cursor circle. |
| 708 | Insert boundary or point. There is no danger that we insert on top of an existing boundary or point, |
| 709 | because we are not 'nearBoundaryOrPoint'. |
| 710 | */ |
| 711 | Melder_assert (isdefined (our startSelection())); // precondition of v_updateText() |
| 712 | if (our selectedTier != oldSelectedTier) |
| 713 | our v_updateText(); // this puts the text of the newly clicked tier into the text area |
| 714 | insertBoundaryOrPoint (this, mouseTier, our startSelection(), our startSelection(), false); |
| 715 | FunctionArea_broadcastDataChanged (this); |
| 716 | } else { |
| 717 | /* |
| 718 | Possibility 3: the user clicked in empty space. |
| 719 | Select the interval, if any. |
| 720 | */ |
| 721 | if (selectedIntervalTier) |
| 722 | our setSelection (startInterval, endInterval); |
| 723 | } |
| 724 | } else if (event -> isDrag ()) { |
| 725 | if (our editable() && isdefined (our anchorTime) && our draggingTiers.size > 0) { |
| 726 | our draggingTime = x_world; |
| 727 | if (! our hasBeenDraggedBeyondVicinityRadiusAtLeastOnce) { |
| 728 | const double distanceToAnchor_mm = fabs (Graphics_dxWCtoMM (our graphics(), x_world - our anchorTime)); |
| 729 | if (distanceToAnchor_mm > draggingVicinityRadius_mm) |
| 730 | our hasBeenDraggedBeyondVicinityRadiusAtLeastOnce = true; |
| 731 | } |
| 732 | } |
| 733 | } else if (event -> isDrop ()) { |
| 734 | if (isundef (our anchorTime) || our draggingTiers.size == 0) { // TODO: figure out a circumstance under which anchorTime could be undefined |
| 735 | our draggingTime = undefined; |
| 736 | our hasBeenDraggedBeyondVicinityRadiusAtLeastOnce = false; |
| 737 | our anchorTime = undefined; |
| 738 | our clickedLeftBoundary = 0; |
| 739 | return FunctionEditor_UPDATE_NEEDED; |
| 740 | } |
| 741 | /* |
| 742 | If the user shift-clicked, we extend the selection (this already happened during click()). |
| 743 | */ |
| 744 | if (event -> shiftKeyPressed && ! our hasBeenDraggedBeyondVicinityRadiusAtLeastOnce) { |
| 745 | our draggingTime = undefined; |
| 746 | our hasBeenDraggedBeyondVicinityRadiusAtLeastOnce = false; |
| 747 | our anchorTime = undefined; |
| 748 | our clickedLeftBoundary = 0; |
| 749 | return FunctionEditor_UPDATE_NEEDED; |
| 750 | } |
| 751 | /* |
| 752 | If the user dropped near an existing boundary in an unselected tier, |
| 753 | we snap to that mark. |
| 754 | */ |
| 755 | const integer itierDrop = localY_fraction_toTier (this, localY_fraction); |
| 756 | bool droppedOnABoundaryOrPointInsideAnUnselectedTier = false; |
| 757 | if (x_world > 0.0 && ! our draggingTiers [itierDrop]) { // dropped inside an unselected tier? |
| 758 | const Function anyTierDrop = our textGrid() -> tiers->at [itierDrop]; |
| 759 | if (anyTierDrop -> classInfo == classIntervalTier) { |
| 760 | const IntervalTier tierDrop = (IntervalTier) anyTierDrop; |
| 761 | for (integer ibound = 1; ibound < tierDrop -> intervals.size; ibound ++) { |
| 762 | const TextInterval left = tierDrop -> intervals.at [ibound]; |
| 763 | const double mouseDistanceToBoundary = fabs (Graphics_dxWCtoMM (our graphics(), x_world - left -> xmax)); |
| 764 | if (mouseDistanceToBoundary < droppingVicinityRadius_mm) { |
| 765 | x_world = left -> xmax; // snap to boundary |
| 766 | droppedOnABoundaryOrPointInsideAnUnselectedTier = true; |
| 767 | } |
| 768 | } |
| 769 | } else { |
| 770 | const TextTier tierDrop = (TextTier) anyTierDrop; |
| 771 | for (integer ipoint = 1; ipoint <= tierDrop -> points.size; ipoint ++) { |
| 772 | const TextPoint point = tierDrop -> points.at [ipoint]; |
| 773 | const double mouseDistanceToPoint_mm = fabs (Graphics_dxWCtoMM (our graphics(), x_world - point -> number)); |
| 774 | if (mouseDistanceToPoint_mm < droppingVicinityRadius_mm) { |
| 775 | x_world = point -> number; // snap to point |
| 776 | droppedOnABoundaryOrPointInsideAnUnselectedTier = true; |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | /* |
| 782 | If the user dropped near the cursor (outside the anchor), |
| 783 | we snap to the cursor. |
| 784 | */ |
| 785 | if (our startSelection() == our endSelection() && our startSelection() != our anchorTime) { |
| 786 | const double mouseDistanceToCursor = fabs (Graphics_dxWCtoMM (our graphics(), x_world - our startSelection())); |
| 787 | if (mouseDistanceToCursor < droppingVicinityRadius_mm) |
| 788 | x_world = our startSelection(); |
| 789 | } |
| 790 | /* |
| 791 | If the user wiggled near the anchor, we snap to the anchor and bail out |
| 792 | (a boundary has been selected, but nothing has been dragged). |
| 793 | */ |
| 794 | if (! our hasBeenDraggedBeyondVicinityRadiusAtLeastOnce && ! droppedOnABoundaryOrPointInsideAnUnselectedTier) { |
| 795 | our setSelection (our anchorTime, our anchorTime); |
| 796 | our draggingTime = undefined; |
| 797 | our hasBeenDraggedBeyondVicinityRadiusAtLeastOnce = false; |
| 798 | our anchorTime = undefined; |
| 799 | our clickedLeftBoundary = 0; |
| 800 | return FunctionEditor_UPDATE_NEEDED; |
| 801 | } |
| 802 | |
| 803 | if (! our editable()) { |
| 804 | our draggingTime = undefined; |
| 805 | our hasBeenDraggedBeyondVicinityRadiusAtLeastOnce = false; |
| 806 | our anchorTime = undefined; |
| 807 | our clickedLeftBoundary = 0; |
| 808 | return FunctionEditor_UPDATE_NEEDED; |
| 809 | } |
| 810 | /* |
| 811 | We cannot move a boundary out of the dragging range. |
| 812 | */ |
| 813 | if (x_world <= our leftDraggingBoundary || x_world >= our rightDraggingBoundary) { |
| 814 | Melder_beep (); |
| 815 | our draggingTime = undefined; |
| 816 | our hasBeenDraggedBeyondVicinityRadiusAtLeastOnce = false; |
| 817 | our anchorTime = undefined; |
| 818 | our clickedLeftBoundary = 0; |
| 819 | return FunctionEditor_UPDATE_NEEDED; |
| 820 | } |
| 821 | |
| 822 | const bool rubberBanding = our functionEditor() -> clickWasModifiedByOptionKey && |
| 823 | our functionEditor() -> clickWasModifiedByCommandKey; |
| 824 | FunctionArea_save (this, rubberBanding ? U"Rubber banding" : U"Drag"); |
| 825 | |
| 826 | for (integer itier = 1; itier <= numberOfTiers; itier ++) { |
| 827 | if (! our draggingTiers [itier]) |
| 828 | continue; |
| 829 | IntervalTier intervalTier; |
| 830 | TextTier textTier; |
| 831 | AnyTextGridTier_identifyClass (our textGrid() -> tiers->at [itier], & intervalTier, & textTier); |
| 832 | if (rubberBanding) { |
| 833 | if (intervalTier) { |
| 834 | for (integer iinterval = 1; iinterval <= intervalTier -> intervals.size; iinterval ++) { |
| 835 | const mutableTextInterval interval = intervalTier -> intervals.at [iinterval]; |
| 836 | if (interval -> xmin > leftDraggingBoundary && interval -> xmin < rightDraggingBoundary) { |
| 837 | if (interval -> xmin < our anchorTime) |
| 838 | NUMscale (& interval -> xmin, our leftDraggingBoundary, our anchorTime, our leftDraggingBoundary, x_world); |
| 839 | else |
| 840 | NUMscale (& interval -> xmin, our anchorTime, our rightDraggingBoundary, x_world, our rightDraggingBoundary); |
| 841 | } |
| 842 | if (interval -> xmax > leftDraggingBoundary && interval -> xmax < rightDraggingBoundary) { |
| 843 | if (interval -> xmax < our anchorTime) |
| 844 | NUMscale (& interval -> xmax, our leftDraggingBoundary, our anchorTime, our leftDraggingBoundary, x_world); |
| 845 | else |
| 846 | NUMscale (& interval -> xmax, our anchorTime, our rightDraggingBoundary, x_world, our rightDraggingBoundary); |
| 847 | } |
| 848 | } |
| 849 | } else { |
| 850 | for (integer ipoint = 1; ipoint <= textTier -> points.size; ipoint ++) { |
| 851 | const mutableTextPoint point = textTier -> points.at [ipoint]; |
| 852 | if (point -> number > leftDraggingBoundary && point -> number < rightDraggingBoundary) { |
| 853 | if (point -> number < our anchorTime) |
| 854 | NUMscale (& point -> number, our leftDraggingBoundary, our anchorTime, our leftDraggingBoundary, x_world); |
| 855 | else |
| 856 | NUMscale (& point -> number, our anchorTime, our rightDraggingBoundary, x_world, our rightDraggingBoundary); |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | } else { |
| 861 | if (intervalTier) { |
| 862 | const integer numberOfIntervals = intervalTier -> intervals.size; |
| 863 | for (integer ibound = 2; ibound <= numberOfIntervals; ibound ++) { |
| 864 | TextInterval left = intervalTier -> intervals.at [ibound - 1], right = intervalTier -> intervals.at [ibound]; |
| 865 | if (left -> xmax == our anchorTime) { // boundary dragged? |
| 866 | left -> xmax = right -> xmin = x_world; // move boundary to drop site |
| 867 | break; |
| 868 | } |
| 869 | } |
| 870 | } else { |
| 871 | Melder_assert (isdefined (our anchorTime)); |
| 872 | const integer iDraggedPoint = AnyTier_hasPoint (textTier->asAnyTier(), our anchorTime); |
| 873 | if (iDraggedPoint) { |
| 874 | Melder_assert (isdefined (x_world)); |
| 875 | const integer dropSiteHasPoint = AnyTier_hasPoint (textTier->asAnyTier(), x_world); |
| 876 | if (dropSiteHasPoint != 0) { |
| 877 | Melder_warning (U"Cannot drop point on an existing point."); |
| 878 | } else { |
| 879 | const TextPoint point = textTier -> points.at [iDraggedPoint]; |
| 880 | /* |
| 881 | Move point to drop site. May have passed another point. |
| 882 | */ |
| 883 | autoTextPoint newPoint = Data_copy (point); |
| 884 | newPoint -> number = x_world; // move point to drop site |
| 885 | textTier -> points. removeItem (iDraggedPoint); |
| 886 | textTier -> points. addItem_move (newPoint.move()); |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | Select the drop site. |
| 895 | */ |
| 896 | our setSelection (x_world, x_world); |
| 897 | our draggingTime = undefined; |
| 898 | our hasBeenDraggedBeyondVicinityRadiusAtLeastOnce = false; |
| 899 | our anchorTime = undefined; |
| 900 | our clickedLeftBoundary = 0; |
| 901 | FunctionArea_broadcastDataChanged (this); |
| 902 | } |
| 903 | return FunctionEditor_UPDATE_NEEDED; |
| 904 | } |
| 905 | |
| 906 | |
| 907 | #pragma mark - TextGridArea File/Save |
| 908 | |
| 909 | static void menu_cb_SaveWholeTextGridAsTextFile (TextGridArea me, EDITOR_ARGS) { |
| 910 | EDITOR_FORM_SAVE (U"Save whole TextGrid as text file", nullptr) |
| 911 | Melder_sprint (defaultName,300, my textGrid() -> name.get(), U".TextGrid"); |
| 912 | EDITOR_DO_SAVE |
| 913 | Data_writeToTextFile (my textGrid(), file); |
| 914 | EDITOR_END |
| 915 | } |
| 916 | void structTextGridArea :: v_createMenuItems_save (EditorMenu menu) { |
| 917 | FunctionAreaMenu_addCommand (menu, U"- Save TextGrid to disk:", 0, nullptr, this); |
| 918 | FunctionAreaMenu_addCommand (menu, |
| 919 | U"Save whole TextGrid as text file... || Save TextGrid as text file... || Write TextGrid to text file...", |
| 920 | 'S' | GuiMenu_DEPTH_1, menu_cb_SaveWholeTextGridAsTextFile, this |
| 921 | ); |
| 922 | } |
| 923 | |
| 924 | |
| 925 | #pragma mark - TextGridArea Edit menu |
| 926 | |
| 927 | #ifndef macintosh |
| 928 | static void menu_cb_Cut (TextGridArea me, EDITOR_ARGS) { |
| 929 | GuiText_cut (my functionEditor() -> textArea); |
| 930 | } |
| 931 | static void menu_cb_Copy (TextGridArea me, EDITOR_ARGS) { |
| 932 | GuiText_copy (my functionEditor() -> textArea); |
| 933 | } |
| 934 | static void menu_cb_Paste (TextGridArea me, EDITOR_ARGS) { |
| 935 | GuiText_paste (my functionEditor() -> textArea); |
| 936 | } |
| 937 | static void menu_cb_Erase (TextGridArea me, EDITOR_ARGS) { |
| 938 | GuiText_remove (my functionEditor() -> textArea); |
| 939 | } |
| 940 | #endif |
| 941 | |
| 942 | static void menu_cb_ConvertToBackslashTrigraphs (TextGridArea me, EDITOR_ARGS) { |
| 943 | FunctionArea_save (me, U"Convert to Backslash Trigraphs"); |
| 944 | TextGrid_convertToBackslashTrigraphs (my textGrid()); |
| 945 | Melder_assert (isdefined (my startSelection())); // precondition of FunctionEditor_updateText() |
| 946 | FunctionArea_broadcastDataChanged (me); |
| 947 | } |
| 948 | static void menu_cb_ConvertToUnicode (TextGridArea me, EDITOR_ARGS) { |
| 949 | FunctionArea_save (me, U"Convert to Unicode"); |
| 950 | TextGrid_convertToUnicode (my textGrid()); |
| 951 | Melder_assert (isdefined (my startSelection())); // precondition of FunctionEditor_updateText() |
| 952 | FunctionArea_broadcastDataChanged (me); |
| 953 | } |
| 954 | static void findInTier (TextGridArea me) { |
| 955 | checkTierSelection (me, U"find a text"); |
| 956 | Function anyTier = my textGrid() -> tiers->at [my selectedTier]; |
| 957 | if (anyTier -> classInfo == classIntervalTier) { |
| 958 | const IntervalTier tier = (IntervalTier) anyTier; |
| 959 | integer iinterval = IntervalTier_timeToIndex (tier, my startSelection()) + 1; |
| 960 | while (iinterval <= tier -> intervals.size) { |
| 961 | TextInterval interval = tier -> intervals.at [iinterval]; |
| 962 | conststring32 text = interval -> text.get(); |
| 963 | if (text) { |
| 964 | const char32 *position = str32str (text, my findString.get()); |
| 965 | if (position) { |
| 966 | my setSelection (interval -> xmin, interval -> xmax); |
| 967 | Melder_assert (isdefined (my startSelection())); // precondition of FunctionEditor_scrollToNewSelection() |
| 968 | FunctionEditor_scrollToNewSelection (my functionEditor(), my startSelection()); |
| 969 | if (my editable ()) |
| 970 | GuiText_setSelection (my functionEditor() -> textArea, |
| 971 | position - text, position - text + Melder_length (my findString.get())); |
| 972 | return; |
| 973 | } |
| 974 | } |
| 975 | iinterval ++; |
| 976 | } |
| 977 | if (iinterval > tier -> intervals.size) |
| 978 | Melder_beep (); |
| 979 | } else { |
| 980 | TextTier tier = (TextTier) anyTier; |
| 981 | integer ipoint = AnyTier_timeToLowIndex (tier->asAnyTier(), my startSelection()) + 1; |
| 982 | while (ipoint <= tier -> points.size) { |
| 983 | const TextPoint point = tier->points.at [ipoint]; |
| 984 | conststring32 text = point -> mark.get(); |
| 985 | if (text) { |
| 986 | const char32 *const position = str32str (text, my findString.get()); |
| 987 | if (position) { |
| 988 | my setSelection (point -> number, point -> number); |
| 989 | Melder_assert (isdefined (my startSelection())); // precondition of FunctionEditor_scrollToNewSelection() |
| 990 | FunctionEditor_scrollToNewSelection (my functionEditor(), point -> number); |
| 991 | if (my editable ()) |
| 992 | GuiText_setSelection (my functionEditor() -> textArea, |
| 993 | position - text, position - text + Melder_length (my findString.get())); |
| 994 | return; |
| 995 | } |
| 996 | } |
| 997 | ipoint ++; |
| 998 | } |
| 999 | if (ipoint > tier -> points.size) |
| 1000 | Melder_beep (); |
| 1001 | } |
| 1002 | } |
| 1003 | static void do_find (TextGridArea me) { |
| 1004 | if (my findString) { |
| 1005 | if (my editable ()) { |
| 1006 | integer left, right; |
| 1007 | autostring32 label = GuiText_getStringAndSelectionPosition (my functionEditor() -> textArea, & left, & right); |
| 1008 | const char32 *const position = str32str (& label [right], my findString.get()); // CRLF BUG? |
| 1009 | if (position) |
| 1010 | GuiText_setSelection (my functionEditor() -> textArea, |
| 1011 | position - label.get(), position - label.get() + Melder_length (my findString.get())); |
| 1012 | else |
| 1013 | findInTier (me); |
| 1014 | } else |
| 1015 | findInTier (me); |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | static void menu_cb_Find (TextGridArea me, EDITOR_ARGS) { |
| 1020 | EDITOR_FORM (U"Find text", nullptr) |
| 1021 | TEXTFIELD (findString, U"Text", U"", 3) |
| 1022 | EDITOR_OK |
| 1023 | EDITOR_DO |
| 1024 | my findString = Melder_dup (findString); |
| 1025 | do_find (me); |
| 1026 | EDITOR_END |
| 1027 | } |
| 1028 | static void menu_cb_FindAgain (TextGridArea me, EDITOR_ARGS) { |
| 1029 | do_find (me); |
| 1030 | } |
| 1031 | void structTextGridArea :: v_createMenuItems_edit (EditorMenu menu) { |
| 1032 | #ifndef macintosh |
| 1033 | if (our editable()) { |
| 1034 | FunctionAreaMenu_addCommand (menu, U"-- cut copy paste --", 0, nullptr, this); |
| 1035 | FunctionAreaMenu_addCommand (menu, U"Cut text", 'X', menu_cb_Cut, this); |
| 1036 | FunctionAreaMenu_addCommand (menu, U"Cut", GuiMenu_HIDDEN, menu_cb_Cut, this); |
| 1037 | FunctionAreaMenu_addCommand (menu, U"Copy text", 'C', menu_cb_Copy, this); |
| 1038 | FunctionAreaMenu_addCommand (menu, U"Copy", GuiMenu_HIDDEN, menu_cb_Copy, this); |
| 1039 | FunctionAreaMenu_addCommand (menu, U"Paste text", 'V', menu_cb_Paste, this); |
| 1040 | FunctionAreaMenu_addCommand (menu, U"Paste", GuiMenu_HIDDEN, menu_cb_Paste, this); |
| 1041 | FunctionAreaMenu_addCommand (menu, U"Erase text", 0, menu_cb_Erase, this); |
| 1042 | FunctionAreaMenu_addCommand (menu, U"Erase", GuiMenu_HIDDEN, menu_cb_Erase, this); |
| 1043 | } |
| 1044 | #endif |
| 1045 | FunctionAreaMenu_addCommand (menu, U"-- search --", 0, nullptr, this); |
| 1046 | FunctionAreaMenu_addCommand (menu, U"Find...", 'F', menu_cb_Find, this); |
| 1047 | FunctionAreaMenu_addCommand (menu, U"Find again", 'G', menu_cb_FindAgain, this); |
| 1048 | } |
| 1049 | |
| 1050 | |
| 1051 | #pragma mark - TextGridArea Query |
| 1052 | |
| 1053 | static void QUERY_DATA_FOR_REAL__GetStartingPointOfInterval (TextGridArea me, EDITOR_ARGS) { |
| 1054 | QUERY_DATA_FOR_REAL |
| 1055 | checkTierSelection (me, U"query the starting point of an interval"); |
| 1056 | const Function anyTier = my textGrid() -> tiers->at [my selectedTier]; |
| 1057 | Melder_require (anyTier -> classInfo == classIntervalTier, |
| 1058 | U"The selected tier is not an interval tier."); |
| 1059 | const IntervalTier tier = (IntervalTier) anyTier; |
| 1060 | const integer iinterval = IntervalTier_timeToIndex (tier, my startSelection()); |
| 1061 | const double result = ( iinterval < 1 || iinterval > tier -> intervals.size ? undefined : |
| 1062 | tier -> intervals.at [iinterval] -> xmin ); |
| 1063 | QUERY_DATA_FOR_REAL_END (U" seconds") |
| 1064 | } |
| 1065 | |
| 1066 | static void QUERY_DATA_FOR_REAL__GetEndPointOfInterval (TextGridArea me, EDITOR_ARGS) { |
| 1067 | QUERY_DATA_FOR_REAL |
| 1068 | checkTierSelection (me, U"query the end point of an interval"); |
| 1069 | const Function anyTier = my textGrid() -> tiers->at [my selectedTier]; |
| 1070 | Melder_require (anyTier -> classInfo == classIntervalTier, |
| 1071 | U"The selected tier is not an interval tier."); |
| 1072 | const IntervalTier tier = (IntervalTier) anyTier; |
| 1073 | const integer iinterval = IntervalTier_timeToIndex (tier, my startSelection()); |
| 1074 | const double result = ( iinterval < 1 || iinterval > tier -> intervals.size ? undefined : |
| 1075 | tier -> intervals.at [iinterval] -> xmax ); |
| 1076 | QUERY_DATA_FOR_REAL_END (U" seconds") |
| 1077 | } |
| 1078 | |
| 1079 | static void QUERY_DATA_FOR_STRING__GetLabelOfInterval (TextGridArea me, EDITOR_ARGS) { |
| 1080 | QUERY_DATA_FOR_STRING |
| 1081 | checkTierSelection (me, U"query the label of an interval"); |
| 1082 | const Function anyTier = my textGrid() -> tiers->at [my selectedTier]; |
| 1083 | Melder_require (anyTier -> classInfo == classIntervalTier, |
| 1084 | U"The selected tier is not an interval tier."); |
| 1085 | const IntervalTier tier = (IntervalTier) anyTier; |
| 1086 | const integer iinterval = IntervalTier_timeToIndex (tier, my startSelection()); |
| 1087 | const conststring32 result = ( iinterval < 1 || iinterval > tier -> intervals.size ? U"" : |
| 1088 | tier -> intervals.at [iinterval] -> text.get() ); |
| 1089 | QUERY_DATA_FOR_STRING_END |
| 1090 | } |
| 1091 | |
| 1092 | |
| 1093 | #pragma mark - TextGridArea changing |
| 1094 | |
| 1095 | static void gui_text_cb_changed (TextGridArea me, GuiTextEvent /* event */) { |
| 1096 | trace (my suppressRedraw); |
| 1097 | if (my suppressRedraw) |
| 1098 | return; // prevent infinite loop if 'draw' method or Editor_broadcastChange calls GuiText_setString |
| 1099 | if (my selectedTier) { |
| 1100 | autostring32 text = GuiText_getString (my functionEditor() -> textArea); |
| 1101 | IntervalTier intervalTier; |
| 1102 | TextTier textTier; |
| 1103 | AnyTextGridTier_identifyClass (my textGrid() -> tiers->at [my selectedTier], & intervalTier, & textTier); |
| 1104 | if (intervalTier) { |
| 1105 | const integer selectedInterval = getSelectedInterval (me); |
| 1106 | if (selectedInterval) { |
| 1107 | TextInterval interval = intervalTier -> intervals.at [selectedInterval]; |
| 1108 | TextInterval_setText (interval, text.get()); |
| 1109 | my suppressTextCursorJump = true; |
| 1110 | FunctionArea_broadcastDataChanged (me); |
| 1111 | my suppressTextCursorJump = false; |
| 1112 | } |
| 1113 | } else { |
| 1114 | const integer selectedPoint = getSelectedPoint (me); |
| 1115 | if (selectedPoint) { |
| 1116 | TextPoint point = textTier -> points.at [selectedPoint]; |
| 1117 | point -> mark. reset(); |
| 1118 | if (Melder_findInk (text.get())) // any visible characters? |
| 1119 | point -> mark = Melder_dup_f (text.get()); |
| 1120 | my suppressTextCursorJump = true; |
| 1121 | FunctionArea_broadcastDataChanged (me); |
| 1122 | my suppressTextCursorJump = false; |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | |
| 1129 | #pragma mark - TextGridArea TextGrid/Prefs |
| 1130 | |
| 1131 | static void menu_cb_TextGridSettings (TextGridArea me, EDITOR_ARGS) { |
| 1132 | EDITOR_FORM (U"TextGrid settings", nullptr) |
| 1133 | POSITIVE (fontSize, U"Font size (points)", my default_fontSize()) |
| 1134 | OPTIONMENU_ENUM (kGraphics_horizontalAlignment, textAlignmentInIntervals, |
| 1135 | U"Text alignment in intervals", kGraphics_horizontalAlignment::DEFAULT) |
| 1136 | OPTIONMENU (useTextStyles, U"The symbols %#_^ in labels", my default_useTextStyles() + 1) |
| 1137 | OPTION (U"are shown as typed") |
| 1138 | OPTION (U"mean italic/bold/sub/super") |
| 1139 | OPTIONMENU_VARIABLE (shiftDragMultiple) |
| 1140 | if (my editable()) { |
| 1141 | OPTIONMENU_FIELD (shiftDragMultiple, U"With the shift key, you drag", my default_shiftDragMultiple() + 1) |
| 1142 | OPTION (U"a single boundary") |
| 1143 | OPTION (U"multiple boundaries") |
| 1144 | } |
| 1145 | OPTIONMENU_ENUM (kTextGridArea_showNumberOf, showNumberOf, |
| 1146 | U"Show number of", kTextGridArea_showNumberOf::DEFAULT) |
| 1147 | OPTIONMENU_ENUM (kMelder_string, paintIntervalsGreenWhoseLabel, |
| 1148 | U"Paint intervals green whose label...", kMelder_string::DEFAULT) |
| 1149 | SENTENCE (theText, U"...the text", my default_greenString()) |
| 1150 | EDITOR_OK |
| 1151 | SET_OPTION (useTextStyles, my instancePref_useTextStyles() + 1) |
| 1152 | SET_REAL (fontSize, my instancePref_fontSize()) |
| 1153 | SET_ENUM (textAlignmentInIntervals, kGraphics_horizontalAlignment, my instancePref_alignment()) |
| 1154 | if (my editable()) |
| 1155 | SET_OPTION (shiftDragMultiple, my instancePref_shiftDragMultiple() + 1) |
| 1156 | SET_ENUM (showNumberOf, kTextGridArea_showNumberOf, my instancePref_showNumberOf()) |
| 1157 | SET_ENUM (paintIntervalsGreenWhoseLabel, kMelder_string, my instancePref_greenMethod()) |
| 1158 | SET_STRING (theText, my instancePref_greenString()) |
| 1159 | EDITOR_DO |
| 1160 | my setInstancePref_useTextStyles (useTextStyles - 1); |
| 1161 | my setInstancePref_fontSize (fontSize); |
| 1162 | my setInstancePref_alignment (textAlignmentInIntervals); |
| 1163 | if (my editable()) |
| 1164 | my setInstancePref_shiftDragMultiple (shiftDragMultiple - 1); |
| 1165 | my setInstancePref_showNumberOf (showNumberOf); |
| 1166 | my setInstancePref_greenMethod (paintIntervalsGreenWhoseLabel); |
| 1167 | my setInstancePref_greenString (theText); |
| 1168 | FunctionEditor_redraw (my functionEditor()); |
| 1169 | EDITOR_END |
| 1170 | } |
| 1171 | |
| 1172 | |
| 1173 | #pragma mark - TextGridArea TextGrid/Select |
| 1174 | |
| 1175 | static void do_selectAdjacentTier (TextGridArea me, bool previous) { |
| 1176 | const integer n = my textGrid() -> tiers->size; |
| 1177 | if (n >= 2) { |
| 1178 | my selectedTier = ( previous ? |
| 1179 | my selectedTier > 1 ? my selectedTier - 1 : n : |
| 1180 | my selectedTier < n ? my selectedTier + 1 : 1 ); |
| 1181 | double startInterval, endInterval; |
| 1182 | timeToInterval (me, my startSelection(), my selectedTier, & startInterval, & endInterval); |
| 1183 | my setSelection (startInterval, endInterval); |
| 1184 | Melder_assert (isdefined (my startSelection())); // precondition of FunctionEditor_selectionMarksChanged() |
| 1185 | FunctionEditor_selectionMarksChanged (my functionEditor()); |
| 1186 | } |
| 1187 | } |
| 1188 | static void menu_cb_SelectPreviousTier (TextGridArea me, EDITOR_ARGS) { |
| 1189 | do_selectAdjacentTier (me, true); |
| 1190 | } |
| 1191 | static void menu_cb_SelectNextTier (TextGridArea me, EDITOR_ARGS) { |
| 1192 | do_selectAdjacentTier (me, false); |
| 1193 | } |
| 1194 | static void do_selectAdjacentInterval (TextGridArea me, bool previous, bool shift) { |
| 1195 | IntervalTier intervalTier; |
| 1196 | TextTier textTier; |
| 1197 | if (my selectedTier < 1 || my selectedTier > my textGrid() -> tiers->size) |
| 1198 | return; |
| 1199 | AnyTextGridTier_identifyClass (my textGrid() -> tiers->at [my selectedTier], & intervalTier, & textTier); |
| 1200 | if (intervalTier) { |
| 1201 | const integer n = intervalTier -> intervals.size; |
| 1202 | if (n >= 2) { |
| 1203 | integer iinterval = IntervalTier_timeToIndex (intervalTier, my startSelection()); |
| 1204 | if (shift) { |
| 1205 | const integer binterval = IntervalTier_timeToIndex (intervalTier, my startSelection()); |
| 1206 | integer einterval = IntervalTier_timeToIndex (intervalTier, my endSelection()); |
| 1207 | if (my endSelection() == intervalTier -> xmax) |
| 1208 | einterval ++; |
| 1209 | if (binterval < iinterval && einterval > iinterval + 1) { |
| 1210 | const TextInterval interval = intervalTier -> intervals.at [iinterval]; |
| 1211 | my setSelection (interval -> xmin, interval -> xmax); |
| 1212 | } else if (previous) { |
| 1213 | if (einterval > iinterval + 1) { |
| 1214 | if (einterval <= n + 1) { |
| 1215 | const TextInterval interval = intervalTier -> intervals.at [einterval - 1]; |
| 1216 | my setSelection (my startSelection(), interval -> xmin); |
| 1217 | } |
| 1218 | } else if (binterval > 1) { |
| 1219 | const TextInterval interval = intervalTier -> intervals.at [binterval - 1]; |
| 1220 | my setSelection (interval -> xmin, my endSelection()); |
| 1221 | } |
| 1222 | } else { |
| 1223 | if (binterval < iinterval) { |
| 1224 | if (binterval > 0) { |
| 1225 | const TextInterval interval = intervalTier -> intervals.at [binterval]; |
| 1226 | my setSelection (interval -> xmax, my endSelection()); |
| 1227 | } |
| 1228 | } else if (einterval <= n) { |
| 1229 | const TextInterval interval = intervalTier -> intervals.at [einterval]; |
| 1230 | my setSelection (my startSelection(), interval -> xmax); |
| 1231 | } |
| 1232 | } |
| 1233 | } else { |
| 1234 | iinterval = ( previous ? |
| 1235 | iinterval > 1 ? iinterval - 1 : n : |
| 1236 | iinterval < n ? iinterval + 1 : 1 ); |
| 1237 | const TextInterval interval = intervalTier -> intervals.at [iinterval]; |
| 1238 | my setSelection (interval -> xmin, interval -> xmax); |
| 1239 | } |
| 1240 | Melder_assert (isdefined (my startSelection())); // precondition of FunctionEditor_scrollToNewSelection() |
| 1241 | FunctionEditor_scrollToNewSelection (my functionEditor(), iinterval == n ? my startSelection() : iinterval == 1 ? my endSelection() : 0.5 * (my startSelection() + my endSelection())); |
| 1242 | } |
| 1243 | } else { |
| 1244 | const integer n = textTier -> points.size; |
| 1245 | if (n >= 2) { |
| 1246 | integer ipoint = AnyTier_timeToHighIndex (textTier->asAnyTier(), my startSelection()); |
| 1247 | ipoint = ( previous ? |
| 1248 | ipoint > 1 ? ipoint - 1 : n : |
| 1249 | ipoint < n ? ipoint + 1 : 1 ); |
| 1250 | const TextPoint point = textTier -> points.at [ipoint]; |
| 1251 | my setSelection (point -> number, point -> number); |
| 1252 | Melder_assert (isdefined (my startSelection())); // precondition of FunctionEditor_scrollToNewSelection() |
| 1253 | FunctionEditor_scrollToNewSelection (my functionEditor(), my startSelection()); |
| 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | static void menu_cb_SelectPreviousInterval (TextGridArea me, EDITOR_ARGS) { |
| 1258 | do_selectAdjacentInterval (me, true, false); |
| 1259 | } |
| 1260 | static void menu_cb_SelectNextInterval (TextGridArea me, EDITOR_ARGS) { |
| 1261 | do_selectAdjacentInterval (me, false, false); |
| 1262 | } |
| 1263 | static void menu_cb_ExtendSelectPreviousInterval (TextGridArea me, EDITOR_ARGS) { |
| 1264 | do_selectAdjacentInterval (me, true, true); |
| 1265 | } |
| 1266 | static void menu_cb_ExtendSelectNextInterval (TextGridArea me, EDITOR_ARGS) { |
| 1267 | do_selectAdjacentInterval (me, false, true); |
| 1268 | } |
| 1269 | |
| 1270 | |
| 1271 | #pragma mark - TextGridArea Draw |
| 1272 | |
| 1273 | static void menu_cb_DrawVisibleTextGrid (TextGridArea me, EDITOR_ARGS) { |
| 1274 | EDITOR_FORM (U"Draw visible TextGrid", nullptr) |
| 1275 | my v_form_pictureWindow (cmd); |
| 1276 | my v_form_pictureMargins (cmd); |
| 1277 | my v_form_pictureSelection (cmd); |
| 1278 | BOOLEAN (garnish, U"Garnish", my default_picture_garnish()) |
| 1279 | EDITOR_OK |
| 1280 | my v_ok_pictureWindow (cmd); |
| 1281 | my v_ok_pictureMargins (cmd); |
| 1282 | my v_ok_pictureSelection (cmd); |
| 1283 | SET_BOOLEAN (garnish, my classPref_picture_garnish()) |
| 1284 | EDITOR_DO |
| 1285 | my v_do_pictureWindow (cmd); |
| 1286 | my v_do_pictureMargins (cmd); |
| 1287 | my v_do_pictureSelection (cmd); |
| 1288 | my setClassPref_picture_garnish (garnish); |
| 1289 | DataGui_openPraatPicture (me); |
| 1290 | TextGrid_Sound_draw (my textGrid(), nullptr, my pictureGraphics(), |
| 1291 | my startWindow(), my endWindow(), true, my instancePref_useTextStyles(), garnish); |
| 1292 | FunctionArea_garnishPicture (me); |
| 1293 | DataGui_closePraatPicture (me); |
| 1294 | EDITOR_END |
| 1295 | } |
| 1296 | static void menu_cb_DrawVisibleSoundAndTextGrid (TextGridArea me, EDITOR_ARGS) { |
| 1297 | EDITOR_FORM (U"Draw visible sound and TextGrid", nullptr) |
| 1298 | my v_form_pictureWindow (cmd); |
| 1299 | my v_form_pictureMargins (cmd); |
| 1300 | my v_form_pictureSelection (cmd); |
| 1301 | BOOLEAN (garnish, U"Garnish", my default_picture_garnish()) |
| 1302 | EDITOR_OK |
| 1303 | my v_ok_pictureWindow (cmd); |
| 1304 | my v_ok_pictureMargins (cmd); |
| 1305 | my v_ok_pictureSelection (cmd); |
| 1306 | SET_BOOLEAN (garnish, my classPref_picture_garnish()) |
| 1307 | EDITOR_DO |
| 1308 | my v_do_pictureWindow (cmd); |
| 1309 | my v_do_pictureMargins (cmd); |
| 1310 | my v_do_pictureSelection (cmd); |
| 1311 | my setClassPref_picture_garnish (garnish); |
| 1312 | DataGui_openPraatPicture (me); |
| 1313 | {// scope |
| 1314 | autoSound sound = ( |
| 1315 | my borrowedSoundArea -> longSound() ? |
| 1316 | LongSound_extractPart (my borrowedSoundArea -> longSound(), |
| 1317 | my startWindow(), my endWindow(), true) |
| 1318 | : Sound_extractPart (my borrowedSoundArea -> sound(), |
| 1319 | my startWindow(), my endWindow(), kSound_windowShape::RECTANGULAR, 1.0, true) |
| 1320 | ); |
| 1321 | TextGrid_Sound_draw (my textGrid(), sound.get(), my pictureGraphics(), |
| 1322 | my startWindow(), my endWindow(), true, my instancePref_useTextStyles(), garnish); |
| 1323 | } |
| 1324 | FunctionArea_garnishPicture (me); |
| 1325 | DataGui_closePraatPicture (me); |
| 1326 | EDITOR_END |
| 1327 | } |
| 1328 | static void menu_cb_DrawTextGridAndPitch (TextGridArea me, EDITOR_ARGS) { |
| 1329 | EDITOR_FORM (U"Draw TextGrid and Pitch separately", nullptr) |
| 1330 | my v_form_pictureWindow (cmd); |
| 1331 | COMMENT (U"TextGrid:") |
| 1332 | BOOLEAN (showBoundariesAndPoints, U"Show boundaries and points", my default_picture_showBoundaries ()); |
| 1333 | COMMENT (U"Pitch:") |
| 1334 | BOOLEAN (speckle, U"Speckle", my default_picture_pitch_speckle ()); |
| 1335 | my v_form_pictureMargins (cmd); |
| 1336 | my v_form_pictureSelection (cmd); |
| 1337 | BOOLEAN (garnish, U"Garnish", my default_picture_garnish ()); |
| 1338 | EDITOR_OK |
| 1339 | my v_ok_pictureWindow (cmd); |
| 1340 | SET_BOOLEAN (showBoundariesAndPoints, my classPref_picture_showBoundaries()) |
| 1341 | SET_BOOLEAN (speckle, my classPref_picture_pitch_speckle()) |
| 1342 | my v_ok_pictureMargins (cmd); |
| 1343 | my v_ok_pictureSelection (cmd); |
| 1344 | SET_BOOLEAN (garnish, my classPref_picture_garnish()) |
| 1345 | EDITOR_DO |
| 1346 | my v_do_pictureWindow (cmd); |
| 1347 | my setClassPref_picture_showBoundaries (showBoundariesAndPoints); // set prefs even if analyses are missing (it would be annoying not to) |
| 1348 | my setClassPref_picture_pitch_speckle (speckle); |
| 1349 | my v_do_pictureMargins (cmd); |
| 1350 | my v_do_pictureSelection (cmd); |
| 1351 | my setClassPref_picture_garnish (garnish); |
| 1352 | SoundAnalysisArea_haveVisiblePitch (my borrowedSoundAnalysisArea); |
| 1353 | DataGui_openPraatPicture (me); |
| 1354 | const kPitch_unit borrowedPitchUnit = my borrowedSoundAnalysisArea -> dynamic_instancePref_pitch_unit(); |
| 1355 | const double pitchFloor_hidden = Function_convertStandardToSpecialUnit (my borrowedSoundAnalysisArea -> d_pitch.get(), |
| 1356 | my borrowedSoundAnalysisArea -> dynamic_instancePref_pitch_floor(), Pitch_LEVEL_FREQUENCY, (int) borrowedPitchUnit); |
| 1357 | const double pitchCeiling_hidden = Function_convertStandardToSpecialUnit (my borrowedSoundAnalysisArea -> d_pitch.get(), |
| 1358 | my borrowedSoundAnalysisArea -> dynamic_instancePref_pitch_ceilingOrTop(), Pitch_LEVEL_FREQUENCY, (int) borrowedPitchUnit); |
| 1359 | const double pitchFloor_overt = Function_convertToNonlogarithmic (my borrowedSoundAnalysisArea -> d_pitch.get(), |
| 1360 | pitchFloor_hidden, Pitch_LEVEL_FREQUENCY, (int) borrowedPitchUnit); |
| 1361 | const double pitchCeiling_overt = Function_convertToNonlogarithmic (my borrowedSoundAnalysisArea -> d_pitch.get(), |
| 1362 | pitchCeiling_hidden, Pitch_LEVEL_FREQUENCY, (int) borrowedPitchUnit); |
| 1363 | const double borrowedPitchViewFrom = my borrowedSoundAnalysisArea -> dynamic_instancePref_pitch_viewFrom(); |
| 1364 | const double borrowedPitchViewTo = my borrowedSoundAnalysisArea -> dynamic_instancePref_pitch_viewTo(); |
| 1365 | const double pitchViewFrom_overt = ( borrowedPitchViewFrom < borrowedPitchViewTo ? borrowedPitchViewFrom : pitchFloor_overt ); |
| 1366 | const double pitchViewTo_overt = ( borrowedPitchViewFrom < borrowedPitchViewTo ? borrowedPitchViewTo : pitchCeiling_overt ); |
| 1367 | TextGrid_Pitch_drawSeparately (my textGrid(), my borrowedSoundAnalysisArea -> d_pitch.get(), my pictureGraphics(), my startWindow(), my endWindow(), |
| 1368 | pitchViewFrom_overt, pitchViewTo_overt, showBoundariesAndPoints, my instancePref_useTextStyles(), garnish, |
| 1369 | speckle, borrowedPitchUnit |
| 1370 | ); |
| 1371 | FunctionArea_garnishPicture (me); |
| 1372 | DataGui_closePraatPicture (me); |
| 1373 | EDITOR_END |
| 1374 | } |
| 1375 | |
| 1376 | |
| 1377 | #pragma mark - TextGridArea Extract |
| 1378 | |
| 1379 | static void CONVERT_DATA_TO_ONE__ExtractSelectedTextGrid_preserveTimes (TextGridArea me, EDITOR_ARGS) { |
| 1380 | CONVERT_DATA_TO_ONE |
| 1381 | if (my endSelection() <= my startSelection()) |
| 1382 | Melder_throw (U"No selection."); |
| 1383 | autoTextGrid result = TextGrid_extractPart (my textGrid(), my startSelection(), my endSelection(), true); |
| 1384 | CONVERT_DATA_TO_ONE_END (U"untitled") |
| 1385 | } |
| 1386 | static void CONVERT_DATA_TO_ONE__ExtractSelectedTextGrid_timeFromZero (TextGridArea me, EDITOR_ARGS) { |
| 1387 | CONVERT_DATA_TO_ONE |
| 1388 | if (my endSelection() <= my startSelection()) |
| 1389 | Melder_throw (U"No selection."); |
| 1390 | autoTextGrid result = TextGrid_extractPart (my textGrid(), my startSelection(), my endSelection(), false); |
| 1391 | CONVERT_DATA_TO_ONE_END (U"untitled") |
| 1392 | } |
| 1393 | |
| 1394 | |
| 1395 | #pragma mark - TextGridArea Interval menu |
| 1396 | |
| 1397 | static void menu_cb_AlignInterval (TextGridArea me, EDITOR_ARGS) { |
| 1398 | checkTierSelection (me, U"align words"); |
| 1399 | const AnyTier tier = static_cast <AnyTier> (my textGrid() -> tiers->at [my selectedTier]); |
| 1400 | if (tier -> classInfo != classIntervalTier) |
| 1401 | Melder_throw (U"Alignment works only for interval tiers, whereas tier ", my selectedTier, U" is a point tier.\nSelect an interval tier instead."); |
| 1402 | const integer intervalNumber = getSelectedInterval (me); |
| 1403 | if (! intervalNumber) |
| 1404 | Melder_throw (U"Select an interval first"); |
| 1405 | if (! my instancePref_align_includeWords() && ! my instancePref_align_includePhonemes()) |
| 1406 | Melder_throw (U"Nothing to be done.\nPlease switch on \"Include words\" and/or \"Include phonemes\" in the \"Alignment settings\"."); |
| 1407 | {// scope |
| 1408 | const autoMelderProgressOff noprogress; |
| 1409 | FunctionArea_save (me, U"Align interval"); |
| 1410 | TextGrid_anySound_alignInterval (my textGrid(), my borrowedSoundArea -> soundOrLongSound(), my selectedTier, intervalNumber, |
| 1411 | my instancePref_align_language(), my instancePref_align_includeWords(), my instancePref_align_includePhonemes()); |
| 1412 | } |
| 1413 | FunctionArea_broadcastDataChanged (me); |
| 1414 | } |
| 1415 | static void menu_cb_AlignmentSettings (TextGridArea me, EDITOR_ARGS) { |
| 1416 | EDITOR_FORM (U"Alignment settings", nullptr) |
| 1417 | OPTIONMENU (language, U"Language", (int) NUMfindFirst (theSpeechSynthesizerLanguageNames, U"English (Great Britain)")) |
| 1418 | for (integer i = 1; i <= theSpeechSynthesizerLanguageNames.size; i ++) { |
| 1419 | OPTION (theSpeechSynthesizerLanguageNames [i]); |
| 1420 | } |
| 1421 | BOOLEAN (includeWords, U"Include words", my default_align_includeWords ()) |
| 1422 | BOOLEAN (includePhonemes, U"Include phonemes", my default_align_includePhonemes ()) |
| 1423 | BOOLEAN (allowSilences, U"Allow silences", my default_align_allowSilences ()) |
| 1424 | EDITOR_OK |
| 1425 | int prefVoice = (int) NUMfindFirst (theSpeechSynthesizerLanguageNames, my instancePref_align_language()); |
| 1426 | if (prefVoice == 0) |
| 1427 | prefVoice = (int) NUMfindFirst (theSpeechSynthesizerLanguageNames, U"English (Great Britain)"); |
| 1428 | SET_OPTION (language, prefVoice) |
| 1429 | SET_BOOLEAN (includeWords, my instancePref_align_includeWords()) |
| 1430 | SET_BOOLEAN (includePhonemes, my instancePref_align_includePhonemes()) |
| 1431 | SET_BOOLEAN (allowSilences, my instancePref_align_allowSilences()) |
| 1432 | EDITOR_DO |
| 1433 | my setInstancePref_align_language (theSpeechSynthesizerLanguageNames [language]); |
| 1434 | my setInstancePref_align_includeWords (includeWords); |
| 1435 | my setInstancePref_align_includePhonemes (includePhonemes); |
| 1436 | my setInstancePref_align_allowSilences (allowSilences); |
| 1437 | EDITOR_END |
| 1438 | } |
| 1439 | |
| 1440 | static void menu_cb_TranscribeInterval (TextGridArea me, EDITOR_ARGS) { |
| 1441 | checkTierSelection (me, U"transcribe interval"); |
| 1442 | const AnyTier tier = static_cast <AnyTier> (my textGrid() -> tiers->at [my selectedTier]); |
| 1443 | if (tier -> classInfo != classIntervalTier) |
| 1444 | Melder_throw (U"Transcription works only for interval tiers, whereas tier ", my selectedTier, U" is a point tier.\nSelect an interval tier instead."); |
| 1445 | const integer intervalNumber = getSelectedInterval (me); |
| 1446 | if (! intervalNumber) |
| 1447 | Melder_throw (U"Select an interval first"); |
| 1448 | {// scope |
| 1449 | const autoMelderProgressOff noprogress; |
| 1450 | FunctionArea_save (me, U"Transcribe interval"); |
| 1451 | TextGrid_Sound_transcribeInterval (my textGrid(), my borrowedSoundArea -> sound(), my selectedTier, intervalNumber, |
| 1452 | my instancePref_transcribe_model(), my instancePref_transcribe_language(), |
| 1453 | my instancePref_transcribe_includeWords(), my instancePref_transcribe_useVad(), |
| 1454 | my instancePref_vad_speechThreshold(), my instancePref_vad_minNonSpeech(), |
| 1455 | my instancePref_vad_minSpeech(),my instancePref_vad_speechPadding(), |
| 1456 | my instancePref_transcribe_diarize(), |
| 1457 | my instancePref_diarize_maxNumSpeakers(), my instancePref_diarize_allowOverlap(), |
| 1458 | my instancePref_diarize_clusterThreshold(), my instancePref_diarize_segmentationStep()); |
| 1459 | } |
| 1460 | FunctionArea_broadcastDataChanged (me); |
| 1461 | } |
| 1462 | |
| 1463 | static void menu_cb_TranscriptionSettings (TextGridArea me, EDITOR_ARGS) { |
| 1464 | EDITOR_FORM (U"Transcription settings", U"transcription with whisper.cpp") |
| 1465 | HEADING (U"Transcription...") |
| 1466 | LISTNUMSTR (modelIndex, modelName, U"Whisper model", constSTRVEC(), 1) |
| 1467 | OPTIONMENU (language, U"Language", (int) NUMfindFirst (theSpeechRecognizerLanguageNames(), TranscriptionDefaults::languageName)) |
| 1468 | for (integer i = 1; i <= theSpeechRecognizerLanguageNames().size; i ++) { |
| 1469 | OPTION (theSpeechRecognizerLanguageNames() [i]); |
| 1470 | } |
| 1471 | BOOLEAN (includeWords, U"Include words", my default_transcribe_includeWords()) |
| 1472 | HEADING (U"Non-speech detection...") |
| 1473 | BOOLEAN (useVad, U"Detect non-speech", my default_transcribe_useVad()) |
| 1474 | NONNEGATIVE (speechProbabilityThreshold, U"Speech probability threshold (0-1)", my default_vad_speechThreshold()) |
| 1475 | NONNEGATIVE (minNonSpeechDuration, U"Min. gap between speech segments (s)", my default_vad_minNonSpeech()) |
| 1476 | NONNEGATIVE (minSpeechDuration, U"Min. speech segment (s)", my default_vad_minSpeech()) |
| 1477 | NONNEGATIVE (speechPad, U"Padding around speech segments (s)", my default_vad_speechPadding()) |
| 1478 | HEADING (U"Diarization...") |
| 1479 | BOOLEAN (includeDiarization, U"Include diarization", my default_transcribe_diarize()) |
| 1480 | NATURAL (maxNumSpeakers, U"Max. number of speakers (≥ 2)", DiarizationDefaults::maxNumSpeakers) |
| 1481 | BOOLEAN (allowSpeakersOverlap, U"Allow speakers to overlap", DiarizationDefaults::allowOverlap) |
| 1482 | POSITIVE (clusterThreshold, U"Clustering threshold (0-2)", DiarizationDefaults::clusterThreshold) |
| 1483 | POSITIVE (segmentationStep, U"Segmentation step (0-1)", DiarizationDefaults::segmentationStep) |
| 1484 | EDITOR_OK |
| 1485 | static autoSTRVEC modelNames; |
| 1486 | modelNames = copy_STRVEC (theCurrentSpeechRecognizerModelNames()); // cannot be called twice in the same scope |
| 1487 | |
| 1488 | Melder_require (modelNames.size > 0, |
| 1489 | U"Found no Whisper-cpp models to do speech recognition with.\n" |
| 1490 | U"You can install them into the subfolders “whispercpp” of the folder “models” in the Praat preferences folder." |
| 1491 | ); |
| 1492 | |
| 1493 | SET_BOOLEAN (includeWords, my instancePref_transcribe_includeWords()) |
| 1494 | SET_BOOLEAN (includeDiarization, my instancePref_transcribe_diarize()) |
| 1495 | SET_BOOLEAN (useVad, my instancePref_transcribe_useVad()) |
| 1496 | SET_REAL (speechProbabilityThreshold, my instancePref_vad_speechThreshold()) |
| 1497 | SET_REAL (minNonSpeechDuration, my instancePref_vad_minNonSpeech()) |
| 1498 | SET_REAL (minSpeechDuration, my instancePref_vad_minSpeech()) |
| 1499 | SET_REAL (speechPad, my instancePref_vad_speechPadding()) |
| 1500 | SET_INTEGER (maxNumSpeakers, my instancePref_diarize_maxNumSpeakers()) |
| 1501 | SET_BOOLEAN (allowSpeakersOverlap, my instancePref_diarize_allowOverlap()) |
| 1502 | SET_REAL (clusterThreshold, my instancePref_diarize_clusterThreshold()) |
| 1503 | SET_REAL (segmentationStep, my instancePref_diarize_segmentationStep()) |
| 1504 | |
| 1505 | integer prefModel = NUMfindFirst (modelNames.get (), my instancePref_transcribe_model()); |
| 1506 | if (prefModel == 0) |
| 1507 | prefModel = NUMfindFirst (modelNames.get (), TranscriptionDefaults::modelName); |
| 1508 | SET_INTEGER (modelIndex, prefModel) |
| 1509 | SET_LIST (modelIndex, modelName, modelNames.get (), prefModel) |
| 1510 | |
| 1511 | integer prefLanguage = NUMfindFirst (theSpeechRecognizerLanguageNames(), my instancePref_transcribe_language()); |
| 1512 | if (prefLanguage == 0) |
| 1513 | prefLanguage = NUMfindFirst (theSpeechRecognizerLanguageNames(), TranscriptionDefaults::languageName); |
| 1514 | SET_OPTION (language, prefLanguage) |
| 1515 | EDITOR_DO |
| 1516 | my setInstancePref_transcribe_model (modelName); |
| 1517 | my setInstancePref_transcribe_language (theSpeechRecognizerLanguageNames() [language]); |
| 1518 | my setInstancePref_transcribe_includeWords (includeWords); |
| 1519 | my setInstancePref_transcribe_diarize (includeDiarization); |
| 1520 | my setInstancePref_transcribe_useVad (useVad); |
| 1521 | my setInstancePref_vad_speechThreshold (speechProbabilityThreshold); |
| 1522 | my setInstancePref_vad_minNonSpeech (minNonSpeechDuration); |
| 1523 | my setInstancePref_vad_minSpeech (minSpeechDuration); |
| 1524 | my setInstancePref_vad_speechPadding (speechPad); |
| 1525 | my setInstancePref_diarize_maxNumSpeakers (maxNumSpeakers); |
| 1526 | my setInstancePref_diarize_allowOverlap (allowSpeakersOverlap); |
| 1527 | my setInstancePref_diarize_clusterThreshold (clusterThreshold); |
| 1528 | my setInstancePref_diarize_segmentationStep (segmentationStep); |
| 1529 | EDITOR_END |
| 1530 | } |
| 1531 | |
| 1532 | static void menu_cb_DiarizeInterval (TextGridArea me, EDITOR_ARGS) { |
| 1533 | checkTierSelection (me, U"diarize interval"); |
| 1534 | const AnyTier tier = static_cast <AnyTier> (my textGrid() -> tiers->at [my selectedTier]); |
| 1535 | if (tier -> classInfo != classIntervalTier) |
| 1536 | Melder_throw (U"Diarization works only for interval tiers, whereas tier ", my selectedTier, U" is a point tier.\nSelect an interval tier instead."); |
| 1537 | const integer intervalNumber = getSelectedInterval (me); |
| 1538 | if (! intervalNumber) |
| 1539 | Melder_throw (U"Select an interval first"); |
| 1540 | {// scope |
| 1541 | const autoMelderProgressOff noprogress; |
| 1542 | FunctionArea_save (me, U"Diarize interval"); |
| 1543 | TextGrid_Sound_diarizeInterval (my textGrid(), my borrowedSoundArea -> sound(), my selectedTier, intervalNumber, |
| 1544 | my instancePref_diarize_maxNumSpeakers(), my instancePref_diarize_allowOverlap(), |
| 1545 | my instancePref_diarize_nonSpeechLabel(), my instancePref_diarize_speechLabel(), |
| 1546 | my instancePref_diarize_clusterThreshold(), my instancePref_diarize_segmentationStep() |
| 1547 | ); |
| 1548 | } |
| 1549 | FunctionArea_broadcastDataChanged (me); |
| 1550 | } |
| 1551 | |
| 1552 | static void menu_cb_DiarizationSettings (TextGridArea me, EDITOR_ARGS) { |
| 1553 | EDITOR_FORM (U"Diarization settings", U"speaker diarization with adapted pyannote.audio") |
| 1554 | NATURAL (maxNumSpeakers, U"Max. number of speakers (≥ 2)", DiarizationDefaults::maxNumSpeakers) |
| 1555 | BOOLEAN (allowSpeakersOverlap, U"Allow speakers to overlap", DiarizationDefaults::allowOverlap) |
| 1556 | WORD (nonSpeechLabel, U"Non-speech interval label", DiarizationDefaults::nonSpeechLabel) |
| 1557 | WORD (speechLabel, U"Speech interval label", DiarizationDefaults::speechLabel) |
| 1558 | POSITIVE (clusterThreshold, U"Clustering threshold (0-2)", DiarizationDefaults::clusterThreshold) |
| 1559 | POSITIVE (segmentationStep, U"Segmentation step (0-1)", DiarizationDefaults::segmentationStep) |
| 1560 | EDITOR_OK |
| 1561 | SET_INTEGER (maxNumSpeakers, my instancePref_diarize_maxNumSpeakers()) |
| 1562 | SET_BOOLEAN (allowSpeakersOverlap, my instancePref_diarize_allowOverlap()) |
| 1563 | SET_STRING (nonSpeechLabel, my instancePref_diarize_nonSpeechLabel()) |
| 1564 | SET_STRING (speechLabel, my instancePref_diarize_speechLabel()) |
| 1565 | SET_REAL (clusterThreshold, my instancePref_diarize_clusterThreshold()) |
| 1566 | SET_REAL (segmentationStep, my instancePref_diarize_segmentationStep()) |
| 1567 | EDITOR_DO |
| 1568 | my setInstancePref_diarize_maxNumSpeakers (maxNumSpeakers); |
| 1569 | my setInstancePref_diarize_allowOverlap (allowSpeakersOverlap); |
| 1570 | my setInstancePref_diarize_nonSpeechLabel (nonSpeechLabel); |
| 1571 | my setInstancePref_diarize_speechLabel (speechLabel); |
| 1572 | my setInstancePref_diarize_clusterThreshold (clusterThreshold); |
| 1573 | my setInstancePref_diarize_segmentationStep (segmentationStep); |
| 1574 | EDITOR_END |
| 1575 | } |
| 1576 | |
| 1577 | static void do_insertIntervalOnTier (TextGridArea me, int itier) { |
| 1578 | try { |
| 1579 | insertBoundaryOrPoint (me, itier, |
| 1580 | my functionEditor() -> duringPlay ? my functionEditor() -> playCursor : my startSelection(), |
| 1581 | my functionEditor() -> duringPlay ? my functionEditor() -> playCursor : my endSelection(), |
| 1582 | true |
| 1583 | ); |
| 1584 | my selectedTier = itier; |
| 1585 | FunctionArea_broadcastDataChanged (me); |
| 1586 | } catch (MelderError) { |
| 1587 | Melder_throw (U"Interval not inserted."); |
| 1588 | } |
| 1589 | } |
| 1590 | static void menu_cb_InsertIntervalOnTier1 (TextGridArea me, EDITOR_ARGS) { do_insertIntervalOnTier (me, 1); } |
| 1591 | static void menu_cb_InsertIntervalOnTier2 (TextGridArea me, EDITOR_ARGS) { do_insertIntervalOnTier (me, 2); } |
| 1592 | static void menu_cb_InsertIntervalOnTier3 (TextGridArea me, EDITOR_ARGS) { do_insertIntervalOnTier (me, 3); } |
| 1593 | static void menu_cb_InsertIntervalOnTier4 (TextGridArea me, EDITOR_ARGS) { do_insertIntervalOnTier (me, 4); } |
| 1594 | static void menu_cb_InsertIntervalOnTier5 (TextGridArea me, EDITOR_ARGS) { do_insertIntervalOnTier (me, 5); } |
| 1595 | static void menu_cb_InsertIntervalOnTier6 (TextGridArea me, EDITOR_ARGS) { do_insertIntervalOnTier (me, 6); } |
| 1596 | static void menu_cb_InsertIntervalOnTier7 (TextGridArea me, EDITOR_ARGS) { do_insertIntervalOnTier (me, 7); } |
| 1597 | static void menu_cb_InsertIntervalOnTier8 (TextGridArea me, EDITOR_ARGS) { do_insertIntervalOnTier (me, 8); } |
| 1598 | |
| 1599 | #pragma mark - TextGridArea Boundary menu |
| 1600 | |
| 1601 | static void menu_cb_RemovePointOrBoundary (TextGridArea me, EDITOR_ARGS) { |
| 1602 | checkTierSelection (me, U"remove a point or boundary"); |
| 1603 | const Function anyTier = my textGrid() -> tiers->at [my selectedTier]; |
| 1604 | if (anyTier -> classInfo == classIntervalTier) { |
| 1605 | const IntervalTier tier = (IntervalTier) anyTier; |
| 1606 | const integer selectedLeftBoundary = getSelectedLeftBoundary (me); |
| 1607 | if (selectedLeftBoundary == 0) |
| 1608 | Melder_throw (U"To remove a boundary, first click on it."); |
| 1609 | |
| 1610 | FunctionArea_save (me, U"Remove boundary"); |
| 1611 | IntervalTier_removeLeftBoundary (tier, selectedLeftBoundary); |
| 1612 | } else { |
| 1613 | const TextTier tier = (TextTier) anyTier; |
| 1614 | const integer selectedPoint = getSelectedPoint (me); |
| 1615 | if (selectedPoint == 0) |
| 1616 | Melder_throw (U"To remove a point, first click on it."); |
| 1617 | |
| 1618 | FunctionArea_save (me, U"Remove point"); |
| 1619 | tier -> points. removeItem (selectedPoint); |
| 1620 | } |
| 1621 | FunctionArea_broadcastDataChanged (me); |
| 1622 | } |
| 1623 | static void do_movePointOrBoundary (TextGridArea me, int where) { |
| 1624 | if (where == 0 && ! (my borrowedSoundArea && my borrowedSoundArea -> sound())) |
| 1625 | return; |
| 1626 | checkTierSelection (me, U"move a point or boundary"); |
| 1627 | const Function anyTier = my textGrid() -> tiers->at [my selectedTier]; |
| 1628 | if (anyTier -> classInfo == classIntervalTier) { |
| 1629 | const IntervalTier tier = (IntervalTier) anyTier; |
| 1630 | static const conststring32 boundarySaveText [3] { U"Move boundary to zero crossing", U"Move boundary to B", U"Move boundary to E" }; |
| 1631 | const integer selectedLeftBoundary = getSelectedLeftBoundary (me); |
| 1632 | if (selectedLeftBoundary == 0) |
| 1633 | Melder_throw (U"To move a boundary, first click on it."); |
| 1634 | const TextInterval left = tier -> intervals.at [selectedLeftBoundary - 1]; |
| 1635 | const TextInterval right = tier -> intervals.at [selectedLeftBoundary]; |
| 1636 | const double position = ( where == 1 ? my startSelection() : where == 2 ? my endSelection() : |
| 1637 | Sound_getNearestZeroCrossing (my borrowedSoundArea -> sound(), left -> xmax, 1) ); // STEREO BUG |
| 1638 | if (isundef (position)) |
| 1639 | Melder_throw (U"There is no zero crossing to move to."); |
| 1640 | if (position <= left -> xmin || position >= right -> xmax) |
| 1641 | Melder_throw (U"Cannot move a boundary past its neighbour."); |
| 1642 | |
| 1643 | FunctionArea_save (me, boundarySaveText [where]); |
| 1644 | |
| 1645 | left -> xmax = right -> xmin = position; |
| 1646 | my setSelection (position, position); |
| 1647 | } else { |
| 1648 | TextTier tier = (TextTier) anyTier; |
| 1649 | static const conststring32 pointSaveText [3] { U"Move point to zero crossing", U"Move point to B", U"Move point to E" }; |
| 1650 | const integer selectedPoint = getSelectedPoint (me); |
| 1651 | if (selectedPoint == 0) |
| 1652 | Melder_throw (U"To move a point, first click on it."); |
| 1653 | const TextPoint point = tier -> points.at [selectedPoint]; |
| 1654 | const double position = ( where == 1 ? my startSelection() : where == 2 ? my endSelection() : |
| 1655 | Sound_getNearestZeroCrossing (my borrowedSoundArea -> sound(), point -> number, 1) ); // STEREO BUG |
| 1656 | if (isundef (position)) |
| 1657 | Melder_throw (U"There is no zero crossing to move to."); |
| 1658 | |
| 1659 | FunctionArea_save (me, pointSaveText [where]); |
| 1660 | |
| 1661 | point -> number = position; |
| 1662 | my setSelection (position, position); |
| 1663 | } |
| 1664 | FunctionArea_broadcastDataChanged (me); |
| 1665 | } |
| 1666 | static void menu_cb_MoveToB (TextGridArea me, EDITOR_ARGS) { |
| 1667 | do_movePointOrBoundary (me, 1); |
| 1668 | } |
| 1669 | static void menu_cb_MoveToE (TextGridArea me, EDITOR_ARGS) { |
| 1670 | do_movePointOrBoundary (me, 2); |
| 1671 | } |
| 1672 | static void menu_cb_MoveToZero (TextGridArea me, EDITOR_ARGS) { |
| 1673 | do_movePointOrBoundary (me, 0); |
| 1674 | } |
| 1675 | static void do_insertOnTier (TextGridArea me, integer itier) { |
| 1676 | try { |
| 1677 | insertBoundaryOrPoint (me, itier, |
| 1678 | my functionEditor() -> duringPlay ? my functionEditor() -> playCursor : my startSelection(), |
| 1679 | my functionEditor() -> duringPlay ? my functionEditor() -> playCursor : my endSelection(), |
| 1680 | false |
| 1681 | ); |
| 1682 | my selectedTier = itier; |
| 1683 | FunctionArea_broadcastDataChanged (me); |
| 1684 | } catch (MelderError) { |
| 1685 | Melder_throw (U"Boundary or point not inserted."); |
| 1686 | } |
| 1687 | } |
| 1688 | static void menu_cb_InsertOnSelectedTier (TextGridArea me, EDITOR_ARGS) { |
| 1689 | do_insertOnTier (me, my selectedTier); |
| 1690 | } |
| 1691 | static void menu_cb_InsertOnTier1 (TextGridArea me, EDITOR_ARGS) { do_insertOnTier (me, 1); } |
| 1692 | static void menu_cb_InsertOnTier2 (TextGridArea me, EDITOR_ARGS) { do_insertOnTier (me, 2); } |
| 1693 | static void menu_cb_InsertOnTier3 (TextGridArea me, EDITOR_ARGS) { do_insertOnTier (me, 3); } |
| 1694 | static void menu_cb_InsertOnTier4 (TextGridArea me, EDITOR_ARGS) { do_insertOnTier (me, 4); } |
| 1695 | static void menu_cb_InsertOnTier5 (TextGridArea me, EDITOR_ARGS) { do_insertOnTier (me, 5); } |
| 1696 | static void menu_cb_InsertOnTier6 (TextGridArea me, EDITOR_ARGS) { do_insertOnTier (me, 6); } |
| 1697 | static void menu_cb_InsertOnTier7 (TextGridArea me, EDITOR_ARGS) { do_insertOnTier (me, 7); } |
| 1698 | static void menu_cb_InsertOnTier8 (TextGridArea me, EDITOR_ARGS) { do_insertOnTier (me, 8); } |
| 1699 | |
| 1700 | static void menu_cb_InsertOnAllTiers (TextGridArea me, EDITOR_ARGS) { |
| 1701 | const integer saveTier = my selectedTier; |
| 1702 | for (integer itier = 1; itier <= my textGrid() -> tiers->size; itier ++) |
| 1703 | do_insertOnTier (me, itier); |
| 1704 | my selectedTier = saveTier; // only if everything went right; otherwise, the tier where something went wrong will stand selected |
| 1705 | } |
| 1706 | |
| 1707 | |
| 1708 | #pragma mark - TextGridArea Tier menu |
| 1709 | |
| 1710 | static void menu_cb_RenameTier (TextGridArea me, EDITOR_ARGS) { |
| 1711 | EDITOR_FORM (U"Rename tier", nullptr) |
| 1712 | SENTENCE (newName, U"New name", U""); |
| 1713 | EDITOR_OK |
| 1714 | checkTierSelection (me, U"rename a tier"); |
| 1715 | const Daata tier = my textGrid() -> tiers->at [my selectedTier]; |
| 1716 | SET_STRING (newName, tier -> name ? tier -> name.get() : U"") |
| 1717 | EDITOR_DO |
| 1718 | checkTierSelection (me, U"rename a tier"); |
| 1719 | const Function tier = my textGrid() -> tiers->at [my selectedTier]; |
| 1720 | |
| 1721 | FunctionArea_save (me, U"Rename tier"); |
| 1722 | |
| 1723 | Thing_setName (tier, newName); |
| 1724 | |
| 1725 | FunctionArea_broadcastDataChanged (me); |
| 1726 | EDITOR_END |
| 1727 | } |
| 1728 | static void CONVERT_DATA_TO_ONE__PublishTier (TextGridArea me, EDITOR_ARGS) { |
| 1729 | CONVERT_DATA_TO_ONE |
| 1730 | checkTierSelection (me, U"publish a tier"); |
| 1731 | const Function tier = my textGrid() -> tiers->at [my selectedTier]; |
| 1732 | autoTextGrid result = TextGrid_createWithoutTiers (1e308, -1e308); |
| 1733 | TextGrid_addTier_copy (result.get(), tier); |
| 1734 | CONVERT_DATA_TO_ONE_END (tier -> name.get()) |
| 1735 | } |
| 1736 | static void menu_cb_RemoveAllTextFromTier (TextGridArea me, EDITOR_ARGS) { |
| 1737 | checkTierSelection (me, U"remove all text from a tier"); |
| 1738 | IntervalTier intervalTier; |
| 1739 | TextTier textTier; |
| 1740 | AnyTextGridTier_identifyClass (my textGrid() -> tiers->at [my selectedTier], & intervalTier, & textTier); |
| 1741 | |
| 1742 | FunctionArea_save (me, U"Remove text from tier"); |
| 1743 | if (intervalTier) |
| 1744 | IntervalTier_removeText (intervalTier); |
| 1745 | else |
| 1746 | TextTier_removeText (textTier); |
| 1747 | |
| 1748 | FunctionArea_broadcastDataChanged (me); |
| 1749 | } |
| 1750 | static void menu_cb_RemoveTier (TextGridArea me, EDITOR_ARGS) { |
| 1751 | if (my textGrid() -> tiers->size <= 1) |
| 1752 | Melder_throw (U"Sorry, I refuse to remove the last tier."); |
| 1753 | checkTierSelection (me, U"remove a tier"); |
| 1754 | |
| 1755 | FunctionArea_save (me, U"Remove tier"); |
| 1756 | my textGrid() -> tiers -> removeItem (my selectedTier); |
| 1757 | |
| 1758 | //my textGridArea -> selectedTier = 1; TRY OUT 2022-07-23 |
| 1759 | FunctionArea_broadcastDataChanged (me); |
| 1760 | } |
| 1761 | static void menu_cb_AddIntervalTier (TextGridArea me, EDITOR_ARGS) { |
| 1762 | EDITOR_FORM (U"Add interval tier", nullptr) |
| 1763 | NATURAL (position, U"Position", U"1 (= at top)") |
| 1764 | SENTENCE (name, U"Name", U"") |
| 1765 | EDITOR_OK |
| 1766 | SET_INTEGER_AS_STRING (position, Melder_cat (my textGrid() -> tiers->size + 1, U" (= at bottom)")) |
| 1767 | SET_STRING (name, U"") |
| 1768 | EDITOR_DO |
| 1769 | {// scope |
| 1770 | autoIntervalTier tier = IntervalTier_create (my textGrid() -> xmin, my textGrid() -> xmax); |
| 1771 | Melder_clipRight (& position, my textGrid() -> tiers->size + 1); |
| 1772 | Thing_setName (tier.get(), name); |
| 1773 | |
| 1774 | FunctionArea_save (me, U"Add interval tier"); |
| 1775 | my textGrid() -> tiers -> addItemAtPosition_move (tier.move(), position); |
| 1776 | } |
| 1777 | |
| 1778 | my selectedTier = position; |
| 1779 | FunctionArea_broadcastDataChanged (me); |
| 1780 | EDITOR_END |
| 1781 | } |
| 1782 | static void menu_cb_AddPointTier (TextGridArea me, EDITOR_ARGS) { |
| 1783 | EDITOR_FORM (U"Add point tier", nullptr) |
| 1784 | NATURAL (position, U"Position", U"1 (= at top)") |
| 1785 | SENTENCE (name, U"Name", U""); |
| 1786 | EDITOR_OK |
| 1787 | SET_INTEGER_AS_STRING (position, Melder_cat (my textGrid() -> tiers->size + 1, U" (= at bottom)")) |
| 1788 | SET_STRING (name, U"") |
| 1789 | EDITOR_DO |
| 1790 | {// scope |
| 1791 | autoTextTier tier = TextTier_create (my textGrid() -> xmin, my textGrid() -> xmax); |
| 1792 | Melder_clipRight (& position, my textGrid() -> tiers->size + 1); |
| 1793 | Thing_setName (tier.get(), name); |
| 1794 | |
| 1795 | FunctionArea_save (me, U"Add point tier"); |
| 1796 | my textGrid() -> tiers -> addItemAtPosition_move (tier.move(), position); |
| 1797 | } |
| 1798 | |
| 1799 | my selectedTier = position; |
| 1800 | FunctionArea_broadcastDataChanged (me); |
| 1801 | EDITOR_END |
| 1802 | } |
| 1803 | static void menu_cb_DuplicateTier (TextGridArea me, EDITOR_ARGS) { |
| 1804 | EDITOR_FORM (U"Duplicate tier", nullptr) |
| 1805 | NATURAL (position, U"Position", U"1 (= at top)") |
| 1806 | SENTENCE (name, U"Name", U"") |
| 1807 | EDITOR_OK |
| 1808 | if (my selectedTier != 0) { |
| 1809 | SET_INTEGER (position, my selectedTier + 1) |
| 1810 | SET_STRING (name, my textGrid() -> tiers->at [my selectedTier] -> name.get()) |
| 1811 | } |
| 1812 | EDITOR_DO |
| 1813 | checkTierSelection (me, U"duplicate a tier"); |
| 1814 | const Function tier = my textGrid() -> tiers->at [my selectedTier]; |
| 1815 | {// scope |
| 1816 | autoFunction newTier = Data_copy (tier); |
| 1817 | Melder_clipRight (& position, my textGrid() -> tiers->size + 1); |
| 1818 | Thing_setName (newTier.get(), name); |
| 1819 | |
| 1820 | FunctionArea_save (me, U"Duplicate tier"); |
| 1821 | my textGrid() -> tiers -> addItemAtPosition_move (newTier.move(), position); |
| 1822 | } |
| 1823 | |
| 1824 | my selectedTier = position; |
| 1825 | FunctionArea_broadcastDataChanged (me); |
| 1826 | EDITOR_END |
| 1827 | } |
| 1828 | |
| 1829 | |
| 1830 | #pragma mark - TextGridArea Spell menu |
| 1831 | |
| 1832 | static void checkSpellingInTier (TextGridArea me) { |
| 1833 | checkTierSelection (me, U"check spelling"); |
| 1834 | const Function anyTier = my textGrid() -> tiers->at [my selectedTier]; |
| 1835 | if (anyTier -> classInfo == classIntervalTier) { |
| 1836 | const IntervalTier tier = (IntervalTier) anyTier; |
| 1837 | integer iinterval = IntervalTier_timeToIndex (tier, my startSelection()) + 1; |
| 1838 | while (iinterval <= tier -> intervals.size) { |
| 1839 | TextInterval interval = tier -> intervals.at [iinterval]; |
| 1840 | conststring32 text = interval -> text.get(); |
| 1841 | if (text) { |
| 1842 | integer position = 0; |
| 1843 | conststring32 notAllowed = SpellingChecker_nextNotAllowedWord (my spellingChecker, text, & position); |
| 1844 | if (notAllowed) { |
| 1845 | my setSelection (interval -> xmin, interval -> xmax); |
| 1846 | Melder_assert (isdefined (my startSelection())); // precondition of FunctionEditor_scrollToNewSelection() |
| 1847 | FunctionEditor_scrollToNewSelection (my functionEditor(), my startSelection()); |
| 1848 | GuiText_setSelection (my functionEditor() -> textArea, |
| 1849 | position, position + Melder_length (notAllowed)); |
| 1850 | return; |
| 1851 | } |
| 1852 | } |
| 1853 | iinterval ++; |
| 1854 | } |
| 1855 | if (iinterval > tier -> intervals.size) |
| 1856 | Melder_beep (); |
| 1857 | } else { |
| 1858 | const TextTier tier = (TextTier) anyTier; |
| 1859 | integer ipoint = AnyTier_timeToLowIndex (tier->asAnyTier(), my startSelection()) + 1; |
| 1860 | while (ipoint <= tier -> points.size) { |
| 1861 | TextPoint point = tier -> points.at [ipoint]; |
| 1862 | conststring32 text = point -> mark.get(); |
| 1863 | if (text) { |
| 1864 | integer position = 0; |
| 1865 | conststring32 notAllowed = SpellingChecker_nextNotAllowedWord (my spellingChecker, text, & position); |
| 1866 | if (notAllowed) { |
| 1867 | my setSelection (point -> number, point -> number); |
| 1868 | Melder_assert (isdefined (my startSelection())); // precondition of FunctionEditor_scrollToNewSelection() |
| 1869 | FunctionEditor_scrollToNewSelection (my functionEditor(), point -> number); |
| 1870 | GuiText_setSelection (my functionEditor() -> textArea, position, position + Melder_length (notAllowed)); |
| 1871 | return; |
| 1872 | } |
| 1873 | } |
| 1874 | ipoint ++; |
| 1875 | } |
| 1876 | if (ipoint > tier -> points.size) |
| 1877 | Melder_beep (); |
| 1878 | } |
| 1879 | } |
| 1880 | static void menu_cb_CheckSpelling (TextGridArea me, EDITOR_ARGS) { |
| 1881 | if (my spellingChecker) { |
| 1882 | integer left, right; |
| 1883 | autostring32 label = GuiText_getStringAndSelectionPosition (my functionEditor() -> textArea, & left, & right); |
| 1884 | integer position = right; |
| 1885 | conststring32 notAllowed = SpellingChecker_nextNotAllowedWord (my spellingChecker, label.get(), & position); |
| 1886 | if (notAllowed) |
| 1887 | GuiText_setSelection (my functionEditor() -> textArea, |
| 1888 | position, position + Melder_length (notAllowed)); |
| 1889 | else |
| 1890 | checkSpellingInTier (me); |
| 1891 | } |
| 1892 | } |
| 1893 | static void menu_cb_CheckSpellingInInterval (TextGridArea me, EDITOR_ARGS) { |
| 1894 | if (my spellingChecker) { |
| 1895 | integer left, right; |
| 1896 | autostring32 label = GuiText_getStringAndSelectionPosition (my functionEditor() -> textArea, & left, & right); |
| 1897 | integer position = right; |
| 1898 | conststring32 notAllowed = SpellingChecker_nextNotAllowedWord (my spellingChecker, label.get(), & position); |
| 1899 | if (notAllowed) |
| 1900 | GuiText_setSelection (my functionEditor() -> textArea, |
| 1901 | position, position + Melder_length (notAllowed)); |
| 1902 | } |
| 1903 | } |
| 1904 | static void menu_cb_AddToUserDictionary (TextGridArea me, EDITOR_ARGS) { |
| 1905 | if (my spellingChecker) { |
| 1906 | const autostring32 word = GuiText_getSelection (my functionEditor() -> textArea); |
| 1907 | SpellingChecker_addNewWord (my spellingChecker, word.get()); |
| 1908 | FunctionArea_broadcastDataChanged (me); |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | |
| 1913 | #pragma mark - TextGridArea all menus |
| 1914 | |
| 1915 | void structTextGridArea :: v_createMenus () { |
| 1916 | EditorMenu textGridMenu = Editor_addMenu (our functionEditor(), U"TextGrid", 0); |
| 1917 | FunctionAreaMenu_addCommand (textGridMenu, U"TextGrid settings... || TextGrid preferences...", 0, |
| 1918 | menu_cb_TextGridSettings, this); // alternative deprecated 2023 |
| 1919 | if (editable()) { |
| 1920 | FunctionAreaMenu_addCommand (textGridMenu, U"-- encoding --", 0, nullptr, this); |
| 1921 | FunctionAreaMenu_addCommand (textGridMenu, U"Convert entire TextGrid to backslash trigraphs", 0, |
| 1922 | menu_cb_ConvertToBackslashTrigraphs, this); |
| 1923 | FunctionAreaMenu_addCommand (textGridMenu, U"Convert entire TextGrid to Unicode", 0, |
| 1924 | menu_cb_ConvertToUnicode, this); |
| 1925 | } |
| 1926 | |
| 1927 | FunctionAreaMenu_addCommand (textGridMenu, U"- Select by TextGrid:", 0, nullptr, this); |
| 1928 | FunctionAreaMenu_addCommand (textGridMenu, U"Select previous tier", GuiMenu_OPTION | GuiMenu_UP_ARROW | GuiMenu_DEPTH_1, |
| 1929 | menu_cb_SelectPreviousTier, this); |
| 1930 | FunctionAreaMenu_addCommand (textGridMenu, U"Select next tier", GuiMenu_OPTION | GuiMenu_DOWN_ARROW | GuiMenu_DEPTH_1, |
| 1931 | menu_cb_SelectNextTier, this); |
| 1932 | FunctionAreaMenu_addCommand (textGridMenu, U"Select previous interval", GuiMenu_OPTION | GuiMenu_LEFT_ARROW | GuiMenu_DEPTH_1, |
| 1933 | menu_cb_SelectPreviousInterval, this); |
| 1934 | FunctionAreaMenu_addCommand (textGridMenu, U"Select next interval", GuiMenu_OPTION | GuiMenu_RIGHT_ARROW | GuiMenu_DEPTH_1, |
| 1935 | menu_cb_SelectNextInterval, this); |
| 1936 | FunctionAreaMenu_addCommand (textGridMenu, U"Extend-select left", GuiMenu_SHIFT | GuiMenu_OPTION | GuiMenu_LEFT_ARROW | GuiMenu_DEPTH_1, |
| 1937 | menu_cb_ExtendSelectPreviousInterval, this); |
| 1938 | FunctionAreaMenu_addCommand (textGridMenu, U"Extend-select right", GuiMenu_SHIFT | GuiMenu_OPTION | GuiMenu_RIGHT_ARROW | GuiMenu_DEPTH_1, |
| 1939 | menu_cb_ExtendSelectNextInterval, this); |
| 1940 | |
| 1941 | FunctionAreaMenu_addCommand (textGridMenu, U"- Draw TextGrid to picture window:", 0, nullptr, this); |
| 1942 | FunctionAreaMenu_addCommand (textGridMenu, U"Draw visible TextGrid...", 1, |
| 1943 | menu_cb_DrawVisibleTextGrid, this); |
| 1944 | if (our borrowedSoundArea) |
| 1945 | FunctionAreaMenu_addCommand (textGridMenu, U"Draw visible sound and TextGrid...", 1, |
| 1946 | menu_cb_DrawVisibleSoundAndTextGrid, this); |
| 1947 | if (our borrowedSoundAnalysisArea) |
| 1948 | FunctionAreaMenu_addCommand (textGridMenu, U"Draw visible pitch contour and TextGrid...", 1, |
| 1949 | menu_cb_DrawTextGridAndPitch, this); |
| 1950 | |
| 1951 | FunctionAreaMenu_addCommand (textGridMenu, U"- Extract TextGrid to objects window:", 0, nullptr, this); |
| 1952 | our extractSelectedTextGridPreserveTimesButton = FunctionAreaMenu_addCommand (textGridMenu, |
| 1953 | U"Extract selected TextGrid (preserve times)", |
| 1954 | 1, CONVERT_DATA_TO_ONE__ExtractSelectedTextGrid_preserveTimes, this |
| 1955 | ); |
| 1956 | our extractSelectedTextGridTimeFromZeroButton = FunctionAreaMenu_addCommand (textGridMenu, |
| 1957 | U"Extract selected TextGrid (time from 0)", |
| 1958 | 1, CONVERT_DATA_TO_ONE__ExtractSelectedTextGrid_timeFromZero, this |
| 1959 | ); |
| 1960 | |
| 1961 | EditorMenu intervalMenu = Editor_addMenu (our functionEditor(), U"Interval", 0); |
| 1962 | if (our editable()) { |
| 1963 | if (our borrowedSoundArea) { |
| 1964 | FunctionAreaMenu_addCommand (intervalMenu, U"Align interval", 'E', |
| 1965 | menu_cb_AlignInterval, this); |
| 1966 | FunctionAreaMenu_addCommand (intervalMenu, U"Alignment settings...", 0, |
| 1967 | menu_cb_AlignmentSettings, this); |
| 1968 | FunctionAreaMenu_addCommand (intervalMenu, U"-- after align --", 0, nullptr, this); |
| 1969 | FunctionAreaMenu_addCommand (intervalMenu, U"Transcribe interval", 'T', |
| 1970 | menu_cb_TranscribeInterval, this); |
| 1971 | FunctionAreaMenu_addCommand (intervalMenu, U"Transcription settings...", 0, |
| 1972 | menu_cb_TranscriptionSettings, this); |
| 1973 | FunctionAreaMenu_addCommand (intervalMenu, U"-- after transcribe --", 0, nullptr, this); |
| 1974 | FunctionAreaMenu_addCommand (intervalMenu, U"Diarize interval", 'D', |
| 1975 | menu_cb_DiarizeInterval, this); |
| 1976 | FunctionAreaMenu_addCommand (intervalMenu, U"Diarization settings...", 0, |
| 1977 | menu_cb_DiarizationSettings, this); |
| 1978 | FunctionAreaMenu_addCommand (intervalMenu, U"-- after diarize --", 0, nullptr, this); |
| 1979 | } |
| 1980 | FunctionAreaMenu_addCommand (intervalMenu, U"New interval:", 0, nullptr, this); |
| 1981 | FunctionAreaMenu_addCommand (intervalMenu, U"Add interval on tier 1", GuiMenu_COMMAND | '1' | GuiMenu_DEPTH_1, |
| 1982 | menu_cb_InsertIntervalOnTier1, this); |
| 1983 | FunctionAreaMenu_addCommand (intervalMenu, U"Add interval on tier 2", GuiMenu_COMMAND | '2' | GuiMenu_DEPTH_1, |
| 1984 | menu_cb_InsertIntervalOnTier2, this); |
| 1985 | FunctionAreaMenu_addCommand (intervalMenu, U"Add interval on tier 3", GuiMenu_COMMAND | '3' | GuiMenu_DEPTH_1, |
| 1986 | menu_cb_InsertIntervalOnTier3, this); |
| 1987 | FunctionAreaMenu_addCommand (intervalMenu, U"Add interval on tier 4", GuiMenu_COMMAND | '4' | GuiMenu_DEPTH_1, |
| 1988 | menu_cb_InsertIntervalOnTier4, this); |
| 1989 | FunctionAreaMenu_addCommand (intervalMenu, U"Add interval on tier 5", GuiMenu_COMMAND | '5' | GuiMenu_DEPTH_1, |
| 1990 | menu_cb_InsertIntervalOnTier5, this); |
| 1991 | FunctionAreaMenu_addCommand (intervalMenu, U"Add interval on tier 6", GuiMenu_COMMAND | '6' | GuiMenu_DEPTH_1, |
| 1992 | menu_cb_InsertIntervalOnTier6, this); |
| 1993 | FunctionAreaMenu_addCommand (intervalMenu, U"Add interval on tier 7", GuiMenu_COMMAND | '7' | GuiMenu_DEPTH_1, |
| 1994 | menu_cb_InsertIntervalOnTier7, this); |
| 1995 | FunctionAreaMenu_addCommand (intervalMenu, U"Add interval on tier 8", GuiMenu_COMMAND | '8' | GuiMenu_DEPTH_1, |
| 1996 | menu_cb_InsertIntervalOnTier8, this); |
| 1997 | } |
| 1998 | |
| 1999 | FunctionAreaMenu_addCommand (intervalMenu, U"- Query interval:", 0, nullptr, this); |
| 2000 | FunctionAreaMenu_addCommand (intervalMenu, U"Get starting point of interval", 1, |
| 2001 | QUERY_DATA_FOR_REAL__GetStartingPointOfInterval, this); |
| 2002 | FunctionAreaMenu_addCommand (intervalMenu, U"Get end point of interval", 1, |
| 2003 | QUERY_DATA_FOR_REAL__GetEndPointOfInterval, this); |
| 2004 | FunctionAreaMenu_addCommand (intervalMenu, U"Get label of interval", 1, |
| 2005 | QUERY_DATA_FOR_STRING__GetLabelOfInterval, this); |
| 2006 | |
| 2007 | EditorMenu boundaryMenu = Editor_addMenu (our functionEditor(), U"Boundary", 0); |
| 2008 | /*FunctionAreaMenu_addCommand (boundaryMenu, U"Move to B", 0, menu_cb_MoveToB, this); |
| 2009 | FunctionAreaMenu_addCommand (boundaryMenu, U"Move to E", 0, menu_cb_MoveToE, this);*/ |
| 2010 | if (our editable()) { |
| 2011 | FunctionAreaMenu_addCommand (boundaryMenu, U"New boundary or point:", 0, nullptr, this); |
| 2012 | FunctionAreaMenu_addCommand (boundaryMenu, U"Add on selected tier", GuiMenu_ENTER | GuiMenu_DEPTH_1, |
| 2013 | menu_cb_InsertOnSelectedTier, this); |
| 2014 | FunctionAreaMenu_addCommand (boundaryMenu, U"Add on tier 1", GuiMenu_COMMAND_EXTRA | '1' | GuiMenu_DEPTH_1, |
| 2015 | menu_cb_InsertOnTier1, this); |
| 2016 | FunctionAreaMenu_addCommand (boundaryMenu, U"Add on tier 2", GuiMenu_COMMAND_EXTRA | '2' | GuiMenu_DEPTH_1, |
| 2017 | menu_cb_InsertOnTier2, this); |
| 2018 | FunctionAreaMenu_addCommand (boundaryMenu, U"Add on tier 3", GuiMenu_COMMAND_EXTRA | '3' | GuiMenu_DEPTH_1, |
| 2019 | menu_cb_InsertOnTier3, this); |
| 2020 | FunctionAreaMenu_addCommand (boundaryMenu, U"Add on tier 4", GuiMenu_COMMAND_EXTRA | '4' | GuiMenu_DEPTH_1, |
| 2021 | menu_cb_InsertOnTier4, this); |
| 2022 | FunctionAreaMenu_addCommand (boundaryMenu, U"Add on tier 5", GuiMenu_COMMAND_EXTRA | '5' | GuiMenu_DEPTH_1, |
| 2023 | menu_cb_InsertOnTier5, this); |
| 2024 | FunctionAreaMenu_addCommand (boundaryMenu, U"Add on tier 6", GuiMenu_COMMAND_EXTRA | '6' | GuiMenu_DEPTH_1, |
| 2025 | menu_cb_InsertOnTier6, this); |
| 2026 | FunctionAreaMenu_addCommand (boundaryMenu, U"Add on tier 7", GuiMenu_COMMAND_EXTRA | '7' | GuiMenu_DEPTH_1, |
| 2027 | menu_cb_InsertOnTier7, this); |
| 2028 | FunctionAreaMenu_addCommand (boundaryMenu, U"Add on tier 8", GuiMenu_COMMAND_EXTRA | '8' | GuiMenu_DEPTH_1, |
| 2029 | menu_cb_InsertOnTier8, this); |
| 2030 | FunctionAreaMenu_addCommand (boundaryMenu, U"Add on all tiers", GuiMenu_COMMAND_EXTRA | '9' | GuiMenu_DEPTH_1, |
| 2031 | menu_cb_InsertOnAllTiers, this); |
| 2032 | FunctionAreaMenu_addCommand (boundaryMenu, U"- Modify boundary or point:", 0, nullptr, this); |
| 2033 | if (our borrowedSoundArea && ! Thing_isa (our borrowedSoundArea, classLongSoundArea)) { |
| 2034 | FunctionAreaMenu_addCommand (boundaryMenu, U"Move to nearest zero crossing", 1, |
| 2035 | menu_cb_MoveToZero, this); |
| 2036 | } |
| 2037 | FunctionAreaMenu_addCommand (boundaryMenu, U"Remove", GuiMenu_OPTION | GuiMenu_BACKSPACE | GuiMenu_DEPTH_1, |
| 2038 | menu_cb_RemovePointOrBoundary, this); |
| 2039 | } |
| 2040 | |
| 2041 | EditorMenu tierMenu = Editor_addMenu (our functionEditor(), U"Tier", 0); |
| 2042 | if (our editable()) { |
| 2043 | FunctionAreaMenu_addCommand (tierMenu, U"New tier:", 0, nullptr, this); |
| 2044 | FunctionAreaMenu_addCommand (tierMenu, U"Add interval tier...", 1, |
| 2045 | menu_cb_AddIntervalTier, this); |
| 2046 | FunctionAreaMenu_addCommand (tierMenu, U"Add point tier...", 1, |
| 2047 | menu_cb_AddPointTier, this); |
| 2048 | FunctionAreaMenu_addCommand (tierMenu, U"Duplicate tier...", 1, |
| 2049 | menu_cb_DuplicateTier, this); |
| 2050 | FunctionAreaMenu_addCommand (tierMenu, U"- Modify tier:", 0, nullptr, this); |
| 2051 | FunctionAreaMenu_addCommand (tierMenu, U"Rename tier...", 1, |
| 2052 | menu_cb_RenameTier, this); |
| 2053 | FunctionAreaMenu_addCommand (tierMenu, U"Remove all text from tier", 1, |
| 2054 | menu_cb_RemoveAllTextFromTier, this); |
| 2055 | FunctionAreaMenu_addCommand (tierMenu, U"Remove entire tier", 1, |
| 2056 | menu_cb_RemoveTier, this); |
| 2057 | } |
| 2058 | FunctionAreaMenu_addCommand (tierMenu, U"- Extract to list of objects:", 0, nullptr, this); |
| 2059 | FunctionAreaMenu_addCommand (tierMenu, U"Extract entire selected tier", 1, |
| 2060 | CONVERT_DATA_TO_ONE__PublishTier, this); |
| 2061 | |
| 2062 | if (our functionEditor() -> textArea) |
| 2063 | GuiText_setChangedCallback (our functionEditor() -> textArea, gui_text_cb_changed, this); |
| 2064 | |
| 2065 | if (our spellingChecker) { |
| 2066 | EditorMenu spellMenu = Editor_addMenu (our functionEditor(), U"Spell", 0); |
| 2067 | FunctionAreaMenu_addCommand (spellMenu, U"Check spelling in tier", GuiMenu_COMMAND_EXTRA | 'L', |
| 2068 | menu_cb_CheckSpelling, this); |
| 2069 | FunctionAreaMenu_addCommand (spellMenu, U"Check spelling in interval", 0, |
| 2070 | menu_cb_CheckSpellingInInterval, this); |
| 2071 | FunctionAreaMenu_addCommand (spellMenu, U"-- edit lexicon --", 0, nullptr, this); |
| 2072 | FunctionAreaMenu_addCommand (spellMenu, U"Add selected word to user dictionary", 0, |
| 2073 | menu_cb_AddToUserDictionary, this); |
| 2074 | } |
| 2075 | } |
| 2076 | void structTextGridArea :: v_updateMenuItems () { |
| 2077 | GuiThing_setSensitive (extractSelectedTextGridPreserveTimesButton, our endSelection() > our startSelection()); |
| 2078 | GuiThing_setSensitive (extractSelectedTextGridTimeFromZeroButton, our endSelection() > our startSelection()); |
| 2079 | } |
| 2080 | |
| 2081 | void structTextGridArea :: v_updateText () { |
| 2082 | if (! our textGrid()) |
| 2083 | return; // BUG: should not be needed |
| 2084 | if (our suppressTextCursorJump) |
| 2085 | return; |
| 2086 | conststring32 newText = U""; |
| 2087 | if (our selectedTier != 0) { |
| 2088 | IntervalTier intervalTier; |
| 2089 | TextTier textTier; |
| 2090 | trace (U"TextGrid ", Melder_pointer (our textGrid())); |
| 2091 | AnyTextGridTier_identifyClass (our textGrid() -> tiers->at [our selectedTier], & intervalTier, & textTier); |
| 2092 | if (intervalTier) { |
| 2093 | const integer iinterval = IntervalTier_timeToIndex (intervalTier, our startSelection()); |
| 2094 | if (iinterval) { |
| 2095 | TextInterval interval = intervalTier -> intervals.at [iinterval]; |
| 2096 | if (interval -> text) |
| 2097 | newText = interval -> text.get(); |
| 2098 | } |
| 2099 | } else { |
| 2100 | Melder_assert (isdefined (our startSelection())); |
| 2101 | const integer ipoint = AnyTier_hasPoint (textTier->asAnyTier(), our startSelection()); |
| 2102 | if (ipoint) { |
| 2103 | TextPoint point = textTier -> points.at [ipoint]; |
| 2104 | if (point -> mark) |
| 2105 | newText = point -> mark.get(); |
| 2106 | } |
| 2107 | } |
| 2108 | } |
| 2109 | if (our functionEditor() -> textArea) { |
| 2110 | our suppressRedraw = true; // prevent valueChangedCallback from redrawing |
| 2111 | trace (U"setting new text ", newText); |
| 2112 | GuiText_setString (our functionEditor() -> textArea, newText); |
| 2113 | const integer cursor = Melder_length (newText); // at end |
| 2114 | GuiText_setSelection (our functionEditor() -> textArea, cursor, cursor); |
| 2115 | our suppressRedraw = false; |
| 2116 | } |
| 2117 | } |
| 2118 | |
| 2119 | |
| 2120 | #pragma mark - TextGridArea IPA chart |
| 2121 | |
| 2122 | static const conststring32 characters [12] [10] = { |
| 2123 | { U"ɑ", U"ɐ", U"ɒ", U"æ", U"ʙ", U"ɓ", U"β", U"ç", U"ɕ", U"ð" }, |
| 2124 | { U"ɗ", U"ɖ", U"ɛ", U"ɜ", U"ə", U"ɢ", U"ɠ", U"ʛ", U"ɣ", U"ɤ" }, |
| 2125 | { U"ˠ", U"ʜ", U"ɦ", U"ħ", U"ʰ", U"ɧ", U"ɪ", U"ɨ", U"ı", U"ɟ" }, |
| 2126 | { U"ʝ", U"ʄ", U"ᴊ", U"ʲ", U"ʟ", U"ɬ", U"ɭ", U"ɮ", U"ɫ", U"ˡ" }, |
| 2127 | { U"ɰ", U"ɯ", U"ɱ", U"ɴ", U"ŋ", U"ɲ", U"ɳ", U"ⁿ", U"ɔ", U"ɵ" }, |
| 2128 | |
| 2129 | { U"ø", U"œ", U"ɶ", U"ɸ", U"ɹ", U"ʀ", U"ʁ", U"ɾ", U"ɽ", U"ɺ" }, |
| 2130 | { U"ɻ", U"ʃ", U"ʂ", U"θ", U"ʈ", U"ʉ", U"ʊ", U"ʌ", U"ʋ", U"ʍ" }, |
| 2131 | { U"ʷ", U"χ", U"ʎ", U"ʏ", U"ɥ", U"ʒ", U"ʐ", U"ʑ", U"ˑ", U"ː" }, |
| 2132 | { U"ʔ", U"ʡ", U"ʕ", U"ʢ", U"ˤ", U"ǃ", U"ʘ", U"ǀ", U"ǁ", U"ǂ" }, |
| 2133 | { U"ʤ", U"ɘ", U"ɚ", U"ɝ", U"ʱ", U"ˢ", U"ʧ", U"ɞ", U"ʦ", U"ʣ" }, |
| 2134 | |
| 2135 | { U"ʨ", U"ʥ", U"z̊", U"z̥", U"z̤", U"z̰", U"z̪", U"z̩", U"z̝", U"z̞" }, |
| 2136 | { U"ý", U"ȳ", U"ỳ", U"ÿ", U"ỹ", U"o̟", U"o̱", U"o̹", U"o̜", U"t̚" }, |
| 2137 | }; |
| 2138 | |
| 2139 | void TextGridArea_drawSelectionViewer (TextGridArea me) { |
| 2140 | Graphics_setWindow (my graphics(), 0.5, 10.5, 0.5, 12.5); |
| 2141 | Graphics_setColour (my graphics(), Melder_WHITE); |
| 2142 | Graphics_fillRectangle (my graphics(), 0.5, 10.5, 0.5, 12.5); |
| 2143 | Graphics_setColour (my graphics(), Melder_BLACK); |
| 2144 | Graphics_setFont (my graphics(), kGraphics_font::TIMES); |
| 2145 | const double pointsPerMillimetre = 72.0 / 25.4; |
| 2146 | const double cellWidth_points = Graphics_dxWCtoMM (my graphics(), 1.0) * pointsPerMillimetre; |
| 2147 | const double cellHeight_points = Graphics_dyWCtoMM (my graphics(), 1.0) * pointsPerMillimetre; |
| 2148 | const double fontSize = std::min (0.8 * cellHeight_points, 1.2 * cellWidth_points); |
| 2149 | Graphics_setFontSize (my graphics(), fontSize); |
| 2150 | Graphics_setTextAlignment (my graphics(), Graphics_CENTRE, Graphics_HALF); |
| 2151 | for (integer irow = 1; irow <= 12; irow ++) |
| 2152 | for (integer icol = 1; icol <= 10; icol ++) |
| 2153 | Graphics_text (my graphics(), 0.0 + 1.0 * icol, 13.0 - 1.0 * irow, characters [irow-1] [icol-1]); |
| 2154 | } |
| 2155 | void TextGridArea_clickSelectionViewer (TextGridArea me, double x_fraction, double y_fraction) { |
| 2156 | const integer rowNumber = Melder_iceiling ((1.0 - y_fraction) * 12.0); |
| 2157 | const integer columnNumber = Melder_iceiling (x_fraction * 10.0); |
| 2158 | if (rowNumber < 1 || rowNumber > 12) |
| 2159 | return; |
| 2160 | if (columnNumber < 1 || columnNumber > 10) |
| 2161 | return; |
| 2162 | conststring32 character = characters [rowNumber-1] [columnNumber-1]; |
| 2163 | character += Melder_length (character) - 1; |
| 2164 | if (my functionEditor() -> textArea) { |
| 2165 | integer first = 0, last = 0; |
| 2166 | autostring32 oldText = GuiText_getStringAndSelectionPosition (my functionEditor() -> textArea, & first, & last); |
| 2167 | static MelderString newText; |
| 2168 | MelderString_empty (& newText); |
| 2169 | MelderString_ncopy (& newText, oldText.get(), first); |
| 2170 | MelderString_append (& newText, character); |
| 2171 | MelderString_append (& newText, oldText.get() + last); |
| 2172 | if (my selectedTier != 0) { |
| 2173 | IntervalTier intervalTier; |
| 2174 | TextTier textTier; |
| 2175 | AnyTextGridTier_identifyClass (my textGrid() -> tiers->at [my selectedTier], & intervalTier, & textTier); |
| 2176 | if (intervalTier) { |
| 2177 | const integer selectedInterval = getSelectedInterval (me); |
| 2178 | if (selectedInterval != 0) { |
| 2179 | TextInterval interval = intervalTier -> intervals.at [selectedInterval]; |
| 2180 | TextInterval_setText (interval, newText.string); |
| 2181 | |
| 2182 | my suppressRedraw = true; // prevent valueChangedCallback from redrawing |
| 2183 | trace (U"setting new text ", newText.string); |
| 2184 | GuiText_setString (my functionEditor() -> textArea, newText.string); |
| 2185 | GuiText_setSelection (my functionEditor() -> textArea, first + 1, first + 1); |
| 2186 | my suppressRedraw = false; |
| 2187 | |
| 2188 | my suppressTextCursorJump = true; |
| 2189 | FunctionArea_broadcastDataChanged (me); |
| 2190 | my suppressTextCursorJump = false; |
| 2191 | } |
| 2192 | } else { |
| 2193 | const integer selectedPoint = getSelectedPoint (me); |
| 2194 | if (selectedPoint != 0) { |
| 2195 | TextPoint point = textTier -> points.at [selectedPoint]; |
| 2196 | point -> mark. reset(); |
| 2197 | if (Melder_findInk (newText.string)) // any visible characters? |
| 2198 | point -> mark = Melder_dup_f (newText.string); |
| 2199 | |
| 2200 | my suppressRedraw = true; // prevent valueChangedCallback from redrawing |
| 2201 | trace (U"setting new text ", newText.string); |
| 2202 | GuiText_setString (my functionEditor() -> textArea, newText.string); |
| 2203 | GuiText_setSelection (my functionEditor() -> textArea, first + 1, first + 1); |
| 2204 | my suppressRedraw = false; |
| 2205 | |
| 2206 | my suppressTextCursorJump = true; |
| 2207 | FunctionArea_broadcastDataChanged (me); |
| 2208 | my suppressTextCursorJump = false; |
| 2209 | } |
| 2210 | } |
| 2211 | } |
| 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | /* End of file TextGridArea.cpp */ |