summaryrefslogtreecommitdiff
path: root/ldmicro/simpledialog.cpp
blob: f1ebd28bf7189e4fe63a76ad79c46fb9487814ea (plain)
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
//-----------------------------------------------------------------------------
// Copyright 2007 Jonathan Westhues
//
// This file is part of LDmicro.
// 
// LDmicro 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 3 of the License, or
// (at your option) any later version.
// 
// LDmicro 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 LDmicro.  If not, see <http://www.gnu.org/licenses/>.
//------
//
// All the simple dialogs that just require a row of text boxes: timer name
// and delay, counter name and count, move and arithmetic destination and
// operands. Try to reuse code a bit.
// Jonathan Westhues, Nov 2004
//-----------------------------------------------------------------------------
#include "linuxUI.h"
//#include <commctrl.h>
#include <stdio.h>
#include <stdlib.h>

#include "ldmicro.h"

static QDialog*     SimpleDialog;

#define MAX_BOXES 5

static QLineEdit*   Textboxes[MAX_BOXES];
static QLabel*      Labels[MAX_BOXES];

static QGridLayout* SimpleGrid;

/*static LONG_PTR PrevAlnumOnlyProc[MAX_BOXES];
static LONG_PTR PrevNumOnlyProc[MAX_BOXES];*/

static BOOL NoCheckingOnBox[MAX_BOXES];

/*static BOOL SIMPLE_DIALOG_ACTIVE = FALSE;*/

/*static SimpleDialogData SDdata;*/

/// Simple dialog data flags
#define SD_TIMER            0x0000001
#define SD_COUNTER          0x0000002
#define SD_CMP              0x0000003
#define SD_MOVE             0x0000004
#define SD_READ_ADC         0x0000005
#define SD_SET_PWM          0x0000006
#define SD_UART             0x0000007
#define SD_MATH             0x0000008
#define SD_SHIFT_REGISTER   0x0000009
#define SD_FORMATTED_STRING 0x0000010
#define SD_PERSIST          0x0000011
/*
//-----------------------------------------------------------------------------
// Don't allow any characters other than -A-Za-z0-9_ in the box.
//-----------------------------------------------------------------------------
static void MyAlnumOnlyProc (GtkEditable *editable, gchar *NewText, gint length, 
    gint *position, gpointer data)
{
    for (int i = 0; i < length; i++)
    {
        if (!(isalpha (NewText[i]) || NewText[i] == '_' || isdigit (NewText[i]) ||
                NewText[i] == '\b' || NewText[i] == '\'' || NewText[i] == '-'))
        {
            g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
            return;
        }
    }
    // if(msg == WM_CHAR) {
    //     if(!(isalpha(wParam) || isdigit(wParam) || wParam == '_' ||
    //         wParam == '\b' || wParam == '-' || wParam == '\''))
    //     {
    //         return 0;
    //     }
    // }

    // int i;
    // for(i = 0; i < MAX_BOXES; i++) {
    //     if(hwnd == Textboxes[i]) {
    //         return CallWindowProc((WNDPROC)PrevAlnumOnlyProc[i], hwnd, msg, 
    //             wParam, lParam);
    //     }
    // }
    // oops();
}

//-----------------------------------------------------------------------------
// Don't allow any characters other than -0-9. in the box.
//-----------------------------------------------------------------------------
static void MyNumOnlyProc (GtkEditable *editable, gchar *NewText, gint length, 
    gint *position, gpointer data)
{
    for (int i = 0; i < length; i++)
    {
        if (!(isdigit (NewText[i]) || NewText[i] == '\b' || 
            NewText[i] == '.' || NewText[i] == '-'))
        {
            g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
            return;
        }
    }
    // if(msg == WM_CHAR) {
    //     if(!(isdigit(wParam) || wParam == '.' || wParam == '\b' 
    //         || wParam == '-'))
    //     {
    //         return 0;
    //     }
    // }

    // int i;
    // for(i = 0; i < MAX_BOXES; i++) {
    //     if(hwnd == Textboxes[i]) {
    //         return CallWindowProc((WNDPROC)PrevNumOnlyProc[i], hwnd, msg, 
    //             wParam, lParam);
    //     }
    // }
    // oops();
}*/

inline void DestroyWindow (int boxes){
    for(int i = 0; i < boxes; i++) {
        delete Labels[i];
        delete Textboxes[i];
    }
    delete SimpleGrid;
    delete SimpleDialog;
    ProgramChanged();
}

