Showing the changes to java/pkg/awt/iPopupMenuPeer.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,652 @@ | |||
| 1 | package inferno.awt; | ||
| 2 | |||
| 3 | import java.awt.*; | ||
| 4 | import java.applet.*; | ||
| 5 | import java.awt.event.*; | ||
| 6 | import java.awt.peer.*; | ||
| 7 | import inferno.awt.*; | ||
| 8 | |||
| 9 | public class iPopupMenuPeer implements PopupMenuPeer,zPopupMenuParent { | ||
| 10 | PopupMenu target; | ||
| 11 | Window child; | ||
| 12 | |||
| 13 | public void show(Event e) | ||
| 14 | { | ||
| 15 | Object o=e.target; | ||
| 16 | |||
| 17 | if (o instanceof Component) | ||
| 18 | { | ||
| 19 | Component c=(Component)o; | ||
| 20 | Point pc=c.getLocationOnScreen(); | ||
| 21 | Container p; | ||
| 22 | |||
| 23 | if (c instanceof Container) | ||
| 24 | p=(Container)c; | ||
| 25 | else | ||
| 26 | p=c.getParent(); | ||
| 27 | |||
| 28 | if (p!=null) | ||
| 29 | { | ||
| 30 | Window w=null; | ||
| 31 | |||
| 32 | while (true) | ||
| 33 | { | ||
| 34 | if (p instanceof Frame) | ||
| 35 | { | ||
| 36 | Frame f=(Frame)p; | ||
| 37 | |||
| 38 | Point pf=f.getLocationOnScreen(); | ||
| 39 | |||
| 40 | closeChild(child); | ||
| 41 | |||
| 42 | child=new zPopupMenu(f,this,target,pc.x+e.x-pf.x,pc.y+e.y-pf.y); | ||
| 43 | child.pack(); | ||
| 44 | child.setVisible(true); | ||
| 45 | |||
| 46 | break; | ||
| 47 | } | ||
| 48 | else | ||
| 49 | if (p instanceof Applet) | ||
| 50 | { | ||
| 51 | Applet a=(Applet)p; | ||
| 52 | |||
| 53 | break; | ||
| 54 | } | ||
| 55 | |||
| 56 | p=p.getParent(); | ||
| 57 | |||
| 58 | if (p==null) break; | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | |||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | } | ||
| 70 | |||
| 71 | public void delItem(int item) | ||
| 72 | { | ||
| 73 | |||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | public void addItem(MenuItem item) | ||
| 78 | { | ||
| 79 | |||
| 80 | } | ||
| 81 | |||
| 82 | public void addSeparator() | ||
| 83 | { | ||
| 84 | } | ||
| 85 | |||
| 86 | public void disable() | ||
| 87 | { | ||
| 88 | |||
| 89 | } | ||
| 90 | |||
| 91 | public void enable() | ||
| 92 | { | ||
| 93 | |||
| 94 | } | ||
| 95 | |||
| 96 | public void setEnabled(boolean state) | ||
| 97 | { | ||
| 98 | |||
| 99 | } | ||
| 100 | |||
| 101 | public void setLabel(String label) | ||
| 102 | { | ||
| 103 | |||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | public void closeChild(Window w) | ||
| 108 | { | ||
| 109 | if (child!=null && child==w) | ||
| 110 | { | ||
| 111 | child.setVisible(false); | ||
| 112 | child.dispose(); | ||
| 113 | child=null; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | public void closeEverything() | ||
| 118 | { | ||
| 119 | closeChild(child); | ||
| 120 | } | ||
| 121 | |||
| 122 | public iPopupMenuPeer(PopupMenu target) { | ||
| 123 | this.target = target; | ||
| 124 | } | ||
| 125 | |||
| 126 | public void dispose() | ||
| 127 | { | ||
| 128 | } | ||
| 129 | |||
| 130 | } | ||
| 131 | |||
| 132 | class zPopupMenu extends Window implements ComponentListener,zPopupMenuParent{ | ||
| 133 | zPopupMenuDisplay menudisplay; | ||
| 134 | zPopupMenuParent ancestor; | ||
| 135 | int xoffset,yoffset; | ||
| 136 | |||
| 137 | public zPopupMenu(Frame f,zPopupMenuParent ancestor,Menu menu,int xoffset,int yoffset) | ||
| 138 | { | ||
| 139 | super(f); | ||
| 140 | this.xoffset=xoffset; | ||
| 141 | this.yoffset=yoffset; | ||
| 142 | this.ancestor=ancestor; | ||
| 143 | Point p=f.getLocation(); | ||
| 144 | p.translate(this.xoffset,this.yoffset); | ||
| 145 | setLocation(p); | ||
| 146 | f.addComponentListener(this); | ||
| 147 | this.menudisplay=new zPopupMenuDisplay(menu); | ||
| 148 | add(this.menudisplay,BorderLayout.CENTER); | ||
| 149 | //System.out.println("zPopupMenu Called: " + this); | ||
| 150 | } | ||
| 151 | |||
| 152 | public void closeChild(Window w) | ||
| 153 | { | ||
| 154 | if (menudisplay!=null) | ||
| 155 | { | ||
| 156 | if (!menudisplay.gotFocus() && ancestor!=null) | ||
| 157 | ancestor.closeChild(this); | ||
| 158 | else | ||
| 159 | menudisplay.closeChild(w); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | public void closeEverything() | ||
| 164 | { | ||
| 165 | if (ancestor!=null) | ||
| 166 | ancestor.closeEverything(); | ||
| 167 | } | ||
| 168 | |||
| 169 | public boolean gotFocus() | ||
| 170 | { | ||
| 171 | if (menudisplay==null) | ||
| 172 | return false; | ||
| 173 | else | ||
| 174 | return menudisplay.gotFocus(); | ||
| 175 | |||
| 176 | } | ||
| 177 | |||
| 178 | public void componentHidden(ComponentEvent e) | ||
| 179 | { | ||
| 180 | |||
| 181 | } | ||
| 182 | |||
| 183 | public void componentMoved(ComponentEvent e) | ||
| 184 | { | ||
| 185 | Point p=e.getComponent().getLocation(); | ||
| 186 | p.translate(xoffset,yoffset); | ||
| 187 | setLocation(p); | ||
| 188 | } | ||
| 189 | |||
| 190 | public void componentResized(ComponentEvent e) | ||
| 191 | { | ||
| 192 | |||
| 193 | } | ||
| 194 | |||
| 195 | public void componentShown(ComponentEvent e) | ||
| 196 | { | ||
| 197 | |||
| 198 | } | ||
| 199 | |||
| 200 | public void dispose() | ||
| 201 | { | ||
| 202 | if (menudisplay!=null) | ||
| 203 | { | ||
| 204 | menudisplay.closeChild(this); | ||
| 205 | } | ||
| 206 | super.dispose(); | ||
| 207 | } | ||
| 208 | |||
| 209 | } | ||
| 210 | |||
| 211 | class zPopupMenuDisplay extends Canvas implements | ||
| 212 | MouseListener,MouseMotionListener, | ||
| 213 | FocusListener{ | ||
| 214 | zPopupMenuObject menuObjects[]; | ||
| 215 | zPopupMenuObject selectedMenuObject; | ||
| 216 | zPopupMenu child; | ||
| 217 | int menuObjectCount; | ||
| 218 | Dimension pSize; | ||
| 219 | boolean focus; | ||
| 220 | Menu menu; | ||
| 221 | static Color highlight = SystemColor.controlHighlight; | ||
| 222 | static Color light = SystemColor.controlLtHighlight; | ||
| 223 | static Color medium = SystemColor.controlShadow; | ||
| 224 | static Color dark = SystemColor.controlDkShadow; | ||
| 225 | |||
| 226 | |||
| 227 | static final int TickBoxSize=13; | ||
| 228 | |||
| 229 | static final int TickLabelGap=4; | ||
| 230 | |||
| 231 | static final int SeparatorHeight=6; | ||
| 232 | |||
| 233 | |||
| 234 | public zPopupMenuDisplay(Menu menu) | ||
| 235 | |||
| 236 | { | ||
| 237 | super(); | ||
| 238 | setMenu(menu); | ||
| 239 | addMouseListener(this); | ||
| 240 | addMouseMotionListener(this); | ||
| 241 | /*addKeyListener(this);*/ | ||
| 242 | addFocusListener(this); | ||
| 243 | |||
| 244 | } | ||
| 245 | |||
| 246 | public boolean isFocusTraversable() { | ||
| 247 | return false; | ||
| 248 | } | ||
| 249 | |||
| 250 | |||
| 251 | void setMenu(Menu menu) | ||
| 252 | |||
| 253 | { | ||
| 254 | |||
| 255 | int maxwidth=0; | ||
| 256 | |||
| 257 | int yy=4; | ||
| 258 | |||
| 259 | int xx=4; | ||
| 260 | |||
| 261 | this.menu=menu; | ||
| 262 | |||
| 263 | |||
| 264 | menuObjectCount=menu.getItemCount(); | ||
| 265 | |||
| 266 | |||
| 267 | selectedMenuObject=null; | ||
| 268 | |||
| 269 | |||
| 270 | if (menuObjectCount==0) | ||
| 271 | |||
| 272 | menuObjects=null; | ||
| 273 | |||
| 274 | else | ||
| 275 | |||
| 276 | { | ||
| 277 | |||
| 278 | menuObjects=new zPopupMenuObject[menuObjectCount]; | ||
| 279 | |||
| 280 | |||
| 281 | boolean submenu=false; | ||
| 282 | |||
| 283 | for (int i=0;i<menuObjectCount;i++) | ||
| 284 | |||
| 285 | { | ||
| 286 | |||
| 287 | MenuItem mi=menu.getItem(i); | ||
| 288 | |||
| 289 | String label=mi.getLabel(); | ||
| 290 | if (!submenu && mi instanceof Menu) submenu=true; | ||
| 291 | |||
| 292 | |||
| 293 | if (!label.equals("-")) | ||
| 294 | |||
| 295 | { | ||
| 296 | |||
| 297 | Font f=mi.getFont(); | ||
| 298 | |||
| 299 | FontMetrics fm=getFontMetrics(f); | ||
| 300 | |||
| 301 | |||
| 302 | maxwidth=Math.max(maxwidth,TickBoxSize+TickLabelGap+fm.stringWidth(label)); | ||
| 303 | |||
| 304 | |||
| 305 | } | ||
| 306 | |||
| 307 | |||
| 308 | } | ||
| 309 | |||
| 310 | maxwidth+=2; // For outside lines | ||
| 311 | |||
| 312 | if (submenu) maxwidth+=8; | ||
| 313 | |||
| 314 | for (int i=0;i<menuObjectCount;i++) | ||
| 315 | { | ||
| 316 | |||
| 317 | MenuItem mi=menu.getItem(i); | ||
| 318 | |||
| 319 | String label=mi.getLabel(); | ||
| 320 | int h; | ||
| 321 | |||
| 322 | |||
| 323 | if (label.equals("-")) | ||
| 324 | { | ||
| 325 | h=SeparatorHeight; // Height | ||
| 326 | |||
| 327 | menuObjects[i]=new zPopupMenuSeparator(new Rectangle(xx,yy,maxwidth,h)); | ||
| 328 | |||
| 329 | } | ||
| 330 | else | ||
| 331 | { | ||
| 332 | Font f=mi.getFont(); | ||
| 333 | FontMetrics fm=getFontMetrics(f); | ||
| 334 | h=Math.max(fm.getHeight(),TickBoxSize)+4; //Height | ||
| 335 | |||
| 336 | menuObjects[i]=new zPopupMenuText(mi,new Rectangle(xx,yy,maxwidth,h)); | ||
| 337 | |||
| 338 | } | ||
| 339 | |||
| 340 | yy+=h; | ||
| 341 | |||
| 342 | |||
| 343 | } | ||
| 344 | |||
| 345 | |||
| 346 | } | ||
| 347 | |||
| 348 | pSize=new Dimension(xx+maxwidth+4,yy+4); | ||
| 349 | |||
| 350 | } | ||
| 351 | |||
| 352 | |||
| 353 | public Dimension getPreferredSize() | ||
| 354 | |||
| 355 | { | ||
| 356 | |||
| 357 | return pSize; | ||
| 358 | |||
| 359 | } | ||
| 360 | |||
| 361 | |||
| 362 | public void paint(Graphics g) | ||
| 363 | { | ||
| 364 | Dimension d=getSize(); | ||
| 365 | |||
| 366 | g.setColor(SystemColor.menu); | ||
| 367 | |||
| 368 | Misc.Draw3DArea(0,0,d.width,d.height,g); | ||
| 369 | |||
| 370 | for (int i=0;i<menuObjectCount;i++) | ||
| 371 | menuObjects[i].paint(g,0); | ||
| 372 | |||
| 373 | } | ||
| 374 | |||
| 375 | void closeChild() | ||
| 376 | { | ||
| 377 | closeChild(child); | ||
| 378 | } | ||
| 379 | |||
| 380 | void closeChild(Window w) | ||
| 381 | { | ||
| 382 | if (child!=null && child==w) | ||
| 383 | { | ||
| 384 | child.setVisible(false); | ||
| 385 | child.dispose(); | ||
| 386 | child=null; | ||
| 387 | } | ||
| 388 | } | ||
| 389 | |||
| 390 | |||
| 391 | |||
| 392 | |||
| 393 | public void mouseClicked(MouseEvent e) | ||
| 394 | { | ||
| 395 | } | ||
| 396 | |||
| 397 | public void mouseEntered(MouseEvent e) | ||
| 398 | { | ||
| 399 | } | ||
| 400 | |||
| 401 | public void mouseExited(MouseEvent e) | ||
| 402 | { | ||
| 403 | } | ||
| 404 | |||
| 405 | public void mousePressed(MouseEvent e) | ||
| 406 | { | ||
| 407 | } | ||
| 408 | |||
| 409 | public void mouseReleased(MouseEvent e) | ||
| 410 | { | ||
| 411 | if (selectedMenuObject!=null && selectedMenuObject instanceof zPopupMenuText) | ||
| 412 | { | ||
| 413 | MenuItem menuItem=((zPopupMenuText)selectedMenuObject).item; | ||
| 414 | if (menuItem.isEnabled() && !(menuItem instanceof Menu)) | ||
| 415 | { | ||
| 416 | String actionCommand=menuItem.getActionCommand(); | ||
| 417 | iToolkit.postEvent(new ActionEvent(menuItem, ActionEvent.ACTION_PERFORMED, | ||
| 418 | actionCommand)); | ||
| 419 | zPopupMenu w=(zPopupMenu)getParent(); | ||
| 420 | Frame f=(Frame)w.getParent(); | ||
| 421 | f.requestFocus(); | ||
| 422 | w.closeEverything(); | ||
| 423 | } | ||
| 424 | } | ||
| 425 | } | ||
| 426 | |||
| 427 | public void mouseMoved(MouseEvent e) | ||
| 428 | { | ||
| 429 | if (!focus) | ||
| 430 | { | ||
| 431 | focus=true; | ||
| 432 | requestFocus(); | ||
| 433 | } | ||
| 434 | |||
| 435 | if (selectedMenuObject==null || | ||
| 436 | !selectedMenuObject.area.contains(e.getPoint())) | ||
| 437 | { | ||
| 438 | Graphics g=getGraphics(); | ||
| 439 | if (selectedMenuObject!=null) | ||
| 440 | { | ||
| 441 | selectedMenuObject.paint(g,0); | ||
| 442 | selectedMenuObject=null; | ||
| 443 | closeChild(child); | ||
| 444 | } | ||
| 445 | for (int i=0;i<menuObjectCount;i++) | ||
| 446 | if (menuObjects[i].area.contains(e.getPoint())) | ||
| 447 | { | ||
| 448 | int mode=1; | ||
| 449 | |||
| 450 | selectedMenuObject=menuObjects[i]; | ||
| 451 | if (selectedMenuObject instanceof zPopupMenuText) | ||
| 452 | { | ||
| 453 | zPopupMenuText mo=(zPopupMenuText)selectedMenuObject; | ||
| 454 | |||
| 455 | if (mo.item.isEnabled()) | ||
| 456 | { | ||
| 457 | zPopupMenu w=(zPopupMenu)getParent(); | ||
| 458 | Frame f=(Frame)w.getParent(); | ||
| 459 | |||
| 460 | if (mo.item instanceof Menu) | ||
| 461 | { | ||
| 462 | int xx=w.xoffset+mo.area.x+mo.area.width; | ||
| 463 | int yy=w.yoffset+mo.area.y; | ||
| 464 | Menu menu=(Menu)mo.item; | ||
| 465 | |||
| 466 | mode=-1; | ||
| 467 | |||
| 468 | zPopupMenu newChild=new zPopupMenu(f,w,menu,xx,yy); | ||
| 469 | newChild.pack(); | ||
| 470 | newChild.setVisible(true); | ||
| 471 | closeChild(child); | ||
| 472 | child=newChild; | ||
| 473 | |||
| 474 | } | ||
| 475 | } | ||
| 476 | selectedMenuObject.paint(g,mode); | ||
| 477 | } | ||
| 478 | break; | ||
| 479 | } | ||
| 480 | |||
| 481 | g.dispose(); | ||
| 482 | } | ||
| 483 | } | ||
| 484 | |||
| 485 | public void mouseDragged(MouseEvent e) | ||
| 486 | { | ||
| 487 | mouseMoved(e); | ||
| 488 | } | ||
| 489 | |||
| 490 | public boolean gotFocus() | ||
| 491 | { | ||
| 492 | if (focus) | ||
| 493 | return true; | ||
| 494 | else | ||
| 495 | if (child==null) | ||
| 496 | return false; | ||
| 497 | else | ||
| 498 | return child.gotFocus(); | ||
| 499 | |||
| 500 | } | ||
| 501 | |||
| 502 | public void focusLost(FocusEvent e) | ||
| 503 | { | ||
| 504 | focus=false; | ||
| 505 | if (!gotFocus()) | ||
| 506 | { | ||
| 507 | zPopupMenu m=(zPopupMenu)getParent(); | ||
| 508 | if (m.ancestor!=null) m.ancestor.closeChild(m); | ||
| 509 | } | ||
| 510 | } | ||
| 511 | |||
| 512 | public void focusGained(FocusEvent e) | ||
| 513 | { | ||
| 514 | focus=true; | ||
| 515 | |||
| 516 | } | ||
| 517 | |||
| 518 | } | ||
| 519 | |||
| 520 | abstract class zPopupMenuObject { | ||
| 521 | Rectangle area; | ||
| 522 | |||
| 523 | public zPopupMenuObject() | ||
| 524 | { | ||
| 525 | } | ||
| 526 | |||
| 527 | public zPopupMenuObject(Rectangle area) | ||
| 528 | { | ||
| 529 | this.area=area; | ||
| 530 | } | ||
| 531 | |||
| 532 | public abstract void paint(Graphics g,int mode); | ||
| 533 | |||
| 534 | } | ||
| 535 | |||
| 536 | class zPopupMenuText extends zPopupMenuObject { | ||
| 537 | MenuItem item; | ||
| 538 | |||
| 539 | public zPopupMenuText(MenuItem item,Rectangle area) | ||
| 540 | { | ||
| 541 | super(area); | ||
| 542 | this.item=item; | ||
| 543 | } | ||
| 544 | |||
| 545 | |||
| 546 | void drawArrow(Graphics g,int x,int y) | ||
| 547 | { | ||
| 548 | for (int i=0;i<4;i++) | ||
| 549 | { | ||
| 550 | g.drawLine(x,y+i,x+3-i,y+i); | ||
| 551 | if (i!=0) | ||
| 552 | g.drawLine(x,y-i,x+3-i,y-i); | ||
| 553 | } | ||
| 554 | } | ||
| 555 | |||
| 556 | public void paint(Graphics g,int mode) | ||
| 557 | { | ||
| 558 | Font f=item.getFont(); | ||
| 559 | FontMetrics fm=g.getFontMetrics(f); | ||
| 560 | int fa=fm.getAscent(); | ||
| 561 | boolean enabled=item.isEnabled(); | ||
| 562 | boolean submenu=item instanceof Menu; | ||
| 563 | boolean checked=(item instanceof CheckboxMenuItem? | ||
| 564 | ((CheckboxMenuItem)item).getState():false); | ||
| 565 | String s=item.getLabel(); | ||
| 566 | int xx=area.x; | ||
| 567 | int yy=area.y; | ||
| 568 | |||
| 569 | g.setFont(f); | ||
| 570 | if (enabled) | ||
| 571 | { | ||
| 572 | g.setColor(SystemColor.menu); | ||
| 573 | if (mode!=0) | ||
| 574 | Misc.fill3DRect(area,mode>0,g); | ||
| 575 | else | ||
| 576 | g.fillRect(xx,yy,area.width,area.height); | ||
| 577 | |||
| 578 | xx+=2; | ||
| 579 | yy+=2; | ||
| 580 | g.setColor(SystemColor.menuText); | ||
| 581 | g.translate(-mode,-mode); | ||
| 582 | if (checked) Misc.drawTick(g,xx,yy,zPopupMenuDisplay.TickBoxSize); | ||
| 583 | |||
| 584 | g.drawString(s,zPopupMenuDisplay.TickBoxSize+zPopupMenuDisplay.TickLabelGap+xx,yy+fa); | ||
| 585 | if (submenu) | ||
| 586 | drawArrow(g,area.x+area.width-6,area.y+area.height/2); | ||
| 587 | g.translate(+mode,+mode); | ||
| 588 | } | ||
| 589 | else | ||
| 590 | { | ||
| 591 | xx+=2; | ||
| 592 | yy+=2; | ||
| 593 | if (checked) | ||
| 594 | { | ||
| 595 | g.setColor(SystemColor.controlLtHighlight); | ||
| 596 | Misc.drawTick(g,xx+1,yy+1,zPopupMenuDisplay.TickBoxSize); | ||
| 597 | g.setColor(SystemColor.controlShadow); | ||
| 598 | Misc.drawTick(g,xx,yy,zPopupMenuDisplay.TickBoxSize); | ||
| 599 | } | ||
| 600 | |||
| 601 | |||
| 602 | Misc.drawDisabledString(s,zPopupMenuDisplay.TickBoxSize+zPopupMenuDisplay.TickLabelGap+xx,yy+fa,g); | ||
| 603 | if (submenu) | ||
| 604 | { | ||
| 605 | g.setColor(SystemColor.controlLtHighlight); | ||
| 606 | drawArrow(g,area.x+area.width-6+1,area.y+area.height/2+1); | ||
| 607 | g.setColor(SystemColor.controlShadow); | ||
| 608 | drawArrow(g,area.x+area.width-6,area.y+area.height/2); | ||
| 609 | } | ||
| 610 | } | ||
| 611 | |||
| 612 | |||
| 613 | } | ||
| 614 | |||
| 615 | } | ||
| 616 | |||
| 617 | class zPopupMenuSeparator extends zPopupMenuObject { | ||
| 618 | |||
| 619 | public zPopupMenuSeparator(Rectangle area) | ||
| 620 | { | ||
| 621 | super(area); | ||
| 622 | } | ||
| 623 | |||
| 624 | public void paint(Graphics g,int mode) | ||
| 625 | { | ||
| 626 | int y=area.y+zPopupMenuDisplay.SeparatorHeight/2-1; | ||
| 627 | |||
| 628 | g.setColor(zPopupMenuDisplay.medium); | ||
| 629 | g.drawLine(area.x,y,area.x+area.width,y); | ||
| 630 | |||
| 631 | y+=1; | ||
| 632 | |||
| 633 | g.setColor(zPopupMenuDisplay.light); | ||
| 634 | g.drawLine(area.x,y,area.x+area.width,y); | ||
| 635 | |||
| 636 | |||
| 637 | } | ||
| 638 | |||
| 639 | } | ||
| 640 | |||
| 641 | interface zPopupMenuParent { | ||
| 642 | |||
| 643 | public void closeChild(Window W); | ||
| 644 | |||
| 645 | public void closeEverything(); | ||
| 646 | |||
| 647 | } | ||
| 648 | |||
| 649 | |||
| 650 | |||
| 651 | |||
| 652 | |||