1 namespace gui::controls;
5 default extern int __ecereVMethodID_class_OnGetDataFromString;
6 default static void _workAround()
9 a.OnGetDataFromString(0);
13 /* We need a dynamic cast :) Easy eC compiler introduction contribution! :)
14 static define pathBoxDataBox = dynamic_cast(pathBox.master);
15 static define pathBoxListBox = pathBoxEditorDataBox ? dynamic_cast((pathBoxDataBox).parent) : null;
16 static define pathBoxDirsBox = pathBoxEditorListBox ? dynamic_cast((pathBoxListBox).parent) : null;
18 static Class _classDataBox() { return class(DataBox); }
19 static Class _classListBox() { return class(ListBox); } // Fix #908
20 static Class _classDirectoriesBox() { return class(DirectoriesBox); }
21 static define pathBoxDataBox = eClass_IsDerived( pathBox.master._class, _classDataBox() ) ? (DataBox)pathBox.master : null;
22 static define pathBoxListBox = (pathBoxDataBox && eClass_IsDerived((pathBoxDataBox).parent._class, _classListBox() )) ? (ListBox)(pathBoxDataBox).parent : null;
23 static define pathBoxDirsBox = (pathBoxListBox && eClass_IsDerived((pathBoxListBox).parent._class, _classDirectoriesBox())) ? (DirectoriesBox)(pathBoxListBox).parent : null;
25 public class FilePath : String
27 Window OnEdit(DataBox dataBox, DataBox obsolete, int x, int y, int w, int h, void * userData)
31 dataBox, borderStyle = 0, anchor = { 0, 0, 0, 0 },
35 DirectoriesBox dirsBox = pathBoxDirsBox;
37 pathBox.browseDialog = dirsBox.browseDialog;
38 // TOCHECK: compiler issues?
39 /*else if(userData && eClass_IsDerived(userData._class, class(FileDialog)))
40 pathBox.browseDialog = (FileDialog)userData;*/
42 pathBox.browseDialog = { type = open, text = $"Select a file..." };
47 bool OnSaveEdit(PathBox pathBox, void * object)
50 if(pathBox.modifiedDocument)
52 DirectoriesBox dirsBox = pathBoxDirsBox;
54 dirsBox.NotifyPathBoxModified(dirsBox.master, dirsBox, pathBox);
56 changed = ((bool (*)(void *, void *, const char *))(void *)_class._vTbl[__ecereVMethodID_class_OnGetDataFromString])(_class, &this, pathBox.systemPath);
61 int OnCompare(DirPath b)
63 return fstrcmp(this, b);
67 public class DirPath : FilePath
69 Window OnEdit(DataBox dataBox, DataBox obsolete, int x, int y, int w, int h, void * userData)
73 dataBox, borderStyle = 0, anchor = { 0, 0, 0, 0 },
74 typeExpected = directory;
77 DirectoriesBox dirsBox = pathBoxDirsBox;
79 pathBox.browseDialog = dirsBox.browseDialog;
80 // TOCHECK: compiler issues? (same)
81 /*else if(userData && eClass_IsDerived(userData._class, class(FileDialog)))
82 pathBox.browseDialog = (FileDialog)userData;*/
84 pathBox.browseDialog = { type = selectDir, text = $"Select a folder..." };
89 bool OnSaveEdit(PathBox pathBox, void * object)
92 if(pathBox.modifiedDocument)
94 DirectoriesBox dirsBox = pathBoxDirsBox;
96 dirsBox.NotifyPathBoxModified(dirsBox.master, dirsBox, pathBox);
98 changed = ((bool (*)(void *, void *, const char *))(void *)_class._vTbl[__ecereVMethodID_class_OnGetDataFromString])(_class, &this, pathBox.systemPath);
105 public enum PathTypeExpected { none, any, directory, file };
107 public class PathBox : CommonControl
110 clientSize = { 64, 18 };
112 watch(background) { editBox.background = background; };
113 watch(foreground) { editBox.foreground = foreground; };
114 watch(opacity) { editBox.opacity = opacity; };
116 #if defined(__WIN32__)
123 PathTypeExpected typeExpected;
124 FileDialog browseDialog;
125 #if defined(__WIN32__)
126 char path[MAX_LOCATION];
129 BitmapResource file { "<:ecere>mimeTypes/file.png", transparent = true, alphaBlend = true };
130 BitmapResource brokenFile { "<:ecere>mimeTypes/brokenFile.png", transparent = true, alphaBlend = true };
131 BitmapResource folder { "<:ecere>places/folder.png", transparent = true, alphaBlend = true };
132 BitmapResource brokenFolder { "<:ecere>places/brokenFolder.png", transparent = true, alphaBlend = true };
136 this/*, size = { 318, 94 }*/, anchor = { left = 1, vert = 0 };
137 image = brokenFolder;
143 public property EditBox editBox { get { return editBox; } }
146 this/*, size = { 290, 22 }*/, position = { 1, 1 }, anchor = { left = 1, top = 1, right = 1 };
149 void NotifyUpdate(EditBox editBox)
152 modifiedDocument = true;
154 bool NotifyModified(EditBox editBox)
156 PathBox pathBox = this;
157 DirectoriesBox dirsBox = pathBoxDirsBox;
159 dirsBox.NotifyPathBoxModified(dirsBox.master, dirsBox, this);
160 return NotifyModified(master, this);
163 /* F4? F4 is properties, f2 works already for this
164 bool OnKeyDown(Key key, unichar ch)
167 ((PathBox)parent).browse.NotifyClicked(parent, ((PathBox)parent).browse, 0, 0, (Modifiers)null);
168 return EditBox::OnKeyDown(key, ch);
173 // For chaining popup-key event
174 bool OnKeyHit(Key key, unichar ch)
176 return editBox.OnKeyHit(key, ch);
181 this, size = { w = 26 }, anchor = { top = 0, right = 0, bottom = 0 };
187 bool NotifyClicked(Button button, int x, int y, Modifiers mods)
191 char browsePath[MAX_LOCATION];
192 char * baseBrowsePath = null;
193 PathBox pathBox = this;
194 DataBox dataBox = pathBoxDataBox;
196 DirectoriesBox dirsBox = pathBoxDirsBox;
197 char * ebContents = editBox.contents;
198 String backFilePath = CopyString(browseDialog.filePath);
200 browsePath[0] = '\0';
201 strncpy(browsePath, browseDialog.filePath, MAX_LOCATION); browsePath[MAX_LOCATION-1] = '\0';
202 if(dirsBox && dirsBox.baseBrowsePath && dirsBox.baseBrowsePath[0] && ((ebContents && ebContents[0]) || !backFilePath || !backFilePath[0]))
203 PathCat(browsePath, dirsBox.baseBrowsePath);
204 PathCat(browsePath, ebContents);
205 browseDialog.filePath = (ebContents && ebContents[0]) ? browsePath : "";
206 if(pathBox.typeExpected == directory && browsePath[0] && FileExists(browsePath).isDirectory && backFilePath && backFilePath[0])
207 StripLastDirectory(browsePath, browsePath);
208 while(browsePath[0] && !FileExists(browsePath).isDirectory)
209 StripLastDirectory(browsePath, browsePath);
212 char path[MAX_LOCATION];
213 LocateModule(null, path);
214 StripLastDirectory(path, path);
215 strncpy(browsePath, path, MAX_LOCATION); browsePath[MAX_LOCATION-1] = '\0';
217 browseDialog.currentDirectory = browsePath;
218 browseDialog.master = rootWindow;
221 if(dirsBox) dirsBox.browsing = true;
222 if(browseDialog.Modal())
224 PathBox pathBox = dataBox ? (PathBox)dataBox.editor : this;
225 pathBox.modifiedDocument = true;
226 pathBox.property::path = browseDialog.filePath;
227 pathBox.NotifyModified(pathBox.master, this);
231 pathBox.editBox.SelectAll();
232 if((listBox = pathBoxListBox))
233 listBox.StopEditing(true);
236 browseDialog.filePath = backFilePath;
238 if(dirsBox) dirsBox.browsing = false;
245 void CheckFileExists()
247 if(typeExpected != none)
249 BitmapResource icon = null;
250 char path[MAX_LOCATION];
251 FileAttribs exists { };
252 GetSystemPathBuffer(path, editBox.contents);
253 if(!(path[0] == DIR_SEP && path[1] == DIR_SEP && (!path[2] || !strchr(&path[2], DIR_SEP))))
254 exists = FileExists(path);
259 // TODO: improvements, add drive, etc, also find a better solution/icon for expect any and file doesn't exist
260 icon = exists ? exists.isFile ? file : exists.isDirectory ? folder : null : null;
263 icon = exists && exists.isDirectory ? folder : brokenFolder;
266 icon = exists && exists.isFile ? file : brokenFile;
270 picture.image = icon;
274 void OnRedraw(Surface surface)
278 surface.SetBackground(formColor);
279 surface.Area(0, 0, clientSize.w, clientSize.h);
282 Window::OnRedraw(surface);
291 virtual bool Window::NotifyModified(PathBox pathBox);
293 property PathTypeExpected typeExpected
297 if(value != typeExpected)
299 if(value == none || typeExpected == none)
301 bool withIcon = value != none;
302 picture.visible = withIcon;
303 editBox.anchor.left = withIcon ? 18 : 1;
306 typeExpected = value;
307 if(browseDialog && browseDialog.type == open && typeExpected == directory)
308 browseDialog.type = selectDir;
313 property FileDialog browseDialog
318 browseDialog = value;
322 if(browseDialog.type == open && typeExpected == directory)
323 browseDialog.type = selectDir;
324 if(!strcmp(browseDialog.text, "Select a file...") && text)
326 char temp[1024] = "Select ";
329 browseDialog.text = temp;
332 browse.visible = browseDialog ? true : false;
333 editBox.anchor.right = browseDialog ? 26 : 1;
335 get { return browseDialog; }
338 void Home() { editBox.Home(); }
339 void End() { editBox.End(); }
341 void SelectAll() { editBox.SelectAll(); }
342 void Deselect() { editBox.Deselect(); }
348 char path[MAX_LOCATION];
349 GetSystemPathBuffer(path, value);
350 editBox.contents = path;
355 get { return editBox.contents; }
357 property String slashPath { get { return GetSlashPathBuffer (path, editBox.contents); } };
358 property String systemPath { get { return GetSystemPathBuffer(path, editBox.contents); } };
360 property Color selectionColor { set { editBox.selectionColor = value; } get { return editBox.selectionColor; }/* isset { return selectionColor ? true : false; }*/ };
361 property Color selectionText { set { editBox.selectionText = value; } get { return editBox.selectionText; }/* isset { return selectionText ? true : false; }*/ };
365 FileDialog browseFileDialog { type = selectDir, text = $"Select directory" };
367 public class DirectoriesBox : CommonControl
369 FileDialog browseDialog;
370 char * baseBrowsePath;
378 virtual bool Window::NotifyModified(DirectoriesBox dirsBox);
379 virtual bool Window::NotifyPathBoxModified(DirectoriesBox dirsBox, PathBox pathBox);
381 virtual bool OnChangedDir(char ** directory);
382 virtual bool OnPrepareBrowseDir(char ** directory);
383 virtual bool OnBrowsedDir(char ** directory);
385 watch(foreground) { list.foreground = foreground; };
386 watch(background) { list.background = background; };
388 property Array<String> strings
397 char temp[MAX_LOCATION];
398 list.AddString(GetSystemPathBuffer(temp, s));
402 list.currentRow = list.firstRow;
403 list.modifiedDocument = false;
407 Array<String> array { };
409 for(row = list.firstRow; row; row = row.next)
411 String string = row.string;
412 if(string && string[0])
413 array.Add(CopyUnixPath(string));
419 property FileDialog browseDialog
424 browseDialog = value;
428 if(browseDialog.type == open)
429 browseDialog.type = selectDir;
430 if(!strcmp(browseDialog.text, "Select a file...") && text)
432 char temp[1024] = "Select ";
435 browseDialog.text = temp;
439 get { return browseDialog; }
442 property String baseBrowsePath
446 delete baseBrowsePath;
448 baseBrowsePath = CopyString(value);
450 get { return baseBrowsePath; }
453 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
455 // Browsing was not being set, fixed by introducing dependency to this class to PathBox
456 if(!active && !browsing)
458 list.StopEditing(true);
459 if(list.modifiedDocument)
461 NotifyModified(master, this);
462 list.modifiedDocument = false;
463 modifiedDocument = true;
471 parent = this, bevelOver = true, inactive = true;
472 position = { 265, 0 }, size = { 22, 22 };
473 anchor = { top = 0, right = 77 };
474 hotKey = plus, bitmap = BitmapResource { fileName = "<:ecere>actions/listAdd.png", alphaBlend = true };
475 bool NotifyClicked(Button button, int x, int y, Modifiers mods)
477 list.StopEditing(true);
478 list.lastRow.Edit(null);
479 list.modifiedDocument = true;
485 parent = this, bevelOver = true, inactive = true;
486 position = { 290, 0 }, size = { 22, 22 };
487 anchor = { top = 0, right = 54 };
488 hotKey = del, bitmap = BitmapResource { fileName = "<:ecere>actions/listRemove.png", alphaBlend = true };
489 bool NotifyClicked(Button button, int x, int y, Modifiers mods)
491 list.StopEditing(true);
492 if(list.currentRow != list.lastRow)
494 list.DeleteRow(null);
495 list.modifiedDocument = true;
502 parent = this, bevelOver = true, inactive = true;
503 position = { 315, 0 }, size = { 22, 22 };
504 anchor = { top = 0, right = 31 };
505 hotKey = ctrlUp, bitmap = BitmapResource { fileName = "<:ecere>actions/goUp.png", alphaBlend = true };
506 bool NotifyClicked(Button button, int x, int y, Modifiers mods)
508 if(list.currentRow != list.lastRow)
510 DataRow current = list.currentRow, row;
513 row = current.previous;
518 list.modifiedDocument = true;
527 parent = this, bevelOver = true, inactive = true;
528 position = { 340, 0 }, size = { 22, 22 };
529 anchor = { top = 0, right = 8 };
530 hotKey = ctrlDown, bitmap = BitmapResource { fileName = "<:ecere>actions/goDown.png", alphaBlend = true };
531 bool NotifyClicked(Button button, int x, int y, Modifiers mods)
533 DataRow current = list.currentRow, row;
537 if(row && row != list.lastRow)
540 list.modifiedDocument = true;
548 this, moveRows = true, hasVertScroll = true, dontHideScroll = true;
549 borderStyle = deep, position = { 0, 22 }, size = { 300, 60 };
550 anchor = { left = 0, top = 22, right = 0, bottom = 0 };
552 bool OnRightButtonDown(int x, int y, Modifiers mods)
554 return parent.OnRightButtonDown(x + position.x + parent.clientStart.x, y + position.y + parent.clientStart.y, mods);
557 bool NotifyChanged(ListBox listBox, DataRow row)
559 char * directory = listBox.GetData(null);
560 if(directory && directory[0])
562 char * dir = CopyString(directory);
563 if(OnChangedDir(&dir))
565 // Put this back to enable making Paths relative by overriding
566 // these DirectoriesBox virtual methods (from FileDialog only)
569 OnPrepareBrowseDir(&dir);
572 listBox.SetData(null, dir);
573 listBox.modifiedDocument = true;
574 if(listBox.currentRow == listBox.lastRow && listBox.lastRow.string)
576 DataRow r = listBox.lastRow;
578 listBox.currentRow = listBox.AddString("");
583 else if(listBox.currentRow != listBox.lastRow)
585 listBox.DeleteRow(null);
586 listBox.modifiedDocument = true;
591 bool NotifyEditDone(ListBox listBox, DataRow row)
596 bool NotifyKeyHit(ListBox listBox, DataRow row, Key key, unichar ch)
598 // Prioritize row moving Ctrl-Up/Down over scrollbar scrolling
599 if(key == ctrlUp || key == ctrlDown)
601 Button btn = (key == ctrlUp) ? up : down;
602 btn.OnKeyHit(hotKey, 0);
608 bool NotifyKeyDown(ListBox listBox, DataRow row, Key key, unichar ch)
610 // Prioritize row moving Ctrl-Up/Down over scrollbar scrolling
611 if(key == ctrlUp || key == ctrlDown)
613 Button btn = (key == ctrlUp) ? up : down;
614 btn.OnKeyDown(hotKey, 0);
619 listBox.StopEditing(true);
620 if(listBox.currentRow != listBox.lastRow)
621 listBox.DeleteRow(null);
627 bool NotifyMove(ListBox listBox, DataRow row, Modifiers mods)
629 if(listBox.currentRow == listBox.lastRow)
631 else if(row == listBox.lastRow)
633 if(listBox.currentRow == row.previous)
635 listBox.currentRow.Move(row.previous);
641 bool NotifyReclick(ListBox listBox, DataRow row, Modifiers mods)
647 DataField dirField { dataType = class(DirPath), editable = true };
651 incref browseFileDialog;
652 browseDialog = browseFileDialog;
653 list.AddField(dirField);
655 list.modifiedDocument = false;
660 delete baseBrowsePath;