| 1 | /* FunctionEditor.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 "FunctionEditor.h" |
| 20 | #include "machine.h" |
| 21 | #include "EditorM.h" |
| 22 | #include "GuiP.h" |
| 23 | #include "FunctionArea.h" |
| 24 | |
| 25 | Thing_implement_pureVirtual (FunctionEditor, Editor, 0); |
| 26 | |
| 27 | #include "Prefs_define.h" |
| 28 | #include "FunctionEditor_prefs.h" |
| 29 | #include "Prefs_install.h" |
| 30 | #include "FunctionEditor_prefs.h" |
| 31 | #include "Prefs_copyToInstance.h" |
| 32 | #include "FunctionEditor_prefs.h" |
| 33 | #include "GraphicsP.h" |
| 34 | |
| 35 | namespace { |
| 36 | constexpr double maximumScrollBarValue = 2'000'000'000.0; |
| 37 | constexpr double RELATIVE_PAGE_INCREMENT = 0.8; |
| 38 | constexpr double SCROLL_INCREMENT_FRACTION = 20.0; |
| 39 | constexpr int TEXT_HEIGHT = 50; |
| 40 | constexpr int BUTTON_X = 3; |
| 41 | constexpr int BUTTON_WIDTH = 40; |
| 42 | constexpr int BUTTON_SPACING = 8; |
| 43 | |
| 44 | constexpr integer THE_MAXIMUM_GROUP_SIZE = 100; |
| 45 | integer theGroupSize = 0; |
| 46 | FunctionEditor theGroupMembers [1 + THE_MAXIMUM_GROUP_SIZE]; |
| 47 | } |
| 48 | |
| 49 | static bool group_equalDomain (double tmin, double tmax) { |
| 50 | if (theGroupSize == 0) |
| 51 | return true; |
| 52 | for (integer i = 1; i <= THE_MAXIMUM_GROUP_SIZE; i ++) |
| 53 | if (theGroupMembers [i]) |
| 54 | return ( tmin == theGroupMembers [i] -> tmin && tmax == theGroupMembers [i] -> tmax ); |
| 55 | return false; // should not occur |
| 56 | } |
| 57 | |
| 58 | static void updateScrollBar (FunctionEditor me) { |
| 59 | /* We cannot call this immediately after creation. */ |
| 60 | if (my endWindow - my startWindow > my tmax - my tmin) |
| 61 | Melder_crash (U"updateScrollBar: the window runs from ", my startWindow, U" to ", my endWindow, U" ", my v_format_units_long (), |
| 62 | U", but the whole domain runs only from ", my tmin, U" to ", my tmax, U" ", my v_format_units_long (), U"."); |
| 63 | const double slider_size = Melder_clippedLeft (1.0, (my endWindow - my startWindow) / (my tmax - my tmin) * maximumScrollBarValue - 1.0); |
| 64 | Melder_assert (maximumScrollBarValue - slider_size >= 1.0); |
| 65 | const double value = Melder_clipped (1.0, (my startWindow - my tmin) / (my tmax - my tmin) * maximumScrollBarValue + 1.0, maximumScrollBarValue - slider_size); |
| 66 | const double increment = slider_size / SCROLL_INCREMENT_FRACTION + 1.0; |
| 67 | const double page_increment = RELATIVE_PAGE_INCREMENT * slider_size + 1.0; |
| 68 | GuiScrollBar_set (my scrollBar, undefined, maximumScrollBarValue, value, slider_size, increment, page_increment); |
| 69 | } |
| 70 | |
| 71 | static void updateGroup (FunctionEditor me, const bool windowMarkersChanged, const bool selectionMarkersChanged) { |
| 72 | if (! my group) |
| 73 | return; |
| 74 | for (integer i = 1; i <= THE_MAXIMUM_GROUP_SIZE; i ++) { |
| 75 | if (theGroupMembers [i] && theGroupMembers [i] != me) { |
| 76 | FunctionEditor thee = theGroupMembers [i]; |
| 77 | if (my classPref_synchronizedZoomAndScroll() && windowMarkersChanged) { |
| 78 | thy startWindow = my startWindow; |
| 79 | thy endWindow = my endWindow; |
| 80 | thy v_windowChanged (); |
| 81 | } |
| 82 | if (selectionMarkersChanged) { |
| 83 | thy startSelection = my startSelection; |
| 84 | thy endSelection = my endSelection; |
| 85 | } |
| 86 | Melder_assert (isdefined (thy startSelection)); // precondition of FunctionEditor_updateText() |
| 87 | FunctionEditor_updateText (thee); |
| 88 | updateScrollBar (thee); |
| 89 | FunctionEditor_redraw (thee); // BUG: does this do *two* updates if thou containst the same data as me? |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static void drawBackgroundAndData (FunctionEditor me) { |
| 95 | if (Melder_debug == 55) |
| 96 | Melder_casual (Thing_messageNameAndAddress (me), U" draw"); |
| 97 | const bool leftFromWindow = ( my startWindow > my tmin ); |
| 98 | const bool rightFromWindow = ( my endWindow < my tmax ); |
| 99 | const bool cursorIsVisible = ( my startSelection == my endSelection && my startSelection >= my startWindow && my startSelection <= my endWindow ); |
| 100 | const bool selectionIsNonempty = ( my endSelection > my startSelection ); |
| 101 | |
| 102 | /* |
| 103 | Update selection. |
| 104 | */ |
| 105 | const bool startIsVisible = ( my startSelection > my startWindow && my startSelection < my endWindow ); |
| 106 | const bool endIsVisible = ( my endSelection > my startWindow && my endSelection < my endWindow ); |
| 107 | |
| 108 | /* |
| 109 | Update markers. |
| 110 | */ |
| 111 | my numberOfMarkers = 0; |
| 112 | if (startIsVisible) |
| 113 | my marker [++ my numberOfMarkers] = my startSelection; |
| 114 | if (endIsVisible && my endSelection != my startSelection) |
| 115 | my marker [++ my numberOfMarkers] = my endSelection; |
| 116 | my marker [++ my numberOfMarkers] = my endWindow; |
| 117 | sort_e_VEC_inout (VEC (& my marker [1], my numberOfMarkers)); |
| 118 | |
| 119 | /* |
| 120 | Update rectangles. |
| 121 | */ |
| 122 | |
| 123 | /* |
| 124 | Initialize to 0; rectangles that stay at zero will not be drawn. |
| 125 | */ |
| 126 | for (integer i = 0; i < 8; i++) |
| 127 | my rect [i]. left = my rect [i]. right = 0; |
| 128 | |
| 129 | /* |
| 130 | 0: rectangle for total. |
| 131 | */ |
| 132 | my rect [0]. left = my _functionViewerLeft + ( leftFromWindow ? 0 : my MARGIN ); |
| 133 | my rect [0]. right = my _functionViewerRight - ( rightFromWindow ? 0 : my MARGIN ); |
| 134 | my rect [0]. bottom = my BOTTOM_MARGIN; |
| 135 | my rect [0]. top = my BOTTOM_MARGIN + my space; |
| 136 | |
| 137 | /* |
| 138 | 1: rectangle for visible part. |
| 139 | */ |
| 140 | my rect [1]. left = my _functionViewerLeft + my MARGIN; |
| 141 | my rect [1]. right = my _functionViewerRight - my MARGIN; |
| 142 | my rect [1]. bottom = my BOTTOM_MARGIN + my space; |
| 143 | my rect [1]. top = my BOTTOM_MARGIN + my space * ( my numberOfMarkers > 1 ? 2 : 3 ); |
| 144 | |
| 145 | /* |
| 146 | 2: rectangle for left from visible part. |
| 147 | */ |
| 148 | if (leftFromWindow) { |
| 149 | my rect [2]. left = my _functionViewerLeft; |
| 150 | my rect [2]. right = my _functionViewerLeft + my MARGIN; |
| 151 | my rect [2]. bottom = my BOTTOM_MARGIN + my space; |
| 152 | my rect [2]. top = my BOTTOM_MARGIN + my space * 2; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | 3: rectangle for right from visible part. |
| 157 | */ |
| 158 | if (rightFromWindow) { |
| 159 | my rect [3]. left = my _functionViewerRight - my MARGIN; |
| 160 | my rect [3]. right = my _functionViewerRight; |
| 161 | my rect [3]. bottom = my BOTTOM_MARGIN + my space; |
| 162 | my rect [3]. top = my BOTTOM_MARGIN + my space * 2; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | 4, 5, 6: rectangles between markers visible in visible part. |
| 167 | */ |
| 168 | if (my numberOfMarkers > 1) { |
| 169 | const double window = my endWindow - my startWindow; |
| 170 | for (integer i = 1; i <= my numberOfMarkers; i ++) { |
| 171 | my rect [3 + i]. left = i == 1 ? my _functionViewerLeft + my MARGIN : my _functionViewerLeft + my MARGIN + (my _functionViewerRight - my _functionViewerLeft - my MARGIN * 2) * |
| 172 | (my marker [i - 1] - my startWindow) / window; |
| 173 | my rect [3 + i]. right = my _functionViewerLeft + my MARGIN + (my _functionViewerRight - my _functionViewerLeft - my MARGIN * 2) * |
| 174 | (my marker [i] - my startWindow) / window; |
| 175 | my rect [3 + i]. bottom = my BOTTOM_MARGIN + my space * 2; |
| 176 | my rect [3 + i]. top = my BOTTOM_MARGIN + my space * 3; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (selectionIsNonempty) { |
| 181 | const double window = my endWindow - my startWindow; |
| 182 | const double left = |
| 183 | my startSelection == my startWindow ? |
| 184 | my _functionViewerLeft + my MARGIN |
| 185 | : my startSelection == my tmin ? |
| 186 | my _functionViewerLeft |
| 187 | : my startSelection < my startWindow ? |
| 188 | my _functionViewerLeft + my MARGIN * 0.3 |
| 189 | : my startSelection < my endWindow ? |
| 190 | my _functionViewerLeft + my MARGIN + (my _functionViewerRight - my _functionViewerLeft - my MARGIN * 2) * (my startSelection - my startWindow) / window |
| 191 | : my startSelection == my endWindow ? |
| 192 | my _functionViewerRight - my MARGIN |
| 193 | : |
| 194 | my _functionViewerRight - my MARGIN * 0.7 |
| 195 | ; |
| 196 | const double right = |
| 197 | my endSelection < my startWindow ? |
| 198 | my _functionViewerLeft + my MARGIN * 0.7 |
| 199 | : my endSelection == my startWindow ? |
| 200 | my _functionViewerLeft + my MARGIN |
| 201 | : my endSelection < my endWindow ? |
| 202 | my _functionViewerLeft + my MARGIN + (my _functionViewerRight - my _functionViewerLeft - my MARGIN * 2) * (my endSelection - my startWindow) / window |
| 203 | : my endSelection == my endWindow ? |
| 204 | my _functionViewerRight - my MARGIN |
| 205 | : my endSelection < my tmax ? |
| 206 | my _functionViewerRight - my MARGIN * 0.3 |
| 207 | : my _functionViewerRight |
| 208 | ; |
| 209 | my rect [7]. left = left; |
| 210 | my rect [7]. right = right; |
| 211 | my rect [7]. bottom = my height_pxlt - my space - my TOP_MARGIN; |
| 212 | my rect [7]. top = my height_pxlt - my TOP_MARGIN; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | Window background. |
| 217 | */ |
| 218 | my viewAllAsPixelettes (); |
| 219 | Graphics_setColour (my graphics.get(), DataGuiColour_WINDOW_BACKGROUND); |
| 220 | Graphics_fillRectangle (my graphics.get(), my _functionViewerLeft, my _selectionViewerRight, my BOTTOM_MARGIN, my height_pxlt); |
| 221 | Graphics_setColour (my graphics.get(), Melder_BLACK); |
| 222 | |
| 223 | /* |
| 224 | Buttons (still without text). |
| 225 | */ |
| 226 | my viewFunctionViewerAsPixelettes (); |
| 227 | Graphics_setFont (my graphics.get(), kGraphics_font :: HELVETICA); |
| 228 | Graphics_setFontSize (my graphics.get(), 12.0); |
| 229 | Graphics_setTextAlignment (my graphics.get(), Graphics_CENTRE, Graphics_HALF); |
| 230 | for (integer i = 0; i < 8; i ++) { |
| 231 | const double left = my rect [i]. left, right = my rect [i]. right; |
| 232 | if (left < right) |
| 233 | Graphics_button (my graphics.get(), left, right, my rect [i]. bottom, my rect [i]. top); |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | Opening triangle (sometimes over button). |
| 238 | */ |
| 239 | if (my v_hasSelectionViewer() && ! my instancePref_showSelectionViewer()) { |
| 240 | const bool weHaveToDrawOverSelectionRectangleWithText = ( selectionIsNonempty && my endSelection == my tmax && my endWindow != my tmax ); |
| 241 | Graphics_setLineWidth (my graphics.get(), 1.0); |
| 242 | const double left = my _functionViewerRight - my space + 9.0, right = my _functionViewerRight - 3.0; |
| 243 | const double bottom = my height_pxlt - my space - my TOP_MARGIN + 3.0, top = my height_pxlt - my TOP_MARGIN - 3.0; |
| 244 | Graphics_setColour (my graphics.get(), Melder_PINK); |
| 245 | const double x [] = { left, right, left }, y [] = { bottom, 0.5 * (bottom + top), top }; |
| 246 | Graphics_fillArea (my graphics.get(), 3, x, y); |
| 247 | if (! weHaveToDrawOverSelectionRectangleWithText) { |
| 248 | Graphics_setColour (my graphics.get(), Melder_GREY); |
| 249 | Graphics_polyline_closed (my graphics.get(), 3, x, y); |
| 250 | } |
| 251 | Graphics_setColour (my graphics.get(), Melder_BLACK); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | Button texts (sometimes over opening triangle). |
| 256 | */ |
| 257 | const double verticalCorrection = my height_pxlt / (my height_pxlt - 111.0 + 11.0) |
| 258 | #ifdef _WIN32 |
| 259 | * 1.5 |
| 260 | #endif |
| 261 | ; |
| 262 | for (integer i = 0; i < 8; i ++) { |
| 263 | const double left = my rect [i]. left, right = my rect [i]. right; |
| 264 | const double bottom = my rect [i]. bottom, top = my rect [i]. top; |
| 265 | if (left < right) { |
| 266 | conststring8 format = my v_format_long (); |
| 267 | double value = undefined, inverseValue = 0.0; |
| 268 | switch (i) { |
| 269 | case 0: { |
| 270 | format = my v_format_totalDuration (); |
| 271 | value = my tmax - my tmin; |
| 272 | } break; case 1: { |
| 273 | format = my v_format_window (); |
| 274 | value = my endWindow - my startWindow; |
| 275 | /* |
| 276 | Window domain text. |
| 277 | */ |
| 278 | Graphics_setColour (my graphics.get(), Melder_BLUE); |
| 279 | Graphics_setTextAlignment (my graphics.get(), Graphics_LEFT, Graphics_HALF); |
| 280 | Graphics_text (my graphics.get(), left, 0.5 * (bottom + top) - verticalCorrection, |
| 281 | Melder_fixed (my startWindow, my v_fixedPrecision_long ())); |
| 282 | Graphics_setTextAlignment (my graphics.get(), Graphics_RIGHT, Graphics_HALF); |
| 283 | Graphics_text (my graphics.get(), right, 0.5 * (bottom + top) - verticalCorrection, |
| 284 | Melder_fixed (my endWindow, my v_fixedPrecision_long ())); |
| 285 | Graphics_setColour (my graphics.get(), Melder_BLACK); |
| 286 | Graphics_setTextAlignment (my graphics.get(), Graphics_CENTRE, Graphics_HALF); |
| 287 | } break; case 2: { |
| 288 | value = my startWindow - my tmin; |
| 289 | } break; case 3: { |
| 290 | value = my tmax - my endWindow; |
| 291 | } break; case 4: { |
| 292 | value = my marker [1] - my startWindow; |
| 293 | } break; case 5: { |
| 294 | value = my marker [2] - my marker [1]; |
| 295 | } break; case 6: { |
| 296 | value = my marker [3] - my marker [2]; |
| 297 | } break; case 7: { |
| 298 | format = my v_format_selection (); |
| 299 | value = my endSelection - my startSelection; |
| 300 | inverseValue = 1.0 / value; |
| 301 | } |
| 302 | } |
| 303 | char text8 [100]; |
| 304 | snprintf (text8, 100, format, value, inverseValue); |
| 305 | autostring32 text = Melder_8to32_e (text8); |
| 306 | if (Graphics_textWidth (my graphics.get(), text.get()) < right - left) { |
| 307 | Graphics_text (my graphics.get(), 0.5 * (left + right), 0.5 * (bottom + top) - verticalCorrection, text.get()); |
| 308 | } else if (format == my v_format_long()) { |
| 309 | snprintf (text8, 100, my v_format_short(), value); |
| 310 | text = Melder_8to32_e (text8); |
| 311 | if (Graphics_textWidth (my graphics.get(), text.get()) < right - left) |
| 312 | Graphics_text (my graphics.get(), 0.5 * (left + right), 0.5 * (bottom + top) - verticalCorrection, text.get()); |
| 313 | } else { |
| 314 | snprintf (text8, 100, my v_format_long(), value); |
| 315 | text = Melder_8to32_e (text8); |
| 316 | if (Graphics_textWidth (my graphics.get(), text.get()) < right - left) { |
| 317 | Graphics_text (my graphics.get(), 0.5 * (left + right), 0.5 * (bottom + top) - verticalCorrection, text.get()); |
| 318 | } else { |
| 319 | snprintf (text8, 100, my v_format_short(), my endSelection - my startSelection); |
| 320 | text = Melder_8to32_e (text8); |
| 321 | if (Graphics_textWidth (my graphics.get(), text.get()) < right - left) |
| 322 | Graphics_text (my graphics.get(), 0.5 * (left + right), 0.5 * (bottom + top) - verticalCorrection, text.get()); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | my viewTallDataAsWorldByPixelettes (); |
| 329 | |
| 330 | /* |
| 331 | Red marker text. |
| 332 | */ |
| 333 | Graphics_setColour (my graphics.get(), Melder_RED); |
| 334 | if (cursorIsVisible) { |
| 335 | Graphics_setTextAlignment (my graphics.get(), Graphics_CENTRE, Graphics_BOTTOM); |
| 336 | Graphics_text (my graphics.get(), my startSelection, my height_pxlt - (my TOP_MARGIN + my space*0.9) - verticalCorrection * 7, |
| 337 | Melder_fixed (my startSelection, my v_fixedPrecision_long())); |
| 338 | } |
| 339 | if (startIsVisible && selectionIsNonempty) { |
| 340 | Graphics_setTextAlignment (my graphics.get(), Graphics_RIGHT, Graphics_HALF); |
| 341 | Graphics_text (my graphics.get(), my startSelection, my height_pxlt - (my TOP_MARGIN + my space/2) - verticalCorrection, |
| 342 | Melder_fixed (my startSelection, my v_fixedPrecision_long())); |
| 343 | } |
| 344 | if (endIsVisible && selectionIsNonempty) { |
| 345 | Graphics_setTextAlignment (my graphics.get(), Graphics_LEFT, Graphics_HALF); |
| 346 | Graphics_text (my graphics.get(), my endSelection, my height_pxlt - (my TOP_MARGIN + my space/2) - verticalCorrection, |
| 347 | Melder_fixed (my endSelection, my v_fixedPrecision_long())); |
| 348 | } |
| 349 | Graphics_setColour (my graphics.get(), Melder_BLACK); |
| 350 | |
| 351 | /* |
| 352 | Start of inner drawing. |
| 353 | */ |
| 354 | my v_distributeAreas (); |
| 355 | //my viewDataAsWorldByFraction (); |
| 356 | my v_draw (); |
| 357 | my v_drawLegends (); |
| 358 | |
| 359 | /* |
| 360 | Red dotted marker lines. |
| 361 | */ |
| 362 | my viewDataAsWorldByFraction (); |
| 363 | Graphics_setColour (my graphics.get(), Melder_RED); |
| 364 | Graphics_setLineType (my graphics.get(), Graphics_DOTTED); |
| 365 | if (cursorIsVisible) |
| 366 | Graphics_line (my graphics.get(), my startSelection, 0.0, my startSelection, 1.0); |
| 367 | if (startIsVisible) |
| 368 | Graphics_line (my graphics.get(), my startSelection, 0.0, my startSelection, 1.0); |
| 369 | if (endIsVisible) |
| 370 | Graphics_line (my graphics.get(), my endSelection, 0.0, my endSelection, 1.0); |
| 371 | Graphics_setColour (my graphics.get(), Melder_BLACK); |
| 372 | Graphics_setLineType (my graphics.get(), Graphics_DRAWN); |
| 373 | } |
| 374 | |
| 375 | /********** METHODS **********/ |
| 376 | |
| 377 | void structFunctionEditor :: v9_destroy () noexcept { |
| 378 | MelderAudio_stopPlaying (MelderAudio_IMPLICIT); |
| 379 | if (our group) { // undangle |
| 380 | integer i = 1; |
| 381 | while (theGroupMembers [i] != this) { |
| 382 | Melder_assert (i < THE_MAXIMUM_GROUP_SIZE); |
| 383 | i ++; |
| 384 | } |
| 385 | theGroupMembers [i] = nullptr; |
| 386 | theGroupSize --; |
| 387 | } |
| 388 | if (Melder_debug == 55) |
| 389 | Melder_casual (Thing_messageNameAndAddress (this), U" v9_destroy"); |
| 390 | FunctionEditor_Parent :: v9_destroy (); |
| 391 | } |
| 392 | |
| 393 | void structFunctionEditor :: v1_info () { |
| 394 | FunctionEditor_Parent :: v1_info (); |
| 395 | MelderInfo_writeLine (U"Editor start: ", our tmin, U" ", v_format_units_long()); |
| 396 | MelderInfo_writeLine (U"Editor end: ", our tmax, U" ", v_format_units_long()); |
| 397 | MelderInfo_writeLine (U"Window start: ", our startWindow, U" ", v_format_units_long()); |
| 398 | MelderInfo_writeLine (U"Window end: ", our endWindow, U" ", v_format_units_long()); |
| 399 | MelderInfo_writeLine (U"Selection start: ", our startSelection, U" ", v_format_units_long()); |
| 400 | MelderInfo_writeLine (U"Selection end: ", our endSelection, U" ", v_format_units_long()); |
| 401 | MelderInfo_writeLine (U"Arrow scroll step: ", our instancePref_arrowScrollStep(), U" ", v_format_units_long()); |
| 402 | MelderInfo_writeLine (U"Group: ", group ? U"yes" : U"no"); |
| 403 | for (integer iarea = 1; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 404 | FunctionArea area = static_cast <FunctionArea> (our functionAreas [iarea].get()); |
| 405 | if (area) |
| 406 | area -> v1_info (); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | void structFunctionEditor :: v_windowChanged () { |
| 411 | for (integer iarea = 1; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 412 | FunctionArea area = static_cast <FunctionArea> (our functionAreas [iarea].get()); |
| 413 | if (area) |
| 414 | area -> v_windowChanged (); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | |
| 419 | /********** FILE MENU **********/ |
| 420 | |
| 421 | static void gui_drawingarea_cb_resize (FunctionEditor me, GuiDrawingArea_ResizeEvent event) { |
| 422 | if (! my graphics) |
| 423 | return; // could be the case in the very beginning |
| 424 | my updateGeometry (event -> width, event -> height); |
| 425 | FunctionEditor_redraw (me); |
| 426 | /* |
| 427 | Save the current shell size as the user's preference for a new FunctionEditor. |
| 428 | */ |
| 429 | my setClassPref_shellWidth (GuiShell_getShellWidth (my windowForm)); |
| 430 | my setClassPref_shellHeight (GuiShell_getShellHeight (my windowForm)); |
| 431 | } |
| 432 | |
| 433 | static void zoom_fromTo (FunctionEditor me, double from, double to) { |
| 434 | if (from < my tmin + 1e-12) |
| 435 | from = my tmin; |
| 436 | if (to > my tmax - 1e-12) |
| 437 | to = my tmax; |
| 438 | Melder_require (to > from, |
| 439 | U"“to” should be greater than “from”."); |
| 440 | my startWindow = from; |
| 441 | my endWindow = to; |
| 442 | if (my endWindow <= my startWindow) { // floating-point underflow |
| 443 | my startWindow = my tmin; |
| 444 | my endWindow = my tmax; |
| 445 | } |
| 446 | my v_windowChanged (); |
| 447 | FunctionEditor_windowMarksChanged (me, false); |
| 448 | } |
| 449 | static void shift_by (FunctionEditor me, const double shift, const bool selectionChanged) { |
| 450 | const double windowLength = my endWindow - my startWindow; |
| 451 | MelderAudio_stopPlaying (MelderAudio_IMPLICIT); // quickly, before window changes |
| 452 | trace (U"shifting by ", shift); |
| 453 | if (shift < 0.0) { |
| 454 | my startWindow += shift; |
| 455 | if (my startWindow < my tmin + 1e-12) |
| 456 | my startWindow = my tmin; |
| 457 | my endWindow = my startWindow + windowLength; |
| 458 | if (my endWindow > my tmax - 1e-12) |
| 459 | my endWindow = my tmax; |
| 460 | } else { |
| 461 | my endWindow += shift; |
| 462 | if (my endWindow > my tmax - 1e-12) |
| 463 | my endWindow = my tmax; |
| 464 | my startWindow = my endWindow - windowLength; |
| 465 | if (my startWindow < my tmin + 1e-12) |
| 466 | my startWindow = my tmin; |
| 467 | } |
| 468 | my v_windowChanged (); |
| 469 | FunctionEditor_windowMarksChanged (me, selectionChanged); |
| 470 | } |
| 471 | static void zoom_by (FunctionEditor me, double factor) { |
| 472 | const double currentSize = my endWindow - my startWindow; |
| 473 | const double newSize = currentSize * factor; |
| 474 | const double increase = newSize - currentSize; |
| 475 | const double shift = 0.5 * increase; |
| 476 | zoom_fromTo (me, my startWindow - shift, my endWindow + shift); |
| 477 | } |
| 478 | |
| 479 | static void gui_drawingarea_cb_zoom (FunctionEditor me, GuiDrawingArea_ZoomEvent event) { |
| 480 | if (! my graphics) |
| 481 | return; // could be the case in the very beginning |
| 482 | const double enlargement = exp (-0.02 * (event -> delta>0.0?+1:-1) * sqrt (fabs (event -> delta))); // 2 percent per step |
| 483 | zoom_by (me, enlargement); |
| 484 | } |
| 485 | |
| 486 | void structFunctionEditor :: v_prefs_addFields (EditorCommand cmd) { |
| 487 | for (integer iarea = 1; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 488 | FunctionArea area = static_cast <FunctionArea> (our functionAreas [iarea].get()); |
| 489 | if (area) |
| 490 | area -> v_prefs_addFields (cmd); |
| 491 | } |
| 492 | } |
| 493 | void structFunctionEditor :: v_prefs_setValues (EditorCommand cmd) { |
| 494 | for (integer iarea = 1; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 495 | FunctionArea area = static_cast <FunctionArea> (our functionAreas [iarea].get()); |
| 496 | if (area) |
| 497 | area -> v_prefs_setValues (cmd); |
| 498 | } |
| 499 | } |
| 500 | void structFunctionEditor :: v_prefs_getValues (EditorCommand cmd) { |
| 501 | for (integer iarea = 1; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 502 | FunctionArea area = static_cast <FunctionArea> (our functionAreas [iarea].get()); |
| 503 | if (area) |
| 504 | area -> v_prefs_getValues (cmd); |
| 505 | } |
| 506 | } |
| 507 | static void menu_cb_editorSettings (FunctionEditor me, EDITOR_ARGS) { // TODO: make visible |
| 508 | EDITOR_FORM (U"Editor settings", nullptr) |
| 509 | BOOLEAN (showSelectionViewer, Melder_cat (U"Show ", my v_selectionViewerName()), my default_showSelectionViewer()) |
| 510 | my v_prefs_addFields (cmd); |
| 511 | EDITOR_OK |
| 512 | SET_BOOLEAN (showSelectionViewer, my instancePref_showSelectionViewer()) |
| 513 | my v_prefs_setValues (cmd); |
| 514 | EDITOR_DO |
| 515 | const bool oldShowSelectionViewer = my instancePref_showSelectionViewer(); |
| 516 | my setInstancePref_showSelectionViewer (showSelectionViewer); |
| 517 | if (my instancePref_showSelectionViewer() != oldShowSelectionViewer) |
| 518 | my updateGeometry (GuiControl_getWidth (my drawingArea), GuiControl_getHeight (my drawingArea)); |
| 519 | my v_prefs_getValues (cmd); |
| 520 | FunctionEditor_redraw (me); |
| 521 | EDITOR_END |
| 522 | } |
| 523 | |
| 524 | |
| 525 | #pragma mark - FuncEd Time query selection |
| 526 | |
| 527 | static void QUERY_EDITOR_FOR_REAL__getStartOfSelection (FunctionEditor me, EDITOR_ARGS) { |
| 528 | QUERY_EDITOR_FOR_REAL |
| 529 | const double result = my startSelection; |
| 530 | QUERY_EDITOR_FOR_REAL_END (U" ", my v_format_units_long()) |
| 531 | } |
| 532 | static void QUERY_EDITOR_FOR_REAL__getCursor (FunctionEditor me, EDITOR_ARGS) { |
| 533 | QUERY_EDITOR_FOR_REAL |
| 534 | const double result = 0.5 * (my startSelection + my endSelection); |
| 535 | QUERY_EDITOR_FOR_REAL_END (U" ", my v_format_units_long()) |
| 536 | } |
| 537 | static void QUERY_EDITOR_FOR_REAL__getEndOfSelection (FunctionEditor me, EDITOR_ARGS) { |
| 538 | QUERY_EDITOR_FOR_REAL |
| 539 | const double result = my endSelection; |
| 540 | QUERY_EDITOR_FOR_REAL_END (U" ", my v_format_units_long()) |
| 541 | } |
| 542 | static void QUERY_EDITOR_FOR_REAL__getLengthOfSelection (FunctionEditor me, EDITOR_ARGS) { |
| 543 | QUERY_EDITOR_FOR_REAL |
| 544 | const double result = my endSelection - my startSelection; |
| 545 | QUERY_EDITOR_FOR_REAL_END (U" ", my v_format_units_long()) |
| 546 | } |
| 547 | |
| 548 | |
| 549 | #pragma mark - FuncEd Time set selection |
| 550 | |
| 551 | static void menu_cb_select (FunctionEditor me, EDITOR_ARGS) { |
| 552 | EDITOR_FORM (U"Select", nullptr) |
| 553 | REAL (startOfSelection, Melder_cat (U"Start of selection (", my v_format_units_short(), U")"), U"0.0") |
| 554 | REAL (endOfSelection, Melder_cat (U"End of selection (", my v_format_units_short(), U")"), U"1.0") |
| 555 | EDITOR_OK |
| 556 | SET_REAL (startOfSelection, my startSelection) |
| 557 | SET_REAL (endOfSelection, my endSelection) |
| 558 | EDITOR_DO |
| 559 | my startSelection = startOfSelection; |
| 560 | if (my startSelection < my tmin + 1e-12) |
| 561 | my startSelection = my tmin; |
| 562 | my endSelection = endOfSelection; |
| 563 | if (my endSelection > my tmax - 1e-12) |
| 564 | my endSelection = my tmax; |
| 565 | if (my startSelection > my endSelection) |
| 566 | std::swap (my startSelection, my endSelection); // this can invalidate the above logic |
| 567 | Melder_clip (my tmin, & my startSelection, my tmax); |
| 568 | Melder_clip (my tmin, & my endSelection, my tmax); |
| 569 | Melder_assert (isdefined (my startSelection)); // precondition of FunctionEditor_selectionMarksChanged() |
| 570 | FunctionEditor_selectionMarksChanged (me); |
| 571 | EDITOR_END |
| 572 | } |
| 573 | static void menu_cb_widenOrShrinkSelection (FunctionEditor me, EDITOR_ARGS) { |
| 574 | EDITOR_FORM (U"Widen or shrink selection", nullptr) |
| 575 | POSITIVE (newWidthOfSelection, Melder_cat (U"New width of selection (", my v_format_units_short(), U")"), U"0.3") |
| 576 | CHOICE_ENUM (kGraphics_horizontalAlignment, alignmentWithCurrentSelection, U"Alignment with current selection", kGraphics_horizontalAlignment::CENTRE) |
| 577 | EDITOR_OK |
| 578 | EDITOR_DO |
| 579 | const double currentWidthOfSelection = my endSelection - my startSelection; |
| 580 | const double degreeOfWidening = newWidthOfSelection - currentWidthOfSelection; |
| 581 | double newStartOfSelection; |
| 582 | switch (alignmentWithCurrentSelection) { |
| 583 | case kGraphics_horizontalAlignment::LEFT: { |
| 584 | newStartOfSelection = my startSelection; |
| 585 | } break; case kGraphics_horizontalAlignment::CENTRE: { |
| 586 | newStartOfSelection = my startSelection - 0.5 * degreeOfWidening; |
| 587 | } break; case kGraphics_horizontalAlignment::RIGHT: { |
| 588 | newStartOfSelection = my startSelection - degreeOfWidening; |
| 589 | } break; case kGraphics_horizontalAlignment::UNDEFINED: { |
| 590 | Melder_throw (U"Undefined alignment."); |
| 591 | } |
| 592 | } |
| 593 | const double newEndOfSelection = newStartOfSelection + newWidthOfSelection; |
| 594 | Melder_require (newStartOfSelection >= my tmin, |
| 595 | U"Widening the selection to ", newWidthOfSelection, U" ", my v_format_units_long(), |
| 596 | U" would move the start of the selection to ", newStartOfSelection, U" ", my v_format_units_long(), |
| 597 | U", which lies before the start of the editor’s ", my v_domainName(), |
| 598 | U" domain, which is at ", my tmin, U" ", my v_format_units_long(), U"." |
| 599 | ); |
| 600 | Melder_require (newEndOfSelection <= my tmax, |
| 601 | U"Widening the selection to ", newWidthOfSelection, U" ", my v_format_units_long(), |
| 602 | U" would move the end of the selection to ", newEndOfSelection, U" ", my v_format_units_long(), |
| 603 | U", which lies past the end of the editor’s ", my v_domainName(), |
| 604 | U" domain, which is at ", my tmax, U" ", my v_format_units_long(), U"." |
| 605 | ); |
| 606 | my startSelection = newStartOfSelection; |
| 607 | my endSelection = newEndOfSelection; |
| 608 | Melder_assert (isdefined (my startSelection)); // precondition of FunctionEditor_selectionMarksChanged() |
| 609 | FunctionEditor_selectionMarksChanged (me); |
| 610 | EDITOR_END |
| 611 | } |
| 612 | static void menu_cb_moveCursorToStartOfSelection (FunctionEditor me, EDITOR_ARGS) { |
| 613 | VOID_EDITOR |
| 614 | my endSelection = my startSelection; |
| 615 | Melder_assert (isdefined (my startSelection)); // precondition of FunctionEditor_selectionMarksChanged() |
| 616 | FunctionEditor_selectionMarksChanged (me); |
| 617 | VOID_EDITOR_END |
| 618 | } |
| 619 | static void menu_cb_moveCursorToEndOfSelection (FunctionEditor me, EDITOR_ARGS) { |
| 620 | VOID_EDITOR |
| 621 | my startSelection = my endSelection; |
| 622 | Melder_assert (isdefined (my startSelection)); // precondition of FunctionEditor_selectionMarksChanged() |
| 623 | FunctionEditor_selectionMarksChanged (me); |
| 624 | VOID_EDITOR_END |
| 625 | } |
| 626 | static void menu_cb_moveCursorTo (FunctionEditor me, EDITOR_ARGS) { |
| 627 | EDITOR_FORM (U"Move cursor to", nullptr) |
| 628 | REAL (position, Melder_cat (U"Position (", my v_format_units_short(), U")"), U"0.0") |
| 629 | EDITOR_OK |
| 630 | SET_REAL (position, 0.5 * (my startSelection + my endSelection)) |
| 631 | EDITOR_DO |
| 632 | if (position < my tmin + 1e-12) |
| 633 | position = my tmin; |
| 634 | if (position > my tmax - 1e-12) |
| 635 | position = my tmax; |
| 636 | my startSelection = my endSelection = position; |
| 637 | Melder_assert (isdefined (my startSelection)); // precondition of FunctionEditor_selectionMarksChanged() |
| 638 | FunctionEditor_selectionMarksChanged (me); |
| 639 | EDITOR_END |
| 640 | } |
| 641 | static void menu_cb_moveCursorBy (FunctionEditor me, EDITOR_ARGS) { |
| 642 | EDITOR_FORM (U"Move cursor by", nullptr) |
| 643 | REAL (distance, Melder_cat (U"Distance (", my v_format_units_short(), U")"), U"0.05") |
| 644 | EDITOR_OK |
| 645 | EDITOR_DO |
| 646 | Melder_assert (my tmax >= my tmin); |
| 647 | const double position = Melder_clipped (my tmin, 0.5 * (my startSelection + my endSelection) + distance, my tmax); |
| 648 | my startSelection = my endSelection = position; |
| 649 | Melder_assert (isdefined (my startSelection)); // precondition of FunctionEditor_selectionMarksChanged() |
| 650 | FunctionEditor_selectionMarksChanged (me); |
| 651 | EDITOR_END |
| 652 | } |
| 653 | static void menu_cb_moveStartOfSelectionBy (FunctionEditor me, EDITOR_ARGS) { |
| 654 | EDITOR_FORM (U"Move start of selection by", nullptr) |
| 655 | REAL (distance, Melder_cat (U"Distance (", my v_format_units_short(), U")"), U"0.05") |
| 656 | EDITOR_OK |
| 657 | EDITOR_DO |
| 658 | Melder_assert (my tmax >= my tmin); |
| 659 | my startSelection = Melder_clipped (my tmin, my startSelection + distance, my tmax); |
| 660 | Melder_sort (& my startSelection, & my endSelection); |
| 661 | Melder_assert (isdefined (my startSelection)); // precondition of FunctionEditor_selectionMarksChanged() |
| 662 | FunctionEditor_selectionMarksChanged (me); |
| 663 | EDITOR_END |
| 664 | } |
| 665 | static void menu_cb_moveEndOfSelectionBy (FunctionEditor me, EDITOR_ARGS) { |
| 666 | EDITOR_FORM (U"Move end of selection by", nullptr) |
| 667 | REAL (distance, Melder_cat (U"Distance (", my v_format_units_short(), U")"), U"0.05") |
| 668 | EDITOR_OK |
| 669 | EDITOR_DO |
| 670 | Melder_assert (my tmax >= my tmin); |
| 671 | my endSelection = Melder_clipped (my tmin, my endSelection + distance, my tmax); |
| 672 | Melder_sort (& my startSelection, & my endSelection); |
| 673 | Melder_assert (isdefined (my startSelection)); // precondition of FunctionEditor_selectionMarksChanged() |
| 674 | FunctionEditor_selectionMarksChanged (me); |
| 675 | EDITOR_END |
| 676 | } |
| 677 | void FunctionEditor_scrollToNewSelection (FunctionEditor me, double t) { |
| 678 | if (t <= my startWindow) { |
| 679 | Melder_assert (isdefined (my startSelection)); // precondition of shift_by() |
| 680 | shift_by (me, t - my startWindow - 0.618 * (my endWindow - my startWindow), true); |
| 681 | } else if (t >= my endWindow) { |
| 682 | Melder_assert (isdefined (my startSelection)); // precondition of shift_by() |
| 683 | shift_by (me, t - my endWindow + 0.618 * (my endWindow - my startWindow), true); |
| 684 | } else { |
| 685 | FunctionEditor_selectionMarksChanged (me); |
| 686 | } |
| 687 | } |
| 688 | static void menu_cb_selectionStepSettings (FunctionEditor me, EDITOR_ARGS) { |
| 689 | EDITOR_FORM (U"Selection step settings", nullptr) |
| 690 | POSITIVE (arrowScrollStep, Melder_cat (U"Arrow scroll step (", my v_format_units_short(), U")"), my default_arrowScrollStep()) |
| 691 | EDITOR_OK |
| 692 | SET_REAL (arrowScrollStep, my instancePref_arrowScrollStep()) |
| 693 | EDITOR_DO |
| 694 | my setInstancePref_arrowScrollStep (arrowScrollStep); |
| 695 | EDITOR_END |
| 696 | } |
| 697 | static void menu_cb_selectEarlier (FunctionEditor me, EDITOR_ARGS) { |
| 698 | VOID_EDITOR |
| 699 | my startSelection -= my instancePref_arrowScrollStep(); |
| 700 | if (my startSelection < my tmin + 1e-12) |
| 701 | my startSelection = my tmin; |
| 702 | my endSelection -= my instancePref_arrowScrollStep(); |
| 703 | if (my endSelection < my tmin + 1e-12) |
| 704 | my endSelection = my tmin; |
| 705 | Melder_assert (isdefined (my startSelection)); // precondition of FunctionEditor_scrollToNewSelection() |
| 706 | FunctionEditor_scrollToNewSelection (me, 0.5 * (my startSelection + my endSelection)); |
| 707 | VOID_EDITOR_END |
| 708 | } |
| 709 | static void menu_cb_selectLater (FunctionEditor me, EDITOR_ARGS) { |
| 710 | VOID_EDITOR |
| 711 | my startSelection += my instancePref_arrowScrollStep(); |
| 712 | if (my startSelection > my tmax - 1e-12) |
| 713 | my startSelection = my tmax; |
| 714 | my endSelection += my instancePref_arrowScrollStep(); |
| 715 | if (my endSelection > my tmax - 1e-12) |
| 716 | my endSelection = my tmax; |
| 717 | Melder_assert (isdefined (my startSelection)); // precondition of FunctionEditor_scrollToNewSelection() |
| 718 | FunctionEditor_scrollToNewSelection (me, 0.5 * (my startSelection + my endSelection)); |
| 719 | VOID_EDITOR_END |
| 720 | } |
| 721 | static void menu_cb_moveStartOfSelectionLeft (FunctionEditor me, EDITOR_ARGS) { |
| 722 | VOID_EDITOR |
| 723 | my startSelection -= my instancePref_arrowScrollStep(); |
| 724 | if (my startSelection < my tmin + 1e-12) |
| 725 | my startSelection = my tmin; |
| 726 | Melder_assert (isdefined (my startSelection)); // precondition of FunctionEditor_scrollToNewSelection() |
| 727 | FunctionEditor_scrollToNewSelection (me, 0.5 * (my startSelection + my endSelection)); |
| 728 | VOID_EDITOR_END |
| 729 | } |
| 730 | static void menu_cb_moveStartOfSelectionRight (FunctionEditor me, EDITOR_ARGS) { |
| 731 | VOID_EDITOR |
| 732 | my startSelection += my instancePref_arrowScrollStep(); |
| 733 | if (my startSelection > my tmax - 1e-12) |
| 734 | my startSelection = my tmax; |
| 735 | Melder_sort (& my startSelection, & my endSelection); |
| 736 | Melder_assert (isdefined (my startSelection)); // precondition of FunctionEditor_scrollToNewSelection() |
| 737 | FunctionEditor_scrollToNewSelection (me, 0.5 * (my startSelection + my endSelection)); |
| 738 | VOID_EDITOR_END |
| 739 | } |
| 740 | static void menu_cb_moveEndOfSelectionLeft (FunctionEditor me, EDITOR_ARGS) { |
| 741 | VOID_EDITOR |
| 742 | my endSelection -= my instancePref_arrowScrollStep(); |
| 743 | if (my endSelection < my tmin + 1e-12) |
| 744 | my endSelection = my tmin; |
| 745 | Melder_sort (& my startSelection, & my endSelection); |
| 746 | Melder_assert (isdefined (my startSelection)); // precondition of FunctionEditor_scrollToNewSelection() |
| 747 | FunctionEditor_scrollToNewSelection (me, 0.5 * (my startSelection + my endSelection)); |
| 748 | VOID_EDITOR_END |
| 749 | } |
| 750 | static void menu_cb_moveEndOfSelectionRight (FunctionEditor me, EDITOR_ARGS) { |
| 751 | VOID_EDITOR |
| 752 | my endSelection += my instancePref_arrowScrollStep(); |
| 753 | if (my endSelection > my tmax - 1e-12) |
| 754 | my endSelection = my tmax; |
| 755 | Melder_assert (isdefined (my startSelection)); // precondition of FunctionEditor_scrollToNewSelection() |
| 756 | FunctionEditor_scrollToNewSelection (me, 0.5 * (my startSelection + my endSelection)); |
| 757 | VOID_EDITOR_END |
| 758 | } |
| 759 | |
| 760 | |
| 761 | #pragma mark - FuncEd Time query visible part |
| 762 | |
| 763 | static void QUERY_EDITOR_FOR_REAL__getStartOfVisiblePart (FunctionEditor me, EDITOR_ARGS) { |
| 764 | QUERY_EDITOR_FOR_REAL |
| 765 | const double result = my startWindow; |
| 766 | QUERY_EDITOR_FOR_REAL_END (U" ", my v_format_units_long()) |
| 767 | } |
| 768 | static void QUERY_EDITOR_FOR_REAL__getEndOfVisiblePart (FunctionEditor me, EDITOR_ARGS) { |
| 769 | QUERY_EDITOR_FOR_REAL |
| 770 | const double result = my endWindow; |
| 771 | QUERY_EDITOR_FOR_REAL_END (U" ", my v_format_units_long()) |
| 772 | } |
| 773 | static void QUERY_EDITOR_FOR_REAL__getLengthOfVisiblePart (FunctionEditor me, EDITOR_ARGS) { |
| 774 | QUERY_EDITOR_FOR_REAL |
| 775 | const double result = my endWindow - my startWindow; |
| 776 | QUERY_EDITOR_FOR_REAL_END (U" ", my v_format_units_long()) |
| 777 | } |
| 778 | |
| 779 | |
| 780 | #pragma mark - FuncEd Time set visible part |
| 781 | |
| 782 | static void menu_cb_zoomAndScrollSettings (FunctionEditor me, EDITOR_ARGS) { |
| 783 | EDITOR_FORM (U"Zoom and scroll settings", nullptr) |
| 784 | BOOLEAN (synchronizeZoomAndScroll, U"Synchronize zoom and scroll", my default_synchronizedZoomAndScroll()) |
| 785 | EDITOR_OK |
| 786 | SET_BOOLEAN (synchronizeZoomAndScroll, my classPref_synchronizedZoomAndScroll()) |
| 787 | EDITOR_DO |
| 788 | const bool oldSynchronizedZoomAndScroll = my classPref_synchronizedZoomAndScroll(); |
| 789 | my setClassPref_synchronizedZoomAndScroll (synchronizeZoomAndScroll); |
| 790 | if (! oldSynchronizedZoomAndScroll && my classPref_synchronizedZoomAndScroll()) |
| 791 | updateGroup (me, true, false); |
| 792 | FunctionEditor_redraw (me); |
| 793 | EDITOR_END |
| 794 | } |
| 795 | static void menu_cb_zoom (FunctionEditor me, EDITOR_ARGS) { |
| 796 | EDITOR_FORM (U"Zoom", nullptr) |
| 797 | REAL (from, Melder_cat (U"From (", my v_format_units_short(), U")"), U"0.0") |
| 798 | REAL (to, Melder_cat (U"To (", my v_format_units_short(), U")"), U"1.0") |
| 799 | EDITOR_OK |
| 800 | SET_REAL (from, my startWindow) |
| 801 | SET_REAL (to, my endWindow) |
| 802 | EDITOR_DO |
| 803 | Melder_require (to > from, |
| 804 | U"“to” should be greater than “from”."); |
| 805 | zoom_fromTo (me, from, to); |
| 806 | EDITOR_END |
| 807 | } |
| 808 | static void do_showAll (FunctionEditor me) { |
| 809 | zoom_fromTo (me, my tmin, my tmax); |
| 810 | } |
| 811 | static void gui_button_cb_showAll (FunctionEditor me, GuiButtonEvent /* event */) { |
| 812 | do_showAll (me); |
| 813 | } |
| 814 | static void do_zoomIn (FunctionEditor me) { |
| 815 | zoom_by (me, 0.5); |
| 816 | } |
| 817 | static void gui_button_cb_zoomIn (FunctionEditor me, GuiButtonEvent /* event */) { |
| 818 | do_zoomIn (me); |
| 819 | } |
| 820 | static void do_zoomOut (FunctionEditor me) { |
| 821 | //MelderAudio_stopPlaying (MelderAudio_IMPLICIT); // quickly, before window changes; ppgb 2022-06-25: why was this here? |
| 822 | zoom_by (me, 2.0); |
| 823 | } |
| 824 | static void gui_button_cb_zoomOut (FunctionEditor me, GuiButtonEvent /*event*/) { |
| 825 | do_zoomOut (me); |
| 826 | } |
| 827 | static void do_zoomToSelection (FunctionEditor me) { |
| 828 | if (my endSelection > my startSelection) { |
| 829 | my startZoomHistory = my startWindow; // remember for Zoom Back |
| 830 | my endZoomHistory = my endWindow; // remember for Zoom Back |
| 831 | zoom_fromTo (me, my startSelection, my endSelection); |
| 832 | } |
| 833 | } |
| 834 | static void gui_button_cb_zoomToSelection (FunctionEditor me, GuiButtonEvent /* event */) { |
| 835 | do_zoomToSelection (me); |
| 836 | } |
| 837 | static void do_zoomBack (FunctionEditor me) { |
| 838 | Melder_clip (my tmin, & my startZoomHistory, my tmax); |
| 839 | Melder_clip (my tmin, & my endZoomHistory, my tmax); |
| 840 | if (my endZoomHistory > my startZoomHistory) |
| 841 | zoom_fromTo (me, my startZoomHistory, my endZoomHistory); |
| 842 | } |
| 843 | static void gui_button_cb_zoomBack (FunctionEditor me, GuiButtonEvent /* event */) { |
| 844 | do_zoomBack (me); |
| 845 | } |
| 846 | static void menu_cb_showAll (FunctionEditor me, EDITOR_ARGS) { |
| 847 | VOID_EDITOR |
| 848 | do_showAll (me); |
| 849 | VOID_EDITOR_END |
| 850 | } |
| 851 | static void menu_cb_zoomIn (FunctionEditor me, EDITOR_ARGS) { |
| 852 | VOID_EDITOR |
| 853 | do_zoomIn (me); |
| 854 | VOID_EDITOR_END |
| 855 | } |
| 856 | static void menu_cb_zoomOut (FunctionEditor me, EDITOR_ARGS) { |
| 857 | VOID_EDITOR |
| 858 | do_zoomOut (me); |
| 859 | VOID_EDITOR_END |
| 860 | } |
| 861 | static void menu_cb_zoomToSelection (FunctionEditor me, EDITOR_ARGS) { |
| 862 | VOID_EDITOR |
| 863 | do_zoomToSelection (me); |
| 864 | VOID_EDITOR_END |
| 865 | } |
| 866 | static void menu_cb_zoomBack (FunctionEditor me, EDITOR_ARGS) { |
| 867 | VOID_EDITOR |
| 868 | do_zoomBack (me); |
| 869 | VOID_EDITOR_END |
| 870 | } |
| 871 | static void menu_cb_pageUp (FunctionEditor me, EDITOR_ARGS) { |
| 872 | VOID_EDITOR |
| 873 | Melder_assert (isdefined (my startSelection)); // precondition of shift_by() |
| 874 | shift_by (me, -RELATIVE_PAGE_INCREMENT * (my endWindow - my startWindow), false); |
| 875 | VOID_EDITOR_END |
| 876 | } |
| 877 | static void menu_cb_pageDown (FunctionEditor me, EDITOR_ARGS) { |
| 878 | VOID_EDITOR |
| 879 | Melder_assert (isdefined (my startSelection)); // precondition of shift_by() |
| 880 | shift_by (me, +RELATIVE_PAGE_INCREMENT * (my endWindow - my startWindow), false); |
| 881 | VOID_EDITOR_END |
| 882 | } |
| 883 | |
| 884 | |
| 885 | #pragma mark - FunctionEditor Play menu |
| 886 | |
| 887 | static void PLAY_DATA__play (FunctionEditor me, EDITOR_ARGS) { |
| 888 | EDITOR_FORM (U"Play", nullptr) |
| 889 | REAL (from, Melder_cat (U"From (", my v_format_units_short(), U")"), U"0.0") |
| 890 | REAL (to, Melder_cat (U"To (", my v_format_units_short(), U")"), U"1.0") |
| 891 | EDITOR_OK |
| 892 | SET_REAL (from, my startWindow) |
| 893 | SET_REAL (to, my endWindow) |
| 894 | EDITOR_DO |
| 895 | MelderAudio_stopPlaying (MelderAudio_IMPLICIT); |
| 896 | my v_play (from, to); |
| 897 | EDITOR_END |
| 898 | } |
| 899 | static void PLAY_DATA__playOrStop (FunctionEditor me, EDITOR_ARGS) { |
| 900 | PLAY_DATA |
| 901 | if (MelderAudio_isPlaying) { |
| 902 | MelderAudio_stopPlaying (MelderAudio_EXPLICIT); |
| 903 | } else if (my startSelection < my endSelection) { |
| 904 | my v_play (my startSelection, my endSelection); |
| 905 | } else { |
| 906 | if (my startSelection == my endSelection && my startSelection > my startWindow && my startSelection < my endWindow) |
| 907 | my v_play (my startSelection, my endWindow); |
| 908 | else |
| 909 | my v_play (my startWindow, my endWindow); |
| 910 | } |
| 911 | PLAY_DATA_END |
| 912 | } |
| 913 | static void PLAY_DATA__playWindow (FunctionEditor me, EDITOR_ARGS) { |
| 914 | PLAY_DATA |
| 915 | MelderAudio_stopPlaying (MelderAudio_IMPLICIT); |
| 916 | my v_play (my startWindow, my endWindow); |
| 917 | PLAY_DATA_END |
| 918 | } |
| 919 | static void PLAY_DATA__interruptPlaying (FunctionEditor me, EDITOR_ARGS) { |
| 920 | PLAY_DATA |
| 921 | MelderAudio_stopPlaying (MelderAudio_IMPLICIT); |
| 922 | PLAY_DATA_END |
| 923 | } |
| 924 | |
| 925 | void structFunctionEditor :: v_createMenuItems_play (EditorMenu menu) { |
| 926 | EditorMenu_addCommand (menu, U"Audio:", 0, nullptr); |
| 927 | EditorMenu_addCommand (menu, U"Play...", |
| 928 | 1, PLAY_DATA__play); |
| 929 | EditorMenu_addCommand (menu, U"Play or stop", |
| 930 | GuiMenu_DEPTH_1 | GuiMenu_TAB, PLAY_DATA__playOrStop); |
| 931 | EditorMenu_addCommand (menu, U"Play window", |
| 932 | GuiMenu_DEPTH_1 | GuiMenu_SHIFT | GuiMenu_TAB, PLAY_DATA__playWindow); |
| 933 | EditorMenu_addCommand (menu, U"Interrupt playing", |
| 934 | GuiMenu_DEPTH_1 | GuiMenu_ESCAPE, PLAY_DATA__interruptPlaying); |
| 935 | for (integer iarea = 1; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 936 | FunctionArea area = static_cast <FunctionArea> (our functionAreas [iarea].get()); |
| 937 | if (area) |
| 938 | area -> v_createMenuItems_play (menu); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | |
| 943 | #pragma mark - FunctionEditor GUI callbacks |
| 944 | |
| 945 | static void gui_cb_scroll (FunctionEditor me, GuiScrollBarEvent event) { |
| 946 | if (! my graphics) |
| 947 | return; // ignore events during creation |
| 948 | const double value = GuiScrollBar_getValue (event -> scrollBar); |
| 949 | const double shift = my tmin + (value - 1) * (my tmax - my tmin) / maximumScrollBarValue - my startWindow; |
| 950 | const bool shifted = ( shift != 0.0 ); |
| 951 | const double oldSliderSize = (my endWindow - my startWindow) / (my tmax - my tmin) * maximumScrollBarValue - 1.0; |
| 952 | const double newSliderSize = GuiScrollBar_getSliderSize (event -> scrollBar); |
| 953 | bool zoomed = ( newSliderSize != oldSliderSize ); |
| 954 | #if ! cocoa |
| 955 | zoomed = false; |
| 956 | #endif |
| 957 | if (shifted) { |
| 958 | my startWindow += shift; |
| 959 | if (my startWindow < my tmin + 1e-12) |
| 960 | my startWindow = my tmin; |
| 961 | my endWindow += shift; |
| 962 | if (my endWindow > my tmax - 1e-12) |
| 963 | my endWindow = my tmax; |
| 964 | } |
| 965 | if (zoomed) { |
| 966 | const double zoom = (newSliderSize + 1) * (my tmax - my tmin) / maximumScrollBarValue; |
| 967 | my endWindow = my startWindow + zoom; |
| 968 | if (my endWindow > my tmax - 1e-12) |
| 969 | my endWindow = my tmax; |
| 970 | } |
| 971 | if (shifted || zoomed) { |
| 972 | my v_windowChanged (); |
| 973 | Melder_assert (isdefined (my startSelection)); // precondition of v_updateText() |
| 974 | my v_updateText (); // BUG: this could be superfluous, because the selection hasn't changed? |
| 975 | //updateScrollBar (me); // commented out, becaus it would be circular |
| 976 | FunctionEditor_redraw (me); |
| 977 | if (! my group || ! my classPref_synchronizedZoomAndScroll()) |
| 978 | return; |
| 979 | for (integer i = 1; i <= THE_MAXIMUM_GROUP_SIZE; i ++) { |
| 980 | if (theGroupMembers [i] && theGroupMembers [i] != me) { |
| 981 | theGroupMembers [i] -> startWindow = my startWindow; |
| 982 | theGroupMembers [i] -> endWindow = my endWindow; |
| 983 | theGroupMembers [i] -> v_windowChanged (); |
| 984 | Melder_assert (isdefined (theGroupMembers [i] -> startSelection)); // precondition of FunctionEditor_updateText() |
| 985 | FunctionEditor_updateText (theGroupMembers [i]); |
| 986 | updateScrollBar (theGroupMembers [i]); |
| 987 | FunctionEditor_redraw (theGroupMembers [i]); |
| 988 | } |
| 989 | } |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | static integer findEmptySpotInGroup () { |
| 994 | integer emptySpot = 1; |
| 995 | while (theGroupMembers [emptySpot]) |
| 996 | emptySpot ++; |
| 997 | return emptySpot; |
| 998 | } |
| 999 | |
| 1000 | static integer findMeInGroup (FunctionEditor me) { |
| 1001 | integer positionInGroup = 1; |
| 1002 | while (theGroupMembers [positionInGroup] != me) |
| 1003 | positionInGroup ++; |
| 1004 | return positionInGroup; |
| 1005 | } |
| 1006 | |
| 1007 | static integer findOtherGroupMember (FunctionEditor me) { |
| 1008 | integer otherGroupMember = 1; |
| 1009 | while (! theGroupMembers [otherGroupMember] || theGroupMembers [otherGroupMember] == me) |
| 1010 | otherGroupMember ++; |
| 1011 | return otherGroupMember; |
| 1012 | } |
| 1013 | |
| 1014 | static void gui_checkbutton_cb_group (FunctionEditor me, GuiCheckButtonEvent /* event */) { |
| 1015 | my group = ! my group; // toggle |
| 1016 | if (my group) { |
| 1017 | const integer emptySpot = findEmptySpotInGroup (); |
| 1018 | theGroupMembers [emptySpot] = me; |
| 1019 | if (++ theGroupSize == 1) { |
| 1020 | FunctionEditor_redraw (me); |
| 1021 | return; |
| 1022 | } |
| 1023 | const integer otherGroupMember = findOtherGroupMember (me); |
| 1024 | const FunctionEditor she = theGroupMembers [otherGroupMember]; |
| 1025 | /* |
| 1026 | Adapt my domain. |
| 1027 | */ |
| 1028 | Melder_clipRight (& my tmin, her tmin); |
| 1029 | Melder_clipLeft (her tmax, & my tmax); |
| 1030 | /* |
| 1031 | Adapt my window. |
| 1032 | */ |
| 1033 | if (my classPref_synchronizedZoomAndScroll()) { |
| 1034 | my startWindow = her startWindow; |
| 1035 | my endWindow = her endWindow; |
| 1036 | my v_windowChanged (); |
| 1037 | } |
| 1038 | /* |
| 1039 | Adapt my selection. |
| 1040 | Or should I impose my selection on the others? Votes? |
| 1041 | */ |
| 1042 | my startSelection = her startSelection; |
| 1043 | my endSelection = her endSelection; |
| 1044 | /* |
| 1045 | Update me. |
| 1046 | */ |
| 1047 | Melder_assert (isdefined (my startSelection)); // precondition of v_updateText() |
| 1048 | my v_updateText (); |
| 1049 | updateScrollBar (me); |
| 1050 | FunctionEditor_redraw (me); |
| 1051 | /* |
| 1052 | Adapt all the other windows. |
| 1053 | */ |
| 1054 | if (my tmin < her tmin || my tmax > her tmax) { |
| 1055 | for (integer imember = 1; imember <= THE_MAXIMUM_GROUP_SIZE; imember ++) { |
| 1056 | FunctionEditor you = theGroupMembers [imember]; |
| 1057 | if (you && you != me) { |
| 1058 | /* |
| 1059 | Adapt your domain. |
| 1060 | */ |
| 1061 | Melder_clipRight (& your tmin, my tmin); |
| 1062 | Melder_clipLeft (my tmax, & your tmax); |
| 1063 | /* |
| 1064 | Adapt your window. |
| 1065 | */ |
| 1066 | if (my classPref_synchronizedZoomAndScroll()) { |
| 1067 | your startWindow = my startWindow; |
| 1068 | your endWindow = my endWindow; |
| 1069 | your v_windowChanged (); |
| 1070 | } |
| 1071 | /* |
| 1072 | Adapt your selection. |
| 1073 | Should already have been set equal earlier, but why not |
| 1074 | (to keep it compatible with the imposure suggested above). |
| 1075 | */ |
| 1076 | your startSelection = my startSelection; // probably void |
| 1077 | your endSelection = my endSelection; // probably void |
| 1078 | /* |
| 1079 | Update you. |
| 1080 | */ |
| 1081 | Melder_assert (isdefined (your startSelection)); // precondition of FunctionEditor_updateText() |
| 1082 | FunctionEditor_updateText (you); |
| 1083 | updateScrollBar (you); |
| 1084 | FunctionEditor_redraw (you); |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | } else { |
| 1089 | const integer myLocationInGroup = findMeInGroup (me); |
| 1090 | theGroupMembers [myLocationInGroup] = nullptr; |
| 1091 | theGroupSize --; |
| 1092 | Melder_assert (isdefined (my startSelection)); // precondition of v_updateText() |
| 1093 | my v_updateText (); |
| 1094 | FunctionEditor_redraw (me); // for setting buttons in draw method |
| 1095 | updateGroup (me, true, true); |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | static void HELP__intro (FunctionEditor /* me */, EDITOR_ARGS) { |
| 1100 | HELP (U"Intro") |
| 1101 | } |
| 1102 | |
| 1103 | void structFunctionEditor :: v_updateText () { |
| 1104 | for (integer iarea = 1; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 1105 | FunctionArea area = static_cast <FunctionArea> (our functionAreas [iarea].get()); |
| 1106 | if (area) |
| 1107 | area -> v_updateText (); |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | void structFunctionEditor :: v_createMenuItems_prefs (EditorMenu menu) { |
| 1112 | FunctionEditor_Parent :: v_createMenuItems_prefs (menu); |
| 1113 | for (integer iarea = 1; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 1114 | FunctionArea area = static_cast <FunctionArea> (our functionAreas [iarea].get()); |
| 1115 | if (area) |
| 1116 | area -> v_createMenuItems_prefs (menu); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | void structFunctionEditor :: v_createMenuItems_save (EditorMenu menu) { |
| 1121 | FunctionEditor_Parent :: v_createMenuItems_save (menu); |
| 1122 | for (integer iarea = 1; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 1123 | FunctionArea area = static_cast <FunctionArea> (our functionAreas [iarea].get()); |
| 1124 | if (area) |
| 1125 | area -> v_createMenuItems_save (menu); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | void structFunctionEditor :: v_createMenuItems_edit (EditorMenu menu) { |
| 1130 | FunctionEditor_Parent :: v_createMenuItems_edit (menu); |
| 1131 | for (integer iarea = 1; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 1132 | FunctionArea area = static_cast <FunctionArea> (our functionAreas [iarea].get()); |
| 1133 | if (area) |
| 1134 | area -> v_createMenuItems_edit (menu); |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | void structFunctionEditor :: v_createMenus () { |
| 1139 | FunctionEditor_Parent :: v_createMenus (); |
| 1140 | |
| 1141 | EditorMenu domainMenu = Editor_addMenu (this, v_format_domain (), 0); // Time or Frequency |
| 1142 | |
| 1143 | EditorMenu_addCommand (domainMenu, U"Query visible part:", 0, nullptr); |
| 1144 | EditorMenu_addCommand (domainMenu, U"Get start of visible part", 1, QUERY_EDITOR_FOR_REAL__getStartOfVisiblePart); |
| 1145 | EditorMenu_addCommand (domainMenu, U"Get end of visible part", 1, QUERY_EDITOR_FOR_REAL__getEndOfVisiblePart); |
| 1146 | EditorMenu_addCommand (domainMenu, U"Get length of visible part", 1, QUERY_EDITOR_FOR_REAL__getLengthOfVisiblePart); |
| 1147 | |
| 1148 | EditorMenu_addCommand (domainMenu, U"- Set visible part:", 0, nullptr); |
| 1149 | EditorMenu_addCommand (domainMenu, U"Zoom and scroll settings...", 1, menu_cb_zoomAndScrollSettings); |
| 1150 | EditorMenu_addCommand (domainMenu, U"Zoom...", 1, menu_cb_zoom); |
| 1151 | EditorMenu_addCommand (domainMenu, U"Show all", 'A' | GuiMenu_DEPTH_1, menu_cb_showAll); |
| 1152 | EditorMenu_addCommand (domainMenu, U"Zoom in", 'I' | GuiMenu_DEPTH_1, menu_cb_zoomIn); |
| 1153 | EditorMenu_addCommand (domainMenu, U"Zoom out", 'O' | GuiMenu_DEPTH_1, menu_cb_zoomOut); |
| 1154 | EditorMenu_addCommand (domainMenu, U"Zoom to selection", 'N' | GuiMenu_DEPTH_1, menu_cb_zoomToSelection); |
| 1155 | EditorMenu_addCommand (domainMenu, U"Zoom back", 'B' | GuiMenu_DEPTH_1, menu_cb_zoomBack); |
| 1156 | EditorMenu_addCommand (domainMenu, U"Scroll page back", GuiMenu_PAGE_UP | GuiMenu_DEPTH_1, menu_cb_pageUp); |
| 1157 | EditorMenu_addCommand (domainMenu, U"Scroll page forward", GuiMenu_PAGE_DOWN | GuiMenu_DEPTH_1, menu_cb_pageDown); |
| 1158 | |
| 1159 | EditorMenu_addCommand (domainMenu, U"- Query selection:", 0, nullptr); |
| 1160 | EditorMenu_addCommand (domainMenu, U"Get start of selection || Get begin of selection", |
| 1161 | 1, QUERY_EDITOR_FOR_REAL__getStartOfSelection); |
| 1162 | EditorMenu_addCommand (domainMenu, U"Get cursor", |
| 1163 | GuiMenu_F6 | GuiMenu_DEPTH_1, QUERY_EDITOR_FOR_REAL__getCursor); |
| 1164 | EditorMenu_addCommand (domainMenu, U"Get end of selection", |
| 1165 | 1, QUERY_EDITOR_FOR_REAL__getEndOfSelection); |
| 1166 | EditorMenu_addCommand (domainMenu, U"Get length of selection || Get selection length", |
| 1167 | 1, QUERY_EDITOR_FOR_REAL__getLengthOfSelection); |
| 1168 | |
| 1169 | EditorMenu_addCommand (domainMenu, U"- Set selection:", 0, nullptr); |
| 1170 | EditorMenu_addCommand (domainMenu, U"Select...", |
| 1171 | 1, menu_cb_select); |
| 1172 | EditorMenu_addCommand (domainMenu, U"Widen or shrink selection...", |
| 1173 | 1, menu_cb_widenOrShrinkSelection); |
| 1174 | EditorMenu_addCommand (domainMenu, U"Move cursor to start of selection || Move cursor to begin of selection", |
| 1175 | 1, menu_cb_moveCursorToStartOfSelection); |
| 1176 | EditorMenu_addCommand (domainMenu, U"Move cursor to end of selection", |
| 1177 | 1, menu_cb_moveCursorToEndOfSelection); |
| 1178 | EditorMenu_addCommand (domainMenu, U"Move cursor to...", |
| 1179 | 1, menu_cb_moveCursorTo); |
| 1180 | EditorMenu_addCommand (domainMenu, U"Move cursor by...", |
| 1181 | 1, menu_cb_moveCursorBy); |
| 1182 | EditorMenu_addCommand (domainMenu, U"Move start of selection by... || Move begin of selection by...", |
| 1183 | 1, menu_cb_moveStartOfSelectionBy); |
| 1184 | EditorMenu_addCommand (domainMenu, U"Move end of selection by...", |
| 1185 | 1, menu_cb_moveEndOfSelectionBy); |
| 1186 | EditorMenu_addCommand (domainMenu, U"Selection step settings...", |
| 1187 | 1, menu_cb_selectionStepSettings); |
| 1188 | EditorMenu_addCommand (domainMenu, U"Select earlier", |
| 1189 | GuiMenu_DEPTH_1 | GuiMenu_UP_ARROW, menu_cb_selectEarlier); |
| 1190 | EditorMenu_addCommand (domainMenu, U"Select later", |
| 1191 | GuiMenu_DEPTH_1 | GuiMenu_DOWN_ARROW, menu_cb_selectLater); |
| 1192 | EditorMenu_addCommand (domainMenu, U"Move start of selection left || Move begin of selection left", |
| 1193 | GuiMenu_DEPTH_1 | GuiMenu_SHIFT | GuiMenu_UP_ARROW, menu_cb_moveStartOfSelectionLeft); |
| 1194 | EditorMenu_addCommand (domainMenu, U"Move start of selection right || Move begin of selection right", |
| 1195 | GuiMenu_DEPTH_1 | GuiMenu_SHIFT | GuiMenu_DOWN_ARROW, menu_cb_moveStartOfSelectionRight); |
| 1196 | EditorMenu_addCommand (domainMenu, U"Move end of selection left", |
| 1197 | GuiMenu_DEPTH_1 | GuiMenu_COMMAND | GuiMenu_UP_ARROW, menu_cb_moveEndOfSelectionLeft); |
| 1198 | EditorMenu_addCommand (domainMenu, U"Move end of selection right", |
| 1199 | GuiMenu_DEPTH_1 | GuiMenu_COMMAND | GuiMenu_DOWN_ARROW, menu_cb_moveEndOfSelectionRight); |
| 1200 | |
| 1201 | if (our v_hasPlayMenu ()) { |
| 1202 | our playMenu = Editor_addMenu (this, U"Play", 0); |
| 1203 | v_createMenuItems_play (our playMenu); |
| 1204 | } |
| 1205 | |
| 1206 | for (integer iarea = 1; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 1207 | FunctionArea area = static_cast <FunctionArea> (our functionAreas [iarea].get()); |
| 1208 | if (area) |
| 1209 | area -> v_createMenus (); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | void structFunctionEditor :: v_updateMenuItems () { |
| 1214 | FunctionEditor_Parent :: v_updateMenuItems (); |
| 1215 | for (integer iarea = 1; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 1216 | FunctionArea area = static_cast <FunctionArea> (our functionAreas [iarea].get()); |
| 1217 | if (area) |
| 1218 | area -> v_updateMenuItems (); |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | void structFunctionEditor :: v_createMenuItems_help (EditorMenu menu) { |
| 1223 | FunctionEditor_Parent :: v_createMenuItems_help (menu); |
| 1224 | EditorMenu_addCommand (menu, U"Praat Intro", 0, HELP__intro); |
| 1225 | } |
| 1226 | |
| 1227 | void structFunctionEditor :: v_draw () { |
| 1228 | for (integer iarea = 1; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 1229 | FunctionArea area = static_cast <FunctionArea> (our functionAreas [iarea].get()); |
| 1230 | if (area) |
| 1231 | FunctionArea_drawOne (area); |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | static void gui_drawingarea_cb_expose (FunctionEditor me, GuiDrawingArea_ExposeEvent /* event */) { |
| 1236 | if (! my graphics) |
| 1237 | return; // could be the case in the very beginning |
| 1238 | if (! my enableUpdates) |
| 1239 | return; |
| 1240 | #ifdef WIN32 |
| 1241 | if (my duringPlay) { |
| 1242 | GraphicsScreen graphics = (GraphicsScreen) my graphics.get(); |
| 1243 | HDC foregroundContext = graphics -> d_gdiGraphicsContext; |
| 1244 | static HDC backgroundContext; |
| 1245 | if (! my backgroundIsUpToDate) { |
| 1246 | /* |
| 1247 | Draw into background. |
| 1248 | */ |
| 1249 | static HBITMAP backgroundBitmap; |
| 1250 | if (backgroundBitmap) |
| 1251 | DeleteObject (backgroundBitmap); |
| 1252 | if (backgroundContext) |
| 1253 | DeleteDC (backgroundContext); |
| 1254 | backgroundContext = CreateCompatibleDC (foregroundContext); |
| 1255 | backgroundBitmap = CreateCompatibleBitmap (foregroundContext, my drawingArea -> d_widget -> width, my drawingArea -> d_widget -> height); |
| 1256 | SelectObject (backgroundContext, backgroundBitmap); |
| 1257 | SetBkMode (backgroundContext, TRANSPARENT); // not the default! |
| 1258 | SelectPen (backgroundContext, GetStockPen (BLACK_PEN)); |
| 1259 | SelectBrush (backgroundContext, GetStockBrush (BLACK_BRUSH)); |
| 1260 | SetTextAlign (backgroundContext, TA_LEFT | TA_BASELINE | TA_NOUPDATECP); // baseline is not the default! |
| 1261 | graphics -> d_gdiGraphicsContext = backgroundContext; |
| 1262 | drawBackgroundAndData (me); |
| 1263 | graphics -> d_gdiGraphicsContext = foregroundContext; |
| 1264 | my backgroundIsUpToDate = true; |
| 1265 | } |
| 1266 | /* |
| 1267 | Copy to foreground. |
| 1268 | */ |
| 1269 | BitBlt (foregroundContext, 0, 0, my drawingArea -> d_widget -> width, my drawingArea -> d_widget -> height, backgroundContext, 0, 0, SRCCOPY); |
| 1270 | } else { |
| 1271 | my backgroundIsUpToDate = false; |
| 1272 | drawBackgroundAndData (me); |
| 1273 | } |
| 1274 | #else |
| 1275 | drawBackgroundAndData (me); |
| 1276 | #endif |
| 1277 | |
| 1278 | /* |
| 1279 | Draw the running cursor. |
| 1280 | */ |
| 1281 | if (my duringPlay) { |
| 1282 | if (Melder_debug == 53) { |
| 1283 | static integer numberOfRunningCursorsDrawn = 0; |
| 1284 | numberOfRunningCursorsDrawn += 1; |
| 1285 | Melder_casual (U"playing cursor ", numberOfRunningCursorsDrawn); |
| 1286 | } |
| 1287 | my viewDataAsWorldByFraction (); |
| 1288 | Graphics_setColour (my graphics.get(), Melder_BLACK); |
| 1289 | Graphics_setLineWidth (my graphics.get(), 3.0); |
| 1290 | Graphics_xorOn (my graphics.get(), Melder_BLACK); |
| 1291 | Graphics_line (my graphics.get(), my playCursor, 0.0, my playCursor, 1.0); |
| 1292 | Graphics_xorOff (my graphics.get()); |
| 1293 | Graphics_setLineWidth (my graphics.get(), 1.0); |
| 1294 | } |
| 1295 | |
| 1296 | /* |
| 1297 | Draw the selection part. |
| 1298 | */ |
| 1299 | if (my instancePref_showSelectionViewer()) { |
| 1300 | /* |
| 1301 | Draw closing box. |
| 1302 | */ |
| 1303 | my viewAllAsPixelettes (); |
| 1304 | Graphics_setLineWidth (my graphics.get(), 1.0); |
| 1305 | const double left = my width_pxlt - my space + 9.0, right = my width_pxlt - 3.0; |
| 1306 | const double bottom = my height_pxlt - my space + 5.0, top = my height_pxlt - 5.0; |
| 1307 | Graphics_setColour (my graphics.get(), Melder_PINK); |
| 1308 | Graphics_fillRectangle (my graphics.get(), left, right, bottom, top); |
| 1309 | Graphics_setColour (my graphics.get(), Melder_GREY); |
| 1310 | Graphics_line (my graphics.get(), left + 2.0, bottom + 2.0, right - 2.0, top - 2.0); |
| 1311 | Graphics_line (my graphics.get(), left + 2.0, top - 2.0, right - 2.0, bottom + 2.0); |
| 1312 | Graphics_setColour (my graphics.get(), Melder_BLACK); |
| 1313 | /* |
| 1314 | Draw content. |
| 1315 | */ |
| 1316 | my viewInnerSelectionViewerAsFractionByFraction (); |
| 1317 | if (my duringPlay) |
| 1318 | my v_drawRealTimeSelectionViewer (my playCursor); |
| 1319 | else |
| 1320 | my v_drawSelectionViewer (); |
| 1321 | } |
| 1322 | |
| 1323 | /* |
| 1324 | Not really drawing, but until further notice we believe that updating the menu items |
| 1325 | (i.e. making them sensitive or insensitive, or checking or unchecking them) could be done as often as redrawing. |
| 1326 | */ |
| 1327 | my v_updateMenuItems (); |
| 1328 | } |
| 1329 | |
| 1330 | bool FunctionEditor_defaultMouseInWideDataView (FunctionEditor me, GuiDrawingArea_MouseEvent event, double mouseTime) { |
| 1331 | my viewDataAsWorldByFraction (); |
| 1332 | Melder_assert (isdefined (mouseTime)); |
| 1333 | Melder_assert (my startSelection <= my endSelection); |
| 1334 | Melder_clip (my startWindow, & mouseTime, my endWindow); // WYSIWYG |
| 1335 | if (event -> isClick()) { |
| 1336 | /* |
| 1337 | Ignore any click that occurs during a drag, |
| 1338 | such as might occur when the user has both a mouse and a trackpad. |
| 1339 | */ |
| 1340 | if (isdefined (my anchorTime)) |
| 1341 | return false; |
| 1342 | const double selectedMiddleTime = 0.5 * (my startSelection + my endSelection); |
| 1343 | const bool theyWantToExtendTheCurrentSelectionAtTheLeft = |
| 1344 | (event -> shiftKeyPressed && mouseTime < selectedMiddleTime) || event -> isLeftBottomFunctionKeyPressed(); |
| 1345 | const bool theyWantToExtendTheCurrentSelectionAtTheRight = |
| 1346 | (event -> shiftKeyPressed && mouseTime >= selectedMiddleTime) || event -> isRightBottomFunctionKeyPressed(); |
| 1347 | if (theyWantToExtendTheCurrentSelectionAtTheLeft) { |
| 1348 | my startSelection = mouseTime; |
| 1349 | my anchorTime = my endSelection; |
| 1350 | } else if (theyWantToExtendTheCurrentSelectionAtTheRight) { |
| 1351 | my endSelection = mouseTime; |
| 1352 | my anchorTime = my startSelection; |
| 1353 | } else { |
| 1354 | my startSelection = mouseTime; |
| 1355 | my endSelection = mouseTime; |
| 1356 | my anchorTime = mouseTime; |
| 1357 | } |
| 1358 | Melder_sort (& my startSelection, & my endSelection); |
| 1359 | Melder_assert (isdefined (my anchorTime)); |
| 1360 | } else if (event -> isDrag() || event -> isDrop()) { |
| 1361 | /* |
| 1362 | Ignore any drag or drop that happens after a descendant preempted the above click handling. |
| 1363 | */ |
| 1364 | if (isundef (my anchorTime)) |
| 1365 | return false; |
| 1366 | if (! my hasBeenDraggedBeyondVicinityRadiusAtLeastOnce) { |
| 1367 | const double distanceToAnchor_mm = fabs (Graphics_dxWCtoMM (my graphics.get(), mouseTime - my anchorTime)); |
| 1368 | constexpr double vicinityRadius_mm = 1.0; |
| 1369 | if (distanceToAnchor_mm > vicinityRadius_mm) |
| 1370 | my hasBeenDraggedBeyondVicinityRadiusAtLeastOnce = true; |
| 1371 | } |
| 1372 | if (my hasBeenDraggedBeyondVicinityRadiusAtLeastOnce) { |
| 1373 | my startSelection = std::min (my anchorTime, mouseTime); |
| 1374 | my endSelection = std::max (my anchorTime, mouseTime); |
| 1375 | } |
| 1376 | if (event -> isDrop()) { |
| 1377 | my anchorTime = undefined; |
| 1378 | my hasBeenDraggedBeyondVicinityRadiusAtLeastOnce = false; |
| 1379 | } |
| 1380 | } |
| 1381 | return true; |
| 1382 | } |
| 1383 | |
| 1384 | bool structFunctionEditor :: v_mouseInWideDataView (GuiDrawingArea_MouseEvent event, double x_world, double globalY_fraction) { |
| 1385 | bool result = false; |
| 1386 | if (event -> isClick ()) |
| 1387 | for (integer iarea = 1; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 1388 | FunctionArea area = static_cast <FunctionArea> (our functionAreas [iarea].get()); |
| 1389 | if (area) |
| 1390 | area -> isClickAnchor = area -> y_fraction_globalIsInside (globalY_fraction); |
| 1391 | } |
| 1392 | {// scope |
| 1393 | integer iarea = 1; |
| 1394 | for (; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 1395 | FunctionArea area = static_cast <FunctionArea> (our functionAreas [iarea].get()); |
| 1396 | if (area && area -> isClickAnchor) { |
| 1397 | const double localY_fraction = area -> y_fraction_globalToLocal (globalY_fraction); |
| 1398 | FunctionArea_setViewport (area); // for Graphics_dxWCtoMM and the like |
| 1399 | result = area -> v_mouse (event, x_world, localY_fraction); |
| 1400 | break; |
| 1401 | } |
| 1402 | } |
| 1403 | if (iarea > FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS) |
| 1404 | result = FunctionEditor_defaultMouseInWideDataView (this, event, x_world); |
| 1405 | } |
| 1406 | if (event -> isDrop()) { |
| 1407 | for (integer iarea = 1; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 1408 | FunctionArea area = static_cast <FunctionArea> (our functionAreas [iarea].get()); |
| 1409 | if (area) |
| 1410 | area -> isClickAnchor = false; |
| 1411 | } |
| 1412 | } |
| 1413 | return result; |
| 1414 | } |
| 1415 | |
| 1416 | void structFunctionEditor :: v_clickSelectionViewer (double /* x_fraction */, double /* y_fraction */) { |
| 1417 | } |
| 1418 | |
| 1419 | static void gui_drawingarea_cb_mouse (FunctionEditor me, GuiDrawingArea_MouseEvent event) { |
| 1420 | if (! my graphics) |
| 1421 | return; // could be the case in the very beginning |
| 1422 | my viewAllAsPixelettes (); |
| 1423 | double x_pxlt, y_pxlt; |
| 1424 | Graphics_DCtoWC (my graphics.get(), event -> x, event -> y, & x_pxlt, & y_pxlt); |
| 1425 | if (event -> isClick()) { |
| 1426 | if (my v_hasSelectionViewer() || my instancePref_showSelectionViewer()) { |
| 1427 | const double left = my width_pxlt - my space + 9.0, right = my width_pxlt - 3.0; |
| 1428 | const double bottom = my height_pxlt - my space + 5.0, top = my height_pxlt - 5.0; |
| 1429 | if (x_pxlt > left && x_pxlt < right && y_pxlt > bottom && y_pxlt < top) { |
| 1430 | my setInstancePref_showSelectionViewer (! my instancePref_showSelectionViewer()); // toggle |
| 1431 | my updateGeometry (GuiControl_getWidth (my drawingArea), GuiControl_getHeight (my drawingArea)); |
| 1432 | FunctionEditor_redraw (me); |
| 1433 | return; |
| 1434 | } |
| 1435 | } |
| 1436 | my clickWasModifiedByShiftKey = event -> shiftKeyPressed; |
| 1437 | my clickWasModifiedByOptionKey = event -> optionKeyPressed; |
| 1438 | my clickWasModifiedByCommandKey = event -> commandKeyPressed; |
| 1439 | my anchorIsInSelectionViewer = my isInSelectionViewer (x_pxlt); |
| 1440 | my anchorIsInWideDataView = ( y_pxlt > my dataBottom_pxlt() && y_pxlt < my dataTop_pxlt() ); |
| 1441 | } |
| 1442 | if (my anchorIsInSelectionViewer) { |
| 1443 | my viewInnerSelectionViewerAsFractionByFraction (); |
| 1444 | double x_fraction, y_fraction; |
| 1445 | Graphics_DCtoWC (my graphics.get(), event -> x, event -> y, & x_fraction, & y_fraction); |
| 1446 | if (event -> isClick()) { |
| 1447 | my v_clickSelectionViewer (x_fraction, y_fraction); |
| 1448 | //Melder_assert (isdefined (my startSelection)); // precondition of v_updateText() |
| 1449 | //my v_updateText (); |
| 1450 | FunctionEditor_redraw (me); |
| 1451 | updateGroup (me, false, false); // TODO: why needed? |
| 1452 | } else; // no dragging (yet?) in any selection viewer |
| 1453 | } else if (my anchorIsInWideDataView) { |
| 1454 | my viewDataAsWorldByFraction (); |
| 1455 | double x_world, y_fraction; |
| 1456 | Graphics_DCtoWC (my graphics.get(), event -> x, event -> y, & x_world, & y_fraction); |
| 1457 | my v_distributeAreas (); |
| 1458 | my v_mouseInWideDataView (event, x_world, y_fraction); // this can change the selection, but not (yet?) the window (last checked 2023-02-13) |
| 1459 | Melder_assert (isdefined (my startSelection)); // precondition of FunctionEditor_selectionMarksChanged() |
| 1460 | FunctionEditor_selectionMarksChanged (me); |
| 1461 | } else { // clicked outside signal region? Let us hear it |
| 1462 | try { |
| 1463 | if (event -> isClick()) { |
| 1464 | for (integer i = 0; i < 8; i ++) { |
| 1465 | if (x_pxlt > my rect [i]. left && x_pxlt < my rect [i]. right && y_pxlt > my rect [i]. bottom && y_pxlt < my rect [i]. top) { |
| 1466 | switch (i) { |
| 1467 | case 0: my v_play (my tmin, my tmax); break; |
| 1468 | case 1: my v_play (my startWindow, my endWindow); break; |
| 1469 | case 2: my v_play (my tmin, my startWindow); break; |
| 1470 | case 3: my v_play (my endWindow, my tmax); break; |
| 1471 | case 4: my v_play (my startWindow, my marker [1]); break; |
| 1472 | case 5: my v_play (my marker [1], my marker [2]); break; |
| 1473 | case 6: my v_play (my marker [2], my marker [3]); break; |
| 1474 | case 7: my v_play (my startSelection, my endSelection); break; |
| 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | } else; // no dragging in the play rectangles |
| 1479 | } catch (MelderError) { |
| 1480 | Melder_flushError (); |
| 1481 | } |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | void structFunctionEditor :: v_createChildren () { |
| 1486 | int x = BUTTON_X; |
| 1487 | |
| 1488 | /* |
| 1489 | Create zoom buttons. |
| 1490 | */ |
| 1491 | GuiButton_createShown (our windowForm, x, x + BUTTON_WIDTH, -4 - Gui_PUSHBUTTON_HEIGHT, -4, |
| 1492 | U"all", gui_button_cb_showAll, this, 0); |
| 1493 | x += BUTTON_WIDTH + BUTTON_SPACING; |
| 1494 | GuiButton_createShown (our windowForm, x, x + BUTTON_WIDTH, -4 - Gui_PUSHBUTTON_HEIGHT, -4, |
| 1495 | U"in", gui_button_cb_zoomIn, this, 0); |
| 1496 | x += BUTTON_WIDTH + BUTTON_SPACING; |
| 1497 | GuiButton_createShown (our windowForm, x, x + BUTTON_WIDTH, -4 - Gui_PUSHBUTTON_HEIGHT, -4, |
| 1498 | U"out", gui_button_cb_zoomOut, this, 0); |
| 1499 | x += BUTTON_WIDTH + BUTTON_SPACING; |
| 1500 | GuiButton_createShown (our windowForm, x, x + BUTTON_WIDTH, -4 - Gui_PUSHBUTTON_HEIGHT, -4, |
| 1501 | U"sel", gui_button_cb_zoomToSelection, this, 0); |
| 1502 | x += BUTTON_WIDTH + BUTTON_SPACING; |
| 1503 | GuiButton_createShown (our windowForm, x, x + BUTTON_WIDTH, -4 - Gui_PUSHBUTTON_HEIGHT, -4, |
| 1504 | U"bak", gui_button_cb_zoomBack, this, 0); |
| 1505 | |
| 1506 | /* |
| 1507 | Create scroll bar. |
| 1508 | */ |
| 1509 | our scrollBar = GuiScrollBar_createShown (our windowForm, |
| 1510 | x += BUTTON_WIDTH + BUTTON_SPACING, -80 - BUTTON_SPACING, -4 - Gui_PUSHBUTTON_HEIGHT, 0, |
| 1511 | 1, maximumScrollBarValue, 1, maximumScrollBarValue - 1, 1, 1, |
| 1512 | gui_cb_scroll, this, GuiScrollBar_HORIZONTAL); |
| 1513 | |
| 1514 | /* |
| 1515 | Create Group button. |
| 1516 | */ |
| 1517 | our groupButton = GuiCheckButton_createShown (our windowForm, -80, 0, -4 - Gui_PUSHBUTTON_HEIGHT, -4, |
| 1518 | U"Group", gui_checkbutton_cb_group, this, group_equalDomain (our tmin, our tmax) ? GuiCheckButton_SET : 0); |
| 1519 | |
| 1520 | /* |
| 1521 | Create optional text field. |
| 1522 | */ |
| 1523 | if (our v_hasText ()) { |
| 1524 | our textArea = GuiText_createShown (our windowForm, 0, 0, |
| 1525 | Machine_getMenuBarBottom (), |
| 1526 | Machine_getMenuBarBottom () + TEXT_HEIGHT, |
| 1527 | GuiText_INKWRAP | GuiText_SCROLLED |
| 1528 | ); |
| 1529 | #if gtk |
| 1530 | Melder_assert (our textArea -> d_widget); |
| 1531 | gtk_widget_grab_focus (GTK_WIDGET (our textArea -> d_widget)); // BUG: can hardly be correct (the text should grab the focus of the window, not the global focus) |
| 1532 | #elif cocoa |
| 1533 | Melder_assert ([(NSView *) our textArea -> d_widget window]); |
| 1534 | //[[(NSView *) our textArea -> d_widget window] setInitialFirstResponder: (NSView *) our textArea -> d_widget]; |
| 1535 | [[(NSView *) our textArea -> d_widget window] makeFirstResponder: (NSView *) our textArea -> d_widget]; |
| 1536 | #endif |
| 1537 | } |
| 1538 | |
| 1539 | /* |
| 1540 | Create drawing area. |
| 1541 | */ |
| 1542 | #if cocoa |
| 1543 | int marginBetweenTextAndDrawingAreaToEnsureCorrectUnhighlighting = 3; |
| 1544 | #else |
| 1545 | int marginBetweenTextAndDrawingAreaToEnsureCorrectUnhighlighting = 0; |
| 1546 | #endif |
| 1547 | our drawingArea = GuiDrawingArea_createShown (our windowForm, |
| 1548 | 0, 0, |
| 1549 | Machine_getMenuBarBottom () + ( our v_hasText () ? TEXT_HEIGHT + marginBetweenTextAndDrawingAreaToEnsureCorrectUnhighlighting : 0), -8 - Gui_PUSHBUTTON_HEIGHT, |
| 1550 | gui_drawingarea_cb_expose, gui_drawingarea_cb_mouse, |
| 1551 | nullptr, gui_drawingarea_cb_resize, gui_drawingarea_cb_zoom, this, 0 |
| 1552 | ); |
| 1553 | GuiDrawingArea_setSwipable (our drawingArea, our scrollBar, nullptr); |
| 1554 | } |
| 1555 | |
| 1556 | void structFunctionEditor :: v1_dataChanged (Editor sender) { |
| 1557 | Melder_assert (our function()); |
| 1558 | Melder_assert (Thing_isa (our function(), classFunction)); |
| 1559 | if (! our group) { |
| 1560 | our tmin = our function() -> xmin; |
| 1561 | our tmax = our function() -> xmax; |
| 1562 | } |
| 1563 | if (our startWindow < our tmin || our startWindow > our tmax) { |
| 1564 | our startWindow = our tmin; |
| 1565 | our v_windowChanged (); |
| 1566 | } |
| 1567 | if (our endWindow < our tmin || our endWindow > our tmax) { |
| 1568 | our endWindow = our tmax; |
| 1569 | our v_windowChanged (); |
| 1570 | } |
| 1571 | if (our startWindow >= our endWindow) { |
| 1572 | our startWindow = our tmin; |
| 1573 | our endWindow = our tmax; |
| 1574 | our v_windowChanged (); |
| 1575 | } |
| 1576 | Melder_clip (our tmin, & our startSelection, our tmax); |
| 1577 | Melder_clip (our tmin, & our endSelection, our tmax); |
| 1578 | Melder_assert (isdefined (our startSelection)); // precondition of v_updateText() |
| 1579 | our v_updateText (); |
| 1580 | updateScrollBar (this); |
| 1581 | FunctionEditor_redraw (this); |
| 1582 | //updateGroup (this, true, true); |
| 1583 | } |
| 1584 | |
| 1585 | int structFunctionEditor :: v_playCallback (int phase, double /* startTime */, double endTime, double currentTime) { |
| 1586 | Melder_assert (isdefined (currentTime)); |
| 1587 | our playCursor = currentTime; |
| 1588 | if (phase == 1) { |
| 1589 | our duringPlay = true; |
| 1590 | return 1; |
| 1591 | } |
| 1592 | if (phase == 3) { |
| 1593 | our duringPlay = false; |
| 1594 | if (currentTime < endTime && MelderAudio_stopWasExplicit ()) { |
| 1595 | if (currentTime > our startSelection && currentTime < our endSelection) |
| 1596 | our startSelection = currentTime; |
| 1597 | else |
| 1598 | our startSelection = our endSelection = currentTime; |
| 1599 | Melder_assert (isdefined (our startSelection)); // precondition of v_updateText() |
| 1600 | our v_updateText (); |
| 1601 | updateGroup (this, false, true); |
| 1602 | } |
| 1603 | } |
| 1604 | if (Melder_debug == 53) |
| 1605 | Melder_casual (U"draining"); |
| 1606 | Graphics_updateWs (our graphics.get()); // note: without setting my backgroundIsUpToDate to false |
| 1607 | GuiShell_drain (our windowForm, false, true); // this may not be needed on all platforms (but on Windows it is, at least on 2020-09-21) |
| 1608 | return 1; |
| 1609 | } |
| 1610 | |
| 1611 | int theFunctionEditor_playCallback (FunctionEditor me, int phase, double startTime, double endTime, double currentTime) { |
| 1612 | return my v_playCallback (phase, startTime, endTime, currentTime); |
| 1613 | } |
| 1614 | |
| 1615 | void FunctionEditor_init (FunctionEditor me, conststring32 title, Function data) { |
| 1616 | Melder_assert (data); |
| 1617 | Thing_cast (Function, function, data); |
| 1618 | |
| 1619 | if (Melder_debug == 55) |
| 1620 | Melder_casual (Thing_messageNameAndAddress (me), U" init"); |
| 1621 | my tmin = function -> xmin; // set before adding children (see group button) |
| 1622 | my tmax = function -> xmax; |
| 1623 | Editor_init (me, 0, 0, my classPref_shellWidth(), my classPref_shellHeight(), title, function); |
| 1624 | |
| 1625 | my startWindow = my tmin; |
| 1626 | my endWindow = my tmax; |
| 1627 | my startSelection = my endSelection = 0.5 * (my tmin + my tmax); |
| 1628 | double maximumInitialLengthOfWindow = undefined; |
| 1629 | for (integer iarea = 1; iarea <= FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS; iarea ++) { |
| 1630 | FunctionArea area = static_cast <FunctionArea> (my functionAreas [iarea].get()); |
| 1631 | if (area) { |
| 1632 | const double areaMaximumInitialLengthOfWindow = area -> maximumInitialLengthOfWindow(); |
| 1633 | if (isdefined (areaMaximumInitialLengthOfWindow) && ! (areaMaximumInitialLengthOfWindow >= maximumInitialLengthOfWindow)) // NaN-safe |
| 1634 | maximumInitialLengthOfWindow = areaMaximumInitialLengthOfWindow; |
| 1635 | } |
| 1636 | } |
| 1637 | if (isdefined (maximumInitialLengthOfWindow)) { |
| 1638 | if (my endWindow - my startWindow > maximumInitialLengthOfWindow) { |
| 1639 | my endWindow = my startWindow + maximumInitialLengthOfWindow; |
| 1640 | if (my startWindow == my tmin) |
| 1641 | my startSelection = my endSelection = 0.5 * (my startWindow + my endWindow); |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | #if motif |
| 1646 | Melder_assert (XtWindow (my drawingArea -> d_widget)); |
| 1647 | #endif |
| 1648 | my graphics = Graphics_create_xmdrawingarea (my drawingArea); |
| 1649 | Graphics_setFontSize (my graphics.get(), 12); |
| 1650 | |
| 1651 | my updateGeometry (GuiControl_getWidth (my drawingArea), GuiControl_getHeight (my drawingArea)); |
| 1652 | |
| 1653 | Melder_assert (isdefined (my startSelection)); // precondition of v_updateText() |
| 1654 | //my v_updateText (); // BUG: superfluous because of Editor_dataChanged below? and too early if function pointers not yet set? |
| 1655 | if (group_equalDomain (my tmin, my tmax)) |
| 1656 | gui_checkbutton_cb_group (me, nullptr); // BUG: nullptr |
| 1657 | my enableUpdates = true; // BUG: explain why still needed |
| 1658 | |
| 1659 | Editor_dataChanged (me, me); // only to self, not to the other editors; BUG: should be in Editor_init? |
| 1660 | } |
| 1661 | |
| 1662 | void FunctionEditor_windowMarksChanged (FunctionEditor me, const bool selectionChanged) { |
| 1663 | if (selectionChanged) { |
| 1664 | Melder_assert (isdefined (my startSelection)); // precondition of v_updateText() |
| 1665 | my v_updateText (); |
| 1666 | } |
| 1667 | updateScrollBar (me); |
| 1668 | FunctionEditor_redraw (me); |
| 1669 | updateGroup (me, true, selectionChanged); |
| 1670 | } |
| 1671 | void FunctionEditor_selectionMarksChanged (FunctionEditor me) { |
| 1672 | Melder_assert (isdefined (my startSelection)); // precondition of v_updateText() |
| 1673 | my v_updateText (); |
| 1674 | FunctionEditor_redraw (me); |
| 1675 | updateGroup (me, false, true); |
| 1676 | } |
| 1677 | |
| 1678 | void FunctionEditor_updateText (FunctionEditor me) { |
| 1679 | Melder_assert (isdefined (my startSelection)); // precondition of v_updateText() |
| 1680 | my v_updateText (); |
| 1681 | } |
| 1682 | |
| 1683 | void FunctionEditor_redraw (FunctionEditor me) { |
| 1684 | my backgroundIsUpToDate = false; |
| 1685 | Graphics_updateWs (my graphics.get()); |
| 1686 | } |
| 1687 | |
| 1688 | void FunctionEditor_enableUpdates (FunctionEditor me, bool enable) { |
| 1689 | my enableUpdates = enable; |
| 1690 | } |
| 1691 | |
| 1692 | void FunctionEditor_ungroup (Daata data) { |
| 1693 | for (integer ieditor = 1; ieditor <= THE_MAXIMUM_GROUP_SIZE; ieditor ++) { |
| 1694 | const FunctionEditor me = theGroupMembers [ieditor]; |
| 1695 | if (me && my group && my data() == data) { // BUG: this may not be precise enough, in case an editor is editing multiple objects |
| 1696 | my group = false; |
| 1697 | GuiCheckButton_setValue (my groupButton, false); |
| 1698 | theGroupMembers [ieditor] = nullptr; |
| 1699 | theGroupSize --; |
| 1700 | Melder_assert (isdefined (my startSelection)); // precondition of v_updateText() |
| 1701 | my v_updateText (); |
| 1702 | FunctionEditor_redraw (me); // for setting buttons in v_draw() method |
| 1703 | } |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | void FunctionEditor_drawRangeMark (FunctionEditor me, double yWC, conststring32 yWC_string, conststring32 units, int verticalAlignment) { |
| 1708 | static MelderString text; |
| 1709 | MelderString_copy (& text, yWC_string, units); |
| 1710 | double textWidth = Graphics_textWidth (my graphics.get(), text.string) + Graphics_dxMMtoWC (my graphics.get(), 0.5); |
| 1711 | Graphics_setColour (my graphics.get(), Melder_BLUE); |
| 1712 | Graphics_line (my graphics.get(), my endWindow, yWC, my endWindow + textWidth, yWC); |
| 1713 | Graphics_setTextAlignment (my graphics.get(), Graphics_LEFT, verticalAlignment); |
| 1714 | if (verticalAlignment == Graphics_BOTTOM) |
| 1715 | yWC -= Graphics_dyMMtoWC (my graphics.get(), 0.5); |
| 1716 | Graphics_text (my graphics.get(), my endWindow, yWC, text.string); |
| 1717 | } |
| 1718 | |
| 1719 | void FunctionEditor_insertCursorFunctionValue (FunctionEditor me, double yWC, conststring32 yWC_string, conststring32 units, double minimum, double maximum) { |
| 1720 | double textX = my endWindow, textY = yWC; |
| 1721 | const bool tooHigh = ( Graphics_dyWCtoMM (my graphics.get(), maximum - textY) < 5.0 ); |
| 1722 | const bool tooLow = ( Graphics_dyWCtoMM (my graphics.get(), textY - minimum) < 5.0 ); |
| 1723 | if (yWC < minimum || yWC > maximum) |
| 1724 | return; |
| 1725 | Graphics_setColour (my graphics.get(), Melder_CYAN); |
| 1726 | Graphics_line (my graphics.get(), 0.99 * my endWindow + 0.01 * my startWindow, yWC, my endWindow, yWC); |
| 1727 | Graphics_fillCircle_mm (my graphics.get(), 0.5 * (my startSelection + my endSelection), yWC, 1.5); |
| 1728 | if (tooHigh) { |
| 1729 | if (tooLow) |
| 1730 | textY = 0.5 * (minimum + maximum); |
| 1731 | else |
| 1732 | textY = maximum - Graphics_dyMMtoWC (my graphics.get(), 5.0); |
| 1733 | } else if (tooLow) { |
| 1734 | textY = minimum + Graphics_dyMMtoWC (my graphics.get(), 5.0); |
| 1735 | } |
| 1736 | static MelderString text; |
| 1737 | MelderString_copy (& text, yWC_string, units); |
| 1738 | double textWidth = Graphics_textWidth (my graphics.get(), text.string); |
| 1739 | Graphics_fillCircle_mm (my graphics.get(), my endWindow + textWidth + Graphics_dxMMtoWC (my graphics.get(), 1.5), textY, 1.5); |
| 1740 | Graphics_setColour (my graphics.get(), Melder_RED); |
| 1741 | Graphics_setTextAlignment (my graphics.get(), Graphics_LEFT, Graphics_HALF); |
| 1742 | Graphics_text (my graphics.get(), textX, textY, text.string); |
| 1743 | } |
| 1744 | |
| 1745 | void FunctionEditor_drawHorizontalHair (FunctionEditor me, double yWC, conststring32 yWC_string, conststring32 units) { |
| 1746 | Graphics_setColour (my graphics.get(), Melder_RED); |
| 1747 | Graphics_line (my graphics.get(), my startWindow, yWC, my endWindow, yWC); |
| 1748 | Graphics_setTextAlignment (my graphics.get(), Graphics_RIGHT, Graphics_HALF); |
| 1749 | Graphics_text (my graphics.get(), my startWindow, yWC, yWC_string, units); |
| 1750 | } |
| 1751 | |
| 1752 | void FunctionEditor_drawGridLine (FunctionEditor me, double yWC) { |
| 1753 | Graphics_setColour (my graphics.get(), Melder_CYAN); |
| 1754 | Graphics_setLineType (my graphics.get(), Graphics_DOTTED); |
| 1755 | Graphics_line (my graphics.get(), my startWindow, yWC, my endWindow, yWC); |
| 1756 | Graphics_setLineType (my graphics.get(), Graphics_DRAWN); |
| 1757 | } |
| 1758 | |
| 1759 | /* End of file FunctionEditor.cpp */ |