Solved

CRichEditView text auto-formatting...

Posted on 2003-11-23
9
396 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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