1 | /** @file scim_attribute.h |
2 | * @brief Definition of scim::Attribute and scim::AttributeList |
3 | * |
4 | * Provide class scim::Attribute to control the |
5 | * drawing effect of strings. |
6 | */ |
7 | |
8 | /* |
9 | * Smart Common Input Method |
10 | * |
11 | * Copyright (c) 2002-2005 James Su <suzhe@tsinghua.org.cn> |
12 | * |
13 | * |
14 | * This library is free software; you can redistribute it and/or |
15 | * modify it under the terms of the GNU Lesser General Public |
16 | * License as published by the Free Software Foundation; either |
17 | * version 2 of the License, or (at your option) any later version. |
18 | * |
19 | * This library is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Lesser General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Lesser General Public |
25 | * License along with this program; if not, write to the |
26 | * Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
27 | * Boston, MA 02111-1307 USA |
28 | * |
29 | * $Id: scim_attribute.h,v 1.7 2005/08/05 16:12:31 suzhe Exp $ |
30 | */ |
31 | |
32 | |
33 | #ifndef __SCIM_ATTRIBUTE_H |
34 | #define __SCIM_ATTRIBUTE_H |
35 | |
36 | namespace scim { |
37 | |
38 | /** |
39 | * @addtogroup Accessories |
40 | * |
41 | * The accessorial classes and functions, including Attribute, IConvert, LookupTable etc. |
42 | * |
43 | * @{ |
44 | */ |
45 | |
46 | /** |
47 | * @brief Enum values of the valid attribute type. |
48 | */ |
49 | enum AttributeType |
50 | { |
51 | SCIM_ATTR_NONE, ///< No attribute. |
52 | SCIM_ATTR_DECORATE, ///< A decorate attribute, eg. underline etc. |
53 | SCIM_ATTR_FOREGROUND, ///< A foreground color attribute, in RGB format. |
54 | SCIM_ATTR_BACKGROUND ///< A background color attribute, in RGB format. |
55 | }; |
56 | |
57 | const unsigned int SCIM_ATTR_DECORATE_NONE = 0; ///< No decorate |
58 | const unsigned int SCIM_ATTR_DECORATE_UNDERLINE = 1; ///< Draw a line under the text |
59 | const unsigned int SCIM_ATTR_DECORATE_HIGHLIGHT = 2; ///< Draw the text in highlighted color |
60 | const unsigned int SCIM_ATTR_DECORATE_REVERSE = 4; ///< Draw the text in reverse color mode |
61 | |
62 | #define SCIM_RGB_COLOR(RED,GREEN,BLUE) ((unsigned int)(((RED)<<16) + ((GREEN)<<8) + (BLUE))) |
63 | #define SCIM_RGB_COLOR_RED(COLOR) ((unsigned int)((COLOR>>16) & 0x00ff)) |
64 | #define SCIM_RGB_COLOR_GREEN(COLOR) ((unsigned int)((COLOR>>8) & 0x00ff)) |
65 | #define SCIM_RGB_COLOR_BLUE(COLOR) ((unsigned int)((COLOR) & 0x00ff)) |
66 | |
67 | /** |
68 | * @brief Class to store the string attributes. |
69 | * |
70 | * The string attributes control the effect of the string |
71 | * drawn by FrontEnds. There are currently four valid types. |
72 | * |
73 | * A attribute could be one of the following types: |
74 | * - SCIM_ATTR_NONE No attribute |
75 | * - SCIM_ATTR_DECORATE Decorate attribute, eg. underline, highlight etc. |
76 | * - SCIM_ATTR_FOREGROUND Foreground color attribute, in RGB format. |
77 | * - SCIM_ATTR_BACKGROUND Background color attribute, in RGB format. |
78 | * |
79 | * For a DECORATE attribute, it can be one of the following values: |
80 | * - SCIM_ATTR_DECORATE_NONE No decorate |
81 | * - SCIM_ATTR_DECORATE_UNDERLINE Underline |
82 | * - SCIM_ATTR_DECORATE_HIGHLIGHT Highlight |
83 | * - SCIM_ATTR_DECORATE_REVERSE Reverse |
84 | * |
85 | * For a FOREGROUND or BACKGROUND attribute, it's a RGB color value generated with |
86 | * SCIM_RGB_COLOR (red,green,blue) macro. |
87 | * You may use SCIM_RGB_COLOR_RED, SCIM_RGB_COLOR_GREEN and SCIM_RGB_COLOR_BLUE to extract |
88 | * the RGB color later. |
89 | */ |
90 | class Attribute |
91 | { |
92 | unsigned int m_start; |
93 | unsigned int m_length; |
94 | |
95 | AttributeType m_type; |
96 | unsigned int m_value; |
97 | |
98 | public: |
99 | /** |
100 | * @brief Constructor |
101 | * |
102 | * @param start - the start position in the string of this attribute. |
103 | * @param length - the length of this attribute, the range is [start,start+length). |
104 | * @param type - the type of this attribute. |
105 | * @param value - the value of this attribute. |
106 | */ |
107 | Attribute (unsigned int start = 0, |
108 | unsigned int length = 0, |
109 | AttributeType type = SCIM_ATTR_NONE, |
110 | unsigned int value = 0) : |
111 | m_start (start), m_length (length), m_type (type), m_value (value) |
112 | { } |
113 | |
114 | /** |
115 | * @brief Get the type of this attribute. |
116 | * |
117 | * @return The type of this attribute. |
118 | */ |
119 | AttributeType get_type () const { return m_type; } |
120 | |
121 | /** |
122 | * @brief Get the value of this attribute. |
123 | * |
124 | * @return The value of this attribute. |
125 | */ |
126 | unsigned int get_value () const { return m_value; } |
127 | |
128 | /** |
129 | * @brief Get the start position of this attribute. |
130 | * @return The start position of this attribute in the string. |
131 | */ |
132 | unsigned int get_start () const { return m_start; } |
133 | |
134 | /** |
135 | * @brief Get the length of this attribute. |
136 | * @return The length of this attribute in the string. |
137 | */ |
138 | unsigned int get_length () const { return m_length; } |
139 | |
140 | /** |
141 | * @brief Get the end position of this attribute. |
142 | * @return The end position of this attribute. |
143 | */ |
144 | unsigned int get_end () const { return m_start + m_length; } |
145 | |
146 | /** |
147 | * @brief Set the type of this attribute. |
148 | * @param type - the new attribute type to be set. |
149 | */ |
150 | void set_type (AttributeType type) { m_type = type; } |
151 | |
152 | /** |
153 | * @brief Set the value of this attribute. |
154 | * @param value - the new attribute value to be set. |
155 | */ |
156 | void set_value (unsigned int value) { m_value = value; } |
157 | |
158 | /** |
159 | * @brief Set the start position of this attribute. |
160 | * @param start - the new start position in the string. |
161 | */ |
162 | void set_start (unsigned int start) { m_start = start; } |
163 | |
164 | /** |
165 | * @brief Set the length of this attribute. |
166 | * @param length - the new length of this attribute. |
167 | */ |
168 | void set_length (unsigned int length) { m_length = length; } |
169 | }; |
170 | |
171 | inline bool |
172 | operator < (const Attribute &lhs, const Attribute &rhs) |
173 | { |
174 | return lhs.get_start () < rhs.get_start () || |
175 | (lhs.get_start () == rhs.get_start () && |
176 | (lhs.get_length () < rhs.get_length () || |
177 | (lhs.get_length () == rhs.get_length () && |
178 | (lhs.get_type () < rhs.get_type () || |
179 | (lhs.get_type () == rhs.get_type () && |
180 | (lhs.get_value () < rhs.get_value ())))))); |
181 | } |
182 | |
183 | /** |
184 | * @typedef typedef std::vector<Attribute> AttributeList |
185 | * @brief The container to store a set of Attribute objects. |
186 | * |
187 | * You should use the STL container methods to manipulate its objects. |
188 | */ |
189 | typedef std::vector<Attribute> AttributeList; |
190 | |
191 | /** @} */ |
192 | |
193 | } // namespace scim |
194 | |
195 | #endif //__SCIM_ATTRIBUTE_H |
196 | |
197 | /* |
198 | vi:ts=4:nowrap:ai:expandtab |
199 | */ |
200 | |