summaryrefslogtreecommitdiffstats
path: root/src/ui/dialog/text-edit.cpp
blob: a00db1715f38dde6b443df9c5ae13d281393a643 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/**
 * @file
 * Text editing dialog.
 */
/* Authors:
 *   Lauris Kaplinski <lauris@ximian.com>
 *   bulia byak <buliabyak@users.sf.net>
 *   Johan Engelen <goejendaagh@zonnet.nl>
 *   Abhishek Sharma
 *   John Smith
 *   Tavmjong Bah
 *
 * Copyright (C) 1999-2013 Authors
 * Copyright (C) 2000-2001 Ximian, Inc.
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "text-edit.h"
#include <libnrtype/font-instance.h>
#include <gtk/gtk.h>

#ifdef WITH_GTKSPELL
extern "C" {
# include <gtkspell/gtkspell.h>
}
#endif

#include <gtkmm/stock.h>
#include <libnrtype/font-instance.h>
#include <libnrtype/font-lister.h>
#include <xml/repr.h>

#include "macros.h"
#include "helper/window.h"
#include "inkscape.h"
#include "document.h"
#include "desktop.h"
#include "desktop-style.h"
#include "desktop-handles.h"
#include "document-undo.h"
#include "selection.h"
#include "style.h"
#include "sp-text.h"
#include "sp-flowtext.h"
#include "text-editing.h"
#include "ui/icon-names.h"
#include "preferences.h"
#include "verbs.h"
#include "interface.h"
#include "svg/css-ostringstream.h"
#include "widgets/icon.h"
#include "widgets/font-selector.h"
#include <glibmm/i18n.h>
#include <glibmm/markup.h>
#include "util/units.h"
#include "sp-textpath.h"

namespace Inkscape {
namespace UI {
namespace Dialog {

TextEdit::TextEdit()
    : UI::Widget::Panel("", "/dialogs/textandfont", SP_VERB_DIALOG_TEXT),
      font_label(_("_Font"), true),
      layout_frame(),
      text_label(_("_Text"), true),
      setasdefault_button(_("Set as _default")),
      close_button(Gtk::Stock::CLOSE),
      apply_button(Gtk::Stock::APPLY),
      desktop(NULL),
      deskTrack(),
      selectChangedConn(),
      subselChangedConn(),
      selectModifiedConn(),
      /*
           TRANSLATORS: Test string used in text and font dialog (when no
           * text has been entered) to get a preview of the font.  Choose
           * some representative characters that users of your locale will be
           * interested in.*/
      blocked(false),
      samplephrase(_("AaBbCcIiPpQq12369$\342\202\254\302\242?.;/()"))
{

    /* Font selector */
    GtkWidget *fontsel = sp_font_selector_new ();
    gtk_widget_set_size_request (fontsel, 0, 150);
    fsel = SP_FONT_SELECTOR(fontsel);
    fontsel_hbox.pack_start(*Gtk::manage(Glib::wrap(fontsel)), true, true);

    /* Align buttons */
    styleButton(&align_left,    _("Align left"),                 INKSCAPE_ICON("format-justify-left"),    NULL);
    styleButton(&align_center,  _("Align center"),               INKSCAPE_ICON("format-justify-center"), &align_left);
    styleButton(&align_right,   _("Align right"),                INKSCAPE_ICON("format-justify-right"),  &align_left);
    styleButton(&align_justify, _("Justify (only flowed text)"), INKSCAPE_ICON("format-justify-fill"),   &align_left);

#if WITH_GTKMM_3_0
    align_sep.set_orientation(Gtk::ORIENTATION_VERTICAL);
#endif

    layout_hbox.pack_start(align_sep, false, false, 10);

    /* Direction buttons */
    styleButton(&text_horizontal, _("Horizontal text"), INKSCAPE_ICON("format-text-direction-horizontal"), NULL);
    styleButton(&text_vertical, _("Vertical text"), INKSCAPE_ICON("format-text-direction-vertical"), &text_horizontal);

#if WITH_GTKMM_3_0
    text_sep.set_orientation(Gtk::ORIENTATION_VERTICAL);
#endif

    layout_hbox.pack_start(text_sep, false, false, 10);

    /* Line Spacing */
    GtkWidget *px = sp_icon_new( Inkscape::ICON_SIZE_SMALL_TOOLBAR, INKSCAPE_ICON("text_line_spacing") );
    layout_hbox.pack_start(*Gtk::manage(Glib::wrap(px)), false, false);

    spacing_combo = gtk_combo_box_text_new_with_entry ();
    gtk_widget_set_size_request (spacing_combo, 90, -1);

    const gchar *spacings[] = {"50%", "80%", "90%", "100%", "110%", "120%", "130%", "140%", "150%", "200%", "300%", NULL};
    for (int i = 0; spacings[i]; i++) {
        gtk_combo_box_text_append_text((GtkComboBoxText *) spacing_combo, spacings[i]);
    }

    gtk_widget_set_tooltip_text (px, _("Spacing between lines (percent of font size)"));
    gtk_widget_set_tooltip_text (spacing_combo, _("Spacing between lines (percent of font size)"));
    layout_hbox.pack_start(*Gtk::manage(Glib::wrap(spacing_combo)), false, false);
    layout_frame.set_padding(4,4,4,4);
    layout_frame.add(layout_hbox);

    // Text start Offset
    {
        startOffset = gtk_combo_box_text_new_with_entry ();
        gtk_widget_set_size_request(startOffset, 90, -1);

        const gchar *spacings[] = {"0%", "10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%", NULL};
        for (int i = 0; spacings[i]; i++) {
            gtk_combo_box_text_append_text(reinterpret_cast<GtkComboBoxText *>(startOffset), spacings[i]);
        }
        gtk_entry_set_text(reinterpret_cast<GtkEntry *>(gtk_bin_get_child(reinterpret_cast<GtkBin *>(startOffset))), "0%");

        gtk_widget_set_tooltip_text(startOffset, _("Text path offset"));

#if WITH_GTKMM_3_0
        Gtk::Separator *sep = Gtk::manage(new Gtk::Separator());
        sep->set_orientation(Gtk::ORIENTATION_VERTICAL);
#else
        Gtk::VSeparator *sep = Gtk::manage(new Gtk::VSeparator);
#endif
        layout_hbox.pack_start(*sep, false, false, 10);

        layout_hbox.pack_start(*Gtk::manage(Glib::wrap(startOffset)), false, false);
    }

    /* Font preview */
    preview_label.set_ellipsize(Pango::ELLIPSIZE_END);
    preview_label.set_justify(Gtk::JUSTIFY_CENTER);
    preview_label.set_line_wrap(FALSE);

    font_vbox.pack_start(fontsel_hbox, true, true);
    font_vbox.pack_start(layout_frame, false, false, VB_MARGIN);
    font_vbox.pack_start(preview_label, true, true, VB_MARGIN);

    /* Text tab */
    scroller.set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC );
    scroller.set_shadow_type(Gtk::SHADOW_IN);

    text_buffer = gtk_text_buffer_new (NULL);
    text_view = gtk_text_view_new_with_buffer (text_buffer);
    gtk_text_view_set_wrap_mode ((GtkTextView *) text_view, GTK_WRAP_WORD);

