Click to See Complete Forum and Search --> : Five little functions required for text manipulation


Ali Imran
June 13th, 2005, 05:14 PM
Question is regarding richedit control

Note: Index must be zero-based. And hwnd is the handle to richedit control.

1. I require a funtion to get global position of cursor in a rich edit control (not the mouse cursor but text cursor), something like


int getCursorPos(HWND hwnd) {
return current character index where cursor is located
}


2. get index of first character in current line


int getLineFirstCharPos(HWND hwnd) {
return index of first character in current line where cursor is located
}


3. get current line number


int getLineNumber(HWND hwnd) {
return current line number where cursor is located
}


4. total number of lines


int getTotalLines(HWND hwnd) {
return Total number of lines
}



5. total number of characters


int getTotalChars(HWND hwnd) {
return Total number of characters in richedit
}


EDITTED
Sory need one more function
6. get index of last character in current line


int getLineFirstCharPos(HWND hwnd) {
return index of last character in current line where cursor is located
}




I have posted few qeustions before, and I could not explain better.
I hope it is not considered spam since post does not include any link.

Waiting for kind urgent reply (if posible).

regards

SuperKoko
June 13th, 2005, 06:02 PM
#define STRICT
#include <windows.h>
#include <richedit.h>

int getCursorPos(HWND hwnd);
int getLineNumber(HWND hwnd);
int getTotalLines(HWND hwnd);
int getLineFirstCharPos(HWND hwnd);
int getTotalChars(HWND hwnd);

int getCursorPos(HWND hwnd)
{
// return current character index where cursor is located

// note that when there is something selected, the "text cursor", also known as "caret", has no defined position
// This function returns the position of the first character of the selection if there is a selection
CHARRANGE range;
SendMessage(hwnd,EM_EXGETSEL,0,LPARAM(&range));
return range.cpMin;
}
int getLineNumber(HWND hwnd)
{
// return current line number where cursor is located

return SendMessage(hwnd, EM_EXLINEFROMCHAR, 0, getCursorPos(hwnd));// index of the line containing the caret!
}
int getTotalLines(HWND hwnd)
{
// return Total number of lines
return SendMessage(hwnd,EM_GETLINECOUNT,0,0);
}
int getLineFirstCharPos(HWND hwnd)
{
// return index of first character in current line where cursor is located
return SendMessage(hwnd, EM_LINEINDEX, getLineNumber(hwnd), 0); // index of first character of the line containing the caret!
}
int getTotalChars(HWND hwnd)
{
// return Total number of characters in richedit
return GetWindowTextLength(hwnd);
}

Note, that carriage-returns are considered as characters, and each carriage-return contain two characters (a return character ('\r'=13) and a newline ('\n'=10) character).

I tested these functions, and it seems to work.
Note, that MSDN says that GetWindowTextLength can return a greater value than the number of characters if some Unicode or DCBS characters are used.
In that case, you should call GetWindowText to get all the text, and check the return value of GetWindowText... but that is slow.

SuperKoko
June 13th, 2005, 06:14 PM
int getLineLastCharPos(HWND hwnd)
{
// return (index of last character)+1 in current line where cursor is located
// i am sure that you want the (index of the last character)+1, instead of the index of last character, like you said

DWORD LineIndex=getLineNumber(hwnd);
DWORD FirstCharIndex=SendMessage(hwnd, EM_LINEINDEX, LineIndex, 0);
return FirstCharIndex+SendMessage(hwnd, EM_LINELENGTH, FirstCharIndex, 0);
}

Ali Imran
June 13th, 2005, 08:48 PM
That is gerat,

few more help needed if possible :d

1. when richedit scrolled, messages EN_VSCROLL and EN_HSCROLL are not being recieved
keeping mind that richedit is subclassed.


2. index of left most visible character (regardless the scroll position)
LONG getLeftCharPos(HWND hwnd) {
return left most visible character index
}


3. How do I prevent richedit not to loose focus when tab key is pressed? anything to change in its style ? And I want it to add tab space when tab key pressed.

Your help is highly appreciated.

regards