cmCursesPathWidget.cxx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <vector>
  5. #include "cmCursesColor.h"
  6. #include "cmCursesMainForm.h"
  7. #include "cmCursesStringWidget.h"
  8. #include "cmStateTypes.h"
  9. #include "cmSystemTools.h"
  10. cmCursesPathWidget::cmCursesPathWidget(int width, int height, int left,
  11. int top)
  12. : cmCursesStringWidget(width, height, left, top)
  13. {
  14. this->Type = cmStateEnums::PATH;
  15. this->Cycle = false;
  16. this->CurrentIndex = 0;
  17. if (cmCursesColor::HasColors()) {
  18. set_field_fore(this->Field, COLOR_PAIR(cmCursesColor::Path));
  19. set_field_back(this->Field, COLOR_PAIR(cmCursesColor::Path));
  20. } else {
  21. set_field_fore(this->Field, A_NORMAL);
  22. set_field_back(this->Field, A_STANDOUT);
  23. }
  24. }
  25. void cmCursesPathWidget::OnType(int& key, cmCursesMainForm* fm, WINDOW* w)
  26. {
  27. this->Cycle = false;
  28. this->CurrentIndex = 0;
  29. this->LastGlob = "";
  30. this->cmCursesStringWidget::OnType(key, fm, w);
  31. }
  32. void cmCursesPathWidget::OnTab(cmCursesMainForm* fm, WINDOW* w)
  33. {
  34. if (!this->GetString()) {
  35. return;
  36. }
  37. FORM* form = fm->GetForm();
  38. form_driver(form, REQ_NEXT_FIELD);
  39. form_driver(form, REQ_PREV_FIELD);
  40. std::string cstr = this->GetString();
  41. cstr = cstr.substr(0, cstr.find_last_not_of(" \t\n\r") + 1);
  42. if (this->LastString != cstr) {
  43. this->Cycle = false;
  44. this->CurrentIndex = 0;
  45. this->LastGlob = "";
  46. }
  47. std::string glob;
  48. if (this->Cycle) {
  49. glob = this->LastGlob;
  50. } else {
  51. glob = cstr + "*";
  52. }
  53. std::vector<std::string> dirs;
  54. cmSystemTools::SimpleGlob(glob, dirs,
  55. (this->Type == cmStateEnums::PATH ? -1 : 0));
  56. if (this->CurrentIndex < dirs.size()) {
  57. cstr = dirs[this->CurrentIndex];
  58. }
  59. if (cstr[cstr.size() - 1] == '*') {
  60. cstr = cstr.substr(0, cstr.size() - 1);
  61. }
  62. if (cmSystemTools::FileIsDirectory(cstr)) {
  63. cstr += "/";
  64. }
  65. this->SetString(cstr);
  66. touchwin(w);
  67. wrefresh(w);
  68. form_driver(form, REQ_END_FIELD);
  69. this->LastGlob = glob;
  70. this->LastString = cstr;
  71. this->Cycle = true;
  72. this->CurrentIndex++;
  73. if (this->CurrentIndex >= dirs.size()) {
  74. this->CurrentIndex = 0;
  75. }
  76. }
  77. void cmCursesPathWidget::OnReturn(cmCursesMainForm* fm, WINDOW* w)
  78. {
  79. this->cmCursesStringWidget::OnReturn(fm, w);
  80. }