#ifdef WITH_GTKSPELL
    GError *error = NULL;

/*
       TODO: Use computed xml:lang attribute of relevant element, if present, to specify the
       language (either as 2nd arg of gtkspell_new_attach, or with explicit
       gtkspell_set_language call in; see advanced.c example in gtkspell docs).
       onReadSelection looks like a suitable place.
*/
    if (gtkspell_new_attach(GTK_TEXT_VIEW(text_view), NULL, &error) == NULL) {
        g_print("gtkspell error: %s\n", error->message);
        g_error_free(error);
    }
#endif

    gtk_widget_set_size_request (text_view, -1, 64);
    gtk_text_view_set_editable (GTK_TEXT_VIEW (text_view), TRUE);
    scroller.add(*Gtk::manage(Glib::wrap(text_view)));
    text_vbox.pack_start(scroller, true, true, 0);

    notebook.append_page(font_vbox, font_label);
    notebook.append_page(text_vbox, text_label);

    /* Buttons */
    setasdefault_button.set_use_underline(true);
    apply_button.set_can_default();
    button_row.pack_start(setasdefault_button, false, false, 0);
    button_row.pack_end(close_button, false, false, VB_MARGIN);
    button_row.pack_end(apply_button, false, false, VB_MARGIN);

    Gtk::Box *contents = _getContents();
    contents->set_spacing(4);
    contents->pack_start(notebook, true, true);
    contents->pack_start(button_row, false, false, VB_MARGIN);

    /* Signal handlers */
    g_signal_connect ( G_OBJECT (fontsel), "font_set", G_CALLBACK (onFontChange), this );
    g_signal_connect ( G_OBJECT (spacing_combo), "changed", G_CALLBACK (onLineSpacingChange), this );
    g_signal_connect ( G_OBJECT (text_buffer), "changed", G_CALLBACK (onTextChange), this );
    g_signal_connect(startOffset, "changed", G_CALLBACK(onStartOffsetChange), this);
    setasdefault_button.signal_clicked().connect(sigc::mem_fun(*this, &TextEdit::onSetDefault));
    apply_button.signal_clicked().connect(sigc::mem_fun(*this, &TextEdit::onApply));
    close_button.signal_clicked().connect(sigc::bind(_signal_response.make_slot(), GTK_RESPONSE_CLOSE));

    desktopChangeConn = deskTrack.connectDesktopChanged( sigc::mem_fun(*this, &TextEdit::setTargetDesktop) );
    deskTrack.connect(GTK_WIDGET(gobj()));

    show_all_children();
}

