============================================================================= EditToggleLineComments()
| 3034 | // EditToggleLineComments() |
| 3035 | // |
| 3036 | void EditToggleLineComments ( HWND hwnd, LPCWSTR pwszComment, BOOL bInsertAtStart ) |
| 3037 | { |
| 3038 | char mszComment[256 * 3] = ""; |
| 3039 | int cchComment; |
| 3040 | int mbcp; |
| 3041 | int iAction = 0; |
| 3042 | int iSelStart = ( int ) SendMessage ( hwnd, SCI_GETSELECTIONSTART, 0, 0 ); |
| 3043 | int iSelEnd = ( int ) SendMessage ( hwnd, SCI_GETSELECTIONEND, 0, 0 ); |
| 3044 | int iCurPos = ( int ) SendMessage ( hwnd, SCI_GETCURRENTPOS, 0, 0 ); |
| 3045 | if ( SendMessage ( hwnd, SCI_GETCODEPAGE, 0, 0 ) == SC_CP_UTF8 ) { |
| 3046 | mbcp = CP_UTF8; |
| 3047 | } else { |
| 3048 | mbcp = CP_ACP; |
| 3049 | } |
| 3050 | if ( lstrlen ( pwszComment ) ) { |
| 3051 | WideCharToMultiByte ( mbcp, 0, pwszComment, -1, mszComment, COUNTOF ( mszComment ), NULL, NULL ); |
| 3052 | } |
| 3053 | cchComment = lstrlenA ( mszComment ); |
| 3054 | if ( SC_SEL_RECTANGLE != SendMessage ( hwnd, SCI_GETSELECTIONMODE, 0, 0 ) && cchComment ) { |
| 3055 | int iLine; |
| 3056 | int iCommentCol = 0; |
| 3057 | int iLineStart = ( int ) SendMessage ( hwnd, SCI_LINEFROMPOSITION, ( WPARAM ) iSelStart, 0 ); |
| 3058 | int iLineEnd = ( int ) SendMessage ( hwnd, SCI_LINEFROMPOSITION, ( WPARAM ) iSelEnd, 0 ); |
| 3059 | if ( iSelEnd <= SendMessage ( hwnd, SCI_POSITIONFROMLINE, ( WPARAM ) iLineEnd, 0 ) ) { |
| 3060 | if ( iLineEnd - iLineStart >= 1 ) { |
| 3061 | iLineEnd--; |
| 3062 | } |
| 3063 | } |
| 3064 | if ( !bInsertAtStart ) { |
| 3065 | iCommentCol = 1024; |
| 3066 | for ( iLine = iLineStart; iLine <= iLineEnd; iLine++ ) { |
| 3067 | int iLineEndPos = ( int ) SendMessage ( hwnd, SCI_GETLINEENDPOSITION, ( WPARAM ) iLine, 0 ); |
| 3068 | int iLineIndentPos = ( int ) SendMessage ( hwnd, SCI_GETLINEINDENTPOSITION, ( WPARAM ) iLine, 0 ); |
| 3069 | if ( iLineIndentPos != iLineEndPos ) { |
| 3070 | int iIndentColumn = ( int ) SendMessage ( hwnd, SCI_GETCOLUMN, ( WPARAM ) iLineIndentPos, 0 ); |
| 3071 | iCommentCol = min ( iCommentCol, iIndentColumn ); |
| 3072 | } |
| 3073 | } |
| 3074 | } |
| 3075 | SendMessage ( hwnd, SCI_BEGINUNDOACTION, 0, 0 ); |
| 3076 | for ( iLine = iLineStart; iLine <= iLineEnd; iLine++ ) { |
| 3077 | int iCommentPos; |
| 3078 | int iIndentPos = ( int ) SendMessage ( hwnd, SCI_GETLINEINDENTPOSITION, ( WPARAM ) iLine, 0 ); |
| 3079 | char tchBuf[32]; |
| 3080 | struct TextRange tr; |
| 3081 | if ( iIndentPos == SendMessage ( hwnd, SCI_GETLINEENDPOSITION, ( WPARAM ) iLine, 0 ) ) { |
| 3082 | continue; |
| 3083 | } |
| 3084 | tr.chrg.cpMin = iIndentPos; |
| 3085 | tr.chrg.cpMax = tr.chrg.cpMin + min ( 31, cchComment ); |
| 3086 | tr.lpstrText = tchBuf; |
| 3087 | SendMessage ( hwnd, SCI_GETTEXTRANGE, 0, ( LPARAM ) &tr ); |
| 3088 | if ( StrCmpNIA ( tchBuf, mszComment, cchComment ) == 0 ) { |
| 3089 | switch ( iAction ) { |
| 3090 | case 0: |
| 3091 | iAction = 2; |
| 3092 | case 2: |
| 3093 | SendMessage ( hwnd, SCI_SETTARGETSTART, ( WPARAM ) iIndentPos, 0 ); |