1 //------------------------------------------------------------------------------
2 // emFileDialog.cpp
3 //
4 // Copyright (C) 2015-2016 Oliver Hamann.
5 //
6 // Homepage: http://eaglemode.sourceforge.net/
7 //
8 // This program is free software: you can redistribute it and/or modify it under
9 // the terms of the GNU General Public License version 3 as published by the
10 // Free Software Foundation.
11 //
12 // This program is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
15 // more details.
16 //
17 // You should have received a copy of the GNU General Public License version 3
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 //------------------------------------------------------------------------------
20
21 #include <emCore/emFileDialog.h>
22 #include <emCore/emLabel.h>
23
24
emFileDialog(emContext & parentContext,ModeType mode,ViewFlags viewFlags,WindowFlags windowFlags,const emString & wmResName)25 emFileDialog::emFileDialog(
26 emContext & parentContext, ModeType mode, ViewFlags viewFlags,
27 WindowFlags windowFlags, const emString & wmResName
28 ) :
29 emDialog(parentContext,viewFlags,windowFlags,wmResName)
30 {
31 Fsb=new emFileSelectionBox(GetContentPanel(),"fsb");
32 Fsb->SetBorderType(emBorder::OBT_NONE,emBorder::IBT_NONE);
33
34 Mode=mode;
35 DirAllowed=false;
36
37 AddOKButton();
38 AddCancelButton();
39
40 SetMode(mode);
41
42 AddWakeUpSignal(Fsb->GetFileTriggerSignal());
43 }
44
45
~emFileDialog()46 emFileDialog::~emFileDialog()
47 {
48 }
49
50
SetMode(ModeType mode)51 void emFileDialog::SetMode(ModeType mode)
52 {
53 emButton * okButton;
54
55 Mode=mode;
56 okButton=GetOKButton();
57 switch (mode) {
58 case M_SELECT:
59 SetRootTitle("Files");
60 if (okButton) okButton->SetCaption("OK");
61 break;
62 case M_OPEN:
63 SetRootTitle("Open");
64 if (okButton) okButton->SetCaption("Open");
65 break;
66 case M_SAVE:
67 SetRootTitle("Save As");
68 if (okButton) okButton->SetCaption("Save");
69 break;
70 }
71 }
72
73
SetDirectoryResultAllowed(bool dirAllowed)74 void emFileDialog::SetDirectoryResultAllowed(bool dirAllowed)
75 {
76 DirAllowed=dirAllowed;
77 }
78
79
Cycle()80 bool emFileDialog::Cycle()
81 {
82 bool busy;
83
84 busy=emDialog::Cycle();
85
86 if (IsSignaled(Fsb->GetFileTriggerSignal())) {
87 Finish(POSITIVE);
88 }
89
90 if (OverwriteDialog && IsSignaled(OverwriteDialog->GetFinishSignal())) {
91 switch (OverwriteDialog->GetResult()) {
92 case POSITIVE:
93 OverwriteConfirmed=OverwriteAsked;
94 OverwriteAsked.Clear();
95 delete OverwriteDialog.Get();
96 Finish(POSITIVE);
97 break;
98 case NEGATIVE:
99 OverwriteAsked.Clear();
100 delete OverwriteDialog.Get();
101 break;
102 }
103 }
104
105 return busy;
106 }
107
108
CheckFinish(int result)109 bool emFileDialog::CheckFinish(int result)
110 {
111 emArray<emString> names;
112 emArray<emString> pathsToOverwrite;
113 emString path,text;
114 int i;
115
116 if (!emDialog::CheckFinish(result)) return false;
117 if (result == NEGATIVE) return true;
118
119 if (!DirAllowed) {
120 names=GetSelectedNames();
121 if (names.GetCount()==0) {
122 // GetSelectedPath() returns a directory...
123 emDialog::ShowMessage(
124 *this,
125 "Error",
126 emString::Format("No file selected")
127 );
128 return false;
129 }
130 for (i=0; i<names.GetCount(); i++) {
131 path=emGetChildPath(GetParentDirectory(),names[i]);
132 if (emIsDirectory(path)) {
133 if (names.GetCount()==1) {
134 Fsb->EnterSubDir(names[i]);
135 }
136 else {
137 emDialog::ShowMessage(
138 *this,
139 "Error",
140 emString::Format("Directory selected: %s",names[i].Get())
141 );
142 }
143 return false;
144 }
145 }
146 }
147
148 if (Mode==M_OPEN) {
149 names=GetSelectedNames();
150 for (i=0; i<names.GetCount(); i++) {
151 path=emGetChildPath(GetParentDirectory(),names[i]);
152 if (!emIsExistingPath(path)) {
153 emDialog::ShowMessage(
154 *this,
155 "Open Error",
156 emString::Format(
157 "The following file cannot be opened, because it does not exist:\n\n%s",
158 path.Get()
159 )
160 );
161 return false;
162 }
163 }
164 }
165 else if (Mode==M_SAVE) {
166 names=GetSelectedNames();
167 pathsToOverwrite.Clear();
168 for (i=0; i<names.GetCount(); i++) {
169 path=emGetChildPath(GetParentDirectory(),names[i]);
170 if (emIsExistingPath(path)) {
171 pathsToOverwrite.Add(path);
172 }
173 }
174 if (!pathsToOverwrite.IsEmpty()) {
175 if (pathsToOverwrite.GetCount() == 1) {
176 text="Are you sure to overwrite the following already existing file?\n";
177 }
178 else {
179 text="Are you sure to overwrite the following already existing files?\n";
180 }
181 for (i=0; i<pathsToOverwrite.GetCount(); i++) {
182 text+="\n";
183 text+=pathsToOverwrite[i];
184 }
185 if (text!=OverwriteConfirmed) {
186 if (OverwriteDialog) delete OverwriteDialog.Get();
187 OverwriteAsked=text;
188 OverwriteDialog=new emDialog((emContext&)*this);
189 OverwriteDialog->SetRootTitle("File Exists");
190 new emLabel(
191 OverwriteDialog->GetContentPanel(),
192 "label",
193 text
194 );
195 OverwriteDialog->AddOKCancelButtons();
196 AddWakeUpSignal(OverwriteDialog->GetFinishSignal());
197 return false;
198 }
199 }
200 OverwriteConfirmed.Clear();
201 }
202
203 return true;
204 }
205