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
|
/**
* Yudit Unicode Editor Source File
*
* GNU Copyright (C) 1997-2023 Gaspar Sinai <gaspar@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 "SPanel.h"
#include <swindow/SAwt.h>
#include <stdio.h>
SAwt* SPanel::awt=0;
int SPanel::count=0;
static int doubleBufferEnabled = true;
void
SPanel::setAllDoubleBuffered (bool yes)
{
doubleBufferEnabled = yes;
}
// default is true, this can turn it on/off
void
SPanel::setThisDoubleBuffered (bool yes)
{
window->setDoubleBuffer (yes);
}
SPanel::SPanel (void) : SComponent ()
{
modalParent = 0;
count++;
if (count == 1) {
awt = new SAwt();
}
window = awt->getWindow (this, "Yudit");
// For some reason the above is calling component on mac...
//comp->setWindowInterface (this);
window->setDoubleBuffer (doubleBufferEnabled);
size = SDimension (window->getWidth(), window->getHeight());
location = SLocation (window->getPositionX(), window->getPositionY());
resizeTimer = 0;
dropListener = 0;
hasParent = false;
layoutOK = true;
}
SPanel::~SPanel ()
{
count--;
if (count == 0) {
delete awt;
}
cleanup ();
delete window;
if (resizeTimer) delete resizeTimer;
resizeTimer = 0;
}
void
SPanel::cleanup ()
{
dropListener = 0;
unsigned int i;
for (i=0; i<children.size(); i++) {
delete children[i];
}
for (i=0; i<containers.size(); i++) {
delete containers[i];
}
}
/**
* This is SWindowListener implementation
*/
void
SPanel::redraw(SCanvas* w, int x, int y, unsigned int width ,unsigned int height)
{
for (unsigned int i=0; i<children.size(); i++)
{
children[i]->redraw(w, x, y, width, height);
}
}
/**
* SWindowListener
*/
void
SPanel::keyPressed (SWindow * w, SWindowListener::SKey key, const SString& s,
bool ctrl, bool shift, bool meta)
{
for (unsigned int i=0; i<children.size(); i++)
{
// TODO: Check if it contains child.
children[i]->keyPressed (w, key, s, ctrl, shift, meta);
}
}
void
SPanel::keyReleased (SWindow * w, SWindowListener::SKey key, const SString& s,
bool ctrl, bool shift, bool meta)
{
for (unsigned int i=0; i<children.size(); i++)
{
// TODO: Check if it contains child.
children[i]->keyReleased (w, key, s, ctrl, shift, meta);
}
}
void
SPanel::buttonPressed (SWindow * w, int button, int x, int y)
{
for (unsigned int i=0; i<children.size(); i++)
{
// TODO: Check if it contains child.
children[i]->buttonPressed (w, button, x, y);
}
}
void
SPanel::buttonReleased (SWindow * w, int button, int x, int y)
{
for (unsigned int i=0; i<children.size(); i++)
{
// TODO: Check if it contains child.
children[i]->buttonReleased (w, button, x, y);
}
}
void
SPanel::buttonDragged (SWindow * w, int button, int x, int y)
{
for (unsigned int i=0; i<children.size(); i++)
{
children[i]->buttonDragged (w, button, x, y);
}
}
/**
* notification that window got resized.
*/
void
SPanel::resized (SWindow* w, int x, int y,
unsigned int width, unsigned int height)
{
/* nothing to do - we did it already...*/
if (width == getSize().width && height == getSize().height)
{
SLocation lme = SLocation (window->getPositionX(), window->getPositionY());
SComponent::move (lme);
return;
}
/* you reach this point because window manager changed our size */
if (hasParent)
{
// to update window sizes we do this.
//_resized ();
bool needed = size.width != width|| size.height != height;
// Mystery. If there are many resize request in X11 queue,
// we might not get the latest honoured.
if (needed) {
//fprintf (stderr, "Forcing re-resize %u,%u.\n", size.width, size.height);
window->resize (size.width, size.height);
window->redraw (true, 0, 0, size.width, size.height);
}
return;
}
if (!window->isShown() || !window->isVisible ())
{
_resized ();
return;
}
// if we dont resize right away it will cause redrawing artefacts
// when window is resized vertically.
#ifdef USE_WINAPI
_resized ();
return;
#else
# ifdef USE_OSX
_resized ();
return;
# else
/*
* Resize after 100 ms.
*/
if (resizeTimer)
{
return;
}
resizeTimer = STimer::newTimer(100, this);
# endif
#endif
}
bool
SPanel::timeout (const SEventSource* s)
{
if (!resizeTimer) return false;
delete resizeTimer;
resizeTimer = 0;
//if (hasParent) return false;
_resized ();
return false;
}
/**
* Set the size to the current window size.
*/
void
SPanel::_resized ()
{
SDimension dme = SDimension (window->getWidth(), window->getHeight());
SLocation lme = SLocation (window->getPositionX(), window->getPositionY());
/* if this came from a timer this may be different */
SComponent::resize (dme);
SComponent::move (lme);
/*
fprintf (stderr, "SPanel%lx ::_resized %u,%u\n",
window->getWindowID(), dme.width, dme.height);
*/
//if (dme.width == 12 && dme.height == 12) {
// return;
//}
for (unsigned int i=0; i<containers.size(); i++)
{
SPanel* p = containers[i];
if (p->layout.isEmpty())
{
//fprintf (stderr, "Empty Layout!\n");
continue;
}
SLocation l = p->layout.getLocation (layout, dme);
SDimension d = p->layout.getDimension (layout, dme);
d = d.maximize (SDimension (15000, 15000));
d = d.minimize (SDimension (2, 2));
l = l.maximize (SLocation (15000, 15000));
l = l.minimize (SLocation (-15000, -15000));
p->move (l);
p->resize (d);
p->layoutOK = true;
}
layoutOK = true;
}
/**
* This is SWindowListener implementation
*/
bool
SPanel::windowClose (SWindow* w)
{
cleanup();
return true;
}
/**
* Add a new component.
* This component will be destroyed by the panel.
* @param comp is the component to add.
*/
void
SPanel::add (SComponent* comp)
{
children.append (comp);
comp->setWindowInterface (this);
SSlidable* slidable = comp->setSliderListener(this);
if (slidable)
{
slidables.append (slidable);
slidableComponents.append (comp);
}
}
/**
* Add a new component.
* This panel will be destroyed by the panel.
* @param panel is the component to add.
*/
void
SPanel::add (SPanel* panel)
{
panel->hasParent = true;
containers.append (panel);
SLayout oldLayout = layout;
layout += panel->layout;
SDimension minimumSize = layout.getDimension ();
if (layout != oldLayout)
{
//setMinimumSize (minimumSize);
}
SWindow *child = panel->window;
child->setParent (window, panel->getLocation().x, panel->getLocation().y);
SSlidable* slidable = panel->setSliderListener(this);
if (slidable)
{
slidables.append (slidable);
slidableComponents.append (panel);
}
panel->window->show();
layoutOK = false;
//panel->show();
}
/**
* should be called after all layout is set
*/
void
SPanel::setMinimumSize (const SDimension& minimumSize)
{
window->setMinimumSize (minimumSize.width, minimumSize.height);
}
/**
* This is the SWindowInterface
* @return this window
*/
SWindow*
SPanel::getComponentWindow ()
{
return window;
}
/**
* Resize the component. This should have immediate effect on size.
* @param d is the new size
*/
void
SPanel::resize(const SDimension& d)
{
bool needed = window->getWidth() != d.width||window->getHeight() != d.height;
if (needed)
{
/* this will generate an event if really needed. */
window->resize (d.width, d.height);
_resized ();
}
else if (d != size) /* never happens */
{
_resized ();
}
}
/**
* Resize the component. This should have immediate effect on size.
* @param l is the new location
*/
void
SPanel::move(const SLocation& l)
{
bool needed = window->getPositionX() != l.x || window->getPositionY() != l.y;
needed = true;
if (needed)
{
window->move (l.x, l.y);
_resized ();
}
else if (l != getLocation()) /* never happens */
{
_resized ();
}
}
void
SPanel::setBackground (const SColor& bg)
{
SComponent::setBackground(bg);
window->setBackground (bg);
unsigned int i;
for (i=0; i<children.size(); i++)
{
children[i]->setBackground (bg);
}
for (i=0; i<containers.size(); i++) {
containers[i]->setBackground (bg);
}
}
void
SPanel::lostKeyboardFocus (SWindow* w)
{
}
bool
SPanel::gainedKeyboardFocus (SWindow* w)
{
return false;
}
void
SPanel::lostClipSelection (SWindow* w)
{
}
void
SPanel::forceLayout (const SLayout& _layout)
{
layout = _layout;
if (layout == _layout) return;
layoutOK = false;
}
void
SPanel::setTitle (const SString& title)
{
window->setTitle(title);
}
void
SPanel::valueChanged (SSlidable* slidable, SSlideType type)
{
/* notify everybody except this one. */
for (unsigned int i=0; i<slidables.size(); i++)
{
if (slidables[i] == slidable) continue;
slidableComponents[i]->valueChanged (slidable, type);
}
}
/**
* Listener!
*/
bool
SPanel::drop (SWindow* w, const SString& mimetype, const SString& data)
{
if (dropListener) return dropListener->dropped (this, mimetype, data);
return false;
}
void
SPanel::setDropListener (SDropListener*ls, const SStringVector& mimes)
{
dropListener = ls;
window->setDroppable (mimes);
}
SDropListener::SDropListener(void)
{
}
SDropListener::~SDropListener()
{
}
/**
* Adjust the layout of all children, assuming their layout were
* calculated using original dimension as parent.
* This is the preferred way to set a new layout.
* Don't do anything with children layout unless this layout is set.
* @param original is the original layout.
*/
void
SPanel::setLayout (const SLayout& _layout)
{
/* nothing to do. */
if (layout.isEmpty ())
{
layout = _layout;
layoutOK = false;
return;
}
if (layout == _layout && isLayoutOK()) return;
/* new dimension */
/* adjust containers - set their new layout */
for (unsigned int i=0; i<containers.size(); i++)
{
SPanel* p = containers[i];
if (p->layout.isEmpty())
{
//fprintf (stderr, "Empty Layout!\n");
continue;
}
SLayout newlayout = p->layout;
newlayout.setLayout (layout, _layout);
p->setLayout (newlayout);
}
layout = _layout;
layoutOK = false;
}
void
SPanel::setModal (SPanel* _parent, bool decorated)
{
if (_parent)
{
window->setModal (_parent->window, decorated);
}
else
{
window->setModal (0, decorated);
}
modalParent = _parent;
}
/**
* Pop up the window
*/
void
SPanel::show()
{
window->show();
}
/**
* Pop down the window
*/
void
SPanel::hide()
{
window->hide();
}
void
SPanel::center ()
{
window->center ((modalParent==0)?0:modalParent->window);
}
void
SPanel::wait ()
{
window->wait();
}
bool
SPanel::isShown()
{
return window->isShown();
}
const SLayout&
SPanel::getLayout () const
{
return layout;
}
/**
* Traverse children and figure out if layout is ok
* A alyout is ok if the resize already worked and there is no
* layout change.
* If one of them is false return false.
* Skip children with no layout.
*/
bool
SPanel::isLayoutOK() const
{
bool isOK = layoutOK;
if (!isOK) return false;
for (unsigned int i=0; i<containers.size(); i++)
{
SPanel* p = containers[i];
if (p->layout.isEmpty()) continue;
if (!p->isLayoutOK())
{
return false;
}
}
return true;
}
void
SPanel::enterWindow (SWindow* w)
{
}
void
SPanel::leaveWindow (SWindow* w)
{
}
|