00001 /* 00002 File : gadget_t.h 00003 Date : 19-Sep-02 00004 Description : Class to represent a generic toolbox gadget template. 00005 00006 Copyright © 1995-2002 Alexander Thoukydides 00007 00008 This program is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU General Public License 00010 as published by the Free Software Foundation; either version 2 00011 of the License, or (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 00021 */ 00022 00023 // Only include header file once 00024 #ifndef gadget_t_h 00025 #define gadget_t_h 00026 00027 // Include alexlib header files 00028 #ifndef gadget_b_h 00029 #include "gadget_b.h" 00030 #endif 00031 00032 // A class to represent a generic gadget template 00033 class gadget_t : public virtual gadget_b 00034 { 00035 public: 00036 00037 #if 0 00038 00039 /* 00040 Constructors need to allocate a block of memory for help_message 00041 and set help_limit appropriately. 00042 class_no and size should be set to the correct constants. 00043 By default component ID should be -1 (toolbox_NULL_COMPONENT) 00044 00045 Need to provide mechanism to read/write component ID 00046 */ 00047 00048 /* 00049 Parameters : header - Pointer to a gadget template structure. 00050 size - Optional size of the gadget object. If 00051 omitted then the size field in the header 00052 must be correct. 00053 Returns : - 00054 Description : Constructor function. 00055 */ 00056 gadget_t(const gadget_object *header, size_t size = 0); 00057 00058 /* 00059 Parameters : header - Gadget template to base this on. 00060 Returns : - 00061 Description : Constructor function. 00062 */ 00063 gadget_t(const gadget_t &header); 00064 00065 /* 00066 Parameters : void 00067 Returns : - 00068 Description : Destructor function. 00069 */ 00070 virtual ~gadget_t(); 00071 00072 /* 00073 Parameters : header - Gadget template to assign. 00074 Returns : gadget_t & - The modified template. 00075 Description : Assign another gadget template. 00076 */ 00077 gadget_t &operator=(const gadget_t &header); 00078 00079 /* 00080 Parameters : void 00081 Returns : gadget_t * - Pointer to a new gadget template. 00082 Description : Clone this gadget template. 00083 */ 00084 virtual gadget_t *clone() const; 00085 00086 /* 00087 Parameters : void 00088 Returns : gadget_object * - Pointer to the corresponding 00089 gadget object. This pointer will 00090 remain valid until this object 00091 is destroyed. 00092 Description : Conversion to a pointer to a gadget object. 00093 */ 00094 operator (const gadget_object *)(); 00095 00096 /* 00097 Parameters : limit - Maximum help message size including terminator. 00098 Returns : void 00099 Description : Set the maximum help message length. 00100 */ 00101 void set_help_message_limit(size_t limit); 00102 00103 /* 00104 Parameters : void 00105 Returns : size_t - Maximum help message size including 00106 terminator. 00107 Description : Get the maximum help message length. 00108 */ 00109 size_t get_help_message_limit() const; 00110 00111 protected: 00112 00113 // Details for this gadget 00114 gadget_object *header; 00115 00116 /* 00117 Parameters : flags - The new flag settings. 00118 Returns : void 00119 Description : Representation specific function to set the flags for 00120 this gadget. 00121 */ 00122 virtual void _set_flags(bits flags); 00123 00124 /* 00125 Parameters : void 00126 Returns : gadget_flags - The flag settings for this gadget. 00127 Description : Representation specific function to get the flags for 00128 this gadget. 00129 */ 00130 virtual gadget_flags _get_flags() const; 00131 00132 /* 00133 Parameters : help - The help text. 00134 Returns : void 00135 Description : Representation specific function to set the help text. 00136 */ 00137 virtual void _set_help_message(const char *help); 00138 00139 /* 00140 Parameters : help - Pointer to buffer to hold the help text, or 00141 NULL to read the size of buffer required. 00142 size - The size of the buffer. 00143 Returns : size_t - Number of bytes written to the buffer, or 00144 the buffer size required if NULL passed. 00145 Description : Representation specific function to get the help text or 00146 the buffer size required. 00147 */ 00148 virtual size_t _get_help_message(char *help, size_t size) const; 00149 00150 /* 00151 Parameters : void 00152 Returns : int - The type of this gadget. 00153 Description : Representation specific function to get the type of this 00154 gadget. 00155 */ 00156 virtual int _get_type() const; 00157 00158 /* 00159 Parameters : bbox - The new bounding box for this gadget. 00160 Returns : void 00161 Description : Representation specific function to set the bounding box 00162 of this gadget. 00163 */ 00164 virtual void _set_bbox(const os_box &bbox); 00165 00166 /* 00167 Parameters : void 00168 Returns : os_box - The bounding box of this gadget. 00169 Description : Representation specific function to get the bounding box 00170 of this gadget. 00171 */ 00172 virtual os_box _get_bbox() const; 00173 00174 private: 00175 00176 /* 00177 Parameters : header - Pointer to a gadget template structure, 00178 or NULL to create a default gadget. 00179 size - Size of the gadget object. 00180 Returns : void 00181 Description : Allocate a block of memory for the gadget template, and 00182 either copy the supplied structure or create a default. 00183 */ 00184 void _create(const gadget_object *header, size_t size); 00185 00186 /* 00187 Parameters : void 00188 Returns : void 00189 Description : Deallocate the block of memory used for the gadget 00190 template. 00191 */ 00192 void _destroy(); 00193 00194 #endif 00195 00196 }; 00197 00198 #if 0 00199 00200 /* 00201 Parameters : header - Pointer to a gadget template structure. 00202 size - Optional size of the gadget object. If 00203 omitted then the size field in the header 00204 must be correct. 00205 Returns : - 00206 Description : Constructor function. 00207 */ 00208 inline gadget_t::gadget_t(const gadget_object *header, 00209 size_t size) 00210 { 00211 _create(header, size ? size : header->size); 00212 } 00213 00214 /* 00215 Parameters : header - Gadget template to base this on. 00216 Returns : - 00217 Description : Constructor function. 00218 */ 00219 inline gadget_t::gadget_t(const gadget_t &header) 00220 { 00221 _create(header.header, header.header->size); 00222 } 00223 00224 /* 00225 Parameters : void 00226 Returns : - 00227 Description : Destructor function. 00228 */ 00229 inline gadget_t::~gadget_t() 00230 { 00231 _destroy(); 00232 } 00233 00234 /* 00235 Parameters : header - Gadget template to assign. 00236 Returns : gadget_t & - The modified template. 00237 Description : Assign another gadget template. 00238 */ 00239 inline gadget_t &gadget_t::operator=(const gadget_t &header) 00240 { 00241 _destroy(); 00242 _create(header.header, header.header->size); 00243 return *this; 00244 } 00245 00246 /* 00247 Parameters : void 00248 Returns : gadget_t * - Pointer to a new gadget template. 00249 Description : Clone this gadget template. 00250 */ 00251 inline gadget_t *gadget_t::clone() const 00252 { 00253 return new gadget_t(*this); 00254 } 00255 00256 /* 00257 Parameters : limit - Maximum help message size including terminator. 00258 Returns : void 00259 Description : Set the maximum help message length. 00260 */ 00261 inline void gadget_t::set_help_message_limit(size_t limit) 00262 { 00263 if (limit < 1) limit = 1; 00264 char *ptr = new char[limit]; 00265 if (ptr) 00266 { 00267 strncpy(ptr, header->help_message, limit - 1); 00268 ptr[limit - 1] = '\0'; 00269 delete[] header->help_message; 00270 header->help_message = ptr; 00271 header->help_limit = limit; 00272 } 00273 } 00274 00275 /* 00276 Parameters : void 00277 Returns : size_t - Maximum help message size including 00278 terminator. 00279 Description : Get the maximum help message length. 00280 */ 00281 inline size_t gadget_t::get_help_message_limit() const 00282 { 00283 return header->help_limit; 00284 } 00285 00286 /* 00287 Parameters : flags - The new flag settings. 00288 Returns : void 00289 Description : Representation specific function to set the flags for 00290 this gadget. 00291 */ 00292 inline void gadget_t::_set_flags(bits flags) 00293 { 00294 header->flags = flags; 00295 } 00296 00297 /* 00298 Parameters : void 00299 Returns : gadget_flags - The flag settings for this gadget. 00300 Description : Representation specific function to get the flags for 00301 this gadget. 00302 */ 00303 inline gadget_flags gadget_t::_get_flags() const 00304 { 00305 return header->flags; 00306 } 00307 00308 /* 00309 Parameters : help - The help text. 00310 Returns : void 00311 Description : Representation specific function to set the help text. 00312 */ 00313 inline void gadget_t::_set_help_message(const char *help) 00314 { 00315 NOT_USED(help); 00316 /* 00317 // Constructor from window block will need to copy any help text buffers 00318 // Also need to update help_limit 00319 delete[] header->help_message; 00320 if (help) 00321 { 00322 header->help_message = new char[strlen(help) + 1]; 00323 // Should throw an error if memory allocation failed 00324 strcpy(header->help_message, help); 00325 } 00326 else header->help_message = NULL; 00327 */ 00328 } 00329 00330 /* 00331 Parameters : help - Pointer to buffer to hold the help text, or 00332 NULL to read the size of buffer required. 00333 size - The size of the buffer. 00334 Returns : size_t - Number of bytes written to the buffer, or 00335 the buffer size required if NULL passed. 00336 Description : Representation specific function to get the help text or 00337 the buffer size required. 00338 */ 00339 inline size_t gadget_t::_get_help_message(char *help, size_t size) const 00340 { 00341 size_t req = header->help_message ? strlen(header->help_message) + 1 : 1; 00342 if (help) 00343 { 00344 if (req < size) size = req; 00345 if (1 < size) strncpy(help, header->help_message, size - 1); 00346 if (size) help[size - 1] = '\0'; 00347 } 00348 return req; 00349 } 00350 00351 /* 00352 Parameters : void 00353 Returns : int - The type of this gadget. 00354 Description : Representation specific function to get the type of this 00355 gadget. 00356 */ 00357 inline int gadget_t::_get_type() const 00358 { 00359 return header->class_no; 00360 } 00361 00362 /* 00363 Parameters : bbox - The new bounding box for this gadget. 00364 Returns : void 00365 Description : Representation specific function to set the bounding box 00366 of this gadget. 00367 */ 00368 inline void gadget_t::_set_bbox(const os_box &bbox) 00369 { 00370 header->bbox = bbox; 00371 } 00372 00373 /* 00374 Parameters : void 00375 Returns : os_box - The bounding box of this gadget. 00376 Description : Representation specific function to get the bounding box 00377 of this gadget. 00378 */ 00379 inline os_box gadget_t::_get_bbox() const 00380 { 00381 return header->bbox; 00382 } 00383 00384 inline void gadget_t::_default(toolbox_c class_no, size_t size) 00385 { 00386 header->flags = 0; 00387 header->class_no = class_no; 00388 header->size = size; 00389 header->bbox.x0 = header->bbox.y0 = header->bbox.x1 = header->bbox.y1 = 0; 00390 header->cmp = toolbox_NULL_COMPONENT; 00391 header->help_message = NULL; 00392 header->help_limit = 0; 00393 _add_string(&header->help_message, &header->help_limit); 00394 } 00395 00396 /* 00397 Parameters : header - Pointer to a gadget template structure, 00398 or NULL to create a default gadget. 00399 size - Size of the gadget object. 00400 Returns : void 00401 Description : Allocate a block of memory for the gadget template, and 00402 either copy the supplied structure or create a default. 00403 */ 00404 inline void gadget_t::_create(const gadget_object *header, size_t size) 00405 { 00406 gadget_t::header = (gadget_object *) new char[size]; 00407 if (header) memcpy(gadget_t::header, header, size); 00408 else 00409 { 00410 gadget_t::flags = 0; 00411 gadget_t::class_no = 0; // Can't have this! 00412 gadget_t:: 00413 } 00414 } 00415 00416 /* 00417 Parameters : void 00418 Returns : void 00419 Description : Deallocate the block of memory used for the gadget 00420 template. 00421 */ 00422 inline void gadget_t::_destroy() 00423 { 00424 delete[] (char *) header; 00425 } 00426 00427 #endif 00428 00429 #endif
1.3.3