Debian Codegraph @

stat/TableEditor.cpp

raw symbols SCIP
1 /* TableEditor.cpp
2 *
3 * Copyright (C) 2006-2013,2015-2023,2025 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 "TableEditor.h"
20 #include "machine.h"
21 #include "EditorM.h"
22
23 Thing_implement (TableEditor, Editor, 0);
24
25 #include "Prefs_define.h"
26 #include "TableEditor_prefs.h"
27 #include "Prefs_install.h"
28 #include "TableEditor_prefs.h"
29 #include "Prefs_copyToInstance.h"
30 #include "TableEditor_prefs.h"
31
32 #define SIZE_INCHES 80
33
34 /********** EDITOR METHODS **********/
35
36 void structTableEditor :: v1_info () {
37 TableEditor_Parent :: v1_info ();
38 MelderInfo_writeLine (U"Table uses text styles: ", our instancePref_useTextStyles());
39 //MelderInfo_writeLine (U"Table font size: ", our p_fontSize);
40 }
41
42 static void updateVerticalScrollBar (TableEditor me) {
43 GuiScrollBar_set (my verticalScrollBar, undefined, my table() -> rows.size + 1, my topRow, undefined, undefined, undefined);
44 }
45
46 static void updateHorizontalScrollBar (TableEditor me) {
47 GuiScrollBar_set (my horizontalScrollBar, undefined, my table() -> numberOfColumns + 1, my leftColumn, undefined, undefined, undefined);
48 }
49
50 void structTableEditor :: v1_dataChanged (Editor /* sender */) {
51 Melder_clipRight (& our topRow, our table() -> rows.size);
52 Melder_clipRight (& our leftColumn, our table() -> numberOfColumns);
53 updateVerticalScrollBar (this);
54 updateHorizontalScrollBar (this);
55 Graphics_updateWs (our graphics.get());
56 }
57
58 /********** FILE MENU **********/
59
60 static void menu_cb_preferences (TableEditor me, EDITOR_ARGS) {
61 EDITOR_FORM (U"TableEditor settings", nullptr);
62 OPTIONMENU (useTextStyles, U"The symbols %#_^ in labels", my default_useTextStyles() + 1)
63 OPTION (U"are shown as typed")
64 OPTION (U"mean italic/bold/sub/super")
65 EDITOR_OK
66 SET_OPTION (useTextStyles, my instancePref_useTextStyles() + 1)
67 EDITOR_DO
68 my setInstancePref_useTextStyles (useTextStyles - 1);
69 Graphics_updateWs (my graphics.get());
70 EDITOR_END
71 }
72
73 /********** EDIT MENU **********/
74
75 #ifndef macintosh
76 /*
77 On macOS, Cut/Copy/Paste are already available in the Praat:Edit menu.
78 */
79 static void menu_cb_CutText (TableEditor me, EDITOR_ARGS) {
80 GuiText_cut (my text);
81 }
82 static void menu_cb_CopyText (TableEditor me, EDITOR_ARGS) {
83 GuiText_copy (my text);
84 }
85 static void menu_cb_PasteText (TableEditor me, EDITOR_ARGS) {
86 GuiText_paste (my text);
87 }
88 static void menu_cb_EraseText (TableEditor me, EDITOR_ARGS) {
89 GuiText_remove (my text);
90 }
91 #endif
92
93 /********** VIEW MENU **********/
94
95 /********** HELP MENU **********/
96
97 static void menu_cb_TableEditorHelp (TableEditor, EDITOR_ARGS) {
98 Melder_help (U"TableEditor");
99 }
100
101 /********** DRAWING AREA **********/
102
103 void structTableEditor :: v_draw () {
104 constexpr double spacing = 2.0; // millimetres at both edges
105 /*
106 We fit 400 rows in 80 inches, which is 14.4 points per row.
107 */
108 integer rowmin = our topRow, rowmax = rowmin + 397;
109 integer colmin = our leftColumn, colmax = colmin + (kTableEditor_MAXNUM_VISIBLE_COLUMNS - 1);
110 Melder_clipLeft (1_integer, & rowmin);
111 Melder_clipRight (& rowmax, our table() -> rows.size);
112 Melder_clipRight (& colmax, our table() -> numberOfColumns);
113 Graphics_clearWs (graphics.get());
114 Graphics_setTextAlignment (graphics.get(), Graphics_CENTRE, Graphics_HALF);
115 Graphics_setWindow (graphics.get(), 0.0, 1.0, rowmin + 397.5, rowmin - 2.5);
116 Graphics_setColour (graphics.get(), Melder_SILVER);
117 Graphics_fillRectangle (graphics.get(), 0.0, 1.0, rowmin - 2.5, rowmin - 0.5);
118 Graphics_setColour (graphics.get(), Melder_BLACK);
119 Graphics_line (graphics.get(), 0.0, rowmin - 0.5, 1.0, rowmin - 0.5);
120 Graphics_setWindow (graphics.get(), 0.0, Graphics_dxWCtoMM (graphics.get(), 1.0), rowmin + 397.5, rowmin - 2.5);
121 /*
122 Determine the width of the column with the row numbers.
123 */
124 {// scope
125 double columnWidth = Graphics_textWidth (graphics.get(), U"row");
126 for (integer irow = rowmin; irow <= rowmax; irow ++) {
127 const double cellWidth = Graphics_textWidth (graphics.get(), Melder_integer (irow));
128 if (cellWidth > columnWidth)
129 columnWidth = cellWidth;
130 }
131 columnLeft [0] = columnWidth + 2 * spacing;
132 }
133 Graphics_setColour (graphics.get(), Melder_SILVER);
134 Graphics_fillRectangle (graphics.get(), 0.0, columnLeft [0], rowmin - 0.5, rowmin + 397.5);
135 Graphics_setColour (graphics.get(), Melder_BLACK);
136 Graphics_line (graphics.get(), columnLeft [0], rowmin - 0.5, columnLeft [0], rowmin + 397.5);
137 /*
138 Determine the widths of the columns.
139 */
140 for (integer icol = colmin; icol <= colmax; icol ++) {
141 conststring32 columnLabel = our table() -> columnHeaders [icol]. label.get();
142 double columnWidth = Graphics_textWidth (graphics.get(), Melder_integer (icol));
143 {// scope
144 if (! columnLabel)
145 columnLabel = U"";
146 const double cellWidth = Graphics_textWidth (graphics.get(), columnLabel);
147 if (cellWidth > columnWidth)
148 columnWidth = cellWidth;
149 }
150 for (integer irow = rowmin; irow <= rowmax; irow ++) {
151 conststring32 cell = Table_getStringValue_a (our table(), irow, icol);
152 Melder_assert (cell);
153 if (cell [0] == U'\0')
154 cell = U"?";
155 const double cellWidth = Graphics_textWidth (graphics.get(), cell);
156 if (cellWidth > columnWidth)
157 columnWidth = cellWidth;
158 }
159 our columnRight [icol - colmin] = columnLeft [icol - colmin] + columnWidth + 2 * spacing;
160 if (icol < colmax)
161 our columnLeft [icol - colmin + 1] = our columnRight [icol - colmin];
162 }
163 /*
164 Text can be "graphic" or not.
165 */
166 Graphics_setPercentSignIsItalic (our graphics.get(), our instancePref_useTextStyles());
167 Graphics_setNumberSignIsBold (our graphics.get(), our instancePref_useTextStyles());
168 Graphics_setCircumflexIsSuperscript (our graphics.get(), our instancePref_useTextStyles());
169 Graphics_setUnderscoreIsSubscript (our graphics.get(), our instancePref_useTextStyles());
170 /*
171 Show the row numbers.
172 */
173 Graphics_text (graphics.get(), our columnLeft [0] / 2, rowmin - 1, U"row");
174 for (integer irow = rowmin; irow <= rowmax; irow ++)
175 Graphics_text (graphics.get(), our columnLeft [0] / 2, irow, irow);
176 /*
177 Show the column labels.
178 */
179 for (integer icol = colmin; icol <= colmax; icol ++) {
180 const double mid = (our columnLeft [icol - colmin] + our columnRight [icol - colmin]) / 2;
181 conststring32 columnLabel = our table() -> columnHeaders [icol]. label.get();
182 if (! columnLabel || columnLabel [0] == U'\0')
183 columnLabel = U"?";
184 Graphics_text (graphics.get(), mid, rowmin - 2, icol);
185 Graphics_text (graphics.get(), mid, rowmin - 1, columnLabel);
186 }
187 /*
188 Show the cell contents.
189 */
190 for (integer irow = rowmin; irow <= rowmax; irow ++) {
191 for (integer icol = colmin; icol <= colmax; icol ++) {
192 if (irow == selectedRow && icol == selectedColumn) {
193 Graphics_setColour (graphics.get(), Melder_YELLOW);
194 const double dx = Graphics_dxMMtoWC (graphics.get(), 0.3);
195 Graphics_fillRectangle (graphics.get(),
196 our columnLeft [icol - colmin] + dx, columnRight [icol - colmin] - dx, irow - 0.45, irow + 0.55);
197 Graphics_setColour (graphics.get(), Melder_BLACK);
198 }
199 const double mid = (our columnLeft [icol - colmin] + our columnRight [icol - colmin]) / 2.0;
200 conststring32 cell = Table_getStringValue_a (our table(), irow, icol);
201 Melder_assert (cell);
202 if (cell [0] == U'\0')
203 cell = U"?";
204 Graphics_text (graphics.get(), mid, irow, cell);
205 }
206 }
207 }
208
209 bool structTableEditor :: v_clickCell (integer row, integer column, bool /* shiftKeyPressed */) {
210 our selectedRow = row;
211 our selectedColumn = column;
212 return true;
213 }
214
215 static void gui_text_cb_changed (TableEditor me, GuiTextEvent /* event */) {
216 Editor_broadcastDataChanged (me);
217 }
218
219 static void gui_drawingarea_cb_expose (TableEditor me, GuiDrawingArea_ExposeEvent /* event */) {
220 if (! my graphics)
221 return;
222 my v_draw ();
223 }
224
225 static void gui_drawingarea_cb_mouse (TableEditor me, GuiDrawingArea_MouseEvent event) {
226 if (! my graphics)
227 return; // could be the case in the very beginning
228 if (! event -> isClick())
229 return;
230 integer rowmin = my topRow, rowmax = rowmin + 397;
231 integer colmin = my leftColumn, colmax = colmin + (kTableEditor_MAXNUM_VISIBLE_COLUMNS - 1);
232 Melder_clipLeft (1_integer, & rowmin);
233 Melder_clipRight (& rowmax, my table() -> rows.size);
234 Melder_clipRight (& colmax, my table() -> numberOfColumns);
235 double xWC, yWC;
236 Graphics_DCtoWC (my graphics.get(), event -> x, event -> y, & xWC, & yWC);
237 if (yWC < rowmin - 0.45 || yWC > rowmax + 0.55)
238 return;
239 for (integer icol = colmin; icol <= colmax; icol ++) {
240 if (xWC > my columnLeft [icol - colmin] && xWC < my columnRight [icol - colmin]) {
241 const integer selectedRow = Melder_iround (yWC);
242 const integer selectedColumn = icol;
243 if (my v_clickCell (selectedRow, selectedColumn, event -> shiftKeyPressed))
244 Graphics_updateWs (my graphics.get());
245 return;
246 }
247 }
248 }
249
250 static void gui_drawingarea_cb_resize (TableEditor me, GuiDrawingArea_ResizeEvent /* event */) {
251 if (! my graphics)
252 return;
253 Graphics_updateWs (my graphics.get());
254 }
255
256 static void gui_cb_scrollHorizontal (TableEditor me, GuiScrollBarEvent event) {
257 const integer value = GuiScrollBar_getValue (event -> scrollBar);
258 if (value != my leftColumn) {
259 my leftColumn = value;
260 Graphics_updateWs (my graphics.get()); // wait for expose event
261 }
262 }
263
264 static void gui_cb_scrollVertical (TableEditor me, GuiScrollBarEvent event) {
265 const integer value = GuiScrollBar_getValue (event -> scrollBar);
266 if (value != my topRow) {
267 my topRow = value;
268 Graphics_updateWs (my graphics.get()); // wait for expose event
269 }
270 }
271
272 void structTableEditor :: v_createChildren () {
273 int y = Machine_getMenuBarBottom () + 4, scrollWidth = Machine_getScrollBarWidth ();
274
275 our text = GuiText_createShown (our windowForm, 0, 0, y, y + Machine_getTextHeight (), 0);
276 GuiText_setChangedCallback (our text, gui_text_cb_changed, this);
277 y += Machine_getTextHeight () + 4;
278
279 our drawingArea = GuiDrawingArea_createShown (our windowForm, 0, - scrollWidth, y, - scrollWidth,
280 gui_drawingarea_cb_expose, gui_drawingarea_cb_mouse,
281 nullptr, gui_drawingarea_cb_resize, nullptr, this, 0
282 );
283
284 our verticalScrollBar = GuiScrollBar_createShown (our windowForm, - scrollWidth, 0, y, - scrollWidth,
285 1, our table() -> rows.size + 1, 1, 1, 1, 10, gui_cb_scrollVertical, this, 0);
286
287 our horizontalScrollBar = GuiScrollBar_createShown (our windowForm, 0, - scrollWidth, - scrollWidth, 0,
288 1, our table() -> numberOfColumns + 1, 1, 1, 1, 3, gui_cb_scrollHorizontal, this, GuiScrollBar_HORIZONTAL);
289
290 GuiDrawingArea_setSwipable (our drawingArea, our horizontalScrollBar, our verticalScrollBar);
291 }
292
293 void structTableEditor :: v_createMenus () {
294 TableEditor_Parent :: v_createMenus ();
295
296 Editor_addCommand (this, U"File", U"Preferences...", 0, menu_cb_preferences);
297 Editor_addCommand (this, U"File", U"-- before scripting --", 0, nullptr);
298
299 #ifndef macintosh
300 Editor_addCommand (this, U"Edit", U"-- cut copy paste text --", 0, nullptr);
301 Editor_addCommand (this, U"Edit", U"Cut text", 'X', menu_cb_CutText);
302 Editor_addCommand (this, U"Edit", U"Copy text", 'C', menu_cb_CopyText);
303 Editor_addCommand (this, U"Edit", U"Paste text", 'V', menu_cb_PasteText);
304 Editor_addCommand (this, U"Edit", U"Erase text", 0, menu_cb_EraseText);
305 #endif
306 }
307
308 void structTableEditor :: v_createMenuItems_help (EditorMenu menu) {
309 TableEditor_Parent :: v_createMenuItems_help (menu);
310 EditorMenu_addCommand (menu, U"TableEditor help", U'?', menu_cb_TableEditorHelp);
311 }
312
313 autoTableEditor TableEditor_create (conststring32 title, Table table) {
314 try {
315 autoTableEditor me = Thing_new (TableEditor);
316 Editor_init (me.get(), 0, 0, 700, 500, title, table);
317 #if motif
318 Melder_assert (XtWindow (my drawingArea -> d_widget));
319 #endif
320 my topRow = 1;
321 my leftColumn = 1;
322 my selectedColumn = 1;
323 my selectedRow = 1;
324 my graphics = Graphics_create_xmdrawingarea (my drawingArea);
325 const double size_pixels = SIZE_INCHES * Graphics_getResolution (my graphics.get());
326 Graphics_setWsViewport (my graphics.get(), 0.0, size_pixels, 0.0, size_pixels);
327 Graphics_setWsWindow (my graphics.get(), 0.0, size_pixels, 0.0, size_pixels);
328 Graphics_setViewport (my graphics.get(), 0.0, size_pixels, 0.0, size_pixels);
329 Graphics_setFont (my graphics.get(), kGraphics_font::COURIER);
330 Graphics_setFontSize (my graphics.get(), 12);
331 Graphics_setUnderscoreIsSubscript (my graphics.get(), false);
332 Graphics_setAtSignIsLink (my graphics.get(), true);
333 return me;
334 } catch (MelderError) {
335 Melder_throw (U"TableEditor not created.");
336 }
337 }
338
339 /* End of file TableEditor.cpp */