Code

81bce14c30ea84b4de0f23a3bea0ec793ae33874
[inkscape.git] / src / ui / dialog / align-and-distribute.cpp
1 /**
2  * \brief Align and Distribute dialog
3  *
4  * Authors:
5  *   Bryce W. Harrington <bryce@bryceharrington.org>
6  *   Aubanel MONNIER <aubi@libertysurf.fr>
7  *   Frank Felfe <innerspace@iname.com>
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *   Tim Dwyer <tgdwyer@gmail.com>
10  *
11  * Copyright (C) 1999-2004, 2005 Authors
12  *
13  * Released under GNU GPL.  Read the file 'COPYING' for more information.
14  */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
21 #include "verbs.h"
23 #include "dialogs/unclump.h"
24 #include "removeoverlap/removeoverlap.h"
25 #include "graphlayout/graphlayout.h"
27 #include <gtkmm/spinbutton.h>
32 #include "util/glib-list-iterators.h"
34 #include "widgets/icon.h"
36 #include "inkscape.h"
37 #include "document.h"
38 #include "selection.h"
39 #include "desktop-handles.h"
40 #include "macros.h"
41 #include "sp-item-transform.h"
42 #include "prefs-utils.h"
43 #include "enums.h"
45 #include "sp-text.h"
46 #include "sp-flowtext.h"
47 #include "text-editing.h"
49 #include "node-context.h" //For node align/distribute function
51 #include "tools-switch.h"
53 #include "align-and-distribute.h"
55 namespace Inkscape {
56 namespace UI {
57 namespace Dialog {
59 /////////helper classes//////////////////////////////////
61 class Action {
62 public :
63     Action(const Glib::ustring &id,
64            const Glib::ustring &tiptext,
65            guint row, guint column,
66            Gtk::Table &parent,
67            Gtk::Tooltips &tooltips,
68            AlignAndDistribute &dialog):
69         _dialog(dialog),
70         _id(id),
71         _parent(parent)
72     {
73         Gtk::Widget*  pIcon = Gtk::manage( sp_icon_get_icon( _id, Inkscape::ICON_SIZE_LARGE_TOOLBAR) );
74         Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
75         pButton->set_relief(Gtk::RELIEF_NONE);
76         pIcon->show();
77         pButton->add(*pIcon);
78         pButton->show();
80         pButton->signal_clicked()
81             .connect(sigc::mem_fun(*this, &Action::on_button_click));
82         tooltips.set_tip(*pButton, tiptext);
83         parent.attach(*pButton, column, column+1, row, row+1, Gtk::FILL, Gtk::FILL);
84     }
85     virtual ~Action(){}
87     AlignAndDistribute &_dialog;
89 private :
90     virtual void on_button_click(){}
92     Glib::ustring _id;
93     Gtk::Table &_parent;
94 };
97 class ActionAlign : public Action {
98 public :
99     struct Coeffs {
100        double mx0, mx1, my0, my1;
101         double sx0, sx1, sy0, sy1;
102     };
103     ActionAlign(const Glib::ustring &id,
104                 const Glib::ustring &tiptext,
105                 guint row, guint column,
106                 AlignAndDistribute &dialog,
107                 guint coeffIndex):
108         Action(id, tiptext, row, column,
109                dialog.align_table(), dialog.tooltips(), dialog),
110         _index(coeffIndex),
111         _dialog(dialog)
112     {}
114 private :
116     virtual void on_button_click() {
117         //Retreive selected objects
118         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
119         if (!desktop) return;
121         Inkscape::Selection *selection = sp_desktop_selection(desktop);
122         if (!selection) return;
124         using Inkscape::Util::GSListConstIterator;
125         std::list<SPItem *> selected;
126         selected.insert<GSListConstIterator<SPItem *> >(selected.end(), selection->itemList(), NULL);
127         if (selected.empty()) return;
129         NR::Point mp; //Anchor point
130         AlignAndDistribute::AlignTarget target = _dialog.getAlignTarget();
131         const Coeffs &a= _allCoeffs[_index];
132         switch (target)
133         {
134         case AlignAndDistribute::LAST:
135         case AlignAndDistribute::FIRST:
136         case AlignAndDistribute::BIGGEST:
137         case AlignAndDistribute::SMALLEST:
138         {
139             //Check 2 or more selected objects
140             std::list<SPItem *>::iterator second(selected.begin());
141             ++second;
142             if (second == selected.end())
143                 return;
144             //Find the master (anchor on which the other objects are aligned)
145             std::list<SPItem *>::iterator master(
146                 _dialog.find_master (
147                     selected,
148                     (a.mx0 != 0.0) ||
149                     (a.mx1 != 0.0) )
150                 );
151             //remove the master from the selection
152             SPItem * thing = *master;
153             selected.erase(master);
154             //Compute the anchor point
155             NR::Rect b = sp_item_bbox_desktop (thing);
156             mp = NR::Point(a.mx0 * b.min()[NR::X] + a.mx1 * b.max()[NR::X],
157                            a.my0 * b.min()[NR::Y] + a.my1 * b.max()[NR::Y]);
158             break;
159         }
161         case AlignAndDistribute::PAGE:
162             mp = NR::Point(a.mx1 * sp_document_width(sp_desktop_document(desktop)),
163                            a.my1 * sp_document_height(sp_desktop_document(desktop)));
164             break;
166         case AlignAndDistribute::DRAWING:
167         {
168             NR::Rect b = sp_item_bbox_desktop
169                 ( (SPItem *) sp_document_root (sp_desktop_document (desktop)) );
170             mp = NR::Point(a.mx0 * b.min()[NR::X] + a.mx1 * b.max()[NR::X],
171                            a.my0 * b.min()[NR::Y] + a.my1 * b.max()[NR::Y]);
172             break;
173         }
175         case AlignAndDistribute::SELECTION:
176         {
177             NR::Rect b =  selection->bounds();
178             mp = NR::Point(a.mx0 * b.min()[NR::X] + a.mx1 * b.max()[NR::X],
179                            a.my0 * b.min()[NR::Y] + a.my1 * b.max()[NR::Y]);
180             break;
181         }
183         default:
184             g_assert_not_reached ();
185             break;
186         };  // end of switch
188         // Top hack: temporarily set clone compensation to unmoved, so that we can align/distribute
189         // clones with their original (and the move of the original does not disturb the
190         // clones). The only problem with this is that if there are outside-of-selection clones of
191         // a selected original, they will be unmoved too, possibly contrary to user's
192         // expecation. However this is a minor point compared to making align/distribute always
193         // work as expected, and "unmoved" is the default option anyway.
194         int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
195         prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
197         bool changed = false;
198         //Move each item in the selected list
199         for (std::list<SPItem *>::iterator it(selected.begin());
200              it != selected.end();
201              it++)
202         {
203             sp_document_ensure_up_to_date(sp_desktop_document (desktop));
204             NR::Rect b = sp_item_bbox_desktop (*it);
205             NR::Point const sp(a.sx0 * b.min()[NR::X] + a.sx1 * b.max()[NR::X],
206                                a.sy0 * b.min()[NR::Y] + a.sy1 * b.max()[NR::Y]);
207             NR::Point const mp_rel( mp - sp );
208             if (LInfty(mp_rel) > 1e-9) {
209                 sp_item_move_rel(*it, NR::translate(mp_rel));
210                 changed = true;
211             }
212         }
214         // restore compensation setting
215         prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
217         if (changed) {
218             sp_document_done ( sp_desktop_document (desktop) , SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
219                                _("Align"));
220         }
223     }
224     guint _index;
225     AlignAndDistribute &_dialog;
227     static const Coeffs _allCoeffs[10];
229 };
230 ActionAlign::Coeffs const ActionAlign::_allCoeffs[10] = {
231     {1., 0., 0., 0., 0., 1., 0., 0.},
232     {1., 0., 0., 0., 1., 0., 0., 0.},
233     {.5, .5, 0., 0., .5, .5, 0., 0.},
234     {0., 1., 0., 0., 0., 1., 0., 0.},
235     {0., 1., 0., 0., 1., 0., 0., 0.},
236     {0., 0., 0., 1., 0., 0., 1., 0.},
237     {0., 0., 0., 1., 0., 0., 0., 1.},
238     {0., 0., .5, .5, 0., 0., .5, .5},
239     {0., 0., 1., 0., 0., 0., 1., 0.},
240     {0., 0., 1., 0., 0., 0., 0., 1.}
241 };
243 struct BBoxSort
245     SPItem *item;
246     float anchor;
247     NR::Rect bbox;
248     BBoxSort(SPItem *pItem, NR::Dim2 orientation, double kBegin, double kEnd) :
249         item(pItem),
250         bbox (sp_item_bbox_desktop (pItem))
251     {
252         anchor = kBegin * bbox.min()[orientation] + kEnd * bbox.max()[orientation];
253     }
254     BBoxSort(const BBoxSort &rhs):
255         //NOTE :  this copy ctor is called O(sort) when sorting the vector
256         //this is bad. The vector should be a vector of pointers.
257         //But I'll wait the bohem GC before doing that
258         item(rhs.item), anchor(rhs.anchor), bbox(rhs.bbox) {
259     }
260 };
261 bool operator< (const BBoxSort &a, const BBoxSort &b)
263     return (a.anchor < b.anchor);
266 class ActionDistribute : public Action {
267 public :
268     ActionDistribute(const Glib::ustring &id,
269                      const Glib::ustring &tiptext,
270                      guint row, guint column,
271                      AlignAndDistribute &dialog,
272                      bool onInterSpace,
273                      NR::Dim2 orientation,
274                      double kBegin, double kEnd
275         ):
276         Action(id, tiptext, row, column,
277                dialog.distribute_table(), dialog.tooltips(), dialog),
278         _dialog(dialog),
279         _onInterSpace(onInterSpace),
280         _orientation(orientation),
281         _kBegin(kBegin),
282         _kEnd( kEnd)
283     {}
285 private :
286     virtual void on_button_click() {
287         //Retreive selected objects
288         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
289         if (!desktop) return;
291         Inkscape::Selection *selection = sp_desktop_selection(desktop);
292         if (!selection) return;
294         using Inkscape::Util::GSListConstIterator;
295         std::list<SPItem *> selected;
296         selected.insert<GSListConstIterator<SPItem *> >(selected.end(), selection->itemList(), NULL);
297         if (selected.empty()) return;
299         //Check 2 or more selected objects
300         std::list<SPItem *>::iterator second(selected.begin());
301         ++second;
302         if (second == selected.end()) return;
305         std::vector< BBoxSort  > sorted;
306         for (std::list<SPItem *>::iterator it(selected.begin());
307             it != selected.end();
308             ++it)
309         {
310             BBoxSort b (*it, _orientation, _kBegin, _kEnd);
311             sorted.push_back(b);
312         }
313         //sort bbox by anchors
314         std::sort(sorted.begin(), sorted.end());
316         // see comment in ActionAlign above
317         int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
318         prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
320         unsigned int len = sorted.size();
321         bool changed = false;
322         if (_onInterSpace)
323         {
324             //overall bboxes span
325             float dist = (sorted.back().bbox.max()[_orientation] -
326                           sorted.front().bbox.min()[_orientation]);
327             //space eaten by bboxes
328             float span = 0;
329             for (unsigned int i = 0; i < len; i++)
330             {
331                 span += sorted[i].bbox.extent(_orientation);
332             }
333             //new distance between each bbox
334             float step = (dist - span) / (len - 1);
335             float pos = sorted.front().bbox.min()[_orientation];
336             for ( std::vector<BBoxSort> ::iterator it (sorted.begin());
337                   it < sorted.end();
338                   it ++ )
339             {
340                 if (!NR_DF_TEST_CLOSE (pos, it->bbox.min()[_orientation], 1e-6)) {
341                     NR::Point t(0.0, 0.0);
342                     t[_orientation] = pos - it->bbox.min()[_orientation];
343                     sp_item_move_rel(it->item, NR::translate(t));
344                     changed = true;
345                 }
346                 pos += it->bbox.extent(_orientation);
347                 pos += step;
348             }
349         }
350         else
351         {
352             //overall anchor span
353             float dist = sorted.back().anchor - sorted.front().anchor;
354             //distance between anchors
355             float step = dist / (len - 1);
357             for ( unsigned int i = 0; i < len ; i ++ )
358             {
359                 BBoxSort & it(sorted[i]);
360                 //new anchor position
361                 float pos = sorted.front().anchor + i * step;
362                 //Don't move if we are really close
363                 if (!NR_DF_TEST_CLOSE (pos, it.anchor, 1e-6)) {
364                     //Compute translation
365                     NR::Point t(0.0, 0.0);
366                     t[_orientation] = pos - it.anchor;
367                     //translate
368                     sp_item_move_rel(it.item, NR::translate(t));
369                     changed = true;
370                 }
371             }
372         }
374         // restore compensation setting
375         prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
377         if (changed) {
378             sp_document_done ( sp_desktop_document (desktop), SP_VERB_DIALOG_ALIGN_DISTRIBUTE, 
379                                _("Distribute"));
380         }
381     }
382     guint _index;
383     AlignAndDistribute &_dialog;
384     bool _onInterSpace;
385     NR::Dim2 _orientation;
387     double _kBegin;
388     double _kEnd;
390 };
393 class ActionNode : public Action {
394 public :
395     ActionNode(const Glib::ustring &id,
396                const Glib::ustring &tiptext,
397                guint column,
398                AlignAndDistribute &dialog,
399                NR::Dim2 orientation, bool distribute):
400         Action(id, tiptext, 0, column,
401                dialog.nodes_table(), dialog.tooltips(), dialog),
402         _orientation(orientation),
403         _distribute(distribute)
404     {}
406 private :
407     NR::Dim2 _orientation;
408     bool _distribute;
409     virtual void on_button_click()
410     {
412         if (!SP_ACTIVE_DESKTOP) return;
413         SPEventContext *event_context = sp_desktop_event_context(SP_ACTIVE_DESKTOP);
414         if (!SP_IS_NODE_CONTEXT (event_context)) return ;
416         Inkscape::NodePath::Path *nodepath = SP_NODE_CONTEXT (event_context)->nodepath;
417         if (!nodepath) return;
418         if (_distribute)
419             sp_nodepath_selected_distribute(nodepath, _orientation);
420         else
421             sp_nodepath_selected_align(nodepath, _orientation);
423     }
424 };
426 class ActionRemoveOverlaps : public Action {
427 private:
428     Gtk::Label removeOverlapXGapLabel;
429     Gtk::Label removeOverlapYGapLabel;
430     Gtk::SpinButton removeOverlapXGap;
431     Gtk::SpinButton removeOverlapYGap;
433 public:
434     ActionRemoveOverlaps(Glib::ustring const &id,
435                          Glib::ustring const &tiptext,
436                          guint row,
437                          guint column,
438                          AlignAndDistribute &dialog) :
439         Action(id, tiptext, row, column + 4,
440                dialog.removeOverlap_table(), dialog.tooltips(), dialog)
441     {
442         dialog.removeOverlap_table().set_col_spacings(3);
443     
444         removeOverlapXGap.set_digits(1);
445         removeOverlapXGap.set_size_request(60, -1);
446         removeOverlapXGap.set_increments(1.0, 5.0);
447         removeOverlapXGap.set_range(-1000.0, 1000.0);
448         removeOverlapXGap.set_value(0);
449         dialog.tooltips().set_tip(removeOverlapXGap,
450                                   _("Minimum horizontal gap (in px units) between bounding boxes"));
451         /* TRANSLATORS: Horizontal gap */
452         removeOverlapXGapLabel.set_label(_("H:"));
454         removeOverlapYGap.set_digits(1);
455         removeOverlapYGap.set_size_request(60, -1);
456         removeOverlapYGap.set_increments(1.0, 5.0);
457         removeOverlapYGap.set_range(-1000.0, 1000.0);
458         removeOverlapYGap.set_value(0);
459         dialog.tooltips().set_tip(removeOverlapYGap,
460                                   _("Minimum vertical gap (in px units) between bounding boxes"));
461         /* TRANSLATORS: Vertical gap */
462         removeOverlapYGapLabel.set_label(_("V:"));
464         dialog.removeOverlap_table().attach(removeOverlapXGapLabel, column, column+1, row, row+1, Gtk::FILL, Gtk::FILL);
465         dialog.removeOverlap_table().attach(removeOverlapXGap, column+1, column+2, row, row+1, Gtk::FILL, Gtk::FILL);
466         dialog.removeOverlap_table().attach(removeOverlapYGapLabel, column+2, column+3, row, row+1, Gtk::FILL, Gtk::FILL);
467         dialog.removeOverlap_table().attach(removeOverlapYGap, column+3, column+4, row, row+1, Gtk::FILL, Gtk::FILL);
469     }
471 private :
472     virtual void on_button_click()
473     {
474         if (!SP_ACTIVE_DESKTOP) return;
476         // see comment in ActionAlign above
477         int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
478         prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
480         // xGap and yGap are the minimum space required between bounding rectangles.
481         double const xGap = removeOverlapXGap.get_value();
482         double const yGap = removeOverlapYGap.get_value();
483         removeoverlap(sp_desktop_selection(SP_ACTIVE_DESKTOP)->itemList(),
484                       xGap, yGap);
486         // restore compensation setting
487         prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
489         sp_document_done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_ALIGN_DISTRIBUTE, 
490                          _("Remove overlaps"));
491     }
492 };
494 class ActionGraphLayout : public Action {
495 public:
496     ActionGraphLayout(Glib::ustring const &id,
497                          Glib::ustring const &tiptext,
498                          guint row,
499                          guint column,
500                          AlignAndDistribute &dialog) :
501         Action(id, tiptext, row, column + 4,
502                dialog.graphLayout_table(), dialog.tooltips(), dialog)
503     {}
505 private :
506     virtual void on_button_click()
507     {
508         if (!SP_ACTIVE_DESKTOP) return;
510         // see comment in ActionAlign above
511         int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
512         prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
514         graphlayout(sp_desktop_selection(SP_ACTIVE_DESKTOP)->itemList());
516         // restore compensation setting
517         prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
519         sp_document_done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_ALIGN_DISTRIBUTE, 
520                          _("Arrange connector network"));
521     }
522 };
524 class ActionUnclump : public Action {
525 public :
526     ActionUnclump(const Glib::ustring &id,
527                const Glib::ustring &tiptext,
528                guint row,
529                guint column,
530                AlignAndDistribute &dialog):
531         Action(id, tiptext, row, column,
532                dialog.distribute_table(), dialog.tooltips(), dialog)
533     {}
535 private :
536     virtual void on_button_click()
537     {
538         if (!SP_ACTIVE_DESKTOP) return;
540         // see comment in ActionAlign above
541         int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
542         prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
544         unclump ((GSList *) sp_desktop_selection(SP_ACTIVE_DESKTOP)->itemList());
546         // restore compensation setting
547         prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
549         sp_document_done (sp_desktop_document (SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_ALIGN_DISTRIBUTE, 
550                           _("Unclump"));
551     }
552 };
554 class ActionRandomize : public Action {
555 public :
556     ActionRandomize(const Glib::ustring &id,
557                const Glib::ustring &tiptext,
558                guint row,
559                guint column,
560                AlignAndDistribute &dialog):
561         Action(id, tiptext, row, column,
562                dialog.distribute_table(), dialog.tooltips(), dialog)
563     {}
565 private :
566     virtual void on_button_click()
567     {
568         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
569         if (!desktop) return;
571         Inkscape::Selection *selection = sp_desktop_selection(desktop);
572         if (!selection) return;
574         using Inkscape::Util::GSListConstIterator;
575         std::list<SPItem *> selected;
576         selected.insert<GSListConstIterator<SPItem *> >(selected.end(), selection->itemList(), NULL);
577         if (selected.empty()) return;
579         //Check 2 or more selected objects
580         if (selected.size() < 2) return;
582         // This bbox is cached between calls to randomize, so that there's no growth nor shrink
583         // nor drift on sequential randomizations. Discard cache on global (or better active
584         // desktop's) selection_change signal.
585         if (!_dialog.randomize_bbox_set) {
586             _dialog.randomize_bbox = selection->bounds();
587             _dialog.randomize_bbox_set = true;
588         }
590         // see comment in ActionAlign above
591         int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
592         prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
594         for (std::list<SPItem *>::iterator it(selected.begin());
595             it != selected.end();
596             ++it)
597         {
598             sp_document_ensure_up_to_date(sp_desktop_document (desktop));
599             NR::Rect item_box = sp_item_bbox_desktop (*it);
600             // find new center, staying within bbox 
601             double x = _dialog.randomize_bbox.min()[NR::X] + item_box.extent(NR::X)/2 +
602                 g_random_double_range (0, _dialog.randomize_bbox.extent(NR::X) - item_box.extent(NR::X));
603             double y = _dialog.randomize_bbox.min()[NR::Y] + item_box.extent(NR::Y)/2 +
604                 g_random_double_range (0, _dialog.randomize_bbox.extent(NR::Y) - item_box.extent(NR::Y));
605             // displacement is the new center minus old:
606             NR::Point t = NR::Point (x, y) - 0.5*(item_box.max() + item_box.min());
607             sp_item_move_rel(*it, NR::translate(t));
608         }
610         // restore compensation setting
611         prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
613         sp_document_done (sp_desktop_document (SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_ALIGN_DISTRIBUTE, 
614                           _("Randomize positions"));
615     }
616 };
618 struct Baselines
620     SPItem *_item;
621     NR::Point _base;
622     NR::Dim2 _orientation;
623     Baselines(SPItem *item, NR::Point base, NR::Dim2 orientation) :
624         _item (item),
625         _base (base),
626         _orientation (orientation)
627     {}
628 };
630 bool operator< (const Baselines &a, const Baselines &b)
632     return (a._base[a._orientation] < b._base[b._orientation]);
635 class ActionBaseline : public Action {
636 public :
637     ActionBaseline(const Glib::ustring &id,
638                const Glib::ustring &tiptext,
639                guint row,
640                guint column,
641                AlignAndDistribute &dialog,
642                Gtk::Table &table,
643                NR::Dim2 orientation, bool distribute):
644         Action(id, tiptext, row, column,
645                table, dialog.tooltips(), dialog),
646         _orientation(orientation),
647         _distribute(distribute)
648     {}
650 private :
651     NR::Dim2 _orientation;
652     bool _distribute;
653     virtual void on_button_click()
654     {
655         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
656         if (!desktop) return;
658         Inkscape::Selection *selection = sp_desktop_selection(desktop);
659         if (!selection) return;
661         using Inkscape::Util::GSListConstIterator;
662         std::list<SPItem *> selected;
663         selected.insert<GSListConstIterator<SPItem *> >(selected.end(), selection->itemList(), NULL);
664         if (selected.empty()) return;
666         //Check 2 or more selected objects
667         if (selected.size() < 2) return;
669         NR::Point b_min = NR::Point (HUGE_VAL, HUGE_VAL);
670         NR::Point b_max = NR::Point (-HUGE_VAL, -HUGE_VAL);
672         std::vector<Baselines> sorted;
674         for (std::list<SPItem *>::iterator it(selected.begin());
675             it != selected.end();
676             ++it)
677         {
678             if (SP_IS_TEXT (*it) || SP_IS_FLOWTEXT (*it)) {
679                 Inkscape::Text::Layout const *layout = te_get_layout(*it);
680                 NR::Point base = layout->characterAnchorPoint(layout->begin()) * sp_item_i2d_affine(*it);
681                 if (base[NR::X] < b_min[NR::X]) b_min[NR::X] = base[NR::X];
682                 if (base[NR::Y] < b_min[NR::Y]) b_min[NR::Y] = base[NR::Y];
683                 if (base[NR::X] > b_max[NR::X]) b_max[NR::X] = base[NR::X];
684                 if (base[NR::Y] > b_max[NR::Y]) b_max[NR::Y] = base[NR::Y];
686                 Baselines b (*it, base, _orientation);
687                 sorted.push_back(b);
688             }
689         }
691         if (sorted.size() <= 1) return;
693         //sort baselines
694         std::sort(sorted.begin(), sorted.end());
696         bool changed = false;
698         if (_distribute) {
699             double step = (b_max[_orientation] - b_min[_orientation])/(sorted.size() - 1);
700             for (unsigned int i = 0; i < sorted.size(); i++) {
701                 SPItem *item = sorted[i]._item;
702                 NR::Point base = sorted[i]._base;
703                 NR::Point t(0.0, 0.0);
704                 t[_orientation] = b_min[_orientation] + step * i - base[_orientation];
705                 sp_item_move_rel(item, NR::translate(t));
706                 changed = true;
707             }
709             if (changed) {
710                 sp_document_done (sp_desktop_document (SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_ALIGN_DISTRIBUTE, 
711                                   _("Distribute text baselines"));
712             }
714         } else {
715             for (std::list<SPItem *>::iterator it(selected.begin());
716                  it != selected.end();
717                  ++it)
718             {
719                 if (SP_IS_TEXT (*it) || SP_IS_FLOWTEXT (*it)) {
720                     Inkscape::Text::Layout const *layout = te_get_layout(*it);
721                     NR::Point base = layout->characterAnchorPoint(layout->begin()) * sp_item_i2d_affine(*it);
722                     NR::Point t(0.0, 0.0);
723                     t[_orientation] = b_min[_orientation] - base[_orientation];
724                     sp_item_move_rel(*it, NR::translate(t));
725                     changed = true;
726                 }
727             }
729             if (changed) {
730                 sp_document_done (sp_desktop_document (SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_ALIGN_DISTRIBUTE, 
731                                   _("Align text baselines"));
732             }
733         }
734     }
735 };
739 void on_tool_changed(Inkscape::Application *inkscape, SPEventContext *context, AlignAndDistribute *daad)
741     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
742     if (desktop && sp_desktop_event_context(desktop))
743         daad->setMode(tools_active(desktop) == TOOLS_NODES);
746 void on_selection_changed(Inkscape::Application *inkscape, Inkscape::Selection *selection, AlignAndDistribute *daad)
748     daad->randomize_bbox_set = false;
751 /////////////////////////////////////////////////////////
756 AlignAndDistribute::AlignAndDistribute() 
757     : Dialog ("dialogs.align", SP_VERB_DIALOG_ALIGN_DISTRIBUTE),
758       randomize_bbox (NR::Point (0, 0), NR::Point (0, 0)),
759       _alignFrame(_("Align")),
760       _distributeFrame(_("Distribute")),
761       _removeOverlapFrame(_("Remove overlaps")),
762       _graphLayoutFrame(_("Connector network layout")),
763       _nodesFrame(_("Nodes")),
764       _alignTable(2, 6, true),
765       _distributeTable(3, 6, true),
766       _removeOverlapTable(1, 5, false),
767       _graphLayoutTable(1, 5, false),
768       _nodesTable(1, 4, true),
769       _anchorLabel(_("Relative to: "))
772     //Instanciate the align buttons
773     addAlignButton("al_left_out",
774                    _("Align right sides of objects to left side of anchor"),
775                    0, 0);
776     addAlignButton("al_left_in",
777                    _("Align left sides"),
778                    0, 1);
779     addAlignButton("al_center_hor",
780                    _("Center on vertical axis"),
781                    0, 2);
782     addAlignButton("al_right_in",
783                    _("Align right sides"),
784                    0, 3);
785     addAlignButton("al_right_out",
786                    _("Align left sides of objects to right side of anchor"),
787                    0, 4);
788     addAlignButton("al_top_out",
789                    _("Align bottoms of objects to top of anchor"),
790                    1, 0);
791     addAlignButton("al_top_in",
792                    _("Align tops"),
793                    1, 1);
794     addAlignButton("al_center_ver",
795                    _("Center on horizontal axis"),
796                    1, 2);
797     addAlignButton("al_bottom_in",
798                    _("Align bottoms"),
799                    1, 3);
800     addAlignButton("al_bottom_out",
801                    _("Align tops of objects to bottom of anchor"),
802                    1, 4);
804     //Baseline aligns
805     addBaselineButton("al_baselines_vert",
806                    _("Align baseline anchors of texts vertically"),
807                       0, 5, this->align_table(), NR::X, false);
808     addBaselineButton("al_baselines_hor",
809                    _("Align baseline anchors of texts horizontally"),
810                      1, 5, this->align_table(), NR::Y, false);
812     //The distribute buttons
813     addDistributeButton("distribute_hdist",
814                         _("Make horizontal gaps between objects equal"),
815                         0, 4, true, NR::X, .5, .5);
817     addDistributeButton("distribute_left",
818                         _("Distribute left sides equidistantly"),
819                         0, 1, false, NR::X, 1., 0.);
820     addDistributeButton("distribute_hcentre",
821                         _("Distribute centers equidistantly horizontally"),
822                         0, 2, false, NR::X, .5, .5);
823     addDistributeButton("distribute_right",
824                         _("Distribute right sides equidistantly"),
825                         0, 3, false, NR::X, 0., 1.);
827     addDistributeButton("distribute_vdist",
828                         _("Make vertical gaps between objects equal"),
829                         1, 4, true, NR::Y, .5, .5);
831     addDistributeButton("distribute_top",
832                         _("Distribute tops equidistantly"),
833                         1, 1, false, NR::Y, 0, 1);
834     addDistributeButton("distribute_vcentre",
835                         _("Distribute centers equidistantly vertically"),
836                         1, 2, false, NR::Y, .5, .5);
837     addDistributeButton("distribute_bottom",
838                         _("Distribute bottoms equidistantly"),
839                         1, 3, false, NR::Y, 1., 0.);
841     //Baseline distribs
842     addBaselineButton("distribute_baselines_hor",
843                    _("Distribute baseline anchors of texts horizontally"),
844                       0, 5, this->distribute_table(), NR::X, true);
845     addBaselineButton("distribute_baselines_vert",
846                    _("Distribute baseline anchors of texts vertically"),
847                      1, 5, this->distribute_table(), NR::Y, true);
849     //Randomize & Unclump
850     addRandomizeButton("distribute_randomize",
851                         _("Randomize centers in both dimensions"),
852                         2, 2);
853     addUnclumpButton("unclump",
854                         _("Unclump objects: try to equalize edge-to-edge distances"),
855                         2, 4);
857     //Remove overlaps
858     addRemoveOverlapsButton("remove_overlaps",
859                             _("Move objects as little as possible so that their bounding boxes do not overlap"),
860                             0, 0);
861     //Graph Layout
862     addGraphLayoutButton("graph_layout",
863                             _("Nicely arrange selected connector network"),
864                             0, 0);
866     //Node Mode buttons
867     addNodeButton("node_halign",
868                   _("Align selected nodes horizontally"),
869                   0, NR::X, false);
870     addNodeButton("node_valign",
871                   _("Align selected nodes vertically"),
872                   1, NR::Y, false);
873     addNodeButton("node_hdistribute",
874                   _("Distribute selected nodes horizontally"),
875                   2, NR::X, true);
876     addNodeButton("node_vdistribute",
877                   _("Distribute selected nodes vertically"),
878                   3, NR::Y, true);
880     //Rest of the widgetry
882     _combo.append_text(_("Last selected"));
883     _combo.append_text(_("First selected"));
884     _combo.append_text(_("Biggest item"));
885     _combo.append_text(_("Smallest item"));
886     _combo.append_text(_("Page"));
887     _combo.append_text(_("Drawing"));
888     _combo.append_text(_("Selection"));
890     _combo.set_active(6);
891     _combo.signal_changed().connect(sigc::mem_fun(*this, &AlignAndDistribute::on_ref_change));
893     _anchorBox.pack_start(_anchorLabel);
894     _anchorBox.pack_start(_combo);
896     _alignBox.pack_start(_anchorBox);
897     _alignBox.pack_start(_alignTable);
899     _alignFrame.add(_alignBox);
900     _distributeFrame.add(_distributeTable);
901     _removeOverlapFrame.add(_removeOverlapTable);
902     _graphLayoutFrame.add(_graphLayoutTable);
903     _nodesFrame.add(_nodesTable);
905     // Top level vbox
906     Gtk::VBox *vbox = get_vbox();
907     vbox->set_spacing(4);
909     // Notebook for individual transformations
911     vbox->pack_start(_alignFrame, true, true);
912     vbox->pack_start(_distributeFrame, true, true);
913     vbox->pack_start(_removeOverlapFrame, true, true);
914     vbox->pack_start(_graphLayoutFrame, true, true);
915     vbox->pack_start(_nodesFrame, true, true);
917     //Connect to the global tool change signal
918     g_signal_connect (G_OBJECT (INKSCAPE), "set_eventcontext", G_CALLBACK (on_tool_changed), this);
920     // Connect to the global selection change, to invalidate cached randomize_bbox
921     g_signal_connect (G_OBJECT (INKSCAPE), "change_selection", G_CALLBACK (on_selection_changed), this);
922     randomize_bbox = NR::Rect (NR::Point (0, 0), NR::Point (0, 0));
923     randomize_bbox_set = false;
925     show_all_children();
927     on_tool_changed (NULL, NULL, this); // set current mode
930 AlignAndDistribute::~AlignAndDistribute() 
932     sp_signal_disconnect_by_data (G_OBJECT (INKSCAPE), this);
934     for (std::list<Action *>::iterator it = _actionList.begin();
935          it != _actionList.end();
936          it ++)
937         delete *it;
940 void AlignAndDistribute::on_ref_change(){
941 //Make blink the master
947 void AlignAndDistribute::setMode(bool nodeEdit)
949     //Act on widgets used in node mode
950     void ( Gtk::Widget::*mNode) ()  = nodeEdit ?
951         &Gtk::Widget::show_all : &Gtk::Widget::hide_all;
953     //Act on widgets used in selection mode
954   void ( Gtk::Widget::*mSel) ()  = nodeEdit ?
955       &Gtk::Widget::hide_all : &Gtk::Widget::show_all;
958     ((_alignFrame).*(mSel))();
959     ((_distributeFrame).*(mSel))();
960     ((_removeOverlapFrame).*(mSel))();
961     ((_graphLayoutFrame).*(mSel))();
962     ((_nodesFrame).*(mNode))();
965 void AlignAndDistribute::addAlignButton(const Glib::ustring &id, const Glib::ustring tiptext,
966                                  guint row, guint col)
968     _actionList.push_back(
969         new ActionAlign(
970             id, tiptext, row, col,
971             *this , col + row * 5));
973 void AlignAndDistribute::addDistributeButton(const Glib::ustring &id, const Glib::ustring tiptext,
974                                       guint row, guint col, bool onInterSpace,
975                                       NR::Dim2 orientation, float kBegin, float kEnd)
977     _actionList.push_back(
978         new ActionDistribute(
979             id, tiptext, row, col, *this ,
980             onInterSpace, orientation,
981             kBegin, kEnd
982             )
983         );
986 void AlignAndDistribute::addNodeButton(const Glib::ustring &id, const Glib::ustring tiptext,
987                    guint col, NR::Dim2 orientation, bool distribute)
989     _actionList.push_back(
990         new ActionNode(
991             id, tiptext, col,
992             *this, orientation, distribute));
995 void AlignAndDistribute::addRemoveOverlapsButton(const Glib::ustring &id, const Glib::ustring tiptext,
996                                       guint row, guint col)
998     _actionList.push_back(
999         new ActionRemoveOverlaps(
1000             id, tiptext, row, col, *this)
1001         );
1004 void AlignAndDistribute::addGraphLayoutButton(const Glib::ustring &id, const Glib::ustring tiptext,
1005                                       guint row, guint col)
1007     _actionList.push_back(
1008         new ActionGraphLayout(
1009             id, tiptext, row, col, *this)
1010         );
1013 void AlignAndDistribute::addUnclumpButton(const Glib::ustring &id, const Glib::ustring tiptext,
1014                                       guint row, guint col)
1016     _actionList.push_back(
1017         new ActionUnclump(
1018             id, tiptext, row, col, *this)
1019         );
1022 void AlignAndDistribute::addRandomizeButton(const Glib::ustring &id, const Glib::ustring tiptext,
1023                                       guint row, guint col)
1025     _actionList.push_back(
1026         new ActionRandomize(
1027             id, tiptext, row, col, *this)
1028         );
1031 void AlignAndDistribute::addBaselineButton(const Glib::ustring &id, const Glib::ustring tiptext,
1032                                     guint row, guint col, Gtk::Table &table, NR::Dim2 orientation, bool distribute)
1034     _actionList.push_back(
1035         new ActionBaseline(
1036             id, tiptext, row, col, 
1037             *this, table, orientation, distribute));
1043 std::list<SPItem *>::iterator AlignAndDistribute::find_master( std::list<SPItem *> &list, bool horizontal){
1044     std::list<SPItem *>::iterator master = list.end();
1045     switch (getAlignTarget()) {
1046     case LAST:
1047         return list.begin();
1048         break;
1050     case FIRST:
1051         return --(list.end());
1052         break;
1054     case BIGGEST:
1055     {
1056         gdouble max = -1e18;
1057         for (std::list<SPItem *>::iterator it = list.begin(); it != list.end(); it++) {
1058             NR::Rect b = sp_item_bbox_desktop (*it);
1059             gdouble dim = b.extent(horizontal ? NR::X : NR::Y);
1060             if (dim > max) {
1061                 max = dim;
1062                 master = it;
1063             }
1064         }
1065         return master;
1066         break;
1067     }
1069     case SMALLEST:
1070     {
1071         gdouble max = 1e18;
1072         for (std::list<SPItem *>::iterator it = list.begin(); it != list.end(); it++) {
1073             NR::Rect b = sp_item_bbox_desktop (*it);
1074             gdouble dim = b.extent(horizontal ? NR::X : NR::Y);
1075             if (dim < max) {
1076                 max = dim;
1077                 master = it;
1078             }
1079         }
1080         return master;
1081         break;
1082     }
1084     default:
1085         g_assert_not_reached ();
1086         break;
1088     } // end of switch statement
1089     return master;
1092 AlignAndDistribute::AlignTarget AlignAndDistribute::getAlignTarget()const {
1093     return AlignTarget(_combo.get_active_row_number());
1098 } // namespace Dialog
1099 } // namespace UI
1100 } // namespace Inkscape
1102 /*
1103   Local Variables:
1104   mode:c++
1105   c-file-style:"stroustrup"
1106   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1107   indent-tabs-mode:nil
1108   fill-column:99
1109   End:
1110 */
1111 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :