summaryrefslogtreecommitdiff
path: root/ldmicro/lib/linuxUI/linuxUI.cpp
blob: eca0b50d12d64444be704b763e309ecdb3abf5b2 (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
#include "linuxUI.h"

/// Global variables to hole mouse click positions
int GLOBAL_mouse_last_clicked_x;
int GLOBAL_mouse_last_clicked_y;

/// Brushes
const COLORREF BLACK_BR(0, 0, 0);
const COLORREF WHITE_BR(255, 255, 255);
const COLORREF GRAY_BR(128, 128, 128);
const COLORREF LTGRAY_BR(211, 211, 211);
const COLORREF DKGRAY_BR(169, 169, 169);

/// Variable to current text color
COLORREF HdcCurrentTextColor;

/// Variable to hold timers
std::vector<TimerRecord> timerRecords;

/// EnableMenuItem Variables
const UINT MF_ENABLED = 0;
const UINT MF_GRAYED = 1;
const UINT MF_CHECKED = 2;
const UINT MF_UNCHECKED = 3;

/// ListStore
HWID view;
HTVC column;
 
/// Wraper function for gtk_window_has_toplevel_focus
BOOL GetFocus(HWID window)
{
    // return (BOOL) gtk_window_has_toplevel_focus(GTK_WINDOW(window));
    return TRUE;
}

COLORREF RGB(int red, int green, int blue)
{
    COLORREF col(red, green, blue);
    // col.red = red/255.0;
    // col.green = green/255.0;
    // col.blue = blue/255.0;
    // col.alpha = 1.0;

    return col;
}

int MessageBox(HWID pWindow, char* message, char* title, UINT mFlags, UINT iFlags)
 {
    QMessageBox msg((QMessageBox::Icon)iFlags, title, message, (QMessageBox::StandardButton)mFlags, pWindow);
    return msg.exec();
 }


BOOL GetSaveFileName(OPENFILENAME *ofn)
{
    std::string strFilter;
    DWORD strFilterLen = 0;
    BOOL filterResetFlag = FALSE;
    char filename[15] = "Untitled";

    if (ofn->lpstrDefExt != NULL)
        sprintf(filename, "Untitled.%s", ofn->lpstrDefExt);
    
    while(!((ofn->lpstrFilter[strFilterLen] == '\0') &&
        (ofn->lpstrFilter[strFilterLen + 1] == '\0')))
    {
        if(filterResetFlag)
        {
            strFilter = strFilter + "(";
            strFilter.append(&ofn->lpstrFilter[strFilterLen]);
            strFilter = strFilter + ");;";
            filterResetFlag = FALSE;
        }
        else
        {
            strFilter.append(&ofn->lpstrFilter[strFilterLen]);
            filterResetFlag = TRUE;
        }
        strFilterLen = strFilterLen + strlen(&ofn->lpstrFilter[strFilterLen]) +1;
    }
    
    QString file = QFileDialog::getSaveFileName(ofn->parentWindow, ofn->lpstrTitle,
        QStandardPaths::locate(QStandardPaths::HomeLocation,"",
            QStandardPaths::LocateDirectory)+ filename,
        strFilter.c_str());
    BOOL exitStatus;
    file.isEmpty() ? exitStatus = FALSE : exitStatus = TRUE;

    if (exitStatus)
    {
        strcpy(ofn->lpstrFile, file.toStdString().c_str());
    }

    return exitStatus;
}

BOOL GetOpenFileName(OPENFILENAME *ofn)
{
    std::string strFilter;
    DWORD strFilterLen = 0;
    BOOL filterResetFlag = FALSE;
    while(!((ofn->lpstrFilter[strFilterLen] == '\0') &&
        (ofn->lpstrFilter[strFilterLen + 1] == '\0')))
    {
        if(filterResetFlag)
        {
            strFilter = strFilter + "(";
            strFilter.append(&ofn->lpstrFilter[strFilterLen]);
            strFilter = strFilter + ");;";
            filterResetFlag = FALSE;
        }
        else
        {
            strFilter.append(&ofn->lpstrFilter[strFilterLen]);
            filterResetFlag = TRUE;
        }
        strFilterLen = strFilterLen + strlen(&ofn->lpstrFilter[strFilterLen]) +1;
    }
    // printf("patterns:%s\n",strFilter.c_str() );
    QString filename = QFileDialog::getOpenFileName(ofn->parentWindow, ofn->lpstrTitle,
        QStandardPaths::locate(QStandardPaths::HomeLocation,".",
            QStandardPaths::LocateDirectory),
        strFilter.c_str());
    if(filename == NULL)
    {
        return FALSE;
    }
    
        strcpy(ofn->lpstrFile,filename.toStdString().c_str());
        // printf("FileName:%s",ofn->lpstrFile);

    return TRUE;
}


void EnableMenuItem(HMENU MenuName, QAction* MenuItem, UINT CheckEnabledItem) 
{
    switch (CheckEnabledItem){
        case MF_ENABLED :
           MenuItem->setEnabled(true);
           // MenuItem->blockSignals(false);
        break;
        case MF_GRAYED :
           MenuItem->setEnabled(false);
           // MenuItem->blockSignals(true);
        break; 
    }
}

// Special function designed for qt, since disabling top-level menu
// Does not disable its child menus. They can still be accessed via
// keyboard shortcuts
void EnableMenuItem(HMENU MenuName, HMENU MenuItem, UINT CheckEnabledItem) 
{
    QList<QAction *> MenuList = MenuItem->actions();
    QList<QAction *>::iterator item = MenuList.begin();
    switch (CheckEnabledItem){
        case MF_ENABLED :
            while((item != MenuList.end()))/* || !(MenuList->isEmpty))*/
            {
                (*item)->setEnabled(true);
                (*item)->blockSignals(false);
                item++;
            }
           MenuItem->setEnabled(true);
           MenuItem->blockSignals(false);
        break;
        case MF_GRAYED :
            while((item != MenuList.end()))/* || !(MenuList->isEmpty))*/
            {
                (*item)->setEnabled(false);
                (*item)->blockSignals(true);
                item++;
            }
           MenuItem->setEnabled(false);
           MenuItem->blockSignals(true);
        break; 
    }
}

void CheckMenuItem(HMENU MenuName, QAction* MenuItem, UINT Check)
{
    switch (Check){
        case MF_CHECKED :
            MenuItem->setChecked(true);
        break;
        case MF_UNCHECKED :
            MenuItem->setChecked(false);
        break;
    }
}

HANDLE GetStockObject(int fnObject)
{
    switch(fnObject)
    {
        case BLACK_BRUSH:
            return (HANDLE)&BLACK_BR;
            break;
        case WHITE_BRUSH:
            return (HANDLE)&WHITE_BR;
            break;
        case GRAY_BRUSH:
            return (HANDLE)&GRAY_BR;
            break;
        case LTGRAY_BRUSH:
            return (HANDLE)&LTGRAY_BR;
            break;
        case DKGRAY_BRUSH:
            return (HANDLE)&DKGRAY_BR;
            break;
        default:
            return (HANDLE)&WHITE_BR;
    }
}

void SelectObject(HCRDC hcr, HFONT hfont)
{
    if (hcr ==NULL)
        return;
    QFont qtfont = hcr->font();
    qtfont.setFamily(hfont->lpszFace);
    qtfont.setPixelSize(hfont->nHeight - 3);
    qtfont.setFixedPitch(TRUE);
    qtfont.setStyle(hfont->fdwItalic ? QFont::StyleItalic : QFont::StyleNormal);
    qtfont.setWeight(hfont->fnWeight == FW_BOLD ? QFont::Bold : QFont::Normal);
    hcr->setFont(qtfont);
}

HBRUSH CreateBrushIndirect(PLOGBRUSH plb)
{
    COLORREF* brush = new COLORREF;
    brush->setRgb(  plb->lbColor.red(), 
                    plb->lbColor.green(),
                    plb->lbColor.blue(),
                    (plb->lbStyle == BS_SOLID) ? 255 : 51);

    return brush;
}

HFONT CreateFont(int nHeight, int nWidth, int nOrientation, int fnWeight,
    DWORD fdwItalic, LPCTSTR lpszFace)
{
    HFONT font = (HFONT)malloc(strlen(lpszFace) + 1 + sizeof(FONT));
    font->nHeight = nHeight;
    font->nWidth = nWidth;
    font->nOrientation = nOrientation;
    font->fnWeight = fnWeight;
    font->fdwItalic = fdwItalic;
    font->lpszFace = (char*)malloc(strlen(lpszFace)+1);
    strcpy(font->lpszFace, lpszFace);
    
    return font;
}

void SetBkColor(HWID widget, HCRDC hcr, COLORREF bkCol)
{
    QPalette pal = widget->palette();
    pal.setColor(QPalette::Background, bkCol);
    widget->setPalette(pal);
}

void SetTextColor(HCRDC hcr, COLORREF color)
{
    if (hcr == NULL)
        return;
    QPen qtpen = hcr->pen();
    qtpen.setColor(color);
    hcr->setPen(qtpen);
    HdcCurrentTextColor = color;
    // gdk_cairo_set_source_rgba (hcr, &color);
}

void TextOut(HWID hWid, HCRDC hcr, int nXStart, int nYStart, LPCTSTR lpString, int cchString)
{
    if (hcr == NULL)
        return;
    int width = hWid->width();
    int height = hWid->height();
    BOOL resize_flag = FALSE;
    QFont newFont= hcr->font();
    // newFont
    /*if(nYStart+(extents.height/2.0) >= height)
    {
        height += extents.height + 50;
        resize_flag = TRUE;
    }
    
    if (nXStart+(extents.width/2.0) >= width)
    {
        width += extents.width;
        resize_flag = TRUE;
    }*/
    char* text = (char*)malloc(cchString);
    strncpy(text, lpString, cchString);
    text[cchString] = '\0';

    hcr->drawText(nXStart, nYStart, (QString)text);
    // cairo_move_to(hcr, nXStart, nYStart);
    // cairo_show_text(hcr, text);

    // cairo_fill (hcr);

    /*if (resize_flag)  // To do later
        hcr->setWindow();*/
    /*if (hcr == NULL)
        return;
    
    nYStart += 30;
    
    cairo_text_extents_t extents;
    cairo_text_extents (hcr, lpString, &extents);
    int width = gtk_widget_get_allocated_width (hWid);
    int height= gtk_widget_get_allocated_height (hWid);
    BOOL resize_flag = FALSE;

    if(nYStart+(extents.height/2.0) >= height)
    {
        height += extents.height + 50;
        resize_flag = TRUE;
    }
    
    if (nXStart+(extents.width/2.0) >= width)
    {
        width += extents.width;
        resize_flag = TRUE;
    }

    if (resize_flag)
        gtk_widget_set_size_request(hWid, width, height);
    
    char* text = (char*)malloc(cchString);
    strncpy(text, lpString, cchString);
    text[cchString] = '\0';

    cairo_move_to(hcr, nXStart, nYStart);
    cairo_show_text(hcr, text);

    cairo_fill (hcr);*/
}

COLORREF GetTextColor(HCRDC Hdc)
{
    // COLORREF col;
    // gtk_style_context_get_color (Hdc,
    //                             gtk_style_context_get_state (Hdc),
    //                             &col);
    
    return HdcCurrentTextColor;
}

BOOL InvalidateRect(HWID hWid, const RECT *lpRect, BOOL bErase)
{
    /*if(!GDK_IS_WINDOW(hWid))
        return FALSE;

    if (lpRect == NULL)
    {
        gdk_window_invalidate_rect (gtk_widget_get_window (hWid), NULL, FALSE);
        return TRUE;
    }

    GDRECT Gdrect;
    RECT_to_GDRECT(lpRect, &Gdrect);
    // gtk_widget_queue_draw(hWid);
    gdk_window_invalidate_rect (gtk_widget_get_window (hWid), &Gdrect, FALSE);
    */
    hWid->repaint();
    return TRUE;
}

int FillRect(HCRDC hDC, const QRect *lprc, HBRUSH hbr)
{
    if (hDC == NULL)
        return -1;
    QBrush curbrush = hDC->brush();
    curbrush.setColor(*hbr);
    curbrush.setStyle(Qt::SolidPattern);
    hDC->setBrush(curbrush);
    hDC->drawRect(*lprc);
    /*
    GDRECT gdrc;
    RECT_to_GDRECT(lprc, &gdrc);

    cairo_set_source_rgb(hDC, hbr->red, hbr->green, hbr->blue);
    cairo_rectangle(hDC, gdrc.x, gdrc.y, gdrc.width, gdrc.height);
    cairo_stroke_preserve(hDC);
    cairo_fill(hDC);*/
    
    return 0;
}

UINT SetTimer(HWID hWid, UINT  nIDEvent, UINT uElapse, UINT TimerID)
{
    if(TimerID != NULL)
        return nIDEvent;
    switch(nIDEvent)
    {
        case TIMER_BLINK_CURSOR:
        {
            TimerID = hWid->startTimer(uElapse);
            CursorObject = new QGroupBox(hWid);
            QPalette pal = CursorObject->palette();
            pal.setColor(QPalette::Background, Qt::white);
            CursorObject->setAutoFillBackground(true);
            CursorObject->setPalette(pal);
            CursorObject->setGeometry(0,0,2,20);
        }
        break;
        
        case TIMER_SIMULATE:
        {
            TimerID = hWid->startTimer(uElapse);
        }
        break;
    }
    // if(hWid!=NULL)
    //     CursorObject->setVisible(TRUE);

    return TimerID;
    // auto record_it = std::find_if(timerRecords.begin(), timerRecords.end(),  [&nIDEvent](TimerRecord &Record) { return Record.ufID == nIDEvent; });

    // if (record_it != timerRecords.end())
    //     return 0;

    // TimerRecord tr;
    // tr.pfun = lpTimerFunc;
    // tr.ufID = nIDEvent;
    // tr.utID = g_timeout_add(uElapse, (GSourceFunc)lpTimerFunc, FALSE);

    // timerRecords.push_back(tr);
    // return tr.utID;
}

BOOL KillTimer(HWID hWid, UINT uIDEvent)
{
    /*auto record_it = std::find_if(timerRecords.begin(), timerRecords.end(),  [&uIDEvent](TimerRecord &Record) { return Record.ufID == uIDEvent; });

    if (record_it == timerRecords.end())
        return FALSE;
    
    record_it->pfun(TRUE);
    g_source_remove (record_it->utID);
    timerRecords.erase(record_it);*/
    // printf("KillTimer\n");
    switch(uIDEvent)
    {
        case TIMER_BLINK_CURSOR:
            hWid->killTimer(CursorTimer);
            CursorTimer = NULL;
            CursorObject->setVisible(FALSE);
        break;

        case TIMER_SIMULATE:
            hWid->killTimer(SimulateTimer);
            SimulateTimer = NULL;
        break;
    }

    return TRUE;
}