Hi all,

I got a problem with "simultaneous" drawing two different areas and can't find 
a clue how to come along with.

The app consists of a class hierarchy like so:

Fl_Double_Window
        Group (derived from Fl_Group)
                Bar (derived from Fl_Widget)
                Scroll (derived from Fl_Scroll)
                        Canvas (derived from Fl_Group)

As you can see Canvas is to be scrolled within the containing Scroll class. 
Canvas contains vertical lines. Each time the draw() method of Canvas is called 
the lines are drawn. No problem so far.

The x position of each drawn line is to be displayed by the Bar class. It's 
positioned above the Scroll widget.
And this is what doesn't work: although the draw-method of Bar is called, no 
drawing happens. Only on resizing the window the last x position is drawn.
I guess my handling of damage and/or redraw is wrong, but I can't figure out 
how to do it right.
Can you help?
The code is shown below.

Don't wonder about this complete senseless application, it's only the most 
simplified version of a more complicated task.

Thanks a lot in advance
testalucida

#include <stdio.h>
#include <string.h>

#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Scroll.H>
#include <FL/fl_draw.H>

#define _menuFile (menu__menubar+0)

int g_cnt = 0;

class Bar;

Fl_Double_Window *_mainwin = NULL;
Bar * _bar = NULL;
int _colwidth = 100;

void linedrawn( int x );

/////////////////////////////////////////////////////////////////////////

class Canvas : public Fl_Group {
public:
        Canvas( int X, int Y, int W, int H ) : Fl_Group( X, Y, W, H, "canvas" ) 
{
                box(FL_FLAT_BOX);
                color( (Fl_Color)197 );
        }


protected:
        void draw() {
                fprintf( stderr, "%d) Canvas::draw(): x=%d, y=%d\n", ++g_cnt, 
x(), y() );
                Fl_Group::draw();
                drawLines();
        }
private:
        void drawLines() {
                int xx = x();
                int yy = y();
                int ww = w();
                int hh = h();

                int endY = yy + hh;
        fl_color( FL_BLACK );
        for( int dx = 0; dx <= ww; dx += _colwidth ) {
            int x = xx + dx;
            fl_line( x, yy, x, endY );
                        linedrawn( x );
        }
        }
};

/////////////////////////////////////////////////////////////////////////

class Scroll : public Fl_Scroll {
public:
        Scroll( int X, int Y, int W, int H ) : Fl_Scroll( X, Y, W, H, "scroll" 
) {
                box(FL_FLAT_BOX);
                color((Fl_Color)196);
                align(Fl_Align(FL_ALIGN_CENTER));
        }
protected:
        void draw() {
                fprintf( stderr, "%d) Scroll::draw(): x=%d, y=%d\n", ++g_cnt, 
x(), y() );
                Fl_Scroll::draw();
        }
};

/////////////////////////////////////////////////////////////////////////

class Bar : public Fl_Widget {
        char _buf[12];
public:
        Bar( int X, int Y, int W, int H ) : Fl_Widget( X, Y, W, H, "bar" ) {
                color( (Fl_Color)187 );
                //this->box( FL_FLAT_BOX );
                _bar = this;
        }

        void drawLine( int x ) {
                fprintf( stderr, "%d) Bar::drawLine(): x=%d\n", ++g_cnt, x );
                sprintf( _buf, "%d", x );

                damage( FL_DAMAGE_ALL );
                draw();
        }

protected:
        void draw() {
                fprintf( stderr, "%d) Bar::draw()\n", ++g_cnt );
                color( FL_BLACK );
                fl_draw( _buf, 50, y()+20 );
        }_pBar = new Bar( X, Y, W, 40 );

};

/////////////////////////////////////////////////////////////////////////

class Group : public Fl_Group {
private:
        Bar * _pBar;
        Scroll * _pScroll;
        Canvas * _pCanvas;
public:
        Group( int X, int Y, int W, int H ) : Fl_Group( X, Y, W, H, "group" ) {
                align(Fl_Align(FL_ALIGN_CENTER));

                _pBar = new Bar( X, Y, W, 40 );

                _pScroll = new Scroll( X, Y+50, W, H-50 );
                        _pCanvas = new Canvas( X, Y+50, 800, 800 );
                        _pCanvas->end();
                _pScroll->end();

                resizable( _pScroll );
                end();

        }

protected:
        void draw() {
                fprintf( stderr, "%d) Group::draw()\n", ++g_cnt );

                Fl_Group::draw();
                _pBar->redraw();
        }
};

void linedrawn( int x ) {
        _bar->drawLine( x );
}

//----------------------------------------------------//


Fl_Menu_Bar *_menubar=(Fl_Menu_Bar *)0;

Fl_Menu_Item menu__menubar[] = {
 {"File", 0,  0, 0, 0, FL_NORMAL_LABEL, 0, 14, 0},
 {0,0,0,0,0,0,0,0,0}
};


int main(int argc, char **argv) {
  { _mainwin = new Fl_Double_Window(500, 400);
    { _menubar = new Fl_Menu_Bar(0, 0, 500, 20, "menubar");
      _menubar->menu(menu__menubar);
    } // Fl_Menu_Bar* _menubar
    { Group * group = new Group(0, 20, 500, 380 );
      Fl_Group::current()->resizable( group );
    } // Fl_Group* _group
    _mainwin->end();
  } // Fl_Double_Window* _mainwin
  _mainwin->show(argc, argv);
  return Fl::run();
}


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

Reply via email to