aboutsummaryrefslogtreecommitdiff
path: root/chatty2.c
diff options
context:
space:
mode:
authorRaymaekers Luca <raymaekers.luca@gmail.com>2024-12-07 19:17:33 +0100
committerRaymaekers Luca <luca@spacehb.net>2025-01-07 18:51:21 +0100
commit0574f5a7c5159a2ae1d7d2182cec982509947db9 (patch)
tree2a9678fd39bcf8ead03c7ee979caf668f2f17892 /chatty2.c
parent4aec2e1331650cabb966bbf24ed83f76cbbe9912 (diff)
checkpoint
Diffstat (limited to 'chatty2.c')
-rw-r--r--chatty2.c90
1 files changed, 90 insertions, 0 deletions
diff --git a/chatty2.c b/chatty2.c
new file mode 100644
index 0000000..5a00fee
--- /dev/null
+++ b/chatty2.c
@@ -0,0 +1,90 @@
+/* Macro's */
+
+#define TB_IMPL
+#include "external/termbox2.h"
+#undef TB_IMPL
+
+#define DEBUG
+#define MAX_INPUT_LEN 255
+
+#define CHATTY_IMPL
+#include "ui.h"
+#undef CHATTY_IMPL
+#include "protocol.h"
+
+#include <locale.h>
+
+#ifdef DEBUG
+#define Assert(expr) \
+ if (!(expr)) \
+ { \
+ tb_shutdown(); \
+ raise(SIGTRAP); \
+ }
+#else
+#define Assert(expr) ;
+#endif
+
+int
+main(int Argc, char *Argv[])
+{
+ struct tb_event ev;
+ rect TextBox = {0, 0, 24, 4};
+ rect TextR = {
+ TextBox.X + TEXTBOX_BORDER_WIDTH + TEXTBOX_PADDING_X,
+ TextBox.Y + TEXTBOX_BORDER_WIDTH,
+ TextBox.W - TEXTBOX_BORDER_WIDTH * 2 - TEXTBOX_PADDING_X * 2,
+ TextBox.H - TEXTBOX_BORDER_WIDTH * 2
+ };
+ wchar_t Input[MAX_INPUT_LEN] = {0};
+ u32 InputLen = 0;
+ u32 InputOffset = 0;
+ u32 InputPos = 0;
+ u32 DidParseKey = 0;
+
+ Assert(setlocale(LC_ALL, ""));
+ tb_init();
+ global.cursor_x = TextR.X;
+ global.cursor_y = TextR.Y;
+ bytebuf_puts(&global.out, global.caps[TB_CAP_SHOW_CURSOR]);
+
+ while (ev.key != TB_KEY_CTRL_C)
+ {
+ tb_clear();
+
+ DrawBox(TextBox, 0);
+ TextBoxDraw(TextR, Input + InputOffset, InputLen);
+
+ InputPos = InputOffset + (global.cursor_x - TextR.X) + (global.cursor_y - TextR.Y) * TextR.W;
+ Assert(InputPos <= InputLen);
+
+ tb_present();
+
+ tb_poll_event(&ev);
+
+ // TODO: Handle resize event
+
+ // Intercept keys
+ if (ev.key == TB_KEY_CTRL_M)
+ {
+ tb_printf(26, 0, 0, 0, "sent.");
+ continue;
+ }
+ else
+ {
+ DidParseKey = TextBoxKeypress(ev, TextR,
+ Input, &InputLen, InputPos, &InputOffset);
+ }
+
+ u32 ShouldInsert = (!DidParseKey) && (ev.ch && InputLen < MAX_INPUT_LEN);
+ if (ShouldInsert)
+ {
+ TextBoxInsert(Input, InputPos, InputLen++, ev.ch);
+ ScrollRight(TextR, &InputOffset);
+ }
+
+ // tb_clear();
+ }
+
+ tb_shutdown();
+}