TextEdit::~TextEdit()
{
    selectModifiedConn.disconnect();
    subselChangedConn.disconnect();
    selectChangedConn.disconnect();
    desktopChangeConn.disconnect();
    deskTrack.disconnect();
}

void TextEdit::styleButton(Gtk::RadioButton *button, gchar const *tooltip, gchar const *icon_name, Gtk::RadioButton *group_button )
{
    GtkWidget *icon = sp_icon_new( Inkscape::ICON_SIZE_SMALL_TOOLBAR, icon_name );
    if (!GTK_IS_IMAGE(icon)) {
        icon = gtk_image_new_from_icon_name ( icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR );
    }

    if (group_button) {
        Gtk::RadioButton::Group group = group_button->get_group();
        button->set_group(group);
    }

    button->add(*Gtk::manage(Glib::wrap(icon)));
    button->set_tooltip_text(tooltip);
    button->set_relief(Gtk::RELIEF_NONE);
    button->set_mode(false);
    button->signal_clicked().connect(sigc::mem_fun(*this, &TextEdit::onToggle));

    layout_hbox.pack_start(*button, false, false);
}

void TextEdit::onSelectionModified(guint flags )
{
    gboolean style, content;

    style =  ((flags & ( SP_OBJECT_CHILD_MODIFIED_FLAG |
                    SP_OBJECT_STYLE_MODIFIED_FLAG  )) != 0 );

    content = ((flags & ( SP_OBJECT_CHILD_MODIFIED_FLAG |
                    SP_TEXT_CONTENT_MODIFIED_FLAG  )) != 0 );

    onReadSelection (style, content);
}

