1 | /*
|
---|
2 | * Copyright (c) 2022 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libui
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file Accelerator processing
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <ctype.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <str.h>
|
---|
40 | #include <ui/accel.h>
|
---|
41 |
|
---|
42 | /** Process text with accelerator markup.
|
---|
43 | *
|
---|
44 | * Parse text with tilde markup into a list of strings. @a *rbuf is set
|
---|
45 | * to point to a newly allocated buffer containing consecutive null-terminated
|
---|
46 | * strings.
|
---|
47 | *
|
---|
48 | * Each part between two '~' becomes one string. '~~' is translated into
|
---|
49 | * a literal '~' character. @a *endptr is set to point to the first character
|
---|
50 | * beyond the end of the list.
|
---|
51 | *
|
---|
52 | * @param str String with tilde markup
|
---|
53 | * @param rbuf Place to store pointer to newly allocated buffer.
|
---|
54 | * @param endptr Place to store end pointer (just after last character)
|
---|
55 | * @return EOK on success or an error code
|
---|
56 | */
|
---|
57 | errno_t ui_accel_process(const char *str, char **rbuf, char **endptr)
|
---|
58 | {
|
---|
59 | const char *sp;
|
---|
60 | char *dp;
|
---|
61 | char *buf;
|
---|
62 |
|
---|
63 | buf = malloc(str_size(str) + 1);
|
---|
64 | if (buf == NULL)
|
---|
65 | return ENOMEM;
|
---|
66 |
|
---|
67 | /* Break down string into list of (non)highlighted parts */
|
---|
68 | sp = str;
|
---|
69 | dp = buf;
|
---|
70 | while (*sp != '\0') {
|
---|
71 | if (*sp == '~') {
|
---|
72 | if (sp[1] == '~') {
|
---|
73 | sp += 2;
|
---|
74 | *dp++ = '~';
|
---|
75 | } else {
|
---|
76 | ++sp;
|
---|
77 | *dp++ = '\0';
|
---|
78 | }
|
---|
79 | } else {
|
---|
80 | *dp++ = *sp++;
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | *dp++ = '\0';
|
---|
85 | *rbuf = buf;
|
---|
86 | *endptr = dp;
|
---|
87 |
|
---|
88 | return EOK;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /** Get accelerator character from marked-up string.
|
---|
92 | *
|
---|
93 | * @param str String with '~' accelerator markup
|
---|
94 | *
|
---|
95 | * @return Accelerator character (lowercase) or the null character if
|
---|
96 | * the menu has no accelerator.
|
---|
97 | */
|
---|
98 | char32_t ui_accel_get(const char *str)
|
---|
99 | {
|
---|
100 | const char *sp = str;
|
---|
101 | size_t off;
|
---|
102 |
|
---|
103 | while (*sp != '\0') {
|
---|
104 | if (*sp == '~') {
|
---|
105 | if (sp[1] == '~') {
|
---|
106 | ++sp;
|
---|
107 | } else {
|
---|
108 | /* Found the accelerator */
|
---|
109 | ++sp;
|
---|
110 | break;
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | ++sp;
|
---|
115 | }
|
---|
116 |
|
---|
117 | if (*sp == '\0') {
|
---|
118 | /* No accelerator */
|
---|
119 | return '\0';
|
---|
120 | }
|
---|
121 |
|
---|
122 | /* Decode accelerator character */
|
---|
123 | off = 0;
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * NOTE: tolower is unlike to actually convert to lowercase
|
---|
127 | * in case of non-ASCII characters
|
---|
128 | */
|
---|
129 | return tolower(str_decode(sp, &off, STR_NO_LIMIT));
|
---|
130 | }
|
---|
131 |
|
---|
132 | /** @}
|
---|
133 | */
|
---|