C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
Ncurses UI Implementation

UI implementation using the ncurses library. More...

Files

file  ui_ncurses.c
 Ncurses-based UI implementation.
file  ui_ncurses_draw.c
 Implementation of drawing functions for the ncurses UI backend.
file  ui_ncurses_input.c
 Input handling for ncurses-based UI.

Functions

int ui_clear_screen (UiRuntime *ui)
 Clear the entire screen.
int ui_cursor_enable (UiRuntime *ui, bool visible)
 Enable or disable the cursor visibility.
int ui_cursor_move (UiSurface *s, int y, int x)
 Move the cursor within a UI surface.
int ui_draw_border (UiSurface *s, UiBorderKind kind, const UiStyle *style)
 Draw a border around a UiSurface.This function draws a border around the UiSurface's window based on the specified UiBorderKind. It uses the ncurses box function to draw the border with the appropriate characters. If a style is provided, it applies that style before drawing the border.
int ui_draw_box_title (UiSurface *s, int x, const UiStyle *style, const char *title)
 Draw a title on the top border of a UiSurface.This function draws the provided title string at the specified x-coordinate on the top border of the UiSurface's window. It uses the ncurses mvwaddstr function to place the title. If a style is provided, it applies that style before drawing the title.
int ui_draw_hline (UiSurface *s, int y, int x, int len, const UiStyle *style)
 Draw a horizontal line on a UiSurface.This function uses the ncurses mvwhline function to draw a horizontal line of a specified length at the given (y, x) coordinates on the UiSurface's window. If a style is provided, it applies that style before drawing the line.
int ui_draw_text (UiSurface *s, int y, int x, const UiStyle *style, const char *text)
 Draw text on a UiSurface at a specified position with a given style.This function uses the ncurses mvwaddstr function to draw the provided text at the specified (y, x) coordinates on the UiSurface's window. If a style is provided, it applies that style before drawing the text.
int ui_draw_text_n (UiSurface *s, int y, int x, const UiStyle *style, const char *text, size_t n)
 Draw a specified number of characters from a string on a UiSurface.This function is similar to ui_draw_text but allows specifying the number of characters to draw from the provided string. It uses the ncurses mvwaddnstr function to achieve this. If a style is provided, it applies that style before drawing the text.
int ui_draw_vline (UiSurface *s, int y, int x, int len, const UiStyle *style)
 Draw a vertical line on a UiSurface.This function uses the ncurses mvwvline function to draw a vertical line of a specified length at the given (y, x) coordinates on the UiSurface's window. If a style is provided, it applies that style before drawing the line.
int ui_get_event (UiRuntime *ui, UiSurface *target, UiEvent *ev, int timeout_ms)
 Retrieves an input event from the specified UI surface.
void ui_get_screen_size (UiRuntime *ui, int *rows, int *cols)
 Get the current screen size.
UiRuntimeui_init (const UiConfig *cfg)
 Initialize NCUrses.
int ui_ncurses_color_pair_from_style (const UiStyle *style)
 Convert an RGB value (0-255) to a 1000-based value for ncurses.Ncurses uses a 1000-based color system, so we need to convert our 0-255 RGB values to that scale.
int ui_ncurses_style_apply (WINDOW *win, const UiStyle *style)
 Apply a UiStyle to an ncurses window.This function sets the appropriate attributes (bold, underline, reverse) on the given ncurses window based on the properties of the UiStyle.
int ui_render (UiRuntime *ui)
 Render the UI by updating panels and refreshing the screen.
int ui_resume (UiRuntime *ui)
 Resume the UI after being suspended, reinitializing the screen.
void ui_shutdown (UiRuntime *ui)
 Shutdown the UI runtime and free its resources.
int ui_surface_clear (UiSurface *s)
 Clear the contents of a UI surface.
void ui_surface_destroy (UiSurface *s)
 Destroy a UI surface and free its resources.
int ui_surface_erase (UiSurface *s)
 Erase the contents of a UI surface.
int ui_surface_hide (UiSurface *s)
 Hide a UI surface.
