[gtkmm] Problem with Canvas



I have some problem to move an object on canvas. I don't understand if I
make a programming error. Someone can help me?
I send my code... in attach (sorry!!!)

Thanks...
-- 
Rocker
  alias "Davide Anastasia"
"Everything you know is wrong"
Linux User #341094

Mail signed with GnuPG <http://www.gnupg.org/faq.html>
//
// File: ModelClassObject.cc
// Created by: Davide "Rocker" Anastasia <s242248 studenti ing unipi it>
// Created on: Thu Jul 29 00:39:16 2004
//

#define SIDE_BORDER			15
#define UP_DOWN_BORDER		6
#define SIZE_FONT			16
#define LIFE_LINE			40
#define LIFE_TIME			200
#define LIFE_TIME_BORDER	6
#define TEXT_TO_LINE		1

#include "ModelClassObject.h"
#include "CanvasMgr.h"
#include "DragBox.h"

#include <libgnomecanvasmm.h>
#include <gtkmm.h>
#include <iostream>

// CanvasMgr è derivato da Gnome::Canvas::Canvas
ModelClassObject::ModelClassObject(CanvasMgr* mgr, int id) :
	ModelObjBase(mgr, id)
{
	// Questo gruppo contiene tutti gli oggetti facenti parte dell'elemento di modello
	my_self_group_ = manage( new Gnome::Canvas::Group( *(mgr->root()) ) );
	
	// Label di testo
	label_ = manage( new Gnome::Canvas::Text(*my_self_group_) );
	label_->property_text() = ":Untitled";
	label_->property_x() = SIDE_BORDER + label_->property_text_width()/2;
	label_->property_y() = UP_DOWN_BORDER + label_->property_text_height()/2;
	label_->property_font() = "Sans SIZE_FONT";
	label_->property_fill_color() = "black";
	// label_->property_underline_set() = "true";
	
	underline_points_ = new Gnome::Canvas::Points();
	underline_points_->push_back(Gnome::Art::Point(SIDE_BORDER, UP_DOWN_BORDER+label_->property_text_height()+TEXT_TO_LINE));
	underline_points_->push_back(Gnome::Art::Point(SIDE_BORDER+label_->property_text_width(), UP_DOWN_BORDER+label_->property_text_height()+TEXT_TO_LINE));
	underline_line_ = manage( new Gnome::Canvas::Line(*my_self_group_, *underline_points_) );
		
	setIsObject(true);

	// Variabili utili
	double right_box_pos = SIDE_BORDER*2 + label_->property_text_width();
	double bottom_box_pos = UP_DOWN_BORDER*2 + label_->property_text_height();
	
	// Box con il nome della classe...
	box_ = manage( new Gnome::Canvas::Rect(*my_self_group_,
			0, 0,
			right_box_pos, bottom_box_pos) );
	box_->property_outline_color() = "black";
	
	// Linea temporale
	upper_points_ = new Gnome::Canvas::Points();
	upper_points_->push_back(Gnome::Art::Point(right_box_pos/2,bottom_box_pos));
	upper_points_->push_back(Gnome::Art::Point(right_box_pos/2,bottom_box_pos + LIFE_LINE));
	upper_line_ = manage( new Gnome::Canvas::Line(*my_self_group_, *upper_points_) );
	upper_line_->property_line_style() = Gdk::LINE_ON_OFF_DASH;				// Linea tratteggiata!
	upper_line_->property_fill_color() = "black";
	upper_line_->property_width_pixels() = 1;
	
	// Vita dell'oggetto
	lifetime_box_ = manage ( new Gnome::Canvas::Rect(*my_self_group_,
			right_box_pos/2 - LIFE_TIME_BORDER, bottom_box_pos + LIFE_LINE,
			right_box_pos/2 + LIFE_TIME_BORDER, bottom_box_pos + LIFE_LINE + LIFE_TIME) );
	lifetime_box_->property_outline_color() = "black";
	
	// Linea temporale
	lower_points_ = new Gnome::Canvas::Points();
	lower_points_->push_back(Gnome::Art::Point(right_box_pos/2,bottom_box_pos + LIFE_LINE + LIFE_TIME));
	lower_points_->push_back(Gnome::Art::Point(right_box_pos/2,bottom_box_pos + LIFE_LINE*2 + LIFE_TIME));
	lower_line_ = manage( new Gnome::Canvas::Line(*my_self_group_, *lower_points_) );
	lower_line_->property_line_style() = Gdk::LINE_ON_OFF_DASH;				// Linea tratteggiata!
	lower_line_->property_fill_color() = "black";
	
	// Definizione dei DragBox...
	db_box_ = manage( new DragBox(*my_self_group_) );
	db_box_->move_drag_box(right_box_pos, bottom_box_pos);
	db_lifeline_up_ = manage( new DragBox(*my_self_group_) );
	db_lifeline_up_->move_drag_box(right_box_pos/2 - LIFE_TIME_BORDER, bottom_box_pos + LIFE_LINE);
	db_lifeline_down_ = manage( new DragBox(*my_self_group_) );
	db_lifeline_down_->move_drag_box(right_box_pos/2 + LIFE_TIME_BORDER, bottom_box_pos + LIFE_LINE + LIFE_TIME);
	
	// Connessione dei segnali
	signal_event().connect(slot(*this, &ModelClassObject::on_item_event));
}