void TextEdit::onReadSelection ( gboolean dostyle, gboolean /*docontent*/ )
{
    if (blocked)
        return;

    if (!desktop || SP_ACTIVE_DESKTOP != desktop)
    {
        return;
    }

    blocked = true;

    SPItem *text = getSelectedTextItem ();

    Glib::ustring phrase = samplephrase;

    if (text)
    {
        guint items = getSelectedTextCount ();
        if (items == 1) {
            gtk_widget_set_sensitive (text_view, TRUE);
            gtk_widget_set_sensitive( startOffset, SP_IS_TEXT_TEXTPATH(text) );
            if (SP_IS_TEXT_TEXTPATH(text)) {
                SPTextPath *tp = SP_TEXTPATH(text->firstChild());
                if (tp->getAttribute("startOffset")) {
                    gtk_entry_set_text(reinterpret_cast<GtkEntry *>(gtk_bin_get_child(reinterpret_cast<GtkBin *>(startOffset))), tp->getAttribute("startOffset"));
                }
            }
        } else {
            gtk_widget_set_sensitive (text_view, FALSE);
            gtk_widget_set_sensitive( startOffset, FALSE );
        }
        apply_button.set_sensitive ( false );
        setasdefault_button.set_sensitive ( true );

        //if (docontent) {  // When would we NOT want to show the content ?
            gchar *str;
            str = sp_te_get_string_multiline (text);
            if (str) {
                if (items == 1) {
                    gtk_text_buffer_set_text (text_buffer, str, strlen (str));
                    gtk_text_buffer_set_modified (text_buffer, FALSE);
                }
                phrase = str;

            } else {
                gtk_text_buffer_set_text (text_buffer, "", 0);
            }
        //} // end of if (docontent)
        text->getRepr(); // was being called but result ignored. Check this.
    } else {
        gtk_widget_set_sensitive (text_view, FALSE);
        gtk_widget_set_sensitive( startOffset, FALSE );
        apply_button.set_sensitive ( false );
        setasdefault_button.set_sensitive ( 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

        Inkscape::FontLister* fontlister = Inkscape::FontLister::get_instance();

        // This is done for us by text-toolbar. No need to do it twice.
        // fontlister->update_font_list( sp_desktop_document( SP_ACTIVE_DESKTOP ));
        // fontlister->selection_update();

        Glib::ustring fontspec = fontlister->get_fontspec();

        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
        int unit = prefs->getInt("/options/font/unitType", SP_CSS_UNIT_PT);
        double size = sp_style_css_size_px_to_units(query->font_size.computed, unit); 
        sp_font_selector_set_fontspec(fsel, fontspec, size );

        setPreviewText (fontspec, phrase);

        if (query->text_anchor.computed == SP_CSS_TEXT_ANCHOR_START) {
            if (query->text_align.computed == SP_CSS_TEXT_ALIGN_JUSTIFY) {
                align_justify.set_active();
            } else {
                align_left.set_active();
            }
        } else if (query->text_anchor.computed == SP_CSS_TEXT_ANCHOR_MIDDLE) {
            align_center.set_active();
        } else {
            align_right.set_active();
        }

        if (query->writing_mode.computed == SP_CSS_WRITING_MODE_LR_TB) {
            text_horizontal.set_active();
        } else {
            text_vertical.set_active();
        }

        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 *) gtk_bin_get_child ((GtkBin *) spacing_combo), sstr);
        g_free(sstr);

        sp_style_unref(query);
    }
    blocked = false;
}


void TextEdit::setPreviewText (Glib::ustring font_spec, Glib::ustring phrase)
{
    if (font_spec.empty()) {
        return;
    }

    Glib::ustring phrase_escaped = Glib::Markup::escape_text( phrase );

    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
    int unit = prefs->getInt("/options/font/unitType", SP_CSS_UNIT_PT);
    double pt_size = Inkscape::Util::Quantity::convert(sp_style_css_size_units_to_px(sp_font_selector_get_size(fsel), unit), "px", "pt");

    // Pango font size is in 1024ths of a point
    // C++11: Glib::ustring size = std::to_string( int(pt_size * PANGO_SCALE) );
    std::ostringstream size_st;
    size_st << int(pt_size * PANGO_SCALE); // Markup code expects integers

    Glib::ustring markup = "<span font=\'" + font_spec +
        "\' size=\'" + size_st.str() + "\'>" + phrase_escaped + "</span>";

    preview_label.set_markup(markup.c_str());
}


SPItem *TextEdit::getSelectedTextItem (void)
{
    if (!SP_ACTIVE_DESKTOP)
        return NULL;

    for (const GSList *item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->itemList();
         item != NULL;
         item = item->next)
    {
        if (SP_IS_TEXT(item->data) || SP_IS_FLOWTEXT(item->data))
            return SP_ITEM (item->data);
    }

    return NULL;
}


unsigned TextEdit::getSelectedTextCount (void)
{
    if (!SP_ACTIVE_DESKTOP)
        return 0;

    unsigned int items = 0;

    for (const GSList *item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->itemList();
         item != NULL;
         item = item->next)
    {
        if (SP_IS_TEXT(item->data) || SP_IS_FLOWTEXT(item->data))
            ++items;
    }

    return items;
}

void TextEdit::onSelectionChange()
{
    onReadSelection (TRUE, TRUE);
}

void TextEdit::updateObjectText ( SPItem *text )
{
        GtkTextIter start, end;

        // write text
        if (gtk_text_buffer_get_modified (text_buffer)) {
            gtk_text_buffer_get_bounds (text_buffer, &start, &end);
            gchar *str = gtk_text_buffer_get_text (text_buffer, &start, &end, TRUE);
            sp_te_set_repr_text_multiline (text, str);
            g_free (str);
            gtk_text_buffer_set_modified (text_buffer, FALSE);
        }
}

