summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2019-06-04 23:31:41 +0200
committerThomas White <taw@physics.org>2019-06-04 23:32:57 +0200
commitb485f458fcc428b1440c1fcd01be30fea30a1684 (patch)
treebad78cd77885ce2136d67d443aebe2c3a732edf5
parentb4f73260c5906c2c8a19998ae3eb9114e12d2e4b (diff)
Add cursor
-rw-r--r--src/nanolight.c47
-rw-r--r--src/nanolight.h3
2 files changed, 38 insertions, 12 deletions
diff --git a/src/nanolight.c b/src/nanolight.c
index e597e68..2ed51ce 100644
--- a/src/nanolight.c
+++ b/src/nanolight.c
@@ -105,8 +105,8 @@ static gboolean draw_sig(GtkWidget *widget, cairo_t *cr, struct nanolight *nl)
int i;
PangoContext *pc;
PangoFontDescription *fontdesc;
- PangoLayout *layout;
double x, y;
+ PangoRectangle cursor;
w = gtk_widget_get_allocated_width(widget);
h = gtk_widget_get_allocated_height(widget);
@@ -143,15 +143,23 @@ static gboolean draw_sig(GtkWidget *widget, cairo_t *cr, struct nanolight *nl)
}
/* Command line */
- layout = pango_layout_new(pc);
- fontdesc = pango_font_description_from_string("Comfortaa Bold 16");
- pango_layout_set_text(layout, nl->cmdline, -1);
- pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
- pango_layout_set_font_description(layout, fontdesc);
+ pango_layout_set_text(nl->layout, nl->cmdline, -1);
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
- cairo_move_to(cr, 0.0, h - OVERALL_BORDER*2 - 20.0);
- pango_cairo_show_layout(cr, layout);
- g_object_unref(layout);
+ cairo_save(cr);
+ cairo_translate(cr, 0.0, h - OVERALL_BORDER*2 - 20.0);
+ cairo_move_to(cr, 0.0, 0.0);
+ pango_cairo_show_layout(cr, nl->layout);
+ pango_layout_get_cursor_pos(nl->layout, nl->cursor_idx, &cursor, NULL);
+ x = pango_units_to_double(cursor.x);
+ y = pango_units_to_double(cursor.y);
+ h = pango_units_to_double(cursor.height);
+ cairo_move_to(cr, x, y);
+ cairo_line_to(cr, x, y+h);
+ cairo_set_line_width(cr, 3.0);
+ cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
+ cairo_set_source_rgb(cr, 0.8, 0.4, 0.4);
+ cairo_stroke(cr);
+ cairo_restore(cr);
return FALSE;
}
@@ -196,6 +204,7 @@ static void execute_command(struct nanolight *nl)
{
if ( command_run(nl->cmdline, nl) == 0 ) {
nl->cmdline[0] = '\0';
+ nl->cursor_idx = 0;
}
redraw(nl);
}
@@ -206,17 +215,21 @@ static gboolean im_commit_sig(GtkIMContext *im, gchar *str, struct nanolight *nl
size_t cmd_len = strlen(nl->cmdline);
if ( cmd_len+strlen(str) > 1023 ) return FALSE;
strcat(nl->cmdline, str);
+ nl->cursor_idx += strlen(str);
redraw(nl);
return FALSE;
}
-static void delete_char(char *str)
+static size_t delete_char(char *str)
{
char *last;
- if ( str[0] == '\0' ) return;
+ size_t len;
+ if ( str[0] == '\0' ) return 0;
last = g_utf8_find_prev_char(str, str+strlen(str));
+ len = strlen(last);
last[0] = '\0';
+ return len;
}
@@ -246,12 +259,13 @@ static gboolean key_press_sig(GtkWidget *da, GdkEventKey *event, struct nanoligh
case GDK_KEY_Escape :
nl->cmdline[0] = '\0';
+ nl->cursor_idx = 0;
redraw(nl);
claim = 1;
break;
case GDK_KEY_BackSpace :
- delete_char(nl->cmdline);
+ nl->cursor_idx -= delete_char(nl->cmdline);
claim = 1;
redraw(nl);
break;
@@ -266,6 +280,8 @@ static gboolean key_press_sig(GtkWidget *da, GdkEventKey *event, struct nanoligh
static gint realise_sig(GtkWidget *da, struct nanolight *nl)
{
GdkWindow *win = gtk_widget_get_window(da);
+ PangoContext *pc;
+ PangoFontDescription *fontdesc;
/* Keyboard and input method stuff */
nl->im_context = gtk_im_multicontext_new();
@@ -274,6 +290,12 @@ static gint realise_sig(GtkWidget *da, struct nanolight *nl)
g_signal_connect(G_OBJECT(nl->im_context), "commit", G_CALLBACK(im_commit_sig), nl);
g_signal_connect(G_OBJECT(da), "key-press-event", G_CALLBACK(key_press_sig), nl);
+ pc = gtk_widget_get_pango_context(da);
+ nl->layout = pango_layout_new(pc);
+ fontdesc = pango_font_description_from_string("Comfortaa Bold 16");
+ pango_layout_set_alignment(nl->layout, PANGO_ALIGN_LEFT);
+ pango_layout_set_font_description(nl->layout, fontdesc);
+
return FALSE;
}
@@ -339,6 +361,7 @@ int main(int argc, char *argv[])
nl.n_fixtures = 0;
nl.max_fixtures = 0;
nl.cmdline[0] = '\0';
+ nl.cursor_idx = 0;
create_fixture(&nl, &cls, "mh1", 1);
create_fixture(&nl, &cls, "mh2", 52);
diff --git a/src/nanolight.h b/src/nanolight.h
index 9e6f19e..5c08cd7 100644
--- a/src/nanolight.h
+++ b/src/nanolight.h
@@ -72,7 +72,10 @@ struct nanolight
GtkWidget *da;
double fixture_width;
+
char cmdline[1024];
+ int cursor_idx;
+ PangoLayout *layout;
};