CRichEditView text auto-formatting...

..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);
}


LVL 1
boodabellyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

boodabellyAuthor Commented:
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
AndyAinscowFreelance programmer / ConsultantCommented:
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
boodabellyAuthor Commented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

AndyAinscowFreelance programmer / ConsultantCommented:
OK I understand what you are trying to do.
I'll have a think.
0
AndyAinscowFreelance programmer / ConsultantCommented:
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
boodabellyAuthor Commented:
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
AndyAinscowFreelance programmer / ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
boodabellyAuthor Commented:
wooooo hoooo d00d, you rock!

Thanks
0
AndyAinscowFreelance programmer / ConsultantCommented:
Your welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.