Tk Source Code

Artifact [aa99d970]
Login

Artifact aa99d970da7d53efbd829aaca32723704bad8f91:

Attachment "tk-lbjustify.patch" to ticket [2996762f] added by russty1971 2011-09-24 02:55:55.
diff -rup tk8.5.8_base/generic/tkListbox.c tk8.5.8/generic/tkListbox.c
--- tk8.5.8_base/generic/tkListbox.c	2009-03-03 18:54:11.000000000 -0500
+++ tk8.5.8/generic/tkListbox.c	2010-06-01 16:22:55.840370396 -0400
@@ -167,6 +167,7 @@ typedef struct {
     Pixmap gray;		/* Pixmap for displaying disabled text. */
     int flags;			/* Various flag bits: see below for
 				 * definitions. */
+    Tk_Justify justify;         /* Justification */
 } Listbox;
 
 /*
@@ -303,6 +304,8 @@ static const Tk_OptionSpec optionSpecs[]
     {TK_OPTION_STRING, "-listvariable", "listVariable", "Variable",
 	 DEF_LISTBOX_LIST_VARIABLE, -1, Tk_Offset(Listbox, listVarName),
 	 TK_OPTION_NULL_OK, 0, 0},
+    {TK_OPTION_JUSTIFY, "-justify", "justify", "Justify",
+	DEF_LISTBOX_JUSTIFY, -1, Tk_Offset(Listbox, justify), 0, 0, 0},
     {TK_OPTION_END, NULL, NULL, NULL, NULL, 0, -1, 0, 0, 0}
 };
 
@@ -430,6 +433,7 @@ static char *		ListboxListVarProc(Client
 			    const char *name2, int flags);
 static void		MigrateHashEntries(Tcl_HashTable *table,
 			    int first, int last, int offset);
+static int              GetMaxOffset(Listbox *listPtr);
 
 /*
  * The structure below defines button class behavior by means of procedures
@@ -542,6 +546,7 @@ Tk_ListboxObjCmd(
     listPtr->cursor		 = None;
     listPtr->state		 = STATE_NORMAL;
     listPtr->gray		 = None;
+    listPtr->justify             = TK_JUSTIFY_LEFT;
 
     /*
      * Keep a hold of the associated tkwin until we destroy the listbox,
@@ -1077,12 +1082,13 @@ ListboxBboxSubCmd(
     Listbox *listPtr,		/* Information about the listbox */
     int index)			/* Index of the element to get bbox info on */
 {
-    int lastVisibleIndex;
+    int lastVisibleIndex, maxOffset;
 
     /*
      * Determine the index of the last visible item in the listbox.
      */
 
+    maxOffset = GetMaxOffset(listPtr);
     lastVisibleIndex = listPtr->topIndex + listPtr->fullLines
 	    + listPtr->partialLine;
     if (listPtr->nElements < lastVisibleIndex) {
@@ -1113,7 +1119,13 @@ ListboxBboxSubCmd(
 	Tk_GetFontMetrics(listPtr->tkfont, &fm);
 	pixelWidth = Tk_TextWidth(listPtr->tkfont, stringRep, stringLen);
 
-	x = listPtr->inset + listPtr->selBorderWidth - listPtr->xOffset;
+        if (listPtr->justify == TK_JUSTIFY_RIGHT) {
+            x = listPtr->inset + listPtr->selBorderWidth + maxOffset - pixelWidth;
+        } else if (listPtr->justify == TK_JUSTIFY_CENTER) {
+            x = listPtr->inset + listPtr->selBorderWidth + (maxOffset/2) - (pixelWidth/2);
+        } else {
+            x = listPtr->inset + listPtr->selBorderWidth - listPtr->xOffset;
+        }
 	y = ((index - listPtr->topIndex)*listPtr->lineHeight)
 		+ listPtr->inset + listPtr->selBorderWidth;
 	sprintf(buf, "%d %d %d %d", x, y, pixelWidth, fm.linespace);
@@ -1240,19 +1252,30 @@ ListboxXviewSubCmd(
 
     int index, count, type, windowWidth, windowUnits;
     int offset = 0;		/* Initialized to stop gcc warnings. */
+    int maxOffset;
     double fraction, fraction2;
 
     windowWidth = Tk_Width(listPtr->tkwin)
 	    - 2*(listPtr->inset + listPtr->selBorderWidth);
+    maxOffset = GetMaxOffset(listPtr);
     if (objc == 2) {
 	if (listPtr->maxWidth == 0) {
 	    Tcl_SetResult(interp, "0.0 1.0", TCL_STATIC);
 	} else {
 	    char buf[TCL_DOUBLE_SPACE];
-
-	    fraction = listPtr->xOffset/((double) listPtr->maxWidth);
-	    fraction2 = (listPtr->xOffset + windowWidth)
-		    / ((double) listPtr->maxWidth);
+            if (listPtr->justify == TK_JUSTIFY_RIGHT) {
+                fraction = (maxOffset - listPtr->xOffset)/((double) listPtr->maxWidth);
+                fraction2 = (maxOffset - listPtr->xOffset + windowWidth)
+                        / ((double) listPtr->maxWidth);
+            } else if (listPtr->justify == TK_JUSTIFY_CENTER) {
+                fraction = ((maxOffset/2) + listPtr->xOffset)/((double) listPtr->maxWidth);
+                fraction2 = ((maxOffset/2) + listPtr->xOffset + windowWidth)
+                        / ((double) listPtr->maxWidth);
+            } else {
+	        fraction = listPtr->xOffset/((double) listPtr->maxWidth);
+	        fraction2 = (listPtr->xOffset + windowWidth)
+		        / ((double) listPtr->maxWidth);
+            }
 	    if (fraction2 > 1.0) {
 		fraction2 = 1.0;
 	    }
@@ -1265,26 +1288,51 @@ ListboxXviewSubCmd(
 	if (Tcl_GetIntFromObj(interp, objv[2], &index) != TCL_OK) {
 	    return TCL_ERROR;
 	}
-	ChangeListboxOffset(listPtr, index*listPtr->xScrollUnit);
+        if (listPtr->justify == TK_JUSTIFY_RIGHT) {
+            ChangeListboxOffset(listPtr, maxOffset - index*listPtr->xScrollUnit);
+        } else if (listPtr->justify == TK_JUSTIFY_CENTER) {
+            ChangeListboxOffset(listPtr, (maxOffset/2) - index*listPtr->xScrollUnit);
+        } else {
+	    ChangeListboxOffset(listPtr, index*listPtr->xScrollUnit);
+        }
     } else {
 	type = Tk_GetScrollInfoObj(interp, objc, objv, &fraction, &count);
 	switch (type) {
 	case TK_SCROLL_ERROR:
 	    return TCL_ERROR;
 	case TK_SCROLL_MOVETO:
-	    offset = (int) (fraction*listPtr->maxWidth + 0.5);
+            if (listPtr->justify == TK_JUSTIFY_RIGHT) {
+                offset = (int) maxOffset - (fraction*listPtr->maxWidth + 0.5);
+            } else if (listPtr->justify == TK_JUSTIFY_CENTER) {
+                offset = 0 - ((int) (maxOffset/2) - (fraction*listPtr->maxWidth + 0.5));
+            } else {
+	        offset = (int) (fraction*listPtr->maxWidth + 0.5);
+            }
 	    break;
 	case TK_SCROLL_PAGES:
 	    windowUnits = windowWidth/listPtr->xScrollUnit;
 	    if (windowUnits > 2) {
-		offset = listPtr->xOffset
-			+ count*listPtr->xScrollUnit*(windowUnits-2);
+                if (listPtr->justify == TK_JUSTIFY_RIGHT) {
+                    offset = listPtr->xOffset
+                            - count*listPtr->xScrollUnit*(windowUnits-2);
+                } else {
+		    offset = listPtr->xOffset
+                            + count*listPtr->xScrollUnit*(windowUnits-2);
+                }
 	    } else {
-		offset = listPtr->xOffset + count*listPtr->xScrollUnit;
+                if (listPtr->justify == TK_JUSTIFY_RIGHT) {
+                    offset = listPtr->xOffset - count*listPtr->xScrollUnit;
+                } else {
+		    offset = listPtr->xOffset + count*listPtr->xScrollUnit;
+                }
 	    }
 	    break;
 	case TK_SCROLL_UNITS:
-	    offset = listPtr->xOffset + count*listPtr->xScrollUnit;
+            if (listPtr->justify == TK_JUSTIFY_RIGHT) {
+                offset = listPtr->xOffset - count*listPtr->xScrollUnit;
+            } else {
+	        offset = listPtr->xOffset + count*listPtr->xScrollUnit;
+            }
 	    break;
 	}
 	ChangeListboxOffset(listPtr, offset);
@@ -1831,7 +1879,7 @@ DisplayListbox(
     register Listbox *listPtr = (Listbox *) clientData;
     register Tk_Window tkwin = listPtr->tkwin;
     GC gc;
-    int i, limit, x, y, prevSelected, freeGC, stringLen;
+    int i, limit, x, y, prevSelected, freeGC, stringLen, maxOffset;
     Tk_FontMetrics fm;
     Tcl_Obj *curElement;
     Tcl_HashEntry *entry;
@@ -1844,7 +1892,9 @@ DisplayListbox(
 				 * or right edge of the listbox is
 				 * off-screen. */
     Pixmap pixmap;
+    int totalLength, height;
 
+    maxOffset = GetMaxOffset(listPtr);
     listPtr->flags &= ~REDRAW_PENDING;
     if (listPtr->flags & LISTBOX_DELETED) {
 	return;
@@ -1900,12 +1950,30 @@ DisplayListbox(
 	limit = listPtr->nElements-1;
     }
     left = right = 0;
-    if (listPtr->xOffset > 0) {
-	left = listPtr->selBorderWidth+1;
-    }
-    if ((listPtr->maxWidth - listPtr->xOffset) > (Tk_Width(listPtr->tkwin)
-	    - 2*(listPtr->inset + listPtr->selBorderWidth))) {
-	right = listPtr->selBorderWidth+1;
+    if (listPtr->justify == TK_JUSTIFY_RIGHT) {
+        if ((maxOffset - listPtr->xOffset) > 0) {
+            left = listPtr->selBorderWidth+1;
+        }
+        if ((listPtr->maxWidth - (maxOffset - listPtr->xOffset)) > (Tk_Width(listPtr->tkwin)
+	        - 2*(listPtr->inset + listPtr->selBorderWidth))) {
+            right = listPtr->selBorderWidth+1;
+        }
+    } else if (listPtr->justify == TK_JUSTIFY_CENTER) {
+        if (((maxOffset/2) + listPtr->xOffset) > 0) {
+            left = listPtr->selBorderWidth+1;
+        }
+        if ((listPtr->maxWidth - ((maxOffset/2) + listPtr->xOffset)) > (Tk_Width(listPtr->tkwin)
+	        - 2*(listPtr->inset + listPtr->selBorderWidth))) {
+            right = listPtr->selBorderWidth+1;
+        }
+    } else {
+        if (listPtr->xOffset > 0) {
+	    left = listPtr->selBorderWidth+1;
+        }
+        if ((listPtr->maxWidth - listPtr->xOffset) > (Tk_Width(listPtr->tkwin)
+	        - 2*(listPtr->inset + listPtr->selBorderWidth))) {
+            right = listPtr->selBorderWidth+1;
+        }
     }
     prevSelected = 0;
 
@@ -2063,12 +2131,22 @@ DisplayListbox(
 	/*
 	 * Draw the actual text of this item.
 	 */
+        Tcl_ListObjIndex(listPtr->interp, listPtr->listObj, i, &curElement);
+        stringRep = Tcl_GetStringFromObj(curElement, &stringLen);
+        Tk_ComputeTextLayout(listPtr->tkfont,
+                stringRep, stringLen, 0,
+                listPtr->justify, TK_IGNORE_NEWLINES, &totalLength, &height);
 
 	Tk_GetFontMetrics(listPtr->tkfont, &fm);
 	y += fm.ascent + listPtr->selBorderWidth;
-	x = listPtr->inset + listPtr->selBorderWidth - listPtr->xOffset;
-	Tcl_ListObjIndex(listPtr->interp, listPtr->listObj, i, &curElement);
-	stringRep = Tcl_GetStringFromObj(curElement, &stringLen);
+
+        if (listPtr->justify == TK_JUSTIFY_LEFT) {
+            x = listPtr->inset + listPtr->selBorderWidth - listPtr->xOffset;
+        } else if (listPtr->justify == TK_JUSTIFY_RIGHT) {
+            x = width - totalLength - listPtr->inset - listPtr->selBorderWidth + listPtr->xOffset - 1;
+        } else {
+            x = (width + maxOffset)/2 - totalLength/2 - ((maxOffset/2) + listPtr->xOffset);
+        }
 	Tk_DrawChars(listPtr->display, pixmap, gc, listPtr->tkfont,
 		stringRep, stringLen, x, y);
 
@@ -2621,7 +2699,6 @@ ListboxEventProc(
 	listPtr->flags |= UPDATE_V_SCROLLBAR|UPDATE_H_SCROLLBAR;
 	ChangeListboxView(listPtr, listPtr->topIndex);
 	ChangeListboxOffset(listPtr, listPtr->xOffset);
-
 	/*
 	 * Redraw the whole listbox. It's hard to tell what needs to be
 	 * redrawn (e.g. if the listbox has shrunk then we may only need to
@@ -2854,16 +2931,31 @@ ChangeListboxOffset(
      * Add half a scroll unit to do entry/text-like synchronization. [Bug
      * #225025]
      */
-
-    offset += listPtr->xScrollUnit / 2;
-    maxOffset = listPtr->maxWidth - (Tk_Width(listPtr->tkwin) -
-	    2*listPtr->inset - 2*listPtr->selBorderWidth)
-	    + listPtr->xScrollUnit - 1;
-    if (offset > maxOffset) {
-	offset = maxOffset;
+    if (listPtr->justify != TK_JUSTIFY_CENTER) {
+        offset += listPtr->xScrollUnit / 2;
     }
-    if (offset < 0) {
-	offset = 0;
+    maxOffset = GetMaxOffset(listPtr);
+    if (listPtr->justify == TK_JUSTIFY_RIGHT) {
+        if ((maxOffset - offset) < 0) {
+            offset = maxOffset;
+        }
+        if (offset < 0) {
+            offset = 0;
+        }
+    } else if (listPtr->justify == TK_JUSTIFY_CENTER) {
+        if (((maxOffset/2) + offset) > maxOffset) {
+            offset = maxOffset / 2;
+        }
+        if (((maxOffset/2) + offset) < 0) {
+            offset = 0 - (maxOffset / 2);
+        }
+    } else {
+        if (offset > maxOffset) {
+	    offset = maxOffset;
+        }
+        if (offset < 0) {
+            offset = 0;
+        }
     }
     offset -= offset % listPtr->xScrollUnit;
     if (offset != listPtr->xOffset) {
@@ -2899,9 +2991,7 @@ ListboxScanTo(
     int newTopIndex, newOffset, maxIndex, maxOffset;
 
     maxIndex = listPtr->nElements - listPtr->fullLines;
-    maxOffset = listPtr->maxWidth + (listPtr->xScrollUnit - 1)
-	    - (Tk_Width(listPtr->tkwin) - 2*listPtr->inset
-	    - 2*listPtr->selBorderWidth - listPtr->xScrollUnit);
+    maxOffset = GetMaxOffset(listPtr);
 
     /*
      * Compute new top line for screen by amplifying the difference between
@@ -2930,13 +3020,33 @@ ListboxScanTo(
      * scan started.
      */
 
-    newOffset = listPtr->scanMarkXOffset - (10*(x - listPtr->scanMarkX));
-    if (newOffset > maxOffset) {
-	newOffset = listPtr->scanMarkXOffset = maxOffset;
-	listPtr->scanMarkX = x;
-    } else if (newOffset < 0) {
-	newOffset = listPtr->scanMarkXOffset = 0;
-	listPtr->scanMarkX = x;
+    if (listPtr->justify == TK_JUSTIFY_RIGHT) {
+        newOffset = listPtr->scanMarkXOffset + (10*(x - listPtr->scanMarkX));
+        if (newOffset > maxOffset) {
+            newOffset = listPtr->scanMarkXOffset = maxOffset;
+            listPtr->scanMarkX = x;
+        } else if (newOffset < 0) {
+            newOffset = listPtr->scanMarkXOffset = 0;
+            listPtr->scanMarkX = x;
+        }
+    } else if (listPtr->justify == TK_JUSTIFY_CENTER) {
+        newOffset = listPtr->scanMarkXOffset - (10*(x - listPtr->scanMarkX));
+        if (newOffset > (maxOffset/2)) {
+            newOffset = listPtr->scanMarkXOffset = maxOffset/2;
+            listPtr->scanMarkX = x;
+        }
+        if ((maxOffset/2) + newOffset < 0) {
+            newOffset = listPtr->scanMarkXOffset = 0 - maxOffset/2;
+        }
+    } else {
+        newOffset = listPtr->scanMarkXOffset - (10*(x - listPtr->scanMarkX));
+        if (newOffset > maxOffset) {
+            newOffset = listPtr->scanMarkXOffset = maxOffset;
+            listPtr->scanMarkX = x;
+        } else if (newOffset < 0) {
+            newOffset = listPtr->scanMarkXOffset = 0;
+            listPtr->scanMarkX = x;
+        }
     }
     ChangeListboxOffset(listPtr, newOffset);
 }
@@ -3314,7 +3424,7 @@ ListboxUpdateHScrollbar(
     register Listbox *listPtr)	/* Information about widget. */
 {
     char firstStr[TCL_DOUBLE_SPACE+1], lastStr[TCL_DOUBLE_SPACE+1];
-    int result, windowWidth;
+    int result, windowWidth, maxOffset;
     double first, last;
     Tcl_Interp *interp;
 
@@ -3328,9 +3438,18 @@ ListboxUpdateHScrollbar(
 	last = 1.0;
     } else {
 	register double maxWide = (double) listPtr->maxWidth;
+        maxOffset = GetMaxOffset(listPtr);
 
-	first = listPtr->xOffset / maxWide;
-	last = (listPtr->xOffset + windowWidth) / maxWide;
+        if (listPtr->justify == TK_JUSTIFY_RIGHT) {
+            first = (maxOffset - listPtr->xOffset) / maxWide;
+            last = ((maxOffset - listPtr->xOffset) + windowWidth) / maxWide;
+        } else if (listPtr->justify == TK_JUSTIFY_CENTER) {
+                first = ((maxOffset/2) + listPtr->xOffset) / maxWide;
+                last = (((maxOffset/2) + listPtr->xOffset) + windowWidth) / maxWide;
+        } else {
+	    first = listPtr->xOffset / maxWide;
+	    last = (listPtr->xOffset + windowWidth) / maxWide;
+        }
 	if (last > 1.0) {
 	    last = 1.0;
 	}
@@ -3553,6 +3672,32 @@ MigrateHashEntries(
 }
 
 /*
+ *----------------------------------------------------------------------
+ *
+ * GetMaxOffset --
+ *
+ * Passing in a listbox pointer, returns the maximum offset for the box
+ *
+ * Results:
+ *       Listbox's maxOffset
+ *
+ * Side effects:
+ *       None
+ *
+ *----------------------------------------------------------------------
+*/
+static int GetMaxOffset(register Listbox *listPtr)
+{
+    int maxOffset;
+
+    maxOffset = listPtr->maxWidth - (Tk_Width(listPtr->tkwin) - 2*listPtr->inset - 2*listPtr->selBorderWidth) + listPtr->xScrollUnit - 1;
+    if (maxOffset < 0) {
+        maxOffset = 0;
+    }
+
+    return maxOffset;
+}
+/*
  * Local Variables:
  * mode: c
  * c-basic-offset: 4
diff -rup tk8.5.8_base/unix/tkUnixDefault.h tk8.5.8/unix/tkUnixDefault.h
--- tk8.5.8_base/unix/tkUnixDefault.h	2007-12-13 10:28:50.000000000 -0500
+++ tk8.5.8/unix/tkUnixDefault.h	2010-05-27 13:31:42.722195996 -0400
@@ -226,6 +226,7 @@
 #define DEF_LISTBOX_RELIEF		"sunken"
 #define DEF_LISTBOX_SCROLL_COMMAND	""
 #define DEF_LISTBOX_LIST_VARIABLE	""
+#define DEF_LISTBOX_JUSTIFY             "left"
 #define DEF_LISTBOX_SELECT_COLOR	SELECT_BG
 #define DEF_LISTBOX_SELECT_MONO		BLACK
 #define DEF_LISTBOX_SELECT_BD		"0"