Add textarea_setup struct to textarea_create API.

This commit is contained in:
Michael Drake 2013-01-12 20:46:01 +00:00
parent ac03806a50
commit 5124b8a02a
3 changed files with 72 additions and 14 deletions

View File

@ -75,6 +75,17 @@ struct textarea {
int vis_width; /**< Visible width, in pixels */
int vis_height; /**< Visible height, in pixels */
int pad_top;
int pad_right;
int pad_bottom;
int pad_left;
int border_width;
colour border_col;
plot_font_style_t fstyle; /**< Text style */
plot_font_style_t sel_fstyle; /**< Text style */
char *text; /**< UTF-8 text */
unsigned int text_alloc; /**< Size of allocated text */
unsigned int text_len; /**< Length of text, in bytes */
@ -92,8 +103,6 @@ struct textarea {
int sel_start; /**< Character index of sel start(inclusive) */
int sel_end; /**< Character index of sel end(exclusive) */
plot_font_style_t fstyle; /**< Text style */
int line_count; /**< Count of lines */
#define LINE_CHUNK_SIZE 16
struct line_info *lines; /**< Line info array */
@ -608,8 +617,7 @@ static bool textarea_replace_text(struct textarea *ta, unsigned int start,
/* exported interface, documented in textarea.h */
struct textarea *textarea_create(int width, int height,
textarea_flags flags, const plot_font_style_t *style,
struct textarea *textarea_create(const textarea_setup *setup,
textarea_redraw_request_callback redraw_request, void *data)
{
struct textarea *ret;
@ -627,14 +635,30 @@ struct textarea *textarea_create(int width, int height,
ret->redraw_request = redraw_request;
ret->data = data;
ret->vis_width = width;
ret->vis_height = height;
ret->flags = setup->flags;
ret->vis_width = setup->width;
ret->vis_height = setup->height;
ret->pad_top = setup->pad_top;
ret->pad_right = setup->pad_right;
ret->pad_bottom = setup->pad_bottom;
ret->pad_left = setup->pad_left;
ret->border_width = setup->border_width;
ret->border_col = setup->border_col;
ret->fstyle = setup->text;
ret->sel_fstyle = setup->text;
ret->sel_fstyle.foreground = setup->selected_text;
ret->sel_fstyle.background = setup->selected_bg;
ret->scroll_x = 0;
ret->scroll_y = 0;
ret->drag_start_char = 0;
ret->flags = flags;
ret->text = malloc(64);
if (ret->text == NULL) {
LOG(("malloc failed"));
@ -646,11 +670,10 @@ struct textarea *textarea_create(int width, int height,
ret->text_len = 1;
ret->text_utf8_len = 0;
ret->fstyle = *style;
ret->line_height = FIXTOINT(FDIV((FMUL(FLTTOFIX(1.2),
FMUL(nscss_screen_dpi,
INTTOFIX((style->size / FONT_SIZE_SCALE))))), F_72));
INTTOFIX((setup->text.size /
FONT_SIZE_SCALE))))), F_72));
ret->caret_pos.line = ret->caret_pos.char_off = 0;
ret->caret_x = MARGIN_LEFT;

View File

@ -36,6 +36,26 @@ typedef enum textarea_flags {
TEXTAREA_READONLY = (1 << 2)
} textarea_flags;
typedef struct textarea_setup {
textarea_flags flags;
int width;
int height;
int pad_top;
int pad_right;
int pad_bottom;
int pad_left;
int border_width;
colour border_col;
colour selected_text;
colour selected_bg;
plot_font_style_t text;
} textarea_setup;
struct textarea;
@ -54,8 +74,7 @@ typedef void(*textarea_redraw_request_callback)(void *data, int x, int y,
* \param data user specified data which will be passed to redraw callbacks
* \return Opaque handle for textarea or 0 on error
*/
struct textarea *textarea_create(int width, int height,
textarea_flags flags, const plot_font_style_t *style,
struct textarea *textarea_create(const textarea_setup *setup,
textarea_redraw_request_callback redraw_request, void *data);
/**

View File

@ -2935,6 +2935,7 @@ void tree_start_edit(struct tree *tree, struct node_element *element)
{
struct node *parent;
int width, height;
textarea_setup ta_setup;
assert(tree != NULL);
assert(element != NULL);
@ -2959,8 +2960,23 @@ void tree_start_edit(struct tree *tree, struct node_element *element)
if (element->type == NODE_ELEMENT_TEXT_PLUS_ICON)
width -= NODE_INSTEP;
tree->textarea = textarea_create(width, height, TEXTAREA_DEFAULT,
&plot_fstyle, tree_textarea_redraw_request, tree);
ta_setup.flags = TEXTAREA_DEFAULT;
ta_setup.width = width;
ta_setup.height = height;
ta_setup.pad_top = 0;
ta_setup.pad_right = 4;
ta_setup.pad_bottom = 0;
ta_setup.pad_left = 4;
ta_setup.border_width = 1;
ta_setup.border_col = 0x000000;
ta_setup.selected_text = 0xffffff;
ta_setup.selected_bg = 0x000000;
ta_setup.text = plot_fstyle;
ta_setup.text.foreground = 0x000000;
ta_setup.text.background = 0xffffff;
tree->textarea = textarea_create(&ta_setup,
tree_textarea_redraw_request, tree);
if (tree->textarea == NULL) {
tree_stop_edit(tree, false);
return;