#include "WidgetsRenderUtil.h" #include using namespace std; // »æÖÆÄÚÈÝÐÅÏ¢ struct TextDrawInfo { wxRect rect;//»æÖƵÄÇøÓò wxColour color;// »æÖƵÄÑÕÉ« wxString text;// ÄÚÈÝ }; // ÐÐÐÅÏ¢ struct LineInfo { int start_index; int end_index; }; int WidgetsRenderUtil::drawText(wxDC* dc, wxString st, wxPoint startP, int lineWidth, int lineHeight, std::list colors , wxColour defaultColor) { ///»æÖƿɾֲ¿±äÉ«£¬¿É»»Ðеĸ»Îı¾,·µ»Ø¸ß¶È /// ʵÏÖ˼·£º1.Ïȸù¾ÝÕûÌå¿Ø¼þ¿í¶È»òÕß»»Ðзû»»ÐУ¬È·¶¨ÐÐÊý /// 2.¸ù¾ÝÑÕÉ«µÄË÷Òý£¬È·¶¨»æÖÆÄÚÈݵÄÎı¾£¬ÑÕÉ«£¬Î»Öà /// 3.¸ù¾ÝµÚ2²½µÃµ½µÄÊý¾Ý£¬¼ÓÉÏÕû¸ö¿Ø¼þÆ«ÒÆÁ¿Í³Ò»»æÖÆÎı¾ÄÚÈÝ // ¼ÆËãÊÇ·ñÐèÒª»»ÐÐ wxString line_text; std::list lines; int start_index = -1; for (int i = 0; i < st.length(); i++) { line_text.Append(st.GetChar(i)); // ¼ÆËã»æÖÆÎı¾Õ¼Óõijߴç wxSize textSize = dc->GetTextExtent(line_text); if (start_index < 0 ) { start_index = i; } int nextCharWidth = 0; if (i + 1 < st.Length()) { // »ñÈ¡ÏÂÒ»¸ö×Ö·ûµÄ³¤¶È wxSize textSize1 = dc->GetTextExtent(st.GetChar(i+1)); nextCharWidth = textSize1.GetWidth(); } if (textSize.GetWidth() + nextCharWidth > lineWidth) { lines.push_back(LineInfo({ start_index, i })); start_index = -1; line_text = ""; } else if (st.GetChar(i) == '\n') { lines.push_back(LineInfo({ start_index, i })); start_index = -1; line_text = ""; } } if (start_index >= 0) { lines.push_back(LineInfo({ start_index, (int)st.length() - 1 })); start_index = -1; } map colorIndexMap; for (list::iterator e = colors.begin(); e != colors.end(); ++e) { colorIndexMap[(*e).start_index] = *e; colorIndexMap[(*e).end_index] = *e; } list drawInfoList; int row = 0; wxColor color = defaultColor; // ĬÈÏÑÕɫΪºÚÉ« for (list::iterator e = lines.begin(); e != lines.end(); ++e) { LineInfo lineInfo = *e; // ÄÚÈÝ¿ªÊ¼ int content_start_index = -1; int positionLeft = 0; for (int i = lineInfo.start_index; i <= lineInfo.end_index; i++) { if (content_start_index < 0) { content_start_index = i; } // »æÖÆ´¥·¢£ºÒ»ÐнáÊø£¬ÑÕÉ«¿ªÊ¼£¬ÑÕÉ«½áÊø if (colorIndexMap.find(i) != colorIndexMap.end()) { // ÓÐÑÕÉ«Ë÷Òý ColorIndexInfo info = colorIndexMap[i]; if (info.start_index == i) { // ÑÕÉ«¿ªÊ¼£¬½«Ö®Ç°µÄÊý¾Ý¼ÓÈë»æÖÆ if (i > content_start_index) { TextDrawInfo drawInfo; drawInfo.text = st.Mid(content_start_index, i - content_start_index); drawInfo.color = color; wxSize ts = dc->GetTextExtent(drawInfo.text); drawInfo.rect = wxRect(positionLeft, row * lineHeight, ts.GetWidth(), lineHeight); drawInfoList.push_back(drawInfo); // Æðʼµã content_start_index = i; positionLeft += ts.GetWidth(); } color = info.color; } if (info.end_index == i) { //ÑÕÉ«½áÊø TextDrawInfo drawInfo; drawInfo.text = st.Mid(content_start_index, i - content_start_index + 1); drawInfo.color = color; wxSize ts = dc->GetTextExtent(drawInfo.text); drawInfo.rect = wxRect(positionLeft, row * lineHeight, ts.GetWidth(), lineHeight); drawInfoList.push_back(drawInfo); // Æðʼµã content_start_index = -1; positionLeft += ts.GetWidth(); //»Ö¸´Ä¬ÈÏÑÕÉ« color = defaultColor; } } if (i == lineInfo.end_index) { // Ò»ÐнáÊø TextDrawInfo drawInfo; drawInfo.text = st.Mid(content_start_index, i - content_start_index + 1); drawInfo.color = color; wxSize ts = dc->GetTextExtent(drawInfo.text); drawInfo.rect = wxRect(positionLeft, row * lineHeight, ts.GetWidth(), lineHeight); drawInfoList.push_back(drawInfo); } } row++; } for (list::iterator e = drawInfoList.begin(); e != drawInfoList.end(); ++e) { TextDrawInfo drawInfo = *e; dc->SetTextForeground(drawInfo.color); wxRect rect = drawInfo.rect; rect.x += startP.x; rect.y += startP.y; dc->DrawLabel(drawInfo.text, rect, wxALIGN_LEFT | wxALIGN_TOP); } return lines.size() * lineHeight; } int WidgetsRenderUtil::drawText(wxGraphicsContext* gc, wxString st, wxPoint startP, int lineWidth, int lineHeight, std::list colors, wxColour defaultColor) { ///»æÖƿɾֲ¿±äÉ«£¬¿É»»Ðеĸ»Îı¾,·µ»Ø¸ß¶È /// ʵÏÖ˼·£º1.Ïȸù¾ÝÕûÌå¿Ø¼þ¿í¶È»òÕß»»Ðзû»»ÐУ¬È·¶¨ÐÐÊý /// 2.¸ù¾ÝÑÕÉ«µÄË÷Òý£¬È·¶¨»æÖÆÄÚÈݵÄÎı¾£¬ÑÕÉ«£¬Î»Öà /// 3.¸ù¾ÝµÚ2²½µÃµ½µÄÊý¾Ý£¬¼ÓÉÏÕû¸ö¿Ø¼þÆ«ÒÆÁ¿Í³Ò»»æÖÆÎı¾ÄÚÈÝ // ¼ÆËãÊÇ·ñÐèÒª»»ÐÐ wxString line_text; std::list lines; int start_index = -1; for (int i = 0; i < st.length(); i++) { line_text.Append(st.GetChar(i)); // ¼ÆËã»æÖÆÎı¾Õ¼Óõijߴç wxDouble width, height; gc->GetTextExtent(line_text, &width,&height); wxSize textSize(width, height); if (start_index < 0) { start_index = i; } int nextCharWidth = 0; if (i + 1 < st.Length()) { // »ñÈ¡ÏÂÒ»¸ö×Ö·ûµÄ³¤¶È wxDouble width, height; gc->GetTextExtent(st.GetChar(i + 1), &width, &height); wxSize textSize1(width, height); nextCharWidth = textSize1.GetWidth(); } if (textSize.GetWidth() + nextCharWidth > lineWidth) { lines.push_back(LineInfo({ start_index, i })); start_index = -1; line_text = ""; } else if (st.GetChar(i) == '\n') { lines.push_back(LineInfo({ start_index, i })); start_index = -1; line_text = ""; } } if (start_index >= 0) { lines.push_back(LineInfo({ start_index, (int)st.length() - 1 })); start_index = -1; } map colorIndexMap; for (list::iterator e = colors.begin(); e != colors.end(); ++e) { colorIndexMap[(*e).start_index] = *e; colorIndexMap[(*e).end_index] = *e; } list drawInfoList; int row = 0; wxColor color = defaultColor; // ĬÈÏÑÕɫΪºÚÉ« for (list::iterator e = lines.begin(); e != lines.end(); ++e) { LineInfo lineInfo = *e; // ÄÚÈÝ¿ªÊ¼ int content_start_index = -1; int positionLeft = 0; for (int i = lineInfo.start_index; i <= lineInfo.end_index; i++) { if (content_start_index < 0) { content_start_index = i; } // »æÖÆ´¥·¢£ºÒ»ÐнáÊø£¬ÑÕÉ«¿ªÊ¼£¬ÑÕÉ«½áÊø if (colorIndexMap.find(i) != colorIndexMap.end()) { // ÓÐÑÕÉ«Ë÷Òý ColorIndexInfo info = colorIndexMap[i]; if (info.start_index == i) { // ÑÕÉ«¿ªÊ¼£¬½«Ö®Ç°µÄÊý¾Ý¼ÓÈë»æÖÆ if (i > content_start_index) { TextDrawInfo drawInfo; drawInfo.text = st.Mid(content_start_index, i - content_start_index); drawInfo.color = color; wxDouble width, height; gc->GetTextExtent(drawInfo.text, &width, &height); wxSize ts(width, height); drawInfo.rect = wxRect(positionLeft, row * lineHeight, ts.GetWidth(), lineHeight); drawInfoList.push_back(drawInfo); // Æðʼµã content_start_index = i; positionLeft += ts.GetWidth(); } color = info.color; } if (info.end_index == i) { //ÑÕÉ«½áÊø TextDrawInfo drawInfo; drawInfo.text = st.Mid(content_start_index, i - content_start_index + 1); drawInfo.color = color; wxDouble width, height; gc->GetTextExtent(drawInfo.text, &width, &height); wxSize ts(width, height); drawInfo.rect = wxRect(positionLeft, row * lineHeight, ts.GetWidth(), lineHeight); drawInfoList.push_back(drawInfo); // Æðʼµã content_start_index = -1; positionLeft += ts.GetWidth(); //»Ö¸´Ä¬ÈÏÑÕÉ« color = defaultColor; } } if (i == lineInfo.end_index) { // Ò»ÐнáÊø TextDrawInfo drawInfo; drawInfo.text = st.Mid(content_start_index, i - content_start_index + 1); drawInfo.color = color; wxDouble width, height; gc->GetTextExtent(drawInfo.text, &width, &height); wxSize ts(width, height); drawInfo.rect = wxRect(positionLeft, row * lineHeight, ts.GetWidth(), lineHeight); drawInfoList.push_back(drawInfo); } } row++; } for (list::iterator e = drawInfoList.begin(); e != drawInfoList.end(); ++e) { TextDrawInfo drawInfo = *e; gc->SetPen(drawInfo.color); wxRect rect = drawInfo.rect; rect.x += startP.x; rect.y += startP.y; gc->DrawText(drawInfo.text, rect.x, rect.y); } return lines.size() * lineHeight; return 0; } void WidgetsRenderUtil::drawBtn(wxDC* dc, wxString st, wxRect rect, wxColour textColor, wxColour bgColor, wxAlignment align) { // »æÖư´Å¥ dc->SetPen(bgColor); dc->SetBrush(bgColor); dc->SetTextForeground(textColor); dc->DrawRectangle(rect); // »æÖư´Å¥µÄÎı¾ dc->DrawLabel(st, rect, align); } void WidgetsRenderUtil::drawBtnByGC(wxGraphicsContext* gc, wxString st, wxRect rect, wxColour textColor, wxColour bgColor) { // »æÖư´Å¥ gc->SetPen(bgColor); gc->SetBrush(wxBrush(bgColor)); gc->DrawRectangle(rect.x,rect.y,rect.width,rect.height); // »æÖư´Å¥µÄÎı¾ wxDouble width, height; gc->GetTextExtent(st, &width, &height); gc->SetPen(textColor); gc->SetBrush(wxBrush(textColor)); gc->DrawText(st, rect.x + (rect.width - width)/2, rect.y + (rect.height - height) / 2); }