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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
|
/**
* 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/SFreeHand.h"
#include "swidget/SIcon.h"
#include "swidget/SIconFactory.h"
#include "stoolkit/SUtil.h"
#include "stoolkit/SEncoder.h"
#include "stoolkit/SIO.h"
#define SS_LEFT_MARGIN 2
static SString SD_NOT_FOUND("???");
SFreeHandListener::SFreeHandListener(void)
{
}
SFreeHandListener::~SFreeHandListener()
{
}
/**
* A drawing component that lets you make a small
* drawing with the mouse.
*/
SFreeHand::SFreeHand (void)
{
listener = 0;
/* top line */
titleLabel = new SLabel (translate ("Handwriting Input"));
titleLabel->setAlignment (SD_Center);
add (titleLabel);
strokesLabel = new SLabel (translate ("Strokes:"));
strokesLabel->setAlignment (SD_Right);
add (strokesLabel);
strokesCount = new SLabel ("0");
strokesCount->setAlignment (SD_Left);
add (strokesCount);
/* next line */
converters = new SListBox (translate ("Converters"));
converters->setListListener (this);
add (converters);
drawing = new SDrawing ();
drawing->setDrawingListener(this);
add (drawing);
/* next four button are one under the other. */
lookupButton = new SButton (translate ("Look-up"),
SIconFactory::getIcon("Yes"));
lookupButton->setButtonListener (this);
lookupButton->setAlignment (SD_Center);
add (lookupButton);
clearButton = new SButton (translate ("Clear"),
SIconFactory::getIcon("Cancel"));
clearButton->setButtonListener (this);
clearButton->setAlignment (SD_Center);
add (clearButton);
isDirected = true;
directedButton = new SButton (translate ("Directed"));
directedButton->setButtonListener (this);
directedButton->setAlignment (SD_Center);
directedButton->setIcon(SIconFactory::getIcon("CircleOn"));
add (directedButton);
candidates = new SListBox (translate ("Candidates"));
candidates->setListListener (this);
add (candidates);
forceLayout (SLayout (SDimension (1000, 1000)));
recalc ();
initConverters();
needConversion = false;
}
/**
* Find converters in datapath, with extension .hwd
*/
void
SFreeHand::initConverters()
{
SBinHashtable<int> mentioned;
SStringVector all;
SStringVector sp = SUniMap::getPath ();
for (unsigned int i=0; i<sp.size(); i++)
{
SDir d(sp[i]);
SStringVector l = d.list("*.hwd");
for (unsigned int j=0; j<l.size(); j++)
{
SString s = l[j];
s.truncate(s.size()-4);
if (mentioned.get(s) != 0) continue;
mentioned.put (s,1);
SString fn = d.getName(); fn.append ("/");
fn.append (s);
fn.append (".hwd");
SFile file (fn);
SFileImage im=file.getFileImage();
if (im.size() < 10 || (im.array())[0] != '#'
|| (im.array())[1] != 'H' || (im.array())[2] != 'W'
|| (im.array())[3] != 'D' || (im.array())[4] != ' '
|| ((im.array())[5] != '1' && (im.array())[5] != '2'))
{
fprintf (stderr,"Bad magic number: %*.*s\n", SSARGS(fn));
fprintf (stderr,"Expected: file that starts with '#HWD 1' or '#HWD 2'\n");
s.insert (0, SString("?"));
s.append (SString("?"));
all.append (s);
}
else
{
all.append (s);
}
}
}
if (all.size())
{
all.sort();
allConverters = all;
converters->setText (all);
setConverter (all[0]);
}
else
{
converters->setText (translate ("not found"));
converters->selectItem (0);
}
}
/**
* Deletes this drawing. Nothing to do.
*/
SFreeHand::~SFreeHand ()
{
}
/*
void
printl (const SLayout lo)
{
SLocation l = lo.getLocation();
SDimension d = lo.getDimension();
fprintf (stderr, "l= (%d,%d) d= (%u,%u) l2 =(%d,%d)\n",
l.x, l.y, d.width, d.height, l.x + (int) d.width, l.y + d.height);
}
*/
/**
* Recalculate constraints.
*/
void
SFreeHand::recalc ()
{
int gap = 2;
int bgap = 4; /* between buttons */
SDimension d = layout.getDimension ();
/* we should fit into this layout */
// From left to right...
SLocation t0 = SLocation (titleLabel->getPreferredSize());
SLocation t1 = SLocation (strokesLabel->getPreferredSize());
SLocation t2 = SLocation (strokesCount->getPreferredSize());
SLocation l0 = SLocation (converters->getPreferredSize());
SLocation m0 = SLocation (drawing->getPreferredSize());
//SLocation b0 = SLocation (insertButton->getPreferredSize());
SLocation b1 = SLocation (lookupButton->getPreferredSize());
SLocation b0 = b1; // removed insertButton
SLocation b2 = SLocation (directedButton->getPreferredSize());
SLocation b3 = SLocation (clearButton->getPreferredSize());
SLocation r0 = SLocation (candidates->getPreferredSize());
b2.x += 20; /* dont ask */
t2.x = t1.x;
/* find longest button */
int bX = 0;
if (b0.x > bX) bX = b0.x;
if (b1.x > bX) bX = b1.x;
if (b2.x > bX) bX = b2.x;
if (b3.x > bX) bX = b3.x;
if (l0.x < r0.x + bX + bgap * 2) l0.x = r0.x + bX + bgap * 2;
if (l0.y < b0.y + bgap + b1.y + bgap + b2.y)
{
l0.y = b0.y + bgap + b1.y + bgap + b2.y + bgap + b3.y;
}
/* assume drawing wants 140 */
if (l0.y < 140) l0.y = 140;
m0.x = 180; /* a bit more */
m0.y = l0.y;
r0.y = l0.y;
r0.x = r0.x + 20; /* just in case */
/* find minimum height */
int minY = 2 + t1.y + + gap + l0.y;
/* find minimum width, assume drawing wants 100. */
int minX = 2 + l0.x + gap + m0.x + bgap + bX + bgap + r0.x;
setMinimumSize (SDimension ((unsigned int)minX, (unsigned int) minY));
preferredSize = SDimension ((unsigned int)minX, (unsigned int) minY);
/* top */
int x = 1;
int y = 1;
titleLabel->setLayout (SLayout (
SLocation (x, y),
SLocation (x+t0.x, y + t0.y),
SLocation (0, 0),
SLocation (0, 0)
));
x += (t0.x + 20);
strokesLabel->setLayout (SLayout (
SLocation (x, y),
SLocation (x + t1.x, y + t0.y),
SLocation (0, 0),
SLocation (0, 0)
));
x += (t1.x + gap);
strokesCount->setLayout (SLayout (
SLocation (x, y),
SLocation (x + t2.x, y + t0.y),
SLocation (0, 0),
SLocation (0, 0)
));
/* new row. */
x = 1;
y += (t1.y + gap);
converters->setLayout (SLayout (
SLocation (x, y),
SLocation (x + l0.x, y + l0.y),
SLocation (0, 0),
SLocation (30, 100)
));
x += (l0.x + gap);
drawing->setLayout (SLayout (
SLocation (x, y),
SLocation (x + m0.x, y + m0.y),
SLocation (30, 0),
SLocation (70, 100)
));
/* set buttons later */
int butX = x + m0.x;
/* right */
//int minX = 2 + l0.x + gap + 100 + bgap + bX + bgap + r0.width;
x = minX - 1 - r0.x;
candidates->setLayout (SLayout (
SLocation (x, y),
SLocation (x + r0.x, y + r0.y),
SLocation (80, 0),
SLocation (100, 100)
));
int tbgap = r0.y - b0.y -bgap - b1.y -bgap -b2.y -bgap -b3.y;
if (tbgap < 0) tbgap = 0;
/* now buttons between butx .. x- bgap */
x = butX + bgap;
lookupButton->setLayout (SLayout (
SLocation (x, y),
SLocation (x + bX, y + b0.y),
SLocation (70, 0),
SLocation (80, 0)
));
y += (b0.y + bgap);
y += ((b1.y + bgap))/2;
/* what is this gap ? there was a button here before ) */
directedButton->setLayout (SLayout (
SLocation (x, y),
SLocation (x + bX, y + b2.y),
SLocation (70, 66),
SLocation (80, 66)
));
y += ((b1.y + bgap))/2 + tbgap;
y += (b2.y + bgap);
clearButton->setLayout (SLayout (
SLocation (x, y),
SLocation (x + bX, y + b3.y),
SLocation (70, 100),
SLocation (80, 100)
));
/* save current */
SLayout goodlayout = layout;
/* pretend we have this layout */
forceLayout (preferredSize);
/* accept old layout */
setLayout (goodlayout);
}
/**
* ButtonListener
*/
void
SFreeHand::buttonPressed (void* source, const SAccelerator* acc)
{
if (source == clearButton)
{
drawing->clear();
lookupText.clear();
SStringVector v;
candidates->setText (v);
}
else if (source == lookupButton)
{
convertOne();
}
if (source == directedButton)
{
isDirected = !isDirected;
const char * str = isDirected?"CircleOn" : "CircleOff";
directedButton->setIcon (SIconFactory::getIcon(str));
convertOne();
}
}
/**
* ItemListener
*/
void
SFreeHand::itemSelected (void* source, const SAccelerator* acc)
{
if (source == candidates->textList)
{
SString str = candidates->textList->getLastSelectedText();
lookupText.clear();
if (str == SD_NOT_FOUND) return;
lookupText = str;
if (lookupText.size()>0 && listener != 0)
{
listener->freeHandTextChanged (this, lookupText);
}
window->putClipUTF8 (lookupText);
}
else
{
SString str = converters->textList->getLastSelectedText();
converter = SHWConverter(str);
convertOne();
}
}
/**
* There can be only one drawing listener.
*/
void
SFreeHand::setFreeHandListener (SFreeHandListener* _listener)
{
listener = _listener;
}
const SString&
SFreeHand::getLookupText()
{
return lookupText;
}
void
SFreeHand::setFont (const SString& font, double fontSize)
{
candidates->textList->setFont (font, fontSize);
recalc ();
}
void
SFreeHand::setFontSize (double fontSize)
{
candidates->textList->setFontSize (fontSize);
recalc ();
}
void
SFreeHand::setButtonFont (const SString& font, double fontSize)
{
lookupButton->setFont (font, fontSize);
clearButton->setFont (font, fontSize);
directedButton->setFont (font, fontSize);
titleLabel->setFont (font, fontSize);
strokesLabel->setFont (font, fontSize);
strokesCount->setFont (font, fontSize);
converters->setFont (font, fontSize);
candidates->setFont (font, fontSize);
recalc ();
}
/**
* @param bg is the backgroud.
*/
void
SFreeHand::setBackground (const SColor& bg)
{
SPanel::setBackground (bg);
}
/**
* Set the background of the drawing itself.
* @param bg is the backgroud.
*/
void
SFreeHand::setDrawingBackground (const SColor& bg)
{
drawing->setTextBackground (bg);
}
void
SFreeHand::setSliderBackground (const SColor& bg)
{
candidates->setSliderBackground (bg);
converters->setSliderBackground (bg);
}
/**
* Set the foreground of the drawing itself.
* @param fg is the foreground
* @param fgrecent is the foreground of the last line, being drawn
*/
void
SFreeHand::setForeground (const SColor& fg)
{
lookupButton->setForeground (fg);
clearButton->setForeground (fg);
directedButton->setForeground (fg);
candidates->setLabelForeground (fg);
converters->setLabelForeground (fg);
titleLabel->setForeground (fg);
strokesLabel->setForeground (fg);
}
/**
* Set the foreground of the label
* @param fg is the foreground
* @param fgrecent is the foreground of the last line, being drawn
*/
void
SFreeHand::setLabelForeground (const SColor& fg)
{
strokesCount->setForeground (fg);
}
/**
* Set the foreground of the drawing itself.
* @param fg is the foreground
* @param fgrecent is the foreground of the last line, being drawn
*/
void
SFreeHand::setDrawingForeground (const SColor& _fg, const SColor& fgrecent)
{
drawing->setForeground (_fg, fgrecent);
}
/**
* Redraw the Component on a canvas
* @param canvas is where we redraw this.
*/
void
SFreeHand::redraw (SWindow *canvas, int x, int y, unsigned int width, unsigned int height)
{
SPanel::redraw (canvas, x, y, width, height);
}
void
SFreeHand::resize (const SDimension& _size)
{
SPanel::resize (_size);
}
void
SFreeHand::setConverter (const SString& name)
{
for (unsigned int i=0; i<allConverters.size(); i++)
{
if (allConverters[i] == name)
{
converter = SHWConverter(name);
drawing->clear();
lookupText.clear();
candidates->setText (SStringVector());
converters->selectItem (i);
return;
}
}
}
const SString&
SFreeHand::getConverter() const
{
return converter.getName();
}
bool
SFreeHand::isOK () const
{
return converter.isOK();
}
void
SFreeHand::clicked (void* source, int button)
{
if (button==2)
{
drawing->undo();
}
if (button==1)
{
drawing->clear();
lookupText.clear();
candidates->setText (SString());
return;
}
}
void
SFreeHand::strokeChanged(void* src, unsigned int newsize)
{
SString str;
str.print ((int)newsize);
strokesCount->setText (str);
candidates->setText (SStringVector());
needConversion = true;
}
void
SFreeHand::convertOne ()
{
needConversion = false;
SLineCurves set = drawing->getDrawing();
if (set.size()==0)
{
lookupText = "";
candidates->setText (SStringVector());
return;
}
SStringVector v = converter.convert (set, isDirected);
if (v.size()==0)
{
lookupText = "";
v.append (SD_NOT_FOUND);
candidates->setText (v);
return;
}
lookupText = v[0];
candidates->setText (v);
window->putClipUTF8 (v[0]);
}
|