Skip to content

Commit

Permalink
Github #215: More defensive programming in an attempt to prevent rare…
Browse files Browse the repository at this point in the history
… crashes

Hopefully more resilience against a cairo context being NULL,
 not that it should happen but that's C programming for you.
  • Loading branch information
rnorris committed Jan 13, 2024
1 parent b3e09d5 commit 36213db
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
15 changes: 11 additions & 4 deletions src/ui_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ GdkPixbuf *ui_get_icon ( const gchar *name, guint size )
*/
void ui_cr_draw_layout ( cairo_t *cr, gdouble xx, gdouble yy, PangoLayout *layout )
{
g_return_if_fail ( cr != NULL );
cairo_move_to ( cr, xx, yy );
pango_cairo_show_layout ( cr, layout );
}
Expand All @@ -723,9 +724,13 @@ void ui_cr_draw_layout ( cairo_t *cr, gdouble xx, gdouble yy, PangoLayout *layou
*
* cairo_stroke() should be called after using this
* (or after consecutive calls to this are done as a group)
*
* cr must not be NULL;
* this test is not checked here, as the caller should be performing it anyway
*/
void ui_cr_draw_line ( cairo_t *cr, gdouble x1, gdouble y1, gdouble x2, gdouble y2 )
{
//g_return_if_fail ( cr != NULL ); // Specifically not done here
cairo_move_to ( cr, x1, y1 );
cairo_line_to ( cr, x2, y2 );
}
Expand All @@ -747,15 +752,17 @@ void ui_cr_draw_rectangle ( cairo_t *cr, gboolean fill, gdouble xx, gdouble yy,

void ui_cr_set_color ( cairo_t *cr, const gchar *name )
{
GdkColor color;
if ( gdk_color_parse(name, &color) ) {
gdk_cairo_set_source_color ( cr, &color );
}
g_return_if_fail ( cr != NULL );
GdkColor color;
if ( gdk_color_parse(name, &color) ) {
gdk_cairo_set_source_color ( cr, &color );
}
}

// Generic dashed line
void ui_cr_set_dash ( cairo_t *cr )
{
g_return_if_fail ( cr != NULL );
// ATM assume this doesn't need to be multiplied by the vvp->scale
static const double dashed[] = {4.0};
cairo_set_dash ( cr, dashed, 1, 0 );
Expand Down
55 changes: 27 additions & 28 deletions src/viktrwlayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -11302,6 +11302,9 @@ static VikLayerToolFuncStatus tool_edit_track_move ( VikTrwLayer *vtl, GdkEventM
w1 = vik_viewport_get_width(vvp);
h1 = vik_viewport_get_height(vvp);

gint x1, y1;
vik_viewport_coord_to_screen ( vvp, &(last_tpt->coord), &x1, &y1 );

#if GTK_CHECK_VERSION (3,0,0)
if ( te->gc ) {
cairo_surface_t *surface = vik_viewport_surface_tool_get ( te->vvp );
Expand All @@ -11317,39 +11320,38 @@ static VikLayerToolFuncStatus tool_edit_track_move ( VikTrwLayer *vtl, GdkEventM
else
ui_cr_clear ( te->gc );

ui_cr_set_color ( te->gc, "red" );
cairo_set_line_width ( te->gc, 2*vik_viewport_get_scale(te->vvp) );
ui_cr_set_dash ( te->gc );
if ( te->gc ) {
ui_cr_set_color ( te->gc, "red" );
cairo_set_line_width ( te->gc, 2*vik_viewport_get_scale(te->vvp) );
ui_cr_set_dash ( te->gc );

gint x1, y1;
vik_viewport_coord_to_screen ( vvp, &(last_tpt->coord), &x1, &y1 );
ui_cr_draw_line ( te->gc, x1, y1, event->x, event->y );
cairo_stroke ( te->gc );
ui_cr_draw_line ( te->gc, x1, y1, event->x, event->y );
cairo_stroke ( te->gc );

//
// Display of the distance 'tooltip' during track creation is controlled by a preference
//
if ( a_vik_get_create_track_tooltip() ) {
//
// Display of the distance 'tooltip' during track creation is controlled by a preference
//
if ( a_vik_get_create_track_tooltip() ) {

gchar *str = distance_string (distance);
gchar *str = distance_string (distance);

PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(vvp), NULL);
pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(vvp))->font_desc);
pango_layout_set_text (pl, str, -1);
gint wd, hd;
pango_layout_get_pixel_size ( pl, &wd, &hd );
PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(vvp), NULL);
pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(vvp))->font_desc);
pango_layout_set_text (pl, str, -1);
gint wd, hd;
pango_layout_get_pixel_size ( pl, &wd, &hd );

gint xd,yd;
// offset from cursor a bit depending on font size
xd = event->x + 10*vik_viewport_get_scale(vvp);
yd = event->y - hd;
gint xd,yd;
// offset from cursor a bit depending on font size
xd = event->x + 10*vik_viewport_get_scale(vvp);
yd = event->y - hd;

ui_cr_label_with_bg ( te->gc, xd, yd, wd, hd, pl );
ui_cr_label_with_bg ( te->gc, xd, yd, wd, hd, pl );

g_object_unref ( G_OBJECT ( pl ) );
g_free (str);
g_object_unref ( G_OBJECT ( pl ) );
g_free (str);
}
}

gtk_widget_queue_draw ( GTK_WIDGET(vvp) );
#else
static GdkPixmap *pixmap = NULL;
Expand All @@ -11369,9 +11371,6 @@ static VikLayerToolFuncStatus tool_edit_track_move ( VikTrwLayer *vtl, GdkEventM
0, 0, 0, 0, -1, -1);

draw_sync_t *passalong;
gint x1, y1;

vik_viewport_coord_to_screen ( vvp, &(last_tpt->coord), &x1, &y1 );

// FOR SCREEN OVERLAYS WE MUST DRAW INTO THIS PIXMAP (when using the reset method)
// otherwise using vik_viewport_draw_* functions puts the data into the base pixmap,
Expand Down
4 changes: 4 additions & 0 deletions src/vikviewport.c
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,7 @@ void vik_viewport_draw_line ( VikViewport *vvp, GdkGC *gc, gint x1, gint y1, gin
/*** clipping, yeah! ***/
a_viewport_clip_line ( &x1, &y1, &x2, &y2 );
#if GTK_CHECK_VERSION (3,0,0)
g_return_if_fail ( gc != NULL );
cairo_set_line_width ( gc, thickness );
if ( gcolor )
gdk_cairo_set_source_color ( gc, gcolor );
Expand All @@ -1618,6 +1619,7 @@ void vik_viewport_draw_rectangle ( VikViewport *vvp, GdkGC *gc, gboolean filled,
// Using 32 as half the default waypoint image size, so this draws ensures the highlight gets done
if ( x1 > -32 && x1 < vvp->width + 32 && y1 > -32 && y1 < vvp->height + 32 ) {
#if GTK_CHECK_VERSION (3,0,0)
g_return_if_fail ( gc != NULL );
if ( gcolor )
gdk_cairo_set_source_color ( gc, gcolor );
ui_cr_draw_rectangle ( gc, filled, x1, y1, x2, y2 );
Expand Down Expand Up @@ -1662,6 +1664,7 @@ void vik_viewport_draw_pixbuf ( VikViewport *vvp, GdkPixbuf *pixbuf, gint src_x,
void vik_viewport_draw_arc ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x, gint y, gint width, gint height, gint angle1, gint angle2, GdkColor *gcolor )
{
#if GTK_CHECK_VERSION (3,0,0)
g_return_if_fail ( gc != NULL );
// ATM Only used for drawing circles - so height is ignored
// other arc drawing usage also currently uses width==height
if ( gcolor )
Expand All @@ -1686,6 +1689,7 @@ void vik_viewport_draw_arc ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint
void vik_viewport_draw_polygon ( VikViewport *vvp, GdkGC *gc, gboolean filled, GdkPoint *points, gint npoints, GdkColor *gcolor )
{
#if GTK_CHECK_VERSION (3,0,0)
g_return_if_fail ( gc != NULL );
if ( gcolor )
gdk_cairo_set_source_color ( gc, gcolor );
// Using cairo no obvious draw polygon method,
Expand Down

0 comments on commit 36213db

Please sign in to comment.