static void MakeControls(int boxes, char **labels, DWORD fixedFontMask)
{
    int i;
    // HDC hdc = GetDC(SimpleDialog);
    // SelectObject(hdc, MyNiceFont);

    // SIZE si;

    // int maxLen = 0;
    // for(i = 0; i < boxes; i++) {
    //     GetTextExtentPoint32(hdc, labels[i], strlen(labels[i]), &si);
    //     if(si.cx > maxLen) maxLen = si.cx;
    // }

    // int adj;
    // if(maxLen > 70) {
    //     adj = maxLen - 70;
    // } else {
    //     adj = 0;
    // }
    QGridLayout* grid = new QGridLayout();
    QDialogButtonBox *ButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok
        | QDialogButtonBox::Cancel, Qt::Vertical,
        SimpleDialog);
    NiceFont(SimpleDialog);
    
    for(i = 0; i < boxes; i++) {
        // GetTextExtentPoint32(hdc, labels[i], strlen(labels[i]), &si);

        Labels[i] = new QLabel(labels[i]);
        NiceFont(Labels[i]);
        grid->addWidget(Labels[i],i,0);

        Textboxes[i] = new QLineEdit();

        if(fixedFontMask & (1 << i)) {
            FixedFont(Textboxes[i]);
        } else {
            NiceFont(Textboxes[i]);
        }
        grid->addWidget(Textboxes[i],i,1);
    }
    SimpleGrid->addLayout(grid, 0,0);
    SimpleGrid->addWidget(ButtonBox, 0, 1);
    QObject::connect(ButtonBox, SIGNAL(accepted()), SimpleDialog, SLOT(accept()));
    QObject::connect(ButtonBox, SIGNAL(rejected()), SimpleDialog, SLOT(reject()));
}
/*
void SimpleDialogWrapUp()
{
    // if(!didCancel)
    for(int i = 0; i < SDdata.boxes; i++) {
        if(NoCheckingOnBox[i]) {
            // char get[64];
            // SendMessage(Textboxes[i], WM_GETTEXT, 60, (LPARAM)get);
            char *get = (char*)gtk_entry_get_text (GTK_ENTRY(Textboxes[i]));
            strcpy(SDdata.dests[i], get);
            if (strlen(get) < 60)
                strcpy(SDdata.dests[i], get);
            else
            {
                strncpy(SDdata.dests[i], get, 60);
                SDdata.dests[i][60] = '\0';
            }
        } else {
            char get[20];
            // SendMessage(Textboxes[i], WM_GETTEXT, 15, (LPARAM)get);
            char *str = (char*)gtk_entry_get_text (GTK_ENTRY(Textboxes[i]));
            strcpy(get, str);
            if (strlen(str) < 15)
                strcpy(get, str);
            else
            {
                strncpy(get, str, 15);
                get[15] = '\0';
            }

            if( (!strchr(get, '\'')) ||
                    (get[0] == '\'' && get[2] == '\'' && strlen(get)==3) )
            {
                if(strlen(get) == 0) {
                    Error(_("Empty textbox; not permitted."));
                } else {
                    strcpy(SDdata.dests[i], get);
                }
            } else {
                Error(_("Bad use of quotes: <%s>"), get);
            }
        }
    }

    switch(SDdata.uflag)
    {
        case SD_TIMER:
        {
            SDdata.str1[0] = 'T';
            strcpy(SDdata.str1+1, SDdata.dests[0]);
            //g_print("%s, %s\n", SDdata.str1, SDdata.dests[0]);
            double del = atof(SDdata.dests[1]);
            if(del > 2140000) { // 2**31/1000, don't overflow signed int
                Error(_("Delay too long; maximum is 2**31 us."));
            } else if(del <= 0) {
                Error(_("Delay cannot be zero or negative."));
            } else {
                *SDdata.num1 = (int)(1000*del + 0.5);
            }
            break;
        }
        case SD_COUNTER:
        {
            *SDdata.num1 = atoi(SDdata.dests[1]);
            break;
        }
        case SD_CMP:
        {
            break;
        }
        case SD_MOVE:
        {
            break;
        }
        case SD_READ_ADC:
        {
            break;
        }
        case SD_SET_PWM:
        {
            *SDdata.num1 = atoi(SDdata.dests[1]);
            break;
        }
        case SD_UART:
        {
            break;
        }
        case SD_MATH:
        {
            break;
        }
        case SD_SHIFT_REGISTER:
        {
            *SDdata.num1 = atoi(SDdata.dests[1]);

            if(*SDdata.num1 <= 0 || *SDdata.num1 >= 200) 
            {
                Error(_("Not a reasonable size for a shift register."));
                *SDdata.num1 = 1;
            }
            break;
        }
        case SD_FORMATTED_STRING:
        {
            break;
        }
        case SD_PERSIST:
        {
            break;
        }
    }

    DestroyWindow(SimpleDialog);
    ProgramChanged();
    SIMPLE_DIALOG_ACTIVE = FALSE;
}

void SimpleDialogCancelProc()
{
    DestroyWindow(SimpleDialog);
    ProgramChanged();
    SIMPLE_DIALOG_ACTIVE = FALSE;
}

static gboolean SimpleDialogKeyPressProc(HWID widget, GdkEventKey* event, gpointer data)
{
    if(event -> keyval == GDK_KEY_Return) 
    {
        // DialogDone = TRUE;
        SimpleDialogWrapUp();
    } else if(event -> keyval == GDK_KEY_Escape) 
    {
        // DialogDone = TRUE;
        // DialogCancel = TRUE;
        SimpleDialogCancelProc();
    }

    return FALSE;
}*/

