Code

NR::Maybe => boost::optional
[inkscape.git] / src / selcue.cpp
1 #define __SELCUE_C__
3 /*
4  * Helper object for showing selected items
5  *
6  * Authors:
7  *   bulia byak <bulia@users.sf.net>
8  *   Carl Hetherington <inkscape@carlh.net>
9  *
10  * Copyright (C) 2004 Authors
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #include <string.h>
17 #include "desktop-handles.h"
18 #include "selection.h"
19 #include "display/sp-canvas-util.h"
20 #include "display/sodipodi-ctrl.h"
21 #include "display/sodipodi-ctrlrect.h"
22 #include "libnrtype/Layout-TNG.h"
23 #include "text-editing.h"
24 #include "sp-text.h"
25 #include "sp-flowtext.h"
26 #include "prefs-utils.h"
27 #include "selcue.h"
29 Inkscape::SelCue::SelCue(SPDesktop *desktop)
30     : _desktop(desktop)
31 {
32     _selection = sp_desktop_selection(_desktop);
34     _sel_changed_connection = _selection->connectChanged(
35         sigc::hide(sigc::mem_fun(*this, &Inkscape::SelCue::_updateItemBboxes))
36         );
38     _sel_modified_connection = _selection->connectModified(
39         sigc::hide(sigc::hide(sigc::mem_fun(*this, &Inkscape::SelCue::_updateItemBboxes)))
40         );
42     _updateItemBboxes();
43 }
45 Inkscape::SelCue::~SelCue()
46 {
47     _sel_changed_connection.disconnect();
48     _sel_modified_connection.disconnect();
50     for (std::list<SPCanvasItem*>::iterator i = _item_bboxes.begin(); i != _item_bboxes.end(); i++) {
51         gtk_object_destroy(*i);
52     }
53     _item_bboxes.clear();
55     for (std::list<SPCanvasItem*>::iterator i = _text_baselines.begin(); i != _text_baselines.end(); i++) {
56         gtk_object_destroy(*i);
57     }
58     _text_baselines.clear();
59 }
61 void Inkscape::SelCue::_updateItemBboxes()
62 {
63     for (std::list<SPCanvasItem*>::iterator i = _item_bboxes.begin(); i != _item_bboxes.end(); i++) {
64         gtk_object_destroy(*i);
65     }
66     _item_bboxes.clear();
68     for (std::list<SPCanvasItem*>::iterator i = _text_baselines.begin(); i != _text_baselines.end(); i++) {
69         gtk_object_destroy(*i);
70     }
71     _text_baselines.clear();
73     gint mode = prefs_get_int_attribute ("options.selcue", "value", MARK);
74     if (mode == NONE) {
75         return;
76     }
78     g_return_if_fail(_selection != NULL);
80     int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
81     SPItem::BBoxType bbox_type = (prefs_bbox ==0)? 
82         SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
83     
84     for (GSList const *l = _selection->itemList(); l != NULL; l = l->next) {
85         SPItem *item = (SPItem *) l->data;
87         boost::optional<NR::Rect> const b = sp_item_bbox_desktop(item, bbox_type);
89         SPCanvasItem* box = NULL;
91         if (b) {
92             if (mode == MARK) {
93                 box = sp_canvas_item_new(sp_desktop_controls(_desktop),
94                                          SP_TYPE_CTRL,
95                                          "mode", SP_CTRL_MODE_XOR,
96                                          "shape", SP_CTRL_SHAPE_DIAMOND,
97                                          "size", 5.0,
98                                          "filled", TRUE,
99                                          "fill_color", 0x000000ff,
100                                          "stroked", FALSE,
101                                          "stroke_color", 0x000000ff,
102                                          NULL);
103                 sp_canvas_item_show(box);
104                 SP_CTRL(box)->moveto(NR::Point(b->min()[NR::X], b->max()[NR::Y]));
106                 sp_canvas_item_move_to_z(box, 0); // just low enough to not get in the way of other draggable knots
108             } else if (mode == BBOX) {
109                 box = sp_canvas_item_new(sp_desktop_controls(_desktop),
110                                          SP_TYPE_CTRLRECT,
111                                          NULL);
113                 SP_CTRLRECT(box)->setRectangle(*b);
114                 SP_CTRLRECT(box)->setColor(0x000000a0, 0, 0);
115                 SP_CTRLRECT(box)->setDashed(true);
117                 sp_canvas_item_move_to_z(box, 0);
118             }
119         }
121         if (box) {
122             _item_bboxes.push_back(box);
123         }
125         SPCanvasItem* baseline_point = NULL;
126         if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) { // visualize baseline
127             Inkscape::Text::Layout const *layout = te_get_layout(item);
128             if (layout != NULL) {
129                 NR::Point a = layout->characterAnchorPoint(layout->begin()) * from_2geom(sp_item_i2d_affine(item));
130                 baseline_point = sp_canvas_item_new(sp_desktop_controls(_desktop), SP_TYPE_CTRL,
131                                                     "mode", SP_CTRL_MODE_XOR,
132                                                     "size", 4.0,
133                                                     "filled", 0,
134                                                     "stroked", 1,
135                                                     "stroke_color", 0x000000ff,
136                                                     NULL);
138                 sp_canvas_item_show(baseline_point);
139                 SP_CTRL(baseline_point)->moveto(a);
140                 sp_canvas_item_move_to_z(baseline_point, 0);
141             }
142         }
144         if (baseline_point) {
145                _text_baselines.push_back(baseline_point);
146         }
147     }
150 /*
151   Local Variables:
152   mode:c++
153   c-file-style:"stroustrup"
154   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
155   indent-tabs-mode:nil
156   fill-column:99
157   End:
158 */
159 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :