summaryrefslogtreecommitdiff
path: root/app/wlib/gtklib/util.c
blob: e6587a0cf3688c7f5f67607917e858439893ecb1 (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
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/** \file util.c
 * Some basic window functions
 */

/*  XTrkCad - Model Railroad CAD
 *  Copyright (C) 2005 Dave Bullis
 *
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>

#define GTK_DISABLE_SINGLE_INCLUDES
#define GDK_DISABLE_DEPRECATED
#define GTK_DISABLE_DEPRECATED
#define GSEAL_ENABLE

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>

#include "gtkint.h"
#include "i18n.h"

wWin_p gtkMainW;

long debugWindow = 0;

char wConfigName[ 256 ];

const char * wNames[] = {
    "MAIN",
    "POPUP",
    "BUTT",
    "CANCEL",
    "POPUP",
    "TEXT",
    "INTEGER",
    "FLOAT",
    "LIST",
    "DROPLIST",
    "COMBOLIST",
    "RADIO",
    "TOGGLE",
    "DRAW",
    "MENU"
    "MULTITEXT",
    "MESSAGE",
    "LINES",
    "MENUITEM",
    "BOX"
};


static wBool_t reverseIcon =
#if defined(linux)
    FALSE;
#else
    TRUE;
#endif



/*
 *****************************************************************************
 *
 * Internal Utility functions
 *
 *****************************************************************************
 */

/**
 * Create a pixbuf from a two colored bitmap in XBM format.
 *
 * \param ip the XBM data
 * \returns the pixbuf
 */

GdkPixbuf* wlibPixbufFromXBM(
                             wIcon_p ip)
{
    GdkPixbuf * pixbuf;

    char line0[40];
    char line2[40];

    char ** pixmapData;
    int row, col, wb;
    long rgb;
    const char * bits;

    wb = (ip->w + 7) / 8;
    pixmapData = (char**) malloc((3 + ip->h) * sizeof *pixmapData);
    pixmapData[0] = line0;
    rgb = wDrawGetRGB(ip->color);
    sprintf(line0, " %d %d 2 1", ip->w, ip->h);
    sprintf(line2, "# c #%2.2lx%2.2lx%2.2lx", (rgb >> 16)&0xFF, (rgb >> 8)&0xFF,
            rgb & 0xFF);
    pixmapData[1] = ". c None s None";
    pixmapData[2] = line2;
    bits = ip->bits;

    for (row = 0; row < ip->h; row++) {
        pixmapData[row + 3] = (char*) malloc((ip->w + 1) * sizeof **pixmapData);

        for (col = 0; col < ip->w; col++) {
            if (bits[ row * wb + (col >> 3) ] & (1 << (col & 07))) {
                pixmapData[row + 3][col] = '#';
            }
            else {
                pixmapData[row + 3][col] = '.';
            }
        }
        pixmapData[row + 3][ip->w] = 0;
    }

    pixbuf = gdk_pixbuf_new_from_xpm_data((const char **) pixmapData);

    for (row = 0; row < ip->h; row++) {
        free(pixmapData[row + 3]);
    }
    free(pixmapData);
    return pixbuf;
}

/**
 * Add a label to an existing widget
 *
 * \param b IN widget
 * \param labelStr IN label to add
 * \returns size of label
 */

int wlibAddLabel(wControl_p b, const char * labelStr)
{
    GtkRequisition requisition, reqwidget;

    if (labelStr == NULL) {
        return 0;
    }

    b->label = gtk_label_new(wlibConvertInput(labelStr));
    gtk_widget_size_request(b->label, &requisition);
    if (b->widget)
       	gtk_widget_size_request(b->widget, &reqwidget);
    else
       	reqwidget.height = requisition.height;
    gtk_container_add(GTK_CONTAINER(b->parent->widget), b->label);
    gtk_fixed_move(GTK_FIXED(b->parent->widget), b->label,
                   b->realX - requisition.width - 8, b->realY + (reqwidget.height/2 - requisition.height/2));
    gtk_widget_show(b->label);
    return requisition.width + 8;
}

/**
 * Allocate and initialize the data structure for a new widget
 *
 * \param parent IN parent window
 * \param type IN type of new widget
 * \param origX IN x position
 * \param origY IN y position
 * \param labelStr IN text label
 * \param size IN size
 * \param data IN user data to keep with widget
 * \returns
 */

void * wlibAlloc(
                 wWin_p parent,
                 wType_e type,
                 wPos_t origX,
                 wPos_t origY,
                 const char * labelStr,
                 int size,
                 void * data)
{
    wControl_p w = (wControl_p) malloc(size);
    char * cp;
    memset(w, 0, size);

    if (w == NULL) {
        abort();
    }

    w->type = type;
    w->parent = parent;
    w->origX = origX;
    w->origY = origY;

    if (labelStr) {
        cp = (char*) malloc(strlen(labelStr) + 1);
        w->labelStr = cp;

        for (; *labelStr; labelStr++)
            if (*labelStr != '&') {
                *cp++ = *labelStr;
            }

        *cp = 0;
    }

    w->doneProc = NULL;
    w->data = data;
    return w;
}

/**
 * Calculate the position for a widget
 *
 * \param b IN widget
 */

void wlibComputePos(
                    wControl_p b)
{
    wWin_p w = b->parent;

    if (b->origX >= 0) {
        b->realX = b->origX;
    }
    else {
        b->realX = w->lastX + (-b->origX) - 1;
    }

    if (b->origY >= 0) {
        b->realY = b->origY + BORDERSIZE + ((w->option & F_MENUBAR) ? w->menu_height : 0);
    }
    else {
        b->realY = w->lastY + (-b->origY) - 1;
    }
}

/**
 * Initialize the internal structure with the size of the widget
 *
 * \param b IN widget
 */

void wlibControlGetSize(
                        wControl_p b)
{
    GtkRequisition requisition;
    gtk_widget_size_request(b->widget, &requisition);
    b->w = requisition.width;
    b->h = requisition.height;
}

/**
 * ???
 * \param b IN widget
 */

void wlibAddButton(
                   wControl_p b)
{
    wWin_p win = b->parent;
    wBool_t resize = FALSE;

    if (win->first == NULL) {
        win->first = b;
    }
    else {
        win->last->next = b;
    }

    win->last = b;
    b->next = NULL;
    b->parent = win;
    win->lastX = b->realX + b->w;
    win->lastY = b->realY + b->h;

    if (win->option & F_AUTOSIZE) {
        if (win->lastX > win->realX) {
            win->realX = win->lastX;

            if (win->w != (win->realX + win->origX)) {
                resize = TRUE;
                win->w = (win->realX + win->origX);
            }
        }

        if (win->lastY > win->realY) {
            win->realY = win->lastY;

            if (win->h != (win->realY + win->origY)) {
                resize = TRUE;
                win->h = (win->realY + win->origY);
            }
        }

        if (win->shown) {
            if (resize) {
                gtk_widget_set_size_request(win->gtkwin, win->w, win->h);
                gtk_widget_set_size_request(win->widget, win->w, win->h);
            }
        }
    }
}

/**
 * Find the widget at a position
 * \param win IN searched widget's parent window
 * \param x IN x position inside parent
 * \param y IN y position inside parent
 * \returns the widget, NULL if none at the position
 */

wControl_p wlibGetControlFromPos(
                                 wWin_p win,
                                 wPos_t x,
                                 wPos_t y)
{
    wControl_p b;
    wPos_t xx, yy;

    for (b = win->first; b != NULL; b = b->next) {
        if (b->widget && gtk_widget_get_visible(b->widget)) {
            xx = b->realX;
            yy = b->realY;

            if (xx <= x && x < xx + b->w &&
                yy <= y && y < yy + b->h) {
                return b;
            }
        }
    }

    return NULL;
}


/*
 *****************************************************************************
 *
 * Exported Utility Functions
 *
 *****************************************************************************
 */

/**
 * Beep!
 * \return
 */
void wBeep(void)
{
    gdk_display_beep(gdk_display_get_default());
}

/**
 * Flushs all commands to the Window.
 */

void wFlush(
            void)
{
    while (gtk_events_pending()) {
        gtk_main_iteration();
    }

    gdk_display_sync(gdk_display_get_default());
}

/**
 * Not implemented
 * \returns
 */

void wWinTop(wWin_p win)
{
}

/**
 * Not implemented
 *
 * \param cursor IN
 */

void wSetCursor(wCursor_t cursor)
{
}

/**
 * Not implemented
 * \returns
 */

const char * wMemStats(void)
{
    return "No stats available";
}

/**
 * Get the size of the screen
 *
 * \param w IN pointer to width
 * \param h IN pointer to height
 */

void wGetDisplaySize(wPos_t * w, wPos_t * h)
{

    *w = gdk_screen_width();
    *h = gdk_screen_height();
}

static dynArr_t conversionBuffer_da;

/**
 * Convert a string to UTF-8
 *
 * \param inString IN string to convert
 * \returns pointer to converted string, valid until next call to conversion function
 */

char * wlibConvertInput(const char * inString)
{
    const char * cp;
    char * cq;
    int extCharCnt, inCharCnt;

    /* Already UTF-8 encoded? */
    if (g_utf8_validate(inString, -1, NULL))
        /* Yes, do not double-convert */ {
        return (char*) inString;
    }

    for (cp = inString, extCharCnt = 0; *cp; cp++) {
        if (((*cp)&0x80) != '\0') {
            extCharCnt++;
        }
    }

    inCharCnt = cp - inString;

    if (extCharCnt == '\0') {
        return (char*) inString;
    }

    DYNARR_SET(char, conversionBuffer_da, inCharCnt + extCharCnt + 1);

    for (cp = inString, cq = (char*) conversionBuffer_da.ptr; *cp; cp++) {
        if (((*cp)&0x80) != 0) {
            *cq++ = 0xC0 + (((*cp)&0xC0) >> 6);
            *cq++ = 0x80 + ((*cp)&0x3F);
        }
        else {
            *cq++ = *cp;
        }
    }

    *cq = 0;
    return (char*) conversionBuffer_da.ptr;
}

/**
 * Convert a string from UTF-8 to system codepage
 *
 * \param inString IN string to convert
 * \returns pointer to converted string, valid until next call to conversion function
 */

char * wlibConvertOutput(const char * inString)
{
    const char * cp;
    char * cq;
    int extCharCnt, inCharCnt;

    for (cp = inString, extCharCnt = 0; *cp; cp++) {
        if (((*cp)&0xC0) == 0x80) {
            extCharCnt++;
        }
    }

    inCharCnt = cp - inString;

    if (extCharCnt == '\0') {
        return (char*) inString;
    }

    DYNARR_SET(char, conversionBuffer_da, inCharCnt + 1);

    for (cp = inString, cq = (char*) conversionBuffer_da.ptr; *cp; cp++) {
        if (((*cp)&0x80) != 0) {
            *cq++ = 0xC0 + (((*cp)&0xC0) >> 6);
            *cq++ = 0x80 + ((*cp)&0x3F);
        }
        else {
            *cq++ = *cp;
        }
    }

    *cq = '\0';
    return (char*) conversionBuffer_da.ptr;
}

/*-----------------------------------------------------------------*/


static dynArr_t accelData_da;
#define accelData(N) DYNARR_N( accelData_t, accelData_da, N )

static guint accelKeyMap[] = {
    0, /* wAccelKey_None, */
    GDK_KEY_Delete, /* wAccelKey_Del, */
    GDK_KEY_Insert, /* wAccelKey_Ins, */
    GDK_KEY_Home, /* wAccelKey_Home, */
    GDK_KEY_End, /* wAccelKey_End, */
    GDK_KEY_Page_Up, /* wAccelKey_Pgup, */
    GDK_KEY_Page_Down, /* wAccelKey_Pgdn, */
    GDK_KEY_Up, /* wAccelKey_Up, */
    GDK_KEY_Down, /* wAccelKey_Down, */
    GDK_KEY_Right, /* wAccelKey_Right, */
    GDK_KEY_Left, /* wAccelKey_Left, */
    GDK_KEY_BackSpace, /* wAccelKey_Back, */
    GDK_KEY_F1, /* wAccelKey_F1, */
    GDK_KEY_F2, /* wAccelKey_F2, */
    GDK_KEY_F3, /* wAccelKey_F3, */
    GDK_KEY_F4, /* wAccelKey_F4, */
    GDK_KEY_F5, /* wAccelKey_F5, */
    GDK_KEY_F6, /* wAccelKey_F6, */
    GDK_KEY_F7, /* wAccelKey_F7, */
    GDK_KEY_F8, /* wAccelKey_F8, */
    GDK_KEY_F9, /* wAccelKey_F9, */
    GDK_KEY_F10, /* wAccelKey_F10, */
    GDK_KEY_F11, /* wAccelKey_F11, */
    GDK_KEY_F12, /* wAccelKey_F12, */
    GDK_KEY_KP_Add, /* wAccelKey_Numpad_Add */
    GDK_KEY_KP_Subtract /* wAccelKey_Numpad_Subtract */
};

/**
 * Create an accelerator key
 *
 * \param key IN primary key stroke
 * \param modifier IN modifier (shift, ctrl, etc.)
 * \param action IN function to call
 * \param data IN data to pass to function
 */

void wAttachAccelKey(
                     wAccelKey_e key,
                     int modifier,
                     wAccelKeyCallBack_p action,
                     void * data)
{
    accelData_t * ad;

//    if (key < 1 || key > wAccelKey_F12) {
//        fprintf(stderr, "wAttachAccelKey(%d) out of range\n", (int) key);
//        return;
//    }

    DYNARR_APPEND(accelData_t, accelData_da, 10);
    ad = &accelData(accelData_da.cnt - 1);
    ad->key = key;
    ad->modifier = modifier;
    ad->action = action;
    ad->data = data;
}

/**
 * Check for accelerator definition a pressed key
 *
 * \param event IN key press event
 * \returns pointer to accel key structure, NULL if not existing
 */

struct accelData_t * wlibFindAccelKey(
                                      GdkEventKey * event)
{
    accelData_t * ad;
    int modifier = 0;

    if ((event->state & GDK_SHIFT_MASK)) {
        modifier |= WKEY_SHIFT;
    }

    if ((event->state & GDK_CONTROL_MASK)) {
        modifier |= WKEY_CTRL;
    }

    if ((event->state & GDK_MOD1_MASK)) {
        modifier |= WKEY_ALT;
    }

    for (ad = &accelData(0); ad<&accelData(accelData_da.cnt); ad++)
        if (event->keyval == accelKeyMap[ad->key] &&
            modifier == ad->modifier) {
            return ad;
        }

    return NULL;
}

/**
 * Perform action when an accelerator key was pressed
 *
 * \param event IN key press event
 * \returns  TRUE if valid accelerator, FALSE if not
 */

wBool_t wlibHandleAccelKey(
                           GdkEventKey *event)
{
    accelData_t * ad = wlibFindAccelKey(event);

    if (ad) {
        ad->action(ad->key, ad->data);
        return TRUE;
    }

    return FALSE;
}

/**
 * Add control to circular list of synonymous controls. Synonymous controls are kept in sync by
 * calling wControlLinkedActive for one member of the list
 * \todo This is similar to the concept of action in gtk/glib \
 * Maybe this would be easier to use
 *
 * \param b1 IN  first control
 * \param b2 IN  second control
 * \return    none
 */

void wControlLinkedSet(wControl_p b1, wControl_p b2)
{

    b2->synonym = b1->synonym;

    if (b2->synonym == NULL) {
        b2->synonym = b1;
    }

    b1->synonym = b2;
}

/**
 * Activate/deactivate a group of synonymous controls.
 *
 * \param b IN  control
 * \param active IN  state
 * \return    none
 */

void wControlLinkedActive(wControl_p b, int active)
{
    wControl_p savePtr = b;

    if (savePtr->type == B_MENUITEM) {
        wMenuPushEnable((wMenuPush_p) savePtr, active);
    }
    else {
        wControlActive(savePtr, active);
    }

    savePtr = savePtr->synonym;

    while (savePtr && savePtr != b) {

        if (savePtr->type == B_MENUITEM) {
            wMenuPushEnable((wMenuPush_p) savePtr, active);
        }
        else {
            wControlActive(savePtr, active);
        }

        savePtr = savePtr->synonym;
    }
}