From 3ceb8cb5709b8485f8d9b6b991bbd1863b306bb1 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Wed, 17 Feb 2010 17:49:03 +0100 Subject: [PATCH] Add support for scrolling in the other direction --- doc/variables.xml | 8 +++++--- src/scroll.cc | 39 ++++++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/doc/variables.xml b/doc/variables.xml index ba3daca1..88766447 100644 --- a/doc/variables.xml +++ b/doc/variables.xml @@ -3075,12 +3075,14 @@ - + - Scroll 'text' by 'step' characters showing + Scroll 'text' by 'step' characters to the left + or right (set 'direction' to 'left' or 'right') showing 'length' number of characters at the same time. The text may also contain variables. 'step' is optional and defaults - to 1 if not set. If a var creates output on multiple lines + to 1 if not set. 'direction' is optional and defaults to left + if not set. If a var creates output on multiple lines then the lines are placed behind each other separated with a '|'-sign. If you change the textcolor inside $scroll it will automatically have it's old value back at the end of diff --git a/src/scroll.cc b/src/scroll.cc index 505c8b47..2255a7b3 100644 --- a/src/scroll.cc +++ b/src/scroll.cc @@ -34,35 +34,49 @@ #include "text_object.h" #include +#define SCROLL_LEFT true +#define SCROLL_RIGHT false + struct scroll_data { char *text; unsigned int show; unsigned int step; - unsigned int start; + signed int start; long resetcolor; + bool direction; }; void parse_scroll_arg(struct text_object *obj, const char *arg, void *free_at_crash, char *free_at_crash2) { struct scroll_data *sd; int n1 = 0, n2 = 0; + char dirarg[6]; sd = (struct scroll_data *)malloc(sizeof(struct scroll_data)); memset(sd, 0, sizeof(struct scroll_data)); sd->resetcolor = get_current_text_color(); sd->step = 1; - if (!arg || sscanf(arg, "%u %n", &sd->show, &n1) <= 0) { + sd->direction = SCROLL_LEFT; + + if (arg && sscanf(arg, "%5s %n", dirarg, &n1) == 1) { + if (strcasecmp(dirarg, "right") == 0 || strcasecmp(dirarg, "r") == 0) + sd->direction = SCROLL_RIGHT; + else if ( strcasecmp(dirarg, "left") != 0 && strcasecmp(dirarg, "l") != 0) + n1 = 0; + } + + if (!arg || sscanf(arg + n1, "%u %n", &sd->show, &n2) <= 0) { free(sd); #ifdef BUILD_X11 free(obj->next); #endif free(free_at_crash2); - CRIT_ERR(obj, free_at_crash, "scroll needs arguments: [] "); + CRIT_ERR(obj, free_at_crash, "scroll needs arguments: [left|right] [] "); } + n1 += n2; - sscanf(arg + n1, "%u %n", &sd->step, &n2); - if (*(arg + n1 + n2)) { + if(sscanf(arg + n1, "%u %n", &sd->step, &n2) == 1) { n1 += n2; } else { sd->step = 1; @@ -135,7 +149,7 @@ void print_scroll(struct text_object *obj, char *p, int p_max_size) } p[j] = 0; //count colorchanges in front of the visible part and place that many colorchanges in front of the visible part - for(j = 0; j < sd->start; j++) { + for(j = 0; j < (unsigned) sd->start; j++) { if(buf[j] == SPECIAL_CHAR) frontcolorchanges++; } pwithcolors=(char*)malloc(strlen(p) + 1 + colorchanges - visibcolorchanges); @@ -153,9 +167,16 @@ void print_scroll(struct text_object *obj, char *p, int p_max_size) strcpy(p, pwithcolors); free(pwithcolors); //scroll - sd->start += sd->step; - if(buf[sd->start] == 0 || sd->start > strlen(&(buf[0]))){ - sd->start = 0; + if(sd->direction == SCROLL_LEFT) { + sd->start += sd->step; + if(buf[sd->start] == 0 || (unsigned) sd->start > strlen(&(buf[0]))) { + sd->start = 0; + } + } else { + if(sd->start < 1) { + sd->start = strlen(&(buf[0])); + } + sd->start -= sd->step; } #ifdef BUILD_X11 //reset color when scroll is finished