123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236 |
- /* Widgets for the Midnight Commander
- Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
- 2004, 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
- Authors: 1994, 1995 Radek Doulik
- 1994, 1995 Miguel de Icaza
- 1995 Jakub Jelinek
- 1996 Andrej Borsenkow
- 1997 Norbert Warmuth
- 2009, 2010 Andrew Borodin
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- /** \file input.c
- * \brief Source: WInput widget
- */
- #include <config.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include "lib/global.h"
- #include "lib/tty/tty.h"
- #include "lib/tty/mouse.h"
- #include "lib/tty/key.h" /* XCTRL and ALT macros */
- #include "lib/fileloc.h"
- #include "lib/skin.h"
- #include "lib/strutil.h"
- #include "lib/util.h"
- #include "lib/keybind.h" /* global_keymap_t */
- #include "lib/widget.h"
- #include "lib/event.h" /* mc_event_raise() */
- #include "input_complete.h"
- #include "src/main.h" /* home_dir */
- #include "src/filemanager/midnight.h" /* current_panel */
- /*** global variables ****************************************************************************/
- int quote = 0;
- const global_keymap_t *input_map;
- /*** file scope macro definitions ****************************************************************/
- #define LARGE_HISTORY_BUTTON 1
- #ifdef LARGE_HISTORY_BUTTON
- #define HISTORY_BUTTON_WIDTH 3
- #else
- #define HISTORY_BUTTON_WIDTH 1
- #endif
- #define should_show_history_button(in) \
- (in->history != NULL && in->field_width > HISTORY_BUTTON_WIDTH * 2 + 1 \
- && in->widget.owner != NULL)
- /*** file scope type declarations ****************************************************************/
- /*** file scope variables ************************************************************************/
- /* Input widgets have a global kill ring */
- /* Pointer to killed data */
- static char *kill_buffer = NULL;
- /*** file scope functions ************************************************************************/
- /* --------------------------------------------------------------------------------------------- */
- static void
- draw_history_button (WInput * in)
- {
- char c;
- gboolean disabled = (((Widget *) in)->options & W_DISABLED) != 0;
- c = in->history->next ? (in->history->prev ? '|' : 'v') : '^';
- widget_move (&in->widget, 0, in->field_width - HISTORY_BUTTON_WIDTH);
- tty_setcolor (disabled ? DISABLED_COLOR : in->color[WINPUTC_HISTORY]);
- #ifdef LARGE_HISTORY_BUTTON
- {
- Dlg_head *h;
- h = in->widget.owner;
- tty_print_string ("[ ]");
- widget_move (&in->widget, 0, in->field_width - HISTORY_BUTTON_WIDTH + 1);
- }
- #endif
- tty_print_char (c);
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- input_set_markers (WInput * in, long m1)
- {
- in->mark = m1;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- input_mark_cmd (WInput * in, gboolean mark)
- {
- if (mark == 0)
- {
- in->highlight = FALSE;
- input_set_markers (in, 0);
- }
- else
- {
- in->highlight = TRUE;
- input_set_markers (in, in->point);
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- static gboolean
- input_eval_marks (WInput * in, long *start_mark, long *end_mark)
- {
- if (in->highlight)
- {
- *start_mark = min (in->mark, in->point);
- *end_mark = max (in->mark, in->point);
- return TRUE;
- }
- else
- {
- *start_mark = *end_mark = 0;
- return FALSE;
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- delete_region (WInput * in, int x_first, int x_last)
- {
- int first = min (x_first, x_last);
- int last = max (x_first, x_last);
- size_t len;
- input_mark_cmd (in, FALSE);
- in->point = first;
- last = str_offset_to_pos (in->buffer, last);
- first = str_offset_to_pos (in->buffer, first);
- len = strlen (&in->buffer[last]) + 1;
- memmove (&in->buffer[first], &in->buffer[last], len);
- in->charpoint = 0;
- in->need_push = TRUE;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- do_show_hist (WInput * in)
- {
- char *r;
- r = history_show (&in->history, &in->widget);
- if (r != NULL)
- {
- input_assign_text (in, r);
- g_free (r);
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- push_history (WInput * in, const char *text)
- {
- /* input widget where urls with passwords are entered without any
- vfs prefix */
- const char *password_input_fields[] = {
- " Link to a remote machine ",
- " FTP to machine ",
- " SMB link to machine "
- };
- const size_t ELEMENTS = (sizeof (password_input_fields) / sizeof (password_input_fields[0]));
- char *t;
- size_t i;
- gboolean empty;
- if (text == NULL)
- return;
- #ifdef ENABLE_NLS
- for (i = 0; i < ELEMENTS; i++)
- password_input_fields[i] = _(password_input_fields[i]);
- #endif
- t = g_strstrip (g_strdup (text));
- empty = *t == '\0';
- g_free (t);
- t = g_strdup (empty ? "" : text);
- if (in->history_name != NULL)
- {
- /* FIXME: It is the strange code. Rewrite is needed. */
- const char *p = in->history_name + 3;
- for (i = 0; i < ELEMENTS; i++)
- if (strcmp (p, password_input_fields[i]) == 0)
- break;
- strip_password (t, i >= ELEMENTS);
- }
- in->history = list_append_unique (in->history, t);
- in->need_push = FALSE;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- move_buffer_backward (WInput * in, int start, int end)
- {
- int i, pos, len;
- int str_len;
- str_len = str_length (in->buffer);
- if (start >= str_len || end > str_len + 1)
- return;
- pos = str_offset_to_pos (in->buffer, start);
- len = str_offset_to_pos (in->buffer, end) - pos;
- for (i = pos; in->buffer[i + len - 1]; i++)
- in->buffer[i] = in->buffer[i + len];
- }
- /* --------------------------------------------------------------------------------------------- */
- static cb_ret_t
- insert_char (WInput * in, int c_code)
- {
- size_t i;
- int res;
- if (in->highlight)
- {
- long m1, m2;
- if (input_eval_marks (in, &m1, &m2))
- delete_region (in, m1, m2);
- }
- if (c_code == -1)
- return MSG_NOT_HANDLED;
- if (in->charpoint >= MB_LEN_MAX)
- return MSG_HANDLED;
- in->charbuf[in->charpoint] = c_code;
- in->charpoint++;
- res = str_is_valid_char (in->charbuf, in->charpoint);
- if (res < 0)
- {
- if (res != -2)
- in->charpoint = 0; /* broken multibyte char, skip */
- return MSG_HANDLED;
- }
- in->need_push = TRUE;
- if (strlen (in->buffer) + 1 + in->charpoint >= in->current_max_size)
- {
- /* Expand the buffer */
- size_t new_length = in->current_max_size + in->field_width + in->charpoint;
- char *narea = g_try_renew (char, in->buffer, new_length);
- if (narea)
- {
- in->buffer = narea;
- in->current_max_size = new_length;
- }
- }
- if (strlen (in->buffer) + in->charpoint < in->current_max_size)
- {
- /* bytes from begin */
- size_t ins_point = str_offset_to_pos (in->buffer, in->point);
- /* move chars */
- size_t rest_bytes = strlen (in->buffer + ins_point);
- for (i = rest_bytes + 1; i > 0; i--)
- in->buffer[ins_point + i + in->charpoint - 1] = in->buffer[ins_point + i - 1];
- memcpy (in->buffer + ins_point, in->charbuf, in->charpoint);
- in->point++;
- }
- in->charpoint = 0;
- return MSG_HANDLED;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- beginning_of_line (WInput * in)
- {
- in->point = 0;
- in->charpoint = 0;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- end_of_line (WInput * in)
- {
- in->point = str_length (in->buffer);
- in->charpoint = 0;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- backward_char (WInput * in)
- {
- const char *act;
- act = in->buffer + str_offset_to_pos (in->buffer, in->point);
- if (in->point > 0)
- in->point -= str_cprev_noncomb_char (&act, in->buffer);
- in->charpoint = 0;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- forward_char (WInput * in)
- {
- const char *act;
- act = in->buffer + str_offset_to_pos (in->buffer, in->point);
- if (act[0] != '\0')
- in->point += str_cnext_noncomb_char (&act);
- in->charpoint = 0;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- forward_word (WInput * in)
- {
- const char *p;
- p = in->buffer + str_offset_to_pos (in->buffer, in->point);
- while (p[0] != '\0' && (str_isspace (p) || str_ispunct (p)))
- {
- str_cnext_char (&p);
- in->point++;
- }
- while (p[0] != '\0' && !str_isspace (p) && !str_ispunct (p))
- {
- str_cnext_char (&p);
- in->point++;
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- backward_word (WInput * in)
- {
- const char *p, *p_tmp;
- for (p = in->buffer + str_offset_to_pos (in->buffer, in->point);
- (p != in->buffer) && (p[0] == '\0'); str_cprev_char (&p), in->point--);
- while (p != in->buffer)
- {
- p_tmp = p;
- str_cprev_char (&p);
- if (!str_isspace (p) && !str_ispunct (p))
- {
- p = p_tmp;
- break;
- }
- in->point--;
- }
- while (p != in->buffer)
- {
- str_cprev_char (&p);
- if (str_isspace (p) || str_ispunct (p))
- break;
- in->point--;
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- backward_delete (WInput * in)
- {
- const char *act = in->buffer + str_offset_to_pos (in->buffer, in->point);
- int start;
- if (in->point == 0)
- return;
- start = in->point - str_cprev_noncomb_char (&act, in->buffer);
- move_buffer_backward (in, start, in->point);
- in->charpoint = 0;
- in->need_push = TRUE;
- in->point = start;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- delete_char (WInput * in)
- {
- const char *act;
- int end = in->point;
- act = in->buffer + str_offset_to_pos (in->buffer, in->point);
- end += str_cnext_noncomb_char (&act);
- move_buffer_backward (in, in->point, end);
- in->charpoint = 0;
- in->need_push = TRUE;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- copy_region (WInput * in, int x_first, int x_last)
- {
- int first = min (x_first, x_last);
- int last = max (x_first, x_last);
- if (last == first)
- {
- /* Copy selected files to clipboard */
- mc_event_raise (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file", NULL);
- /* try use external clipboard utility */
- mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
- return;
- }
- g_free (kill_buffer);
- first = str_offset_to_pos (in->buffer, first);
- last = str_offset_to_pos (in->buffer, last);
- kill_buffer = g_strndup (in->buffer + first, last - first);
- mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", kill_buffer);
- /* try use external clipboard utility */
- mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- kill_word (WInput * in)
- {
- int old_point = in->point;
- int new_point;
- forward_word (in);
- new_point = in->point;
- in->point = old_point;
- delete_region (in, old_point, new_point);
- in->need_push = TRUE;
- in->charpoint = 0;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- back_kill_word (WInput * in)
- {
- int old_point = in->point;
- int new_point;
- backward_word (in);
- new_point = in->point;
- in->point = old_point;
- delete_region (in, old_point, new_point);
- in->need_push = TRUE;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- yank (WInput * in)
- {
- if (kill_buffer != NULL)
- {
- char *p;
- in->charpoint = 0;
- for (p = kill_buffer; *p != '\0'; p++)
- insert_char (in, *p);
- in->charpoint = 0;
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- kill_line (WInput * in)
- {
- int chp;
- chp = str_offset_to_pos (in->buffer, in->point);
- g_free (kill_buffer);
- kill_buffer = g_strdup (&in->buffer[chp]);
- in->buffer[chp] = '\0';
- in->charpoint = 0;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- clear_line (WInput * in)
- {
- in->need_push = 1;
- in->buffer[0] = '\0';
- in->point = 0;
- in->mark = 0;
- in->highlight = FALSE;
- in->charpoint = 0;
- }
- static void
- ins_from_clip (WInput * in)
- {
- char *p = NULL;
- ev_clipboard_text_from_file_t event_data;
- /* try use external clipboard utility */
- mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
- event_data.text = &p;
- mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_from_file", &event_data);
- if (event_data.ret)
- {
- char *pp;
- for (pp = p; *pp != '\0'; pp++)
- insert_char (in, *pp);
- g_free (p);
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- hist_prev (WInput * in)
- {
- GList *prev;
- if (in->history == NULL)
- return;
- if (in->need_push)
- push_history (in, in->buffer);
- prev = g_list_previous (in->history);
- if (prev != NULL)
- {
- in->history = prev;
- input_assign_text (in, (char *) prev->data);
- in->need_push = FALSE;
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- hist_next (WInput * in)
- {
- if (in->need_push)
- {
- push_history (in, in->buffer);
- input_assign_text (in, "");
- return;
- }
- if (in->history == NULL)
- return;
- if (in->history->next == NULL)
- input_assign_text (in, "");
- else
- {
- in->history = g_list_next (in->history);
- input_assign_text (in, (char *) in->history->data);
- in->need_push = FALSE;
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- port_region_marked_for_delete (WInput * in)
- {
- in->buffer[0] = '\0';
- in->point = 0;
- in->first = FALSE;
- in->charpoint = 0;
- }
- /* --------------------------------------------------------------------------------------------- */
- static cb_ret_t
- input_execute_cmd (WInput * in, unsigned long command)
- {
- cb_ret_t res = MSG_HANDLED;
- /* a highlight command like shift-arrow */
- if (command == CK_MarkLeft || command == CK_MarkRight ||
- command == CK_MarkToWordBegin || command == CK_MarkToWordEnd ||
- command == CK_MarkToHome || command == CK_MarkToEnd)
- {
- if (!in->highlight)
- {
- input_mark_cmd (in, FALSE); /* clear */
- input_mark_cmd (in, TRUE); /* marking on */
- }
- }
- switch (command)
- {
- case CK_WordRight:
- case CK_WordLeft:
- case CK_Right:
- case CK_Left:
- if (in->highlight)
- input_mark_cmd (in, FALSE);
- }
- switch (command)
- {
- case CK_Home:
- case CK_MarkToHome:
- beginning_of_line (in);
- break;
- case CK_End:
- case CK_MarkToEnd:
- end_of_line (in);
- break;
- case CK_Left:
- case CK_MarkLeft:
- backward_char (in);
- break;
- case CK_WordLeft:
- case CK_MarkToWordBegin:
- backward_word (in);
- break;
- case CK_Right:
- case CK_MarkRight:
- forward_char (in);
- break;
- case CK_WordRight:
- case CK_MarkToWordEnd:
- forward_word (in);
- break;
- case CK_BackSpace:
- if (in->highlight)
- {
- long m1, m2;
- if (input_eval_marks (in, &m1, &m2))
- delete_region (in, m1, m2);
- }
- else
- backward_delete (in);
- break;
- case CK_Delete:
- if (in->first)
- port_region_marked_for_delete (in);
- else if (in->highlight)
- {
- long m1, m2;
- if (input_eval_marks (in, &m1, &m2))
- delete_region (in, m1, m2);
- }
- else
- delete_char (in);
- break;
- case CK_DeleteToWordEnd:
- kill_word (in);
- break;
- case CK_DeleteToWordBegin:
- back_kill_word (in);
- break;
- case CK_Mark:
- input_mark_cmd (in, TRUE);
- break;
- case CK_Remove:
- delete_region (in, in->point, in->mark);
- break;
- case CK_DeleteToEnd:
- kill_line (in);
- break;
- case CK_Clear:
- clear_line (in);
- break;
- case CK_Store:
- copy_region (in, in->mark, in->point);
- break;
- case CK_Cut:
- copy_region (in, in->mark, in->point);
- delete_region (in, in->point, in->mark);
- break;
- case CK_Yank:
- yank (in);
- break;
- case CK_Paste:
- ins_from_clip (in);
- break;
- case CK_HistoryPrev:
- hist_prev (in);
- break;
- case CK_HistoryNext:
- hist_next (in);
- break;
- case CK_History:
- do_show_hist (in);
- break;
- case CK_Complete:
- complete (in);
- break;
- default:
- res = MSG_NOT_HANDLED;
- }
- if (command != CK_MarkLeft && command != CK_MarkRight &&
- command != CK_MarkToWordBegin && command != CK_MarkToWordEnd &&
- command != CK_MarkToHome && command != CK_MarkToEnd)
- in->highlight = FALSE;
- return res;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- input_destroy (WInput * in)
- {
- if (in == NULL)
- {
- fprintf (stderr, "Internal error: null Input *\n");
- exit (EXIT_FAILURE);
- }
- input_clean (in);
- if (in->history != NULL)
- {
- if (!in->is_password && (((Widget *) in)->owner->ret_value != B_CANCEL))
- history_put (in->history_name, in->history);
- in->history = g_list_first (in->history);
- g_list_foreach (in->history, (GFunc) g_free, NULL);
- g_list_free (in->history);
- }
- g_free (in->buffer);
- input_free_completions (in);
- g_free (in->history_name);
- g_free (kill_buffer);
- kill_buffer = NULL;
- }
- /* --------------------------------------------------------------------------------------------- */
- static int
- input_event (Gpm_Event * event, void *data)
- {
- WInput *in = (WInput *) data;
- if ((event->type & GPM_DOWN) != 0)
- {
- in->first = FALSE;
- input_mark_cmd (in, FALSE);
- }
- if ((event->type & (GPM_DOWN | GPM_DRAG)) != 0)
- {
- dlg_select_widget (in);
- if (event->x >= in->field_width - HISTORY_BUTTON_WIDTH + 1
- && should_show_history_button (in))
- do_show_hist (in);
- else
- {
- in->point = str_length (in->buffer);
- if (event->x + in->term_first_shown - 1 < str_term_width1 (in->buffer))
- in->point = str_column_to_pos (in->buffer, event->x + in->term_first_shown - 1);
- }
- input_update (in, TRUE);
- }
- /* A lone up mustn't do anything */
- if (in->highlight && (event->type & (GPM_UP | GPM_DRAG)) != 0)
- return MOU_NORMAL;
- if ((event->type & GPM_DRAG) == 0)
- input_mark_cmd (in, TRUE);
- return MOU_NORMAL;
- }
- /* --------------------------------------------------------------------------------------------- */
- /*** public functions ****************************************************************************/
- /* --------------------------------------------------------------------------------------------- */
- /** Create new instance of WInput object.
- * @param y Y coordinate
- * @param x X coordinate
- * @param input_colors Array of used colors
- * @param width Widget width
- * @param def_text Default text filled in widget
- * @param histname Name of history
- * @param completion_flags Flags for specify type of completions
- * @returns WInput object
- */
- WInput *
- input_new (int y, int x, const int *input_colors, int width, const char *def_text,
- const char *histname, input_complete_t completion_flags)
- {
- WInput *in = g_new (WInput, 1);
- size_t initial_buffer_len;
- init_widget (&in->widget, y, x, 1, width, input_callback, input_event);
- /* history setup */
- in->history_name = NULL;
- in->history = NULL;
- if ((histname != NULL) && (*histname != '\0'))
- {
- in->history_name = g_strdup (histname);
- in->history = history_get (histname);
- }
- if (def_text == NULL)
- def_text = "";
- else if (def_text == INPUT_LAST_TEXT)
- {
- if ((in->history != NULL) && (in->history->data != NULL))
- def_text = (char *) in->history->data;
- else
- def_text = "";
- }
- initial_buffer_len = strlen (def_text);
- initial_buffer_len = 1 + max ((size_t) width, initial_buffer_len);
- in->widget.options |= W_IS_INPUT;
- in->completions = NULL;
- in->completion_flags = completion_flags;
- in->current_max_size = initial_buffer_len;
- in->buffer = g_new (char, initial_buffer_len);
- memmove (in->color, input_colors, sizeof (input_colors_t));
- in->field_width = width;
- in->first = TRUE;
- in->highlight = FALSE;
- in->term_first_shown = 0;
- in->disable_update = 0;
- in->mark = 0;
- in->need_push = TRUE;
- in->is_password = FALSE;
- strcpy (in->buffer, def_text);
- in->point = str_length (in->buffer);
- in->charpoint = 0;
- return in;
- }
- /* --------------------------------------------------------------------------------------------- */
- cb_ret_t
- input_callback (Widget * w, widget_msg_t msg, int parm)
- {
- WInput *in = (WInput *) w;
- cb_ret_t v;
- switch (msg)
- {
- case WIDGET_KEY:
- if (parm == XCTRL ('q'))
- {
- quote = 1;
- v = input_handle_char (in, ascii_alpha_to_cntrl (tty_getch ()));
- quote = 0;
- return v;
- }
- /* Keys we want others to handle */
- if (parm == KEY_UP || parm == KEY_DOWN || parm == ESC_CHAR
- || parm == KEY_F (10) || parm == '\n')
- return MSG_NOT_HANDLED;
- /* When pasting multiline text, insert literal Enter */
- if ((parm & ~KEY_M_MASK) == '\n')
- {
- quote = 1;
- v = input_handle_char (in, '\n');
- quote = 0;
- return v;
- }
- return input_handle_char (in, parm);
- case WIDGET_COMMAND:
- return input_execute_cmd (in, parm);
- case WIDGET_FOCUS:
- case WIDGET_UNFOCUS:
- case WIDGET_DRAW:
- input_update (in, FALSE);
- return MSG_HANDLED;
- case WIDGET_CURSOR:
- widget_move (&in->widget, 0, str_term_width2 (in->buffer, in->point)
- - in->term_first_shown);
- return MSG_HANDLED;
- case WIDGET_DESTROY:
- input_destroy (in);
- return MSG_HANDLED;
- default:
- return default_proc (msg, parm);
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- /** Get default colors for WInput widget.
- * @returns default colors
- */
- const int *
- input_get_default_colors (void)
- {
- static input_colors_t standart_colors;
- standart_colors[WINPUTC_MAIN] = INPUT_COLOR;
- standart_colors[WINPUTC_MARK] = INPUT_MARK_COLOR;
- standart_colors[WINPUTC_UNCHANGED] = INPUT_UNCHANGED_COLOR;
- standart_colors[WINPUTC_HISTORY] = INPUT_HISTORY_COLOR;
- return standart_colors;
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- input_set_origin (WInput * in, int x, int field_width)
- {
- in->widget.x = x;
- in->field_width = in->widget.cols = field_width;
- input_update (in, FALSE);
- }
- /* --------------------------------------------------------------------------------------------- */
- cb_ret_t
- input_handle_char (WInput * in, int key)
- {
- cb_ret_t v;
- unsigned long command;
- v = MSG_NOT_HANDLED;
- if (quote != 0)
- {
- input_free_completions (in);
- v = insert_char (in, key);
- input_update (in, TRUE);
- quote = 0;
- return v;
- }
- command = keybind_lookup_keymap_command (input_map, key);
- if (command == CK_IgnoreKey)
- {
- if (key > 255)
- return MSG_NOT_HANDLED;
- if (in->first)
- port_region_marked_for_delete (in);
- input_free_completions (in);
- v = insert_char (in, key);
- }
- else
- {
- if (command != CK_Complete)
- input_free_completions (in);
- input_execute_cmd (in, command);
- v = MSG_HANDLED;
- if (in->first)
- input_update (in, TRUE); /* needed to clear in->first */
- }
- input_update (in, TRUE);
- return v;
- }
- /* --------------------------------------------------------------------------------------------- */
- /* This function is a test for a special input key used in complete.c */
- /* Returns 0 if it is not a special key, 1 if it is a non-complete key
- and 2 if it is a complete key */
- int
- input_key_is_in_map (WInput * in, int key)
- {
- unsigned long command;
- (void) in;
- command = keybind_lookup_keymap_command (input_map, key);
- if (command == CK_IgnoreKey)
- return 0;
- return (command == CK_Complete) ? 2 : 1;
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- input_assign_text (WInput * in, const char *text)
- {
- input_free_completions (in);
- g_free (in->buffer);
- in->buffer = g_strdup (text); /* was in->buffer->text */
- in->current_max_size = strlen (in->buffer) + 1;
- in->point = str_length (in->buffer);
- in->mark = 0;
- in->need_push = TRUE;
- in->charpoint = 0;
- }
- /* --------------------------------------------------------------------------------------------- */
- /* Inserts text in input line */
- void
- input_insert (WInput * in, const char *text, gboolean insert_extra_space)
- {
- input_disable_update (in);
- while (*text != '\0')
- input_handle_char (in, (unsigned char) *text++); /* unsigned extension char->int */
- if (insert_extra_space)
- input_handle_char (in, ' ');
- input_enable_update (in);
- input_update (in, TRUE);
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- input_set_point (WInput * in, int pos)
- {
- int max_pos;
- max_pos = str_length (in->buffer);
- pos = min (pos, max_pos);
- if (pos != in->point)
- input_free_completions (in);
- in->point = pos;
- in->charpoint = 0;
- input_update (in, TRUE);
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- input_update (WInput * in, gboolean clear_first)
- {
- int has_history = 0;
- int i;
- int buf_len;
- const char *cp;
- int pw;
- if (should_show_history_button (in))
- has_history = HISTORY_BUTTON_WIDTH;
- if (in->disable_update != 0)
- return;
- buf_len = str_length (in->buffer);
- pw = str_term_width2 (in->buffer, in->point);
- /* Make the point visible */
- if ((pw < in->term_first_shown) || (pw >= in->term_first_shown + in->field_width - has_history))
- {
- in->term_first_shown = pw - (in->field_width / 3);
- if (in->term_first_shown < 0)
- in->term_first_shown = 0;
- }
- /* Adjust the mark */
- in->mark = min (in->mark, buf_len);
- if (has_history != 0)
- draw_history_button (in);
- if ((((Widget *) in)->options & W_DISABLED) != 0)
- tty_setcolor (DISABLED_COLOR);
- else if (in->first)
- tty_setcolor (in->color[WINPUTC_UNCHANGED]);
- else
- tty_setcolor (in->color[WINPUTC_MAIN]);
- widget_move (&in->widget, 0, 0);
- if (!in->is_password)
- {
- if (!in->highlight)
- tty_print_string (str_term_substring (in->buffer, in->term_first_shown,
- in->field_width - has_history));
- else
- {
- long m1, m2;
- if (input_eval_marks (in, &m1, &m2))
- {
- tty_setcolor (in->color[WINPUTC_MAIN]);
- cp = str_term_substring (in->buffer, in->term_first_shown,
- in->field_width - has_history);
- tty_print_string (cp);
- tty_setcolor (in->color[WINPUTC_MARK]);
- if (m1 < in->term_first_shown)
- {
- widget_move (&in->widget, 0, 0);
- tty_print_string (str_term_substring
- (in->buffer, in->term_first_shown,
- m2 - in->term_first_shown));
- }
- else
- {
- int sel_width;
- widget_move (&in->widget, 0, m1 - in->term_first_shown);
- sel_width =
- min (m2 - m1,
- (in->field_width - has_history) - (str_term_width2 (in->buffer, m1) -
- in->term_first_shown));
- tty_print_string (str_term_substring (in->buffer, m1, sel_width));
- }
- }
- }
- }
- else
- {
- cp = str_term_substring (in->buffer, in->term_first_shown, in->field_width - has_history);
- for (i = 0; i < in->field_width - has_history; i++)
- {
- if (i >= 0)
- {
- tty_setcolor (in->color[WINPUTC_MAIN]);
- tty_print_char ((cp[0] != '\0') ? '*' : ' ');
- }
- if (cp[0] != '\0')
- str_cnext_char (&cp);
- }
- }
- if (clear_first)
- in->first = FALSE;
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- input_enable_update (WInput * in)
- {
- in->disable_update--;
- input_update (in, FALSE);
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- input_disable_update (WInput * in)
- {
- in->disable_update++;
- }
- /* --------------------------------------------------------------------------------------------- */
- /* Cleans the input line and adds the current text to the history */
- void
- input_clean (WInput * in)
- {
- push_history (in, in->buffer);
- in->need_push = TRUE;
- in->buffer[0] = '\0';
- in->point = 0;
- in->charpoint = 0;
- in->mark = 0;
- in->highlight = FALSE;
- input_free_completions (in);
- input_update (in, FALSE);
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- input_free_completions (WInput * in)
- {
- g_strfreev (in->completions);
- in->completions = NULL;
- }
- /* --------------------------------------------------------------------------------------------- */
|