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 () const {return m_Type;}
00159 void SetId (gchar const *Id);
00163 gchar const *GetId () const {return m_Id;}
00170 virtual void AddChild (Object* object);
00178 Object* GetMolecule () const;
00185 Object* GetReaction () const;
00193 Object* GetGroup () const;
00200 Document* GetDocument () const;
00210 Object* GetParentOfType (TypeId Id) const;
00217 Object* GetChild (const gchar* Id) const;
00224 Object *GetFirstChild (std::map<std::string, Object*>::iterator& i);
00225 Object const *GetFirstChild (std::map<std::string, Object*>::const_iterator& i) const;
00232 Object *GetNextChild (std::map<std::string, Object*>::iterator& i);
00233 Object const *GetNextChild (std::map<std::string, Object*>::const_iterator& i) const;
00240 Object* GetDescendant (const gchar* Id) const;
00244 Object* GetParent () const {return m_Parent;};
00251 void SetParent (Object* Parent);
00260 virtual xmlNodePtr Save (xmlDocPtr xml) const;
00277 virtual bool Load (xmlNodePtr node);
00286 virtual void Move (double x, double y, double z = 0.);
00297 virtual void Transform2D (Matrix2D& m, double x, double y);
00306 bool SaveChildren (xmlDocPtr xml, xmlNodePtr node) const;
00312 void SaveId (xmlNodePtr node) const;
00323 xmlNodePtr GetNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
00333 xmlNodePtr GetNextNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
00343 xmlNodePtr GetNodeByName (xmlNodePtr node, char const *Name);
00352 xmlNodePtr GetNextNodeByName (xmlNodePtr node, char const *Name);
00359 virtual void Add (GtkWidget* w) const;
00366 virtual void Update (GtkWidget* w) const;
00374 virtual void SetSelected (GtkWidget* w, int state);
00378 bool HasChildren () const {return m_Children.size () != 0;}
00379
00383 unsigned GetChildrenNumber () const {return m_Children.size ();}
00384
00393 virtual Object* GetAtomAt (double x, double y, double z = 0.);
00394
00401 virtual bool Build (std::list<Object*>& Children) throw (std::invalid_argument);
00402
00408 virtual double GetYAlign ();
00409
00423 virtual bool BuildContextualMenu (GtkUIManager *UIManager, Object *object, double x, double y);
00424
00431 void EmitSignal (SignalId Signal);
00432
00442 virtual bool OnSignal (SignalId Signal, Object *Child);
00443
00451 void Lock (bool state = true);
00452
00459 bool IsLocked () {return m_Locked > 0;}
00460
00468 Object* GetFirstLink (std::set<Object*>::iterator& i);
00469
00476 Object* GetNextLink (std::set<Object*>::iterator& i);
00477
00483 void Unlink (Object *object);
00484
00491 virtual void OnUnlink (Object *object);
00492
00498 void GetPossibleAncestorTypes (std::set<TypeId>& types) const;
00499
00509 virtual bool SetProperty (unsigned property, char const *value);
00510
00516 virtual std::string GetProperty (unsigned property) const;
00517
00521 virtual void OnLoaded ();
00522
00527 void SetDirty (bool dirty = true);
00528
00538 static TypeId AddType (std::string TypeName, Object* (*CreateFunc) (), TypeId id = OtherType);
00539
00550 static Object* CreateObject (const std::string& TypeName, Object* parent = NULL);
00551
00557 static TypeId GetTypeId (const std::string& Name);
00558
00564 static std::string GetTypeName (TypeId Id);
00565
00572 static void AddMenuCallback (TypeId Id, BuildMenuCb cb);
00573
00581 static void AddRule (TypeId type1, RuleId rule, TypeId type2);
00582
00590 static void AddRule (const std::string& type1, RuleId rule, const std::string& type2);
00591
00598 static const std::set<TypeId>& GetRules (TypeId type, RuleId rule);
00599
00606 static const std::set<TypeId>& GetRules (const std::string& type, RuleId rule);
00607
00615 static void SetCreationLabel (TypeId Id, std::string Label);
00616
00622 static const std::string& GetCreationLabel (TypeId Id);
00623
00629 static const std::string& GetCreationLabel (const std::string& TypeName);
00630
00634 static SignalId CreateNewSignalId ();
00635
00636 private:
00637 Object* RealGetDescendant (const gchar* Id) const;
00638
00639 private:
00640 gchar* m_Id;
00641 TypeId m_Type;
00642 Object *m_Parent;
00643 std::map<std::string, Object*> m_Children;
00644 std::set<Object*> m_Links;
00645
00646 private:
00650 int m_Locked;
00651
00656 GCU_RO_PROP (bool, Dirty);
00657 };
00658
00659 }
00660 #endif //GCU_OBJECT_H