summaryrefslogtreecommitdiffstats
path: root/chromeos/dbus/ibus/ibus_text.h
blob: 1ed7ef1cd79fd768085965d91c4dff54df802791 (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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_DBUS_IBUS_IBUS_TEXT_H_
#define CHROMEOS_DBUS_IBUS_IBUS_TEXT_H_

#include <string>
#include <vector>
#include "base/basictypes.h"
#include "chromeos/chromeos_export.h"

namespace dbus {
class MessageWriter;
class MessageReader;
}  // dbus

namespace chromeos {

// The IBusText is one of IBusObjects and it contains IBusAttrList object which
// contains array of IBusAttribute object. The overview of each data structure
// is as follows:
//
// DATA STRUCTURE OVERVIEW:
//
// IBusAttribute: (signature is "uuuu")
// variant  struct {
//     string "IBusAttribute"
//     array[]
//     uint32 1  // Type of attribute.
//     uint32 1  // The value of attribute.
//     uint32 0  // The start index of the text.
//     uint32 1  // The end index of the text.
// }
//
// IBusAttrList: (signature is "av")
// variant  struct {
//     string "IBusAttrList"
//     array[]
//     array[  // The array of IBusAttribute.
//       variant  struct{
//           string "IBusAttribute"
//           ...
//       }
//       variant  struct{
//           string "IBusAttribute"
//           ...
//       }
//       variant  struct{
//           string "IBusAttribute"
//           ...
//       }
//    ]
// }
//
// IBusText: (signature is "sv")
// variant  struct {
//     string "IBusText"
//     array[]
//     string "A"
//     variant  struct {
//         string "IBusAttrList"
//         array[]
//         array[
//           variant  struct{
//               string "IBusAttribute"
//               ...
//             }
//           variant  struct{
//               string "IBusAttribute"
//               ...
//             }
//        ]
//      }
//  }
//
class IBusText;

// Pops a IBusText from |reader|.
// Returns false if an error occurs.
bool CHROMEOS_EXPORT PopIBusText(dbus::MessageReader* reader,
                                 IBusText* ibus_text);
// Pops a IBusText from |reader| and stores it's text field into text. Use
// PopIBusText instead in the case of using any attribute entries in IBusText.
// Returns true on success.
bool CHROMEOS_EXPORT PopStringFromIBusText(dbus::MessageReader* reader,
                                           std::string* text);
// Appends a IBusText to |writer|. Annotation and description field is not
// filled with AppendIBusText.
// TODO(nona): Support annotation/description appending if necessary.
void CHROMEOS_EXPORT AppendIBusText(const IBusText& ibus_text,
                                    dbus::MessageWriter* writer);

// Appends a string to |writer| as IBusText without any attributes. Use
// AppendIBusText instead in the case of using any attribute entries.
// TODO(nona): Support annotation/description appending if necessary.
void CHROMEOS_EXPORT AppendStringAsIBusText(const std::string& text,
                                            dbus::MessageWriter* writer);

// Handles IBusText object which is used in dbus communication with ibus-daemon.
// The IBusAttribute has four uint32 variables and the IBusAttributes represents
// three type of decoration based on it's values.
// 1. Underline decoration (corresponds to UnderlineAttribute structure)
//   1st value: indicates underline attribute.
//   2nd value: type of decoration. Chrome only support single and double
//              underline and error line.
//   3rd value: the start index of this attribute in multibyte.
//   4th value: the end index of this attribute in multibyte.
//
// 2. Background decoration (corresponds to SelectionAttribute structure)
//   NOTE: Background decoration is treated as selection in Chrome.
//   1st value: indicates background attribute.
//   2nd value: Represents color but not supported in Chrome.
//   3rd value: the start index of this attribute in multibyte.
//   4th value: the end index of this attribute in multibyte.
//
// 3. Forward decoration
//   Not supported in Chrome.
class CHROMEOS_EXPORT IBusText {
 public:
  enum IBusTextUnderlineType {
    IBUS_TEXT_UNDERLINE_SINGLE = 1,
    IBUS_TEXT_UNDERLINE_DOUBLE = 2,
    IBUS_TEXT_UNDERLINE_ERROR  = 4,
  };

  struct UnderlineAttribute {
    IBusTextUnderlineType type;
    uint32 start_index;  // The inclusive start index.
    uint32 end_index;  // The exclusive end index.
  };

  struct SelectionAttribute {
    uint32 start_index;  // The inclusive start index.
    uint32 end_index;  // The exclusive end index.
  };

  // Accessors
  IBusText();
  virtual ~IBusText();

  const std::string& text() const { return text_; }
  void set_text(const std::string& text) { text_ = text; }

  const std::string& annotation() const { return annotation_; }
  void set_annotation(const std::string& annotation) {
    annotation_ = annotation;
  }

  const std::string& description_title() const { return description_title_; }
  void set_description_title(const std::string& title) {
    description_title_ = title;
  }

  const std::string& description_body() const { return description_body_; }
  void set_description_body(const std::string& body) {
    description_body_ = body;
  }

  const std::vector<UnderlineAttribute>& underline_attributes() const {
    return underline_attributes_;
  }

  std::vector<UnderlineAttribute>* mutable_underline_attributes() {
    return &underline_attributes_;
  }

  const std::vector<SelectionAttribute>& selection_attributes() const {
    return selection_attributes_;
  }
  std::vector<SelectionAttribute>* mutable_selection_attributes() {
    return &selection_attributes_;
  }

  void CopyFrom(const IBusText& obj);

 private:
  std::string text_;
  std::string annotation_;
  std::string description_title_;
  std::string description_body_;
  std::vector<UnderlineAttribute> underline_attributes_;
  std::vector<SelectionAttribute> selection_attributes_;

  DISALLOW_COPY_AND_ASSIGN(IBusText);
};

}  // namespace chromeos

#endif  // CHROMEOS_DBUS_IBUS_IBUS_TEXT_H_