| 1 | #ifndef _FunctionArea_h_ |
| 2 | #define _FunctionArea_h_ |
| 3 | /* FunctionArea.h |
| 4 | * |
| 5 | * Copyright (C) 1992-2005,2007-2012,2015-2018,2020-2022 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 2 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 "DataGui.h" |
| 22 | #include "FunctionEditor.h" |
| 23 | |
| 24 | Thing_define (FunctionArea, DataGui) { |
| 25 | /* |
| 26 | Accessors. |
| 27 | */ |
| 28 | public: // all readonly |
| 29 | Function function() const { Thing_cast (Function, function, our data()); return function; } |
| 30 | FunctionEditor functionEditor() const { Thing_cast (FunctionEditor, functionEditor, our boss()); return functionEditor; } |
| 31 | double startWindow() const { return our functionEditor() -> startWindow; } |
| 32 | double endWindow() const { return our functionEditor() -> endWindow; } |
| 33 | double startSelection() const { return our functionEditor() -> startSelection; } |
| 34 | double endSelection() const { return our functionEditor() -> endSelection; } |
| 35 | double tmin() const { return our functionEditor() -> tmin; } |
| 36 | double tmax() const { return our functionEditor() -> tmax; } |
| 37 | Graphics graphics() const { return our functionEditor() -> graphics.get(); } |
| 38 | |
| 39 | /* |
| 40 | Initialization. |
| 41 | */ |
| 42 | public: |
| 43 | friend void FunctionArea_init (FunctionArea me, bool editable, Function optionalFunctionToCopy, FunctionEditor boss) { |
| 44 | DataGui_init (me, nullptr, editable, boss); |
| 45 | my _optionalFunctionCopy = Data_copy (optionalFunctionToCopy); |
| 46 | } |
| 47 | bool functionHasBeenCopied () { |
| 48 | return !! our _optionalFunctionCopy; |
| 49 | } |
| 50 | private: |
| 51 | autoFunction _optionalFunctionCopy; |
| 52 | |
| 53 | public: |
| 54 | void functionChanged (Function newFunction) { |
| 55 | our setData (our _optionalFunctionCopy ? our _optionalFunctionCopy.get() : newFunction); |
| 56 | our v_invalidateAllDerivedDataCaches (); |
| 57 | our v_computeAuxiliaryData (); |
| 58 | } |
| 59 | void invalidateAllDerivedDataCaches () { |
| 60 | our v_invalidateAllDerivedDataCaches (); |
| 61 | } |
| 62 | virtual void v_windowChanged () { } |
| 63 | |
| 64 | protected: |
| 65 | virtual void v_invalidateAllDerivedDataCaches () { } // derived classes can call inherited at end |
| 66 | virtual void v_computeAuxiliaryData () { } // derived classes can call inherited at start |
| 67 | |
| 68 | public: |
| 69 | /* |
| 70 | To be called just before drawing or tracking: |
| 71 | */ |
| 72 | void setGlobalYRange_fraction (double ymin_fraction, double ymax_fraction) { |
| 73 | our _ymin_fraction = ymin_fraction; |
| 74 | our _ymax_fraction = ymax_fraction; |
| 75 | } |
| 76 | /* |
| 77 | FunctionArea_setViewport() is used both for drawing and for mousing. |
| 78 | */ |
| 79 | friend void FunctionArea_setViewport (constFunctionArea me) { |
| 80 | Graphics_setViewport (my graphics(), my left_pxlt(), my right_pxlt(), my bottom_pxlt(), my top_pxlt()); |
| 81 | } |
| 82 | friend void FunctionArea_drawOne (FunctionArea me) { |
| 83 | FunctionArea_prepareCanvas (me); |
| 84 | FunctionArea_drawInside (me); |
| 85 | } |
| 86 | friend void FunctionArea_drawTwo (FunctionArea me, FunctionArea you) { |
| 87 | FunctionArea_prepareCanvas (me); |
| 88 | FunctionArea_drawInside (me); |
| 89 | FunctionArea_drawInside (you); |
| 90 | } |
| 91 | friend void FunctionArea_prepareCanvas (FunctionArea me) { |
| 92 | FunctionArea_setViewport (me); |
| 93 | FunctionArea_drawBackground (me); |
| 94 | } |
| 95 | friend void FunctionArea_drawInside (FunctionArea me) { |
| 96 | my v_drawInside (); |
| 97 | } |
| 98 | friend void FunctionArea_drawBackground (constFunctionArea me) { |
| 99 | FunctionArea_eraseBackground (me); |
| 100 | FunctionArea_highlightSelectionBackground (me); |
| 101 | FunctionArea_drawFrame (me); |
| 102 | } |
| 103 | friend void FunctionArea_eraseBackground (constFunctionArea me) { |
| 104 | Graphics_setWindow (my graphics(), 0.0, 1.0, 0.0, 1.0); |
| 105 | Graphics_setColour (my graphics(), DataGuiColour_AREA_BACKGROUND); |
| 106 | Graphics_fillRectangle (my graphics(), 0.0, 1.0, 0.0, 1.0); |
| 107 | Graphics_setColour (my graphics(), DataGuiColour_NONEDITABLE); |
| 108 | } |
| 109 | friend void FunctionArea_highlightSelectionBackground (constFunctionArea me) { |
| 110 | Graphics_setWindow (my graphics(), my startWindow(), my endWindow(), 0.0, 1.0); |
| 111 | my v_specializedHighlightBackground (); |
| 112 | double left = my startSelection(), right = my endSelection(); |
| 113 | if (left < right && left < my endWindow() && right > my startWindow()) { |
| 114 | Melder_clipLeft (my startWindow(), & left); |
| 115 | Melder_clipRight (& right, my endWindow()); |
| 116 | Graphics_highlight (my graphics(), left, right, 0.0, 1.0); |
| 117 | } |
| 118 | } |
| 119 | friend void FunctionArea_drawFrame (constFunctionArea me) { |
| 120 | Graphics_setWindow (my graphics(), 0.0, 1.0, 0.0, 1.0); |
| 121 | if (my editable()) { |
| 122 | Graphics_setLineWidth (my graphics(), 2.0); |
| 123 | Graphics_setColour (my graphics(), DataGuiColour_EDITABLE_FRAME); |
| 124 | } else { |
| 125 | Graphics_setLineWidth (my graphics(), 1.0); |
| 126 | Graphics_setColour (my graphics(), DataGuiColour_NONEDITABLE_FRAME); |
| 127 | } |
| 128 | Graphics_rectangle (my graphics(), 0.0, 1.0, 0.0, 1.0); |
| 129 | Graphics_setColour (my graphics(), DataGuiColour_NONEDITABLE); |
| 130 | Graphics_setLineWidth (my graphics(), 1.0); |
| 131 | } |
| 132 | friend void FunctionArea_drawRightMark (FunctionArea me, double yWC, conststring32 yWC_string, conststring32 units, int verticalAlignment); |
| 133 | friend void FunctionArea_drawLegend_ (FunctionArea me, |
| 134 | conststring32 cattableText1, MelderColour colour1, |
| 135 | conststring32 cattableText2, MelderColour colour2, |
| 136 | conststring32 cattableText3, MelderColour colour3, |
| 137 | conststring32 cattableText4, MelderColour colour4 |
| 138 | ); |
| 139 | |
| 140 | private: |
| 141 | virtual void v_specializedHighlightBackground () const { } |
| 142 | protected: |
| 143 | virtual void v_drawInside () { } |
| 144 | public: |
| 145 | void setSelection (double startSelection, double endSelection) { |
| 146 | Melder_sort (& startSelection, & endSelection); |
| 147 | our functionEditor() -> startSelection = startSelection; |
| 148 | our functionEditor() -> endSelection = endSelection; |
| 149 | } |
| 150 | bool isClickAnchor = false; |
| 151 | bool y_fraction_globalIsInside (double globalY_fraction) const { |
| 152 | const double y_pxlt = globalY_fraction_to_pxlt (globalY_fraction); |
| 153 | return y_pxlt >= our bottom_pxlt() && y_pxlt < our top_pxlt(); |
| 154 | } |
| 155 | double y_fraction_globalToLocal (double globalY_fraction) const { |
| 156 | const double y_pxlt = globalY_fraction_to_pxlt (globalY_fraction); |
| 157 | return (y_pxlt - our bottom_pxlt()) / (our top_pxlt() - our bottom_pxlt()); |
| 158 | } |
| 159 | double functionViewerRight_WC () const { |
| 160 | return our functionEditor() -> functionViewerRight_WC(); |
| 161 | } |
| 162 | private: |
| 163 | double _ymin_fraction, _ymax_fraction; |
| 164 | double globalY_fraction_to_pxlt (double globalY_fraction) const { |
| 165 | return our functionEditor() -> dataBottom_pxlt() + |
| 166 | globalY_fraction * (our functionEditor() -> dataTop_pxlt() - our functionEditor() -> dataBottom_pxlt()); |
| 167 | } |
| 168 | double left_pxlt() const { return our functionEditor() -> dataLeft_pxlt(); } |
| 169 | double right_pxlt() const { return our functionEditor() -> dataRight_pxlt(); } |
| 170 | double bottom_pxlt() const { |
| 171 | return globalY_fraction_to_pxlt (_ymin_fraction); |
| 172 | } |
| 173 | double top_pxlt() const { |
| 174 | constexpr double legendMargin = 23.0; |
| 175 | return globalY_fraction_to_pxlt (our _ymax_fraction) - legendMargin; |
| 176 | } |
| 177 | public: |
| 178 | virtual void v_form_pictureSelection (EditorCommand); |
| 179 | virtual void v_ok_pictureSelection (EditorCommand); |
| 180 | virtual void v_do_pictureSelection (EditorCommand); |
| 181 | |
| 182 | virtual bool v_mouse (GuiDrawingArea_MouseEvent event, double x_world, double localY_fraction) { |
| 183 | (void) localY_fraction; |
| 184 | return FunctionEditor_defaultMouseInWideDataView (our functionEditor(), event, x_world); |
| 185 | } |
| 186 | |
| 187 | public: |
| 188 | /* |
| 189 | The data handling functions assume that editable functions |
| 190 | are identical to or part of the data of the editor, |
| 191 | hence that if the function changes, the data has changed. |
| 192 | */ |
| 193 | friend void FunctionArea_save (FunctionArea me, conststring32 undoText) { |
| 194 | Melder_assert (my editable()); |
| 195 | Editor_save (my functionEditor(), undoText); |
| 196 | } |
| 197 | friend void FunctionArea_broadcastDataChanged (FunctionArea me) { |
| 198 | Melder_assert (my editable()); |
| 199 | Editor_broadcastDataChanged (my boss()); |
| 200 | } |
| 201 | |
| 202 | virtual double maximumInitialLengthOfWindow() { return undefined; } |
| 203 | |
| 204 | virtual void v_updateText () { } |
| 205 | |
| 206 | #include "FunctionArea_prefs.h" |
| 207 | }; |
| 208 | |
| 209 | #define DEFINE_FunctionArea(which, FunctionAreaType, variableName) \ |
| 210 | auto##FunctionAreaType & variableName() { \ |
| 211 | return * reinterpret_cast <auto##FunctionAreaType *> (& our functionAreas [which]); \ |
| 212 | } |
| 213 | |
| 214 | #define DEFINE_FunctionArea_create(FunctionAreaType, FunctionType) \ |
| 215 | inline auto##FunctionAreaType FunctionAreaType##_create (bool editable, FunctionType functionToCopy, FunctionEditor boss) { \ |
| 216 | auto##FunctionAreaType me = Thing_new (FunctionAreaType); \ |
| 217 | FunctionArea_init (me.get(), editable, functionToCopy, boss); \ |
| 218 | return me; \ |
| 219 | } |
| 220 | |
| 221 | typedef MelderCallback <void, structFunctionArea, EditorCommand, UiForm, integer /*narg*/, Stackel /*args*/, conststring32, |
| 222 | Interpreter> FunctionAreaCommandCallback; |
| 223 | |
| 224 | GuiMenuItem FunctionAreaMenu_addCommand (EditorMenu me, conststring32 itemTitle /* cattable */, uint32 flags, |
| 225 | FunctionAreaCommandCallback commandCallback, FunctionArea commandBoss); |
| 226 | |
| 227 | #if defined (macintosh) |
| 228 | #define FunctionArea_legend_LINES U"—" |
| 229 | #define FunctionArea_legend_SPECKLES U"• •" |
| 230 | #define FunctionArea_legend_LINES_SPECKLES U"–\\s{●}–" |
| 231 | #define FunctionArea_legend_POLES U" ⃒ ⃒ ⃒" |
| 232 | #define FunctionArea_legend_WAVEFORM U"\u301C" |
| 233 | #define FunctionArea_legend_GREYS U"\\s{▩}" |
| 234 | #define FunctionArea_legend_TEXTGRID U"☵" |
| 235 | #elif defined (_WIN32) |
| 236 | #define FunctionArea_legend_LINES U"—" |
| 237 | #define FunctionArea_legend_SPECKLES U"\\s{• •}" |
| 238 | #define FunctionArea_legend_LINES_SPECKLES U"–●–" |
| 239 | #define FunctionArea_legend_POLES U"|||" |
| 240 | #define FunctionArea_legend_WAVEFORM U"~" |
| 241 | #define FunctionArea_legend_GREYS U"\\s{▒}" |
| 242 | #define FunctionArea_legend_TEXTGRID U"☵" |
| 243 | #else |
| 244 | #define FunctionArea_legend_LINES U"—" |
| 245 | #define FunctionArea_legend_SPECKLES U"• •" |
| 246 | #define FunctionArea_legend_LINES_SPECKLES U"–●–" |
| 247 | #define FunctionArea_legend_POLES U"|||" |
| 248 | #define FunctionArea_legend_WAVEFORM U"\u301C" |
| 249 | #define FunctionArea_legend_GREYS U"\\s{▒}" |
| 250 | #define FunctionArea_legend_TEXTGRID U"☵" |
| 251 | #endif |
| 252 | |
| 253 | inline void FunctionArea_drawLegend (FunctionArea me, |
| 254 | conststring32 cattableText1, MelderColour colour1, |
| 255 | conststring32 cattableText2 = U"", MelderColour colour2 = Melder_BLACK, |
| 256 | conststring32 cattableText3 = U"", MelderColour colour3 = Melder_BLACK, |
| 257 | conststring32 cattableText4 = U"", MelderColour colour4 = Melder_BLACK |
| 258 | ) { |
| 259 | FunctionArea_drawLegend_ (me, |
| 260 | cattableText1, colour1, |
| 261 | cattableText2, colour2, |
| 262 | cattableText3, colour3, |
| 263 | cattableText4, colour4 |
| 264 | ); // this looks strange, but friend function cannot have default parameters unless defined when declared |
| 265 | } |
| 266 | |
| 267 | void FunctionArea_garnishPicture (FunctionArea me); // optionally selection times and selection hairs |
| 268 | |
| 269 | /* End of file FunctionArea.h */ |
| 270 | #endif |