aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/table/e-cell-hbox.c
blob: 722f1eb5070b99802baf4629b0f775d2b025f2f6 (plain) (blame)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * e-cell-hbox.c - Hbox cell object.
 * Copyright 2006 Novell, Inc.
 *
 * Authors:
 *   Srinivasa Ragavan <sragavan@novell.com>
 *
 * A majority of code taken from:
 *
 * the ECellText renderer.
 * Copyright 1999, 2000, Ximian, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License, version 2, as published by the Free Software Foundation.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include <config.h>

#include <ctype.h>
#include <math.h>
#include <stdio.h>

#include <gtk/gtk.h>

/* #include "a11y/e-table/gal-a11y-e-cell-registry.h" */
/* #include "a11y/e-table/gal-a11y-e-cell-vbox.h" */
#include "e-util/e-util.h"

#include "e-cell-hbox.h"
#include "e-table-item.h"

#define PARENT_TYPE e_cell_get_type ()

static ECellClass *parent_class;

#define INDENT_AMOUNT 16
#define MAX_CELL_SIZE 25

/*
 * ECell::new_view method
 */
static ECellView *
ecv_new_view (ECell *ecell, ETableModel *table_model, void *e_table_item_view)
{
    ECellHbox *ecv = E_CELL_HBOX (ecell);
    ECellHboxView *hbox_view = g_new0 (ECellHboxView, 1);
    int i;
    
    hbox_view->cell_view.ecell = ecell;
    hbox_view->cell_view.e_table_model = table_model;
    hbox_view->cell_view.e_table_item_view = e_table_item_view;
    
    /* create our subcell view */
    hbox_view->subcell_view_count = ecv->subcell_count;
    hbox_view->subcell_views = g_new (ECellView *, hbox_view->subcell_view_count);
    hbox_view->model_cols = g_new (int, hbox_view->subcell_view_count);
    hbox_view->def_size_cols = g_new (int, hbox_view->subcell_view_count);

    for (i = 0; i < hbox_view->subcell_view_count; i++) {
        hbox_view->subcell_views[i] = e_cell_new_view (ecv->subcells[i], table_model, e_table_item_view /* XXX */);
        hbox_view->model_cols[i] = ecv->model_cols[i];
        hbox_view->def_size_cols[i] = ecv->def_size_cols[i];
    }

    return (ECellView *)hbox_view;
}

/*
 * ECell::kill_view method
 */
static void
ecv_kill_view (ECellView *ecv)
{
    ECellHboxView *hbox_view = (ECellHboxView *) ecv;
    int i;

    /* kill our subcell view */
    for (i = 0; i < hbox_view->subcell_view_count; i++)
        e_cell_kill_view (hbox_view->subcell_views[i]);

    g_free (hbox_view->model_cols);
    g_free (hbox_view->def_size_cols);
    g_free (hbox_view->subcell_views);
    g_free (hbox_view);
}

/*
 * ECell::realize method
 */
static void
ecv_realize (ECellView *ecell_view)
{
    ECellHboxView *hbox_view = (ECellHboxView *) ecell_view;
    int i;

    /* realize our subcell view */
    for (i = 0; i < hbox_view->subcell_view_count; i++)
        e_cell_realize (hbox_view->subcell_views[i]);

    if (parent_class->realize)
        (* parent_class->realize) (ecell_view);
}

/*
 * ECell::unrealize method
 */
static void
ecv_unrealize (ECellView *ecv)
{
    ECellHboxView *hbox_view = (ECellHboxView *) ecv;
    int i;

    /* unrealize our subcell view. */
    for (i = 0; i < hbox_view->subcell_view_count; i++)
        e_cell_unrealize (hbox_view->subcell_views[i]);

    if (parent_class->unrealize)
        (* parent_class->unrealize) (ecv);
}

/*
 * ECell::draw method
 */
static void
ecv_draw (ECellView *ecell_view, GdkDrawable *drawable,
      int model_col, int view_col, int row, ECellFlags flags,
      int x1, int y1, int x2, int y2)
{
    ECellHboxView *hbox_view = (ECellHboxView *)ecell_view;

    int subcell_offset = 0;
    int i;
    int allotted_width = x2-x1;
    
    for (i = 0; i < hbox_view->subcell_view_count; i++) {
        /* Now cause our subcells to draw their contents,
           shifted by subcell_offset pixels */
        int width = allotted_width * hbox_view->def_size_cols[i] / 100;
            //e_cell_max_width_by_row (hbox_view->subcell_views[i], hbox_view->model_cols[i], view_col, row);
//      if (width < hbox_view->def_size_cols[i])
    //      width = hbox_view->def_size_cols[i];
//      printf("width of %d %d of %d\n", width,hbox_view->def_size_cols[i], allotted_width );
        e_cell_draw (hbox_view->subcell_views[i], drawable,
                 hbox_view->model_cols[i], view_col, row, flags,
                 x1 + subcell_offset , y1, x1 + subcell_offset + width, y2);

        subcell_offset += width; //e_cell_max_width_by_row (hbox_view->subcell_views[i], hbox_view->model_cols[i], view_col, row);
    }
}

