1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-22 19:45:54 -04:00

Added _inp_win_refresh() macro, renamed and moved some functions

This commit is contained in:
James Booth 2013-01-06 01:06:12 +00:00
parent 82ce22ac9d
commit dae4f1bb70
3 changed files with 44 additions and 57 deletions

View File

@ -61,14 +61,17 @@
#include "theme.h" #include "theme.h"
#include "ui.h" #include "ui.h"
#define _inp_win_refresh() prefresh(inp_win, 0, pad_start, rows-1, 0, rows-1, cols-1)
static WINDOW *inp_win; static WINDOW *inp_win;
static int pad_start = 0; static int pad_start = 0;
static int rows, cols;
static int _handle_edit(const wint_t ch, char *input, int *size); static int _handle_edit(const wint_t ch, char *input, int *size);
static int _printable(const wint_t ch); static int _printable(const wint_t ch);
static gboolean _special_key(const wint_t ch); static gboolean _special_key(const wint_t ch);
static void _inp_clear_no_pad(void); static void _clear_input(void);
static void _go_to_end(int display_size, int rows, int cols); static void _go_to_end(int display_size);
void void
create_input_window(void) create_input_window(void)
@ -78,21 +81,18 @@ create_input_window(void)
#else #else
ESCDELAY = 25; ESCDELAY = 25;
#endif #endif
int rows, cols;
getmaxyx(stdscr, rows, cols); getmaxyx(stdscr, rows, cols);
inp_win = newpad(1, INP_WIN_MAX); inp_win = newpad(1, INP_WIN_MAX);
wbkgd(inp_win, COLOUR_INPUT_TEXT); wbkgd(inp_win, COLOUR_INPUT_TEXT);
keypad(inp_win, TRUE); keypad(inp_win, TRUE);
wmove(inp_win, 0, 0); wmove(inp_win, 0, 0);
prefresh(inp_win, 0, pad_start, rows-1, 0, rows-1, cols-1); _inp_win_refresh();
} }
void void
inp_win_resize(const char * const input, const int size) inp_win_resize(const char * const input, const int size)
{ {
int rows, cols, inp_x; int inp_x;
getmaxyx(stdscr, rows, cols); getmaxyx(stdscr, rows, cols);
inp_x = getcurx(inp_win); inp_x = getcurx(inp_win);
@ -104,17 +104,7 @@ inp_win_resize(const char * const input, const int size)
} }
} }
prefresh(inp_win, pad_start, 0, rows-1, 0, rows-1, cols-1); _inp_win_refresh();
}
void
inp_clear(void)
{
int rows, cols;
getmaxyx(stdscr, rows, cols);
_inp_clear_no_pad();
pad_start = 0;
prefresh(inp_win, 0, pad_start, rows-1, 0, rows-1, cols-1);
} }
void void
@ -164,8 +154,6 @@ inp_get_char(char *input, int *size)
// if it wasn't an arrow key etc // if it wasn't an arrow key etc
if (!_handle_edit(ch, input, size)) { if (!_handle_edit(ch, input, size)) {
if (_printable(ch)) { if (_printable(ch)) {
int rows, cols;
getmaxyx(stdscr, rows, cols);
inp_x = getcurx(inp_win); inp_x = getcurx(inp_win);
// handle insert if not at end of input // handle insert if not at end of input
@ -189,7 +177,7 @@ inp_get_char(char *input, int *size)
if (inp_x - pad_start > cols-3) { if (inp_x - pad_start > cols-3) {
pad_start++; pad_start++;
prefresh(inp_win, 0, pad_start, rows-1, 0, rows-1, cols-1); _inp_win_refresh();
} }
// otherwise just append // otherwise just append
@ -213,7 +201,7 @@ inp_get_char(char *input, int *size)
getmaxyx(stdscr, rows, cols); getmaxyx(stdscr, rows, cols);
if (display_size - pad_start > cols-2) { if (display_size - pad_start > cols-2) {
pad_start++; pad_start++;
prefresh(inp_win, 0, pad_start, rows-1, 0, rows-1, cols-1); _inp_win_refresh();
} }
} }
} }
@ -230,11 +218,8 @@ inp_get_char(char *input, int *size)
void void
inp_get_password(char *passwd) inp_get_password(char *passwd)
{ {
int rows, cols;
getmaxyx(stdscr, rows, cols);
wclear(inp_win); wclear(inp_win);
prefresh(inp_win, 0, pad_start, rows-1, 0, rows-1, cols-1); _inp_win_refresh();
noecho(); noecho();
mvwgetnstr(inp_win, 0, 1, passwd, 20); mvwgetnstr(inp_win, 0, 1, passwd, 20);
wmove(inp_win, 0, 0); wmove(inp_win, 0, 0);
@ -245,21 +230,36 @@ inp_get_password(char *passwd)
void void
inp_put_back(void) inp_put_back(void)
{ {
int rows, cols; _inp_win_refresh();
getmaxyx(stdscr, rows, cols);
prefresh(inp_win, 0, pad_start, rows-1, 0, rows-1, cols-1);
} }
void void
inp_replace_input(char *input, const char * const new_input, int *size) inp_replace_input(char *input, const char * const new_input, int *size)
{ {
int display_size;
strcpy(input, new_input); strcpy(input, new_input);
*size = strlen(input); *size = strlen(input);
inp_clear(); display_size = g_utf8_strlen(input, *size);
inp_win_reset();
input[*size] = '\0'; input[*size] = '\0';
wprintw(inp_win, input); wprintw(inp_win, input);
_go_to_end(display_size);
} }
void
inp_win_reset(void)
{
_clear_input();
pad_start = 0;
_inp_win_refresh();
}
static void
_clear_input(void)
{
wclear(inp_win);
wmove(inp_win, 0, 0);
}
/* /*
* Deal with command editing, return 1 if ch was an edit * Deal with command editing, return 1 if ch was an edit
@ -269,7 +269,6 @@ inp_replace_input(char *input, const char * const new_input, int *size)
static int static int
_handle_edit(const wint_t ch, char *input, int *size) _handle_edit(const wint_t ch, char *input, int *size)
{ {
int rows, cols;
char *prev = NULL; char *prev = NULL;
char *next = NULL; char *next = NULL;
int inp_x = 0; int inp_x = 0;
@ -280,7 +279,6 @@ _handle_edit(const wint_t ch, char *input, int *size)
display_size = g_utf8_strlen(input, *size); display_size = g_utf8_strlen(input, *size);
} }
getmaxyx(stdscr, rows, cols);
inp_x = getcurx(inp_win); inp_x = getcurx(inp_win);
switch(ch) { switch(ch) {
@ -327,7 +325,7 @@ _handle_edit(const wint_t ch, char *input, int *size)
return 1; return 1;
} else { } else {
*size = 0; *size = 0;
inp_clear(); inp_win_reset();
return 1; return 1;
} }
@ -346,7 +344,7 @@ _handle_edit(const wint_t ch, char *input, int *size)
g_free(start); g_free(start);
_inp_clear_no_pad(); _clear_input();
wprintw(inp_win, input); wprintw(inp_win, input);
wmove(inp_win, 0, inp_x -1); wmove(inp_win, 0, inp_x -1);
@ -366,7 +364,7 @@ _handle_edit(const wint_t ch, char *input, int *size)
g_free(end); g_free(end);
g_string_free(new, FALSE); g_string_free(new, FALSE);
_inp_clear_no_pad(); _clear_input();
wprintw(inp_win, input); wprintw(inp_win, input);
wmove(inp_win, 0, inp_x -1); wmove(inp_win, 0, inp_x -1);
} }
@ -378,7 +376,7 @@ _handle_edit(const wint_t ch, char *input, int *size)
pad_start = 0; pad_start = 0;
} }
prefresh(inp_win, 0, pad_start, rows-1, 0, rows-1, cols-1); _inp_win_refresh();
} }
} }
return 1; return 1;
@ -393,7 +391,7 @@ _handle_edit(const wint_t ch, char *input, int *size)
g_free(start); g_free(start);
_inp_clear_no_pad(); _clear_input();
wprintw(inp_win, input); wprintw(inp_win, input);
} else if (inp_x < display_size-1) { } else if (inp_x < display_size-1) {
gchar *start = g_utf8_substring(input, 0, inp_x); gchar *start = g_utf8_substring(input, 0, inp_x);
@ -410,7 +408,7 @@ _handle_edit(const wint_t ch, char *input, int *size)
g_free(end); g_free(end);
g_string_free(new, FALSE); g_string_free(new, FALSE);
_inp_clear_no_pad(); _clear_input();
wprintw(inp_win, input); wprintw(inp_win, input);
wmove(inp_win, 0, inp_x); wmove(inp_win, 0, inp_x);
} }
@ -423,7 +421,7 @@ _handle_edit(const wint_t ch, char *input, int *size)
// current position off screen to left // current position off screen to left
if (inp_x - 1 < pad_start) { if (inp_x - 1 < pad_start) {
pad_start--; pad_start--;
prefresh(inp_win, 0, pad_start, rows-1, 0, rows-1, cols-1); _inp_win_refresh();
} }
return 1; return 1;
@ -434,7 +432,7 @@ _handle_edit(const wint_t ch, char *input, int *size)
// current position off screen to right // current position off screen to right
if ((inp_x + 1 - pad_start) >= cols) { if ((inp_x + 1 - pad_start) >= cols) {
pad_start++; pad_start++;
prefresh(inp_win, 0, pad_start, rows-1, 0, rows-1, cols-1); _inp_win_refresh();
} }
} }
return 1; return 1;
@ -443,8 +441,6 @@ _handle_edit(const wint_t ch, char *input, int *size)
prev = history_previous(input, size); prev = history_previous(input, size);
if (prev) { if (prev) {
inp_replace_input(input, prev, size); inp_replace_input(input, prev, size);
display_size = g_utf8_strlen(input, *size);
_go_to_end(display_size, rows, cols);
} }
return 1; return 1;
@ -452,19 +448,17 @@ _handle_edit(const wint_t ch, char *input, int *size)
next = history_next(input, size); next = history_next(input, size);
if (next) { if (next) {
inp_replace_input(input, next, size); inp_replace_input(input, next, size);
display_size = g_utf8_strlen(input, *size);
_go_to_end(display_size, rows, cols);
} }
return 1; return 1;
case KEY_HOME: case KEY_HOME:
wmove(inp_win, 0, 0); wmove(inp_win, 0, 0);
pad_start = 0; pad_start = 0;
prefresh(inp_win, 0, pad_start, rows-1, 0, rows-1, cols-1); _inp_win_refresh();
return 1; return 1;
case KEY_END: case KEY_END:
_go_to_end(display_size, rows, cols); _go_to_end(display_size);
return 1; return 1;
case 9: // tab case 9: // tab
@ -477,12 +471,12 @@ _handle_edit(const wint_t ch, char *input, int *size)
} }
static void static void
_go_to_end(int display_size, int rows, int cols) _go_to_end(int display_size)
{ {
wmove(inp_win, 0, display_size); wmove(inp_win, 0, display_size);
if (display_size > cols-2) { if (display_size > cols-2) {
pad_start = display_size - cols + 1; pad_start = display_size - cols + 1;
prefresh(inp_win, 0, pad_start, rows-1, 0, rows-1, cols-1); _inp_win_refresh();
} }
} }
@ -505,10 +499,3 @@ _special_key(const wint_t ch)
char *str = unctrl(ch); char *str = unctrl(ch);
return ((strlen(str) > 1) && g_str_has_prefix(str, "^")); return ((strlen(str) > 1) && g_str_has_prefix(str, "^"));
} }
static void
_inp_clear_no_pad(void)
{
wclear(inp_win);
wmove(inp_win, 0, 0);
}

View File

@ -458,7 +458,7 @@ _process_input(char *inp)
result = cmd_execute_default(inp); result = cmd_execute_default(inp);
} }
inp_clear(); inp_win_reset();
contact_list_reset_search_attempts(); contact_list_reset_search_attempts();
win_current_page_off(); win_current_page_off();

View File

@ -172,7 +172,7 @@ void status_bar_update_time(void);
// input window actions // input window actions
wint_t inp_get_char(char *input, int *size); wint_t inp_get_char(char *input, int *size);
void inp_clear(void); void inp_win_reset(void);
void inp_win_resize(const char * input, const int size); void inp_win_resize(const char * input, const int size);
void inp_put_back(void); void inp_put_back(void);
void inp_non_block(void); void inp_non_block(void);