File: SLayout.cpp

package info (click to toggle)
yudit 2.5.4-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,528 kB
  • ctags: 8,403
  • sloc: cpp: 59,394; ansic: 2,585; perl: 2,398; makefile: 864; sh: 321
file content (207 lines) | stat: -rw-r--r-- 5,249 bytes parent folder | download
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
/** 
 *  Yudit Unicode Editor Source File
 *
 *  GNU Copyright (C) 2002  Gaspar Sinai <gsinai@yudit.org>  
 *  GNU Copyright (C) 2001  Gaspar Sinai <gsinai@yudit.org>  
 *  GNU Copyright (C) 2000  Gaspar Sinai <gsinai@yudit.org>  
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License, version 2,
 *  dated June 1991. See file COPYYING for details.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include "swidget/SLayout.h"
#include <stdio.h>

SLayout::SLayout (void) : lc(0,0), rc(0,0), dlc(0,0), drc(0,0)
{
}

/**
 * The layout looks like this:
 * lc-------------+
 * |              |
 * |              |
 * +-------------rc
 * When resize happens the lc is moved by dlc *(lc-newlc)
 * so these are deltas. The unit for deltas are 1000, so resizing
 * by unit 1 is achieved by 100.
 * Example a component that has a minimum size of 100 and grows 
 * twice when resized:
 * lc.x lc.y  rc.y  rc.y  dlc.x dlc.y  drc.x drc.y
 * 0,   0,    10,   10     0,   0      100,  100
 * it will also move down if:
 * 0,   0,    10,   10     0,   100    100,  100
 * it will also move right if:
 * 0,   0,    10,   10     100,  0     100,  100
 */
SLayout::SLayout (const SLocation& _lc, const SLocation _rc,
       const  SLocation& _dlc, const SLocation _drc) : 
  lc (_lc), rc (_rc), dlc(_dlc), drc(_drc)
{
}

SLayout::SLayout (const SLocation& _lc, const SLocation _rc) 
   : lc (_lc), rc (_rc)
{
  dlc = SLocation (100, 100);
  drc = SLocation (100, 100);
}

SLayout::SLayout (const SDimension& _rc) : rc (_rc)
{
  lc = SLocation (0, 0);
  dlc = SLocation (100, 100);
  drc = SLocation (100, 100);
}

/**
 * copy the layout.
 */
SLayout::SLayout (const SLayout& l)
{
  lc = l.lc;
  rc = l.rc;
  dlc = l.dlc;
  drc = l.drc;
}
SLayout
SLayout::operator = (const SLayout& l)
{
  lc = l.lc;
  rc = l.rc;
  dlc = l.dlc;
  drc = l.drc;
  return *this;
}

SLayout::~SLayout ()
{
}

/**
 * Add layout to this to make a bigger layout.
 */
SLayout
SLayout::operator += (const SLayout l)
{
  if (l.rc.x > rc.x) rc.x = l.rc.x;
  if (l.rc.y > rc.y) rc.y = l.rc.y;
  return *this;
}

bool
SLayout::isEmpty() const
{
  return (lc == rc && rc == drc && drc == dlc && dlc == SLocation(0,0));
}

/**
 * This is in fact the minimum size.
 */
SDimension
SLayout::getDimension () const
{
  SLocation delta = rc - lc;
  if (delta.x < 0) delta.x = 2;
  if (delta.y < 0) delta.y = 2;
  return SDimension (delta.x, delta.y);
}

/**
 * This is in fact the minimum size.
 */
SLocation
SLayout::getLocation () const
{
  SLocation min = lc;
  return SLocation (min.minmerge(rc));
}

/**
 * Suppose in is the parent layout. we are resized.
 * @return the new location of this child component.
 */ 
SLocation 
SLayout::getLocation (const SLayout& parent, const SDimension& d) const
{
  SLocation l(d.width, d.height);
  SLocation delta = l - (parent.rc-parent.lc);
//fprintf (stderr, "dim=%u,%u delta=%d,%d parent=%d,%d\n", 
 //    d.width, d.height,
  //    delta.x, delta.y, parent.rc.x, parent.rc.y);
  SLocation lnew (lc.x + (delta.x * dlc.x) / 100, 
      lc.y + (delta.y * dlc.y) / 100);
/*
 NEW
  if (lnew.x < lc.x) lnew.x = lc.x;
  if (lnew.y < lc.y) lnew.y = lc.y;
*/
  return SLocation (lnew);
}

void SLayout::print()
{
  fprintf (stderr, "Layout lc=%d %d / %d%% %d%%\n", lc.x, lc.y, dlc.x, dlc.y);
  fprintf (stderr, "Layout rc=%d %d / %d%% %d%%\n", rc.x, rc.y, drc.x, drc.y);
}

/**
 * Suppose in is the parent layout. we are resized.
 * @return the new dimension of this child component.
 */ 
SDimension 
SLayout::getDimension (const SLayout& parent, const SDimension& d) const
{
  SLocation l(d.width, d.height);
  SLocation delta = l - (parent.rc - parent.lc);
  SLocation lnew (rc.x + (delta.x * drc.x) / 100, 
     rc.y + (delta.y * drc.y) / 100);
/*
 NEW
  if (lnew.x <= rc.x) lnew.x = rc.x +1;
  if (lnew.y <= rc.y) lnew.y = rc.y +1;
*/
  SLocation left = getLocation (parent, d);
  if (lnew.x <= left.x) lnew.x = left.x+1;
  if (lnew.y <= left.y) lnew.y = left.y+1;
  return SDimension (lnew.x - left.x, lnew.y - left.y);
}
bool
SLayout::operator == (const SLayout l) const
{
  return (lc == l.lc && rc == l.rc && dlc == l.dlc && drc == l.drc);
}

bool
SLayout::operator != (const SLayout l) const
{
  return (lc != l.lc || rc != l.rc || dlc != l.dlc || drc != l.drc);
}

/**
 * Accept the layout. This layout has to change.
 * becomes this size.
 * We should not call this with an empty layout.
 * @param old is the layout this layout was attached to.
 * @param nl is the layout this layout will be attached to.
 */
void
SLayout::setLayout (const SLayout& old, const SLayout& nl)
{
  if (old.isEmpty() || nl.isEmpty()) return;
  SDimension d = getDimension (old, nl.getDimension());
  SLocation l = getLocation (old, nl.getDimension());
  lc = l;
  rc = l + SLocation (d);
}