+/** Try to fit a string into a given horizontal space by ellipsizing it.
+ * @param cr Cairo context in which the text will be plotted.
+ * @param name Text.
+ * @param avail Available horizontal space.
+ * @return (Text, possibly ellipsized) and (horizontal size of text)
+ */
+
+std::pair<std::string, double>
+fit_to_pixels (cairo_t* cr, std::string name, double avail)
+{
+ /* XXX hopefully there exists a more efficient way of doing this */
+
+ bool abbreviated = false;
+ uint32_t width = 0;
+
+ while (1) {
+ cairo_text_extents_t ext;
+ cairo_text_extents (cr, name.c_str(), &ext);
+
+ if (ext.width < avail || name.length() <= 4) {
+ width = ext.width;
+ break;
+ }
+
+ if (abbreviated) {
+ name = name.substr (0, name.length() - 4) + "...";
+ } else {
+ name = name.substr (0, name.length() - 3) + "...";
+ abbreviated = true;
+ }
+ }
+
+ return std::make_pair (name, width);
+}
+
+
+/** Add an element to a menu, settings its sensitivity.
+ * @param m Menu to add to.
+ * @param e Element to add.
+ * @param s true to make sensitive, false to make insensitive
+ */
+void
+add_item_with_sensitivity (Menu_Helpers::MenuList& m, Menu_Helpers::MenuElem e, bool s)
+{
+ m.push_back (e);
+ if (!s) {
+ m.back().set_sensitive (false);
+ }
+}
+
+