draw.h (6237B)
1 /* This program is licensed under the terms of GNU GPL v3 or (at your option) 2 * any later version. Copyright (C) 2021-2026 Страхиња Радић. 3 * See the file LICENSE for exact copyright and license details. */ 4 5 #include <stddef.h> 6 #include <sys/types.h> 7 #include <termios.h> 8 #include <unistd.h> 9 #include <wchar.h> 10 11 #include "termbox.h" 12 13 #define BORDER_NW ((uint32_t)0x250c) 14 #define BORDER_NE ((uint32_t)0x2510) 15 #define BORDER_SW ((uint32_t)0x2514) 16 #define BORDER_SE ((uint32_t)0x2518) 17 #define BORDER_VER ((uint32_t)0x2502) 18 #define BORDER_HOR ((uint32_t)0x2500) 19 #define ARROW_UP ((uint32_t)0x2191) 20 #define ARROW_DOWN ((uint32_t)0x2193) 21 22 #define EDIT_WIDTH 70 23 #define EDIT_HEIGHT 20 24 #define HELP_WIDTH 50 25 #define HELP_HEIGHT 22 26 #define SEARCH_WIDTH 20 27 #define SEARCH_HEIGHT 4 28 #define TAB_SIZE 8 29 #define MAXSEARCH 512 30 31 typedef enum { LEFT, CENTER, RIGHT } Alignment; 32 33 typedef enum { 34 C_ACTION, 35 C_CLEAR, 36 C_DLG, 37 C_EDIT_MSGID, 38 C_EDIT_MSGID_FOCUS, 39 C_EDIT_MSGID_HEADING, 40 C_EDIT_MSGSTR, 41 C_EDIT_MSGSTR_FOCUS, 42 C_ERROR, 43 C_FLAG, 44 C_LIST_FLAGS, 45 C_LIST_FLAGS_SEL, 46 C_LIST_MSGID, 47 C_LIST_MSGID_SEL, 48 C_LIST_MSGID_SPECIAL, 49 C_LIST_MSGID_SPECIAL_SEL, 50 C_LIST_MSGSTR, 51 C_LIST_MSGSTR_SEL, 52 C_LIST_MSGSTR_WARN, 53 C_LIST_MSGSTR_SEL_WARN, 54 C_LIST_SEL, 55 C_MSGID, 56 C_MSGSTR, 57 C_PROMPT, 58 C_SEARCH, 59 C_SEARCH_EDIT, 60 C_SEARCH_HL, 61 C_SHADOW, 62 C_STATUS_1, 63 C_STATUS_2, 64 C_STATUS_3, 65 C_TITLE 66 } ColorIndex; 67 68 typedef enum { UNCHANGED, SET, CLEARED } FlagChange; 69 70 struct Color { 71 uint16_t fg; 72 uint16_t bg; 73 }; 74 75 struct Coord { 76 int col; 77 int row; 78 }; 79 80 struct BufferLine { 81 uint32_t* text; 82 ssize_t length; /* both in uint32_t units */ 83 ssize_t size; 84 uint16_t fg; 85 uint16_t bg; 86 }; 87 88 struct DrawState { 89 char* prompt; 90 char* filename; 91 char* orig_real_filename; /* what symlink points to */ 92 char* real_filename; /* directory holding the symlink prepended to it */ 93 char* backup_filename; 94 ssize_t rfn_len; 95 uint32_t* search; 96 int error; 97 int action; 98 struct BufferLine* input_buffer; /* edit box */ 99 ssize_t buffer_size; /* buffer parameter */ 100 struct Coord coord; /* buffer coordinates */ 101 struct Coord first_shown; /* display coordinates */ 102 struct Coord input_first_shown; /* display coordinates */ 103 ssize_t input_rows_count; /* buffer parameter */ 104 int input_display_column; /* display coordinate */ 105 FlagChange input_changed_fuzzy; 106 int saved_column; /* display coordinate */ 107 struct BufferLine* info_buffer; /* information pane above 108 * edit box */ 109 struct Coord info_first_shown; /* display coordinates */ 110 int info_rows_count; 111 int info_maxlen; 112 int edit_width; /* display parameter */ 113 int help_width; /* display parameter */ 114 int search_width; /* display parameter */ 115 struct BufferLine* paste; 116 ssize_t paste_count; 117 ssize_t paste_size; 118 int search_column; 119 int search_first_shown_column; 120 int search_display_column; 121 struct PoEntry* entries; 122 int nplurals; 123 int (*prompt_callback)(struct DrawState*, struct tb_event*); 124 int running; 125 int dirty; /* tracking changes in the file */ 126 int edit_dirty; /* tracking changes in the edit box */ 127 ssize_t fuzzy_before_edit; 128 int in_prompt; 129 int reshow_edit; /* edit box shown again after save/cancel? */ 130 int show_edit; 131 int show_help; 132 int edit_info_focused; 133 int show_search; 134 int recalculate_size; 135 int maxx; 136 int maxy; 137 ssize_t msgid_count; /* without obsolete msgids */ 138 ssize_t msgid_number; /* "real" msgid (inc. obsolete msgids) */ 139 ssize_t msgid_size; 140 ssize_t fuzzy_count; 141 ssize_t untranslated_count; 142 ssize_t obsolete_count; 143 ssize_t obsolete_at_end_count; 144 ssize_t real_msgid_count; /* msgid_count + obsolete_count */ 145 int msgstr_index; /* with msgstr[] */ 146 ssize_t first_shown_msgid; 147 int backup_possible; /* is saving backup possible? */ 148 }; 149 150 struct Key { 151 struct tb_event ev; 152 int (*callback)(struct DrawState*); 153 }; 154 155 struct StatusSegment { 156 const char* format; 157 void (*callback)(char*, ssize_t, const char*, const ssize_t, 158 const struct DrawState*); 159 ColorIndex color_index; 160 Alignment alignment; 161 }; 162 163 void assign_coord(struct Coord* dest, const struct Coord src); 164 ssize_t buffer_length(uint32_t* buffer, const ssize_t max_index); 165 int calculate_edit_width(const struct DrawState* state); 166 int calculate_help_width(const struct DrawState* state); 167 int calculate_search_width(const struct DrawState* state); 168 ssize_t display_length(uint32_t* buffer, const ssize_t max_index); 169 int draw(const struct DrawState* state); 170 void draw_box(const int startx, const int endx, const int starty, 171 const int endy, const uint16_t fg, const uint16_t bg); 172 int draw_help(const struct DrawState* state); 173 int draw_editbox(const struct DrawState* state); 174 int draw_list_visible(const struct DrawState* state); 175 void draw_rect(const int startx, const int endx, const int starty, 176 const int endy, const uint16_t fg, const uint16_t bg); 177 int draw_search(const struct DrawState* state); 178 int draw_status(const struct DrawState* state); 179 int draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg, 180 const char* s, const int max_cols, const int fill_cols, 181 const int padding_left, const int padding_right, const Alignment align, 182 const int ds, const int fill, const uint32_t* srch, const uint16_t sfg, 183 const uint16_t sbg); 184 void format_filename(char* result, ssize_t result_size, const char* format, 185 const ssize_t seg_size, const struct DrawState* state); 186 void format_msgs(char* result, ssize_t result_size, const char* format, 187 const ssize_t seg_size, const struct DrawState* state); 188 void free_bufferline(struct BufferLine* bl); 189 void free_drawstate(struct DrawState* state); 190 void init_bufferline(struct BufferLine* bl); 191 void init_coord(struct Coord* c, const int col, const int row); 192 void init_drawstate(struct DrawState* state, char* filename, char* prompt); 193 ssize_t input_length(uint32_t* buffer, const ssize_t max_index); 194 size_t real_msgid_to_display_msgid(const struct DrawState* state); 195 void recalculate_size(struct DrawState* state); 196 void u32_draw_string(const int x, const int y, const uint16_t fg, 197 const uint16_t bg, const uint32_t* s, const int max_cols, 198 const int fill_cols, const int padding_left, const int padding_right, 199 const Alignment align, const int ds, const int fill, 200 const uint32_t* srch, const uint16_t sfg, const uint16_t sbg);