Eclipse SUMO - Simulation of Urban MObility
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
MFXUtils.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2006-2025 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/****************************************************************************/
18// Some helper functions for FOX
19/****************************************************************************/
20#include <config.h>
21
24
25#include "MFXUtils.h"
26
27// ===========================================================================
28// method definitions
29// ===========================================================================
30void
32 while (w->numChildren() != 0) {
33 FXWindow* child = w->childAtIndex(0);
34 delete child;
35 }
36}
37
38
39FXbool
41 const FXString& file) {
42 if (!FXStat::exists(file)) {
43 return TRUE;
44 }
45 int answer =
46 FXMessageBox::question(parent, MBOX_YES_NO, "File Exists", "Overwrite '%s'?", file.text());
47 if (answer == MBOX_CLICKED_NO) {
48 return FALSE;
49 }
50 return TRUE;
51}
52
53
54FXString
55MFXUtils::getDocumentName(const FXString& filename) {
56 return FXPath::name(filename);
57}
58
59
60FXString
61MFXUtils::getTitleText(const FXString& appname, FXString filename) {
62 if (filename.length() == 0) {
63 return appname;
64 }
65 return getDocumentName(filename) + " - " + appname;
66}
67
68
69FXString
70MFXUtils::assureExtension(const FXFileDialog& openDialog) {
71 const auto extensions = parseExtensions(openDialog.getPatternText(openDialog.getCurrentPattern()));
72 const auto filename = openDialog.getFilename();
73 // iterate over all extension to check if is the same extension
74 for (const auto& extension : extensions) {
75 if (extension.length() < filename.length()) {
76 bool sameExtension = true;
77 for (auto i = 0; i < extension.length(); i++) {
78 if (filename[i + filename.length() - extension.length()] != extension[i]) {
79 sameExtension = false;
80 }
81 }
82 if (sameExtension) {
83 return filename;
84 }
85 }
86 }
87 // in this point, we have to give an extension (if exist)
88 if (extensions.size() > 0) {
89 return filename + "." + extensions.front();
90 } else {
91 return filename;
92 }
93}
94
95
96std::vector<FXString>
97MFXUtils::parseExtensions(FXString patternText) {
98 std::vector<FXString> extensions;
99 // first take elementes between parentheses
100 patternText = patternText.after('(');
101 patternText = patternText.before(')');
102 // check files extension
103 if (patternText != "*") {
104 // split extensions
105 const auto extensionsStr = StringTokenizer(patternText.text(), ", ").getVector();
106 for (const auto& extensionStr : extensionsStr) {
107 FXString extension = extensionStr.c_str();
108 extensions.push_back(extension.after('.'));
109 }
110 }
111 return extensions;
112}
113
114
115FXString
116MFXUtils::getFilename2Write(FXWindow* parent, const FXString& header, const FXString& extensions,
117 FXIcon* icon, FXString& currentFolder) {
118 // get the new file name
119 FXFileDialog opendialog(parent, header);
120 opendialog.setIcon(icon);
121 opendialog.setSelectMode(SELECTFILE_ANY);
122 opendialog.setPatternList(extensions);
123 if (currentFolder.length() != 0) {
124 opendialog.setDirectory(currentFolder);
125 }
126 if (!opendialog.execute()) {
127 return "";
128 }
129 const auto filename = assureExtension(opendialog);
130 if (!userPermitsOverwritingWhenFileExists(parent, filename)) {
131 return "";
132 }
133 currentFolder = opendialog.getDirectory();
134 return filename;
135}
136
137
140 return RGBColor(FXREDVAL(col), FXGREENVAL(col), FXBLUEVAL(col), FXALPHAVAL(col));
141}
142
143
144FXColor
146 return FXRGBA(col.red(), col.green(), col.blue(), col.alpha());
147}
148
149
150/****************************************************************************/
static FXString getFilename2Write(FXWindow *parent, const FXString &header, const FXString &extensions, FXIcon *icon, FXString &currentFolder)
Returns the file name to write.
Definition MFXUtils.cpp:116
static void deleteChildren(FXWindow *w)
Deletes all children of the given window.
Definition MFXUtils.cpp:31
static FXString getDocumentName(const FXString &filename)
Returns the document name.
Definition MFXUtils.cpp:55
static FXColor getFXColor(const RGBColor &col)
converts FXColor to RGBColor
Definition MFXUtils.cpp:145
static FXString getTitleText(const FXString &appname, FXString filename="")
Returns the title text in dependence to an optional file name.
Definition MFXUtils.cpp:61
static RGBColor getRGBColor(FXColor col)
converts FXColor to RGBColor
Definition MFXUtils.cpp:139
static FXbool userPermitsOverwritingWhenFileExists(FXWindow *const parent, const FXString &file)
Returns true if either the file given by its name does not exist or the user allows overwriting it.
Definition MFXUtils.cpp:40
static std::vector< FXString > parseExtensions(FXString patternText)
parse extensions
Definition MFXUtils.cpp:97
static FXString assureExtension(const FXFileDialog &openDialog)
Corrects missing extension.
Definition MFXUtils.cpp:70
unsigned char red() const
Returns the red-amount of the color.
Definition RGBColor.cpp:74
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition RGBColor.cpp:92
unsigned char green() const
Returns the green-amount of the color.
Definition RGBColor.cpp:80
unsigned char blue() const
Returns the blue-amount of the color.
Definition RGBColor.cpp:86
std::vector< std::string > getVector()
return vector of strings