Tcl Source Code

Artifact [94159ff0ac]
Login

Artifact 94159ff0ac4b366b9886f7d9081547a7d8c9927d:

Attachment "tk-lbjustify.patch" to ticket [2996707fff] added by russty1971 2010-05-05 00:25:38.
diff -r -up tk8.5.6/generic/tkListbox.c tk8.5.6_lbjust/generic/tkListbox.c
--- tk8.5.6/generic/tkListbox.c	2008-10-09 15:20:45.000000000 -0600
+++ tk8.5.6_lbjust/generic/tkListbox.c	2010-05-04 10:40:23.248365433 -0600
@@ -167,6 +167,8 @@ typedef struct {
     Pixmap gray;		/* Pixmap for displaying disabled text. */
     int flags;			/* Various flag bits: see below for
 				 * definitions. */
+    Tk_Justify justify;         /* Justification */
+    int oldMaxOffset;           /* Used in scrolling for right/center justification */
 } Listbox;
 
 /*
@@ -303,6 +305,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 +434,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 +547,8 @@ Tk_ListboxObjCmd(
     listPtr->cursor		 = None;
     listPtr->state		 = STATE_NORMAL;
     listPtr->gray		 = None;
+    listPtr->justify             = TK_JUSTIFY_LEFT;
+    listPtr->oldMaxOffset        = 0;
 
     /*
      * Keep a hold of the associated tkwin until we destroy the listbox,
@@ -568,6 +575,13 @@ Tk_ListboxObjCmd(
 	return TCL_ERROR;
     }
 
+    if (listPtr->justify == TK_JUSTIFY_RIGHT) {
+        listPtr->xOffset = GetMaxOffset(listPtr);
+    } else if (listPtr->justify == TK_JUSTIFY_CENTER) {
+        listPtr->xOffset = GetMaxOffset(listPtr) / 2;
+        listPtr->xOffset -= listPtr->xOffset % listPtr->xScrollUnit;
+    }
+
     Tcl_SetResult(interp, Tk_PathName(listPtr->tkwin), TCL_STATIC);
     return TCL_OK;
 }
@@ -1112,8 +1126,7 @@ ListboxBboxSubCmd(
 	stringRep = Tcl_GetStringFromObj(el, &stringLen);
 	Tk_GetFontMetrics(listPtr->tkfont, &fm);
 	pixelWidth = Tk_TextWidth(listPtr->tkfont, stringRep, stringLen);
-
-	x = listPtr->inset + listPtr->selBorderWidth - listPtr->xOffset;
+        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);
@@ -1549,7 +1562,6 @@ ConfigureListbox(
     Tcl_Obj *oldListObj = NULL;
     Tcl_Obj *errorResult = NULL;
     int oldExport, error;
-
     oldExport = listPtr->exportSelection;
     if (listPtr->listVarName != NULL) {
 	Tcl_UntraceVar(interp, listPtr->listVarName,
@@ -1843,6 +1855,7 @@ DisplayListbox(
 				 * or right edge of the listbox is
 				 * off-screen. */
     Pixmap pixmap;
+    int totalLength, height;
 
     listPtr->flags &= ~REDRAW_PENDING;
     if (listPtr->flags & LISTBOX_DELETED) {
@@ -2062,12 +2075,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 + GetMaxOffset(listPtr) - 1;
+        } else {
+            x = (width + GetMaxOffset(listPtr))/2 - totalLength/2 - listPtr->xOffset;
+        }
 	Tk_DrawChars(listPtr->display, pixmap, gc, listPtr->tkfont,
 		stringRep, stringLen, x, y);
 
@@ -2588,6 +2611,7 @@ ListboxEventProc(
     ClientData clientData,	/* Information about window. */
     XEvent *eventPtr)		/* Information about event. */
 {
+    int tmpOffset, tmpOffset2, maxOffset;
     Listbox *listPtr = (Listbox *) clientData;
 
     if (eventPtr->type == Expose) {
@@ -2619,8 +2643,54 @@ ListboxEventProc(
 	}
 	listPtr->flags |= UPDATE_V_SCROLLBAR|UPDATE_H_SCROLLBAR;
 	ChangeListboxView(listPtr, listPtr->topIndex);
+        if (listPtr->justify == TK_JUSTIFY_RIGHT) {
+            maxOffset = GetMaxOffset(listPtr);
+            if (maxOffset != listPtr->oldMaxOffset && listPtr->oldMaxOffset > 0) {  // window has shrunk
+                if (maxOffset > listPtr->oldMaxOffset) {
+                    tmpOffset = maxOffset - listPtr->oldMaxOffset;
+                } else {
+                    tmpOffset = listPtr->oldMaxOffset - maxOffset;
+                }
+                tmpOffset -= tmpOffset % listPtr->xScrollUnit;
+                if ((tmpOffset + listPtr->xOffset) > maxOffset) {
+                    tmpOffset = maxOffset - listPtr->xOffset;
+                }
+                if (tmpOffset < 0) {
+                    tmpOffset = 0;
+                }
+                listPtr->xOffset += tmpOffset;
+            } else {
+                listPtr->xOffset = maxOffset;
+            }
+            listPtr->oldMaxOffset = maxOffset;
+        } else if (listPtr->justify == TK_JUSTIFY_CENTER) {
+            maxOffset = GetMaxOffset(listPtr);
+            if (maxOffset != listPtr->oldMaxOffset && listPtr->oldMaxOffset > 0) {  // window has shrunk
+                tmpOffset2 = maxOffset / 2;
+                if (maxOffset > listPtr->oldMaxOffset) {
+                    tmpOffset = maxOffset/2 - listPtr->oldMaxOffset/2;
+                } else {
+                    tmpOffset = listPtr->oldMaxOffset/2 - maxOffset/2;
+                }
+                tmpOffset -= tmpOffset % listPtr->xScrollUnit;
+                if ((tmpOffset + listPtr->xOffset) > maxOffset) {
+                    tmpOffset = maxOffset - listPtr->xOffset;
+                }
+                if (tmpOffset < 0) {
+                    tmpOffset = 0;
+                }
+                if (listPtr->xOffset < tmpOffset2) {
+                    listPtr->xOffset += tmpOffset;
+                } else {
+                    listPtr->xOffset -= tmpOffset;
+                }
+            } else {
+                listPtr->xOffset = maxOffset/2;
+                listPtr->xOffset -= listPtr->xOffset % listPtr->xScrollUnit;
+            }
+            listPtr->oldMaxOffset = maxOffset;
+        }
 	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
@@ -2853,7 +2923,6 @@ 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)
@@ -3322,6 +3391,7 @@ ListboxUpdateHScrollbar(
     }
     windowWidth = Tk_Width(listPtr->tkwin) - 2*(listPtr->inset
 	    + listPtr->selBorderWidth);
+
     if (listPtr->maxWidth == 0) {
 	first = 0;
 	last = 1.0;
@@ -3552,6 +3622,33 @@ 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;
+    }
+    maxOffset -= maxOffset % listPtr->xScrollUnit;
+
+    return maxOffset;
+}
+/*
  * Local Variables:
  * mode: c
  * c-basic-offset: 4
diff -r -up tk8.5.6/unix/tkUnixDefault.h tk8.5.6_lbjust/unix/tkUnixDefault.h
--- tk8.5.6/unix/tkUnixDefault.h	2007-12-13 08:28:50.000000000 -0700
+++ tk8.5.6_lbjust/unix/tkUnixDefault.h	2010-05-04 10:41:16.109366014 -0600
@@ -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"