SPCSSAttr *TextEdit::fillTextStyle ()
{
        SPCSSAttr *css = sp_repr_css_attr_new ();

        Glib::ustring fontspec = sp_font_selector_get_fontspec (fsel);

        if( !fontspec.empty() ) {

            Inkscape::FontLister *fontlister = Inkscape::FontLister::get_instance();
            fontlister->fill_css( css, fontspec );

            // TODO, possibly move this to FontLister::set_css to be shared.
            Inkscape::CSSOStringStream os;
            Inkscape::Preferences *prefs = Inkscape::Preferences::get();
            int unit = prefs->getInt("/options/font/unitType", SP_CSS_UNIT_PT);
            if (prefs->getBool("/options/font/textOutputPx", true)) {
                os << sp_style_css_size_units_to_px(sp_font_selector_get_size (fsel), unit) << sp_style_get_css_unit_string(SP_CSS_UNIT_PX);
            } else {
                os << sp_font_selector_get_size (fsel) << sp_style_get_css_unit_string(unit);
            }
            sp_repr_css_set_property (css, "font-size", os.str().c_str());
        }

        // Layout
        if ( align_left.get_active() ) {
            sp_repr_css_set_property (css, "text-anchor", "start");
            sp_repr_css_set_property (css, "text-align", "start");
        } else  if ( align_center.get_active() ) {
            sp_repr_css_set_property (css, "text-anchor", "middle");
            sp_repr_css_set_property (css, "text-align", "center");
        } else  if ( align_right.get_active() ){
            sp_repr_css_set_property (css, "text-anchor", "end");
            sp_repr_css_set_property (css, "text-align", "end");
        } else {
            // Align Justify
            sp_repr_css_set_property (css, "text-anchor", "start");
            sp_repr_css_set_property (css, "text-align", "justify");
        }

        if (text_horizontal.get_active()) {
            sp_repr_css_set_property (css, "writing-mode", "lr");
        } else {
            sp_repr_css_set_property (css, "writing-mode", "tb");
        }

        // Note that CSS 1.1 does not support line-height; we set it for consistency, but also set
        // sodipodi:linespacing for backwards compatibility; in 1.2 we use line-height for flowtext

        const gchar *sstr = gtk_combo_box_text_get_active_text ((GtkComboBoxText *) spacing_combo);
        sp_repr_css_set_property (css, "line-height", sstr);

        return css;
}

void TextEdit::onSetDefault()
{
    SPCSSAttr *css = fillTextStyle ();
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();

    blocked = true;
    prefs->mergeStyle("/tools/text/style", css);
    blocked = false;

    sp_repr_css_attr_unref (css);

    setasdefault_button.set_sensitive ( false );
}

void TextEdit::onApply()
{
    blocked = true;

    SPDesktop *desktop = SP_ACTIVE_DESKTOP;

    unsigned items = 0;
    const GSList *item_list = sp_desktop_selection(desktop)->itemList();
    SPCSSAttr *css = fillTextStyle ();
    sp_desktop_set_style(desktop, css, true);

    for (; item_list != NULL; item_list = item_list->next) {
        // apply style to the reprs of all text objects in the selection
        if (SP_IS_TEXT (item_list->data)) {

            // backwards compatibility:
            reinterpret_cast<SPObject*>(item_list->data)->getRepr()->setAttribute("sodipodi:linespacing", sp_repr_css_property (css, "line-height", NULL));

            ++items;
        }
        else if (SP_IS_FLOWTEXT (item_list->data))
            // no need to set sodipodi:linespacing, because Inkscape never supported it on flowtext
            ++items;
    }

    if (items == 0) {
        // no text objects; apply style to prefs for new objects
        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
        prefs->mergeStyle("/tools/text/style", css);
        setasdefault_button.set_sensitive ( false );

    } else if (items == 1) {
        // exactly one text object; now set its text, too
        SPItem *item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
        if (SP_IS_TEXT (item) || SP_IS_FLOWTEXT(item)) {
            updateObjectText (item);
        }
    }

    // Update FontLister
    Glib::ustring fontspec = sp_font_selector_get_fontspec (fsel);
    if( !fontspec.empty() ) {
        Inkscape::FontLister *fontlister = Inkscape::FontLister::get_instance();
        fontlister->set_fontspec( fontspec, false );
    }

    // complete the transaction
    DocumentUndo::done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_CONTEXT_TEXT,
                       _("Set text style"));
    apply_button.set_sensitive ( false );

    sp_repr_css_attr_unref (css);
    blocked = false;
}

