Skip to content

Commit

Permalink
Added arrst_insert_n0, listbox_del_elem(), bfile_rename(). Fix str_upd()
Browse files Browse the repository at this point in the history
  • Loading branch information
frang75 committed Nov 27, 2024
1 parent 40356b8 commit 50887ca
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 18 deletions.
6 changes: 5 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

### Added

- `.clang-format` file. [Issue](https://github.com/frang75/nappgui_src/issues/161). [Commit]().
- `.clang-format` file. [Issue](https://github.com/frang75/nappgui_src/issues/161). [Commit](https://github.com/frang75/nappgui_src/commit/40356b8b17a7cb1f39c2bdfa3c7d4e8ce6ef35d8).
- `arrst_insert_n0()`. [Doc](). [Commit]().
- `listbox_del_elem()`. [Doc](). [Commit]().
- `bfile_rename()`. [Doc](). [Commit]().

### Fixed

- Issue in `Layout` when window becomes very small. [Commit](https://github.com/frang75/nappgui_src/commit/3d616fa82e072b6c46f4cf196df0516912ab634c).
- Issue in `bmath_prec()`. [Commit](https://github.com/frang75/nappgui_src/commit/062d2a69ea187c2c983b0db02f022a7552295a19).
- Vulnerability in `str_upd()`. [Commit]().

### Improved

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cd nappgui_src
cmake -S . -B build
cmake --build build --config Debug
// Run examples in 'demo' and 'howto' folders
// Run examples in 'demo' folder
.\build\Debug\bin\Die.exe
.\build\Debug\bin\Bricks.exe
.\build\Debug\bin\Products.exe
Expand All @@ -39,7 +39,7 @@ cd nappgui_src
cmake -G Xcode -S . -B build
cmake --build build --config Debug
// Run examples in 'demo' and 'howto' folders
// Run examples in 'demo' folder
./build/Debug/bin/Die.app/Contents/MacOS/Die
./build/Debug/bin/Bricks.app/Contents/MacOS/Bricks
./build/Debug/bin/Products.app/Contents/MacOS/Products
Expand All @@ -59,7 +59,7 @@ sudo apt-get install git
sudo apt-get install cmake
// Development libraries
sudo apt-get install libgtk-3-dev // Mandatory Gui Toolkit
sudo apt-get install libgtk-3-dev // Gui Toolkit
sudo apt-get install libcurl4-openssl-dev // For HTTP support
sudo apt-get install libwebkit2gtk-4.1-dev // For WebView support
sudo apt-get install mesa-common-dev libeg1-mesa-dev // For OpenGL support
Expand All @@ -71,7 +71,7 @@ cd nappgui_src
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build -j 4
// Run examples in 'demo' and 'howto' folders
// Run examples in 'demo' folder
./build/Debug/bin/Die
./build/Debug/bin/Bricks
./build/Debug/bin/Products
Expand Down
2 changes: 1 addition & 1 deletion prj/build.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5584
5597
2 changes: 1 addition & 1 deletion src/core/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ static const void *i_get_ptr_elem(const byte_t *data, const uint32_t elem_id, co
static const void *i_get_str_elem(const byte_t *data, const uint32_t elem_id, const uint32_t esize)
{
cassert_no_null(data);
return *dcast(data + elem_id * esize, void);
return cast(data + elem_id * esize, void);
}

/*---------------------------------------------------------------------------*/
Expand Down
3 changes: 3 additions & 0 deletions src/core/arrst.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
#define arrst_insert_n(array, pos, n, type) \
arrst_##type##_insert(array, pos, n)

#define arrst_insert_n0(array, pos, n, type) \
arrst_##type##_insert0(array, pos, n)

#define arrst_append(array, value, type) \
(*arrst_##type##_insert(array, UINT32_MAX, 1)) = (value)

Expand Down
10 changes: 10 additions & 0 deletions src/core/arrst.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ struct ArrSt

static type *insert_n(ArrSt<type> *array, const uint32_t pos, const uint32_t n);

static type *insert_n0(ArrSt<type> *array, const uint32_t pos, const uint32_t n);

static void append(ArrSt<type> *array, const type value);

static void prepend(ArrSt<type> *array, const type value);
Expand Down Expand Up @@ -302,6 +304,14 @@ type *ArrSt<type>::insert_n(ArrSt<type> *array, const uint32_t pos, const uint32

/*---------------------------------------------------------------------------*/

template <typename type>
type *ArrSt<type>::insert_n0(ArrSt<type> *array, const uint32_t pos, const uint32_t n)
{
return cast(array_insert0(cast(array, Array), pos, n), type);
}

/*---------------------------------------------------------------------------*/

template <typename type>
void ArrSt<type>::append(ArrSt<type> *array, const type value)
{
Expand Down
15 changes: 8 additions & 7 deletions src/core/strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -576,19 +576,20 @@ void str_cat_c(char_t *dest, const uint32_t size, const char_t *src)

void str_upd(String **str, const char_t *new_str)
{
cassert_no_null(str);

if (tc(*str) == new_str)
return;
String *nstr = NULL;

if (*str != NULL)
heap_free(dcast(str, byte_t), i_SIZE(*str) + sizeof32(uint32_t), "String");
cassert_no_null(str);

if (new_str != NULL)
{
uint32_t length = blib_strlen(new_str);
*str = i_create_string(length + 1, new_str);
nstr = i_create_string(length + 1, new_str);
}

if (*str != NULL)
heap_free(dcast(str, byte_t), i_SIZE(*str) + sizeof32(uint32_t), "String");

*str = nstr;
}

/*---------------------------------------------------------------------------*/
Expand Down
18 changes: 17 additions & 1 deletion src/gui/button.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/* Button */

#include "button.h"
#include "buttonh.h"
#include "button.inl"
#include "component.inl"
#include "cell.inl"
Expand All @@ -33,6 +34,7 @@ struct _button_t
GuiComponent component;
uint32_t flags;
S2Df size;
Font *font;
ResId textid;
ResId taltid;
ResId ttipid;
Expand All @@ -52,6 +54,7 @@ void _button_destroy(Button **button)
_component_destroy_imp(&(*button)->component);
listener_destroy(&(*button)->OnClick);
str_destroy(&(*button)->text);
ptr_destopt(font_destroy, &(*button)->font, Font);
ptr_destopt(str_destroy, &(*button)->talt, String);
ptr_destopt(image_destroy, &(*button)->image, Image);
ptr_destopt(image_destroy, &(*button)->imalt, Image);
Expand Down Expand Up @@ -194,7 +197,11 @@ static Button *i_create(const uint32_t flags, const align_t halign)
button->text = str_c("");

if (button_get_type(flags) != ekBUTTON_FLAT && button_get_type(flags) != ekBUTTON_FLATGLE)
{
button->font = _gui_create_default_font();
context->func_button_set_align(button->component.ositem, (enum_t)halign);
context->func_button_set_font(button->component.ositem, button->font);
}

context->func_button_OnClick(button->component.ositem, obj_listener(button, i_OnClick, Button));
return button;
Expand Down Expand Up @@ -309,7 +316,16 @@ void button_tooltip(Button *button, const char_t *text)
void button_font(Button *button, const Font *font)
{
cassert_no_null(button);
button->component.context->func_button_set_font(button->component.ositem, font);
if (_gui_update_font(&button->font, NULL, font) == TRUE)
button->component.context->func_button_set_font(button->component.ositem, button->font);
}

/*---------------------------------------------------------------------------*/

const Font *button_get_font(const Button *button)
{
cassert_no_null(button);
return button->font;
}

/*---------------------------------------------------------------------------*/
Expand Down
19 changes: 19 additions & 0 deletions src/gui/buttonh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* NAppGUI Cross-platform C SDK
* 2015-2024 Francisco Garcia Collado
* MIT Licence
* https://nappgui.com/en/legal/license.html
*
* File: buttonh.h
*
*/

/* Button */

#include "gui.hxx"

__EXTERN_C

const Font *button_get_font(const Button *button);

__END_C
58 changes: 58 additions & 0 deletions src/gui/layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,64 @@ real32_t layout_get_vsize(const Layout *layout, const uint32_t row)

/*---------------------------------------------------------------------------*/

real32_t layout_get_hmargin(const Layout *layout, const uint32_t col)
{
i_LineDim *dim = NULL;
cassert_no_null(layout);
cassert(col < arrst_size(layout->lines_dim[0], i_LineDim) - 1);
dim = arrst_get(layout->lines_dim[0], col + 1, i_LineDim);
return dim->margin;
}

/*---------------------------------------------------------------------------*/

real32_t layout_get_vmargin(const Layout *layout, const uint32_t row)
{
i_LineDim *dim = NULL;
cassert_no_null(layout);
cassert_msg(row < arrst_size(layout->lines_dim[1], i_LineDim) - 1, "'row' out of range");
dim = arrst_get(layout->lines_dim[1], row + 1, i_LineDim);
return dim->margin;
}

/*---------------------------------------------------------------------------*/

real32_t layout_get_margin_top(const Layout *layout)
{
i_LineDim *first_row = NULL;
cassert_no_null(layout);
first_row = arrst_get(layout->lines_dim[1], 0, i_LineDim);
return first_row->margin;
}

/*---------------------------------------------------------------------------*/

real32_t layout_get_margin_bottom(const Layout *layout)
{
cassert_no_null(layout);
return layout->dim_margin[1];
}

/*---------------------------------------------------------------------------*/

real32_t layout_get_margin_left(const Layout *layout)
{
i_LineDim *first_column = NULL;
cassert_no_null(layout);
first_column = arrst_get(layout->lines_dim[0], 0, i_LineDim);
return first_column->margin;
}

/*---------------------------------------------------------------------------*/

real32_t layout_get_margin_right(const Layout *layout)
{
cassert_no_null(layout);
return layout->dim_margin[0];
}

/*---------------------------------------------------------------------------*/

void layout_remove_cell(Layout *layout, const uint32_t col, const uint32_t row)
{
Cell *cell = i_get_cell(layout, col, row);
Expand Down
12 changes: 12 additions & 0 deletions src/gui/layouth.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ _gui_api real32_t layout_get_hsize(const Layout *layout, const uint32_t col);

_gui_api real32_t layout_get_vsize(const Layout *layout, const uint32_t row);

_gui_api real32_t layout_get_hmargin(const Layout *layout, const uint32_t col);

_gui_api real32_t layout_get_vmargin(const Layout *layout, const uint32_t row);

_gui_api real32_t layout_get_margin_top(const Layout *layout);

_gui_api real32_t layout_get_margin_bottom(const Layout *layout);

_gui_api real32_t layout_get_margin_left(const Layout *layout);

_gui_api real32_t layout_get_margin_right(const Layout *layout);

_gui_api void layout_remove_cell(Layout *layout, const uint32_t col, const uint32_t row);

__END_C
11 changes: 11 additions & 0 deletions src/gui/listbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,17 @@ void listbox_set_elem(ListBox *listbox, const uint32_t index, const char_t *text

/*---------------------------------------------------------------------------*/

void listbox_del_elem(ListBox *listbox, const uint32_t index)
{
LData *data = view_get_data(cast(listbox, View), LData);
cassert_no_null(data);
arrst_delete(data->elems, index, i_remove_elem, PElem);
i_document_size(data);
view_update(cast(listbox, View));
}

/*---------------------------------------------------------------------------*/

void listbox_font(ListBox *listbox, const Font *font)
{
LData *data = view_get_data(cast(listbox, View), LData);
Expand Down
2 changes: 2 additions & 0 deletions src/gui/listbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ _gui_api void listbox_add_elem(ListBox *listbox, const char_t *text, const Image

_gui_api void listbox_set_elem(ListBox *listbox, const uint32_t index, const char_t *text, const Image *image);

_gui_api void listbox_del_elem(ListBox *listbox, const uint32_t index);

_gui_api void listbox_font(ListBox *listbox, const Font *font);

_gui_api void listbox_clear(ListBox *listbox);
Expand Down
2 changes: 2 additions & 0 deletions src/osbs/bfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ _osbs_api uint64_t bfile_pos(const File *file);

_osbs_api bool_t bfile_delete(const char_t *pathname, ferror_t *error);

_osbs_api bool_t bfile_rename(const char_t *current_pathname, const char_t *new_pathname, ferror_t *error);

__END_C
55 changes: 55 additions & 0 deletions src/osbs/unix/bfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,58 @@ bool_t bfile_delete(const char_t *filepath, ferror_t *error)
return FALSE;
}
}

/*---------------------------------------------------------------------------*/

bool_t bfile_rename(const char_t *current_pathname, const char_t *new_pathname, ferror_t *error)
{
int res = rename(cast_const(current_pathname, char), cast_const(new_pathname, char));
if (res == 0)
{
ptr_assign(error, ekFOK);
return TRUE;
}
else
{
if (error != NULL)
{
switch (errno)
{
case EACCES:
case EPERM:
*error = ekFNOACCESS;
break;
case EISDIR:
*error = ekFNOFILE;
break;
case ENAMETOOLONG:
*error = ekFBIGNAME;
break;
case ENOENT:
*error = ekFNOPATH;
break;
case ENOTDIR:
*error = ekFNOPATH;
break;
case EEXIST:
case ENOTEMPTY:
*error = ekFNOEMPTY;
break;

/* https://man7.org/linux/man-pages/man2/rename.2.html */
case EROFS:
case EXDEV:
case EBUSY:
case EFAULT:
case EINVAL:
case ELOOP:
case EMLINK:
case ENOMEM:
case ENOSPC:
default:
cassert_msg(FALSE, "file_rename: undefined");
*error = ekFUNDEF;
}
}
}
}
Loading

0 comments on commit 50887ca

Please sign in to comment.