Scrolling in a text buffer

This is a discussion on Scrolling in a text buffer within the C Programming forums, part of the General Programming Boards category; Hello, this is my first post here. I'm writing a text file viewer for DOS using Turbo C. My code ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    5

    Scrolling in a text buffer

    Hello, this is my first post here. I'm writing a text file viewer for DOS using Turbo C. My code copies the contents of a text file into a buffer & displays in a text window. I can't figure out how to scroll the viewable area up and down. any ideas?

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    That's one little question that bugged me for quite a while. You know what? I never solved it, and have never needed to since.


    ^^^ Disclaimer: I know this post had no usefull content and I should be slapped. To avoid possible retaliation, I disown all knowledge that I ever posted it
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Something like this?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    #define length(x) ( sizeof (x) / sizeof *(x) )
    
    enum {
      KEY_ESC = 27,
      ARROW_UP = 256 + 72,
      ARROW_DOWN = 256 + 80,
    };
    
    static const char *text[] = {
      "Line 1",
      "Line 2",
      "Line 3",
      "Line 4",
      "Line 5",
      "Line 6"
    };
    
    static int get_code ( void )
    {
      int ch = getch();
    
      if ( ch == 0 || ch == 224 )
        ch = 256 + getch();
    
      return ch;
    }
    
    static void redraw ( int start, int end )
    {
      int i;
    
      clrscr();
    
      for ( i = start; i < end; i++ )
        puts ( text[i] );
    }
    
    int main ( void )
    {
      int start = 0, end = 3;
      int op = 0;
    
      while ( redraw ( start, end ), ( op = get_code() ) != KEY_ESC ) {
        switch ( op ) {
        case ARROW_UP:
          if ( start == 0 ) {
            end = length ( text );
            start = end - 3;
          }
          else {
            --start;
            --end;
          }
          break;
        case ARROW_DOWN:
          if ( end == length ( text ) ) {
            start = 0;
            end = start + 3;
          }
          else {
            ++start;
            ++end;
          }
          break;
        default: break;
        }
      }
    
      return 0;
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    5

    Thumbs up

    Thanks Prelude, that code compiled and works great! I think i can learn from it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-09-2006, 07:49 PM
  2. help with scrolling text please
    By Nemini in forum Game Programming
    Replies: 4
    Last Post: 03-17-2005, 11:05 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 10:45 PM
  5. scrolling text
    By Shadow in forum C Programming
    Replies: 0
    Last Post: 05-20-2002, 10:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21