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