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 "matrix2d.h"
00029 #include <glib.h>
00030 #include <libxml/parser.h>
00031 #include <map>
00032 #include <set>
00033 #include <list>
00034 #include <string>
00035 #include <stdexcept>
00036 #include <gtk/gtk.h>
00037 #include <libgnomeprint/gnome-print.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 void AddChild (Object* object);
00177 Object* GetMolecule ();
00184 Object* GetReaction ();
00192 Object* GetGroup ();
00199 Document* GetDocument ();
00209 Object* GetParentOfType (TypeId Id);
00216 Object* GetChild (const gchar* Id);
00223 Object* GetFirstChild (std::map<std::string, Object*>::iterator& i);
00230 Object* GetNextChild (std::map<std::string, Object*>::iterator& i);
00237 Object* GetDescendant (const gchar* Id);
00241 Object* GetParent () {return m_Parent;}
00248 void SetParent (Object* Parent);
00257 virtual xmlNodePtr Save (xmlDocPtr xml);
00274 virtual bool Load (xmlNodePtr node);
00283 virtual void Move (double x, double y, double z = 0.);
00294 virtual void Transform2D (Matrix2D& m, double x, double y);
00303 bool SaveChildren (xmlDocPtr xml, xmlNodePtr node);
00309 void SaveId (xmlNodePtr node);
00320 xmlNodePtr GetNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
00330 xmlNodePtr GetNextNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
00340 xmlNodePtr GetNodeByName (xmlNodePtr node, char const *Name);
00349 xmlNodePtr GetNextNodeByName (xmlNodePtr node, char const *Name);
00356 virtual void Add (GtkWidget* w);
00362 virtual void Print (GnomePrintContext *pc);
00369 virtual void Update (GtkWidget* w);
00377 virtual void SetSelected (GtkWidget* w, int state);
00381 bool HasChildren () {return m_Children.size () != 0;}
00382
00386 unsigned GetChildrenNumber () {return m_Children.size ();}
00387
00396 virtual Object* GetAtomAt (double x, double y, double z = 0.);
00397
00404 virtual bool Build (std::list<Object*>& Children) throw (std::invalid_argument);
00405
00411 virtual double GetYAlign ();
00412
00426 virtual bool BuildContextualMenu (GtkUIManager *UIManager, Object *object, double x, double y);
00427
00434 void EmitSignal (SignalId Signal);
00435
00445 virtual bool OnSignal (SignalId Signal, Object *Child);
00446
00454 void Lock (bool state = true);
00455
00462 bool IsLocked () {return m_Locked > 0;}
00463
00471 Object* GetFirstLink (std::set<Object*>::iterator& i);
00472
00479 Object* GetNextLink (std::set<Object*>::iterator& i);
00480
00486 void Unlink (Object *object);
00487
00494 virtual void OnUnlink (Object *object);
00495
00501 void GetPossibleAncestorTypes (std::set<TypeId>& types);
00502
00512 static TypeId AddType (std::string TypeName, Object* (*CreateFunc) (), TypeId id = OtherType);
00513
00524 static Object* CreateObject (const std::string& TypeName, Object* parent = NULL);
00525
00531 static TypeId GetTypeId (const std::string& Name);
00532
00538 static std::string GetTypeName (TypeId Id);
00539
00546 static void AddMenuCallback (TypeId Id, BuildMenuCb cb);
00547
00555 static void AddRule (TypeId type1, RuleId rule, TypeId type2);
00556
00564 static void AddRule (const std::string& type1, RuleId rule, const std::string& type2);
00565
00572 static const std::set<TypeId>& GetRules (TypeId type, RuleId rule);
00573
00580 static const std::set<TypeId>& GetRules (const std::string& type, RuleId rule);
00581
00589 static void SetCreationLabel (TypeId Id, std::string Label);
00590
00596 static const std::string& GetCreationLabel (TypeId Id);
00597
00603 static const std::string& GetCreationLabel (const std::string& TypeName);
00604
00608 static SignalId CreateNewSignalId ();
00609
00610 private:
00611 Object* RealGetDescendant (const gchar* Id);
00612
00613 private:
00614 gchar* m_Id;
00615 TypeId m_Type;
00616 Object *m_Parent;
00617 std::map<std::string, Object*> m_Children;
00618 std::set<Object*> m_Links;
00619
00620 private:
00624 int m_Locked;
00625 };
00626
00627 }
00628 #endif //GCU_OBJECT_H