ModelClassObject::~ModelClassObject()
{
	delete lower_points_;
	delete upper_points_;
}

void
ModelClassObject::setIsObject(bool var)
{
	if ( var == true ) {
		underline_line_->property_fill_color() = "black";
	} else {
		underline_line_->property_fill_color().reset_value();
	} 
}

bool
ModelClassObject::getIsObject() {
	return is_object_;	
}

bool
//ModelClassObject::on_item_event(GdkEvent* event, Gnome::Canvas::Item* item )
ModelClassObject::on_item_event(GdkEvent* event)
{
	
	
	std::cerr << "Evento gestito!"; // DEBUG!
	
	static double x, y;
	double item_x, item_y;
	static bool dragging;
	
	item_x = event->button.x;
	item_y = event->button.y;
	
	my_self_group_->property_parent().get_value()->w2i(item_x, item_y);
	
	switch(event->type) {
		case GDK_BUTTON_PRESS:
			switch(event->button.button) {
				case 1:
					// Tasto 1 del mouse (<-)
					if(event->button.state & GDK_SHIFT_MASK) {
						// Eliminazione dell'oggetto "on_focus"
						// dal vector in CanvasMgr... come si fa?!
						// delete item;
					}  else {
						// Spostamento dell'oggetto
						x = item_x;
						y = item_y;
						
						my_self_group_->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
									Gdk::Cursor(Gdk::FLEUR),
									event->button.time);
						
						dragging = true;
					}
					break;
				
				case 2:
					// Tasto 2 del mouse (->)
					if(event->button.state & GDK_SHIFT_MASK) {
						// Porta in primo piano (tasto 2 + shift)
						my_self_group_->lower_to_bottom();
					} else {
						// Porta verso su di 1
						my_self_group_->lower(1);
					}
					break;
				
				case 3:
					// Tasto 3 del mouse (<->)
					if(event->button.state & GDK_SHIFT_MASK) {
						// Porta sullo sfondo (tasto 3 + shift)
						my_self_group_->raise_to_top();
					} else {
						// Porta verso giù di 1
						my_self_group_->raise(1);
					}
					break;
				
				default:
					break;
			}
			break;
    
		case GDK_MOTION_NOTIFY:
			if(dragging &&(event->motion.state & GDK_BUTTON1_MASK)) {
				double new_x = item_x;
				double new_y = item_y;
				
				my_self_group_->move(new_x - x, new_y - y);
				x = new_x;
				y = new_y;
			}
			
			break;
		
		case GDK_BUTTON_RELEASE:
			my_self_group_->ungrab(event->button.time);
			dragging = false;
			
			break;
		
		default:
			break;
	}
	
	return false;
}

void
ModelClassObject::move(double x, double y)
{
	my_self_group_->move(x, y);
}

