1
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
3
* Copyright 2013 Inalogic® Inc.
5
* This program is free software: you can redistribute it and/or modify it
6
* under the terms of the GNU Lesser General Public License, as
7
* published by the Free Software Foundation; either version 2.1 or 3.0
10
* This program is distributed in the hope that it will be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranties of
12
* MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
13
* PURPOSE. See the applicable version of the GNU Lesser General Public
14
* License for more details.
16
* You should have received a copy of both the GNU Lesser General Public
17
* License along with this program. If not, see <http://www.gnu.org/licenses/>
19
* Authored by: Brandon Schaefer
25
#include "TextEntry.h"
26
#include "XICClient.h"
30
#include "NuxCore/Logger.h"
32
DECLARE_LOGGER(logger, "xim.callbacks");
39
int const FEEDBACK_MASK = (XIMUnderline | XIMReverse);
41
int XICClient::PreeditStartCallback(XIC xic,
45
TextEntry* text_entry = (TextEntry*)clientdata;
48
text_entry->PreeditStarted();
54
int XICClient::PreeditDoneCallback(XIC xic,
58
TextEntry* text_entry = (TextEntry*)clientdata;
61
text_entry->ClearPreedit();
67
void add_feedback_attr(PangoAttrList* attrs,
75
int start_index = g_utf8_offset_to_pointer(str, start) - str;
76
int end_index = g_utf8_offset_to_pointer(str, end) - str;
78
if (feedback & XIMUnderline)
80
attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
81
attr->start_index = start_index;
82
attr->end_index = end_index;
84
pango_attr_list_insert (attrs, attr);
87
if (feedback & XIMReverse)
89
attr = pango_attr_foreground_new(0xFFFF, 0xFFFF, 0xFFFF);
90
attr->start_index = start_index;
91
attr->end_index = end_index;
93
pango_attr_list_insert (attrs, attr);
95
attr = pango_attr_background_new(0x0, 0x0, 0x0);
97
attr->start_index = start_index;
98
attr->end_index = end_index;
100
pango_attr_list_insert (attrs, attr);
103
if (feedback & ~FEEDBACK_MASK)
105
LOG_WARN(logger) << "Unrenderer feedback: " << (feedback & ~FEEDBACK_MASK);
109
void feedback_to_pango_list(PangoAttrList** attrs,
112
XIMFeedback* feedbacks)
114
XIMFeedback last_feedback = 0;
120
*attrs = pango_attr_list_new();
122
for (i = 0; i < nfeedbacks; ++i)
124
XIMFeedback new_feedback = (feedbacks[i] & FEEDBACK_MASK);
125
if (new_feedback != last_feedback)
127
last_feedback = new_feedback;
132
PangoAttribute* attr;
134
int start_index = g_utf8_offset_to_pointer(str, start) - str;
135
int end_index = g_utf8_offset_to_pointer(str, i) - str;
137
attr = pango_attr_foreground_new(0x0, 0x0, 0x0);
138
attr->start_index = start_index;
139
attr->end_index = end_index;
141
pango_attr_list_insert (*attrs, attr);
143
attr = pango_attr_background_new(0xFFFF, 0xFFFF, 0xFFFF);
145
attr->start_index = start_index;
146
attr->end_index = end_index;
148
pango_attr_list_insert (*attrs, attr);
153
add_feedback_attr(*attrs, str, last_feedback, start, i);
157
int XICClient::PreeditDrawCallback(XIC xic,
159
XIMPreeditDrawCallbackStruct* call_data)
161
TextEntry* text_entry = (TextEntry*)clientdata;
163
if (call_data->text && text_entry)
167
// TODO Actually handle this correctly...
168
if (call_data->text->encoding_is_wchar)
170
preedit = string("");
174
preedit = string(call_data->text->string.multi_byte);
177
PangoAttrList* preedit_attr;
178
feedback_to_pango_list(&preedit_attr, preedit.c_str(),
179
call_data->text->length,
180
call_data->text->feedback);
182
text_entry->UpdatePreeditAttribs(preedit_attr);
183
text_entry->UpdatePreedit(preedit, call_data->caret);