Example #1
0
static int ReadColorize(CurPos &cp, EColorize *Colorize, const char *ModeName) {
    unsigned char obj;
    unsigned short len;

    long LastState = -1;

    while ((obj = GetObj(cp, len)) != 0xFF) {
        switch (obj) {
        case CF_COLOR:
            if (ReadHilitColors(cp, Colorize, ModeName) == -1) return -1;
            break;

        case CF_KEYWORD:
            {
                const char *colorstr;

                if ((colorstr = GetCharStr(cp, len)) == 0) return -1;

                unsigned int Col;
                unsigned int ColBg, ColFg;

                if (sscanf(colorstr, "%1X %1X", &ColFg, &ColBg) != 2)
                    return 0;

                Col = ColFg | (ColBg << 4);

                int color = ChColor(Col);
                if (ReadKeywords(cp, &Colorize->Keywords, color) == -1) return -1;
            }
            break;

        case CF_HSTATE:
            {
                long stateno;
                long color;

                if (Colorize->hm == 0)
                    Colorize->hm = new HMachine();

                assert(Colorize->hm != 0);

                if (GetNum(cp, stateno) == 0)
                    return -1;

                assert(stateno == LastState + 1);

                obj = GetObj(cp, len);
                assert(obj == CF_INT);

                if (GetNum(cp, color) == 0)
                    return -1;

                HState newState;

                newState.InitState();

                newState.color = color;

                Colorize->hm->AddState(newState);
                LastState = stateno;
            }
            break;

        case CF_HTRANS:
            {
                HTrans newTrans;
                long nextState;
                long matchFlags;
                const char *match;
                long color;

                if (GetNum(cp, nextState) == 0)
                    return -1;
                obj = GetObj(cp, len);
                assert(obj == CF_INT);
                if (GetNum(cp, matchFlags) == 0)
                    return -1;
                obj = GetObj(cp, len);
                assert(obj == CF_INT);
                if (GetNum(cp, color) == 0)
                    return -1;
                obj = GetObj(cp, len);
                assert(matchFlags & MATCH_REGEXP ? obj == CF_REGEXP : obj == CF_STRING);
                if ((match = GetCharStr(cp, len)) == 0)
                    return -1;

                newTrans.InitTrans();

                newTrans.matchFlags = matchFlags;
                newTrans.nextState = nextState;
                newTrans.color = color;

                if (newTrans.matchFlags & MATCH_REGEXP) {
                    newTrans.regexp = RxCompile(match);
                    newTrans.matchLen = 0;
                } else if ((newTrans.matchFlags & MATCH_SET) ||
                           (newTrans.matchFlags & MATCH_NOTSET))
                {
                    newTrans.matchLen = 1;
                    newTrans.match = (char *)malloc(256/8);
                    assert(newTrans.match != NULL);
                    SetWordChars(newTrans.match, match);
                } else {
                    newTrans.match = strdup(match);
                    newTrans.matchLen = strlen(match);
                }

                Colorize->hm->AddTrans(newTrans);
            }
            break;

        case CF_HWTYPE:
            {
                long nextKwdMatchedState;
                long nextKwdNotMatchedState;
                long nextKwdNoCharState;
                long options;
                const char *wordChars;

                obj = GetObj(cp, len);
                assert(obj == CF_INT);
                if (GetNum(cp, nextKwdMatchedState) == 0)
                    return -1;

                obj = GetObj(cp, len);
                assert(obj == CF_INT);
                if (GetNum(cp, nextKwdNotMatchedState) == 0)
                    return -1;

                obj = GetObj(cp, len);
                assert(obj == CF_INT);
                if (GetNum(cp, nextKwdNoCharState) == 0)
                    return -1;

                obj = GetObj(cp, len);
                assert(obj == CF_INT);
                if (GetNum(cp, options) == 0)
                    return -1;

                obj = GetObj(cp, len);
                assert(obj == CF_STRING);
                if ((wordChars = GetCharStr(cp, len)) == 0)
                    return -1;

                Colorize->hm->LastState()->options = options;
                Colorize->hm->LastState()->nextKwdMatchedState = nextKwdMatchedState;
                Colorize->hm->LastState()->nextKwdNotMatchedState = nextKwdNotMatchedState;
                Colorize->hm->LastState()->nextKwdNoCharState = nextKwdNoCharState;

                if (wordChars && *wordChars) {
                    Colorize->hm->LastState()->wordChars = (char *)malloc(256/8);
                    assert(Colorize->hm->LastState()->wordChars != NULL);
                    SetWordChars(Colorize->hm->LastState()->wordChars, wordChars);
                }
            }
            break;

        case CF_HWORDS:
            {
                const char *colorstr;
                int color;

                if ((colorstr = GetCharStr(cp, len)) == 0) return -1;

                color = hcPlain_Keyword;

                if (strcmp(colorstr, "-") != 0) {
                    const char *Value = colorstr;
                    int Col;

                    if (*Value == '-') {
                        Value++;
                        if (sscanf(Value, "%1X", &Col) != 1) return -1;
                        Col |= (hcPlain_Background & 0xF0);
                    } else if (Value[1] == '-') {
                        if (sscanf(Value, "%1X", &Col) != 1) return -1;
                        Col <<= 4;
                        Col |= (hcPlain_Background & 0x0F);
                    } else {
                        unsigned int ColBg, ColFg;

                        if (sscanf(colorstr, "%1X %1X", &ColFg, &ColBg) != 2)
                            return 0;

                        Col = ColFg | (ColBg << 4);
                    }
                    color = Col;
                }
                if (ReadKeywords(cp, &Colorize->hm->LastState()->keywords, color) == -1) return -1;
            }
            break;

        case CF_SETVAR:
            {
                long what;

                if (GetNum(cp, what) == 0) return -1;
                switch (GetObj(cp, len)) {
                case CF_STRING:
                    {
                        const char *val = GetCharStr(cp, len);
                        if (len == 0) return -1;
                        if (SetColorizeString(Colorize, what, val) != 0) return -1;
                    }
                    break;
                    /*                case CF_INT:
                     {
                     long num;

                     if (GetNum(cp, num) == 0) return -1;
                     if (SetModeNumber(Mode, what, num) != 0) return -1;
                     }
                     break;*/
                default:
                    return -1;
                }
            }
            break;
        case CF_END:
            return 0;
        default:
            return -1;
        }
    }
    return -1;
}
Example #2
0
// WM_COMMAND message handler.
LRESULT KeywordEdit::OnCommand( UINT nNotifyCode, UINT nCtrlID, HWND hWndCtrl )
{
	// What's the ID?
	switch ( nCtrlID )
	{
		case	EditToolbar::ETID_EDIT:
		{
			// Valid entry?
			LPKEYWORDS pKW = ( LPKEYWORDS )m_List.GetItemData( m_List.GetCurSel());
			if ( pKW != ( LPKEYWORDS )LB_ERR )
			{
				// Edit the keywords.
				KWEdit kwe;
				kwe.Edit( *this, pKW, this );
			}
			return 0;
		}

		case	EditToolbar::ETID_INSERT:
		{
			// Popup the file dialog.
			ClsFileDialog fd;
			fd.Filters() = _T( "Text files (*.txt)\0*.txt\0All files (*.*)\0*.*\0\0" );
			fd.Caption().LoadString( IDS_INSERT_KEYWORDS );
			
			if ( fd.DoModal( this, NULL, NULL, TRUE, OFN_FILEMUSTEXIST ))
			{
				// Get the selected filename.
				ClsString name;
				fd.GetName( 0, name );

				// Read the file and add the read keywords
				// to the selected node.
				ReadKeywords( name );

				// Make sure the keywords are
				// displayed.
				SendMessage( WM_COMMAND, MAKEWPARAM( IDC_LIST, LBN_SELCHANGE ), ( LPARAM )m_List.GetSafeHWND());
			}
			return 0;
		}

			
		case	EditToolbar::ETID_COLOR:
		{
			// Get the current selection.
			int nSel = m_List.GetCurSel();

			// Only show the popup when there is
			// a valid selection.
			if ( nSel != LB_ERR )
			{
				// Get the position of the selected entry.
				ClsRect rc;
				m_Tools.GetItemRect( 6, rc );
				m_Tools.ClientToScreen( rc );

				// Obtain the entry data.
				LPKEYWORDS pKW = ( LPKEYWORDS )m_List.GetItemData( nSel );
				if ( pKW != ( LPKEYWORDS )LB_ERR )
				{
					// Create the popup. The popup will automatically destroy
					// itself.
					new ClsColorPopup( ClsPoint( rc.Left(), rc.Bottom()), 
							   pKW->crColor, 
							   this,
							   CS_FORE,
							   NULL, 
							   ClsString( MAKEINTRESOURCE( IDS_CUSTOM )), 
							   NULL, 
							   TRUE,
							   FALSE );
				}
			}
			return 0;
		}

		case	EditToolbar::ETID_BGCOLOR:
		{
			// Get the current selection.
			int nSel = m_List.GetCurSel();

			// Only show the popup when there is
			// a valid selection.
			if ( nSel != LB_ERR )
			{
				// Get the position of the selected entry.
				ClsRect rc;
				m_Tools.GetItemRect( 7, rc );
				m_Tools.ClientToScreen( rc );

				// Obtain the entry data.
				LPKEYWORDS pKW = ( LPKEYWORDS )m_List.GetItemData( nSel );
				if ( pKW != ( LPKEYWORDS )LB_ERR )
				{
					// Create the popup. The popup will automatically destroy
					// itself.
					new ClsColorPopup( ClsPoint( rc.Left(), rc.Bottom()), 
							   pKW->crBgColor, 
							   this,
							   CS_BACK,
							   ClsString( MAKEINTRESOURCE( IDS_TRANSPARENT )), 
							   ClsString( MAKEINTRESOURCE( IDS_CUSTOM )), 
							   NULL, 
							   TRUE,
							   FALSE );
				}
			}
			return 0;
		}

		case	IDC_LIST:
		{
			// Selection change?
			if ( nNotifyCode == LBN_SELCHANGE )
			{
				// Valid entry?
				LPKEYWORDS pKW = ( LPKEYWORDS )m_List.GetItemData( m_List.GetCurSel());
				if ( pKW != ( LPKEYWORDS )LB_ERR )
				{
					// Reset words content.
					m_KeyList.ResetContent();

					// Add keywords.
					m_KeyList.SetRedraw( FALSE );
					for ( int i = 0; i < ::ArrayGetSize( pKW->lpaKeywords ); i++ )
						m_KeyList.AddString(( LPCTSTR )*(( LPTSTR * )::ArrayGetAt( pKW->lpaKeywords, i )));
					m_KeyList.SetRedraw();

					// Setup UI.
					SetupControls();
				}
			}
			return 0;
		}

		case	EditToolbar::ETID_DELETE:
		{			
			// Valid entry?
			LPKEYWORDS pKW = ( LPKEYWORDS )m_List.GetItemData( m_List.GetCurSel());
			if ( pKW != ( LPKEYWORDS )LB_ERR )
			{
				ClsMessageBox mb;
				mb.Title() = ClsGetApp()->GetAppTitle();
				mb.Buttons().LoadString( IDS_YESNO );
				mb.Body().LoadString( IDS_KEYWORD_DELETE );
				mb.Flags() = ClsMessageBox::MBF_ICONEXCLAMATION;
				// Sure?
				if ( mb.MsgBox( GetSafeHWND()))
				{
					// Save current selection
					// position.
					int nSel = m_List.GetCurSel();
					int nPos = nSel;

					// Select the next one or
					// the previous one.
					if ( nSel == m_List.GetCount() - 1 ) nSel--;
					else				     nSel++;

					// Select the other item.
					m_List.SetCurSel( nSel );
					
					// Setup UI.
					SetupControls();

					// Clear the list contents
					m_KeyList.ResetContent();

					// Reset selection.
					SendMessage( WM_COMMAND, MAKEWPARAM( IDC_LIST, LBN_SELCHANGE ), ( LPARAM )m_List.GetSafeHWND());

					// Remove the selection.
					m_List.DeleteString( nPos );

					// Remove the node.
					Remove(( LPNODE )pKW );

					// Free the keyword array and the node.
					if ( pKW->lpaKeywords ) ::ArrayDelete( pKW->lpaKeywords );
					::FreePooled( pParserPool, pKW );
				}	
			}
			return 0;
		}

		case	EditToolbar::ETID_NEW:
		{
			// Allocate a new node.
			LPKEYWORDS pKW = ( LPKEYWORDS )::AllocPooled( pParserPool, sizeof( KEYWORDS ));
			if ( pKW )
			{
				// Allocate an array.
				pKW->lpaKeywords = ::ArrayCreate( 0, 500, sizeof( LPTSTR * ));
				if ( pKW->lpaKeywords )
				{
					// Set color.
					pKW->crColor   = ::GetSysColor( COLOR_WINDOWTEXT );
					pKW->crBgColor = CLR_DEFAULT;

					// Add the node to the list.
					AddTail(( LPLIST )&m_KeyLst, ( LPNODE )pKW );

					// Add it to the listview and select it.
					int nPos = m_List.AddString(( LPCTSTR )pKW );
					m_List.SetCurSel( nPos );

					// Clear the keywords list.
					m_KeyList.ResetContent();

					// Setup UI.
					SetupControls();
					return 0;
				}
				else
					::FreePooled( pParserPool, pKW );
			}
			// Error.
			MessageBox( ClsString( MAKEINTRESOURCE( IDS_NO_MEMORY )), ClsGetApp()->GetAppTitle(), MB_ICONERROR | MB_OK );
			return 0;
		}

		case	IDC_CASE:
			// Get value.
			m_bCase = ( BOOL )( m_Case.GetCheck() == BST_CHECKED );
			return 0;

		case	IDC_OK:
			// Return OK
			EndDialog( TRUE );
			return 0;

		case	IDC_CANCEL:
			// Cancel.
			EndDialog( FALSE );
			return 0;
	}
	// Pass onto the base class.
	return ClsDialog::OnCommand( nNotifyCode, nCtrlID, hWndCtrl );
}