diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/button.c | 57 | ||||
-rw-r--r-- | src/gui/group.c | 88 | ||||
-rw-r--r-- | src/gui/window.c | 60 |
3 files changed, 205 insertions, 0 deletions
diff --git a/src/gui/button.c b/src/gui/button.c new file mode 100644 index 0000000..96cbd9a --- /dev/null +++ b/src/gui/button.c | |||
@@ -0,0 +1,57 @@ | |||
1 | #include <gui/button.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <assert.h> | ||
4 | #include <common/mem.h> | ||
5 | #include <string.h> | ||
6 | #include <MLV/MLV_all.h> | ||
7 | #include <gui/component.h> | ||
8 | |||
9 | |||
10 | bool button_is_selected(int x, int y, Button *button) { | ||
11 | assert(x >= 0); | ||
12 | assert(y >= 0); | ||
13 | assert(button != NULL); | ||
14 | int x1 = button->component.x_pos; | ||
15 | int y1 = button->component.y_pos; | ||
16 | int x2 = button->component.x_pos + button->component.width; | ||
17 | int y2 = button->component.y_pos + button->component.height; | ||
18 | if (x >= x1 && x <= x2 && y >= y1 && y <= y2) { | ||
19 | return true; | ||
20 | } | ||
21 | return false; | ||
22 | } | ||
23 | |||
24 | void button_print(Component *parameterSelf) { | ||
25 | assert(parameterSelf != NULL); | ||
26 | Button *self = (Button *) parameterSelf; | ||
27 | MLV_draw_adapted_text_box(self->component.x_pos, self->component.y_pos, self->label, self->sizeInterligne, | ||
28 | MLV_COLOR_BLACK, MLV_COLOR_WHITE, MLV_COLOR_DARK_GREY, MLV_TEXT_CENTER); | ||
29 | } | ||
30 | |||
31 | void button_click_test(int x, int y, Component *parameterSelf) { | ||
32 | assert(x >= 0); | ||
33 | assert(y >= 0); | ||
34 | assert(parameterSelf != NULL); | ||
35 | Button *self = (Button *) parameterSelf; | ||
36 | if (button_is_selected(x, y, self) && self->component.activated) { | ||
37 | printf("OK\n"); | ||
38 | } | ||
39 | } | ||
40 | |||
41 | void | ||
42 | button_init(Button *button, const char *text, int sizeInterligne, int x_pos, int y_pos, ClickHandler clickHandler) { | ||
43 | assert(button != NULL); | ||
44 | assert(text != NULL); | ||
45 | assert(sizeInterligne >= 0); | ||
46 | assert(x_pos >= 0); | ||
47 | assert(y_pos >= 0); | ||
48 | button->label = malloc_or_die(sizeof(char) * (strlen(text) + 1)); | ||
49 | strcpy(button->label, text); | ||
50 | button->sizeInterligne = sizeInterligne; | ||
51 | MLV_get_size_of_adapted_text_box(text, sizeInterligne, &button->component.width, &button->component.height); | ||
52 | button->component.x_pos = x_pos; | ||
53 | button->component.y_pos = y_pos; | ||
54 | button->component.activated = true; | ||
55 | button->component.print_method = button_print; | ||
56 | button->component.click_handler = clickHandler; | ||
57 | } | ||
diff --git a/src/gui/group.c b/src/gui/group.c new file mode 100644 index 0000000..11a0583 --- /dev/null +++ b/src/gui/group.c | |||
@@ -0,0 +1,88 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include <gui/group.h> | ||
3 | #include <common/mem.h> | ||
4 | #include <assert.h> | ||
5 | #include <gui/component.h> | ||
6 | #include "MLV/MLV_shape.h" | ||
7 | #include "MLV/MLV_window.h" | ||
8 | |||
9 | void group_print(Component *parameterSelf) { | ||
10 | assert(parameterSelf != NULL); | ||
11 | Group *self = (Group *) parameterSelf; | ||
12 | if (self->group_head != NULL) { | ||
13 | GroupElement *p = self->group_head; | ||
14 | while (p != NULL) { | ||
15 | p->sub_component->print_method(p->sub_component); | ||
16 | p = p->next; | ||
17 | } | ||
18 | } | ||
19 | } | ||
20 | |||
21 | void group_click_handler(int x_pos, int y_pos, Component *parameterSelf) { | ||
22 | assert(parameterSelf != NULL); | ||
23 | Group *self = (Group *) parameterSelf; | ||
24 | |||
25 | if (self->group_head != NULL && self->component.activated) { | ||
26 | GroupElement *p = self->group_head; | ||
27 | while (p != NULL) { | ||
28 | p->sub_component->click_handler(x_pos, y_pos, p->sub_component); | ||
29 | p = p->next; | ||
30 | } | ||
31 | } | ||
32 | } | ||
33 | |||
34 | void group_init(Group *group, int width, int height, int x_pos, int y_pos, int margin) { | ||
35 | assert(group != NULL); | ||
36 | assert(width > 0); | ||
37 | assert(height > 0); | ||
38 | assert(x_pos >= 0); | ||
39 | assert(y_pos >= 0); | ||
40 | assert(margin >= 0); | ||
41 | group->component.width = width; | ||
42 | group->component.height = height; | ||
43 | group->component.x_pos = x_pos; | ||
44 | group->component.y_pos = y_pos; | ||
45 | group->component.activated = true; | ||
46 | group->component.print_method = group_print; | ||
47 | group->component.click_handler = group_click_handler; | ||
48 | group->margin = margin; | ||
49 | } | ||
50 | |||
51 | void group_free(Group *group) { | ||
52 | assert(group != NULL); | ||
53 | if (group->group_head != NULL) { | ||
54 | GroupElement *p = group->group_head; | ||
55 | while (p != NULL) { | ||
56 | GroupElement *tmp = p; | ||
57 | p = p->next; | ||
58 | free(tmp); | ||
59 | } | ||
60 | group->group_head = NULL; | ||
61 | } | ||
62 | } | ||
63 | |||
64 | void group_add_component(Group *group, Component *component) { | ||
65 | assert(group != NULL); | ||
66 | assert(component != NULL); | ||
67 | /*Initialize the new node*/ | ||
68 | GroupElement *tmp = malloc_or_die(sizeof(GroupElement)); | ||
69 | tmp->sub_component = component; | ||
70 | tmp->next = NULL; | ||
71 | if (group->group_head != NULL) { | ||
72 | GroupElement *p = group->group_head; | ||
73 | /*Browsing*/ | ||
74 | while (p->next != NULL) { | ||
75 | p = p->next; | ||
76 | } | ||
77 | p->next = tmp; | ||
78 | /*Modifying for margin*/ | ||
79 | tmp->sub_component->y_pos = p->sub_component->y_pos; | ||
80 | tmp->sub_component->x_pos = p->sub_component->x_pos + p->sub_component->width + group->margin; | ||
81 | } else { | ||
82 | tmp->sub_component->y_pos = group->component.y_pos; | ||
83 | tmp->sub_component->x_pos = group->component.x_pos; | ||
84 | group->group_head = tmp; | ||
85 | |||
86 | } | ||
87 | } | ||
88 | |||
diff --git a/src/gui/window.c b/src/gui/window.c new file mode 100644 index 0000000..6e287e5 --- /dev/null +++ b/src/gui/window.c | |||
@@ -0,0 +1,60 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include <gui/window.h> | ||
3 | #include <gui/button.h> | ||
4 | #include <gui/pictureframe.h> | ||
5 | #include <gui/group.h> | ||
6 | #include "common/mem.h" | ||
7 | #include "string.h" | ||
8 | #include "assert.h" | ||
9 | #include "MLV/MLV_window.h" | ||
10 | |||
11 | void window_init(Window *window, int width, int height, char *title) { | ||
12 | assert(window != NULL); | ||
13 | assert(width > 0); | ||
14 | assert(height > 0); | ||
15 | assert(title != NULL); | ||
16 | window->width = width; | ||
17 | window->height = height; | ||
18 | window->title = malloc_or_die(sizeof(char) * (strlen(title) + 1)); | ||
19 | strcpy(window->title, title); | ||
20 | window->group_buttons = malloc_or_die(sizeof(Group)); | ||
21 | group_init(window->group_buttons, window->width, 100, 0, window->height - 100, 5); | ||
22 | window->group_pictureframe = malloc_or_die(sizeof(Group)); | ||
23 | group_init(window->group_pictureframe, window->width, window->height - 100, 0, 0, 5); | ||
24 | } | ||
25 | |||
26 | void window_free(Window *window) { | ||
27 | assert(window != NULL); | ||
28 | free(window->title); | ||
29 | group_free(window->group_buttons); | ||
30 | group_free(window->group_pictureframe); | ||
31 | } | ||
32 | |||
33 | void window_add_button(Window *window, Button *button) { | ||
34 | assert(window != NULL); | ||
35 | assert(button != NULL); | ||
36 | group_add_component(window->group_buttons, &(button->component)); | ||
37 | } | ||
38 | |||
39 | void window_add_pictureframe(Window *window, PictureFrame *pictureFrame) { | ||
40 | assert(window != NULL); | ||
41 | assert(pictureFrame != NULL); | ||
42 | group_add_component(window->group_pictureframe, &(pictureFrame->component)); | ||
43 | } | ||
44 | |||
45 | void window_create(Window *window) { | ||
46 | assert(window != NULL); | ||
47 | MLV_create_window(window->title, window->title, (unsigned int) window->width, (unsigned int) window->height); | ||
48 | } | ||
49 | |||
50 | void window_print_buttons(Window *window) { | ||
51 | assert(window != NULL); | ||
52 | window->group_buttons->component.print_method(&(window->group_buttons->component)); | ||
53 | MLV_actualise_window(); | ||
54 | } | ||
55 | |||
56 | void window_print_pictureframes(Window *window) { | ||
57 | assert(window != NULL); | ||
58 | window->group_pictureframe->component.print_method(&(window->group_pictureframe->component)); | ||
59 | MLV_actualise_window(); | ||
60 | } \ No newline at end of file | ||