//------------------------------------------------------------------------------ // gui object base class -- // (C) Piero Giubilato 2008-2010, Berkeley Lab -- //------------------------------------------------------------------------------ //______________________________________________________________________________ // {Trace} // [File name] "gui_Object.cpp" // [Author] "Piero Giubilato" // [Version] "2.0" // [Modified by] "Piero Giubilato, Devis Contarato" // [Last revision] "06 Jun 2009" // [Language] "C++" // [Compiler] "Visual C++ 8.x 9.x" // [Member of] "Cool SEAL" // [Project] "SEAL" // [Description] "gui_Object class definition" // [Key documentation] // "Visual C++ Reference Help" // "Root reference guide" // {Trace} //______________________________________________________________________________ // Root Component #include // Application component #include "global.h" #include "gui_Const.h" #include "gui_Main.h" #include "gui_Object.h" // CINT Preprocessor class import definition ClassImp(gui_Object) //______________________________________________________________________________ // Static member declaration std::vector gui_Object::gui_obj_Is; std::vector gui_Object::gui_obj_Ref; bool gui_Object::redraw_all_On = false; bool gui_Object::update_auto_On = true; //______________________________________________________________________________ gui_Object::gui_Object() { // Default constructor. gui_Object provides a common interface for all // gui objects, so it is composed mainly by virtual declarations plus // some commons control mechanism. dbg_Print("gui_Object::gui_Object:(default)", DBG_LVL_ZERO); // Set a new object owned by the default container Init(0); } //______________________________________________________________________________ gui_Object::gui_Object(gui_Object* parent) { // Set a new object owned by a parent dbg_Print("gui_Object::gui_Object:(gui_Object*)", DBG_LVL_ZERO); // Init Init(parent); } //______________________________________________________________________________ void gui_Object::Init(gui_Object* parent) { // Initializes the new user interface graphic object dbg_Print("gui_Object::Init:(gui_Object*)", DBG_LVL_ZERO); // Checks for present status of objects IDs list gui_obj_Id = 0; while (gui_obj_Id < gui_obj_Is.size()) { if (!gui_obj_Is[gui_obj_Id]) break; gui_obj_Id++; } // Registers current object own Id if (gui_obj_Id == gui_obj_Is.size()) gui_obj_Is.push_back(true); else gui_obj_Is[gui_obj_Id] = true; // Register current object reference if (gui_obj_Id == gui_obj_Ref.size()) gui_obj_Ref.push_back(this); else gui_obj_Ref[gui_obj_Id] = this; // Creates the string version of the object Id gui_obj_id_Text.Form("%i", gui_obj_Id); // Sets the object family membership if(parent) { parent->child_Add(this); // Tell the parent he has also this child! obj_Parent = parent; // Make the child aware of his parent! // No parent given, the parent will be object ZERO by default, // i.e. the gui_Main itself. Note that object ZERO is the child // of itself... basically god! } else obj_Parent = gui_obj_Ref[0]; // Initializes the callback functor to null Clbk = 0; // Sets default appearance properties obj_Left = 0; obj_Top = 0; obj_Width = 64; obj_Height = 32; obj_foreColor = gui_Const::obj_foreColor; obj_backColor = obj_Parent->shadedColor(); obj_Transparent = false; obj_Render = false; // Text obj_Text.Form("Text"); obj_fontName.Form("Helvetica"); obj_fontBold = false; obj_fontSize = 16; // Sets TGLabel pointer to NULL gui_obj_Label = NULL; } //______________________________________________________________________________ gui_Object::~gui_Object() { // Default destructor. dbg_Print("gui_Object::~gui_Object:()", DBG_LVL_ZERO); // Kills all the childs! for (UInt_t i = 0; i < obj_Child.size(); i++) delete obj_Child[i]; // Deletes the callback functor(s) in case if (Clbk) delete Clbk; // Delete the label, if any if(gui_obj_Label) { Parent()->Window()->RemoveFrame(gui_obj_Label); gui_obj_Label->DestroyWindow(); delete gui_obj_Label; } // Sets the object as killed! gui_obj_Is[gui_obj_Id] = false; gui_obj_Ref[gui_obj_Id] = NULL; } //______________________________________________________________________________ UInt_t gui_Object::obj_Id() const { // Returns the unique object Id return gui_obj_Id; } //______________________________________________________________________________ const TString gui_Object::obj_id_Text() const { // Returns the unique object Id as a TString return gui_obj_id_Text; } // ******************************** // ** Window/Container functions ** // ******************************** //______________________________________________________________________________ TGCompositeFrame* gui_Object::Window() const { // Returns the pointer to the object graphic frame itself. // By default this function just returns the parent // container, and MUST BE OVERLOADED by those objects // which provides container capabilities. return obj_Parent->Window(); } //______________________________________________________________________________ TGMenuBar* gui_Object::menu_Window() const { // Returns the pointer to the object owned menu frame. // By default this function just returns the parent // container, and MUST BE OVERLOADED by those objects // which provides menu handling capabilities. return obj_Parent->menu_Window(); } // ********************************* // ** Global management functions ** // ********************************* //______________________________________________________________________________ void gui_Object::update_Auto(bool state) { // Sets the auto update function on and off. Basically, when auto update // is TRUE (default at startup), every time a property of a gui_Object is // changed the entire windows is refreshed to show the new object appearance. // If auto update is set to FALSE, the update function has to be called in // order to refresh the window. dbg_Print("gui_Object::auto_update_Set:(bool)", DBG_LVL_FLOW); // Set he value! update_auto_On = state; } //______________________________________________________________________________ void gui_Object::Update(bool force) const { // This is called by single-object when one of its appearance setting is // modified and hence the object needs to be redrawn. if ((!redraw_all_On && update_auto_On) || force) { // Debug dbg_Print("gui_Object::Update:()", DBG_LVL_FLOW); // Repaints the object (except that for object 0, the container itself) if (gui_obj_Id) Parent()->Window()->MapSubwindows(); } } //______________________________________________________________________________ void gui_Object::redraw_All() { // Regenerates all the objects who have interaction with the // main gui parameters dbg_Print("gui_Object::redraw_All:()", DBG_LVL_FLOW); // Stop the single object update redraw_all_On = true; // Search trhough all the objects. Note how object #0 is gui_Main itself, // so it is skipped by default (loop starts at 1) for (UInt_t i = 1; i < gui_obj_Is.size(); i++) if (gui_obj_Is[i]) gui_obj_Ref[i]->ReDraw(); // Refreshes the main window redraw_all_On = false; } //______________________________________________________________________________ void gui_Object::ReDraw() { // This is called when the GUI appearance general // parameters (background color, light direction, ...) are changed, and // so all the GUI objects using them must be redrawn. // Overload it for object which needs it! dbg_Print("gui_Object::ReDraw:()", DBG_LVL_FLOW); // The only default handled thing is the eventual label if (gui_obj_Label) color_Set(0, Parent()->backColor()); } //______________________________________________________________________________ void gui_Object::delete_All() { // Delete all objects exept the #0 dbg_Print("gui_Object::delete_All:()", DBG_LVL_FLOW); // Search trhough all the objects. Note how object #0 is gui_Main itself, // so it is skipped by default (loop starts at 1) for (UInt_t i = 1; i < gui_obj_Is.size(); i++) { gui_obj_Is[i] = false; delete gui_obj_Ref[i]; } } // ********************** // ** Family functions ** // ********************** //______________________________________________________________________________ gui_Object* gui_Object::Parent() const { // Returns the parent of the object dbg_Print("gui_Object::Parent:()", DBG_LVL_FLOW); return obj_Parent; } //______________________________________________________________________________ void gui_Object::Parent(gui_Object* parent) { // Sets the parent of the object. Note: this function is provided // for completeness only, in principle it should NEVER be called! dbg_Print("gui_Object::Parent:(gui_Object*)", DBG_LVL_FLOW); obj_Parent = parent; } //______________________________________________________________________________ gui_Object* gui_Object::Child(UInt_t child_Id) const { // Returns the pointer to a child dbg_Print("gui_Object::Child:(UInt)", DBG_LVL_FLOW); // Check and return if (child_Id < obj_Child.size()) return obj_Child[child_Id]; else return 0; } //______________________________________________________________________________ UInt_t gui_Object::child_Count() const { // Returns the number of childs dbg_Print("gui_Object::child_Count:()", DBG_LVL_FLOW); // Check and return return obj_Child.size(); } //______________________________________________________________________________ void gui_Object::child_Add(gui_Object* child) { // Adds a child to the object dbg_Print("gui_Object::child_Add:(gui_Object*)", DBG_LVL_FLOW); // Checks if the child already exists for (UInt_t i = 0; i < obj_Child.size(); i++) if (obj_Child[i] == child) return; // Add it otherwise obj_Child.push_back(child); } //______________________________________________________________________________ void gui_Object::child_Del(gui_Object* child) { // Adds a child to the object dbg_Print("gui_Object::child_Add:(gui_Object*)", DBG_LVL_FLOW); // Checks if the child already exists for (UInt_t i = 0; i < obj_Child.size(); i++) { if (obj_Child[i] == child) { for (UInt_t j = i; j < obj_Child.size() - 1; j++) { obj_Child[j] = obj_Child[j + 1]; } obj_Child.pop_back(); return; } } } // *********************************** // ** Appearance/position functions ** // *********************************** /* //______________________________________________________________________________ void gui_Object::Move(UInt_t left, UInt_t top) { // Moves the object MoveResize(left, top, obj_Width, obj_Height); } */ /* //______________________________________________________________________________ void gui_Object::Resize(UInt_t width, UInt_t height) { // Resizes the object MoveResize(obj_Left, obj_Top, width, height); } */ //______________________________________________________________________________ UInt_t gui_Object::Left() const { // Returns the object lef position return obj_Left; } //______________________________________________________________________________ UInt_t gui_Object::Top() const { // Returns the object top position return obj_Top; } //______________________________________________________________________________ UInt_t gui_Object::Width() const { // Returns the object width return obj_Width; } //______________________________________________________________________________ UInt_t gui_Object::Height() const { // Returns the object height return obj_Height; } //______________________________________________________________________________ void gui_Object::color_Set(UInt_t foreColor, UInt_t backColor) { // Sets the color of the object std::cout << "gui_Object::color_Set()\n"; // Here i used just to render the attached label // transparency behaviour if (gui_obj_Label) gui_obj_Label->SetBackgroundColor(backColor); } //______________________________________________________________________________ void gui_Object::foreColor(UInt_t color) { // Sets the object foreground color color_Set(color, obj_backColor); } //______________________________________________________________________________ void gui_Object::backColor(UInt_t color) { // Sets the object background color color_Set(obj_foreColor, color); } //______________________________________________________________________________ UInt_t gui_Object::foreColor() const { // Returns the object foreground color return obj_foreColor; } //______________________________________________________________________________ UInt_t gui_Object::backColor() const { // Returns the object background color return obj_backColor; } //______________________________________________________________________________ UInt_t gui_Object::shadedColor() const { // Returns the object shadedd color, if any. This function must // be overloaded by thos eobjects which use a shaded background return obj_backColor; } //______________________________________________________________________________ bool gui_Object::Transparent() const { // Returns the object background color return obj_Transparent; } //______________________________________________________________________________ bool gui_Object::Render() const { // Returns the object background color return obj_Render; } //______________________________________________________________________________ const TString gui_Object::Text() const { // Returns the object text return obj_Text; } //______________________________________________________________________________ bool gui_Object::fontBold() const { // Returns the font-boldness return obj_fontBold; } //______________________________________________________________________________ UInt_t gui_Object::fontSize() const { // Returns the font-boldness return obj_fontSize; } //______________________________________________________________________________ const char* gui_Object::fontName() const { // Returns the font-boldness return obj_fontName.Data(); } //______________________________________________________________________________ void gui_Object::label_Set(const char* labelText, UInt_t align, UInt_t foreColor, UInt_t backColor, bool transp) { // Sets a label to be put around the widget dbg_Print("gui_Object::label_Set:(default)", DBG_LVL_MAKE); // Allow only one label per object if(!gui_obj_Label ){ // Create a label object on purpose gui_obj_Label = new TGLabel(Parent()->Window(), labelText); Parent()->Window()->AddFrame(gui_obj_Label, new TGLayoutHints(kLHintsNormal, 0, 0, 0, 0)); // With a label, the object is transparent by default obj_Transparent = transp; // The horizontal and vertical reference coordinates UInt_t hanchor, vanchor; // Sets label positioning from object dimensions according to align parameter; // hanchor and vanchor are the horizontal and vertical coordinates of the label anchor point switch(align) { // Left | Top = 4 case (gui_Const::align_Left + gui_Const::align_Top) : hanchor = Left(); vanchor = Top() - Height() + (UInt_t)(0.5 * gui_obj_Label->GetDefaultHeight()); break; // Center | Top = 5 case (gui_Const::align_Center + gui_Const::align_Top) : hanchor = Left() + (UInt_t)(0.5*Width()) - (UInt_t)(0.5*gui_obj_Label->GetDefaultWidth()); vanchor = Top() - Height() + (UInt_t)(0.5 * gui_obj_Label->GetDefaultHeight()); break; // Right | Top = 6 case (gui_Const::align_Right + gui_Const::align_Top) : hanchor = Left() + Width() + gui_Const::lbl_offset_Right; vanchor = Top() - Height() + (UInt_t)(0.5 * gui_obj_Label->GetDefaultHeight()); break; // Left | Middle = 8 case (gui_Const::align_Left + gui_Const::align_Middle) : hanchor = Left() - gui_obj_Label->GetDefaultWidth() - gui_Const::lbl_offset_Left; vanchor = Top() + (UInt_t)(0.5 * Height()) - (UInt_t)(0.5 * gui_obj_Label->GetDefaultHeight()); break; // Right | Middle = 10 case (gui_Const::align_Right + gui_Const::align_Middle) : hanchor = Left() + Width() + gui_Const::lbl_offset_Right; vanchor = Top() + (UInt_t)(0.5 * Height()) - (UInt_t)(0.5 * gui_obj_Label->GetDefaultHeight()); break; // Left | Bottom = 16 case (gui_Const::align_Left + gui_Const::align_Bottom) : hanchor = Left(); vanchor = Top() + Height(); break; // Center | Bottom = 17 case (gui_Const::align_Center + gui_Const::align_Bottom) : hanchor = Left() + (UInt_t)(0.5 * Width()) - (UInt_t)(0.5 * gui_obj_Label->GetDefaultWidth()); vanchor = Top() + Height(); break; // Right | Bottom = 18 case (gui_Const::align_Right + gui_Const::align_Bottom) : hanchor = Left() + Width() + gui_Const::lbl_offset_Right; vanchor = Top() + Height(); break; // Default; set equal to left and top default : hanchor = Left(); vanchor = Top() - Height() + (UInt_t)(0.5 * gui_obj_Label->GetDefaultHeight()); break; } // Move the label in place gui_obj_Label->Move(hanchor, vanchor); // Sets label color, if they are not provided uses the object colors if (foreColor <= 0xFFFFFF) gui_obj_Label->SetForegroundColor(foreColor); else gui_obj_Label->SetForegroundColor(this->foreColor()); // Set background color if (transp) { gui_obj_Label->SetBackgroundColor(Parent()->Window()->GetBackground()); } else { if (backColor <= 0xFFFFFF) gui_obj_Label->SetBackgroundColor(backColor); else gui_obj_Label->SetBackgroundColor(Parent()->Window()->GetBackground()); } } else { // Warning dbg_Print("Label already defined", DBG_LVL_WARN); } }