2002-05-01 21:35:30 +04:00
|
|
|
//
|
2005-03-20 07:00:48 +03:00
|
|
|
// Alignment code for the Fast Light Tool Kit (FLTK).
|
2002-05-01 21:35:30 +04:00
|
|
|
//
|
2023-08-26 16:17:28 +03:00
|
|
|
// Copyright 1998-2023 by Bill Spitzak and others.
|
2002-05-01 21:35:30 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
2002-05-01 21:35:30 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2005-04-16 04:13:17 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
2002-05-01 21:35:30 +04:00
|
|
|
//
|
|
|
|
|
2021-12-08 17:52:15 +03:00
|
|
|
#include "align_widget.h"
|
|
|
|
|
|
|
|
#include "fluid.h"
|
|
|
|
#include "Fl_Group_Type.h"
|
|
|
|
#include "undo.h"
|
|
|
|
|
2002-05-01 21:35:30 +04:00
|
|
|
#include <FL/Fl.H>
|
2003-01-10 22:29:09 +03:00
|
|
|
#include <FL/Fl_Window.H>
|
2002-05-01 21:35:30 +04:00
|
|
|
|
|
|
|
/**
|
2023-08-26 16:17:28 +03:00
|
|
|
the first behavior always uses the first selected widget as a reference
|
|
|
|
the second behavior uses the largest widget (most extreme positions) as
|
2018-06-23 23:50:22 +03:00
|
|
|
a reference.
|
2002-05-01 21:35:30 +04:00
|
|
|
*/
|
|
|
|
#define BREAK_ON_FIRST break
|
|
|
|
//#define BREAK_ON_FIRST
|
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
void align_widget_cb(Fl_Widget*, long how)
|
2002-05-01 21:35:30 +04:00
|
|
|
{
|
|
|
|
const int max = 32768, min = -32768;
|
|
|
|
int left, right, top, bot, wdt, hgt, n;
|
|
|
|
Fl_Type *o;
|
2005-03-20 07:00:48 +03:00
|
|
|
int changed = 0;
|
2002-05-01 21:35:30 +04:00
|
|
|
switch ( how )
|
|
|
|
{
|
|
|
|
//---- align
|
|
|
|
case 10: // align left
|
|
|
|
left = max;
|
|
|
|
for (o = Fl_Type::first; o; o = o->next)
|
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
|
|
|
if (w->x()<left)
|
|
|
|
left = w->x();
|
|
|
|
BREAK_ON_FIRST;
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
if (left!=max)
|
|
|
|
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
|
2020-07-01 19:03:10 +03:00
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
|
|
|
if (!changed) {
|
|
|
|
changed = 1;
|
|
|
|
undo_checkpoint();
|
|
|
|
}
|
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
2023-11-01 00:00:29 +03:00
|
|
|
Fl_Type::allow_layout++;
|
|
|
|
w->resize(left, w->y(), w->w(), w->h());
|
|
|
|
Fl_Type::allow_layout--;
|
2020-07-01 19:03:10 +03:00
|
|
|
w->redraw();
|
|
|
|
if (w->window()) w->window()->redraw();
|
|
|
|
}
|
2002-05-01 21:35:30 +04:00
|
|
|
break;
|
|
|
|
case 11: // align h.center
|
|
|
|
left = max; right = min;
|
|
|
|
for (o = Fl_Type::first; o; o = o->next)
|
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
|
|
|
if (w->x()<left)
|
|
|
|
left = w->x();
|
|
|
|
if (w->x()+w->w()>right)
|
|
|
|
right = w->x()+w->w();
|
|
|
|
BREAK_ON_FIRST;
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
if (left!=max)
|
|
|
|
{
|
|
|
|
int center2 = left+right;
|
|
|
|
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
|
2020-07-01 19:03:10 +03:00
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
|
|
|
if (!changed) {
|
|
|
|
changed = 1;
|
|
|
|
undo_checkpoint();
|
|
|
|
}
|
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
2023-11-01 00:00:29 +03:00
|
|
|
Fl_Type::allow_layout++;
|
|
|
|
w->resize((center2-w->w())/2, w->y(), w->w(), w->h());
|
|
|
|
Fl_Type::allow_layout--;
|
2020-07-01 19:03:10 +03:00
|
|
|
w->redraw();
|
|
|
|
if (w->window()) w->window()->redraw();
|
|
|
|
}
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 12: // align right
|
|
|
|
right = min;
|
|
|
|
for (o = Fl_Type::first; o; o = o->next)
|
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
|
|
|
if (w->x()+w->w()>right)
|
|
|
|
right = w->x()+w->w();
|
|
|
|
BREAK_ON_FIRST;
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
if (right!=min)
|
|
|
|
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
|
2020-07-01 19:03:10 +03:00
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
|
|
|
if (!changed) {
|
|
|
|
changed = 1;
|
|
|
|
undo_checkpoint();
|
|
|
|
}
|
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
2023-11-01 00:00:29 +03:00
|
|
|
Fl_Type::allow_layout++;
|
|
|
|
w->resize(right-w->w(), w->y(), w->w(), w->h());
|
|
|
|
Fl_Type::allow_layout--;
|
2020-07-01 19:03:10 +03:00
|
|
|
w->redraw();
|
|
|
|
if (w->window()) w->window()->redraw();
|
|
|
|
}
|
2002-05-01 21:35:30 +04:00
|
|
|
break;
|
|
|
|
case 13: // align top
|
|
|
|
top = max;
|
|
|
|
for (o = Fl_Type::first; o; o = o->next)
|
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
|
|
|
if (w->y()<top)
|
|
|
|
top = w->y();
|
|
|
|
BREAK_ON_FIRST;
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
if (top!=max)
|
|
|
|
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
|
2020-07-01 19:03:10 +03:00
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
|
|
|
if (!changed) {
|
|
|
|
changed = 1;
|
|
|
|
undo_checkpoint();
|
|
|
|
}
|
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
2023-11-01 00:00:29 +03:00
|
|
|
Fl_Type::allow_layout++;
|
|
|
|
w->resize(w->x(), top, w->w(), w->h());
|
|
|
|
Fl_Type::allow_layout--;
|
2020-07-01 19:03:10 +03:00
|
|
|
w->redraw();
|
|
|
|
if (w->window()) w->window()->redraw();
|
|
|
|
}
|
2002-05-01 21:35:30 +04:00
|
|
|
break;
|
|
|
|
case 14: // align v.center
|
|
|
|
top = max; bot = min;
|
|
|
|
for (o = Fl_Type::first; o; o = o->next)
|
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
|
|
|
if (w->y()<top)
|
|
|
|
top = w->y();
|
|
|
|
if (w->y()+w->h()>bot)
|
|
|
|
bot = w->y()+w->h();
|
|
|
|
BREAK_ON_FIRST;
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
if (top!=max)
|
|
|
|
{
|
|
|
|
int center2 = top+bot;
|
|
|
|
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
|
2020-07-01 19:03:10 +03:00
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
|
|
|
if (!changed) {
|
|
|
|
changed = 1;
|
|
|
|
undo_checkpoint();
|
|
|
|
}
|
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
2023-11-01 00:00:29 +03:00
|
|
|
Fl_Type::allow_layout++;
|
|
|
|
w->resize(w->x(), (center2-w->h())/2, w->w(), w->h());
|
|
|
|
Fl_Type::allow_layout--;
|
2020-07-01 19:03:10 +03:00
|
|
|
w->redraw();
|
|
|
|
if (w->window()) w->window()->redraw();
|
|
|
|
}
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 15: // align bottom
|
|
|
|
bot = min;
|
|
|
|
for (o = Fl_Type::first; o; o = o->next)
|
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
|
|
|
if (w->y()+w->h()>bot)
|
|
|
|
bot = w->y()+w->h();
|
|
|
|
BREAK_ON_FIRST;
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
if (bot!=min)
|
|
|
|
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
|
2020-07-01 19:03:10 +03:00
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
|
|
|
if (!changed) {
|
|
|
|
changed = 1;
|
|
|
|
undo_checkpoint();
|
|
|
|
}
|
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
2023-11-01 00:00:29 +03:00
|
|
|
Fl_Type::allow_layout++;
|
|
|
|
w->resize( w->x(), bot-w->h(), w->w(), w->h());
|
|
|
|
Fl_Type::allow_layout--;
|
2020-07-01 19:03:10 +03:00
|
|
|
w->redraw();
|
|
|
|
if (w->window()) w->window()->redraw();
|
|
|
|
}
|
2002-05-01 21:35:30 +04:00
|
|
|
break;
|
2023-08-26 16:17:28 +03:00
|
|
|
//---- space evenly
|
2002-05-01 21:35:30 +04:00
|
|
|
case 20: // space evenly across
|
|
|
|
left = max; right = min; wdt = 0, n = 0;
|
|
|
|
for (o = Fl_Type::first; o; o = o->next)
|
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
|
|
|
if (w->x()<left)
|
|
|
|
left = w->x();
|
|
|
|
if (w->x()+w->w()>right)
|
|
|
|
right = w->x()+w->w();
|
|
|
|
wdt += w->w();
|
|
|
|
n++;
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
wdt = (right-left)-wdt;
|
|
|
|
n--;
|
|
|
|
if (n>0)
|
|
|
|
{
|
2023-01-20 17:34:14 +03:00
|
|
|
wdt = wdt/n*n; // make sure that all gaps are the same, possibly moving the rightmost widget
|
2002-05-01 21:35:30 +04:00
|
|
|
int cnt = 0, wsum = 0;
|
|
|
|
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
|
2020-07-01 19:03:10 +03:00
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
|
|
|
if (!changed) {
|
|
|
|
changed = 1;
|
|
|
|
undo_checkpoint();
|
|
|
|
}
|
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
2023-11-01 00:00:29 +03:00
|
|
|
Fl_Type::allow_layout++;
|
|
|
|
w->resize(left+wsum+wdt*cnt/n, w->y(), w->w(), w->h());
|
|
|
|
Fl_Type::allow_layout--;
|
2020-07-01 19:03:10 +03:00
|
|
|
w->redraw();
|
|
|
|
if (w->window()) w->window()->redraw();
|
|
|
|
cnt++;
|
|
|
|
wsum += w->w();
|
|
|
|
}
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 21: // space evenly down
|
|
|
|
top = max; bot = min; hgt = 0, n = 0;
|
|
|
|
for (o = Fl_Type::first; o; o = o->next)
|
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
|
|
|
if (w->y()<top)
|
|
|
|
top = w->y();
|
|
|
|
if (w->y()+w->h()>bot)
|
|
|
|
bot = w->y()+w->h();
|
|
|
|
hgt += w->h();
|
|
|
|
n++;
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
hgt = (bot-top)-hgt;
|
|
|
|
n--;
|
|
|
|
if (n>0)
|
|
|
|
{
|
2023-01-20 17:34:14 +03:00
|
|
|
hgt = hgt/n*n; // make sure that all gaps are the same, possibly moving the rightmost widget
|
2002-05-01 21:35:30 +04:00
|
|
|
int cnt = 0, hsum = 0;
|
|
|
|
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
|
2020-07-01 19:03:10 +03:00
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
|
|
|
if (!changed) {
|
|
|
|
changed = 1;
|
|
|
|
undo_checkpoint();
|
|
|
|
}
|
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
2023-11-01 00:00:29 +03:00
|
|
|
Fl_Type::allow_layout++;
|
|
|
|
w->resize(w->x(), top+hsum+hgt*cnt/n, w->w(), w->h());
|
|
|
|
Fl_Type::allow_layout--;
|
2020-07-01 19:03:10 +03:00
|
|
|
w->redraw();
|
|
|
|
if (w->window()) w->window()->redraw();
|
|
|
|
cnt++;
|
|
|
|
hsum += w->h();
|
|
|
|
}
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
//---- make same size
|
|
|
|
case 30: // same width
|
|
|
|
wdt = min;
|
|
|
|
for (o = Fl_Type::first; o; o = o->next)
|
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
|
|
|
if (w->w()>wdt)
|
|
|
|
wdt = w->w();
|
|
|
|
BREAK_ON_FIRST;
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
if (wdt!=min)
|
|
|
|
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
|
2020-07-01 19:03:10 +03:00
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
|
|
|
if (!changed) {
|
|
|
|
changed = 1;
|
|
|
|
undo_checkpoint();
|
|
|
|
}
|
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
2023-11-01 00:00:29 +03:00
|
|
|
Fl_Type::allow_layout++;
|
|
|
|
w->resize(w->x(), w->y(), wdt, w->h());
|
|
|
|
Fl_Type::allow_layout--;
|
2020-07-01 19:03:10 +03:00
|
|
|
w->redraw();
|
|
|
|
if (w->window()) w->window()->redraw();
|
|
|
|
}
|
2002-05-01 21:35:30 +04:00
|
|
|
break;
|
|
|
|
case 31: // same height
|
|
|
|
hgt = min;
|
|
|
|
for (o = Fl_Type::first; o; o = o->next)
|
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
|
|
|
if (w->h()>hgt)
|
|
|
|
hgt = w->h();
|
|
|
|
BREAK_ON_FIRST;
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
if (hgt!=min)
|
|
|
|
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
|
2020-07-01 19:03:10 +03:00
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
|
|
|
if (!changed) {
|
|
|
|
changed = 1;
|
|
|
|
undo_checkpoint();
|
|
|
|
}
|
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
2023-11-01 00:00:29 +03:00
|
|
|
Fl_Type::allow_layout++;
|
|
|
|
w->resize( w->x(), w->y(), w->w(), hgt);
|
|
|
|
Fl_Type::allow_layout--;
|
2020-07-01 19:03:10 +03:00
|
|
|
w->redraw();
|
|
|
|
if (w->window()) w->window()->redraw();
|
|
|
|
}
|
2002-05-01 21:35:30 +04:00
|
|
|
break;
|
|
|
|
case 32: // same size
|
|
|
|
hgt = min; wdt = min;
|
|
|
|
for (o = Fl_Type::first; o; o = o->next)
|
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
|
|
|
if (w->w()>wdt)
|
|
|
|
wdt = w->w();
|
|
|
|
if (w->h()>hgt)
|
|
|
|
hgt = w->h();
|
|
|
|
BREAK_ON_FIRST;
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
if (hgt!=min)
|
|
|
|
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
|
2020-07-01 19:03:10 +03:00
|
|
|
if (o->selected && o->is_widget())
|
|
|
|
{
|
|
|
|
if (!changed) {
|
|
|
|
changed = 1;
|
|
|
|
undo_checkpoint();
|
|
|
|
}
|
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
2023-11-01 00:00:29 +03:00
|
|
|
Fl_Type::allow_layout++;
|
|
|
|
w->resize( w->x(), w->y(), wdt, hgt);
|
|
|
|
Fl_Type::allow_layout--;
|
2020-07-01 19:03:10 +03:00
|
|
|
w->redraw();
|
|
|
|
if (w->window()) w->window()->redraw();
|
|
|
|
}
|
2002-05-01 21:35:30 +04:00
|
|
|
break;
|
|
|
|
//---- center in group
|
|
|
|
case 40: // center hor
|
|
|
|
for (o = Fl_Type::first; o; o = o->next)
|
|
|
|
if (o->selected && o->is_widget() && o->parent)
|
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
if (!changed) {
|
|
|
|
changed = 1;
|
|
|
|
undo_checkpoint();
|
|
|
|
}
|
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
|
|
|
Fl_Widget *p = ((Fl_Widget_Type *)o->parent)->o;
|
|
|
|
int center2;
|
2004-04-06 21:38:36 +04:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
if (w->window() == p) center2 = p->w();
|
|
|
|
else center2 = 2*p->x()+p->w();
|
2004-04-06 22:32:52 +04:00
|
|
|
|
2023-11-01 00:00:29 +03:00
|
|
|
Fl_Type::allow_layout++;
|
|
|
|
w->resize((center2-w->w())/2, w->y(), w->w(), w->h());
|
|
|
|
Fl_Type::allow_layout--;
|
2020-07-01 19:03:10 +03:00
|
|
|
w->redraw();
|
|
|
|
if (w->window()) w->window()->redraw();
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 41: // center vert
|
|
|
|
for (o = Fl_Type::first; o; o = o->next)
|
|
|
|
if (o->selected && o->is_widget() && o->parent)
|
|
|
|
{
|
2020-07-01 19:03:10 +03:00
|
|
|
if (!changed) {
|
|
|
|
changed = 1;
|
|
|
|
undo_checkpoint();
|
|
|
|
}
|
|
|
|
Fl_Widget *w = ((Fl_Widget_Type *)o)->o;
|
|
|
|
Fl_Widget *p = ((Fl_Widget_Type *)o->parent)->o;
|
|
|
|
int center2;
|
2004-04-06 21:38:36 +04:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
if (w->window() == p) center2 = p->h();
|
|
|
|
else center2 = 2*p->y()+p->h();
|
2004-04-06 22:32:52 +04:00
|
|
|
|
2023-11-01 00:00:29 +03:00
|
|
|
Fl_Type::allow_layout++;
|
|
|
|
w->resize(w->x(), (center2-w->h())/2, w->w(), w->h());
|
|
|
|
Fl_Type::allow_layout--;
|
2020-07-01 19:03:10 +03:00
|
|
|
set_modflag(1);
|
|
|
|
w->redraw();
|
|
|
|
if (w->window()) w->window()->redraw();
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-11-26 03:35:50 +03:00
|
|
|
if (changed)
|
|
|
|
set_modflag(1);
|
2002-05-01 21:35:30 +04:00
|
|
|
}
|