C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
ui_ncurses.c File Reference

Ncurses-based UI implementation. More...

#include "../include/cm.h"
#include "ui_ncurses_internal.h"
#include <stdlib.h>
#include <string.h>
Include dependency graph for ui_ncurses.c:

Go to the source code of this file.

Functions

int ui_bkgrnd (UiSurface *s, const UiStyle *style, const char *c)
int ui_bkgrnd_set (UiSurface *s, const UiStyle *style, const char *c)
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.
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_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.
void ui_style_destroy (UiStyle *style)
UiStyleui_style_from_cch (const cchar_t *cch)
UiStyleui_style_new ()
cchar_t ui_style_to_cch (const UiStyle *style, const char *c)
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_show (UiSurface *s)
 Show a UI surface.
int ui_suspend (UiRuntime *ui)
 Suspend the UI, restoring the terminal to its normal state.

Variables

UiSurfaceui_surface_box [MAXWIN]
UiSurfaceui_surface_win [MAXWIN]
UiSurfaceui_surface_win2 [MAXWIN]

Detailed Description

Ncurses-based UI implementation.

This file implements the UI runtime and surface management using the ncurses library. It provides functions for creating and managing surfaces, handling input, and rendering the UI. The implementation is designed to be modular and can be extended to support additional features as needed.

Definition in file ui_ncurses.c.

Function Documentation

◆ ui_bkgrnd()

int ui_bkgrnd ( UiSurface * s,
const UiStyle * style,
const char * c )

Definition at line 330 of file ui_ncurses.c.

330 {
331 if (!s)
332 return -1;
333 cchar_t cch = ui_style_to_cch(style, c);
334 wbkgrnd(s->win, &cch);
335 return 0;
336}
cchar_t ui_style_to_cch(const UiStyle *style, const char *c)
Definition ui_ncurses.c:393

References ui_style_to_cch(), and UiSurface::win.

Here is the call graph for this function:

◆ ui_bkgrnd_set()

int ui_bkgrnd_set ( UiSurface * s,
const UiStyle * style,
const char * c )

Definition at line 338 of file ui_ncurses.c.

338 {
339 if (!s)
340 return -1;
341 cchar_t cch = ui_style_to_cch(style, c);
342 wbkgrndset(s->win, &cch);
343 return 0;
344}

References ui_style_to_cch(), and UiSurface::win.

Here is the call graph for this function:

◆ ui_style_destroy()

void ui_style_destroy ( UiStyle * style)

Definition at line 387 of file ui_ncurses.c.

387 {
388 if (!style)
389 return;
390 free(style);
391}

◆ ui_style_from_cch()

UiStyle * ui_style_from_cch ( const cchar_t * cch)

Definition at line 345 of file ui_ncurses.c.

345 {
346 UiStyle *style = calloc(1, sizeof(*style));
347 if (!style)
348 return NULL;
349 wchar_t wc[2] = {L'\0', L'\0'};
350 attr_t attrs;
351 short cpx;
352 int fg, bg;
353 RGB rgb;
354 getcchar(cch, wc, &attrs, &cpx, nullptr);
355 style->bold = (attrs & WA_BOLD) != 0;
356 style->dim = (attrs & WA_DIM) != 0;
357 style->italic = (attrs & WA_ITALIC) != 0;
358 style->underline = (attrs & WA_UNDERLINE) != 0;
359 style->blink = (attrs & WA_BLINK) != 0;
360 style->reverse = (attrs & WA_REVERSE) != 0;
361 style->invis = (attrs & WA_INVIS) != 0;
362 extended_pair_content(cpx, &fg, &bg);
363 extended_color_content(fg, &rgb.r, &rgb.g, &rgb.b);
364 style->fg.r = (rgb.r * 255) / 1000;
365 style->fg.g = (rgb.g * 255) / 1000;
366 style->fg.b = (rgb.b * 255) / 1000;
367 extended_color_content(bg, &rgb.r, &rgb.g, &rgb.b);
368 style->bg.r = (rgb.r * 255) / 1000;
369 style->bg.g = (rgb.g * 255) / 1000;
370 style->bg.b = (rgb.b * 255) / 1000;
371 return style;
372}
Definition cm.h:381
int r
Definition cm.h:382
int b
Definition cm.h:382
int g
Definition cm.h:382
uint8_t g
Definition ui_backend.h:134
uint8_t r
Definition ui_backend.h:135
uint8_t b
Definition ui_backend.h:133
Structure representing the style attributes for UI elements.
Definition ui_backend.h:158
bool dim
Definition ui_backend.h:162
UiColor bg
Definition ui_backend.h:160
bool underline
Definition ui_backend.h:164
bool blink
Definition ui_backend.h:165
UiColor fg
Definition ui_backend.h:159
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 RGB::b, UiStyle::bg, UiStyle::blink, UiStyle::bold, UiStyle::dim, UiStyle::fg, RGB::g, UiStyle::invis, UiStyle::italic, RGB::r, UiStyle::reverse, and UiStyle::underline.

◆ ui_style_new()

UiStyle * ui_style_new ( )

Definition at line 374 of file ui_ncurses.c.

374 {
375 UiStyle *style = calloc(1, sizeof(*style));
376 if (!style)
377 return NULL;
378 style->fg.r = 255;
379 style->fg.g = 255;
380 style->fg.b = 255;
381 style->bg.r = 0;
382 style->bg.g = 0;
383 style->bg.b = 0;
384 return style;
385}

References UiStyle::bg, and UiStyle::fg.

◆ ui_style_to_cch()

cchar_t ui_style_to_cch ( const UiStyle * style,
const char * c )

Definition at line 393 of file ui_ncurses.c.

393 {
394 int fg, bg;
395 attr_t attrs = 0;
396 uint32_t cpx = 0;
397 mbstate_t mbstate;
398 memset(&mbstate, 0, sizeof(mbstate));
399 attrs |= style->bold ? WA_BOLD : 0;
400 attrs |= style->dim ? WA_DIM : 0;
401 attrs |= style->italic ? WA_ITALIC : 0;
402 attrs |= style->underline ? WA_UNDERLINE : 0;
403 attrs |= style->blink ? WA_BLINK : 0;
404 attrs |= style->reverse ? WA_REVERSE : 0;
405 attrs |= style->invis ? WA_INVIS : 0;
406 RGB rgb = (RGB){style->fg.r, style->fg.g, style->fg.b};
407 fg = rgb_to_curses_clr(&rgb);
408 rgb = (RGB){style->bg.r, style->bg.g, style->bg.b};
409 bg = rgb_to_curses_clr(&rgb);
410 cpx = get_clr_pair(fg, bg);
411 const cchar_t cch = mkcc(cpx, attrs, c);
412 return cch;
413}
int rgb_to_curses_clr(RGB *)
Get color index for RGB color.
Definition dwin.c:432
cchar_t mkcc(int, attr_t, const char *)
Create a cchar_t with the specified color pair index.
Definition dwin.c:748
int get_clr_pair(int, int)
Get color pair index for foreground and background colors.
Definition dwin.c:401

References UiStyle::bg, UiStyle::blink, UiStyle::bold, UiStyle::dim, UiStyle::fg, get_clr_pair(), UiStyle::invis, UiStyle::italic, mkcc(), UiStyle::reverse, rgb_to_curses_clr(), and UiStyle::underline.

Referenced by ui_bkgrnd(), and ui_bkgrnd_set().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ ui_surface_box

UiSurface* ui_surface_box[MAXWIN]

Definition at line 26 of file ui_ncurses.c.

◆ ui_surface_win

UiSurface* ui_surface_win[MAXWIN]

Definition at line 27 of file ui_ncurses.c.

◆ ui_surface_win2

UiSurface* ui_surface_win2[MAXWIN]

Definition at line 28 of file ui_ncurses.c.