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
00042 namespace gcu
00043 {
00044
00045 class Dialog;
00046 class Application;
00047
00072 enum GcuTypeId
00073 {
00074 NoType,
00075 AtomType,
00076 FragmentType,
00077 BondType,
00078 MoleculeType,
00079 ChainType,
00080 CycleType,
00081 ReactantType,
00082 ReactionArrowType,
00083 ReactionOperatorType,
00084 ReactionType,
00085 MesomeryType,
00086 MesomeryArrowType,
00087 DocumentType,
00088 TextType,
00089 OtherType
00090 };
00091
00096 typedef unsigned TypeId;
00097
00098 class Object;
00099
00108 typedef bool (*BuildMenuCb) (Object *target, GtkUIManager *UIManager, Object *object, double x, double y);
00109
00110 class TypeDesc
00111 {
00112 public:
00113 TypeDesc ();
00114
00115 TypeId Id;
00116 Object* (*Create) ();
00117 std::set <TypeId> PossibleChildren;
00118 std::set <TypeId> PossibleParents;
00119 std::set <TypeId> RequiredChildren;
00120 std::set <TypeId> RequiredParents;
00121 std::string CreationLabel;
00122 std::list <BuildMenuCb> MenuCbs;
00123 };
00124
00125 class Object;
00126
00139 enum RuleId
00140 {
00141 RuleMayContain,
00142 RuleMustContain,
00143 RuleMayBeIn,
00144 RuleMustBeIn
00145 };
00146
00151 typedef unsigned SignalId;
00152
00153 class Document;
00154
00158 class Object
00159 {
00160 friend class Application;
00161 public:
00165 Object (TypeId Id = OtherType);
00169 virtual ~Object ();
00170
00175 TypeId GetType () const {return m_Type;}
00181 void SetId (gchar const *Id);
00185 char const *GetId () const {return m_Id;}
00192 virtual void AddChild (Object* object);
00200 Object* GetMolecule () const;
00207 Object* GetReaction () const;
00215 Object* GetGroup () const;
00222 Document* GetDocument () const;
00228 Application* GetApplication () const;
00238 Object* GetParentOfType (TypeId Id) const;
00245 Object* GetChild (const gchar* Id) const;
00252 Object *GetFirstChild (std::map<std::string, Object*>::iterator& i);
00253 Object const *GetFirstChild (std::map<std::string, Object*>::const_iterator& i) const;
00260 Object *GetNextChild (std::map<std::string, Object*>::iterator& i);
00261 Object const *GetNextChild (std::map<std::string, Object*>::const_iterator& i) const;
00268 Object* GetDescendant (const gchar* Id) const;
00272 Object* GetParent () const {return m_Parent;};
00279 void SetParent (Object* Parent);
00288 virtual xmlNodePtr Save (xmlDocPtr xml) const;
00305 virtual bool Load (xmlNodePtr node);
00314 virtual bool GetCoords (double *x, double *y, double *z = NULL) const;
00323 virtual void Move (double x, double y, double z = 0.);
00334 virtual void Transform2D (Matrix2D& m, double x, double y);
00343 bool SaveChildren (xmlDocPtr xml, xmlNodePtr node) const;
00349 void SaveId (xmlNodePtr node) const;
00360 xmlNodePtr GetNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
00370 xmlNodePtr GetNextNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
00380 xmlNodePtr GetNodeByName (xmlNodePtr node, char const *Name);
00389 xmlNodePtr GetNextNodeByName (xmlNodePtr node, char const *Name);
00393 bool HasChildren () const {return m_Children.size () != 0;}
00394
00398 unsigned GetChildrenNumber () const {return m_Children.size ();}
00399
00408 virtual Object* GetAtomAt (double x, double y, double z = 0.);
00409
00416 virtual bool Build (std::list<Object*>& Children) throw (std::invalid_argument);
00417
00423 virtual double GetYAlign ();
00424
00438 virtual bool BuildContextualMenu (GtkUIManager *UIManager, Object *object, double x, double y);
00439
00446 void EmitSignal (SignalId Signal);
00447
00457 virtual bool OnSignal (SignalId Signal, Object *Child);
00458
00466 void Lock (bool state = true);
00467
00474 bool IsLocked () {return m_Locked > 0;}
00475
00483 Object* GetFirstLink (std::set<Object*>::iterator& i);
00484
00491 Object* GetNextLink (std::set<Object*>::iterator& i);
00492
00498 void Link (Object *object);
00499
00505 void Unlink (Object *object);
00506
00513 virtual void OnUnlink (Object *object);
00514
00520 void GetPossibleAncestorTypes (std::set<TypeId>& types) const;
00521
00531 virtual bool SetProperty (unsigned property, char const *value);
00532
00539 virtual std::string GetProperty (unsigned property) const;
00540
00544 virtual void OnLoaded ();
00545
00550 void SetDirty (bool dirty = true);
00551
00555 virtual void Clear ();
00556
00560 virtual std::string Name ();
00561
00566 std::string Identity ();
00567
00571 virtual char const *HasPropertiesDialog () const;
00572
00576 virtual bool CanSelect () const {return true;}
00577
00582 virtual void NotifyEmpty () {;}
00583
00587 void ShowPropertiesDialog ();
00588
00601 static TypeId AddType (std::string TypeName, Object* (*CreateFunc) (), TypeId id = OtherType);
00602
00609 static void AddAlias (TypeId id, std::string TypeName);
00610
00623 static Object* CreateObject (const std::string& TypeName, Object* parent = NULL);
00624
00630 static TypeId GetTypeId (const std::string& Name);
00631
00637 static std::string GetTypeName (TypeId Id);
00638
00647 static void AddMenuCallback (TypeId Id, BuildMenuCb cb);
00648
00658 static void AddRule (TypeId type1, RuleId rule, TypeId type2);
00659
00669 static void AddRule (const std::string& type1, RuleId rule, const std::string& type2);
00670
00679 static const std::set<TypeId>& GetRules (TypeId type, RuleId rule);
00680
00689 static const std::set<TypeId>& GetRules (const std::string& type, RuleId rule);
00690
00700 static void SetCreationLabel (TypeId Id, std::string Label);
00701
00709 static const std::string& GetCreationLabel (TypeId Id);
00710
00718 static const std::string& GetCreationLabel (const std::string& TypeName);
00719
00723 static SignalId CreateNewSignalId ();
00724
00725 protected:
00729 virtual Dialog *BuildPropertiesDialog ();
00730
00731 private:
00732 Object* RealGetDescendant (const gchar* Id) const;
00733
00734 private:
00735 char* m_Id;
00736 TypeId m_Type;
00737 Object *m_Parent;
00738 std::map<std::string, Object*> m_Children;
00739 std::set<Object*> m_Links;
00740 TypeDesc const *m_TypeDesc;
00741
00742 private:
00746 int m_Locked;
00747
00752 GCU_RO_PROP (bool, Dirty);
00753 };
00754
00755 }
00756 #endif //GCU_OBJECT_H