Bladeren bron

Start working on adding tab support

Andy Cedilnik 23 jaren geleden
bovenliggende
commit
12215e56f3

+ 125 - 0
Source/CursesDialog/cmCursesPathWidget.cxx

@@ -16,10 +16,135 @@
 =========================================================================*/
 #include "cmCursesPathWidget.h"
 
+#include "cmCursesMainForm.h"
+#include "cmSystemTools.h"
+#include "cmDirectory.h"
+
 cmCursesPathWidget::cmCursesPathWidget(int width, int height, 
                                            int left, int top) :
   cmCursesStringWidget(width, height, left, top)
 {
   m_Type = cmCacheManager::PATH;
+  m_Cycle = false;
+  m_CurrentIndex = 0;
+}
+
+void GlobDirs(const std::string& fullPath,
+              std::vector<std::string>& files,
+              std::ofstream& of)
+{
+  if ( fullPath[fullPath.size()-1] != '*' )
+    {
+    files.push_back(fullPath);
+    return;
+    }
+  std::string path = cmSystemTools::GetFilenamePath(fullPath);
+  std::string ppath = cmSystemTools::GetFilenameName(fullPath);
+  ppath = ppath.substr(0, ppath.size()-1);
+  of << "Search in directory: " << path << std::endl;
+  of << "Search pattern: " << ppath << std::endl;
+
+  cmDirectory d;
+  if (d.Load(path.c_str()))
+    {
+    for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
+      {
+      if((std::string(d.GetFile(i)) != ".")
+         && (std::string(d.GetFile(i)) != ".."))
+        {
+        std::string fname = path;
+        fname +="/";
+        fname += d.GetFile(i);
+        std::string sfname = d.GetFile(i);
+        if(cmSystemTools::FileIsDirectory(fname.c_str()))
+          {
+          of << "Compare: " << sfname.substr(0, ppath.size()) << " and "
+             << ppath << std::endl;
+          if ( sfname.size() >= ppath.size() && 
+               sfname.substr(0, ppath.size()) == 
+               ppath )
+            {
+            files.push_back(fname);
+            }
+          }
+        }
+      }
+    }
+}
+
+void cmCursesPathWidget::OnType(int& key, cmCursesMainForm* fm, WINDOW* w)
+{
+  m_Cycle = false;
+  m_CurrentIndex = 0;
+  m_LastGlob = "";
+  this->cmCursesStringWidget::OnType(key, fm, w);
 }
 
+void cmCursesPathWidget::OnTab(cmCursesMainForm* fm, WINDOW* w)
+{
+  std::ofstream of("lala.log");
+  std::string::size_type cc;
+  if ( !this->GetString() )
+    {
+    return;
+    }
+  FORM* form = fm->GetForm();
+  form_driver(form, REQ_NEXT_FIELD);
+  form_driver(form, REQ_PREV_FIELD);
+  std::string cstr = this->GetString();
+  cstr = cstr.substr(0, cstr.find_last_not_of(" \t\n\r")+1);
+  of << "Cstr: " << cstr << " <> " << m_LastString << std::endl;
+  if ( m_LastString != cstr )
+    {
+    m_Cycle = false;
+    m_CurrentIndex = 0;
+    m_LastGlob = "";
+    of << "Reset" << std::endl;
+    }
+  std::string glob;
+  if ( m_Cycle )
+    {
+    of << "We are cycling, try same glob" << std::endl;
+    glob = m_LastGlob;
+    }
+  else
+    {
+    glob = cstr + "*";
+    of << "Try new glob: " << glob << std::endl;
+    }
+  std::vector<std::string> dirs;
+
+  ::GlobDirs(glob.c_str(), dirs, of);
+  if ( m_CurrentIndex < dirs.size() )
+    {
+    cstr = dirs[m_CurrentIndex];
+    }
+  if ( cstr[cstr.size()-1] == '*' )
+    {
+    cstr = cstr.substr(0, cstr.size()-1);
+    }
+
+  of << "Glob: " << glob << std::endl;
+  for ( cc =0; cc < dirs.size(); cc ++ )
+    {
+    of << "\t" << cc << ": " << dirs[cc] << std::endl;
+    }
+  
+  this->SetString(cstr.c_str());
+  touchwin(w); 
+  wrefresh(w); 
+  form_driver(form, REQ_END_FIELD);
+  m_LastGlob = glob;
+  m_LastString = cstr;
+  m_Cycle = true;
+  m_CurrentIndex ++;
+  if ( m_CurrentIndex >= dirs.size() )
+    {
+    m_CurrentIndex = 0;
+    }
+}
+
+void cmCursesPathWidget::OnReturn(cmCursesMainForm* fm, WINDOW* w)
+{
+  this->cmCursesStringWidget::OnReturn(fm, w);
+}

+ 12 - 0
Source/CursesDialog/cmCursesPathWidget.h

@@ -24,10 +24,22 @@ class cmCursesPathWidget : public cmCursesStringWidget
 public:
   cmCursesPathWidget(int width, int height, int left, int top);
 
+  /**
+   * This method is called when different keys are pressed. The
+   * subclass can have a special implementation handler for this.
+   */
+  virtual void OnTab(cmCursesMainForm* fm, WINDOW* w);
+  virtual void OnReturn(cmCursesMainForm* fm, WINDOW* w);
+  virtual void OnType(int& key, cmCursesMainForm* fm, WINDOW* w);
+
 protected:
   cmCursesPathWidget(const cmCursesPathWidget& from);
   void operator=(const cmCursesPathWidget&);
 
