# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

window_displayer.cpp

Go to the documentation of this file.
00001 
00008 /* Copyright, 2001 Nevrax Ltd.
00009  *
00010  * This file is part of NEVRAX NEL.
00011  * NEVRAX NEL is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2, or (at your option)
00014  * any later version.
00015 
00016  * NEVRAX NEL is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00019  * General Public License for more details.
00020 
00021  * You should have received a copy of the GNU General Public License
00022  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00023  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00024  * MA 02111-1307, USA.
00025  */
00026 
00027 #include "stdmisc.h"
00028 
00029 #include <iostream>
00030 #include <fstream>
00031 #include <iomanip>
00032 #include <signal.h>
00033 
00034 #include "nel/misc/path.h"
00035 #include "nel/misc/command.h"
00036 #include "nel/misc/thread.h"
00037 
00038 #include "nel/misc/window_displayer.h"
00039 
00040 using namespace std;
00041 
00042 namespace NLMISC {
00043 
00044 class CUpdateThread : public IRunnable
00045 {
00046         CWindowDisplayer *Disp;
00047         string WindowNameEx;
00048         sint X, Y, W, H, HS;
00049         uint32 FS;
00050         bool WW;
00051         string FN;
00052         bool Iconified;
00053 
00054 public:
00055         CUpdateThread (CWindowDisplayer *disp, string windowNameEx, bool iconified, sint x, sint y, sint w, sint h, sint hs, sint fs, const std::string &fn, bool ww) :
00056           Disp(disp), WindowNameEx(windowNameEx), X(x), Y(y), W(w), H(h), HS(hs), Iconified(iconified), FS(fs), FN(fn), WW(ww)
00057         {
00058         }
00059 
00060         void run()
00061         {
00062                 Disp->open (WindowNameEx, Iconified, X, Y, W, H, HS, FS, FN, WW);
00063                 Disp->display_main ();
00064         }
00065 };
00066 
00067 CWindowDisplayer::~CWindowDisplayer ()
00068 {
00069         // we have to wait the exit of the thread
00070         _Continue = false;
00071         nlassert (_Thread != NULL);
00072         _Thread->wait();
00073         delete _Thread;
00074 }
00075 
00076 bool CWindowDisplayer::update ()
00077 {
00078         vector<string> copy;
00079         {
00080                 CSynchronized<std::vector<std::string> >::CAccessor access (&_CommandsToExecute);
00081                 copy = access.value();
00082                 access.value().clear ();
00083         }
00084 
00085         // execute all commands in the main thread
00086         for (uint i = 0; i < copy.size(); i++)
00087         {
00088                 ICommand::execute (copy[i], *InfoLog);
00089         }
00090 
00091         return _Continue;
00092 }
00093 
00094 uint CWindowDisplayer::createLabel (const char *value)
00095 {
00096         int pos;
00097         {
00098                 CSynchronized<std::vector<CLabelEntry> >::CAccessor access (&_Labels);
00099                 access.value().push_back (CLabelEntry(value));
00100                 pos = access.value().size()-1;
00101         }
00102         return pos;
00103 }
00104 
00105 void CWindowDisplayer::setLabel (uint label, const string &value)
00106 {
00107         {
00108                 CSynchronized<std::vector<CLabelEntry> >::CAccessor access (&_Labels);
00109                 nlassert (label < access.value().size());
00110                 if (access.value()[label].Value != value)
00111                 {
00112                         access.value()[label].Value = value;
00113                         access.value()[label].NeedUpdate = true;
00114                 }
00115         }
00116 }
00117 
00118 void CWindowDisplayer::create (string windowNameEx, bool iconified, sint x, sint y, sint w, sint h, sint hs, sint fs, const std::string &fn, bool ww)
00119 {
00120         nlassert (_Thread == NULL);
00121         _Thread = IThread::create (new CUpdateThread(this, windowNameEx, iconified, x, y, w, h, hs, fs, fn, ww));
00122         
00123         _Thread->start ();
00124 }
00125 
00126 void CWindowDisplayer::doDisplay (const NLMISC::TDisplayInfo &args, const char *message)
00127 {
00128         bool needSpace = false;
00129         stringstream ss;
00130 
00131         uint32 color = 0xFF000000;
00132 
00133         if (args.LogType != CLog::LOG_NO)
00134         {
00135                 ss << logTypeToString(args.LogType);
00136                 if (args.LogType == CLog::LOG_ERROR || args.LogType == CLog::LOG_ASSERT) color = 0x00FF0000;
00137                 else if (args.LogType == CLog::LOG_WARNING) color = 0x00800000;
00138                 else if (args.LogType == CLog::LOG_DEBUG) color = 0x00808080;
00139                 else color = 0;
00140                 needSpace = true;
00141         }
00142 
00143         // Write thread identifier
00144         if ( args.ThreadId != 0 )
00145         {
00146                 if (needSpace) { ss << " "; needSpace = false; }
00147                 ss << setw(4) << args.ThreadId;
00148                 needSpace = true;
00149         }
00150 
00151         if (args.Filename != NULL)
00152         {
00153                 if (needSpace) { ss << " "; needSpace = false; }
00154                 ss << setw(20) << CFile::getFilename(args.Filename);
00155                 needSpace = true;
00156         }
00157 
00158         if (args.Line != -1)
00159         {
00160                 if (needSpace) { ss << " "; needSpace = false; }
00161                 ss << setw(4) << args.Line;
00162                 needSpace = true;
00163         }
00164 
00165         if (needSpace) { ss << ": "; needSpace = false; }
00166 
00167         uint nbl = 1;
00168 
00169         char *npos, *pos = const_cast<char *>(message);
00170         while ((npos = strchr (pos, '\n')))
00171         {
00172                 *npos = '\0';
00173                 ss << pos;
00174                 if (needSlashR)
00175                         ss << "\r";
00176                 ss << "\n";
00177                 *npos = '\n';
00178                 pos = npos+1;
00179                 nbl++;
00180         }
00181         ss << pos;
00182 
00183         pos = const_cast<char *>(args.CallstackAndLog.c_str());
00184         while ((npos = strchr (pos, '\n')))
00185         {
00186                 *npos = '\0';
00187                 ss << pos;
00188                 if (needSlashR)
00189                         ss << "\r";
00190                 ss << "\n";
00191                 *npos = '\n';
00192                 pos = npos+1;
00193                 nbl++;
00194         }
00195         ss << pos;
00196 
00197         {
00198                 CSynchronized<std::vector<std::pair<uint32, std::string> > >::CAccessor access (&_Buffer);
00199                 access.value().push_back (make_pair (color, ss.str()));
00200         }
00201 }
00202 
00203 } // NLMISC