2013-05-04 16:14:25 +02:00
|
|
|
/*
|
2013-05-11 04:17:39 +02:00
|
|
|
* StarDict terminal UI
|
2013-05-04 16:14:25 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2013, Přemysl Janouch <p.janouch@gmail.com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, 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-06-02 00:01:23 +02:00
|
|
|
#define _XOPEN_SOURCE 500 //!< wcwidth
|
|
|
|
#define _XOPEN_SOURCE_EXTENDED //!< Yes, we want ncursesw.
|
2013-05-04 16:14:25 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <limits.h>
|
2013-05-14 21:49:39 +02:00
|
|
|
#include <string.h>
|
2013-05-04 16:14:25 +02:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <gio/gio.h>
|
2013-05-15 20:58:40 +02:00
|
|
|
#include <pango/pango.h>
|
2013-05-04 16:14:25 +02:00
|
|
|
#include <ncurses.h>
|
2013-05-19 02:46:05 +02:00
|
|
|
#include <glib/gi18n.h>
|
2013-05-04 16:14:25 +02:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2013-05-17 18:26:08 +02:00
|
|
|
#include "config.h"
|
2013-05-04 16:14:25 +02:00
|
|
|
#include "stardict.h"
|
|
|
|
|
|
|
|
|
2013-07-09 06:47:19 +02:00
|
|
|
#define CTRL_KEY(x) ((x) - 'A' + 1)
|
|
|
|
|
|
|
|
#define KEY_SOH CTRL_KEY ('A') //!< Ctrl-A
|
|
|
|
#define KEY_STX CTRL_KEY ('B') //!< Ctrl-B
|
|
|
|
#define KEY_ENQ CTRL_KEY ('E') //!< Ctrl-E
|
|
|
|
#define KEY_ACK CTRL_KEY ('F') //!< Ctrl-F
|
|
|
|
#define KEY_VT CTRL_KEY ('K') //!< Ctrl-K
|
|
|
|
#define KEY_FF CTRL_KEY ('L') //!< Ctrl-L
|
|
|
|
#define KEY_SO CTRL_KEY ('N') //!< Ctrl-N
|
|
|
|
#define KEY_DLE CTRL_KEY ('P') //!< Ctrl-P
|
|
|
|
#define KEY_DC4 CTRL_KEY ('T') //!< Ctrl-T
|
|
|
|
#define KEY_NAK CTRL_KEY ('U') //!< Ctrl-U
|
|
|
|
#define KEY_ETB CTRL_KEY ('W') //!< Ctrl-W
|
2013-05-18 22:26:57 +02:00
|
|
|
|
|
|
|
#define KEY_RETURN 13 //!< Enter
|
|
|
|
#define KEY_ESCAPE 27 //!< Esc
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
TERMINAL_UNKNOWN, //!< No extra handling
|
|
|
|
TERMINAL_XTERM, //!< xterm and VTE extra keycodes
|
|
|
|
TERMINAL_RXVT //!< rxvt extra keycodes
|
|
|
|
} TerminalType; //!< Type of the terminal
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
KEY_NOT_RECOGNISED, //!< Not recognised
|
|
|
|
|
|
|
|
KEY_CTRL_UP, //!< Ctrl + Up arrow
|
|
|
|
KEY_CTRL_DOWN, //!< Ctrl + Down arrow
|
|
|
|
KEY_CTRL_LEFT, //!< Ctrl + Left arrow
|
|
|
|
KEY_CTRL_RIGHT, //!< Ctrl + Right arrow
|
|
|
|
|
|
|
|
KEY_ALT_UP, //!< Alt + Up arrow
|
|
|
|
KEY_ALT_DOWN, //!< Alt + Down arrow
|
|
|
|
KEY_ALT_LEFT, //!< Alt + Left arrow
|
|
|
|
KEY_ALT_RIGHT //!< Alt + Right arrow
|
|
|
|
} ExtraKeyCode; //!< Translated key codes above KEY_MAX
|
2013-05-04 16:14:25 +02:00
|
|
|
|
2013-05-12 01:27:25 +02:00
|
|
|
// --- Utilities ---------------------------------------------------------------
|
2013-05-04 16:14:25 +02:00
|
|
|
|
|
|
|
static int
|
|
|
|
poll_restart (struct pollfd *fds, nfds_t nfds, int timeout)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
do
|
|
|
|
ret = poll (fds, nfds, timeout);
|
|
|
|
while (ret == -1 && errno == EINTR);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-12 01:27:25 +02:00
|
|
|
/** Wrapper for curses event data. */
|
|
|
|
typedef struct curses_event CursesEvent;
|
|
|
|
|
|
|
|
struct curses_event
|
|
|
|
{
|
|
|
|
wint_t code;
|
|
|
|
guint is_char : 1;
|
|
|
|
MEVENT mouse;
|
|
|
|
};
|
|
|
|
|
2013-06-02 00:01:23 +02:00
|
|
|
static size_t
|
|
|
|
unichar_width (gunichar ch)
|
|
|
|
{
|
|
|
|
if (g_unichar_iszerowidth (ch))
|
|
|
|
return 0;
|
|
|
|
return 1 + g_unichar_iswide (ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef HAVE_WCWIDTH
|
|
|
|
#define wcwidth(x) 1
|
|
|
|
#endif // ! HAVE_WCWIDTH
|
|
|
|
|
2013-05-19 18:42:38 +02:00
|
|
|
static gboolean
|
|
|
|
is_character_in_locale (wchar_t c)
|
|
|
|
{
|
2013-05-19 19:28:08 +02:00
|
|
|
wchar_t s[] = { c, 0 };
|
|
|
|
return wcstombs (NULL, s, 0) != (size_t) -1;
|
2013-05-19 18:42:38 +02:00
|
|
|
}
|
|
|
|
|
2013-05-18 22:26:57 +02:00
|
|
|
/** Translate key codes above KEY_MAX returned from ncurses into something
|
|
|
|
* meaningful, based on the terminal type. The values have been obtained
|
|
|
|
* experimentally. Some keycodes make ncurses return KEY_ESCAPE, even
|
|
|
|
* depending on actual terminal settings, thus this is not reliable at all.
|
|
|
|
* xterm/VTE seems to behave nicely, though.
|
|
|
|
*/
|
|
|
|
static guint
|
|
|
|
translate_extra_keycode (wchar_t code, TerminalType terminal)
|
|
|
|
{
|
|
|
|
switch (terminal)
|
|
|
|
{
|
|
|
|
case TERMINAL_XTERM:
|
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case 565: return KEY_CTRL_UP;
|
|
|
|
case 524: return KEY_CTRL_DOWN;
|
|
|
|
case 544: return KEY_CTRL_LEFT;
|
|
|
|
case 559: return KEY_CTRL_RIGHT;
|
|
|
|
|
|
|
|
case 563: return KEY_ALT_UP;
|
|
|
|
case 522: return KEY_ALT_DOWN;
|
|
|
|
case 542: return KEY_ALT_LEFT;
|
|
|
|
case 557: return KEY_ALT_RIGHT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TERMINAL_RXVT:
|
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case 521: return KEY_CTRL_UP;
|
|
|
|
case 514: return KEY_CTRL_DOWN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TERMINAL_UNKNOWN:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return KEY_NOT_RECOGNISED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the type of the terminal based on the TERM environment variable. */
|
|
|
|
static TerminalType
|
|
|
|
get_terminal_type (void)
|
|
|
|
{
|
|
|
|
const gchar *term = g_getenv ("TERM");
|
|
|
|
if (!term) return TERMINAL_UNKNOWN;
|
|
|
|
|
|
|
|
gchar term_copy[strcspn (term, "-") + 1];
|
|
|
|
g_strlcpy (term_copy, term, sizeof term_copy);
|
|
|
|
if (!strcmp (term_copy, "xterm")) return TERMINAL_XTERM;
|
|
|
|
if (!strcmp (term_copy, "rxvt")) return TERMINAL_RXVT;
|
|
|
|
return TERMINAL_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2013-05-12 01:27:25 +02:00
|
|
|
// --- Application -------------------------------------------------------------
|
|
|
|
|
2013-05-14 21:49:39 +02:00
|
|
|
/** Data relating to one entry within the dictionary. */
|
|
|
|
typedef struct view_entry ViewEntry;
|
2013-05-15 17:26:39 +02:00
|
|
|
/** Encloses application data. */
|
|
|
|
typedef struct application Application;
|
2013-05-14 21:49:39 +02:00
|
|
|
|
|
|
|
struct view_entry
|
|
|
|
{
|
|
|
|
gchar * word; //!< Word
|
|
|
|
gchar ** definitions; //!< Word definition entries
|
|
|
|
gsize definitions_length; //!< Length of the @a definitions array
|
|
|
|
};
|
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
struct application
|
|
|
|
{
|
2013-05-18 22:26:57 +02:00
|
|
|
TerminalType terminal_type; //!< Type of the terminal
|
|
|
|
GIConv utf8_to_wchar; //!< utf-8 -> wchar_t conversion
|
|
|
|
GIConv wchar_to_utf8; //!< wchar_t -> utf-8 conversion
|
2013-05-15 12:53:50 +02:00
|
|
|
|
2013-05-18 22:26:57 +02:00
|
|
|
StardictDict * dict; //!< The current dictionary
|
2013-05-18 23:48:24 +02:00
|
|
|
guint show_help : 1; //!< Whether help can be shown
|
2013-05-04 16:14:25 +02:00
|
|
|
|
2013-05-18 22:26:57 +02:00
|
|
|
guint32 top_position; //!< Index of the topmost dict. entry
|
|
|
|
guint top_offset; //!< Offset into the top entry
|
|
|
|
guint selected; //!< Offset to the selected definition
|
|
|
|
GPtrArray * entries; //!< ViewEntry's within the view
|
2013-05-12 01:27:25 +02:00
|
|
|
|
2013-05-18 22:26:57 +02:00
|
|
|
gchar * search_label; //!< Text of the "Search" label
|
|
|
|
GArray * input; //!< The current search input
|
|
|
|
guint input_pos; //!< Cursor position within input
|
|
|
|
gboolean input_confirmed; //!< Input has been confirmed
|
2013-05-16 23:56:01 +02:00
|
|
|
|
2013-05-18 22:26:57 +02:00
|
|
|
gfloat division; //!< Position of the division column
|
2013-05-15 17:26:39 +02:00
|
|
|
};
|
2013-05-12 01:27:25 +02:00
|
|
|
|
2013-05-14 21:49:39 +02:00
|
|
|
|
2013-05-15 20:58:40 +02:00
|
|
|
/** Splits the entry and adds it to a pointer array. */
|
|
|
|
static void
|
|
|
|
view_entry_split_add (GPtrArray *out, const gchar *text)
|
|
|
|
{
|
|
|
|
gchar **it, **tmp = g_strsplit (text, "\n", -1);
|
|
|
|
for (it = tmp; *it; it++)
|
|
|
|
if (**it)
|
|
|
|
g_ptr_array_add (out, g_strdup (*it));
|
|
|
|
g_strfreev (tmp);
|
|
|
|
}
|
|
|
|
|
2013-05-14 21:49:39 +02:00
|
|
|
/** Decomposes a dictionary entry into the format we want. */
|
|
|
|
static ViewEntry *
|
|
|
|
view_entry_new (StardictIterator *iterator)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (stardict_iterator_is_valid (iterator), NULL);
|
|
|
|
|
|
|
|
ViewEntry *ve = g_slice_alloc (sizeof *ve);
|
|
|
|
GString *word = g_string_new (stardict_iterator_get_word (iterator));
|
|
|
|
|
|
|
|
StardictEntry *entry = stardict_iterator_get_entry (iterator);
|
|
|
|
g_return_val_if_fail (entry != NULL, NULL);
|
|
|
|
|
|
|
|
GPtrArray *definitions = g_ptr_array_new ();
|
|
|
|
const GList *fields = stardict_entry_get_fields (entry);
|
2013-05-15 20:58:40 +02:00
|
|
|
gboolean found_anything_displayable = FALSE;
|
2013-05-14 21:49:39 +02:00
|
|
|
while (fields)
|
|
|
|
{
|
|
|
|
const StardictEntryField *field = fields->data;
|
|
|
|
switch (field->type)
|
|
|
|
{
|
|
|
|
case STARDICT_FIELD_MEANING:
|
2013-05-15 20:58:40 +02:00
|
|
|
view_entry_split_add (definitions, field->data);
|
|
|
|
found_anything_displayable = TRUE;
|
|
|
|
break;
|
|
|
|
case STARDICT_FIELD_PANGO:
|
2013-05-14 21:49:39 +02:00
|
|
|
{
|
2013-05-15 20:58:40 +02:00
|
|
|
char *text;
|
|
|
|
if (!pango_parse_markup (field->data, -1,
|
|
|
|
0, NULL, &text, NULL, NULL))
|
|
|
|
text = g_strdup_printf ("<%s>", _("error in entry"));
|
|
|
|
view_entry_split_add (definitions, text);
|
|
|
|
found_anything_displayable = TRUE;
|
|
|
|
g_free (text);
|
2013-05-14 21:49:39 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case STARDICT_FIELD_PHONETIC:
|
|
|
|
g_string_append_printf (word, " /%s/", (const gchar *) field->data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// TODO support more of them
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fields = fields->next;
|
|
|
|
}
|
|
|
|
g_object_unref (entry);
|
|
|
|
|
2013-05-15 20:58:40 +02:00
|
|
|
if (!found_anything_displayable)
|
|
|
|
g_ptr_array_add (definitions,
|
|
|
|
g_strdup_printf ("<%s>", _("no usable field found")));
|
|
|
|
|
2013-05-14 21:49:39 +02:00
|
|
|
ve->word = g_string_free (word, FALSE);
|
|
|
|
ve->definitions_length = definitions->len;
|
|
|
|
g_ptr_array_add (definitions, NULL);
|
|
|
|
ve->definitions = (gchar **) g_ptr_array_free (definitions, FALSE);
|
|
|
|
return ve;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Release resources associated with the view entry. */
|
|
|
|
static void
|
|
|
|
view_entry_free (ViewEntry *ve)
|
|
|
|
{
|
|
|
|
g_free (ve->word);
|
|
|
|
g_strfreev (ve->definitions);
|
|
|
|
g_slice_free1 (sizeof *ve, ve);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Reload view items. */
|
|
|
|
static void
|
2013-05-15 17:26:39 +02:00
|
|
|
app_reload_view (Application *self)
|
2013-05-14 21:49:39 +02:00
|
|
|
{
|
2013-05-15 17:26:39 +02:00
|
|
|
if (self->entries->len != 0)
|
|
|
|
g_ptr_array_remove_range (self->entries, 0, self->entries->len);
|
2013-05-14 21:49:39 +02:00
|
|
|
|
2013-05-19 01:02:19 +02:00
|
|
|
gint remains = LINES - 1 + self->top_offset;
|
2013-05-15 17:26:39 +02:00
|
|
|
StardictIterator *iterator =
|
|
|
|
stardict_iterator_new (self->dict, self->top_position);
|
2013-05-14 21:49:39 +02:00
|
|
|
while (remains > 0 && stardict_iterator_is_valid (iterator))
|
|
|
|
{
|
|
|
|
ViewEntry *entry = view_entry_new (iterator);
|
|
|
|
remains -= entry->definitions_length;
|
2013-05-15 17:26:39 +02:00
|
|
|
g_ptr_array_add (self->entries, entry);
|
2013-05-14 21:49:39 +02:00
|
|
|
stardict_iterator_next (iterator);
|
|
|
|
}
|
|
|
|
g_object_unref (iterator);
|
|
|
|
}
|
|
|
|
|
2013-05-12 01:27:25 +02:00
|
|
|
/** Initialize the application core. */
|
|
|
|
static void
|
2013-05-15 17:26:39 +02:00
|
|
|
app_init (Application *self, const gchar *filename)
|
2013-05-04 16:14:25 +02:00
|
|
|
{
|
2013-05-19 05:04:47 +02:00
|
|
|
GError *error = NULL;
|
2013-05-15 17:26:39 +02:00
|
|
|
self->dict = stardict_dict_new (filename, &error);
|
|
|
|
if (!self->dict)
|
2013-05-12 01:27:25 +02:00
|
|
|
{
|
2013-05-19 02:46:05 +02:00
|
|
|
g_printerr ("%s: %s\n", _("Error loading dictionary"), error->message);
|
2013-05-12 01:27:25 +02:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2013-05-18 22:26:57 +02:00
|
|
|
self->terminal_type = get_terminal_type ();
|
2013-05-18 23:48:24 +02:00
|
|
|
self->show_help = TRUE;
|
2013-05-18 22:26:57 +02:00
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
self->top_position = 0;
|
|
|
|
self->top_offset = 0;
|
|
|
|
self->selected = 0;
|
|
|
|
self->entries = g_ptr_array_new_with_free_func
|
2013-05-14 21:49:39 +02:00
|
|
|
((GDestroyNotify) view_entry_free);
|
2013-05-12 01:27:25 +02:00
|
|
|
|
2013-05-19 02:46:05 +02:00
|
|
|
self->search_label = g_strdup_printf ("%s: ", _("Search"));
|
2013-05-18 01:14:49 +02:00
|
|
|
|
2013-05-15 19:30:47 +02:00
|
|
|
self->input = g_array_new (TRUE, FALSE, sizeof (gunichar));
|
2013-05-15 17:26:39 +02:00
|
|
|
self->input_pos = 0;
|
2013-05-16 23:56:01 +02:00
|
|
|
self->input_confirmed = FALSE;
|
|
|
|
|
|
|
|
self->division = 0.5;
|
2013-05-14 21:49:39 +02:00
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
self->wchar_to_utf8 = g_iconv_open ("utf-8//translit", "wchar_t");
|
|
|
|
self->utf8_to_wchar = g_iconv_open ("wchar_t//translit", "utf-8");
|
2013-05-15 12:53:50 +02:00
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
app_reload_view (self);
|
2013-05-14 21:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Free any resources used by the application. */
|
|
|
|
static void
|
2013-05-15 17:26:39 +02:00
|
|
|
app_destroy (Application *self)
|
2013-05-14 21:49:39 +02:00
|
|
|
{
|
2013-05-15 17:26:39 +02:00
|
|
|
g_object_unref (self->dict);
|
|
|
|
g_ptr_array_free (self->entries, TRUE);
|
2013-05-18 01:14:49 +02:00
|
|
|
g_free (self->search_label);
|
2013-05-15 19:30:47 +02:00
|
|
|
g_array_free (self->input, TRUE);
|
2013-05-15 12:53:50 +02:00
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
g_iconv_close (self->wchar_to_utf8);
|
|
|
|
g_iconv_close (self->utf8_to_wchar);
|
2013-05-12 01:27:25 +02:00
|
|
|
}
|
|
|
|
|
2013-05-18 01:14:49 +02:00
|
|
|
/** Write the given utf-8 string padded with spaces.
|
|
|
|
* @param[in] n The number of characters to write, or -1 for the whole string.
|
|
|
|
* @return The number of wide characters written.
|
|
|
|
*/
|
|
|
|
static gsize
|
2013-05-18 23:48:24 +02:00
|
|
|
app_add_utf8_string (Application *self, const gchar *str, int n)
|
2013-05-15 12:53:50 +02:00
|
|
|
{
|
|
|
|
wchar_t *wide_str = (wchar_t *) g_convert_with_iconv
|
2013-05-15 17:26:39 +02:00
|
|
|
(str, -1, self->utf8_to_wchar, NULL, NULL, NULL);
|
2013-05-18 01:14:49 +02:00
|
|
|
g_return_val_if_fail (wide_str != NULL, 0);
|
2013-05-15 12:53:50 +02:00
|
|
|
|
|
|
|
ssize_t wide_len = wcslen (wide_str);
|
2013-05-19 18:42:38 +02:00
|
|
|
wchar_t padding = L' ', error = L'?', ellipsis = L'…';
|
2013-05-15 12:53:50 +02:00
|
|
|
|
2013-06-02 00:01:23 +02:00
|
|
|
if (!n)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Compute how many wide characters fit in the limit
|
|
|
|
gint cols, i;
|
|
|
|
for (cols = i = 0; i < wide_len; i++)
|
|
|
|
{
|
|
|
|
if (!is_character_in_locale (wide_str[i]))
|
|
|
|
wide_str[i] = error;
|
|
|
|
|
|
|
|
gint width = wcwidth (wide_str[i]);
|
|
|
|
if (n >= 0 && cols + width > n)
|
|
|
|
break;
|
|
|
|
cols += width;
|
|
|
|
}
|
|
|
|
|
2013-05-18 01:14:49 +02:00
|
|
|
if (n < 0)
|
2013-06-02 00:01:23 +02:00
|
|
|
n = cols;
|
2013-05-18 01:14:49 +02:00
|
|
|
|
2013-06-02 00:01:23 +02:00
|
|
|
// Append ellipsis if the whole string didn't fit
|
|
|
|
gint len = i;
|
|
|
|
if (len != wide_len)
|
2013-05-19 18:42:38 +02:00
|
|
|
{
|
2013-06-02 00:01:23 +02:00
|
|
|
if (is_character_in_locale (ellipsis))
|
|
|
|
{
|
|
|
|
if (cols + wcwidth (ellipsis) > n)
|
|
|
|
cols -= wcwidth (wide_str[len - 1]);
|
|
|
|
else
|
|
|
|
len++;
|
|
|
|
|
|
|
|
wide_str[len - 1] = ellipsis;
|
|
|
|
cols += wcwidth (ellipsis);
|
|
|
|
}
|
|
|
|
else if (n >= 3 && len >= 3)
|
2013-05-19 18:42:38 +02:00
|
|
|
{
|
2013-06-02 00:01:23 +02:00
|
|
|
// With zero-width characters this overflows
|
|
|
|
// It's just a fallback anyway
|
|
|
|
cols -= wcwidth (wide_str[len - 1]);
|
|
|
|
cols -= wcwidth (wide_str[len - 2]);
|
|
|
|
cols -= wcwidth (wide_str[len - 3]);
|
|
|
|
cols += 3;
|
|
|
|
|
|
|
|
wide_str[len - 1] = L'.';
|
|
|
|
wide_str[len - 2] = L'.';
|
|
|
|
wide_str[len - 3] = L'.';
|
2013-05-19 18:42:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-15 12:53:50 +02:00
|
|
|
cchar_t cch;
|
2013-06-02 00:01:23 +02:00
|
|
|
for (i = 0; i < len; i++)
|
2013-05-15 12:53:50 +02:00
|
|
|
{
|
2013-06-02 00:01:23 +02:00
|
|
|
if (setcchar (&cch, &wide_str[i], A_NORMAL, 0, NULL) == OK)
|
|
|
|
add_wch (&cch);
|
|
|
|
else
|
|
|
|
// This shouldn't happen
|
|
|
|
cols -= wcwidth (wide_str[i]);
|
2013-05-15 12:53:50 +02:00
|
|
|
}
|
|
|
|
|
2013-06-02 00:01:23 +02:00
|
|
|
setcchar (&cch, &padding, A_NORMAL, 0, NULL);
|
|
|
|
while (cols++ < n)
|
|
|
|
add_wch (&cch);
|
|
|
|
|
2013-05-15 12:53:50 +02:00
|
|
|
g_free (wide_str);
|
2013-05-18 01:14:49 +02:00
|
|
|
return n;
|
2013-05-15 12:53:50 +02:00
|
|
|
}
|
|
|
|
|
2013-05-15 19:30:47 +02:00
|
|
|
/** Render the top bar. */
|
|
|
|
static void
|
|
|
|
app_redraw_top (Application *self)
|
|
|
|
{
|
|
|
|
mvwhline (stdscr, 0, 0, A_UNDERLINE, COLS);
|
|
|
|
attrset (A_UNDERLINE);
|
2013-05-18 23:48:24 +02:00
|
|
|
gsize indent = app_add_utf8_string (self, self->search_label, -1);
|
2013-05-15 19:30:47 +02:00
|
|
|
|
|
|
|
gchar *input_utf8 = g_ucs4_to_utf8
|
|
|
|
((gunichar *) self->input->data, -1, NULL, NULL, NULL);
|
|
|
|
g_return_if_fail (input_utf8 != NULL);
|
|
|
|
|
2013-05-16 23:56:01 +02:00
|
|
|
if (self->input_confirmed)
|
|
|
|
attron (A_BOLD);
|
2013-05-18 23:48:24 +02:00
|
|
|
app_add_utf8_string (self, input_utf8, COLS - indent);
|
2013-05-15 19:30:47 +02:00
|
|
|
g_free (input_utf8);
|
|
|
|
|
2013-06-02 00:01:23 +02:00
|
|
|
guint offset, i;
|
|
|
|
for (offset = i = 0; i < self->input_pos; i++)
|
|
|
|
// This may be inconsistent with the output of app_add_utf8_string()
|
|
|
|
offset += unichar_width (g_array_index (self->input, gunichar, i));
|
|
|
|
|
|
|
|
move (0, indent + offset);
|
2013-05-15 19:30:47 +02:00
|
|
|
refresh ();
|
|
|
|
}
|
|
|
|
|
2013-05-16 23:56:01 +02:00
|
|
|
/** Computes width for the left column. */
|
|
|
|
static guint
|
|
|
|
app_get_left_column_width (Application *self)
|
|
|
|
{
|
|
|
|
gint width = COLS * self->division + 0.5;
|
|
|
|
if (width < 1)
|
|
|
|
width = 1;
|
|
|
|
else if (width > COLS - 2)
|
|
|
|
width = COLS - 2;
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2013-05-18 23:48:24 +02:00
|
|
|
/** Display a message in the view area. */
|
|
|
|
static void
|
|
|
|
app_show_message (Application *self, const gchar *lines[], gsize len)
|
|
|
|
{
|
|
|
|
gint top = (LINES - 1 - len) / 2;
|
|
|
|
|
|
|
|
gint i;
|
|
|
|
for (i = 0; i < top; i++)
|
|
|
|
{
|
|
|
|
move (1 + i, 0);
|
|
|
|
clrtoeol ();
|
|
|
|
}
|
|
|
|
|
|
|
|
attrset (0);
|
|
|
|
while (len-- && i < LINES - 1)
|
|
|
|
{
|
|
|
|
move (1 + i, 0);
|
|
|
|
clrtoeol ();
|
|
|
|
|
|
|
|
gint x = (COLS - g_utf8_strlen (*lines, -1)) / 2;
|
|
|
|
if (x < 0)
|
|
|
|
x = 0;
|
|
|
|
|
|
|
|
move (1 + i, x);
|
|
|
|
app_add_utf8_string (self, *lines, COLS - x);
|
|
|
|
|
|
|
|
lines++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
clrtobot ();
|
|
|
|
refresh ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Show some information about the program. */
|
|
|
|
static void
|
|
|
|
app_show_help (Application *self)
|
|
|
|
{
|
|
|
|
const gchar *lines[] =
|
|
|
|
{
|
|
|
|
PROJECT_NAME " " PROJECT_VERSION,
|
2013-05-19 02:46:05 +02:00
|
|
|
_("Terminal UI for StarDict dictionaries"),
|
2013-05-18 23:48:24 +02:00
|
|
|
"Copyright (c) 2013, Přemysl Janouch",
|
|
|
|
"",
|
2013-05-19 02:46:05 +02:00
|
|
|
_("Type to search")
|
2013-05-18 23:48:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
app_show_message (self, lines, G_N_ELEMENTS (lines));
|
|
|
|
}
|
|
|
|
|
2013-05-12 01:27:25 +02:00
|
|
|
/** Redraw the dictionary view. */
|
|
|
|
static void
|
2013-05-15 17:26:39 +02:00
|
|
|
app_redraw_view (Application *self)
|
2013-05-12 01:27:25 +02:00
|
|
|
{
|
2013-05-18 23:48:24 +02:00
|
|
|
if (self->show_help)
|
|
|
|
{
|
|
|
|
app_show_help (self);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-14 21:49:39 +02:00
|
|
|
move (1, 0);
|
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
guint i, k = self->top_offset, shown = 0;
|
|
|
|
for (i = 0; i < self->entries->len; i++)
|
2013-05-14 21:49:39 +02:00
|
|
|
{
|
2013-05-15 17:26:39 +02:00
|
|
|
ViewEntry *ve = g_ptr_array_index (self->entries, i);
|
2013-05-14 21:49:39 +02:00
|
|
|
for (; k < ve->definitions_length; k++)
|
|
|
|
{
|
|
|
|
int attrs = 0;
|
2013-05-15 17:26:39 +02:00
|
|
|
if (shown == self->selected) attrs |= A_REVERSE;
|
2013-05-14 21:49:39 +02:00
|
|
|
if (k + 1 == ve->definitions_length) attrs |= A_UNDERLINE;
|
|
|
|
attrset (attrs);
|
|
|
|
|
2013-05-16 23:56:01 +02:00
|
|
|
guint left_width = app_get_left_column_width (self);
|
2013-05-18 23:48:24 +02:00
|
|
|
app_add_utf8_string (self, ve->word, left_width);
|
2013-05-15 12:53:50 +02:00
|
|
|
addwstr (L" ");
|
2013-05-18 23:48:24 +02:00
|
|
|
app_add_utf8_string (self,
|
|
|
|
ve->definitions[k], COLS - left_width - 1);
|
2013-05-15 12:53:50 +02:00
|
|
|
|
2013-05-14 21:49:39 +02:00
|
|
|
if ((gint) ++shown == LINES - 1)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
k = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
attrset (0);
|
|
|
|
clrtobot ();
|
2013-05-12 01:27:25 +02:00
|
|
|
refresh ();
|
|
|
|
}
|
|
|
|
|
2013-05-14 21:49:39 +02:00
|
|
|
/** Just prepends a new view entry into the entries array. */
|
|
|
|
static ViewEntry *
|
2013-05-15 17:26:39 +02:00
|
|
|
prepend_entry (Application *self, guint32 position)
|
2013-05-14 21:49:39 +02:00
|
|
|
{
|
2013-05-15 17:26:39 +02:00
|
|
|
StardictIterator *iterator = stardict_iterator_new (self->dict, position);
|
2013-05-14 21:49:39 +02:00
|
|
|
ViewEntry *ve = view_entry_new (iterator);
|
|
|
|
g_object_unref (iterator);
|
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
g_ptr_array_add (self->entries, NULL);
|
|
|
|
memmove (self->entries->pdata + 1, self->entries->pdata,
|
|
|
|
sizeof ve * (self->entries->len - 1));
|
|
|
|
return g_ptr_array_index (self->entries, 0) = ve;
|
2013-05-14 21:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Just appends a new view entry to the entries array. */
|
|
|
|
static ViewEntry *
|
2013-05-15 17:26:39 +02:00
|
|
|
append_entry (Application *self, guint32 position)
|
2013-05-14 21:49:39 +02:00
|
|
|
{
|
|
|
|
ViewEntry *ve = NULL;
|
|
|
|
StardictIterator *iterator = stardict_iterator_new
|
2013-05-15 17:26:39 +02:00
|
|
|
(self->dict, position);
|
2013-05-14 21:49:39 +02:00
|
|
|
if (stardict_iterator_is_valid (iterator))
|
|
|
|
{
|
|
|
|
ve = view_entry_new (iterator);
|
2013-05-15 17:26:39 +02:00
|
|
|
g_ptr_array_add (self->entries, ve);
|
2013-05-14 21:49:39 +02:00
|
|
|
}
|
|
|
|
g_object_unref (iterator);
|
|
|
|
return ve;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Counts the number of definitions available for seeing. */
|
|
|
|
static guint
|
2013-05-19 01:02:19 +02:00
|
|
|
app_count_view_items (Application *self)
|
2013-05-14 21:49:39 +02:00
|
|
|
{
|
|
|
|
guint i, n_definitions = 0;
|
2013-05-15 17:26:39 +02:00
|
|
|
for (i = 0; i < self->entries->len; i++)
|
2013-05-14 21:49:39 +02:00
|
|
|
{
|
2013-05-15 17:26:39 +02:00
|
|
|
ViewEntry *entry = g_ptr_array_index (self->entries, i);
|
2013-05-14 21:49:39 +02:00
|
|
|
n_definitions += entry->definitions_length;
|
|
|
|
}
|
|
|
|
return n_definitions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Scroll up @a n entries. */
|
|
|
|
static gboolean
|
2013-05-15 17:26:39 +02:00
|
|
|
app_scroll_up (Application *self, guint n)
|
2013-05-14 21:49:39 +02:00
|
|
|
{
|
|
|
|
gboolean success = TRUE;
|
2013-05-19 01:02:19 +02:00
|
|
|
guint n_definitions = app_count_view_items (self);
|
2013-05-14 21:49:39 +02:00
|
|
|
while (n--)
|
|
|
|
{
|
2013-05-15 17:26:39 +02:00
|
|
|
if (self->top_offset > 0)
|
2013-05-14 21:49:39 +02:00
|
|
|
{
|
2013-05-15 17:26:39 +02:00
|
|
|
self->top_offset--;
|
2013-05-14 21:49:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We've reached the top */
|
2013-05-15 17:26:39 +02:00
|
|
|
if (self->top_position == 0)
|
2013-05-14 21:49:39 +02:00
|
|
|
{
|
|
|
|
success = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
ViewEntry *ve = prepend_entry (self, --self->top_position);
|
|
|
|
self->top_offset = ve->definitions_length - 1;
|
2013-05-14 21:49:39 +02:00
|
|
|
n_definitions += ve->definitions_length;
|
|
|
|
|
|
|
|
/* Remove the last entry if not shown */
|
|
|
|
ViewEntry *last_entry =
|
2013-05-15 17:26:39 +02:00
|
|
|
g_ptr_array_index (self->entries, self->entries->len - 1);
|
|
|
|
if ((gint) (n_definitions - self->top_offset
|
2013-05-14 21:49:39 +02:00
|
|
|
- last_entry->definitions_length) >= LINES - 1)
|
|
|
|
{
|
2013-05-15 13:19:36 +02:00
|
|
|
n_definitions -= last_entry->definitions_length;
|
2013-05-14 21:49:39 +02:00
|
|
|
g_ptr_array_remove_index_fast
|
2013-05-15 17:26:39 +02:00
|
|
|
(self->entries, self->entries->len - 1);
|
2013-05-14 21:49:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
app_redraw_view (self);
|
2013-05-14 21:49:39 +02:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Scroll down @a n entries. */
|
|
|
|
static gboolean
|
2013-05-15 17:26:39 +02:00
|
|
|
app_scroll_down (Application *self, guint n)
|
2013-05-14 21:49:39 +02:00
|
|
|
{
|
|
|
|
gboolean success = TRUE;
|
2013-05-19 01:02:19 +02:00
|
|
|
guint n_definitions = app_count_view_items (self);
|
2013-05-14 21:49:39 +02:00
|
|
|
while (n--)
|
|
|
|
{
|
2013-05-15 17:26:39 +02:00
|
|
|
if (self->entries->len == 0)
|
2013-05-14 21:49:39 +02:00
|
|
|
{
|
|
|
|
success = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
ViewEntry *first_entry = g_ptr_array_index (self->entries, 0);
|
|
|
|
if (self->top_offset < first_entry->definitions_length - 1)
|
|
|
|
self->top_offset++;
|
2013-05-14 21:49:39 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
n_definitions -= first_entry->definitions_length;
|
2013-05-15 17:26:39 +02:00
|
|
|
g_ptr_array_remove_index (self->entries, 0);
|
|
|
|
self->top_position++;
|
|
|
|
self->top_offset = 0;
|
2013-05-14 21:49:39 +02:00
|
|
|
}
|
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
if ((gint) (n_definitions - self->top_offset) < LINES - 1)
|
2013-05-14 21:49:39 +02:00
|
|
|
{
|
2013-05-15 17:26:39 +02:00
|
|
|
ViewEntry *ve = append_entry (self,
|
|
|
|
self->top_position + self->entries->len);
|
2013-05-14 21:49:39 +02:00
|
|
|
if (ve != NULL)
|
|
|
|
n_definitions += ve->definitions_length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fix cursor to not point below the view items */
|
2013-05-15 17:26:39 +02:00
|
|
|
if (self->selected >= n_definitions - self->top_offset)
|
|
|
|
self->selected = n_definitions - self->top_offset - 1;
|
2013-05-14 21:49:39 +02:00
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
app_redraw_view (self);
|
2013-05-14 21:49:39 +02:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2013-05-16 23:56:01 +02:00
|
|
|
/** Moves the selection one entry up. */
|
|
|
|
static gboolean
|
|
|
|
app_one_entry_up (Application *self)
|
|
|
|
{
|
|
|
|
if (self->selected == 0 && self->top_offset == 0)
|
|
|
|
{
|
|
|
|
if (self->top_position == 0)
|
|
|
|
return FALSE;
|
|
|
|
prepend_entry (self, --self->top_position);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the last entry that starts above the selection
|
|
|
|
gint first = -self->top_offset;
|
|
|
|
guint i;
|
|
|
|
for (i = 0; i < self->entries->len; i++)
|
|
|
|
{
|
|
|
|
ViewEntry *ve = g_ptr_array_index (self->entries, i);
|
|
|
|
gint new_first = first + ve->definitions_length;
|
|
|
|
if (new_first >= (gint) self->selected)
|
|
|
|
break;
|
|
|
|
first = new_first;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first < 0)
|
|
|
|
{
|
|
|
|
self->selected = 0;
|
|
|
|
app_scroll_up (self, -first);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
self->selected = first;
|
|
|
|
app_redraw_view (self);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Moves the selection one entry down. */
|
|
|
|
static void
|
|
|
|
app_one_entry_down (Application *self)
|
|
|
|
{
|
|
|
|
// Find the first entry that starts below the selection
|
|
|
|
gint first = -self->top_offset;
|
|
|
|
guint i;
|
|
|
|
for (i = 0; i < self->entries->len; i++)
|
|
|
|
{
|
|
|
|
ViewEntry *ve = g_ptr_array_index (self->entries, i);
|
|
|
|
first += ve->definitions_length;
|
|
|
|
if (first > (gint) self->selected)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first > LINES - 2)
|
|
|
|
{
|
|
|
|
self->selected = LINES - 2;
|
|
|
|
app_scroll_down (self, first - (LINES - 2));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
self->selected = first;
|
|
|
|
app_redraw_view (self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-12 01:27:25 +02:00
|
|
|
/** Redraw everything. */
|
|
|
|
static void
|
2013-05-15 17:26:39 +02:00
|
|
|
app_redraw (Application *self)
|
2013-05-12 01:27:25 +02:00
|
|
|
{
|
2013-05-15 17:26:39 +02:00
|
|
|
app_redraw_view (self);
|
|
|
|
app_redraw_top (self);
|
2013-05-04 16:14:25 +02:00
|
|
|
}
|
|
|
|
|
2013-05-14 21:49:39 +02:00
|
|
|
/** Search for the current entry. */
|
|
|
|
static void
|
2013-05-15 17:26:39 +02:00
|
|
|
app_search_for_entry (Application *self)
|
2013-05-14 21:49:39 +02:00
|
|
|
{
|
2013-05-15 19:30:47 +02:00
|
|
|
gchar *input_utf8 = g_ucs4_to_utf8
|
|
|
|
((gunichar *) self->input->data, -1, NULL, NULL, NULL);
|
|
|
|
g_return_if_fail (input_utf8 != NULL);
|
|
|
|
|
|
|
|
StardictIterator *iterator =
|
|
|
|
stardict_dict_search (self->dict, input_utf8, NULL);
|
|
|
|
g_free (input_utf8);
|
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
self->top_position = stardict_iterator_get_offset (iterator);
|
|
|
|
self->top_offset = 0;
|
2013-05-19 01:02:19 +02:00
|
|
|
self->selected = 0;
|
2013-05-14 21:49:39 +02:00
|
|
|
g_object_unref (iterator);
|
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
app_reload_view (self);
|
|
|
|
app_redraw_view (self);
|
2013-05-14 21:49:39 +02:00
|
|
|
}
|
|
|
|
|
2013-05-18 22:26:57 +02:00
|
|
|
#define SAVE_CURSOR \
|
|
|
|
int last_x, last_y; \
|
|
|
|
getyx (stdscr, last_y, last_x);
|
|
|
|
|
|
|
|
#define RESTORE_CURSOR \
|
|
|
|
move (last_y, last_x); \
|
|
|
|
refresh ();
|
|
|
|
|
|
|
|
/** Process input above KEY_MAX. */
|
2013-05-04 16:14:25 +02:00
|
|
|
static gboolean
|
2013-05-18 22:26:57 +02:00
|
|
|
app_process_extra_code (Application *self, CursesEvent *event)
|
2013-05-04 16:14:25 +02:00
|
|
|
{
|
2013-05-18 22:26:57 +02:00
|
|
|
SAVE_CURSOR
|
|
|
|
switch (translate_extra_keycode (event->code, self->terminal_type))
|
|
|
|
{
|
|
|
|
case KEY_CTRL_UP:
|
|
|
|
app_one_entry_up (self);
|
|
|
|
RESTORE_CURSOR
|
|
|
|
break;
|
|
|
|
case KEY_CTRL_DOWN:
|
|
|
|
app_one_entry_down (self);
|
|
|
|
RESTORE_CURSOR
|
|
|
|
break;
|
2013-05-18 00:38:18 +02:00
|
|
|
|
2013-05-18 22:26:57 +02:00
|
|
|
case KEY_ALT_LEFT:
|
|
|
|
self->division = (app_get_left_column_width (self) - 1.) / COLS;
|
|
|
|
app_redraw_view (self);
|
|
|
|
RESTORE_CURSOR
|
|
|
|
break;
|
|
|
|
case KEY_ALT_RIGHT:
|
|
|
|
self->division = (app_get_left_column_width (self) + 1.) / COLS;
|
|
|
|
app_redraw_view (self);
|
|
|
|
RESTORE_CURSOR
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-05-18 00:38:18 +02:00
|
|
|
|
2013-05-18 22:26:57 +02:00
|
|
|
/** Process input that's not a character. */
|
|
|
|
static gboolean
|
|
|
|
app_process_nonchar_code (Application *self, CursesEvent *event)
|
|
|
|
{
|
|
|
|
SAVE_CURSOR
|
2013-05-15 17:26:39 +02:00
|
|
|
switch (event->code)
|
2013-05-04 16:14:25 +02:00
|
|
|
{
|
2013-05-15 17:26:39 +02:00
|
|
|
case KEY_RESIZE:
|
2013-05-19 01:02:19 +02:00
|
|
|
{
|
2013-05-15 17:26:39 +02:00
|
|
|
app_reload_view (self);
|
2013-05-19 01:02:19 +02:00
|
|
|
|
|
|
|
guint n_visible = app_count_view_items (self) - self->top_offset;
|
|
|
|
if ((gint) n_visible > LINES - 1)
|
|
|
|
n_visible = LINES - 1;
|
|
|
|
|
|
|
|
if (self->selected >= n_visible)
|
|
|
|
{
|
|
|
|
app_scroll_down (self, self->selected - n_visible + 1);
|
|
|
|
self->selected = n_visible - 1;
|
|
|
|
}
|
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
app_redraw (self);
|
|
|
|
break;
|
2013-05-19 01:02:19 +02:00
|
|
|
}
|
2013-05-15 17:26:39 +02:00
|
|
|
case KEY_MOUSE:
|
2013-05-18 01:14:49 +02:00
|
|
|
if (!(event->mouse.bstate & BUTTON1_PRESSED))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (event->mouse.y == 0)
|
|
|
|
{
|
|
|
|
gsize label_len = g_utf8_strlen (self->search_label, -1);
|
|
|
|
gint pos = event->mouse.x - label_len;
|
|
|
|
if (pos >= 0)
|
|
|
|
{
|
|
|
|
self->input_pos = MIN ((guint) pos, self->input->len);
|
|
|
|
move (0, label_len + self->input_pos);
|
|
|
|
refresh ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (event->mouse.y <= (int)
|
2013-05-19 01:02:19 +02:00
|
|
|
(app_count_view_items (self) - self->top_offset))
|
2013-05-04 16:14:25 +02:00
|
|
|
{
|
2013-05-15 17:26:39 +02:00
|
|
|
self->selected = event->mouse.y - 1;
|
|
|
|
app_redraw_view (self);
|
2013-05-18 00:38:18 +02:00
|
|
|
RESTORE_CURSOR
|
2013-05-15 17:26:39 +02:00
|
|
|
}
|
|
|
|
break;
|
2013-05-14 21:49:39 +02:00
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
case KEY_UP:
|
|
|
|
if (self->selected > 0)
|
|
|
|
{
|
|
|
|
self->selected--;
|
|
|
|
app_redraw_view (self);
|
2013-05-04 16:14:25 +02:00
|
|
|
}
|
2013-05-15 17:26:39 +02:00
|
|
|
else
|
|
|
|
app_scroll_up (self, 1);
|
2013-05-18 00:38:18 +02:00
|
|
|
RESTORE_CURSOR
|
2013-05-15 17:26:39 +02:00
|
|
|
break;
|
|
|
|
case KEY_DOWN:
|
|
|
|
if ((gint) self->selected < LINES - 2 &&
|
2013-05-19 01:02:19 +02:00
|
|
|
self->selected < app_count_view_items (self) - self->top_offset - 1)
|
2013-05-15 17:26:39 +02:00
|
|
|
{
|
|
|
|
self->selected++;
|
|
|
|
app_redraw_view (self);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
app_scroll_down (self, 1);
|
2013-05-18 00:38:18 +02:00
|
|
|
RESTORE_CURSOR
|
2013-05-15 17:26:39 +02:00
|
|
|
break;
|
|
|
|
case KEY_PPAGE:
|
|
|
|
app_scroll_up (self, LINES - 1);
|
2013-05-18 00:38:18 +02:00
|
|
|
// FIXME selection
|
|
|
|
RESTORE_CURSOR
|
2013-05-15 17:26:39 +02:00
|
|
|
break;
|
|
|
|
case KEY_NPAGE:
|
|
|
|
app_scroll_down (self, LINES - 1);
|
2013-05-18 00:38:18 +02:00
|
|
|
// FIXME selection
|
|
|
|
RESTORE_CURSOR
|
2013-05-15 17:26:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_HOME:
|
|
|
|
self->input_pos = 0;
|
|
|
|
app_redraw_top (self);
|
|
|
|
break;
|
|
|
|
case KEY_END:
|
2013-05-15 19:30:47 +02:00
|
|
|
self->input_pos = self->input->len;
|
2013-05-15 17:26:39 +02:00
|
|
|
app_redraw_top (self);
|
|
|
|
break;
|
|
|
|
case KEY_LEFT:
|
|
|
|
if (self->input_pos > 0)
|
|
|
|
{
|
|
|
|
self->input_pos--;
|
|
|
|
app_redraw_top (self);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_RIGHT:
|
2013-05-15 19:30:47 +02:00
|
|
|
if (self->input_pos < self->input->len)
|
2013-05-15 17:26:39 +02:00
|
|
|
{
|
|
|
|
self->input_pos++;
|
|
|
|
app_redraw_top (self);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_BACKSPACE:
|
|
|
|
if (self->input_pos > 0)
|
|
|
|
{
|
2013-05-15 19:30:47 +02:00
|
|
|
g_array_remove_index (self->input, --self->input_pos);
|
2013-05-15 17:26:39 +02:00
|
|
|
app_search_for_entry (self);
|
|
|
|
app_redraw_top (self);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_DC:
|
2013-05-15 19:30:47 +02:00
|
|
|
if (self->input_pos < self->input->len)
|
2013-05-15 17:26:39 +02:00
|
|
|
{
|
2013-05-15 19:30:47 +02:00
|
|
|
g_array_remove_index (self->input, self->input_pos);
|
2013-05-15 17:26:39 +02:00
|
|
|
app_search_for_entry (self);
|
|
|
|
app_redraw_top (self);
|
|
|
|
}
|
|
|
|
break;
|
2013-05-18 22:26:57 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
return app_process_extra_code (self, event);
|
2013-05-04 16:14:25 +02:00
|
|
|
}
|
2013-05-15 17:26:39 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Process input events from ncurses. */
|
|
|
|
static gboolean
|
|
|
|
app_process_curses_event (Application *self, CursesEvent *event)
|
|
|
|
{
|
|
|
|
if (!event->is_char)
|
|
|
|
return app_process_nonchar_code (self, event);
|
2013-05-04 16:14:25 +02:00
|
|
|
|
2013-05-12 01:27:25 +02:00
|
|
|
switch (event->code)
|
2013-05-04 16:14:25 +02:00
|
|
|
{
|
2013-05-12 01:27:25 +02:00
|
|
|
case KEY_ESCAPE:
|
2013-05-04 16:14:25 +02:00
|
|
|
return FALSE;
|
2013-05-16 23:56:01 +02:00
|
|
|
case KEY_RETURN:
|
|
|
|
self->input_confirmed = TRUE;
|
|
|
|
app_redraw_top (self);
|
|
|
|
break;
|
|
|
|
|
2013-05-18 03:06:35 +02:00
|
|
|
case KEY_FF: // Ctrl-L -- redraw everything
|
|
|
|
clear ();
|
|
|
|
app_redraw (self);
|
|
|
|
break;
|
2013-05-18 22:26:57 +02:00
|
|
|
|
|
|
|
case KEY_STX: // Ctrl-B -- back
|
|
|
|
case KEY_ACK: // Ctrl-F -- forward
|
|
|
|
case KEY_DLE: // Ctrl-P -- previous
|
|
|
|
case KEY_SO: // Ctrl-N -- next
|
|
|
|
// TODO map this to something useful
|
|
|
|
break;
|
|
|
|
|
2013-05-16 20:46:33 +02:00
|
|
|
case KEY_SOH: // Ctrl-A -- move to the start of line
|
|
|
|
self->input_pos = 0;
|
|
|
|
app_redraw_top (self);
|
|
|
|
break;
|
|
|
|
case KEY_ENQ: // Ctrl-E -- move to the end of line
|
|
|
|
self->input_pos = self->input->len;
|
|
|
|
app_redraw_top (self);
|
|
|
|
break;
|
2013-05-12 01:27:25 +02:00
|
|
|
case KEY_VT: // Ctrl-K -- delete until the end of line
|
2013-05-15 19:30:47 +02:00
|
|
|
if (self->input_pos < self->input->len)
|
|
|
|
{
|
|
|
|
g_array_remove_range (self->input,
|
|
|
|
self->input_pos, self->input->len - self->input_pos);
|
|
|
|
app_search_for_entry (self);
|
|
|
|
app_redraw_top (self);
|
|
|
|
}
|
2013-05-12 01:27:25 +02:00
|
|
|
return TRUE;
|
|
|
|
case KEY_ETB: // Ctrl-W -- delete word before cursor
|
|
|
|
{
|
2013-05-15 19:30:47 +02:00
|
|
|
if (self->input_pos == 0)
|
2013-05-12 01:27:25 +02:00
|
|
|
return TRUE;
|
|
|
|
|
2013-05-15 19:30:47 +02:00
|
|
|
gint i = self->input_pos;
|
|
|
|
while (i)
|
|
|
|
if (g_array_index (self->input, gunichar, --i) != L' ')
|
|
|
|
break;
|
|
|
|
while (i--)
|
|
|
|
if (g_array_index (self->input, gunichar, i) == L' ')
|
|
|
|
break;
|
2013-05-12 01:27:25 +02:00
|
|
|
|
2013-05-15 19:30:47 +02:00
|
|
|
i++;
|
|
|
|
g_array_remove_range (self->input, i, self->input_pos - i);
|
|
|
|
self->input_pos = i;
|
2013-05-12 01:27:25 +02:00
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
app_search_for_entry (self);
|
|
|
|
app_redraw_top (self);
|
2013-05-12 01:27:25 +02:00
|
|
|
return TRUE;
|
2013-05-04 16:14:25 +02:00
|
|
|
}
|
2013-05-12 01:27:25 +02:00
|
|
|
case KEY_NAK: // Ctrl-U -- delete everything before the cursor
|
2013-05-15 19:30:47 +02:00
|
|
|
if (self->input->len != 0)
|
|
|
|
{
|
|
|
|
g_array_remove_range (self->input, 0, self->input_pos);
|
|
|
|
self->input_pos = 0;
|
2013-05-14 21:49:39 +02:00
|
|
|
|
2013-05-15 19:30:47 +02:00
|
|
|
app_search_for_entry (self);
|
|
|
|
app_redraw_top (self);
|
|
|
|
}
|
2013-05-12 01:27:25 +02:00
|
|
|
return TRUE;
|
2013-07-09 06:47:19 +02:00
|
|
|
case KEY_DC4: // Ctrl-T -- transposition
|
|
|
|
{
|
|
|
|
if (!self->input_pos || self->input->len < 2)
|
|
|
|
break;
|
|
|
|
|
|
|
|
guint start = self->input_pos - 1;
|
|
|
|
if (self->input_pos >= self->input->len)
|
|
|
|
start--;
|
|
|
|
|
|
|
|
gunichar tmp = g_array_index (self->input, gunichar, start);
|
|
|
|
g_array_index (self->input, gunichar, start)
|
|
|
|
= g_array_index (self->input, gunichar, start + 1);
|
|
|
|
g_array_index (self->input, gunichar, start + 1) = tmp;
|
|
|
|
|
|
|
|
if (self->input_pos < self->input->len)
|
|
|
|
self->input_pos++;
|
|
|
|
|
|
|
|
app_search_for_entry (self);
|
|
|
|
app_redraw_top (self);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-05-12 01:27:25 +02:00
|
|
|
}
|
|
|
|
|
2013-05-15 12:53:50 +02:00
|
|
|
wchar_t code = event->code;
|
|
|
|
gchar *letter = g_convert_with_iconv ((gchar *) &code, sizeof code,
|
2013-05-15 17:26:39 +02:00
|
|
|
self->wchar_to_utf8, NULL, NULL, NULL);
|
2013-05-12 01:27:25 +02:00
|
|
|
g_return_val_if_fail (letter != NULL, FALSE);
|
|
|
|
|
2013-05-15 19:30:47 +02:00
|
|
|
gunichar c = g_utf8_get_char (letter);
|
|
|
|
if (g_unichar_isprint (c))
|
2013-05-12 01:27:25 +02:00
|
|
|
{
|
2013-05-18 23:48:24 +02:00
|
|
|
self->show_help = FALSE;
|
|
|
|
|
2013-05-16 23:56:01 +02:00
|
|
|
if (self->input_confirmed)
|
|
|
|
{
|
|
|
|
if (self->input->len != 0)
|
|
|
|
g_array_remove_range (self->input, 0, self->input->len);
|
|
|
|
self->input_pos = 0;
|
|
|
|
self->input_confirmed = FALSE;
|
|
|
|
}
|
|
|
|
|
2013-05-15 19:30:47 +02:00
|
|
|
g_array_insert_val (self->input, self->input_pos++, c);
|
2013-05-15 17:26:39 +02:00
|
|
|
app_search_for_entry (self);
|
|
|
|
app_redraw_top (self);
|
2013-05-12 01:27:25 +02:00
|
|
|
}
|
|
|
|
g_free (letter);
|
2013-05-04 16:14:25 +02:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
// --- SIGWINCH ----------------------------------------------------------------
|
|
|
|
|
|
|
|
static int g_winch_pipe[2]; /**< SIGWINCH signalling pipe. */
|
|
|
|
static void (*g_old_winch_handler) (int);
|
|
|
|
|
|
|
|
static void
|
|
|
|
winch_handler (int signum)
|
|
|
|
{
|
|
|
|
/* Call the ncurses handler. */
|
|
|
|
if (g_old_winch_handler)
|
|
|
|
g_old_winch_handler (signum);
|
|
|
|
|
|
|
|
/* And wake up the poll() call. */
|
|
|
|
write (g_winch_pipe[1], "x", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
install_winch_handler (void)
|
|
|
|
{
|
|
|
|
struct sigaction act, oldact;
|
|
|
|
|
|
|
|
act.sa_handler = winch_handler;
|
|
|
|
act.sa_flags = SA_RESTART;
|
|
|
|
sigemptyset (&act.sa_mask);
|
|
|
|
sigaction (SIGWINCH, &act, &oldact);
|
|
|
|
|
|
|
|
/* Save the ncurses handler. */
|
|
|
|
if (oldact.sa_handler != SIG_DFL
|
|
|
|
&& oldact.sa_handler != SIG_IGN)
|
|
|
|
g_old_winch_handler = oldact.sa_handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Initialisation, event handling ------------------------------------------
|
|
|
|
|
|
|
|
Application g_application;
|
2013-05-12 01:27:25 +02:00
|
|
|
|
2013-05-04 16:14:25 +02:00
|
|
|
static gboolean
|
|
|
|
process_stdin_input (void)
|
|
|
|
{
|
|
|
|
CursesEvent event;
|
|
|
|
int sta;
|
|
|
|
|
|
|
|
while ((sta = get_wch (&event.code)) != ERR)
|
|
|
|
{
|
|
|
|
event.is_char = (sta == OK);
|
|
|
|
if (sta == KEY_CODE_YES && event.code == KEY_MOUSE
|
|
|
|
&& getmouse (&event.mouse) == ERR)
|
|
|
|
abort ();
|
2013-05-15 17:26:39 +02:00
|
|
|
if (!app_process_curses_event (&g_application, &event))
|
2013-05-04 16:14:25 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
process_winch_input (int fd)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
read (fd, &c, 1);
|
|
|
|
return process_stdin_input ();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
2013-05-14 21:49:39 +02:00
|
|
|
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
|
|
|
if (glib_check_version (2, 36, 0))
|
|
|
|
g_type_init ();
|
|
|
|
G_GNUC_END_IGNORE_DEPRECATIONS
|
|
|
|
|
2013-05-17 18:26:08 +02:00
|
|
|
gboolean show_version = FALSE;
|
|
|
|
GOptionEntry entries[] =
|
2013-05-04 16:14:25 +02:00
|
|
|
{
|
2013-05-17 18:26:08 +02:00
|
|
|
{ "version", 0, G_OPTION_FLAG_IN_MAIN,
|
|
|
|
G_OPTION_ARG_NONE, &show_version,
|
2013-05-19 02:46:05 +02:00
|
|
|
N_("Output version information and exit"), NULL },
|
2013-05-04 16:14:25 +02:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!setlocale (LC_ALL, ""))
|
2013-05-19 05:26:20 +02:00
|
|
|
g_printerr ("%s: %s\n", _("Warning"), _("failed to set the locale"));
|
2013-05-04 16:14:25 +02:00
|
|
|
|
2013-05-19 02:46:05 +02:00
|
|
|
bindtextdomain (GETTEXT_PACKAGE, GETTEXT_DIRNAME);
|
|
|
|
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
|
|
|
|
textdomain (GETTEXT_PACKAGE);
|
|
|
|
|
2013-05-04 16:14:25 +02:00
|
|
|
GError *error = NULL;
|
2013-05-12 01:27:25 +02:00
|
|
|
GOptionContext *ctx = g_option_context_new
|
2013-05-19 02:46:05 +02:00
|
|
|
(N_("dictionary.ifo - StarDict terminal UI"));
|
|
|
|
g_option_context_add_main_entries (ctx, entries, GETTEXT_PACKAGE);
|
|
|
|
g_option_context_set_translation_domain (ctx, GETTEXT_PACKAGE);
|
2013-05-04 16:14:25 +02:00
|
|
|
if (!g_option_context_parse (ctx, &argc, &argv, &error))
|
|
|
|
{
|
2013-05-12 01:27:25 +02:00
|
|
|
g_printerr ("%s: %s: %s\n", _("Error"), _("option parsing failed"),
|
|
|
|
error->message);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2013-05-17 18:26:08 +02:00
|
|
|
if (show_version)
|
|
|
|
{
|
|
|
|
g_print (PROJECT_NAME " " PROJECT_VERSION "\n");
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2013-05-12 01:27:25 +02:00
|
|
|
if (argc != 2)
|
|
|
|
{
|
|
|
|
gchar *help = g_option_context_get_help (ctx, TRUE, FALSE);
|
|
|
|
g_printerr ("%s", help);
|
|
|
|
g_free (help);
|
2013-05-04 16:14:25 +02:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2013-05-12 01:27:25 +02:00
|
|
|
g_option_context_free (ctx);
|
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
app_init (&g_application, argv[1]);
|
2013-05-12 01:27:25 +02:00
|
|
|
|
2013-05-04 16:14:25 +02:00
|
|
|
if (!initscr ()
|
|
|
|
|| cbreak () == ERR
|
2013-05-12 01:27:25 +02:00
|
|
|
|| noecho () == ERR
|
|
|
|
|| nonl () == ERR)
|
2013-05-04 16:14:25 +02:00
|
|
|
abort ();
|
|
|
|
|
|
|
|
keypad (stdscr, TRUE); /* Enable character processing. */
|
|
|
|
nodelay (stdscr, TRUE); /* Don't block on get_wch(). */
|
|
|
|
|
2013-05-12 01:27:25 +02:00
|
|
|
mousemask (ALL_MOUSE_EVENTS, NULL); /* Register mouse events. */
|
2013-05-15 13:03:54 +02:00
|
|
|
mouseinterval (0);
|
2013-05-04 16:14:25 +02:00
|
|
|
|
|
|
|
if (pipe (g_winch_pipe) == -1)
|
|
|
|
abort ();
|
|
|
|
install_winch_handler ();
|
|
|
|
|
2013-05-15 17:26:39 +02:00
|
|
|
app_redraw (&g_application);
|
2013-05-04 16:14:25 +02:00
|
|
|
|
2013-05-12 01:27:25 +02:00
|
|
|
/* Message loop. */
|
2013-05-04 16:14:25 +02:00
|
|
|
struct pollfd pollfd[2];
|
|
|
|
|
|
|
|
pollfd[0].fd = fileno (stdin);
|
|
|
|
pollfd[0].events = POLLIN;
|
|
|
|
pollfd[1].fd = g_winch_pipe[0];
|
|
|
|
pollfd[1].events = POLLIN;
|
|
|
|
|
|
|
|
while (TRUE)
|
|
|
|
{
|
2013-05-19 20:06:29 +02:00
|
|
|
if (poll_restart (pollfd, 2, -1) == -1)
|
2013-05-04 16:14:25 +02:00
|
|
|
abort ();
|
|
|
|
|
|
|
|
if ((pollfd[0].revents & POLLIN)
|
|
|
|
&& !process_stdin_input ())
|
|
|
|
break;
|
|
|
|
if ((pollfd[1].revents & POLLIN)
|
2013-05-12 01:27:25 +02:00
|
|
|
&& !process_winch_input (pollfd[1].fd))
|
2013-05-04 16:14:25 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
endwin ();
|
2013-05-15 17:26:39 +02:00
|
|
|
app_destroy (&g_application);
|
2013-05-04 16:14:25 +02:00
|
|
|
|
|
|
|
if (close (g_winch_pipe[0]) == -1
|
|
|
|
|| close (g_winch_pipe[1]) == -1)
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|