summaryrefslogtreecommitdiff
path: root/src/gui/group.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/group.c')
-rw-r--r--src/gui/group.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/gui/group.c b/src/gui/group.c
new file mode 100644
index 0000000..af9abac
--- /dev/null
+++ b/src/gui/group.c
@@ -0,0 +1,87 @@
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
9void 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
21void 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) {
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
34void 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.print_method = group_print;
46 group->component.click_handler = group_click_handler;
47 group->margin = margin;
48}
49
50void group_free(Group *group) {
51 assert(group != NULL);
52 if (group->group_head != NULL) {
53 GroupElement *p = group->group_head;
54 while (p != NULL) {
55 GroupElement *tmp = p;
56 p = p->next;
57 free(tmp);
58 }
59 group->group_head = NULL;
60 }
61}
62
63void group_add_component(Group *group, Component *component) {
64 assert(group != NULL);
65 assert(component != NULL);
66 /*Initialize the new node*/
67 GroupElement *tmp = malloc_or_die(sizeof(GroupElement));
68 tmp->sub_component = component;
69 tmp->next = NULL;
70 if (group->group_head != NULL) {
71 GroupElement *p = group->group_head;
72 /*Browsing*/
73 while (p->next != NULL) {
74 p = p->next;
75 }
76 p->next = tmp;
77 /*Modifying for margin*/
78 tmp->sub_component->y_pos = p->sub_component->y_pos;
79 tmp->sub_component->x_pos = p->sub_component->x_pos + p->sub_component->width + group->margin;
80 } else {
81 tmp->sub_component->y_pos = group->component.y_pos;
82 tmp->sub_component->x_pos = group->component.x_pos;
83 group->group_head = tmp;
84
85 }
86}
87