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)
29 DirectoriesBox dirsBox;
32 dataBox, borderStyle = 0, anchor = { 0, 0, 0, 0 },
36 if((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;
53 if((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)
71 DirectoriesBox dirsBox;
74 dataBox, borderStyle = 0, anchor = { 0, 0, 0, 0 },
75 typeExpected = directory;
78 if((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;
95 if((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;
158 if((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 = new char[MAX_LOCATION];
192 char * fileName = new char[MAX_LOCATION];
193 char * baseBrowsePath = null;
194 PathBox pathBox = this;
195 DataBox dataBox = pathBoxDataBox;
197 DirectoriesBox dirsBox;
198 browsePath[0] = fileName[0] = '\0';
199 strncpy(browsePath, browseDialog.filePath, MAX_LOCATION); browsePath[MAX_LOCATION-1] = '\0';
200 if((dirsBox = pathBoxDirsBox) && dirsBox.baseBrowsePath && dirsBox.baseBrowsePath[0])
201 PathCat(browsePath, dirsBox.baseBrowsePath);
202 PathCat(browsePath, editBox.contents);
205 GetLastDirectory(browsePath, fileName);
206 StripLastDirectory(browsePath, browsePath);
210 char * path = new char[MAX_LOCATION];
211 LocateModule(null, path);
212 StripLastDirectory(path, path);
213 strncpy(browsePath, path, MAX_LOCATION); browsePath[MAX_LOCATION-1] = '\0';
216 while(browsePath[0] && !FileExists(browsePath).isDirectory)
218 char * temp = new char[MAX_LOCATION];
219 GetLastDirectory(browsePath, temp);
220 PathCat(temp, fileName);
221 strcpy(fileName, temp);
222 StripLastDirectory(browsePath, browsePath);
225 browseDialog.filePath = fileName;
226 browseDialog.currentDirectory = browsePath;
227 browseDialog.master = rootWindow;
232 if(dirsBox) dirsBox.browsing = true;
233 if(browseDialog.Modal())
235 PathBox pathBox = dataBox ? (PathBox)dataBox.editor : this;
236 pathBox.modifiedDocument = true;
237 pathBox.property::path = browseDialog.filePath;
238 pathBox.NotifyModified(pathBox.master, this);
242 pathBox.editBox.SelectAll();
243 if((listBox = pathBoxListBox))
244 listBox.StopEditing(true);
246 if(dirsBox) dirsBox.browsing = false;
253 void CheckFileExists()
255 if(typeExpected != none)
257 BitmapResource icon = null;
258 char path[MAX_LOCATION];
260 GetSystemPathBuffer(path, editBox.contents);
261 exists = FileExists(path);
266 // TODO: improvements, add drive, etc, also find a better solution/icon for expect any and file doesn't exist
267 icon = exists ? exists.isFile ? file : exists.isDirectory ? folder : null : null;
270 icon = exists && exists.isDirectory ? folder : brokenFolder;
273 icon = exists && exists.isFile ? file : brokenFile;
277 picture.image = icon;
281 void OnRedraw(Surface surface)
285 surface.SetBackground(formColor);
286 surface.Area(0, 0, clientSize.w, clientSize.h);
289 Window::OnRedraw(surface);
298 virtual bool Window::NotifyModified(PathBox pathBox);
300 property PathTypeExpected typeExpected
304 if(value != typeExpected)
306 if(value == none || typeExpected == none)
308 bool withIcon = value != none;
309 picture.visible = withIcon;
310 editBox.anchor.left = withIcon ? 18 : 1;
313 typeExpected = value;
314 if(browseDialog && browseDialog.type == open && typeExpected == directory)
315 browseDialog.type = selectDir;
320 property FileDialog browseDialog
325 browseDialog = value;
329 if(browseDialog.type == open && typeExpected == directory)
330 browseDialog.type = selectDir;
331 if(!strcmp(browseDialog.text, "Select a file...") && text)
333 char temp[1024] = "Select ";
336 browseDialog.text = temp;
339 browse.visible = browseDialog ? true : false;
340 editBox.anchor.right = browseDialog ? 26 : 1;
342 get { return browseDialog; }
345 void Home() { editBox.Home(); }
346 void End() { editBox.End(); }
348 void SelectAll() { editBox.SelectAll(); }
349 void Deselect() { editBox.Deselect(); }
355 char path[MAX_LOCATION];
356 GetSystemPathBuffer(path, value);
357 editBox.contents = path;
362 get { return editBox.contents; }
364 property String slashPath { get { return GetSlashPathBuffer (path, editBox.contents); } };
365 property String systemPath { get { return GetSystemPathBuffer(path, editBox.contents); } };
367 property Color selectionColor { set { editBox.selectionColor = value; } get { return editBox.selectionColor; }/* isset { return selectionColor ? true : false; }*/ };
368 property Color selectionText { set { editBox.selectionText = value; } get { return editBox.selectionText; }/* isset { return selectionText ? true : false; }*/ };
372 FileDialog browseFileDialog { type = selectDir, text = $"Select directory" };
374 public class DirectoriesBox : CommonControl
376 FileDialog browseDialog;
377 char * baseBrowsePath;
385 virtual bool Window::NotifyModified(DirectoriesBox dirsBox);
386 virtual bool Window::NotifyPathBoxModified(DirectoriesBox dirsBox, PathBox pathBox);
388 virtual bool OnChangedDir(char ** directory);
389 virtual bool OnPrepareBrowseDir(char ** directory);
390 virtual bool OnBrowsedDir(char ** directory);
392 watch(foreground) { list.foreground = foreground; };
393 watch(background) { list.background = background; };
395 property Array<String> strings
404 char temp[MAX_LOCATION];
405 list.AddString(GetSystemPathBuffer(temp, s));
409 list.currentRow = list.firstRow;
410 list.modifiedDocument = false;
414 Array<String> array { };
416 for(row = list.firstRow; row; row = row.next)
418 String string = row.string;
419 if(string && string[0])
420 array.Add(CopyUnixPath(string));
426 property FileDialog browseDialog
431 browseDialog = value;
435 if(browseDialog.type == open)
436 browseDialog.type = selectDir;
437 if(!strcmp(browseDialog.text, "Select a file...") && text)
439 char temp[1024] = "Select ";
442 browseDialog.text = temp;
446 get { return browseDialog; }
449 property String baseBrowsePath
453 delete baseBrowsePath;
455 baseBrowsePath = CopyString(value);
457 get { return baseBrowsePath; }
460 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
462 // Browsing was not being set, fixed by introducing dependency to this class to PathBox
463 if(!active && !browsing)
465 list.StopEditing(true);
466 if(list.modifiedDocument)
468 NotifyModified(master, this);
469 list.modifiedDocument = false;
470 modifiedDocument = true;
478 parent = this, bevelOver = true, inactive = true;
479 position = { 265, 0 }, size = { 22, 22 };
480 anchor = { top = 0, right = 77 };
481 hotKey = plus, bitmap = BitmapResource { fileName = "<:ecere>actions/listAdd.png", alphaBlend = true };
482 bool NotifyClicked(Button button, int x, int y, Modifiers mods)
484 list.StopEditing(true);
485 list.lastRow.Edit(null);
486 list.modifiedDocument = true;
492 parent = this, bevelOver = true, inactive = true;
493 position = { 290, 0 }, size = { 22, 22 };
494 anchor = { top = 0, right = 54 };
495 hotKey = del, bitmap = BitmapResource { fileName = "<:ecere>actions/listRemove.png", alphaBlend = true };
496 bool NotifyClicked(Button button, int x, int y, Modifiers mods)
498 list.StopEditing(true);
499 if(list.currentRow != list.lastRow)
501 list.DeleteRow(null);
502 list.modifiedDocument = true;
509 parent = this, bevelOver = true, inactive = true;
510 position = { 315, 0 }, size = { 22, 22 };
511 anchor = { top = 0, right = 31 };
512 hotKey = ctrlUp, bitmap = BitmapResource { fileName = "<:ecere>actions/goUp.png", alphaBlend = true };
513 bool NotifyClicked(Button button, int x, int y, Modifiers mods)
515 if(list.currentRow != list.lastRow)
517 DataRow current = list.currentRow, row;
520 row = current.previous;
525 list.modifiedDocument = true;
534 parent = this, bevelOver = true, inactive = true;
535 position = { 340, 0 }, size = { 22, 22 };
536 anchor = { top = 0, right = 8 };
537 hotKey = ctrlDown, bitmap = BitmapResource { fileName = "<:ecere>actions/goDown.png", alphaBlend = true };
538 bool NotifyClicked(Button button, int x, int y, Modifiers mods)
540 DataRow current = list.currentRow, row;
544 if(row && row != list.lastRow)
547 list.modifiedDocument = true;
555 this, moveRows = true, hasVertScroll = true, dontHideScroll = true;
556 borderStyle = deep, position = { 0, 22 }, size = { 300, 60 };
557 anchor = { left = 0, top = 22, right = 0, bottom = 0 };
559 bool OnRightButtonDown(int x, int y, Modifiers mods)
561 return parent.OnRightButtonDown(x + position.x + parent.clientStart.x, y + position.y + parent.clientStart.y, mods);
564 bool NotifyChanged(ListBox listBox, DataRow row)
566 char * directory = listBox.GetData(null);
567 if(directory && directory[0])
569 char * dir = CopyString(directory);
570 if(OnChangedDir(&dir))
572 // Put this back to enable making Paths relative by overriding
573 // these DirectoriesBox virtual methods (from FileDialog only)
576 OnPrepareBrowseDir(&dir);
579 listBox.SetData(null, dir);
580 listBox.modifiedDocument = true;
581 if(listBox.currentRow == listBox.lastRow && listBox.lastRow.string)
583 DataRow r = listBox.lastRow;
585 listBox.currentRow = listBox.AddString("");
590 else if(listBox.currentRow != listBox.lastRow)
592 listBox.DeleteRow(null);
593 listBox.modifiedDocument = true;
598 bool NotifyEditDone(ListBox listBox, DataRow row)
603 bool NotifyKeyHit(ListBox listBox, DataRow row, Key key, unichar ch)
605 // Prioritize row moving Ctrl-Up/Down over scrollbar scrolling
606 if(key == ctrlUp || key == ctrlDown)
608 Button btn = (key == ctrlUp) ? up : down;
609 btn.OnKeyHit(hotKey, 0);
615 bool NotifyKeyDown(ListBox listBox, DataRow row, Key key, unichar ch)
617 // Prioritize row moving Ctrl-Up/Down over scrollbar scrolling
618 if(key == ctrlUp || key == ctrlDown)
620 Button btn = (key == ctrlUp) ? up : down;
621 btn.OnKeyDown(hotKey, 0);
626 listBox.StopEditing(true);
627 if(listBox.currentRow != listBox.lastRow)
628 listBox.DeleteRow(null);
634 bool NotifyMove(ListBox listBox, DataRow row, Modifiers mods)
636 if(listBox.currentRow == listBox.lastRow)
638 else if(row == listBox.lastRow)
640 if(listBox.currentRow == row.previous)
642 listBox.currentRow.Move(row.previous);
648 bool NotifyReclick(ListBox listBox, DataRow row, Modifiers mods)
654 DataField dirField { dataType = class(DirPath), editable = true };
658 incref browseFileDialog;
659 browseDialog = browseFileDialog;
660 list.AddField(dirField);
662 list.modifiedDocument = false;
667 delete baseBrowsePath;