summaryrefslogtreecommitdiffstats
path: root/include/widgets/textbox.h
blob: a1c77445f777dc35b0b096bb94ea1741782f714a (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#ifndef ROFI_TEXTBOX_H
#define ROFI_TEXTBOX_H

#include <xkbcommon/xkbcommon.h>
#include <pango/pango.h>
#include <pango/pango-fontmap.h>
#include <pango/pangocairo.h>
#include <cairo.h>
#include "widgets/widget.h"
#include "widgets/widget-internal.h"
#include "x11-helper.h"
#include "keyb.h"

/**
 * @defgroup Textbox Textbox
 * @ingroup widget
 *
 * @{
 */
/**
 * Internal structure of a textbox widget.
 * TODO make this internal to textbox
 */
typedef struct
{
    widget           widget;
    unsigned long    flags;
    short            cursor;
    char             *text;
    PangoLayout      *layout;
    int              tbft;
    int              markup;
    int              changed;

    int              blink;
    guint            blink_timeout;

    PangoFontMetrics *metrics;
    //
    const char       *theme_name;
} textbox;

/**
 * Flags for configuring textbox behaviour and looks during creation.
 */
typedef enum
{
    TB_AUTOHEIGHT = 1 << 0,
    TB_AUTOWIDTH  = 1 << 1,
    TB_LEFT       = 1 << 16,
    TB_RIGHT      = 1 << 17,
    TB_CENTER     = 1 << 18,
    TB_EDITABLE   = 1 << 19,
    TB_MARKUP     = 1 << 20,
    TB_WRAP       = 1 << 21,
    TB_PASSWORD   = 1 << 22,
    TB_INDICATOR  = 1 << 23,
} TextboxFlags;
/**
 * Flags indicating current state of the textbox.
 */
typedef enum
{
    /** Normal */
    NORMAL     = 0,
    /** Text in box is urgent. */
    URGENT     = 1,
    /** Text in box is active. */
    ACTIVE     = 2,
    /** Text in box is selected. */
    SELECTED   = 4,
    /** Text in box has pango markup. */
    MARKUP     = 8,

    /** Text is on an alternate row */
    ALT        = 16,
    /** Render font highlighted (inverted colors.) */
    HIGHLIGHT  = 32,
    /** Mask for alternate and highlighted */
    FMOD_MASK  = ( ALT | HIGHLIGHT ),
    /** Mask of bits indicating state */
    STATE_MASK = ~( SELECTED | MARKUP | ALT | HIGHLIGHT )
} TextBoxFontType;

/**
 * @param name The name of the to be created widget.
 * @param flags #TextboxFlags indicating the type of textbox.
 * @param tbft #TextBoxFontType current state of textbox.
 * @param text intial text to display.
 *
 * Create a new textbox widget.
 *
 * free with #widget_free
 * @returns a new #textbox
 */
textbox* textbox_create ( const char *name, TextboxFlags flags,
                          TextBoxFontType tbft,
                          const char *text );
/**
 * @param tb  Handle to the textbox
 * @param tbft The style of font to render.
 *
 * Set the font render style.
 */
void textbox_font ( textbox *tb, TextBoxFontType tbft );

/**
 * @param tb  Handle to the textbox
 * @param text The text to show in the textbox
 *
 * Set the text to show. Cursor is moved to end (if visible)
 */
void textbox_text ( textbox *tb, const char *text );

/**
 * @param tb Handle to the textbox
 * @param action the #KeyBindingAction to execute on textbox
 *
 * Execute an action on the textbox.
 *
 * @return TRUE if action was taken.
 */
int textbox_keybinding ( textbox *tb, KeyBindingAction action );
/**
 * @param tb Handle to the textbox
 * @param pad The text to insert
 * @param pad_len the length of the text
 *
 * The text should be one insert from a keypress..  the first gunichar is validated to be (or not) control
 * return TRUE if inserted
 */
gboolean textbox_append_char ( textbox *tb, const char *pad, const int pad_len );

/**
 * @param tb  Handle to the textbox
 * @param pos New cursor position
 *
 * Set the cursor position (string index)
 */
void textbox_cursor ( textbox *tb, int pos );

/**
 * @param tb   Handle to the textbox
 * @param pos  The position to insert the string at
 * @param str  The string to insert.
 * @param slen The length of the string.
 *
 * Insert the string str at position pos.
 */
void textbox_insert ( textbox *tb, const int pos, const char *str, const int slen );

/**
 * Setup the cached fonts. This is required to do
 * before any of the textbox_ functions is called.
 * Clean with textbox_cleanup()
 */
void textbox_setup ( void );

/**
 * Cleanup the allocated colors and fonts by textbox_setup().
 */
void textbox_cleanup ( void );

/**
 * @param tb Handle to the textbox
 *
 * Get the height of the textbox
 *
 * @returns the height of the textbox in pixels.
 */
int textbox_get_height ( const textbox *tb );

/**
 * @param tb Handle to the textbox
 *
 * Get the height of the rendered string.
 *
 * @returns the height of the string in pixels.
 */
int textbox_get_font_height ( const textbox *tb );

/**
 * @param tb Handle to the textbox
 *
 * Get the width of the rendered string.
 *
 * @returns the width of the string in pixels.
 */
int textbox_get_font_width ( const textbox *tb );

/**
 * Estimate the width of a character.
 *
 * @returns the width of a character in pixels.
 */
double textbox_get_estimated_char_width ( void );

/**
 * Estimate the height of a character.
 *
 * @returns the height of a character in pixels.
 */
double textbox_get_estimated_char_height ( void  );

/**
 * @param tb Handle to the textbox
 * @param pos The start position
 * @param dlen The length
 *
 * Remove dlen bytes from position pos.
 */
void textbox_delete ( textbox *tb, int pos, int dlen );

/**
 * @param tb Handle to the textbox
 * @param x The new horizontal position to place with textbox
 * @param y The new vertical position to place with textbox
 * @param w The new width of the textbox
 * @param h The new height of the textbox
 *
 * Move and resize the textbox.
 * TODO remove for #widget_resize and #widget_move
 */
void textbox_moveresize ( textbox *tb, int x, int y, int w, int h );

/**
 * @param tb Handle to the textbox
 * @param eh The number of rows to display
 *
 * Get the (estimated) with of a character, can be used to calculate window width.
 * This includes padding.
 *
 * @returns the estimated width of a character.
 */
int textbox_get_estimated_height ( const textbox *tb, int eh );
/**
 * @param font The name of the font used.
 * @param p The new default PangoContext
 *
 * Set the default pango context (with font description) for all textboxes.
 */
void textbox_set_pango_context ( const char *font, PangoContext *p );
/**
 * @param tb Handle to the textbox
 * @param list New pango attributes
 *
 * Sets list as active pango attributes.
 */
void textbox_set_pango_attributes ( textbox *tb, PangoAttrList *list );

/**
 * @param tb Handle to the textbox
 *
 * Get the list of currently active pango attributes.
 *
 * @returns the pango attributes
 */
PangoAttrList *textbox_get_pango_attributes ( textbox *tb );

/**
 * @param tb Handle to the textbox
 *
 * @returns the visible text.
 */
const char *textbox_get_visible_text ( const textbox *tb );
/*@}*/
#endif //ROFI_TEXTBOX_H