| 1 |
#ifndef _FunctionArea_h_ |
| 2 |
#define _FunctionArea_h_ |
| 3 |
/* FunctionArea.h |
| 4 |
* |
| 5 |
* Copyright (C) 1992-2005,2007-2012,2015-2018,2020-2022,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 "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 functionViewerLeft_WC () const { |
| 160 |
return our functionEditor() -> functionViewerLeft_WC(); |
| 161 |
} |
| 162 |
double functionViewerRight_WC () const { |
| 163 |
return our functionEditor() -> functionViewerRight_WC(); |
| 164 |
} |
| 165 |
private: |
| 166 |
double _ymin_fraction, _ymax_fraction; |
| 167 |
double globalY_fraction_to_pxlt (double globalY_fraction) const { |
| 168 |
return our functionEditor() -> dataBottom_pxlt() + |
| 169 |
globalY_fraction * (our functionEditor() -> dataTop_pxlt() - our functionEditor() -> dataBottom_pxlt()); |
| 170 |
} |
| 171 |
double left_pxlt() const { return our functionEditor() -> dataLeft_pxlt(); } |
| 172 |
double right_pxlt() const { return our functionEditor() -> dataRight_pxlt(); } |
| 173 |
double bottom_pxlt() const { |
| 174 |
return globalY_fraction_to_pxlt (_ymin_fraction); |
| 175 |
} |
| 176 |
double top_pxlt() const { |
| 177 |
constexpr double legendMargin = 23.0; |
| 178 |
return globalY_fraction_to_pxlt (our _ymax_fraction) - legendMargin; |
| 179 |
} |
| 180 |
public: |
| 181 |
virtual void v_form_pictureSelection (EditorCommand); |
| 182 |
virtual void v_ok_pictureSelection (EditorCommand); |
| 183 |
virtual void v_do_pictureSelection (EditorCommand); |
| 184 |
|
| 185 |
virtual bool v_mouse (GuiDrawingArea_MouseEvent event, double x_world, double localY_fraction) { |
| 186 |
(void) localY_fraction; |
| 187 |
return FunctionEditor_defaultMouseInWideDataView (our functionEditor(), event, x_world); |
| 188 |
} |
| 189 |
|
| 190 |
public: |
| 191 |
/* |
| 192 |
The data handling functions assume that editable functions |
| 193 |
are identical to or part of the data of the editor, |
| 194 |
hence that if the function changes, the data has changed. |
| 195 |
*/ |
| 196 |
friend void FunctionArea_save (FunctionArea me, conststring32 undoText) { |
| 197 |
Melder_assert (my editable()); |
| 198 |
Editor_save (my functionEditor(), undoText); |
| 199 |
} |
| 200 |
friend void FunctionArea_broadcastDataChanged (FunctionArea me) { |
| 201 |
Melder_assert (my editable()); |
| 202 |
Editor_broadcastDataChanged (my boss()); |
| 203 |
} |
| 204 |
|
| 205 |
virtual double maximumInitialLengthOfWindow() { return undefined; } |
| 206 |
|
| 207 |
virtual void v_updateText () { } |
| 208 |
|
| 209 |
#include "FunctionArea_prefs.h" |
| 210 |
}; |
| 211 |
|
| 212 |
#define DEFINE_FunctionArea(which, FunctionAreaType, variableName) \ |
| 213 |
auto##FunctionAreaType & variableName() { \ |
| 214 |
return * reinterpret_cast <auto##FunctionAreaType *> (& our functionAreas [which]); \ |
| 215 |
} |
| 216 |
|
| 217 |
#define DEFINE_FunctionArea_create(FunctionAreaType, FunctionType) \ |
| 218 |
inline auto##FunctionAreaType FunctionAreaType##_create (bool editable, FunctionType functionToCopy, FunctionEditor boss) { \ |
| 219 |
auto##FunctionAreaType me = Thing_new (FunctionAreaType); \ |
| 220 |
FunctionArea_init (me.get(), editable, functionToCopy, boss); \ |
| 221 |
return me; \ |
| 222 |
} |
| 223 |
|
| 224 |
typedef MelderCallback <void, structFunctionArea, EditorCommand, UiForm, integer /*narg*/, Stackel /*args*/, conststring32, |
| 225 |
Interpreter> FunctionAreaCommandCallback; |
| 226 |
|
| 227 |
GuiMenuItem FunctionAreaMenu_addCommand (EditorMenu me, conststring32 itemTitle /* cattable */, uint32 flags, |
| 228 |
FunctionAreaCommandCallback commandCallback, FunctionArea commandBoss); |
| 229 |
|
| 230 |
#if defined (macintosh) |
| 231 |
#define FunctionArea_legend_LINES U"—" |
| 232 |
#define FunctionArea_legend_SPECKLES U"• •" |
| 233 |
#define FunctionArea_legend_LINES_SPECKLES U"–\\s{●}–" |
| 234 |
#define FunctionArea_legend_POLES U" ⃒ ⃒ ⃒" |
| 235 |
#define FunctionArea_legend_WAVEFORM U"\u301C" |
| 236 |
#define FunctionArea_legend_GREYS U"\\s{▩}" |
| 237 |
#define FunctionArea_legend_TEXTGRID U"☵" |
| 238 |
#elif defined (_WIN32) |
| 239 |
#define FunctionArea_legend_LINES U"—" |
| 240 |
#define FunctionArea_legend_SPECKLES U"\\s{• •}" |
| 241 |
#define FunctionArea_legend_LINES_SPECKLES U"–●–" |
| 242 |
#define FunctionArea_legend_POLES U"|||" |
| 243 |
#define FunctionArea_legend_WAVEFORM U"~" |
| 244 |
#define FunctionArea_legend_GREYS U"\\s{▒}" |
| 245 |
#define FunctionArea_legend_TEXTGRID U"☵" |
| 246 |
#else |
| 247 |
#define FunctionArea_legend_LINES U"—" |
| 248 |
#define FunctionArea_legend_SPECKLES U"• •" |
| 249 |
#define FunctionArea_legend_LINES_SPECKLES U"–●–" |
| 250 |
#define FunctionArea_legend_POLES U"|||" |
| 251 |
#define FunctionArea_legend_WAVEFORM U"\u301C" |
| 252 |
#define FunctionArea_legend_GREYS U"\\s{▒}" |
| 253 |
#define FunctionArea_legend_TEXTGRID U"☵" |
| 254 |
#endif |
| 255 |
|
| 256 |
inline void FunctionArea_drawLegend (FunctionArea me, |
| 257 |
conststring32 cattableText1, MelderColour colour1, |
| 258 |
conststring32 cattableText2 = U"", MelderColour colour2 = Melder_BLACK, |
| 259 |
conststring32 cattableText3 = U"", MelderColour colour3 = Melder_BLACK, |
| 260 |
conststring32 cattableText4 = U"", MelderColour colour4 = Melder_BLACK |
| 261 |
) { |
| 262 |
FunctionArea_drawLegend_ (me, |
| 263 |
cattableText1, colour1, |
| 264 |
cattableText2, colour2, |
| 265 |
cattableText3, colour3, |
| 266 |
cattableText4, colour4 |
| 267 |
); // this looks strange, but friend function cannot have default parameters unless defined when declared |
| 268 |
} |
| 269 |
|
| 270 |
void FunctionArea_garnishPicture (FunctionArea me); // optionally selection times and selection hairs |
| 271 |
|
| 272 |
/* End of file FunctionArea.h */ |
| 273 |
#endif |