1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
|
/*****************************************************************//**
* \file toolbar.c
* \brief Toolbar specific functions and data
*********************************************************************/
/* XTrackCad - Model Railroad CAD
* Copyright (C) 2005,2023 Dave Bullis, Martin Fischer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "common.h"
#include "custom.h"
#include "fileio.h"
#include "param.h"
#include "track.h"
#include "include/toolbar.h"
EXPORT void ToolbarLayout(void* unused);
struct sToolbarState {
int previousGroup; // control variable for change control ops
int layerButton; // number of layer controls shown
wWinPix_t nextX; // drawing position for next control
wWinPix_t rowHeight; // height of row
};
// local function prototypes
static void InitializeToolbarDialog(void);
static void ToolbarChange(long changes);
static void ToolbarOk(void* unused);
static void ToolbarButtonPlace(struct sToolbarState* tbState, wIndex_t inx);
static void SaveToolbarConfig(void);
// toolbar properties
static long toolbarSet;
static wWinPix_t toolbarHeight = 0;
#define TOOLBARSET_INIT (0xFFFF)
#define TOOLBAR_SECTION "misc"
#define TOOLBAR_VARIABLE "toolbarset"
#define GROUP_DISTANCE (5) // default distance between button groups
#define GROUP_BIG_DISTANCE (GROUP_DISTANCE * 3) // big gap
#define TOOLBAR_MARGIN (20) // left and right margins of toolbar
#define FIXEDLAYERCONTROLS (2) // the layer groups has two controls that are
// always visible (list and background)
/*
* Bit handling macros
* these macros do not change the passed values but return the result.
* so if you want to change the value it has to be assigned eg.
* bits = SETBIT(bits, 2);
* in order to set bit 2 of the variable "bits"
*
*/
#define GETBIT(value, bitpos ) ((value) & (1UL << (bitpos)))
#define ISBITSET(value, bitpos ) (((value)&(1UL <<bitpos))!=0)
#define CLEARBIT(value, bitpos ) ((value) & ~(1UL <<(bitpos)))
#define SETBIT(value, bitpos) ((value) | (1UL <<(bitpos)))
#define ISGROUPVISIBLE(group) ISBITSET(toolbarSet, group)
// toolbar button list
#define BUTTON_MAX (250)
static struct {
wControl_p control;
wBool_t enabled;
wWinPix_t x, y;
long options;
int group;
wIndex_t cmdInx;
} buttonList[BUTTON_MAX];
EXPORT int buttonCnt = 0; // TODO-misc-refactor
// control the order of the button groups inside the toolbar
struct buttonGroups {
char* label; // display label
int group; // id of group
bool biggap; // control distance to previous group
};
static struct buttonGroups allToolbarGroups[] = {
{N_("File Buttons"), BG_FILE, false},
{N_("Print Buttons"), BG_PRINT, false},
{N_("Import/Export Buttons"), BG_EXPORTIMPORT, false},
{N_("Zoom Buttons"), BG_ZOOM, false},
{N_("Undo Buttons"), BG_UNDO, false},
{N_("Easement Button"), BG_EASE, false},
{N_("SnapGrid Buttons"), BG_SNAP, false},
{N_("Create Track Buttons"), BG_TRKCRT, true},
{N_("Layout Control Elements"), BG_CONTROL, true},
{N_("Modify Track Buttons"), BG_TRKMOD, false},
{N_("Properties/Select"), BG_SELECT, false},
{N_("Track Group Buttons"), BG_TRKGRP, false},
{N_("Train Group Buttons"), BG_TRAIN, true},
{N_("Create Misc Buttons"), BG_MISCCRT, false},
{N_("Ruler Button"), BG_RULER, false},
{N_("Layer Buttons"), BG_LAYER, true},
{N_("Hot Bar"), BG_HOTBAR},
{NULL, 0L}
};
#define COUNTTOOLBARGROUPS (BG_LAST)
// toolbar options dialog
static wWin_p toolbarW;
static unsigned long toggleSet;
// callbacks for button presses
static void SelectAllGroups(void* unused);
static void InvertSelection(void* unused);
static paramData_t toolbarPLs[] = {
{ PD_TOGGLE, &toggleSet, "toolbarset", 0, NULL},
#define I_SELECTALL (1)
{ PD_BUTTON, SelectAllGroups, "selectall", PDO_DLGBOXEND, NULL, N_("Select All") },
#define I_INVERT (2)
{ PD_BUTTON, InvertSelection, "invert", PDO_DLGHORZ, NULL, N_("Invert Selection")}
};
static paramGroup_t toolbarPG = { "toolbar", PGO_RECORD, toolbarPLs,
COUNT(toolbarPLs)
};
/**
* Initialize the list of available options. The list of labels is created
* from the allToolbarGroups array. Memory allocated here
* is never freed as it might be used when opening the dialog
*
* \param unused
*/
static void
InitializeToolbarDialog(void)
{
char** labels = MyMalloc((COUNT(allToolbarGroups)) * sizeof(char*));
for (int i = 0; i < COUNT(allToolbarGroups); i++) {
labels[i] = allToolbarGroups[i].label;
}
toolbarPLs[0].winData = labels;
ParamRegister(&toolbarPG);
}
static void ToolbarChange(long changes)
{
if ((changes & CHANGE_TOOLBAR)) {
MainProc(mainW, wResize_e, NULL, NULL);
}
}
/**
* Handle button press to select all groups. Set all bits to 1, unused bits
* will be ignored
*
* \param unused
*/
static void SelectAllGroups(void* unused)
{
toggleSet = ~(0UL);
ParamLoadControls(&toolbarPG);
}
/**
* Handle button press to invert the current selection. Invert all bits by,
* XOR with 1s, unused bits will be ignored
*
* \param unused
*/
static void InvertSelection(void* unused)
{
toggleSet ^= ~(0UL);
ParamLoadControls(&toolbarPG);
}
/**
* Handle the ok press. The bit pattern set up from the dialog is converted
* to the pattern used by the toolbar. Then the toolbar is refreshed.
*
* \param unused
*/
static void ToolbarOk(void* unused)
{
toolbarSet = 0;
for (int i = 0; i < COUNTTOOLBARGROUPS; i++) {
if (toggleSet & (1UL << i)) {
toolbarSet = SETBIT(toolbarSet, allToolbarGroups[i].group);
}
}
SaveToolbarConfig();
ToolbarLayout(unused);
MainProc(mainW, wResize_e, NULL, NULL);
wHide(toolbarW);
}
/**
* When selected from the menu the toolbar config dialog is opened. First lazy
* initialization is done on first call. Then the toggle states are set from
* the toolbar configuration bit pattern and the dialog is shown.
*
* \param unused
*/
EXPORT void DoToolbar(void* unused)
{
if (!toolbarW) {
InitializeToolbarDialog();
toolbarW = ParamCreateDialog(&toolbarPG,
MakeWindowTitle(_("Toolbar Options")), _("OK"), ToolbarOk, ParamCancel_Restore,
TRUE, NULL, 0, NULL);
}
toggleSet = 0;
for (int i = 0; i < COUNTTOOLBARGROUPS; i++) {
if (ISBITSET(toolbarSet, allToolbarGroups[i].group)) {
toggleSet = SETBIT(toggleSet, i);
}
}
ParamLoadControls(&toolbarPG);
wShow(toolbarW);
}
/**
* Check whether button group is configured to be visible.
*
* \param group single group to check
* \return true if visible
*/
EXPORT bool
ToolbarIsGroupVisible(int group)
{
CHECK(group > 0);
CHECK(group <= COUNTTOOLBARGROUPS);
return(ISGROUPVISIBLE(group));
}
/**
* Get the current height of the toolbar.
*
* \return
*/
EXPORT wWinPix_t
ToolbarGetHeight(void)
{
return(toolbarHeight);
}
/**
* .
*/
EXPORT void
ToolbarSetHeight(wWinPix_t newHeight)
{
toolbarHeight = newHeight;
}
/**
* Buttons are visible when the command is enabled or when additional
* layer buttons need to be shown.
*
* \param inx
*/
bool
IsButtonVisible(int group, long mode, long options, long layerButtons)
{
if (group == BG_LAYER) {
if (layerButtons < layerCount+FIXEDLAYERCONTROLS) {
return true;
} else {
return false;
}
}
return(IsCommandEnabled(mode, options));
}
/**
* Calculate the position and visibility of a button and display it.
*
* \param inx index into button list
*/
static void ToolbarButtonPlace(struct sToolbarState *tbState, wIndex_t inx)
{
wWinPix_t w, h, offset;
wWinPix_t width;
wWinPix_t gap = GROUP_DISTANCE;
int currentGroup = buttonList[inx].group;
wWinGetSize(mainW, &width, &h);
if (buttonList[inx].control) {
if (tbState->rowHeight <= 0) {
tbState->rowHeight = wControlGetHeight(buttonList[inx].control);
toolbarHeight = tbState->rowHeight + 5;
}
if (currentGroup != tbState->previousGroup) {
for (int i = 0; i < COUNTTOOLBARGROUPS; i++) {
if (allToolbarGroups[i].group == currentGroup &&
allToolbarGroups[i].biggap) {
gap = GROUP_BIG_DISTANCE;
}
}
}
if ((ISGROUPVISIBLE(currentGroup)) &&
IsButtonVisible(currentGroup, programMode,
buttonList[inx].options, tbState->layerButton )) {
if (currentGroup != tbState->previousGroup) {
tbState->nextX += gap;
tbState->previousGroup = currentGroup;
}
w = wControlGetWidth(buttonList[inx].control);
h = wControlGetHeight(buttonList[inx].control);
if (h < tbState->rowHeight) {
offset = (h - tbState->rowHeight) / 2;
h = tbState->rowHeight; //Uniform
} else {
offset = 0;
}
if (inx < buttonCnt - 1 &&
(buttonList[inx + 1].options & IC_ABUT)) {
w += wControlGetWidth(buttonList[inx + 1].control);
}
if (tbState->nextX + w > width - TOOLBAR_MARGIN) {
tbState->nextX = 5;
toolbarHeight += h + 5;
}
if ((currentGroup == BG_LAYER) &&
tbState->layerButton >= FIXEDLAYERCONTROLS &&
GetLayerHidden(tbState->layerButton - FIXEDLAYERCONTROLS)) {
wControlShow(buttonList[inx].control, FALSE);
tbState->layerButton++;
} else {
wWinPix_t newX = tbState->nextX;
wWinPix_t newY = toolbarHeight - (h + 5 + offset);
// count number of shown layer buttons
if (currentGroup == BG_LAYER) {
tbState->layerButton++;
}
if ((newX != buttonList[inx].x) || (newY != buttonList[inx].y)) {
wControlShow(buttonList[inx].control, FALSE);
wControlSetPos(buttonList[inx].control, newX,
newY);
}
buttonList[inx].x = newX;
buttonList[inx].y = newY;
tbState->nextX += wControlGetWidth(buttonList[inx].control);
wControlShow(buttonList[inx].control, TRUE);
}
} else {
wControlShow(buttonList[inx].control, FALSE);
}
}
}
EXPORT void ToolbarLayout(void* data)
{
int inx;
struct sToolbarState state = {
.previousGroup = 0,
.nextX = 0,
.layerButton = 0,
.rowHeight = 0,
};
for (inx = 0; inx < buttonCnt; inx++) {
ToolbarButtonPlace(&state, inx);
}
if (ISBITSET(toolbarSet, BG_HOTBAR)) {
LayoutHotBar(data);
} else {
HideHotBar();
}
}
/**
* Set the 'pressed' state of a toolbar button.
*
* \param button index into button list
* \param busy desired button state
*/
EXPORT void ToolbarButtonBusy(wIndex_t button, wBool_t busy)
{
wButtonSetBusy((wButton_p)buttonList[button].control,
busy);
}
/**
* Set state of a toolbar button .
*
* \param button index into button list
* \param enable desired state, FALSE if disabled, TRUE if enabled
*/
EXPORT void ToolbarButtonEnable(wIndex_t button, wBool_t enable)
{
wControlActive(buttonList[button].control,
enable);
}
/**
* Enable toolbar buttons that depend on selected track.
*
* \param selected true if any track is selected
*/
EXPORT void ToolbarButtonEnableIfSelect(bool selected)
{
for (int inx = 0; inx < buttonCnt; inx++) {
if (buttonList[inx].cmdInx < 0
&& (buttonList[inx].options & IC_SELECTED)) {
ToolbarButtonEnable(inx, selected );
}
}
}
/**
* Place a control onto the toolbar. The control is added to the toolbar
* control list and initially hidden. Placement and visibility is controlled
* by ToolbarButtonPlace()
*
* \param control the control to add
* \param options control options
*/
EXPORT void ToolbarControlAdd(wControl_p control, long options, int cmdGroup)
{
CHECK(buttonCnt < BUTTON_MAX - 1);
buttonList[buttonCnt].enabled = TRUE;
buttonList[buttonCnt].options = options;
buttonList[buttonCnt].group = cmdGroup;
buttonList[buttonCnt].x = 0;
buttonList[buttonCnt].y = 0;
buttonList[buttonCnt].control = control;
buttonList[buttonCnt].cmdInx = -1;
wControlShow(control, FALSE);
buttonCnt++;
}
/**
* Link a command to a specific toolbar button.
*
* \param button the button
* \param command command to activate when button is pressed
* \return
*/
EXPORT void ToolbarButtonCommandLink(wIndex_t button, int command)
{
if (button >= 0 && buttonList[button].cmdInx == -1) {
// set button back-link
buttonList[button].cmdInx = commandCnt;
}
}
/**
* Update the toolbar button for selected command eg. circle vs. filled
* circle.
*
* \param button toolbar button
* \param command current command
* \param icon new icon
* \param helpKey new help key
* \param context new command context
*/
EXPORT void ToolbarUpdateButton(wIndex_t button, wIndex_t command,
char * icon,
const char * helpKey,
void * context)
{
if (buttonList[button].cmdInx != command) {
wButtonSetLabel((wButton_p) buttonList[button].control,icon);
wControlSetHelp(buttonList[button].control,
GetBalloonHelpStr(helpKey));
wControlSetContext(buttonList[button].control,
context);
buttonList[button].cmdInx = command;
}
}
/*--------------------------------------------------------------------*/
/**
* Handle simulated button press during playbook.
*
* \param buttInx selected button
*/
EXPORT void PlaybackButtonMouse(wIndex_t buttInx)
{
wWinPix_t cmdX, cmdY;
coOrd pos;
if (buttInx < 0 || buttInx >= buttonCnt) {
return;
}
if (buttonList[buttInx].control == NULL) {
return;
}
cmdX = buttonList[buttInx].x + 17;
cmdY = toolbarHeight - (buttonList[buttInx].y + 17)
+ (wWinPix_t)(mainD.size.y / mainD.scale * mainD.dpi) + 30;
mainD.Pix2CoOrd(&mainD, cmdX, cmdY, &pos);
MovePlaybackCursor(&mainD, pos, TRUE, buttonList[buttInx].control);
if (playbackTimer == 0) {
wButtonSetBusy((wButton_p)buttonList[buttInx].control, TRUE);
wFlush();
wPause(500);
wButtonSetBusy((wButton_p)buttonList[buttInx].control, FALSE);
wFlush();
}
}
/**
* Handle cursor positioning for toolbar buttons during playback .
*
* \param buttonInx
*/
EXPORT void ToolbarButtonPlayback(wIndex_t buttonInx)
{
wWinPix_t cmdX, cmdY;
coOrd pos;
cmdX = buttonList[buttonInx].x + 17;
cmdY = toolbarHeight - (buttonList[buttonInx].y + 17)
+ (wWinPix_t)(mainD.size.y / mainD.scale * mainD.dpi) + 30;
mainD.Pix2CoOrd(&mainD, cmdX, cmdY, &pos);
MovePlaybackCursor(&mainD, pos, TRUE, buttonList[buttonInx].control);
}
/**
* Save the toolbar setting to the configuration file.
*
*/
static void
SaveToolbarConfig(void)
{
wPrefSetInteger(TOOLBAR_SECTION, TOOLBAR_VARIABLE, toolbarSet);
if (recordF)
fprintf(recordF, "PARAMETER %s %s %ld", TOOLBAR_SECTION,
TOOLBAR_VARIABLE, toolbarSet);
}
/**
* Get the preferences for the toolbar from the configuration file.
* Bits unused are cleared just to be sure;
*
*/
EXPORT void
ToolbarLoadConfig(void)
{
unsigned long maxToolbarSet = (1 << COUNTTOOLBARGROUPS) - 1;
long toolbarSetIni;
wPrefGetInteger(TOOLBAR_SECTION, TOOLBAR_VARIABLE, &toolbarSetIni,
TOOLBARSET_INIT);
toolbarSet = (unsigned long)toolbarSetIni & maxToolbarSet;
// unused but saved to stay compatible
wPrefSetInteger(TOOLBAR_SECTION, "max-toolbarset", maxToolbarSet);
if (recordF)
fprintf(recordF, "PARAMETER %s %s %lX -> %lX", TOOLBAR_SECTION,
TOOLBAR_VARIABLE, toolbarSetIni, toolbarSet);
}
/**
* Initialize toolbar functions.
*
*/
EXPORT void InitToolbar(void)
{
RegisterChangeNotification(ToolbarChange);
ToolbarLoadConfig();
}
|