All pastes #93667 Raw Edit

Someone

public text v1 · immutable
#93667 ·published 2006-07-20 17:26 UTC
rendered paste body
#include <FL/Fl_Shared_Image.H>
#include <Fl/Fl.H>
#include <Fl/Fl_Box.H>
#include <Fl/Fl_Double_Window.H>
#include <Fl/Fl_PNG_Image.H>
#include <Fl/Fl_Bitmap.H>
#include <Fl/Fl_Scroll.H>
#include <Fl/Fl_Window.H>

class dragbox : public Fl_Box {
  public:
    dragbox::dragbox() : Fl_Box(32, 32, 64, 64, 0UL)
    {
      color(FL_RED);
      box(FL_BORDER_FRAME);
      orig_img = 0UL;
      scaled_img = 0UL;
      align(FL_ALIGN_INSIDE | FL_ALIGN_TOP | FL_ALIGN_LEFT);
    }
    int dragbox::handle(int e)
    {
      switch (e) {
        case FL_PUSH: return 1;
        case FL_DRAG:
          get_wh();
          scale();
          parent()->redraw();
          break;
        case FL_RELEASE:
          get_wh();
          scale();
          scale_img();
          parent()->redraw();
          break;
        default:
          return Fl_Box::handle(e);
      }
      /* not reached */
      return 0;
    }
    void image(Fl_Image *ni)
    {
      orig_img = ni;
      Fl_Box::image(ni);
    }
  private:
    void get_wh()
    {
      nw = Fl::event_x() - Fl_Box::x();
      nh = Fl::event_y() - Fl_Box::y();
      if (nw < 64) nw = 64;
      if (nh < 64) nh = 64;
    }
    void scale()
    {
      Fl_Box::size(nw, nh); 
    }
    void scale_img()
    {
      if (scaled_img) {
        delete scaled_img;
        scaled_img = 0UL;
      }
      scaled_img = orig_img->copy(nw, nh);
      Fl_Box::image(scaled_img);
    }
    Fl_Image *orig_img;
    Fl_Image *scaled_img;
    int nw;
    int nh;
};

int main()
{
  fl_register_images();

  Fl_Double_Window win(800, 600);
  Fl_PNG_Image img("/tmp/thing2.png");
  dragbox box;

  box.image(&img);
  win.label("test2");
  win.end();
  win.show();
  return Fl::run();
}