Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / ui / widget / entity-entry.cpp
1 /** \file
2  *
3  * Authors:
4  *   bulia byak <buliabyak@users.sf.net>
5  *   Bryce W. Harrington <bryce@bryceharrington.org>
6  *   Lauris Kaplinski <lauris@kaplinski.com>
7  *   Jon Phillips <jon@rejon.org>
8  *   Ralf Stephan <ralf@ark.in-berlin.de> (Gtkmm)
9  *   Jon A. Cruz <jon@joncruz.org>
10  *   Abhishek Sharma
11  *
12  * Copyright (C) 2000 - 2005 Authors
13  *
14  * Released under GNU GPL.  Read the file 'COPYING' for more information
15  */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
21 #include <gtkmm/scrolledwindow.h>
22 #include <gtkmm/entry.h>
24 #include "inkscape.h"
25 #include "sp-object.h"
26 #include "rdf.h"
27 #include "ui/widget/registry.h"
29 #include "entity-entry.h"
31 namespace Inkscape {
32 namespace UI {
33 namespace Widget {
35 //===================================================
37 //---------------------------------------------------
39 EntityEntry*
40 EntityEntry::create (rdf_work_entity_t* ent, Gtk::Tooltips& tt, Registry& wr)
41 {
42     g_assert (ent);
43     EntityEntry* obj = 0;
44     switch (ent->format)
45     {
46         case RDF_FORMAT_LINE: 
47             obj = new EntityLineEntry (ent, tt, wr);
48             break;
49         case RDF_FORMAT_MULTILINE: 
50             obj = new EntityMultiLineEntry (ent, tt, wr);
51             break;
52         default:
53             g_warning ("An unknown RDF format was requested.");
54     }
56     g_assert (obj);
57     obj->_label.show();
58     return obj;
59 }
61 EntityEntry::EntityEntry (rdf_work_entity_t* ent, Gtk::Tooltips& tt, Registry& wr)
62 : _label(Glib::ustring(_(ent->title)), 1.0, 0.5), _packable(0), 
63   _entity(ent), _tt(&tt), _wr(&wr)
64 {
65 }
67 EntityEntry::~EntityEntry()
68 {
69     _changed_connection.disconnect();
70 }
72 EntityLineEntry::EntityLineEntry (rdf_work_entity_t* ent, Gtk::Tooltips& tt, Registry& wr)
73 : EntityEntry (ent, tt, wr)
74 {
75     Gtk::Entry *e = new Gtk::Entry;
76     tt.set_tip (*e, _(ent->tip));
77     _packable = e;
78     _changed_connection = e->signal_changed().connect (sigc::mem_fun (*this, &EntityLineEntry::on_changed));
79 }
81 EntityLineEntry::~EntityLineEntry()
82 {
83     delete static_cast<Gtk::Entry*>(_packable);
84 }
86 void EntityLineEntry::update(SPDocument *doc)
87 {
88     const char *text = rdf_get_work_entity (doc, _entity);
89     // If RDF title is not set, get the document's <title> and set the RDF:
90     if ( !text && !strcmp(_entity->name, "title") && doc->root ) {
91         text = doc->root->title();
92         rdf_set_work_entity(doc, _entity, text);
93     }
94     static_cast<Gtk::Entry*>(_packable)->set_text (text ? text : "");
95 }
97 void
98 EntityLineEntry::on_changed()
99 {
100     if (_wr->isUpdating()) return;
102     _wr->setUpdating (true);
103     SPDocument *doc = SP_ACTIVE_DOCUMENT;
104     Glib::ustring text = static_cast<Gtk::Entry*>(_packable)->get_text();
105     if (rdf_set_work_entity (doc, _entity, text.c_str())) {
106         DocumentUndo::done(doc, SP_VERB_NONE, 
107                            /* TODO: annotate */ "entity-entry.cpp:101");
108     }
109     _wr->setUpdating (false);
112 EntityMultiLineEntry::EntityMultiLineEntry (rdf_work_entity_t* ent, Gtk::Tooltips& tt, Registry& wr)
113 : EntityEntry (ent, tt, wr)
115     Gtk::ScrolledWindow *s = new Gtk::ScrolledWindow;
116     s->set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
117     s->set_shadow_type (Gtk::SHADOW_IN);
118     _packable = s;
119     _v.set_size_request (-1, 5);
120     _v.set_wrap_mode (Gtk::WRAP_WORD);
121     _v.set_accepts_tab (false);
122     s->add (_v);
123     tt.set_tip (_v, _(ent->tip));
124     _changed_connection = _v.get_buffer()->signal_changed().connect (sigc::mem_fun (*this, &EntityMultiLineEntry::on_changed));
127 EntityMultiLineEntry::~EntityMultiLineEntry()
129     delete static_cast<Gtk::ScrolledWindow*>(_packable);
132 void EntityMultiLineEntry::update(SPDocument *doc)
134     const char *text = rdf_get_work_entity (doc, _entity);
135     // If RDF title is not set, get the document's <title> and set the RDF:
136     if ( !text && !strcmp(_entity->name, "title") && doc->root ) {
137         text = doc->root->title();
138         rdf_set_work_entity(doc, _entity, text);
139     }
140     Gtk::ScrolledWindow *s = static_cast<Gtk::ScrolledWindow*>(_packable);
141     Gtk::TextView *tv = static_cast<Gtk::TextView*>(s->get_child());
142     tv->get_buffer()->set_text (text ? text : "");
145 void
146 EntityMultiLineEntry::on_changed()
148     if (_wr->isUpdating()) return;
150     _wr->setUpdating (true);
151     SPDocument *doc = SP_ACTIVE_DOCUMENT;
152     Gtk::ScrolledWindow *s = static_cast<Gtk::ScrolledWindow*>(_packable);
153     Gtk::TextView *tv = static_cast<Gtk::TextView*>(s->get_child());
154     Glib::ustring text = tv->get_buffer()->get_text();
155     if (rdf_set_work_entity (doc, _entity, text.c_str())) {
156         DocumentUndo::done(doc, SP_VERB_NONE, 
157                             /* TODO: annotate */ "entity-entry.cpp:146");
158     }
159     _wr->setUpdating (false);
162 } // namespace Dialog
163 } // namespace UI
164 } // namespace Inkscape
166 /*
167   Local Variables:
168   mode:c++
169   c-file-style:"stroustrup"
170   c-file-offsets:((innamespace . 0)(inline-open . 0))
171   indent-tabs-mode:nil
172   fill-column:99
173   End:
174 */
175 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :