cmCursesPathWidget.cxx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCursesPathWidget.h"
  4. #include "cmCursesMainForm.h"
  5. #include "cmCursesStringWidget.h"
  6. #include "cmStateTypes.h"
  7. #include "cmSystemTools.h"
  8. #include <vector>
  9. cmCursesPathWidget::cmCursesPathWidget(int width, int height, int left,
  10. int top)
  11. : cmCursesStringWidget(width, height, left, top)
  12. {
  13. this->Type = cmStateEnums::PATH;
  14. this->Cycle = false;
  15. this->CurrentIndex = 0;
  16. }
  17. void cmCursesPathWidget::OnType(int& key, cmCursesMainForm* fm, WINDOW* w)
  18. {
  19. this->Cycle = false;
  20. this->CurrentIndex = 0;
  21. this->LastGlob = "";
  22. this->cmCursesStringWidget::OnType(key, fm, w);
  23. }
  24. void cmCursesPathWidget::OnTab(cmCursesMainForm* fm, WINDOW* w)
  25. {
  26. if (!this->GetString()) {
  27. return;
  28. }
  29. FORM* form = fm->GetForm();
  30. form_driver(form, REQ_NEXT_FIELD);
  31. form_driver(form, REQ_PREV_FIELD);
  32. std::string cstr = this->GetString();
  33. cstr = cstr.substr(0, cstr.find_last_not_of(" \t\n\r") + 1);
  34. if (this->LastString != cstr) {
  35. this->Cycle = false;
  36. this->CurrentIndex = 0;
  37. this->LastGlob = "";
  38. }
  39. std::string glob;
  40. if (this->Cycle) {
  41. glob = this->LastGlob;
  42. } else {
  43. glob = cstr + "*";
  44. }
  45. std::vector<std::string> dirs;
  46. cmSystemTools::SimpleGlob(glob, dirs,
  47. (this->Type == cmStateEnums::PATH ? -1 : 0));
  48. if (this->CurrentIndex < dirs.size()) {
  49. cstr = dirs[this->CurrentIndex];
  50. }
  51. if (cstr[cstr.size() - 1] == '*') {
  52. cstr = cstr.substr(0, cstr.size() - 1);
  53. }
  54. if (cmSystemTools::FileIsDirectory(cstr)) {
  55. cstr += "/";
  56. }
  57. this->SetString(cstr);
  58. touchwin(w);
  59. wrefresh(w);
  60. form_driver(form, REQ_END_FIELD);
  61. this->LastGlob = glob;
  62. this->LastString = cstr;
  63. this->Cycle = true;
  64. this->CurrentIndex++;
  65. if (this->CurrentIndex >= dirs.size()) {
  66. this->CurrentIndex = 0;
  67. }
  68. }
  69. void cmCursesPathWidget::OnReturn(cmCursesMainForm* fm, WINDOW* w)
  70. {
  71. this->cmCursesStringWidget::OnReturn(fm, w);
  72. }