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