Skip to content

Commit

Permalink
Improve "-align"
Browse files Browse the repository at this point in the history
  • Loading branch information
jan.nijtmans committed Jan 19, 2024
1 parent 7d01290 commit f17310a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 28 deletions.
9 changes: 5 additions & 4 deletions generic/tk.h
Original file line number Diff line number Diff line change
Expand Up @@ -1552,8 +1552,7 @@ typedef struct Tk_ElementSpec {
*----------------------------------------------------------------------
*
* The definitions below provide backward compatibility for functions and
* types related to event handling that used to be in Tk but have moved to
* Tcl.
* types that used to be in Tk but have moved to Tcl.
*
*----------------------------------------------------------------------
*/
Expand Down Expand Up @@ -1592,6 +1591,10 @@ typedef struct Tk_ElementSpec {
#define Tk_FreeProc Tcl_FreeProc
#define Tk_Preserve Tcl_Preserve
#define Tk_Release Tcl_Release

/* Related to USE_OLD_IMAGE: */

#define Tk_InitImageArgs(interp, argc, argv) /**/
#endif

/* Removed Tk_Main, use macro instead */
Expand All @@ -1611,8 +1614,6 @@ EXTERN const char * Tk_PkgInitStubsCheck(Tcl_Interp *interp,
#define Tk_InitStubs(interp, version, exact) \
Tk_PkgInitStubsCheck(interp, version, exact)
#endif /* USE_TK_STUBS */

#define Tk_InitImageArgs(interp, argc, argv) /**/

/*
*----------------------------------------------------------------------
Expand Down
8 changes: 6 additions & 2 deletions generic/tkText.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ typedef struct TkTextEmbWindowClient {
struct TkTextEmbWindowClient *next;
} TkTextEmbWindowClient;

typedef enum {
TK_ALIGN_BASELINE, TK_ALIGN_BOTTOM, TK_ALIGN_CENTER, TK_ALIGN_TOP
} TkAlignMode;

typedef struct TkTextEmbWindow {
struct TkSharedText *sharedTextPtr;
/* Information about the shared portion of the
Expand All @@ -112,7 +116,7 @@ typedef struct TkTextEmbWindow {
* window. */
char *create; /* Script to create window on-demand. NULL
* means no such script. Malloc-ed. */
int align; /* How to align window in vertical space. See
TkAlignMode align; /* How to align window in vertical space. See
* definitions in tkTextWind.c. */
int padX, padY; /* Padding to leave around each side of
* window, in pixels. */
Expand Down Expand Up @@ -144,7 +148,7 @@ typedef struct TkTextEmbImage {
* the image. */
Tk_Image image; /* Image for this segment. NULL means that the
* image hasn't been created yet. */
int align; /* How to align image in vertical space. See
TkAlignMode align; /* How to align image in vertical space. See
* definitions in tkTextImage.c. */
int padX, padY; /* Padding to leave around each side of image,
* in pixels. */
Expand Down
18 changes: 7 additions & 11 deletions generic/tkTextImage.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,14 @@ static const char *const alignStrings[] = {
"baseline", "bottom", "center", "top", NULL
};

typedef enum {
ALIGN_BASELINE, ALIGN_BOTTOM, ALIGN_CENTER, ALIGN_TOP
} alignMode;

/*
* Information used for parsing image configuration options:
*/

static const Tk_OptionSpec optionSpecs[] = {
{TK_OPTION_STRING_TABLE, "-align", NULL, NULL,
"center", TCL_INDEX_NONE, offsetof(TkTextEmbImage, align),
0, alignStrings, 0},
TK_OPTION_ENUM_VAR, alignStrings, 0},
{TK_OPTION_PIXELS, "-padx", NULL, NULL,
"0", TCL_INDEX_NONE, offsetof(TkTextEmbImage, padX), 0, 0, 0},
{TK_OPTION_PIXELS, "-pady", NULL, NULL,
Expand Down Expand Up @@ -255,7 +251,7 @@ TkTextImageCmd(
eiPtr->body.ei.imageString = NULL;
eiPtr->body.ei.name = NULL;
eiPtr->body.ei.image = NULL;
eiPtr->body.ei.align = ALIGN_CENTER;
eiPtr->body.ei.align = TK_ALIGN_CENTER;
eiPtr->body.ei.padX = eiPtr->body.ei.padY = 0;
eiPtr->body.ei.chunkCount = 0;
eiPtr->body.ei.optionTable = Tk_CreateOptionTable(interp, optionSpecs);
Expand Down Expand Up @@ -566,7 +562,7 @@ EmbImageLayoutProc(
chunkPtr->measureProc = NULL;
chunkPtr->bboxProc = EmbImageBboxProc;
chunkPtr->numBytes = 1;
if (eiPtr->body.ei.align == ALIGN_BASELINE) {
if (eiPtr->body.ei.align == TK_ALIGN_BASELINE) {
chunkPtr->minAscent = height - eiPtr->body.ei.padY;
chunkPtr->minDescent = eiPtr->body.ei.padY;
chunkPtr->minHeight = 0;
Expand Down Expand Up @@ -728,16 +724,16 @@ EmbImageBboxProc(
*xPtr = chunkPtr->x + eiPtr->body.ei.padX;

switch (eiPtr->body.ei.align) {
case ALIGN_BOTTOM:
case TK_ALIGN_BOTTOM:
*yPtr = y + (lineHeight - *heightPtr - eiPtr->body.ei.padY);
break;
case ALIGN_CENTER:
case TK_ALIGN_CENTER:
*yPtr = y + (lineHeight - *heightPtr)/2;
break;
case ALIGN_TOP:
case TK_ALIGN_TOP:
*yPtr = y + eiPtr->body.ei.padY;
break;
case ALIGN_BASELINE:
case TK_ALIGN_BASELINE:
*yPtr = y + (baseline - *heightPtr);
break;
}
Expand Down
18 changes: 7 additions & 11 deletions generic/tkTextWind.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ static const char *const alignStrings[] = {
"baseline", "bottom", "center", "top", NULL
};

typedef enum {
ALIGN_BASELINE, ALIGN_BOTTOM, ALIGN_CENTER, ALIGN_TOP
} alignMode;

/*
* Information used for parsing window configuration options:
*/
Expand Down Expand Up @@ -295,7 +291,7 @@ TkTextWindowCmd(
ewPtr->body.ew.linePtr = NULL;
ewPtr->body.ew.tkwin = NULL;
ewPtr->body.ew.create = NULL;
ewPtr->body.ew.align = ALIGN_CENTER;
ewPtr->body.ew.align = TK_ALIGN_CENTER;
ewPtr->body.ew.padX = ewPtr->body.ew.padY = 0;
ewPtr->body.ew.stretch = 0;
ewPtr->body.ew.optionTable = Tk_CreateOptionTable(interp, optionSpecs);
Expand Down Expand Up @@ -1009,7 +1005,7 @@ EmbWinLayoutProc(
chunkPtr->measureProc = NULL;
chunkPtr->bboxProc = EmbWinBboxProc;
chunkPtr->numBytes = 1;
if (ewPtr->body.ew.align == ALIGN_BASELINE) {
if (ewPtr->body.ew.align == TK_ALIGN_BASELINE) {
chunkPtr->minAscent = height - ewPtr->body.ew.padY;
chunkPtr->minDescent = ewPtr->body.ew.padY;
chunkPtr->minHeight = 0;
Expand Down Expand Up @@ -1257,23 +1253,23 @@ EmbWinBboxProc(
}
*xPtr = chunkPtr->x + ewPtr->body.ew.padX;
if (ewPtr->body.ew.stretch) {
if (ewPtr->body.ew.align == ALIGN_BASELINE) {
if (ewPtr->body.ew.align == TK_ALIGN_BASELINE) {
*heightPtr = baseline - ewPtr->body.ew.padY;
} else {
*heightPtr = lineHeight - 2*ewPtr->body.ew.padY;
}
}
switch (ewPtr->body.ew.align) {
case ALIGN_BOTTOM:
case TK_ALIGN_BOTTOM:
*yPtr = y + (lineHeight - *heightPtr - ewPtr->body.ew.padY);
break;
case ALIGN_CENTER:
case TK_ALIGN_CENTER:
*yPtr = y + (lineHeight - *heightPtr)/2;
break;
case ALIGN_TOP:
case TK_ALIGN_TOP:
*yPtr = y + ewPtr->body.ew.padY;
break;
case ALIGN_BASELINE:
case TK_ALIGN_BASELINE:
*yPtr = y + (baseline - *heightPtr);
break;
}
Expand Down

0 comments on commit f17310a

Please sign in to comment.