[dasher: 96/217] Implement GetTextAroundCursor for the internal Gtk editor. Implement sentence and paragraph



commit b5a43356ec9060c144b91b9c727c0ae748fc423b
Author: lbaudoin <lbaudoin google com>
Date:   Tue Oct 20 23:36:17 2015 -0700

        Implement GetTextAroundCursor for the internal Gtk editor.
        Implement sentence and paragraph moves for the Gtk editor.
        Update the forward word move to reach the beginning of the next word like the windows version.

 Src/Gtk2/DasherControl.cpp    |    4 +++
 Src/Gtk2/DasherControl.h      |    1 +
 Src/Gtk2/GtkDasherControl.cpp |    5 +++
 Src/Gtk2/GtkDasherControl.h   |    1 +
 Src/Gtk2/dasher_editor.cpp    |   56 ++++++++++++++++++++++++++++++++++++++++-
 Src/Gtk2/dasher_editor.h      |    1 +
 6 files changed, 67 insertions(+), 1 deletions(-)
---
diff --git a/Src/Gtk2/DasherControl.cpp b/Src/Gtk2/DasherControl.cpp
index 1f897ca..e806199 100644
--- a/Src/Gtk2/DasherControl.cpp
+++ b/Src/Gtk2/DasherControl.cpp
@@ -158,6 +158,10 @@ int CDasherControl::GetAllContextLenght()
   return g_utf8_strlen(text,-1);
 }
 