void TextEdit::onTextChange (GtkTextBuffer *text_buffer, TextEdit *self)
{
    if (!self || self->blocked) {
        return;
    }

    SPItem *text = self->getSelectedTextItem();

    GtkTextIter start;
    GtkTextIter end;
    gtk_text_buffer_get_bounds (text_buffer, &start, &end);
    gchar *str = gtk_text_buffer_get_text(text_buffer, &start, &end, TRUE);
    Glib::ustring fontspec = sp_font_selector_get_fontspec(self->fsel);

    if( !fontspec.empty() ) {
        const gchar *phrase = str && *str ? str : self->samplephrase.c_str();
        self->setPreviewText(fontspec, phrase);
    } else {
        self->preview_label.set_markup("");
    }
    g_free (str);

    if (text) {
        self->apply_button.set_sensitive ( true );
        //self->onApply();
    }
    self->setasdefault_button.set_sensitive ( true);
}

void TextEdit::onFontChange(SPFontSelector * /*fontsel*/, gchar* fontspec, TextEdit *self)
{
    GtkTextIter start, end;
    gchar *str;

    if (!self || self->blocked)
        return;

    SPItem *text = self->getSelectedTextItem ();

    gtk_text_buffer_get_bounds (self->text_buffer, &start, &end);
    str = gtk_text_buffer_get_text (self->text_buffer, &start, &end, TRUE);

    if (fontspec) {
        const gchar *phrase = str && *str ? str : self->samplephrase.c_str();
        self->setPreviewText(fontspec, phrase);
    } else {
        self->preview_label.set_markup("");
    }
    g_free(str);

    if (text) {
        self->apply_button.set_sensitive ( true );
        //self->onApply();
    }
    self->setasdefault_button.set_sensitive ( true );

}

void TextEdit::onStartOffsetChange(GtkTextBuffer * /*text_buffer*/, TextEdit *self)
{
    SPItem *text = self->getSelectedTextItem();
    if (text && SP_IS_TEXT_TEXTPATH(text))
    {
        SPTextPath *tp = SP_TEXTPATH(text->firstChild());
        const gchar *sstr = gtk_combo_box_text_get_active_text(reinterpret_cast<GtkComboBoxText *>(self->startOffset));
        tp->setAttribute("startOffset", sstr);

        DocumentUndo::maybeDone(sp_desktop_document(SP_ACTIVE_DESKTOP), "startOffset", SP_VERB_CONTEXT_TEXT, _("Set text style"));
    }
}

void TextEdit::onToggle()
{
    if (blocked)
        return;

    SPItem *text = getSelectedTextItem ();

    if (text) {
        apply_button.set_sensitive ( true );
        //onApply();
    }
    setasdefault_button.set_sensitive ( true );
}


void TextEdit::onLineSpacingChange(GtkComboBox* /*widget*/, gpointer data)
{
    TextEdit *self  = static_cast<TextEdit *>(data);
    if (!self || self->blocked)
        return;

    SPItem *text = self->getSelectedTextItem ();

    if (text) {
        self->apply_button.set_sensitive ( true );
        //self->onApply();
    }
    self->setasdefault_button.set_sensitive ( true );
}

void TextEdit::setDesktop(SPDesktop *desktop)
{
    Panel::setDesktop(desktop);
    deskTrack.setBase(desktop);
}

void TextEdit::setTargetDesktop(SPDesktop *desktop)
{
    if (this->desktop != desktop) {
        if (this->desktop) {
            selectModifiedConn.disconnect();
            subselChangedConn.disconnect();
            selectChangedConn.disconnect();
        }
        this->desktop = desktop;
        if (desktop && desktop->selection) {
            selectChangedConn = desktop->selection->connectChanged(sigc::hide(sigc::mem_fun(*this, &TextEdit::onSelectionChange)));
            subselChangedConn = desktop->connectToolSubselectionChanged(sigc::hide(sigc::mem_fun(*this, &TextEdit::onSelectionChange)));
            selectModifiedConn = desktop->selection->connectModified(sigc::hide<0>(sigc::mem_fun(*this, &TextEdit::onSelectionModified)));
        }
        //widget_setup();
        onReadSelection (TRUE, TRUE);
    }
}

} //namespace Dialog
} //namespace UI
} //namespace Inkscape

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :