Solved

CRichEditView text auto-formatting...

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

Expert Comment

by:AndyAinscow
ID: 9808948
OK I understand what you are trying to do.
I'll have a think.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 48
C++ BOOL WINAPI ReadFile fails on windows 10 when reading from USB cable 9 383
Tab names are off by one 5 53
sum67 challenge 35 93
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. …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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.

867 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

19 Experts available now in Live!

Get 1:1 Help Now