I started learning how to use FLTK 1.1.7 last night, and I'm making good 
progress, but I'm getting hung up a bit trying to figure this out.

At the moment, I have a main window.  In the window I have an Fl_Scroll.  In 
the Fl_Scroll, I have an object of my own class (my_canvas) which I derived 
from an Fl_Widget.  I then draw grid lines on my_canvas.

What I really need is a scrollable area (my_canvas right now) where I can draw 
lines and bitmaps, so I guess my first question is, am I moving in the right 
direction as I have it structured now?

My second question is, what do I need to do so that while I'm scrolling, the 
lines on my_canvas get redrawn properly and don't get messed up?  I know I need 
to implement my own draw() and handle() functions (which I've tried to do), but 
while I scroll, the lines get drawn in at the wrong coordinates or otherwise 
end up looking wrong.

If anyone can give me some insight on the best way of implementing this, I'd 
really appreciate it.

Here's the code as I have it so far.  Note that it's my first attempt at 
learning FLTK and I was working on it late at night, so I'll be the first to 
admit it may have some oddities.  Also, I took out my attempt at the handle() 
function since it wasn't really working:

/*  PROJECT.CXX -------------------------------------- */
#include <stdio.h>
#include "project.h"

int main(int argc, char **argv)
{
  Fl::visual(FL_RGB);
  Fl_Double_Window w_main(800, 600);
  w_main.box(FL_FLAT_BOX);

  Fl_Scroll w_canvas(100, 100, 500, 400, NULL);
  w_canvas.box(FL_NO_BOX);

  my_canvas box1(0, 0, 1200, 1200, NULL);
  box1.grid_color = fl_rgb_color(0, 255, 255);
  box1.set_horiz_grid(20, 10);
  box1.set_vert_grid(30, 50);
  box1.box(FL_DOWN_BOX);
  box1.color(fl_rgb_color(0, 0, 128));

  w_main.end();
  w_main.show(argc, argv);

  return Fl::run();
}

/*  PROJECT.H -------------------------------------- */
#include <FL/Fl.H>
#include <FL/Fl_Draw.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Scroll.H>
#include <FL/Fl_Box.H>

#ifndef PROJECT_H
#define PROJECT_H

class my_canvas : public Fl_Widget
{
  private:
    void draw()
    {
      fl_rectf(0, 0, w(), h(), 0, 0, 128);
      draw_grids();
    }

    void draw_grids()
    {
      int grid_draw;
      if (grid_horiz)
      {
        fl_color(grid_color);
        grid_draw = grid_horiz_offset;
        while (grid_draw < h())
        {
          fl_line(0, grid_draw, w(), grid_draw);
          grid_draw = grid_draw + grid_horiz_spacing;
        }
      }

      if (grid_vert)
      {
        fl_color(grid_color);
        grid_draw = grid_vert_offset;
        while (grid_draw < w())
        {
          fl_line(grid_draw, 0, grid_draw, h());
          grid_draw = grid_draw + grid_vert_spacing;
        }
      }
    }

  public:
    int       event_x;
    int       event_y;
    Fl_Color  grid_color;
    bool      grid_horiz;
    bool      grid_vert;
    int       grid_horiz_offset;
    int       grid_vert_offset;
    int       grid_horiz_spacing;
    int       grid_vert_spacing;

    my_canvas(int X, int Y, int W, int H, const char * lbl = 0) : Fl_Widget(X, 
Y, W, H, lbl)
    {
    box(FL_FLAT_BOX);
    }

    void set_horiz_grid(int offset, int spacing)
    {
      if (offset >= 0 && spacing >= 0)
      {
        grid_horiz = true;
      }
      grid_horiz_offset = offset;
      grid_horiz_spacing = spacing;
    }

    void set_vert_grid(int offset, int spacing)
    {
      if (offset >= 0 && spacing >= 0)
      {
        grid_vert = true;
      }
      grid_vert_offset = offset;
      grid_vert_spacing = spacing;
    }
};

#endif

_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to