void
ModelClassObject::move_to(double x, double y)
{
	my_self_group_->property_x() = x;
	my_self_group_->property_y() = y;
}

void
ModelClassObject::foo()
{
	std::cout << "evento gestito!";	
}
//
// File: ModelClassObject.h
// Created by: Davide "Rocker" Anastasia <s242248 studenti ing unipi it>
// Created on: Thu Jul 29 00:39:16 2004
//
// Questa classe definisce la forma di una classe o di un oggetto nel diagramma
// La sola differenza tra l'una e l'altra è la sottolineatura della label.


#ifndef _MODELCLASSOBJECT_H_
#define _MODELCLASSOBJECT_H_

#include "ModelObjBase.h"
#include "CanvasMgr.h"
#include "DragBox.h"

class ModelClassObject : public ModelObjBase
{
	public:
		ModelClassObject(CanvasMgr*, int);
		 ~ModelClassObject();
		
		// Gestione delle proprietà
		void setIsConcurrent(bool);
		bool getIsConcurrent();
	
		void setIsObject(bool);
		bool getIsObject();
		
		// Gestione degli eventi
		bool on_item_event(GdkEvent*);
		
		// Gestione del movimento
		void move(double, double);
		void move_to(double, double);
		
	
		// Fuck function!
		virtual void getXMI() {};
		virtual void getSVG() {};	
		virtual void update() {};
	
		void foo();
		
	protected:
		Gnome::Canvas::Group* my_self_group_;

		Gnome::Canvas::Rect* box_;
		Gnome::Canvas::Text* label_;
	
		Gnome::Canvas::Points* underline_points_;
		Gnome::Canvas::Line* underline_line_;
	
		Gnome::Canvas::Points* upper_points_;
		Gnome::Canvas::Line* upper_line_;
	
		Gnome::Canvas::Rect* lifetime_box_;
	
		Gnome::Canvas::Points* lower_points_;
		Gnome::Canvas::Line* lower_line_;
	
		DragBox* db_box_;
		DragBox* db_lifeline_up_;
		DragBox* db_lifeline_down_;

		// Definisco se l'oggetto è una classe o un oggetto e se è o no concorrente
		bool is_concurrent_;
		bool is_object_;
		
		//SigC::Signal0<void> signal_event_;
		
};


#endif	//_MODELCLASSOBJECT_H_
//
// File: ModelObjBase.cc
// Created by: Davide "Rocker" Anastasia <s242248 studenti ing unipi it>
// Created on: Sun May 30 23:55:33 2004
//

#include "ModelObjBase.h"
#include "CanvasMgr.h"

#include <libgnomecanvasmm.h>
//#include <gdkmm.h>

ModelObjBase::ModelObjBase(CanvasMgr* canvas, int id) //:
	// Gnome::Canvas::Group( *(canvas->root()) )
{
	my_manager_ = canvas;
	myId = id;
}


ModelObjBase::~ModelObjBase()
{
}
//
// File: ModelObjBase.h
// Created by: Davide "Rocker" Anastasia <s242248 studenti ing unipi it>
// Created on: Sun May 30 23:55:33 2004
//

#ifndef _MODELOBJBASE_H_
#define _MODELOBJBASE_H_

#include <libgnomecanvasmm.h>
#include <gtkmm.h>

#include "CanvasMgr.h"


class CanvasMgr;

class ModelObjBase : // public SigC::Object
						public Gtk::EventBox
{
	public:
		ModelObjBase(CanvasMgr*, int);
		virtual ~ModelObjBase();
	
		// ModelObjBase interface
		virtual void getXMI() = 0;	// Da definire il tipo di ritorno!
		virtual void getSVG() = 0;	// Da definire il tipo di ritorno!
									// Probabilmente il tipo di ritorno di queste
									// due funzioni è un oggetto di Xerces-C++
	
		virtual void update() = 0;
	
	protected:
		CanvasMgr* my_manager_;
		int myId;
		
};


#endif	//_MODELOBJBASE_H_

Attachment: signature.asc
Description: Questa parte del messaggio =?ISO-8859-1?Q?=E8?= firmata



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]