aboutsummaryrefslogtreecommitdiffstats
path: root/vncviewer/fltk/util.h
blob: 9ce3c7aa4b80282e3fc998f3180c3189b9726703 (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
/* Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef __FLTK_UTIL_H__
#define __FLTK_UTIL_H__

#include <FL/Fl_Menu_.H>

/* Escapes all @ in text as those have special meaning in labels */
static inline size_t fltk_escape(const char *in, char *out, size_t maxlen)
{
    size_t len;

    len = 0;

    while (*in != '\0') {
        if (*in == '@') {
            if (maxlen >= 3) {
                *out++ = '@';
                *out++ = '@';
                maxlen -= 2;
            }

            len += 2;
        } else {
            if (maxlen >= 2) {
                *out++ = *in;
                maxlen--;
            }

            len += 1;
        }

        in++;
    }

    if (maxlen)
        *out = '\0';

    return len;
}

/* Filter out unsafe characters for menu entries */
static inline size_t fltk_menu_escape(const char *in, char *out, size_t maxlen)
{
    size_t len;

    len = 0;

    while (*in != '\0') {
        if (*in == '/') {
            if (maxlen >= 3) {
                *out++ = '\\';
                *out++ = '/';
                maxlen -= 2;
            }

            len += 2;
        } else {
            if (maxlen >= 2) {
                *out++ = *in;
                maxlen--;
            }

            len += 1;
        }

        in++;
    }

    if (maxlen)
        *out = '\0';

    return len;
}

/* Helper to add menu entries safely */
static inline void fltk_menu_add(Fl_Menu_ *menu, const char *text,
                                 int shortcut, Fl_Callback *cb,
                                 void *data=nullptr, int flags=0)
{
    char buffer[1024];

    if (fltk_menu_escape(text, buffer, sizeof(buffer)) >= sizeof(buffer))
        return;

    menu->add(buffer, shortcut, cb, data, flags);
}

#endif