File: String.h

package info (click to toggle)
lsp-plugins 1.2.21-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 120,408 kB
  • sloc: cpp: 589,849; xml: 74,078; makefile: 13,396; php: 1,268; sh: 185
file content (283 lines) | stat: -rw-r--r-- 11,749 bytes parent folder | download
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
/*
 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
 *           (C) 2025 Vladimir Sadovnikov <sadko4u@gmail.com>
 *
 * This file is part of lsp-tk-lib
 * Created on: 2 мар. 2020 г.
 *
 * lsp-tk-lib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * lsp-tk-lib 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with lsp-tk-lib. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef LSP_PLUG_IN_TK_PROP_STRING_H_
#define LSP_PLUG_IN_TK_PROP_STRING_H_

#ifndef LSP_PLUG_IN_TK_IMPL
    #error "use <lsp-plug.in/tk/tk.h>"
#endif

#include <lsp-plug.in/expr/Parameters.h>
#include <lsp-plug.in/i18n/IDictionary.h>

namespace lsp
{
    namespace tk
    {
        /**
         * A localized string that requires access to dictionary to resolve the actual
         * localized string value. There is also possible to define raw text without
         * access to the dictionary.
         */
        class String: public SimpleProperty
        {
            protected:
                enum flags_t
                {
                    F_LOCALIZED     = 1 << 0,
                    F_MATCHING      = 1 << 1
                };

            protected:
                class Params: public expr::Parameters
                {
                    private:
                        String  *pString;
                        bool     bLock;

                    public:
                        explicit inline Params(String *ps) { pString = ps; bLock = false; }

                    protected:
                        virtual void        modified() override;

                    public:
                        inline void         set_lock(bool lock) { bLock = lock; }
                };

            protected:
                LSPString           sText;      // Text used for rendering
                mutable LSPString   sCache;     // Cache
                Params              sParams;    // Parameters
                mutable size_t      nFlags;     // Different flags
                i18n::IDictionary  *pDict;      // Related dictionary

            protected:
                status_t            fmt_internal(LSPString *out, const LSPString *lang) const;
                LSPString          *fmt_for_update();
                status_t            lookup_template(LSPString *templ, const LSPString *lang) const;

            protected:
                status_t            bind(atom_t property, Style *style, i18n::IDictionary *dict);
                status_t            bind(const char *property, Style *style, i18n::IDictionary *dict);
                status_t            bind(const LSPString *property, Style *style, i18n::IDictionary *dict);
                status_t            unbind();
                void                invalidate();
                status_t            commit_raw(const LSPString *s);
                status_t            commit(const LSPString *s, const expr::Parameters *params);

                virtual void        push() override;
                virtual void        commit(atom_t property) override;

            protected:
                explicit String(prop::Listener *listener = NULL);
                String(const String &) = delete;
                String(String &&) = delete;
                virtual ~String() override;

                String & operator = (const String &) = delete;
                String & operator = (String &&) = delete;

            public:
                /**
                 * Check wheter the string is localized
                 * @return true if the string is localized
                 */
                inline bool localized() const               { return nFlags & F_LOCALIZED;    }

                /**
                 * Get raw text
                 * @return raw text or NULL if string is localized
                 */
                inline const LSPString *raw() const         { return (nFlags & F_LOCALIZED) ? NULL : &sText;        }

                /**
                 * Get localization key
                 * @return localization key or NULL if string is not localized
                 */
                inline const LSPString *key() const         { return (nFlags & F_LOCALIZED) ? &sText : NULL;        }

                /**
                 * Get formatting parameters for localized string
                 * @return parameters
                 */
                inline const expr::Parameters *params() const { return &sParams; }

                /**
                 * Get formatting parameters for localized string
                 * @return parameters
                 */
                inline expr::Parameters *params()           { return &sParams; }

                /**
                 * Check whether string contains localized data or not
                 * @return true if string contains localized data
                 */
                inline bool is_localized() const            { return nFlags & F_LOCALIZED; }

                /**
                 * Check for emptiness
                 * @return true if string is empty
                 */
                inline bool is_empty() const                { return sText.is_empty(); }

            public:
                /**
                 * Set raw (non-localized) value
                 * @param value value to set
                 * @return status of operation
                 */
                status_t set_raw(const LSPString *value);

                /**
                 * Set raw (non-localized) value
                 * @param value UTF-8 text to set
                 * @return status of operation
                 */
                status_t set_raw(const char *value);

                /**
                 * Set key to the localized string
                 * @param value value to set
                 * @return status of operation
                 */
                status_t set_key(const LSPString *value);

                /**
                 * Set key to the localized string
                 * @param value value to set
                 * @return status of operation
                 */
                status_t set_key(const char *value);

                /**
                 * Set parameters only to the localized string
                 * @param params parameters to set
                 * @return status of operation
                 */
                status_t set_params(const expr::Parameters *params);

                /**
                 * Set localized value
                 * @param key the bundle key to use as a text source
                 * @param params additional optional parameters for message formatting
                 * @return status of operation
                 */
                status_t set(const LSPString *key, const expr::Parameters *params);
                inline status_t set(const LSPString *key)   { return set(key, static_cast<expr::Parameters *>(NULL)); };

                /**
                 * Set localized value
                 * @param key the UTF8 bundle key to use as a text source
                 * @param params additional optional parameters for message formatting
                 * @return status of operation
                 */
                status_t set(const char *key, const expr::Parameters *params);
                inline status_t set(const char *key)        { return set(key, static_cast<expr::Parameters *>(NULL)); };

                /**
                 * Make a copy of data from the source local string to this local string
                 * @param value source string value
                 * @return status of operation
                 */
                status_t set(const String *value);

                /**
                 * Clear the localized string
                 */
                void clear();

                /**
                 * Format the message using dictionary and style from derived widget
                 * @param out output string
                 * @return status of operation
                 */
                status_t format(LSPString *out) const;

                /**
                 * Format the message using dictionary and style from derived widget
                 * @param out output string
                 * @param lang language
                 * @return status of operation
                 */
                status_t format(LSPString *out, const char *lang) const;

                /**
                 * Format the message using dictionary and style from derived widget
                 * @param out output string
                 * @param lang language
                 * @return status of operation
                 */
                status_t format(LSPString *out, const LSPString *lang) const;

                /**
                 * Swap contents
                 * @param dst destination string to perform swap
                 */
                void swap(String *dst);
        };

        namespace prop
        {
            class String: public tk::String
            {
                public:
                    explicit String(prop::Listener *listener = NULL): tk::String(listener) {};
                    String(const String &)= delete;
                    String(String &&)= delete;

                    String & operator = (const String &) = delete;
                    String & operator = (String &&) = delete;

                public:
                    using tk::String::format;

                    /**
                     * Get formatted string for update
                     * @return pointer to cached formatted string, never NULL
                     */
                    inline LSPString   *formatted()                         { return tk::String::fmt_for_update();      }
                    bool                invalidate();
                    inline status_t     commit_raw(const LSPString *s)      { return tk::String::commit_raw(s);         }
                    inline status_t     commit_value(const LSPString *s, const expr::Parameters *params = NULL)   { return tk::String::commit(s, params);     }

                    /**
                     * Bind property with specified name to the style of linked widget
                     */
                    inline status_t     bind(atom_t id, Style *style, i18n::IDictionary *dict)              { return tk::String::bind(id, style, dict); }
                    inline status_t     bind(const LSPString *id, Style *style, i18n::IDictionary *dict)    { return tk::String::bind(id, style, dict); }
                    inline status_t     bind(const char *id, Style *style, i18n::IDictionary *dict)         { return tk::String::bind(id, style, dict); }
                    inline status_t     bind(Style *style, i18n::IDictionary *dict)                         { return tk::String::bind(LSP_TK_PROP_LANGUAGE, style, dict); }

                    /**
                     * Unbind property
                     */
                    inline status_t     unbind()                            { return tk::String::unbind();              }

                    inline void         listener(prop::Listener *listener)  { pListener = listener;                     }
            };

        } /* namespace prop */
    } /* namespace tk */
} /* namespace lsp */

#endif /* LSP_PLUG_IN_TK_PROP_STRING_H_ */