BOOL ShowSimpleDialog(char *title, int boxes, char **labels, DWORD numOnlyMask,
    DWORD alnumOnlyMask, DWORD fixedFontMask, char **dests)
{
    /*if(SIMPLE_DIALOG_ACTIVE)
        return;
    
    SIMPLE_DIALOG_ACTIVE = TRUE;

    BOOL didCancel = FALSE;*/
    BOOL RetVal = FALSE;

    if(boxes > MAX_BOXES) oops();

    SimpleDialog = CreateWindowClient(title, 100, 100,
        304, 15 + 30*(boxes < 2 ? 2 : boxes), MainWindow);
    SimpleGrid = new QGridLayout(SimpleDialog);
    SimpleDialog->setWindowTitle(title);
    MakeControls(boxes, labels, fixedFontMask);
    int i;
    for(i = 0; i < boxes; i++) {
        Textboxes[i]->setText(dests[i]);

        if(numOnlyMask & (1 << i)) {
            Textboxes[i]->setValidator(
        new QRegExpValidator(QRegExp("-?[0-9]+[.]?[0-9]+")));
        }
        if(alnumOnlyMask & (1 << i)) {
            /*PrevAlnumOnlyProc[i] = SetWindowLongPtr(Textboxes[i], GWLP_WNDPROC, 
                (LONG_PTR)MyAlnumOnlyProc);*/
            Textboxes[i]->setValidator(
                new QRegExpValidator(
                    QRegExp("[a-zA-Z0-9_'-]+")));
        }
    }
    int ret = SimpleDialog->exec();
    switch(ret)
    {
        case QDialog::Accepted:
        {
            for(i = 0; i < boxes; i++) {
                if(NoCheckingOnBox[i]) {
                    strncpy(dests[i],
                        Textboxes[i]->text().toStdString().c_str(),60);
                } else {
                    char get[20];
                    // SendMessage(Textboxes[i], WM_GETTEXT, 15, (LPARAM)get);
                    strncpy(get, Textboxes[i]->text().toStdString().c_str(),15);

                    if( (!strchr(get, '\'')) ||
                            (get[0] == '\'' && get[2] == '\'' && strlen(get)==3) )
                    {
                        if(strlen(get) == 0) {
                            Error(_("Empty textbox; not permitted."));
                        } else {
                            strcpy(dests[i], get);
                        }
                    } else {
                        Error(_("Bad use of quotes: <%s>"), get);
                    }
                }
            }
            RetVal = TRUE;
            break;
        }
        case  QDialog::Rejected:
        RetVal = FALSE;
        break;
    }
    DestroyWindow(boxes);
    return RetVal;
}

void ShowTimerDialog(int which, int *delay, char *name)
{
    char *s;
    switch(which) { 
        case ELEM_TON: s = _("Turn-On Delay"); break;
        case ELEM_TOF: s = _("Turn-Off Delay"); break;
        case ELEM_RTO: s = _("Retentive Turn-On Delay"); break;
        default: oops(); break;
    }
   
    char *labels[] = { _("Name:"), _("Delay (ms):") };

    char delBuf[16];
    char nameBuf[16];
    sprintf(delBuf, "%.3f", (*delay / 1000.0));
    strcpy(nameBuf, name+1);
    char *dests[] = { nameBuf, delBuf };
    if(ShowSimpleDialog(s, 2, labels, (1 << 1), (1 << 0), (1 << 0), dests)) {
        name[0] = 'T';
        strcpy(name+1, nameBuf);
        double del = atof(delBuf);
        if(del > 2140000) { // 2**31/1000, don't overflow signed int
            Error(_("Delay too long; maximum is 2**31 us."));
        } else if(del <= 0) {
            Error(_("Delay cannot be zero or negative."));
        } else {
            *delay = (int)(1000*del + 0.5);
        }
    }
}

