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>
|
2013-05-30 15:57:01 +04:00
|
|
|
#include <stdbool.h>
|
2012-06-21 23:52:19 +04:00
|
|
|
|
|
|
|
#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;
|
2013-04-18 18:47:34 +04:00
|
|
|
uint32_t delete_index;
|
|
|
|
uint32_t delete_length;
|
2013-05-30 15:57:05 +04:00
|
|
|
bool invalid_delete;
|
2013-01-31 18:52:23 +04:00
|
|
|
} pending_commit;
|
2013-04-18 18:47:36 +04:00
|
|
|
struct wl_text_input *text_input;
|
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-04-18 18:47:33 +04:00
|
|
|
uint32_t reset_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;
|
2013-04-18 18:47:16 +04:00
|
|
|
char *preferred_language;
|
2013-05-30 15:57:01 +04:00
|
|
|
bool button_pressed;
|
2012-06-21 23:52:19 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct editor {
|
2013-04-18 18:47:36 +04:00
|
|
|
struct wl_text_input_manager *text_input_manager;
|
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 *
|
2013-04-18 18:47:42 +04:00
|
|
|
utf8_end_char(const char *p)
|
2012-11-05 06:26:39 +04:00
|
|
|
{
|
2013-04-18 18:47:42 +04:00
|
|
|
while ((*p & 0xc0) == 0x80)
|
|
|
|
p++;
|
|
|
|
return p;
|
2012-11-05 06:26:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2013-04-18 18:47:42 +04:00
|
|
|
utf8_prev_char(const char *s, const char *p)
|
2012-11-05 06:26:39 +04:00
|
|
|
{
|
2013-04-18 18:47:42 +04:00
|
|
|
for (--p; p >= s; --p) {
|
|
|
|
if ((*p & 0xc0) != 0x80)
|
|
|
|
return p;
|
|
|
|
}
|
2012-11-05 06:26:39 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
utf8_next_char(const char *p)
|
|
|
|
{
|
|
|
|
if (*p != 0)
|
|
|
|
return utf8_end_char(++p);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-04-18 14:50:14 +04:00
|
|
|
static void
|
|
|
|
move_up(const char *p, uint32_t *cursor)
|
|
|
|
{
|
|
|
|
const char *posr, *posr_i;
|
|
|
|
char text[16];
|
|
|
|
|
|
|
|
xkb_keysym_to_utf8(XKB_KEY_Return, text, sizeof(text));
|
|
|
|
|
|
|
|
posr = strstr(p, text);
|
|
|
|
while (posr) {
|
|
|
|
if (*cursor > (unsigned)(posr-p)) {
|
|
|
|
posr_i = strstr(posr+1, text);
|
|
|
|
if (!posr_i || !(*cursor > (unsigned)(posr_i-p))) {
|
|
|
|
*cursor = posr-p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
posr = posr_i;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
move_down(const char *p, uint32_t *cursor)
|
|
|
|
{
|
|
|
|
const char *posr;
|
|
|
|
char text[16];
|
|
|
|
|
|
|
|
xkb_keysym_to_utf8(XKB_KEY_Return, text, sizeof(text));
|
|
|
|
|
|
|
|
posr = strstr(p, text);
|
|
|
|
while (posr) {
|
|
|
|
if (*cursor <= (unsigned)(posr-p)) {
|
|
|
|
*cursor = posr-p + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
posr = strstr(posr+1, text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2014-01-07 22:41:50 +04:00
|
|
|
static void text_entry_touch_handler(struct widget *widget, struct input *input,
|
|
|
|
uint32_t serial, uint32_t time, int32_t id,
|
|
|
|
float tx, float ty, void *data);
|
2013-05-30 15:57:01 +04:00
|
|
|
static int text_entry_motion_handler(struct widget *widget,
|
|
|
|
struct input *input, uint32_t time,
|
|
|
|
float x, float y, 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);
|
2013-04-18 18:47:27 +04:00
|
|
|
static void text_entry_get_cursor_rectangle(struct text_entry *entry, struct rectangle *rectangle);
|
|
|
|
static void text_entry_update(struct text_entry *entry);
|
2012-06-21 23:52:19 +04:00
|
|
|
|
|
|
|
static void
|
2013-04-18 18:47:24 +04:00
|
|
|
text_input_commit_string(void *data,
|
2013-04-18 18:47:36 +04:00
|
|
|
struct wl_text_input *text_input,
|
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-04-18 18:47:33 +04:00
|
|
|
if ((entry->serial - serial) > (entry->serial - entry->reset_serial)) {
|
|
|
|
fprintf(stderr, "Ignore commit. Serial: %u, Current: %u, Reset: %u\n",
|
|
|
|
serial, entry->serial, entry->reset_serial);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-30 15:57:05 +04:00
|
|
|
if (entry->pending_commit.invalid_delete) {
|
|
|
|
fprintf(stderr, "Ignore commit. Invalid previous delete_surrounding event.\n");
|
|
|
|
memset(&entry->pending_commit, 0, sizeof entry->pending_commit);
|
|
|
|
return;
|
|
|
|
}
|
2013-01-17 00:26:40 +04:00
|
|
|
|
2013-05-30 15:57:05 +04:00
|
|
|
text_entry_reset_preedit(entry);
|
2013-04-18 18:47:34 +04:00
|
|
|
|
|
|
|
if (entry->pending_commit.delete_length) {
|
|
|
|
text_entry_delete_text(entry,
|
|
|
|
entry->pending_commit.delete_index,
|
|
|
|
entry->pending_commit.delete_length);
|
2013-05-30 15:57:02 +04:00
|
|
|
} else {
|
|
|
|
text_entry_delete_selected_text(entry);
|
2013-04-18 18:47:34 +04:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-05-30 15:57:05 +04:00
|
|
|
static void
|
|
|
|
clear_pending_preedit(struct text_entry *entry)
|
|
|
|
{
|
|
|
|
memset(&entry->pending_commit, 0, sizeof entry->pending_commit);
|
|
|
|
|
|
|
|
pango_attr_list_unref(entry->preedit_info.attr_list);
|
|
|
|
|
|
|
|
entry->preedit_info.cursor = 0;
|
|
|
|
entry->preedit_info.attr_list = NULL;
|
|
|
|
|
|
|
|
memset(&entry->preedit_info, 0, sizeof entry->preedit_info);
|
|
|
|
}
|
|
|
|
|
2012-08-10 18:47:19 +04:00
|
|
|
static void
|
2013-04-18 18:47:24 +04:00
|
|
|
text_input_preedit_string(void *data,
|
2013-04-18 18:47:36 +04:00
|
|
|
struct wl_text_input *text_input,
|
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;
|
|
|
|
|
2013-05-30 15:57:05 +04:00
|
|
|
if ((entry->serial - serial) > (entry->serial - entry->reset_serial)) {
|
|
|
|
fprintf(stderr, "Ignore preedit_string. Serial: %u, Current: %u, Reset: %u\n",
|
|
|
|
serial, entry->serial, entry->reset_serial);
|
|
|
|
clear_pending_preedit(entry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->pending_commit.invalid_delete) {
|
|
|
|
fprintf(stderr, "Ignore preedit_string. Invalid previous delete_surrounding event.\n");
|
|
|
|
clear_pending_preedit(entry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-30 15:57:02 +04:00
|
|
|
if (entry->pending_commit.delete_length) {
|
|
|
|
text_entry_delete_text(entry,
|
|
|
|
entry->pending_commit.delete_index,
|
|
|
|
entry->pending_commit.delete_length);
|
|
|
|
} else {
|
|
|
|
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-05-30 15:57:05 +04:00
|
|
|
entry->preedit.attr_list = pango_attr_list_ref(entry->preedit_info.attr_list);
|
2013-01-17 00:26:40 +04:00
|
|
|
|
2013-05-30 15:57:05 +04:00
|
|
|
clear_pending_preedit(entry);
|
2012-09-10 01:08:43 +04:00
|
|
|
|
2013-04-18 18:47:27 +04:00
|
|
|
text_entry_update(entry);
|
2013-04-18 18:47:36 +04:00
|
|
|
|
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
|
2013-04-18 18:47:24 +04:00
|
|
|
text_input_delete_surrounding_text(void *data,
|
2013-04-18 18:47:36 +04:00
|
|
|
struct wl_text_input *text_input,
|
2012-09-10 01:08:44 +04:00
|
|
|
int32_t index,
|
|
|
|
uint32_t length)
|
|
|
|
{
|
|
|
|
struct text_entry *entry = data;
|
2013-04-18 18:47:34 +04:00
|
|
|
uint32_t text_length;
|
|
|
|
|
|
|
|
entry->pending_commit.delete_index = entry->cursor + index;
|
|
|
|
entry->pending_commit.delete_length = length;
|
2013-05-30 15:57:05 +04:00
|
|
|
entry->pending_commit.invalid_delete = false;
|
2012-09-10 01:08:44 +04:00
|
|
|
|
2013-04-18 18:47:42 +04:00
|
|
|
text_length = strlen(entry->text);
|
2013-04-18 18:47:34 +04:00
|
|
|
|
2013-05-30 15:57:05 +04:00
|
|
|
if (entry->pending_commit.delete_index > text_length ||
|
|
|
|
length > text_length ||
|
2013-05-30 15:57:04 +04:00
|
|
|
entry->pending_commit.delete_index + length > text_length) {
|
2013-05-30 15:57:05 +04:00
|
|
|
fprintf(stderr, "delete_surrounding_text: Invalid index: %d," \
|
|
|
|
"length %u'; cursor: %u text length: %u\n", index, length, entry->cursor, text_length);
|
|
|
|
entry->pending_commit.invalid_delete = true;
|
2012-09-10 01:08:44 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-31 18:52:23 +04:00
|
|
|
static void
|
2013-04-18 18:47:24 +04:00
|
|
|
text_input_cursor_position(void *data,
|
2013-04-18 18:47:36 +04:00
|
|
|
struct wl_text_input *text_input,
|
2013-01-31 18:52:23 +04:00
|
|
|
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
|
2013-04-18 18:47:24 +04:00
|
|
|
text_input_preedit_styling(void *data,
|
2013-04-18 18:47:36 +04:00
|
|
|
struct wl_text_input *text_input,
|
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) {
|
2013-04-18 18:47:36 +04:00
|
|
|
case WL_TEXT_INPUT_PREEDIT_STYLE_DEFAULT:
|
|
|
|
case WL_TEXT_INPUT_PREEDIT_STYLE_UNDERLINE:
|
2013-01-17 00:26:42 +04:00
|
|
|
attr1 = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
|
|
|
|
break;
|
2013-04-18 18:47:36 +04:00
|
|
|
case WL_TEXT_INPUT_PREEDIT_STYLE_INCORRECT:
|
2013-01-17 00:26:42 +04:00
|
|
|
attr1 = pango_attr_underline_new(PANGO_UNDERLINE_ERROR);
|
|
|
|
attr2 = pango_attr_underline_color_new(65535, 0, 0);
|
|
|
|
break;
|
2013-04-18 18:47:36 +04:00
|
|
|
case WL_TEXT_INPUT_PREEDIT_STYLE_SELECTION:
|
2013-01-17 00:26:42 +04:00
|
|
|
attr1 = pango_attr_background_new(0.3 * 65535, 0.3 * 65535, 65535);
|
|
|
|
attr2 = pango_attr_foreground_new(65535, 65535, 65535);
|
|
|
|
break;
|
2013-04-18 18:47:36 +04:00
|
|
|
case WL_TEXT_INPUT_PREEDIT_STYLE_HIGHLIGHT:
|
|
|
|
case WL_TEXT_INPUT_PREEDIT_STYLE_ACTIVE:
|
2013-01-17 00:26:42 +04:00
|
|
|
attr1 = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
|
|
|
|
attr2 = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
|
|
|
|
break;
|
2013-04-18 18:47:36 +04:00
|
|
|
case WL_TEXT_INPUT_PREEDIT_STYLE_INACTIVE:
|
2013-01-17 00:26:42 +04:00
|
|
|
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
|
2013-04-18 18:47:24 +04:00
|
|
|
text_input_preedit_cursor(void *data,
|
2013-04-18 18:47:36 +04:00
|
|
|
struct wl_text_input *text_input,
|
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
|
2013-04-18 18:47:24 +04:00
|
|
|
text_input_modifiers_map(void *data,
|
2013-04-18 18:47:36 +04:00
|
|
|
struct wl_text_input *text_input,
|
2012-11-18 22:06:43 +04:00
|
|
|
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
|
2013-04-18 18:47:24 +04:00
|
|
|
text_input_keysym(void *data,
|
2013-04-18 18:47:36 +04:00
|
|
|
struct wl_text_input *text_input,
|
2012-11-18 22:06:43 +04:00
|
|
|
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-05 06:26:39 +04:00
|
|
|
const char *new_char;
|
2012-09-10 01:08:45 +04:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-04-18 18:47:42 +04:00
|
|
|
if (!(modifiers & entry->keysym.shift_mask))
|
|
|
|
entry->anchor = entry->cursor;
|
|
|
|
widget_schedule_redraw(entry->widget);
|
|
|
|
|
2012-11-18 22:06:44 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-18 14:50:14 +04:00
|
|
|
if (key == XKB_KEY_Up ||
|
|
|
|
key == XKB_KEY_Down) {
|
|
|
|
if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (key == XKB_KEY_Up)
|
|
|
|
move_up(entry->text, &entry->cursor);
|
|
|
|
else
|
|
|
|
move_down(entry->text, &entry->cursor);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2013-04-18 18:47:26 +04:00
|
|
|
if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
|
|
|
|
return;
|
|
|
|
|
2013-01-17 00:26:52 +04:00
|
|
|
text_entry_commit_and_reset(entry);
|
|
|
|
|
|
|
|
start = utf8_prev_char(entry->text, entry->text + entry->cursor);
|
|
|
|
if (start == NULL)
|
|
|
|
return;
|
|
|
|
|
2013-06-28 13:59:44 +04:00
|
|
|
end = utf8_next_char(start);
|
|
|
|
|
2013-01-17 00:26:52 +04:00
|
|
|
text_entry_delete_text(entry,
|
|
|
|
start - entry->text,
|
|
|
|
end - start);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-18 14:50:14 +04:00
|
|
|
if (key == XKB_KEY_Tab ||
|
|
|
|
key == XKB_KEY_KP_Enter ||
|
|
|
|
key == XKB_KEY_Return) {
|
|
|
|
char text[16];
|
2012-09-10 01:08:45 +04:00
|
|
|
|
2014-04-18 14:50:14 +04:00
|
|
|
if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
xkb_keysym_to_utf8(key, text, sizeof(text));
|
|
|
|
|
|
|
|
text_entry_insert_at_cursor(entry, text, 0, 0);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2012-08-10 18:47:19 +04:00
|
|
|
}
|
|
|
|
|
2012-08-10 18:47:21 +04:00
|
|
|
static void
|
2013-04-18 18:47:24 +04:00
|
|
|
text_input_enter(void *data,
|
2013-04-18 18:47:36 +04:00
|
|
|
struct wl_text_input *text_input,
|
2012-09-24 16:51:14 +04:00
|
|
|
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;
|
|
|
|
|
2013-04-18 18:47:33 +04:00
|
|
|
text_entry_update(entry);
|
|
|
|
entry->reset_serial = entry->serial;
|
|
|
|
|
2012-08-10 18:47:21 +04:00
|
|
|
widget_schedule_redraw(entry->widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-04-18 18:47:24 +04:00
|
|
|
text_input_leave(void *data,
|
2013-04-18 18:47:36 +04:00
|
|
|
struct wl_text_input *text_input)
|
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-04-18 18:47:36 +04:00
|
|
|
wl_text_input_hide_input_panel(text_input);
|
2013-01-31 18:52:21 +04:00
|
|
|
|
2012-08-10 18:47:21 +04:00
|
|
|
widget_schedule_redraw(entry->widget);
|
|
|
|
}
|
|
|
|
|
2013-01-31 18:52:21 +04:00
|
|
|
static void
|
2013-04-18 18:47:24 +04:00
|
|
|
text_input_input_panel_state(void *data,
|
2013-04-18 18:47:36 +04:00
|
|
|
struct wl_text_input *text_input,
|
2013-01-31 18:52:21 +04:00
|
|
|
uint32_t state)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-04-18 18:47:15 +04:00
|
|
|
static void
|
2013-04-18 18:47:24 +04:00
|
|
|
text_input_language(void *data,
|
2013-04-18 18:47:36 +04:00
|
|
|
struct wl_text_input *text_input,
|
2013-04-18 18:47:15 +04:00
|
|
|
uint32_t serial,
|
|
|
|
const char *language)
|
|
|
|
{
|
2013-04-18 18:47:16 +04:00
|
|
|
fprintf(stderr, "input language is %s \n", language);
|
2013-04-18 18:47:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-04-18 18:47:24 +04:00
|
|
|
text_input_text_direction(void *data,
|
2013-04-18 18:47:36 +04:00
|
|
|
struct wl_text_input *text_input,
|
2013-04-18 18:47:15 +04:00
|
|
|
uint32_t serial,
|
|
|
|
uint32_t direction)
|
|
|
|
{
|
2013-04-18 18:47:16 +04:00
|
|
|
struct text_entry *entry = data;
|
|
|
|
PangoContext *context = pango_layout_get_context(entry->layout);
|
|
|
|
PangoDirection pango_direction;
|
|
|
|
|
|
|
|
|
|
|
|
switch (direction) {
|
2013-04-18 18:47:36 +04:00
|
|
|
case WL_TEXT_INPUT_TEXT_DIRECTION_LTR:
|
2013-04-18 18:47:16 +04:00
|
|
|
pango_direction = PANGO_DIRECTION_LTR;
|
|
|
|
break;
|
2013-04-18 18:47:36 +04:00
|
|
|
case WL_TEXT_INPUT_TEXT_DIRECTION_RTL:
|
2013-04-18 18:47:16 +04:00
|
|
|
pango_direction = PANGO_DIRECTION_RTL;
|
|
|
|
break;
|
2013-04-18 18:47:36 +04:00
|
|
|
case WL_TEXT_INPUT_TEXT_DIRECTION_AUTO:
|
2013-04-18 18:47:16 +04:00
|
|
|
default:
|
|
|
|
pango_direction = PANGO_DIRECTION_NEUTRAL;
|
|
|
|
}
|
2013-04-18 18:47:36 +04:00
|
|
|
|
2013-04-18 18:47:16 +04:00
|
|
|
pango_context_set_base_dir(context, pango_direction);
|
2013-04-18 18:47:15 +04:00
|
|
|
}
|
|
|
|
|
2013-04-18 18:47:36 +04:00
|
|
|
static const struct wl_text_input_listener text_input_listener = {
|
2013-04-18 18:47:24 +04:00
|
|
|
text_input_enter,
|
|
|
|
text_input_leave,
|
|
|
|
text_input_modifiers_map,
|
|
|
|
text_input_input_panel_state,
|
|
|
|
text_input_preedit_string,
|
|
|
|
text_input_preedit_styling,
|
|
|
|
text_input_preedit_cursor,
|
|
|
|
text_input_commit_string,
|
|
|
|
text_input_cursor_position,
|
|
|
|
text_input_delete_surrounding_text,
|
|
|
|
text_input_keysym,
|
|
|
|
text_input_language,
|
|
|
|
text_input_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;
|
|
|
|
|
2013-08-08 02:34:59 +04:00
|
|
|
entry = xmalloc(sizeof *entry);
|
|
|
|
memset(entry, 0, 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;
|
2013-04-18 18:47:36 +04:00
|
|
|
entry->text_input = wl_text_input_manager_create_text_input(editor->text_input_manager);
|
|
|
|
wl_text_input_add_listener(entry->text_input, &text_input_listener, entry);
|
2012-06-21 23:52:19 +04:00
|
|
|
|
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);
|
2013-05-30 15:57:01 +04:00
|
|
|
widget_set_motion_handler(entry->widget, text_entry_motion_handler);
|
2014-01-07 22:41:50 +04:00
|
|
|
widget_set_touch_down_handler(entry->widget, text_entry_touch_handler);
|
2012-09-10 01:08:34 +04:00
|
|
|
|
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);
|
2013-04-18 18:47:36 +04:00
|
|
|
wl_text_input_destroy(entry->text_input);
|
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) {
|
2013-04-18 18:47:36 +04:00
|
|
|
wl_text_input_show_input_panel(entry->text_input);
|
2013-01-31 18:52:21 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry->click_to_show)
|
2013-04-18 18:47:36 +04:00
|
|
|
wl_text_input_show_input_panel(entry->text_input);
|
2013-01-31 18:52:21 +04:00
|
|
|
|
2013-04-18 18:47:36 +04:00
|
|
|
wl_text_input_activate(entry->text_input,
|
|
|
|
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
|
|
|
{
|
2013-04-18 18:47:36 +04:00
|
|
|
wl_text_input_deactivate(entry->text_input,
|
|
|
|
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
|
|
|
|
2013-04-18 18:47:42 +04:00
|
|
|
assert(entry->cursor <= (strlen(entry->text) +
|
|
|
|
(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)
|
|
|
|
{
|
2013-04-18 18:47:27 +04:00
|
|
|
struct rectangle cursor_rectangle;
|
|
|
|
|
2013-04-18 18:47:36 +04:00
|
|
|
wl_text_input_set_content_type(entry->text_input,
|
|
|
|
WL_TEXT_INPUT_CONTENT_HINT_NONE,
|
|
|
|
entry->content_purpose);
|
2013-01-17 00:26:45 +04:00
|
|
|
|
2013-04-18 18:47:36 +04:00
|
|
|
wl_text_input_set_surrounding_text(entry->text_input,
|
|
|
|
entry->text,
|
|
|
|
entry->cursor,
|
|
|
|
entry->anchor);
|
2013-01-31 18:52:20 +04:00
|
|
|
|
2013-04-18 18:47:16 +04:00
|
|
|
if (entry->preferred_language)
|
2013-04-18 18:47:36 +04:00
|
|
|
wl_text_input_set_preferred_language(entry->text_input,
|
|
|
|
entry->preferred_language);
|
2013-04-18 18:47:16 +04:00
|
|
|
|
2013-04-18 18:47:27 +04:00
|
|
|
text_entry_get_cursor_rectangle(entry, &cursor_rectangle);
|
2013-04-18 18:47:36 +04:00
|
|
|
wl_text_input_set_cursor_rectangle(entry->text_input, cursor_rectangle.x, cursor_rectangle.y,
|
|
|
|
cursor_rectangle.width, cursor_rectangle.height);
|
2013-04-18 18:47:27 +04:00
|
|
|
|
2013-04-18 18:47:36 +04:00
|
|
|
wl_text_input_commit_state(entry->text_input, ++entry->serial);
|
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;
|
2013-04-18 18:47:42 +04:00
|
|
|
|
2013-01-31 18:52:23 +04:00
|
|
|
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);
|
|
|
|
}
|
2013-04-18 18:47:18 +04:00
|
|
|
|
2013-04-18 18:47:36 +04:00
|
|
|
wl_text_input_reset(entry->text_input);
|
2013-04-18 18:47:33 +04:00
|
|
|
text_entry_update(entry);
|
|
|
|
entry->reset_serial = entry->serial;
|
2013-01-17 00:26:40 +04:00
|
|
|
}
|
|
|
|
|
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;
|
2013-04-18 18:47:42 +04:00
|
|
|
const char *text;
|
2013-01-17 00:26:47 +04:00
|
|
|
|
|
|
|
if (!entry->preedit.text)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pango_layout_xy_to_index(entry->layout,
|
|
|
|
x * PANGO_SCALE, y * PANGO_SCALE,
|
|
|
|
&index, &trailing);
|
2013-04-18 18:47:42 +04:00
|
|
|
|
|
|
|
text = pango_layout_get_text(entry->layout);
|
|
|
|
cursor = g_utf8_offset_to_pointer(text + index, trailing) - text;
|
2013-01-17 00:26:47 +04:00
|
|
|
|
|
|
|
if (cursor < entry->cursor ||
|
|
|
|
cursor > entry->cursor + strlen(entry->preedit.text)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == WL_POINTER_BUTTON_STATE_RELEASED)
|
2013-04-18 18:47:36 +04:00
|
|
|
wl_text_input_invoke_action(entry->text_input,
|
|
|
|
button,
|
|
|
|
cursor - entry->cursor);
|
2013-01-17 00:26:47 +04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-05-30 15:57:01 +04:00
|
|
|
static bool
|
|
|
|
text_entry_has_preedit(struct text_entry *entry)
|
|
|
|
{
|
|
|
|
return entry->preedit.text && (strlen(entry->preedit.text) > 0);
|
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:36 +04:00
|
|
|
static void
|
|
|
|
text_entry_set_cursor_position(struct text_entry *entry,
|
2013-05-30 15:57:01 +04:00
|
|
|
int32_t x, int32_t y,
|
|
|
|
bool move_anchor)
|
2012-09-10 01:08:36 +04:00
|
|
|
{
|
2013-01-17 00:26:42 +04:00
|
|
|
int index, trailing;
|
2013-04-18 18:47:42 +04:00
|
|
|
const char *text;
|
2013-05-30 15:57:01 +04:00
|
|
|
uint32_t cursor;
|
2013-01-17 00:26:40 +04:00
|
|
|
|
2013-01-17 00:26:42 +04:00
|
|
|
pango_layout_xy_to_index(entry->layout,
|
|
|
|
x * PANGO_SCALE, y * PANGO_SCALE,
|
|
|
|
&index, &trailing);
|
2013-04-18 18:47:42 +04:00
|
|
|
|
|
|
|
text = pango_layout_get_text(entry->layout);
|
2012-09-10 01:08:36 +04:00
|
|
|
|
2013-05-30 15:57:01 +04:00
|
|
|
cursor = g_utf8_offset_to_pointer(text + index, trailing) - text;
|
2012-09-10 01:08:39 +04:00
|
|
|
|
2013-05-30 15:57:01 +04:00
|
|
|
if (move_anchor)
|
|
|
|
entry->anchor = cursor;
|
2013-01-17 00:26:42 +04:00
|
|
|
|
2013-05-30 15:57:01 +04:00
|
|
|
if (text_entry_has_preedit(entry)) {
|
|
|
|
text_entry_commit_and_reset(entry);
|
2012-09-10 01:08:36 +04:00
|
|
|
|
2013-05-30 15:57:01 +04:00
|
|
|
assert(!text_entry_has_preedit(entry));
|
|
|
|
}
|
2013-01-17 00:26:42 +04:00
|
|
|
|
2013-05-30 15:57:01 +04:00
|
|
|
if (entry->cursor == cursor)
|
|
|
|
return;
|
2013-04-18 18:47:42 +04:00
|
|
|
|
2013-05-30 15:57:01 +04:00
|
|
|
entry->cursor = cursor;
|
2013-01-17 00:26:42 +04:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2013-04-18 18:47:42 +04:00
|
|
|
uint32_t l;
|
|
|
|
|
2013-05-30 15:57:04 +04:00
|
|
|
assert(index <= strlen(entry->text));
|
|
|
|
assert(index + length <= strlen(entry->text));
|
|
|
|
assert(index + length >= length);
|
2012-09-17 17:28:10 +04:00
|
|
|
|
2013-04-18 18:47:42 +04:00
|
|
|
l = strlen(entry->text + index + length);
|
|
|
|
memmove(entry->text + index,
|
|
|
|
entry->text + index + length,
|
|
|
|
l + 1);
|
2012-09-10 01:08:44 +04:00
|
|
|
|
2013-05-30 15:57:03 +04:00
|
|
|
if (entry->cursor > (index + length))
|
|
|
|
entry->cursor -= length;
|
|
|
|
else if (entry->cursor > index)
|
|
|
|
entry->cursor = index;
|
|
|
|
|
|
|
|
entry->anchor = entry->cursor;
|
|
|
|
|
2012-09-10 01:08:44 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-04-18 18:47:27 +04:00
|
|
|
static void
|
|
|
|
text_entry_get_cursor_rectangle(struct text_entry *entry, struct rectangle *rectangle)
|
|
|
|
{
|
|
|
|
struct rectangle allocation;
|
|
|
|
PangoRectangle extents;
|
|
|
|
PangoRectangle cursor_pos;
|
|
|
|
|
|
|
|
widget_get_allocation(entry->widget, &allocation);
|
|
|
|
|
|
|
|
if (entry->preedit.text && entry->preedit.cursor < 0) {
|
|
|
|
rectangle->x = 0;
|
|
|
|
rectangle->y = 0;
|
|
|
|
rectangle->width = 0;
|
|
|
|
rectangle->height = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-18 18:47:42 +04:00
|
|
|
|
2013-04-18 18:47:27 +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);
|
|
|
|
|
|
|
|
rectangle->x = allocation.x + (allocation.height / 2) + PANGO_PIXELS(cursor_pos.x);
|
|
|
|
rectangle->y = allocation.y + 10 + PANGO_PIXELS(cursor_pos.y);
|
|
|
|
rectangle->width = PANGO_PIXELS(cursor_pos.width);
|
|
|
|
rectangle->height = PANGO_PIXELS(cursor_pos.height);
|
|
|
|
}
|
|
|
|
|
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-07-06 22:55:54 +04:00
|
|
|
cairo_move_to(cr, PANGO_PIXELS(cursor_pos.x), PANGO_PIXELS(cursor_pos.y));
|
|
|
|
cairo_line_to(cr, PANGO_PIXELS(cursor_pos.x), PANGO_PIXELS(cursor_pos.y) + PANGO_PIXELS(cursor_pos.height));
|
2012-09-10 01:08:36 +04:00
|
|
|
cairo_stroke(cr);
|
|
|
|
}
|
|
|
|
|
2014-05-08 15:55:50 +04:00
|
|
|
static int
|
|
|
|
text_offset_left(struct rectangle *allocation)
|
|
|
|
{
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
text_offset_top(struct rectangle *allocation)
|
|
|
|
{
|
|
|
|
return allocation->height / 2;
|
|
|
|
}
|
2012-10-02 13:06:53 +04:00
|
|
|
|
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);
|
|
|
|
|
2014-05-08 15:55:50 +04:00
|
|
|
cairo_translate(cr,
|
|
|
|
text_offset_left(&allocation),
|
|
|
|
text_offset_top(&allocation));
|
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;
|
2014-05-08 15:55:50 +04:00
|
|
|
int tx, ty;
|
2012-09-10 01:08:38 +04:00
|
|
|
|
2013-05-30 15:57:01 +04:00
|
|
|
if (!entry->button_pressed) {
|
|
|
|
return CURSOR_IBEAM;
|
|
|
|
}
|
|
|
|
|
2012-09-10 01:08:38 +04:00
|
|
|
widget_get_allocation(entry->widget, &allocation);
|
|
|
|
|
2014-05-08 15:55:50 +04:00
|
|
|
tx = x - allocation.x - text_offset_left(&allocation);
|
|
|
|
ty = y - allocation.y - text_offset_top(&allocation);
|
|
|
|
|
|
|
|
text_entry_set_cursor_position(entry, tx, ty, false);
|
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
|
|
|
|
2014-05-08 15:55:50 +04:00
|
|
|
x -= allocation.x + text_offset_left(&allocation);
|
|
|
|
y -= allocation.y + text_offset_top(&allocation);
|
2013-01-17 00:26:47 +04:00
|
|
|
|
2012-11-18 22:06:49 +04:00
|
|
|
editor = window_get_user_data(entry->window);
|
|
|
|
|
2013-05-30 15:57:01 +04:00
|
|
|
if (button == BTN_LEFT) {
|
|
|
|
entry->button_pressed = (state == WL_POINTER_BUTTON_STATE_PRESSED);
|
2013-01-17 00:26:47 +04:00
|
|
|
|
2013-05-30 15:57:01 +04:00
|
|
|
if (state == WL_POINTER_BUTTON_STATE_PRESSED)
|
|
|
|
input_grab(input, entry->widget, button);
|
|
|
|
else
|
|
|
|
input_ungrab(input);
|
2012-06-21 23:52:19 +04:00
|
|
|
}
|
|
|
|
|
2013-05-30 15:57:01 +04:00
|
|
|
if (text_entry_has_preedit(entry)) {
|
|
|
|
result = text_entry_try_invoke_preedit_action(entry, x, y, button, state);
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
return;
|
|
|
|
}
|
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-05-30 15:57:01 +04:00
|
|
|
text_entry_set_cursor_position(entry, x, y, true);
|
2012-09-10 01:08:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 22:41:50 +04:00
|
|
|
static void
|
|
|
|
text_entry_touch_handler(struct widget *widget, struct input *input,
|
|
|
|
uint32_t serial, uint32_t time, int32_t id,
|
|
|
|
float tx, float ty, void *data)
|
|
|
|
{
|
|
|
|
struct text_entry *entry = data;
|
|
|
|
struct wl_seat *seat = input_get_seat(input);
|
|
|
|
struct rectangle allocation;
|
|
|
|
struct editor *editor;
|
|
|
|
int32_t x, y;
|
|
|
|
|
|
|
|
widget_get_allocation(entry->widget, &allocation);
|
|
|
|
|
2014-05-08 15:55:50 +04:00
|
|
|
x = tx - (allocation.x + text_offset_left(&allocation));
|
|
|
|
y = ty - (allocation.y + text_offset_top(&allocation));
|
2014-01-07 22:41:50 +04:00
|
|
|
|
|
|
|
editor = window_get_user_data(entry->window);
|
|
|
|
text_entry_activate(entry, seat);
|
|
|
|
editor->active_entry = entry;
|
|
|
|
|
|
|
|
text_entry_set_cursor_position(entry, x, y, true);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 22:41:50 +04:00
|
|
|
static void
|
|
|
|
editor_touch_handler(struct widget *widget, struct input *input,
|
|
|
|
uint32_t serial, uint32_t time, int32_t id,
|
|
|
|
float tx, float ty, void *data)
|
|
|
|
{
|
|
|
|
struct editor *editor = data;
|
|
|
|
|
|
|
|
struct wl_seat *seat = input_get_seat(input);
|
|
|
|
|
|
|
|
text_entry_deactivate(editor->entry, seat);
|
|
|
|
text_entry_deactivate(editor->editor, seat);
|
|
|
|
editor->active_entry = NULL;
|
|
|
|
}
|
2014-01-02 11:57:42 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
keyboard_focus_handler(struct window *window,
|
|
|
|
struct input *device, void *data)
|
|
|
|
{
|
|
|
|
struct editor *editor = data;
|
|
|
|
|
|
|
|
window_schedule_redraw(editor->window);
|
|
|
|
}
|
|
|
|
|
2012-11-18 22:06:49 +04:00
|
|
|
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;
|
2013-04-18 18:47:42 +04:00
|
|
|
const char *new_char;
|
2012-11-18 22:06:49 +04:00
|
|
|
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);
|
|
|
|
|
2013-04-18 18:47:42 +04:00
|
|
|
new_char = utf8_prev_char(entry->text, entry->text + entry->cursor);
|
|
|
|
if (new_char != NULL)
|
|
|
|
text_entry_delete_text(entry,
|
|
|
|
new_char - entry->text,
|
|
|
|
(entry->text + entry->cursor) - new_char);
|
2012-11-18 22:06:49 +04:00
|
|
|
break;
|
|
|
|
case XKB_KEY_Delete:
|
2013-01-17 00:26:40 +04:00
|
|
|
text_entry_commit_and_reset(entry);
|
|
|
|
|
2013-04-18 18:47:42 +04:00
|
|
|
new_char = utf8_next_char(entry->text + entry->cursor);
|
|
|
|
if (new_char != NULL)
|
|
|
|
text_entry_delete_text(entry,
|
|
|
|
entry->cursor,
|
|
|
|
new_char - (entry->text + entry->cursor));
|
2012-11-18 22:06:49 +04:00
|
|
|
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;
|
2013-07-11 19:00:00 +04:00
|
|
|
if (!(input_get_modifiers(input) & MOD_SHIFT_MASK))
|
|
|
|
entry->anchor = entry->cursor;
|
2012-11-18 22:06:49 +04:00
|
|
|
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;
|
2013-07-11 19:00:00 +04:00
|
|
|
if (!(input_get_modifiers(input) & MOD_SHIFT_MASK))
|
|
|
|
entry->anchor = entry->cursor;
|
2012-11-18 22:06:49 +04:00
|
|
|
widget_schedule_redraw(entry->widget);
|
|
|
|
}
|
|
|
|
break;
|
2014-04-18 14:50:14 +04:00
|
|
|
case XKB_KEY_Up:
|
|
|
|
text_entry_commit_and_reset(entry);
|
|
|
|
|
|
|
|
move_up(entry->text, &entry->cursor);
|
|
|
|
if (!(input_get_modifiers(input) & MOD_SHIFT_MASK))
|
|
|
|
entry->anchor = entry->cursor;
|
|
|
|
widget_schedule_redraw(entry->widget);
|
|
|
|
break;
|
|
|
|
case XKB_KEY_Down:
|
|
|
|
text_entry_commit_and_reset(entry);
|
|
|
|
|
|
|
|
move_down(entry->text, &entry->cursor);
|
|
|
|
if (!(input_get_modifiers(input) & MOD_SHIFT_MASK))
|
|
|
|
entry->anchor = entry->cursor;
|
|
|
|
widget_schedule_redraw(entry->widget);
|
|
|
|
break;
|
2013-07-06 22:42:59 +04:00
|
|
|
case XKB_KEY_Escape:
|
|
|
|
break;
|
2012-11-18 22:06:49 +04:00
|
|
|
default:
|
|
|
|
if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0)
|
|
|
|
break;
|
|
|
|
|
2013-07-06 22:42:59 +04:00
|
|
|
text_entry_commit_and_reset(entry);
|
2013-01-17 00:26:40 +04:00
|
|
|
|
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;
|
|
|
|
|
2013-04-18 18:47:36 +04:00
|
|
|
if (!strcmp(interface, "wl_text_input_manager")) {
|
2013-04-18 18:47:24 +04:00
|
|
|
editor->text_input_manager =
|
2012-10-11 05:34:26 +04:00
|
|
|
display_bind(display, name,
|
2013-04-18 18:47:36 +04:00
|
|
|
&wl_text_input_manager_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;
|
2013-04-18 18:47:16 +04:00
|
|
|
const char *preferred_language = NULL;
|
2013-01-31 18:52:21 +04:00
|
|
|
|
2013-04-18 18:47:16 +04:00
|
|
|
for (i = 1; i < argc; i++) {
|
2013-01-31 18:52:21 +04:00
|
|
|
if (strcmp("--click-to-show", argv[i]) == 0)
|
|
|
|
click_to_show = 1;
|
2013-04-18 18:47:16 +04:00
|
|
|
else if (strcmp("--preferred-language", argv[i]) == 0) {
|
|
|
|
if (i + 1 < argc) {
|
|
|
|
preferred_language = argv[i + 1];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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);
|
2013-10-14 04:08:38 +04:00
|
|
|
editor.widget = window_frame_create(editor.window, &editor);
|
2012-06-21 23:52:19 +04:00
|
|
|
|
|
|
|
editor.entry = text_entry_create(&editor, "Entry");
|
2013-01-31 18:52:21 +04:00
|
|
|
editor.entry->click_to_show = click_to_show;
|
2013-04-18 18:47:16 +04:00
|
|
|
if (preferred_language)
|
|
|
|
editor.entry->preferred_language = strdup(preferred_language);
|
2013-01-17 00:26:45 +04:00
|
|
|
editor.editor = text_entry_create(&editor, "Numeric");
|
2013-04-18 18:47:36 +04:00
|
|
|
editor.editor->content_purpose = WL_TEXT_INPUT_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);
|
2014-01-02 11:57:42 +04:00
|
|
|
window_set_keyboard_focus_handler(editor.window,
|
|
|
|
keyboard_focus_handler);
|
2012-11-18 22:06:49 +04:00
|
|
|
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);
|
2014-01-07 22:41:50 +04:00
|
|
|
widget_set_touch_down_handler(editor.widget, editor_touch_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);
|
2014-05-15 17:28:16 +04:00
|
|
|
widget_destroy(editor.widget);
|
|
|
|
window_destroy(editor.window);
|
|
|
|
display_destroy(editor.display);
|
2012-06-21 23:52:19 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|