view third_party/raylib/custom.c @ 161:87d8d3eb3491

[PostDog] WIP to make it more mordern looking
author June Park <parkjune1995@gmail.com>
date Thu, 15 Jan 2026 08:29:26 -0800
parents 7bd795bac997
children 058de208e640
line wrap: on
line source

#include "third_party/raylib/include/raylib.h"
#define RAYGUI_IMPLEMENTATION
#include "third_party/raylib/include/raygui.h"
#include "third_party/raylib/custom.h"

// -- forward declarations --//
void DefaultBehaviours()
{
  // Font sizes 
  IncreaseFontSize();
  DecreaseFontSize();
}

void IncreaseFontSize()
{
  if ((IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_LEFT_CONTROL)) && IsKeyDown(KEY_EQUAL))
    GuiSetStyle(DEFAULT, TEXT_SIZE, GuiGetStyle(DEFAULT, TEXT_SIZE) + 1);
}

void DecreaseFontSize()
{
  if ((IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_LEFT_CONTROL)) && IsKeyDown(KEY_MINUS))
    GuiSetStyle(DEFAULT, TEXT_SIZE, GuiGetStyle(DEFAULT, TEXT_SIZE) - 1);
}

// --- Layout helper --- //
Rectangle RightOf(Rectangle ref, float padding)
{
  return (Rectangle){
    .x = ref.x + ref.width + padding,
    .y = ref.y,
    .width = 0,
    .height = ref.height
  };
}

Rectangle Below(Rectangle ref, float padding)
{
  return (Rectangle){
    .x = ref.x,
    .y = ref.y + ref.height + padding,
    .width = ref.width,
    .height = 0
  };
}

Rectangle LeftColumn(Rectangle container, float ratio, float padding)
{
  return (Rectangle){
    .x = container.x + padding,
    .y = container.y + padding,
    .width = (container.width * ratio) - padding,
    .height = container.height - (2 * padding)
  };
}

Rectangle RightColumn(Rectangle container, Rectangle leftCol, float padding)
{
  return (Rectangle){
    .x = leftCol.x + leftCol.width + padding,
    .y = container.y + padding,
    .width = container.width - leftCol.width - (3 * padding),
    .height = container.height - (2 * padding)
  };
}

Rectangle HorizontalSplit(Rectangle container, float ratio)
{
  return (Rectangle){
    .x = container.x,
    .y = container.y,
    .width = container.width * ratio,
    .height = container.height
  };
}

void DrawRectangleSelectiveRounded(Rectangle rec, float radius, int segments, Color color,
                                   boolean roundTL, boolean roundTR, boolean roundBR, boolean roundBL) {
  DrawRectangle(rec.x + radius, rec.y + radius, rec.width - 2*radius, rec.height - 2*radius, color);
  // Top edge (excluding rounded corners)
  DrawRectangle(rec.x + (roundTL ? radius : 0), rec.y, rec.width - (roundTL ? radius : 0) - (roundTR ? radius : 0), radius, color);
  // Bottom edge
  DrawRectangle(rec.x + (roundBL ? radius : 0), rec.y + rec.height - radius, rec.width - (roundBL ? radius : 0) - (roundBR ? radius : 0), radius, color);
  // Left edge
  DrawRectangle(rec.x, rec.y + (roundTL ? radius : 0), radius, rec.height - (roundTL ? radius : 0) - (roundBL ? radius : 0), color);
  // Right edge
  DrawRectangle(rec.x + rec.width - radius, rec.y + (roundTR ? radius : 0), radius, rec.height - (roundTR ? radius : 0) - (roundBR ? radius : 0), color);

  if (roundTL)
    DrawCircleSector((Vector2){rec.x + radius, rec.y + radius}, radius, 180, 270, segments, color);

  if (roundTR)
    DrawCircleSector((Vector2){rec.x + rec.width - radius, rec.y + radius}, radius, 270, 360, segments, color);

  if (roundBR)
    DrawCircleSector((Vector2){rec.x + rec.width - radius, rec.y + rec.height - radius}, radius, 0, 90, segments, color);

  if (roundBL)
    DrawCircleSector((Vector2){rec.x + radius, rec.y + rec.height - radius}, radius, 90, 180, segments, color);
}

Rectangle AddPadding(Rectangle rect, float padding)
{
  return (Rectangle){
    rect.x + padding,
    rect.y + padding,
    rect.width - (2 * padding),
    rect.height - (2 * padding)
  };
}

Rectangle AddPaddingAll(Rectangle rect, float top, float right,float down, float left)
{
  return (Rectangle){
    rect.x + left,
    rect.y + top,
    rect.width - (right + left),
    rect.height - (top + down),
  };
}



Rectangle AddPaddingHorizontal(Rectangle rect, float padding)
{
  return (Rectangle){
    rect.x + padding,
    rect.y,
    rect.width - (2 * padding),
    rect.height
  };
}

Rectangle AddPaddingVertical(Rectangle rect, float padding)
{
  return (Rectangle){
    rect.x,
    rect.y + padding,
    rect.width,
    rect.height - (2 * padding)
  };
}

