// src/cairo_widget.hh - Copyright 2005, 2006, University
//               of Padova, dept. of Pure and Applied
//               Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#ifndef CAIRO_WIDGET_HH
#define CAIRO_WIDGET_HH 1

#include "config.h"

#include <cairo.h>
#include <gtkmm/drawingarea.h>
#include <gtkmm/widget.h>
#include <gdkmm/pixmap.h>

namespace sgpem
{
  class CairoWidget : public Gtk::Widget
  {
  public:
    enum scaling_mode
    {
      scaling_none   =  0,      // without any scaling
      scaling_to_w   =  1 << 0, // uses width to calculate scale factor
      scaling_to_h   =  1 << 1, // uses height to calculate scale factor
      scaling_min    =  1 << 2, // uses minimum scale factor from width, height
      scaling_max    =  1 << 3, // uses maximum scale factor from width, height
      scaling_all    =  1 << 4  // scale to stretch into client area
    };

    CairoWidget();
    virtual ~CairoWidget();

    // scaling related functions
    scaling_mode set_scaling_mode(scaling_mode scaling_mode);
    scaling_mode get_scaling_mode();

    // need for redrawing the widget (double buffering related)
    void redraw();

    // need for auto resizing and redrawing the widget (double buffering related)
    void resize_redraw();
    
    // TODO - temporary: will be removed soon
    // void change_scaling_mode();

  protected:
    typedef unsigned int    size_t;

    // implements fundamental widget's method
    virtual void on_realize();
    virtual void on_size_allocate(const Gtk::Allocation& allocation);
    virtual void on_size_request(Gtk::Requisition* request);
    virtual bool on_expose_event(GdkEventExpose* event);
    virtual bool on_button_press_event(GdkEventButton* event);

    // in this method the implemented widget must "do the things"
    virtual void draw_widget(cairo_t* ctx) = 0;

    // with this method CairoWidget tells the needed drawing dimensions
    virtual void calc_drawing_size(cairo_t* ctx, size_t& width, size_t& height);

    // with this method CairoWidget tells the needed widgwt dimensions
    virtual void calc_widget_size(size_t& width, size_t& height);

    void calc_scale_factors();

    typedef Gdk::Rectangle                           area_t;
    typedef sigc::mem_functor0<void, CairoWidget>    method0_t;
    typedef std::pair<area_t, method0_t>             area_callback_t;
    typedef std::vector<area_callback_t>             areas_vect_t;

    mutable size_t      _draw_w;
    mutable size_t      _draw_h;
  
  private:
    // The width and height the widget will assume, must be determined
    // before starting drawing by calc_size()
    // drawing dimensions required
    // window client area
    mutable size_t      _client_w;
    mutable size_t      _client_h;
    // pixmap client area
    mutable size_t      _pixmap_w;
    mutable size_t      _pixmap_h;
    // cairo scale factors
    mutable double      _scale_x;
    mutable double      _scale_y;

    mutable bool        _need_redraw;
    scaling_mode        _scaling_mode;

    // The offscreen pixmap we use for double-buffering
    Glib::RefPtr<Gdk::Pixmap>  _buf;
  };

} //~ namespace sgpem


#endif //~ CAIRO_WIDGET_HH