/*
 * ECell::event method
 */
static gint
ecv_event (ECellView *ecell_view, GdkEvent *event, int model_col, int view_col, int row, ECellFlags flags, ECellActions *actions)
{
    ECellHboxView *hbox_view = (ECellHboxView *)ecell_view;
    int y = 0;
    int i;
    int subcell_offset = 0;

    switch (event->type) {
    case GDK_BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
    case GDK_2BUTTON_PRESS:
    case GDK_3BUTTON_PRESS:
        y = event->button.y;
        break;
    case GDK_MOTION_NOTIFY:
        y = event->motion.y;
        break;
    default:
        /* nada */
        break;
    }

    for (i = 0; i < hbox_view->subcell_view_count; i++) {
        int width = e_cell_max_width_by_row (hbox_view->subcell_views[i], hbox_view->model_cols[i], view_col, row);
        if (width < hbox_view->def_size_cols[i])
            width = hbox_view->def_size_cols[i];
        if (y < subcell_offset + width)
            return e_cell_event(hbox_view->subcell_views[i], event, hbox_view->model_cols[i], view_col, row, flags, actions);
        subcell_offset += width;
    }
    return 0;
}

/*
 * ECell::height method
 */
static int
ecv_height (ECellView *ecell_view, int model_col, int view_col, int row) 
{
    ECellHboxView *hbox_view = (ECellHboxView *)ecell_view;
    int height = 0, max_height = 0;
    int i;

    for (i = 0; i < hbox_view->subcell_view_count; i++) {
        height = e_cell_height (hbox_view->subcell_views[i], hbox_view->model_cols[i], view_col, row);
        max_height = MAX(max_height, height);
    }
    return max_height;
}

/*
 * ECell::max_width method
 */
static int
ecv_max_width (ECellView *ecell_view, int model_col, int view_col)
{
    ECellHboxView *hbox_view = (ECellHboxView *)ecell_view;
    int width = 0;
    int i;

    for (i = 0; i < hbox_view->subcell_view_count; i++) {
        int cell_width = e_cell_max_width (hbox_view->subcell_views[i], hbox_view->model_cols[i], view_col);
        
        if (cell_width < hbox_view->def_size_cols[i])
            cell_width = hbox_view->def_size_cols[i];   
        width += cell_width;
    }

    return width;
}

/*
 * GObject::dispose method
 */
static void
ecv_dispose (GObject *object)
{
    ECellHbox *ecv = E_CELL_HBOX (object);
    int i;

    /* destroy our subcell */
    for (i = 0; i < ecv->subcell_count; i++)
        if (ecv->subcells[i])
            g_object_unref (ecv->subcells[i]);
    g_free (ecv->subcells);
    ecv->subcells = NULL;
    ecv->subcell_count = 0;

    G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
e_cell_hbox_class_init (GObjectClass *object_class)
{
    ECellClass *ecc = (ECellClass *) object_class;

    object_class->dispose = ecv_dispose;

    ecc->new_view         = ecv_new_view;
    ecc->kill_view        = ecv_kill_view;
    ecc->realize          = ecv_realize;
    ecc->unrealize        = ecv_unrealize;
    ecc->draw             = ecv_draw;
    ecc->event            = ecv_event;
    ecc->height           = ecv_height;

    ecc->max_width        = ecv_max_width;

    parent_class = g_type_class_ref (PARENT_TYPE);

/*  gal_a11y_e_cell_registry_add_cell_type (NULL, E_CELL_HBOX_TYPE, gal_a11y_e_cell_hbox_new); */
}

static void
e_cell_hbox_init (GtkObject *object)
{
    ECellHbox *ecv = E_CELL_HBOX (object);

    ecv->subcells = NULL;
    ecv->subcell_count = 0;
}

E_MAKE_TYPE(e_cell_hbox, "ECellHbox", ECellHbox, e_cell_hbox_class_init, e_cell_hbox_init, PARENT_TYPE)

/**
 * e_cell_hbox_new:
 * 
 * Creates a new ECell renderer that can be used to render multiple
 * child cells.
 * 
 * Return value: an ECell object that can be used to render multiple
 * child cells.
 **/
ECell *
e_cell_hbox_new (void)
{
    ECellHbox *ecv = g_object_new (E_CELL_HBOX_TYPE, NULL);

    return (ECell *) ecv;
}

void
e_cell_hbox_append (ECellHbox *hbox, ECell *subcell, int model_col, int size)
{
    hbox->subcell_count ++;

    hbox->subcells   = g_renew (ECell *, hbox->subcells,   hbox->subcell_count);
    hbox->model_cols = g_renew (int,     hbox->model_cols, hbox->subcell_count);
    hbox->def_size_cols = g_renew (int,     hbox->def_size_cols, hbox->subcell_count);
    
    hbox->subcells[hbox->subcell_count - 1]   = subcell;
    hbox->model_cols[hbox->subcell_count - 1] = model_col;
    hbox->def_size_cols[hbox->subcell_count - 1] = size;

    if (subcell)
        g_object_ref (subcell);
}