I'm trying to convert text to HTML.
Here is the code.
m_pTextCtrl is a wxTextCtrl.
wxString MainFrame::TextToHTML()
{
const wxString sOO = _T("00");
const wxString s8O = _T("80");
const wxString sFF = _T("FF");
wxString htmlString;
htmlString << _T("<html><head><title></title></head><body><tt>");
long max = m_pTextCtrl->GetLastPosition();
wxTextAttr style;
wxTextAttr oldStyle;
wxChar ch;
for(long pos = 0; pos < max; pos++)
{
m_pTextCtrl->GetStyle(pos, style);
if(style != oldStyle)
{
if(pos > 0)
{
htmlString << _T("</font>");
}
wxString sR, sG, sB;
wxColour colour = style.GetTextColour();
sR.Printf(_T("%02X"), colour.Red());
sG.Printf(_T("%02X"), colour.Green());
sB.Printf(_T("%02X"), colour.Blue());
htmlString << _T("<font color = \"#") << sR << sG << sB << _T("\">"); ;
}
if (m_pTextCtrl->GetInsertionPoint() == m_pTextCtrl->GetLastPosition())
{
ch = _T('\0');
}
else
{
ch = m_pTextCtrl->GetValue[m_pTextCtrl->GetInsertionPoint()];
}
switch(ch)
{
case _T('\n'):
{
htmlString << _T("\n<br>");
}
break;
case _T('<'):
{
htmlString << _T("<");
}
break;
case _T('&'):
{
htmlString << _T("&");
}
break;
case _T(' '):
{
htmlString << _T(" ");
}
break;
default:
{
htmlString << ch;
}
break;
}
oldStyle = style;
}
htmlString << _T("</font></tt></body></html>");
return htmlString;
}
I get build errors though...
Switching to target: default
Compiling: main.cpp
main.cpp: In member function `wxString MainFrame::TextToHTML()':
main.cpp:214: error: no match for 'operator!=' in 'style != oldStyle'
C:/wxWidgets-2.6.2/include/wx/string.h:1428: note: candidates are: bool operator!=(const wxString&, const wxString&)
C:/wxWidgets-2.6.2/include/wx/string.h:1430: note: bool operator!=(const wxString&, const wxChar*)
C:/wxWidgets-2.6.2/include/wx/string.h:1432: note: bool operator!=(const wxChar*, const wxString&)
C:/wxWidgets-2.6.2/include/wx/string.h:1473: note: bool operator!=(const wxString&, const wxCharBuffer&)
C:/wxWidgets-2.6.2/include/wx/string.h:1475: note: bool operator!=(const wxCharBuffer&, const wxString&)
C:/wxWidgets-2.6.2/include/wx/string.h:1496: note: bool operator!=(wxChar, const wxString&)
C:/wxWidgets-2.6.2/include/wx/string.h:1497: note: bool operator!=(const wxString&, wxChar)
C:/wxWidgets-2.6.2/include/wx/longlong.h:917: note: bool operator!=(long int, const wxLongLong&)
C:/wxWidgets-2.6.2/include/wx/longlong.h:930: note: bool operator!=(long unsigned int, const wxULongLong&)
C:/Program Files/CodeBlocks/include/objbase.h:82: note: BOOL operator!=(const GUID&, const GUID&)
main.cpp:239: error: invalid types `<unknown type>[long int]' for array subscript
Process terminated with status 1 (0 minutes, 11 seconds)
What am I doing wrong?
but what about this.
error: invalid types `<unknown type>[long int]' for array subscript
I tried that. I still get errors
main.cpp:235: error: ambiguous overload for 'operator[]' in '*(((MainFrame*)this)->MainFrame::m_pTextCtrl->wxTextCtrl::_vptr$wxObject + 768u)()[(*(((MainFrame*)this)->MainFrame::m_pTextCtrl->wxTextCtrl::_vptr$wxObject + 936u))(((MainFrame*)this)->MainFrame::m_pTextCtrl)]'
main.cpp:235: note: candidates are: operator[](const wxChar*, int) <built-in>
C:/wxWidgets-2.6.2/include/wx/string.h:767: note: wxChar& wxString::operator[](int)
C:/wxWidgets-2.6.2/include/wx/string.h:769: note: wxChar& wxString::operator[](size_t)
Process terminated with status 1 (0 minutes, 13 seconds)
I got
m_pTextCtrl->GetValue[m_pTextCtrl->GetInsertionPoint()];
From the wxWidgets manual so it should work I thought....
Well, that error happened so many times to me, but it was with an older version of wxWidgets IIRC.
The problem is operator [] for wxString is overloaded for both int and size_t, and GetInsertionPoint() returns long. The compiler doesn't know if it should convert that long to int or to size_t, then the ambiguity.
Try this:
m_pTextCtrl->GetValue()[static_cast<int>(m_pTextCtrl->GetInsertionPoint())];
If that doesn't help, then try:
int insertionPoint = m_pTextCtrl->GetInsertionPoint();
m_pTextCtrl->GetValue()[insertionPoint];
Ok I get it now. Here is my code. Compiles with no erros now. Thanks Ceniza and mandrav. :)
wxString MainFrame::TextToHTML()
{
const wxString sOO = "00";
const wxString s8O = "80";
const wxString sFF = "FF";
wxString htmlString;
htmlString << "<html><head><title></title></head><body><tt>";
long max = m_pTextCtrl->GetLastPosition();
wxTextAttr style, oldStyle;
wxChar ch;
for(long pos = 0; pos < max; pos++)
{
m_pTextCtrl->GetStyle(pos, style);
//if(style != oldStyle)
//{
if(pos > 0)
{
htmlString << "</font>";
}
wxString sR, sG, sB;
wxColour colour = style.GetTextColour();
sR.Printf("%02X", colour.Red());
sG.Printf("%02X", colour.Green());
sB.Printf("%02X", colour.Blue());
htmlString << "<font color = \"#" << sR << sG << sB << "\">"; ;
//}
if (m_pTextCtrl->GetInsertionPoint() == max)
{
return htmlString;
}
else
{
ch = m_pTextCtrl->GetValue()[static_cast<int>(pos)];
}
switch(ch)
{
case '\n':
{
htmlString << "\n<br>";
}
break;
case '<':
{
htmlString << "<";
}
break;
case '&':
{
htmlString << "&";
}
break;
case ' ':
{
htmlString << " ";
}
break;
default:
{
htmlString << ch;
}
break;
}
oldStyle = style;
}
htmlString << "</font></tt></body></html>";
return htmlString;
}
One more question. :)
Is there any way to compare wxTextAttr's? I need to check if one is different than the other. What is the best way of doing this?
See the commented out part. :)
Ah nevermind here is the code. :)
wxString MainFrame::TextToHTML()
{
wxString htmlString;
htmlString << "<html><head><title></title></head><body>";
long max = m_pTextCtrl->GetLastPosition();
wxTextAttr style, oldStyle;
wxChar ch;
for(long pos = 0; pos < max; pos++)
{
ch = m_pTextCtrl->GetValue()[static_cast<int>(pos)];
switch(ch)
{
case '\n':
{
htmlString << "<br />";
}
break;
case '<':
{
htmlString << "<";
}
break;
case '&':
{
htmlString << "&";
}
break;
case ' ':
{
htmlString << " ";
}
break;
default:
{
htmlString << ch;
}
break;
}
oldStyle = style;
}
htmlString << "</body></html>";
return htmlString;
}