Example #1
0
static void
sp_text_edit_dialog_read_selection (GtkWidget *dlg, gboolean dostyle, gboolean docontent)
{
	GtkWidget *notebook, *textw, *fontsel, *preview, *apply, *def;
	GtkTextBuffer *tb;
	SPText *text;
	SPRepr *repr;
	SPStyle *style;
	unsigned items;

	if (g_object_get_data (G_OBJECT (dlg), "blocked")) return;
	g_object_set_data (G_OBJECT (dlg), "blocked", GINT_TO_POINTER (TRUE));

	notebook = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "notebook");
	textw = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "textw");
	tb = (GtkTextBuffer*)g_object_get_data (G_OBJECT (dlg), "text");
	fontsel = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "fontsel");
	preview = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "preview");
	apply = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "apply");
	def = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "default");

	text = sp_ted_get_selected_text_item ();

	if (text) {
		items = sp_ted_get_selected_text_count ();
		if (items == 1) {
			gtk_widget_set_sensitive (textw, TRUE);
		} else {
			gtk_widget_set_sensitive (textw, FALSE);
		}
		gtk_widget_set_sensitive (apply, FALSE);
		gtk_widget_set_sensitive (def, TRUE);
		style = SP_OBJECT_STYLE (text);
		if (docontent) {
			gchar *str;
			str = sp_text_get_string_multiline (text);
			if (str) {
				int pos;
				pos = 0;
				if (items == 1) {
					gtk_text_buffer_set_text (tb, str, strlen (str));
				}
				sp_font_preview_set_phrase (SP_FONT_PREVIEW (preview), str);
				g_free (str);
			} else {
				gtk_text_buffer_set_text (tb, "", 0);
				sp_font_preview_set_phrase (SP_FONT_PREVIEW (preview), NULL);
			}
		}
		repr = SP_OBJECT_REPR (text);
	} else {
		gtk_widget_set_sensitive (textw, FALSE);
		gtk_widget_set_sensitive (apply, FALSE);
		gtk_widget_set_sensitive (def, FALSE);
		repr = inkscape_get_repr (INKSCAPE, "tools.text");
		if (repr) {
			gtk_widget_set_sensitive (notebook, TRUE);
			style = sp_style_new ();
			sp_style_read_from_repr (style, repr);
		} else {
			gtk_widget_set_sensitive (notebook, FALSE);
			style = sp_style_new ();
		}
	}

	if (dostyle) {
		NRTypeFace *tf;
		NRFont *font;
		GtkWidget *b, *combo;
		gchar *c;
		const gchar *sstr;

		c = sp_text_edit_dialog_font_style_to_lookup (style);
		tf = nr_type_directory_lookup_fuzzy ((gchar*)style->text->font_family.value, c);
		font = nr_font_new_default (tf, NR_TYPEFACE_METRICS_HORIZONTAL, style->font_size.computed);
		nr_typeface_unref (tf);

		if (font) {
			sp_font_selector_set_font (SP_FONT_SELECTOR (fontsel), font);
			sp_font_preview_set_font (SP_FONT_PREVIEW (preview), font);
			nr_font_unref (font);
		}

		if (style->text_anchor.computed == SP_CSS_TEXT_ANCHOR_START) {
			b = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "text_anchor_start");
		} else if (style->text_anchor.computed == SP_CSS_TEXT_ANCHOR_MIDDLE) {
			b = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "text_anchor_middle");
		} else {
			b = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "text_anchor_end");
		}
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (b), TRUE);
		if (style->writing_mode.computed == SP_CSS_WRITING_MODE_LR) {
			b = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "writing_mode_lr");
		} else {
			b = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "writing_mode_tb");
		}
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (b), TRUE);
		combo = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "line_spacing");
		sstr = (repr) ? sp_repr_attr (repr, "sodipodi:linespacing") : NULL;
		gtk_entry_set_text ((GtkEntry *) ((GtkCombo *) (combo))->entry, (sstr) ? sstr : (const gchar *) "100%");
	}

	g_object_set_data (G_OBJECT (dlg), "blocked", NULL);
}
static void
sp_text_edit_dialog_read_selection ( GtkWidget *dlg,
                                     gboolean dostyle,
                                     gboolean docontent )
{
    if (g_object_get_data (G_OBJECT (dlg), "blocked"))
        return;

    g_object_set_data (G_OBJECT (dlg), "blocked", GINT_TO_POINTER (TRUE));

    GtkWidget *notebook = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "notebook");
    GtkWidget *textw = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "textw");
    GtkWidget *fontsel = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "fontsel");
    GtkWidget *preview = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "preview");
    GtkWidget *apply = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "apply");
    GtkWidget *def = (GtkWidget*)g_object_get_data (G_OBJECT (dlg), "default");

    GtkTextBuffer *tb = (GtkTextBuffer*)g_object_get_data (G_OBJECT (dlg), "text");

    SPItem *text = sp_ted_get_selected_text_item ();

    Inkscape::XML::Node *repr;
    if (text)
    {
        guint items = sp_ted_get_selected_text_count ();
        if (items == 1) {
            gtk_widget_set_sensitive (textw, TRUE);
        } else {
            gtk_widget_set_sensitive (textw, FALSE);
        }
        gtk_widget_set_sensitive (apply, FALSE);
        gtk_widget_set_sensitive (def, TRUE);

        if (docontent) {
            gchar *str;
            str = sp_te_get_string_multiline (text);

            if (str) {
                int pos;
                pos = 0;

                if (items == 1) {
                    gtk_text_buffer_set_text (tb, str, strlen (str));
                    gtk_text_buffer_set_modified (tb, FALSE);
                }
                sp_font_preview_set_phrase (SP_FONT_PREVIEW (preview), str);
                g_free (str);

            } else {
                gtk_text_buffer_set_text (tb, "", 0);
                sp_font_preview_set_phrase (SP_FONT_PREVIEW (preview), NULL);
            }
        } // end of if (docontent)
        repr = SP_OBJECT_REPR (text);

    } else {
        gtk_widget_set_sensitive (textw, FALSE);
        gtk_widget_set_sensitive (apply, FALSE);
        gtk_widget_set_sensitive (def, FALSE);
    }

    if (dostyle) {

        // create temporary style
        SPStyle *query = sp_style_new (SP_ACTIVE_DOCUMENT);
        // query style from desktop into it. This returns a result flag and fills query with the style of subselection, if any, or selection
        //int result_fontspec = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_FONT_SPECIFICATION);
        int result_family = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_FONTFAMILY);
        int result_style = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_FONTSTYLE);
        int result_numbers = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_FONTNUMBERS);

        // If querying returned nothing, read the style from the text tool prefs (default style for new texts)
        // (Ok to not get a font specification - must just rely on the family and style in that case)
        if (result_family == QUERY_STYLE_NOTHING || result_style == QUERY_STYLE_NOTHING
                || result_numbers == QUERY_STYLE_NOTHING) {
            sp_style_read_from_prefs(query, "/tools/text");
        }

        // FIXME: process result_family/style == QUERY_STYLE_MULTIPLE_DIFFERENT by showing "Many" in the lists

        // Get a font_instance using the font-specification attribute stored in SPStyle if available
        font_instance *font = font_factory::Default()->FaceFromStyle(query);


        if (font) {
            // the font is oversized, so we need to pass the true size separately
            sp_font_selector_set_font (SP_FONT_SELECTOR (fontsel), font, query->font_size.computed);
            sp_font_preview_set_font (SP_FONT_PREVIEW (preview), font, SP_FONT_SELECTOR(fontsel));
            font->Unref();
            font=NULL;
        }

        GtkWidget *b;
        if (query->text_anchor.computed == SP_CSS_TEXT_ANCHOR_START) {
            if (query->text_align.computed == SP_CSS_TEXT_ALIGN_JUSTIFY) {
                b = (GtkWidget*)g_object_get_data ( G_OBJECT (dlg), "text_anchor_justify" );
            } else {
                b = (GtkWidget*)g_object_get_data ( G_OBJECT (dlg), "text_anchor_start" );
            }
        } else if (query->text_anchor.computed == SP_CSS_TEXT_ANCHOR_MIDDLE) {
            b = (GtkWidget*)g_object_get_data ( G_OBJECT (dlg), "text_anchor_middle" );
        } else {
            b = (GtkWidget*)g_object_get_data ( G_OBJECT (dlg), "text_anchor_end" );
        }
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (b), TRUE);

        if (query->writing_mode.computed == SP_CSS_WRITING_MODE_LR_TB) {
            b = (GtkWidget*)g_object_get_data ( G_OBJECT (dlg), INKSCAPE_STOCK_WRITING_MODE_LR );
        } else {
            b = (GtkWidget*)g_object_get_data ( G_OBJECT (dlg), INKSCAPE_STOCK_WRITING_MODE_TB );
        }
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (b), TRUE);

        GtkWidget *combo = (GtkWidget*)g_object_get_data ( G_OBJECT (dlg), "line_spacing" );
        double height;
        if (query->line_height.normal) height = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL;
        else if (query->line_height.unit == SP_CSS_UNIT_PERCENT)
            height = query->line_height.value;
        else height = query->line_height.computed;
        gchar *sstr = g_strdup_printf ("%d%%", (int) floor(height * 100 + 0.5));
        gtk_entry_set_text ((GtkEntry *) ((GtkCombo *) (combo))->entry, sstr);
        g_free(sstr);

        sp_style_unref(query);
    }

    g_object_set_data (G_OBJECT (dlg), "blocked", NULL);
}