00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <stdlib.h>
00016 #include "xyleg.h"
00017
00018 const char* xy_id_xyleg_cpp="$Id: xyleg.cpp,v 1.20 2000/06/13 13:46:12 camilo Exp $";
00019
00020 #define AREA_MM 15 // dimensão da região para o ícone da legenda
00021 #define SPACE_MM 4 // espaço para a legenda não colar em seu boundingBox
00022 #define MARG 2 // espessura da moldura da legenda
00023
00024 XYLegend::XYLegend (XYCoordinate x,
00025 XYCoordinate y,
00026 int nlines,
00027 int ncolumns,
00028 xybool box,
00029 xybool visible)
00030 : XYObject(x, y, visible),
00031 _nLines(nlines),
00032 _nColumns(ncolumns),
00033 _box(box),
00034 _color(XY_WHITE),
00035 _bColor(XY_BLACK)
00036 {
00037 }
00038
00039 XYLegend::~XYLegend (void)
00040 {
00041 }
00042
00043 void XYLegend::mask (const XYList<XYMask>* mask)
00044 {
00045 _masks = mask;
00046 }
00047
00048 const XYList<XYMask>* XYLegend::mask (void) const
00049 {
00050 return _masks;
00051 }
00052
00053 void XYLegend::backgroundColor (long color)
00054 {
00055 _color = color;
00056 }
00057
00058 long XYLegend::backgroundColor (void) const
00059 {
00060 return _color;
00061 }
00062
00063 void XYLegend::frameColor (long color)
00064 {
00065 _bColor = color;
00066 }
00067
00068 long XYLegend::frameColor (void) const
00069 {
00070 return _bColor;
00071 }
00072
00073 void XYLegend::lines (int nl)
00074 {
00075 _nLines = nl;
00076 }
00077
00078 int XYLegend::lines (void) const
00079 {
00080 return _nLines;
00081 }
00082
00083 void XYLegend::columns (int nc)
00084 {
00085 _nColumns = nc;
00086 }
00087
00088 int XYLegend::columns (void) const
00089 {
00090 return _nColumns;
00091 }
00092
00093 void XYLegend::box (xybool b)
00094 {
00095 _box = b;
00096 }
00097
00098 xybool XYLegend::box (void) const
00099 {
00100 return _box;
00101 }
00102
00103 xybool XYLegend::pick (int px, int py)
00104 {
00105 if (visible() == xyfalse)
00106 return xyfalse;
00107
00108
00109 int x1, y1, x2, y2;
00110 boundingBox (x1, y1, x2, y2);
00111
00112 return mtPointInRect (px, py, x1, y1, x2, y2);
00113 }
00114
00115 XYMask* XYLegend::pickItem (int px, int py)
00116 {
00117 if (visible() == xyfalse)
00118 return NULL;
00119
00120 XYListIterator<XYMask> im(_masks);
00121 XYMask* cont;
00122
00123 int xmn, ymn, xmx, ymx;
00124
00125 while (!im.End())
00126 {
00127 cont = im.Next();
00128 cont -> getItemViewport(xmn, xmx, ymn, ymx);
00129
00130 if (mtPointInRect(px, py, xmn, ymn, xmx, ymx))
00131 return cont;
00132 }
00133
00134 return NULL;
00135 }
00136
00137 xybool XYLegend::fence (int x2, int y2, int x3, int y3)
00138 {
00139 if (visible() == xyfalse)
00140 return xyfalse;
00141
00142
00143 int x0, y0, x1, y1;
00144 boundingBox (x0, y0, x1, y1);
00145
00146 return mtInclude (x0, y0, x1, y1, x2, y2, x3, y3);
00147 }
00148
00149 void XYLegend::clear(void) const
00150 {
00151
00152 int xmn, ymn, xmx, ymx;
00153 getViewport(xmn, xmx, ymn, ymx);
00154
00155
00156 cdForeground(backgroundColor());
00157 cdInteriorStyle(CD_SOLID);
00158
00159 cdBox(xmn, xmx, ymn, ymx);
00160 }
00161
00162 void XYLegend::clear(int xmin, int ymin, int xmax, int ymax) const
00163 {
00164
00165 cdForeground(backgroundColor());
00166 cdInteriorStyle(CD_SOLID);
00167
00168 cdBox(xmin, xmax, ymin, ymax);
00169 }
00170
00171 void XYLegend::draw (void)
00172 {
00173 draw(_xmin, _ymin, _xmax, _ymax);
00174 }
00175
00176 void XYLegend::draw (int , int , int , int ) const
00177 {
00178 if (visible() == xyfalse)
00179 return;
00180
00181
00182 int txmin, tymin, txmax, tymax;
00183 boundingBox(txmin, tymin, txmax, tymax);
00184
00185
00186 if ((txmin == txmax) && (tymin == tymax))
00187 return;
00188
00189 int spx;
00190 double smx;
00191 cdGetCanvasSize( &spx, NULL, &smx, NULL );
00192 double resx = spx / smx;
00193 int AREA = (int)(AREA_MM * resx);
00194 int SPACE = (int)(SPACE_MM * resx);
00195
00196 if (_box)
00197 {
00198
00199 cdForeground(frameColor());
00200 cdInteriorStyle(CD_SOLID);
00201
00202 cdBox(txmin, txmax, tymin, tymax);
00203 }
00204
00205
00206 clear(txmin + MARG, tymin + MARG, txmax - MARG, tymax - MARG);
00207
00208
00209 int dx = (int) (((txmax - txmin) - (2 * MARG)) / _nColumns);
00210 int dy = (int) (((tymax - tymin) - (2 * MARG)) / _nLines);
00211
00212 int x = txmin + MARG + SPACE;
00213 int y = tymax - MARG - dy;
00214
00215 XYListIterator<XYMask> im(_masks);
00216
00217 XYMask* cont = 0;
00218 XYText* name;
00219
00220 int bbx1, bby1, bbx2, bby2;
00221
00222
00223
00224 for (int i = 1; i <= _nLines; i++)
00225 {
00226 for (int j = 1; j <= _nColumns; j++)
00227 {
00228 if (!im.End())
00229 {
00230 name = NULL;
00231
00232
00233 while ((name == NULL) && (!im.End()))
00234 {
00235 cont = im.Next();
00236 name = cont -> name();
00237 }
00238
00239 if (name != NULL)
00240 {
00241
00242 name -> align (XYText::southWest);
00243
00244
00245 name -> position (x + AREA + SPACE, y);
00246
00247
00248 name -> boundingBox (bbx1, bby1, bbx2, bby2);
00249
00250
00251 name -> draw(x, y, bbx2, bby2);
00252
00253 cont -> setItemViewport(x, bbx2, y, bby2);
00254
00255
00256
00257 cont -> drawIcon(x, y + SPACE, x + AREA, bby2 - SPACE);
00258
00259
00260 x += dx;
00261 }
00262 }
00263 }
00264
00265
00266 x = txmin + MARG + SPACE;
00267
00268 y -= dy;
00269 }
00270 }
00271
00272 void XYLegend::boundingBox (int &bxmin, int &bymin, int &bxmax, int &bymax)
00273 const
00274 {
00275 if (visible() == xyfalse)
00276 return;
00277
00278
00279 int xd, yd;
00280 position (&xd, &yd);
00281
00282
00283 bxmin = xd;
00284 bymin = yd;
00285
00286
00287 bxmax = bxmin;
00288 bymax = bymin;
00289
00290 XYListIterator<XYMask> im(_masks);
00291
00292 XYMask* cont;
00293 XYText* name = NULL;
00294
00295
00296 while ((name == NULL) && (!im.End()))
00297 {
00298 cont = im.Next();
00299 name = cont -> name();
00300 }
00301
00302
00303 if (name == NULL)
00304 return;
00305
00306 im.Reset();
00307
00308
00309 int x1, x2, y1, y2;
00310 getViewport(x1, x2, y1, y2);
00311
00312
00313 name = NULL;
00314
00315 int bbx1, bbx2, bby1, bby2;
00316
00317
00318 while (!im.End())
00319 {
00320 cont = im.Next();
00321 name = cont -> name();
00322
00323 if (name != NULL)
00324 {
00325
00326 name -> align (XYText::southWest);
00327
00328
00329 name -> setViewport(x1, x2, y1, y2);
00330
00331
00332 name -> position (xd, yd);
00333
00334
00335 name -> boundingBox (bbx1, bby1, bbx2, bby2);
00336
00337
00338 if (bxmax < bbx2)
00339 bxmax = bbx2;
00340
00341
00342 if (bymax < bby2)
00343 bymax = bby2;
00344 }
00345 }
00346
00347
00348
00349 int spx;
00350 double smx;
00351 cdGetCanvasSize( &spx, NULL, &smx, NULL );
00352 double resx = spx / smx;
00353 int AREA = (int)( AREA_MM * resx );
00354 int SPACE = (int)( SPACE_MM * resx );
00355 bxmax += AREA + 3 * SPACE;
00356
00357
00358
00359
00360 bxmax += (_nColumns - 1) * (bxmax - bxmin);
00361
00362
00363
00364
00365 bymax += (_nLines - 1) * (bymax - bymin);
00366
00367
00368 bxmax += 2 * MARG;
00369 bymax += 2 * MARG;
00370 }
00371