00001
00002
00003
00004
00005 #ifdef WIN32
00006 #pragma warning(disable : 4786 )
00007 #endif
00008
00009 #include "CSHTMLPanel.h"
00010 #include "CSFont.h"
00011 #include "CSDesktop.h"
00012 #include "CSWindow.h"
00013 #include "CSLAF.h"
00014 #include "CSHelper.h"
00015 #include "CSHTMLElement.h"
00016
00017 const char *CSHTMLPanel::CLASS = "CSHTMLPanel";
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 CSHTMLPanel::CSHTMLPanel(int height, int width, const std::string &filename)
00040 : CSGrafikElement(height, width)
00041 {
00042 static char *functionName="CSHTMLPanel";
00043 LOG_ENTER
00044 initHTMLPanel();
00045 setPage(filename);
00046 LOG_EXIT
00047 }
00048
00049 CSHTMLPanel::~CSHTMLPanel()
00050 {
00051 if (mMessageBox)
00052 {
00053 delete mMessageBox;
00054 mMessageBox = 0;
00055 }
00056 deleteOldPage();
00057 }
00058
00059 void CSHTMLPanel::initHTMLPanel()
00060 {
00061 static char *functionName="initHTMLPanel";
00062 LOG_ENTER
00063 CSLAF *laf = CSLAF::getCurrentLAF();
00064 mMessageBox = 0;
00065 mBaseDirectory = "";
00066 mHelper = 0;
00067 mHTMLHeightInPixel = 0;
00068 mHTMLWidthInPixel = 0;
00069 mCleared = true;
00070 addMessageListener(this, GUI_MESSAGE);
00071
00072 CSLayoutManagerXY *layoutManager = new CSLayoutManagerXY();
00073
00074
00075
00076
00077
00078
00079
00080
00081 layoutManager->setPropagateSizeChangesOnly(true);
00082 setLayoutManager(layoutManager);
00083
00084 rebuildElement();
00085
00086 CSLayoutData layout = getLayoutData();
00087 layout.setCenteredHorizontal(true);
00088 layout.setCenteredVertical(true);
00089 layout.setStretchedHorizontal(true);
00090 layout.setStretchedVertical(true);
00091 layout.setPosition(POSITION_CENTER);
00092 setLayoutData(layout);
00093 setInset(CSInset(0));
00094 LOG_EXIT
00095 }
00096
00097 void CSHTMLPanel::deleteOldPage()
00098 {
00099 if (mHelper != 0)
00100 {
00101 delete mHelper;
00102 mHelper = 0;
00103 }
00104 HTMLElements::iterator iter;
00105 for (iter = mHTMLElements.begin(); iter != mHTMLElements.end();)
00106 {
00107 HTMLElement *element = *iter;
00108 mHTMLElements.erase(iter);
00109 delete element;
00110 iter = mHTMLElements.begin();
00111 }
00112 mHTMLHeightInPixel = 0;
00113 mHTMLWidthInPixel = 0;
00114 }
00115
00116
00117
00118 std::string CSHTMLPanel::getBaseDirectory(const std::string &filename)
00119 {
00120
00121 char *posStart = strrchr(filename.c_str(), '/');
00122 if (posStart != 0)
00123 {
00124 std::string newText = filename.substr(0, (posStart - filename.c_str()+1));
00125 return newText;
00126 }
00127 posStart = strrchr(filename.c_str(), '\\');
00128 if (posStart != 0)
00129 {
00130 std::string newText = filename.substr(0, (posStart - filename.c_str()+1));
00131 return newText;
00132 }
00133 return "";
00134 }
00135
00136
00137
00138 std::string CSHTMLPanel::extractAnker(std::string &filename)
00139 {
00140
00141 char *posStart = strrchr(filename.c_str(), '#');
00142 if (posStart != 0)
00143 {
00144 std::string newFilename = filename.substr(0, (posStart - filename.c_str()));
00145 std::string anker = filename.substr((posStart - filename.c_str())+1, filename.size());
00146 filename = newFilename;
00147 return anker;
00148 }
00149 return "";
00150 }
00151
00152
00153
00154 void CSHTMLPanel::setPage(const std::string &filename)
00155 {
00156 static char *functionName="setPage";
00157
00158
00159
00160 CSHTMLHelper *helper;
00161 std::string newFilename = filename;
00162 std::string anker = extractAnker(newFilename);
00163 helper = new CSHTMLHelper(newFilename);
00164 if (helper->getError() != CSHTML_OK)
00165 {
00166 delete helper;
00167 helper = 0;
00168
00169 newFilename = mBaseDirectory + filename;
00170 anker = extractAnker(newFilename);
00171 helper = new CSHTMLHelper(newFilename);
00172 if (helper->getError() != CSHTML_OK)
00173 {
00174 delete helper;
00175 helper = 0;
00176 if (mMessageBox)
00177 {
00178 removeFrontElement(mMessageBox);
00179 delete mMessageBox;
00180 mMessageBox = 0;
00181 }
00182 mMessageBox = new CSMessageBox("Error loading Page:\n"+filename, "Error loading html page!", ERROR_ICON);
00183 if (mMessageBox)
00184 {
00185 addFrontElementCenter(mMessageBox);
00186 mMessageBox->setVisible(true);
00187 }
00188
00189 return;
00190 }
00191 }
00192 mBaseDirectory = getBaseDirectory(newFilename);
00193 deleteOldPage();
00194 mHelper = helper;
00195 mFilename = filename;
00196 CSHTMLNode node = mHelper->getRootNode();
00197
00198
00199
00200 HTMLElement *element = new HTMLElement(node);
00201 mHTMLElements.push_back(element);
00202 mHTMLHeightInPixel = 0;
00203 mHTMLWidthInPixel = 0;
00204 CSWindow *window = getParentWindow();
00205
00206
00207
00208
00209
00210
00211
00212
00213 {
00214 if (window)
00215 {
00216 window->setVerticalDisplayStart(0);
00217 window->setHorizontalDisplayStart(0);
00218
00219
00220 }
00221 }
00222 layoutChanged(true);
00223 }
00224
00225
00226
00227 void CSHTMLPanel::paint(SDL_Surface *destination, SDL_Rect *parentViewport)
00228 {
00229 static char *functionName="paint";
00230 paintChildren(destination, parentViewport);
00231 }
00232
00233 void CSHTMLPanel::reactOnMessage(CSMessage *message)
00234 {
00235 reactOnMessageHTMLPanel(message);
00236 }
00237
00238 void CSHTMLPanel::reactOnMessageHTMLPanel(CSMessage *message)
00239 {
00240 static char *functionName="reactOnMessage";
00241 if (message->mIsHandled)
00242 {
00243 reactOnMessageGrafikElement(message);
00244 return;
00245 }
00246 switch (message->getType())
00247 {
00248 case GUI_MESSAGE:
00249 {
00250 GuiMessage *gm = (GuiMessage *) message;
00251 if (gm->receiver != this)
00252 {
00253 }
00254 else
00255 {
00256 switch (message->getSubtype())
00257 {
00258
00259 case MOUSE_BUTTON_PRESSED_MESSAGE:
00260 {
00261 int x,y;
00262 x = gm->receiverX;
00263 y = gm->receiverY;
00264
00265 HTMLElements::iterator iter;
00266 for (iter = mHTMLElements.begin(); iter != mHTMLElements.end(); iter++)
00267 {
00268 HTMLElement *checked = (*iter)->checkPostion(x - getViewportX() ,y - getViewportY());
00269 if (checked != 0)
00270 {
00271
00272 HTMLElement *linkNode = checked->getAncestorType(HTML_A);
00273 if (linkNode)
00274 {
00275
00276
00277 std::string linkValue = linkNode->getAttributValue("HREF");
00278 if (linkValue.size() != 0)
00279 {
00280 setPage(linkValue);
00281 break;
00282 }
00283 }
00284 }
00285 }
00286 gm->mIsHandled = true;
00287 break;
00288 }
00289 }
00290 }
00291 }
00292 }
00293 reactOnMessageGrafikElement(message);
00294 }
00295
00296
00297
00298
00299
00300
00301 int CSHTMLPanel::getBackgroundColorHTML()
00302 {
00303 HTMLElement *bodyElement = 0;
00304 int value = -1;
00305 HTMLElements::iterator iter;
00306 for (iter = mHTMLElements.begin(); iter != mHTMLElements.end(); iter++)
00307 {
00308 HTMLElement *element = *iter;
00309 bodyElement = element->getChildType(HTML_BODY, "", "");
00310 if (bodyElement)
00311 {
00312 std::string bgColor = bodyElement->getAttributValue("BGCOLOR");
00313 value = bodyElement->getColorFromString(bgColor);
00314 break;
00315 }
00316 }
00317 if (value == -1)
00318 {
00319 CSLAF *laf = CSLAF::getCurrentLAF();
00320 value = laf->getBackgroundColorEnabled(CSWindow::CLASS);
00321 }
00322 return value;
00323 }
00324
00325
00326 void CSHTMLPanel::startAtAnker(const std::string &anker)
00327 {
00328 HTMLElement *ankerElement = 0;
00329
00330 HTMLElements::iterator iter;
00331 for (iter = mHTMLElements.begin(); iter != mHTMLElements.end(); iter++)
00332 {
00333 HTMLElement *element = *iter;
00334 ankerElement = element->getChildType(HTML_A, "NAME", anker);
00335 if (ankerElement)
00336 {
00337 break;
00338 }
00339 }
00340
00341 CSWindow *window = getParentWindow();
00342 if ((ankerElement) && (window))
00343 {
00344 int y = ankerElement->getFirstYPostion();
00345 if (y != -1)
00346 {
00347 window->setVerticalDisplayStart(y);
00348 window->setHorizontalDisplayStart(0);
00349 }
00350 }
00351 }
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 void CSHTMLPanel::getHTMLSize(int &w, int &h)
00365 {
00366 static char *functionName="getVerticalHTMLSize";
00367
00368
00369
00370
00371
00372
00373 clearElements();
00374 CSLAF *laf = CSLAF::getCurrentLAF();
00375
00376 int x = 0;
00377 int y = 0;
00378
00379
00380
00381 SDL_Rect thisViewportArea = getViewportArea();
00382 CSGrafikElement *parent = getParent();
00383 if (parent)
00384 {
00385 thisViewportArea = parent->getViewportArea();
00386 thisViewportArea.w-=30;
00387 thisViewportArea.h-=30;
00388
00389
00390 if (thisViewportArea.w > 60000)
00391 {
00392 thisViewportArea.w = 0;
00393 }
00394 if (thisViewportArea.h < 60000)
00395 {
00396 thisViewportArea.h = 0;
00397 }
00398 }
00399
00400 HTMLCursor htmlCursor;
00401 htmlCursor.mXPos = 0;
00402 htmlCursor.mYPos = 0;
00403 htmlCursor.mStyle = 0;
00404 htmlCursor.mFont = getFont();
00405 htmlCursor.mXPosMax = 0;
00406 htmlCursor.nextAdd = htmlCursor.mFont->getHeight();
00407 HTMLElements::iterator iter;
00408 for (iter = mHTMLElements.begin(); iter != mHTMLElements.end(); iter++)
00409 {
00410 std::string title = "";
00411 HTMLElement *element = *iter;
00412
00413 htmlCursor = element->buildDisplay(htmlCursor, 0, thisViewportArea.w, title);
00414 }
00415
00416
00417
00418
00419
00420 if (thisViewportArea.w < htmlCursor.mXPosMax)
00421 {
00422 thisViewportArea.w = htmlCursor.mXPosMax-30;
00423 clearElements();
00424 htmlCursor.mXPos = 0;
00425 htmlCursor.mYPos = 0;
00426 htmlCursor.mStyle = 0;
00427 htmlCursor.mFont = getFont();
00428 htmlCursor.mXPosMax = 0;
00429 htmlCursor.nextAdd = htmlCursor.mFont->getHeight();
00430 for (iter = mHTMLElements.begin(); iter != mHTMLElements.end(); iter++)
00431 {
00432 std::string title = "";
00433 HTMLElement *element = *iter;
00434
00435 htmlCursor = element->buildDisplay(htmlCursor, 0, thisViewportArea.w, title);
00436 }
00437 }
00438
00439 h = mHTMLHeightInPixel = htmlCursor.mYPos + htmlCursor.mFont->getHeight();
00440 w = mHTMLWidthInPixel = htmlCursor.mXPosMax;
00441 }
00442
00443 void CSHTMLPanel::rebuildElement()
00444 {
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 }
00455
00456 void CSHTMLPanel::clearElements()
00457 {
00458 HTMLElements::iterator iter;
00459 for (iter = mHTMLElements.begin(); iter != mHTMLElements.end(); iter++)
00460 {
00461 HTMLElement *element = *iter;
00462 element->clearPosition();
00463 }
00464 deleteElements();
00465 mCleared = true;
00466 }
00467
00468
00469
00470 void CSHTMLPanel::layoutSetupHTMLPanel()
00471 {
00472 static char *functionName="layoutSetupHTMLPanel";
00473 LOG_ENTER
00474 CSLAF *laf = CSLAF::getCurrentLAF();
00475
00476
00477
00478 getHTMLSize(mMinWidth, mMinHeight);
00479 clearElements();
00480
00481 if (mCleared)
00482 {
00483
00484 HTMLCursor htmlCursor;
00485 htmlCursor.mBackgroundColor = getBackgroundColor();
00486 htmlCursor.mXPos = 0;
00487 htmlCursor.mYPos = 0;
00488 htmlCursor.mXPosMax = 0;
00489
00490 htmlCursor.mFont = getFont();
00491 htmlCursor.nextAdd = htmlCursor.mFont->getHeight();
00492
00493
00494
00495 HTMLElements::iterator iter;
00496 for (iter = mHTMLElements.begin(); iter != mHTMLElements.end(); iter++)
00497 {
00498 std::string title = "";
00499 HTMLElement *element = *iter;
00500 htmlCursor = element->buildDisplay(htmlCursor, this, getViewportWidth(), title);
00501 if (title.size() >0)
00502 {
00503 CSWindow *window = getParentWindow();
00504 if (window)
00505 {
00506 window->setTitle(title);
00507 }
00508 }
00509 }
00510 mCleared = false;
00511 }
00512
00513 setBackgroundColor(getBackgroundColorHTML());
00514 }