int ui_surface_move (UiSurface *s, int y, int x)
 Move a UI surface to a new position.
UiSurfaceui_surface_new (UiRuntime *ui, UiSurface *parent, UiRect rect)
 Create a new UI surface.
int ui_surface_resize (UiSurface *s, int rows, int cols)
 Resize a UI surface to new dimensions.
int ui_surface_set_base (UiSurface *s, const UiStyle *style, uint32_t fill_ch)
 Set the base fill character and style for a UiSurface.This function sets the background fill character for the UiSurface's ncurses window and applies the given UiStyle. If fill_ch is 0, it defaults to a space character.
int ui_surface_set_style (UiSurface *s, const UiStyle *style)
 Set the style for a UiSurface.This function applies the given UiStyle to the ncurses window associated with the UiSurface. It uses the ui_ncurses_style_apply function to set the appropriate attributes on the window.
int ui_surface_show (UiSurface *s)
 Show a UI surface.
int ui_suspend (UiRuntime *ui)
 Suspend the UI, restoring the terminal to its normal state.

Detailed Description

UI implementation using the ncurses library.

This module provides functions for creating and managing UI surfaces, handling input, and rendering the UI using ncurses. It is designed to be used as part of a larger UI framework and can be extended with additional features as needed.

Function Documentation

◆ ui_clear_screen()

int ui_clear_screen ( UiRuntime * ui)

Clear the entire screen.

Parameters
uiThe UiRuntime instance.
Returns
0 on success, or -1 on failure.

Definition at line 284 of file ui_ncurses.c.

284 {
285 (void)ui;
286 erase();
287 return 0;
288}

◆ ui_cursor_enable()

int ui_cursor_enable ( UiRuntime * ui,
bool visible )

Enable or disable the cursor visibility.

Parameters
uiThe UiRuntime instance.
visibletrue to show the cursor, false to hide it.
Returns
0 on success, or -1 on failure.

Definition at line 322 of file ui_ncurses.c.

322 {
323 if (!ui)
324 return -1;
325 ui->cursor_visible = visible;
326 curs_set(visible ? 1 : 0);
327 return 0;
328}

References UiRuntime::cursor_visible.

◆ ui_cursor_move()

int ui_cursor_move ( UiSurface * s,
int y,
int x )

Move the cursor within a UI surface.

Parameters
sThe UiSurface to move the cursor in.
yThe new y-coordinate of the cursor.
xThe new x-coordinate of the cursor.
Returns
0 on success, or -1 on failure.

Definition at line 170 of file ui_ncurses.c.

170 {
171 if (!s)
172 return -1;
173 return wmove(s->win, y, x);
174}

References UiSurface::win.

◆ ui_draw_border()

int ui_draw_border ( UiSurface * s,
UiBorderKind kind,
const UiStyle * style )

Draw a border around a UiSurface.This function draws a border around the UiSurface's window based on the specified UiBorderKind. It uses the ncurses box function to draw the border with the appropriate characters. If a style is provided, it applies that style before drawing the border.

Parameters
sThe UiSurface on which to draw the border.
kindThe kind of border to draw (e.g., none, ASCII, light, rounded).
styleThe UiStyle to apply to the border (can be NULL for default).
Returns
0 on success, -1 on failure (e.g., if s is NULL).

Definition at line 194 of file ui_ncurses_draw.c.

