/* * Copyright (C) 2004-2005 Vadim Berezniker * http://www.kryptolus.com * * This Program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This Program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GNU Make; see the file COPYING. If not, write to * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * http://www.gnu.org/copyleft/gpl.html * */ #include "stdafx.h" #include "common.h" /* * Represents a collection of colors (usually associated with a UI element). * Color tables might be shared between multiple objects so a reference count is kept by * the object. The color IDs utilized for the colors within the color table must start * at 0 and continue sequentially. * * The reference counting functions must only be used if the object is dynamically allocated. */ /* * Initializes the table with given amount of colors. All the colors are created in an unitialized state. * n_colors: Number of colors the table will have. * The object is created with a reference count of one. */ kryColorTable::kryColorTable(int n_colors) : kryObject(kryColorTable::SIGNAL_COUNT), m_refcount(1) { if(n_colors < 0) { g_warning("Attempt to create a color table with a negative number of colors."); n_colors = 0; } this->m_colors = new kryColor [n_colors]; this->m_color_count = n_colors; } /* * Destroys all the color data stored inside. * * If the object is dynamically allocated, it should not be destroyed by the user. * Instead the user should use the Unref method. */ kryColorTable::~kryColorTable() { if(this->GetRefCount() > 0) g_warning("Color Table is being destroyed even though the reference count is not zero."); delete [] this->m_colors; } /* * Checks if the given ID is within the allowed range. * Returns TRUE if it is, FALSE if it is not. */ gboolean kryColorTable::CheckId(int id) { if(id < 0 || id > this->m_color_count) { g_warning("Requested color is out of range of the color table. "); g_warning("ID must be greater than 0 and less than %d", this->m_color_count); return FALSE; } return TRUE; } /* * Returns a pointer to the color with the given ID. * NOTE: The pointer is only valid as long as this color table object still exists. * If the ID is out of range, NULL is returned. */ kryColor *kryColorTable::Get(int id) { if(!this->CheckId(id)) return NULL; return &this->m_colors[id]; } /* * Static wrapper for the kryColorChanged method. * (Called when a kryColor within the table changes) * * Notifies users of the color table that a color has changed. */ void kryColorTable::kryColorChangedW(kryObject *obj, void *param, kryColorTable *color_table) { kryColor *color = (kryColor *) obj; color_table->InvokeSignal(kryColorTable::SIGNAL_COLOR_CHANGED, color); } /* * Utility function for initializing a color. * Same as calling the Get method and then initializing the returned color. * See the Set method of the kryColor class for parameter details. * * If the ID is not valid, nothing is affected. */ void kryColorTable::Set(int id, char *t_id, char *name, int red, int green, int blue) { if(!this->CheckId(id)) return; /* We want to know when a color changes so we can notify the users of this table. */ if(!this->m_colors[id].IsInitialized()) this->m_colors[id].ConnectSignal(kryColor::SIGNAL_CHANGED, (krySignalFunc1) kryColorTable::kryColorChangedW, this); this->m_colors[id].Set(id, t_id, name, red, green, blue); } /* * Utility function for retreiving user data within a color. * See the GetUserData method of kryColor for more information. * * If the ID is not valid, returns NULL. */ void *kryColorTable::GetUserData(int id) { if(!this->CheckId(id)) return NULL; return this->m_colors[id].GetUserData(); } /* * Utility function for storing user data within a color. * See the SetUserData method of kryColor for more information. * * If the ID is not valid, nothing is affected. */ void kryColorTable::SetUserData(int id, void *data) { if(!this->CheckId(id)) return; this->m_colors[id].SetUserData(data); } /* * Returns the reference count of the object. */ int kryColorTable::GetRefCount() { return this->m_refcount; } /* * Increases the reference count of this object. * * This function can only be used if the table object is dynamically allocated. */ void kryColorTable::Ref() { this->m_refcount++; } /* * Reduces the reference count by one. * * If this was the last reference, the object is destroyed. * In this case it is important to make sure there are no pointers to * this object that might be used. * * This function can only be used if the table object is dynamically allocated. */ void kryColorTable::Unref() { this->m_refcount--; /* This is potentially dangerous because if the reference count is incorrect, a user might use a pointer to a destroyed object. */ if(this->m_refcount == 0) delete this; } /* * Returns the number of colors in the color table. */ int kryColorTable::GetColorCount() { return this->m_color_count; } /* * Enables the opacity channel on all the initialized colors within the color table. */ void kryColorTable::EnableOpacity() { for(int i = 0; i < this->m_color_count; i++) this->m_colors[i].EnableOpacity(); }