LCOV - code coverage report
Current view: top level - src/utils/gui/div - GUIMessageWindow.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 65.0 % 217 141
Test Date: 2026-10-12 15:53:19 Functions: 50.0 % 18 9

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2003-2026 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    GUIMessageWindow.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Michael Behrisch
      17              : /// @author  Jakob Erdmann
      18              : /// @date    Tue, 25 Nov 2003
      19              : ///
      20              : // A logging window for the gui
      21              : /****************************************************************************/
      22              : #include <config.h>
      23              : 
      24              : #include <cassert>
      25              : #include <utils/common/MsgHandler.h>
      26              : #include <utils/gui/globjects/GUIGlObjectStorage.h>
      27              : #include <utils/gui/windows/GUIGlChildWindow.h>
      28              : #include <utils/gui/windows/GUIMainWindow.h>
      29              : #include <utils/gui/windows/GUIAppEnum.h>
      30              : #include <utils/gui/div/GUIDesigns.h>
      31              : #include <utils/gui/div/GUIGlobalSelection.h>
      32              : #include <utils/gui/images/GUIIconSubSys.h>
      33              : #include <fxkeys.h>
      34              : #include "GUIMessageWindow.h"
      35              : 
      36              : 
      37              : // ===========================================================================
      38              : // static members
      39              : // ===========================================================================
      40              : 
      41              : bool GUIMessageWindow::myLocateLinks = true;
      42              : SUMOTime GUIMessageWindow::myBreakPointOffset = TIME2STEPS(-5);
      43              : FXHiliteStyle* GUIMessageWindow::myStyles = new FXHiliteStyle[8];
      44              : std::string GUIMessageWindow::myTimeText;
      45              : std::map<std::string, std::string> GUIMessageWindow::myTypeStrings;
      46              : 
      47              : // ===========================================================================
      48              : // FOX callback mapping
      49              : // ===========================================================================
      50              : 
      51              : FXDEFMAP(GUIMessageWindow) GUIMessageWindowMap[] = {
      52              :     FXMAPFUNC(SEL_KEYPRESS,           0,                      GUIMessageWindow::onKeyPress),
      53              :     FXMAPFUNC(SEL_RIGHTBUTTONRELEASE, 0,                      GUIMessageWindow::onRightBtnRelease),
      54              :     FXMAPFUNC(SEL_COMMAND,            MID_CLEARMESSAGEWINDOW, GUIMessageWindow::onCmdClear),
      55              :     FXMAPFUNC(SEL_UPDATE,             MID_CLEARMESSAGEWINDOW, GUIMessageWindow::onUpdClear),
      56              : };
      57              : 
      58      2160264 : FXIMPLEMENT_ABSTRACT(GUIMessageWindow, FXText, GUIMessageWindowMap, ARRAYNUMBER(GUIMessageWindowMap))
      59              : 
      60              : // ===========================================================================
      61              : // method definitions
      62              : // ===========================================================================
      63         9020 : GUIMessageWindow::GUIMessageWindow(FXComposite* parent, GUIMainWindow* mainWindow) :
      64              :     FXText(parent, nullptr, 0, 0, 0, 0, 0, 50),
      65         9020 :     myMainWindow(mainWindow),
      66         9020 :     myErrorRetriever(nullptr),
      67         9020 :     myMessageRetriever(nullptr),
      68         9020 :     myWarningRetriever(nullptr) {
      69         9020 :     setStyled(true);
      70         9020 :     setEditable(false);
      71              :     // fill styles
      72         9020 :     fillStyles();
      73              :     // set styles
      74         9020 :     setHiliteStyles(myStyles);
      75         9020 :     myTimeText = TL(" time");
      76              :     // see GUIGlObject.cpp
      77         9020 :     myTypeStrings[StringUtils::to_lower_case(TL("edge"))] = "edge";
      78         9020 :     myTypeStrings[StringUtils::to_lower_case(TL("lane"))] = "lane";
      79         9020 :     myTypeStrings[StringUtils::to_lower_case(TL("junction"))] = "junction";
      80         9020 :     myTypeStrings[StringUtils::to_lower_case(TL("vehicle"))] = "vehicle";
      81         9020 :     myTypeStrings[StringUtils::to_lower_case(TL("person"))] = "person";
      82         9020 :     myTypeStrings[StringUtils::to_lower_case(TL("tlLogic"))] = "tlLogic";
      83         9020 :     myTypeStrings[StringUtils::to_lower_case(TL("busStop"))] = "busStop";
      84         9020 :     myTypeStrings[StringUtils::to_lower_case(TL("trainStop"))] = "busStop";
      85         9020 :     myTypeStrings[StringUtils::to_lower_case(TL("containerStop"))] = "containerStop";
      86         9020 :     myTypeStrings[StringUtils::to_lower_case(TL("chargingStation"))] = "chargingStation";
      87         9020 :     myTypeStrings[StringUtils::to_lower_case(TL("overheadWireSegment"))] = "overheadWireSegment";
      88         9020 :     myTypeStrings[StringUtils::to_lower_case(TL("parkingArea"))] = "parkingArea";
      89         9020 : }
      90              : 
      91              : 
      92        17986 : GUIMessageWindow::~GUIMessageWindow() {
      93         8993 :     delete[] myStyles;
      94         8993 :     delete myMessageRetriever;
      95         8993 :     delete myErrorRetriever;
      96         8993 :     delete myWarningRetriever;
      97        17986 : }
      98              : 
      99              : 
     100              : const GUIGlObject*
     101       106874 : GUIMessageWindow::getActiveStringObject(const FXString& text, const FXint pos, const FXint lineS, const FXint lineE) const {
     102       106874 :     const FXint idS = MAX2(text.rfind(" '", pos), text.rfind("='", pos));
     103       106874 :     const FXint idE = text.find("'", pos);
     104       106874 :     if (idS >= 0 && idE >= 0 && idS >= lineS && idE <= lineE) {
     105        80210 :         FXint typeS = text.rfind(" ", idS - 1);
     106        80210 :         if (typeS >= 0) {
     107        78588 :             if (text.at(typeS + 1) == '(') {
     108              :                 typeS++;
     109              :             }
     110        78588 :             std::string type(text.mid(typeS + 1, idS - typeS - 1).lower().text());
     111              :             const auto& tsIt = myTypeStrings.find(type);
     112        78588 :             if (tsIt != myTypeStrings.end()) {
     113        49247 :                 type = tsIt->second;
     114              :             }
     115        78588 :             const std::string id(text.mid(idS + 2, idE - idS - 2).text());
     116        78588 :             const std::string typedID = type + ":" + id;
     117        78588 :             const GUIGlObject* o = GUIGlObjectStorage::gIDStorage.getObjectBlocking(typedID);
     118              :             //std::cout << " getActiveStringObject '" << typedID << "' o=" << (o == nullptr ? "NULL" : o->getMicrosimID()) << "\n";
     119              :             return o;
     120              :         }
     121              :     }
     122              :     return nullptr;
     123              : }
     124              : 
     125              : SUMOTime
     126        26431 : GUIMessageWindow::getTimeString(const FXString& text, const FXint pos) const {
     127        26431 :     const FXint end = text.find_first_of(" ,", pos + 1);
     128              :     std::string time;
     129        26431 :     if (end >= 0) {
     130         3070 :         time = text.mid(pos, end - pos).text();
     131              :     } else {
     132        49792 :         time = text.mid(pos, text.length() - pos).text();
     133        24896 :         if (time.empty()) {
     134              :             return -1;
     135              :         }
     136        24896 :         if (time.back() == '\n') {
     137              :             time.pop_back();
     138              :         }
     139        24896 :         if (time.empty()) {
     140              :             return -1;
     141              :         }
     142        24893 :         if (time.back() == '.') {
     143              :             time.pop_back();
     144              :         }
     145              :     }
     146        26428 :     if (time.empty()) {
     147              :         return -1;
     148              :     }
     149        26381 :     if (time.front() == ' ') {
     150        17068 :         time = time.substr(1);
     151              :     }
     152              :     //std::cout << "text='" << text.text() << "' pos=" << pos << " time='" << time << "'\n";
     153              :     try {
     154              :         //std::cout << "  SUMOTime=" << string2time(time) << "\n";
     155        26381 :         return string2time(time);
     156           94 :     } catch (...) {
     157              :         return -1;
     158           94 :     }
     159              : }
     160              : 
     161              : 
     162              : void
     163            0 : GUIMessageWindow::setCursorPos(FXint pos, FXbool notify) {
     164            0 :     FXText::setCursorPos(pos, notify);
     165            0 :     if (myLocateLinks) {
     166            0 :         std::vector<std::string> viewIDs = myMainWindow->getViewIDs();
     167            0 :         if (viewIDs.empty()) {
     168              :             return;
     169              :         }
     170            0 :         GUIGlChildWindow* const child = myMainWindow->getViewByID(viewIDs[0]);
     171            0 :         const FXString text = getText();
     172            0 :         const GUIGlObject* const glObj = getActiveStringObject(text, pos, lineStart(pos), lineEnd(pos));
     173            0 :         if (glObj != nullptr) {
     174            0 :             child->setView(glObj->getGlID());
     175            0 :             GUIGlObjectStorage::gIDStorage.unblockObject(glObj->getGlID());
     176            0 :             if (getApp()->getKeyState(KEY_Control_L)) {
     177            0 :                 gSelected.toggleSelection(glObj->getGlID());
     178              :             }
     179            0 :         } else if (gSimulation) {
     180              :             const int lookback = MIN2(pos, 20);
     181            0 :             const int start = MAX2(lineStart(pos), pos - lookback);
     182            0 :             const FXString candidate = text.mid(start, lineEnd(pos) - start);
     183            0 :             FXint timePos = candidate.find(myTimeText.c_str());
     184            0 :             if (timePos > -1) {
     185            0 :                 timePos += (int)myTimeText.size() + 1;
     186            0 :                 if (pos >= 0 && pos > start + timePos) {
     187            0 :                     const SUMOTime t = getTimeString(candidate, timePos);
     188            0 :                     if (t >= 0) {
     189            0 :                         myMainWindow->addBreakpoint(t + myBreakPointOffset);
     190              :                     }
     191              :                 }
     192              :             }
     193            0 :         }
     194            0 :     }
     195              : }
     196              : 
     197              : 
     198              : void
     199        80434 : GUIMessageWindow::appendMsg(GUIEventType eType, const std::string& msg) {
     200        80434 :     if (!isEnabled()) {
     201            0 :         show();
     202              :     }
     203              :     // build the styled message
     204              :     FXint style = 1;
     205        80434 :     switch (eType) {
     206              :         case GUIEventType::DEBUG_OCCURRED:
     207              :             // color: blue
     208              :             style = 0;
     209              :             break;
     210              :         case GUIEventType::GLDEBUG_OCCURRED:
     211              :             // color: fuchsia
     212              :             style = 7;
     213              :             break;
     214              :         case GUIEventType::ERROR_OCCURRED:
     215              :             // color: red
     216              :             style = 2;
     217              :             break;
     218              :         case GUIEventType::WARNING_OCCURRED:
     219              :             // color: yellow
     220              :             style = 3;
     221              :             break;
     222              :         case GUIEventType::MESSAGE_OCCURRED:
     223              :             // color: green
     224              :             style = 1;
     225              :             break;
     226        80434 :         default:
     227              :             assert(false);
     228              :     }
     229       160868 :     FXString text(msg.c_str());
     230        80434 :     if (myLocateLinks) {
     231        80434 :         FXint pos = text.find("'");
     232       187308 :         while (pos >= 0) {
     233       106874 :             const GUIGlObject* const glObj = getActiveStringObject(text, pos + 1, 0, text.length());
     234       106874 :             if (glObj != nullptr) {
     235        30844 :                 GUIGlObjectStorage::gIDStorage.unblockObject(glObj->getGlID());
     236        30844 :                 FXString insText = text.left(pos + 1);
     237        30844 :                 FXText::appendStyledText(insText, style + 1);
     238        30844 :                 text.erase(0, pos + 1);
     239        30844 :                 pos = text.find("'");
     240        30844 :                 insText = text.left(pos);
     241        30844 :                 FXText::appendStyledText(insText, style + 4);
     242        30844 :                 text.erase(0, pos);
     243        30844 :             }
     244       106874 :             pos = text.find("'", pos + 1);
     245              :         }
     246              :         // find time links
     247        80434 :         pos = text.find(myTimeText.c_str());
     248        80434 :         const int timeTerm = (int)myTimeText.size() + 1;
     249              :         SUMOTime t = -1;
     250        80434 :         if (pos >= 0) {
     251        26431 :             t = getTimeString(text, pos + timeTerm);
     252              :         }
     253        26431 :         if (t >= 0) {
     254        26287 :             FXString insText = text.left(pos + timeTerm);
     255        26287 :             FXText::appendStyledText(insText, style + 1);
     256        26287 :             text.erase(0, pos + timeTerm);
     257        26287 :             pos = text.find(" ");
     258        26287 :             if (pos < 0) {
     259        16285 :                 pos = text.rfind(".");
     260              :             }
     261        26287 :             insText = text.left(pos);
     262        26287 :             FXText::appendStyledText(insText, style + 4);
     263        26287 :             text.erase(0, pos);
     264        26287 :         }
     265              :     }
     266              :     // insert rest of the message
     267        80434 :     FXText::appendStyledText(text, style + 1, true);
     268        80434 :     FXText::setCursorPos(getLength() - 1);
     269        80434 :     FXText::setBottomLine(getLength() - 1);
     270        80434 :     if (isEnabled()) {
     271        80434 :         layout();
     272        80434 :         update();
     273              :     }
     274        80434 : }
     275              : 
     276              : 
     277              : void
     278        26487 : GUIMessageWindow::addSeparator() {
     279        26487 :     std::string msg = std::string(100, '-') + "\n";
     280        26487 :     FXText::appendStyledText(msg.c_str(), (FXint) msg.length(), 1, true);
     281        26487 :     FXText::setCursorPos(getLength() - 1);
     282        26487 :     FXText::setBottomLine(getLength() - 1);
     283        26487 :     if (isEnabled()) {
     284        26487 :         layout();
     285        26487 :         update();
     286              :     }
     287        26487 : }
     288              : 
     289              : 
     290              : void
     291            0 : GUIMessageWindow::clear() {
     292            0 :     if (getLength() == 0) {
     293              :         return;
     294              :     }
     295            0 :     FXText::removeText(0, getLength(), true);
     296            0 :     if (isEnabled()) {
     297            0 :         layout();
     298            0 :         update();
     299              :     }
     300              : }
     301              : 
     302              : 
     303              : void
     304            0 : GUIMessageWindow::registerMsgHandlers() {
     305            0 :     if (myMessageRetriever == nullptr) {
     306              :         // initialize only if registration is requested
     307            0 :         myMessageRetriever = new MsgOutputDevice(this, GUIEventType::MESSAGE_OCCURRED);
     308            0 :         myErrorRetriever = new MsgOutputDevice(this, GUIEventType::ERROR_OCCURRED);
     309            0 :         myWarningRetriever = new MsgOutputDevice(this, GUIEventType::WARNING_OCCURRED);
     310              :     }
     311            0 :     MsgHandler::getMessageInstance()->addRetriever(myMessageRetriever);
     312            0 :     MsgHandler::getErrorInstance()->addRetriever(myErrorRetriever);
     313            0 :     MsgHandler::getWarningInstance()->addRetriever(myWarningRetriever);
     314            0 : }
     315              : 
     316              : 
     317              : void
     318            0 : GUIMessageWindow::unregisterMsgHandlers() {
     319            0 :     MsgHandler::getMessageInstance()->removeRetriever(myMessageRetriever);
     320            0 :     MsgHandler::getErrorInstance()->removeRetriever(myErrorRetriever);
     321            0 :     MsgHandler::getWarningInstance()->removeRetriever(myWarningRetriever);
     322            0 : }
     323              : 
     324              : 
     325              : long
     326            0 : GUIMessageWindow::onKeyPress(FXObject* o, FXSelector sel, void* ptr) {
     327              :     FXEvent* e = (FXEvent*) ptr;
     328              :     // permit ctrl+a, ctrl+c
     329            0 :     if (e->state & CONTROLMASK) {
     330            0 :         return FXText::onKeyPress(o, sel, ptr);
     331              :     }
     332              :     return 0;
     333              : }
     334              : 
     335              : 
     336              : long
     337            0 : GUIMessageWindow::onRightBtnRelease(FXObject* o, FXSelector sel, void* ptr) {
     338            0 :     FXText::onRightBtnRelease(o, sel, ptr);
     339            0 :     if (!isEnabled()) {
     340              :         return 0;
     341              :     }
     342              :     FXEvent* event = (FXEvent*)ptr;
     343            0 :     if (event->moved) {
     344              :         return 1;
     345              :     }
     346            0 :     setFocus();
     347            0 :     FXMenuPane contextMenu(this);
     348            0 :     FXMenuCommand* copyCmd = GUIDesigns::buildFXMenuCommandShortcut(&contextMenu, TL("Copy"), "Ctrl+C", TL("Copy selection to clipboard"),
     349              :                              GUIIconSubSys::getIcon(GUIIcon::COPY), this, FXText::ID_COPY_SEL);
     350            0 :     FXMenuCommand* selAllCmd = GUIDesigns::buildFXMenuCommandShortcut(&contextMenu, TL("Select all"), "Ctrl+A", TL("Select all text"),
     351              :                                nullptr, this, FXText::ID_SELECT_ALL);
     352            0 :     if (getSelStartPos() >= getSelEndPos()) {
     353            0 :         copyCmd->disable();
     354              :     }
     355            0 :     if (getLength() == 0) {
     356            0 :         selAllCmd->disable();
     357              :     }
     358            0 :     new FXMenuSeparator(&contextMenu);
     359            0 :     FXMenuCommand* clearCmd = GUIDesigns::buildFXMenuCommand(&contextMenu, TL("Clear"), TL("Clear message window"),
     360              :                               GUIIconSubSys::getIcon(GUIIcon::CLEARMESSAGEWINDOW), this, MID_CLEARMESSAGEWINDOW);
     361            0 :     if (getLength() == 0) {
     362            0 :         clearCmd->disable();
     363              :     }
     364            0 :     contextMenu.create();
     365            0 :     contextMenu.popup(nullptr, event->root_x, event->root_y);
     366            0 :     getApp()->runModalWhileShown(&contextMenu);
     367              :     return 1;
     368              : }
     369              : 
     370              : 
     371              : long
     372            0 : GUIMessageWindow::onCmdClear(FXObject*, FXSelector, void*) {
     373            0 :     clear();
     374            0 :     return 1;
     375              : }
     376              : 
     377              : 
     378              : long
     379            0 : GUIMessageWindow::onUpdClear(FXObject* sender, FXSelector, void* ptr) {
     380            0 :     sender->handle(this, getLength() > 0 ? FXSEL(SEL_COMMAND, ID_ENABLE) : FXSEL(SEL_COMMAND, ID_DISABLE), ptr);
     381            0 :     return 1;
     382              : }
     383              : 
     384              : 
     385              : FXHiliteStyle*
     386            0 : GUIMessageWindow::getStyles() {
     387            0 :     return myStyles;
     388              : }
     389              : 
     390              : 
     391              : void
     392         9020 : GUIMessageWindow::fillStyles() {
     393              :     const FXColor white   = FXRGB(0xff, 0xff, 0xff);
     394              :     const FXColor blue    = FXRGB(0x00, 0x00, 0x88);
     395              :     const FXColor green   = FXRGB(0x00, 0x88, 0x00);
     396              :     const FXColor red     = FXRGB(0x88, 0x00, 0x00);
     397              :     const FXColor yellow  = FXRGB(0xe6, 0x98, 0x00);
     398              :     const FXColor fuchsia = FXRGB(0x88, 0x00, 0x88);
     399              :     // set separator style
     400         9020 :     myStyles[0].normalForeColor = blue;
     401         9020 :     myStyles[0].normalBackColor = white;
     402         9020 :     myStyles[0].selectForeColor = white;
     403         9020 :     myStyles[0].selectBackColor = blue;
     404         9020 :     myStyles[0].hiliteForeColor = blue;
     405         9020 :     myStyles[0].hiliteBackColor = white;
     406         9020 :     myStyles[0].activeBackColor = white;
     407         9020 :     myStyles[0].style = 0;
     408              :     // set message text style
     409         9020 :     myStyles[1] = myStyles[0];
     410         9020 :     myStyles[1].normalForeColor = green;
     411         9020 :     myStyles[1].selectBackColor = green;
     412         9020 :     myStyles[1].hiliteForeColor = green;
     413         9020 :     myStyles[4] = myStyles[1];
     414         9020 :     myStyles[4].style = STYLE_UNDERLINE;
     415              :     // set error text style
     416         9020 :     myStyles[2] = myStyles[0];
     417         9020 :     myStyles[2].normalForeColor = red;
     418         9020 :     myStyles[2].selectBackColor = red;
     419         9020 :     myStyles[2].hiliteForeColor = red;
     420         9020 :     myStyles[5] = myStyles[2];
     421         9020 :     myStyles[5].style = STYLE_UNDERLINE;
     422              :     // set warning text style
     423         9020 :     myStyles[3] = myStyles[0];
     424         9020 :     myStyles[3].normalForeColor = yellow;
     425         9020 :     myStyles[3].selectBackColor = yellow;
     426         9020 :     myStyles[3].hiliteForeColor = yellow;
     427         9020 :     myStyles[6] = myStyles[3];
     428         9020 :     myStyles[6].style = STYLE_UNDERLINE;
     429              :     // set GLDebug text style
     430         9020 :     myStyles[7] = myStyles[0];
     431         9020 :     myStyles[7].normalForeColor = fuchsia;
     432         9020 :     myStyles[7].selectBackColor = fuchsia;
     433         9020 :     myStyles[7].hiliteForeColor = fuchsia;
     434         9020 : }
     435              : 
     436              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1