Solved

CRichEditView text auto-formatting...

Posted on 2003-11-23
9
390 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
OK I understand what you are trying to do.
I'll have a think.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
wooooo hoooo d00d, you rock!

Thanks
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Your welcome
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now