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