void ShowCounterDialog(int which, int *maxV, char *name)
{
    char *title;

    switch(which) {
        case ELEM_CTU:  title = _("Count Up"); break;
        case ELEM_CTD:  title = _("Count Down"); break;
        case ELEM_CTC:  title = _("Circular Counter"); break;

        default: oops();
    }

    char *labels[] = { _("Name:"), (which == ELEM_CTC ? _("Max value:") : 
        _("True if >= :")) };
    char maxS[128];
    sprintf(maxS, "%d", *maxV);
    char *dests[] = { name+1, maxS };
    ShowSimpleDialog(title, 2, labels, 0x2, 0x1, 0x1, dests);
    *maxV = atoi(maxS);
}

void ShowCmpDialog(int which, char *op1, char *op2)
{
    char *title;
    char *l2;
    switch(which) {
        case ELEM_EQU:
            title = _("If Equals");
            l2 = "= :";
            break;

        case ELEM_NEQ:
            title = _("If Not Equals");
            l2 = "/= :";
            break;

        case ELEM_GRT:
            title = _("If Greater Than");
            l2 = "> :";
            break;

        case ELEM_GEQ:
            title = _("If Greater Than or Equal To");
            l2 = ">= :";
            break;

        case ELEM_LES:
            title = _("If Less Than");
            l2 = "< :";
            break;

        case ELEM_LEQ:
            title = _("If Less Than or Equal To");
            l2 = "<= :";
            break;

        default:
            oops();
    }
    char *labels[] = { _("'Closed' if:"), l2 };
    char *dests[] = { op1, op2 };
    ShowSimpleDialog(title, 2, labels, 0, 0x3, 0x3, dests);
}

void ShowMoveDialog(char *dest, char *src)
{
    char *labels[] = { _("Destination:"), _("Source:") };
    char *dests[] = { dest, src };
    ShowSimpleDialog(_("Move"), 2, labels, 0, 0x3, 0x3, dests);
}

void ShowReadAdcDialog(char *name)
{
    char *labels[] = { _("Destination:") };
    char *dests[] = { name };
    ShowSimpleDialog(_("Read A/D Converter"), 1, labels, 0, 0x1, 0x1, dests);
}

void ShowSetPwmDialog(char *name, int *targetFreq)
{
    char freq[100];
    sprintf(freq, "%d", *targetFreq);

    char *labels[] = { _("Duty cycle var:"), _("Frequency (Hz):") };
    char *dests[] = { name, freq };
    ShowSimpleDialog(_("Set PWM Duty Cycle"), 2, labels, 0x2, 0x1, 0x1, dests);
    *targetFreq = atoi(freq);
}

void ShowUartDialog(int which, char *name)
{
    char *labels[] = { (which == ELEM_UART_RECV) ? _("Destination:") :
        _("Source:") };
    char *dests[] = { name };

    ShowSimpleDialog((which == ELEM_UART_RECV) ? _("Receive from UART") :
        _("Send to UART"), 1, labels, 0, 0x1, 0x1, dests);
}

void ShowMathDialog(int which, char *dest, char *op1, char *op2)
{
    char *l2, *title;
    if(which == ELEM_ADD) {
        l2 = "+ :";
        title = _("Add");
    } else if(which == ELEM_SUB) {
        l2 = "- :";
        title = _("Subtract");
    } else if(which == ELEM_MUL) {
        l2 = "* :";
        title = _("Multiply");
    } else if(which == ELEM_DIV) {
        l2 = "/ :";
        title = _("Divide");
    } else oops();

    char *labels[] = { _("Destination:"), _("is set := :"), l2 };
    char *dests[] = { dest, op1, op2 };
    ShowSimpleDialog(title, 3, labels, 0, 0x7, 0x7, dests);
}

void ShowShiftRegisterDialog(char *name, int *stages)
{
    char stagesStr[20];
    sprintf(stagesStr, "%d", *stages);

    char *labels[] = { _("Name:"), _("Stages:") };
    char *dests[] = { name, stagesStr };

    ShowSimpleDialog(_("Shift Register"), 2, labels, 0x2, 0x1, 0x1, dests);
    *stages = atoi(stagesStr);

    if(*stages <= 0 || *stages >= 200) {
        Error(_("Not a reasonable size for a shift register."));
        *stages = 1;
    }
}

void ShowFormattedStringDialog(char *var, char *string)
{
    char *labels[] = { _("Variable:"), _("String:") };
    char *dests[] = { var, string };
    NoCheckingOnBox[0] = TRUE;
    NoCheckingOnBox[1] = TRUE;
    ShowSimpleDialog(_("Formatted String Over UART"), 2, labels, 0x0,
        0x1, 0x3, dests);
    NoCheckingOnBox[0] = FALSE;
    NoCheckingOnBox[1] = FALSE;
}

void ShowPersistDialog(char *var)
{
    char *labels[] = { _("Variable:") };
    char *dests[] = { var };
    ShowSimpleDialog(_("Make Persistent"), 1, labels, 0, 1, 1, dests);
}