Atrinik Client  4.0
container.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
33 #include <global.h>
34 
36 static void widget_draw(widgetdata *widget)
37 {
38  /* Special case, menuitem is highlighted when mouse is moved over it. */
39  if (widget->sub_type == MENU_ID) {
40  widget_highlight_menu(widget);
41  }
42 
43 }
44 
46 static int widget_event(widgetdata *widget, SDL_Event *event)
47 {
48  if (widget->sub_type == MENUITEM_ID && event->type == SDL_MOUSEBUTTONDOWN) {
49  _menuitem *menuitem;
50 
51  if (widget->env->sub_type != MENU_ID) {
52  return 0;
53  }
54 
55  menuitem = MENUITEM(widget);
56  widget_mouse_event.owner = (MENU(widget->env))->owner;
57 
58  if (widget_mouse_event.owner && menuitem->menu_type != MENU_SUBMENU && menuitem->menu_func_ptr) {
59  if (menuitem->menu_type == MENU_RADIO && menuitem->val != 0) {
60  return 1;
61  }
62 
63  menuitem->menu_func_ptr(widget_mouse_event.owner, widget, event);
64  }
65 
66  return 1;
67  }
68 
69  return 0;
70 }
71 
73 static int widget_load(widgetdata *widget, const char *keyword, const char *parameter)
74 {
75  _widget_container *container;
76 
77  container = CONTAINER(widget);
78 
79  if (strcmp(keyword, "padding") == 0) {
80  int padding[5];
81 
82  if (sscanf(parameter, "%d %d %d %d %d", &padding[0], &padding[1], &padding[2], &padding[3], &padding[4]) >= 4) {
83  if (widget->sub_type == CONTAINER_STRIP_ID || widget->sub_type == MENU_ID || widget->sub_type == MENUITEM_ID) {
84  _widget_container_strip *container_strip;
85 
86  container_strip = CONTAINER_STRIP(widget);
87  container_strip->inner_padding = padding[4];
88  }
89 
90  container->outer_padding_top = padding[0];
91  container->outer_padding_right = padding[1];
92  container->outer_padding_bottom = padding[2];
93  container->outer_padding_left = padding[3];
94 
95  return 1;
96  }
97  }
98 
99  return 0;
100 }
101 
103 static void widget_save(widgetdata *widget, FILE *fp, const char *padding)
104 {
105  _widget_container *container;
106 
107  container = CONTAINER(widget);
108 
109  fprintf(fp, "%spadding = %d %d %d %d", padding, container->outer_padding_top, container->outer_padding_right, container->outer_padding_bottom, container->outer_padding_left);
110 
111  if (widget->sub_type == CONTAINER_STRIP_ID || widget->sub_type == MENU_ID || widget->sub_type == MENUITEM_ID) {
112  _widget_container_strip *container_strip;
113 
114  container_strip = CONTAINER_STRIP(widget);
115  fprintf(fp, " %d", container_strip->inner_padding);
116  }
117 
118  fprintf(fp, "\n");
119 }
120 
122 static void widget_deinit(widgetdata *widget)
123 {
124  _widget_container *container = widget->subwidget;
125 
126  if (container->subcontainer != NULL) {
127  _widget_container_strip *container_strip = container->subcontainer;
128 
129  if (container_strip->subcontainer_strip != NULL) {
130  efree(container_strip->subcontainer_strip);
131  }
132 
133  efree(container->subcontainer);
134  }
135 }
136 
143 {
144  _widget_container *container;
145 
146  container = ecalloc(1, sizeof(_widget_container));
147  container->widget_type = -1;
148 
149  widget->draw_func = widget_draw;
150  widget->event_func = widget_event;
151  widget->load_func = widget_load;
152  widget->save_func = widget_save;
153  widget->deinit_func = widget_deinit;
154  widget->subwidget = container;
155 
156  if (widget->sub_type == CONTAINER_STRIP_ID || widget->sub_type == MENU_ID || widget->sub_type == MENUITEM_ID) {
157  _widget_container_strip *container_strip;
158 
159  container_strip = ecalloc(1, sizeof(_widget_container_strip));
160  container_strip->inner_padding = 10;
161 
162  container->subcontainer = container_strip;
163 
164  if (widget->sub_type == MENU_ID) {
165  _menu *menu;
166 
167  menu = ecalloc(1, sizeof(_menu));
168  container_strip->subcontainer_strip = menu;
169  } else if (widget->sub_type == MENUITEM_ID) {
170  _menuitem *menuitem;
171 
172  menuitem = emalloc(sizeof(_menuitem));
173  menuitem->menu_type = MENU_NORMAL;
174 
175  container_strip->subcontainer_strip = menuitem;
176  }
177  }
178 }
static int widget_load(widgetdata *widget, const char *keyword, const char *parameter)
Definition: container.c:73
int outer_padding_left
Definition: widget.h:178
int outer_padding_top
Definition: widget.h:170
static void widget_deinit(widgetdata *widget)
Definition: container.c:122
struct widgetdata * env
Definition: widget.h:98
void * subwidget
Definition: widget.h:107
int sub_type
Definition: widget.h:122
static void widget_save(widgetdata *widget, FILE *fp, const char *padding)
Definition: container.c:103
int menu_type
Definition: widget.h:274
Definition: widget.h:252
void widget_container_init(widgetdata *widget)
Definition: container.c:142
void * subcontainer_strip
Definition: widget.h:247
void(* menu_func_ptr)(widgetdata *, widgetdata *, SDL_Event *event)
Definition: widget.h:271
static int widget_event(widgetdata *widget, SDL_Event *event)
Definition: container.c:46
static void widget_draw(widgetdata *widget)
Definition: container.c:36
widgetdata * owner
Definition: widget.h:360
widgetevent widget_mouse_event
Definition: widget.c:82
int outer_padding_bottom
Definition: widget.h:174
int outer_padding_right
Definition: widget.h:182
void widget_highlight_menu(widgetdata *widget)
Definition: menu.c:36