/cui/filebox.d

http://github.com/wilkie/djehuty · D · 387 lines · 288 code · 77 blank · 22 comment · 58 complexity · dd3b75280b577c062c3b442e852dd6e4 MD5 · raw file

  1. /*
  2. * filebox.d
  3. *
  4. * This module implements a CuiWidget that lists a directory.
  5. *
  6. * Author: Dave Wilkinson
  7. * Originated: August 20th 2009
  8. *
  9. */
  10. module cui.filebox;
  11. import cui.listbox;
  12. import cui.widget;
  13. import io.directory;
  14. import io.console;
  15. import djehuty;
  16. import data.list;
  17. class CuiFileBox : CuiWidget, Iterable!(string) {
  18. this(uint x, uint y, uint width, uint height) {
  19. super(x,y,width,height);
  20. _path = new Directory();
  21. _list = new List!(string);
  22. string[] list = _path.list.sort;
  23. if (!_path.isRoot) {
  24. list = [".."] ~ list;
  25. }
  26. foreach(item; list) {
  27. _list.add(item);
  28. }
  29. }
  30. override void onDraw() {
  31. uint i;
  32. for (i = _firstVisible; (i < this.height + _firstVisible) && (i < _list.length); i++) {
  33. drawLine(i);
  34. }
  35. Console.forecolor = _forecolor;
  36. Console.backcolor = _backcolor;
  37. for (; i < this.height + _firstVisible; i++) {
  38. Console.position(0, i-_firstVisible);
  39. Console.putSpaces(this.width);
  40. }
  41. }
  42. override void onKeyDown(Key key) {
  43. if (key.code == Key.Up) {
  44. if (_pos == 0) {
  45. return;
  46. }
  47. if (_pos == _firstVisible) {
  48. _firstVisible--;
  49. _pos--;
  50. onDraw();
  51. return;
  52. }
  53. if (_pos > 0) {
  54. _pos--;
  55. drawLine(_pos+1);
  56. drawLine(_pos);
  57. }
  58. }
  59. else if (key.code == Key.Down) {
  60. if (_pos == _list.length - 1) {
  61. return;
  62. }
  63. if (_pos == (_firstVisible + this.height - 1)) {
  64. _firstVisible++;
  65. _pos++;
  66. onDraw();
  67. return;
  68. }
  69. if (_list.length > 0 && _pos < _list.length - 1) {
  70. _pos++;
  71. drawLine(_pos-1);
  72. drawLine(_pos);
  73. }
  74. }
  75. else if (key.code == Key.Return) {
  76. // Traverse Directory
  77. if (_list[_pos] == "..") {
  78. _path = _path.parent;
  79. string[] list = _path.list.sort;
  80. if (!_path.isRoot) {
  81. list = [".."] ~ list;
  82. }
  83. _list.clear();
  84. // XXX: Make this work for List!()
  85. foreach(item; list) {
  86. _list.add(item);
  87. }
  88. _pos = 0;
  89. _firstVisible = 0;
  90. onDraw();
  91. onDirectorySelect(_path.path);
  92. }
  93. else if (_path.isDir(_list[_pos])) {
  94. _path = _path.traverse(_list[_pos]);
  95. string[] list = _path.list.sort;
  96. if (!_path.isRoot) {
  97. list = [".."] ~ list;
  98. }
  99. _pos = 0;
  100. _firstVisible = 0;
  101. _list.clear();
  102. foreach(item; list) {
  103. _list.add(item);
  104. }
  105. onDraw();
  106. onDirectorySelect(_path.path);
  107. }
  108. else {
  109. onFileSelect(_path.path ~ "/" ~ _list[_pos]);
  110. }
  111. }
  112. else if (key.code == Key.PageUp) {
  113. if (_pos == 0) {
  114. return;
  115. }
  116. if (_pos != _firstVisible) {
  117. _pos = _firstVisible;
  118. }
  119. else {
  120. if (_firstVisible > this.height - 1) {
  121. _firstVisible -= this.height - 1;
  122. }
  123. else {
  124. _firstVisible = 0;
  125. }
  126. if (_pos > this.height - 1) {
  127. _pos -= this.height - 1;
  128. }
  129. else {
  130. _pos = 0;
  131. }
  132. }
  133. onDraw();
  134. }
  135. else if (key.code == Key.PageDown) {
  136. if (_pos == _list.length - 1) {
  137. return;
  138. }
  139. if (_pos != _firstVisible + this.height - 1) {
  140. _pos = _firstVisible + this.height - 1;
  141. }
  142. else {
  143. _firstVisible += this.height - 1;
  144. _pos += this.height - 1;
  145. if (_firstVisible > _list.length - this.height) {
  146. _firstVisible = _list.length - this.height;
  147. }
  148. }
  149. if ( _pos >= _list.length) {
  150. _pos = _list.length - 1;
  151. }
  152. onDraw();
  153. }
  154. }
  155. override void onLostFocus() {
  156. if (_list.length > 0) {
  157. drawLine(_pos);
  158. }
  159. }
  160. override void onGotFocus() {
  161. Console.hideCaret();
  162. if (_list.length > 0) {
  163. drawLine(_pos);
  164. }
  165. }
  166. // Events
  167. // Description: This event will be fired upon selection of a file within the widget.
  168. // file: The complete path to the file.
  169. void onFileSelect(string file) {
  170. }
  171. // Description: This event will be fired upon selection of a directory within the widget.
  172. // dir: The complete path to the directory.
  173. void onDirectorySelect(string dir) {
  174. }
  175. // Methods
  176. override bool isTabStop() {
  177. return true;
  178. }
  179. void add(string c) {
  180. return;
  181. }
  182. string remove() {
  183. return _list.peek();
  184. }
  185. string removeAt(size_t idx){
  186. return _list.peekAt(idx);
  187. }
  188. string peek() {
  189. return _list.peek();
  190. }
  191. string peekAt(size_t idx) {
  192. return _list.peekAt(idx);
  193. }
  194. void set(string c) {
  195. return;
  196. }
  197. void apply(string delegate(string) func) {
  198. return;
  199. }
  200. bool contains(string c) {
  201. return _list.contains(c);
  202. }
  203. bool empty() {
  204. return _list.empty();
  205. }
  206. void clear() {
  207. return;
  208. }
  209. string[] array() {
  210. return _list.array();
  211. }
  212. List!(string) dup() {
  213. return _list.dup();
  214. }
  215. List!(string) slice(size_t start, size_t end) {
  216. return _list.slice(start, end);
  217. }
  218. List!(string) reverse() {
  219. return _list.reverse();
  220. }
  221. size_t length() {
  222. return _list.length();
  223. }
  224. string opIndex(size_t i1) {
  225. return _list.opIndex(i1);
  226. }
  227. int opApply(int delegate(ref string) loopFunc) {
  228. return _list.opApply(loopFunc);
  229. }
  230. int opApply(int delegate(ref size_t, ref string) loopFunc) {
  231. return _list.opApply(loopFunc);
  232. }
  233. int opApplyReverse(int delegate(ref string) loopFunc) {
  234. return _list.opApplyReverse(loopFunc);
  235. }
  236. int opApplyReverse(int delegate(ref size_t, ref string) loopFunc) {
  237. return _list.opApplyReverse(loopFunc);
  238. }
  239. void opCatAssign(string[] list) {
  240. _list.opCatAssign(list);
  241. }
  242. void opCatAssign(Iterable!(string) list) {
  243. _list.opCatAssign(list);
  244. }
  245. void opCatAssign(string item) {
  246. _list.opCatAssign(item);
  247. }
  248. Iterable!(string) opCat(string[] list) {
  249. return _list.opCat(list);
  250. }
  251. Iterable!(string) opCat(Iterable!(string) list) {
  252. return _list.opCat(list);
  253. }
  254. Iterable!(string) opCat(string item) {
  255. return _list.opCat(item);
  256. }
  257. // Propeties
  258. // Description: This property is for setting the backcolor for normal items.
  259. Color backcolor() {
  260. return _backcolor;
  261. }
  262. void backcolor(Color value) {
  263. _backcolor = value;
  264. }
  265. // Description: This property is for setting the forecolor for normal items.
  266. Color forecolor() {
  267. return _forecolor;
  268. }
  269. void forecolor(Color value) {
  270. _forecolor = value;
  271. }
  272. // Description: This property is for setting the forecolor for selected items.
  273. Color selectedForecolor() {
  274. return _selectedForecolor;
  275. }
  276. void selectedForecolor(Color value) {
  277. _selectedForecolor = value;
  278. }
  279. // Description: This property is for setting the backcolor for selected items.
  280. Color selectedBackcolor() {
  281. return _selectedBackcolor;
  282. }
  283. void selectedBackcolor(Color value) {
  284. _selectedBackcolor = value;
  285. }
  286. protected:
  287. void drawLine(uint pos) {
  288. Console.position(0, pos - _firstVisible);
  289. if(pos == _pos) {
  290. Console.backcolor = _selectedBackcolor;
  291. Console.forecolor = _selectedForecolor;
  292. }
  293. else {
  294. Console.backcolor = _backcolor;
  295. Console.forecolor = _forecolor;
  296. }
  297. Console.put(_list[pos]);
  298. if(_list[pos].length < this.width) {
  299. Console.putSpaces(this.width - _list[pos].length);
  300. }
  301. }
  302. uint _pos = 0;
  303. uint _firstVisible = 0;
  304. Directory _path;
  305. List!(string) _list;
  306. Color _forecolor = Color.White;
  307. Color _backcolor = Color.Black;
  308. Color _selectedForecolor = Color.Yellow;
  309. Color _selectedBackcolor = Color.Black;
  310. }