+std::string CDasherControl::GetTextAroundCursor(CControlManager::EditDistance dist) {
+  return gtk_dasher_control_get_text_around_cursor(m_pDasherControl, dist);
+}
+
 std::string CDasherControl::GetContext(unsigned int iStart, unsigned int iLength) {
   const gchar *text = gtk_dasher_control_get_context(m_pDasherControl, iStart, iLength);
   return text;
diff --git a/Src/Gtk2/DasherControl.h b/Src/Gtk2/DasherControl.h
index 2183c0b..6967b57 100644
--- a/Src/Gtk2/DasherControl.h
+++ b/Src/Gtk2/DasherControl.h
@@ -139,6 +139,7 @@ public:
   virtual void ClearAllContext();
   virtual std::string GetAllContext();
   virtual int GetAllContextLenght();
+  std::string GetTextAroundCursor(CControlManager::EditDistance dist) override;
   std::string GetContext(unsigned int iStart, unsigned int iLength);
 
   virtual bool SupportsClipboard();
diff --git a/Src/Gtk2/GtkDasherControl.cpp b/Src/Gtk2/GtkDasherControl.cpp
index 0103142..50c0e39 100644
--- a/Src/Gtk2/GtkDasherControl.cpp
+++ b/Src/Gtk2/GtkDasherControl.cpp
@@ -231,6 +231,11 @@ gtk_dasher_control_get_all_text(GtkDasherControl *pControl) {
   return dasher_editor_get_all_text(pPrivate->pEditor);
 }
 
+const gchar* gtk_dasher_control_get_text_around_cursor(GtkDasherControl *pControl, 
CControlManager::EditDistance dist) {
+  GtkDasherControlPrivate *pPrivate = GTK_DASHER_CONTROL_GET_PRIVATE(pControl);
+  return dasher_editor_get_text_around_cursor(pPrivate->pEditor, dist);
+}
+
 void gtk_dasher_control_clear_all_context(GtkDasherControl *pControl) {
   GtkDasherControlPrivate *pPrivate = GTK_DASHER_CONTROL_GET_PRIVATE(pControl);
   dasher_editor_clear(pPrivate->pEditor);
diff --git a/Src/Gtk2/GtkDasherControl.h b/Src/Gtk2/GtkDasherControl.h
index 058aecc..c93bbaa 100644
--- a/Src/Gtk2/GtkDasherControl.h
+++ b/Src/Gtk2/GtkDasherControl.h
@@ -73,6 +73,7 @@ void gtk_dasher_control_set_editor(GtkDasherControl *pControl, DasherEditor *pEd
 void gtk_dasher_control_clear_all_context(GtkDasherControl *pControl);
 const gchar* gtk_dasher_control_get_all_text(GtkDasherControl *pControl);
 const gchar* gtk_dasher_control_get_context(GtkDasherControl *pControl, unsigned int iOffset, unsigned int 
iLength);
+const gchar* gtk_dasher_control_get_text_around_cursor(GtkDasherControl *pControl, 
Dasher::CControlManager::EditDistance dist);
 //void gtk_dasher_control_invalidate_context(GtkDasherControl *pControl, bool bForceStart);
 void gtk_dasher_control_set_buffer(GtkDasherControl *pControl, int iOffset);
 void gtk_dasher_control_set_offset(GtkDasherControl *pControl, int iOffset);
diff --git a/Src/Gtk2/dasher_editor.cpp b/Src/Gtk2/dasher_editor.cpp
index 9f53546..eef3ba8 100644
--- a/Src/Gtk2/dasher_editor.cpp
+++ b/Src/Gtk2/dasher_editor.cpp
@@ -41,6 +41,7 @@ static void dasher_editor_internal_select_all(DasherEditor *pSelf);
 static void dasher_editor_internal_command_new(DasherEditor *pSelf);
 static void dasher_editor_internal_command_open(DasherEditor *pSelf);
 static void dasher_editor_internal_command_save(DasherEditor *pSelf, gboolean bPrompt, gboolean bAppend);
+static void edit_find(bool bForwards, Dasher::CControlManager::EditDistance iDist, DasherEditorPrivate 
*pPrivate, GtkTextIter *pPos);
 
 #ifdef HAVE_GIO
 static void dasher_editor_internal_gvfs_print_error(DasherEditor *pSelf, GError *error, const char 
*myfilename);
@@ -437,6 +438,17 @@ dasher_editor_get_context(DasherEditor *pSelf, int iOffset, int iLength) {
   return gtk_text_buffer_get_text( pPrivate->pBuffer, &start, &end, false );
 }
 
+const gchar* dasher_editor_get_text_around_cursor(DasherEditor *pSelf, Dasher::CControlManager::EditDistance 
distance) {
+  DasherEditorPrivate *pPrivate = DASHER_EDITOR_GET_PRIVATE(pSelf);
+  // TODO: handle the external editor.
+  GtkTextIter end_pos, start_pos;
+  gtk_text_buffer_get_iter_at_mark(pPrivate->pBuffer, &end_pos, 
gtk_text_buffer_get_insert(pPrivate->pBuffer));
+  start_pos = end_pos;
+  edit_find(true /* forward */, distance, pPrivate, &end_pos);
+  edit_find(false /* backward */, distance, pPrivate, &start_pos);
+  return gtk_text_buffer_get_slice(pPrivate->pBuffer, &start_pos, &end_pos, FALSE /* hidden chars */);
+}
+
 // gint
 // dasher_editor_get_offset(DasherEditor *pSelf) {
 //   if(DASHER_EDITOR_GET_CLASS(pSelf)->get_offset)
@@ -1101,6 +1113,31 @@ dasher_editor_edit_protect(DasherEditor *pSelf) {
   pPrivate->iLastOffset = gtk_text_buffer_get_char_count(pPrivate->pBuffer);
 }
 
+// Backward version of gtk_text_iter_forward_to_line_end.
+static void text_iter_backward_to_line_end(GtkTextIter *pPos) {
+  // If already at the end of a paragraph back up to the first non
+  // paragraph character.
+  while (gtk_text_iter_ends_line(pPos)) {
+    if (!gtk_text_iter_backward_char(pPos)) {
+      return;
+    }
+  }
+  while (!gtk_text_iter_ends_line(pPos)) {
+    if (!gtk_text_iter_backward_char (pPos)) {
+      return;  // Start of the buffer.
+    }
+  }
+}
+
+// If 'pPos' is on whitespace, advance it until the next non-whitespace character.
+static void skip_whitespace_forward(GtkTextIter *pPos) {
+  while (g_unichar_isspace(gtk_text_iter_get_char(pPos))) {
+    if (!gtk_text_iter_forward_char (pPos)) {
+      return;  // Start of the buffer.
+    }
+  }
+}
+
 static void edit_find(bool bForwards, Dasher::CControlManager::EditDistance iDist, DasherEditorPrivate 
*pPrivate, GtkTextIter *pPos) {
   if(bForwards) {
     switch(iDist) {
@@ -1109,6 +1146,7 @@ static void edit_find(bool bForwards, Dasher::CControlManager::EditDistance iDis
       break;
     case Dasher::CControlManager::EDIT_WORD:
       gtk_text_iter_forward_word_end(pPos);
+      skip_whitespace_forward(pPos);
       break;
     case Dasher::CControlManager::EDIT_LINE:
       if(!gtk_text_view_forward_display_line_end(GTK_TEXT_VIEW(pPrivate->pTextView), pPos))
@@ -1117,6 +1155,16 @@ static void edit_find(bool bForwards, Dasher::CControlManager::EditDistance iDis
         gtk_text_view_forward_display_line_end(GTK_TEXT_VIEW(pPrivate->pTextView), pPos);
       }
       break;
+    case Dasher::CControlManager::EDIT_SENTENCE:
+      // Use the Pango sentence end rules.
+      gtk_text_iter_forward_sentence_end(pPos);
+      skip_whitespace_forward(pPos);
+      break;
+    case Dasher::CControlManager::EDIT_PARAGRAPH:
+      // Moves to the next \n, \r, \r\n or the Unicode paragraph separator character.
+      gtk_text_iter_forward_to_line_end(pPos);
+      skip_whitespace_forward(pPos);
+      break;
     case Dasher::CControlManager::EDIT_FILE:
       gtk_text_iter_forward_to_end(pPos);
       break;
@@ -1131,10 +1179,16 @@ static void edit_find(bool bForwards, Dasher::CControlManager::EditDistance iDis
       gtk_text_iter_backward_word_start(pPos);
       break;
     case Dasher::CControlManager::EDIT_LINE:
-
       if(!gtk_text_view_backward_display_line_start(GTK_TEXT_VIEW(pPrivate->pTextView), pPos))
         gtk_text_view_backward_display_line(GTK_TEXT_VIEW(pPrivate->pTextView), pPos);
       break;
+    case Dasher::CControlManager::EDIT_SENTENCE:
+      gtk_text_iter_backward_sentence_start(pPos);
+      break;
+    case Dasher::CControlManager::EDIT_PARAGRAPH:
+      // Moves to the previous \n, \r, \r\n or the Unicode paragraph separator character.
+      text_iter_backward_to_line_end(pPos);
+      break;
     case Dasher::CControlManager::EDIT_FILE:
       gtk_text_buffer_get_start_iter(pPrivate->pBuffer, pPos);
       break;
diff --git a/Src/Gtk2/dasher_editor.h b/Src/Gtk2/dasher_editor.h
index bf87b68..3efdc25 100644
--- a/Src/Gtk2/dasher_editor.h
+++ b/Src/Gtk2/dasher_editor.h
@@ -77,6 +77,7 @@ void dasher_editor_end_compose(DasherEditor *pSelf, bool bKeep);
 
 /* Function for reading the active buffer */
 const gchar *dasher_editor_get_context(DasherEditor *pSelf, int iOffset, int iLength);
+const gchar* dasher_editor_get_text_around_cursor(DasherEditor *pSelf, Dasher::CControlManager::EditDistance 
dist);
 gint dasher_editor_get_offset(DasherEditor *pSelf);
 
 gint dasher_editor_ctrl_move(DasherEditor *pSelf, bool bForwards, Dasher::CControlManager::EditDistance 
dist);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]