/* 
 * © 2007-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifndef _CONNECTION_FIGURE_H_
#define _CONNECTION_FIGURE_H_


#include "mdc.h"
#include <grts/structs.model.h>

namespace wbfig {
  class FigureEventHub;
  
  class ConnectionLineLayouter : public mdc::OrthogonalLineLayouter
  {
    typedef mdc::OrthogonalLineLayouter super;

    virtual std::vector<mdc::ItemHandle*> create_handles(mdc::Line *line, mdc::InteractionLayer *ilayer);

    virtual bool update_start_point();
    virtual bool update_end_point();

    virtual bool handle_dragged(mdc::Line *line, mdc::ItemHandle *handle, const mdc::Point &pos, bool dragging);

  public:
    enum Type
    {
      NormalLine,
      ZLine
    };

  private:
    Type _type;

    virtual std::vector<mdc::Point> get_points_for_subline(int subline);

  public:
    ConnectionLineLayouter(mdc::Connector *sconn, mdc::Connector *econn);

    void set_type(Type type);
    Type get_type() const { return _type; }
  };


  class Connection : public mdc::Line
  {
    typedef mdc::Line super;
  public:
    enum DiamondType
    {
      None,
      Filled,
      LeftEmpty,
      RightEmpty,
      Empty
    };
    enum CaptionPos
    {
      Below,
      Above,
      Middle
    };

    Connection(mdc::Layer *layer, FigureEventHub *hub, model_Object *represented_object);

    void set_splitted(bool flag);

    mdc::Point get_middle_caption_pos(const mdc::Size &size, CaptionPos pos);
    mdc::Point get_start_caption_pos(const mdc::Size &size);
    mdc::Point get_end_caption_pos(const mdc::Size &size);

    void set_start_dashed(bool flag);
    void set_end_dashed(bool flag);
    virtual void render(mdc::CairoCtx *cr);

    void set_start_figure(mdc::CanvasItem *item);
    void set_end_figure(mdc::CanvasItem *item);
    mdc::CanvasItem *get_start_figure() { return _start_figure; }
    mdc::CanvasItem *get_end_figure() { return _end_figure; }

    void set_diamond_type(DiamondType type);

    double get_segment_offset(int subline);
    void set_segment_offset(int subline, double offset);

    virtual bool on_click(mdc::CanvasItem *target, const mdc::Point &point, mdc::MouseButton button, mdc::EventState state);
    virtual bool on_enter(mdc::CanvasItem *target, const mdc::Point &point);
    virtual bool on_leave(mdc::CanvasItem *target, const mdc::Point &point);
    virtual bool on_button_press(mdc::CanvasItem *target, const mdc::Point &point, mdc::MouseButton button, mdc::EventState state);
    virtual bool on_button_release(mdc::CanvasItem *target, const mdc::Point &point, mdc::MouseButton button, mdc::EventState state);

    void set_center_captions(bool flag);
    bool get_center_captions() { return _center_captions; }

  private:
    model_Object *_represented_object;
    FigureEventHub *_hub;
    
    mdc::CanvasItem *_start_figure;
    mdc::CanvasItem *_end_figure;

    DiamondType _diamond;
    bool _start_dashed;
    bool _end_dashed;
    bool _splitted;
    bool _center_captions;

    virtual bool contains_point(const mdc::Point &point) const;

    virtual void stroke_outline(mdc::CairoCtx *cr, float offset= 0) const;
    //virtual void create_handles(mdc::InteractionLayer *ilayer);

    double get_middle_segment_angle();

    void update_layouter();

    virtual void mark_crossings(mdc::Line *line);
  };

};


#endif
