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
293
294
295
296
297
298
299
300
301
302
303
304
305
306 | /*
* This file is part of libslab.
*
* Copyright (c) 2006 Novell, Inc.
*
* Libslab is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* Libslab 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 Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libslab; if not, write to the Free Software Foundation, Inc., 51
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <ctk/ctk.h>
#include "app-shell.h"
#include "app-resizer.h"
static void app_resizer_size_allocate (CtkWidget * resizer, CtkAllocation * allocation);
static gboolean app_resizer_paint_window (CtkWidget * widget, cairo_t * cr, AppShellData * app_data);
G_DEFINE_TYPE (AppResizer, app_resizer, CTK_TYPE_LAYOUT);
static void
app_resizer_class_init (AppResizerClass * klass)
{
CtkWidgetClass *widget_class;
widget_class = CTK_WIDGET_CLASS (klass);
widget_class->size_allocate = app_resizer_size_allocate;
}
static void
app_resizer_init (AppResizer * window)
{
ctk_style_context_add_class (ctk_widget_get_style_context (CTK_WIDGET (window)),
CTK_STYLE_CLASS_VIEW);
}
void
remove_container_entries (CtkContainer * widget)
{
GList *children, *l;
children = ctk_container_get_children (widget);
for (l = children; l; l = l->next)
{
CtkWidget *child = CTK_WIDGET (l->data);
ctk_container_remove (CTK_CONTAINER (widget), CTK_WIDGET (child));
}
if (children)
g_list_free (children);
}
static void
resize_table (AppResizer *widget, CtkGrid * table, gint columns)
{
remove_container_entries (CTK_CONTAINER (table));
widget->column = columns;
}
static void
relayout_table (AppResizer *widget, CtkGrid * table, GList * element_list)<--- Parameter 'widget' can be declared as pointer to const
{
gint row = 0, col = 0;
do
{
CtkWidget *element = CTK_WIDGET (element_list->data);
ctk_grid_attach (table, element, col, row, 1, 1);
col++;
if (col == widget->column)
{
col = 0;
row++;
}
}
while (NULL != (element_list = g_list_next (element_list)));
}
void
app_resizer_layout_table_default (AppResizer * widget, CtkGrid * table, GList * element_list)
{
resize_table (widget, table, widget->cur_num_cols);
relayout_table (widget, table, element_list);
}
static void
relayout_tables (AppResizer * widget, gint num_cols)
{
CtkGrid *table;<--- The scope of the variable 'table' can be reduced. [+]The scope of the variable 'table' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
GList *table_list, *launcher_list;<--- The scope of the variable 'launcher_list' can be reduced. [+]The scope of the variable 'launcher_list' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
for (table_list = widget->cached_tables_list; table_list != NULL;
table_list = g_list_next (table_list))
{
table = CTK_GRID (table_list->data);
launcher_list = ctk_container_get_children (CTK_CONTAINER (table));
launcher_list = g_list_reverse (launcher_list); /* Fixme - ugly hack because table stores prepend */
resize_table (widget, table, num_cols);
relayout_table (widget, table, launcher_list);
g_list_free (launcher_list);
}
}
static gint
calculate_num_cols (AppResizer * resizer, gint avail_width)
{
if (resizer->table_elements_homogeneous)
{
gint num_cols;
if (resizer->cached_element_width == -1)
{
CtkGrid *table = CTK_GRID (resizer->cached_tables_list->data);
GList *children = ctk_container_get_children (CTK_CONTAINER (table));
CtkWidget *table_element = CTK_WIDGET (children->data);
gint natural_width;
g_list_free (children);
ctk_widget_get_preferred_width (table_element, NULL, &natural_width);
resizer->cached_element_width = natural_width;
resizer->cached_table_spacing = ctk_grid_get_column_spacing (table);
}
num_cols =
(avail_width +
resizer->cached_table_spacing) / (resizer->cached_element_width +
resizer->cached_table_spacing);
return num_cols;
}
else
g_assert_not_reached (); /* Fixme - implement... */
}
static gint
relayout_tables_if_needed (AppResizer * widget, gint avail_width, gint current_num_cols)
{
gint num_cols = calculate_num_cols (widget, avail_width);
if (num_cols < 1)
{
num_cols = 1; /* just horiz scroll if avail_width is less than one column */
}
if (current_num_cols != num_cols)
{
relayout_tables (widget, num_cols);
current_num_cols = num_cols;
}
return current_num_cols;
}
void
app_resizer_set_table_cache (AppResizer * widget, GList * cache_list)
{
widget->cached_tables_list = cache_list;
}
static void
app_resizer_size_allocate (CtkWidget * widget, CtkAllocation * allocation)
{
AppResizer *resizer = APP_RESIZER (widget);
CtkWidget *child = CTK_WIDGET (APP_RESIZER (resizer)->child);
CtkAllocation widget_allocation;
CtkRequisition child_requisition;
static gboolean first_time = TRUE;
gint new_num_cols;
if (first_time)
{
/* we are letting the first show be the "natural" size of the child widget so do nothing. */
if (CTK_WIDGET_CLASS (app_resizer_parent_class)->size_allocate)
(*CTK_WIDGET_CLASS (app_resizer_parent_class)->size_allocate) (widget, allocation);
first_time = FALSE;
ctk_widget_get_allocation (child, &widget_allocation);
ctk_layout_set_size (CTK_LAYOUT (resizer), widget_allocation.width,
widget_allocation.height);
return;
}
ctk_widget_get_preferred_size (child, &child_requisition, NULL);
if (!resizer->cached_tables_list) /* if everthing is currently filtered out - just return */
{
CtkAllocation child_allocation;
if (CTK_WIDGET_CLASS (app_resizer_parent_class)->size_allocate)
(*CTK_WIDGET_CLASS (app_resizer_parent_class)->size_allocate) (widget, allocation);
/* We want the message to center itself and only scroll if it's bigger than the available real size. */
child_allocation.x = 0;
child_allocation.y = 0;
child_allocation.width = MAX (allocation->width, child_requisition.width);
child_allocation.height = MAX (allocation->height, child_requisition.height);
ctk_widget_size_allocate (child, &child_allocation);
ctk_layout_set_size (CTK_LAYOUT (resizer), child_allocation.width,
child_allocation.height);
return;
}
CtkRequisition other_requisiton;
ctk_widget_get_preferred_size (CTK_WIDGET (resizer->cached_tables_list->data), &other_requisiton, NULL);
new_num_cols =
relayout_tables_if_needed (APP_RESIZER (resizer), allocation->width,
resizer->cur_num_cols);
if (resizer->cur_num_cols != new_num_cols)
{
CtkRequisition req;
/* Have to do this so that it requests, and thus gets allocated, new amount */
ctk_widget_get_preferred_size (child, &req, NULL);
resizer->cur_num_cols = new_num_cols;
}
if (CTK_WIDGET_CLASS (app_resizer_parent_class)->size_allocate)
(*CTK_WIDGET_CLASS (app_resizer_parent_class)->size_allocate) (widget, allocation);
ctk_widget_get_allocation (child, &widget_allocation);
ctk_layout_set_size (CTK_LAYOUT (resizer), widget_allocation.width,
widget_allocation.height);
}
CtkWidget *
app_resizer_new (CtkBox * child, gint initial_num_columns, gboolean homogeneous,
AppShellData * app_data)
{
AppResizer *widget;
g_assert (child != NULL);
widget = g_object_new (APP_RESIZER_TYPE, NULL);
widget->cached_element_width = -1;
widget->cur_num_cols = initial_num_columns;
widget->table_elements_homogeneous = homogeneous;
widget->setting_style = FALSE;
widget->app_data = app_data;
g_signal_connect (G_OBJECT (widget), "draw", G_CALLBACK (app_resizer_paint_window), app_data);
ctk_container_add (CTK_CONTAINER (widget), CTK_WIDGET (child));
widget->child = child;
return CTK_WIDGET (widget);
}
void
app_resizer_set_vadjustment_value (CtkWidget * widget, gdouble value)
{
CtkAdjustment *adjust;
adjust = ctk_scrollable_get_vadjustment (CTK_SCROLLABLE (widget));
gdouble upper = ctk_adjustment_get_upper (adjust);
gdouble page_size = ctk_adjustment_get_page_size (adjust);
if (value > upper - page_size)
{
value = upper - page_size;
}
ctk_adjustment_set_value (adjust, value);
}
static gboolean
app_resizer_paint_window (CtkWidget * widget, cairo_t * cr, AppShellData * app_data)
{
cairo_save(cr);
CtkAllocation widget_allocation;
ctk_widget_get_allocation (widget, &widget_allocation);
cdk_cairo_set_source_color (cr, ctk_widget_get_style (widget)->base);
cairo_set_line_width(cr, 1);
cairo_rectangle(cr, widget_allocation.x, widget_allocation.y, widget_allocation.width, widget_allocation.height);
cairo_stroke_preserve(cr);
cairo_fill(cr);
if (app_data->selected_group)
{
CtkWidget *selected_widget = CTK_WIDGET (app_data->selected_group);
CtkAllocation selected_widget_allocation;
ctk_widget_get_allocation (selected_widget, &selected_widget_allocation);
cdk_cairo_set_source_color (cr, ctk_widget_get_style (selected_widget)->light);
cairo_set_line_width(cr, 1);
cairo_rectangle(cr, selected_widget_allocation.x, selected_widget_allocation.y, selected_widget_allocation.width, selected_widget_allocation.height);
cairo_stroke_preserve(cr);
cairo_fill(cr);
}
cairo_restore(cr);
return FALSE;
}
|