summaryrefslogtreecommitdiff
path: root/src/gui/group.c
blob: 75737af4be3ebbeccd084356138f3d5033ef7376 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "gui/group.h"
#include <assert.h>
#include <stdlib.h>
#include <MLV/MLV_shape.h>
#include "common/mem.h"

/**
 * File: group.c
 *
 * Author:
 *   Adam NAILI
 */

void group_print(Component *parameterSelf) {
  assert(parameterSelf != NULL);
  Group *self = (Group *) parameterSelf;
  if (self->group_head != NULL) {
    GroupElement *p = self->group_head;
    while (p != NULL) {
      p->sub_component->print_method(p->sub_component);
      p = p->next;
    }
  }
}

void group_click_handler(int x_pos, int y_pos, Component *parameterSelf) {
  assert(parameterSelf != NULL);
  Group *self = (Group *) parameterSelf;

  if (self->group_head != NULL) {
    GroupElement *p = self->group_head;
    while (p != NULL) {
      p->sub_component->click_handler(x_pos, y_pos, p->sub_component);
      p = p->next;
    }
  }
}

void group_init(Group *group, int width, int height, int x_pos, int y_pos, int margin) {
  assert(group != NULL);
  assert(width > 0);
  assert(height > 0);
  assert(x_pos >= 0);
  assert(y_pos >= 0);
  assert(margin >= 0);
  group->component.width = width;
  group->component.height = height;
  group->component.x_pos = x_pos;
  group->component.y_pos = y_pos;
  group->component.print_method = group_print;
  group->component.click_handler = group_click_handler;
  group->margin = margin;
}

void group_free(Group *group) {
  assert(group != NULL);
  if (group->group_head != NULL) {
    GroupElement *p = group->group_head;
    while (p != NULL) {
      GroupElement *tmp = p;
      p = p->next;
      free(tmp);
    }
    group->group_head = NULL;
  }
}

void group_add_component(Group *group, Component *component) {
  assert(group != NULL);
  assert(component != NULL);
  /*Initialize the new node*/
  GroupElement *tmp = malloc_or_die(sizeof(GroupElement));
  tmp->sub_component = component;
  tmp->next = NULL;
  if (group->group_head != NULL) {
    GroupElement *p = group->group_head;
    /*Browsing*/
    while (p->next != NULL) {
      p = p->next;
    }
    p->next = tmp;
    /*Modifying for margin*/
    tmp->sub_component->y_pos = p->sub_component->y_pos;
    tmp->sub_component->x_pos = p->sub_component->x_pos + p->sub_component->width + group->margin;
  } else {
    tmp->sub_component->y_pos = group->component.y_pos;
    tmp->sub_component->x_pos = group->component.x_pos;
    group->group_head = tmp;

  }
}