pageup/pagedown implementation using C language

Dear all, I am in the process of implementing pageup/pagedown feature in our consumer electronics project.The idea is to provide feature to the customers to that similar to viewing a single sms message in a mobile device.With in the given view area if the whole message does not fit,we need to provide the ability for users to scroll through the entire message using pageup/pagedown or key up and key down.In our case we have the whole contents to be displayed stored in a buffer.I am giving below one implementation I have tried:

Note:We are using only pure C code and dont have active x controls like tabs or pageup/down buttons as in .NET or VC++.

#include #include "string.h"

void pageup(); void pagedown(); static int pagecounter=0; /*bytes per page calculated by trial and error considering the viewable area in my device*/ int bytesperpage=161; int numpages=0; int contentremaining=0; char* customstringcopy=NULL; char arr[161]={0}; int main(int argc, char *argv[]) { int totlen=0; int pagenum=0;

char* customstring=NULL;

char* data="Bloodshed Dev-C++ is a full-featured Integrated Development Environment (IDE) for the C/C++ programming language. It uses Mingw port of GCC (GNU Compiler Collec."; char* stringdata="Bloodshed Dev-C++ is a full-featured Integrated Development Environment (IDE) for the C/C++ programming language. It uses Mingw port of GCC (GNU Compiler Collection) as it's compiler. Dev- C++ can also be used in combination with Cygwin or any other GCC based compiler."; char* stringdata1="the #bloodshed channel has recently been created on the Undernet IRC server. I will be please to talk with you there so feel free to join :) If you want have an IRC client you can get one for Windows at and for Linux at"; char* stringdata2="You can subscribe to the Dev-C++ mailing list (for asking and answering questions on Dev-C++ and C/C++ programming) by clicking here and filling out the subscribe form there.";

totlen=strlen(stringdata)+strlen(stringdata)+strlen(stringdata2); printf("total length is %d\n",totlen); printf("length of data is %d\n",strlen(data) ); customstring=(char*)(malloc)(totlen+4); customstringcopy=customstring; memset(customstring,0,totlen+4);

memcpy(customstring,stringdata,strlen(stringdata)); customstring=customstring+strlen(stringdata); customstring[0]='\n'; customstring++; /*printf("%s\n",stringdata); printf("%s\n",customstringcopy);*/

/*customstring[0]='\n'; customstring++;*/

memcpy(customstring,stringdata1,strlen(stringdata1)); customstring=customstring+strlen(stringdata1); customstring[0]='\n'; customstring++; /*printf("%s\n",stringdata1); printf("%s\n",customstringcopy);*/

memcpy(customstring,stringdata2,strlen(stringdata2)); customstring=customstring+strlen(stringdata2); customstring[0]='\n'; customstring++; /* printf("%s\n",stringdata2);*/ printf("%s\n",customstringcopy);

numpages=totlen/bytesperpage; printf("total number of pages is %d\n",numpages); contentremaining=(totlen)%(bytesperpage); if(contentremaining > 0) { numpages=numpages+1;

} printf("total number of pages is %d\n",numpages); for(pagenum=0;pagenum0;pagenum--) { pagedown(); } system("PAUSE"); return EXIT_SUCCESS; }

void pageup() {

if(pagecounter1) { pagecounter--; printf("pagecounter value is %d \n",pagecounter); customstringcopy=customstringcopy-(bytesperpage); memcpy(arr,customstringcopy,161); printf("%s\n\n",arr); }

} /* if(pagecounter>0) {

customstringcopy=customstringcopy- bytesperpage; memcpy(arr,customstringcopy,161); printf("%s\n\n",arr);

} */


Incase you find bugs in above code please let me know ways to fix it.

I believe there are much better ways then the one I have tried here.It would be helpful if some one could provide me some sample code for similar features you would have come across in your product or some sample links which shows me sample code on how to implement such a feature.

Note:I am looking for only C code and not C#,JAVA,.NET,VC++

Looking farward for all your replies and advanced thanks for the same, Regards, s.subbarayan

Reply to
Loading thread data ...

Here's another bit of free advice - posting the same long imprecise question three times with slightly different wording puts you on the fast-track to the killfiles of many Usenet regulars.

Reply to
David Brown

I found your code as unreadable as others have suggested. Perhap /temporarily/ use a text editor to shorten variable names then the logic might become clearer.

I've had a go at some pageup/pagedown code. (First time I've used C for this and was quite a painful experience, especially trying to read a single key using standard C. If you have a choice of using a rapid development language, then use it!)

I don't know about your display; the display here is filled up left to right, top to bottom, without caring about splitting lines in the middle of words, and assuming a fixed pitch font.

Also, I've split the logic into two: display the text using as input:

  • The text
  • A single number indicating where to start

