ui_text_buf.h
Growable UTF-8 text that the fields and the edit core can grow in place.
#include "isolenui/ui/ui_text_buf.h". The guide starts at Text buffers.
The type
Explained in Text buffers.
ui::text_bufui::k_text_buf_maxCalls
Explained in Text buffers.
ui::text_buf_reserveui::text_buf_setui::text_buf_appendui::text_buf_formatui::text_buf_clearui::text_buf_freeui::text_buf_swapui::text_buf_dropExample
note.cpp
#include "isolenui/ui/ui.h"
#include "isolenui/kit/kit.h"
#include "isolenui/ui/ui_text_buf.h"
#include <stdint.h>
// a text_buf owns its bytes on the heap and grows as it is written
struct note {
ui::text_buf title{ "untitled" };
ui::text_buf body;
uint32_t saves = 0;
};
static void build(note* n)
{
ui::text_buf_append(&n->body, "first line\n");
ui::text_buf_format(&n->title, "draft %u", n->saves + 1u);
// c_str never returns null, an empty text reads as ""
const char* shown = n->title.c_str();
(void)shown;
}
static void editor(ui::ctx* u, note* n)
{
// the growable overloads take the text itself, no capacity
if (ui::text_box(u, "title", &n->title)) {
++n->saves;
}
ui::search_box(u, "find in note", &n->body);
ui::textarea(u, "body", &n->body, ui::textarea_of().lines(6u, 20u));
}
// state kept in the store starts with the text, zeroed memory is an empty text
struct draft {
ui::text_buf text;
bool sent;
};
static void compose(ui::ctx* u)
{
const uint64_t id = ui::next_id(u);
bool fresh = false;
draft* d = static_cast<draft*>(
ui::store(u, id, sizeof(draft), ui::text_buf_drop, &fresh));
ui::field_ex(u, "message", &d->text, ui::field_live, nullptr);
}