forked from QW-Group/ezquake-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathez_listviewitem.c
292 lines (231 loc) · 9.04 KB
/
ez_listviewitem.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
Copyright (C) 2008 ezQuake team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
$Id: ez_listviewitem.c,v 1.78 2007/10/27 14:51:15 cokeman1982 Exp $
*/
#include "quakedef.h"
#include "keys.h"
#include "utils.h"
#include "common_draw.h"
#include "ez_listviewitem.h"
#define VALID_COLUMN(column) ((column >= 0) && (column < self->item_count))
//
// Listview item - Creates a new listview item and initializes it.
//
ez_listviewitem_t *EZ_listviewitem_Create(ez_tree_t *tree, ez_control_t *parent,
char *name, char *description,
int x, int y, int width, int height,
ez_control_flags_t flags)
{
ez_listviewitem_t *listviewitem = NULL;
// We have to have a tree to add the control to.
if (!tree)
{
return NULL;
}
listviewitem = (ez_listviewitem_t *)Q_malloc(sizeof(ez_listviewitem_t));
memset(listviewitem, 0, sizeof(ez_listviewitem_t));
EZ_listviewitem_Init(listviewitem, tree, parent, name, description, x, y, width, height, flags);
return listviewitem;
}
//
// Listview item - Initializes a listview item.
//
void EZ_listviewitem_Init(ez_listviewitem_t *listviewitem, ez_tree_t *tree, ez_control_t *parent,
char *name, char *description,
int x, int y, int width, int height,
ez_control_flags_t flags)
{
int i;
ez_control_t *self = (ez_control_t *)listviewitem;
// Initialize the inherited class first.
EZ_control_Init(&listviewitem->super, tree, parent, name, description, x, y, width, height, flags);
((ez_control_t *)listviewitem)->CLASS_ID = EZ_LISTVIEWITEM_ID;
((ez_control_t *)listviewitem)->ext_flags |= (flags | control_focusable | control_contained | control_resizeable);
// Make all columns visible by default :)
for (i = 0; i < LISTVIEW_COLUMN_COUNT; i++)
{
listviewitem->item_visibile[i] = true;
}
// Listview item specific events.
CONTROL_REGISTER_EVENT(listviewitem, EZ_listviewitem_OnColumnAdded, OnColumnAdded, ez_listviewitem_t);
CONTROL_REGISTER_EVENT(listviewitem, EZ_listviewitem_OnColumnVisibilityChanged, OnColumnVisibilityChanged, ez_listviewitem_t);
CONTROL_REGISTER_EVENT(listviewitem, EZ_listviewitem_OnColumnGapChanged, OnColumnGapChanged, ez_listviewitem_t);
CONTROL_REGISTER_EVENT(listviewitem, EZ_listviewitem_OnColumnWidthChanged, OnColumnWidthChanged, ez_listviewitem_t);
//CONTROL_REGISTER_EVENT(listviewitem, EZ_listviewitem_OnSubItemChanged, OnSubItemChanged, ez_listviewitem_t);
}
//
// Listview item - Destroys a listview item.
//
int EZ_listviewitem_Destroy(ez_control_t *self, qbool destroy_children)
{
int i;
ez_listviewitem_t *listviewitem = (ez_listviewitem_t *)self;
CONTROL_EVENT_HANDLER_CALL(NULL, self, ez_control_t, OnDestroy, destroy_children);
// TODO : Remove any event handlers.
EZ_eventhandler_Remove(listviewitem->event_handlers.OnColumnAdded, NULL, true);
EZ_eventhandler_Remove(listviewitem->event_handlers.OnColumnVisibilityChanged, NULL, true);
//EZ_eventhandler_Remove(listviewitem->event_handlers.OnSubItemChanged, NULL, true);
// Cleanup columns.
for (i = 0; i < listviewitem->item_count; i++)
{
// TODO: Hmm should we raise it like this?
CONTROL_RAISE_EVENT(NULL, listviewitem->items[i], ez_control_t, OnDestroy, true);
}
EZ_control_Destroy((ez_control_t *)self, destroy_children);
return 0;
}
//
// Listview item - Adds a column to the listview item.
//
void EZ_listviewitem_AddColumn(ez_listviewitem_t *self, ez_listview_subitem_t data, int width)
{
int column = 0;
ez_label_t *label = NULL;
ez_control_t *ctrl = (ez_control_t *)self;
if (self->item_count >= sizeof(self->items))
{
Sys_Error("EZ_listviewitem_AddColumn: Max allowed columns %i exceeded.\n", sizeof(self->items));
}
// Create a new label to use as the column.
label = EZ_label_Create(ctrl->control_tree, ctrl, "Listview item column", "", 0, 0, width, 5, 0, 0, data.text);
EZ_control_SetPayload(ctrl, data.payload);
EZ_control_SetAnchor(ctrl, anchor_left | anchor_top | anchor_bottom);
// Add the item to the columns.
self->items[self->item_count] = label;
column = self->item_count;
self->item_count++;
CONTROL_RAISE_EVENT(NULL, ctrl, ez_listviewitem_t, OnColumnAdded, (void *)column);
}
//
// Listview item - Event for when a new column was added to the listview item.
//
int EZ_listviewitem_OnColumnAdded(ez_control_t *self, void *column)
{
ez_listviewitem_t *lvi = (ez_listviewitem_t *)self;
EZ_listviewitem_LayoutControl(lvi);
CONTROL_EVENT_HANDLER_CALL(NULL, lvi, ez_listviewitem_t, OnColumnAdded, column);
// TODO: Layout the control again if some mischevaous event handler changed the size/position of the columns? :D
return 0;
}
//
// Listview item - Lays out the control.
//
void EZ_listviewitem_LayoutControl(ez_listviewitem_t *self)
{
int i;
int x = 0;
ez_control_t *ctrl = (ez_control_t *)self;
ez_control_t *lblctrl = NULL;
for (i = 0; i < self->item_count; i++)
{
if (!self->item_visibile[i])
continue;
lblctrl = (ez_control_t *)self->items[i];
// Space out the labels in columns.
EZ_control_SetPosition(lblctrl, x, 0);
x += lblctrl->width + self->item_gap;
// Make sure we reset the height if some naughty person changed it since last time.
EZ_control_SetSize(lblctrl, lblctrl->width, ctrl->height);
}
}
//
// Listview item - Gets a column from the listview item.
//
ez_label_t *EZ_listviewitem_GetColumn(ez_listviewitem_t *self, int column)
{
return VALID_COLUMN(column) ? self->items[column] : NULL;
}
//
// Listview item - Sets if a column should be visible or not.
//
void EZ_listviewitem_SetColumnVisible(ez_listviewitem_t *self, int column, qbool visible)
{
if (!VALID_COLUMN(column))
return;
self->item_visibile[column] = visible;
EZ_control_SetVisible((ez_control_t *)self->items[column], visible);
CONTROL_RAISE_EVENT(NULL, self, ez_listviewitem_t, OnColumnVisibilityChanged, (void *)column);
}
//
// Listview item - The visibility changed for a column.
//
int EZ_listviewitem_OnColumnVisibilityChanged(ez_control_t *self, void *column)
{
CONTROL_EVENT_HANDLER_CALL(NULL, self, ez_listviewitem_t, OnColumnVisibilityChanged, column);
return 0;
}
//
// Listview item - Sets the gap between columns.
//
void EZ_listviewitem_SetColumnGap(ez_listviewitem_t *self, int gap)
{
self->item_gap = gap;
CONTROL_RAISE_EVENT(NULL, self, ez_listviewitem_t, OnColumnGapChanged, NULL);
}
//
// Listview item - Event for when the column gap has changed.
//
int EZ_listviewitem_OnColumnGapChanged(ez_control_t *self, void *ext_event_info)
{
ez_listviewitem_t *lvi = (ez_listviewitem_t *)self;
EZ_listviewitem_LayoutControl(lvi);
CONTROL_EVENT_HANDLER_CALL(NULL, self, ez_listviewitem_t, OnColumnGapChanged, NULL);
return 0;
}
//
// Listview item - Sets the column width for a given column.
//
void EZ_listviewitem_SetColumnWidth(ez_listviewitem_t *self, int column, int width)
{
if (!VALID_COLUMN(column))
return;
self->item_widths[column] = width;
CONTROL_RAISE_EVENT(NULL, self, ez_listviewitem_t, OnColumnWidthChanged, (void *)column);
}
//
// Listview item - The column width has changed for some column.
//
int EZ_listviewitem_OnColumnWidthChanged(ez_control_t *self, void *ext_event_info)
{
ez_listviewitem_t *lvi = (ez_listviewitem_t *)self;
int column = (int)ext_event_info;
EZ_control_SetSize(self, lvi->item_widths[column], self->height);
CONTROL_EVENT_HANDLER_CALL(NULL, lvi, ez_listviewitem_t, OnColumnWidthChanged, ext_event_info);
return 0;
}
#if 0
//
// Listview item - A sub items text has changed.
//
int EZ_listviewitem_OnSubItemChanged(ez_control_t *self, void *ext_event_info)
{
ez_listviewitem_changeinfo_t *change = (ez_listviewitem_changeinfo_t *)ext_event_info;
CONTROL_EVENT_HANDLER_CALL(NULL, self, ez_listviewitem_t, OnSubItemChanged, ext_event_info);
return 0;
}
//
// Listview item - The text of a label in a sub item changed (event handler).
//
static int EZ_listviewitem_OnLabelTextChanged(ez_control_t *self, void *payload, void *ext_event_info)
{
ez_label_t *label = (ez_label_t *)self;
ez_listviewitem_changeinfo_t change;
ez_listview_subitem_t item;
item.payload = self->payload;
//item.text = label->
change.column = (int)*self->payload; // TODO: Save the column in the label controls payload.
change.payload = self;
CONTROL_RAISE_EVENT(NULL, self, ez_listviewitem_t, OnSubItemChanged, (void *)&change);
}
#endif