Solved

CRichEditView text auto-formatting...

Posted on 2003-11-23
9
392 Views
Last Modified: 2013-11-20
..as the user types (like MSVC++).  I would like to implement a little auto-formatting for a code editor that we are making, this is the start of it but we have not been able to get it to work.

I am trying the following, but it does not change the character in the buffer and only outpus the currently pressed character m_indent times

void Cbyy_view::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)  // inherited from CRichEditView
{
      int i;
      UINT temp_char;


      switch(nChar)
      {
            // {
            case(123):
                  m_indent++;
                  break;

            // }
            case(125):
                  m_indent > 0 ? m_indent-- : 0;
                  break;

            // enter
            case(13):
                  nChar = nChar;
                  break;

            // backspace
            case(8):
                  break;
      };

      for(i = 0; i < m_indent; i++)
      {
            temp_char = 9;
            nFlags = 15;
            CRichEditView::OnChar(nChar, nRepCnt, nFlags);
      }

      CRichEditView::OnChar(nChar, nRepCnt, nFlags);
}


0
Comment
Question by:boodabelly
  • 5
  • 4
9 Comments
 
LVL 1

Author Comment

by:boodabelly
ID: 9808127
just noticed an error in my code, but it does not change the result at all

     for(i = 0; i < m_indent; i++)
     {
          temp_char = 9;
          nFlags = 15;
          CRichEditView::OnChar(nChar, nRepCnt, nFlags);
     }

tried...

     for(i = 0; i < m_indent; i++)
     {
          temp_char = 9;
          nFlags = 15;
          CRichEditView::OnChar(temp_char, nRepCnt, nFlags);
     }

still does not make a difference
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9808836
I don't really understand wwhat you are trying to do BUT does this do anything to your app?

tried...

     for(i = 0; i < m_indent; i++)
     {
          temp_char = _T('x');     // 9;
          nFlags = 15;
          CRichEditView::OnChar(temp_char, nRepCnt, nFlags);
     }
0
 
LVL 1

Author Comment

by:boodabelly
ID: 9808880
no that does not, basically what I am trying to accomplish, when typing code in most editors, the user presses the '{' then 'enter', the result would be.

int foo()
{ // now the remaining code is indented
     int i;
     int j;
 
     if(hello())
     {
          exit(0);
     }

     return 0;
} // now the remain code is back to left margin

The m_indent member variable just keeps track of the number of tabs to insert in the line before adding the text.  Is there an easier way to do this?  I do not want to mess with the margin, only insert some tabs.  I cannot figure out how to just add some text to the rich edit view and calling CRichEditView::OnChar() is not working either.  Just FYI

temp_char = 9 = tab char
nFlags = 15 = flags that are normally set when the user presses the 'tab' key
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9808948
OK I understand what you are trying to do.
I'll have a think.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9809109
I've had a look at the logic and think I have seen a problem
eg. users types hello, appears on screen hello
(Using space instead of tab) users wants it indented one tab and types hello, appears h e l l o
and two tabs appears h  e  l  l  o
you only need to pump the tabs AFTER a new line

how about
switch
...
         case(123):
               m_indent++;
               return;//exit function

          // }
          case(125):
               m_indent > 0 ? m_indent-- : 0;
               return;//exit function

         case(13):
   CRichEditView::OnChar(nChar, nRepCnt, nFlags);
    for(i = 0; i < m_indent; i++)
     {
          temp_char = 9;
          nFlags = 15;
          CRichEditView::OnChar(temp_char, nRepCnt, nFlags);
     }

               return;  //exit function


//end of switch
          CRichEditView::OnChar(temp_char, nRepCnt, nFlags);



0
 
LVL 1

Author Comment

by:boodabelly
ID: 9809244
Andy,

I am aware that the logic is not on par, I am only trying to add characters to the EditView right now, once that is done I can work on getting it to function properly, but yeah, it only needs to indent after they hit the enter key....well ok, so I changed it and tried it out

void Cbyy_view::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
      int i;
      UINT temp_char;


      switch(nChar)
      {
            // {
            case(123):
                  m_indent++;
                  break;

            // }
            case(125):
                  m_indent > 0 ? m_indent-- : 0;
                  break;

            // enter
            case(13):
                  CRichEditView::OnChar(nChar, nRepCnt, nFlags);

                  temp_char = 9;
                  nFlags = 15;

                  for(i = 0; i < m_indent; i++)
                  {
                        CRichEditView::OnChar(9, nRepCnt, 15);
                  }

                  break;

            // backspace
            case(8):
                  break;
      };

      CRichEditView::OnChar(nChar, nRepCnt, nFlags);
}

this now only adds multiple (m_indent times) 'enter' characters to the CRichEditView.  It does not seem to matter what ::OnChar() is called with, it seems that it still reads stdin.  I also tried writing to stdin, but appearantly there is a seperate keyboard buffer that the MFC app reads out of.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 9809283
How about sending a tab character multiple times and let the normal behaviour handle it.
instead of
CRichEditView::OnChar(9, nRepCnt, 15);

use (or similar)
SendMessage(WM_KEYDOWN...)

in your loop to generate the tabbed section
0
 
LVL 1

Author Comment

by:boodabelly
ID: 9809355
wooooo hoooo d00d, you rock!

Thanks
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9809366
Your welcome
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 103
sameEnds challenge 3 160
java ^ examples 8 67
wordcount challenge 11 121
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question