00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef GCU_OBJECT_H
00026 #define GCU_OBJECT_H
00027
00028 #include "macros.h"
00029 #include "matrix2d.h"
00030 #include <glib.h>
00031 #include <libxml/parser.h>
00032 #include <map>
00033 #include <set>
00034 #include <list>
00035 #include <string>
00036 #include <stdexcept>
00037 #include <gtk/gtk.h>
00038
00039 #define square(x) ((x)*(x))
00040
00041 namespace gcu
00042 {
00043
00068 enum GcuTypeId
00069 {
00070 NoType,
00071 AtomType,
00072 FragmentType,
00073 BondType,
00074 MoleculeType,
00075 ChainType,
00076 CycleType,
00077 ReactantType,
00078 ReactionArrowType,
00079 ReactionOperatorType,
00080 ReactionType,
00081 MesomeryType,
00082 MesomeryArrowType,
00083 DocumentType,
00084 TextType,
00085 OtherType
00086 };
00087
00092 typedef unsigned TypeId;
00093
00094 class Object;
00095
00104 typedef bool (*BuildMenuCb) (Object *target, GtkUIManager *UIManager, Object *object, double x, double y);
00105
00118 enum RuleId
00119 {
00120 RuleMayContain,
00121 RuleMustContain,
00122 RuleMayBeIn,
00123 RuleMustBeIn
00124 };
00125
00130 typedef unsigned SignalId;
00131
00132 class Document;
00133
00137 class Object
00138 {
00139 public:
00143 Object (TypeId Id = OtherType);
00147 virtual ~Object ();
00148
00153 TypeId GetType () {return m_Type;}
00159 void SetId (gchar const *Id);
00163 const gchar* GetId () {return m_Id;}
00170 virtual void AddChild (Object* object);
00178 Object* GetMolecule ();
00185 Object* GetReaction ();
00193 Object* GetGroup ();
00200 Document* GetDocument ();
00210 Object* GetParentOfType (TypeId Id);
00217 Object* GetChild (const gchar* Id);
00224 Object* GetFirstChild (std::map<std::string, Object*>::iterator& i);
00231 Object* GetNextChild (std::map<std::string, Object*>::iterator& i);
00238 Object* GetDescendant (const gchar* Id);
00242 Object* GetParent () {return m_Parent;}
00249 void SetParent (Object* Parent);
00258 virtual xmlNodePtr Save (xmlDocPtr xml);
00275 virtual bool Load (xmlNodePtr node);
00284 virtual void Move (double x, double y, double z = 0.);
00295 virtual void Transform2D (Matrix2D& m, double x, double y);
00304 bool SaveChildren (xmlDocPtr xml, xmlNodePtr node);
00310 void SaveId (xmlNodePtr node);
00321 xmlNodePtr GetNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
00331 xmlNodePtr GetNextNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
00341 xmlNodePtr GetNodeByName (xmlNodePtr node, char const *Name);
00350 xmlNodePtr GetNextNodeByName (xmlNodePtr node, char const *Name);
00357 virtual void Add (GtkWidget* w);
00364 virtual void Update (GtkWidget* w);
00372 virtual void SetSelected (GtkWidget* w, int state);
00376 bool HasChildren () {return m_Children.size () != 0;}
00377
00381 unsigned GetChildrenNumber () {return m_Children.size ();}
00382
00391 virtual Object* GetAtomAt (double x, double y, double z = 0.);
00392
00399 virtual bool Build (std::list<Object*>& Children) throw (std::invalid_argument);
00400
00406 virtual double GetYAlign ();
00407
00421 virtual bool BuildContextualMenu (GtkUIManager *UIManager, Object *object, double x, double y);
00422
00429 void EmitSignal (SignalId Signal);
00430
00440 virtual bool OnSignal (SignalId Signal, Object *Child);
00441
00449 void Lock (bool state = true);
00450
00457 bool IsLocked () {return m_Locked > 0;}
00458
00466 Object* GetFirstLink (std::set<Object*>::iterator& i);
00467
00474 Object* GetNextLink (std::set<Object*>::iterator& i);
00475
00481 void Unlink (Object *object);
00482
00489 virtual void OnUnlink (Object *object);
00490
00496 void GetPossibleAncestorTypes (std::set<TypeId>& types);
00497
00507 virtual bool SetProperty (unsigned property, char const *value);
00508
00514 virtual std::string GetProperty (unsigned property) const;
00515
00519 virtual void OnLoaded ();
00520
00525 void SetDirty (bool dirty = true);
00526
00536 static TypeId AddType (std::string TypeName, Object* (*CreateFunc) (), TypeId id = OtherType);
00537
00548 static Object* CreateObject (const std::string& TypeName, Object* parent = NULL);
00549
00555 static TypeId GetTypeId (const std::string& Name);
00556
00562 static std::string GetTypeName (TypeId Id);
00563
00570 static void AddMenuCallback (TypeId Id, BuildMenuCb cb);
00571
00579 static void AddRule (TypeId type1, RuleId rule, TypeId type2);
00580
00588 static void AddRule (const std::string& type1, RuleId rule, const std::string& type2);
00589
00596 static const std::set<TypeId>& GetRules (TypeId type, RuleId rule);
00597
00604 static const std::set<TypeId>& GetRules (const std::string& type, RuleId rule);
00605
00613 static void SetCreationLabel (TypeId Id, std::string Label);
00614
00620 static const std::string& GetCreationLabel (TypeId Id);
00621
00627 static const std::string& GetCreationLabel (const std::string& TypeName);
00628
00632 static SignalId CreateNewSignalId ();
00633
00634 private:
00635 Object* RealGetDescendant (const gchar* Id);
00636
00637 private:
00638 gchar* m_Id;
00639 TypeId m_Type;
00640 Object *m_Parent;
00641 std::map<std::string, Object*> m_Children;
00642 std::set<Object*> m_Links;
00643
00644 private:
00648 int m_Locked;
00649
00654 GCU_RO_PROP (bool, Dirty);
00655 };
00656
00657 }
00658 #endif //GCU_OBJECT_H