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
|
/*
This file is part of HelpViewer (http://www.roard.com/helpviewer)
Copyright (C) 2003 Nicolas Roard (nicolas@roard.com)
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.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#import "Table.h"
@implementation Table
- (id) init
{
if (self = [super init])
{
table = [[NSMutableArray alloc] init];
}
return self;
}
- (BOOL) addCell: (NSString*) text withSize: (float) size withRowspan: (int) rowspan
withColspan: (int) colspan atRow: (int) row
{
BOOL res = NO;
printf ("addCell with size : %.2f\n", size);
if (row < [table count])
{
TableCell* cell = [[TableCell alloc] initTextCell: text];
[cell setBordered: YES];
[cell setSize: size];
[cell setRowspan: rowspan];
[cell setColspan: colspan];
printf ("rowspan : %d\n", [cell rowspan]);
printf ("sizePercent : %.2f\n", [cell sizePercent]);
[[table objectAtIndex: row] addObject: cell];
res = YES;
}
return res;
}
- (void) addRow
{
[table addObject: [NSMutableArray array]];
}
- (float) height
{
return [table count] * 16;
}
- (void) setWidth: (float) width
{
_width = width - 10;
}
- (NSSize) cellSize
{
int width, height;
if ((table != nil) && ([table count] > 0))
height = 16 * [table count] + 3;
else height = 16 + 3;
if (_width <= 0) width = 300;
else width = _width + 10;
return NSMakeSize (width,height);
}
- (void) moveCellsFrom: (int) pos ofRow: (int) irow of: (int) nb
{
int i;
NSArray* row = [table objectAtIndex: irow];
printf ("moveCellsFrom: %d ofRow: %d\n", pos, irow);
for (i=0; i < [row count]; i++)
{
TableCell* cell = [row objectAtIndex: i];
if ([cell x] >= pos)
{
printf ("[%d] (%d)=><%d>\n", irow, [cell x], [cell x] + nb);
[cell setX: [cell x] + nb];
}
}
}
- (int) numberOfCols
{
int i, j, k;
int ret = 0;
for (i = 0; i < [table count]; i++)
{
NSArray* row = [table objectAtIndex: i];
for (j = 0; j < [row count]; j++)
{
TableCell* cell = [row objectAtIndex: j];
[cell setX: j];
}
}
for (i = 0; i < [table count]; i++)
{
NSArray* row = [table objectAtIndex: i];
for (j = 0; j < [row count]; j++)
{
TableCell* cell = [row objectAtIndex: j];
if ([cell colspan] > 1)
{
if (j+1 < [row count])
[self moveCellsFrom: [cell x] +1 ofRow: i of: [cell colspan] - 1];
}
if ([cell rowspan] > 1)
{
for (k = 1; k < [cell rowspan]; k++)
{
if (i+k < [table count])
{
// On doit dcaler toutes les cellules de la
// ligne du dessous partir de cette position
[self moveCellsFrom: [cell x] ofRow: i+k of: [cell colspan]];
}
}
}
}
}
for (i = 0; i < [table count]; i++)
{
NSArray* row = [table objectAtIndex: i];
for (j = 0; j < [row count]; j++)
{
TableCell* cell = [row objectAtIndex: j];
if([cell x] > ret) ret = [cell x];
}
}
ret++; // (vu qu'on commence l'index 0)
printf ("ret final : %d\n", ret);
return ret;
}
- (void) drawInteriorWithFrame: (NSRect) cellFrame
inView: (NSView*) controllView
{
int i,j;
float width = cellFrame.size.width -3;
int cols = [self numberOfCols];
printf ("cellFrame : x <%.2f> y <%.2f> w <%.2f> h <%.2f>\n", cellFrame.origin.x,
cellFrame.origin.y, cellFrame.size.width, cellFrame.size.height);
printf ("table count : %d \n", [table count]);
for (i = 0; i < [table count]; i++)
{
NSArray* row = [table objectAtIndex: i];
printf ("row count : %d \n", [row count]);
for (j = 0; j < [row count]; j++)
{
NSRect rect;
TableCell* cell = [row objectAtIndex: j];
float widthCell = width / cols;
rect = NSMakeRect (cellFrame.origin.x + 2 + widthCell*[cell x],
cellFrame.origin.y + 2 + i*16, widthCell*[cell colspan] - 1, 16*[cell rowspan] - 1);
[cell drawWithFrame: rect inView: controllView];
printf ("[%d]==>(%d)\n", i, [cell x]);
}
}
}
@end
@implementation TableCell
- (void) setX: (int) px { x = px; }
- (void) setY: (int) py { y = py; }
- (int) x { return x; }
- (int) y { return y; }
- (int) colspan { return colspan; }
- (int) rowspan { return rowspan; }
- (void) setColspan: (int) col { colspan = col; }
- (void) setRowspan: (int) row { rowspan = row; }
- (void) setSize: (float) percent
{
sizeIsPixel = NO;
sizePercent = percent;
}
- (float) sizePercent
{
return sizePercent;
}
@end
|