194 {
195 if (!s)
196 return -1;
197 if (style)
198 ui_ncurses_style_apply(s->win, style);
199
200 switch (kind) {
201 case UI_BORDER_NONE:
202 return 0;
203 case UI_BORDER_ASCII:
204 box(s->win, '|', '-');
205 return 0;
206 case UI_BORDER_LIGHT:
208 default:
209 box(s->win, 0, 0);
210 return 0;
211 }
212}
@ UI_BORDER_ROUNDED
Definition ui_backend.h:119
@ UI_BORDER_ASCII
Definition ui_backend.h:117
@ UI_BORDER_LIGHT
Definition ui_backend.h:118
@ UI_BORDER_NONE
Definition ui_backend.h:116
int ui_ncurses_style_apply(WINDOW *win, const UiStyle *style)
Apply a UiStyle to an ncurses window.This function sets the appropriate attributes (bold,...

References UI_BORDER_ASCII, UI_BORDER_LIGHT, UI_BORDER_NONE, UI_BORDER_ROUNDED, ui_ncurses_style_apply(), and UiSurface::win.

Here is the call graph for this function:

◆ ui_draw_box_title()

int ui_draw_box_title ( UiSurface * s,
int x,
const UiStyle * style,
const char * title )

Draw a title on the top border of a UiSurface.This function draws the provided title string at the specified x-coordinate on the top border of the UiSurface's window. It uses the ncurses mvwaddstr function to place the title. If a style is provided, it applies that style before drawing the title.

Parameters
sThe UiSurface on which to draw the box title.
xThe x-coordinate (column) where the title should start.
styleThe UiStyle to apply to the title (can be NULL for default).
titleThe null-terminated string to use as the box title.
Returns
0 on success, -1 on failure (e.g., if s or title is NULL).

Definition at line 226 of file ui_ncurses_draw.c.

226 {
227 if (!s || !title)
228 return -1;
229 if (style)
230 ui_ncurses_style_apply(s->win, style);
231 mvwaddstr(s->win, 0, x, title);
232 return 0;
233}

References ui_ncurses_style_apply(), and UiSurface::win.

Here is the call graph for this function:

◆ ui_draw_hline()

int ui_draw_hline ( UiSurface * s,
int y,
int x,
int len,
const UiStyle * style )

Draw a horizontal line on a UiSurface.This function uses the ncurses mvwhline function to draw a horizontal line of a specified length at the given (y, x) coordinates on the UiSurface's window. If a style is provided, it applies that style before drawing the line.

Parameters
sThe UiSurface on which to draw the horizontal line.
yThe y-coordinate (row) where the line should be drawn.
xThe x-coordinate (column) where the line should start.
lenThe length of the horizontal line to draw.
styleThe UiStyle to apply to the line (can be NULL for default).
Returns
0 on success, -1 on failure (e.g., if s is NULL).

Definition at line 153 of file ui_ncurses_draw.c.

153 {
154 if (!s)
155 return -1;
156 if (style)
157 ui_ncurses_style_apply(s->win, style);
158 mvwhline(s->win, y, x, 0, len);
159 return 0;
160}

References ui_ncurses_style_apply(), and UiSurface::win.

Here is the call graph for this function:

◆ ui_draw_text()

int ui_draw_text ( UiSurface * s,
int y,
int x,
const UiStyle * style,
const char * text )

Draw text on a UiSurface at a specified position with a given style.This function uses the ncurses mvwaddstr function to draw the provided text at the specified (y, x) coordinates on the UiSurface's window. If a style is provided, it applies that style before drawing the text.

Parameters
sThe UiSurface on which to draw the text.
yThe y-coordinate (row) where the text should be drawn.
xThe x-coordinate (column) where the text should be drawn.
styleThe UiStyle to apply to the text (can be NULL for default).
textThe null-terminated string to draw.
Returns
0 on success, -1 on failure (e.g., if s or text is NULL).

Definition at line 109 of file ui_ncurses_draw.c.

109 {
110 if (!s || !text)
111 return -1;
112 if (style)
113 ui_ncurses_style_apply(s->win, style);
114 mvwaddstr(s->win, y, x, text);
115 return 0;
116}

References ui_ncurses_style_apply(), and UiSurface::win.

Here is the call graph for this function:

◆ ui_draw_text_n()

int ui_draw_text_n ( UiSurface * s,
int y,
int x,
const UiStyle * style,
const char * text,
size_t n )

Draw a specified number of characters from a string on a UiSurface.This function is similar to ui_draw_text but allows specifying the number of characters to draw from the provided string. It uses the ncurses mvwaddnstr function to achieve this. If a style is provided, it applies that style before drawing the text.

Parameters
sThe UiSurface on which to draw the text.
yThe y-coordinate (row) where the text should be drawn.
xThe x-coordinate (column) where the text should be drawn.
styleThe UiStyle to apply to the text (can be NULL for default).
textThe string from which to draw characters (not necessarily null-terminated).
nThe number of characters to draw from the string.
Returns
0 on success, -1 on failure (e.g., if s or text is NULL).

Definition at line 132 of file ui_ncurses_draw.c.

132 {
133 if (!s || !text)
134 return -1;
135 if (style)
136 ui_ncurses_style_apply(s->win, style);
137 mvwaddnstr(s->win, y, x, text, (int)n);
138 return 0;
139}

References ui_ncurses_style_apply(), and UiSurface::win.

Here is the call graph for this function:

◆ ui_draw_vline()

int ui_draw_vline ( UiSurface * s,
int y,
int x,
int len,
const UiStyle * style )

Draw a vertical line on a UiSurface.This function uses the ncurses mvwvline function to draw a vertical line of a specified length at the given (y, x) coordinates on the UiSurface's window. If a style is provided, it applies that style before drawing the line.

Parameters
sThe UiSurface on which to draw the vertical line.
yThe y-coordinate (row) where the line should start.
xThe x-coordinate (column) where the line should be drawn.
lenThe length of the vertical line to draw.
styleThe UiStyle to apply to the line (can be NULL for default).
Returns
0 on success, -1 on failure (e.g., if s is NULL).

Definition at line 174 of file ui_ncurses_draw.c.

174 {
175 if (!s)
176 return -1;
177 if (style)
178 ui_ncurses_style_apply(s->win, style);
179 mvwvline(s->win, y, x, 0, len);
180 return 0;
181}

References ui_ncurses_style_apply(), and UiSurface::win.

Here is the call graph for this function:

◆ ui_get_event()

int ui_get_event ( UiRuntime * ui,
UiSurface * target,
UiEvent * ev,
int timeout_ms )

Retrieves an input event from the specified UI surface.

This function waits for an input event on the given UI surface (or the standard screen if no surface is specified) and fills the provided UiEvent structure with the details of the event. It handles both keyboard and mouse events.

Parameters
uiThe UI runtime context (unused in this implementation).
targetThe UI surface to monitor for events, or NULL to use stdscr.
evA pointer to a UiEvent structure to be filled with event details.
timeout_msThe maximum time to wait for an event in milliseconds, or -1 to wait indefinitely.
Returns
0 on success, or -1 on error (e.g., if ev is NULL).

Definition at line 108 of file ui_ncurses_input.c.

108 {
109 int ch;
110 WINDOW *win;
111
112 (void)ui;
113 if (!ev)
114 return -1;
115 memset(ev, 0, sizeof(*ev));
116
117 win = target ? target->win : stdscr;
118
119 if (timeout_ms < 0) {
120 wtimeout(win, -1);
121 } else {
122 wtimeout(win, timeout_ms);
123 }
124
125 ch = wgetch(win);
126 ev->key = translate_key(ch);
127
128 if (ev->key == UI_KEY_CHAR) {
129 ev->ch = (uint32_t)ch;
130 } else if (ev->key == UI_KEY_MOUSE) {
131 MEVENT me;
132 if (getmouse(&me) == OK) {
133 ev->y = me.y;
134 ev->x = me.x;
135#ifdef BUTTON4_PRESSED
136 if (me.bstate & BUTTON4_PRESSED) {
138#endif
139#ifdef BUTTON5_PRESSED
140 } else if (me.bstate & BUTTON5_PRESSED) {
142#endif
143 } else if (me.bstate & BUTTON1_PRESSED) {
145 } else if (me.bstate & BUTTON1_RELEASED) {
147 }
148 }
149 }
150
151 return 0;
152}
@ UI_KEY_CHAR
Definition ui_backend.h:60
@ UI_KEY_MOUSE
Definition ui_backend.h:77
@ UI_MOUSE_SCROLL_DOWN
Definition ui_backend.h:105
@ UI_MOUSE_PRESS
Definition ui_backend.h:101
@ UI_MOUSE_SCROLL_UP
Definition ui_backend.h:104
@ UI_MOUSE_RELEASE
Definition ui_backend.h:102
uint32_t ch
Definition ui_backend.h:180
UiMouseAction mouse_action
Definition ui_backend.h:186
UiKey key
Definition ui_backend.h:179

References UiEvent::ch, UiEvent::key, UiEvent::mouse_action, UI_KEY_CHAR, UI_KEY_MOUSE, UI_MOUSE_PRESS, UI_MOUSE_RELEASE, UI_MOUSE_SCROLL_DOWN, UI_MOUSE_SCROLL_UP, UiSurface::win, UiEvent::x, and UiEvent::y.

◆ ui_get_screen_size()

void ui_get_screen_size ( UiRuntime * ui,
int * rows,
int * cols )

Get the current screen size.

Parameters
uiThe UiRuntime instance.
rowsOutput parameter for the number of rows.
colsOutput parameter for the number of columns.

Definition at line 257 of file ui_ncurses.c.

257 {
258 if (!ui)
259 return;
260 getmaxyx(stdscr, ui->rows, ui->cols);
261 if (rows)
262 *rows = ui->rows;
263 if (cols)
264 *cols = ui->cols;
265}

References UiRuntime::cols, and UiRuntime::rows.

Referenced by bare_box_new().

Here is the caller graph for this function:

◆ ui_init()

UiRuntime * ui_init ( const UiConfig * cfg)

Initialize NCUrses.

Parameters
cfgThe UI runtime instance.
Returns
0 on success, or -1 on failure.

Definition at line 180 of file ui_ncurses.c.

180 {
181 UiRuntime *ui = calloc(1, sizeof(*ui));
182 if (!ui)
183 return NULL;
184 char tty_name[XLEN];
185
186 // Get the name of the terminal device
187 if (ttyname_r(STDERR_FILENO, tty_name, sizeof(tty_name)) != 0) {
188 Perror("ui_init: ttyname_r() failed");
189 exit(EXIT_FAILURE);
190 }
191 // open the terminal device for NCurses input and output
192 tty_fp = fopen(tty_name, "r+");
193 if (tty_fp == nullptr) {
194 Perror("ui_init: fopen() failed");
195 exit(EXIT_FAILURE);
196 }
197 // newterm() allows us to specify a tty device for NCurses input and
198 // output. nullptr for the first argument means to use the default terminal type.
199 screen = newterm(nullptr, tty_fp, tty_fp);
200 if (screen == nullptr) {
201 Perror("ui_init: newterm() failed");
202 exit(EXIT_FAILURE);
203 }
204 set_term(screen);
205 f_curses_open = true;
206 if (!has_colors()) {
207 ui_shutdown(ui);
208 Perror("Terminal does not support colors");
209 exit(EXIT_FAILURE);
210 }
211 if (!can_change_color()) {
212 ui_shutdown(ui);
213 Perror("Terminal cannot change colors");
214 exit(EXIT_FAILURE);
215 }
216 start_color();
217 use_default_colors();
218 cbreak();
219 noecho();
220 keypad(stdscr, TRUE);
221 if (cfg) {
222 ui->mouse_enabled = cfg->enable_mouse;
223 ui->alt_screen = cfg->enable_alt_screen;
225 } else {
226 ui->cursor_visible = true;
227 }
228 if (ui->mouse_enabled)
229 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
230 curs_set(ui->cursor_visible ? 1 : 0);
231 getmaxyx(stdscr, ui->rows, ui->cols);
232 win_ptr = -1;
233 return ui;
234}
bool f_curses_open
Definition sig.c:33
SCREEN * screen
Definition dwin.c:42
int win_ptr
Definition dwin.c:164
FILE * tty_fp
Definition dwin.c:43
#define XLEN
#define TRUE
Definition iloan.c:19
int Perror(char *)
Display a simple error message window or print to stderr.
Definition dwin.c:1526
void ui_shutdown(UiRuntime *ui)
Shutdown the UI runtime and free its resources.
Definition ui_ncurses.c:240
bool cursor_visible
Definition ui_backend.h:214
bool enable_mouse
Definition ui_backend.h:212
bool enable_alt_screen
Definition ui_backend.h:213
Opaque structure representing the UI runtime environment.

References UiRuntime::alt_screen, UiRuntime::cols, UiConfig::cursor_visible, UiRuntime::cursor_visible, UiConfig::enable_alt_screen, UiConfig::enable_mouse, f_curses_open, UiRuntime::mouse_enabled, Perror(), UiRuntime::rows, screen, tty_fp, ui_shutdown(), and win_ptr.

Here is the call graph for this function:

◆ ui_ncurses_color_pair_from_style()

int ui_ncurses_color_pair_from_style ( const UiStyle * style)

Convert an RGB value (0-255) to a 1000-based value for ncurses.Ncurses uses a 1000-based color system, so we need to convert our 0-255 RGB values to that scale.

Parameters
vThe RGB value (0-255).
Returns
The corresponding 1000-based value for ncurses.

Convert a UiStyle to an ncurses color pair index.

This function maps the foreground and background colors from a UiStyle to an ncurses color pair index. For simplicity, this example assumes a limited set of colors and does not handle all possible combinations.

Parameters
styleThe UiStyle containing the foreground and background colors.
Returns
The ncurses color pair index corresponding to the style.

Definition at line 32 of file ui_ncurses_draw.c.

32 {
33 (void)style;
34 return 0;
35}

◆ ui_ncurses_style_apply()

int ui_ncurses_style_apply ( WINDOW * win,
const UiStyle * style )

Apply a UiStyle to an ncurses window.This function sets the appropriate attributes (bold, underline, reverse) on the given ncurses window based on the properties of the UiStyle.

Parameters
winThe ncurses window to which the style should be applied.
styleThe UiStyle containing the attributes to apply.
Returns
0 on success, -1 on failure (e.g., if win or style is NULL).

Definition at line 45 of file ui_ncurses_draw.c.

45 {
46 if (!win || !style)
47 return -1;
48 wchar_t wstr[2] = {L'\0', L'\0'};
49 cchar_t cc = {0};
50 attr_t attrs = 0;
51 uint32_t cpx = 0;
52 attrs |= style->bold ? WA_BOLD : 0;
53 attrs |= style->dim ? WA_DIM : 0;
54 attrs |= style->italic ? WA_ITALIC : 0;
55 attrs |= style->underline ? WA_UNDERLINE : 0;
56 attrs |= style->blink ? WA_BLINK : 0;
57 attrs |= style->reverse ? WA_REVERSE : 0;
58 attrs |= style->invis ? WA_INVIS : 0;
59 setcchar(&cc, wstr, attrs, cpx, nullptr);
60 return 0;
61}
bool dim
Definition ui_backend.h:162
bool underline
Definition ui_backend.h:164
bool blink
Definition ui_backend.h:165
bool invis
Definition ui_backend.h:167
bool italic
Definition ui_backend.h:163
bool reverse
Definition ui_backend.h:166
bool bold
Definition ui_backend.h:161

References UiStyle::blink, UiStyle::bold, UiStyle::dim, UiStyle::invis, UiStyle::italic, UiStyle::reverse, and UiStyle::underline.

Referenced by ui_draw_border(), ui_draw_box_title(), ui_draw_hline(), ui_draw_text(), ui_draw_text_n(), ui_draw_vline(), ui_surface_set_base(), and ui_surface_set_style().

Here is the caller graph for this function:

◆ ui_render()

int ui_render ( UiRuntime * ui)

Render the UI by updating panels and refreshing the screen.

Parameters
uiThe UiRuntime instance.
Returns
0 on success, or -1 on failure.

Definition at line 272 of file ui_ncurses.c.

272 {
273 (void)ui;
274 update_panels();
275 doupdate();
276 return 0;
277}

◆ ui_resume()

int ui_resume ( UiRuntime * ui)

Resume the UI after being suspended, reinitializing the screen.

Parameters
uiThe UiRuntime instance.
Returns
0 on success, or -1 on failure.

Definition at line 307 of file ui_ncurses.c.

307 {
308 (void)ui;
309 reset_prog_mode();
310 refresh();
311 update_panels();
312 doupdate();
313 return 0;
314}

◆ ui_shutdown()

void ui_shutdown ( UiRuntime * ui)

Shutdown the UI runtime and free its resources.

Parameters
uiThe UiRuntime instance to shutdown.

Definition at line 240 of file ui_ncurses.c.

240 {
241 if (!ui)
242 return;
243 // ui_surface_destroy (ui->surface[0]);
244 endwin();
245 delscreen(screen);
246 fclose(tty_fp);
247 f_curses_open = false;
248 free(ui);
249}

References f_curses_open, screen, and tty_fp.

Referenced by ui_init().

Here is the caller graph for this function:

◆ ui_surface_clear()

int ui_surface_clear ( UiSurface * s)

Clear the contents of a UI surface.

Parameters
sThe UiSurface to clear.
Returns
0 on success, or -1 on failure.

Definition at line 119 of file ui_ncurses.c.

119 {
120 if (!s)
121 return -1;
122 wclear(s->win);
123 return 0;
124}

References UiSurface::win.

◆ ui_surface_destroy()

void ui_surface_destroy ( UiSurface * s)

Destroy a UI surface and free its resources.

Parameters
sThe UiSurface to destroy.

Definition at line 74 of file ui_ncurses.c.

74 {
75 if (!s)
76 return;
77 if (s->pan)
78 del_panel(s->pan);
79 if (s->win)
80 delwin(s->win);
81 free(s);
82}

References UiSurface::pan, and UiSurface::win.

◆ ui_surface_erase()

int ui_surface_erase ( UiSurface * s)

Erase the contents of a UI surface.

Parameters
sThe UiSurface to erase.
Returns
0 on success, or -1 on failure.

Definition at line 131 of file ui_ncurses.c.

131 {
132 if (!s)
133 return -1;
134 werase(s->win);
135 return 0;
136}

References UiSurface::win.

◆ ui_surface_hide()

int ui_surface_hide ( UiSurface * s)

Hide a UI surface.

Parameters
sThe UiSurface to hide.
Returns
0 on success, or -1 on failure.

Definition at line 155 of file ui_ncurses.c.

155 {
156 if (!s)
157 return -1;
158 hide_panel(s->pan);
159 s->hidden = true;
160 return 0;
161}

References UiSurface::hidden, and UiSurface::pan.

◆ ui_surface_move()

int ui_surface_move ( UiSurface * s,
int y,
int x )

Move a UI surface to a new position.

Parameters
sThe UiSurface to move.
yThe new y-coordinate of the surface.
xThe new x-coordinate of the surface.
Returns
0 on success, or -1 on failure.

Definition at line 91 of file ui_ncurses.c.

91 {
92 if (!s)
93 return -1;
94 s->y = y;
95 s->x = x;
96 return move_panel(s->pan, y, x);
97}

References UiSurface::pan, UiSurface::x, and UiSurface::y.

◆ ui_surface_new()

UiSurface * ui_surface_new ( UiRuntime * ui,
UiSurface * parent,
UiRect rect )

Create a new UI surface.

*

Parameters
uiThe UI runtime instance.
parentThe parent surface, or NULL for a top-level surface.
rectThe rectangle defining the position and size of the surface.
Returns
A pointer to the newly created UiSurface, or NULL on failure.

Definition at line 37 of file ui_ncurses.c.

37 {
38 UiSurface *s = calloc(1, sizeof(*s));
39 if (!s)
40 return NULL;
41
42 s->runtime = ui;
43 s->parent = parent;
44 s->y = rect.y;
45 s->x = rect.x;
46 s->rows = rect.rows;
47 s->cols = rect.cols;
48
49 if (parent && parent->win) {
50 s->win = derwin(parent->win, rect.rows, rect.cols, rect.y, rect.x);
51 } else {
52 s->win = newwin(rect.rows, rect.cols, rect.y, rect.x);
53 }
54
55 if (!s->win) {
56 free(s);
57 return NULL;
58 }
59
60 s->pan = new_panel(s->win);
61 if (!s->pan) {
62 delwin(s->win);
63 free(s);
64 return NULL;
65 }
66
67 return s;
68}
int cols
Definition ui_backend.h:200
int rows
Definition ui_backend.h:199
Opaque structure representing a drawable surface in the UI.
struct UiSurface * parent
struct UiRuntime * runtime

References UiRect::cols, UiSurface::cols, UiSurface::pan, UiSurface::parent, UiRect::rows, UiSurface::rows, UiSurface::runtime, UiSurface::win, UiRect::x, UiSurface::x, UiRect::y, and UiSurface::y.

◆ ui_surface_resize()

int ui_surface_resize ( UiSurface * s,
int rows,
int cols )

Resize a UI surface to new dimensions.

Parameters
sThe UiSurface to resize.
rowsThe new number of rows for the surface.
colsThe new number of columns for the surface.
Returns
0 on success, or -1 on failure.

Definition at line 106 of file ui_ncurses.c.

106 {
107 if (!s)
108 return -1;
109 s->rows = rows;
110 s->cols = cols;
111 return wresize(s->win, rows, cols);
112}

References UiSurface::cols, UiSurface::rows, and UiSurface::win.

◆ ui_surface_set_base()

int ui_surface_set_base ( UiSurface * s,
const UiStyle * style,
uint32_t fill_ch )

Set the base fill character and style for a UiSurface.This function sets the background fill character for the UiSurface's ncurses window and applies the given UiStyle. If fill_ch is 0, it defaults to a space character.

Parameters
sThe UiSurface to set the base for.
styleThe UiStyle to apply to the surface.
fill_chThe character to use for filling the background (0 for space).
Returns
0 on success, -1 on failure (e.g., if s is NULL).

Definition at line 88 of file ui_ncurses_draw.c.

88 {
89 if (!s)
90 return -1;
91 if (style)
92 ui_ncurses_style_apply(s->win, style);
93 wbkgdset(s->win, (chtype)(fill_ch ? fill_ch : ' '));
94 return 0;
95}

References ui_ncurses_style_apply(), and UiSurface::win.

Here is the call graph for this function:

◆ ui_surface_set_style()

int ui_surface_set_style ( UiSurface * s,
const UiStyle * style )

Set the style for a UiSurface.This function applies the given UiStyle to the ncurses window associated with the UiSurface. It uses the ui_ncurses_style_apply function to set the appropriate attributes on the window.

Parameters
sThe UiSurface whose style is to be set.
styleThe UiStyle to apply to the surface.
Returns
0 on success, -1 on failure (e.g., if s or style is NULL).

Definition at line 72 of file ui_ncurses_draw.c.

72 {
73 if (!s || !style)
74 return -1;
75 return ui_ncurses_style_apply(s->win, style);
76}

References ui_ncurses_style_apply(), and UiSurface::win.

Here is the call graph for this function:

◆ ui_surface_show()

int ui_surface_show ( UiSurface * s)

Show a UI surface.

Parameters
sThe UiSurface to show.
Returns
0 on success, or -1 on failure.

Definition at line 143 of file ui_ncurses.c.

143 {
144 if (!s)
145 return -1;
146 show_panel(s->pan);
147 s->hidden = false;
148 return 0;
149}

References UiSurface::hidden, and UiSurface::pan.

◆ ui_suspend()

int ui_suspend ( UiRuntime * ui)

Suspend the UI, restoring the terminal to its normal state.

Parameters
uiThe UiRuntime instance.
Returns
0 on success, or -1 on failure.

Definition at line 295 of file ui_ncurses.c.

295 {
296 (void)ui;
297 def_prog_mode();
298 endwin();
299 return 0;
300}