// // --- Components ---/
// #define TEXT_SIZE_DEFAULT 10 
// #define TEXT_AREA_LINE_HEIGHT 20
// #define TEXT_AREA_PADDING 8
// #define TEXT_AREA_CURSOR_WIDTH 2
// #define TEXT_AREA_MAX_UNDO_STATES 64
// #define TEXT_AREA_MAX_INSTANCES 8
// 
// typedef struct {
//   char *text;
//   int cursor_pos;
//   int selection_start;
//   int selection_end;
// } TextAreaUndoEntry;
// 
// typedef struct {
//   int id;             // Unique ID for this text area
//   int cursor_pos;         // Current cursor position in text
//   int selection_start;      // Selection start (-1 if no selection)
//   int selection_end;      // Selection end (-1 if no selection)
//   float scroll_offset_y;    // Vertical scroll offset
//   float scroll_offset_x;    // Horizontal scroll offset (for non-wrap mode)
//   boolean is_selecting;     // Currently dragging to select
//   boolean is_initialized;     // State has been initialized
// 
//   // Undo history
//   TextAreaUndoEntry *undo_stack;  // Dowa array of undo entries
//   int undo_index;         // Current position in undo stack
// 
//   // Internal tracking
//   double last_blink_time;
//   boolean cursor_visible;
// } TextAreaState;
// 
// static TextAreaState g_text_area_states[TEXT_AREA_MAX_INSTANCES] = {0};
// static int g_text_area_state_count = 0;
// static char *g_clipboard_text = NULL;
// static Dowa_Arena *g_text_area_arena = NULL;
// 
// // Helper functions
// static int TA_Min_Int(int a, int b) { return a < b ? a : b; }
// static int TA_Max_Int(int a, int b) { return a > b ? a : b; }
// static float TA_Min_Float(float a, float b) { return a < b ? a : b; }
// static float TA_Max_Float(float a, float b) { return a > b ? a : b; }
// 
// static TextAreaState* GetTextAreaState(int id) {
//   for (int i = 0; i < g_text_area_state_count; i++) {
//     if (g_text_area_states[i].id == id && g_text_area_states[i].is_initialized) {
//       return &g_text_area_states[i];
//     }
//   }
//   return NULL;
// }
// 
// static TextAreaState* CreateTextAreaState(int id) {
//   if (g_text_area_state_count >= TEXT_AREA_MAX_INSTANCES)
//     return &g_text_area_states[0]; // Reuse first slot
// 
//   TextAreaState *state = &g_text_area_states[g_text_area_state_count++];
//   memset(state, 0, sizeof(TextAreaState));
//   state->id = id;
//   state->selection_start = -1;
//   state->selection_end = -1;
//   state->undo_index = -1;
//   state->cursor_visible = TRUE;
//   state->is_initialized = TRUE;
//   return state;
// }
// 
// static void GetLineAndColumn(const char *text, int pos, int *out_line, int *out_column) {
//   int line = 0;
//   int column = 0;
//   for (int i = 0; i < pos && text[i] != '\0'; i++)
//   {
//     if (text[i] == '\n')
//     {
//       line++;
//       column = 0;
//     }
//     else
//       column++;
//   }
//   *out_line = line;
//   *out_column = column;
// }
// 
// static int GetPosFromLineColumn(const char *text, int line, int column) {
//   int current_line = 0;
//   int current_col = 0;
//   int i = 0;
// 
//   while (text[i] != '\0')
//   {
//     if (current_line == line && current_col == column)
//       return i;
// 
//     if (text[i] == '\n')
//     {
//       if (current_line == line)
//         return i;
//       current_line++;
//       current_col = 0;
//     }
//     else
//       current_col++;
//     i++;
//   }
//   return i;
// }
// 
// static int GetLineStart(const char *text, int pos) {
//   int i = pos;
//   while (i > 0 && text[i - 1] != '\n')
//     i--;
//   return i;
// }
// 
// static int GetLineEnd(const char *text, int pos) {
//   int i = pos;
//   int len = strlen(text);
//   while (i < len && text[i] != '\n')
//     i++;
//   return i;
// }
// 
// static int CountLines(const char *text) {
//   int count = 1;
//   for (int i = 0; text[i] != '\0'; i++) 
//     if (text[i] == '\n') count++;
//   return count;
// }
// 
// static int MeasureTextRange(const char *text, int start, int end, int font_size) {
//   if (start >= end) return 0;
// 
//   char temp[1024];
//   int len = TA_Min_Int(end - start, 1023);
//   strncpy(temp, text + start, len);
//   temp[len] = '\0';
// 
//   return MeasureTextEx(GuiGetFont(), temp, font_size, TEXT_SIZE_DEFAULT/GuiGetStyle(DEFAULT, TEXT_SIZE)).x;
// }
// 
// static int GetCharIndexFromPos(const char *text, Rectangle bounds, Vector2 pos,
//                 boolean wrap, float scroll_y, int font_size, int line_height) {
//   if (!text || strlen(text) == 0) return 0;
// 
//   float content_x = bounds.x + TEXT_AREA_PADDING;
//   float content_y = bounds.y + TEXT_AREA_PADDING - scroll_y;
//   float content_width = bounds.width - TEXT_AREA_PADDING * 2;
// 
//   int text_len = strlen(text);
// 
//   float click_line_y = (pos.y - content_y) / line_height;
//   int target_visual_line = (int)click_line_y;
//   if (target_visual_line < 0) target_visual_line = 0;
// 
//   int current_visual_line = 0;
//   int i = 0;
//   int line_char_start = 0;
// 
//   while (i <= text_len) {
//     boolean is_newline = (i < text_len && text[i] == '\n');
// 
//     if (wrap && i > line_char_start) {
//       int line_width = MeasureTextRange(text, line_char_start, i, font_size);
//       if (line_width > content_width && i > line_char_start + 1) {
//         int wrap_pos = i - 1;
//         for (int j = i - 1; j > line_char_start; j--) {
//           if (text[j] == ' ') {
//             wrap_pos = j;
//             break;
//           }
//         }
// 
//         if (current_visual_line == target_visual_line) {
//           float click_x = pos.x - content_x;
//           int best_pos = line_char_start;
//           float best_dist = 99999;
// 
//           for (int k = line_char_start; k <= wrap_pos; k++) {
//             int char_x = MeasureTextRange(text, line_char_start, k, font_size);
//             float dist = (float)(click_x - char_x);
//             if (dist < 0) dist = -dist;
//             if (dist < best_dist) {
//               best_dist = dist;
//               best_pos = k;
//             }
//           }
//           return best_pos;
//         }
// 
//         current_visual_line++;
//         line_char_start = (text[wrap_pos] == ' ') ? wrap_pos + 1 : wrap_pos;
//         i = line_char_start;
//         continue;
//       }
//     }
// 
//     if (is_newline || i == text_len) {
//       if (current_visual_line == target_visual_line || i == text_len) {
//         float click_x = pos.x - content_x;
//         int line_end = i;
//         int best_pos = line_char_start;
//         float best_dist = 99999;
// 
//         for (int k = line_char_start; k <= line_end; k++) {
//           int char_x = MeasureTextRange(text, line_char_start, k, font_size);
//           float dist = (float)(click_x - char_x);
//           if (dist < 0) dist = -dist;
//           if (dist < best_dist) {
//             best_dist = dist;
//             best_pos = k;
//           }
//         }
//         return TA_Min_Int(best_pos, text_len);
//       }
// 
//       current_visual_line++;
//       line_char_start = i + 1;
//     }
// 
//     i++;
//   }
// 
//   return text_len;
// }
// 
// static Vector2 GetCursorScreenPos(const char *text, int cursor_pos, Rectangle bounds,
//                    boolean wrap, float scroll_y, int font_size, int line_height) {
//   float content_x = bounds.x + TEXT_AREA_PADDING;
//   float content_y = bounds.y + TEXT_AREA_PADDING - scroll_y;
//   float content_width = bounds.width - TEXT_AREA_PADDING * 2;
// 
//   if (!text || cursor_pos == 0) {
//     return (Vector2){content_x, content_y};
//   }
// 
//   int text_len = strlen(text);
//   cursor_pos = TA_Min_Int(cursor_pos, text_len);
// 
//   int visual_line = 0;
//   int line_char_start = 0;
// 
//   for (int i = 0; i <= cursor_pos; i++) {
//     if (i == cursor_pos) {
//       float x = content_x + MeasureTextRange(text, line_char_start, cursor_pos, font_size);
//       float y = content_y + visual_line * line_height;
//       return (Vector2){x, y};
//     }
// 
//     if (text[i] == '\n') {
//       visual_line++;
//       line_char_start = i + 1;
//     } else if (wrap) {
//       int line_width = MeasureTextRange(text, line_char_start, i + 1, font_size);
//       if (line_width > content_width && i > line_char_start) {
//         int wrap_pos = i;
//         for (int j = i; j > line_char_start; j--) {
//           if (text[j] == ' ') {
//             wrap_pos = j;
//             break;
//           }
//         }
// 
//         if (cursor_pos <= wrap_pos) {
//           float x = content_x + MeasureTextRange(text, line_char_start, cursor_pos, font_size);
//           float y = content_y + visual_line * line_height;
//           return (Vector2){x, y};
//         }
// 
//         visual_line++;
//         line_char_start = (text[wrap_pos] == ' ') ? wrap_pos + 1 : wrap_pos;
//       }
//     }
//   }
// 
//   float x = content_x + MeasureTextRange(text, line_char_start, cursor_pos, font_size);
//   float y = content_y + visual_line * line_height;
//   return (Vector2){x, y};
// }
// 
// static float GetContentHeight(const char *text, Rectangle bounds, boolean wrap,
//                  int font_size, int line_height) {
//   if (!text || strlen(text) == 0) return line_height;
// 
//   float content_width = bounds.width - TEXT_AREA_PADDING * 2;
//   int visual_lines = 1;
//   int line_char_start = 0;
//   int text_len = strlen(text);
// 
//   for (int i = 0; i <= text_len; i++) {
//     if (i == text_len || text[i] == '\n') {
//       visual_lines++;
//       line_char_start = i + 1;
//     } else if (wrap) {
//       int line_width = MeasureTextRange(text, line_char_start, i + 1, font_size);
//       if (line_width > content_width && i > line_char_start) {
//         int wrap_pos = i;
//         for (int j = i; j > line_char_start; j--) {
//           if (text[j] == ' ') {
//             wrap_pos = j;
//             break;
//           }
//         }
//         visual_lines++;
//         line_char_start = (text[wrap_pos] == ' ') ? wrap_pos + 1 : wrap_pos;
//       }
//     }
//   }
// 
//   return visual_lines * line_height;
// }
// 
// static void PushUndoState(TextAreaState *state, const char *text, Dowa_Arena *arena) {
//   TextAreaUndoEntry entry;
//   entry.text = Dowa_String_Copy_Arena((char*)text, arena);
//   entry.cursor_pos = state->cursor_pos;
//   entry.selection_start = state->selection_start;
//   entry.selection_end = state->selection_end;
// 
//   if (state->undo_index < (int)Dowa_Array_Length(state->undo_stack) - 1) {
//     dowa__header(state->undo_stack)->length = state->undo_index + 1;
//   }
// 
//   Dowa_Array_Push_Arena(state->undo_stack, entry, arena);
//   state->undo_index = Dowa_Array_Length(state->undo_stack) - 1;
// 
//   if (Dowa_Array_Length(state->undo_stack) > TEXT_AREA_MAX_UNDO_STATES) {
//     for (int i = 0; i < (int)Dowa_Array_Length(state->undo_stack) - 1; i++) {
//       state->undo_stack[i] = state->undo_stack[i + 1];
//     }
//     dowa__header(state->undo_stack)->length--;
//     state->undo_index--;
//   }
// }
// 
// static boolean PerformUndo(TextAreaState *state, char *text, int text_size) {
//   if (state->undo_index <= 0) return FALSE;
// 
//   state->undo_index--;
//   TextAreaUndoEntry *entry = &state->undo_stack[state->undo_index];
// 
//   strncpy(text, entry->text, text_size - 1);
//   text[text_size - 1] = '\0';
//   state->cursor_pos = entry->cursor_pos;
//   state->selection_start = entry->selection_start;
//   state->selection_end = entry->selection_end;
// 
//   return TRUE;
// }
// 
// static boolean PerformRedo(TextAreaState *state, char *text, int text_size) {
//   if (state->undo_index >= (int)Dowa_Array_Length(state->undo_stack) - 1) return FALSE;
// 
//   state->undo_index++;
//   TextAreaUndoEntry *entry = &state->undo_stack[state->undo_index];
// 
//   strncpy(text, entry->text, text_size - 1);
//   text[text_size - 1] = '\0';
//   state->cursor_pos = entry->cursor_pos;
//   state->selection_start = entry->selection_start;
//   state->selection_end = entry->selection_end;
// 
//   return TRUE;
// }
// 
// static void InsertTextAtCursor(char *text, int text_size, int cursor_pos,
//                 const char *insert_text, int *new_cursor_pos) {
//   int text_len = strlen(text);
//   int insert_len = strlen(insert_text);
// 
//   if (text_len + insert_len >= text_size - 1) {
//     insert_len = text_size - 1 - text_len;
//     if (insert_len <= 0) return;
//   }
// 
//   memmove(text + cursor_pos + insert_len,
//       text + cursor_pos,
//       text_len - cursor_pos + 1);
// 
//   memcpy(text + cursor_pos, insert_text, insert_len);
// 
//   *new_cursor_pos = cursor_pos + insert_len;
// }
// 
// static void DeleteTextRange(char *text, int start, int end) {
//   if (start >= end) return;
//   int text_len = strlen(text);
//   if (start < 0) start = 0;
//   if (end > text_len) end = text_len;
// 
//   memmove(text + start, text + end, text_len - end + 1);
// }
// 
// static char* GetSelectedText(const char *text, int sel_start, int sel_end, Dowa_Arena *arena) {
//   if (sel_start < 0 || sel_end < 0 || sel_start >= sel_end) return NULL;
// 
//   int len = sel_end - sel_start;
//   char *result = Dowa_Arena_Allocate(arena, len + 1);
//   strncpy(result, text + sel_start, len);
//   result[len] = '\0';
//   return result;
// }
// 
// boolean GuiTextArea(int id, Rectangle bounds, char *text, int text_size, boolean is_edit_mode,
//           boolean should_text_wrap, Dowa_Arena *arena) {
//   boolean should_toggle_edit_mode = FALSE;
// 
//   // Get or create state for this text area
//   TextAreaState *state = GetTextAreaState(id);
//   if (!state)
//   {
//     state = CreateTextAreaState(id);
//     state->cursor_pos = strlen(text);
//     state->last_blink_time = GetTime();
//     PushUndoState(state, text, arena);
//   }
// 
//   int text_len = strlen(text);
//   Vector2 mouse_pos = GetMousePosition();
//   boolean mouse_in_bounds = CheckCollisionPointRec(mouse_pos, bounds);
// 
//   // Handle click to enter/exit edit mode
//   if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
//     if (mouse_in_bounds && !is_edit_mode) {
//       should_toggle_edit_mode = TRUE;
//     } else if (!mouse_in_bounds && is_edit_mode) {
//       should_toggle_edit_mode = TRUE;
//     }
//   }
// 
//   // Content area
//   float content_height = GetContentHeight(text, bounds, should_text_wrap,
//                       GuiGetStyle(DEFAULT, TEXT_SIZE), TEXT_AREA_LINE_HEIGHT);
//   float visible_height = bounds.height - TEXT_AREA_PADDING * 2;
//   float max_scroll = TA_Max_Float(0, content_height - visible_height);
// 
//   // Handle scrolling
//   float wheel = GetMouseWheelMove();
//   if (mouse_in_bounds && wheel != 0) {
//     state->scroll_offset_y -= wheel * TEXT_AREA_LINE_HEIGHT * 3;
//     state->scroll_offset_y = TA_Max_Float(0, TA_Min_Float(state->scroll_offset_y, max_scroll));
//   }
// 
//   if (is_edit_mode) {
//     boolean ctrl_pressed = IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL) ||
//                  IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_RIGHT_SUPER);
//     boolean shift_pressed = IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT);
//     boolean text_changed = FALSE;
// 
//     double current_time = GetTime();
//     if (current_time - state->last_blink_time > 0.5) {
//       state->cursor_visible = !state->cursor_visible;
//       state->last_blink_time = current_time;
//     }
// 
//     // Mouse Selection
//     if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && mouse_in_bounds) {
//       int click_pos = GetCharIndexFromPos(text, bounds, mouse_pos, should_text_wrap,
//                          state->scroll_offset_y, GuiGetStyle(DEFAULT, TEXT_SIZE),
//                          TEXT_AREA_LINE_HEIGHT);
//       state->cursor_pos = click_pos;
//       state->selection_start = -1;
//       state->selection_end = -1;
//       state->is_selecting = TRUE;
//       state->cursor_visible = TRUE;
//       state->last_blink_time = current_time;
//     }
// 
//     if (state->is_selecting && IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
//       int drag_pos = GetCharIndexFromPos(text, bounds, mouse_pos, should_text_wrap,
//                         state->scroll_offset_y, GuiGetStyle(DEFAULT, TEXT_SIZE),
//                         TEXT_AREA_LINE_HEIGHT);
//       if (drag_pos != state->cursor_pos) {
//         if (state->selection_start < 0) {
//           state->selection_start = state->cursor_pos;
//         }
//         state->selection_end = drag_pos;
//       }
//     }
// 
//     if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) {
//       state->is_selecting = FALSE;
//       if (state->selection_start >= 0 && state->selection_end >= 0) {
//         if (state->selection_start > state->selection_end) {
//           int temp = state->selection_start;
//           state->selection_start = state->selection_end;
//           state->selection_end = temp;
//         }
//         if (state->selection_start == state->selection_end) {
//           state->selection_start = -1;
//           state->selection_end = -1;
//         }
//       }
//     }
// 
//     // Ctrl+A: Select All
//     if (ctrl_pressed && IsKeyPressed(KEY_A)) {
//       state->selection_start = 0;
//       state->selection_end = text_len;
//       state->cursor_pos = text_len;
//     }
// 
//     // Ctrl+C: Copy
//     if (ctrl_pressed && IsKeyPressed(KEY_C)) {
//       if (state->selection_start >= 0 && state->selection_end >= 0 &&
//         state->selection_start != state->selection_end) {
//         int sel_min = TA_Min_Int(state->selection_start, state->selection_end);
//         int sel_max = TA_Max_Int(state->selection_start, state->selection_end);
//         char *selected = GetSelectedText(text, sel_min, sel_max, arena);
//         if (selected) {
//           if (g_clipboard_text) free(g_clipboard_text);
//           g_clipboard_text = strdup(selected);
//           SetClipboardText(g_clipboard_text);
//         }
//       }
//     }
// 
//     // Ctrl+X: Cut
//     if (ctrl_pressed && IsKeyPressed(KEY_X)) {
//       if (state->selection_start >= 0 && state->selection_end >= 0 &&
//         state->selection_start != state->selection_end) {
//         int sel_min = TA_Min_Int(state->selection_start, state->selection_end);
//         int sel_max = TA_Max_Int(state->selection_start, state->selection_end);
//         char *selected = GetSelectedText(text, sel_min, sel_max, arena);
//         if (selected) {
//           if (g_clipboard_text) free(g_clipboard_text);
//           g_clipboard_text = strdup(selected);
//           SetClipboardText(g_clipboard_text);
//         }
// 
//         PushUndoState(state, text, arena);
//         DeleteTextRange(text, sel_min, sel_max);
//         state->cursor_pos = sel_min;
//         state->selection_start = -1;
//         state->selection_end = -1;
//         text_changed = TRUE;
//       }
//     }
// 
//     // Ctrl+V: Paste
//     if (ctrl_pressed && IsKeyPressed(KEY_V)) {
//       const char *clipboard = GetClipboardText();
//       if (clipboard && strlen(clipboard) > 0) {
//         if (state->selection_start >= 0 && state->selection_end >= 0 &&
//           state->selection_start != state->selection_end) {
//           int sel_min = TA_Min_Int(state->selection_start, state->selection_end);
//           int sel_max = TA_Max_Int(state->selection_start, state->selection_end);
//           PushUndoState(state, text, arena);
//           DeleteTextRange(text, sel_min, sel_max);
//           state->cursor_pos = sel_min;
//           state->selection_start = -1;
//           state->selection_end = -1;
//         } else {
//           PushUndoState(state, text, arena);
//         }
// 
//         int new_cursor;
//         InsertTextAtCursor(text, text_size, state->cursor_pos, clipboard, &new_cursor);
//         state->cursor_pos = new_cursor;
//         text_changed = TRUE;
//       }
//     }
// 
//     // Ctrl+Z: Undo / Ctrl+Shift+Z: Redo
//     if (ctrl_pressed && IsKeyPressed(KEY_Z)) {
//       if (shift_pressed) {
//         PerformRedo(state, text, text_size);
//       } else {
//         PerformUndo(state, text, text_size);
//       }
//     }
// 
//     // Arrow Keys Navigation
//     if (IsKeyPressed(KEY_LEFT) || IsKeyPressedRepeat(KEY_LEFT)) {
//       if (state->selection_start >= 0 && !shift_pressed) {
//         state->cursor_pos = TA_Min_Int(state->selection_start, state->selection_end);
//         state->selection_start = -1;
//         state->selection_end = -1;
//       } else if (state->cursor_pos > 0) {
//         if (shift_pressed) {
//           if (state->selection_start < 0) {
//             state->selection_start = state->cursor_pos;
//             state->selection_end = state->cursor_pos;
//           }
//           state->cursor_pos--;
//           state->selection_end = state->cursor_pos;
//         } else {
//           state->cursor_pos--;
//         }
//       }
//       state->cursor_visible = TRUE;
//       state->last_blink_time = current_time;
//     }
// 
//     if (IsKeyPressed(KEY_RIGHT) || IsKeyPressedRepeat(KEY_RIGHT)) {
//       if (state->selection_start >= 0 && !shift_pressed) {
//         state->cursor_pos = TA_Max_Int(state->selection_start, state->selection_end);
//         state->selection_start = -1;
//         state->selection_end = -1;
//       } else if (state->cursor_pos < text_len) {
//         if (shift_pressed) {
//           if (state->selection_start < 0) {
//             state->selection_start = state->cursor_pos;
//             state->selection_end = state->cursor_pos;
//           }
//           state->cursor_pos++;
//           state->selection_end = state->cursor_pos;
//         } else {
//           state->cursor_pos++;
//         }
//       }
//       state->cursor_visible = TRUE;
//       state->last_blink_time = current_time;
//     }
// 
//     if (IsKeyPressed(KEY_UP) || IsKeyPressedRepeat(KEY_UP)) {
//       int line, col;
//       GetLineAndColumn(text, state->cursor_pos, &line, &col);
//       if (line > 0) {
//         int new_pos = GetPosFromLineColumn(text, line - 1, col);
//         if (shift_pressed) {
//           if (state->selection_start < 0) {
//             state->selection_start = state->cursor_pos;
//             state->selection_end = state->cursor_pos;
//           }
//           state->selection_end = new_pos;
//         } else {
//           state->selection_start = -1;
//           state->selection_end = -1;
//         }
//         state->cursor_pos = new_pos;
//       }
//       state->cursor_visible = TRUE;
//       state->last_blink_time = current_time;
//     }
// 
//     if (IsKeyPressed(KEY_DOWN) || IsKeyPressedRepeat(KEY_DOWN))
//     {
//       int line, col;
//       GetLineAndColumn(text, state->cursor_pos, &line, &col);
//       int total_lines = CountLines(text);
//       if (line < total_lines - 1) {
//         int new_pos = GetPosFromLineColumn(text, line + 1, col);
//         if (shift_pressed) {
//           if (state->selection_start < 0) {
//             state->selection_start = state->cursor_pos;
//             state->selection_end = state->cursor_pos;
//           }
//           state->selection_end = new_pos;
//         } else {
//           state->selection_start = -1;
//           state->selection_end = -1;
//         }
//         state->cursor_pos = new_pos;
//       }
//       state->cursor_visible = TRUE;
//       state->last_blink_time = current_time;
//     }
// 
//     // Home/End keys
//     if (IsKeyPressed(KEY_HOME)) {
//       int line_start = GetLineStart(text, state->cursor_pos);
//       if (shift_pressed) {
//         if (state->selection_start < 0) {
//           state->selection_start = state->cursor_pos;
//           state->selection_end = state->cursor_pos;
//         }
//         state->selection_end = line_start;
//       } else {
//         state->selection_start = -1;
//         state->selection_end = -1;
//       }
//       state->cursor_pos = line_start;
//     }
// 
//     if (IsKeyPressed(KEY_END)) {
//       int line_end = GetLineEnd(text, state->cursor_pos);
//       if (shift_pressed) {
//         if (state->selection_start < 0) {
//           state->selection_start = state->cursor_pos;
//           state->selection_end = state->cursor_pos;
//         }
//         state->selection_end = line_end;
//       } else {
//         state->selection_start = -1;
//         state->selection_end = -1;
//       }
//       state->cursor_pos = line_end;
//     }
// 
//     // Text Input
//     if (!ctrl_pressed) {
//       int key = GetCharPressed();
//       while (key > 0) {
//         if (key >= 32 && key <= 126) {
//           if (state->selection_start >= 0 && state->selection_end >= 0 &&
//             state->selection_start != state->selection_end) {
//             int sel_min = TA_Min_Int(state->selection_start, state->selection_end);
//             int sel_max = TA_Max_Int(state->selection_start, state->selection_end);
//             PushUndoState(state, text, arena);
//             DeleteTextRange(text, sel_min, sel_max);
//             state->cursor_pos = sel_min;
//             state->selection_start = -1;
//             state->selection_end = -1;
//             text_changed = TRUE;
//           } else if (!text_changed) {
//             PushUndoState(state, text, arena);
//           }
// 
//           char insert_str[2] = {(char)key, '\0'};
//           int new_cursor;
//           InsertTextAtCursor(text, text_size, state->cursor_pos, insert_str, &new_cursor);
//           state->cursor_pos = new_cursor;
//           text_changed = TRUE;
//         }
//         key = GetCharPressed();
//       }
//     }
// 
//     // Enter key
//     if (IsKeyPressed(KEY_ENTER) || IsKeyPressed(KEY_KP_ENTER)) {
//       if (state->selection_start >= 0 && state->selection_end >= 0 &&
//         state->selection_start != state->selection_end) {
//         int sel_min = TA_Min_Int(state->selection_start, state->selection_end);
//         int sel_max = TA_Max_Int(state->selection_start, state->selection_end);
//         PushUndoState(state, text, arena);
//         DeleteTextRange(text, sel_min, sel_max);
//         state->cursor_pos = sel_min;
//         state->selection_start = -1;
//         state->selection_end = -1;
//       } else {
//         PushUndoState(state, text, arena);
//       }
// 
//       int new_cursor;
//       InsertTextAtCursor(text, text_size, state->cursor_pos, "\n", &new_cursor);
//       state->cursor_pos = new_cursor;
//       text_changed = TRUE;
//     }
// 
//     // Backspace
//     if (IsKeyPressed(KEY_BACKSPACE) || IsKeyPressedRepeat(KEY_BACKSPACE)) {
//       if (state->selection_start >= 0 && state->selection_end >= 0 &&
//         state->selection_start != state->selection_end) {
//         int sel_min = TA_Min_Int(state->selection_start, state->selection_end);
//         int sel_max = TA_Max_Int(state->selection_start, state->selection_end);
//         PushUndoState(state, text, arena);
//         DeleteTextRange(text, sel_min, sel_max);
//         state->cursor_pos = sel_min;
//         state->selection_start = -1;
//         state->selection_end = -1;
//         text_changed = TRUE;
//       } else if (state->cursor_pos > 0) {
//         PushUndoState(state, text, arena);
//         DeleteTextRange(text, state->cursor_pos - 1, state->cursor_pos);
//         state->cursor_pos--;
//         text_changed = TRUE;
//       }
//     }
// 
//     // Delete key
//     if (IsKeyPressed(KEY_DELETE) || IsKeyPressedRepeat(KEY_DELETE)) {
//       text_len = strlen(text);
//       if (state->selection_start >= 0 && state->selection_end >= 0 &&
//         state->selection_start != state->selection_end) {
//         int sel_min = TA_Min_Int(state->selection_start, state->selection_end);
//         int sel_max = TA_Max_Int(state->selection_start, state->selection_end);
//         PushUndoState(state, text, arena);
//         DeleteTextRange(text, sel_min, sel_max);
//         state->cursor_pos = sel_min;
//         state->selection_start = -1;
//         state->selection_end = -1;
//         text_changed = TRUE;
//       } else if (state->cursor_pos < text_len) {
//         PushUndoState(state, text, arena);
//         DeleteTextRange(text, state->cursor_pos, state->cursor_pos + 1);
//         text_changed = TRUE;
//       }
//     }
// 
//     // Tab key
//     if (IsKeyPressed(KEY_TAB)) {
//       if (state->selection_start >= 0 && state->selection_end >= 0 &&
//         state->selection_start != state->selection_end) {
//         int sel_min = TA_Min_Int(state->selection_start, state->selection_end);
//         int sel_max = TA_Max_Int(state->selection_start, state->selection_end);
//         PushUndoState(state, text, arena);
//         DeleteTextRange(text, sel_min, sel_max);
//         state->cursor_pos = sel_min;
//         state->selection_start = -1;
//         state->selection_end = -1;
//       } else {
//         PushUndoState(state, text, arena);
//       }
// 
//       int new_cursor;
//       InsertTextAtCursor(text, text_size, state->cursor_pos, "  ", &new_cursor);
//       state->cursor_pos = new_cursor;
//       text_changed = TRUE;
//     }
// 
//     // Auto-scroll to keep cursor visible
//     Vector2 cursor_screen = GetCursorScreenPos(text, state->cursor_pos, bounds,
//                           should_text_wrap, state->scroll_offset_y,
//                           GuiGetStyle(DEFAULT, TEXT_SIZE), TEXT_AREA_LINE_HEIGHT);
// 
//     float visible_top = bounds.y + TEXT_AREA_PADDING;
//     float visible_bottom = bounds.y + bounds.height - TEXT_AREA_PADDING - TEXT_AREA_LINE_HEIGHT;
// 
//     if (cursor_screen.y < visible_top) {
//       state->scroll_offset_y -= visible_top - cursor_screen.y;
//     } else if (cursor_screen.y > visible_bottom) {
//       state->scroll_offset_y += cursor_screen.y - visible_bottom;
//     }
// 
//     state->scroll_offset_y = TA_Max_Float(0, TA_Min_Float(state->scroll_offset_y, max_scroll));
//   }
// 
//   // Drawing
//   DrawRectangleRec(bounds, is_edit_mode ? DARKGRAY : (Color){40, 40, 40, 255});
//   DrawRectangleLinesEx(bounds, 1, is_edit_mode ? WHITE : GRAY);
// 
//   BeginScissorMode((int)bounds.x, (int)bounds.y, (int)bounds.width, (int)bounds.height);
// 
//   float content_x = bounds.x + TEXT_AREA_PADDING;
//   float content_y = bounds.y + TEXT_AREA_PADDING - state->scroll_offset_y;
//   float content_width = bounds.width - TEXT_AREA_PADDING * 2;
// 
//   text_len = strlen(text);
// 
//   // Draw selection highlight
//   if (state->selection_start >= 0 && state->selection_end >= 0 &&
//     state->selection_start != state->selection_end) {
//     int sel_min = TA_Min_Int(state->selection_start, state->selection_end);
//     int sel_max = TA_Max_Int(state->selection_start, state->selection_end);
// 
//     int line_char_start = 0;
//     int visual_line = 0;
// 
//     for (int i = 0; i <= text_len; i++) {
//       boolean is_end = (i == text_len);
//       boolean is_newline = (!is_end && text[i] == '\n');
//       boolean should_draw_line = is_end || is_newline;
// 
//       if (should_text_wrap && !is_end && !is_newline) {
//         int line_width = MeasureTextRange(text, line_char_start, i + 1, GuiGetStyle(DEFAULT, TEXT_SIZE));
//         if (line_width > content_width && i > line_char_start) {
//           should_draw_line = TRUE;
//         }
//       }
// 
//       if (should_draw_line) {
//         int line_end = i;
// 
//         if (sel_min < line_end && sel_max > line_char_start) {
//           int highlight_start = TA_Max_Int(sel_min, line_char_start);
//           int highlight_end = TA_Min_Int(sel_max, line_end);
// 
//           float x1 = content_x + MeasureTextRange(text, line_char_start, highlight_start, GuiGetStyle(DEFAULT, TEXT_SIZE));
//           float x2 = content_x + MeasureTextRange(text, line_char_start, highlight_end, GuiGetStyle(DEFAULT, TEXT_SIZE));
//           float y = content_y + visual_line * TEXT_AREA_LINE_HEIGHT;
// 
//           DrawRectangle((int)x1, (int)y, (int)(x2 - x1), TEXT_AREA_LINE_HEIGHT,
//                   Fade(SKYBLUE, 0.5f));
//         }
// 
//         visual_line++;
//         line_char_start = i + 1;
//       }
//     }
//   }
// 
//   // Draw text with wrapping support
//   if (should_text_wrap) {
//     int line_char_start = 0;
//     int visual_line = 0;
// 
//     for (int i = 0; i <= text_len; i++) {
//       boolean is_end = (i == text_len);
//       boolean is_newline = (!is_end && text[i] == '\n');
//       boolean should_draw_line = is_end || is_newline;
// 
//       if (!is_end && !is_newline) {
//         int line_width = MeasureTextRange(text, line_char_start, i + 1, GuiGetStyle(DEFAULT, TEXT_SIZE));
//         if (line_width > content_width && i > line_char_start) {
//           should_draw_line = TRUE;
//         }
//       }
// 
//       if (should_draw_line && i > line_char_start) {
//         char line_buffer[1024];
//         int line_len = TA_Min_Int(i - line_char_start, 1023);
//         strncpy(line_buffer, text + line_char_start, line_len);
//         line_buffer[line_len] = '\0';
// 
//         Vector2 draw_text_vector = {
//           .x = content_x,
//           .y = content_y + visual_line * TEXT_AREA_LINE_HEIGHT
//         };
//         DrawTextEx(GuiGetFont(), line_buffer, draw_text_vector,
//             GuiGetStyle(DEFAULT, TEXT_SIZE), TEXT_SIZE_DEFAULT/GuiGetStyle(DEFAULT, TEXT_SIZE), WHITE);
// 
//         visual_line++;
//         line_char_start = is_newline ? i + 1 : i;
//       } else if (is_newline) {
//         visual_line++;
//         line_char_start = i + 1;
//       }
//     }
//   } else {
//     int line_start = 0;
//     int visual_line = 0;
// 
//     for (int i = 0; i <= text_len; i++) {
//       if (i == text_len || text[i] == '\n') {
//         if (i > line_start) {
//           char line_buffer[1024];
//           int line_len = TA_Min_Int(i - line_start, 1023);
//           strncpy(line_buffer, text + line_start, line_len);
//           line_buffer[line_len] = '\0';
// 
//           Vector2 draw_text_vector = {
//             .x = content_x,
//             .y = content_y + visual_line * TEXT_AREA_LINE_HEIGHT
//           };
//           DrawTextEx(GuiGetFont(), line_buffer, draw_text_vector ,
//               GuiGetStyle(DEFAULT, TEXT_SIZE), TEXT_SIZE_DEFAULT/GuiGetStyle(DEFAULT, TEXT_SIZE), WHITE);
//         }
//         visual_line++;
//         line_start = i + 1;
//       }
//     }
//   }
// 
//   // Draw cursor
//   if (is_edit_mode && state->cursor_visible) {
//     Vector2 cursor_pos = GetCursorScreenPos(text, state->cursor_pos, bounds,
//                          should_text_wrap, state->scroll_offset_y,
//                          GuiGetStyle(DEFAULT, TEXT_SIZE), TEXT_AREA_LINE_HEIGHT);
// 
//     DrawRectangle((int)cursor_pos.x, (int)cursor_pos.y,
//             TEXT_AREA_CURSOR_WIDTH, TEXT_AREA_LINE_HEIGHT, WHITE);
//   }
// 
//   EndScissorMode();
// 
//   // Draw scrollbar if needed
//   if (max_scroll > 0) {
//     float scrollbar_height = (visible_height / content_height) * visible_height;
//     float scrollbar_y = bounds.y + TEXT_AREA_PADDING +
//                (state->scroll_offset_y / max_scroll) * (visible_height - scrollbar_height);
// 
//     DrawRectangle((int)(bounds.x + bounds.width - 8), (int)scrollbar_y,
//             6, (int)scrollbar_height, Fade(WHITE, 0.3f));
//   }
// 
//   return should_toggle_edit_mode;
// }
// 
// void GuiTextAreaResetState(int id) {
//   TextAreaState *state = GetTextAreaState(id);
//   if (state) {
//     if (state->undo_stack) {
//       Dowa_Array_Free(state->undo_stack);
//       state->undo_stack = NULL;
//     }
//     state->is_initialized = FALSE;
//   }
// }
// 
// void GuiTextAreaResetAllStates(void) {
//   for (int i = 0; i < g_text_area_state_count; i++) {
//     if (g_text_area_states[i].undo_stack) {
//       Dowa_Array_Free(g_text_area_states[i].undo_stack);
//       g_text_area_states[i].undo_stack = NULL;
//     }
//     g_text_area_states[i].is_initialized = FALSE;
//   }
//   g_text_area_state_count = 0;
// }