From e1edcbabb63e65d5d47c07a0836dd51d7e01de0e Mon Sep 17 00:00:00 2001 From: Eric Mullins Date: Fri, 16 Oct 2009 22:54:12 -0600 Subject: [PATCH] Added internal wrapping ability. New options: -wrapContinuationSequence "foo" (default: "\\ " -useInternalWrap (Winboard default: false, XBoard default: true) Note: -keepLineBreaksICS if set, prevents wrapping, usurping the useInternalWrap setting. The idea is if you want to keep ICS line breaks, then you don't want internal wrapping. Because of this, the new default for keepLineBreaksICS is now false for XBoard. --- backend.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ backend.h | 3 + common.h | 2 + winboard/winboard.c | 113 ++++++++++++++++++++++++++++++++++++-------------- xboard.c | 33 ++++++++++++++- 5 files changed, 225 insertions(+), 34 deletions(-) diff --git a/backend.c b/backend.c index 06796c1..5b8dcfa 100755 --- a/backend.c +++ b/backend.c @@ -1042,6 +1042,7 @@ InitBackEnd2() fprintf(debugFP, "%s\n", programVersion); } + set_cont_sequence(appData.wrapContSeq); if (appData.matchGames > 0) { appData.matchMode = TRUE; } else if (appData.matchMode) { @@ -13968,3 +13969,110 @@ EditPositionPasteFEN(char *fen) } } } + +static char cseq[12] = "\\ "; + +Boolean set_cont_sequence(char *new_seq) +{ + int len; + Boolean ret; + + // handle bad attempts to set the sequence + if (!new_seq) + return 0; // acceptable error - no debug + + len = strlen(new_seq); + ret = (len > 0) && (len < sizeof(cseq)); + if (ret) + strcpy(cseq, new_seq); + else if (appData.debugMode) + fprintf(debugFP, "Invalid continuation sequence \"%s\" (maximum length is: %d)\n", new_seq, sizeof(cseq)-1); + return ret; +} + +/* + reformat a source message so words don't cross the width boundary. internal + newlines are not removed. returns the wrapped size (no null character unless + included in source message). If dest is NULL, only calculate the size required + for the dest buffer. lp argument indicats line position upon entry, and it's + passed back upon exit. +*/ +int wrap(char *dest, char *src, int count, int width, int *lp) +{ + int len, i, ansi, cseq_len, line, old_line, old_i, old_len, clen; + + cseq_len = strlen(cseq); + old_line = line = *lp; + ansi = len = clen = 0; + + for (i=0; i < count; i++) + { + if (src[i] == '\033') + ansi = 1; + + // if we hit the width, back up + if (!ansi && (line >= width) && src[i] != '\n' && src[i] != ' ') + { + // store i & len in case the word is too long + old_i = i, old_len = len; + + // find the end of the last word + while (i && src[i] != ' ' && src[i] != '\n') + { + i--; + len--; + } + + // word too long? restore i & len before splitting it + if ((old_i-i+clen) >= width) + { + i = old_i; + len = old_len; + } + + // extra space? + if (i && src[i-1] == ' ') + len--; + + if (src[i] != ' ' && src[i] != '\n') + { + i--; + if (len) + len--; + } + + // now append the newline and continuation sequence + if (dest) + dest[len] = '\n'; + len++; + if (dest) + strncpy(dest+len, cseq, cseq_len); + len += cseq_len; + line = cseq_len; + clen = cseq_len; + continue; + } + + if (dest) + dest[len] = src[i]; + len++; + if (!ansi) + line++; + if (src[i] == '\n') + line = 0; + if (src[i] == 'm') + ansi = 0; + } + if (dest && appData.debugMode) + { + fprintf(debugFP, "wrap(count:%d,width:%d,line:%d,len:%d,*lp:%d,src: ", + count, width, line, len, *lp); + show_bytes(debugFP, src, count); + fprintf(debugFP, "\ndest: "); + show_bytes(debugFP, dest, len); + fprintf(debugFP, "\n"); + } + *lp = dest ? line : old_line; + + return len; +} diff --git a/backend.h b/backend.h index 0d6b3a4..b44c45a 100644 --- a/backend.h +++ b/backend.h @@ -259,6 +259,9 @@ char * GameListLineFull P(( int, GameInfo *)); extern char* StripHighlight P((char *)); /* returns static data */ extern char* StripHighlightAndTitle P((char *)); /* returns static data */ extern void ics_update_width P((int new_width)); +extern Boolean set_cont_sequence P((char *new_seq)); +extern int wrap P((char *dest, char *src, int count, int width, int *lp)); + typedef enum { CheckBox, ComboBox, TextBox, Button, Spin, ResetButton, SaveButton, FileName, PathName, Slider, Message } Control; diff --git a/common.h b/common.h index 9ab3048..a48aee5 100644 --- a/common.h +++ b/common.h @@ -603,6 +603,8 @@ typedef struct { Boolean keepAlive; /* [HGM] alive */ Boolean forceIllegal;/*[HGM] illegal */ Boolean noJoin; /* [HGM] join */ + char *wrapContSeq; /* continuation sequence when xboard wraps text */ + Boolean useInternalWrap; /* use internal wrapping -- noJoin usurps this if set */ } AppData, *AppDataPtr; /* [AS] PGN tags (for showing in the game list) */ diff --git a/winboard/winboard.c b/winboard/winboard.c index 4ab4f93..62d5072 100644 --- a/winboard/winboard.c +++ b/winboard/winboard.c @@ -1393,7 +1393,9 @@ ArgDescriptor argDescriptors[] = { { "secondNPS", ArgInt, (LPVOID) &appData.secondNPS, FALSE }, { "noGUI", ArgTrue, (LPVOID) &appData.noGUI, FALSE }, { "keepLineBreaksICS", ArgBoolean, (LPVOID) &appData.noJoin, TRUE }, - + { "wrapContinuationSequence", ArgString, (LPVOID) &appData.wrapContSeq, FALSE }, + { "useInternalWrap", ArgTrue, (LPVOID) &appData.useInternalWrap, FALSE }, /* noJoin usurps this if set */ + // [HGM] placement: put all window layouts last in ini file, but man X,Y before all others { "minX", ArgZ, (LPVOID) &minX, FALSE }, // [HGM] placement: to make suer auxialary windows can be placed { "minY", ArgZ, (LPVOID) &minY, FALSE }, @@ -5277,34 +5279,55 @@ LoadGameDialog(HWND hwnd, char* title) } } +int get_term_width() +{ + HDC hdc; + TEXTMETRIC tm; + RECT rc; + HFONT hfont, hold_font; + LOGFONT lf; + HWND hText; + + if (hwndConsole) + hText = GetDlgItem(hwndConsole, OPT_ConsoleText); + else + return 79; + + // get the text metrics + hdc = GetDC(hText); + lf = font[boardSize][CONSOLE_FONT]->lf; + if (consoleCF.dwEffects & CFE_BOLD) + lf.lfWeight = FW_BOLD; + if (consoleCF.dwEffects & CFE_ITALIC) + lf.lfItalic = TRUE; + if (consoleCF.dwEffects & CFE_STRIKEOUT) + lf.lfStrikeOut = TRUE; + if (consoleCF.dwEffects & CFE_UNDERLINE) + lf.lfUnderline = TRUE; + hfont = CreateFontIndirect(&lf); + hold_font = SelectObject(hdc, hfont); + GetTextMetrics(hdc, &tm); + SelectObject(hdc, hold_font); + DeleteObject(hfont); + ReleaseDC(hText, hdc); + + // get the rectangle + SendMessage(hText, EM_GETRECT, 0, (LPARAM)&rc); + + return (rc.right-rc.left) / tm.tmAveCharWidth; +} + void UpdateICSWidth(HWND hText) { - HDC hdc; - TEXTMETRIC tm; - RECT rc; - HFONT hfont, hold_font; - LONG old_width, new_width; - - // get the text metrics - hdc = GetDC(hText); - hfont = CreateFontIndirect(&font[boardSize][CONSOLE_FONT]->lf); - hold_font = SelectObject(hdc, hfont); - GetTextMetrics(hdc, &tm); - SelectObject(hdc, hold_font); - DeleteObject(hfont); - ReleaseDC(hText, hdc); - - // get the rectangle - SendMessage(hText, EM_GETRECT, 0, (LPARAM)&rc); - - // update the width - new_width = (rc.right-rc.left) / tm.tmAveCharWidth; - old_width = GetWindowLong(hText, GWL_USERDATA); - if (new_width != old_width) - { - ics_update_width(new_width); - SetWindowLong(hText, GWL_USERDATA, new_width); - } + LONG old_width, new_width; + + new_width = get_term_width(hText, FALSE); + old_width = GetWindowLong(hText, GWL_USERDATA); + if (new_width != old_width) + { + ics_update_width(new_width); + SetWindowLong(hText, GWL_USERDATA, new_width); + } } VOID @@ -8026,7 +8049,7 @@ ConsoleWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) } // Allow hText to highlight URLs and send notifications on them - wMask = SendMessage(hText, EM_GETEVENTMASK, 0, 0L); + wMask = (WORD) SendMessage(hText, EM_GETEVENTMASK, 0, 0L); SendMessage(hText, EM_SETEVENTMASK, 0, wMask | ENM_LINK); SendMessage(hText, EM_AUTOURLDETECT, TRUE, 0L); SetWindowLong(hText, GWL_USERDATA, 79); // initialize the text window's width @@ -10340,6 +10363,11 @@ RemoveInputSource(InputSourceRef isr) } } +int no_wrap(char *message, int count) +{ + ConsoleOutput(message, count, FALSE); + return count; +} int OutputToProcess(ProcRef pr, char *message, int count, int *outError) @@ -10348,11 +10376,32 @@ OutputToProcess(ProcRef pr, char *message, int count, int *outError) int outCount = SOCKET_ERROR; ChildProc *cp = (ChildProc *) pr; static OVERLAPPED ovl; + static int line = 0; - if (pr == NoProc) { - ConsoleOutput(message, count, FALSE); - return count; - } + if (pr == NoProc) + { + if (appData.noJoin || !appData.useInternalWrap) + return no_wrap(message, count); + else + { + int width = get_term_width(); + int len = wrap(NULL, message, count, width, &line); + char *msg = malloc(len); + int dbgchk; + + if (!msg) + return no_wrap(message, count); + else + { + dbgchk = wrap(msg, message, count, width, &line); + if (dbgchk != len && appData.debugMode) + fprintf(debugFP, "wrap(): dbgchk(%d) != len(%d)\n", dbgchk, len); + ConsoleOutput(msg, len, FALSE); + free(msg); + return len; + } + } + } if (ovl.hEvent == NULL) { ovl.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); diff --git a/xboard.c b/xboard.c index 59221fc..911f005 100644 --- a/xboard.c +++ b/xboard.c @@ -1409,6 +1409,12 @@ XtResource clientResources[] = { XtRImmediate, (XtPointer) False}, { "keepLineBreaksICS", "keepLineBreaksICS", XtRBoolean, sizeof(Boolean), XtOffset(AppDataPtr, noJoin), + XtRImmediate, (XtPointer) False}, + { "wrapContinuationSequence", "wrapContinuationSequence", XtRString, + sizeof(String), XtOffset(AppDataPtr, wrapContSeq), + XtRString, ""}, + { "useInternalWrap", "useInternalWrap", XtRBoolean, + sizeof(Boolean), XtOffset(AppDataPtr, useInternalWrap), XtRImmediate, (XtPointer) True}, }; @@ -1780,9 +1786,10 @@ XrmOptionDescRec shellOptions[] = { { "-keepAlive", "keepAlive", XrmoptionSepArg, NULL }, { "-forceIllegalMoves", "forceIllegalMoves", XrmoptionNoArg, "True" }, { "-keepLineBreaksICS", "keepLineBreaksICS", XrmoptionSepArg, NULL }, + { "-wrapContinuationSequence", "wrapContinuationSequence", XrmoptionSepArg, NULL }, + { "-useInternalWrap", "useInternalWrap", XrmoptionSepArg, NULL }, }; - XtActionsRec boardActions[] = { { "DrawPosition", DrawPositionProc }, { "HandleUserMove", HandleUserMove }, @@ -8506,11 +8513,33 @@ int OutputToProcess(pr, message, count, outError) int count; int *outError; { + static int line = 0; ChildProc *cp = (ChildProc *) pr; int outCount; if (pr == NoProc) - outCount = fwrite(message, 1, count, stdout); + { + if (appData.noJoin || !appData.useInternalWrap) + outCount = fwrite(message, 1, count, stdout); + else + { + int width = get_term_width(); + int len = wrap(NULL, message, count, width, &line); + char *msg = malloc(len); + int dbgchk; + + if (!msg) + outCount = fwrite(message, 1, count, stdout); + else + { + dbgchk = wrap(msg, message, count, width, &line); + if (dbgchk != len && appData.debugMode) + fprintf(debugFP, "wrap(): dbgchk(%d) != len(%d)\n", dbgchk, len); + outCount = fwrite(msg, 1, dbgchk, stdout); + free(msg); + } + } + } else outCount = write(cp->fdTo, message, count); -- 1.7.0.4