Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

FuiTheme.cpp

Go to the documentation of this file.
00001 /*
00002  * FuiTheme.cpp
00003  *
00004  * Part of Fly! Legacy project
00005  *
00006  * Copyright 2003 Chris Wallace
00007  *
00008  * Fly! Legacy is free software; you can redistribute it and/or modify
00009  *   it under the terms of the GNU General Public License as published by
00010  *   the Free Software Foundation; either version 2 of the License, or
00011  *   (at your option) any later version.
00012  *
00013  * Fly! Legacy 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 Fly! Legacy; if not, write to the Free Software
00020  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00027 #include "../Include/Fui.h"
00028 #include "../Include/Globals.h"
00029 
00030 using namespace std;
00031 
00032 
00033 //
00034 // Get theme size element type
00035 //
00036 static bool GetSizeType (const char* s, EFuiThemeSizeType &type)
00037 {
00038   bool rc = true;
00039 
00040   if (stricmp (s, "SMALL") == 0) 
00041     type = THEME_SIZE_SMALL;
00042   else if (stricmp (s, "NORMAL") == 0)
00043     type = THEME_SIZE_NORMAL;
00044   else if (stricmp (s, "LARGE") == 0)
00045     type = THEME_SIZE_LARGE;
00046   else if (stricmp (s, "SMALL-INLINE") == 0) 
00047     type = THEME_SIZE_SMALL_INLINE;
00048   else if (stricmp (s, "NORMAL-INLINE") == 0)
00049     type = THEME_SIZE_NORMAL_INLINE;
00050   else if (stricmp (s, "LARGE-INLINE") == 0)
00051     type = THEME_SIZE_LARGE_INLINE;
00052   else
00053     rc = false;
00054 
00055   return rc;
00056 }
00057 
00058 //
00059 // CFuiThemeWidget
00060 //
00061 CFuiThemeWidget::CFuiThemeWidget (void)
00062 {
00063 }
00064 
00065 CFuiThemeWidget::~CFuiThemeWidget (void)
00066 {
00067   // Free bitmaps
00068   map<string,SBitmap*>::iterator i;
00069   for (i=bmMap.begin(); i!=bmMap.end(); i++) {
00070     SBitmap *bm = i->second;
00071     FreeBitmap (bm);
00072     delete bm;
00073   }
00074 }
00075 
00076 void CFuiThemeWidget::ParseBitmap (char* s)
00077 {
00078   // First token is element type
00079   const char *delimiters = " \t";
00080   char *token = strtok (NULL, delimiters);
00081   SBitmap *bm = new SBitmap;
00082 
00083   // Get bitmap name (.PBM/.PBG file) and load
00084   char *bmName = strtok (NULL, delimiters);
00085   strcpy (bm->bitmapName, bmName);
00086   if (!LoadBitmap (bm)) {
00087     // ERROR
00088     gtfo ("CFuiThemeWidget : Invalid bitmap %s", bmName);
00089     delete bm;
00090   }
00091 
00092   // Check if map entry already exists
00093   map<string,SBitmap*>::iterator i = bmMap.find(token);
00094   if (i != bmMap.end()) {
00095     // Generate warning
00096     globals->logWarning->Write ("CFuiThemeWidget : Duplicate bitmap tag %s", token);
00097     FreeBitmap (bm);
00098     delete bm;
00099   } else {
00100     // Add the bitmap to the map
00101     bmMap[token] = bm;
00102   }
00103 }
00104 
00105 void CFuiThemeWidget::ParseColour (char* s)
00106 {
00107   // First token is element name
00108   const char *delimiters = " \t";
00109   char *token = strtok (NULL, delimiters);
00110   string elementName = token;
00111 
00112   // Next tokens are R, G, B colour values
00113   token = strtok (NULL, delimiters);
00114   int r = atoi (token);
00115   token = strtok (NULL, delimiters);
00116   int g = atoi (token);
00117   token = strtok (NULL, delimiters);
00118   int b = atoi (token);
00119 
00120   colourMap[elementName] = MakeRGB (r, g, b);
00121 }
00122 
00123 void CFuiThemeWidget::ParseSize (char* s)
00124 {
00127 
00128   // First token is element type
00129   const char *delimiters = " \t";
00130   char *token = strtok (NULL, delimiters);
00131   EFuiThemeSizeType type;
00132   if (GetSizeType (token, type)) {
00133     // Get tokens for width, height
00134     token = strtok (NULL, delimiters);
00135     token = strtok (NULL, delimiters);
00136   } else {
00137     // Invalid type
00138     gtfo ("CFuiThemeWidget : Invalid SIZE type %s", token);
00139   }
00140 }
00141 
00142 void CFuiThemeWidget::ParseFrameRate (char *s)
00143 {
00144   // First token is element type
00145   const char *delimiters = " \t";
00146   char *token = strtok (NULL, delimiters);
00147   framerate = atoi (token);
00148 }
00149 
00150 void CFuiThemeWidget::ParseThickness (char *s)
00151 {
00152   // First token is element type
00153   const char *delimiters = " \t";
00154   char *token = strtok (NULL, delimiters);
00155   thickness = atoi (token);
00156 }
00157 
00158 void CFuiThemeWidget::ParseComponentName (char *s, string type)
00159 {
00160   // First token is element type
00161   const char *delimiters = " \t";
00162   char *token = strtok (NULL, delimiters);
00163   componentNameMap[type] = token;
00164 }
00165 
00166 void CFuiThemeWidget::ParseButtonWidth (char *s)
00167 {
00168   // First token is element type
00169   const char *delimiters = " \t";
00170   char *token = strtok (NULL, delimiters);
00171   buttonWidth = atoi (token);
00172 }
00173 
00174 void CFuiThemeWidget::SetFlag (string name)
00175 {
00176   flagVector.push_back (name);
00177 }
00178 
00179 void CFuiThemeWidget::SetName (const char* s)
00180 {
00181   strcpy (name, s);
00182 }
00183 
00184 SBitmap *CFuiThemeWidget::GetBitmap (string name)
00185 {
00186   return bmMap[name];
00187 }
00188 
00189 unsigned int CFuiThemeWidget::GetColour (string name)
00190 {
00191   return colourMap[name];
00192 }
00193 
00194 const char *CFuiThemeWidget::GetComponentName (string name)
00195 {
00196   return componentNameMap[name].c_str();
00197 }
00198 
00199 bool CFuiThemeWidget::GetFlag (string name)
00200 {
00201   bool rc = false;
00202 
00203   vector<string>::iterator i;
00204   for (i=flagVector.begin(); i!=flagVector.end(); i++) {
00205     if (i->compare(name) == 0) {
00206       rc = true;
00207       break;
00208     }
00209   }
00210 
00211   return rc;
00212 }
00213 
00214 void CFuiThemeWidget::Print (FILE *f)
00215 {
00216   // Print bitmap elements
00217   map<string,SBitmap*>::iterator i;
00218   for (i=bmMap.begin(); i!=bmMap.end(); i++) {
00219     SBitmap *bm = i->second;
00220     fprintf (f, "\tBITMAP\t%s\t%s\n", i->first.c_str(), bm->bitmapName);
00221   }
00222   
00223   // Print colour elements
00224   map<string,unsigned int>::iterator j;
00225   for (j=colourMap.begin(); j!=colourMap.end(); j++) {
00226     unsigned int r, g, b;
00227     UnmakeRGB (j->second, &r, &g, &b);
00228     fprintf (f, "\tCOLOR\t%s\t%d %d %d\n", j->first.c_str(), r, g, b);
00229   }
00230 
00232 
00233   // Print flags
00234   vector<string>::iterator k;
00235   for (k=flagVector.begin(); k!=flagVector.end(); k++) {
00236     fprintf (f, "\t%s\n", k->c_str());
00237   }
00238 
00239   // Print sub-component names
00240   map<string,string>::iterator m;
00241   for (m=componentNameMap.begin(); m!=componentNameMap.end(); m++) {
00242     fprintf (f, "\t%s\t%s\n", m->first.c_str(), m->second.c_str());
00243   }
00244 }
00245 
00246 
00247 //
00248 // CFuiTheme
00249 //
00250 CFuiTheme::CFuiTheme (void)
00251 {
00252   strcpy (name, "");
00253   id = 0;
00254 }
00255 
00256 CFuiTheme::CFuiTheme (const char *themeFilename)
00257 {
00258   strcpy (name, "");
00259   id = 0;
00260   static string activeWidget;
00261 
00262   // Open .SCH file and parse lines
00263   PODFILE *p = popen (&globals->pfs, themeFilename);
00264   if (p != NULL) {
00265     // Begin parsing lines
00266     char s[256];
00267     pgets (s, 256, p);
00268     const char *delimiters = " \t";
00269     while (!peof(p)) {
00270       // Trim trailing whitespace
00271       TrimTrailingWhitespace (s);
00272 
00273       // Check for comment or blank line
00274       if ((strncmp (s, "//", 2) == 0) || (strlen(s) == 0)) {
00275         pgets (s, 256, p);
00276         continue;
00277       }
00278         
00279       // Check first token
00280       char *token = strtok (s, delimiters);
00281       if (stricmp (token, "THEMENAME") == 0) {
00282         // Get the theme user-visible name
00283         token = strtok (NULL, "");
00284         strcpy (this->name, token);
00285       } else if (stricmp (token, "THEMEID") == 0) {
00286         // Get the theme unique identifier
00287         token = strtok (NULL, delimiters);
00288         this->id = StringToTag (token);
00289       } else if (stricmp (token, "WIDGET") == 0) {
00290         // Set the active widget
00291         token = strtok (NULL, delimiters);
00292         activeWidget = token;
00293         widgetMap[activeWidget] = new CFuiThemeWidget;
00294       } else {
00295 
00296         // If the active widget is valid, parse the element type and allow
00297         //   the widget to parse the details
00298 
00299         if (!activeWidget.empty()) {
00300 
00301           // Get reference to the active widget
00302           CFuiThemeWidget *widget = widgetMap[activeWidget];
00303 
00304           if (stricmp (token, "BITMAP") == 0) {
00305             widget->ParseBitmap (s);
00306           } else if (stricmp (token, "COLOR") == 0) {
00307             widget->ParseColour (s);
00308           } else if (stricmp (token, "SIZE") == 0) {
00309             widget->ParseSize (s);
00310           } else if (stricmp (token, "DONT_DRAW_BACKGROUND") == 0) {
00311             widget->SetFlag (token);
00312           } else if (stricmp (token, "IGNORE_BITMAP_BACKGROUND") == 0) {
00313             widget->SetFlag (token);
00314           } else if (stricmp (token, "USESHADOW") == 0) {
00315             widget->SetFlag (token);
00316           } else if (stricmp (token, "FRAMERATE") == 0) {
00317             widget->ParseFrameRate (s);
00318           } else if (stricmp (token, "THICKNESS") == 0) {
00319             widget->ParseThickness (s);
00320           } else if (stricmp (token, "TITLE") == 0) {
00321             widget->ParseComponentName (s, token);
00322           } else if (stricmp (token, "CLOSE") == 0) {
00323             widget->ParseComponentName (s, token);
00324           } else if (stricmp (token, "MINIMIZE") == 0) {
00325             widget->ParseComponentName (s, token);
00326           } else if (stricmp (token, "ZOOM") == 0) {
00327             widget->ParseComponentName (s, token);
00328           } else if (stricmp (token, "BUTTONWIDTH") == 0) {
00329             widget->ParseButtonWidth (s);
00330           } else {
00331             // Generate warning
00332             globals->logWarning->Write ("CFuiTheme : Unknown element type %s", token);
00333           }
00334         } else {
00335           globals->logWarning->Write ("CFuiTheme : Token %s found without valid widget",
00336             token);
00337         }
00338       }
00339 
00340       // Get next line
00341       pgets (s, 256, p);
00342     }
00343     pclose (p);
00344   } else {
00345     gtfo ("CFuiTheme : Cannot open theme file %s", themeFilename);
00346   }
00347 }
00348 
00349 CFuiTheme::~CFuiTheme (void)
00350 {
00351   map<string,CFuiThemeWidget*>::iterator i;
00352   for (i=widgetMap.begin(); i!=widgetMap.end(); i++) {
00353     delete i->second;
00354   }
00355 }
00356 
00357 CFuiThemeWidget *CFuiTheme::GetWidget (string name)
00358 {
00359   CFuiThemeWidget *rc = NULL;
00360 
00361   map<string,CFuiThemeWidget*>::iterator i = widgetMap.find(name);
00362   if (i != widgetMap.end()) {
00363     rc = i->second;
00364   }
00365 
00366   return rc;
00367 }
00368 
00369 void CFuiTheme::Print (FILE *f)
00370 {
00371   fprintf (f, "THEMENAME\t%s\n", name);
00372   char s[8];
00373   TagToString (s, id);
00374   fprintf (f, "THEMEID\t\t%s\n", s);
00375   map<string,CFuiThemeWidget*>::iterator i;
00376   for (i=widgetMap.begin(); i!=widgetMap.end(); i++) {
00377     fprintf (f, "WIDGET\t%s\n", i->first.c_str());
00378     i->second->Print (f);
00379     fprintf (f, "\n");
00380   }
00381   fprintf (f, "\n");
00382 }
00383 
SourceForge.net Logo Documentation generated by doxygen