Clipboard problem with CRichEdit
-
Hi! I want to retain only *one* text-format in a CRichEdit derived class of mine. Now if the user paste text into my edit this plan goes not so well. Then I try something like this:
BOOL CCalEdit::PreTranslateMessage(MSG *pMSG) { BOOL bResult = CRichEditCtrl::PreTranslateMessage(pMSG); if(pMSG) { if((pMSG->message & WM_COPY) == 1 && IsKeyPressed(VK_CONTROL)):-O { if(::OpenClipboard(NULL)) { HANDLE hCB = ::GetClipboardData(CF_TEXT); if(hCB) { LPVOID pCB_Void = (LPVOID)hCB; LPSTR pCB_String = (LPSTR)pCB_Void; CString cstrCB(pCB_String); if(!cstrCB.IsEmpty()) { HideSelection(TRUE, FALSE); SetSel(0, -1); CString cstrThis(GetSelText()); ReplaceSel(cstrThis); HideSelection(FALSE, FALSE); } } ::CloseClipboard(); } } } return bResult; }
Seems like I got several problems. a. My :-O-if(...) is not a 100% secure way to determine if a paste op. is in progress - ??? How do I do that? b. Second - how do I apply my format to the text on the clipboard? I've have a SetDefaultCharFormat() in my class that I call when I create the edit and it's the job it does that I want to apply also on the text on the clipboars *before* it is pasted in. I appreciate any idéa. Ps. There seems to be a LOT of WM_PASTE going ind - after some testing I ended up with: (pMSG->message & WM_COPY) == 1 - don't say it's perfect yet. Regards, Michael Mogensen, mm it-consult dk. ><((((º> ·.¸¸.· ><((((º> ·.¸¸.· ><((((º> -
Hi! I want to retain only *one* text-format in a CRichEdit derived class of mine. Now if the user paste text into my edit this plan goes not so well. Then I try something like this:
BOOL CCalEdit::PreTranslateMessage(MSG *pMSG) { BOOL bResult = CRichEditCtrl::PreTranslateMessage(pMSG); if(pMSG) { if((pMSG->message & WM_COPY) == 1 && IsKeyPressed(VK_CONTROL)):-O { if(::OpenClipboard(NULL)) { HANDLE hCB = ::GetClipboardData(CF_TEXT); if(hCB) { LPVOID pCB_Void = (LPVOID)hCB; LPSTR pCB_String = (LPSTR)pCB_Void; CString cstrCB(pCB_String); if(!cstrCB.IsEmpty()) { HideSelection(TRUE, FALSE); SetSel(0, -1); CString cstrThis(GetSelText()); ReplaceSel(cstrThis); HideSelection(FALSE, FALSE); } } ::CloseClipboard(); } } } return bResult; }
Seems like I got several problems. a. My :-O-if(...) is not a 100% secure way to determine if a paste op. is in progress - ??? How do I do that? b. Second - how do I apply my format to the text on the clipboard? I've have a SetDefaultCharFormat() in my class that I call when I create the edit and it's the job it does that I want to apply also on the text on the clipboars *before* it is pasted in. I appreciate any idéa. Ps. There seems to be a LOT of WM_PASTE going ind - after some testing I ended up with: (pMSG->message & WM_COPY) == 1 - don't say it's perfect yet. Regards, Michael Mogensen, mm it-consult dk. ><((((º> ·.¸¸.· ><((((º> ·.¸¸.· ><((((º>A simpler solution would be to handle the EN_UPDATE notification message, and set all the text in the control to the format that you want. You could try to make it more sophisticated by only changing the text that is new, but since the operations are fairly infrequent (in computer time...), I wouldn't worry. FYI, EN_UPDATE notification messages are sent after the text changes, but BEFORE the display is updated - a perfect time for changing formatting. Hope this helps Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact" -
A simpler solution would be to handle the EN_UPDATE notification message, and set all the text in the control to the format that you want. You could try to make it more sophisticated by only changing the text that is new, but since the operations are fairly infrequent (in computer time...), I wouldn't worry. FYI, EN_UPDATE notification messages are sent after the text changes, but BEFORE the display is updated - a perfect time for changing formatting. Hope this helps Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"Thanx' sharing the ideas! And actually it helped this time. What I did was that I made the CHARFORMAT-change to the full selection in response to *not* an UPDATE event but a CHANGE ev. As it turned out an update-ev. came over and over again if I did the work on EN_UPDATE - ON_CHANGE was less sensitive. So this is how the final solution looks:
const void CCalFrameWnd::CCalendar::Note_EditChange() // Called from parent to signal that user are editing a note. { TRACE(_T("Note_EditChange()\n")); // Ensure usage of one charformat only. long lChar_Start = 0, lChar_End = 0; CHARFORMAT cf_def; cf_def.cbSize = sizeof(CHARFORMAT); m_pEdit_Note->GetDefaultCharFormat(cf_def); m_pEdit_Note->GetSel(lChar_Start, lChar_End); m_pEdit_Note->HideSelection(TRUE, FALSE); m_pEdit_Note->SetSel(0, -1); m_pEdit_Note->SetSelectionCharFormat(cf_def); m_pEdit_Note->SetSel(lChar_End, lChar_End); m_pEdit_Note->HideSelection(FALSE, FALSE); }
So I just force the def-CHARFORMAT on to the entire selection having always set the def-format first. Also I remember to be able to restore to the orig. caret pos. It works! :rose: Regards, Michael Mogensen, mm it-consult dk. ><((((º> ·.¸¸.· ><((((º> ·.¸¸.· ><((((º> -
Thanx' sharing the ideas! And actually it helped this time. What I did was that I made the CHARFORMAT-change to the full selection in response to *not* an UPDATE event but a CHANGE ev. As it turned out an update-ev. came over and over again if I did the work on EN_UPDATE - ON_CHANGE was less sensitive. So this is how the final solution looks:
const void CCalFrameWnd::CCalendar::Note_EditChange() // Called from parent to signal that user are editing a note. { TRACE(_T("Note_EditChange()\n")); // Ensure usage of one charformat only. long lChar_Start = 0, lChar_End = 0; CHARFORMAT cf_def; cf_def.cbSize = sizeof(CHARFORMAT); m_pEdit_Note->GetDefaultCharFormat(cf_def); m_pEdit_Note->GetSel(lChar_Start, lChar_End); m_pEdit_Note->HideSelection(TRUE, FALSE); m_pEdit_Note->SetSel(0, -1); m_pEdit_Note->SetSelectionCharFormat(cf_def); m_pEdit_Note->SetSel(lChar_End, lChar_End); m_pEdit_Note->HideSelection(FALSE, FALSE); }
So I just force the def-CHARFORMAT on to the entire selection having always set the def-format first. Also I remember to be able to restore to the orig. caret pos. It works! :rose: Regards, Michael Mogensen, mm it-consult dk. ><((((º> ·.¸¸.· ><((((º> ·.¸¸.· ><((((º>Michael Mogensen wrote: As it turned out an update-ev. came over and over again if I did the work on EN_UPDATE :-O Oops. I forgot to mention that by setting the text formatting, you cause another EN_UPDATE message to be sent, which becomes an endless recursion. You would need to set a static/class-member variable inside the handler and check it when the handler is entered to see if you're already processing the message, and return immediately if you are. Glad to be of help :) Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"