Get the item_edit cell padding from CSS instead of defines

This commit sets up getting the vertical and horizontal item_edit cell
margins, borders and padding from CSS. This increases the option to
specify individual values for top, right, bottom and left instead of
just specifying just two values.
This commit is contained in:
Robert Fewell 2017-11-01 11:54:05 +00:00
parent 4f9716362c
commit 9f76441d50
4 changed files with 124 additions and 27 deletions

View File

@ -21,13 +21,14 @@
color: mix (currentColor, grey, 0.2); color: mix (currentColor, grey, 0.2);
} }
/* Register Cursor padding settings, make sure entry matches sheet.h */ /* Register Cursor settings, top, right, bottom, left */
cursor entry { .cursor .entry {
padding: 2px 5px 2px 5px; margin: 2px 5px 2px 5px; /* this only works by doing it in code, yellow area */
padding: 2px 2px 2px 2px; /* all work with different values, around the text blue area */
} }
cursor button { .cursor .button {
padding: 1px 1px 1px 1px; margin: 1px 1px 1px 1px; /* does not work, not used, here for completeness */
} }
/* Register defaults */ /* Register defaults */
@ -37,7 +38,7 @@ cursor button {
@define-color register_split_bg_color #EDE7D3; @define-color register_split_bg_color #EDE7D3;
@define-color register_cursor_bg_color #FFEF98; @define-color register_cursor_bg_color #FFEF98;
.register-header { *.register-header {
background-color: @register_header_bg_color; background-color: @register_header_bg_color;
} }

View File

@ -12,13 +12,14 @@
color: @negative-numbers; color: @negative-numbers;
} }
/* Register Cursor padding settings, make sure entry matches sheet.h */ /* Register Cursor settings, top, right, bottom, left */
cursor entry { cursor entry {
padding: 2px 5px 2px 5px; margin: 2px 5px 2px 5px;
padding: 0px 2px 0px 2px;
} }
cursor button { cursor button {
padding: 1px 1px 1px 1px; margin: 1px 1px 1px 1px;
} }
/* Register defaults */ /* Register defaults */
@ -28,7 +29,7 @@ cursor button {
@define-color register_split_bg_color #EDE7D3; @define-color register_split_bg_color #EDE7D3;
@define-color register_cursor_bg_color #FFEF98; @define-color register_cursor_bg_color #FFEF98;
.register-header { *.register-header {
background-color: @register_header_bg_color; background-color: @register_header_bg_color;
} }

View File

@ -361,6 +361,7 @@ draw_background_cb (GtkWidget *widget, cairo_t *cr, gpointer user_data)
static gboolean static gboolean
draw_text_cursor_cb (GtkWidget *widget, cairo_t *cr, gpointer user_data) draw_text_cursor_cb (GtkWidget *widget, cairo_t *cr, gpointer user_data)
{ {
GncItemEdit *item_edit = GNC_ITEM_EDIT(user_data);
GtkEditable *editable = GTK_EDITABLE(widget); GtkEditable *editable = GTK_EDITABLE(widget);
GtkStyleContext *stylectxt = gtk_widget_get_style_context (GTK_WIDGET(widget)); GtkStyleContext *stylectxt = gtk_widget_get_style_context (GTK_WIDGET(widget));
gint height = gtk_widget_get_allocated_height (widget); gint height = gtk_widget_get_allocated_height (widget);
@ -401,8 +402,15 @@ draw_text_cursor_cb (GtkWidget *widget, cairo_t *cr, gpointer user_data)
// Now draw a vertical line // Now draw a vertical line
cairo_set_source_rgb (cr, fg_color->red, fg_color->green, fg_color->blue); cairo_set_source_rgb (cr, fg_color->red, fg_color->green, fg_color->blue);
cairo_set_line_width (cr, 1.0); cairo_set_line_width (cr, 1.0);
cairo_move_to (cr, cursor_x + 0.5, 2); #if GTK_CHECK_VERSION(3,20,0)
cairo_rel_line_to (cr, 0, height - 4); cairo_move_to (cr, cursor_x + 0.5, gnc_item_edit_get_margin (item_edit, top) +
gnc_item_edit_get_padding_border (item_edit, top));
cairo_rel_line_to (cr, 0, height - gnc_item_edit_get_margin (item_edit, top_bottom) -
gnc_item_edit_get_padding_border (item_edit, top_bottom));
#else
cairo_move_to (cr, cursor_x + 0.5, gnc_item_edit_get_padding_border (item_edit, top));
cairo_rel_line_to (cr, 0, height - gnc_item_edit_get_padding_border (item_edit, top_bottom));
#endif
cairo_stroke (cr); cairo_stroke (cr);
return FALSE; return FALSE;
@ -608,13 +616,60 @@ gnc_item_edit_get_type (void)
return gnc_item_edit_type; return gnc_item_edit_type;
} }
gint
gnc_item_edit_get_margin (GncItemEdit *item_edit, Sides side)
{
switch (side)
{
case left:
return item_edit->margin.left;
case right:
return item_edit->margin.right;
case top:
return item_edit->margin.top;
case bottom:
return item_edit->margin.bottom;
case left_right:
return item_edit->margin.left + item_edit->margin.right;
case top_bottom:
return item_edit->margin.top + item_edit->margin.bottom;
default:
return 2;
}
}
gint
gnc_item_edit_get_padding_border (GncItemEdit *item_edit, Sides side)
{
switch (side)
{
case left:
return item_edit->padding.left + item_edit->border.left;
case right:
return item_edit->padding.right + item_edit->border.right;
case top:
return item_edit->padding.top + item_edit->border.top;
case bottom:
return item_edit->padding.bottom + item_edit->border.bottom;
case left_right:
return item_edit->padding.left + item_edit->border.left +
item_edit->padding.right + item_edit->border.right;
case top_bottom:
return item_edit->padding.top + item_edit->border.top +
item_edit->padding.bottom + item_edit->border.bottom;
default:
return 2;
}
}
GtkWidget * GtkWidget *
gnc_item_edit_new (GnucashSheet *sheet) gnc_item_edit_new (GnucashSheet *sheet)
{ {
char *hpad_str, *vpad_str, *entry_css; GtkStyleContext *stylectxt;
GtkStyleContext *stylecontext; GtkBorder padding;
GtkCssProvider *provider; GtkBorder margin;
GtkBorder border;
GtkWidget *vb = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
GncItemEdit *item_edit = GncItemEdit *item_edit =
g_object_new (GNC_TYPE_ITEM_EDIT, g_object_new (GNC_TYPE_ITEM_EDIT,
"sheet", sheet, "sheet", sheet,
@ -630,32 +685,54 @@ gnc_item_edit_new (GnucashSheet *sheet)
item_edit->editor = gtk_entry_new(); item_edit->editor = gtk_entry_new();
sheet->entry = item_edit->editor; sheet->entry = item_edit->editor;
gtk_entry_set_width_chars (GTK_ENTRY(item_edit->editor), 1); gtk_entry_set_width_chars (GTK_ENTRY(item_edit->editor), 1);
gtk_box_pack_start (GTK_BOX(item_edit), item_edit->editor, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX(item_edit), item_edit->editor, TRUE, TRUE, 0);
// Get the CSS space settings for the entry
stylectxt = gtk_widget_get_style_context (GTK_WIDGET(item_edit->editor));
gtk_style_context_get_padding (stylectxt, GTK_STATE_FLAG_NORMAL, &padding);
gtk_style_context_get_margin (stylectxt, GTK_STATE_FLAG_NORMAL, &margin);
gtk_style_context_get_border (stylectxt, GTK_STATE_FLAG_NORMAL, &border);
item_edit->padding = padding;
item_edit->margin = margin;
item_edit->border = border;
// Make sure the Entry can not have focus and no frame // Make sure the Entry can not have focus and no frame
gtk_widget_set_can_focus (GTK_WIDGET(item_edit->editor), FALSE); gtk_widget_set_can_focus (GTK_WIDGET(item_edit->editor), FALSE);
gtk_entry_set_has_frame (GTK_ENTRY(item_edit->editor), FALSE); gtk_entry_set_has_frame (GTK_ENTRY(item_edit->editor), FALSE);
#if !GTK_CHECK_VERSION(3,20,0)
#if GTK_CHECK_VERSION(3,12,0)
gtk_widget_set_margin_start (GTK_WIDGET(item_edit->editor),
gnc_item_edit_get_margin (item_edit, left));
gtk_widget_set_margin_end (GTK_WIDGET(item_edit->editor),
gnc_item_edit_get_margin (item_edit, right));
#else
gtk_widget_set_margin_left (GTK_WIDGET(item_edit->editor),
gnc_item_edit_get_margin (item_edit, left));
gtk_widget_set_margin_right (GTK_WIDGET(item_edit->editor),
gnc_item_edit_get_margin (item_edit, right));
#endif
gtk_widget_set_margin_top (GTK_WIDGET(item_edit->editor),
gnc_item_edit_get_margin (item_edit, top));
gtk_widget_set_margin_bottom (GTK_WIDGET(item_edit->editor),
gnc_item_edit_get_margin (item_edit, bottom));
#endif
// Connect to the draw signal so we can draw a cursor // Connect to the draw signal so we can draw a cursor
g_signal_connect_after (item_edit->editor, "draw", g_signal_connect_after (item_edit->editor, "draw",
G_CALLBACK (draw_text_cursor_cb), NULL); G_CALLBACK (draw_text_cursor_cb), item_edit);
// Fill in the background so the underlying sheet cell can not be seen // Fill in the background so the underlying sheet cell can not be seen
g_signal_connect (item_edit, "draw", g_signal_connect (item_edit, "draw",
G_CALLBACK (draw_background_cb), item_edit); G_CALLBACK (draw_background_cb), item_edit);
/* Force padding on the entry to align with the rest of the register this
is done in the gnucash.css file which should be in line with sheet.h */
/* Create the popup button /* Create the popup button
It will only be displayed when the cell being edited provides It will only be displayed when the cell being edited provides
a popup item (like a calendar or account list) */ a popup item (like a calendar or account list) */
item_edit->popup_toggle.tbutton = gtk_toggle_button_new(); item_edit->popup_toggle.tbutton = gtk_toggle_button_new();
gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (item_edit->popup_toggle.tbutton), FALSE); gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (item_edit->popup_toggle.tbutton), FALSE);
/* Force padding on the button to keep it small and display as much as
possible of the arrow which is done in the gnucash.css file */
/* Wrap the popup button in an event box to give it its own gdkwindow. /* Wrap the popup button in an event box to give it its own gdkwindow.
* Without one the button would disappear behind the grid object. */ * Without one the button would disappear behind the grid object. */
item_edit->popup_toggle.ebox = gtk_event_box_new(); item_edit->popup_toggle.ebox = gtk_event_box_new();
@ -663,11 +740,13 @@ gnc_item_edit_new (GnucashSheet *sheet)
gtk_container_add(GTK_CONTAINER(item_edit->popup_toggle.ebox), gtk_container_add(GTK_CONTAINER(item_edit->popup_toggle.ebox),
item_edit->popup_toggle.tbutton); item_edit->popup_toggle.tbutton);
gtk_box_pack_start (GTK_BOX(item_edit), /* The button needs to be packed into a vertical box so that the height and position
item_edit->popup_toggle.ebox, * can be controlled in ealier than Gtk3.20 versions */
FALSE, TRUE, 0); gtk_box_pack_start (GTK_BOX(vb), item_edit->popup_toggle.ebox,
gtk_widget_show_all(GTK_WIDGET(item_edit)); FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX(item_edit), vb, FALSE, FALSE, 0);
gtk_widget_show_all(GTK_WIDGET(item_edit));
return GTK_WIDGET(item_edit); return GTK_WIDGET(item_edit);
} }

View File

@ -86,6 +86,10 @@ typedef struct
PopupGetWidth popup_get_width; PopupGetWidth popup_get_width;
gpointer popup_user_data; gpointer popup_user_data;
GtkBorder padding;
GtkBorder margin;
GtkBorder border;
/* Where are we */ /* Where are we */
VirtualLocation virt_loc; VirtualLocation virt_loc;
@ -97,6 +101,15 @@ typedef struct
GtkBoxClass parent_class; GtkBoxClass parent_class;
} GncItemEditClass; } GncItemEditClass;
typedef enum
{
left,
right,
top,
bottom,
left_right,
top_bottom,
} Sides;
GType gnc_item_edit_get_type (void); GType gnc_item_edit_get_type (void);
@ -130,5 +143,8 @@ gboolean gnc_item_edit_get_has_selection (GncItemEdit *item_edit);
void gnc_item_edit_focus_in (GncItemEdit *item_edit); void gnc_item_edit_focus_in (GncItemEdit *item_edit);
void gnc_item_edit_focus_out (GncItemEdit *item_edit); void gnc_item_edit_focus_out (GncItemEdit *item_edit);
gint gnc_item_edit_get_margin (GncItemEdit *item_edit, Sides side);
gint gnc_item_edit_get_padding_border (GncItemEdit *item_edit, Sides side);
/** @} */ /** @} */
#endif /* GNUCASH_ITEM_EDIT_H */ #endif /* GNUCASH_ITEM_EDIT_H */