/* * tool_text.c * * Colloquium - A tiny presentation program * * Copyright (c) 2011 Thomas White * * 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 3 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, see . * */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include "presentation.h" #include "objects.h" #include "mainwindow.h" #include "slide_render.h" struct text_toolinfo { struct toolinfo base; PangoContext *pc; }; struct text_object { struct object base; char *text; size_t text_len; int insertion_point; PangoLayout *layout; PangoFontDescription *fontdesc; }; static void calculate_size_from_style(struct text_object *o, double *peright, double *pebottom, double *pmw, double *pmh) { double max_width, max_height; double ebottom, eright, mw, mh; eright = o->base.parent->parent->slide_width - o->base.style->margin_right; ebottom = o->base.parent->parent->slide_height - o->base.style->margin_bottom; mw = o->base.parent->parent->slide_width; mh = o->base.parent->parent->slide_height; *peright = eright; *pebottom = ebottom; *pmw = mw; *pmh = mh; max_width = mw - o->base.style->margin_left - o->base.style->margin_right; /* Use the provided maximum width if it exists and is smaller */ if ( o->base.style->use_max_width && (o->base.style->max_width < max_width) ) { max_width = o->base.style->max_width; } max_height = mh - o->base.style->margin_top - o->base.style->margin_bottom; pango_layout_set_width(o->layout, max_width*PANGO_SCALE); pango_layout_set_height(o->layout, max_height*PANGO_SCALE); pango_layout_set_wrap(o->layout, PANGO_WRAP_WORD_CHAR); pango_layout_set_ellipsize(o->layout, PANGO_ELLIPSIZE_MIDDLE); switch ( o->base.style->halign ) { case J_LEFT : pango_layout_set_alignment(o->layout, PANGO_ALIGN_LEFT); break; case J_RIGHT : pango_layout_set_alignment(o->layout, PANGO_ALIGN_RIGHT); break; case J_CENTER : pango_layout_set_alignment(o->layout, PANGO_ALIGN_CENTER); break; } } static void calculate_position_from_style(struct text_object *o, double eright, double ebottom, double mw, double mh, double *pxo, double *pyo) { double xo, yo; PangoRectangle ink, logical; pango_layout_get_extents(o->layout, &ink, &logical); xo = ink.x/PANGO_SCALE; yo = logical.y/PANGO_SCALE; switch ( o->base.style->halign ) { case J_LEFT : o->base.x = -xo + o->base.style->margin_left; break; case J_RIGHT : o->base.x = -xo + eright - o->base.bb_width; break; case J_CENTER : o->base.x = mw/2.0 - o->base.bb_width/2.0 - xo + o->base.style->offset_x; break; } if ( o->base.style->halign == J_CENTER ) { if ( o->base.x+xo < o->base.style->margin_left ) { o->base.x = o->base.style->margin_left - xo; } if ( o->base.x+xo + o->base.bb_width > mw-o->base.style->margin_right ) { o->base.x = mw-o->base.style->margin_right - xo - o->base.bb_width; } } switch ( o->base.style->valign ) { case V_TOP : o->base.y = o->base.style->margin_top; break; case V_BOTTOM : o->base.y = ebottom - o->base.bb_height; break; case V_CENTER : o->base.y = mh/2.0 - o->base.bb_height/2.0 + yo - o->base.style->offset_y; break; } if ( o->base.style->valign == V_CENTER ) { if ( o->base.y < o->base.style->margin_top ) { o->base.y = o->base.style->margin_top; } if ( o->base.y+yo + o->base.bb_height > mh - o->base.style->margin_bottom ) { o->base.y = mh-o->base.style->margin_bottom - yo - o->base.bb_height; } } *pxo = xo; *pyo = yo; } static void update_text(struct text_object *o) { PangoRectangle ink, logical; double eright = 0.0; double ebottom = 0.0; double mw = 0.0; double mh = 0.0; double xo, yo; int furniture = 0; furniture = o->base.style != o->base.parent->parent->ss->styles[0]; pango_layout_set_text(o->layout, o->text, -1); o->fontdesc = pango_font_description_from_string(o->base.style->font); pango_layout_set_font_description(o->layout, o->fontdesc); if ( furniture ) { calculate_size_from_style(o, &eright, &ebottom, &mw, &mh); } else { pango_layout_set_alignment(o->layout, PANGO_ALIGN_LEFT); } pango_layout_get_extents(o->layout, &ink, &logical); o->base.bb_width = ink.width / PANGO_SCALE; o->base.bb_height = logical.height/PANGO_SCALE; if ( furniture ) { calculate_position_from_style(o, eright, ebottom, mw, mh, &xo, &yo); } if ( furniture ) { o->base.x += xo; o->base.y += yo; } } void insert_text(struct object *op, char *t) { struct text_object *o = (struct text_object *)op; char *tmp; size_t tlen, olen; int i; assert(o->base.type == TEXT); tlen = strlen(t); olen = strlen(o->text); if ( tlen + olen + 1 > o->text_len ) { char *try; try = realloc(o->text, o->text_len + tlen + 64); if ( try == NULL ) return; /* Failed to insert */ o->text = try; o->text_len += 64; o->text_len += tlen; } tmp = malloc(o->text_len); if ( tmp == NULL ) return; for ( i=0; iinsertion_point; i++ ) { tmp[i] = o->text[i]; } for ( i=0; iinsertion_point] = t[i]; } for ( i=0; iinsertion_point; i++ ) { tmp[i+o->insertion_point+tlen] = o->text[i+o->insertion_point]; } tmp[olen+tlen] = '\0'; memcpy(o->text, tmp, o->text_len); free(tmp); update_text(o); o->insertion_point += tlen; o->base.parent->object_seq++; o->base.empty = 0; } static int find_prev_index(const char *t, int p) { int i, nback; if ( p == 0 ) return 0; if ( !(t[p-1] & 0x80) ) { nback = 1; } else { nback = 0; for ( i=1; i<=6; i++ ) { if ( p-i == 0 ) return 0; if ( !(t[p-i] & 0xC0) ) nback++; } } return p - nback; } static int find_next_index(const char *t, int p) { int i, nfor; if ( t[p] == '\0' ) return p; if ( !(t[p+1] & 0x80) ) { nfor = 1; } else { nfor = 0; for ( i=1; i<=6; i++ ) { if ( t[p+i] == '\0' ) return p+i; if ( !(t[p+i] & 0xC0) ) nfor++; } } return p + nfor; } void handle_text_backspace(struct object *op) { int prev_index; struct text_object *o = (struct text_object *)op; assert(o->base.type == TEXT); if ( o->insertion_point == 0 ) return; /* Nothing to delete */ prev_index = find_prev_index(o->text, o->insertion_point); memmove(o->text+prev_index, o->text+o->insertion_point, o->text_len-o->insertion_point); o->insertion_point = prev_index; if ( strlen(o->text) == 0 ) o->base.empty = 1; update_text(o); o->base.parent->object_seq++; } void move_cursor_left(struct object *op) { struct text_object *o = (struct text_object *)op; o->insertion_point = find_prev_index(o->text, o->insertion_point); } void move_cursor_right(struct object *op) { struct text_object *o = (struct text_object *)op; o->insertion_point = find_next_index(o->text, o->insertion_point); } static void render_text_object(cairo_t *cr, struct object *op) { struct text_object *o = (struct text_object *)op; GdkColor col; cairo_move_to(cr, o->base.x, o->base.y); gdk_color_parse(o->base.style->colour, &col); gdk_cairo_set_source_color(cr, &col); /* FIXME: Honour alpha as well */ pango_cairo_update_layout(cr, o->layout); pango_cairo_show_layout(cr, o->layout); } static void draw_caret(cairo_t *cr, struct object *op) { int line, xpos; double xposd, cx; double clow, chigh; const double t = 1.8; struct text_object *o = (struct text_object *)op; assert(o->base.type == TEXT); pango_layout_index_to_line_x(o->layout, o->insertion_point, 0, &line, &xpos); xposd = xpos/PANGO_SCALE; cx = o->base.x+xposd; clow = o->base.y; chigh = o->base.y+o->base.bb_height; cairo_move_to(cr, cx, clow); cairo_line_to(cr, cx, chigh); cairo_move_to(cr, cx-t, clow-t); cairo_line_to(cr, cx, clow); cairo_move_to(cr, cx+t, clow-t); cairo_line_to(cr, cx, clow); cairo_move_to(cr, cx-t, chigh+t); cairo_line_to(cr, cx, chigh); cairo_move_to(cr, cx+t, chigh+t); cairo_line_to(cr, cx, chigh); cairo_set_source_rgb(cr, 0.86, 0.0, 0.0); cairo_set_line_width(cr, 1.0); cairo_stroke(cr); } static void delete_text_object(struct object *op) { struct text_object *o = (struct text_object *)op; if ( o->layout != NULL ) g_object_unref(o->layout); if ( o->fontdesc != NULL ) pango_font_description_free(o->fontdesc); } static struct object *add_text_object(struct slide *s, double x, double y, struct style *sty, struct text_toolinfo *ti) { struct text_object *new; new = calloc(1, sizeof(*new)); if ( new == NULL ) return NULL; /* Base properties */ new->base.x = x; new->base.y = y; new->base.bb_width = 10.0; new->base.bb_height = 40.0; new->base.type = TEXT; new->base.empty = 1; new->base.parent = NULL; new->base.style = sty; /* Text-specific stuff */ new->text = malloc(1); new->text[0] = '\0'; new->text_len = 1; new->insertion_point = 0; new->layout = pango_layout_new(ti->pc); new->fontdesc = NULL; /* Methods for this object */ new->base.render_object = render_text_object; new->base.delete_object = delete_text_object; if ( add_object_to_slide(s, (struct object *)new) ) { delete_object((struct object *)new); return NULL; } s->object_seq++; return (struct object *)new; } static void click_create(struct presentation *p, struct toolinfo *tip, double x, double y) { struct object *n; struct text_toolinfo *ti = (struct text_toolinfo *)tip; /* FIXME: Insert ESP here and possibly select a different style */ n = add_text_object(p->view_slide, x, y, p->ss->styles[0], ti); p->editing_object = n; } static void click_select(struct presentation *p, struct toolinfo *tip, double x, double y) { int xp, yp; gboolean v; struct text_object *o = (struct text_object *)p->editing_object; int idx, trail; assert(o->base.type == TEXT); xp = (x - o->base.x)*PANGO_SCALE; yp = (y - o->base.y)*PANGO_SCALE; v = pango_layout_xy_to_index(o->layout, xp, yp, &idx, &trail); o->insertion_point = idx+trail; } static void drag_object(struct toolinfo *tip, struct presentation *p, struct object *o, double x, double y) { /* Do nothing */ } static void create_default(struct presentation *p, struct style *sty, struct toolinfo *tip) { struct object *n; struct text_toolinfo *ti = (struct text_toolinfo *)tip; n = add_text_object(p->view_slide, 0.0, 0.0, sty, ti); p->editing_object = n; } static void select_object(struct object *o,struct toolinfo *tip) { /* Do nothing */ } static void deselect_object(struct object *o,struct toolinfo *tip) { if ( (o != NULL) && o->empty ) { delete_object(o); } } static void draw_overlay(cairo_t *cr, struct object *o) { draw_editing_box(cr, o->x, o->y, o->bb_width, o->bb_height); draw_caret(cr, o); } struct toolinfo *initialise_text_tool(GtkWidget *w) { struct text_toolinfo *ti; ti = malloc(sizeof(*ti)); ti->pc = gtk_widget_get_pango_context(w); ti->base.click_create = click_create; ti->base.click_select = click_select; ti->base.create_default = create_default; ti->base.select = select_object; ti->base.deselect = deselect_object; ti->base.drag_object = drag_object; ti->base.draw_editing_overlay = draw_overlay; return (struct toolinfo *)ti; }