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 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
|
/********************************************************************************
* *
* P o p u p W i n d o w O b j e c t *
* *
*********************************************************************************
* Copyright (C) 1998,2022 by Jeroen van der Zijp. All Rights Reserved. *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This library 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/> *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "fxkeys.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXMutex.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXSize.h"
#include "FXPoint.h"
#include "FXRectangle.h"
#include "FXStringDictionary.h"
#include "FXSettings.h"
#include "FXRegistry.h"
#include "FXEvent.h"
#include "FXWindow.h"
#include "FXDCWindow.h"
#include "FXApp.h"
#include "FXPopup.h"
#include "fxpriv.h"
/*
Notes:
- FXPopup now supports stretchable children; this is useful when popups
are displayed & forced to other size than default, e.g. for a ComboBox!
- LAYOUT_FIX_xxx takes precedence over PACK_UNIFORM_xxx!!
- Perhaps the grab owner could be equal to the owner?
- Perhaps popup should resize when recalc() has been called!
- Need to implement Mikael Aronssen's suggestion to make it easier to
run FXPopup in a modal loop. This shouls also return a code, just
like FXDialogBox does. Thus it becomes very straighforward to run
a popup menu just to get a single choice value back.
- Do not assume root window is at (0,0); multi-monitor machines may
have secondary monitor anywhere relative to primary display.
- If any child is resized due to GUI update, and FXPopup is in
POPUP_SHRINKWRAP mode, ID_LAYOUT causes resize() of the popup it
self as well as layout() of its interior. For normal shell windows
only layout() is called.
*/
// Frame styles
#define FRAME_MASK (FRAME_SUNKEN|FRAME_RAISED|FRAME_THICK)
using namespace FX;
/*******************************************************************************/
namespace FX {
// Map
FXDEFMAP(FXPopup) FXPopupMap[]={
FXMAPFUNC(SEL_PAINT,0,FXPopup::onPaint),
FXMAPFUNC(SEL_ENTER,0,FXPopup::onEnter),
FXMAPFUNC(SEL_LEAVE,0,FXPopup::onLeave),
FXMAPFUNC(SEL_MOTION,0,FXPopup::onMotion),
FXMAPFUNC(SEL_MAP,0,FXPopup::onMap),
FXMAPFUNC(SEL_FOCUS_NEXT,0,FXPopup::onDefault),
FXMAPFUNC(SEL_FOCUS_PREV,0,FXPopup::onDefault),
FXMAPFUNC(SEL_FOCUS_UP,0,FXPopup::onFocusUp),
FXMAPFUNC(SEL_FOCUS_DOWN,0,FXPopup::onFocusDown),
FXMAPFUNC(SEL_FOCUS_LEFT,0,FXPopup::onFocusLeft),
FXMAPFUNC(SEL_FOCUS_RIGHT,0,FXPopup::onFocusRight),
FXMAPFUNC(SEL_LEFTBUTTONPRESS,0,FXPopup::onButtonPress),
FXMAPFUNC(SEL_LEFTBUTTONRELEASE,0,FXPopup::onButtonRelease),
FXMAPFUNC(SEL_MIDDLEBUTTONPRESS,0,FXPopup::onButtonPress),
FXMAPFUNC(SEL_MIDDLEBUTTONRELEASE,0,FXPopup::onButtonRelease),
FXMAPFUNC(SEL_RIGHTBUTTONPRESS,0,FXPopup::onButtonPress),
FXMAPFUNC(SEL_RIGHTBUTTONRELEASE,0,FXPopup::onButtonRelease),
FXMAPFUNC(SEL_KEYPRESS,0,FXPopup::onKeyPress),
FXMAPFUNC(SEL_KEYRELEASE,0,FXPopup::onKeyRelease),
FXMAPFUNC(SEL_UNGRABBED,0,FXPopup::onUngrabbed),
FXMAPFUNC(SEL_CHORE,FXPopup::ID_LAYOUT,FXPopup::onLayout),
FXMAPFUNC(SEL_COMMAND,FXWindow::ID_UNPOST,FXPopup::onCmdUnpost),
FXMAPFUNCS(SEL_COMMAND,FXPopup::ID_CHOICE,FXPopup::ID_CHOICE+999,FXPopup::onCmdChoice),
};
// Object implementation
FXIMPLEMENT(FXPopup,FXShell,FXPopupMap,ARRAYNUMBER(FXPopupMap))
// Deserialization
FXPopup::FXPopup():prevActive(nullptr),nextActive(nullptr){
}
// Transient window used for popups
FXPopup::FXPopup(FXWindow* own,FXuint opts,FXint x,FXint y,FXint w,FXint h):FXShell(own,opts,x,y,w,h),prevActive(nullptr),nextActive(nullptr){
defaultCursor=getApp()->getDefaultCursor(DEF_RARROW_CURSOR);
dragCursor=getApp()->getDefaultCursor(DEF_RARROW_CURSOR);
flags|=FLAG_ENABLED;
grabowner=nullptr;
baseColor=getApp()->getBaseColor();
hiliteColor=getApp()->getHiliteColor();
shadowColor=getApp()->getShadowColor();
borderColor=getApp()->getBorderColor();
border=(options&FRAME_THICK)?2:(options&(FRAME_SUNKEN|FRAME_RAISED))?1:0;
}
// Popups do override-redirect
FXbool FXPopup::doesOverrideRedirect() const {
return true;
}
// Popups do save-unders
FXbool FXPopup::doesSaveUnder() const {
return true;
}
#ifdef WIN32
const void* FXPopup::GetClass() const { return TEXT("FXPopup"); }
#endif
// Setting focus causes keyboard grab, because all keyboard
// events need to be reported to the active popup.
void FXPopup::setFocus(){
FXShell::setFocus();
//grabKeyboard();
}
// Killing the focus should revert the keyboard grab to the
// previously active popup, or release the keyboard grab
void FXPopup::killFocus(){
FXShell::killFocus();
//if(prevActive){
// prevActive->setFocus();
// }
//else{
// ungrabKeyboard();
// }
}
// Get owner; if it has none, it's owned by itself
FXWindow* FXPopup::getGrabOwner(){
return grabowner ? grabowner : this;
}
// Get width
FXint FXPopup::getDefaultWidth(){
FXWindow* child;
FXint w,wmax,wcum;
FXuint hints;
wmax=wcum=0;
for(child=getFirst(); child; child=child->getNext()){
if(child->shown()){
hints=child->getLayoutHints();
if(hints&LAYOUT_FIX_WIDTH) w=child->getWidth();
else w=child->getDefaultWidth();
if(wmax<w) wmax=w;
}
}
for(child=getFirst(); child; child=child->getNext()){
if(child->shown()){
hints=child->getLayoutHints();
if(hints&LAYOUT_FIX_WIDTH) w=child->getWidth();
else if(options&PACK_UNIFORM_WIDTH) w=wmax;
else w=child->getDefaultWidth();
wcum+=w;
}
}
if(!(options&POPUP_HORIZONTAL)) wcum=wmax;
return wcum+(border<<1);
}
// Get height
FXint FXPopup::getDefaultHeight(){
FXWindow* child;
FXint h,hmax,hcum;
FXuint hints;
hmax=hcum=0;
for(child=getFirst(); child; child=child->getNext()){
if(child->shown()){
hints=child->getLayoutHints();
if(hints&LAYOUT_FIX_HEIGHT) h=child->getHeight();
else h=child->getDefaultHeight();
if(hmax<h) hmax=h;
}
}
for(child=getFirst(); child; child=child->getNext()){
if(child->shown()){
hints=child->getLayoutHints();
if(hints&LAYOUT_FIX_HEIGHT) h=child->getHeight();
else if(options&PACK_UNIFORM_HEIGHT) h=hmax;
else h=child->getDefaultHeight();
hcum+=h;
}
}
if(options&POPUP_HORIZONTAL) hcum=hmax;
return hcum+(border<<1);
}
// Recalculate layout
void FXPopup::layout(){
FXWindow *child;
FXint w,h,x,y,remain,t;
FXuint hints;
FXint sumexpand=0;
FXint numexpand=0;
FXint mw=0;
FXint mh=0;
FXint e=0;
// Horizontal
if(options&POPUP_HORIZONTAL){
// Get maximum size if uniform packed
if(options&PACK_UNIFORM_WIDTH) mw=maxChildWidth();
// Space available
remain=width-(border<<1);
// Find number of paddable children and total space remaining
for(child=getFirst(); child; child=child->getNext()){
if(child->shown()){
hints=child->getLayoutHints();
if(hints&LAYOUT_FIX_WIDTH) w=child->getWidth();
else if(options&PACK_UNIFORM_WIDTH) w=mw;
else w=child->getDefaultWidth();
FXASSERT(w>=0);
if((hints&LAYOUT_FILL_X) && !(hints&LAYOUT_FIX_WIDTH)){
sumexpand+=w;
numexpand++;
}
else{
remain-=w;
}
}
}
// Do the layout
for(child=getFirst(),x=border; child; child=child->getNext()){
if(child->shown()){
hints=child->getLayoutHints();
if(hints&LAYOUT_FIX_WIDTH) w=child->getWidth();
else if(options&PACK_UNIFORM_WIDTH) w=mw;
else w=child->getDefaultWidth();
if((hints&LAYOUT_FILL_X) && !(hints&LAYOUT_FIX_WIDTH)){
if(sumexpand>0){
t=w*remain;
FXASSERT(sumexpand>0);
w=t/sumexpand;
e+=t%sumexpand;
if(e>=sumexpand){w++;e-=sumexpand;}
}
else{
FXASSERT(numexpand>0);
w=remain/numexpand;
e+=remain%numexpand;
if(e>=numexpand){w++;e-=numexpand;}
}
}
child->position(x,border,w,height-(border<<1));
x+=w;
}
}
}
// Vertical
else{
// Get maximum size if uniform packed
if(options&PACK_UNIFORM_HEIGHT) mh=maxChildHeight();
// Space available
remain=height-(border<<1);
// Find number of paddable children and total space remaining
for(child=getFirst(); child; child=child->getNext()){
if(child->shown()){
hints=child->getLayoutHints();
if(hints&LAYOUT_FIX_HEIGHT) h=child->getHeight();
else if(options&PACK_UNIFORM_HEIGHT) h=mh;
else h=child->getDefaultHeight();
FXASSERT(h>=0);
if((hints&LAYOUT_FILL_Y) && !(hints&LAYOUT_FIX_HEIGHT)){
sumexpand+=h;
numexpand++;
}
else{
remain-=h;
}
}
}
// Do the layout
for(child=getFirst(),y=border; child; child=child->getNext()){
if(child->shown()){
hints=child->getLayoutHints();
if(hints&LAYOUT_FIX_HEIGHT) h=child->getHeight();
else if(options&PACK_UNIFORM_HEIGHT) h=mh;
else h=child->getDefaultHeight();
if((hints&LAYOUT_FILL_Y) && !(hints&LAYOUT_FIX_HEIGHT)){
if(sumexpand>0){
t=h*remain;
FXASSERT(sumexpand>0);
h=t/sumexpand;
e+=t%sumexpand;
if(e>=sumexpand){h++;e-=sumexpand;}
}
else{
FXASSERT(numexpand>0);
h=remain/numexpand;
e+=remain%numexpand;
if(e>=numexpand){h++;e-=numexpand;}
}
}
child->position(border,y,width-(border<<1),h);
y+=h;
}
}
}
flags&=~FLAG_DIRTY;
}
void FXPopup::drawBorderRectangle(FXDCWindow& dc,FXint x,FXint y,FXint w,FXint h){
dc.setForeground(borderColor);
dc.drawRectangle(x,y,w-1,h-1);
}
void FXPopup::drawRaisedRectangle(FXDCWindow& dc,FXint x,FXint y,FXint w,FXint h){
dc.setForeground(shadowColor);
dc.fillRectangle(x,y+h-1,w,1);
dc.fillRectangle(x+w-1,y,1,h);
dc.setForeground(hiliteColor);
dc.fillRectangle(x,y,w,1);
dc.fillRectangle(x,y,1,h);
}
void FXPopup::drawSunkenRectangle(FXDCWindow& dc,FXint x,FXint y,FXint w,FXint h){
dc.setForeground(shadowColor);
dc.fillRectangle(x,y,w,1);
dc.fillRectangle(x,y,1,h);
dc.setForeground(hiliteColor);
dc.fillRectangle(x,y+h-1,w,1);
dc.fillRectangle(x+w-1,y,1,h);
}
void FXPopup::drawRidgeRectangle(FXDCWindow& dc,FXint x,FXint y,FXint w,FXint h){
dc.setForeground(hiliteColor);
dc.fillRectangle(x,y,w,1);
dc.fillRectangle(x,y,1,h);
dc.fillRectangle(x+1,y+h-2,w-2,1);
dc.fillRectangle(x+w-2,y+1,1,h-2);
dc.setForeground(shadowColor);
dc.fillRectangle(x+1,y+1,w-3,1);
dc.fillRectangle(x+1,y+1,1,h-3);
dc.fillRectangle(x,y+h-1,w,1);
dc.fillRectangle(x+w-1,y,1,h);
}
void FXPopup::drawGrooveRectangle(FXDCWindow& dc,FXint x,FXint y,FXint w,FXint h){
dc.setForeground(shadowColor);
dc.fillRectangle(x,y,w,1);
dc.fillRectangle(x,y,1,h);
dc.fillRectangle(x+1,y+h-2,w-2,1);
dc.fillRectangle(x+w-2,y+1,1,h-2);
dc.setForeground(hiliteColor);
dc.fillRectangle(x+1,y+1,w-2,1);
dc.fillRectangle(x+1,y+1,1,h-2);
dc.fillRectangle(x+1,y+h-1,w,1);
dc.fillRectangle(x+w-1,y+1,1,h);
}
void FXPopup::drawDoubleRaisedRectangle(FXDCWindow& dc,FXint x,FXint y,FXint w,FXint h){
dc.setForeground(baseColor);
dc.fillRectangle(x,y,w-1,1);
dc.fillRectangle(x,y,1,h-1);
dc.setForeground(hiliteColor);
dc.fillRectangle(x+1,y+1,w-2,1);
dc.fillRectangle(x+1,y+1,1,h-2);
dc.setForeground(shadowColor);
dc.fillRectangle(x+1,y+h-2,w-2,1);
dc.fillRectangle(x+w-2,y+1,1,h-1);
dc.setForeground(borderColor);
dc.fillRectangle(x,y+h-1,w,1);
dc.fillRectangle(x+w-1,y,1,h);
}
void FXPopup::drawDoubleSunkenRectangle(FXDCWindow& dc,FXint x,FXint y,FXint w,FXint h){
dc.setForeground(shadowColor);
dc.fillRectangle(x,y,w-1,1);
dc.fillRectangle(x,y,1,h-1);
dc.setForeground(borderColor);
dc.fillRectangle(x+1,y+1,w-3,1);
dc.fillRectangle(x+1,y+1,1,h-3);
dc.setForeground(hiliteColor);
dc.fillRectangle(x,y+h-1,w,1);
dc.fillRectangle(x+w-1,y,1,h);
dc.setForeground(baseColor);
dc.fillRectangle(x+1,y+h-2,w-2,1);
dc.fillRectangle(x+w-2,y+1,1,h-2);
}
// Draw border
void FXPopup::drawFrame(FXDCWindow& dc,FXint x,FXint y,FXint w,FXint h){
switch(options&FRAME_MASK){
case FRAME_LINE: drawBorderRectangle(dc,x,y,w,h); break;
case FRAME_SUNKEN: drawSunkenRectangle(dc,x,y,w,h); break;
case FRAME_RAISED: drawRaisedRectangle(dc,x,y,w,h); break;
case FRAME_GROOVE: drawGrooveRectangle(dc,x,y,w,h); break;
case FRAME_RIDGE: drawRidgeRectangle(dc,x,y,w,h); break;
case FRAME_SUNKEN|FRAME_THICK: drawDoubleSunkenRectangle(dc,x,y,w,h); break;
case FRAME_RAISED|FRAME_THICK: drawDoubleRaisedRectangle(dc,x,y,w,h); break;
}
}
// Handle repaint
long FXPopup::onPaint(FXObject*,FXSelector,void* ptr){
FXEvent *ev=(FXEvent*)ptr;
FXDCWindow dc(this,ev);
dc.setForeground(backColor);
dc.fillRectangle(border,border,width-(border<<1),height-(border<<1));
drawFrame(dc,0,0,width,height);
return 1;
}
// Moving focus up
long FXPopup::onFocusUp(FXObject* sender,FXSelector sel,void* ptr){
if(!(options&POPUP_HORIZONTAL)) return FXPopup::onFocusPrev(sender,sel,ptr);
return 0;
}
// Moving focus down
long FXPopup::onFocusDown(FXObject* sender,FXSelector sel,void* ptr){
if(!(options&POPUP_HORIZONTAL)) return FXPopup::onFocusNext(sender,sel,ptr);
return 0;
}
// Moving focus left
long FXPopup::onFocusLeft(FXObject* sender,FXSelector sel,void* ptr){
if(options&POPUP_HORIZONTAL) return FXPopup::onFocusPrev(sender,sel,ptr);
return 0;
}
// Moving focus right
long FXPopup::onFocusRight(FXObject* sender,FXSelector sel,void* ptr){
if(options&POPUP_HORIZONTAL) return FXPopup::onFocusNext(sender,sel,ptr);
return 0;
}
// Focus moved down; wrap back to begin if at end
long FXPopup::onFocusNext(FXObject*,FXSelector,void* ptr){
FXWindow *child;
if(getFocus()){
child=getFocus()->getNext();
while(child){
if(child->shown()){
if(child->handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr)) return 1;
}
child=child->getNext();
}
}
child=getFirst();
while(child){
if(child->shown()){
if(child->handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr)) return 1;
}
child=child->getNext();
}
return 0;
}
// Focus moved up; wrap back to end if at begin
long FXPopup::onFocusPrev(FXObject*,FXSelector,void* ptr){
FXWindow *child;
if(getFocus()){
child=getFocus()->getPrev();
while(child){
if(child->shown()){
if(child->handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr)) return 1;
}
child=child->getPrev();
}
}
child=getLast();
while(child){
if(child->shown()){
if(child->handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr)) return 1;
}
child=child->getPrev();
}
return 0;
}
// Moved into the popup:- tell the target
long FXPopup::onEnter(FXObject* sender,FXSelector sel,void* ptr){
FXint px,py;
FXShell::onEnter(sender,sel,ptr);
translateCoordinatesTo(px,py,getParent(),((FXEvent*)ptr)->win_x,((FXEvent*)ptr)->win_y);
if(contains(px,py) && getGrabOwner()->grabbed()) getGrabOwner()->ungrab();
return 1;
}
// Moved outside the popup:- tell the target
long FXPopup::onLeave(FXObject* sender,FXSelector sel,void* ptr){
FXint px,py;
FXShell::onLeave(sender,sel,ptr);
translateCoordinatesTo(px,py,getParent(),((FXEvent*)ptr)->win_x,((FXEvent*)ptr)->win_y);
if(!contains(px,py) && shown() && !getGrabOwner()->grabbed() && getGrabOwner()->shown()) getGrabOwner()->grab();
return 1;
}
// Moved (while outside the popup):- tell the target
long FXPopup::onMotion(FXObject*,FXSelector,void* ptr){
FXEvent* ev=(FXEvent*)ptr;
FXint xx,yy;
if(contains(ev->root_x,ev->root_y)){
if(getGrabOwner()->grabbed()) getGrabOwner()->ungrab();
}
else{
getGrabOwner()->getParent()->translateCoordinatesFrom(xx,yy,getRoot(),ev->root_x,ev->root_y);
if(!getGrabOwner()->contains(xx,yy)){
if(!getGrabOwner()->grabbed() && getGrabOwner()->shown()) getGrabOwner()->grab();
}
}
return 1;
}
// Window may have appeared under the cursor, so ungrab if it was grabbed
long FXPopup::onMap(FXObject* sender,FXSelector sel,void* ptr){
FXint x,y; FXuint buttons;
FXShell::onMap(sender,sel,ptr);
getCursorPosition(x,y,buttons);
if(0<=x && 0<=y && x<width && y<height){
FXTRACE((200,"under cursor\n"));
if(getGrabOwner()->grabbed()) getGrabOwner()->ungrab();
}
return 1;
}
// Perform layout; return 0 because no GUI update is needed
long FXPopup::onLayout(FXObject*,FXSelector,void*){
if(getShrinkWrap()){
resize(getDefaultWidth(),getDefaultHeight());
}
else{
layout();
}
return 0;
}
// Pressed button outside popup
long FXPopup::onButtonPress(FXObject*,FXSelector,void*){
FXTRACE((200,"%s::onButtonPress %p\n",getClassName(),this));
handle(this,FXSEL(SEL_COMMAND,ID_UNPOST),nullptr);
//popdown(0);
return 1;
}
// Released button outside popup
long FXPopup::onButtonRelease(FXObject*,FXSelector,void* ptr){
FXEvent* event=(FXEvent*)ptr;
FXTRACE((200,"%s::onButtonRelease %p\n",getClassName(),this));
if(event->moved){handle(this,FXSEL(SEL_COMMAND,ID_UNPOST),nullptr);}
//popdown(0);
return 1;
}
// The widget lost the grab for some reason; unpost the menu
long FXPopup::onUngrabbed(FXObject* sender,FXSelector sel,void* ptr){
FXShell::onUngrabbed(sender,sel,ptr);
handle(this,FXSEL(SEL_COMMAND,ID_UNPOST),nullptr);
return 1;
}
// Key press; escape cancels popup
long FXPopup::onKeyPress(FXObject* sender,FXSelector sel,void* ptr){
FXEvent* event=(FXEvent*)ptr;
if(event->code==KEY_Escape || event->code==KEY_Cancel){
handle(this,FXSEL(SEL_COMMAND,ID_UNPOST),nullptr);
return 1;
}
return FXShell::onKeyPress(sender,sel,ptr);
}
// Key release; escape cancels popup
long FXPopup::onKeyRelease(FXObject* sender,FXSelector sel,void* ptr){
FXEvent* event=(FXEvent*)ptr;
if(event->code==KEY_Escape || event->code==KEY_Cancel){
handle(this,FXSEL(SEL_COMMAND,ID_UNPOST),nullptr);
return 1;
}
return FXShell::onKeyRelease(sender,sel,ptr);
}
// Unpost menu in case it was its own owner; otherwise
// tell the owner to do so.
long FXPopup::onCmdUnpost(FXObject*,FXSelector,void* ptr){
FXTRACE((150,"%s::onCmdUnpost %p\n",getClassName(),this));
if(grabowner){
grabowner->handle(this,FXSEL(SEL_COMMAND,ID_UNPOST),ptr);
}
else{
popdown();
if(grabbed()) ungrab();
}
return 1;
}
// Show popup and add to popup stack
void FXPopup::show(){
if(!(flags&FLAG_SHOWN)){
FXShell::show();
prevActive=getApp()->popupWindow;
if(prevActive) prevActive->nextActive=this;
getApp()->popupWindow=this;
setFocus();
}
}
// Hide popup and remove from popup stack
void FXPopup::hide(){
if(flags&FLAG_SHOWN){
FXShell::hide();
if(getApp()->popupWindow==this) getApp()->popupWindow=prevActive;
if(prevActive) prevActive->nextActive=nextActive;
if(nextActive) nextActive->prevActive=prevActive;
nextActive=nullptr;
prevActive=nullptr;
killFocus();
}
// Focus back to popup under this one, iff this was top one
}
// Popup the menu at some location
void FXPopup::popup(FXWindow* grabto,FXint x,FXint y,FXint w,FXint h){
FXint rx,ry,rw,rh;
#ifdef WIN32
RECT rect;
MONITORINFO minfo;
HMONITOR monitor;
rect.left=x;
rect.right=x+w;
rect.top=y;
rect.bottom=y+h;
// Get monitor info if we have this API
monitor=MonitorFromRect(&rect,MONITOR_DEFAULTTOPRIMARY);
if(monitor){
memset(&minfo,0,sizeof(minfo));
minfo.cbSize=sizeof(minfo);
GetMonitorInfo(monitor,&minfo);
rx=minfo.rcWork.left;
ry=minfo.rcWork.top;
rw=minfo.rcWork.right-minfo.rcWork.left;
rh=minfo.rcWork.bottom-minfo.rcWork.top;
}
// Otherwise use the work-area
else{
SystemParametersInfo(SPI_GETWORKAREA,sizeof(RECT),&rect,0);
rx=rect.left;
ry=rect.top;
rw=rect.right-rect.left;
rh=rect.bottom-rect.top;
}
#else
rx=getRoot()->getX();
ry=getRoot()->getY();
rw=getRoot()->getWidth();
rh=getRoot()->getHeight();
#endif
FXTRACE((150,"%s::popup %p\n",getClassName(),this));
grabowner=grabto;
if((options&POPUP_SHRINKWRAP) || w<=1) w=getDefaultWidth();
if((options&POPUP_SHRINKWRAP) || h<=1) h=getDefaultHeight();
if(x+w>rx+rw) x=rx+rw-w;
if(y+h>ry+rh) y=ry+rh-h;
if(x<rx) x=rx;
if(y<ry) y=ry;
position(x,y,w,h);
show();
raise();
setFocus();
if(!grabowner) grab();
}
// Pops down menu and its submenus
void FXPopup::popdown(){
FXTRACE((150,"%s::popdown %p\n",getClassName(),this));
if(!grabowner) ungrab();
grabowner=nullptr;
killFocus();
hide();
}
/*
// Popup the menu and grab to the given owner
FXint FXPopup::popup(FXint x,FXint y,FXint w,FXint h){
FXint rw,rh;
create();
if((options&POPUP_SHRINKWRAP) || w<=1) w=getDefaultWidth();
if((options&POPUP_SHRINKWRAP) || h<=1) h=getDefaultHeight();
rw=getRoot()->getWidth();
rh=getRoot()->getHeight();
if(x+w>rw) x=rw-w;
if(y+h>rh) y=rh-h;
if(x<0) x=0;
if(y<0) y=0;
position(x,y,w,h);
show();
raise();
return getApp()->runPopup(this);
}
// Pop down the menu
void FXPopup::popdown(FXint value){
getApp()->stopModal(this,value);
hide();
}
*/
// Close popup
long FXPopup::onCmdChoice(FXObject*,FXSelector,void*){
//popdown(SELID(sel)-ID_CHOICE);
return 1;
}
// Set base color
void FXPopup::setBaseColor(FXColor clr){
if(baseColor!=clr){
baseColor=clr;
update();
}
}
// Set highlight color
void FXPopup::setHiliteColor(FXColor clr){
if(hiliteColor!=clr){
hiliteColor=clr;
update();
}
}
// Set shadow color
void FXPopup::setShadowColor(FXColor clr){
if(shadowColor!=clr){
shadowColor=clr;
update();
}
}
// Set border color
void FXPopup::setBorderColor(FXColor clr){
if(borderColor!=clr){
borderColor=clr;
update();
}
}
// Get popup orientation
FXuint FXPopup::getOrientation() const {
return (options&POPUP_HORIZONTAL);
}
// Set popup orientation
void FXPopup::setOrientation(FXuint orient){
FXuint opts=((orient^options)&POPUP_HORIZONTAL)^options;
if(options!=opts){
options=opts;
recalc();
}
}
// Return shrinkwrap mode
FXbool FXPopup::getShrinkWrap() const {
return (options&POPUP_SHRINKWRAP)!=0;
}
// Change shrinkwrap mode
void FXPopup::setShrinkWrap(FXbool flag){
options^=((0-flag)^options)&POPUP_SHRINKWRAP;
}
// Change frame border style
void FXPopup::setFrameStyle(FXuint style){
FXuint opts=((style^options)&FRAME_MASK)^options;
if(options!=opts){
FXint b=(opts&FRAME_THICK) ? 2 : (opts&(FRAME_SUNKEN|FRAME_RAISED)) ? 1 : 0;
options=opts;
if(border!=b){
border=b;
recalc();
}
update();
}
}
// Get frame style
FXuint FXPopup::getFrameStyle() const {
return (options&FRAME_MASK);
}
// Unlink popup from active popup stack
FXPopup::~FXPopup(){
if(getApp()->popupWindow==this) getApp()->popupWindow=prevActive;
if(prevActive) prevActive->nextActive=nextActive;
if(nextActive) nextActive->prevActive=prevActive;
prevActive=(FXPopup*)-1L;
nextActive=(FXPopup*)-1L;
grabowner=(FXWindow*)-1L;
}
}
|