History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/awt/iMenuBarPeer.java (1 of 18352 files). Show all

Unified · Split

java/pkg/awt/iMenuBarPeer.java+906−0
@@ -0,0 +1,906 @@
1package inferno.awt;
2
3import java.awt.*;
4import java.awt.event.*;
5import java.awt.peer.*;
6
7public class iMenuBarPeer implements
8MenuBarPeer,MouseListener,MouseMotionListener,zMenuParent {
9 MenuBar target;
10 int height;
11 zMenuBarObject selectedMenuObject;
12 zMenuBarObject menuObjects[];
13 int menuObjectCount;
14 Window child;
15
16 public void closeChild(Window w)
17 {
18 // Called from child via zMenuParent interface to
19 // close itself down. Since Window may take some time to
20 // close down, check to see if we are still the child window
21 // not the previous window
22 if (child!=null && child==w)
23 {
24
25 child.setVisible(false); // Hide Window
26 child.dispose(); // Destroy window
27 child=null; // Lose reference to Window
28 if (selectedMenuObject!=null)
29 {
30 // If menu item selected, display not selected
31 Frame frm=(Frame)target.getParent();
32 Graphics g=frm.getGraphics();
33 selectedMenuObject.paint(g,0);
34 g.dispose();
35 }
36 }
37 }
38
39 public void closeEverything()
40 {
41 // Close our child
42 closeChild(child);
43 }
44
45 void create(Frame f)
46 {
47 // Calculate sizes
48 recalculate();
49 }
50
51 public iMenuBarPeer(MenuBar target) {
52 Frame frm=(Frame)target.getParent();
53 frm.addMouseListener(this);
54 frm.addMouseMotionListener(this);
55 this.target = target;
56 create(frm);
57 }
58
59 // Required by MenuBarPeer interface but not used
60 public void dispose()
61 {
62 }
63
64 public void addMenu(Menu m) {
65 // Calculate sizes
66 recalculate();
67 }
68
69 public void delMenu(int index) {
70 // Calculate sizes
71 recalculate();
72 }
73
74 public void addHelpMenu(Menu m) {
75 // Calculate sizes
76 recalculate();
77 }
78
79 // Calculate height of menubar
80 public int getHeight()
81 {
82 height=0;
83
84 if (target!=null)
85 {
86 Frame frm=(Frame)target.getParent();
87 for (int i=target.getMenuCount()-1;i>=0;--i)
88 {
89 Menu m=target.getMenu(i);
90 Font f=m.getFont();
91 FontMetrics fm=frm.getFontMetrics(f);
92
93 height=Math.max(height,fm.getHeight()+2);
94 }
95 }
96 return height;
97 }
98
99 public void recalculate()
100 {
101 // Create all the objects
102 if (target!=null)
103 {
104 Frame frm=(Frame)target.getParent();
105 Rectangle d=frm.getBounds();
106 Insets is=frm.getInsets();
107 Menu hm=target.getHelpMenu();
108
109 selectedMenuObject=null;
110 menuObjects=null;
111 menuObjectCount=target.getMenuCount();
112 if (menuObjectCount!=0)
113 {
114 int x=is.left;
115 int y=is.top-height;
116 menuObjects=new zMenuBarObject[menuObjectCount];
117
118
119 for (int i=0;i<menuObjectCount;i++)
120 {
121 int w,xx;
122 Menu m=target.getMenu(i);
123 Font f=m.getFont();
124 if (f==null)
125 {
126 w=0;
127 xx=0;
128 }
129 else
130 {
131 FontMetrics fm=frm.getFontMetrics(f);
132 w=fm.stringWidth(" "+m.getLabel()+" ")+2;
133
134 if (m==hm)
135 {
136 xx=d.width-is.right-w;
137 }
138 else
139 {
140 xx=x;
141 x+=w;
142 }
143
144 }
145 menuObjects[i]=new zMenuBarObject(m,new Rectangle(xx,y,w,height));
146
147 }
148 }
149
150 }
151 }
152
153 // Not the Component paint but similar
154 // called from Frame.paint
155 public void paint(Graphics gg)
156 {
157 Frame frm=(Frame)target.getParent();
158 Insets is=frm.getInsets();
159 Rectangle d=frm.getBounds();
160 int y=is.top-height;
161 int x=is.left;
162 int w=d.width-is.left-is.right;
163 int h=height;
164 Graphics g=gg.create();
165
166 // Display the menu objects
167 g.setClip(x,y,w,h);
168 g.setColor(SystemColor.menu);
169 g.fillRect(x,y,w,h);
170
171 for (int i=0;i<menuObjectCount;i++)
172 menuObjects[i].paint(g);
173
174
175 g.dispose();
176 }
177
178 public void mouseExited(MouseEvent e)
179 {
180 // Deselect if mouse exits window
181 // (Almost certainly never called by inferno)
182 if (selectedMenuObject!=null)
183 {
184 Frame frm=(Frame)target.getParent();
185 Graphics g=frm.getGraphics();
186 selectedMenuObject.paint(g,0);
187 selectedMenuObject=null;
188 g.dispose();
189 }
190 }
191
192 // Required by listener interface but not used
193 public void mouseEntered(MouseEvent e)
194 {
195 }
196
197 // Required by listener interface but not used
198 public void mouseClicked(MouseEvent e)
199 {
200 }
201
202 public void mousePressed(MouseEvent e)
203 {
204 // If within an object (selectMenuObject set by mouseMove)
205 if (selectedMenuObject!=null && selectedMenuObject.area.contains(e.getPoint()))
206 {
207 Frame frm=(Frame)target.getParent();
208 Graphics g=frm.getGraphics();
209 selectedMenuObject.paint(g,-1);
210 g.dispose();
211
212 Menu menu=selectedMenuObject.menu;
213 if(menu != null){
214 Rectangle md=selectedMenuObject.area;
215 Rectangle fd=frm.getBounds();
216 int xoff=md.x;
217 int yoff=md.y+md.height;
218
219 closeChild(child);
220 child=new zMenuWindow(frm,this,menu,xoff,yoff);
221 child.pack();
222 child.setVisible(true);
223 }
224 }
225 else
226 {
227 // Close child window (if any)
228 if (child != null)
229 closeChild(child);
230 // Display deselected
231 if(selectedMenuObject != null){
232 Frame frm=(Frame)target.getParent();
233 Graphics g=frm.getGraphics();
234 selectedMenuObject.paint(g,0);
235 g.dispose();
236 }
237 }
238 }
239
240 // Require by listener interface but not used
241 public void mouseReleased(MouseEvent e)
242 {
243 }
244
245 public void mouseMoved(MouseEvent e)
246 {
247 // Track the mouse movement over the menuObjects
248 // setting selectedMenuObject as appropriate
249 if (selectedMenuObject==null ||
250 !selectedMenuObject.area.contains(e.getPoint()))
251 {
252 Frame frm=(Frame)target.getParent();
253 Graphics g=frm.getGraphics();
254 // if (selectedMenuObject!=null && child==null)
255 if (selectedMenuObject!=null)
256 {
257 selectedMenuObject.paint(g,0);
258 selectedMenuObject=null;
259 }
260
261 for (int i=0;i<menuObjectCount;i++)
262 if (menuObjects[i].area.contains(e.getPoint()))
263 {
264 if (selectedMenuObject!=null)
265 {
266 selectedMenuObject.paint(g,0);
267 selectedMenuObject=null;
268 }
269 if (menuObjects[i].menu.isEnabled())
270 {
271 int mode=1;
272 selectedMenuObject=menuObjects[i];
273 if (child!=null)
274 {
275 Menu menu=selectedMenuObject.menu;
276 Rectangle md=selectedMenuObject.area;
277 Rectangle fd=frm.getBounds();
278 int xoff=md.x;
279 int yoff=md.y+md.height;
280
281 Window newChild=new zMenuWindow(frm,this,menu,xoff,yoff);
282 newChild.pack();
283 newChild.setVisible(true);
284 closeChild(child);
285 child=newChild;
286 mode=-1;
287 }
288
289 selectedMenuObject.paint(g,mode);
290 }
291 break;
292 }
293
294 g.dispose();
295 }
296 }
297
298 // Require by listener interface but not used
299 public void mouseDragged(MouseEvent e)
300 {
301 }
302
303
304}
305
306// This is the object corresponding to the menu items
307// on the menubar
308class zMenuBarObject {
309 Menu menu;
310 Rectangle area;
311 int mode;
312
313 public zMenuBarObject(Menu menu,Rectangle area)
314 {
315 this.menu=menu;
316 this.area=area;
317 this.mode=0;
318 }
319
320 public void paint(Graphics g,int mode)
321 {
322 this.mode=mode;
323 paint(g);
324 }
325
326 // Not the component paint but similar
327 // displays the menu item
328 public void paint(Graphics g)
329 {
330 Font f=menu.getFont();
331 FontMetrics fm=g.getFontMetrics(f);
332
333 g.setColor(SystemColor.menu);
334
335 if (mode!=0)
336 Misc.fill3DRect(area,mode>0,g);
337 else
338 g.fillRect(area.x,area.y,area.width,area.height);
339
340 g.translate(-mode,-mode);
341
342 g.setFont(f);
343 if (menu.isEnabled())
344 {
345 g.setColor(SystemColor.menuText);
346 g.drawString(" "+menu.getLabel(),area.x+1,area.y+fm.getAscent()+1);
347 }
348 else
349 {
350 Misc.drawDisabledString(" "+menu.getLabel(),area.x+1,area.y+fm.getAscent()+1,g);
351
352 }
353
354 g.translate(+mode,+mode);
355
356 }
357
358}
359
360// This the window containing the dropdown menus
361// the painting is done by zMenuDisplay
362// zMenuDisplay returns its size and the Window sizes itself
363// accordingly
364
365class zMenuWindow extends Window implements ComponentListener,zMenuParent{
366 zMenuDisplay menudisplay;
367 zMenuParent ancestor;
368 int xoffset,yoffset;
369
370 public zMenuWindow(Frame f,zMenuParent ancestor,Menu menu,int xoffset,int yoffset)
371{
372 super(f);
373 this.xoffset=xoffset;
374 this.yoffset=yoffset;
375 this.ancestor=ancestor;
376 Point p=f.getLocation();
377 p.translate(this.xoffset,this.yoffset);
378 setLocation(p);
379 f.addComponentListener(this);
380 this.menudisplay=new zMenuDisplay(menu);
381 add(this.menudisplay,BorderLayout.CENTER);
382 }
383
384 public void closeChild(Window w)
385 {
386 if (menudisplay!=null)
387 {
388 if (!menudisplay.gotFocus() && ancestor!=null)
389 ancestor.closeChild(this);
390 else
391 menudisplay.closeChild(w);
392 }
393 }
394
395 public void closeEverything()
396{
397 if (ancestor!=null)
398 ancestor.closeEverything();
399}
400
401 public boolean gotFocus()
402 {
403 if (menudisplay==null)
404 return false;
405 else
406 return menudisplay.gotFocus();
407
408 }
409
410 public void componentHidden(ComponentEvent e)
411 {
412
413 }
414
415 public void componentMoved(ComponentEvent e)
416 {
417 Point p=e.getComponent().getLocation();
418 p.translate(xoffset,yoffset);
419 setLocation(p);
420 }
421
422 public void componentResized(ComponentEvent e)
423 {
424
425 }
426
427 public void componentShown(ComponentEvent e)
428 {
429
430 }
431
432 public void dispose()
433 {
434 if (menudisplay!=null)
435 {
436 menudisplay.closeChild(this);
437 }
438 super.dispose();
439 }
440
441}
442
443// This is the display part of the zMenuWindow
444// it covers the whole window
445
446class zMenuDisplay extends Canvas implements
447MouseListener,MouseMotionListener,
448FocusListener{
449 zMenuObject menuObjects[];
450 zMenuObject selectedMenuObject;
451 zMenuWindow child;
452 int menuObjectCount;
453 Dimension pSize;
454 boolean focus;
455 Menu menu;
456 static Color highlight = SystemColor.controlHighlight;
457 static Color light = SystemColor.controlLtHighlight;
458 static Color medium = SystemColor.controlShadow;
459 static Color dark = SystemColor.controlDkShadow;
460
461
462 static final int TickBoxSize=13;
463
464 static final int TickLabelGap=4;
465
466 static final int SeparatorHeight=6;
467
468
469 public zMenuDisplay(Menu menu)
470
471 {
472 super();
473 setMenu(menu);
474 addMouseListener(this);
475 addMouseMotionListener(this);
476 addFocusListener(this);
477 }
478
479 public boolean isFocusTraversable() {
480 return false;
481 }
482
483
484 void setMenu(Menu menu)
485
486 {
487
488 int maxwidth=0;
489
490 int yy=4;
491
492 int xx=4;
493
494 this.menu=menu;
495
496
497 menuObjectCount=menu.getItemCount();
498
499
500 selectedMenuObject=null;
501
502
503 if (menuObjectCount==0)
504
505 menuObjects=null;
506
507 else
508
509 {
510
511 menuObjects=new zMenuObject[menuObjectCount];
512
513
514 boolean submenu=false;
515
516 for (int i=0;i<menuObjectCount;i++)
517
518 {
519
520 MenuItem mi=menu.getItem(i);
521
522 String label=mi.getLabel();
523 if (!submenu && mi instanceof Menu) submenu=true;
524
525
526 if (!label.equals("-"))
527
528 {
529
530 Font f=mi.getFont();
531
532 FontMetrics fm=getFontMetrics(f);
533
534
535 maxwidth=Math.max(maxwidth,TickBoxSize+TickLabelGap+fm.stringWidth(label));
536
537
538 }
539
540
541 }
542
543 maxwidth+=2; // For outside lines
544
545 if (submenu) maxwidth+=8;
546
547 for (int i=0;i<menuObjectCount;i++)
548 {
549
550 MenuItem mi=menu.getItem(i);
551
552 String label=mi.getLabel();
553 int h;
554
555
556 if (label.equals("-"))
557 {
558 h=SeparatorHeight; // Height
559
560 menuObjects[i]=new zMenuSeparator(new Rectangle(xx,yy,maxwidth,h));
561
562 }
563 else
564 {
565 Font f=mi.getFont();
566 FontMetrics fm=getFontMetrics(f);
567 h=Math.max(fm.getHeight(),TickBoxSize)+4; //Height
568
569 menuObjects[i]=new zMenuText(mi,new Rectangle(xx,yy,maxwidth,h));
570
571 }
572
573 yy+=h;
574
575
576 }
577
578
579 }
580
581 pSize=new Dimension(xx+maxwidth+4,yy+4);
582
583 }
584
585
586 public Dimension getPreferredSize()
587
588 {
589
590 return pSize;
591
592 }
593
594
595 public void paint(Graphics g)
596 {
597 Dimension d=getSize();
598
599 g.setColor(SystemColor.menu);
600
601 Misc.Draw3DArea(0,0,d.width,d.height,g);
602
603 for (int i=0;i<menuObjectCount;i++)
604 menuObjects[i].paint(g,0);
605
606 }
607
608 void closeChild()
609 {
610 closeChild(child);
611 }
612
613 void closeChild(Window w)
614 {
615 if (w!=null && w == child)
616 {
617 w.setVisible(false);
618 w.dispose();
619 w=null;
620 }
621 if (child!=null)
622 {
623 child.setVisible(false);
624 child.dispose();
625 child = null;
626 }
627 }
628
629
630
631 // Required by listener interface but not used
632 public void mouseClicked(MouseEvent e)
633 {
634 }
635
636 // Required by listener interface but not used
637 public void mouseEntered(MouseEvent e)
638 {
639 }
640
641 // Required by listener interface but not used
642 public void mouseExited(MouseEvent e)
643 {
644 }
645
646 // Required by listener interface but not used
647 public void mousePressed(MouseEvent e)
648 {
649 }
650
651 public void mouseReleased(MouseEvent e)
652 {
653 if (selectedMenuObject!=null && selectedMenuObject instanceof zMenuText)
654 {
655 MenuItem menuItem=((zMenuText)selectedMenuObject).item;
656 if (menuItem.isEnabled() && !(menuItem instanceof Menu))
657 {
658 String actionCommand=menuItem.getActionCommand();
659 iToolkit.postEvent(new ActionEvent(menuItem, ActionEvent.ACTION_PERFORMED,
660 actionCommand));
661 zMenuWindow w=(zMenuWindow)getParent();
662 Frame f=(Frame)w.getParent();
663 f.requestFocus();
664 w.closeEverything();
665 }
666 }
667 }
668
669 public void mouseMoved(MouseEvent e)
670 {
671 if (!focus)
672 {
673 focus=true;
674 requestFocus();
675 }
676
677 if (selectedMenuObject==null ||
678 !selectedMenuObject.area.contains(e.getPoint()))
679 {
680 Graphics g=getGraphics();
681 if (selectedMenuObject!=null)
682 {
683 selectedMenuObject.paint(g,0);
684 selectedMenuObject=null;
685 closeChild(child);
686 }
687 for (int i=0;i<menuObjectCount;i++)
688 if (menuObjects[i].area.contains(e.getPoint()))
689 {
690 int mode=1;
691
692 selectedMenuObject=menuObjects[i];
693 if (selectedMenuObject instanceof zMenuText)
694 {
695 zMenuText mo=(zMenuText)selectedMenuObject;
696
697 if (mo.item.isEnabled())
698 {
699 zMenuWindow w=(zMenuWindow)getParent();
700 Frame f=(Frame)w.getParent();
701
702 if (mo.item instanceof Menu)
703 {
704 int xx=w.xoffset+mo.area.x+mo.area.width;
705 int yy=w.yoffset+mo.area.y;
706 Menu menu=(Menu)mo.item;
707
708 mode=-1;
709
710 zMenuWindow newChild=new zMenuWindow(f,w,menu,xx,yy);
711 newChild.pack();
712 newChild.setVisible(true);
713 closeChild(child);
714 child=newChild;
715 }
716 }
717 selectedMenuObject.paint(g,mode);
718 }
719 break;
720 }
721
722 g.dispose();
723 }
724 }
725
726 public void mouseDragged(MouseEvent e)
727 {
728 mouseMoved(e);
729 }
730
731 public boolean gotFocus()
732 {
733 if (focus)
734 return true;
735 else
736 if (child==null)
737 return false;
738 else
739 return child.gotFocus();
740
741 }
742
743 public void focusLost(FocusEvent e)
744 {
745 focus=false;
746 if (!gotFocus())
747 {
748 zMenuWindow m=(zMenuWindow)getParent();
749 if (m.ancestor!=null) m.ancestor.closeChild(m);
750 }
751 }
752
753 public void focusGained(FocusEvent e)
754 {
755 focus=true;
756
757 }
758
759}
760
761// base class for the menuObjects
762
763abstract class zMenuObject {
764 Rectangle area;
765
766 public zMenuObject()
767 {
768 }
769
770 public zMenuObject(Rectangle area)
771 {
772 this.area=area;
773 }
774
775 public abstract void paint(Graphics g,int mode);
776
777}
778
779// Standard item item class
780
781class zMenuText extends zMenuObject {
782 MenuItem item;
783
784public zMenuText(MenuItem item,Rectangle area)
785{
786 super(area);
787 this.item=item;
788}
789
790
791void drawArrow(Graphics g,int x,int y)
792{
793 for (int i=0;i<4;i++)
794 {
795 g.drawLine(x,y+i,x+3-i,y+i);
796 if (i!=0)
797 g.drawLine(x,y-i,x+3-i,y-i);
798 }
799}
800
801public void paint(Graphics g,int mode)
802{
803 Font f=item.getFont();
804 FontMetrics fm=g.getFontMetrics(f);
805 int fa=fm.getAscent();
806 boolean enabled=item.isEnabled();
807 boolean submenu=item instanceof Menu;
808 boolean checked=(item instanceof CheckboxMenuItem?
809 ((CheckboxMenuItem)item).getState():false);
810 String s=item.getLabel();
811 int xx=area.x;
812 int yy=area.y;
813
814 g.setFont(f);
815 if (enabled)
816 {
817 g.setColor(SystemColor.menu);
818 if (mode!=0)
819 Misc.fill3DRect(area,mode>0,g);
820 else
821 g.fillRect(xx,yy,area.width,area.height);
822
823 xx+=2;
824 yy+=2;
825 g.setColor(SystemColor.menuText);
826 g.translate(-mode,-mode);
827 if (checked) Misc.drawTick(g,xx,yy,zMenuDisplay.TickBoxSize);
828
829g.drawString(s,zMenuDisplay.TickBoxSize+zMenuDisplay.TickLabelGap+xx,yy+fa);
830 if (submenu)
831 drawArrow(g,area.x+area.width-6,area.y+area.height/2);
832 g.translate(+mode,+mode);
833 }
834 else
835 {
836 xx+=2;
837 yy+=2;
838 if (checked)
839 {
840 g.setColor(SystemColor.controlLtHighlight);
841 Misc.drawTick(g,xx+1,yy+1,zMenuDisplay.TickBoxSize);
842 g.setColor(SystemColor.controlShadow);
843 Misc.drawTick(g,xx,yy,zMenuDisplay.TickBoxSize);
844 }
845
846Misc.drawDisabledString(s,zMenuDisplay.TickBoxSize+zMenuDisplay.TickLabelGap+xx,yy+fa,g);
847 if (submenu)
848 {
849 g.setColor(SystemColor.controlLtHighlight);
850 drawArrow(g,area.x+area.width-6+1,area.y+area.height/2+1);
851 g.setColor(SystemColor.controlShadow);
852 drawArrow(g,area.x+area.width-6,area.y+area.height/2);
853 }
854 }
855
856
857}
858
859}
860
861// separator bar
862
863class zMenuSeparator extends zMenuObject {
864
865public zMenuSeparator(Rectangle area)
866{
867 super(area);
868}
869
870public void paint(Graphics g,int mode)
871{
872 int y=area.y+zMenuDisplay.SeparatorHeight/2-1;
873
874 g.setColor(zMenuDisplay.medium);
875 g.drawLine(area.x,y,area.x+area.width,y);
876
877 y+=1;
878
879 g.setColor(zMenuDisplay.light);
880 g.drawLine(area.x,y,area.x+area.width,y);
881
882
883}
884
885}
886
887// The interface to look backwards to parent to close ourselves
888
889interface zMenuParent {
890
891
892 // Called by child (or parent) to close and delete itself
893 public void closeChild(Window W);
894
895 // Close all the windows back to the menubar
896 public void closeEverything();
897
898}
899
900
901
902
903
904
905
906