Implement SheetBlockStyle using gtables.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@2869 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Dave Peticolas 2000-09-16 20:53:48 +00:00
parent 5ef2d46f48
commit 73e562ab36
7 changed files with 236 additions and 199 deletions

View File

@ -260,6 +260,7 @@ draw_cell (GnucashGrid *grid, int block,
Table *table = grid->sheet->table;
gchar *text;
GdkFont *font;
CellStyle *cs;
SheetBlock *sheet_block;
VirtualCellLocation vcell_loc = { block, 0 };
@ -272,22 +273,24 @@ draw_cell (GnucashGrid *grid, int block,
gdk_gc_set_foreground (grid->gc, &gn_black);
cs = gnucash_style_get_cell_style (style, i, j);
/* top */
if (style->borders[i][j] & STYLE_BORDER_TOP)
if (cs->border & STYLE_BORDER_TOP)
gdk_draw_line (drawable, grid->gc, x, y, x+width, y);
/* right */
if (style->borders[i][j] & STYLE_BORDER_RIGHT)
if (cs->border & STYLE_BORDER_RIGHT)
gdk_draw_line (drawable, grid->gc, x+width, y,
x+width, y+height);
/* bottom */
if (style->borders[i][j] & STYLE_BORDER_BOTTOM)
if (cs->border & STYLE_BORDER_BOTTOM)
gdk_draw_line (drawable, grid->gc, x+width,
y+height, x, y+height);
/* left */
if (style->borders[i][j] & STYLE_BORDER_LEFT)
if (cs->border & STYLE_BORDER_LEFT)
gdk_draw_line (drawable, grid->gc, x, y+height, x, y);
#undef ROUNDED_CORNERS
@ -302,7 +305,7 @@ draw_cell (GnucashGrid *grid, int block,
gdk_draw_line (drawable, grid->gc, x+width, y+5, x+width, y+height-5);
gdk_draw_line (drawable, grid->gc, x+width-5, y+height, x+5, y+height);
gdk_draw_line (drawable, grid->gc, x, y+height-5, x, y+5);
gdk_draw_arc (drawable, grid->gc, FALSE,
x, y,
10, 10,
@ -334,7 +337,7 @@ draw_cell (GnucashGrid *grid, int block,
(!text || strlen(text) == 0)) {
font = grid->italic_font;
gdk_gc_set_foreground (grid->gc, &gn_light_gray);
text = style->labels[i][j];
text = cs->label;
}
if (text) {
@ -343,7 +346,7 @@ draw_cell (GnucashGrid *grid, int block,
y_offset = height - MAX(CELL_VPADDING, font->descent + 4);
switch (style->alignments[i][j ]) {
switch (cs->alignment) {
default:
case GTK_JUSTIFY_LEFT:
case GTK_JUSTIFY_FILL:

View File

@ -71,12 +71,14 @@ gnucash_header_draw (GnomeCanvasItem *item, GdkDrawable *drawable,
int w = 0, h = 0;
gchar *text;
GdkFont *font;
CellStyle *cs;
header_style = header->sheet->cursor_style[GNUCASH_CURSOR_HEADER];
cs = gnucash_style_get_cell_style (header_style, 0, 0);
/* Assume all cells have the same color */
gdk_gc_set_foreground(header->gc,
header_style->inactive_bg_color[0][0]);
gdk_gc_set_foreground(header->gc, cs->inactive_bg_color);
gdk_draw_rectangle(drawable, header->gc, TRUE, 0, 0, width, height);
gdk_gc_set_line_attributes (header->gc, 1, GDK_LINE_SOLID, -1, -1);
@ -105,6 +107,7 @@ gnucash_header_draw (GnomeCanvasItem *item, GdkDrawable *drawable,
consistently, and cut down on maintenance issues. */
for (j = 0; j < style->ncols; j++) {
cd = gnucash_style_get_cell_dimensions (style, i, j);
cs = gnucash_style_get_cell_style (style, i, j);
if (header->in_resize && (j == header->resize_col))
w = header->resize_col_width;
@ -116,7 +119,7 @@ gnucash_header_draw (GnomeCanvasItem *item, GdkDrawable *drawable,
gdk_draw_rectangle (drawable, header->gc, FALSE,
xpaint, ypaint, w, h);
text = style->labels[i][j];
text = cs->label;
if (text) {
gint x_offset, y_offset;
@ -125,7 +128,7 @@ gnucash_header_draw (GnomeCanvasItem *item, GdkDrawable *drawable,
y_offset = h - MAX(CELL_VPADDING,
font->descent + 4);
switch (style->alignments[i][j]) {
switch (cs->alignment) {
default:
case GTK_JUSTIFY_LEFT:
case GTK_JUSTIFY_FILL:

View File

@ -123,6 +123,8 @@ item_edit_draw_info(ItemEdit *item_edit, int x, int y, TextDrawInfo *info)
GtkJustification align;
SheetBlockStyle *style;
GtkEditable *editable;
CellStyle *cs;
int text_len, total_width;
int pre_cursor_width;
int width_1, width_2;
@ -135,8 +137,11 @@ item_edit_draw_info(ItemEdit *item_edit, int x, int y, TextDrawInfo *info)
info->font = GNUCASH_GRID(item_edit->sheet->grid)->normal_font;
info->bg_color = style->active_bg_color[item_edit->cell_row]
[item_edit->cell_col];
cs = gnucash_style_get_cell_style (style,
item_edit->cell_row,
item_edit->cell_col);
info->bg_color = cs->active_bg_color;
info->fg_color = &gn_black;
info->bg_color2 = &gn_dark_gray;
@ -176,8 +181,7 @@ item_edit_draw_info(ItemEdit *item_edit, int x, int y, TextDrawInfo *info)
info->bg_rect.width = wd - (2 * CELL_HPADDING);
info->bg_rect.height = hd - (2 * CELL_VPADDING - info->font->descent);
align = item_edit->style->alignments[item_edit->cell_row]
[item_edit->cell_col];
align = cs->alignment;
toggle_space = (item_edit->is_combo) ?
item_edit->combo_toggle.toggle_offset : 0;
@ -492,9 +496,13 @@ item_edit_set_cursor_pos (ItemEdit *item_edit,
if (changed_cells) {
GtkJustification align;
CellStyle *cs;
align = item_edit->style->alignments[item_edit->cell_row]
[item_edit->cell_col];
cs = gnucash_style_get_cell_style (item_edit->style,
item_edit->cell_row,
item_edit->cell_col);
align = cs->alignment;
if (align == GTK_JUSTIFY_RIGHT)
gtk_editable_set_position(editable, -1);

View File

@ -2124,7 +2124,10 @@ gnucash_sheet_col_max_width (GnucashSheet *sheet, gint virt_col, gint cell_col)
font = GNUCASH_GRID(sheet->grid)->normal_font;
if (!text || strlen(text) == 0) {
text = style->labels[cell_row][cell_col];
CellStyle *cs;
cs = gnucash_style_get_cell_style (style, cell_row, cell_col);
text = cs->label;
font = style->header_font;
}

View File

@ -63,12 +63,10 @@ typedef struct _SheetBlockStyle SheetBlockStyle;
typedef struct
{
/* the virtual row/column in the table this block
is associated to */
/* The virtual location in the table of this block */
VirtualCellLocation vcell_loc;
/* The style for this block, derived from the handlers for
the virt row/col */
/* The style for this block */
SheetBlockStyle *style;
GdkColor ***fg_colors;

View File

@ -1215,28 +1215,197 @@ gnucash_sheet_style_set_col_width (GnucashSheet *sheet, SheetBlockStyle *style,
}
/* Recompiles the style information from the cellblock, without
* recomputing the layout info or the dimensions. WARNING: this
* function assumes that the space for the style info has been
* allocated already. */
void
gnucash_sheet_style_destroy (GnucashSheet *sheet, SheetBlockStyle *style)
gnucash_sheet_style_recompile(SheetBlockStyle *style, CellBlock *cellblock,
SplitRegister *sr, gint cursor_type)
{
gint i, j;
gint i, j, type;
char *label;
for (i = 0; i < style->nrows; i++) {
for (j = 0; j < style->ncols; j++) {
CellBlockCell *cb_cell;
CellStyle *cs;
cb_cell = gnc_cellblock_get_cell (cellblock, i, j);
cs = gnucash_style_get_cell_style (style, i, j);
type = cb_cell->cell_type;
style->header_font = gnucash_register_font;
gnucash_style_set_borders (style, reg_borders);
if (type > -1)
label = sr->header_label_cells[type]->value;
else if (cursor_type == GNUCASH_CURSOR_HEADER)
label = cb_cell->cell->value;
else
label = "";
g_free(cs->label);
cs->label = g_strdup(label);
cs->active_bg_color =
gnucash_color_argb_to_gdk
(cellblock->active_bg_color);
cs->inactive_bg_color =
gnucash_color_argb_to_gdk
(cellblock->passive_bg_color);
switch (cb_cell->alignment) {
case CELL_ALIGN_RIGHT:
cs->alignment = GTK_JUSTIFY_RIGHT;
break;
case CELL_ALIGN_CENTER:
cs->alignment = GTK_JUSTIFY_CENTER;
break;
default:
case CELL_ALIGN_FILL:
case CELL_ALIGN_LEFT:
cs->alignment = GTK_JUSTIFY_LEFT;
break;
}
}
}
}
void
gnucash_style_set_cell_borders (SheetBlockStyle *style,
int row, int col, int border)
{
CellStyle *cs;
if (style == NULL)
return;
cs = gnucash_style_get_cell_style (style, row, col);
if (cs == NULL)
return;
cs->border = border;
}
void
gnucash_style_set_register_borders (int reg_borders_new)
{
reg_borders = reg_borders_new;
}
void
gnucash_style_set_borders (SheetBlockStyle *style, int border)
{
int row, col;
g_return_if_fail (style != NULL);
for ( i = 0; i < style->nrows; i++) {
g_free(style->alignments[i]);
g_free(style->active_bg_color[i]);
g_free(style->inactive_bg_color[i]);
for (j = 0; j < style->ncols; j++)
g_free (style->labels[i][j]);
g_free (style->labels[i]);
g_free (style->borders[i]);
}
for (row = 0; row < style->nrows; row++) {
CellStyle *cs;
g_free(style->alignments);
g_free(style->active_bg_color);
g_free(style->inactive_bg_color);
g_free(style->labels);
g_free (style->borders);
for (col = 0; col < style->ncols; col++)
gnucash_style_set_cell_borders (style, row, col,
border);
cs = gnucash_style_get_cell_style (style, row, 0);
cs->border |= STYLE_BORDER_LEFT;
cs = gnucash_style_get_cell_style (style, row,
style->ncols - 1);
cs->border |= STYLE_BORDER_RIGHT;
}
}
void
gnucash_sheet_set_borders (GnucashSheet *sheet, int border)
{
int i;
g_return_if_fail (GNUCASH_IS_SHEET (sheet));
for (i = 0; i < GNUCASH_CURSOR_LAST; i++)
gnucash_style_set_borders (sheet->cursor_style[i], border);
}
CellStyle *
gnucash_style_get_cell_style (SheetBlockStyle *style, int row, int col)
{
if (style == NULL)
return NULL;
return g_table_index (style->cell_styles, row, col);
}
static gpointer
cell_style_new (void)
{
CellStyle *cs;
cs = g_new0 (CellStyle, 1);
return cs;
}
static void
cell_style_free (gpointer _cs)
{
CellStyle *cs = _cs;
g_free(cs->label);
cs->label = NULL;
g_free(cs);
}
SheetBlockStyle *
gnucash_sheet_style_compile (GnucashSheet *sheet, CellBlock *cellblock,
gint cursor_type)
{
SheetBlockStyle *style;
SplitRegister *sr;
g_return_val_if_fail (sheet != NULL, NULL);
g_return_val_if_fail (GNUCASH_IS_SHEET (sheet), NULL);
g_return_val_if_fail (cellblock != NULL, NULL);
sr = sheet->split_register;
style = g_new0(SheetBlockStyle, 1);
style->reg_type = sr->type;
style->cursor_type = cursor_type;
style->nrows = cellblock->num_rows;
style->ncols = cellblock->num_cols;
style->cell_styles = g_table_new (cell_style_new, cell_style_free);
g_table_resize (style->cell_styles, style->nrows, style->ncols);
gnucash_sheet_style_recompile(style, cellblock, sr, cursor_type);
gnucash_style_layout_init (sheet, style);
gnucash_style_dimensions_init (sheet, style);
gnucash_sheet_style_set_dimensions (sheet, style, DEFAULT_STYLE_WIDTH);
return style;
}
void
gnucash_sheet_style_destroy (GnucashSheet *sheet, SheetBlockStyle *style)
{
if (sheet == NULL)
return;
if (style == NULL)
return;
g_table_destroy (style->cell_styles);
style->cell_styles = NULL;
style->layout_info->refcount--;
@ -1258,159 +1427,6 @@ gnucash_sheet_style_destroy (GnucashSheet *sheet, SheetBlockStyle *style)
}
/* Recompiles the style information from the cellblock, without
* recomputing the layout info or the dimensions. WARNING: this
* function assumes that the space for the style info has been
* allocated already. */
void
gnucash_sheet_style_recompile(SheetBlockStyle *style, CellBlock *cellblock,
SplitRegister *sr, gint cursor_type)
{
gint i, j, type;
char *label;
for (i = 0; i < style->nrows; i++) {
for (j = 0; j < style->ncols; j++) {
CellBlockCell *cb_cell;
cb_cell = gnc_cellblock_get_cell (cellblock, i, j);
type = cb_cell->cell_type;
style->header_font = gnucash_register_font;
gnucash_style_set_borders (style, reg_borders);
if (type > -1)
label = sr->header_label_cells[type]->value;
else if (cursor_type == GNUCASH_CURSOR_HEADER)
label = cb_cell->cell->value;
else
label = "";
g_free(style->labels[i][j]);
style->labels[i][j] = g_strdup(label);
style->active_bg_color[i][j] =
gnucash_color_argb_to_gdk
(cellblock->active_bg_color);
style->inactive_bg_color[i][j] =
gnucash_color_argb_to_gdk
(cellblock->passive_bg_color);
switch (cb_cell->alignment) {
case CELL_ALIGN_RIGHT:
style->alignments[i][j] =
GTK_JUSTIFY_RIGHT;
break;
case CELL_ALIGN_CENTER:
style->alignments[i][j] =
GTK_JUSTIFY_CENTER;
break;
default:
case CELL_ALIGN_FILL:
case CELL_ALIGN_LEFT:
style->alignments[i][j] =
GTK_JUSTIFY_LEFT;
break;
}
}
}
}
void
gnucash_style_set_cell_borders (SheetBlockStyle *style,
int row, int col, int border)
{
g_return_if_fail (style != NULL);
if (row >= 0 && row < style->nrows && col >= 0 && col < style->ncols)
style->borders[row][col] = border;
}
void
gnucash_style_set_register_borders (int reg_borders_new)
{
reg_borders = reg_borders_new;
}
void
gnucash_style_set_borders (SheetBlockStyle *style, int border)
{
int row, col;
g_return_if_fail (style != NULL);
for (row = 0; row < style->nrows; row++) {
for (col = 0; col < style->ncols; col++)
gnucash_style_set_cell_borders (style, row, col,
border);
style->borders[row][0] |= STYLE_BORDER_LEFT;
style->borders[row][style->ncols - 1] |= STYLE_BORDER_RIGHT;
}
}
void
gnucash_sheet_set_borders (GnucashSheet *sheet, int border)
{
int i;
g_return_if_fail (GNUCASH_IS_SHEET (sheet));
for (i = 0; i < GNUCASH_CURSOR_LAST; i++)
gnucash_style_set_borders (sheet->cursor_style[i], border);
}
SheetBlockStyle *
gnucash_sheet_style_compile (GnucashSheet *sheet, CellBlock *cellblock,
gint cursor_type)
{
SheetBlockStyle *style;
SplitRegister *sr;
gint i;
g_return_val_if_fail (sheet != NULL, NULL);
g_return_val_if_fail (GNUCASH_IS_SHEET (sheet), NULL);
g_return_val_if_fail (cellblock != NULL, NULL);
sr = sheet->split_register;
style = g_new0(SheetBlockStyle, 1);
style->reg_type = sr->type;
style->cursor_type = cursor_type;
style->nrows = cellblock->num_rows;
style->ncols = cellblock->num_cols;
style->alignments = g_new0 (GtkJustification *, style->nrows);
style->active_bg_color = g_new0 (GdkColor **, style->nrows);
style->inactive_bg_color = g_new0 (GdkColor **, style->nrows);
style->labels = g_new0 (char **, style->nrows);
style->borders = g_new0 (int *, style->nrows);
for ( i = 0; i < style->nrows; i++) {
style->alignments[i] = g_new0 (GtkJustification, style->ncols);
style->active_bg_color[i] = g_new0 (GdkColor *, style->ncols);
style->inactive_bg_color[i] = g_new0(GdkColor *, style->ncols);
style->labels[i] = g_new0 (char *, style->ncols);
style->borders[i] = g_new0 (int, style->ncols);
}
gnucash_sheet_style_recompile(style, cellblock, sr, cursor_type);
gnucash_style_layout_init (sheet, style);
gnucash_style_dimensions_init (sheet, style);
gnucash_sheet_style_set_dimensions (sheet, style, DEFAULT_STYLE_WIDTH);
return style;
}
/* FIXME: maybe we can precompute these for each style */
void
gnucash_sheet_style_get_cell_pixel_rel_coords (SheetBlockStyle *style,

View File

@ -57,8 +57,16 @@ typedef struct
gint refcount;
} BlockDimensions;
typedef struct _CellLayoutInfo CellLayoutInfo;
typedef struct
{
gchar *label;
GtkJustification alignment;
GdkColor *active_bg_color;
GdkColor *inactive_bg_color;
int border;
} CellStyle;
typedef struct _CellLayoutInfo CellLayoutInfo;
struct _SheetBlockStyle
{
gint nrows;
@ -70,15 +78,10 @@ struct _SheetBlockStyle
CellLayoutInfo *layout_info;
BlockDimensions *dimensions;
gchar ***labels; /* for the header */
GTable *cell_styles;
GdkFont *header_font;
GtkJustification **alignments;
GdkColor ***active_bg_color;
GdkColor ***inactive_bg_color;
int **borders;
gint refcount;
};
@ -98,6 +101,9 @@ gint gnucash_style_col_is_resizable (SheetBlockStyle *style, int col);
CellDimensions * gnucash_style_get_cell_dimensions (SheetBlockStyle *style,
int row, int col);
CellStyle * gnucash_style_get_cell_style (SheetBlockStyle *style,
int row, int col);
void gnucash_sheet_style_set_col_width (GnucashSheet *sheet,
SheetBlockStyle *style,
int col, int width, int same_size);