sled

Једноставни уређивач текста
git clone https://git.sr.ht/~strahinja/sled
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

draw.h (5775B)


      1 /* This program is licensed under the terms of GNU GPL v3 or (at your option)
      2  * any later version. Copyright (C) 2022-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 TB_KEY_CTRL_SPACE 0x00
     14 
     15 #define BORDER_NW  ((uint32_t)0x250c)
     16 #define BORDER_NE  ((uint32_t)0x2510)
     17 #define BORDER_SW  ((uint32_t)0x2514)
     18 #define BORDER_SE  ((uint32_t)0x2518)
     19 #define BORDER_VER ((uint32_t)0x2502)
     20 #define BORDER_HOR ((uint32_t)0x2500)
     21 
     22 #define HELP_WIDTH	 60
     23 #define HELP_HEIGHT	 22
     24 #define INPUT_DLG_WIDTH	 40
     25 #define INPUT_DLG_HEIGHT 4
     26 #define TAB_SIZE	 8
     27 
     28 enum {
     29 	M_NONE	    = 0,
     30 	M_COMMAND   = 1,
     31 	M_OVERWRITE = 1 << 1,
     32 	M_SELECTION = 1 << 2,
     33 	M_PROMPT    = 1 << 3,
     34 	M_HELP	    = 1 << 4,
     35 	M_INPUT	    = 1 << 5,
     36 	M_INPUT_NUM = 1 << 6
     37 };
     38 
     39 typedef enum { LEFT, CENTER, RIGHT } Alignment;
     40 
     41 typedef enum {
     42 	C_DLG,
     43 	C_ERROR,
     44 	C_ACTION,
     45 	C_INPUT,
     46 	C_INPUT_EDIT,
     47 	C_LINENO,
     48 	C_PROMPT,
     49 	C_SEARCH,
     50 	C_SEL,
     51 	C_SHADOW,
     52 	C_STATUS_1,
     53 	C_STATUS_2,
     54 	C_STATUS_3,
     55 	C_STATUS_4,
     56 	C_STATUS_5,
     57 	C_TEXT,
     58 	C_TITLE
     59 } ColorIndex;
     60 
     61 struct Color {
     62 	uint16_t fg;
     63 	uint16_t bg;
     64 };
     65 
     66 struct Coord {
     67 	int col;
     68 	int row;
     69 };
     70 
     71 struct BufferLine {
     72 	uint32_t* text;
     73 	ssize_t length; /* both in uint32_t units */
     74 	ssize_t size;
     75 	uint16_t fg;
     76 	uint16_t bg;
     77 };
     78 
     79 struct DrawState {
     80 	char* prompt;
     81 	char* filename;
     82 	char* orig_real_filename; /* what symlink points to */
     83 	char* real_filename; /* directory holding the symlink prepended to it */
     84 	char* backup_filename;
     85 	ssize_t rfn_len;
     86 	uint32_t* input;
     87 	uint32_t* search;
     88 	int error;
     89 	int action;
     90 	int chain_dialog; /* show input dialogs in sequence? */
     91 	struct BufferLine* buffer;
     92 	ssize_t buffer_size;	      /* buffer parameter */
     93 	struct Coord coord;	      /* buffer coordinates */
     94 	struct Coord first_shown;     /* display coordinates */
     95 	int input_first_shown_column; /* display coordinate */
     96 	int input_display_column;     /* display coordinate */
     97 	int input_column;	      /* buffer coordinates */
     98 	char* input_title;
     99 	ssize_t rows_count;	/* buffer parameter */
    100 	int display_column;	/* display coordinate */
    101 	int saved_column;	/* display coordinate */
    102 	int help_width;		/* display parameter */
    103 	int input_dialog_width; /* display parameter */
    104 	int line_numbers;	/* runtime value */
    105 	int line_numbers_width;
    106 	struct BufferLine* paste;
    107 	ssize_t paste_count;
    108 	ssize_t paste_size;
    109 	int (*prompt_callback)(struct DrawState*, struct tb_event*);
    110 	int (*input_callback)(struct DrawState*);
    111 	int (*input_update_callback)(struct DrawState*);
    112 	int running;
    113 	int dirty;
    114 	int new_file;
    115 	int recalculate_size;
    116 	int mode;
    117 	int maxx;
    118 	int maxy;
    119 	struct Coord sel_start; /* buffer coordinate */
    120 	struct Coord sel_end;	/* buffer coordinate */
    121 	int backup_possible;	/* is saving backup possible? */
    122 };
    123 
    124 struct Key {
    125 	struct tb_event ev;
    126 	int (*callback)(struct DrawState*);
    127 };
    128 
    129 struct StatusSegment {
    130 	const char* format;
    131 	void (*callback)(char*, ssize_t, const char*, const ssize_t,
    132 		const struct DrawState*);
    133 	ColorIndex color_index;
    134 	Alignment alignment;
    135 };
    136 
    137 void assign_coord(struct Coord* dest, const struct Coord src);
    138 ssize_t buffer_length(uint32_t* buffer, const ssize_t max_index);
    139 int calculate_help_width(const struct DrawState* state);
    140 int calculate_input_dialog_width(const struct DrawState* state);
    141 ssize_t display_length(uint32_t* buffer, const ssize_t max_index);
    142 int draw(const struct DrawState* state);
    143 void draw_box(const int startx, const int endx, const int starty,
    144 	const int endy, const uint16_t fg, const uint16_t bg);
    145 int draw_help(const struct DrawState* state);
    146 int draw_input_dialog(const struct DrawState* state);
    147 void draw_line_numbers(const struct DrawState* state);
    148 void draw_rect(const int startx, const int endx, const int starty,
    149 	const int endy, const uint16_t fg, const uint16_t bg);
    150 int draw_screen(const struct DrawState* state);
    151 int draw_status(const struct DrawState* state);
    152 int draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg,
    153 	const char* s, const int max_cols, const int fill_cols,
    154 	const int padding_left, const int padding_right, const Alignment align,
    155 	const int ds, const int fill, const uint32_t* srch, const uint16_t sfg,
    156 	const uint16_t sbg, const int ssi, const int sei, const uint16_t slfg,
    157 	const uint16_t slbg);
    158 void format_codep(char* result, ssize_t result_size, const char* format,
    159 	const ssize_t seg_size, const struct DrawState* state);
    160 void format_filename(char* result, ssize_t result_size, const char* format,
    161 	const ssize_t seg_size, const struct DrawState* state);
    162 void format_mode(char* result, ssize_t result_size, const char* format,
    163 	const ssize_t seg_size, const struct DrawState* state);
    164 void format_position(char* result, ssize_t result_size, const char* format,
    165 	const ssize_t seg_size, const struct DrawState* state);
    166 void free_bufferline(struct BufferLine* bl);
    167 void free_drawstate(struct DrawState* state);
    168 void init_bufferline(struct BufferLine* bl);
    169 void init_coord(struct Coord* c, const int col, const int row);
    170 void init_drawstate(struct DrawState* state, char* filename, char* prompt,
    171 	const int skip_paste_and_mode_init, const int skip_filenames_init);
    172 void recalculate_line_numbers(struct DrawState* state);
    173 void recalculate_size(struct DrawState* state);
    174 void redraw_status(struct DrawState* state);
    175 void u32_draw_string(const int x, const int y, const uint16_t fg,
    176 	const uint16_t bg, const uint32_t* s, const int max_cols,
    177 	const int fill_cols, const int padding_left, const int padding_right,
    178 	const Alignment align, const int ds, const int fill,
    179 	const uint32_t* srch, const uint16_t sfg, const uint16_t sbg,
    180 	const int ssi, const int sei, const uint16_t slfg, const uint16_t slbg);