+  std::string m_LastString;
+  std::string m_LastGlob;
+  bool m_Cycle;
+  std::string::size_type m_CurrentIndex;
 };
 
 #endif // __cmCursesPathWidget_h

+ 54 - 31
Source/CursesDialog/cmCursesStringWidget.cxx

@@ -33,6 +33,40 @@ cmCursesStringWidget::cmCursesStringWidget(int width, int height,
   field_opts_off(m_Field,  O_STATIC);
 }
 
+void cmCursesStringWidget::OnTab(cmCursesMainForm*, WINDOW*)
+{
+  //FORM* form = fm->GetForm();
+}
+
+void cmCursesStringWidget::OnReturn(cmCursesMainForm* fm, WINDOW*)
+{
+  FORM* form = fm->GetForm();
+  if (m_InEdit)
+    {
+    cmCursesForm::LogMessage("String widget leaving edit.");
+    m_InEdit = false;
+    fm->PrintKeys();
+    delete[] m_OriginalString;
+    // trick to force forms to update the field buffer
+    form_driver(form, REQ_NEXT_FIELD);
+    form_driver(form, REQ_PREV_FIELD);
+    m_Done = true;
+    }
+  else
+    {
+    cmCursesForm::LogMessage("String widget entering edit.");
+    m_InEdit = true;
+    fm->PrintKeys();
+    char* buf = field_buffer(m_Field, 0);
+    m_OriginalString = new char[strlen(buf)+1];
+    strcpy(m_OriginalString, buf);
+    }
+}
+
+void cmCursesStringWidget::OnType(int& key, cmCursesMainForm* fm, WINDOW* w)
+{
+  form_driver(fm->GetForm(), key);
+}
 
 bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm, 
                                        WINDOW* w)
@@ -46,12 +80,13 @@ bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm,
     return false;
     }
 
-  char* originalStr=0;
+  m_OriginalString=0;
+  m_Done = false;
 
   char debugMessage[128];
 
   // <Enter> is used to change edit mode (like <Esc> in vi).
-  while(1) 
+  while(!m_Done)
     {
     sprintf(debugMessage, "String widget handling input, key: %d", key);
     cmCursesForm::LogMessage(debugMessage);
@@ -83,26 +118,7 @@ bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm,
     // 10 == enter
     if (key == 10 || key == KEY_ENTER) 
       {
-      if (m_InEdit)
-        {
-        cmCursesForm::LogMessage("String widget leaving edit.");
-        m_InEdit = false;
-        fm->PrintKeys();
-        delete[] originalStr;   
-        // trick to force forms to update the field buffer
-        form_driver(form, REQ_NEXT_FIELD);
-        form_driver(form, REQ_PREV_FIELD);
-        return true;
-        }
-      else
-        {
-        cmCursesForm::LogMessage("String widget entering edit.");
-        m_InEdit = true;
-        fm->PrintKeys();
-        char* buf = field_buffer(m_Field, 0);
-        originalStr = new char[strlen(buf)+1];
-        strcpy(originalStr, buf);
-        }
+      this->OnReturn(fm, w);
       }
     else if ( key == KEY_DOWN || key == ctrl('n') ||
               key == KEY_UP || key == ctrl('p') ||
@@ -110,7 +126,7 @@ bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm,
               key == KEY_PPAGE || key == ctrl('u'))
       {
       m_InEdit = false;
-      delete[] originalStr;     
+      delete[] m_OriginalString;     
       // trick to force forms to update the field buffer
       form_driver(form, REQ_NEXT_FIELD);
       form_driver(form, REQ_PREV_FIELD);
@@ -123,13 +139,17 @@ bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm,
         {
         m_InEdit = false;
         fm->PrintKeys();
-        this->SetString(originalStr);
-        delete[] originalStr;
+        this->SetString(m_OriginalString);
+        delete[] m_OriginalString;
         touchwin(w); 
         wrefresh(w); 
         return true;
         }
       }
+    else if ( key == 9 )
+      {
+      this->OnTab(fm, w);
+      }
     else if ( key == KEY_LEFT || key == ctrl('b') )
       {
       form_driver(form, REQ_PREV_CHAR);
@@ -157,14 +177,17 @@ bool cmCursesStringWidget::HandleInput(int& key, cmCursesMainForm* fm,
       }
     else
       {
-      form_driver(form, key);
+      this->OnType(key, fm, w);
+      }
+    if ( !m_Done )
+      {
+      touchwin(w); 
+      wrefresh(w); 
+      
+      key=getch(); 
       }
-
-    touchwin(w); 
-    wrefresh(w); 
-
-    key=getch(); 
     }
+  return true;
 }
 
 void cmCursesStringWidget::SetString(const char* value)

+ 10 - 0
Source/CursesDialog/cmCursesStringWidget.h

@@ -55,6 +55,14 @@ public:
   bool GetInEdit()
     { return m_InEdit; }
 
+  /**
+   * This method is called when different keys are pressed. The
+   * subclass can have a special implementation handler for this.
+   */
+  virtual void OnTab(cmCursesMainForm* fm, WINDOW* w);
+  virtual void OnReturn(cmCursesMainForm* fm, WINDOW* w);
+  virtual void OnType(int& key, cmCursesMainForm* fm, WINDOW* w);
+
   /**
    * If there are any, print the widget specific commands
    * in the toolbar and return true. Otherwise, return false
@@ -68,6 +76,8 @@ protected:
 
   // true if the widget is in edit mode
   bool m_InEdit;
+  char* m_OriginalString;
+  bool m_Done;
 };
 
 #endif // __cmCursesStringWidget_h