-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_create_buttons_list.c
74 lines (62 loc) · 2.39 KB
/
lib_create_buttons_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "exncwi.h"
/*
* TODO: проверку, что создаваемые кнопки не находятся частично/полностью
* за пределами экрана.
*/
Button* create_buttons_list(wchar_t* buttons_text[],
uint32_t buttons_amount, uint16_t y, uint16_t x,
uint16_t y_size, uint16_t x_size,
int16_t y_interval, int16_t x_interval)
{
/* Проверка входных параметров. */
if ((buttons_text == NULL) || (buttons_amount < 1)
|| (y_size < 1) || (x_size < 1))
{
return NULL;
}
Button* temp = NULL;
Button* root_button_ptr = NULL;
/* Координаты верхнего левого угла текущей кнопки. */
uint16_t y_temp = y;
uint16_t x_temp = x;
for (uint32_t i = 0; i < buttons_amount; i++)
{
/* Создание новой кнопки. */
if (i != 0)
{
temp->next = (Button*) malloc(sizeof(Button));
temp = temp->next;
}
else
{
temp = (Button*) malloc(sizeof(Button));
/* Сохранение указателя на корень списка. */
root_button_ptr = temp;
}
/* Если возникла ошибка при выделении памяти. */
if (temp == NULL)
{
/* Очистка памяти от созданных ранее кнопок. */
free_buttons(root_button_ptr);
return NULL;
}
/* Инициализация текущей кнопки. */
temp->next = NULL;
temp->id = i + 1;
temp->y_size = y_size;
temp->x_size = x_size;
temp->text = buttons_text[i];
temp->button_win = newwin(y_size, x_size, y_temp, x_temp);
/* Если возникла ошибка при создании окна кнопки. */
if (temp->button_win == NULL)
{
/* Очистка памяти от созданных ранее кнопок. */
free_buttons(root_button_ptr);
return NULL;
}
/* Перезапись координат следующей кнопки. */
y_temp += y_size + y_interval;
x_temp += x_size + x_interval;
}
return root_button_ptr;
}