2012-06-21 23:52:19 +04:00
|
|
|
/*
|
|
|
|
* Copyright © 2012 Openismus GmbH
|
2012-09-10 01:08:30 +04:00
|
|
|
* Copyright © 2012 Intel Corporation
|
2012-06-21 23:52:19 +04:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and
|
|
|
|
* its documentation for any purpose is hereby granted without fee, provided
|
|
|
|
* that the above copyright notice appear in all copies and that both that
|
|
|
|
* copyright notice and this permission notice appear in supporting
|
|
|
|
* documentation, and that the name of the copyright holders not be used in
|
|
|
|
* advertising or publicity pertaining to distribution of the software
|
|
|
|
* without specific, written prior permission. The copyright holders make
|
|
|
|
* no representations about the suitability of this software for any
|
|
|
|
* purpose. It is provided "as is" without express or implied warranty.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
|
|
|
|
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
|
|
|
|
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
|
|
|
|
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-01-17 00:26:42 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-07-12 00:25:29 +04:00
|
|
|
#include <assert.h>
|
2012-06-21 23:52:19 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <linux/input.h>
|
|
|
|
#include <cairo.h>
|
|
|
|
|
2013-01-17 00:26:42 +04:00
|
|
|
#include <pango/pangocairo.h>
|
|
|
|
|
2012-06-21 23:52:19 +04:00
|
|
|
#include "window.h"
|
|
|
|
#include "text-client-protocol.h"
|
|
|
|
|
|
|
|
struct text_entry {
|
|
|
|
struct widget *widget;
|
2012-08-10 18:47:22 +04:00
|
|
|
struct window *window;
|
2012-06-21 23:52:19 +04:00
|
|
|
char *text;
|
|
|
|
int active;
|
2012-09-10 01:08:36 +04:00
|
|
|
uint32_t cursor;
|
2012-09-10 01:08:38 +04:00
|
|
|
uint32_t anchor;
|
2013-01-17 00:26:38 +04:00
|
|
|
struct {
|
|
|
|
char *text;
|
|
|
|
int32_t cursor;
|
|
|
|
char *commit;
|
2013-01-17 00:26:42 +04:00
|
|
|
PangoAttrList *attr_list;
|
2013-01-17 00:26:38 +04:00
|
|
|
} preedit;
|
|
|
|
struct {
|
2013-01-17 00:26:42 +04:00
|
|
|
PangoAttrList *attr_list;
|
2013-01-17 00:26:38 +04:00
|
|
|
int32_t cursor;
|
|
|
|
} preedit_info;
|
2013-01-31 18:52:23 +04:00
|
|
|
struct {
|
|
|
|
int32_t cursor;
|
|
|
|
int32_t anchor;
|
|
|
|
} pending_commit;
|
2012-06-21 23:52:19 +04:00
|
|
|
struct text_model *model;
|
2013-01-17 00:26:42 +04:00
|
|
|
PangoLayout *layout;
|
2012-11-18 22:06:44 +04:00
|
|
|
struct {
|
|
|
|
xkb_mod_mask_t shift_mask;
|
|
|
|
} keysym;
|
2013-01-17 00:26:39 +04:00
|
|
|
uint32_t serial;
|
2013-01-17 00:26:45 +04:00
|
|
|
uint32_t content_purpose;
|
2013-01-31 18:52:21 +04:00
|
|
|
uint32_t click_to_show;
|
2012-06-21 23:52:19 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct editor {
|
2012-08-10 18:47:20 +04:00
|
|
|
struct text_model_factory *text_model_factory;
|
2012-06-21 23:52:19 +04:00
|
|
|
struct display *display;
|
|
|
|
struct window *window;
|
|
|
|
struct widget *widget;
|
|
|
|
struct text_entry *entry;
|
|
|
|
struct text_entry *editor;
|
2012-11-18 22:06:49 +04:00
|
|
|
struct text_entry *active_entry;
|
2012-06-21 23:52:19 +04:00
|
|
|
};
|
|
|
|
|
2012-11-05 06:26:39 +04:00
|
|
|
static const char *
|
|
|
|
utf8_start_char(const char *text, const char *p)
|
|
|
|
{
|
|
|
|
for (; p >= text; --p) {
|
|
|
|
if ((*p & 0xc0) != 0x80)
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
utf8_prev_char(const char *text, const char *p)
|
|
|
|
{
|
|
|
|
if (p > text)
|
|
|
|
return utf8_start_char(text, --p);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
utf8_end_char(const char *p)
|
|
|
|
{
|
|
|
|
while ((*p & 0xc0) == 0x80)
|
|
|
|
p++;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
utf8_next_char(const char *p)
|
|
|
|
{
|
|
|
|
if (*p != 0)
|
|
|
|
return utf8_end_char(++p);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:34 +04:00
|
|
|
static void text_entry_redraw_handler(struct widget *widget, void *data);
|
|
|
|
static void text_entry_button_handler(struct widget *widget,
|
|
|
|
struct input *input, uint32_t time,
|
|
|
|
uint32_t button,
|
|
|
|
enum wl_pointer_button_state state, void *data);
|
2013-01-31 18:52:23 +04:00
|
|
|
static void text_entry_insert_at_cursor(struct text_entry *entry, const char *text,
|
|
|
|
int32_t cursor, int32_t anchor);
|
2012-09-10 01:08:43 +04:00
|
|
|
static void text_entry_set_preedit(struct text_entry *entry,
|
|
|
|
const char *preedit_text,
|
|
|
|
int preedit_cursor);
|
2012-09-10 01:08:44 +04:00
|
|
|
static void text_entry_delete_text(struct text_entry *entry,
|
|
|
|
uint32_t index, uint32_t length);
|
2012-09-17 17:28:09 +04:00
|
|
|
static void text_entry_delete_selected_text(struct text_entry *entry);
|
2013-01-17 00:26:40 +04:00
|
|
|
static void text_entry_reset_preedit(struct text_entry *entry);
|
|
|
|
static void text_entry_commit_and_reset(struct text_entry *entry);
|
2012-06-21 23:52:19 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
text_model_commit_string(void *data,
|
|
|
|
struct text_model *text_model,
|
2013-01-17 00:26:39 +04:00
|
|
|
uint32_t serial,
|
2013-01-31 18:52:23 +04:00
|
|
|
const char *text)
|
2012-06-21 23:52:19 +04:00
|
|
|
{
|
|
|
|
struct text_entry *entry = data;
|
|
|
|
|
2013-01-17 00:26:40 +04:00
|
|
|
text_entry_reset_preedit(entry);
|
|
|
|
|
2012-09-17 17:28:09 +04:00
|
|
|
text_entry_delete_selected_text(entry);
|
2013-01-31 18:52:23 +04:00
|
|
|
text_entry_insert_at_cursor(entry, text,
|
|
|
|
entry->pending_commit.cursor,
|
|
|
|
entry->pending_commit.anchor);
|
|
|
|
|
|
|
|
memset(&entry->pending_commit, 0, sizeof entry->pending_commit);
|
2012-06-21 23:52:19 +04:00
|
|
|
|
|
|
|
widget_schedule_redraw(entry->widget);
|
|
|
|
}
|
|
|
|
|
2012-08-10 18:47:19 +04:00
|
|
|
static void
|
|
|
|
text_model_preedit_string(void *data,
|
|
|
|
struct text_model *text_model,
|
2013-01-17 00:26:39 +04:00
|
|
|
uint32_t serial,
|
2012-08-10 18:47:19 +04:00
|
|
|
const char *text,
|
2013-01-17 00:26:38 +04:00
|
|
|
const char *commit)
|
2012-08-10 18:47:19 +04:00
|
|
|
{
|
2012-09-10 01:08:43 +04:00
|
|
|
struct text_entry *entry = data;
|
|
|
|
|
2012-09-17 17:28:09 +04:00
|
|
|
text_entry_delete_selected_text(entry);
|
2013-01-17 00:26:38 +04:00
|
|
|
text_entry_set_preedit(entry, text, entry->preedit_info.cursor);
|
2013-01-17 00:26:40 +04:00
|
|
|
entry->preedit.commit = strdup(commit);
|
2013-01-17 00:26:42 +04:00
|
|
|
entry->preedit.attr_list = entry->preedit_info.attr_list;
|
2013-01-17 00:26:40 +04:00
|
|
|
|
|
|
|
entry->preedit_info.cursor = 0;
|
2013-01-17 00:26:42 +04:00
|
|
|
entry->preedit_info.attr_list = NULL;
|
2012-09-10 01:08:43 +04:00
|
|
|
|
|
|
|
widget_schedule_redraw(entry->widget);
|
2012-08-10 18:47:19 +04:00
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:44 +04:00
|
|
|
static void
|
|
|
|
text_model_delete_surrounding_text(void *data,
|
|
|
|
struct text_model *text_model,
|
2013-01-17 00:26:39 +04:00
|
|
|
uint32_t serial,
|
2012-09-10 01:08:44 +04:00
|
|
|
int32_t index,
|
|
|
|
uint32_t length)
|
|
|
|
{
|
|
|
|
struct text_entry *entry = data;
|
|
|
|
uint32_t cursor_index = index + entry->cursor;
|
2012-11-05 06:26:39 +04:00
|
|
|
const char *start, *end;
|
2012-09-10 01:08:44 +04:00
|
|
|
|
|
|
|
if (cursor_index > strlen(entry->text)) {
|
|
|
|
fprintf(stderr, "Invalid cursor index %d\n", index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cursor_index + length > strlen(entry->text)) {
|
|
|
|
fprintf(stderr, "Invalid length %d\n", length);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
return;
|
|
|
|
|
2012-11-05 06:26:39 +04:00
|
|
|
start = utf8_start_char(entry->text, entry->text + cursor_index);
|
|
|
|
end = utf8_end_char(entry->text + cursor_index + length);
|
|
|
|
|
|
|
|
text_entry_delete_text(entry,
|
|
|
|
start - entry->text,
|
|
|
|
end - start);
|
2012-09-10 01:08:44 +04:00
|
|
|
}
|
|
|
|
|
2013-01-31 18:52:23 +04:00
|
|
|
static void
|
|
|
|
text_model_cursor_position(void *data,
|
|
|
|
struct text_model *text_model,
|
|
|
|
uint32_t serial,
|
|
|
|
int32_t index,
|
|
|
|
int32_t anchor)
|
|
|
|
{
|
|
|
|
struct text_entry *entry = data;
|
|
|
|
|
|
|
|
entry->pending_commit.cursor = index;
|
|
|
|
entry->pending_commit.anchor = anchor;
|
|
|
|
}
|
|
|
|
|
2012-08-10 18:47:19 +04:00
|
|
|
static void
|
|
|
|
text_model_preedit_styling(void *data,
|
2013-01-17 00:26:38 +04:00
|
|
|
struct text_model *text_model,
|
2013-01-17 00:26:39 +04:00
|
|
|
uint32_t serial,
|
2013-01-17 00:26:38 +04:00
|
|
|
uint32_t index,
|
|
|
|
uint32_t length,
|
|
|
|
uint32_t style)
|
|
|
|
{
|
2013-01-17 00:26:42 +04:00
|
|
|
struct text_entry *entry = data;
|
|
|
|
PangoAttribute *attr1 = NULL;
|
|
|
|
PangoAttribute *attr2 = NULL;
|
|
|
|
|
|
|
|
if (!entry->preedit_info.attr_list)
|
|
|
|
entry->preedit_info.attr_list = pango_attr_list_new();
|
|
|
|
|
|
|
|
switch (style) {
|
|
|
|
case TEXT_MODEL_PREEDIT_STYLE_DEFAULT:
|
|
|
|
case TEXT_MODEL_PREEDIT_STYLE_UNDERLINE:
|
|
|
|
attr1 = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
|
|
|
|
break;
|
|
|
|
case TEXT_MODEL_PREEDIT_STYLE_INCORRECT:
|
|
|
|
attr1 = pango_attr_underline_new(PANGO_UNDERLINE_ERROR);
|
|
|
|
attr2 = pango_attr_underline_color_new(65535, 0, 0);
|
|
|
|
break;
|
|
|
|
case TEXT_MODEL_PREEDIT_STYLE_SELECTION:
|
|
|
|
attr1 = pango_attr_background_new(0.3 * 65535, 0.3 * 65535, 65535);
|
|
|
|
attr2 = pango_attr_foreground_new(65535, 65535, 65535);
|
|
|
|
break;
|
|
|
|
case TEXT_MODEL_PREEDIT_STYLE_HIGHLIGHT:
|
|
|
|
case TEXT_MODEL_PREEDIT_STYLE_ACTIVE:
|
|
|
|
attr1 = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
|
|
|
|
attr2 = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
|
|
|
|
break;
|
|
|
|
case TEXT_MODEL_PREEDIT_STYLE_INACTIVE:
|
|
|
|
attr1 = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
|
|
|
|
attr2 = pango_attr_foreground_new(0.3 * 65535, 0.3 * 65535, 0.3 * 65535);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr1) {
|
|
|
|
attr1->start_index = entry->cursor + index;
|
|
|
|
attr1->end_index = entry->cursor + index + length;
|
|
|
|
pango_attr_list_insert(entry->preedit_info.attr_list, attr1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr2) {
|
|
|
|
attr2->start_index = entry->cursor + index;
|
|
|
|
attr2->end_index = entry->cursor + index + length;
|
|
|
|
pango_attr_list_insert(entry->preedit_info.attr_list, attr2);
|
|
|
|
}
|
2013-01-17 00:26:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
text_model_preedit_cursor(void *data,
|
|
|
|
struct text_model *text_model,
|
2013-01-17 00:26:39 +04:00
|
|
|
uint32_t serial,
|
2013-01-17 00:26:38 +04:00
|
|
|
int32_t index)
|
2012-08-10 18:47:19 +04:00
|
|
|
{
|
2013-01-17 00:26:38 +04:00
|
|
|
struct text_entry *entry = data;
|
|
|
|
|
|
|
|
entry->preedit_info.cursor = index;
|
2012-08-10 18:47:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-11-18 22:06:43 +04:00
|
|
|
text_model_modifiers_map(void *data,
|
|
|
|
struct text_model *text_model,
|
|
|
|
struct wl_array *map)
|
|
|
|
{
|
2012-11-18 22:06:44 +04:00
|
|
|
struct text_entry *entry = data;
|
|
|
|
|
|
|
|
entry->keysym.shift_mask = keysym_modifiers_get_mask(map, "Shift");
|
2012-11-18 22:06:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
text_model_keysym(void *data,
|
|
|
|
struct text_model *text_model,
|
|
|
|
uint32_t serial,
|
|
|
|
uint32_t time,
|
|
|
|
uint32_t key,
|
|
|
|
uint32_t state,
|
|
|
|
uint32_t modifiers)
|
2012-08-10 18:47:19 +04:00
|
|
|
{
|
2012-09-17 17:28:07 +04:00
|
|
|
struct text_entry *entry = data;
|
2012-11-18 22:06:44 +04:00
|
|
|
const char *state_label = "release";
|
|
|
|
const char *key_label = "Unknown";
|
2012-11-05 06:26:39 +04:00
|
|
|
const char *new_char;
|
2012-09-10 01:08:45 +04:00
|
|
|
|
|
|
|
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
|
|
|
|
state_label = "pressed";
|
|
|
|
}
|
|
|
|
|
2012-11-18 22:06:44 +04:00
|
|
|
if (key == XKB_KEY_Left ||
|
|
|
|
key == XKB_KEY_Right) {
|
|
|
|
if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (key == XKB_KEY_Left)
|
|
|
|
new_char = utf8_prev_char(entry->text, entry->text + entry->cursor);
|
|
|
|
else
|
|
|
|
new_char = utf8_next_char(entry->text + entry->cursor);
|
|
|
|
|
|
|
|
if (new_char != NULL) {
|
|
|
|
entry->cursor = new_char - entry->text;
|
|
|
|
if (!(modifiers & entry->keysym.shift_mask))
|
|
|
|
entry->anchor = entry->cursor;
|
|
|
|
widget_schedule_redraw(entry->widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-17 00:26:52 +04:00
|
|
|
if (key == XKB_KEY_BackSpace) {
|
|
|
|
const char *start, *end;
|
|
|
|
|
|
|
|
text_entry_commit_and_reset(entry);
|
|
|
|
|
|
|
|
start = utf8_prev_char(entry->text, entry->text + entry->cursor);
|
|
|
|
|
|
|
|
if (start == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
end = utf8_end_char(entry->text + entry->cursor);
|
|
|
|
text_entry_delete_text(entry,
|
|
|
|
start - entry->text,
|
|
|
|
end - start);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:45 +04:00
|
|
|
switch (key) {
|
|
|
|
case XKB_KEY_Tab:
|
|
|
|
key_label = "Tab";
|
|
|
|
break;
|
|
|
|
case XKB_KEY_KP_Enter:
|
2012-11-18 22:06:44 +04:00
|
|
|
case XKB_KEY_Return:
|
2012-09-10 01:08:45 +04:00
|
|
|
key_label = "Enter";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "%s key was %s.\n", key_label, state_label);
|
2012-08-10 18:47:19 +04:00
|
|
|
}
|
|
|
|
|
2012-08-10 18:47:21 +04:00
|
|
|
static void
|
2012-09-24 16:51:14 +04:00
|
|
|
text_model_enter(void *data,
|
|
|
|
struct text_model *text_model,
|
|
|
|
struct wl_surface *surface)
|
2012-08-10 18:47:21 +04:00
|
|
|
{
|
|
|
|
struct text_entry *entry = data;
|
|
|
|
|
2012-09-24 16:51:14 +04:00
|
|
|
if (surface != window_get_wl_surface(entry->window))
|
|
|
|
return;
|
|
|
|
|
2012-08-10 18:47:21 +04:00
|
|
|
entry->active = 1;
|
|
|
|
|
|
|
|
widget_schedule_redraw(entry->widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-09-24 16:51:14 +04:00
|
|
|
text_model_leave(void *data,
|
|
|
|
struct text_model *text_model)
|
2012-08-10 18:47:21 +04:00
|
|
|
{
|
|
|
|
struct text_entry *entry = data;
|
|
|
|
|
2013-01-17 00:26:40 +04:00
|
|
|
text_entry_commit_and_reset(entry);
|
|
|
|
|
2012-08-10 18:47:21 +04:00
|
|
|
entry->active = 0;
|
|
|
|
|
2013-01-31 18:52:21 +04:00
|
|
|
text_model_hide_input_panel(text_model);
|
|
|
|
|
2012-08-10 18:47:21 +04:00
|
|
|
widget_schedule_redraw(entry->widget);
|
|
|
|
}
|
|
|
|
|
2013-01-31 18:52:21 +04:00
|
|
|
static void
|
|
|
|
text_model_input_panel_state(void *data,
|
|
|
|
struct text_model *text_model,
|
|
|
|
uint32_t state)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-04-18 18:47:15 +04:00
|
|
|
static void
|
|
|
|
text_model_language(void *data,
|
|
|
|
struct text_model *text_model,
|
|
|
|
uint32_t serial,
|
|
|
|
const char *language)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
text_model_text_direction(void *data,
|
|
|
|
struct text_model *text_model,
|
|
|
|
uint32_t serial,
|
|
|
|
uint32_t direction)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-21 23:52:19 +04:00
|
|
|
static const struct text_model_listener text_model_listener = {
|
2012-08-10 18:47:19 +04:00
|
|
|
text_model_commit_string,
|
|
|
|
text_model_preedit_string,
|
2012-09-10 01:08:44 +04:00
|
|
|
text_model_delete_surrounding_text,
|
2013-01-31 18:52:23 +04:00
|
|
|
text_model_cursor_position,
|
2012-08-10 18:47:19 +04:00
|
|
|
text_model_preedit_styling,
|
2013-01-17 00:26:38 +04:00
|
|
|
text_model_preedit_cursor,
|
2012-11-18 22:06:43 +04:00
|
|
|
text_model_modifiers_map,
|
|
|
|
text_model_keysym,
|
2012-09-24 16:51:14 +04:00
|
|
|
text_model_enter,
|
2013-01-31 18:52:21 +04:00
|
|
|
text_model_leave,
|
2013-04-18 18:47:15 +04:00
|
|
|
text_model_input_panel_state,
|
|
|
|
text_model_language,
|
|
|
|
text_model_text_direction
|
2012-06-21 23:52:19 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct text_entry*
|
|
|
|
text_entry_create(struct editor *editor, const char *text)
|
|
|
|
{
|
|
|
|
struct text_entry *entry;
|
|
|
|
|
2012-11-18 22:06:44 +04:00
|
|
|
entry = calloc(1, sizeof *entry);
|
2012-06-21 23:52:19 +04:00
|
|
|
|
2012-09-10 01:08:34 +04:00
|
|
|
entry->widget = widget_add_widget(editor->widget, entry);
|
2012-08-10 18:47:22 +04:00
|
|
|
entry->window = editor->window;
|
2012-06-21 23:52:19 +04:00
|
|
|
entry->text = strdup(text);
|
|
|
|
entry->active = 0;
|
2012-09-10 01:08:36 +04:00
|
|
|
entry->cursor = strlen(text);
|
2012-09-10 01:08:38 +04:00
|
|
|
entry->anchor = entry->cursor;
|
2012-09-10 01:08:30 +04:00
|
|
|
entry->model = text_model_factory_create_text_model(editor->text_model_factory);
|
2012-06-21 23:52:19 +04:00
|
|
|
text_model_add_listener(entry->model, &text_model_listener, entry);
|
|
|
|
|
2012-09-10 01:08:34 +04:00
|
|
|
widget_set_redraw_handler(entry->widget, text_entry_redraw_handler);
|
|
|
|
widget_set_button_handler(entry->widget, text_entry_button_handler);
|
|
|
|
|
2012-06-21 23:52:19 +04:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
text_entry_destroy(struct text_entry *entry)
|
|
|
|
{
|
2012-09-10 01:08:34 +04:00
|
|
|
widget_destroy(entry->widget);
|
2012-06-21 23:52:19 +04:00
|
|
|
text_model_destroy(entry->model);
|
2013-01-17 00:26:42 +04:00
|
|
|
g_clear_object(&entry->layout);
|
2012-06-21 23:52:19 +04:00
|
|
|
free(entry->text);
|
|
|
|
free(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
redraw_handler(struct widget *widget, void *data)
|
|
|
|
{
|
|
|
|
struct editor *editor = data;
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
struct rectangle allocation;
|
|
|
|
cairo_t *cr;
|
|
|
|
|
|
|
|
surface = window_get_surface(editor->window);
|
|
|
|
widget_get_allocation(editor->widget, &allocation);
|
|
|
|
|
|
|
|
cr = cairo_create(surface);
|
|
|
|
cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
|
|
|
|
cairo_clip(cr);
|
|
|
|
|
|
|
|
cairo_translate(cr, allocation.x, allocation.y);
|
|
|
|
|
|
|
|
/* Draw background */
|
|
|
|
cairo_push_group(cr);
|
2012-09-10 01:08:34 +04:00
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
|
2012-06-21 23:52:19 +04:00
|
|
|
cairo_set_source_rgba(cr, 1, 1, 1, 1);
|
|
|
|
cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
|
|
|
|
cairo_fill(cr);
|
|
|
|
|
|
|
|
cairo_pop_group_to_source(cr);
|
|
|
|
cairo_paint(cr);
|
|
|
|
|
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
text_entry_allocate(struct text_entry *entry, int32_t x, int32_t y,
|
|
|
|
int32_t width, int32_t height)
|
|
|
|
{
|
2012-09-10 01:08:34 +04:00
|
|
|
widget_set_allocation(entry->widget, x, y, width, height);
|
2012-06-21 23:52:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
resize_handler(struct widget *widget,
|
|
|
|
int32_t width, int32_t height, void *data)
|
|
|
|
{
|
|
|
|
struct editor *editor = data;
|
2012-09-10 01:08:34 +04:00
|
|
|
struct rectangle allocation;
|
2012-06-21 23:52:19 +04:00
|
|
|
|
2012-09-10 01:08:34 +04:00
|
|
|
widget_get_allocation(editor->widget, &allocation);
|
2012-06-21 23:52:19 +04:00
|
|
|
|
2012-09-10 01:08:34 +04:00
|
|
|
text_entry_allocate(editor->entry,
|
|
|
|
allocation.x + 20, allocation.y + 20,
|
|
|
|
width - 40, height / 2 - 40);
|
|
|
|
text_entry_allocate(editor->editor,
|
|
|
|
allocation.x + 20, allocation.y + height / 2 + 20,
|
|
|
|
width - 40, height / 2 - 40);
|
2012-06-21 23:52:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-08-10 18:47:22 +04:00
|
|
|
text_entry_activate(struct text_entry *entry,
|
2012-09-10 01:08:34 +04:00
|
|
|
struct wl_seat *seat)
|
2012-06-21 23:52:19 +04:00
|
|
|
{
|
2012-08-10 18:47:22 +04:00
|
|
|
struct wl_surface *surface = window_get_wl_surface(entry->window);
|
|
|
|
|
2013-01-31 18:52:21 +04:00
|
|
|
if (entry->click_to_show && entry->active) {
|
|
|
|
text_model_show_input_panel(entry->model);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry->click_to_show)
|
|
|
|
text_model_show_input_panel(entry->model);
|
|
|
|
|
2013-01-17 00:26:39 +04:00
|
|
|
entry->serial++;
|
|
|
|
|
2012-08-10 18:47:22 +04:00
|
|
|
text_model_activate(entry->model,
|
2013-01-17 00:26:39 +04:00
|
|
|
entry->serial,
|
2012-08-10 18:47:22 +04:00
|
|
|
seat,
|
|
|
|
surface);
|
2012-06-21 23:52:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-08-10 18:47:22 +04:00
|
|
|
text_entry_deactivate(struct text_entry *entry,
|
|
|
|
struct wl_seat *seat)
|
2012-06-21 23:52:19 +04:00
|
|
|
{
|
2012-08-10 18:47:22 +04:00
|
|
|
text_model_deactivate(entry->model,
|
|
|
|
seat);
|
2012-06-21 23:52:19 +04:00
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:39 +04:00
|
|
|
static void
|
|
|
|
text_entry_update_layout(struct text_entry *entry)
|
|
|
|
{
|
|
|
|
char *text;
|
2013-01-17 00:26:42 +04:00
|
|
|
PangoAttrList *attr_list;
|
2012-09-10 01:08:39 +04:00
|
|
|
|
2012-10-02 13:06:51 +04:00
|
|
|
assert(((unsigned int)entry->cursor) <= strlen(entry->text) +
|
2013-01-17 00:26:38 +04:00
|
|
|
(entry->preedit.text ? strlen(entry->preedit.text) : 0));
|
2012-09-10 01:08:39 +04:00
|
|
|
|
2013-01-17 00:26:42 +04:00
|
|
|
if (entry->preedit.text) {
|
|
|
|
text = malloc(strlen(entry->text) + strlen(entry->preedit.text) + 1);
|
|
|
|
strncpy(text, entry->text, entry->cursor);
|
|
|
|
strcpy(text + entry->cursor, entry->preedit.text);
|
|
|
|
strcpy(text + entry->cursor + strlen(entry->preedit.text),
|
|
|
|
entry->text + entry->cursor);
|
|
|
|
} else {
|
|
|
|
text = strdup(entry->text);
|
2012-09-10 01:08:39 +04:00
|
|
|
}
|
|
|
|
|
2013-01-17 00:26:42 +04:00
|
|
|
if (entry->cursor != entry->anchor) {
|
|
|
|
int start_index = MIN(entry->cursor, entry->anchor);
|
|
|
|
int end_index = MAX(entry->cursor, entry->anchor);
|
|
|
|
PangoAttribute *attr;
|
2012-09-10 01:08:39 +04:00
|
|
|
|
2013-01-17 00:26:42 +04:00
|
|
|
attr_list = pango_attr_list_copy(entry->preedit.attr_list);
|
2012-09-10 01:08:39 +04:00
|
|
|
|
2013-01-17 00:26:42 +04:00
|
|
|
if (!attr_list)
|
|
|
|
attr_list = pango_attr_list_new();
|
2012-09-10 01:08:40 +04:00
|
|
|
|
2013-01-17 00:26:42 +04:00
|
|
|
attr = pango_attr_background_new(0.3 * 65535, 0.3 * 65535, 65535);
|
|
|
|
attr->start_index = start_index;
|
|
|
|
attr->end_index = end_index;
|
|
|
|
pango_attr_list_insert(attr_list, attr);
|
|
|
|
|
|
|
|
attr = pango_attr_foreground_new(65535, 65535, 65535);
|
|
|
|
attr->start_index = start_index;
|
|
|
|
attr->end_index = end_index;
|
|
|
|
pango_attr_list_insert(attr_list, attr);
|
|
|
|
} else {
|
|
|
|
attr_list = pango_attr_list_ref(entry->preedit.attr_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->preedit.text && !entry->preedit.attr_list) {
|
|
|
|
PangoAttribute *attr;
|
|
|
|
|
|
|
|
if (!attr_list)
|
|
|
|
attr_list = pango_attr_list_new();
|
|
|
|
|
|
|
|
attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
|
|
|
|
attr->start_index = entry->cursor;
|
|
|
|
attr->end_index = entry->cursor + strlen(entry->preedit.text);
|
|
|
|
pango_attr_list_insert(attr_list, attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->layout) {
|
|
|
|
pango_layout_set_text(entry->layout, text, -1);
|
|
|
|
pango_layout_set_attributes(entry->layout, attr_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(text);
|
|
|
|
pango_attr_list_unref(attr_list);
|
2012-09-10 01:08:39 +04:00
|
|
|
}
|
|
|
|
|
2013-01-17 00:26:45 +04:00
|
|
|
static void
|
|
|
|
text_entry_update(struct text_entry *entry)
|
|
|
|
{
|
|
|
|
text_model_set_content_type(entry->model,
|
|
|
|
TEXT_MODEL_CONTENT_HINT_NONE,
|
|
|
|
entry->content_purpose);
|
|
|
|
|
|
|
|
text_model_set_surrounding_text(entry->model,
|
|
|
|
entry->text,
|
|
|
|
entry->cursor,
|
|
|
|
entry->anchor);
|
2013-01-31 18:52:20 +04:00
|
|
|
|
|
|
|
text_model_commit(entry->model);
|
2013-01-17 00:26:45 +04:00
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:37 +04:00
|
|
|
static void
|
2013-01-31 18:52:23 +04:00
|
|
|
text_entry_insert_at_cursor(struct text_entry *entry, const char *text,
|
|
|
|
int32_t cursor, int32_t anchor)
|
2012-09-10 01:08:37 +04:00
|
|
|
{
|
|
|
|
char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
|
|
|
|
|
|
|
|
strncpy(new_text, entry->text, entry->cursor);
|
|
|
|
strcpy(new_text + entry->cursor, text);
|
|
|
|
strcpy(new_text + entry->cursor + strlen(text),
|
|
|
|
entry->text + entry->cursor);
|
|
|
|
|
|
|
|
free(entry->text);
|
|
|
|
entry->text = new_text;
|
2013-01-31 18:52:23 +04:00
|
|
|
if (anchor >= 0)
|
|
|
|
entry->anchor = entry->cursor + strlen(text) + anchor;
|
|
|
|
else
|
|
|
|
entry->anchor = entry->cursor + 1 + anchor;
|
|
|
|
if (cursor >= 0)
|
|
|
|
entry->cursor += strlen(text) + cursor;
|
|
|
|
else
|
|
|
|
entry->cursor += 1 + cursor;
|
2012-09-10 01:08:37 +04:00
|
|
|
|
2012-09-10 01:08:39 +04:00
|
|
|
text_entry_update_layout(entry);
|
2013-01-17 00:26:42 +04:00
|
|
|
|
|
|
|
widget_schedule_redraw(entry->widget);
|
|
|
|
|
2013-01-17 00:26:45 +04:00
|
|
|
text_entry_update(entry);
|
2012-09-10 01:08:39 +04:00
|
|
|
}
|
|
|
|
|
2013-01-17 00:26:40 +04:00
|
|
|
static void
|
|
|
|
text_entry_reset_preedit(struct text_entry *entry)
|
|
|
|
{
|
|
|
|
entry->preedit.cursor = 0;
|
|
|
|
|
|
|
|
free(entry->preedit.text);
|
|
|
|
entry->preedit.text = NULL;
|
|
|
|
|
|
|
|
free(entry->preedit.commit);
|
|
|
|
entry->preedit.commit = NULL;
|
2013-01-17 00:26:42 +04:00
|
|
|
|
|
|
|
pango_attr_list_unref(entry->preedit.attr_list);
|
|
|
|
entry->preedit.attr_list = NULL;
|
2013-01-17 00:26:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
text_entry_commit_and_reset(struct text_entry *entry)
|
|
|
|
{
|
|
|
|
char *commit = NULL;
|
|
|
|
|
|
|
|
if (entry->preedit.commit)
|
|
|
|
commit = strdup(entry->preedit.commit);
|
|
|
|
|
|
|
|
text_entry_reset_preedit(entry);
|
|
|
|
if (commit) {
|
2013-01-31 18:52:23 +04:00
|
|
|
text_entry_insert_at_cursor(entry, commit, 0, 0);
|
2013-01-17 00:26:40 +04:00
|
|
|
free(commit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:39 +04:00
|
|
|
static void
|
|
|
|
text_entry_set_preedit(struct text_entry *entry,
|
|
|
|
const char *preedit_text,
|
|
|
|
int preedit_cursor)
|
|
|
|
{
|
2013-01-17 00:26:40 +04:00
|
|
|
text_entry_reset_preedit(entry);
|
2012-09-10 01:08:39 +04:00
|
|
|
|
|
|
|
if (!preedit_text)
|
|
|
|
return;
|
|
|
|
|
2013-01-17 00:26:38 +04:00
|
|
|
entry->preedit.text = strdup(preedit_text);
|
|
|
|
entry->preedit.cursor = preedit_cursor;
|
2012-09-10 01:08:39 +04:00
|
|
|
|
|
|
|
text_entry_update_layout(entry);
|
2013-01-17 00:26:42 +04:00
|
|
|
|
|
|
|
widget_schedule_redraw(entry->widget);
|
2012-09-10 01:08:37 +04:00
|
|
|
}
|
|
|
|
|
2013-01-17 00:26:47 +04:00
|
|
|
static uint32_t
|
|
|
|
text_entry_try_invoke_preedit_action(struct text_entry *entry,
|
|
|
|
int32_t x, int32_t y,
|
|
|
|
uint32_t button,
|
|
|
|
enum wl_pointer_button_state state)
|
|
|
|
{
|
|
|
|
int index, trailing;
|
|
|
|
uint32_t cursor;
|
|
|
|
|
|
|
|
if (!entry->preedit.text)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pango_layout_xy_to_index(entry->layout,
|
|
|
|
x * PANGO_SCALE, y * PANGO_SCALE,
|
|
|
|
&index, &trailing);
|
|
|
|
cursor = index + trailing;
|
|
|
|
|
|
|
|
if (cursor < entry->cursor ||
|
|
|
|
cursor > entry->cursor + strlen(entry->preedit.text)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == WL_POINTER_BUTTON_STATE_RELEASED)
|
|
|
|
text_model_invoke_action(entry->model,
|
|
|
|
button,
|
|
|
|
cursor - entry->cursor);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:36 +04:00
|
|
|
static void
|
|
|
|
text_entry_set_cursor_position(struct text_entry *entry,
|
|
|
|
int32_t x, int32_t y)
|
|
|
|
{
|
2013-01-17 00:26:42 +04:00
|
|
|
int index, trailing;
|
|
|
|
|
2013-01-17 00:26:40 +04:00
|
|
|
text_entry_commit_and_reset(entry);
|
|
|
|
|
2013-01-17 00:26:42 +04:00
|
|
|
pango_layout_xy_to_index(entry->layout,
|
|
|
|
x * PANGO_SCALE, y * PANGO_SCALE,
|
|
|
|
&index, &trailing);
|
|
|
|
entry->cursor = index + trailing;
|
2012-09-10 01:08:36 +04:00
|
|
|
|
2013-01-17 00:26:39 +04:00
|
|
|
entry->serial++;
|
|
|
|
|
|
|
|
text_model_reset(entry->model, entry->serial);
|
2012-09-10 01:08:46 +04:00
|
|
|
|
2012-09-10 01:08:39 +04:00
|
|
|
text_entry_update_layout(entry);
|
|
|
|
|
2012-09-10 01:08:36 +04:00
|
|
|
widget_schedule_redraw(entry->widget);
|
2013-01-17 00:26:42 +04:00
|
|
|
|
2013-01-17 00:26:45 +04:00
|
|
|
text_entry_update(entry);
|
2012-09-10 01:08:36 +04:00
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:38 +04:00
|
|
|
static void
|
|
|
|
text_entry_set_anchor_position(struct text_entry *entry,
|
|
|
|
int32_t x, int32_t y)
|
|
|
|
{
|
2013-01-17 00:26:42 +04:00
|
|
|
int index, trailing;
|
|
|
|
|
|
|
|
pango_layout_xy_to_index(entry->layout,
|
|
|
|
x * PANGO_SCALE, y * PANGO_SCALE,
|
|
|
|
&index, &trailing);
|
|
|
|
entry->anchor = index + trailing;
|
|
|
|
|
|
|
|
text_entry_update_layout(entry);
|
2012-09-10 01:08:38 +04:00
|
|
|
|
|
|
|
widget_schedule_redraw(entry->widget);
|
2013-01-17 00:26:42 +04:00
|
|
|
|
2013-01-17 00:26:45 +04:00
|
|
|
text_entry_update(entry);
|
2012-09-10 01:08:38 +04:00
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:44 +04:00
|
|
|
static void
|
|
|
|
text_entry_delete_text(struct text_entry *entry,
|
|
|
|
uint32_t index, uint32_t length)
|
|
|
|
{
|
|
|
|
if (entry->cursor > index)
|
|
|
|
entry->cursor -= length;
|
|
|
|
|
2012-09-17 17:28:10 +04:00
|
|
|
entry->anchor = entry->cursor;
|
|
|
|
|
2012-09-10 01:08:44 +04:00
|
|
|
entry->text[index] = '\0';
|
|
|
|
strcat(entry->text, entry->text + index + length);
|
|
|
|
|
|
|
|
text_entry_update_layout(entry);
|
|
|
|
|
|
|
|
widget_schedule_redraw(entry->widget);
|
2013-01-17 00:26:42 +04:00
|
|
|
|
2013-01-17 00:26:45 +04:00
|
|
|
text_entry_update(entry);
|
2012-09-10 01:08:44 +04:00
|
|
|
}
|
|
|
|
|
2012-09-17 17:28:09 +04:00
|
|
|
static void
|
|
|
|
text_entry_delete_selected_text(struct text_entry *entry)
|
|
|
|
{
|
|
|
|
uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor;
|
|
|
|
uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor;
|
|
|
|
|
|
|
|
if (entry->anchor == entry->cursor)
|
|
|
|
return;
|
|
|
|
|
|
|
|
text_entry_delete_text(entry, start_index, end_index - start_index);
|
|
|
|
|
|
|
|
entry->anchor = entry->cursor;
|
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:36 +04:00
|
|
|
static void
|
|
|
|
text_entry_draw_cursor(struct text_entry *entry, cairo_t *cr)
|
|
|
|
{
|
2013-01-17 00:26:42 +04:00
|
|
|
PangoRectangle extents;
|
|
|
|
PangoRectangle cursor_pos;
|
2012-09-10 01:08:36 +04:00
|
|
|
|
2013-01-17 00:26:38 +04:00
|
|
|
if (entry->preedit.text && entry->preedit.cursor < 0)
|
|
|
|
return;
|
|
|
|
|
2013-01-17 00:26:42 +04:00
|
|
|
pango_layout_get_extents(entry->layout, &extents, NULL);
|
|
|
|
pango_layout_get_cursor_pos(entry->layout,
|
|
|
|
entry->cursor + entry->preedit.cursor,
|
|
|
|
&cursor_pos, NULL);
|
2012-09-10 01:08:36 +04:00
|
|
|
|
|
|
|
cairo_set_line_width(cr, 1.0);
|
2013-01-17 00:26:42 +04:00
|
|
|
cairo_move_to(cr, PANGO_PIXELS(cursor_pos.x), PANGO_PIXELS(extents.height) + 2);
|
|
|
|
cairo_line_to(cr, PANGO_PIXELS(cursor_pos.x), - 2);
|
2012-09-10 01:08:36 +04:00
|
|
|
cairo_stroke(cr);
|
|
|
|
}
|
|
|
|
|
2012-10-02 13:06:53 +04:00
|
|
|
static const int text_offset_left = 10;
|
|
|
|
|
2012-06-21 23:52:19 +04:00
|
|
|
static void
|
2012-09-10 01:08:34 +04:00
|
|
|
text_entry_redraw_handler(struct widget *widget, void *data)
|
2012-06-21 23:52:19 +04:00
|
|
|
{
|
2012-09-10 01:08:34 +04:00
|
|
|
struct text_entry *entry = data;
|
|
|
|
cairo_surface_t *surface;
|
2012-06-21 23:52:19 +04:00
|
|
|
struct rectangle allocation;
|
2012-09-10 01:08:34 +04:00
|
|
|
cairo_t *cr;
|
|
|
|
|
|
|
|
surface = window_get_surface(entry->window);
|
|
|
|
widget_get_allocation(entry->widget, &allocation);
|
|
|
|
|
|
|
|
cr = cairo_create(surface);
|
|
|
|
cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
|
|
|
|
cairo_clip(cr);
|
2012-06-21 23:52:19 +04:00
|
|
|
|
2012-09-10 01:08:34 +04:00
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
|
|
|
|
|
|
|
|
cairo_push_group(cr);
|
|
|
|
cairo_translate(cr, allocation.x, allocation.y);
|
|
|
|
|
|
|
|
cairo_set_source_rgba(cr, 1, 1, 1, 1);
|
|
|
|
cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
|
|
|
|
cairo_fill(cr);
|
|
|
|
|
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
|
|
|
|
|
|
|
|
if (entry->active) {
|
|
|
|
cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
|
|
|
|
cairo_set_line_width (cr, 3);
|
|
|
|
cairo_set_source_rgba(cr, 0, 0, 1, 1.0);
|
|
|
|
cairo_stroke(cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_set_source_rgba(cr, 0, 0, 0, 1);
|
|
|
|
|
2012-10-02 13:06:53 +04:00
|
|
|
cairo_translate(cr, text_offset_left, allocation.height / 2);
|
2012-09-10 01:08:36 +04:00
|
|
|
|
2013-01-17 00:26:42 +04:00
|
|
|
if (!entry->layout)
|
|
|
|
entry->layout = pango_cairo_create_layout(cr);
|
|
|
|
else
|
|
|
|
pango_cairo_update_layout(cr, entry->layout);
|
2012-09-10 01:08:38 +04:00
|
|
|
|
2013-01-17 00:26:42 +04:00
|
|
|
text_entry_update_layout(entry);
|
2012-09-10 01:08:36 +04:00
|
|
|
|
2013-01-17 00:26:42 +04:00
|
|
|
pango_cairo_show_layout(cr, entry->layout);
|
|
|
|
|
|
|
|
text_entry_draw_cursor(entry, cr);
|
2012-09-10 01:08:39 +04:00
|
|
|
|
2012-09-10 01:08:34 +04:00
|
|
|
cairo_pop_group_to_source(cr);
|
|
|
|
cairo_paint(cr);
|
|
|
|
|
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:38 +04:00
|
|
|
static int
|
|
|
|
text_entry_motion_handler(struct widget *widget,
|
|
|
|
struct input *input, uint32_t time,
|
|
|
|
float x, float y, void *data)
|
|
|
|
{
|
|
|
|
struct text_entry *entry = data;
|
|
|
|
struct rectangle allocation;
|
|
|
|
|
|
|
|
widget_get_allocation(entry->widget, &allocation);
|
|
|
|
|
|
|
|
text_entry_set_cursor_position(entry,
|
2012-10-02 13:06:53 +04:00
|
|
|
x - allocation.x - text_offset_left,
|
|
|
|
y - allocation.y - text_offset_left);
|
2012-09-10 01:08:38 +04:00
|
|
|
|
|
|
|
return CURSOR_IBEAM;
|
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:34 +04:00
|
|
|
static void
|
|
|
|
text_entry_button_handler(struct widget *widget,
|
|
|
|
struct input *input, uint32_t time,
|
|
|
|
uint32_t button,
|
|
|
|
enum wl_pointer_button_state state, void *data)
|
|
|
|
{
|
|
|
|
struct text_entry *entry = data;
|
2012-09-10 01:08:36 +04:00
|
|
|
struct rectangle allocation;
|
2012-11-18 22:06:49 +04:00
|
|
|
struct editor *editor;
|
2012-09-10 01:08:36 +04:00
|
|
|
int32_t x, y;
|
2013-01-17 00:26:47 +04:00
|
|
|
uint32_t result;
|
2012-09-10 01:08:36 +04:00
|
|
|
|
|
|
|
widget_get_allocation(entry->widget, &allocation);
|
|
|
|
input_get_position(input, &x, &y);
|
2012-09-10 01:08:34 +04:00
|
|
|
|
2013-01-17 00:26:47 +04:00
|
|
|
x -= allocation.x + text_offset_left;
|
|
|
|
y -= allocation.y + text_offset_left;
|
|
|
|
|
2012-11-18 22:06:49 +04:00
|
|
|
editor = window_get_user_data(entry->window);
|
|
|
|
|
2013-01-17 00:26:47 +04:00
|
|
|
result = text_entry_try_invoke_preedit_action(entry, x, y, button, state);
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
return;
|
|
|
|
|
2012-09-10 01:08:34 +04:00
|
|
|
if (button != BTN_LEFT) {
|
2012-06-21 23:52:19 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-17 00:26:47 +04:00
|
|
|
text_entry_set_cursor_position(entry, x, y);
|
2012-09-10 01:08:36 +04:00
|
|
|
|
2012-09-10 01:08:34 +04:00
|
|
|
if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
|
|
|
|
struct wl_seat *seat = input_get_seat(input);
|
2012-06-21 23:52:19 +04:00
|
|
|
|
2012-09-10 01:08:34 +04:00
|
|
|
text_entry_activate(entry, seat);
|
2012-11-18 22:06:49 +04:00
|
|
|
editor->active_entry = entry;
|
2012-09-10 01:08:38 +04:00
|
|
|
|
2013-01-17 00:26:47 +04:00
|
|
|
text_entry_set_anchor_position(entry, x, y);
|
2012-09-10 01:08:38 +04:00
|
|
|
|
|
|
|
widget_set_motion_handler(entry->widget, text_entry_motion_handler);
|
|
|
|
} else {
|
|
|
|
widget_set_motion_handler(entry->widget, NULL);
|
2012-09-10 01:08:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
editor_button_handler(struct widget *widget,
|
|
|
|
struct input *input, uint32_t time,
|
|
|
|
uint32_t button,
|
|
|
|
enum wl_pointer_button_state state, void *data)
|
|
|
|
{
|
|
|
|
struct editor *editor = data;
|
2012-06-21 23:52:19 +04:00
|
|
|
|
2012-09-10 01:08:34 +04:00
|
|
|
if (button != BTN_LEFT) {
|
|
|
|
return;
|
|
|
|
}
|
2012-07-12 00:25:29 +04:00
|
|
|
|
2012-09-10 01:08:34 +04:00
|
|
|
if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
|
|
|
|
struct wl_seat *seat = input_get_seat(input);
|
2012-08-10 18:47:22 +04:00
|
|
|
|
|
|
|
text_entry_deactivate(editor->entry, seat);
|
|
|
|
text_entry_deactivate(editor->editor, seat);
|
2012-11-18 22:06:49 +04:00
|
|
|
editor->active_entry = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
key_handler(struct window *window,
|
|
|
|
struct input *input, uint32_t time,
|
|
|
|
uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct editor *editor = data;
|
|
|
|
struct text_entry *entry;
|
|
|
|
const char *start, *end, *new_char;
|
|
|
|
char text[16];
|
|
|
|
|
|
|
|
if (!editor->active_entry)
|
|
|
|
return;
|
|
|
|
|
|
|
|
entry = editor->active_entry;
|
|
|
|
|
|
|
|
if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (sym) {
|
|
|
|
case XKB_KEY_BackSpace:
|
2013-01-17 00:26:40 +04:00
|
|
|
text_entry_commit_and_reset(entry);
|
|
|
|
|
2012-11-18 22:06:49 +04:00
|
|
|
start = utf8_prev_char(entry->text, entry->text + entry->cursor);
|
|
|
|
|
|
|
|
if (start == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
end = utf8_end_char(entry->text + entry->cursor);
|
|
|
|
text_entry_delete_text(entry,
|
|
|
|
start - entry->text,
|
|
|
|
end - start);
|
|
|
|
break;
|
|
|
|
case XKB_KEY_Delete:
|
2013-01-17 00:26:40 +04:00
|
|
|
text_entry_commit_and_reset(entry);
|
|
|
|
|
2012-11-18 22:06:49 +04:00
|
|
|
start = utf8_start_char(entry->text, entry->text + entry->cursor);
|
|
|
|
|
|
|
|
if (start == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
end = utf8_next_char(start);
|
|
|
|
|
|
|
|
if (end == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
text_entry_delete_text(entry,
|
|
|
|
start - entry->text,
|
|
|
|
end - start);
|
|
|
|
break;
|
|
|
|
case XKB_KEY_Left:
|
2013-01-17 00:26:40 +04:00
|
|
|
text_entry_commit_and_reset(entry);
|
|
|
|
|
2012-11-18 22:06:49 +04:00
|
|
|
new_char = utf8_prev_char(entry->text, entry->text + entry->cursor);
|
|
|
|
if (new_char != NULL) {
|
|
|
|
entry->cursor = new_char - entry->text;
|
|
|
|
entry->anchor = entry->cursor;
|
|
|
|
widget_schedule_redraw(entry->widget);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case XKB_KEY_Right:
|
2013-01-17 00:26:40 +04:00
|
|
|
text_entry_commit_and_reset(entry);
|
|
|
|
|
2012-11-18 22:06:49 +04:00
|
|
|
new_char = utf8_next_char(entry->text + entry->cursor);
|
|
|
|
if (new_char != NULL) {
|
|
|
|
entry->cursor = new_char - entry->text;
|
|
|
|
entry->anchor = entry->cursor;
|
|
|
|
widget_schedule_redraw(entry->widget);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0)
|
|
|
|
break;
|
|
|
|
|
2013-01-17 00:26:40 +04:00
|
|
|
text_entry_commit_and_reset(entry);
|
|
|
|
|
2013-01-31 18:52:23 +04:00
|
|
|
text_entry_insert_at_cursor(entry, text, 0, 0);
|
2012-11-18 22:06:49 +04:00
|
|
|
break;
|
2012-06-21 23:52:19 +04:00
|
|
|
}
|
2012-11-18 22:06:49 +04:00
|
|
|
|
|
|
|
widget_schedule_redraw(entry->widget);
|
2012-06-21 23:52:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-10-11 05:34:26 +04:00
|
|
|
global_handler(struct display *display, uint32_t name,
|
2012-06-21 23:52:19 +04:00
|
|
|
const char *interface, uint32_t version, void *data)
|
|
|
|
{
|
|
|
|
struct editor *editor = data;
|
|
|
|
|
2012-08-10 18:47:20 +04:00
|
|
|
if (!strcmp(interface, "text_model_factory")) {
|
2012-10-11 05:34:26 +04:00
|
|
|
editor->text_model_factory =
|
|
|
|
display_bind(display, name,
|
|
|
|
&text_model_factory_interface, 1);
|
2012-06-21 23:52:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct editor editor;
|
2013-01-31 18:52:21 +04:00
|
|
|
int i;
|
|
|
|
uint32_t click_to_show = 0;
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
if (strcmp("--click-to-show", argv[i]) == 0)
|
|
|
|
click_to_show = 1;
|
2012-06-21 23:52:19 +04:00
|
|
|
|
2012-11-05 06:26:40 +04:00
|
|
|
memset(&editor, 0, sizeof editor);
|
|
|
|
|
2013-01-17 00:26:42 +04:00
|
|
|
#ifdef HAVE_PANGO
|
|
|
|
g_type_init();
|
|
|
|
#endif
|
|
|
|
|
2013-02-21 00:27:49 +04:00
|
|
|
editor.display = display_create(&argc, argv);
|
2012-06-21 23:52:19 +04:00
|
|
|
if (editor.display == NULL) {
|
|
|
|
fprintf(stderr, "failed to create display: %m\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-11 05:34:26 +04:00
|
|
|
display_set_user_data(editor.display, &editor);
|
|
|
|
display_set_global_handler(editor.display, global_handler);
|
2012-06-21 23:52:19 +04:00
|
|
|
|
|
|
|
editor.window = window_create(editor.display);
|
|
|
|
editor.widget = frame_create(editor.window, &editor);
|
|
|
|
|
|
|
|
editor.entry = text_entry_create(&editor, "Entry");
|
2013-01-31 18:52:21 +04:00
|
|
|
editor.entry->click_to_show = click_to_show;
|
2013-01-17 00:26:45 +04:00
|
|
|
editor.editor = text_entry_create(&editor, "Numeric");
|
|
|
|
editor.editor->content_purpose = TEXT_MODEL_CONTENT_PURPOSE_NUMBER;
|
2013-01-31 18:52:21 +04:00
|
|
|
editor.editor->click_to_show = click_to_show;
|
2012-06-21 23:52:19 +04:00
|
|
|
|
|
|
|
window_set_title(editor.window, "Text Editor");
|
2012-11-18 22:06:49 +04:00
|
|
|
window_set_key_handler(editor.window, key_handler);
|
|
|
|
window_set_user_data(editor.window, &editor);
|
2012-06-21 23:52:19 +04:00
|
|
|
|
|
|
|
widget_set_redraw_handler(editor.widget, redraw_handler);
|
|
|
|
widget_set_resize_handler(editor.widget, resize_handler);
|
2012-09-10 01:08:34 +04:00
|
|
|
widget_set_button_handler(editor.widget, editor_button_handler);
|
2012-06-21 23:52:19 +04:00
|
|
|
|
|
|
|
window_schedule_resize(editor.window, 500, 400);
|
|
|
|
|
|
|
|
display_run(editor.display);
|
|
|
|
|
|
|
|
text_entry_destroy(editor.entry);
|
|
|
|
text_entry_destroy(editor.editor);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|