And some logic which manipulates that number. (The example displaytext() function just rewrites everything; probably you will want to use extra logic to scroll most of the display where possible.)

#include #include #include

char *text="The #bloodshed channel has recently been " "created on the Undernet IRC server. I will be please to talk with you" " there so feel free to join :) If you want have an IRC client you can" " get one for Windows at and for Linux at";

#define rowchars 16 /* Chars displayed per row */ #define displayrows 4 /* Rows available on display */

int nlines; /* Lines required by entire message */ int startline; /* Current display starts at this line# (line 1 = top) */

void displaytext(char *text, int nlines, int startline);

int main(void) { char c; int quit=0;

nlines=(strlen(text)-1)/rowchars+1; startline=1;

while (! quit) { displaytext(text,nlines,startline);

printf("\nPress U-Up D-Down A-Pageup B-Pagedown H-Home E-End Q-Quit then Enter:\n"); BODGE1: c=getc(stdin);

switch (c) { case 'U': case 'u': if (startline>1) --startline; break;

case 'D': case 'd': if (startlinedisplayrows) n=displayrows;

/* clear display */ /* ... */ puts("--------------------------");

for (i=1; i

Reply to

Isn't it perfectly clear that the only clc-acceptable way to do this is:


(With, of course,all the usual extra stuff needed to make it compile and to pass muster in CLC. And, of course, some parsing of argv[]).

Reply to
Kenny McCormack

You're telling me you don't have a graphical display on this 'device' but are forced to implment it using a kind of Unix terminal?

I can't imagine a device that doesn't have a graphical color display these days, and for that you always need libraries, such as MicroWindows or some other free open-source GUI system. And with those, you don't need to implement a page-up / down since they are part of the textcontrols.

Reply to


Then I guess that you don't own or use (and have never seen) microwave ovens or clock-radios or computer printers or watches or mp3 players.

Your lack of imagination doesn't invalidate the OP's requirements. Instead, it points out how little you know.

Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
 Click to see the full signature
Reply to
Lew Pitcher

Google Groups went through another one of its fits. Users weren't getting new posts displayed. Bleah.


Reply to
Default User

A microwave or clock radio with a pageup / pagedown button? Lew, get out of your cave!

Reply to

Here is my suggestion:

/** @file scrolltest.c */ #define DEBUG #ifdef DEBUG #include #endif

#define DISPLAY_CHUNK 160

/* + test data */ static char *buffer = "Long data string to be scrolled. Please note" " there are no (char)0 elements in the string, which would allow " "use of strcat to combine buffers. " "If everything is in one buffer anyway, the length of the buffer is" " known, and the chunk size of output data is fix, then just one " "pointer to the current start position of the display is " "sufficient. " "The C compiler is assumed to combine separate strings into " "one long string, so for our test we skip any special code for " "just constructing a demo."; /* - test data */

static int curpos; static int minpos; static int maxpos; static char arr[DISPLAY_CHUNK + 1]; /* trailing (char)0 */

void update_display(){ /* force legal start position */ if (curpos < minpos) { curpos = minpos; } if (curpos > maxpos) { curpos = maxpos; } memcpy(arr, &buffer[curpos], DISPLAY_CHUNK); arr[DISPLAY_CHUNK]='\0'; #ifdef DEBUG printf("pos %d display content:\n%s\n", curpos, arr); #else output_to_display(arr); #endif }

void page_up(){ curpos -= DISPLAY_CHUNK; update_display(); }

void page_down(){ curpos += DISPLAY_CHUNK; update_display(); }

int main(int argc, char * argv[]){ /* required initialization */ curpos = 0; minpos = 0; maxpos = strlen(buffer) - DISPLAY_CHUNK; /* initial output */ update_display(); /* scroll to bottom */ while (curpos < maxpos) page_down(); /* scroll to top */ while (curpos > minpos) page_up(); } /* end of scrolltest.c **/

Have fun! :)

ssubbarayan wrote:

BUG: adding lengths of stringdata twice(!) plus of stringdata2

BUG: using variable for navigation but number literal for memcpy

WARN> I found your code as unreadable as others have suggested. Perhap

Recommendation: GNU indent v2.2.9 (with line length set to 68 chars) plus short variable names.

Reply to
Hagen Patzke

Hi, Yes that was the reason that thinking my question did not appear I posted it thrice!Sorry about it. Regards, s.subbarayan

Reply to

[code deleted]

Even in C you would have some graphics libraries. The scrolling you are looking for would be an easy application in most such libraries. Given this is an embedded device, you may have to implement your own. the key is to think of the different levels of abstraction. What does scrolling mean at the topmost level? What functions would you need? This is called top down programming.


Reply to
Ed Prochak

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.