From: Paul Davis Date: Tue, 8 Feb 2011 23:42:45 +0000 (+0000) Subject: added Gtkmm2ext::rounded_rectangle() cairo pseudo-method X-Git-Tag: 3.0-alpha5~663 X-Git-Url: https://git.carlh.net/gitweb/?a=commitdiff_plain;h=4ed504acd729fedb76aaa3b534dfd2b43082b5bb;p=ardour.git added Gtkmm2ext::rounded_rectangle() cairo pseudo-method git-svn-id: svn://localhost/ardour2/branches/3.0@8787 d708f5d6-7413-0410-9779-e7cbd77b26cf --- diff --git a/libs/gtkmm2ext/gtkmm2ext/utils.h b/libs/gtkmm2ext/gtkmm2ext/utils.h index 3528435ed1..60123bb843 100644 --- a/libs/gtkmm2ext/gtkmm2ext/utils.h +++ b/libs/gtkmm2ext/gtkmm2ext/utils.h @@ -24,12 +24,18 @@ #include #include +#include + #include #include #include /* for WMDecoration */ #include #include +namespace Cairo { + class Context; +} + namespace Gtk { class ComboBoxText; class Widget; @@ -89,6 +95,7 @@ namespace Gtkmm2ext { int physical_screen_width (Glib::RefPtr); void container_clear (Gtk::Container&); + void rounded_rectangle (Cairo::RefPtr context, double x, double y, double w, double h, double r=10); }; #endif /* __gtkmm2ext_utils_h__ */ diff --git a/libs/gtkmm2ext/utils.cc b/libs/gtkmm2ext/utils.cc index df79e535a7..878d5402af 100644 --- a/libs/gtkmm2ext/utils.cc +++ b/libs/gtkmm2ext/utils.cc @@ -386,3 +386,85 @@ Gtkmm2ext::container_clear (Gtk::Container& c) c.remove (**child); } } + +#if 1 +void +Gtkmm2ext::rounded_rectangle (Cairo::RefPtr context, double x, double y, double w, double h, double r) +{ + /* renders small shapes better than most others */ + +/* A****BQ + H C + * * + G D + F****E +*/ + context->move_to(x+r,y); // Move to A + context->line_to(x+w-r,y); // Straight line to B + context->curve_to(x+w,y,x+w,y,x+w,y+r); // Curve to C, Control points are both at Q + context->line_to(x+w,y+h-r); // Move to D + context->curve_to(x+w,y+h,x+w,y+h,x+w-r,y+h); // Curve to E + context->line_to(x+r,y+h); // Line to F + context->curve_to(x,y+h,x,y+h,x,y+h-r); // Curve to G + context->line_to(x,y+r); // Line to H + context->curve_to(x,y,x,y,x+r,y); // Curve to A +} + +#else + +void +Gtkmm2ext::rounded_rectangle (Cairo::RefPtr context, double x, double y, double width, double height, double radius) +{ + /* doesn't render small shapes well at all, and does not absolutely honor width & height */ + + double x0 = x+radius/2.0; + double y0 = y+radius/2.0; + double rect_width = width - radius; + double rect_height = height - radius; + + context->save(); + + double x1=x0+rect_width; + double y1=y0+rect_height; + + if (rect_width/2move_to (x0, (y0 + y1)/2); + context->curve_to (x0 ,y0, x0, y0, (x0 + x1)/2, y0); + context->curve_to (x1, y0, x1, y0, x1, (y0 + y1)/2); + context->curve_to (x1, y1, x1, y1, (x1 + x0)/2, y1); + context->curve_to (x0, y1, x0, y1, x0, (y0 + y1)/2); + } else { + context->move_to (x0, y0 + radius); + context->curve_to (x0 ,y0, x0, y0, (x0 + x1)/2, y0); + context->curve_to (x1, y0, x1, y0, x1, y0 + radius); + context->line_to (x1 , y1 - radius); + context->curve_to (x1, y1, x1, y1, (x1 + x0)/2, y1); + context->curve_to (x0, y1, x0, y1, x0, y1- radius); + } + } else { + if (rect_height/2move_to (x0, (y0 + y1)/2); + context->curve_to (x0 , y0, x0 , y0, x0 + radius, y0); + context->line_to (x1 - radius, y0); + context->curve_to (x1, y0, x1, y0, x1, (y0 + y1)/2); + context->curve_to (x1, y1, x1, y1, x1 - radius, y1); + context->line_to (x0 + radius, y1); + context->curve_to (x0, y1, x0, y1, x0, (y0 + y1)/2); + } else { + context->move_to (x0, y0 + radius); + context->curve_to (x0 , y0, x0 , y0, x0 + radius, y0); + context->line_to (x1 - radius, y0); + context->curve_to (x1, y0, x1, y0, x1, y0 + radius); + context->line_to (x1 , y1 - radius); + context->curve_to (x1, y1, x1, y1, x1 - radius, y1); + context->line_to (x0 + radius, y1); + context->curve_to (x0, y1, x0, y1, x0, y1- radius); + } + } + + context->close_path (); + context->restore(); +} + +#endif