source: gaycko/trunk/src/render/table.c @ 4768

Last change on this file since 4768 was 4768, checked in by Jean-Yves Lamoureux, 9 years ago
  • Reworked table handling
File size: 3.3 KB
Line 
1/*
2 *  Gaycko        Text mode web browser
3 *  Copyright (c) 2011 Jean-Yves Lamoureux <jylam@lnxscene.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13#include "renderer.h"
14void gaycko_fix_table(gNode *table) {
15        unsigned int tr = 0;
16    unsigned int td = 0;
17        table->specific.table->tr_count = 0;
18   
19        /* Count TR by TABLE, and TD by TR */
20        for(tr = 0 ; tr < table->children_count; tr++) {
21                if(table->children[tr]->type == ELEM_TR) {
22            table->specific.table->tr = realloc(table->specific.table->tr,
23                                                sizeof(gNode*)*(table->specific.table->tr_count+1));
24            table->specific.table->tr[table->specific.table->tr_count] = table->children[tr];
25                        table->specific.table->tr_count++;
26            gaycko_fix_tr(table->children[tr]);
27                }
28        }
29   
30    /* Get largest TD by TR, and resize same column accordingly */
31    for(td = 0; td < table->specific.table->tr[0]->specific.tr->td_count; td++) {
32        unsigned int widest_td = 0;
33       
34        /* Get largest TD on this column */
35        for(tr = 0; tr < table->specific.table->tr_count; tr++) {
36            unsigned int w = gaycko_get_td_width_at(table->specific.table->tr[tr], td);
37            if(w>widest_td) widest_td=w;
38        }
39        /* Resize the others to this width */
40        for(tr = 0; tr < table->specific.table->tr_count; tr++) {
41            gaycko_set_td_width_at(table->specific.table->tr[tr], td, widest_td);
42        }
43    }
44}
45
46void gaycko_fix_tr(gNode *tr) {
47        unsigned int td = 0;
48        tr->specific.tr->td_count = 0;
49   
50        for(td = 0 ; td < tr->children_count; td++) {
51                if(tr->children[td]->type == ELEM_TD) {
52            tr->specific.tr->td = realloc(tr->specific.tr->td,
53                                                sizeof(gNode*)*(tr->specific.tr->td_count+1));
54            tr->specific.tr->td[tr->specific.tr->td_count] = tr->children[td];
55                        tr->specific.tr->td_count++;
56                }
57        }
58}
59
60/* Set TD width and offset following TD's accordingly */
61void gaycko_set_td_width_at(gNode *tr, unsigned int td, unsigned int width) {
62   
63   
64   
65    unsigned int add_x = width-tr->specific.tr->td[td]->properties->width;
66    unsigned int tdn   = 0;
67   
68   
69    tr->specific.tr->td[td]->properties->width = width;
70   
71    for(tdn = td+1; tdn < tr->specific.tr->td_count; tdn++) {
72        unsigned int oldw = tr->specific.tr->td[tdn]->properties->width;
73        gaycko_move_td_contents(tr->specific.tr->td[tdn], add_x);
74        add_x+=(width-oldw);
75    }
76}
77
78/* Move TD to the right (add_x) and do the same for its children */
79void gaycko_move_td_contents(gNode *td, unsigned int add_x) {
80    unsigned int c = 0;
81       
82    td->properties->x+=add_x;
83   
84    for(c = 0; c < td->children_count; td++) {
85        gaycko_move_td_contents(td->children[c], add_x);
86    }
87}
88
89
90/* Get TD width at specified offset of a TR */
91unsigned int gaycko_get_td_width_at(gNode *tr, unsigned int td) {
92    return gaycko_get_td_width(tr->specific.tr->td[td]);
93}
94
95
96/* Get TD width */
97unsigned int gaycko_get_td_width(gNode *td) {
98    return td->properties->width;
99}
100
Note: See TracBrowser for help on using the repository browser.