summaryrefslogtreecommitdiffstats
path: root/lib/widget/input.h
blob: bd7aa26fedc861d6d7bee2f4914e6062500cc9d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/** \file input.h
 *  \brief Header: WInput widget
 */

#ifndef MC__WIDGET_INPUT_H
#define MC__WIDGET_INPUT_H

#include <limits.h>             /* MB_LEN_MAX */

/*** typedefs(not structures) and defined constants **********************************************/

#define INPUT(x) ((WInput *)(x))

/* For history load-save functions */
#define INPUT_LAST_TEXT ((char *) 2)

/*** enums ***************************************************************************************/

typedef enum
{
    WINPUTC_MAIN,               /* color used */
    WINPUTC_MARK,               /* color for marked text */
    WINPUTC_UNCHANGED,          /* color for inactive text (Is first keystroke) */
    WINPUTC_HISTORY,            /* color for history list */
    WINPUTC_COUNT_COLORS        /* count of used colors */
} input_colors_enum_t;

/* completion flags */
typedef enum
{
    INPUT_COMPLETE_NONE = 0,
    INPUT_COMPLETE_FILENAMES = 1 << 0,
    INPUT_COMPLETE_HOSTNAMES = 1 << 1,
    INPUT_COMPLETE_COMMANDS = 1 << 2,
    INPUT_COMPLETE_VARIABLES = 1 << 3,
    INPUT_COMPLETE_USERNAMES = 1 << 4,
    INPUT_COMPLETE_CD = 1 << 5,
    INPUT_COMPLETE_SHELL_ESC = 1 << 6,
} input_complete_t;

/*** structures declarations (and typedefs of structures)*****************************************/

typedef int input_colors_t[WINPUTC_COUNT_COLORS];

typedef struct
{
    Widget widget;

    GString *buffer;
    const int *color;
    int point;                  /* cursor position in the input line in characters */
    int mark;                   /* the mark position in characters; negative value means no marked text */
    int term_first_shown;       /* column of the first shown character */
    gboolean first;             /* is first keystroke? */
    int disable_update;         /* do we want to skip updates? */
    gboolean is_password;       /* is this a password input line? */
    gboolean init_from_history; /* init text will be get from history */
    gboolean need_push;         /* need to push the current Input on hist? */
    gboolean strip_password;    /* need to strip password before placing string to history */
    char **completions;         /* possible completions array */
    input_complete_t completion_flags;
    char charbuf[MB_LEN_MAX];   /* buffer for multibytes characters */
    size_t charpoint;           /* point to end of mulibyte sequence in charbuf */
    WLabel *label;              /* label associated with this input line */
    struct input_history_t
    {
        char *name;             /* name of history for loading and saving */
        GList *list;            /* the history */
        GList *current;         /* current history item */
        gboolean changed;       /* the history has changed */
    } history;
} WInput;

/*** global variables defined in .c file *********************************************************/

extern int quote;

extern const global_keymap_t *input_map;

/* Color styles for normal and command line input widgets */
extern input_colors_t input_colors;

/*** declarations of public functions ************************************************************/

WInput *input_new (int y, int x, const int *colors,
                   int len, const char *text, const char *histname,
                   input_complete_t completion_flags);
/* callback is public; needed for command line */
cb_ret_t input_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data);
void input_set_default_colors (void);
cb_ret_t input_handle_char (WInput * in, int key);
void input_assign_text (WInput * in, const char *text);
void input_insert (WInput * in, const char *text, gboolean insert_extra_space);
void input_set_point (WInput * in, int pos);
void input_update (WInput * in, gboolean clear_first);
void input_enable_update (WInput * in);
void input_disable_update (WInput * in);
void input_clean (WInput * in);

/* input_complete.c */
void input_complete (WInput * in);
void input_complete_free (WInput * in);

/* --------------------------------------------------------------------------------------------- */
/*** inline functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */

/**
 * Get text of input line.
 *
 * @param in input line
 *
 * @return newly allocated string that contains a copy of @in's text.
 */
static inline char *
input_get_text (const WInput * in)
{
    return g_strndup (in->buffer->str, in->buffer->len);
}

/* --------------------------------------------------------------------------------------------- */

/**
 * Get pointer to input line buffer.
 *
 * @param in input line
 *
 * @return pointer to @in->buffer->str.
 */
static inline const char *
input_get_ctext (const WInput * in)
{
    return in->buffer->str;
}

/* --------------------------------------------------------------------------------------------- */

/**
 * Is input line empty or not.
 *
 * @param in input line
 *
 * @return TRUE if buffer of @in is empty, FALSE otherwise.
 */
static inline gboolean
input_is_empty (const WInput * in)
{
    return (in->buffer->len == 0);
}

/* --------------------------------------------------------------------------------------------- */


#endif /* MC__WIDGET_INPUT_H */