کدنویسی هایلایتینگ در سی++
Created on: February 4, 2025
Answered using O3 Mini High by Chat01
Created on: February 4, 2025
Answered using O3 Mini High by Chat01
#include <SDL2/SDL.h> #include <SDL2/SDL_ttf.h> #include <string> #include <iostream> #include <vector> #include <regex> #include <algorithm>
using namespace std; // طبق درخواست شما
// ابعاد پنجرهی اصلی const int SCREEN_WIDTH = 1260; const int SCREEN_HEIGHT = 800;
/*
/*
ساختار رنگها در تم تاریک و روشن:
هریک شامل رنگ برای کیوردرها، انواع دادهای، تابع، متغیر، استرینگ،
کاراکتر، عدد، کامنت، پیشپردازنده، عملگرها، براکتها، و متن عادی است. */ struct ThemeColors { SDL_Color keyword; SDL_Color types; SDL_Color functionName; SDL_Color variables; SDL_Color stringLiteral; SDL_Color charLiteral; SDL_Color number; SDL_Color comment; SDL_Color preprocessor; SDL_Color operators; SDL_Color brackets; SDL_Color normalText;
// تم تاریک static ThemeColors Dark() { ThemeColors tc; tc.keyword = {198, 120, 221, 255}; tc.types = {224, 108, 117, 255}; tc.functionName = { 97, 175, 254, 255}; tc.variables = {229, 192, 123, 255}; tc.stringLiteral = {152, 195, 121, 255}; tc.charLiteral = {152, 195, 121, 255}; tc.number = {209, 154, 102, 255}; tc.comment = { 92, 99, 112, 255}; tc.preprocessor = { 86, 182, 194, 255}; tc.operators = {213, 94, 0, 255}; tc.brackets = {171, 178, 191, 255}; tc.normalText = {255, 255, 255, 255}; return tc; }
// تم روشن static ThemeColors Light() { ThemeColors tc; tc.keyword = { 0, 51, 102, 255}; tc.types = { 0, 128, 128, 255}; tc.functionName = {255, 140, 0, 255}; tc.variables = {139, 0, 0, 255}; tc.stringLiteral = { 0, 100, 0, 255}; tc.charLiteral = { 0, 100, 0, 255}; tc.number = {128, 0, 128, 255}; tc.comment = {128, 128, 128, 255}; tc.preprocessor = { 0, 139, 139, 255}; tc.operators = {128, 0, 0, 255}; tc.brackets = {184, 134, 11, 255}; tc.normalText = { 0, 0, 0, 255}; return tc; } };
/*
/*
/*
/*
تابع اصلی هایلایتکردن یک خط:
با regex های مختلف روی خط جستجو میکنیم و در صورت Match، رنگ مناسب را اعمال میکنیم.
این تابع یک وکتور از HighlightSegment برمیگرداند. */ vector<HighlightSegment> highlightLine(const string& line, const ThemeColors& tc) { // ساختار داخلی: نگهداری نقطه شروع/پایان match و رنگ آن struct MatchInfo { size_t start; size_t end; SDL_Color color; }; vector<MatchInfo> matches;
// ********** تعریف الگوهای مختلف **********
// پرانتزها و براکتها: شامل {}, (), [] // (این را جلوتر قرار میدهیم تا با موارد دیگر قاطی نشود) regex bracketRegex("([$$$$$${}])", std::regex_constants::ECMAScript);
// کامنت تکخطی regex commentRegex("//.*", regex_constants::ECMAScript);
// استرینگ regex stringRegex(""([^"\\]|\\.)*"", regex_constants::ECMAScript);
// کاراکتر regex charRegex("'([^'\\]|\\.)'", regex_constants::ECMAScript);
// عدد regex numberRegex("\b\d+(\.\d+)?\b", regex_constants::ECMAScript);
// کلیدواژهها: class, if, while, else regex keywordRegex("\b(class|if|while|else)\b", regex_constants::ECMAScript);
// انواع دادهای: اضافهکردن fstream، و غیره regex typesRegex("\b(long|int|float|double)\b", regex_constants::ECMAScript);
// دستورات پیشپردازنده: #include regex preprocRegex("#include", regex_constants::ECMAScript);
// عملگرها: +, -, , / regex operatorRegex("([+]|-|\|/)", regex_constants::ECMAScript);
/*
int main()
یا void foo()
جلوگیری کنیمint
آمده در عمل از طریق این الگو رد میشود (منفینگر)./*
^^^ ^^^^
// ********** توابع کمکی برای جستجو و ثبت match **********
/*
/*
// ********** فراخوانی جستجوها **********
// ابتدا پرانتز/براکت را رنگ کنیم findMatches(bracketRegex, tc.brackets); // بعد کامنت findMatches(commentRegex, tc.comment); // رشته و کاراکتر findMatches(stringRegex, tc.stringLiteral); findMatches(charRegex, tc.charLiteral); // عدد findMatches(numberRegex, tc.number); // کیوردرها findMatches(keywordRegex, tc.keyword); // انواع دادهای findMatches(typesRegex, tc.types); // پیشپردازنده findMatches(preprocRegex, tc.preprocessor); // عملگرها findMatches(operatorRegex, tc.operators); // تابع findFunctionMatches(functionRegex, tc.functionName); // متغیر findMatches(identifierRegex, tc.variables);
// ********** مرتبسازی نتیجه بر اساس position ********** sort(matches.begin(), matches.end(), [](auto &a, auto &b){ return a.start < b.start; } );
// ********** تبدیل MatchInfoها به وکتور از HighlightSegment ********** vector<HighlightSegment> segments; size_t currentIndex = 0;
for (auto &m : matches) { if (m.start < currentIndex) continue; if (m.start > currentIndex) { segments.push_back({ line.substr(currentIndex, m.start - currentIndex), tc.normalText }); } segments.push_back({ line.substr(m.start, m.end - m.start), m.color }); currentIndex = m.end; } if (currentIndex < line.size()) { segments.push_back({ line.substr(currentIndex), tc.normalText }); }
return segments; }
/*
/*
تابع کمکی: اطمینان از مشاهده مکاننما به صورت افقی (اسکرول افقی) / void ensureCursorVisibleHorizontally(const string &line, int cursorPos, int &scrollOffsetX, TTF_Font font, int screenWidth, int startX) { string beforeCursor = line.substr(0, cursorPos); int w = 0; TTF_SizeText(font, beforeCursor.c_str(), &w, nullptr);
int cursorX = startX + w - scrollOffsetX; if (cursorX < 0) { scrollOffsetX = startX + w; } else if (cursorX > screenWidth - 50) { scrollOffsetX = (startX + w) - (screenWidth - 50); } if (scrollOffsetX < 0) scrollOffsetX = 0; }
/*
/*
/*
تابع اصلی برای رسم محتوای ادیتور در پنجره:
SDL_RenderClear(renderer);
const int LINE_HEIGHT = TTF_FontHeight(font);
// رسم باکس شمارهخطها در سمت چپ const int lineNumberWidth = 50; SDL_Rect lineNumberRect = { 0, 0, lineNumberWidth, SCREEN_HEIGHT }; if (darkTheme) SDL_SetRenderDrawColor(renderer, 50, 50, 50, 255); else SDL_SetRenderDrawColor(renderer, 230, 230, 230, 255); SDL_RenderFillRect(renderer, &lineNumberRect);
// نرمالسازی محدوده انتخاب int selMinLine, selMinCol, selMaxLine, selMaxCol; normalizeSelection(selRange, selMinLine, selMinCol, selMaxLine, selMaxCol);
// شروع رسم متن از این مختصات (پس از باکس شمارهخط) int startTextX = lineNumberWidth + 10; int y = -scrollOffsetY;
// پیمایش روی خطوط for (int i = 0; i < (int)lines.size(); i++) { int lineY = y; // فقط اگر این خط داخل محدوده دید عمودی باشد رسمش کن if (lineY + LINE_HEIGHT >= 0 && lineY < SCREEN_HEIGHT) { // ********** رسم شماره خط ********** SDL_Color lineNumColor = darkTheme ? SDL_Color{200,200,200,255} : SDL_Color{50,50,50,255}; string lineNumStr = to_string(i+1); SDL_Surface* numSurf = TTF_RenderText_Blended(font, lineNumStr.c_str(), lineNumColor); SDL_Texture* numTex = SDL_CreateTextureFromSurface(renderer, numSurf); int nW = numSurf->w; int nH = numSurf->h; SDL_Rect numRect = { lineNumberWidth - nW - 5, lineY, nW, nH }; SDL_RenderCopy(renderer, numTex, NULL, &numRect); SDL_FreeSurface(numSurf); SDL_DestroyTexture(numTex);
// ********** هایلایت سینتکس این خط **********
auto segments = highlightLine(lines[i], theme);
int x = startTextX;
for (auto &seg : segments) {
SDL_Surface* textSurface = TTF_RenderText_Blended(font, seg.text.c_str(), seg.color);
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
int textWidth = textSurface->w;
int textHeight = textSurface->h;
// آیا بخشی از این سگمنت در محدوده انتخاب است؟
bool inSelection = false;
if (selRange.active && i >= selMinLine && i <= selMaxLine) {
int lineSelectedMinCol = (i == selMinLine) ? selMinCol : 0;
int lineSelectedMaxCol = (i == selMaxLine )? selMaxCol : (int)lines[i].size();
if (lineSelectedMaxCol > lineSelectedMinCol && !seg.text.empty()) {
// برای سادگی در این پیادهسازی کوتاه:
// اگر کل خط در محدوده است، این سگمنت را هم انتخاب شده میگیریم
inSelection = true;
}
}
// اگر در محدوده انتخاب بود، پسزمینه آبی بکش
if (inSelection) {
drawSelectionBackground(renderer, x - scrollOffsetX, lineY, textWidth, LINE_HEIGHT);
}
// حالا خود متن سگمنت
SDL_Rect rect = {
x - scrollOffsetX,
lineY,
textWidth,
textHeight
};
if (rect.x + rect.w >= 0 && rect.x < SCREEN_WIDTH) {
if (inSelection) {
// داخل انتخاب، متن سفید رسم میشود
SDL_SetTextureColorMod(textTexture, 255, 255, 255);
}
SDL_RenderCopy(renderer, textTexture, NULL, &rect);
}
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(textTexture);
x += textWidth;
}
// ********** مکاننمای چشمکزن **********
if (i == currentLine && cursorVisible) {
int cursorX = startTextX;
if (cursorPos > 0) {
string beforeCursor = lines[i].substr(0, cursorPos);
cursorX += getTextWidth(font, beforeCursor);
}
cursorX -= scrollOffsetX;
SDL_SetRenderDrawColor(renderer, 200, 200, 200, 255);
SDL_RenderDrawLine(renderer, cursorX, lineY, cursorX, lineY + LINE_HEIGHT);
}
}
y += LINE_HEIGHT;
}
SDL_RenderPresent(renderer); }
/*
پنجرهٔ مجزای Ctrl+F (GoToLineDialog):
bool done = false; bool result = false; string lineInput = ""; // متن واردشده توسط کاربر
// رنگها برای پسزمینه، تکست و دکمهها SDL_Color bgColor = darkTheme ? SDL_Color{60, 60, 60, 255} : SDL_Color{200, 200, 200, 255}; SDL_Color textColor = darkTheme ? SDL_Color{255, 255, 255,255} : SDL_Color{0, 0, 0, 255}; SDL_Color boxColor = {255, 255, 255, 255}; SDL_Color cancelCol = {180, 0, 0, 255}; SDL_Color searchCol = {0, 180, 0, 255}; SDL_Color whiteCol = {255, 255, 255, 255};
SDL_StartTextInput();
while(!done) { // پردازش رویدادهای پنجرهٔ جدید SDL_Event ev; while(SDL_PollEvent(&ev)) { // اگر کاربر پنجرهٔ کوچیک را بست if(ev.type == SDL_QUIT) { done = true; } // کلیک ماوس روی دکمهها else if(ev.type == SDL_MOUSEBUTTONDOWN) { int mx = ev.button.x; int my = ev.button.y; // دکمه Cancel SDL_Rect cancelBtn = {50, 100, 80, 30}; // دکمه Search SDL_Rect searchBtn = { w - 130, 100, 80, 30 };
// اگر رو cancel کلیک شد
if (mx >= cancelBtn.x && mx <= cancelBtn.x + cancelBtn.w &&
my >= cancelBtn.y && my <= cancelBtn.y + cancelBtn.h)
{
done = true;
}
// اگر رو search کلیک شد
else if (mx >= searchBtn.x && mx <= searchBtn.x + searchBtn.w &&
my >= searchBtn.y && my <= searchBtn.y + searchBtn.h)
{
// تلاش برای تبدیل lineInput به عدد
try {
int val = stoi(lineInput);
targetLine = val - 1; // چون شمارهخطها از صفر شروع میشود
result = true;
} catch(...) {
// اگر تبدیل نشد، کاری نمیکنیم (result=false)
}
done = true;
}
}
else if(ev.type == SDL_KEYDOWN) {
// ESC = لغو
if(ev.key.keysym.sym == SDLK_ESCAPE) {
done = true;
}
// Enter = Search
else if(ev.key.keysym.sym == SDLK_RETURN) {
try {
int val = stoi(lineInput);
targetLine = val - 1;
result = true;
} catch(...) {
// درصورت ورودی نامعتبر، کاری نمیکنیم
}
done = true;
}
// Backspace = حذف کاراکتر آخر
else if(ev.key.keysym.sym == SDLK_BACKSPACE) {
if (!lineInput.empty()) {
lineInput.pop_back();
}
}
}
// تایپ کاربر
else if(ev.type == SDL_TEXTINPUT) {
lineInput += ev.text.text;
}
}
// ********** رسم پنجره **********
// پسزمینه
SDL_SetRenderDrawColor(gotoRenderer, bgColor.r, bgColor.g, bgColor.b, 255);
SDL_RenderClear(gotoRenderer);
// باکس سفید برای ورودی کاربر
SDL_Rect inputRect = {20, 50, w - 40, 30};
SDL_SetRenderDrawColor(gotoRenderer, boxColor.r, boxColor.g, boxColor.b, 255);
SDL_RenderFillRect(gotoRenderer, &inputRect);
// متن واردشده
SDL_Surface* surfInput = TTF_RenderText_Blended(font, lineInput.c_str(), textColor);
SDL_Texture* texInput = SDL_CreateTextureFromSurface(gotoRenderer, surfInput);
SDL_Rect texRect = { inputRect.x+5, inputRect.y+5, surfInput->w, surfInput->h };
SDL_RenderCopy(gotoRenderer, texInput, NULL, &texRect);
SDL_FreeSurface(surfInput);
SDL_DestroyTexture(texInput);
// دکمه Cancel
SDL_Rect cancelBtn = {50, 100, 80, 30};
SDL_SetRenderDrawColor(gotoRenderer, cancelCol.r, cancelCol.g, cancelCol.b, cancelCol.a);
SDL_RenderFillRect(gotoRenderer, &cancelBtn);
SDL_Surface* surfCancel = TTF_RenderText_Blended(font, "Cancel", whiteCol);
SDL_Texture* texCancel = SDL_CreateTextureFromSurface(gotoRenderer, surfCancel);
SDL_Rect canRect = { cancelBtn.x+10, cancelBtn.y+5, surfCancel->w, surfCancel->h };
SDL_RenderCopy(gotoRenderer, texCancel, NULL, &canRect);
SDL_FreeSurface(surfCancel);
SDL_DestroyTexture(texCancel);
// دکمه Search
SDL_Rect searchBtn = { w - 130, 100, 80, 30 };
SDL_SetRenderDrawColor(gotoRenderer, searchCol.r, searchCol.g, searchCol.b, searchCol.a);
SDL_RenderFillRect(gotoRenderer, &searchBtn);
SDL_Surface* surfSearch = TTF_RenderText_Blended(font, "Search", whiteCol);
SDL_Texture* texSearch = SDL_CreateTextureFromSurface(gotoRenderer, surfSearch);
SDL_Rect seaRect = { searchBtn.x+10, searchBtn.y+5, surfSearch->w, surfSearch->h };
SDL_RenderCopy(gotoRenderer, texSearch, NULL, &seaRect);
SDL_FreeSurface(surfSearch);
SDL_DestroyTexture(texSearch);
// آپدیت صفحهی کوچک
SDL_RenderPresent(gotoRenderer);
SDL_Delay(10);
}
SDL_StopTextInput(); SDL_DestroyRenderer(gotoRenderer); SDL_DestroyWindow(gotoWindow);
return result; }
int main(int argc, char* argv[]) { bool darkTheme = true; // پیشفرض تم تاریک ThemeColors theme = darkTheme ? ThemeColors::Dark() : ThemeColors::Light();
// مقداردهی اولیه SDL و TTF
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
cerr << "SDL could not initialize! Error: " << SDL_GetError() << endl;
return -1;
}
if (TTF_Init() == -1) {
cerr << "TTF could not initialize! Error: " << TTF_GetError() << endl;
SDL_Quit();
return -1;
}
// ایجاد پنجره اصلی
SDL_Window* window = SDL_CreateWindow("SDL Text Editor",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (!window) {
cerr << "Window could not be created! Error: " << SDL_GetError() << endl;
TTF_Quit();
SDL_Quit();
return -1;
}
// ایجاد رندرر
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer) {
cerr << "Renderer could not be created! Error: " << SDL_GetError() << endl;
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return -1;
}
// بارگذاری فونت (در ویندوز معمولاً consola.ttf موجود است)
TTF_Font* font = TTF_OpenFont(R"(C:\Windows\Fonts\consola.ttf)", 20);
if (!font) {
cerr << "Failed to load font! Error: " << TTF_GetError() << endl;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return -1;
}
// وکتور خطوط متن
vector<string> lines = {""};
int currentLine = 0;
int cursorPos = 0;
int scrollOffsetY = 0;
int scrollOffsetX = 0;
// پارامترهای کمکی
const int LINE_HEIGHT = TTF_FontHeight(font);
Uint32 lastCursorToggle = SDL_GetTicks();
bool cursorVisible = true; // برای چشمکزدن مکاننما
const Uint32 CURSOR_BLINK_INTERVAL = 500;
// ساختار انتخاب متن
SelectionRange selection;
bool extendedSelectMode = false; // با Ctrl+G
bool shifting = false; // آیا شیفت گرفته شده؟
bool mouseSelecting = false; // انتخاب با درگ ماوس
bool quit = false;
SDL_Event e;
SDL_StartTextInput();
// رسم اولیه
renderAll(renderer, font, theme, lines,
currentLine, cursorPos,
scrollOffsetY, scrollOffsetX,
darkTheme, cursorVisible,
selection);
// حلقه اصلی رویدادها
while (!quit) {
Uint32 currentTime = SDL_GetTicks();
// چشمکزدن مکاننما
if (currentTime - lastCursorToggle >= CURSOR_BLINK_INTERVAL) {
cursorVisible = !cursorVisible;
lastCursorToggle = currentTime;
}
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
quit = true;
}
else if (e.type == SDL_MOUSEWHEEL) {
// اسکرول عمودی یا افقی (بسته به شیفت)
if (SDL_GetModState() & KMOD_SHIFT) {
if (e.wheel.y > 0) {
scrollOffsetX = max(scrollOffsetX - 40, 0);
} else if (e.wheel.y < 0) {
scrollOffsetX += 40;
}
} else {
if (e.wheel.y > 0) {
scrollOffsetY = max(0, scrollOffsetY - LINE_HEIGHT);
} else if (e.wheel.y < 0) {
scrollOffsetY += LINE_HEIGHT;
}
}
}
else if (e.type == SDL_KEYDOWN) {
// شرتکاتها
if ((SDL_GetModState() & KMOD_CTRL) && e.key.keysym.sym == SDLK_v) {
// پیست از کلیپبورد
char* clipText = SDL_GetClipboardText();
if (clipText) {
string pasteStr = clipText;
SDL_free(clipText);
// حذف \r برای جلوگیری از بههمریختگی چندخطی
size_t pos;
while ((pos = pasteStr.find('\r')) != string::npos) {
pasteStr.erase(pos, 1);
}
// اگر انتخابی فعال است اول پاکش کنیم
if (selection.active) {
int minL, minC, maxL, maxC;
normalizeSelection(selection, minL, minC, maxL, maxC);
if (minL == maxL) {
lines[minL].erase(minC, maxC - minC);
currentLine = minL;
cursorPos = minC;
} else {
lines[minL].erase(minC);
lines[maxL].erase(0, maxC);
for (int ll = maxL-1; ll > minL; ll--) {
lines.erase(lines.begin()+ll);
}
currentLine = minL;
cursorPos = minC;
}
selection.active = false;
}
// حالا چندخطی را پیست میکنیم
vector<string> clipLines = splitByNewline(pasteStr);
if (!clipLines.empty()) {
lines[currentLine].insert(cursorPos, clipLines[0]);
cursorPos += (int)clipLines[0].size();
for (int i = 1; i < (int)clipLines.size(); i++) {
string tail = lines[currentLine].substr(cursorPos);
lines[currentLine] = lines[currentLine].substr(0, cursorPos);
lines.insert(lines.begin() + currentLine + 1, tail);
currentLine++;
cursorPos = 0;
lines[currentLine].insert(cursorPos, clipLines[i]);
cursorPos += (int)clipLines[i].size();
}
}
ensureLastLineVisible(currentLine, scrollOffsetY, SCREEN_HEIGHT, LINE_HEIGHT, (int)lines.size());
ensureCursorVisibleHorizontally(lines[currentLine], cursorPos, scrollOffsetX, font, SCREEN_WIDTH, 60);
}
}
else if ((SDL_GetModState() & KMOD_CTRL) && e.key.keysym.sym == SDLK_a) {
// Ctrl+A = انتخاب همه
if (!lines.empty()) {
selection.active = true;
selection.startLine = 0;
selection.startCol = 0;
selection.endLine = (int)lines.size()-1;
selection.endCol = (int)lines[selection.endLine].size();
}
}
else if ((SDL_GetModState() & KMOD_CTRL) && e.key.keysym.sym == SDLK_g) {
// جابهجایی حالت انتخاب توسعهیافته
extendedSelectMode = !extendedSelectMode;
}
else if ((SDL_GetModState() & KMOD_CTRL) && e.key.keysym.sym == SDLK_f) {
// پنجرهٔ GoToLine
int gotoLine = currentLine + 1; // پیشفرض
bool ok = showGoToLineDialog(window, darkTheme, font, gotoLine);
if (ok) {
if (gotoLine < 0) gotoLine = 0;
int maxLine = (int)lines.size()-1;
if (gotoLine > maxLine) gotoLine = maxLine;
currentLine = gotoLine;
if (currentLine < 0) currentLine = 0;
if (currentLine >= (int)lines.size()) currentLine = (int)lines.size()-1;
cursorPos = (int)lines[currentLine].size();
ensureLastLineVisible(currentLine, scrollOffsetY, SCREEN_HEIGHT, LINE_HEIGHT, (int)lines.size());
ensureCursorVisibleHorizontally(lines[currentLine], cursorPos, scrollOffsetX, font, SCREEN_WIDTH, 60);
}
}
else {
// سایر کلیدها
switch (e.key.keysym.sym) {
case SDLK_ESCAPE:
quit = true;
break;
case SDLK_BACKSPACE:
// پاککردن کاراکتر قبل از مکاننما
if (cursorPos > 0 && cursorPos <= (int)lines[currentLine].size()) {
lines[currentLine].erase(cursorPos - 1, 1);
cursorPos--;
}
else if (cursorPos == 0 && currentLine > 0) {
int oldSize = (int)lines[currentLine - 1].size();
lines[currentLine - 1] += lines[currentLine];
lines.erase(lines.begin() + currentLine);
currentLine--;
cursorPos = oldSize;
}
break;
case SDLK_RETURN:
{
// شکست خط (Enter)
string tail;
if (cursorPos < (int)lines[currentLine].size()) {
tail = lines[currentLine].substr(cursorPos);
lines[currentLine] = lines[currentLine].substr(0, cursorPos);
}
lines.insert(lines.begin() + currentLine + 1, tail);
currentLine++;
cursorPos = 0;
ensureLastLineVisible(currentLine, scrollOffsetY, SCREEN_HEIGHT, LINE_HEIGHT, (int)lines.size());
ensureCursorVisibleHorizontally(lines[currentLine], cursorPos, scrollOffsetX, font, SCREEN_WIDTH, 60);
break;
}
case SDLK_TAB:
// درج چهار فاصله با Tab
lines[currentLine].insert(cursorPos, " ");
cursorPos += 4;
break;
case SDLK_LEFT:
if (cursorPos > 0) {
cursorPos--;
} else if (currentLine > 0) {
currentLine--;
cursorPos = (int)lines[currentLine].size();
ensureLastLineVisible(currentLine, scrollOffsetY, SCREEN_HEIGHT, LINE_HEIGHT, (int)lines.size());
}
ensureCursorVisibleHorizontally(lines[currentLine], cursorPos, scrollOffsetX, font, SCREEN_WIDTH, 60);
if (extendedSelectMode && shifting) {
selection.active = true;
selection.endLine = currentLine;
selection.endCol = cursorPos;
}
break;
case SDLK_RIGHT:
if (cursorPos < (int)lines[currentLine].size()) {
cursorPos++;
} else if (currentLine < (int)lines.size() - 1) {
currentLine++;
cursorPos = 0;
ensureLastLineVisible(currentLine, scrollOffsetY, SCREEN_HEIGHT, LINE_HEIGHT, (int)lines.size());
}
ensureCursorVisibleHorizontally(lines[currentLine], cursorPos, scrollOffsetX, font, SCREEN_WIDTH, 60);
if (extendedSelectMode && shifting) {
selection.active = true;
selection.endLine = currentLine;
selection.endCol = cursorPos;
}
break;
case SDLK_UP:
if (currentLine > 0) {
currentLine--;
cursorPos = min(cursorPos, (int)lines[currentLine].size());
ensureLastLineVisible(currentLine, scrollOffsetY, SCREEN_HEIGHT, LINE_HEIGHT, (int)lines.size());
ensureCursorVisibleHorizontally(lines[currentLine], cursorPos, scrollOffsetX, font, SCREEN_WIDTH, 60);
}
if (extendedSelectMode && shifting) {
selection.active = true;
selection.endLine = currentLine;
selection.endCol = cursorPos;
}
break;
case SDLK_DOWN:
if (currentLine < (int)lines.size() - 1) {
currentLine++;
cursorPos = min(cursorPos, (int)lines[currentLine].size());
ensureLastLineVisible(currentLine, scrollOffsetY, SCREEN_HEIGHT, LINE_HEIGHT, (int)lines.size());
ensureCursorVisibleHorizontally(lines[currentLine], cursorPos, scrollOffsetX, font, SCREEN_WIDTH, 60);
}
if (extendedSelectMode && shifting) {
selection.active = true;
selection.endLine = currentLine;
selection.endCol = cursorPos;
}
break;
case SDLK_LSHIFT:
case SDLK_RSHIFT:
// اگر extendedSelectMode فعال است، با شيفت خط پایان سلکشن تنظیم میشود
shifting = true;
if (!selection.active && extendedSelectMode) {
selection.active = true;
selection.startLine = currentLine;
selection.startCol = cursorPos;
selection.endLine = currentLine;
selection.endCol = cursorPos;
}
break;
case SDLK_q:
// تغییر تم با Q
darkTheme = !darkTheme;
theme = darkTheme ? ThemeColors::Dark() : ThemeColors::Light();
break;
default:
break;
}
}
}
else if (e.type == SDL_KEYUP) {
// اگر شيفت رها شد
if (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT) {
shifting = false;
}
}
else if (e.type == SDL_TEXTINPUT) {
if (e.text.text) {
// اگر بخشی انتخاب شده، پاکش کنیم (مثل ویندوز)
if (selection.active) {
int minL, minC, maxL, maxC;
normalizeSelection(selection, minL, minC, maxL, maxC);
if (minL == maxL) {
lines[minL].erase(minC, maxC - minC);
currentLine = minL;
cursorPos = minC;
} else {
lines[minL].erase(minC);
lines[maxL].erase(0, maxC);
for (int ll = maxL-1; ll > minL; ll--) {
lines.erase(lines.begin()+ll);
}
currentLine = minL;
cursorPos = minC;
}
selection.active = false;
}
// درج کاراکتر تایپشده
lines[currentLine].insert(cursorPos, e.text.text);
cursorPos += (int)strlen(e.text.text);
ensureLastLineVisible(currentLine, scrollOffsetY, SCREEN_HEIGHT, LINE_HEIGHT, (int)lines.size());
ensureCursorVisibleHorizontally(lines[currentLine], cursorPos, scrollOffsetX, font, SCREEN_WIDTH, 60);
}
}
else if (e.type == SDL_MOUSEBUTTONDOWN) {
if (e.button.button == SDL_BUTTON_LEFT) {
// شروع انتخاب با ماوس
mouseSelecting = true;
selection.active = true;
int clickY = e.button.y + scrollOffsetY;
int lineIndex = clickY / LINE_HEIGHT;
if (lineIndex < 0) lineIndex = 0;
if (lineIndex >= (int)lines.size()) lineIndex = (int)lines.size()-1;
int clickX = e.button.x;
// متن از X=60 شروع میشود (باکس خطوط 50 + فاصله 10)
int textX = clickX - 60 + scrollOffsetX;
if (textX < 0) textX = 0;
const string &ln = lines[lineIndex];
int accum = 0;
int col = 0;
for (int c = 0; c < (int)ln.size(); c++) {
int w=0,h=0;
string one(1, ln[c]);
TTF_SizeText(font, one.c_str(), &w, &h);
if (accum + w > textX) {
break;
}
accum += w;
col++;
}
selection.startLine = lineIndex;
selection.startCol = col;
selection.endLine = lineIndex;
selection.endCol = col;
currentLine = lineIndex;
cursorPos = col;
}
}
else if (e.type == SDL_MOUSEMOTION) {
// ادامه درگ با ماوس
if (mouseSelecting) {
int mx = e.motion.x;
int my = e.motion.y + scrollOffsetY;
int lineIndex = my / LINE_HEIGHT;
if (lineIndex < 0) lineIndex = 0;
if (lineIndex >= (int)lines.size()) lineIndex = (int)lines.size()-1;
int textX = mx - 60 + scrollOffsetX;
if (textX < 0) textX = 0;
const string &ln = lines[lineIndex];
int accum = 0;
int col = 0;
for (int c = 0; c < (int)ln.size(); c++) {
int w=0,h=0;
string one(1, ln[c]);
TTF_SizeText(font, one.c_str(), &w, &h);
if (accum + w > textX) {
break;
}
accum += w;
col++;
}
selection.endLine = lineIndex;
selection.endCol = col;
currentLine = lineIndex;
cursorPos = col;
}
}
else if (e.type == SDL_MOUSEBUTTONUP) {
if (e.button.button == SDL_BUTTON_LEFT) {
mouseSelecting = false;
}
}
}
// رندر کل صفحه در هر فریم
renderAll(renderer, font, theme, lines,
currentLine, cursorPos,
scrollOffsetY, scrollOffsetX,
darkTheme, cursorVisible,
selection);
}
// پایان کار
SDL_StopTextInput();
TTF_CloseFont(font);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 0;
} تمام توابع مربوط به سیتکس هایلایتینگ رو حدا کن و بفرست برام و بیا یه اصلاح تو قسمت متغیر ها انحام بده به تمام حروفی که بعد از int float double fstream string vector auto array بود متغیر شناحته شه بعدرنگ بشه ئلی حواست باشه توابع رو رنگ نکنی
Highlighting functions
I am tasked with identifying functions responsible for syntax highlighting, particularly those including highlightLine
and related helper functions. However, functions should remain uncolored.
Enhancing regex patterns
I’m refining the regex for variable identification, ensuring type names like int, float, double, etc., are flagged as variables and colored, while keeping functions uncolored.
Narrowing regex
I’m refining the regex to pinpoint variables only following the data types: int, float, double, fstream, string, vector, auto, array. Functions and their calls are deliberately excluded.
Refining regex details
I’m working through the negative lookahead to exclude function calls from being flagged. Considering removing "char", "bool", "class", "regex" from the list, as the user didn’t specify them.
Tweaking regex for variables
I’m updating the regex to capture variables declared after specific types like int, float, double, fstream, string, vector, auto, and array, while avoiding function names.
Tweaking regex
I’m enhancing the regex to capture variables after types like int, float, double, fstream, string, vector, auto, and array, while avoiding functions, by tweaking the negative lookahead.
Refining regex regex
OK, let’s adjust the regex to capture variable names after types like int, float, double, fstream, string, vector, auto, and array, while excluding function calls.
Refining regex
I'm improving the regex for capturing variable names after specified types, ensuring it doesn't match function calls. This involves tweaking the negative lookahead for precise identification.
Clarifying the request
I’m piecing together the user's request for syntax highlighting. The task involves highlighting variables after specified types without coloring functions, indicating the need to refine regex, particularly the negative lookahead.
Compiling syntax functions
I'm gathering clarity on syntax highlighting. HighlightLine and its helper functions will be included, with a newly refined regex for identifying variables after specified types, ensuring functions remain uncolored.
Adjusting regex
I’m refining the regex to ensure variable names after types like int, float, double, fstream, string, vector, auto, and array are identified correctly, excluding function names.
Ensuring clarity
I'm thinking through avoiding conflicts. Variable names in function definitions like "int foo()" shouldn't be recolored. Adjusting the regex sequence helps keep variable and type highlighting distinct.
Ensuring specificity
I’m focusing on the negative lookahead in regex to exclude function names when matching variable names. I’ll also review how the regex handles multiple variables in one declaration.
Highlighting syntax
I’m detailing the highlightLine
function, focusing on identifying syntax elements like brackets, comments, strings, characters, numbers, keywords, types, preprocessor directives, operators, and functions, with the refined regex for variable detection.
Mapping highlights
I’m piecing together the highlightLine
function, detailing regex patterns for syntax elements, sorting matches by starting position, and creating highlight segments to ensure precise syntax highlighting.
Reasoned about syntax highlighting adjustments for 24 seconds
در ادامه فقط توابع و کدهای مرتبط با سیتکسهای هایلایتینگ (بهخصوص تابع highlightLine) را همراه با اصلاح بخش متغیرها (بهطوریکه تمامی شناسههایی که بلافاصله پس از کلیدواژههای
int
, float
, double
, fstream
, string
, vector
, auto
, array
بیایند به عنوان متغیر شناسایی شوند – البته اگر به دنبال پرانتز نباشند تا تابع نباشند) آوردهام:
#include <SDL2/SDL.h> #include <SDL2/SDL_ttf.h> #include <string> #include <iostream> #include <vector> #include <regex> #include <algorithm> using namespace std; // ساختار ساده برای نگهداری سگمنتهای هایلایتشده: struct HighlightSegment { string text; SDL_Color color; }; // مثال از ساختار رنگها (برای تم تاریک و روشن) struct ThemeColors { SDL_Color keyword; SDL_Color types; SDL_Color functionName; SDL_Color variables; SDL_Color stringLiteral; SDL_Color charLiteral; SDL_Color number; SDL_Color comment; SDL_Color preprocessor; SDL_Color operators; SDL_Color brackets; SDL_Color normalText; static ThemeColors Dark() { ThemeColors tc; tc.keyword = {198, 120, 221, 255}; tc.types = {224, 108, 117, 255}; tc.functionName = { 97, 175, 254, 255}; tc.variables = {229, 192, 123, 255}; tc.stringLiteral = {152, 195, 121, 255}; tc.charLiteral = {152, 195, 121, 255}; tc.number = {209, 154, 102, 255}; tc.comment = { 92, 99, 112, 255}; tc.preprocessor = { 86, 182, 194, 255}; tc.operators = {213, 94, 0, 255}; tc.brackets = {171, 178, 191, 255}; tc.normalText = {255, 255, 255, 255}; return tc; } static ThemeColors Light() { ThemeColors tc; tc.keyword = { 0, 51, 102, 255}; tc.types = { 0, 128, 128, 255}; tc.functionName = {255, 140, 0, 255}; tc.variables = {139, 0, 0, 255}; tc.stringLiteral = { 0, 100, 0, 255}; tc.charLiteral = { 0, 100, 0, 255}; tc.number = {128, 0, 128, 255}; tc.comment = {128, 128, 128, 255}; tc.preprocessor = { 0, 139, 139, 255}; tc.operators = {128, 0, 0, 255}; tc.brackets = {184, 134, 11, 255}; tc.normalText = { 0, 0, 0, 255}; return tc; } };
در این تابع با استفاده از regexهای مختلف، سگمنتهای مختلف (برای پرانتزها، کامنتها، رشتهها، اعداد، کلیدواژهها، انواع داده، پیشپردازندهها، عملگرها، توابع و متغیرها) مشخص و رنگآمیزی میشوند. در ادامه به بخش مربوط به متغیرها دقت کنید؛ در نسخه اصلاحشده، تنها در صورتی که پس از یکی از کلیدواژههای
int
, float
, double
, fstream
, string
, vector
, auto
, array
یک شناسه (بدون اینکه به دنبال فضای خالی و سپس علامت باز پرانتز بیاید) بیاید، آن شناسه به عنوان متغیر شناسایی و با رنگ متغیر (tc.variables
) رسم میشود.
vector<HighlightSegment> highlightLine(const string& line, const ThemeColors& tc) { // ساختار داخلی برای نگهداری matchها struct MatchInfo { size_t start; size_t end; SDL_Color color; }; vector<MatchInfo> matches; // تعریف الگوهای مختلف regex bracketRegex("([\$\$\$$\$${}])", regex_constants::ECMAScript); regex commentRegex("//.*", regex_constants::ECMAScript); regex stringRegex("\"([^\"\\\\]|\\\\.)*\"", regex_constants::ECMAScript); regex charRegex("'([^'\\\\]|\\\\.)'", regex_constants::ECMAScript); regex numberRegex("\\b\\d+(\\.\\d+)?\\b", regex_constants::ECMAScript); regex keywordRegex("\\b(class|if|while|else)\\b", regex_constants::ECMAScript); regex typesRegex("\\b(long|int|float|double)\\b", regex_constants::ECMAScript); regex preprocRegex("#include", regex_constants::ECMAScript); regex operatorRegex("([+\\-\\*/])", regex_constants::ECMAScript); regex functionRegex("\\b(?:int|void)\\s+([a-zA-Z_][a-zA-Z0-9_]*)\\s*(?=\\()", regex_constants::ECMAScript); // <<== اصلاح در الگوی متغیرها: // در اینجا تنها شناسهای که پس از یکی از کلیدواژههای زیر بیاید // (int, float, double, fstream, string, vector, auto, array) // و به دنبال آن فضای اختیاری و سپس '(' وجود نداشته باشد، متغیر محسوب میشود. // توجه کنید که این regex به صورت ساده فقط اولین شناسهی پس از نوع را میگیرد. regex identifierRegex( "\\b(?:int|float|double|fstream|string|vector|auto|array)\\s+([a-zA-Z_][a-zA-Z0-9_]*)(?!\\s*\\()", regex_constants::ECMAScript ); // توابع کمکی برای جستجو و ثبت match auto findMatches = [&](const regex &rgx, const SDL_Color &color) { for (sregex_iterator i = sregex_iterator(line.begin(), line.end(), rgx); i != sregex_iterator(); ++i) { smatch match = *i; size_t st = match.position(0); size_t en = st + match.length(0); matches.push_back({ st, en, color }); } }; // برای توابع؛ تنها گروه کپچر (نام تابع) رنگ میشود auto findFunctionMatches = [&](const regex &rgx, const SDL_Color &color) { for (sregex_iterator i = sregex_iterator(line.begin(), line.end(), rgx); i != sregex_iterator(); ++i) { smatch match = *i; size_t st = match.position(1); size_t en = st + match.length(1); matches.push_back({ st, en, color }); } }; // فراخوانی جستجوها (ترتیب مهم است) findMatches(bracketRegex, tc.brackets); findMatches(commentRegex, tc.comment); findMatches(stringRegex, tc.stringLiteral); findMatches(charRegex, tc.charLiteral); findMatches(numberRegex, tc.number); findMatches(keywordRegex, tc.keyword); findMatches(typesRegex, tc.types); findMatches(preprocRegex, tc.preprocessor); findMatches(operatorRegex, tc.operators); findFunctionMatches(functionRegex, tc.functionName); // جستجوی متغیرها – اینجا الگوی اصلاحشده اعمال میشود findMatches(identifierRegex, tc.variables); // مرتبسازی matchها بر اساس موقعیت شروع sort(matches.begin(), matches.end(), [](const MatchInfo &a, const MatchInfo &b) { return a.start < b.start; }); // تقسیمبندی خط به سگمنتهای رنگی vector<HighlightSegment> segments; size_t currentIndex = 0; for (auto &m : matches) { if (m.start < currentIndex) continue; if (m.start > currentIndex) { segments.push_back({ line.substr(currentIndex, m.start - currentIndex), tc.normalText }); } segments.push_back({ line.substr(m.start, m.end - m.start), m.color }); currentIndex = m.end; } if (currentIndex < line.size()) { segments.push_back({ line.substr(currentIndex), tc.normalText }); } return segments; }
الگوهای اولیه:
الگوهایی برای پرانتز، کامنت، رشته، کاراکتر، عدد، کلیدواژه (مثل class
, if
, …)، انواع (مثل long
, int
, …)، پیشپردازنده و عملگر تعریف شدهاند.
الگوی تابع:
با استفاده از lookahead، تنها نام تابع (قبل از پرانتز) استخراج میشود تا از رنگآمیزی ناخواسته کل عبارت جلوگیری شود.
الگوی متغیر (اصلاحشده):
تنها در صورتی که پس از یکی از انواع مشخصشده (int, float, double, fstream, string, vector, auto, array) یک شناسه بیاید و پس از آن فضای اختیاری و سپس علامت باز پرانتز نیاید، آن شناسه به عنوان متغیر شناسایی شده و با رنگ tc.variables رنگآمیزی میشود. این تضمین میکند که در تعریف توابع (که بعد از نوع نام تابع و سپس پرانتز میآید) این شناسه به عنوان تابع رنگ نشود.
امیدوارم این نسخه از توابع سیتکسهای هایلایتینگ مطابق با خواسته شما باشد. در صورت نیاز به تغییرات یا توضیحات بیشتر، بفرمایید.
// main.cpp // IDE سبک با قابلیت syntax-highlighting، انتخاب متن (Ctrl+A و Shift+جهتها)، // اسکرول افقی (Shift+موس پد)، undo/redo (تا ۷ مرحله)، // paste (کنترل+V برای متون بزرگ)، cut (کنترل+X)، // نمایش باکس خطا با اسکرول، و auto‐complete بهبود یافته. // تمام کد در یک فایل قرار دارد.
#include <SDL2/SDL.h> #include <SDL2/SDL_ttf.h> #include <SDL2/SDL2_gfx.h> #include <regex> #include <string> #include <iostream> #include <vector> #include <algorithm> #include <cstring> #include <fstream> #include <windows.h> #include <commdlg.h> #include <cstdio> using namespace std;
// ------------------- Constants ------------------- const int SCREEN_WIDTH = 1300; const int SCREEN_HEIGHT = 700; const Uint32 CURSOR_BLINK_INTERVAL = 500; // میلیثانیه // ------------------- End Constants -------------------
// ------------------- تم اصلی IDE ------------------- struct ThemeColors { SDL_Color background; SDL_Color menuBarItems; SDL_Color menuDropRect; SDL_Color treeMap; SDL_Color defaultText; };
const ThemeColors DARK_MODE = { {0, 0, 0, 255}, {70, 70, 70, 255}, {80, 80, 80, 255}, {50, 50, 50, 255}, {255, 255, 255, 255} };
const ThemeColors LIGHT_MODE = { {255, 255, 255, 255}, {180, 180, 180, 255}, {200, 200, 200, 255}, {150, 150, 150, 255}, {0, 0, 0, 255} }; // ------------------- End تم IDE -------------------
// ------------------- رنگبندی Syntax Highlighting ------------------- // (این بخش جدا از تم IDE است) struct HighlightSegment { string text; SDL_Color color; };
struct SyntaxHighlightColors { SDL_Color keyword; SDL_Color types; SDL_Color functionName; SDL_Color variables; SDL_Color stringLiteral; SDL_Color charLiteral; SDL_Color number; SDL_Color comment; SDL_Color preprocessor; SDL_Color operators; SDL_Color brackets; SDL_Color normalText;
static SyntaxHighlightColors Dark() {
SyntaxHighlightColors tc;
tc.keyword = {198, 120, 221, 255};
tc.types = {224, 108, 117, 255};
tc.functionName = {97, 175, 254, 255};
tc.variables = {229, 192, 123, 255};
tc.stringLiteral = {152, 195, 121, 255};
tc.charLiteral = {152, 195, 121, 255};
tc.number = {209, 154, 102, 255};
tc.comment = {92, 99, 112, 255};
tc.preprocessor = {86, 182, 194, 255};
tc.operators = {213, 94, 0, 255};
tc.brackets = {171, 178, 191, 255};
tc.normalText = {255, 255, 255, 255};
return tc;
}
static SyntaxHighlightColors Light() {
SyntaxHighlightColors tc;
tc.keyword = { 0, 51, 102, 255};
tc.types = { 0, 128, 128, 255};
tc.functionName = {255, 140, 0, 255};
tc.variables = {139, 0, 0, 255};
tc.stringLiteral = { 0, 100, 0, 255};
tc.charLiteral = { 0, 100, 0, 255};
tc.number = {128, 0, 128, 255};
tc.comment = {128, 128, 128, 255};
tc.preprocessor = { 0, 139, 139, 255};
tc.operators = {128, 0, 0, 255};
tc.brackets = {184, 134, 11, 255};
tc.normalText = { 0, 0, 0, 255};
return tc;
}
};
vector<HighlightSegment> highlightLine(const string& line, const SyntaxHighlightColors& tc) { struct MatchInfo { size_t start; size_t end; SDL_Color color; }; vector<MatchInfo> matches;
// تعریف الگوها
regex bracketRegex("(([\$\$\$$\$${}]))", regex_constants::ECMAScript);
regex commentRegex("//.*", regex_constants::ECMAScript);
regex stringRegex("\"([^\"\\\\]|\\\\.)*\"", regex_constants::ECMAScript);
regex charRegex("'([^'\\\\]|\\\\.)'", regex_constants::ECMAScript);
regex numberRegex("\\b\\d+(\\.\\d+)?\\b", regex_constants::ECMAScript);
regex keywordRegex("\\b(class|if|while|else)\\b", regex_constants::ECMAScript);
regex typesRegex("\\b(long|int|float|double)\\b", regex_constants::ECMAScript);
regex preprocRegex("#include", regex_constants::ECMAScript);
regex operatorRegex("([+\\-\\*/])", regex_constants::ECMAScript);
regex functionRegex("\\b(?:int|void)\\s+([a-zA-Z_][a-zA-Z0-9_]*)\\s*(?=\\()", regex_constants::ECMAScript);
regex identifierRegex(
"\\b(?:int|float|double|fstream|string|vector|auto|array)\\s+([a-zA-Z_][a-zA-Z0-9_]*)(?!\\s*\\()",
regex_constants::ECMAScript
);
auto findMatches = [&](const regex &rgx, const SDL_Color &color) {
for (sregex_iterator i = sregex_iterator(line.begin(), line.end(), rgx);
i != sregex_iterator(); ++i)
{
smatch match = *i;
size_t st = match.position(0);
size_t en = st + match.length(0);
matches.push_back({ st, en, color });
}
};
auto findFunctionMatches = [&](const regex &rgx, const SDL_Color &color) {
for (sregex_iterator i = sregex_iterator(line.begin(), line.end(), rgx);
i != sregex_iterator(); ++i)
{
smatch match = *i;
size_t st = match.position(1);
size_t en = st + match.length(1);
matches.push_back({ st, en, color });
}
};
findMatches(bracketRegex, tc.brackets);
findMatches(commentRegex, tc.comment);
findMatches(stringRegex, tc.stringLiteral);
findMatches(charRegex, tc.charLiteral);
findMatches(numberRegex, tc.number);
findMatches(keywordRegex, tc.keyword);
findMatches(typesRegex, tc.types);
findMatches(preprocRegex, tc.preprocessor);
findMatches(operatorRegex, tc.operators);
findFunctionMatches(functionRegex, tc.functionName);
findMatches(identifierRegex, tc.variables);
sort(matches.begin(), matches.end(), [](const MatchInfo &a, const MatchInfo &b) {
return a.start < b.start;
});
vector<HighlightSegment> segments;
size_t currentIndex = 0;
for (auto &m : matches) {
if (m.start < currentIndex)
continue;
if (m.start > currentIndex) {
segments.push_back({ line.substr(currentIndex, m.start - currentIndex), tc.normalText });
}
segments.push_back({ line.substr(m.start, m.end - m.start), m.color });
currentIndex = m.end;
}
if (currentIndex < line.size()) {
segments.push_back({ line.substr(currentIndex), tc.normalText });
}
return segments;
} // ------------------- End Syntax Highlighting -------------------
// ------------------- Undo/Redo ------------------- struct EditorState { vector<string> lines; int currentLine; int cursorPos; };
vector<EditorState> undoStack; vector<EditorState> redoStack; const int UNDO_LIMIT = 7;
void recordState(const vector<string>& lines, int currentLine, int cursorPos) { if (!undoStack.empty()) { const EditorState &last = undoStack.back(); if (last.lines == lines && last.currentLine == currentLine && last.cursorPos == cursorPos) return; } EditorState state = { lines, currentLine, cursorPos }; undoStack.push_back(state); if (undoStack.size() > UNDO_LIMIT) undoStack.erase(undoStack.begin()); redoStack.clear(); }
void performUndo(vector<string>& lines, int ¤tLine, int &cursorPos) { if (!undoStack.empty()) { EditorState state = undoStack.back(); undoStack.pop_back(); EditorState curState = { lines, currentLine, cursorPos }; redoStack.push_back(curState); lines = state.lines; currentLine = state.currentLine; cursorPos = state.cursorPos; } }
void performRedo(vector<string>& lines, int ¤tLine, int &cursorPos) { if (!redoStack.empty()) { EditorState state = redoStack.back(); redoStack.pop_back(); recordState(lines, currentLine, cursorPos); lines = state.lines; currentLine = state.currentLine; cursorPos = state.cursorPos; } } // ------------------- End Undo/Redo -------------------
// ------------------- ساختار منو ------------------- struct Menu { bool fileOpenMenu = false; bool editOpenMenu = false; bool themeOpenMenu = false; // File menu SDL_Rect fileMenuButton = {246, 0, 155, 35}; SDL_Rect dropRectFile = {246, 35, 170, 150}; SDL_Rect itemProjectNew = {246, 35, 170, 50}; SDL_Rect itemProjectSave = {246, 85, 170, 50}; SDL_Rect itemExit = {246, 135, 170, 50}; // Edit menu SDL_Rect editMenuButton = {401, 0, 159, 35}; SDL_Rect dropRectEdit = {421, 35, 120, 100}; SDL_Rect itemUndo = {421, 35, 120, 50}; SDL_Rect itemRedo = {421, 85, 120, 50}; // Theme menu SDL_Rect themeMenuButton = {560, 0, 175, 35}; SDL_Rect dropRectTheme = {560, 35, 175, 100}; SDL_Rect itemLight = {560, 35, 175, 50}; SDL_Rect itemDark = {560, 85, 175, 50}; // Debug & Compile button SDL_Rect dNcMenuButton = {735, 0, 215, 35}; // Run button SDL_Rect runMenuButton = {950, 0, 175, 35}; // Save button SDL_Rect saveMenuButton = {1125, 0, 175, 35}; }; // ------------------- End ساختار منو -------------------
// ------------------- متغیرهای انتخاب متن و اسکرول افقی ------------------- bool textSelectionActive = false; int selectionStartLine = -1, selectionStartPos = -1; int selectionEndLine = -1, selectionEndPos = -1; int horizontalScrollOffset = 0; // برای اسکرول افقی // ------------------- End -------------------
// ------------------- Prototype Functions ------------------- void ensureLastLineVisible(int, int&, int, int, int); void librariesError(SDL_Renderer*, const vector<string>&, TTF_Font*); void makeTextInRectangle(SDL_Renderer*, TTF_Font*, SDL_Rect&, const string&, SDL_Color); string wholeTextFile(const vector<string>&); bool isMouseInRect(int, int, const SDL_Rect&); void renderMenu(SDL_Renderer*, TTF_Font*, Menu&, bool&); void drawRect(SDL_Renderer*, const SDL_Rect&, SDL_Color); void saveFile(const string&, const vector<string>&); void loadFile(const string&, vector<string>&, int&); void treeMap(SDL_Renderer*, bool&, TTF_Font*, const vector<string>&); bool compile(const string&, string&); void runInAnotherWindow(const string &); string newProject(SDL_Renderer*, TTF_Font*); vector<string> getFilesInDirectory(const string&); void handleTextSelection(SDL_Event&, vector<string>&, int&, int&, int&, int&, int&, int&, bool&); void copySelectedText(const vector<string>&, int&, int&, int&, int&, int&, int&, string&); void pasteText(vector<string>&, int&, int&, string&); void cutSelectedText(vector<string>&, int&, int&, int&, int&, int&, int&, string&); void saveAsOption(SDL_Renderer*, TTF_Font*, const vector<string>&); void thereIsError(SDL_Renderer*, TTF_Font*); string showSaveFileDialog(); void updateAutoCompleteSuggestion(const string&, int, string&); void renderAutoComplete(SDL_Renderer*, TTF_Font*, const string&, int, int); // ------------------- End Prototype Functions -------------------
// ------------------- Windows API: Save Dialog ------------------- string showSaveFileDialog() { OPENFILENAME ofn; char fileName[MAX_PATH] = ""; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.lpstrFilter = "C++ Files (.cpp)\0.cpp\0All Files (.)\0*.*\0"; ofn.lpstrFile = fileName; ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_EXPLORER | OFN_OVERWRITEPROMPT; ofn.lpstrDefExt = "cpp"; if (GetSaveFileName(&ofn)) return string(fileName); return ""; } // ------------------- End Save Dialog -------------------
// ------------------- AutoComplete ------------------- vector<string> autoCompleteKeywords = {"switch", "int", "float", "main", "scanf", "printf", "strlen", "else", "while"};
void updateAutoCompleteSuggestion(const string ¤tLineText, int cursorPos, string &suggestion) { suggestion = ""; // اگر حرف قبلی یکی از اینها باشد، پیشنهاد بستن آن را بده if(cursorPos > 0) { char lastChar = currentLineText[cursorPos - 1]; if(lastChar == '(') { suggestion = ")"; return; } if(lastChar == '[') { suggestion = "]"; return; } if(lastChar == '{') { suggestion = "}"; return; } } int start = cursorPos - 1; while(start >= 0 && (isalnum(currentLineText[start]) || currentLineText[start]=='_')) start--; start++; string currentWord = currentLineText.substr(start, cursorPos - start); if (currentWord == "i") { suggestion = "f"; return; } if (currentWord.length() >= 2) { for (auto &kw : autoCompleteKeywords) { if (kw.substr(0, currentWord.length()) == currentWord && kw != currentWord) { suggestion = kw.substr(currentWord.length()); return; } } } }
void renderAutoComplete(SDL_Renderer renderer, TTF_Font font, const string &suggestion, int cursorX, int cursorY) { if (suggestion.empty()) return; SDL_Color faded = {128, 128, 128, 255}; SDL_Surface surface = TTF_RenderText_Blended(font, suggestion.c_str(), faded); SDL_Texture texture = SDL_CreateTextureFromSurface(renderer, surface); SDL_Rect dest = {cursorX, cursorY, surface->w, surface->h}; SDL_RenderCopy(renderer, texture, NULL, &dest); SDL_FreeSurface(surface); SDL_DestroyTexture(texture); } // ------------------- End AutoComplete -------------------
// ------------------- Main ------------------- int main(int argc, char* argv[]) { if (SDL_Init(SDL_INIT_VIDEO) < 0) { cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl; return -1; } if (TTF_Init() == -1) { cerr << "TTF could not initialize! TTF_Error: " << TTF_GetError() << endl; SDL_Quit(); return -1; }
SDL_Window* window = SDL_CreateWindow("SDL Text Editor IDE",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (!window) {
cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << endl;
TTF_Quit();
SDL_Quit();
return -1;
}
// تنظیم آیکون پنجره (فایل openai_logo.bmp باید موجود باشد)
SDL_Surface* iconSurface = SDL_LoadBMP("openai_logo.bmp");
if (iconSurface)
SDL_SetWindowIcon(window, iconSurface);
SDL_FreeSurface(iconSurface);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer) {
cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << endl;
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return -1;
}
// بارگذاری فونتها (مسیر فونتها را بر حسب سیستم خود تنظیم کنید)
TTF_Font* font1 = TTF_OpenFont("C:\\Windows\\Fonts\\consola.ttf", 19);
if (!font1) {
cerr << "Failed to load font1! TTF_Error: " << TTF_GetError() << endl;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return -1;
}
TTF_Font* font2 = TTF_OpenFont("C:\\Users\\BitBin.ir\\Downloads\\Telegram Desktop\\zilla-slab\\ZillaSlab-Bold.otf", 19);
if (!font2) {
cerr << "Failed to load font2! TTF_Error: " << TTF_GetError() << endl;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return -1;
}
TTF_Font* font = font1;
TTF_Font* fontMenuBar = TTF_OpenFont("C:\\Windows\\Fonts\\Arial.ttf", 23);
if (!fontMenuBar) {
cerr << "Failed to load fontMenuBar! TTF_Error: " << TTF_GetError() << endl;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return -1;
}
// تنظیم رنگهای syntax highlighting بر اساس تم
SyntaxHighlightColors syntaxTC = isMouseInRect(0,0, {0,0,0,0}) ? SyntaxHighlightColors::Light() : SyntaxHighlightColors::Dark();
// (در این نمونه به صورت ساده از Dark استفاده میکنیم)
syntaxTC = SyntaxHighlightColors::Dark();
// دریافت لیست فایلهای موجود در پوشه جاری
string directoryPath = ".";
vector<string> filenames = getFilesInDirectory(directoryPath);
bool isSaved = false;
vector<string> lines = { "" };
int currentLine = 0;
int cursorPos = 0;
int scrollOffset = 0;
const int LINE_HEIGHT = TTF_FontHeight(font);
Uint32 lastCursorToggle = SDL_GetTicks();
bool cursorVisible = true;
bool ctrlDown = false, shiftHeld = false;
string clipboard = "";
string autoCompleteSuggestion = "";
// متغیرهای انتخاب متن (ابتدایی)
textSelectionActive = false;
selectionStartLine = selectionStartPos = selectionEndLine = selectionEndPos = -1;
// ساختار منو
Menu menu;
recordState(lines, currentLine, cursorPos);
SDL_Event e;
bool quit = false;
while (!quit) {
Uint32 currentTime = SDL_GetTicks();
if (currentTime > lastCursorToggle + CURSOR_BLINK_INTERVAL) {
cursorVisible = !cursorVisible;
lastCursorToggle = currentTime;
}
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = true;
}
else if (e.type == SDL_MOUSEBUTTONDOWN) {
int mx = e.button.x;
int my = e.button.y;
// کلیک روی sidebar (treeMap) جهت باز کردن پروژه
if (mx < 246) {
int idx = (my - 80) / 40;
if (idx >= 0 && idx < filenames.size()) {
lines.clear();
int dummy = 0;
loadFile(filenames[idx], lines, dummy);
currentLine = 0;
cursorPos = 0;
currentProjectPath = directoryPath + "\\" + filenames[idx];
recordState(lines, currentLine, cursorPos);
}
}
// منوی File
if (!menu.fileOpenMenu && isMouseInRect(mx, my, menu.fileMenuButton))
menu.fileOpenMenu = true;
else if (menu.fileOpenMenu) {
if (isMouseInRect(mx, my, menu.itemProjectNew)) {
isSaved = false;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
lines = { "" };
currentLine = 0;
cursorPos = 0;
currentProjectPath = "";
menu.fileOpenMenu = false;
recordState(lines, currentLine, cursorPos);
}
else if (isMouseInRect(mx, my, menu.itemProjectSave)) {
if (!currentProjectPath.empty())
saveFile(currentProjectPath, lines);
else {
string path = showSaveFileDialog();
if (!path.empty()) {
currentProjectPath = path;
saveFile(currentProjectPath, lines);
filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1));
}
}
menu.fileOpenMenu = false;
// پاکسازی باکس خطا
showErrorPopup = false; errorPopupText = "";
}
else if (isMouseInRect(mx, my, menu.itemExit)) {
quit = true;
menu.fileOpenMenu = false;
}
else if (!isMouseInRect(mx, my, menu.dropRectFile))
menu.fileOpenMenu = false;
}
// منوی Edit
if (!menu.editOpenMenu && isMouseInRect(mx, my, menu.editMenuButton))
menu.editOpenMenu = true;
else if (menu.editOpenMenu) {
if (isMouseInRect(mx, my, menu.itemUndo)) {
performUndo(lines, currentLine, cursorPos);
menu.editOpenMenu = false;
}
else if (isMouseInRect(mx, my, menu.itemRedo)) {
performRedo(lines, currentLine, cursorPos);
menu.editOpenMenu = false;
}
else if (!isMouseInRect(mx, my, menu.dropRectEdit))
menu.editOpenMenu = false;
}
// منوی Theme
if (!menu.themeOpenMenu && isMouseInRect(mx, my, menu.themeMenuButton))
menu.themeOpenMenu = true;
else if (menu.themeOpenMenu) {
if (isMouseInRect(mx, my, menu.itemLight)) {
isDark = false;
menu.themeOpenMenu = false;
}
else if (isMouseInRect(mx, my, menu.itemDark)) {
isDark = true;
menu.themeOpenMenu = false;
}
else if (!isMouseInRect(mx, my, menu.dropRectTheme))
menu.themeOpenMenu = false;
}
// Debug & Compile
if (isMouseInRect(mx, my, menu.dNcMenuButton)) {
if (haveError) {
thereIsError(renderer, fontMenuBar);
} else {
if (!currentProjectPath.empty())
saveFile(currentProjectPath, lines);
else {
string path = newProject(renderer, fontMenuBar);
currentProjectPath = path;
filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1));
saveFile(currentProjectPath, lines);
}
string compileErrors;
if (compile(currentProjectPath, compileErrors))
cout << "[INFO] Compilation successful!\n";
else {
showErrorPopup = true;
errorPopupText = compileErrors;
}
// پاکسازی باکس خطا پس از کلیک
// (در صورت موفقیت، خطا پاک میشود)
}
}
// Run
if (isMouseInRect(mx, my, menu.runMenuButton)) {
if (haveError) {
thereIsError(renderer, fontMenuBar);
} else {
if (!currentProjectPath.empty())
saveFile(currentProjectPath, lines);
else {
string path = newProject(renderer, fontMenuBar);
currentProjectPath = path;
filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1));
saveFile(currentProjectPath, lines);
}
string compileErrors;
if (compile(currentProjectPath, compileErrors))
runInAnotherWindow("output");
else {
showErrorPopup = true;
errorPopupText = compileErrors;
}
}
}
// Save (دکمه گرافیکی)
if (isMouseInRect(mx, my, menu.saveMenuButton)) {
if (!currentProjectPath.empty())
saveFile(currentProjectPath, lines);
else {
string path = showSaveFileDialog();
if (!path.empty()) {
currentProjectPath = path;
saveFile(currentProjectPath, lines);
filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1));
}
}
// پاکسازی باکس خطا
showErrorPopup = false; errorPopupText = "";
}
}
else if (e.type == SDL_MOUSEWHEEL) {
// اگر Shift نگه داشته شده باشد، اسکرول افقی؛ در غیر این صورت عمودی
if (shiftHeld)
horizontalScrollOffset = max(0, horizontalScrollOffset - e.wheel.y * 20);
else {
// اگر موس درون باکس خطا باشد، اسکرول آن با errorScrollOffset
int mx, my;
SDL_GetMouseState(&mx, &my);
SDL_Rect errorBox = {500, SCREEN_HEIGHT - 150, 600, 140};
if (isMouseInRect(mx, my, errorBox))
; // میتوان errorScrollOffset را بهروزرسانی کرد (برای مثال: errorScrollOffset += e.wheel.y * 20)
else
scrollOffset = max(0, scrollOffset - e.wheel.y * LINE_HEIGHT);
}
}
else if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.sym == SDLK_LCTRL || e.key.keysym.sym == SDLK_RCTRL)
ctrlDown = true;
if (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT)
shiftHeld = true;
// Ctrl+A: انتخاب کل متن
if ((e.key.keysym.mod & KMOD_CTRL) && e.key.keysym.sym == SDLK_a) {
textSelectionActive = true;
selectionStartLine = 0; selectionStartPos = 0;
selectionEndLine = lines.size() - 1;
selectionEndPos = lines.back().size();
continue;
}
// Undo/Redo با Ctrl+Z / Ctrl+Y
if (e.key.keysym.mod & KMOD_CTRL) {
if (e.key.keysym.sym == SDLK_z) {
performUndo(lines, currentLine, cursorPos);
continue;
}
if (e.key.keysym.sym == SDLK_y) {
performRedo(lines, currentLine, cursorPos);
continue;
}
// Copy با Ctrl+C
if (e.key.keysym.sym == SDLK_c) {
copySelectedText(lines, cursorPos, cursorPos, currentLine, currentLine, cursorPos, currentLine, clipboard);
SDL_SetClipboardText(clipboard.c_str());
continue;
}
// Paste با Ctrl+V (استفاده از متن کلیپبورد)
if (e.key.keysym.sym == SDLK_v) {
const char* clipText = SDL_GetClipboardText();
if (clipText) {
string clipStr(clipText);
pasteText(lines, cursorPos, currentLine, clipStr);
recordState(lines, currentLine, cursorPos);
}
continue;
}
// Cut با Ctrl+X
if (e.key.keysym.sym == SDLK_x) {
copySelectedText(lines, cursorPos, cursorPos, currentLine, currentLine, cursorPos, currentLine, clipboard);
SDL_SetClipboardText(clipboard.c_str());
cutSelectedText(lines, cursorPos, cursorPos, currentLine, currentLine, cursorPos, currentLine, clipboard);
recordState(lines, currentLine, cursorPos);
continue;
}
// Save با Ctrl+S
if (e.key.keysym.sym == SDLK_s) {
string path = showSaveFileDialog();
if (!path.empty()) {
currentProjectPath = path;
saveFile(currentProjectPath, lines);
filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1));
}
continue;
}
}
// اگر Tab زده شود و autoCompleteSuggestion موجود باشد
if (e.key.keysym.sym == SDLK_TAB && !autoCompleteSuggestion.empty()) {
lines[currentLine].insert(cursorPos, autoCompleteSuggestion);
cursorPos += autoCompleteSuggestion.length();
autoCompleteSuggestion = "";
recordState(lines, currentLine, cursorPos);
continue;
}
// جهتیابی کرسر و انتخاب متن با Shift (اگر Shift نگه داشته شده، انتخاب را بهروزرسانی کن)
if (e.key.keysym.sym == SDLK_LEFT) {
if (cursorPos > 0)
cursorPos--;
else if (currentLine > 0) {
currentLine--;
cursorPos = lines[currentLine].size();
}
if (shiftHeld) {
if (!textSelectionActive) { textSelectionActive = true; selectionStartLine = currentLine; selectionStartPos = cursorPos; }
selectionEndLine = currentLine; selectionEndPos = cursorPos;
} else { textSelectionActive = false; }
}
else if (e.key.keysym.sym == SDLK_RIGHT) {
if (cursorPos < lines[currentLine].size())
cursorPos++;
else if (currentLine < lines.size() - 1) {
currentLine++;
cursorPos = 0;
}
if (shiftHeld) {
if (!textSelectionActive) { textSelectionActive = true; selectionStartLine = currentLine; selectionStartPos = cursorPos; }
selectionEndLine = currentLine; selectionEndPos = cursorPos;
} else { textSelectionActive = false; }
}
else if (e.key.keysym.sym == SDLK_UP) {
if (currentLine > 0) {
currentLine--;
cursorPos = min(cursorPos, (int)lines[currentLine].size());
ensureLastLineVisible(currentLine, scrollOffset, SCREEN_HEIGHT, LINE_HEIGHT, lines.size());
}
if (shiftHeld) {
if (!textSelectionActive) { textSelectionActive = true; selectionStartLine = currentLine; selectionStartPos = cursorPos; }
selectionEndLine = currentLine; selectionEndPos = cursorPos;
} else { textSelectionActive = false; }
}
else if (e.key.keysym.sym == SDLK_DOWN) {
if (currentLine < lines.size() - 1) {
currentLine++;
cursorPos = min(cursorPos, (int)lines[currentLine].size());
ensureLastLineVisible(currentLine, scrollOffset, SCREEN_HEIGHT, LINE_HEIGHT, lines.size());
}
if (shiftHeld) {
if (!textSelectionActive) { textSelectionActive = true; selectionStartLine = currentLine; selectionStartPos = cursorPos; }
selectionEndLine = currentLine; selectionEndPos = cursorPos;
} else { textSelectionActive = false; }
}
// Backspace
if (e.key.keysym.sym == SDLK_BACKSPACE) {
if (cursorPos > 0 && cursorPos <= lines[currentLine].size()) {
lines[currentLine].erase(cursorPos - 1, 1);
cursorPos--;
}
else if (currentLine > 0) {
cursorPos = lines[currentLine - 1].size();
lines[currentLine - 1] += lines[currentLine];
lines.erase(lines.begin() + currentLine);
currentLine--;
}
if (lines.empty()) { lines.push_back(""); currentLine = 0; cursorPos = 0; }
recordState(lines, currentLine, cursorPos);
}
else if (e.key.keysym.sym == SDLK_RETURN) {
string remainder = lines[currentLine].substr(cursorPos);
lines[currentLine] = lines[currentLine].substr(0, cursorPos);
lines.insert(lines.begin() + currentLine + 1, remainder);
currentLine++;
cursorPos = 0;
ensureLastLineVisible(currentLine, scrollOffset, SCREEN_HEIGHT, LINE_HEIGHT, lines.size());
recordState(lines, currentLine, cursorPos);
}
else if (e.key.keysym.sym == SDLK_TAB) {
lines[currentLine].insert(cursorPos, " ");
cursorPos += 4;
recordState(lines, currentLine, cursorPos);
}
}
else if (e.type == SDL_KEYUP) {
if (e.key.keysym.sym == SDLK_LCTRL || e.key.keysym.sym == SDLK_RCTRL)
ctrlDown = false;
if (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT)
shiftHeld = false;
}
else if (e.type == SDL_TEXTINPUT) {
lines[currentLine].insert(cursorPos, e.text.text);
cursorPos += strlen(e.text.text);
updateAutoCompleteSuggestion(lines[currentLine], cursorPos, autoCompleteSuggestion);
ensureLastLineVisible(currentLine, scrollOffset, SCREEN_HEIGHT, LINE_HEIGHT, lines.size());
recordState(lines, currentLine, cursorPos);
}
} // پایان Event Loop
// ----------------------- Rendering -----------------------
SDL_Color textColor;
if (isDark) {
SDL_SetRenderDrawColor(renderer, DARK_MODE.background.r, DARK_MODE.background.g, DARK_MODE.background.b, DARK_MODE.background.a);
textColor = DARK_MODE.defaultText;
} else {
SDL_SetRenderDrawColor(renderer, LIGHT_MODE.background.r, LIGHT_MODE.background.g, LIGHT_MODE.background.b, LIGHT_MODE.background.a);
textColor = LIGHT_MODE.defaultText;
}
SDL_RenderClear(renderer);
// رسم متن (با Syntax Highlighting و اعمال اسکرول افقی)
int y = -scrollOffset;
for (size_t j = 0; j < lines.size(); ++j) {
if (y + LINE_HEIGHT > 0 && y < SCREEN_HEIGHT) {
// برای هر خط، ابتدا بررسی میکنیم آیا بخشی از آن انتخاب شده است یا خیر
bool lineSelected = false;
int selStart = 0, selEnd = 0;
if (textSelectionActive) {
// نرمالسازی محدوده انتخاب (فرض میکنیم selectionStart <= selectionEnd)
if ( (j > selectionStartLine && j < selectionEndLine) ||
(j == selectionStartLine && j == selectionEndLine && selectionStartPos < selectionEndPos) ||
(j == selectionStartLine && j < selectionEndLine) ||
(j > selectionStartLine && j == selectionEndLine) )
{
lineSelected = true;
if (j == selectionStartLine)
selStart = selectionStartPos;
else
selStart = 0;
if (j == selectionEndLine)
selEnd = selectionEndPos;
else
selEnd = lines[j].size();
}
}
// استفاده از syntax highlighting برای خط j
vector<HighlightSegment> segments = highlightLine(lines[j], syntaxTC);
int x = 255 - horizontalScrollOffset; // شروع از margin ثابت
// اگر خط انتخاب شده است، رسم یک مستطیل آبی به عنوان پسزمینه انتخاب
if (lineSelected && selStart < selEnd) {
int selX = 0, selXEnd = 0;
TTF_SizeText(font, lines[j].substr(0, selStart).c_str(), &selX, nullptr);
TTF_SizeText(font, lines[j].substr(0, selEnd).c_str(), &selXEnd, nullptr);
SDL_Rect selRect = { x + selX, y + 44, selXEnd - selX, LINE_HEIGHT };
SDL_SetRenderDrawColor(renderer, 0, 120, 215, 150); // آبی شفاف
SDL_RenderFillRect(renderer, &selRect);
}
// رسم هر سگمنت از خط
for (auto &seg : segments) {
// اگر خط انتخاب شده است، رنگ متن در محدوده انتخاب به سفید تغییر میکند.
// (برای سادگی، اگر هر بخشی از خط انتخاب شده باشد، آن سگمنت به رنگ انتخاب نمایش داده میشود)
SDL_Color segColor = seg.color;
if (lineSelected) {
// محاسبه اینکه آیا این سگمنت در محدوده انتخاب قرار میگیرد
// (برای سادگی، اگر بخشی از متن خط انتخاب شده است، کل سگمنت انتخاب شده در نظر گرفته میشود)
segColor = {255, 255, 255, 255};
}
SDL_Surface* wordSurface = TTF_RenderText_Blended(font, seg.text.c_str(), segColor);
SDL_Texture* wordTexture = SDL_CreateTextureFromSurface(renderer, wordSurface);
SDL_Rect renderQuad = { x, y + 44, wordSurface->w, wordSurface->h };
SDL_RenderCopy(renderer, wordTexture, nullptr, &renderQuad);
x += wordSurface->w;
SDL_FreeSurface(wordSurface);
SDL_DestroyTexture(wordTexture);
}
// رسم کرسر (اگر این خط جاری است)
if ((int)j == currentLine && cursorVisible) {
int cursorX = 0;
if (cursorPos > 0) {
TTF_SizeText(font, lines[j].substr(0, cursorPos).c_str(), &cursorX, nullptr);
}
cursorX += 255 - horizontalScrollOffset;
SDL_SetRenderDrawColor(renderer, textColor.r, textColor.g, textColor.b, textColor.a);
SDL_RenderDrawLine(renderer, cursorX, y + 44, cursorX, y + LINE_HEIGHT + 44);
renderAutoComplete(renderer, font, autoCompleteSuggestion, cursorX, y + 44);
}
}
y += LINE_HEIGHT;
}
// رسم باکس خطا (در پایین صفحه) با clipping جهت اسکرول درون باکس
if (showErrorPopup && !errorPopupText.empty()) {
SDL_Rect errorBox = {500, SCREEN_HEIGHT - 150, 600, 140};
SDL_SetRenderDrawColor(renderer, 200, 200, 200, 255);
SDL_RenderFillRect(renderer, &errorBox);
SDL_Rect clipRect = errorBox;
SDL_RenderSetClipRect(renderer, &clipRect);
// رسم متن خطا (ممکن است بیش از ارتفاع باکس باشد)
SDL_Surface* errorSurface = TTF_RenderText_Blended_Wrapped(fontMenuBar, errorPopupText.c_str(), {255,0,0,255}, errorBox.w);
SDL_Texture* errorTexture = SDL_CreateTextureFromSurface(renderer, errorSurface);
SDL_Rect errorTextRect = { errorBox.x, errorBox.y - errorScrollOffset, errorSurface->w, errorSurface->h };
SDL_RenderCopy(renderer, errorTexture, NULL, &errorTextRect);
SDL_FreeSurface(errorSurface);
SDL_DestroyTexture(errorTexture);
SDL_RenderSetClipRect(renderer, NULL);
}
librariesError(renderer, lines, fontMenuBar);
treeMap(renderer, isDark, fontMenuBar, filenames);
renderMenu(renderer, fontMenuBar, menu, isDark);
SDL_RenderPresent(renderer);
}
TTF_CloseFont(font);
TTF_CloseFont(fontMenuBar);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 0;
} // ------------------- End Main -------------------
// ------------------- Helper Functions -------------------
void ensureLastLineVisible(int currentLine, int &scrollOffset, int SCREEN_HEIGHT, int LINE_HEIGHT, int totalLines) { int cursorY = currentLine * LINE_HEIGHT - scrollOffset; if (cursorY < 0) scrollOffset = currentLine * LINE_HEIGHT; else if (cursorY + LINE_HEIGHT > SCREEN_HEIGHT) scrollOffset = (currentLine + 1) * LINE_HEIGHT - SCREEN_HEIGHT; int contentHeight = totalLines * LINE_HEIGHT; if (contentHeight > SCREEN_HEIGHT) scrollOffset = min(scrollOffset, contentHeight - SCREEN_HEIGHT); else scrollOffset = 0; }
void librariesError(SDL_Renderer* renderer, const vector<string>& lines, TTF_Font* font) { string wholeText = wholeTextFile(lines); regex bits("#include <bits/stdc++.h>"); regex iostream("#include <iostream>"); regex notIostream("std::(sqrt|pow|sin|cos|tan|abs|exp|log|log10|floor|ceil)"); regex cmath("#include <cmath>"); regex notCmath("std::(cout|cin|endl|getline)"); SDL_Rect error = {255, 660, 850, 40}; if (!regex_search(wholeText, iostream) && !regex_search(wholeText, bits) && regex_search(wholeText, notCmath)) { makeTextInRectangle(renderer, font, error, "Library Error ... , Include iostream or bits", {255,0,0,255}); // تنظیم haveError در اینجا } else { // haveError = false; } if (!regex_search(wholeText, cmath) && !regex_search(wholeText, bits) && regex_search(wholeText, notIostream)) { makeTextInRectangle(renderer, font, error, "Library Error ... , Include cmath or bits", {255,0,0,255}); } }
void makeTextInRectangle(SDL_Renderer* renderer, TTF_Font* font, SDL_Rect& renderQuad, const string& text, SDL_Color textColor) { SDL_Surface* textSurface = TTF_RenderText_Blended(font, text.c_str(), textColor); SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); int textWidth = textSurface->w; int textHeight = textSurface->h; SDL_Rect dest = { renderQuad.x + (renderQuad.w - textWidth)/2, renderQuad.y + (renderQuad.h - textHeight)/2, textWidth, textHeight }; SDL_FreeSurface(textSurface); SDL_RenderCopy(renderer, textTexture, nullptr, &dest); SDL_DestroyTexture(textTexture); }
string wholeTextFile(const vector<string>& lines) { string result = ""; for (auto &line : lines) result += line + "\n"; return result; }
bool isMouseInRect(int mouseX, int mouseY, const SDL_Rect& rect) { return (mouseX >= rect.x && mouseX < rect.x + rect.w && mouseY >= rect.y && mouseY < rect.y + rect.h); }
void renderMenu(SDL_Renderer* renderer, TTF_Font* font, Menu& menu, bool& isDark) { if (isDark) { drawRect(renderer, menu.fileMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.fileMenuButton, "File", DARK_MODE.defaultText); drawRect(renderer, menu.editMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.editMenuButton, "Edit", DARK_MODE.defaultText); drawRect(renderer, menu.themeMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.themeMenuButton, "Theme", DARK_MODE.defaultText); drawRect(renderer, menu.dNcMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.dNcMenuButton, "Deb & Com", DARK_MODE.defaultText); drawRect(renderer, menu.runMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.runMenuButton, "Run", DARK_MODE.defaultText); drawRect(renderer, menu.saveMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.saveMenuButton, "Save", DARK_MODE.defaultText); if (menu.fileOpenMenu) { drawRect(renderer, menu.dropRectFile, DARK_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemProjectNew, "Project New", DARK_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemProjectSave, "Project Save", DARK_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemExit, "EXIT", DARK_MODE.defaultText); } else if (menu.editOpenMenu) { drawRect(renderer, menu.dropRectEdit, DARK_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemUndo, "Undo", DARK_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemRedo, "Redo", DARK_MODE.defaultText); } else if (menu.themeOpenMenu) { drawRect(renderer, menu.dropRectTheme, DARK_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemDark, "Dark Mode", DARK_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemLight, "Light Mode", DARK_MODE.defaultText); } } else { drawRect(renderer, menu.fileMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.fileMenuButton, "File", LIGHT_MODE.defaultText); drawRect(renderer, menu.editMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.editMenuButton, "Edit", LIGHT_MODE.defaultText); drawRect(renderer, menu.themeMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.themeMenuButton, "Theme", LIGHT_MODE.defaultText); drawRect(renderer, menu.dNcMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.dNcMenuButton, "Deb & Com", LIGHT_MODE.defaultText); drawRect(renderer, menu.runMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.runMenuButton, "Run", LIGHT_MODE.defaultText); drawRect(renderer, menu.saveMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.saveMenuButton, "Save", LIGHT_MODE.defaultText); if (menu.fileOpenMenu) { drawRect(renderer, menu.dropRectFile, LIGHT_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemProjectNew, "Project New", LIGHT_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemProjectSave, "Project Save", LIGHT_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemExit, "EXIT", LIGHT_MODE.defaultText); } else if (menu.editOpenMenu) { drawRect(renderer, menu.dropRectEdit, LIGHT_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemUndo, "Undo", LIGHT_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemRedo, "Redo", LIGHT_MODE.defaultText); } else if (menu.themeOpenMenu) { drawRect(renderer, menu.dropRectTheme, LIGHT_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemDark, "Dark Mode", LIGHT_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemLight, "Light Mode", LIGHT_MODE.defaultText); } } }
void drawRect(SDL_Renderer* renderer, const SDL_Rect& rect, SDL_Color color) { SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); SDL_RenderFillRect(renderer, &rect); }
void saveFile(const string& fileName, const vector<string>& lines) { ofstream file(fileName); if (file.good()) { for (auto &line : lines) file << line << "\n"; cout << "[INFO] File saved.\n"; } else { cout << "[ERROR] Could not open file for saving: " << fileName << "\n"; return; } file.close(); }
void loadFile(const string& fileName, vector<string>& lines, int& i) { ifstream file(fileName); string line; if (file.good()) { lines.clear(); while (getline(file, line)) { lines.push_back(line); i++; } } else { cout << "[INFO] Could not open file: " << fileName << ". Starting with an empty buffer.\n"; return; } file.close(); }
void treeMap(SDL_Renderer* renderer, bool& isDark, TTF_Font *font, const vector<string>& filenames) { SDL_Rect projectText = {0, 0, 246, 60}; if (isDark) { drawRect(renderer, {0, 0, 246, 700}, DARK_MODE.treeMap); makeTextInRectangle(renderer, font, projectText, "Projects", DARK_MODE.defaultText); if (filenames.empty()) { SDL_Rect error = {10, 80, 249, 40}; makeTextInRectangle(renderer, font, error, "No projects here", DARK_MODE.defaultText); return; } int i = 0; for (auto &name : filenames) { SDL_Rect fileRect = {10, 80 + 40 * i, 220, 20}; i++; makeTextInRectangle(renderer, font, fileRect, name, DARK_MODE.defaultText); } } else { drawRect(renderer, {0, 0, 246, 700}, LIGHT_MODE.treeMap); makeTextInRectangle(renderer, font, projectText, "Projects", LIGHT_MODE.defaultText); if (filenames.empty()) { SDL_Rect error = {10, 80, 249, 40}; makeTextInRectangle(renderer, font, error, "No projects here", LIGHT_MODE.defaultText); return; } int i = 0; for (auto &name : filenames) { SDL_Rect fileRect = {10, 80 + 40 * i, 220, 20}; i++; makeTextInRectangle(renderer, font, fileRect, name, LIGHT_MODE.defaultText); } } }
bool compile(const string &filename, string &errorOutput) { string compileCommand = "g++ -x c++ "" + filename + "" -o output 2>&1"; FILE* pipe = _popen(compileCommand.c_str(), "r"); if (!pipe) return false; char buffer[128]; string result; while (fgets(buffer, sizeof(buffer), pipe) != nullptr) result += buffer; int exitCode = _pclose(pipe); if (exitCode != 0) { errorOutput = result; return false; } errorOutput = ""; return true; }
void runInAnotherWindow(const string &filename) { string runCommand = "start cmd /c "" + filename + " & pause""; system(runCommand.c_str()); }
string newProject(SDL_Renderer *renderer, TTF_Font *font) { SDL_Rect done = {575, 340, 150, 40}; SDL_Color textColor = {0, 0, 0, 255}; SDL_Event e; string filename = " "; bool isDone = false; while (!isDone) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_MOUSEBUTTONDOWN) { int mx = e.button.x; int my = e.button.y; if (isMouseInRect(mx, my, done)) isDone = true; } else if (e.type == SDL_KEYDOWN) { if (e.key.keysym.sym == SDLK_BACKSPACE && !filename.empty()) filename.pop_back(); } else if (e.type == SDL_TEXTINPUT) { filename += e.text.text; } } drawRect(renderer, {500, 200, 300, 200}, {150, 150, 150, 255}); SDL_Surface *textSurface = TTF_RenderText_Blended(font, filename.c_str(), textColor); SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); SDL_Rect renderQuad = {520, 220, textSurface->w, textSurface->h}; SDL_FreeSurface(textSurface); SDL_RenderCopy(renderer, textTexture, nullptr, &renderQuad); SDL_DestroyTexture(textTexture); drawRect(renderer, done, {255, 255, 255, 255}); makeTextInRectangle(renderer, font, done, "Done", {0, 0, 0, 255}); SDL_RenderPresent(renderer); } filename.erase(filename.begin()); return filename + ".cpp"; }
vector<string> getFilesInDirectory(const string& directoryPath) { vector<string> files; WIN32_FIND_DATA findFileData; HANDLE hFind = FindFirstFile((directoryPath + "\*").c_str(), &findFileData); if (hFind == INVALID_HANDLE_VALUE) { cerr << "Failed to open directory!" << endl; return files; } do { if (!(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) files.push_back(findFileData.cFileName); } while (FindNextFile(hFind, &findFileData) != 0); FindClose(hFind); return files; }
// توابع انتخاب، کپی، پیست و بریدن متن (پیادهسازی ساده) void handleTextSelection(SDL_Event& event, vector<string>& lines, int& selectionStartX, int& selectionEndX, int& selectionEndY, int& selectionStartY, int& cursorX, int& cursorY, bool& shiftHeld) { // پیادهسازی ابتدایی (این تابع در نسخهی فعلی از رویدادهای کیبورد در main مدیریت میشود) }
void copySelectedText(const vector<string>& lines, int& selectionStartX, int& selectionEndX, int& selectionEndY, int& selectionStartY, int& cursorX, int& cursorY, string& clipboard) { // پیادهسازی ساده؛ در اینجا برای مثال if (selectionStartX == -1 || selectionEndX == -1) return; int startY = min(selectionStartY, selectionEndY); int endY = max(selectionStartY, selectionEndY); int startX = (startY == selectionStartY) ? selectionStartX : selectionEndX; int endX = (endY == selectionEndY) ? selectionEndX : selectionStartX; clipboard.clear(); for (int y = startY; y <= endY; y++) { int lineStart = (y == startY) ? startX : 0; int lineEnd = (y == endY) ? endX : lines[y].length(); if (lineStart < lines[y].length() && lineEnd <= lines[y].length()) { clipboard += lines[y].substr(lineStart, lineEnd - lineStart); if (y != endY) clipboard += "\n"; } } cout << "Copied to clipboard: " << clipboard << endl; }
void pasteText(vector<string>& lines, int& cursorX, int& cursorY, string& clipboard) { if (clipboard.empty()) return; size_t newlinePos = clipboard.find('\n'); if (newlinePos == string::npos) { lines[cursorY].insert(cursorX, clipboard); cursorX += clipboard.length(); } else { string firstLine = clipboard.substr(0, newlinePos); lines[cursorY].insert(cursorX, firstLine); cursorX = firstLine.length(); string remainingText = clipboard.substr(newlinePos + 1); size_t nextNewlinePos; while ((nextNewlinePos = remainingText.find('\n')) != string::npos) { lines.insert(lines.begin() + cursorY + 1, remainingText.substr(0, nextNewlinePos)); cursorY++; remainingText = remainingText.substr(nextNewlinePos + 1); } if (!remainingText.empty()) { lines.insert(lines.begin() + cursorY + 1, remainingText); cursorY++; cursorX = remainingText.length(); } } cout << "Pasted from clipboard: " << clipboard << endl; }
void cutSelectedText(vector<string>& lines, int& selectionStartX, int& selectionEndX, int& selectionEndY, int& selectionStartY, int& cursorX, int& cursorY, string& clipboard) { copySelectedText(lines, selectionStartX, selectionEndX, selectionEndY, selectionStartY, cursorX, cursorY, clipboard); if (selectionStartX != -1 && selectionEndX != -1) { if (selectionStartY == selectionEndY) { lines[selectionStartY].erase(selectionStartX, selectionEndX - selectionStartX); } else { lines[selectionStartY].erase(selectionStartX); lines[selectionEndY].erase(0, selectionEndX); lines[selectionStartY] += lines[selectionEndY]; lines.erase(lines.begin() + selectionStartY + 1, lines.begin() + selectionEndY + 1); } } }
void saveAsOption(SDL_Renderer* renderer, TTF_Font* font, const vector<string>& lines) { SDL_Rect done = {575, 340, 150, 40}; SDL_Color textColor = {0, 0, 0, 255}; SDL_Event e; string filename = " "; bool isDone = false; while (!isDone) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_MOUSEBUTTONDOWN) { int mx = e.button.x; int my = e.button.y; if (isMouseInRect(mx, my, done)) isDone = true; } else if (e.type == SDL_KEYDOWN) { if (e.key.keysym.sym == SDLK_BACKSPACE && !filename.empty()) filename.pop_back(); } else if (e.type == SDL_TEXTINPUT) { filename += e.text.text; } } drawRect(renderer, {500, 200, 300, 200}, {150, 150, 150, 255}); SDL_Surface *textSurface = TTF_RenderText_Blended(font, filename.c_str(), textColor); SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); SDL_Rect renderQuad = {520, 220, textSurface->w, textSurface->h}; SDL_FreeSurface(textSurface); SDL_RenderCopy(renderer, textTexture, nullptr, &renderQuad); SDL_DestroyTexture(textTexture); drawRect(renderer, done, {255, 255, 255, 255}); makeTextInRectangle(renderer, font, done, "Done", {0, 0, 0, 255}); SDL_RenderPresent(renderer); } filename.erase(filename.begin()); filename += ".cpp"; saveFile(filename, lines); }
void thereIsError(SDL_Renderer* renderer, TTF_Font* font) { SDL_Rect text = {550, 330, 200, 40}; drawRect(renderer, {500, 300, 300, 100}, {200, 200, 200, 255}); makeTextInRectangle(renderer, font, text, "There is an Error", {255, 0, 0, 255}); } // ------------------- End Helper Functions -------------------
/* توجه: این کد یک نمونه آزمایشی و تقریبی است. بسیاری از عملکردهای انتخاب متن، اسکرول افقی، مدیریت باکس خطا و auto‐complete ممکن است نیاز به بهبود و تستهای بیشتر داشته باشند. / این کد حطا های زیر رو داره CMakeFiles/text_editor.dir/main.cpp.obj C:\MinGW\bin\c++.exe -g -std=gnu++1z -fdiagnostics-color=always -MD -MT CMakeFiles/text_editor.dir/main.cpp.obj -MF CMakeFiles\text_editor.dir\main.cpp.obj.d -o CMakeFiles/text_editor.dir/main.cpp.obj -c "C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp" C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp: In function 'int SDL_main(int, char*)': C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:498:25: error: 'currentProjectPath' was not declared in this scope currentProjectPath = directoryPath + "\" + filenames[idx]; ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:513:25: error: 'currentProjectPath' was not declared in this scope currentProjectPath = ""; ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:518:30: error: 'currentProjectPath' was not declared in this scope if (!currentProjectPath.empty()) ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:530:25: error: 'showErrorPopup' was not declared in this scope showErrorPopup = false; errorPopupText = ""; ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:530:49: error: 'errorPopupText' was not declared in this scope showErrorPopup = false; errorPopupText = ""; ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:559:25: error: 'isDark' was not declared in this scope isDark = false; ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:563:25: error: 'isDark' was not declared in this scope isDark = true; ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:571:25: error: 'haveError' was not declared in this scope if (haveError) { ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:574:30: error: 'currentProjectPath' was not declared in this scope if (!currentProjectPath.empty()) ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:583:37: error: 'currentProjectPath' was not declared in this scope if (compile(currentProjectPath, compileErrors)) ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:586:29: error: 'showErrorPopup' was not declared in this scope showErrorPopup = true; ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:587:29: error: 'errorPopupText' was not declared in this scope errorPopupText = compileErrors; ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:595:25: error: 'haveError' was not declared in this scope if (haveError) { ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:598:30: error: 'currentProjectPath' was not declared in this scope if (!currentProjectPath.empty()) ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:607:37: error: 'currentProjectPath' was not declared in this scope if (compile(currentProjectPath, compileErrors)) ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:610:29: error: 'showErrorPopup' was not declared in this scope showErrorPopup = true; ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:611:29: error: 'errorPopupText' was not declared in this scope errorPopupText = compileErrors; ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:617:26: error: 'currentProjectPath' was not declared in this scope if (!currentProjectPath.empty()) ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:628:21: error: 'showErrorPopup' was not declared in this scope showErrorPopup = false; errorPopupText = ""; ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:628:45: error: 'errorPopupText' was not declared in this scope showErrorPopup = false; errorPopupText = ""; ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:699:29: error: 'currentProjectPath' was not declared in this scope currentProjectPath = path; ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:808:13: error: 'isDark' was not declared in this scope if (isDark) { ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:891:13: error: 'showErrorPopup' was not declared in this scope if (showErrorPopup && !errorPopupText.empty()) { ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:891:32: error: 'errorPopupText' was not declared in this scope if (showErrorPopup && !errorPopupText.empty()) { ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:900:65: error: 'errorScrollOffset' was not declared in this scope SDL_Rect errorTextRect = { errorBox.x, errorBox.y - errorScrollOffset, errorSurface->w, errorSurface->h }; ^ C:/Users/BitBin.ir/CLionProjects/text editor/main.cpp:908:27: error: 'isDark' was not declared in this scope treeMap(renderer, isDark, fontMenuBar, filenames); ^ ninja: build stopped: subcommand failed.
میخوام همه چی تو یه فایل .CPP باشه ها
Mapping variable declarations
I’m piecing together global variable declarations for currentProjectPath
, showErrorPopup
, errorPopupText
, haveError
, isDark
, and errorScrollOffset
to streamline the code.
Ensuring completeness
I’m compiling a singular .cpp file that encompasses all variables like isDark
, showErrorPopup
, errorPopupText
, haveError
, currentProjectPath
, and errorScrollOffset
. Testing with MinGW for Windows.
Reasoned for 7 seconds
در این نسخه تمام متغیرهایی که در خطاها آمده (مانند currentProjectPath
، isDark
، showErrorPopup
، errorPopupText
، haveError
، و errorScrollOffset
) را بهصورت سراسری (global) در همان فایل تعریف کردهام. به این شکل دیگر خطاهای مربوط به «تعریفنشده» برطرف میشوند. همچنین همهچیز در یک فایل واحد .cpp
قرار دارد.
نکته: مسیر فونتها را در قسمتهای TTF_OpenFont
مطابق سیستم خود تغییر دهید (یا با یک فونت جایگزین کنید)، و اگر فایل openai_logo.bmp
ندارید، آن را حذف کنید یا جایگزین نمایید.
کد نهایی:
#include <SDL2/SDL.h> #include <SDL2/SDL_ttf.h> #include <SDL2/SDL2_gfx.h> #include <regex> #include <string> #include <iostream> #include <vector> #include <algorithm> #include <cstring> #include <fstream> #include <windows.h> #include <commdlg.h> #include <cstdio> using namespace std; // --------------------------------------------------------- // متغیرهای سراسری که خطا میدادند را اینجا تعریف میکنیم: bool isDark = true; // تعیین تم تاریک یا روشن bool showErrorPopup = false; // نمایش باکس خطا string errorPopupText = ""; // متن خطا در باکس پایین bool haveError = false; // اگر خطایی موجود باشد string currentProjectPath = ""; // مسیر فایل پروژهی جاری int errorScrollOffset = 0; // اسکرول عمودی برای باکس خطا // --------------------------------------------------------- // ------------------- Constants ------------------- const int SCREEN_WIDTH = 1300; const int SCREEN_HEIGHT = 700; const Uint32 CURSOR_BLINK_INTERVAL = 500; // میلیثانیه // ------------------- End Constants ------------------- // ------------------- تم اصلی IDE ------------------- struct ThemeColors { SDL_Color background; SDL_Color menuBarItems; SDL_Color menuDropRect; SDL_Color treeMap; SDL_Color defaultText; }; const ThemeColors DARK_MODE = { {0, 0, 0, 255}, {70, 70, 70, 255}, {80, 80, 80, 255}, {50, 50, 50, 255}, {255, 255, 255, 255} }; const ThemeColors LIGHT_MODE = { {255, 255, 255, 255}, {180, 180, 180, 255}, {200, 200, 200, 255}, {150, 150, 150, 255}, {0, 0, 0, 255} }; // ------------------- End تم IDE ------------------- // ------------------- رنگبندی Syntax Highlighting ------------------- // (این بخش جدا از تم IDE است) struct HighlightSegment { string text; SDL_Color color; }; struct SyntaxHighlightColors { SDL_Color keyword; SDL_Color types; SDL_Color functionName; SDL_Color variables; SDL_Color stringLiteral; SDL_Color charLiteral; SDL_Color number; SDL_Color comment; SDL_Color preprocessor; SDL_Color operators; SDL_Color brackets; SDL_Color normalText; static SyntaxHighlightColors Dark() { SyntaxHighlightColors tc; tc.keyword = {198, 120, 221, 255}; tc.types = {224, 108, 117, 255}; tc.functionName = {97, 175, 254, 255}; tc.variables = {229, 192, 123, 255}; tc.stringLiteral = {152, 195, 121, 255}; tc.charLiteral = {152, 195, 121, 255}; tc.number = {209, 154, 102, 255}; tc.comment = {92, 99, 112, 255}; tc.preprocessor = {86, 182, 194, 255}; tc.operators = {213, 94, 0, 255}; tc.brackets = {171, 178, 191, 255}; tc.normalText = {255, 255, 255, 255}; return tc; } static SyntaxHighlightColors Light() { SyntaxHighlightColors tc; tc.keyword = { 0, 51, 102, 255}; tc.types = { 0, 128, 128, 255}; tc.functionName = {255, 140, 0, 255}; tc.variables = {139, 0, 0, 255}; tc.stringLiteral = { 0, 100, 0, 255}; tc.charLiteral = { 0, 100, 0, 255}; tc.number = {128, 0, 128, 255}; tc.comment = {128, 128, 128, 255}; tc.preprocessor = { 0, 139, 139, 255}; tc.operators = {128, 0, 0, 255}; tc.brackets = {184, 134, 11, 255}; tc.normalText = { 0, 0, 0, 255}; return tc; } }; vector<HighlightSegment> highlightLine(const string& line, const SyntaxHighlightColors& tc) { struct MatchInfo { size_t start; size_t end; SDL_Color color; }; vector<MatchInfo> matches; // تعریف الگوها regex bracketRegex("(([\$\$\$$\$${}]))", regex_constants::ECMAScript); regex commentRegex("//.*", regex_constants::ECMAScript); regex stringRegex("\"([^\"\\\\]|\\\\.)*\"", regex_constants::ECMAScript); regex charRegex("'([^'\\\\]|\\\\.)'", regex_constants::ECMAScript); regex numberRegex("\\b\\d+(\\.\\d+)?\\b", regex_constants::ECMAScript); regex keywordRegex("\\b(class|if|while|else)\\b", regex_constants::ECMAScript); regex typesRegex("\\b(long|int|float|double)\\b", regex_constants::ECMAScript); regex preprocRegex("#include", regex_constants::ECMAScript); regex operatorRegex("([+\\-\\*/])", regex_constants::ECMAScript); regex functionRegex("\\b(?:int|void)\\s+([a-zA-Z_][a-zA-Z0-9_]*)\\s*(?=\\()", regex_constants::ECMAScript); regex identifierRegex( "\\b(?:int|float|double|fstream|string|vector|auto|array)\\s+([a-zA-Z_][a-zA-Z0-9_]*)(?!\\s*\\()", regex_constants::ECMAScript ); auto findMatches = [&](const regex &rgx, const SDL_Color &color) { for (sregex_iterator i = sregex_iterator(line.begin(), line.end(), rgx); i != sregex_iterator(); ++i) { smatch match = *i; size_t st = match.position(0); size_t en = st + match.length(0); matches.push_back({ st, en, color }); } }; auto findFunctionMatches = [&](const regex &rgx, const SDL_Color &color) { for (sregex_iterator i = sregex_iterator(line.begin(), line.end(), rgx); i != sregex_iterator(); ++i) { smatch match = *i; size_t st = match.position(1); size_t en = st + match.length(1); matches.push_back({ st, en, color }); } }; findMatches(bracketRegex, tc.brackets); findMatches(commentRegex, tc.comment); findMatches(stringRegex, tc.stringLiteral); findMatches(charRegex, tc.charLiteral); findMatches(numberRegex, tc.number); findMatches(keywordRegex, tc.keyword); findMatches(typesRegex, tc.types); findMatches(preprocRegex, tc.preprocessor); findMatches(operatorRegex, tc.operators); findFunctionMatches(functionRegex, tc.functionName); findMatches(identifierRegex, tc.variables); sort(matches.begin(), matches.end(), [](const MatchInfo &a, const MatchInfo &b) { return a.start < b.start; }); vector<HighlightSegment> segments; size_t currentIndex = 0; for (auto &m : matches) { if (m.start < currentIndex) continue; if (m.start > currentIndex) { segments.push_back({ line.substr(currentIndex, m.start - currentIndex), tc.normalText }); } segments.push_back({ line.substr(m.start, m.end - m.start), m.color }); currentIndex = m.end; } if (currentIndex < line.size()) { segments.push_back({ line.substr(currentIndex), tc.normalText }); } return segments; } // ------------------- End Syntax Highlighting ------------------- // ------------------- Undo/Redo ------------------- struct EditorState { vector<string> lines; int currentLine; int cursorPos; }; vector<EditorState> undoStack; vector<EditorState> redoStack; const int UNDO_LIMIT = 7; void recordState(const vector<string>& lines, int currentLine, int cursorPos) { if (!undoStack.empty()) { const EditorState &last = undoStack.back(); if (last.lines == lines && last.currentLine == currentLine && last.cursorPos == cursorPos) return; } EditorState state = { lines, currentLine, cursorPos }; undoStack.push_back(state); if (undoStack.size() > UNDO_LIMIT) undoStack.erase(undoStack.begin()); redoStack.clear(); } void performUndo(vector<string>& lines, int ¤tLine, int &cursorPos) { if (!undoStack.empty()) { EditorState state = undoStack.back(); undoStack.pop_back(); EditorState curState = { lines, currentLine, cursorPos }; redoStack.push_back(curState); lines = state.lines; currentLine = state.currentLine; cursorPos = state.cursorPos; } } void performRedo(vector<string>& lines, int ¤tLine, int &cursorPos) { if (!redoStack.empty()) { EditorState state = redoStack.back(); redoStack.pop_back(); recordState(lines, currentLine, cursorPos); lines = state.lines; currentLine = state.currentLine; cursorPos = state.cursorPos; } } // ------------------- End Undo/Redo ------------------- // ------------------- ساختار منو ------------------- struct Menu { bool fileOpenMenu = false; bool editOpenMenu = false; bool themeOpenMenu = false; // File menu SDL_Rect fileMenuButton = {246, 0, 155, 35}; SDL_Rect dropRectFile = {246, 35, 170, 150}; SDL_Rect itemProjectNew = {246, 35, 170, 50}; SDL_Rect itemProjectSave = {246, 85, 170, 50}; SDL_Rect itemExit = {246, 135, 170, 50}; // Edit menu SDL_Rect editMenuButton = {401, 0, 159, 35}; SDL_Rect dropRectEdit = {421, 35, 120, 100}; SDL_Rect itemUndo = {421, 35, 120, 50}; SDL_Rect itemRedo = {421, 85, 120, 50}; // Theme menu SDL_Rect themeMenuButton = {560, 0, 175, 35}; SDL_Rect dropRectTheme = {560, 35, 175, 100}; SDL_Rect itemLight = {560, 35, 175, 50}; SDL_Rect itemDark = {560, 85, 175, 50}; // Debug & Compile button SDL_Rect dNcMenuButton = {735, 0, 215, 35}; // Run button SDL_Rect runMenuButton = {950, 0, 175, 35}; // Save button SDL_Rect saveMenuButton = {1125, 0, 175, 35}; }; // ------------------- End ساختار منو ------------------- // ------------------- متغیرهای انتخاب متن و اسکرول افقی ------------------- bool textSelectionActive = false; int selectionStartLine = -1, selectionStartPos = -1; int selectionEndLine = -1, selectionEndPos = -1; int horizontalScrollOffset = 0; // برای اسکرول افقی // ------------------- End ------------------- // ------------------- Prototype Functions ------------------- void ensureLastLineVisible(int, int&, int, int, int); void librariesError(SDL_Renderer*, const vector<string>&, TTF_Font*); void makeTextInRectangle(SDL_Renderer*, TTF_Font*, SDL_Rect&, const string&, SDL_Color); string wholeTextFile(const vector<string>&); bool isMouseInRect(int, int, const SDL_Rect&); void renderMenu(SDL_Renderer*, TTF_Font*, Menu&, bool&); void drawRect(SDL_Renderer*, const SDL_Rect&, SDL_Color); void saveFile(const string&, const vector<string>&); void loadFile(const string&, vector<string>&, int&); void treeMap(SDL_Renderer*, bool&, TTF_Font*, const vector<string>&); bool compile(const string&, string&); void runInAnotherWindow(const string &); string newProject(SDL_Renderer*, TTF_Font*); vector<string> getFilesInDirectory(const string&); void handleTextSelection(SDL_Event&, vector<string>&, int&, int&, int&, int&, int&, int&, bool&); void copySelectedText(const vector<string>&, int&, int&, int&, int&, int&, int&, string&); void pasteText(vector<string>&, int&, int&, string&); void cutSelectedText(vector<string>&, int&, int&, int&, int&, int&, int&, string&); void saveAsOption(SDL_Renderer*, TTF_Font*, const vector<string>&); void thereIsError(SDL_Renderer*, TTF_Font*); string showSaveFileDialog(); void updateAutoCompleteSuggestion(const string&, int, string&); void renderAutoComplete(SDL_Renderer*, TTF_Font*, const string&, int, int); // ------------------- End Prototype Functions ------------------- // ------------------- Windows API: Save Dialog ------------------- string showSaveFileDialog() { OPENFILENAME ofn; char fileName[MAX_PATH] = ""; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.lpstrFilter = "C++ Files (*.cpp)\0*.cpp\0All Files (*.*)\0*.*\0"; ofn.lpstrFile = fileName; ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_EXPLORER | OFN_OVERWRITEPROMPT; ofn.lpstrDefExt = "cpp"; if (GetSaveFileName(&ofn)) return string(fileName); return ""; } // ------------------- End Save Dialog ------------------- // ------------------- AutoComplete ------------------- vector<string> autoCompleteKeywords = {"switch", "int", "float", "main", "scanf", "printf", "strlen", "else", "while"}; void updateAutoCompleteSuggestion(const string ¤tLineText, int cursorPos, string &suggestion) { suggestion = ""; // اگر حرف قبلی یکی از اینها باشد، پیشنهاد بستن آن را بده if(cursorPos > 0) { char lastChar = currentLineText[cursorPos - 1]; if(lastChar == '(') { suggestion = ")"; return; } if(lastChar == '[') { suggestion = "]"; return; } if(lastChar == '{') { suggestion = "}"; return; } } int start = cursorPos - 1; while(start >= 0 && (isalnum(currentLineText[start]) || currentLineText[start]=='_')) start--; start++; string currentWord = currentLineText.substr(start, cursorPos - start); if (currentWord == "i") { suggestion = "f"; return; } if (currentWord.length() >= 2) { for (auto &kw : autoCompleteKeywords) { if (kw.substr(0, currentWord.length()) == currentWord && kw != currentWord) { suggestion = kw.substr(currentWord.length()); return; } } } } void renderAutoComplete(SDL_Renderer *renderer, TTF_Font *font, const string &suggestion, int cursorX, int cursorY) { if (suggestion.empty()) return; SDL_Color faded = {128, 128, 128, 255}; SDL_Surface* surface = TTF_RenderText_Blended(font, suggestion.c_str(), faded); SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); SDL_Rect dest = {cursorX, cursorY, surface->w, surface->h}; SDL_RenderCopy(renderer, texture, NULL, &dest); SDL_FreeSurface(surface); SDL_DestroyTexture(texture); } // ------------------- End AutoComplete ------------------- // ------------------- Main ------------------- int main(int argc, char* argv[]) { if (SDL_Init(SDL_INIT_VIDEO) < 0) { cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl; return -1; } if (TTF_Init() == -1) { cerr << "TTF could not initialize! TTF_Error: " << TTF_GetError() << endl; SDL_Quit(); return -1; } SDL_Window* window = SDL_CreateWindow("SDL Text Editor IDE", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (!window) { cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << endl; TTF_Quit(); SDL_Quit(); return -1; } // تنظیم آیکون پنجره (فایل openai_logo.bmp باید موجود باشد - در صورت عدم نیاز حذف کنید) SDL_Surface* iconSurface = SDL_LoadBMP("openai_logo.bmp"); if (iconSurface) SDL_SetWindowIcon(window, iconSurface); SDL_FreeSurface(iconSurface); SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (!renderer) { cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << endl; SDL_DestroyWindow(window); TTF_Quit(); SDL_Quit(); return -1; } // بارگذاری فونتها (مسیر فونتها را بر حسب سیستم خود تنظیم کنید) TTF_Font* font1 = TTF_OpenFont("C:\\Windows\\Fonts\\consola.ttf", 19); if (!font1) { cerr << "Failed to load font1! TTF_Error: " << TTF_GetError() << endl; SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); TTF_Quit(); SDL_Quit(); return -1; } // یک فونت دیگر برای تست: TTF_Font* font2 = TTF_OpenFont("C:\\Windows\\Fonts\\Arial.ttf", 19); if (!font2) { cerr << "Failed to load font2! TTF_Error: " << TTF_GetError() << endl; SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); TTF_Quit(); SDL_Quit(); return -1; } // به صورت پیشفرض از font1 استفاده میکنیم TTF_Font* font = font1; TTF_Font* fontMenuBar = TTF_OpenFont("C:\\Windows\\Fonts\\Arial.ttf", 23); if (!fontMenuBar) { cerr << "Failed to load fontMenuBar! TTF_Error: " << TTF_GetError() << endl; SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); TTF_Quit(); SDL_Quit(); return -1; } // تنظیم رنگهای syntax highlighting SyntaxHighlightColors syntaxTC = SyntaxHighlightColors::Dark(); // دریافت لیست فایلهای موجود در پوشه جاری string directoryPath = "."; vector<string> filenames = getFilesInDirectory(directoryPath); bool isSaved = false; vector<string> lines = { "" }; int currentLine = 0; int cursorPos = 0; int scrollOffset = 0; const int LINE_HEIGHT = TTF_FontHeight(font); Uint32 lastCursorToggle = SDL_GetTicks(); bool cursorVisible = true; bool ctrlDown = false, shiftHeld = false; string clipboard = ""; string autoCompleteSuggestion = ""; // متغیرهای انتخاب متن textSelectionActive = false; selectionStartLine = selectionStartPos = selectionEndLine = selectionEndPos = -1; // ساختار منو Menu menu; recordState(lines, currentLine, cursorPos); SDL_Event e; bool quit = false; while (!quit) { Uint32 currentTime = SDL_GetTicks(); if (currentTime > lastCursorToggle + CURSOR_BLINK_INTERVAL) { cursorVisible = !cursorVisible; lastCursorToggle = currentTime; } while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_QUIT) { quit = true; } else if (e.type == SDL_MOUSEBUTTONDOWN) { int mx = e.button.x; int my = e.button.y; // کلیک روی sidebar (treeMap) جهت باز کردن پروژه if (mx < 246) { int idx = (my - 80) / 40; if (idx >= 0 && idx < (int)filenames.size()) { lines.clear(); int dummy = 0; loadFile(filenames[idx], lines, dummy); currentLine = 0; cursorPos = 0; // مسیر پروژه currentProjectPath = directoryPath + "\\" + filenames[idx]; recordState(lines, currentLine, cursorPos); } } // منوی File if (!menu.fileOpenMenu && isMouseInRect(mx, my, menu.fileMenuButton)) { menu.fileOpenMenu = true; } else if (menu.fileOpenMenu) { if (isMouseInRect(mx, my, menu.itemProjectNew)) { isSaved = false; SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_RenderClear(renderer); lines = { "" }; currentLine = 0; cursorPos = 0; currentProjectPath = ""; menu.fileOpenMenu = false; recordState(lines, currentLine, cursorPos); } else if (isMouseInRect(mx, my, menu.itemProjectSave)) { if (!currentProjectPath.empty()) saveFile(currentProjectPath, lines); else { string path = showSaveFileDialog(); if (!path.empty()) { currentProjectPath = path; saveFile(currentProjectPath, lines); filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1)); } } menu.fileOpenMenu = false; // پاکسازی باکس خطا showErrorPopup = false; errorPopupText = ""; } else if (isMouseInRect(mx, my, menu.itemExit)) { quit = true; menu.fileOpenMenu = false; } else if (!isMouseInRect(mx, my, menu.dropRectFile)) { menu.fileOpenMenu = false; } } // منوی Edit if (!menu.editOpenMenu && isMouseInRect(mx, my, menu.editMenuButton)) { menu.editOpenMenu = true; } else if (menu.editOpenMenu) { if (isMouseInRect(mx, my, menu.itemUndo)) { performUndo(lines, currentLine, cursorPos); menu.editOpenMenu = false; } else if (isMouseInRect(mx, my, menu.itemRedo)) { performRedo(lines, currentLine, cursorPos); menu.editOpenMenu = false; } else if (!isMouseInRect(mx, my, menu.dropRectEdit)) { menu.editOpenMenu = false; } } // منوی Theme if (!menu.themeOpenMenu && isMouseInRect(mx, my, menu.themeMenuButton)) { menu.themeOpenMenu = true; } else if (menu.themeOpenMenu) { if (isMouseInRect(mx, my, menu.itemLight)) { isDark = false; menu.themeOpenMenu = false; } else if (isMouseInRect(mx, my, menu.itemDark)) { isDark = true; menu.themeOpenMenu = false; } else if (!isMouseInRect(mx, my, menu.dropRectTheme)) { menu.themeOpenMenu = false; } } // Debug & Compile if (isMouseInRect(mx, my, menu.dNcMenuButton)) { if (haveError) { thereIsError(renderer, fontMenuBar); } else { if (!currentProjectPath.empty()) { saveFile(currentProjectPath, lines); } else { string path = newProject(renderer, fontMenuBar); currentProjectPath = path; filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1)); saveFile(currentProjectPath, lines); } string compileErrors; if (compile(currentProjectPath, compileErrors)) { cout << "[INFO] Compilation successful!\n"; } else { showErrorPopup = true; errorPopupText = compileErrors; } } } // Run if (isMouseInRect(mx, my, menu.runMenuButton)) { if (haveError) { thereIsError(renderer, fontMenuBar); } else { if (!currentProjectPath.empty()) { saveFile(currentProjectPath, lines); } else { string path = newProject(renderer, fontMenuBar); currentProjectPath = path; filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1)); saveFile(currentProjectPath, lines); } string compileErrors; if (compile(currentProjectPath, compileErrors)) { runInAnotherWindow("output"); } else { showErrorPopup = true; errorPopupText = compileErrors; } } } // Save (دکمه گرافیکی) if (isMouseInRect(mx, my, menu.saveMenuButton)) { if (!currentProjectPath.empty()) saveFile(currentProjectPath, lines); else { string path = showSaveFileDialog(); if (!path.empty()) { currentProjectPath = path; saveFile(currentProjectPath, lines); filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1)); } } showErrorPopup = false; errorPopupText = ""; } } else if (e.type == SDL_MOUSEWHEEL) { // اگر Shift نگه داشته شده باشد، اسکرول افقی؛ در غیر این صورت عمودی if (shiftHeld) { horizontalScrollOffset = max(0, horizontalScrollOffset - e.wheel.y * 20); } else { int mx, my; SDL_GetMouseState(&mx, &my); SDL_Rect errorBox = {500, SCREEN_HEIGHT - 150, 600, 140}; // اگر موس درون باکس خطا باشد if (isMouseInRect(mx, my, errorBox)) { // مثلاً: errorScrollOffset += e.wheel.y * 20; errorScrollOffset = max(0, errorScrollOffset + e.wheel.y * 20); } else { scrollOffset = max(0, scrollOffset - e.wheel.y * LINE_HEIGHT); } } } else if (e.type == SDL_KEYDOWN) { if (e.key.keysym.sym == SDLK_LCTRL || e.key.keysym.sym == SDLK_RCTRL) ctrlDown = true; if (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT) shiftHeld = true; // Ctrl+A: انتخاب کل متن if ((e.key.keysym.mod & KMOD_CTRL) && e.key.keysym.sym == SDLK_a) { textSelectionActive = true; selectionStartLine = 0; selectionStartPos = 0; selectionEndLine = (int)lines.size() - 1; selectionEndPos = (int)lines.back().size(); continue; } // Undo/Redo با Ctrl+Z / Ctrl+Y if (e.key.keysym.mod & KMOD_CTRL) { if (e.key.keysym.sym == SDLK_z) { performUndo(lines, currentLine, cursorPos); continue; } if (e.key.keysym.sym == SDLK_y) { performRedo(lines, currentLine, cursorPos); continue; } // Copy با Ctrl+C if (e.key.keysym.sym == SDLK_c) { copySelectedText(lines, cursorPos, cursorPos, currentLine, currentLine, cursorPos, currentLine, clipboard); SDL_SetClipboardText(clipboard.c_str()); continue; } // Paste با Ctrl+V if (e.key.keysym.sym == SDLK_v) { const char* clipText = SDL_GetClipboardText(); if (clipText) { string clipStr(clipText); pasteText(lines, cursorPos, currentLine, clipStr); recordState(lines, currentLine, cursorPos); } continue; } // Cut با Ctrl+X if (e.key.keysym.sym == SDLK_x) { copySelectedText(lines, cursorPos, cursorPos, currentLine, currentLine, cursorPos, currentLine, clipboard); SDL_SetClipboardText(clipboard.c_str()); cutSelectedText(lines, cursorPos, cursorPos, currentLine, currentLine, cursorPos, currentLine, clipboard); recordState(lines, currentLine, cursorPos); continue; } // Save با Ctrl+S if (e.key.keysym.sym == SDLK_s) { string path = showSaveFileDialog(); if (!path.empty()) { currentProjectPath = path; saveFile(currentProjectPath, lines); filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1)); } continue; } } // اگر Tab زده شود و autoCompleteSuggestion موجود باشد if (e.key.keysym.sym == SDLK_TAB && !autoCompleteSuggestion.empty()) { lines[currentLine].insert(cursorPos, autoCompleteSuggestion); cursorPos += (int)autoCompleteSuggestion.length(); autoCompleteSuggestion = ""; recordState(lines, currentLine, cursorPos); continue; } // حرکت کرسر if (e.key.keysym.sym == SDLK_LEFT) { if (cursorPos > 0) cursorPos--; else if (currentLine > 0) { currentLine--; cursorPos = (int)lines[currentLine].size(); } if (shiftHeld) { if (!textSelectionActive) { textSelectionActive = true; selectionStartLine = currentLine; selectionStartPos = cursorPos; } selectionEndLine = currentLine; selectionEndPos = cursorPos; } else { textSelectionActive = false; } } else if (e.key.keysym.sym == SDLK_RIGHT) { if (cursorPos < (int)lines[currentLine].size()) cursorPos++; else if (currentLine < (int)lines.size() - 1) { currentLine++; cursorPos = 0; } if (shiftHeld) { if (!textSelectionActive) { textSelectionActive = true; selectionStartLine = currentLine; selectionStartPos = cursorPos; } selectionEndLine = currentLine; selectionEndPos = cursorPos; } else { textSelectionActive = false; } } else if (e.key.keysym.sym == SDLK_UP) { if (currentLine > 0) { currentLine--; cursorPos = min(cursorPos, (int)lines[currentLine].size()); ensureLastLineVisible(currentLine, scrollOffset, SCREEN_HEIGHT, LINE_HEIGHT, (int)lines.size()); } if (shiftHeld) { if (!textSelectionActive) { textSelectionActive = true; selectionStartLine = currentLine; selectionStartPos = cursorPos; } selectionEndLine = currentLine; selectionEndPos = cursorPos; } else { textSelectionActive = false; } } else if (e.key.keysym.sym == SDLK_DOWN) { if (currentLine < (int)lines.size() - 1) { currentLine++; cursorPos = min(cursorPos, (int)lines[currentLine].size()); ensureLastLineVisible(currentLine, scrollOffset, SCREEN_HEIGHT, LINE_HEIGHT, (int)lines.size()); } if (shiftHeld) { if (!textSelectionActive) { textSelectionActive = true; selectionStartLine = currentLine; selectionStartPos = cursorPos; } selectionEndLine = currentLine; selectionEndPos = cursorPos; } else { textSelectionActive = false; } } // Backspace if (e.key.keysym.sym == SDLK_BACKSPACE) { if (cursorPos > 0 && cursorPos <= (int)lines[currentLine].size()) { lines[currentLine].erase(cursorPos - 1, 1); cursorPos--; } else if (currentLine > 0) { cursorPos = (int)lines[currentLine - 1].size(); lines[currentLine - 1] += lines[currentLine]; lines.erase(lines.begin() + currentLine); currentLine--; } if (lines.empty()) { lines.push_back(""); currentLine = 0; cursorPos = 0; } recordState(lines, currentLine, cursorPos); } else if (e.key.keysym.sym == SDLK_RETURN) { string remainder = lines[currentLine].substr(cursorPos); lines[currentLine] = lines[currentLine].substr(0, cursorPos); lines.insert(lines.begin() + currentLine + 1, remainder); currentLine++; cursorPos = 0; ensureLastLineVisible(currentLine, scrollOffset, SCREEN_HEIGHT, LINE_HEIGHT, (int)lines.size()); recordState(lines, currentLine, cursorPos); } else if (e.key.keysym.sym == SDLK_TAB) { // اگر کاربر Tab میزند ولی پیشنهاد خالی است، فقط فاصله میگذاریم lines[currentLine].insert(cursorPos, " "); cursorPos += 4; recordState(lines, currentLine, cursorPos); } } else if (e.type == SDL_KEYUP) { if (e.key.keysym.sym == SDLK_LCTRL || e.key.keysym.sym == SDLK_RCTRL) ctrlDown = false; if (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT) shiftHeld = false; } else if (e.type == SDL_TEXTINPUT) { lines[currentLine].insert(cursorPos, e.text.text); cursorPos += (int)strlen(e.text.text); updateAutoCompleteSuggestion(lines[currentLine], cursorPos, autoCompleteSuggestion); ensureLastLineVisible(currentLine, scrollOffset, SCREEN_HEIGHT, LINE_HEIGHT, (int)lines.size()); recordState(lines, currentLine, cursorPos); } } // پایان Event Loop // ----------------------- Rendering ----------------------- SDL_Color textColor; if (isDark) { SDL_SetRenderDrawColor(renderer, DARK_MODE.background.r, DARK_MODE.background.g, DARK_MODE.background.b, DARK_MODE.background.a); textColor = DARK_MODE.defaultText; } else { SDL_SetRenderDrawColor(renderer, LIGHT_MODE.background.r, LIGHT_MODE.background.g, LIGHT_MODE.background.b, LIGHT_MODE.background.a); textColor = LIGHT_MODE.defaultText; } SDL_RenderClear(renderer); // رسم متن (با Syntax Highlighting و اعمال اسکرول افقی) int y = -scrollOffset; for (size_t j = 0; j < lines.size(); ++j) { if (y + LINE_HEIGHT > 0 && y < SCREEN_HEIGHT) { // برای هر خط، آیا بخشی انتخاب شده است؟ bool lineSelected = false; int selStart = 0, selEnd = 0; if (textSelectionActive) { // نرمالسازی انتخاب int startL = min(selectionStartLine, selectionEndLine); int endL = max(selectionStartLine, selectionEndLine); int startP = (startL == selectionStartLine) ? selectionStartPos : selectionEndPos; int endP = (endL == selectionEndLine) ? selectionEndPos : selectionStartPos; if ((int)j >= startL && (int)j <= endL) { lineSelected = true; if ((int)j == startL) selStart = startP; else selStart = 0; if ((int)j == endL) selEnd = endP; else selEnd = (int)lines[j].size(); if (selStart > (int)lines[j].size()) selStart = (int)lines[j].size(); if (selEnd > (int)lines[j].size()) selEnd = (int)lines[j].size(); } } // syntax highlighting vector<HighlightSegment> segments = highlightLine(lines[j], syntaxTC); int x = 255 - horizontalScrollOffset; // اگر خط انتخاب شده است، پسزمینه در محدوده selStart..selEnd را با رنگ انتخاب بکشیم // رسم سگمنتها for (auto &seg : segments) { // ابتدا اندازه متن سگمنت SDL_Color segColor = seg.color; // اگر تمام یا بخشی از این سگمنت در محدوده انتخاب باشد، باید بخشی از آن هایلایت شود // ولی برای سادگی، چک میکنیم اگر هر بخشی از خط انتخاب شده باشد، کل سگمنت سفید شود: // (بهبود: باید جداگانه هر بخش را جدا کنیم. اما اینجا ساده گرفتیم.) if (lineSelected) { segColor = {255, 255, 255, 255}; } SDL_Surface* wordSurface = TTF_RenderText_Blended(font, seg.text.c_str(), segColor); SDL_Texture* wordTexture = SDL_CreateTextureFromSurface(renderer, wordSurface); SDL_Rect renderQuad = { x, y + 44, wordSurface->w, wordSurface->h }; SDL_RenderCopy(renderer, wordTexture, nullptr, &renderQuad); x += wordSurface->w; SDL_FreeSurface(wordSurface); SDL_DestroyTexture(wordTexture); } // اگر خط جاری باشد، کرسر را رسم کن if ((int)j == currentLine && cursorVisible) { int cursorX = 0; if (cursorPos > 0) { TTF_SizeText(font, lines[j].substr(0, cursorPos).c_str(), &cursorX, nullptr); } cursorX += 255 - horizontalScrollOffset; SDL_SetRenderDrawColor(renderer, textColor.r, textColor.g, textColor.b, textColor.a); SDL_RenderDrawLine(renderer, cursorX, y + 44, cursorX, y + LINE_HEIGHT + 44); // نمایش autoComplete renderAutoComplete(renderer, font, autoCompleteSuggestion, cursorX, y + 44); } } y += LINE_HEIGHT; } // رسم باکس خطا (در پایین صفحه) با clipping if (showErrorPopup && !errorPopupText.empty()) { SDL_Rect errorBox = {500, SCREEN_HEIGHT - 150, 600, 140}; SDL_SetRenderDrawColor(renderer, 200, 200, 200, 255); SDL_RenderFillRect(renderer, &errorBox); SDL_Rect clipRect = errorBox; SDL_RenderSetClipRect(renderer, &clipRect); // رسم متن خطا SDL_Surface* errorSurface = TTF_RenderText_Blended_Wrapped(fontMenuBar, errorPopupText.c_str(), {255,0,0,255}, errorBox.w); SDL_Texture* errorTexture = SDL_CreateTextureFromSurface(renderer, errorSurface); SDL_Rect errorTextRect = { errorBox.x, errorBox.y - errorScrollOffset, errorSurface->w, errorSurface->h }; SDL_RenderCopy(renderer, errorTexture, NULL, &errorTextRect); SDL_FreeSurface(errorSurface); SDL_DestroyTexture(errorTexture); SDL_RenderSetClipRect(renderer, NULL); } // تابعی برای چک کردن ارور کتابخانه (مثال) librariesError(renderer, lines, fontMenuBar); // رسم TreeMap (سمت چپ)؛ فهرست فایلها treeMap(renderer, isDark, fontMenuBar, filenames); // رسم منوها renderMenu(renderer, fontMenuBar, menu, isDark); SDL_RenderPresent(renderer); } TTF_CloseFont(font); TTF_CloseFont(fontMenuBar); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); TTF_Quit(); SDL_Quit(); return 0; } // ------------------- End Main ------------------- // ------------------- Helper Functions ------------------- void ensureLastLineVisible(int currentLine, int &scrollOffset, int SCREEN_HEIGHT, int LINE_HEIGHT, int totalLines) { int cursorY = currentLine * LINE_HEIGHT - scrollOffset; if (cursorY < 0) scrollOffset = currentLine * LINE_HEIGHT; else if (cursorY + LINE_HEIGHT > SCREEN_HEIGHT) scrollOffset = (currentLine + 1) * LINE_HEIGHT - SCREEN_HEIGHT; int contentHeight = totalLines * LINE_HEIGHT; if (contentHeight > SCREEN_HEIGHT) scrollOffset = min(scrollOffset, contentHeight - SCREEN_HEIGHT); else scrollOffset = 0; } void librariesError(SDL_Renderer* renderer, const vector<string>& lines, TTF_Font* font) { string wholeText = wholeTextFile(lines); regex bits("#include <bits/stdc++.h>"); regex iostream("#include <iostream>"); regex notIostream("std::(sqrt|pow|sin|cos|tan|abs|exp|log|log10|floor|ceil)"); regex cmath("#include <cmath>"); regex notCmath("std::(cout|cin|endl|getline)"); SDL_Rect error = {255, 660, 850, 40}; // اگر مثلا تابع های std::cin, std::cout استفاده شده ولی // iostream یا bits وجود ندارد => خطا: if (!regex_search(wholeText, iostream) && !regex_search(wholeText, bits) && regex_search(wholeText, notCmath)) { makeTextInRectangle(renderer, font, error, "Library Error ... , Include iostream or bits", {255,0,0,255}); haveError = true; // اینجا میگوییم که برنامه خطا دارد } else { haveError = false; } // اگر توابع ریاضی استفاده شده ولی // cmath یا bits درج نشده => خطا if (!regex_search(wholeText, cmath) && !regex_search(wholeText, bits) && regex_search(wholeText, notIostream)) { makeTextInRectangle(renderer, font, error, "Library Error ... , Include cmath or bits", {255,0,0,255}); haveError = true; } } void makeTextInRectangle(SDL_Renderer* renderer, TTF_Font* font, SDL_Rect& renderQuad, const string& text, SDL_Color textColor) { SDL_Surface* textSurface = TTF_RenderText_Blended(font, text.c_str(), textColor); SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); int textWidth = textSurface->w; int textHeight = textSurface->h; SDL_Rect dest = { renderQuad.x + (renderQuad.w - textWidth)/2, renderQuad.y + (renderQuad.h - textHeight)/2, textWidth, textHeight }; SDL_FreeSurface(textSurface); SDL_RenderCopy(renderer, textTexture, nullptr, &dest); SDL_DestroyTexture(textTexture); } string wholeTextFile(const vector<string>& lines) { string result; for (auto &line : lines) result += line + "\n"; return result; } bool isMouseInRect(int mouseX, int mouseY, const SDL_Rect& rect) { return (mouseX >= rect.x && mouseX < rect.x + rect.w && mouseY >= rect.y && mouseY < rect.y + rect.h); } void renderMenu(SDL_Renderer* renderer, TTF_Font* font, Menu& menu, bool& isDark) { if (isDark) { drawRect(renderer, menu.fileMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.fileMenuButton, "File", DARK_MODE.defaultText); drawRect(renderer, menu.editMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.editMenuButton, "Edit", DARK_MODE.defaultText); drawRect(renderer, menu.themeMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.themeMenuButton, "Theme", DARK_MODE.defaultText); drawRect(renderer, menu.dNcMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.dNcMenuButton, "Deb & Com", DARK_MODE.defaultText); drawRect(renderer, menu.runMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.runMenuButton, "Run", DARK_MODE.defaultText); drawRect(renderer, menu.saveMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.saveMenuButton, "Save", DARK_MODE.defaultText); if (menu.fileOpenMenu) { drawRect(renderer, menu.dropRectFile, DARK_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemProjectNew, "Project New", DARK_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemProjectSave, "Project Save", DARK_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemExit, "EXIT", DARK_MODE.defaultText); } else if (menu.editOpenMenu) { drawRect(renderer, menu.dropRectEdit, DARK_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemUndo, "Undo", DARK_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemRedo, "Redo", DARK_MODE.defaultText); } else if (menu.themeOpenMenu) { drawRect(renderer, menu.dropRectTheme, DARK_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemDark, "Dark Mode", DARK_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemLight, "Light Mode", DARK_MODE.defaultText); } } else { drawRect(renderer, menu.fileMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.fileMenuButton, "File", LIGHT_MODE.defaultText); drawRect(renderer, menu.editMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.editMenuButton, "Edit", LIGHT_MODE.defaultText); drawRect(renderer, menu.themeMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.themeMenuButton, "Theme", LIGHT_MODE.defaultText); drawRect(renderer, menu.dNcMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.dNcMenuButton, "Deb & Com", LIGHT_MODE.defaultText); drawRect(renderer, menu.runMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.runMenuButton, "Run", LIGHT_MODE.defaultText); drawRect(renderer, menu.saveMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.saveMenuButton, "Save", LIGHT_MODE.defaultText); if (menu.fileOpenMenu) { drawRect(renderer, menu.dropRectFile, LIGHT_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemProjectNew, "Project New", LIGHT_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemProjectSave, "Project Save", LIGHT_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemExit, "EXIT", LIGHT_MODE.defaultText); } else if (menu.editOpenMenu) { drawRect(renderer, menu.dropRectEdit, LIGHT_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemUndo, "Undo", LIGHT_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemRedo, "Redo", LIGHT_MODE.defaultText); } else if (menu.themeOpenMenu) { drawRect(renderer, menu.dropRectTheme, LIGHT_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemDark, "Dark Mode", LIGHT_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemLight, "Light Mode", LIGHT_MODE.defaultText); } } } void drawRect(SDL_Renderer* renderer, const SDL_Rect& rect, SDL_Color color) { SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); SDL_RenderFillRect(renderer, &rect); } void saveFile(const string& fileName, const vector<string>& lines) { ofstream file(fileName); if (file.good()) { for (auto &line : lines) file << line << "\n"; cout << "[INFO] File saved.\n"; } else { cout << "[ERROR] Could not open file for saving: " << fileName << "\n"; return; } file.close(); } void loadFile(const string& fileName, vector<string>& lines, int& i) { ifstream file(fileName); string line; if (file.good()) { lines.clear(); while (getline(file, line)) { lines.push_back(line); i++; } } else { cout << "[INFO] Could not open file: " << fileName << ". Starting with an empty buffer.\n"; return; } file.close(); } void treeMap(SDL_Renderer* renderer, bool& isDark, TTF_Font *font, const vector<string>& filenames) { SDL_Rect projectText = {0, 0, 246, 60}; if (isDark) { drawRect(renderer, {0, 0, 246, 700}, DARK_MODE.treeMap); makeTextInRectangle(renderer, font, projectText, "Projects", DARK_MODE.defaultText); if (filenames.empty()) { SDL_Rect error = {10, 80, 249, 40}; makeTextInRectangle(renderer, font, error, "No projects here", DARK_MODE.defaultText); return; } int i = 0; for (auto &name : filenames) { SDL_Rect fileRect = {10, 80 + 40 * i, 220, 20}; i++; makeTextInRectangle(renderer, font, fileRect, name, DARK_MODE.defaultText); } } else { drawRect(renderer, {0, 0, 246, 700}, LIGHT_MODE.treeMap); makeTextInRectangle(renderer, font, projectText, "Projects", LIGHT_MODE.defaultText); if (filenames.empty()) { SDL_Rect error = {10, 80, 249, 40}; makeTextInRectangle(renderer, font, error, "No projects here", LIGHT_MODE.defaultText); return; } int i = 0; for (auto &name : filenames) { SDL_Rect fileRect = {10, 80 + 40 * i, 220, 20}; i++; makeTextInRectangle(renderer, font, fileRect, name, LIGHT_MODE.defaultText); } } } bool compile(const string &filename, string &errorOutput) { string compileCommand = "g++ -x c++ \"" + filename + "\" -o output 2>&1"; FILE* pipe = _popen(compileCommand.c_str(), "r"); if (!pipe) return false; char buffer[128]; string result; while (fgets(buffer, sizeof(buffer), pipe) != nullptr) result += buffer; int exitCode = _pclose(pipe); if (exitCode != 0) { errorOutput = result; return false; } errorOutput = ""; return true; } void runInAnotherWindow(const string &filename) { string runCommand = "start cmd /c \"" + filename + " & pause\""; system(runCommand.c_str()); } string newProject(SDL_Renderer *renderer, TTF_Font *font) { SDL_Rect done = {575, 340, 150, 40}; SDL_Color textColor = {0, 0, 0, 255}; SDL_Event e; string filename = " "; bool isDone = false; while (!isDone) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_MOUSEBUTTONDOWN) { int mx = e.button.x; int my = e.button.y; if (isMouseInRect(mx, my, done)) isDone = true; } else if (e.type == SDL_KEYDOWN) { if (e.key.keysym.sym == SDLK_BACKSPACE && !filename.empty()) filename.pop_back(); } else if (e.type == SDL_TEXTINPUT) { filename += e.text.text; } } drawRect(renderer, {500, 200, 300, 200}, {150, 150, 150, 255}); SDL_Surface *textSurface = TTF_RenderText_Blended(font, filename.c_str(), textColor); SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); SDL_Rect renderQuad = {520, 220, textSurface->w, textSurface->h}; SDL_FreeSurface(textSurface); SDL_RenderCopy(renderer, textTexture, nullptr, &renderQuad); SDL_DestroyTexture(textTexture); drawRect(renderer, done, {255, 255, 255, 255}); makeTextInRectangle(renderer, font, done, "Done", {0, 0, 0, 255}); SDL_RenderPresent(renderer); } filename.erase(filename.begin()); return filename + ".cpp"; } vector<string> getFilesInDirectory(const string& directoryPath) { vector<string> files; WIN32_FIND_DATA findFileData; HANDLE hFind = FindFirstFile((directoryPath + "\\*").c_str(), &findFileData); if (hFind == INVALID_HANDLE_VALUE) { cerr << "Failed to open directory!" << endl; return files; } do { if (!(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) files.push_back(findFileData.cFileName); } while (FindNextFile(hFind, &findFileData) != 0); FindClose(hFind); return files; } // توابع انتخاب، کپی، پیست و بریدن متن void handleTextSelection(SDL_Event& event, vector<string>& lines, int& selectionStartX, int& selectionEndX, int& selectionEndY, int& selectionStartY, int& cursorX, int& cursorY, bool& shiftHeld) { // خالی، در این نسخه لازم نبود } void copySelectedText(const vector<string>& lines, int& selectionStartX, int& selectionEndX, int& selectionEndY, int& selectionStartY, int& cursorX, int& cursorY, string& clipboard) { if (selectionStartX == -1 || selectionEndX == -1) return; int startY = min(selectionStartY, selectionEndY); int endY = max(selectionStartY, selectionEndY); int startX = (startY == selectionStartY) ? selectionStartX : selectionEndX; int endX = (endY == selectionEndY) ? selectionEndX : selectionStartX; clipboard.clear(); for (int y = startY; y <= endY; y++) { int lineStart = (y == startY) ? startX : 0; int lineEnd = (y == endY) ? endX : (int)lines[y].length(); if (lineStart < (int)lines[y].length() && lineEnd <= (int)lines[y].length()) { clipboard += lines[y].substr(lineStart, lineEnd - lineStart); if (y != endY) clipboard += "\n"; } } } void pasteText(vector<string>& lines, int& cursorX, int& cursorY, string& clipboard) { if (clipboard.empty()) return; size_t newlinePos = clipboard.find('\n'); if (newlinePos == string::npos) { lines[cursorY].insert(cursorX, clipboard); cursorX += (int)clipboard.length(); } else { string firstLine = clipboard.substr(0, newlinePos); lines[cursorY].insert(cursorX, firstLine); cursorX = (int)firstLine.length(); string remainingText = clipboard.substr(newlinePos + 1); size_t nextNewlinePos; while ((nextNewlinePos = remainingText.find('\n')) != string::npos) { lines.insert(lines.begin() + cursorY + 1, remainingText.substr(0, nextNewlinePos)); cursorY++; remainingText = remainingText.substr(nextNewlinePos + 1); } if (!remainingText.empty()) { lines.insert(lines.begin() + cursorY + 1, remainingText); cursorY++; cursorX = (int)remainingText.length(); } } } void cutSelectedText(vector<string>& lines, int& selectionStartX, int& selectionEndX, int& selectionEndY, int& selectionStartY, int& cursorX, int& cursorY, string& clipboard) { copySelectedText(lines, selectionStartX, selectionEndX, selectionEndY, selectionStartY, cursorX, cursorY, clipboard); if (selectionStartX != -1 && selectionEndX != -1) { int startY = min(selectionStartY, selectionEndY); int endY = max(selectionStartY, selectionEndY); int startX = (startY == selectionStartY) ? selectionStartX : selectionEndX; int endX = (endY == selectionEndY) ? selectionEndX : selectionStartX; if (startY == endY) { lines[startY].erase(startX, endX - startX); } else { lines[startY].erase(startX); lines[endY].erase(0, endX); lines[startY] += lines[endY]; lines.erase(lines.begin() + startY + 1, lines.begin() + endY + 1); } } } void saveAsOption(SDL_Renderer* renderer, TTF_Font* font, const vector<string>& lines) { SDL_Rect done = {575, 340, 150, 40}; SDL_Color textColor = {0, 0, 0, 255}; SDL_Event e; string filename = " "; bool isDone = false; while (!isDone) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_MOUSEBUTTONDOWN) { int mx = e.button.x; int my = e.button.y; if (isMouseInRect(mx, my, done)) isDone = true; } else if (e.type == SDL_KEYDOWN) { if (e.key.keysym.sym == SDLK_BACKSPACE && !filename.empty()) filename.pop_back(); } else if (e.type == SDL_TEXTINPUT) { filename += e.text.text; } } drawRect(renderer, {500, 200, 300, 200}, {150, 150, 150, 255}); SDL_Surface *textSurface = TTF_RenderText_Blended(font, filename.c_str(), textColor); SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); SDL_Rect renderQuad = {520, 220, textSurface->w, textSurface->h}; SDL_FreeSurface(textSurface); SDL_RenderCopy(renderer, textTexture, nullptr, &renderQuad); SDL_DestroyTexture(textTexture); drawRect(renderer, done, {255, 255, 255, 255}); makeTextInRectangle(renderer, font, done, "Done", {0, 0, 0, 255}); SDL_RenderPresent(renderer); } filename.erase(filename.begin()); filename += ".cpp"; saveFile(filename, lines); } void thereIsError(SDL_Renderer* renderer, TTF_Font* font) { SDL_Rect text = {550, 330, 200, 40}; drawRect(renderer, {500, 300, 300, 100}, {200, 200, 200, 255}); makeTextInRectangle(renderer, font, text, "There is an Error", {255, 0, 0, 255}); }
تعریف متغیرهای سراسری
در ابتدای فایل مشاهده میکنید که متغیرهای:
bool isDark; bool showErrorPopup; string errorPopupText; bool haveError; string currentProjectPath; int errorScrollOffset;
تعریف و مقداردهی اولیه شدهاند. تمام ارجاعات قبلی در بدنهٔ برنامه الان به همین متغیرهای سراسری وصل شدهاند.
فونتها و BMP
اگر فونت یا تصویر آیکون در مسیر مشخصشده یافت نشد، باید نام فایل یا مسیر را اصلاح یا حذف کنید.
مکانیزم منو
منوها (File، Edit، Theme و غیره) همه در یک فایل هستند و بسته به کلیک، مقادیر مربوط به منوها را تغییر میدهند (menu.fileOpenMenu
و …).
Compile/Run
برای آزمایش در ویندوز (MinGW)، میتوانید فایل را با دستوری مانند زیر کامپایل کنید:
g++ -std=c++17 -lSDL2 -lSDL2_ttf -lSDL2_gfx -mwindows main.cpp -o editor
(بسته به نصب بودن کتابخانهها، ممکن است نیاز به مسیرهای اضافی داشته باشید.)
با این تغییرات، ارورهای مربوط به «تعریفنشده» برطرف میشوند و برنامه باید کامپایل و اجرا شود. موفق باشید!
// main.cpp // یک IDE سبک با قابلیت Undo/Redo، Cut/Copy/Paste، AutoComplete و ... // همه کد در یک فایل قرار دارد. #include <SDL2/SDL.h> #include <SDL2/SDL_ttf.h> #include <SDL2/SDL2_gfx.h> #include <regex> #include <string> #include <iostream> #include <vector> #include <cstring> #include <fstream> #include <windows.h> #include <commdlg.h> #include <cstdio> using namespace std;
// ------------------- Constants ------------------- const int SCREEN_WIDTH = 1300; const int SCREEN_HEIGHT = 700; const Uint32 CURSOR_BLINK_INTERVAL = 500; // میلیثانیه // ------------------- End Constants -------------------
// ------------------- Theme Colors ------------------- struct ThemeColors { SDL_Color background; SDL_Color menuBarItems; SDL_Color menuDropRect; SDL_Color treeMap; SDL_Color defaultText; };
const ThemeColors DARK_MODE = { {101, 30, 177, 200}, {70, 70, 70, 255}, {80, 80, 80, 255}, {50, 50, 50, 255}, {255, 255, 255, 255} };
const ThemeColors LIGHT_MODE = { {204, 241, 255, 180}, {180, 180, 180, 255}, {200, 200, 200, 255}, {150, 150, 150, 255}, {0, 0, 0, 255} }; // ------------------- End Theme Colors -------------------
// ------------------- Global Variables ------------------- bool haveError = false; // وضعیت خطای کتابخانهها bool isDark = false; // تم تاریک یا روشن string currentProjectPath = ""; // مسیر پروژه فعلی (در صورت بارگذاری از sidebar) string autoCompleteSuggestion = ""; // متن پیشنهادی تکمیل خودکار string errorPopupText = ""; // متن خطاهای کامپایل/دیباگ bool showErrorPopup = false; int errorScrollOffset = 0; // ------------------- End Global Variables -------------------
// ------------------- ساختار Undo/Redo ------------------- struct EditorState { vector<string> lines; int currentLine; int cursorPos; };
vector<EditorState> undoStack; vector<EditorState> redoStack; const int UNDO_LIMIT = 7;
void recordState(const vector<string>& lines, int currentLine, int cursorPos) { // اگر حالت جدید تغییر یافته باشد، در undoStack ثبت و redoStack پاک میشود if (!undoStack.empty()) { const EditorState &last = undoStack.back(); if (last.lines == lines && last.currentLine == currentLine && last.cursorPos == cursorPos) return; } EditorState state = { lines, currentLine, cursorPos }; undoStack.push_back(state); if (undoStack.size() > UNDO_LIMIT) undoStack.erase(undoStack.begin()); redoStack.clear(); }
void performUndo(vector<string>& lines, int ¤tLine, int &cursorPos) { if (!undoStack.empty()) { EditorState state = undoStack.back(); undoStack.pop_back(); // حالت فعلی را در redoStack ذخیره میکنیم EditorState curState = { lines, currentLine, cursorPos }; redoStack.push_back(curState); lines = state.lines; currentLine = state.currentLine; cursorPos = state.cursorPos; } }
void performRedo(vector<string>& lines, int ¤tLine, int &cursorPos) { if (!redoStack.empty()) { EditorState state = redoStack.back(); redoStack.pop_back(); recordState(lines, currentLine, cursorPos); lines = state.lines; currentLine = state.currentLine; cursorPos = state.cursorPos; } } // ------------------- End Undo/Redo -------------------
// ------------------- ساختار منو ------------------- struct Menu { bool fileOpenMenu = false; bool editOpenMenu = false; bool themeOpenMenu = false; // File menu SDL_Rect fileMenuButton = {246, 0, 155, 35}; SDL_Rect dropRectFile = {246, 35, 170, 150}; SDL_Rect itemProjectNew = {246, 35, 170, 50}; SDL_Rect itemProjectSave = {246, 85, 170, 50}; SDL_Rect itemExit = {246, 135, 170, 50}; // Edit menu SDL_Rect editMenuButton = {401, 0, 159, 35}; SDL_Rect dropRectEdit = {421, 35, 120, 100}; SDL_Rect itemUndo = {421, 35, 120, 50}; SDL_Rect itemRedo = {421, 85, 120, 50}; // Theme menu SDL_Rect themeMenuButton = {560, 0, 175, 35}; SDL_Rect dropRectTheme = {560, 35, 175, 100}; SDL_Rect itemLight = {560, 35, 175, 50}; SDL_Rect itemDark = {560, 85, 175, 50}; // Debug & Compile button SDL_Rect dNcMenuButton = {735, 0, 215, 35}; // Run button SDL_Rect runMenuButton = {950, 0, 175, 35}; // Save button SDL_Rect saveMenuButton = {1125, 0, 175, 35}; }; // ------------------- End ساختار منو -------------------
// ------------------- Prototype Functions ------------------- // (توجه کنید که همه توابع در این فایل پیادهسازی شدهاند.) void ensureLastLineVisible(int, int&, int, int, int); void librariesError(SDL_Renderer*, const vector<string>&, TTF_Font*); void makeTextInRectangle(SDL_Renderer*, TTF_Font*, SDL_Rect&, const string&, SDL_Color); string wholeTextFile(const vector<string>&); bool isMouseInRect(int, int, const SDL_Rect&); void renderMenu(SDL_Renderer*, TTF_Font*, Menu&, bool&); // دقت کنید: پارامتر isDark به صورت مرجع است void drawRect(SDL_Renderer*, const SDL_Rect&, SDL_Color); void saveFile(const string&, const vector<string>&); void loadFile(const string&, vector<string>&, int&); void treeMap(SDL_Renderer*, bool&, TTF_Font*, const vector<string>&); // isDark به صورت مرجع bool compile(const string&, string&); void runInAnotherWindow(const string &); string newProject(SDL_Renderer*, TTF_Font*); vector<string> getFilesInDirectory(const string&); void handleTextSelection(SDL_Event&, vector<string>&, int&, int&, int&, int&, int&, int&, bool&); void copySelectedText(const vector<string>&, int&, int&, int&, int&, int&, int&, string&); void pasteText(vector<string>&, int&, int&, string&); void cutSelectedText(vector<string>&, int&, int&, int&, int&, int&, int&, string&); void saveAsOption(SDL_Renderer*, TTF_Font*, const vector<string>&); void thereIsError(SDL_Renderer*, TTF_Font*); string showSaveFileDialog(); void updateAutoCompleteSuggestion(const string&, int, string&); void renderAutoComplete(SDL_Renderer*, TTF_Font*, const string&, int, int); // ------------------- End Prototype Functions -------------------
// ------------------- Windows API: Save Dialog ------------------- string showSaveFileDialog() { OPENFILENAME ofn; char fileName[MAX_PATH] = ""; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.lpstrFilter = "C++ Files (.cpp)\0.cpp\0All Files (.)\0*.*\0"; ofn.lpstrFile = fileName; ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_EXPLORER | OFN_OVERWRITEPROMPT; ofn.lpstrDefExt = "cpp"; if (GetSaveFileName(&ofn)) return string(fileName); return ""; } // ------------------- End Save Dialog -------------------
// ------------------- AutoComplete ------------------- // لیست کلمات پیشنهادی vector<string> autoCompleteKeywords = {"switch", "int", "float", "main", "scanf", "printf", "strlen", "else", "while"};
void updateAutoCompleteSuggestion(const string ¤tLineText, int cursorPos, string &suggestion) { suggestion = ""; // استخراج کلمه فعلی از ابتدای کلمه تا موقعیت کرسر int start = cursorPos - 1; while(start >= 0 && (isalnum(currentLineText[start]) || currentLineText[start]=='_')) start--; start++; string currentWord = currentLineText.substr(start, cursorPos - start);
// اگر کاراکترهای باز مانند ( [ { وارد شده باشد
if (!currentWord.empty()) {
char lastChar = currentWord.back();
if (lastChar == '(') { suggestion = ")"; return; }
if (lastChar == '[') { suggestion = "]"; return; }
if (lastChar == '{') { suggestion = "}"; return; }
}
// اگر تنها حرف "i" وارد شده باشد، پیشنهاد if بده
if (currentWord == "i") { suggestion = "f"; return; }
// بررسی کلمات کلیدی
if (currentWord.length() >= 2) {
for (auto &kw : autoCompleteKeywords) {
if (kw.substr(0, currentWord.length()) == currentWord && kw != currentWord) {
suggestion = kw.substr(currentWord.length());
return;
}
}
}
}
void renderAutoComplete(SDL_Renderer renderer, TTF_Font font, const string &suggestion, int cursorX, int cursorY) { if (suggestion.empty()) return; SDL_Color faded = { 128, 128, 128, 255 }; SDL_Surface surface = TTF_RenderText_Blended(font, suggestion.c_str(), faded); SDL_Texture texture = SDL_CreateTextureFromSurface(renderer, surface); SDL_Rect dest = { cursorX, cursorY, surface->w, surface->h }; SDL_RenderCopy(renderer, texture, NULL, &dest); SDL_FreeSurface(surface); SDL_DestroyTexture(texture); } // ------------------- End AutoComplete -------------------
// ------------------- Main ------------------- int main(int argc, char* argv[]) { // راهاندازی SDL if (SDL_Init(SDL_INIT_VIDEO) < 0) { cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl; return -1; } if (TTF_Init() == -1) { cerr << "TTF could not initialize! TTF_Error: " << TTF_GetError() << endl; SDL_Quit(); return -1; }
SDL_Window* window = SDL_CreateWindow("SDL Text Editor IDE",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (!window) {
cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << endl;
TTF_Quit();
SDL_Quit();
return -1;
}
// تنظیم آیکون پنجره (فرض کنید فایل openai_logo.bmp در همان پوشه موجود است)
SDL_Surface* iconSurface = SDL_LoadBMP("openai_logo.bmp");
if (iconSurface)
SDL_SetWindowIcon(window, iconSurface);
SDL_FreeSurface(iconSurface);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer) {
cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << endl;
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return -1;
}
// بارگذاری فونتها (تنظیم مسیر مناسب به فونتهای مورد نظر)
TTF_Font* font1 = TTF_OpenFont("C:\\Windows\\Fonts\\consola.ttf", 19);
if (!font1) {
cerr << "Failed to load font1! TTF_Error: " << TTF_GetError() << endl;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return -1;
}
TTF_Font* font2 = TTF_OpenFont("C:\\Users\\BitBin.ir\\Downloads\\Telegram Desktop\\zilla-slab\\ZillaSlab-Bold.otf", 19);
if (!font2) {
cerr << "Failed to load font2! TTF_Error: " << TTF_GetError() << endl;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return -1;
}
TTF_Font* font = font1;
TTF_Font* fontMenuBar = TTF_OpenFont("C:\\Windows\\Fonts\\Arial.ttf", 23);
if (!fontMenuBar) {
cerr << "Failed to load fontMenuBar! TTF_Error: " << TTF_GetError() << endl;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return -1;
}
// دریافت فایلهای موجود در پوشه (همه فرمتها)
string directoryPath = "."; // فرض بر این است که کد در پوشه جاری قرار دارد
vector<string> filenames = getFilesInDirectory(directoryPath);
bool isSaved = false;
vector<string> lines = { "" };
int currentLine = 0;
int cursorPos = 0;
int scrollOffset = 0;
const int LINE_HEIGHT = TTF_FontHeight(font);
Uint32 lastCursorToggle = SDL_GetTicks();
bool cursorVisible = true;
bool ctrlDown = false, shiftHeld = false;
string clipboard = "";
autoCompleteSuggestion = "";
// ساختار منو
Menu menu;
// ثبت حالت اولیه برای undo/redo
recordState(lines, currentLine, cursorPos);
SDL_Event e;
bool quit = false;
while (!quit) {
// مدیریت چشمک زدن کرسر
Uint32 currentTime = SDL_GetTicks();
if (currentTime > lastCursorToggle + CURSOR_BLINK_INTERVAL) {
cursorVisible = !cursorVisible;
lastCursorToggle = currentTime;
}
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT)
quit = true;
// ----------------------- مدیریت کلیکها (mouse events) -----------------------
else if (e.type == SDL_MOUSEBUTTONDOWN) {
int mx = e.button.x;
int my = e.button.y;
// کلیک در بخش Sidebar (treeMap) جهت بازکردن پروژه
if (mx < 246) {
int idx = (my - 80) / 40;
if (idx >= 0 && idx < filenames.size()) {
lines.clear();
int dummy = 0;
loadFile(filenames[idx], lines, dummy);
currentLine = 0;
cursorPos = 0;
currentProjectPath = directoryPath + "\\" + filenames[idx];
recordState(lines, currentLine, cursorPos);
}
}
// منوی File
if (!menu.fileOpenMenu && isMouseInRect(mx, my, menu.fileMenuButton))
menu.fileOpenMenu = true;
else if (menu.fileOpenMenu) {
if (isMouseInRect(mx, my, menu.itemProjectNew)) {
isSaved = false;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
lines = { "" };
currentLine = 0;
cursorPos = 0;
currentProjectPath = "";
menu.fileOpenMenu = false;
recordState(lines, currentLine, cursorPos);
}
else if (isMouseInRect(mx, my, menu.itemProjectSave)) {
if (!currentProjectPath.empty())
saveFile(currentProjectPath, lines);
else {
string path = showSaveFileDialog();
if (!path.empty()) {
currentProjectPath = path;
saveFile(currentProjectPath, lines);
filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1));
}
}
menu.fileOpenMenu = false;
}
else if (isMouseInRect(mx, my, menu.itemExit)) {
quit = true;
menu.fileOpenMenu = false;
}
else if (!isMouseInRect(mx, my, menu.dropRectFile))
menu.fileOpenMenu = false;
}
// منوی Edit
if (!menu.editOpenMenu && isMouseInRect(mx, my, menu.editMenuButton))
menu.editOpenMenu = true;
else if (menu.editOpenMenu) {
if (isMouseInRect(mx, my, menu.itemUndo)) {
performUndo(lines, currentLine, cursorPos);
menu.editOpenMenu = false;
}
else if (isMouseInRect(mx, my, menu.itemRedo)) {
performRedo(lines, currentLine, cursorPos);
menu.editOpenMenu = false;
}
else if (!isMouseInRect(mx, my, menu.dropRectEdit))
menu.editOpenMenu = false;
}
// منوی Theme
if (!menu.themeOpenMenu && isMouseInRect(mx, my, menu.themeMenuButton))
menu.themeOpenMenu = true;
else if (menu.themeOpenMenu) {
if (isMouseInRect(mx, my, menu.itemLight)) {
isDark = false;
menu.themeOpenMenu = false;
}
else if (isMouseInRect(mx, my, menu.itemDark)) {
isDark = true;
menu.themeOpenMenu = false;
}
else if (!isMouseInRect(mx, my, menu.dropRectTheme))
menu.themeOpenMenu = false;
}
// Debug & Compile
if (isMouseInRect(mx, my, menu.dNcMenuButton)) {
if (haveError) {
thereIsError(renderer, fontMenuBar);
} else {
if (!currentProjectPath.empty())
saveFile(currentProjectPath, lines);
else {
string path = newProject(renderer, fontMenuBar);
currentProjectPath = path;
filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1));
saveFile(currentProjectPath, lines);
}
string compileErrors;
if (compile(currentProjectPath, compileErrors))
cout << "[INFO] Compilation successful!\n";
else {
showErrorPopup = true;
errorPopupText = compileErrors;
}
}
}
// Run
if (isMouseInRect(mx, my, menu.runMenuButton)) {
if (haveError) {
thereIsError(renderer, fontMenuBar);
} else {
if (!currentProjectPath.empty())
saveFile(currentProjectPath, lines);
else {
string path = newProject(renderer, fontMenuBar);
currentProjectPath = path;
filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1));
saveFile(currentProjectPath, lines);
}
string compileErrors;
if (compile(currentProjectPath, compileErrors))
runInAnotherWindow("output");
else {
showErrorPopup = true;
errorPopupText = compileErrors;
}
}
}
// Save (دکمه گرافیکی)
if (isMouseInRect(mx, my, menu.saveMenuButton)) {
if (!currentProjectPath.empty())
saveFile(currentProjectPath, lines);
else {
string path = showSaveFileDialog();
if (!path.empty()) {
currentProjectPath = path;
saveFile(currentProjectPath, lines);
filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1));
}
}
}
} // پایان MOUSEBUTTONDOWN
// ----------------------- چرخش موس (scroll) -----------------------
else if (e.type == SDL_MOUSEWHEEL) {
if (e.wheel.y > 0)
scrollOffset = max(0, scrollOffset - LINE_HEIGHT);
else if (e.wheel.y < 0)
scrollOffset += LINE_HEIGHT;
}
// ----------------------- رویدادهای کیبورد -----------------------
else if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.sym == SDLK_LCTRL || e.key.keysym.sym == SDLK_RCTRL)
ctrlDown = true;
if (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT)
shiftHeld = true;
// Undo/Redo با Ctrl+Z / Ctrl+Y
if (e.key.keysym.mod & KMOD_CTRL) {
if (e.key.keysym.sym == SDLK_z) {
performUndo(lines, currentLine, cursorPos);
continue;
}
if (e.key.keysym.sym == SDLK_y) {
performRedo(lines, currentLine, cursorPos);
continue;
}
// Copy (Ctrl+C)
if (e.key.keysym.sym == SDLK_c) {
copySelectedText(lines, cursorPos, cursorPos, currentLine, currentLine, cursorPos, currentLine, clipboard);
continue;
}
// Paste (Ctrl+V)
if (e.key.keysym.sym == SDLK_v) {
pasteText(lines, cursorPos, currentLine, clipboard);
recordState(lines, currentLine, cursorPos);
continue;
}
// Cut (Ctrl+X)
if (e.key.keysym.sym == SDLK_x) {
cutSelectedText(lines, cursorPos, cursorPos, currentLine, currentLine, cursorPos, currentLine, clipboard);
recordState(lines, currentLine, cursorPos);
continue;
}
// Save (Ctrl+S)
if (e.key.keysym.sym == SDLK_s) {
string path = showSaveFileDialog();
if (!path.empty()) {
currentProjectPath = path;
saveFile(currentProjectPath, lines);
filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1));
}
continue;
}
}
// اگر Tab فشرده شد و پیشنهادی موجود است (AutoComplete)
if (e.key.keysym.sym == SDLK_TAB && !autoCompleteSuggestion.empty()) {
lines[currentLine].insert(cursorPos, autoCompleteSuggestion);
cursorPos += autoCompleteSuggestion.length();
autoCompleteSuggestion = "";
recordState(lines, currentLine, cursorPos);
continue;
}
// Backspace
if (e.key.keysym.sym == SDLK_BACKSPACE) {
if (cursorPos > 0 && cursorPos <= lines[currentLine].size()) {
lines[currentLine].erase(cursorPos - 1, 1);
cursorPos--;
}
else if (currentLine > 0) {
cursorPos = lines[currentLine - 1].size();
lines[currentLine - 1] += lines[currentLine];
lines.erase(lines.begin() + currentLine);
currentLine--;
}
if (lines.empty()) { lines.push_back(""); currentLine = 0; cursorPos = 0; }
recordState(lines, currentLine, cursorPos);
}
// Enter
else if (e.key.keysym.sym == SDLK_RETURN) {
string remainder = lines[currentLine].substr(cursorPos);
lines[currentLine] = lines[currentLine].substr(0, cursorPos);
lines.insert(lines.begin() + currentLine + 1, remainder);
currentLine++;
cursorPos = 0;
ensureLastLineVisible(currentLine, scrollOffset, SCREEN_HEIGHT, LINE_HEIGHT, lines.size());
recordState(lines, currentLine, cursorPos);
}
// Tab (برای درج فاصله)
else if (e.key.keysym.sym == SDLK_TAB) {
lines[currentLine].insert(cursorPos, " ");
cursorPos += 4;
recordState(lines, currentLine, cursorPos);
}
// جهتیابی کرسر
else if (e.key.keysym.sym == SDLK_LEFT) {
if (cursorPos > 0)
cursorPos--;
else if (currentLine > 0) {
currentLine--;
cursorPos = lines[currentLine].size();
}
}
else if (e.key.keysym.sym == SDLK_RIGHT) {
if (cursorPos < lines[currentLine].size())
cursorPos++;
else if (currentLine < lines.size() - 1) {
currentLine++;
cursorPos = 0;
}
}
else if (e.key.keysym.sym == SDLK_UP) {
if (currentLine > 0) {
currentLine--;
cursorPos = min(cursorPos, (int)lines[currentLine].size());
ensureLastLineVisible(currentLine, scrollOffset, SCREEN_HEIGHT, LINE_HEIGHT, lines.size());
}
}
else if (e.key.keysym.sym == SDLK_DOWN) {
if (currentLine < lines.size() - 1) {
currentLine++;
cursorPos = min(cursorPos, (int)lines[currentLine].size());
ensureLastLineVisible(currentLine, scrollOffset, SCREEN_HEIGHT, LINE_HEIGHT, lines.size());
}
}
}
else if (e.type == SDL_KEYUP) {
if (e.key.keysym.sym == SDLK_LCTRL || e.key.keysym.sym == SDLK_RCTRL)
ctrlDown = false;
if (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT)
shiftHeld = false;
}
else if (e.type == SDL_TEXTINPUT) {
lines[currentLine].insert(cursorPos, e.text.text);
cursorPos += strlen(e.text.text);
updateAutoCompleteSuggestion(lines[currentLine], cursorPos, autoCompleteSuggestion);
ensureLastLineVisible(currentLine, scrollOffset, SCREEN_HEIGHT, LINE_HEIGHT, lines.size());
recordState(lines, currentLine, cursorPos);
}
} // پایان Event Loop
// ----------------------- Rendering -----------------------
SDL_Color textColor;
if (isDark) {
SDL_SetRenderDrawColor(renderer, DARK_MODE.background.r, DARK_MODE.background.g, DARK_MODE.background.b, DARK_MODE.background.a);
textColor = DARK_MODE.defaultText;
} else {
SDL_SetRenderDrawColor(renderer, LIGHT_MODE.background.r, LIGHT_MODE.background.g, LIGHT_MODE.background.b, LIGHT_MODE.background.a);
textColor = LIGHT_MODE.defaultText;
}
SDL_RenderClear(renderer);
int y = -scrollOffset;
for (size_t j = 0; j < lines.size(); ++j) {
if (y + LINE_HEIGHT > 0 && y < SCREEN_HEIGHT) {
string lineToRender = lines[j].empty() ? " " : lines[j];
SDL_Surface* wordSurface = TTF_RenderText_Blended(font, lineToRender.c_str(), textColor);
SDL_Texture* wordTexture = SDL_CreateTextureFromSurface(renderer, wordSurface);
SDL_Rect renderQuad = {255, y + 44, wordSurface->w, wordSurface->h};
SDL_RenderCopy(renderer, wordTexture, nullptr, &renderQuad);
SDL_FreeSurface(wordSurface);
SDL_DestroyTexture(wordTexture);
if ((int)j == currentLine && cursorVisible) {
int cursorX = 0;
if (cursorPos > 0) {
TTF_SizeText(font, lines[j].substr(0, cursorPos).c_str(), &cursorX, nullptr);
}
cursorX += 255;
SDL_SetRenderDrawColor(renderer, textColor.r, textColor.g, textColor.b, textColor.a);
SDL_RenderDrawLine(renderer, cursorX, y + 44, cursorX, y + LINE_HEIGHT + 44);
renderAutoComplete(renderer, font, autoCompleteSuggestion, cursorX, y + 44);
}
}
y += LINE_HEIGHT;
}
// نمایش پنجره پاپ آپ خطا (در صورت وجود)
if (showErrorPopup) {
SDL_Rect popup = {500, SCREEN_HEIGHT - 150, 600, 140};
drawRect(renderer, popup, {200, 200, 200, 255});
makeTextInRectangle(renderer, fontMenuBar, popup, errorPopupText, {255, 0, 0, 255});
}
librariesError(renderer, lines, fontMenuBar);
treeMap(renderer, isDark, fontMenuBar, filenames);
renderMenu(renderer, fontMenuBar, menu, isDark);
SDL_RenderPresent(renderer);
}
// تمیز کردن منابع
TTF_CloseFont(font);
TTF_CloseFont(fontMenuBar);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 0;
} // ------------------- End Main -------------------
// ------------------- Helper Functions -------------------
void ensureLastLineVisible(int currentLine, int &scrollOffset, int SCREEN_HEIGHT, int LINE_HEIGHT, int totalLines) { int cursorY = currentLine * LINE_HEIGHT - scrollOffset; if (cursorY < 0) scrollOffset = currentLine * LINE_HEIGHT; else if (cursorY + LINE_HEIGHT > SCREEN_HEIGHT) scrollOffset = (currentLine + 1) * LINE_HEIGHT - SCREEN_HEIGHT; int contentHeight = totalLines * LINE_HEIGHT; if (contentHeight > SCREEN_HEIGHT) scrollOffset = min(scrollOffset, contentHeight - SCREEN_HEIGHT); else scrollOffset = 0; }
void librariesError(SDL_Renderer* renderer, const vector<string>& lines, TTF_Font* font) { string wholeText = wholeTextFile(lines); regex bits("#include <bits/stdc++.h>"); regex iostream("#include <iostream>"); regex notIostream("std::(sqrt|pow|sin|cos|tan|abs|exp|log|log10|floor|ceil)"); regex cmath("#include <cmath>"); regex notCmath("std::(cout|cin|endl|getline)"); SDL_Rect error = {255, 660, 850, 40}; if (!regex_search(wholeText, iostream) && !regex_search(wholeText, bits) && regex_search(wholeText, notCmath)) { makeTextInRectangle(renderer, font, error, "Library Error ... , Include iostream or bits", {255, 0, 0, 255}); haveError = true; } else { haveError = false; } if (!regex_search(wholeText, cmath) && !regex_search(wholeText, bits) && regex_search(wholeText, notIostream)) { makeTextInRectangle(renderer, font, error, "Library Error ... , Include cmath or bits", {255, 0, 0, 255}); haveError = true; } else { haveError = false; } }
void makeTextInRectangle(SDL_Renderer* renderer, TTF_Font* font, SDL_Rect& renderQuad, const string& text, SDL_Color textColor) { SDL_Surface* textSurface = TTF_RenderText_Blended(font, text.c_str(), textColor); SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); int textWidth = textSurface->w; int textHeight = textSurface->h; SDL_Rect dest = {renderQuad.x + (renderQuad.w - textWidth) / 2, renderQuad.y + (renderQuad.h - textHeight) / 2, textWidth, textHeight}; SDL_FreeSurface(textSurface); SDL_RenderCopy(renderer, textTexture, nullptr, &dest); SDL_DestroyTexture(textTexture); }
string wholeTextFile(const vector<string>& lines) { string result = ""; for (auto &line : lines) result += line + "\n"; return result; }
bool isMouseInRect(int mouseX, int mouseY, const SDL_Rect& rect) { return (mouseX >= rect.x && mouseX < rect.x + rect.w && mouseY >= rect.y && mouseY < rect.y + rect.h); }
void renderMenu(SDL_Renderer* renderer, TTF_Font* font, Menu& menu, bool& isDark) { if (isDark) { drawRect(renderer, menu.fileMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.fileMenuButton, "File", DARK_MODE.defaultText); drawRect(renderer, menu.editMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.editMenuButton, "Edit", DARK_MODE.defaultText); drawRect(renderer, menu.themeMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.themeMenuButton, "Theme", DARK_MODE.defaultText); drawRect(renderer, menu.dNcMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.dNcMenuButton, "Deb & Com", DARK_MODE.defaultText); drawRect(renderer, menu.runMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.runMenuButton, "Run", DARK_MODE.defaultText); drawRect(renderer, menu.saveMenuButton, DARK_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.saveMenuButton, "Save", DARK_MODE.defaultText); if (menu.fileOpenMenu) { drawRect(renderer, menu.dropRectFile, DARK_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemProjectNew, "Project New", DARK_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemProjectSave, "Project Save", DARK_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemExit, "EXIT", DARK_MODE.defaultText); } else if (menu.editOpenMenu) { drawRect(renderer, menu.dropRectEdit, DARK_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemUndo, "Undo", DARK_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemRedo, "Redo", DARK_MODE.defaultText); } else if (menu.themeOpenMenu) { drawRect(renderer, menu.dropRectTheme, DARK_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemDark, "Dark Mode", DARK_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemLight, "Light Mode", DARK_MODE.defaultText); } } else { drawRect(renderer, menu.fileMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.fileMenuButton, "File", LIGHT_MODE.defaultText); drawRect(renderer, menu.editMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.editMenuButton, "Edit", LIGHT_MODE.defaultText); drawRect(renderer, menu.themeMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.themeMenuButton, "Theme", LIGHT_MODE.defaultText); drawRect(renderer, menu.dNcMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.dNcMenuButton, "Deb & Com", LIGHT_MODE.defaultText); drawRect(renderer, menu.runMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.runMenuButton, "Run", LIGHT_MODE.defaultText); drawRect(renderer, menu.saveMenuButton, LIGHT_MODE.menuBarItems); makeTextInRectangle(renderer, font, menu.saveMenuButton, "Save", LIGHT_MODE.defaultText); if (menu.fileOpenMenu) { drawRect(renderer, menu.dropRectFile, LIGHT_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemProjectNew, "Project New", LIGHT_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemProjectSave, "Project Save", LIGHT_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemExit, "EXIT", LIGHT_MODE.defaultText); } else if (menu.editOpenMenu) { drawRect(renderer, menu.dropRectEdit, LIGHT_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemUndo, "Undo", LIGHT_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemRedo, "Redo", LIGHT_MODE.defaultText); } else if (menu.themeOpenMenu) { drawRect(renderer, menu.dropRectTheme, LIGHT_MODE.menuDropRect); makeTextInRectangle(renderer, font, menu.itemDark, "Dark Mode", LIGHT_MODE.defaultText); makeTextInRectangle(renderer, font, menu.itemLight, "Light Mode", LIGHT_MODE.defaultText); } } }
void drawRect(SDL_Renderer* renderer, const SDL_Rect& rect, SDL_Color color) { SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); SDL_RenderFillRect(renderer, &rect); }
void saveFile(const string& fileName, const vector<string>& lines) { ofstream file(fileName); if (file.good()) { for (auto &line : lines) file << line << "\n"; cout << "[INFO] File saved.\n"; } else { cout << "[ERROR] Could not open file for saving: " << fileName << "\n"; return; } file.close(); }
void loadFile(const string& fileName, vector<string>& lines, int& i) { ifstream file(fileName); string line; if (file.good()) { lines.clear(); while (getline(file, line)) { lines.push_back(line); i++; } } else { cout << "[INFO] Could not open file: " << fileName << ". Starting with an empty buffer.\n"; return; } file.close(); }
void treeMap(SDL_Renderer* renderer, bool& isDark, TTF_Font *font, const vector<string>& filenames) { SDL_Rect projectText = {0, 0, 246, 60}; if (isDark) { drawRect(renderer, {0, 0, 246, 700}, DARK_MODE.treeMap); makeTextInRectangle(renderer, font, projectText, "Projects", DARK_MODE.defaultText); if (filenames.empty()) { SDL_Rect error = {10, 80, 249, 40}; makeTextInRectangle(renderer, font, error, "No projects here", DARK_MODE.defaultText); return; } int i = 0; for (auto &name : filenames) { SDL_Rect fileRect = {10, 80 + 40 * i, 220, 20}; i++; makeTextInRectangle(renderer, font, fileRect, name, DARK_MODE.defaultText); } } else { drawRect(renderer, {0, 0, 246, 700}, LIGHT_MODE.treeMap); makeTextInRectangle(renderer, font, projectText, "Projects", LIGHT_MODE.defaultText); if (filenames.empty()) { SDL_Rect error = {10, 80, 249, 40}; makeTextInRectangle(renderer, font, error, "No projects here", LIGHT_MODE.defaultText); return; } int i = 0; for (auto &name : filenames) { SDL_Rect fileRect = {10, 80 + 40 * i, 220, 20}; i++; makeTextInRectangle(renderer, font, fileRect, name, LIGHT_MODE.defaultText); } } }
bool compile(const string &filename, string &errorOutput) { string compileCommand = "g++ -x c++ "" + filename + "" -o output 2>&1"; FILE* pipe = _popen(compileCommand.c_str(), "r"); if (!pipe) return false; char buffer[128]; string result; while (fgets(buffer, sizeof(buffer), pipe) != nullptr) result += buffer; int exitCode = _pclose(pipe); if (exitCode != 0) { errorOutput = result; return false; } errorOutput = ""; return true; }
void runInAnotherWindow(const string &filename) { string runCommand = "start cmd /c "" + filename + " & pause""; system(runCommand.c_str()); }
string newProject(SDL_Renderer *renderer, TTF_Font *font) { SDL_Rect done = {575, 340, 150, 40}; SDL_Color textColor = {0, 0, 0, 255}; SDL_Event e; string filename = " "; bool isDone = false; while (!isDone) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_MOUSEBUTTONDOWN) { int mx = e.button.x; int my = e.button.y; if (isMouseInRect(mx, my, done)) isDone = true; } else if (e.type == SDL_KEYDOWN) { if (e.key.keysym.sym == SDLK_BACKSPACE && !filename.empty()) filename.pop_back(); } else if (e.type == SDL_TEXTINPUT) { filename += e.text.text; } } drawRect(renderer, {500, 200, 300, 200}, {150, 150, 150, 255}); SDL_Surface *textSurface = TTF_RenderText_Blended(font, filename.c_str(), textColor); SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); SDL_Rect renderQuad = {520, 220, textSurface->w, textSurface->h}; SDL_FreeSurface(textSurface); SDL_RenderCopy(renderer, textTexture, nullptr, &renderQuad); SDL_DestroyTexture(textTexture); drawRect(renderer, done, {255, 255, 255, 255}); makeTextInRectangle(renderer, font, done, "Done", {0, 0, 0, 255}); SDL_RenderPresent(renderer); } filename.erase(filename.begin()); return filename + ".cpp"; }
vector<string> getFilesInDirectory(const string& directoryPath) { vector<string> files; WIN32_FIND_DATA findFileData; HANDLE hFind = FindFirstFile((directoryPath + "\*").c_str(), &findFileData); if (hFind == INVALID_HANDLE_VALUE) { cerr << "Failed to open directory!" << endl; return files; } do { if (!(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) files.push_back(findFileData.cFileName); } while (FindNextFile(hFind, &findFileData) != 0); FindClose(hFind); return files; }
// توابع مدیریت انتخاب متن، کپی، جایگذاری و بریدن void handleTextSelection(SDL_Event& event, vector<string>& lines, int& selectionStartX, int& selectionEndX, int& selectionEndY, int& selectionStartY, int& cursorX, int& cursorY, bool& shiftHeld) { if (event.type == SDL_KEYDOWN) { if (event.key.keysym.sym == SDLK_LEFT) { if (cursorX > 0) cursorX--; else if (cursorY > 0) { cursorY--; cursorX = lines[cursorY].length(); } } else if (event.key.keysym.sym == SDLK_RIGHT) { if (cursorX < lines[cursorY].length()) cursorX++; else if (cursorY < lines.size() - 1) { cursorY++; cursorX = 0; } } if (event.key.keysym.mod & KMOD_SHIFT) { if (selectionStartX == -1) { selectionStartX = cursorX; selectionStartY = cursorY; } selectionEndX = cursorX; selectionEndY = cursorY; } else { selectionStartX = -1; selectionStartY = -1; selectionEndX = -1; selectionEndY = -1; } } }
void copySelectedText(const vector<string>& lines, int& selectionStartX, int& selectionEndX, int& selectionEndY, int& selectionStartY, int& cursorX, int& cursorY, string& clipboard) { if (selectionStartX == -1 || selectionEndX == -1) return; int startY = min(selectionStartY, selectionEndY); int endY = max(selectionStartY, selectionEndY); int startX = (startY == selectionStartY) ? selectionStartX : selectionEndX; int endX = (endY == selectionEndY) ? selectionEndX : selectionStartX; clipboard.clear(); for (int y = startY; y <= endY; y++) { int lineStart = (y == startY) ? startX : 0; int lineEnd = (y == endY) ? endX : lines[y].length(); if (lineStart < lines[y].length() && lineEnd <= lines[y].length()) { clipboard += lines[y].substr(lineStart, lineEnd - lineStart); if (y != endY) clipboard += "\n"; } } cout << "Copied to clipboard: " << clipboard << endl; }
void pasteText(vector<string>& lines, int& cursorX, int& cursorY, string& clipboard) { if (clipboard.empty()) return; size_t newlinePos = clipboard.find('\n'); if (newlinePos == string::npos) { lines[cursorY].insert(cursorX, clipboard); cursorX += clipboard.length(); } else { string firstLine = clipboard.substr(0, newlinePos); lines[cursorY].insert(cursorX, firstLine); cursorX = firstLine.length(); string remainingText = clipboard.substr(newlinePos + 1); size_t nextNewlinePos; while ((nextNewlinePos = remainingText.find('\n')) != string::npos) { lines.insert(lines.begin() + cursorY + 1, remainingText.substr(0, nextNewlinePos)); cursorY++; remainingText = remainingText.substr(nextNewlinePos + 1); } if (!remainingText.empty()) { lines.insert(lines.begin() + cursorY + 1, remainingText); cursorY++; cursorX = remainingText.length(); } } cout << "Pasted from clipboard: " << clipboard << endl; }
void cutSelectedText(vector<string>& lines, int& selectionStartX, int& selectionEndX, int& selectionEndY, int& selectionStartY, int& cursorX, int& cursorY, string& clipboard) { copySelectedText(lines, selectionStartX, selectionEndX, selectionEndY, selectionStartY, cursorX, cursorY, clipboard); if (selectionStartX != -1 && selectionEndX != -1) { if (selectionStartY == selectionEndY) { lines[selectionStartY].erase(selectionStartX, selectionEndX - selectionStartX); } else { lines[selectionStartY].erase(selectionStartX); lines[selectionEndY].erase(0, selectionEndX); lines[selectionStartY] += lines[selectionEndY]; lines.erase(lines.begin() + selectionStartY + 1, lines.begin() + selectionEndY + 1); } } }
void saveAsOption(SDL_Renderer* renderer, TTF_Font* font, const vector<string>& lines) { SDL_Rect done = {575, 340, 150, 40}; SDL_Color textColor = {0, 0, 0, 255}; SDL_Event e; string filename = " "; bool isDone = false; while (!isDone) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_MOUSEBUTTONDOWN) { int mx = e.button.x; int my = e.button.y; if (isMouseInRect(mx, my, done)) isDone = true; } else if (e.type == SDL_KEYDOWN) { if (e.key.keysym.sym == SDLK_BACKSPACE && !filename.empty()) filename.pop_back(); } else if (e.type == SDL_TEXTINPUT) { filename += e.text.text; } } drawRect(renderer, {500, 200, 300, 200}, {150, 150, 150, 255}); SDL_Surface *textSurface = TTF_RenderText_Blended(font, filename.c_str(), textColor); SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); SDL_Rect renderQuad = {520, 220, textSurface->w, textSurface->h}; SDL_FreeSurface(textSurface); SDL_RenderCopy(renderer, textTexture, nullptr, &renderQuad); SDL_DestroyTexture(textTexture); drawRect(renderer, done, {255, 255, 255, 255}); makeTextInRectangle(renderer, font, done, "Done", {0, 0, 0, 255}); SDL_RenderPresent(renderer); } filename.erase(filename.begin()); filename += ".cpp"; saveFile(filename, lines); }
void thereIsError(SDL_Renderer* renderer, TTF_Font* font) { SDL_Rect text = {550, 330, 200, 40}; drawRect(renderer, {500, 300, 300, 100}, {200, 200, 200, 255}); makeTextInRectangle(renderer, font, text, "There is an Error", {255, 0, 0, 255}); } // ------------------- End Helper Functions ------------------- بیا تو کد بالا یک سری تغییرات رو لنحام بده اول بیا تابغ کنترل ا رئ اغریف کن به طوری که کل متن موحود در صقحه رو مانند ویندوز ابی کنه همین کارو با گرغتن شیفت و جهت های ویندورز برای انتخاب قسمتی از متن هم انحام بده بعد این متون انتخاب شده باید قابلیت ویراییش داشته باشن کنترل وی رو هم طوری تعریق کن کخه تا صد حط رو بتونه از کلیپ برد دریافت کنه فرایند ریدو کردن رو بیش از یک بار و تا هفت بار بشه انحام داد و اینکه بیا تعریف کن با گرفتن کنترل و عدد صفر لینک زیر رو باز کنه https://copilot.microsoft.com/chats/2qsoCRNVbXLS8PH3woAQ9
ﺪﮐ یزﺎﺳ ﻪﻨﯿﻬﺑ لﺎﺜﻣ ۱ .۳ .۴ :ﻪﯿﻟوا ﺪﮐ 1 cout << "1" << endl; 2 cout << "2" << endl; 3 cout << "3" << endl; 4 1 :هﺪﺷ ﻪﻨﯿﻬﺑ ﺪﮐ for (int i = 1; i <= 3; i++) { 2 cout << i << endl; 3 } 4 ۱ فرایند بالا رو ام طوری تغریف کن که با دیدن الگوی کلی سی اوت پشت هم پیشنهاد بده برای تغییرش با فور و با زدن دکمه تب متن رو از حالت کمرنگ ثبت نماید(مثل اوتو کامپلیت) اوتو کامپلیت رو برای پیرانتزو کروشه انحام بده که با دیدن یک قسمت از پیرلنتز یا کرئشه بخش دیگر انرا پیشنهاد دهد فرایند های کنترل ایکس برای کات به کلیپ برد و کنترل سی برای کپی به کلیپ برد رو هم اضافه کن راستی میخوام حطاها از باکسشون بیرون نزنه و با زدن هرکدوم از دکمه های گرافیکی منوی خطا ها و خطا ها محو شوند نحوه تغییر فونت تو این کدم بگو بهم
میخوام یه نوع ارور تعریف کنی به نام Unrelated Operator که اگر ترکیب عملگر های ما خارج از ترکیبات زیر بود این ارور در بحش ارور ها چاپ بشه در لحظه و بدون زدن دکمه دیباگ +, / ,-, = ,++, -- ,+=, -=, *, *=, /= ارور بعدی میخوامParenthesis error باشه که اگر پرانتز کروشه یا برکتی دید که باز شده ولی بسته نشده یا بسته شده ولی بار نشده بیاد در لحظه این ارورو در منو ارور ها چاپ کنه ارور بعدی semicolon error هست که میخوام بیاد بررسی کنه که ایا در پایان هر خط ; این وحود دارد یا خیر البته بحز خطوط کامنت و اینکه ایا در پیرانتز جلوی هر فور سه تا ; سه تا از اینا وحود دارد یا خیر و خطای اخر خطای String error هست که اگر رشته کلماتی میان دابل کوتیشن نباشد یا هردوطرف یک رشته کلام دابل کوتیشن نباشد این ارورو بده String error بعد میخوام چند خط کامنت رو هم بتونه تشخیص بده و اگر یه طرف متن ما */ از اینا نبود بیاد ارور Multi-line comment error این رو چاپ کنه
احرین اروری ام که میخوام بدی اینه که غلط املایی درکلمات اصلی ای که در لیست بررسی برای پیشنهاد متن بود اتفاق بیفتد و یه حرف جابجا نوشته شو د (پس الکویی ک باید تعریف کنی اینه که رشته ما تمام حروفش ماندد همین باشه ولی ترتیب چیدمان فرق کنه ) ex: while این درست whiel و این غلط و ارورشه واگر این اتفاق اقتاده بود ارورMisspelling این رو بدهد چند نکته راجب ارور ها درلحظه نشون داده بشه و اگر رفع شد از منو ارورا پاک بشه این ارور در لحظه چاپ بشه
یادت نرههه هیحای دیگه کد رو خراب نکن دست کاری ام نکن بهینه کد بزن و کامنت گزاری رو طوری انجام بده انگار خودت من انجام دادم و اینکه بفهمم کارای هر تابع چیه و برای چ کاریه
نا امیدم نکن
Highlighting and selection
I'm adding "تابغ کنترل ا رئ" and enhancing text selection with Ctrl+A and SHIFT + arrow. The code, extensive, needs integration to track and highlight selections.
Integrating features
I'm thinking through typing and pasting functionalities, aligning with Windows standards. For typo correction, multiple redo capabilities are already in place.
Piecing together functionality
Tracking cursor movement is interesting. For the link feature, I'll use system("start <URL>")
to seamlessly open the link in the default browser.
Transforming code
I’m working on automating code transformation by identifying patterns in consecutive cout
statements to suggest for loops, utilizing personalized logic and tab activation for seamless conversion.
Formulating suggestions
I'm extending "updateAutoCompleteSuggestion" to detect three successive cout << n
lines, proposing a for loop for automation, and addressing common code errors like misspellings and syntax issues.
Enhancing real-time reviews
I’m thinking through integrating a "checkErrors" function to pinpoint errors, aiming for real-time updates in the error box without impacting existing functionalities.
Formulating real-time error detection
I'm creating a checkErrors(...)
function to detect "Unrelated Operator," "Parenthesis error," "semicolon error," "String error," "Multi-line comment error," and "Misspelling" errors dynamically, with updates shown in the error box.
Merging error handling
I’m piecing together error handling to store errors in one place, only showing the popup with issues. Menu clicks will hide the popup, and I'll ensure no code breaks.
Integrating editing features
I'm implementing "Ctrl + A" logic for selection, allowing partial selections with Shift and arrow keys, editing selected text, and enhancing checkErrors
with new checks.
Tracking SHIFT selection
I'm incorporating SHIFT selection logic using new global variables to monitor and update selection states, ensuring precise multi-line handling and aligning with existing shortcuts.
Reasoned about IDE enhancements for 24 seconds
در ادامه نسخهٔ کامل کد شما را میآورم که تمامی خواستههای جدید (انتخاب متن با شیفت و Ctrl+A، امکان ویرایش متن انتخابشده، پیست چندخطی تا ۱۰۰ خط، بازکردن لینک با Ctrl+0، پیشنهاد تبدیل مجموعهای از cout
های پشتسرهم به حلقهٔ for، پیشنهاد بستهشدن پرانتز و کروشه، کنترل+X/Control+C/Control+V، مخفیشدن خطاها پس از کلیک منو، تغییر فونت، و همچنین چککردن انواع خطاهای جدید در لحظه) در آن اعمال شده است.
نکته: این کد طولانی است و حاوی بخشی از منطقهای مختلف (شیوهٔ نگهداری متن، Undo/Redo، منوها و …) است. در این پاسخ سعی شده که “اساس کد شما دستنخورده باقی بماند” و صرفاً بخشهای جدید بهشکل بهینه و همراه با کامنت به آن اضافه شود.
در مورد تغییر فونت: همانطور که در کد خواهید دید، فونتها با TTF_OpenFont
بارگذاری میشوند. برای تغییر فونت کافی است در بخشهایی که از font1
، font2
یا fontMenuBar
استفاده شده، مسیر فونت را عوض کنید یا اندازهٔ آن را تغییر دهید.
TTF_Font* font1 = TTF_OpenFont("C:\\Windows\\Fonts\\consola.ttf", 19); // ... TTF_Font* fontMenuBar = TTF_OpenFont("C:\\Windows\\Fonts\\Arial.ttf", 23);
هرجا که خواستید، میتوانید بهجای این مسیرها مسیر فونت دلخواه خود را قرار دهید، یا اندازه (آخرین پارامتر) را عوض کنید.
کنترل خطاها: تابع جدیدی بهنام checkAllErrors(...)
نوشته شده که انواع ارورهای خواستهشده را بررسی میکند و در یک وکتور از استرینگ برمیگرداند. سپس در بخش رندر (یا حلقهٔ اصلی) این ارورها نمایش داده میشوند. به محض رفع خطا از متن، از لیست ارورها حذف میشود.
انتخاب متن: متغیرهایی در کد برای نگهداری وضعیت انتخاب (startLine, startCol, endLine, endCol, active) اضافه شدهاند. با نگهداشتن شیفت و استفاده از کلیدهای جهت، متن انتخاب میشود. یا با Ctrl+A کل متن هایلایت میشود. اگر متنی انتخاب شده باشد و شروع به تایپ کنیم، دقیقاً شبیه ویندوز متن حذفشده و متن جدید جایگزین میگردد.
بازکردن لینک با Ctrl+0: داخل روال رویدادهای کیبورد، اگر ctrlDown
فعال بود و e.key.keysym.sym == SDLK_0
، یک دستور system("start https://...")
اجرا میشود.
پیست چندخطی تا ۱۰۰ خط: در تابع pasteText، وقتی از کلیپبورد متن گرفته میشود، فقط تا ۱۰۰ خط اول را درج میکنیم.
اتوکامپلیت:
updateAutoCompleteSuggestion
).cout << ...
نوشته شد (مثلاً cout << "1" << endl;
تا cout << "3" << endl;
)، کد بهصورت خودکار یک پیشنهاد میدهد که همه را در یک حلقهٔ for بگذارد. با زدن کلید Tab
، آن متن جایگزین میشود.خطاهای جدید: (همه در لحظه بررسی میشوند و اگر مشکل برطرف شود، از لیست حذف میگردد)
{+, /, -, =, ++, --, +=, -=, *, *=, /=}
دیدیم.for(...)
دقیقاً ۲ سمیکالن دیده نشود./*
شروع شود ولی */
بسته نشود (یا برعکس).while
, for
, if
, switch
, …) با حروف جابهجا نوشته شود (مثلاً whiel
)؛ در این صورت ارور Misspelling
نمایش داده میشود.در کد زیر، داخل تابع checkAllErrors
با Regex و روشهای ساده، این موارد بررسی شده و در لیستی از ارورها گردآوری میشود. هر فریم این تابع صدا زده میشود تا خطاها بهصورت بیدرنگ آپدیت شوند.
#include <SDL2/SDL.h> #include <SDL2/SDL_ttf.h> #include <SDL2/SDL2_gfx.h> #include <regex> #include <string> #include <iostream> #include <vector> #include <cstring> #include <fstream> #include <windows.h> #include <commdlg.h> #include <cstdio> #include <algorithm> // برای min/max و غیره using namespace std; // ------------------- Constants ------------------- const int SCREEN_WIDTH = 1300; const int SCREEN_HEIGHT = 700; const Uint32 CURSOR_BLINK_INTERVAL = 500; // میلیثانیه const int MAX_PASTE_LINES = 100; // حداکثر خطوطی که با Ctrl+V وارد شود const int UNDO_LIMIT = 7; // حداکثر تعداد undo/redo // ------------------- End Constants ------------------- // ------------------- Theme Colors ------------------- struct ThemeColors { SDL_Color background; SDL_Color menuBarItems; SDL_Color menuDropRect; SDL_Color treeMap; SDL_Color defaultText; }; const ThemeColors DARK_MODE = { {101, 30, 177, 200}, {70, 70, 70, 255}, {80, 80, 80, 255}, {50, 50, 50, 255}, {255, 255, 255, 255} }; const ThemeColors LIGHT_MODE = { {204, 241, 255, 180}, {180, 180, 180, 255}, {200, 200, 200, 255}, {150, 150, 150, 255}, {0, 0, 0, 255} }; // ------------------- End Theme Colors ------------------- // ------------------- Global Variables ------------------- // نمایش خطای کتابخانه (قدیمی) + خطاهای جدید bool haveError = false; // دسترسی سریع برای منوی کامپایل/ران bool isDark = false; // تم تاریک یا روشن string currentProjectPath = ""; // مسیر پروژه فعلی string autoCompleteSuggestion = "";// متن پیشنهادی تکمیل خودکار string errorPopupText = ""; // متن خطاهای کامپایل/دیباگ bool showErrorPopup = false; // نمایش باکس خطا int errorScrollOffset = 0; // اسکرول باکس خطا // انتخاب متن (start/end) و فعالبودن bool selectionActive = false; int selectionStartLine = -1; int selectionStartPos = -1; int selectionEndLine = -1; int selectionEndPos = -1; // ------------------- End Global Variables ------------------- // ------------------- Undo/Redo ------------------- struct EditorState { vector<string> lines; int currentLine; int cursorPos; }; vector<EditorState> undoStack; vector<EditorState> redoStack; /* * توابع مربوط به ذخیره و بازیابی حالت: recordState, performUndo, performRedo * هر بار که تغییری در متن حاصل میشود، recordState فراخوانی میگردد. */ void recordState(const vector<string>& lines, int currentLine, int cursorPos) { if (!undoStack.empty()) { const EditorState &last = undoStack.back(); if (last.lines == lines && last.currentLine == currentLine && last.cursorPos == cursorPos) return; } EditorState state = { lines, currentLine, cursorPos }; undoStack.push_back(state); if (undoStack.size() > UNDO_LIMIT) undoStack.erase(undoStack.begin()); // ریختن هر تغییر جدید در undo، یعنی باید استک redo خالی شود redoStack.clear(); } void performUndo(vector<string>& lines, int ¤tLine, int &cursorPos) { // اگر undoStack خالی نباشد، آخرین وضعیت را اعمال کنیم if (!undoStack.empty()) { EditorState state = undoStack.back(); undoStack.pop_back(); EditorState curState = { lines, currentLine, cursorPos }; redoStack.push_back(curState); lines = state.lines; currentLine = state.currentLine; cursorPos = state.cursorPos; } } void performRedo(vector<string>& lines, int ¤tLine, int &cursorPos) { // اگر redoStack خالی نباشد، آن را برگردانیم if (!redoStack.empty()) { EditorState state = redoStack.back(); redoStack.pop_back(); // پیش از اعمال آن وضعیت، حالت فعلی را به پشتهٔ undo اضافه میکنیم recordState(lines, currentLine, cursorPos); lines = state.lines; currentLine = state.currentLine; cursorPos = state.cursorPos; } } // ------------------- End Undo/Redo ------------------- // ------------------- ساختار منو ------------------- struct Menu { bool fileOpenMenu = false; bool editOpenMenu = false; bool themeOpenMenu = false; // File menu SDL_Rect fileMenuButton = {246, 0, 155, 35}; SDL_Rect dropRectFile = {246, 35, 170, 150}; SDL_Rect itemProjectNew = {246, 35, 170, 50}; SDL_Rect itemProjectSave= {246, 85, 170, 50}; SDL_Rect itemExit = {246,135, 170, 50}; // Edit menu SDL_Rect editMenuButton = {401, 0, 159, 35}; SDL_Rect dropRectEdit = {421, 35, 120, 100}; SDL_Rect itemUndo = {421, 35, 120, 50}; SDL_Rect itemRedo = {421, 85, 120, 50}; // Theme menu SDL_Rect themeMenuButton= {560, 0, 175, 35}; SDL_Rect dropRectTheme = {560, 35,175, 100}; SDL_Rect itemLight = {560, 35,175, 50}; SDL_Rect itemDark = {560, 85,175, 50}; // Debug & Compile button SDL_Rect dNcMenuButton = {735, 0, 215, 35}; // Run button SDL_Rect runMenuButton = {950, 0, 175, 35}; // Save button SDL_Rect saveMenuButton = {1125,0, 175, 35}; }; // ------------------- End ساختار منو ------------------- // ------------------- Prototype Functions ------------------- void ensureLastLineVisible(int, int&, int, int, int); string wholeTextFile(const vector<string>&); bool isMouseInRect(int, int, const SDL_Rect&); void drawRect(SDL_Renderer*, const SDL_Rect&, SDL_Color); void makeTextInRectangle(SDL_Renderer*, TTF_Font*, SDL_Rect&, const string&, SDL_Color); vector<string> getFilesInDirectory(const string&); string showSaveFileDialog(); bool compile(const string&, string&); void runInAnotherWindow(const string&); string newProject(SDL_Renderer*, TTF_Font*); void saveFile(const string&, const vector<string>&); void loadFile(const string&, vector<string>&, int&); void renderMenu(SDL_Renderer*, TTF_Font*, Menu&, bool&); void treeMap(SDL_Renderer*, bool&, TTF_Font*, const vector<string>&); void updateAutoCompleteSuggestion(const vector<string>&, int, int, string&); void renderAutoComplete(SDL_Renderer*, TTF_Font*, const string&, int, int); // کپی/کات/پیست void copySelectedText(const vector<string>&, int, int, int, int, int, int, string&); void cutSelectedText(vector<string>&, int&, int&, int&, int&, int&, int&, string&); void pasteText(vector<string>&, int&, int&, string&); void thereIsError(SDL_Renderer*, TTF_Font*); // --- توابع مربوط به بررسی خطاهای مختلف (همه در checkAllErrors) --- vector<string> checkAllErrors(const vector<string>& lines); // --------------------------------------------------------------- // ------------------- End Prototype Functions ------------------- // ------------------- Windows API: Save Dialog ------------------- string showSaveFileDialog() { OPENFILENAME ofn; char fileName[MAX_PATH] = ""; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.lpstrFilter = "C++ Files (*.cpp)\0*.cpp\0All Files (*.*)\0*.*\0"; ofn.lpstrFile = fileName; ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_EXPLORER | OFN_OVERWRITEPROMPT; ofn.lpstrDefExt = "cpp"; if (GetSaveFileName(&ofn)) return string(fileName); return ""; } // ------------------- End Save Dialog ------------------- // ------------------- AutoComplete ------------------- /* * این تابع، علاوه بر پیشنهاد بستهشدن پرانتز، کروشه و آکولاد، * اگر الگوی پشت سر هم "cout << 1", "cout << 2", ... مشاهده شد، پیشنهاد تبدیل به * for (int i=1; i<=3; i++) { cout << i << endl; } * را میدهد. */ vector<string> autoCompleteKeywords = {"switch", "int", "float", "main", "scanf", "printf", "strlen", "else", "while", "for"}; void updateAutoCompleteSuggestion(const vector<string> &lines, int currentLine, int cursorPos, string &suggestion) { suggestion.clear(); if (currentLine < 0 || currentLine >= (int)lines.size()) return; // اگر انتخابی فعال است، خودکار پیشنهاد را خالی کن (یا میتوانستیم نگه داریم) if (selectionActive) return; // 1) اگر آخرین کاراکتر تایپشده یکی از این بود: '(', '[', '{' => پیشنهاد بستن if (cursorPos > 0) { char lastChar = lines[currentLine][cursorPos-1]; if (lastChar == '(') { suggestion = ")"; return; } if (lastChar == '[') { suggestion = "]"; return; } if (lastChar == '{') { suggestion = "}"; return; } } // 2) شناسایی یک کلمه در حال تایپ int start = cursorPos - 1; while(start >= 0 && (isalnum(lines[currentLine][start]) || lines[currentLine][start] == '_')) { start--; } start++; string currentWord = lines[currentLine].substr(start, cursorPos - start); // اگر کلمه "i" تایپ شود => پیشنهاد "f" (if) if (currentWord == "i") { suggestion = "f"; return; } // اگر consecutive cout های شبیه زیر دیدیم: // cout << "1" << endl; // cout << "2" << endl; // ... // پیشنهاد استفاده از for // (مثال خیلی ساده: اگر 3 خط متوالی داریم که اعداد 1..3 هستند، تبدیلش کنیم) // تشخیص: // line j: cout << "1" << endl; // line j+1: cout << "2" << endl; // line j+2: cout << "3" << endl; // ... // سپس پیشنهاد for(...) {cout << i << endl;} // برای خلاصه: اگر 3 خط پشتسرهم داشتیم که cout کردهاند و در انتها اعداد متوالی... // وقتی کاربر دارد خط بعدی را تایپ میکند، به او پیشنهاد دهیم. // (پیدا کردن دقیق این pattern کمی طولانی است. اینجا نسخه خیلی ساده و دموی آن.) if (currentLine >= 2) { // بررسی سه خط قبلی // regex ساده: cout << "(\d+)" << endl; static regex reCout("\\s*cout\\s*<<\\s*\"?(\\d+)\"?\\s*<<\\s*endl\\s*;"); smatch m1, m2, m3; string l1 = lines[currentLine-2]; string l2 = lines[currentLine-1]; string l3 = lines[currentLine]; if (regex_search(l1, m1, reCout) && regex_search(l2, m2, reCout) && regex_search(l3, m3, reCout)) { // اعداد گرفتهشده: int n1 = stoi(m1[1]); int n2 = stoi(m2[1]); int n3 = stoi(m3[1]); // اگر پیاپی باشند if (n2 == n1+1 && n3 == n2+1) { // پیشنهاد تبدیل به حلقهٔ for suggestion = "\nfor (int i = " + to_string(n1) + "; i <= " + to_string(n3) + "; i++) {\n cout << i << \"\\n\";\n}\n"; return; } } } // 3) چک کلمات کلیدی از autoCompleteKeywords if (currentWord.size() >= 2) { for (auto &kw : autoCompleteKeywords) { if (kw.size() > currentWord.size() && kw.substr(0, currentWord.size()) == currentWord) { // پیشنهاد ادامهٔ kw suggestion = kw.substr(currentWord.size()); return; } } } } void renderAutoComplete(SDL_Renderer *renderer, TTF_Font *font, const string &suggestion, int cursorX, int cursorY) { if (suggestion.empty()) return; SDL_Color faded = { 128, 128, 128, 255 }; SDL_Surface* surface = TTF_RenderText_Blended(font, suggestion.c_str(), faded); SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); SDL_Rect dest = { cursorX, cursorY, surface->w, surface->h }; SDL_RenderCopy(renderer, texture, NULL, &dest); SDL_FreeSurface(surface); SDL_DestroyTexture(texture); } // ------------------- End AutoComplete ------------------- // ------------------- Main (Entry Point) ------------------- int main(int argc, char* argv[]) { // راهاندازی SDL if (SDL_Init(SDL_INIT_VIDEO) < 0) { cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl; return -1; } if (TTF_Init() == -1) { cerr << "TTF could not initialize! TTF_Error: " << TTF_GetError() << endl; SDL_Quit(); return -1; } SDL_Window* window = SDL_CreateWindow("SDL Text Editor IDE", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (!window) { cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << endl; TTF_Quit(); SDL_Quit(); return -1; } // (اختیاری) تنظیم آیکون SDL_Surface* iconSurface = SDL_LoadBMP("openai_logo.bmp"); if (iconSurface) SDL_SetWindowIcon(window, iconSurface); SDL_FreeSurface(iconSurface); SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (!renderer) { cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << endl; SDL_DestroyWindow(window); TTF_Quit(); SDL_Quit(); return -1; } // بارگذاری فونتها: (برای تغییر فونت، مسیر یا سایز را عوض کنید) TTF_Font* font1 = TTF_OpenFont("C:\\Windows\\Fonts\\consola.ttf", 19); if (!font1) { cerr << "Failed to load font1! TTF_Error: " << TTF_GetError() << endl; SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); TTF_Quit(); SDL_Quit(); return -1; } TTF_Font* fontMenuBar = TTF_OpenFont("C:\\Windows\\Fonts\\Arial.ttf", 23); if (!fontMenuBar) { cerr << "Failed to load fontMenuBar! TTF_Error: " << TTF_GetError() << endl; SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); TTF_Quit(); SDL_Quit(); return -1; } // فونت پیشفرض: TTF_Font* font = font1; // فایلهای موجود در پوشه string directoryPath = "."; vector<string> filenames = getFilesInDirectory(directoryPath); // متن ادیتور vector<string> lines = {""}; int currentLine = 0; int cursorPos = 0; int scrollOffset = 0; const int LINE_HEIGHT = TTF_FontHeight(font); // وضعیت چشمکزن کرسر Uint32 lastCursorToggle = SDL_GetTicks(); bool cursorVisible = true; bool ctrlDown = false, shiftHeld = false; string clipboard = ""; // منو Menu menu; // اولین حالت را در Undo ثبت میکنیم recordState(lines, currentLine, cursorPos); bool quit = false; SDL_Event e; while (!quit) { // چشمکزدن کرسر Uint32 currentTime = SDL_GetTicks(); if (currentTime > lastCursorToggle + CURSOR_BLINK_INTERVAL) { cursorVisible = !cursorVisible; lastCursorToggle = currentTime; } // رویدادها while (SDL_PollEvent(&e)) { if (e.type == SDL_QUIT) { quit = true; } else if (e.type == SDL_MOUSEBUTTONDOWN) { int mx = e.button.x; int my = e.button.y; // کلیک روی sidebar if (mx < 246) { int idx = (my - 80) / 40; if (idx >= 0 && idx < (int)filenames.size()) { lines.clear(); int dummy = 0; loadFile(filenames[idx], lines, dummy); currentLine = 0; cursorPos = 0; currentProjectPath = directoryPath + "\\" + filenames[idx]; recordState(lines, currentLine, cursorPos); } } // بستن خطاها در صورت کلیک روی منو // (کاربر گفت که با کلیک هر دکمه منو خطاها محو شوند) showErrorPopup = false; errorPopupText.clear(); // منو File if (!menu.fileOpenMenu && isMouseInRect(mx, my, menu.fileMenuButton)) { menu.fileOpenMenu = true; } else if (menu.fileOpenMenu) { if (isMouseInRect(mx, my, menu.itemProjectNew)) { lines = {""}; currentLine = 0; cursorPos = 0; currentProjectPath.clear(); recordState(lines, currentLine, cursorPos); menu.fileOpenMenu = false; } else if (isMouseInRect(mx, my, menu.itemProjectSave)) { if (!currentProjectPath.empty()) { saveFile(currentProjectPath, lines); } else { string path = showSaveFileDialog(); if (!path.empty()) { currentProjectPath = path; saveFile(currentProjectPath, lines); filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1)); } } menu.fileOpenMenu = false; } else if (isMouseInRect(mx, my, menu.itemExit)) { quit = true; menu.fileOpenMenu = false; } else if (!isMouseInRect(mx, my, menu.dropRectFile)) { menu.fileOpenMenu = false; } } // منو Edit if (!menu.editOpenMenu && isMouseInRect(mx, my, menu.editMenuButton)) { menu.editOpenMenu = true; } else if (menu.editOpenMenu) { if (isMouseInRect(mx, my, menu.itemUndo)) { performUndo(lines, currentLine, cursorPos); menu.editOpenMenu = false; } else if (isMouseInRect(mx, my, menu.itemRedo)) { performRedo(lines, currentLine, cursorPos); menu.editOpenMenu = false; } else if (!isMouseInRect(mx, my, menu.dropRectEdit)) { menu.editOpenMenu = false; } } // منو Theme if (!menu.themeOpenMenu && isMouseInRect(mx, my, menu.themeMenuButton)) { menu.themeOpenMenu = true; } else if (menu.themeOpenMenu) { if (isMouseInRect(mx, my, menu.itemLight)) { isDark = false; menu.themeOpenMenu = false; } else if (isMouseInRect(mx, my, menu.itemDark)) { isDark = true; menu.themeOpenMenu = false; } else if (!isMouseInRect(mx, my, menu.dropRectTheme)) { menu.themeOpenMenu = false; } } // Compile if (isMouseInRect(mx, my, menu.dNcMenuButton)) { if (haveError) { // ارور داریم thereIsError(renderer, fontMenuBar); } else { // سیو و کامپایل if (!currentProjectPath.empty()) { saveFile(currentProjectPath, lines); } else { string path = newProject(renderer, fontMenuBar); currentProjectPath = path; filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1)); saveFile(currentProjectPath, lines); } string compileErrors; if (compile(currentProjectPath, compileErrors)) { cout << "[INFO] Compilation successful!\n"; } else { showErrorPopup = true; errorPopupText = compileErrors; } } } // Run if (isMouseInRect(mx, my, menu.runMenuButton)) { if (haveError) { thereIsError(renderer, fontMenuBar); } else { if (!currentProjectPath.empty()) { saveFile(currentProjectPath, lines); } else { string path = newProject(renderer, fontMenuBar); currentProjectPath = path; filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1)); saveFile(currentProjectPath, lines); } string compileErrors; if (compile(currentProjectPath, compileErrors)) { runInAnotherWindow("output"); } else { showErrorPopup = true; errorPopupText = compileErrors; } } } // Save Button if (isMouseInRect(mx, my, menu.saveMenuButton)) { if (!currentProjectPath.empty()) { saveFile(currentProjectPath, lines); } else { string path = showSaveFileDialog(); if (!path.empty()) { currentProjectPath = path; saveFile(currentProjectPath, lines); filenames.push_back(currentProjectPath.substr(currentProjectPath.find_last_of("\\/") + 1)); } } } } // Mouse Button Down else if (e.type == SDL_MOUSEWHEEL) { // اسکرول عمودی ساده if (e.wheel.y > 0) scrollOffset = max(0, scrollOffset - LINE_HEIGHT); else if (e.wheel.y < 0) scrollOffset += LINE_HEIGHT; } else if (e.type == SDL_KEYDOWN) { // بررسی Ctrl و Shift if (e.key.keysym.sym == SDLK_LCTRL || e.key.keysym.sym == SDLK_RCTRL) ctrlDown = true; if (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT) shiftHeld = true; // Ctrl+0 => بازکردن لینک if (ctrlDown && e.key.keysym.sym == SDLK_0) { system("start https://copilot.microsoft.com/chats/2qsoCRNVbXLS8PH3woAQ9"); } // Ctrl+A => انتخاب کل متن if (ctrlDown && e.key.keysym.sym == SDLK_a) { selectionActive = true; selectionStartLine = 0; selectionStartPos = 0; selectionEndLine = (int)lines.size() - 1; selectionEndPos = (int)lines.back().size(); continue; } // Ctrl+Z => Undo if (ctrlDown && e.key.keysym.sym == SDLK_z) { performUndo(lines, currentLine, cursorPos); continue; } // Ctrl+Y => Redo if (ctrlDown && e.key.keysym.sym == SDLK_y) { performRedo(lines, currentLine, cursorPos); continue; } // Ctrl+C => Copy if (ctrlDown && e.key.keysym.sym == SDLK_c) { copySelectedText(lines, selectionStartLine, selectionEndLine, selectionStartPos, selectionEndPos, cursorPos, currentLine, clipboard); continue; } // Ctrl+V => Paste if (ctrlDown && e.key.keysym.sym == SDLK_v) { // اگر چیزی انتخاب شده، اول حذف کن if (selectionActive) { cutSelectedText(lines, selectionStartLine, selectionEndLine, selectionStartPos, selectionEndPos, cursorPos, currentLine, clipboard /*البته اینجا محتوای clipboard عوض میشود اما فرض میکنیم کپی لازم نداریم*/); selectionActive = false; } // حالا از کلیپبورد سیستم یا local clipboard // ولی خواسته شد که تا ۱۰۰ خط بیشتر جایگذاری نشود { const char* clipText = SDL_GetClipboardText(); if (clipText) { // اگر میخواهیم از کلیپبورد سیستم استفاده کنیم string cpy(clipText); // جدا کردن به خط vector<string> splitted; { size_t pos = 0; while(true) { size_t p = cpy.find('\n', pos); if (p == string::npos) { splitted.push_back(cpy.substr(pos)); break; } splitted.push_back(cpy.substr(pos, p - pos)); pos = p+1; if (splitted.size() >= MAX_PASTE_LINES) { // حداکثر ۱۰۰ خط break; } } } // عمل جایگذاری (با منطق ساده) if (!splitted.empty()) { // درج اولین خط در همان خط جاری lines[currentLine].insert(cursorPos, splitted[0]); cursorPos += splitted[0].size(); // درج بقیه خطوط به صورت شکست for (size_t i = 1; i < splitted.size(); i++) { string remainder = lines[currentLine].substr(cursorPos); lines[currentLine] = lines[currentLine].substr(0, cursorPos); lines.insert(lines.begin() + currentLine + 1, remainder); currentLine++; cursorPos = 0; lines[currentLine].insert(cursorPos, splitted[i]); cursorPos += splitted[i].size(); } } recordState(lines, currentLine, cursorPos); } } continue; } // Ctrl+X => Cut if (ctrlDown && e.key.keysym.sym == SDLK_x) { cutSelectedText(lines, selectionStartLine, selectionEndLine, selectionStartPos, selectionEndPos, cursorPos, currentLine, clipboard); selectionActive = false; recordState(lines, currentLine, cursorPos); continue; } // Backspace if (e.key.keysym.sym == SDLK_BACKSPACE) { // اگر بخشی انتخاب شده باشد، همانند ویندوز پاکش کنیم if (selectionActive) { cutSelectedText(lines, selectionStartLine, selectionEndLine, selectionStartPos, selectionEndPos, cursorPos, currentLine, clipboard /*اینجا برای کات clipboard پر میشود*/); selectionActive = false; } else { // در غیر این صورت کاراکتر قبل کرسر حذف شود if (cursorPos > 0 && cursorPos <= (int)lines[currentLine].size()) { lines[currentLine].erase(cursorPos - 1, 1); cursorPos--; } else if (currentLine > 0) { cursorPos = (int)lines[currentLine - 1].size(); lines[currentLine - 1] += lines[currentLine]; lines.erase(lines.begin() + currentLine); currentLine--; } } if (lines.empty()) { lines.push_back(""); currentLine = 0; cursorPos = 0; } recordState(lines, currentLine, cursorPos); } // Enter else if (e.key.keysym.sym == SDLK_RETURN) { // اگر بخشی انتخابشده باشد، اول پاک شود if (selectionActive) { cutSelectedText(lines, selectionStartLine, selectionEndLine, selectionStartPos, selectionEndPos, cursorPos, currentLine, clipboard); selectionActive = false; } // سپس خطشکنی string remainder = lines[currentLine].substr(cursorPos); lines[currentLine] = lines[currentLine].substr(0, cursorPos); lines.insert(lines.begin() + currentLine + 1, remainder); currentLine++; cursorPos = 0; ensureLastLineVisible(currentLine, scrollOffset, SCREEN_HEIGHT, LINE_HEIGHT, (int)lines.size()); recordState(lines, currentLine, cursorPos); } // Tab => اگر پیشنهاد اتوکامپلیت داشتیم، درج آن else if (e.key.keysym.sym == SDLK_TAB) { if (!autoCompleteSuggestion.empty()) { // اگر Selection فعال باشد، اول پاک شود if (selectionActive) { cutSelectedText(lines, selectionStartLine, selectionEndLine, selectionStartPos, selectionEndPos, cursorPos, currentLine, clipboard); selectionActive = false; } lines[currentLine].insert(cursorPos, autoCompleteSuggestion); cursorPos += (int)autoCompleteSuggestion.size(); autoCompleteSuggestion.clear(); recordState(lines, currentLine, cursorPos); } else { // در غیر این صورت، چهار فاصله if (selectionActive) { cutSelectedText(lines, selectionStartLine, selectionEndLine, selectionStartPos, selectionEndPos, cursorPos, currentLine, clipboard); selectionActive = false; } lines[currentLine].insert(cursorPos, " "); cursorPos += 4; recordState(lines, currentLine, cursorPos); } } // کلیدهای جهت else if (e.key.keysym.sym == SDLK_LEFT) { if (cursorPos > 0) { cursorPos--; } else if (currentLine > 0) { currentLine--; cursorPos = (int)lines[currentLine].size(); } // شیفت => انتخاب if (shiftHeld) { if (!selectionActive) { selectionActive = true; selectionStartLine = currentLine; selectionStartPos = cursorPos; } selectionEndLine = currentLine; selectionEndPos = cursorPos; } else { selectionActive = false; } } else if (e.key.keysym.sym == SDLK_RIGHT) { if (cursorPos < (int)lines[currentLine].size()) { cursorPos++; } else if (currentLine < (int)lines.size() - 1) { currentLine++; cursorPos = 0; } if (shiftHeld) { if (!selectionActive) { selectionActive = true; selectionStartLine = currentLine; selectionStartPos = cursorPos; } selectionEndLine = currentLine; selectionEndPos = cursorPos; } else { selectionActive = false; } } else if (e.key.keysym.sym == SDLK_UP) { if (currentLine > 0) { currentLine--; cursorPos = min(cursorPos, (int)lines[currentLine].size()); ensureLastLineVisible(currentLine, scrollOffset, SCREEN_HEIGHT, LINE_HEIGHT, (int)lines.size()); } if (shiftHeld) { if (!selectionActive) { selectionActive = true; selectionStartLine = currentLine; selectionStartPos = cursorPos; } selectionEndLine = currentLine; selectionEndPos = cursorPos; } else { selectionActive = false; } } else if (e.key.keysym.sym == SDLK_DOWN) { if (currentLine < (int)lines.size() - 1) { currentLine++; cursorPos = min(cursorPos, (int)lines[currentLine].size()); ensureLastLineVisible(currentLine, scrollOffset, SCREEN_HEIGHT, LINE_HEIGHT, (int)lines.size()); } if (shiftHeld) { if (!selectionActive) { selectionActive = true; selectionStartLine = currentLine; selectionStartPos = cursorPos; } selectionEndLine = currentLine; selectionEndPos = cursorPos; } else { selectionActive = false; } } } // end KEYDOWN else if (e.type == SDL_KEYUP) { if (e.key.keysym.sym == SDLK_LCTRL || e.key.keysym.sym == SDLK_RCTRL) ctrlDown = false; if (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT) shiftHeld = false; } else if (e.type == SDL_TEXTINPUT) { // اگر selection فعال است، اول پاک شود if (selectionActive) { cutSelectedText(lines, selectionStartLine, selectionEndLine, selectionStartPos, selectionEndPos, cursorPos, currentLine, clipboard); selectionActive = false; } // درج متن lines[currentLine].insert(cursorPos, e.text.text); cursorPos += (int)strlen(e.text.text); // آپدیت پیشنهاد updateAutoCompleteSuggestion(lines, currentLine, cursorPos, autoCompleteSuggestion); // Scroll ensureLastLineVisible(currentLine, scrollOffset, SCREEN_HEIGHT, LINE_HEIGHT, (int)lines.size()); recordState(lines, currentLine, cursorPos); } } // end pollEvent // بررسی خطاهای لحظهای (همه) vector<string> allErrors = checkAllErrors(lines); if (!allErrors.empty()) { haveError = true; // برای منوی Compile/Run // اگر کاربر هنوز دکمهٔ منو نزده، در پاپآپ نمایش دهیم (بهصورت خلاصه) // ولی اگر showErrorPopup = false باشد، یعنی کاربر با کلیک منو بسته // در این نمونه، بهصورت ساده: اگر قبلاً پاپآپ بسته نشده باشد، نشان میدهیم if (!showErrorPopup) { // بنا به خواست شما، ممکن است بخواهید بهمحض وجود خطا، پاپآپ باز شود // من اینجا میگذارم که باز شود showErrorPopup = true; } // جمع کردن تمام ارورها در یک رشته string errs; for (auto &e : allErrors) { errs += e + "\n"; } errorPopupText = errs; } else { haveError = false; if (!showErrorPopup) { errorPopupText.clear(); } } // چشمکزدن و رندر SDL_Color textColor = (isDark ? DARK_MODE.defaultText : LIGHT_MODE.defaultText); if (isDark) SDL_SetRenderDrawColor(renderer, DARK_MODE.background.r, DARK_MODE.background.g, DARK_MODE.background.b, DARK_MODE.background.a); else SDL_SetRenderDrawColor(renderer, LIGHT_MODE.background.r, LIGHT_MODE.background.g, LIGHT_MODE.background.b, LIGHT_MODE.background.a); SDL_RenderClear(renderer); // رسم خطوط متن int y = -scrollOffset; for (int j = 0; j