cmCursesPathWidget.cxx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCursesPathWidget.h"
  11. #include "cmCursesMainForm.h"
  12. #include "cmCursesStringWidget.h"
  13. #include "cmState.h"
  14. #include "cmSystemTools.h"
  15. #include <vector>
  16. cmCursesPathWidget::cmCursesPathWidget(int width, int height, int left,
  17. int top)
  18. : cmCursesStringWidget(width, height, left, top)
  19. {
  20. this->Type = cmState::PATH;
  21. this->Cycle = false;
  22. this->CurrentIndex = 0;
  23. }
  24. void cmCursesPathWidget::OnType(int& key, cmCursesMainForm* fm, WINDOW* w)
  25. {
  26. this->Cycle = false;
  27. this->CurrentIndex = 0;
  28. this->LastGlob = "";
  29. this->cmCursesStringWidget::OnType(key, fm, w);
  30. }
  31. void cmCursesPathWidget::OnTab(cmCursesMainForm* fm, WINDOW* w)
  32. {
  33. if (!this->GetString()) {
  34. return;
  35. }
  36. FORM* form = fm->GetForm();
  37. form_driver(form, REQ_NEXT_FIELD);
  38. form_driver(form, REQ_PREV_FIELD);
  39. std::string cstr = this->GetString();
  40. cstr = cstr.substr(0, cstr.find_last_not_of(" \t\n\r") + 1);
  41. if (this->LastString != cstr) {
  42. this->Cycle = false;
  43. this->CurrentIndex = 0;
  44. this->LastGlob = "";
  45. }
  46. std::string glob;
  47. if (this->Cycle) {
  48. glob = this->LastGlob;
  49. } else {
  50. glob = cstr + "*";
  51. }
  52. std::vector<std::string> dirs;
  53. cmSystemTools::SimpleGlob(glob, dirs,
  54. (this->Type == cmState::PATH ? -1 : 0));
  55. if (this->CurrentIndex < dirs.size()) {
  56. cstr = dirs[this->CurrentIndex];
  57. }
  58. if (cstr[cstr.size() - 1] == '*') {
  59. cstr = cstr.substr(0, cstr.size() - 1);
  60. }
  61. if (cmSystemTools::FileIsDirectory(cstr)) {
  62. cstr += "/";
  63. }
  64. this->SetString(cstr);
  65. touchwin(w);
  66. wrefresh(w);
  67. form_driver(form, REQ_END_FIELD);
  68. this->LastGlob = glob;
  69. this->LastString = cstr;
  70. this->Cycle = true;
  71. this->CurrentIndex++;
  72. if (this->CurrentIndex >= dirs.size()) {
  73. this->CurrentIndex = 0;
  74. }
  75. }
  76. void cmCursesPathWidget::OnReturn(cmCursesMainForm* fm, WINDOW* w)
  77. {
  78. this->cmCursesStringWidget::OnReturn(fm, w);
  79. }