3030import java .applet .JSApplet ;
3131import java .awt .Component ;
3232import java .awt .Container ;
33+ import java .awt .Dimension ;
3334import java .awt .EventQueue ;
3435import java .awt .FontMetrics ;
3536import java .awt .JSFrame ;
6263import swingjs .JSAppletViewer ;
6364import swingjs .JSUtil ;
6465import swingjs .api .Interface ;
66+ import swingjs .api .js .DOMNode ;
67+ import swingjs .plaf .JSComponentUI ;
6568
6669/**
6770 * A collection of utility methods for Swing.
@@ -858,39 +861,43 @@ public static String layoutCompoundLabel(JComponent c,
858861 Rectangle textR ,
859862 int textIconGap )
860863 {
861- boolean orientationIsLeftToRight = true ;
862- int hAlign = horizontalAlignment ;
863- int hTextPos = horizontalTextPosition ;
864-
865- if (c != null ) {
866- if (!(c .getComponentOrientation ().isLeftToRight ())) {
867- orientationIsLeftToRight = false ;
868- }
869- }
870-
871- // Translate LEADING/TRAILING values in horizontalAlignment
872- // to LEFT/RIGHT values depending on the components orientation
873- switch (horizontalAlignment ) {
874- case LEADING :
875- hAlign = (orientationIsLeftToRight ) ? LEFT : RIGHT ;
876- break ;
877- case TRAILING :
878- hAlign = (orientationIsLeftToRight ) ? RIGHT : LEFT ;
879- break ;
880- }
881-
882- // Translate LEADING/TRAILING values in horizontalTextPosition
883- // to LEFT/RIGHT values depending on the components orientation
884- switch (horizontalTextPosition ) {
885- case LEADING :
886- hTextPos = (orientationIsLeftToRight ) ? LEFT : RIGHT ;
887- break ;
888- case TRAILING :
889- hTextPos = (orientationIsLeftToRight ) ? RIGHT : LEFT ;
890- break ;
891- }
892-
893- return layoutCompoundLabelImpl (c ,
864+
865+ JSComponentUI ui = (JSComponentUI ) c .getUI ();
866+
867+ boolean ltr = (c == null || c .getComponentOrientation ().isLeftToRight ());
868+ int hAlign , hTextPos ;
869+
870+ if (ui .menuAnchorNode == null ) {
871+ // Translate LEADING/TRAILING values in horizontalAlignment
872+ // to LEFT/RIGHT values depending on the components orientation
873+ switch (horizontalAlignment ) {
874+ case LEADING :
875+ hAlign = (ltr ) ? LEFT : RIGHT ;
876+ break ;
877+ case TRAILING :
878+ hAlign = (ltr ) ? RIGHT : LEFT ;
879+ break ;
880+ default :
881+ hAlign = horizontalAlignment ;
882+ break ;
883+ }
884+ switch (horizontalTextPosition ) {
885+ case LEADING :
886+ hTextPos = (ltr ) ? LEFT : RIGHT ;
887+ break ;
888+ case TRAILING :
889+ hTextPos = (ltr ) ? RIGHT : LEFT ;
890+ break ;
891+ default :
892+ hTextPos = horizontalTextPosition ;
893+ break ;
894+ }
895+ } else {
896+ // menus are far simpler!
897+ hAlign = hTextPos = (ltr ? LEFT : RIGHT );
898+ }
899+
900+ return layoutCompoundLabelImpl (c , ui ,
894901 fm ,
895902 text ,
896903 icon ,
@@ -927,7 +934,7 @@ public static String layoutCompoundLabel(
927934 Rectangle textR ,
928935 int textIconGap )
929936 {
930- return layoutCompoundLabelImpl (null , fm , text , icon ,
937+ return layoutCompoundLabelImpl (null , null , fm , text , icon ,
931938 verticalAlignment ,
932939 horizontalAlignment ,
933940 verticalTextPosition ,
@@ -947,6 +954,7 @@ public static String layoutCompoundLabel(
947954 */
948955 private static String layoutCompoundLabelImpl (
949956 JComponent c ,
957+ JSComponentUI ui ,
950958 FontMetrics fm ,
951959 String text ,
952960 Icon icon ,
@@ -962,12 +970,16 @@ private static String layoutCompoundLabelImpl(
962970 /* Initialize the icon bounds rectangle iconR.
963971 */
964972
973+ System .out .println ("SwingUtil tiv0 " + text + "\n " + textR + "\n " + iconR + "\n " + viewR + "\n "
974+ + fm .getDescent () + " " + fm .getAscent () + " " + fm .getHeight () + " " + fm .getMaxAscent ());
975+
965976 if (icon != null ) {
966977 iconR .width = icon .getIconWidth ();
967978 iconR .height = icon .getIconHeight ();
968- }
969- else {
970- iconR .width = iconR .height = 0 ;
979+ // }
980+ // else {
981+ // SwingJS -- we allow passing in the radio button
982+ // iconR.width = iconR.height = 0;
971983 }
972984
973985 /* Initialize the text bounds rectangle textR. If a null
@@ -991,7 +1003,7 @@ private static String layoutCompoundLabelImpl(
9911003 }
9921004 else {
9931005 int availTextWidth ;
994- gap = (icon == null ) ? 0 : textIconGap ;
1006+ gap = (iconR . width == 0 ) ? 0 : textIconGap ;
9951007
9961008 if (horizontalTextPosition == CENTER ) {
9971009 availTextWidth = viewR .width ;
@@ -1005,8 +1017,10 @@ private static String layoutCompoundLabelImpl(
10051017 (int ) v .getPreferredSpan (View .X_AXIS ));
10061018 textR .height = (int ) v .getPreferredSpan (View .Y_AXIS );
10071019 } else {
1008- textR .width = SwingUtilities2 .stringWidth (c , fm , text );
1009-
1020+ Dimension d = ui .getHTMLSize (ui .textNode );
1021+ textR .width = d .width ;
1022+ textR .height = d .height ;
1023+ System .out .println ("swingutil " + text + " " + d + " " + fm .getHeight ());
10101024 // Take into account the left and right side bearings.
10111025 // This gives more space than it is actually needed,
10121026 // but there are two reasons:
@@ -1015,21 +1029,21 @@ private static String layoutCompoundLabelImpl(
10151029 // themselves. NOTE: all pref size calculations don't do it.
10161030 // 2. You can do a drawString at the returned location
10171031 // and the text won't be clipped.
1018- lsb = SwingUtilities2 .getLeftSideBearing (c , fm , text );
1019- if (lsb < 0 ) {
1020- textR .width -= lsb ;
1021- }
1022- rsb = SwingUtilities2 .getRightSideBearing (c , fm , text );
1023- if (rsb > 0 ) {
1024- textR .width += rsb ;
1025- }
1032+ // SwingJS lsb = SwingUtilities2.getLeftSideBearing(c, fm, text);
1033+ // if (lsb < 0) {
1034+ // textR.width -= lsb;
1035+ // }
1036+ // rsb = SwingUtilities2.getRightSideBearing(c, fm, text);
1037+ // if (rsb > 0) {
1038+ // textR.width += rsb;
1039+ // }
10261040
10271041 if (textR .width > availTextWidth ) {
10281042 text = SwingUtilities2 .clipString (c , fm , text ,
10291043 availTextWidth );
10301044 textR .width = SwingUtilities2 .stringWidth (c , fm , text );
10311045 }
1032- textR .height = fm .getHeight ();
1046+ // textR.height = fm.getHeight();
10331047 }
10341048 }
10351049
@@ -1041,13 +1055,14 @@ private static String layoutCompoundLabelImpl(
10411055 if (verticalTextPosition == TOP ) {
10421056 if (horizontalTextPosition != CENTER ) {
10431057 textR .y = 0 ;
1058+ // textR.y = iconR.height - textR.height;//(int) (4 * textR.height/54f);//- textR.height;
10441059 }
10451060 else {
10461061 textR .y = -(textR .height + gap );
10471062 }
10481063 }
1049- else if (verticalTextPosition == CENTER ) {
1050- textR .y = (iconR .height / 2 ) - (textR .height / 2 );
1064+ else if (verticalTextPosition == CENTER ) { // 16 pt max; height is 18, ascent is 12, actually
1065+ textR .y = (iconR .height / 2 ) - (textR .height / 2 );
10511066 }
10521067 else { // (verticalTextPosition == BOTTOM)
10531068 if (horizontalTextPosition != CENTER ) {
@@ -1118,17 +1133,24 @@ else if (horizontalAlignment == RIGHT) {
11181133 iconR .x += dx ;
11191134 iconR .y += dy ;
11201135
1121- if (lsb < 0 ) {
1122- // lsb is negative. Shift the x location so that the text is
1123- // visually drawn at the right location.
1124- textR .x -= lsb ;
1136+ //SwingJS if (lsb < 0) {
1137+ // // lsb is negative. Shift the x location so that the text is
1138+ // // visually drawn at the right location.
1139+ // textR.x -= lsb;
1140+ //
1141+ // textR.width += lsb;
1142+ // }
1143+ // if (rsb > 0) {
1144+ // textR.width -= rsb;
1145+ // }
11251146
1126- textR .width += lsb ;
1147+ if (viewR .width == Short .MAX_VALUE ) {
1148+ // SwingJS, for JSGraphicsUtil setting preferred button size;
1149+ viewR .width = labelR_width ;
1150+ viewR .height = labelR_height ;
11271151 }
1128- if (rsb > 0 ) {
1129- textR .width -= rsb ;
1130- }
1131-
1152+
1153+ System .out .println ("SwingUtil tiv " + text + "\n " + textR + "\n " + iconR + "\n " + viewR + "\n " );
11321154 return text ;
11331155 }
11341156
0 commit comments