commit d992e1c9a4486549c34466de7e54c5a87d5eb6a6
parent 105a46b568587d48c05ba2a848d8107d5eeed1b3
Author: lumidify <nobody@lumidify.org>
Date: Wed, 13 May 2020 19:04:58 +0200
Add LtkTextEdit (non-working)
Diffstat:
3 files changed, 97 insertions(+), 0 deletions(-)
diff --git a/text_buffer.c b/text_buffer.c
@@ -560,6 +560,7 @@ ltk_text_line_recalculate(LtkTextManager *tm, struct ltk_text_line *tl) {
ltk_text_line_shape(tm, tl);
}
+
void
ltk_text_line_insert_text(struct ltk_text_line *tl, uint32_t *text, size_t len) {
/* check if any characters have a different script, only recalc then */
@@ -597,6 +598,22 @@ ltk_text_line_insert_text(struct ltk_text_line *tl, uint32_t *text, size_t len)
ltk_text_line_recalculate(tl);
}
+/* must be NULL-terminated */
+void
+ltk_text_line_insert_utf8(struct ltk_text_line *tl, char *text) {
+ size_t len = u8_strlen(text);
+ uint32_t *new = malloc(sizeof(uint32_t) * len);
+ if (!new) {
+ (void)fprintf("Error allocating memory for string\n");
+ exit(1);
+ ]
+ size_t inc = 0;
+ for (int i = 0; i < len; i++)
+ new[i] = u8_nextmemchar(text, &inc);
+ ltk_text_line_insert_text(tl, new, len);
+ free(new);
+}
+
struct ltk_text_line *
ltk_text_line_create(void) {
struct ltk_text_line *line = malloc(sizeof(struct ltk_text_line));
diff --git a/text_edit.c b/text_edit.c
@@ -0,0 +1,48 @@
+/*
+ * This file is part of the Lumidify ToolKit (LTK)
+ * Copyright (c) 2020 lumidify <nobody@lumidify.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+extern Ltk *ltk_global;
+
+void
+ltk_draw_text_edit(LtkTextEdit *te) {
+ XColor fg = ltk_global->theme->window->fg;
+ XColor bg = ltk_global->theme->window->bg;
+ LtkRect rect = te->widget.rect;
+ LtkWindow *window = te->widget.window;
+ if (!te->tl->img)
+ ltk_render_text_line(te->tl, rect.w, ltk_global->display, window->xwindow, window->gc, ltk_global->colormap, fg, bg);
+ XPutImage(ltk_global->display, window->xwindow, window->gc, te->tl->img, 0, 0, rect.x, rect.y, te->tl->w, te->tl->h);
+}
+
+LtkTextEdit *ltk_create_text_edit(LtkWindow *window, const char *text) {
+ LtkTextEdit *te = malloc(sizeof(LtkTextEdit));
+ if (!te)
+ ltk_fatal("ERROR: Unable to allocate memory for LtkTextEdit.\n");
+ ltk_fill_widget_defaults(&te->widget, window, <k_draw_text_edit, <k_destroy_text_edit, 1);
+ te->tl = ltk_text_line_create();
+ ltk_text_line_insert_utf8(te->tl, text);
+}
+void ltk_destroy_text_edit(LtkTextEdit *te) {
+ ltk_text_line_destroy(te->tl);
+ free(te);
+}
diff --git a/text_edit.h b/text_edit.h
@@ -0,0 +1,32 @@
+/*
+ * This file is part of the Lumidify ToolKit (LTK)
+ * Copyright (c) 2020 lumidify <nobody@lumidify.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+typedef struct {
+ LtkWidget widget;
+ struct ltk_text_line *tl;
+} LtkTextEdit;
+
+/* FIXME: standardize ltk_<widget>_destroy, etc. instead of ltk_destroy_<widget> */
+void ltk_draw_text_edit(LtkTextEdit *te);
+LtkTextEdit *ltk_create_text_edit(LtkWindow *window, const char *text);
+void ltk_destroy_text_edit(LtkTextEdit *te);