| 1 | #ifndef _FunctionEditor_h_ |
| 2 | #define _FunctionEditor_h_ |
| 3 | /* FunctionEditor.h |
| 4 | * |
| 5 | * Copyright (C) 1992-2023,2025,2026 Paul Boersma |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | * See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "ScriptEditor.h" |
| 22 | #include "Graphics.h" |
| 23 | #include "Function.h" |
| 24 | |
| 25 | struct FunctionEditor_picture { |
| 26 | /* KEEP IN SYNC WITH PREFS. */ |
| 27 | bool garnish; |
| 28 | }; |
| 29 | |
| 30 | constexpr integer FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS = 5; |
| 31 | |
| 32 | Thing_define (FunctionEditor, Editor) { |
| 33 | /* |
| 34 | Inherited attributes: |
| 35 | data: must be a Function. |
| 36 | */ |
| 37 | Function function() { return static_cast <Function> (our data()); } |
| 38 | |
| 39 | autoDataGui functionAreas [1 + FunctionEditor_MAXIMUM_NUMBER_OF_FUNCTION_AREAS]; |
| 40 | |
| 41 | /* |
| 42 | Subclasses may change the following attributes, |
| 43 | but have to respect the invariants, |
| 44 | and have to call FunctionEditor_windowMarksChanged() or FunctionEditor_selectionMarksChanged() |
| 45 | immediately after making those changes. |
| 46 | Invariants: |
| 47 | tmin <= startWindow < endWindow <= tmax; |
| 48 | tmin <= startSelection <= endSelection <= tmax; |
| 49 | */ |
| 50 | double tmin, tmax, startWindow, endWindow; |
| 51 | double startSelection, endSelection; // markers |
| 52 | |
| 53 | autoGraphics graphics; // used in the expose callback |
| 54 | /* |
| 55 | The Normalized Device cordinates are in "pixelettes", which are a bit smaller than pixels. |
| 56 | The purpose of this is optimal look and feel. |
| 57 | The extent to which a pixelette is smaller than a pixel depends on the size of the drawing area: |
| 58 | for smaller drawing areas, texts are a bit more cramped in their rectangles |
| 59 | than for larger drawing areas. |
| 60 | */ |
| 61 | double width_pxlt, height_pxlt; // size of drawing area in pixelettes |
| 62 | double _functionViewerLeft, _functionViewerRight; // location of function viewer in pixelettes |
| 63 | double _selectionViewerLeft, _selectionViewerRight; // location of selection viewer in pixelettes |
| 64 | void updateGeometry (int width_pixels, int height_pixels) { |
| 65 | Graphics_setWsViewport (our graphics.get(), 0.0, width_pixels, 0.0, height_pixels); |
| 66 | our width_pxlt = width_pixels + 21; // the +21 means that the horizontal margin becomes a tiny bit larger when the window grows |
| 67 | our height_pxlt = height_pixels + 111; // the +111 means that the vertical margins become a bit larger when the window grows |
| 68 | Graphics_setWsWindow (our graphics.get(), 0.0, our width_pxlt, 0.0, our height_pxlt); |
| 69 | //my viewAllAsPixelettes (); |
| 70 | /* |
| 71 | Put the function viewer at the left and the selection viewer at the right. |
| 72 | */ |
| 73 | our _functionViewerLeft = 0; |
| 74 | our _functionViewerRight = ( our instancePref_showSelectionViewer() ? our width_pxlt * (2.0/3.0) : our width_pxlt ); |
| 75 | our _selectionViewerLeft = our _functionViewerRight; |
| 76 | our _selectionViewerRight = our width_pxlt; |
| 77 | } |
| 78 | bool isInSelectionViewer (double x_pxlt) const { |
| 79 | return x_pxlt > our _selectionViewerLeft; |
| 80 | } |
| 81 | void viewAllAsPixelettes () const { |
| 82 | Graphics_setViewport (our graphics.get(), 0.0, our width_pxlt, 0.0, our height_pxlt); |
| 83 | Graphics_setWindow (our graphics.get(), 0.0, our width_pxlt, 0.0, our height_pxlt); |
| 84 | } |
| 85 | void viewFunctionViewerAsPixelettes () const { |
| 86 | Graphics_setViewport (our graphics.get(), our _functionViewerLeft, our _functionViewerRight, 0.0, our height_pxlt); |
| 87 | Graphics_setWindow (our graphics.get(), our _functionViewerLeft, our _functionViewerRight, 0.0, our height_pxlt); |
| 88 | } |
| 89 | void viewSelectionViewerAsPixelettes () const { |
| 90 | Graphics_setViewport (our graphics.get(), our _selectionViewerLeft, our _selectionViewerRight, 0.0, our height_pxlt); |
| 91 | Graphics_setWindow (our graphics.get(), our _selectionViewerLeft, our _selectionViewerRight, 0.0, our height_pxlt); |
| 92 | } |
| 93 | constexpr static double space = 30.0; |
| 94 | constexpr static double MARGIN = 107.0; |
| 95 | constexpr static double BOTTOM_MARGIN = 2.0; |
| 96 | constexpr static double TOP_MARGIN = 3.0; |
| 97 | double dataLeft_pxlt () const { return our _functionViewerLeft + our MARGIN; } |
| 98 | double dataRight_pxlt () const { return our _functionViewerRight - our MARGIN; } |
| 99 | double dataBottom_pxlt () const { return our BOTTOM_MARGIN + our space * 3; } |
| 100 | double dataTop_pxlt () const { return our height_pxlt - (our TOP_MARGIN + our space); } |
| 101 | void viewDataAsWorldByFraction () const { |
| 102 | Graphics_setViewport (our graphics.get(), our dataLeft_pxlt(), our dataRight_pxlt(), our dataBottom_pxlt(), our dataTop_pxlt()); |
| 103 | Graphics_setWindow (our graphics.get(), our startWindow, our endWindow, 0.0, 1.0); |
| 104 | } |
| 105 | void viewTallDataAsWorldByPixelettes () const { |
| 106 | Graphics_setViewport (our graphics.get(), our dataLeft_pxlt(), our dataRight_pxlt(), 0.0, our height_pxlt); |
| 107 | Graphics_setWindow (our graphics.get(), our startWindow, our endWindow, 0.0, our height_pxlt); |
| 108 | } |
| 109 | constexpr static double SELECTION_VIEWER_MARGIN = 0.0; |
| 110 | void viewInnerSelectionViewerAsFractionByFraction () const { |
| 111 | Graphics_setViewport (our graphics.get(), our _selectionViewerLeft + our MARGIN, our _selectionViewerRight - our MARGIN, |
| 112 | our BOTTOM_MARGIN + our space * 3, our height_pxlt - (our TOP_MARGIN + our space)); |
| 113 | Graphics_setViewport (our graphics.get(), |
| 114 | our _selectionViewerLeft + our SELECTION_VIEWER_MARGIN, our _selectionViewerRight - our SELECTION_VIEWER_MARGIN, |
| 115 | our SELECTION_VIEWER_MARGIN, our height_pxlt - our space - our SELECTION_VIEWER_MARGIN |
| 116 | ); |
| 117 | Graphics_setWindow (our graphics.get(), 0.0, 1.0, 0.0, 1.0); |
| 118 | } |
| 119 | double functionViewerLeft_WC () const { |
| 120 | return our startWindow - our MARGIN * (our endWindow - our startWindow) / (our dataRight_pxlt() - our dataLeft_pxlt()); |
| 121 | } |
| 122 | double functionViewerRight_WC () const { |
| 123 | return our endWindow + our MARGIN * (our endWindow - our startWindow) / (our dataRight_pxlt() - our dataLeft_pxlt()); |
| 124 | } |
| 125 | |
| 126 | GuiText textArea; // optional text at top |
| 127 | bool clickWasModifiedByShiftKey, clickWasModifiedByOptionKey, clickWasModifiedByCommandKey; // information for drag-and-drop and for start of play |
| 128 | bool duringPlay, backgroundIsUpToDate; |
| 129 | struct FunctionEditor_picture picture; |
| 130 | bool anchorIsInSelectionViewer = false; |
| 131 | bool anchorIsInWideDataView = false; |
| 132 | |
| 133 | /* Private: */ |
| 134 | GuiDrawingArea drawingArea; |
| 135 | GuiScrollBar scrollBar; |
| 136 | GuiCheckButton groupButton; |
| 137 | GuiObject bottomArea; |
| 138 | bool group, enableUpdates; |
| 139 | int nrect; |
| 140 | struct { double left, right, bottom, top; } rect [8]; |
| 141 | double marker [1 + 3], playCursor, startZoomHistory, endZoomHistory; |
| 142 | int numberOfMarkers; |
| 143 | |
| 144 | void v9_destroy () noexcept |
| 145 | override; |
| 146 | void v1_info () |
| 147 | override; |
| 148 | |
| 149 | bool v_hasPlayMenu () override { return true; } |
| 150 | void v_createMenus () |
| 151 | override; |
| 152 | void v_createMenuItems_prefs (EditorMenu) |
| 153 | override; |
| 154 | void v_createMenuItems_save (EditorMenu) |
| 155 | override; |
| 156 | void v_createMenuItems_edit (EditorMenu) |
| 157 | override; |
| 158 | void v_createMenuItems_play (EditorMenu) |
| 159 | override; |
| 160 | void v_createChildren () |
| 161 | override; |
| 162 | void v_createMenuItems_help (EditorMenu) |
| 163 | override; |
| 164 | void v_updateMenuItems () |
| 165 | override; |
| 166 | void v_prefs_addFields (EditorCommand) |
| 167 | override; |
| 168 | void v_prefs_setValues (EditorCommand) |
| 169 | override; |
| 170 | void v_prefs_getValues (EditorCommand) |
| 171 | override; |
| 172 | |
| 173 | void v1_dataChanged (Editor sender) |
| 174 | override; |
| 175 | |
| 176 | virtual void v_distributeAreas () { } |
| 177 | virtual void v_draw (); |
| 178 | virtual void v_windowChanged (); |
| 179 | /* |
| 180 | Behaviour of structFunctionEditor::v_windowChanged (): |
| 181 | dispatches to a function area. |
| 182 | */ |
| 183 | virtual bool v_hasSelectionViewer () { return false; } |
| 184 | virtual void v_drawSelectionViewer () { } |
| 185 | virtual void v_drawRealTimeSelectionViewer (double /* time */) { } |
| 186 | virtual conststring32 v_domainName () { return U"time"; } |
| 187 | virtual conststring32 v_selectionViewerName () { return U"selection viewer"; } |
| 188 | virtual conststring32 v_format_domain () { return U"Time"; } |
| 189 | virtual const char *v_format_short () { return u8"%.3f"; } |
| 190 | virtual const char *v_format_long () { return u8"%f"; } |
| 191 | virtual conststring32 v_format_units_long () { return U"seconds"; } |
| 192 | virtual conststring32 v_format_units_short () { return U"s"; } |
| 193 | virtual const char *v_format_totalDuration () { return u8"Total duration %f seconds"; } |
| 194 | virtual const char *v_format_window () { return u8"Visible part %f seconds"; } |
| 195 | virtual const char *v_format_selection () { return u8"%f (%.3f / s)"; } |
| 196 | virtual int v_fixedPrecision_long () { return 6; } |
| 197 | virtual bool v_hasText () { return false; } |
| 198 | virtual void v_play (double /* startTime */, double /* endTime */) { } |
| 199 | virtual bool v_mouseInWideDataView (GuiDrawingArea_MouseEvent event, double x_world, double globalY_fraction); |
| 200 | /* |
| 201 | Message: "they clicked in the data part of the window, or in the left or right margin." |
| 202 | 'event' is the mouse event, with still relevant info on phase and modifier keys; |
| 203 | 'x_world' is the time (or another world unit); |
| 204 | 'globalY_fraction' is a value between 0.0 (bottom) and 1.0 (top); |
| 205 | Behaviour of structFunctionEditor::v_mouseInWideDataView (): |
| 206 | dispatches to a function area, or otherwise calls FunctionEditor_defaultMouseInWideDataView(). |
| 207 | */ |
| 208 | virtual void v_clickSelectionViewer (double x_fraction, double y_fraction); |
| 209 | virtual int v_playCallback (int phase, double startTime, double endTime, double currentTime); |
| 210 | virtual void v_updateText (); |
| 211 | virtual void v_drawLegends () { } |
| 212 | |
| 213 | #include "FunctionEditor_prefs.h" |
| 214 | |
| 215 | public: |
| 216 | double anchorTime = undefined; |
| 217 | bool hasBeenDraggedBeyondVicinityRadiusAtLeastOnce = false; |
| 218 | }; |
| 219 | |
| 220 | int theFunctionEditor_playCallback (FunctionEditor me, int phase, double startTime, double endTime, double currentTime); |
| 221 | |
| 222 | #define FunctionEditor_UPDATE_NEEDED true |
| 223 | #define FunctionEditor_NO_UPDATE_NEEDED false |
| 224 | |
| 225 | void FunctionEditor_init (FunctionEditor me, conststring32 title, Function data); |
| 226 | /* |
| 227 | Function: |
| 228 | creates an Editor with a drawing area, a scroll bar and some buttons. |
| 229 | Postconditions: |
| 230 | my drawingArea is attached to the form at all sides, |
| 231 | my scrollBar only to the bottom, left and right sides. |
| 232 | */ |
| 233 | |
| 234 | void FunctionEditor_windowMarksChanged (FunctionEditor me, const bool selectionChanged); |
| 235 | /* |
| 236 | Function: |
| 237 | update the scroll bar, the drawing area and the buttons |
| 238 | (and if `selectionChanged` is true also the optional text field), |
| 239 | from the current total time and window |
| 240 | (and if `selectionChanged` is true also the current cursor and selection), |
| 241 | and redraw the contents. |
| 242 | Usage: |
| 243 | call this after a change in window marks. |
| 244 | (Upon a change in the domain, ungroup before calling this.) |
| 245 | */ |
| 246 | void FunctionEditor_selectionMarksChanged (FunctionEditor me); |
| 247 | /* |
| 248 | Function: |
| 249 | update optional text field, the drawing area and the buttons, |
| 250 | from the current cursor and selection, and redraw the contents. |
| 251 | Usage: |
| 252 | call this after a change in cursor or selection marks. |
| 253 | */ |
| 254 | |
| 255 | void FunctionEditor_scrollToNewSelection (FunctionEditor me, double t); |
| 256 | /* |
| 257 | Function: |
| 258 | shift (scroll) the window through time, to the new selection, keeping the window length constant. |
| 259 | Usage: |
| 260 | call this after a search. |
| 261 | */ |
| 262 | |
| 263 | void FunctionEditor_updateText (FunctionEditor me); |
| 264 | /* |
| 265 | Function: |
| 266 | update the optional text widget. |
| 267 | Usage: |
| 268 | call this after moving the cursor, if that would have to change the text. |
| 269 | The generic FunctionEditor also calls this if one of the other marks have changed. |
| 270 | Behaviour: |
| 271 | we just call the updateText method, which the inheritor will have to modify, |
| 272 | since FunctionEditor::updateText does nothing. |
| 273 | */ |
| 274 | |
| 275 | void FunctionEditor_redraw (FunctionEditor me); |
| 276 | /* |
| 277 | Function: |
| 278 | update the drawing area of a single editor. |
| 279 | Usage: |
| 280 | call this after she changes a view option (font, scaling, hide/show xx) |
| 281 | or after any of the data have changed. In the latter case, also call Editor_broadcastChange. |
| 282 | Behaviour: |
| 283 | we just call Graphics_updateWs (my graphics). |
| 284 | */ |
| 285 | |
| 286 | void FunctionEditor_enableUpdates (FunctionEditor me, bool enable); |
| 287 | /* |
| 288 | Function: |
| 289 | temporarily disable update event to cause 'draw' messages. |
| 290 | Usage: |
| 291 | If you call from your 'draw' method functions that may trigger expose events, |
| 292 | you should bracket those routines between |
| 293 | FunctionEditor_enableUpdates (me, false); |
| 294 | and |
| 295 | FunctionEditor_enableUpdates (me, true); |
| 296 | This may happen if you call an analysis routine which calls Melder_progress. |
| 297 | */ |
| 298 | |
| 299 | void FunctionEditor_ungroup (Daata data); |
| 300 | /* |
| 301 | Function: |
| 302 | force all editors containing `data` out of the group. |
| 303 | Usage: |
| 304 | Start cut or paste methods by calling this function, |
| 305 | as the grouped editors will not be synchronized |
| 306 | after either of those actions. Worse, the selection |
| 307 | may get outside the common interval of the editors. |
| 308 | */ |
| 309 | |
| 310 | /* |
| 311 | Some functions to enforce a common look to all function editors. |
| 312 | The x axis of the window is supposed to have been set to [my startWindow, my endWindow]. |
| 313 | Preconditions: |
| 314 | default line type, default line width. |
| 315 | Postconditions: |
| 316 | default line type, default line width, undefined colour, undefined text alignment. |
| 317 | */ |
| 318 | void FunctionEditor_drawRangeMark (FunctionEditor me, double yWC, conststring32 yWC_string, conststring32 units, int verticalAlignment); |
| 319 | void FunctionEditor_insertCursorFunctionValue (FunctionEditor me, double yWC, conststring32 yWC_string, conststring32 units, double minimum, double maximum); |
| 320 | void FunctionEditor_drawHorizontalHair (FunctionEditor me, double yWC, conststring32 yWC_string, conststring32 units); |
| 321 | void FunctionEditor_drawGridLine (FunctionEditor me, double yWC); |
| 322 | |
| 323 | bool FunctionEditor_defaultMouseInWideDataView (FunctionEditor me, GuiDrawingArea_MouseEvent event, double x_world); |
| 324 | /* |
| 325 | Behaviour: |
| 326 | moves the cursor to 'x_world', drags to create a selection, or extends the selection. |
| 327 | */ |
| 328 | |
| 329 | /* End of file FunctionEditor.h */ |
| 330 | #endif |