Rectangle func::rectTabs::DrawTabs(int sel) {
    Rectangle tabRec;
    int textIndex = 0;
    std::vector<Rectangle> tabRects;

    P_passRect = {RectPos().x, RectPos().y, TabSize().x, TabSize().y};

    for (X = 0; X < C; X++) {
        for (Y = 0; Y < R; Y++) {



            float tabWidth = P_passRect.width + Overlap();
            float tabHeight = P_passRect.height + Overlap();

            if (sel == 0) {
                tabRec = {P_passRect.x + X * (tabWidth - Overlap()), P_passRect.y + Y * (tabHeight - Overlap()), tabWidth, tabHeight};
            } else if (sel == 1) {
                float tabWidth = GetCentre().width + Overlap();
                float tabHeight = GetCentre().height + Overlap();
                tabRec = {GetCentre().x + X * (tabWidth - Overlap()), GetCentre().y + Y * (tabHeight - Overlap()), tabWidth, tabHeight};
            } else {
                throw std::out_of_range("Index must be 0 or 1");
            }

            DrawRectangleRoundedLines(tabRec, Roundness(), Smoothness(), LineThick(), OutlineColor());
            DrawRectangleRounded(tabRec, Roundness(), Smoothness(), MainColor());



            bool isMouseOverTab = false;
            
            isMouseOverTab = (F.mousePoint().x > tabRec.x + 12 && F.mousePoint().x < (tabRec.x + tabRec.width) &&
                    F.mousePoint().y > tabRec.y + 12 && F.mousePoint().y < (tabRec.y + tabRec.height));
            

            if (isMouseOverTab && isSelected == false) {
                // Highlight the tab
                //DrawRectangleRoundedLinesEx(tabRec, Roundness(), Smoothness(), LineThick(), LIGHTGRAY);
                DrawRectangleRoundedLines(tabRec, Roundness(), Smoothness(), LineThick(), LIGHTGRAY);
                char buf[20];
                sprintf(buf, "%d", (int)tabRec.y * (Y + 1));
                DrawText(buf, tabRec.x + 10, tabRec.y + 10, 20, GREEN);
                if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
                    isSelected = true;
                }
            }
            if (isMouseOverTab && isSelected == true) {
                //DrawRectangleRoundedLinesEx(tabRec, Roundness(), Smoothness(), LineThick(), GOLD);
                DrawRectangleRoundedLines(tabRec, Roundness(), Smoothness(), LineThick(), GOLD);
            
            }

        tabRects.push_back(tabRec);    
        }
        
    }

    if (textMode != NO && textIndex < v.charInsert.size()) {
        float fontSize = 25.0f;
        float rotation = 0.0f;
        float textSizeX = MeasureTextEx(GetFontDefault(), v.charInsert[textIndex], fontSize, 1.0f).x;
        float textSizeY = MeasureTextEx(GetFontDefault(), v.charInsert[textIndex], fontSize, 1.0f).y;
        
        Vector2 position;
        if (textMode == WORDx && textIndex < v.charInsert.size()) {
            // Determine the total length of the combined word
            int totalLength = 0;
            Vector2 origin;
            for (const char* letter : v.charInsert) {
                totalLength += strlen(letter);
            }
            // Allocate a char array to store the combined word
            char* combinedWord = new char[totalLength + 1];  // +1 for the null terminator
            combinedWord[0] = '\0';  // Start with an empty string
            // Concatenate each letter into combinedWord
            for (const char* letter : v.charInsert) {
                strcat(combinedWord, letter);
            }
            // Measure the size of the combined word
            float textSizeXn = MeasureTextEx(GetFontDefault(), combinedWord, fontSize, 1.0f).x;
            float textSizeYn = MeasureTextEx(GetFontDefault(), combinedWord, fontSize, 1.0f).y;
            origin = {textSizeXn / 2.0f, textSizeYn / 2.0f};
            position = {
                GetCentre().x + (P_passRect.width) / 2,
                GetCentre().y + (P_passRect.height) / 2
            };
            // Draw the combined word once in the center of the current tab
            DrawTextPro(GetFontDefault(), combinedWord, position, origin, rotation, fontSize, 1.0f, BLACK);
            // Move to the next tab only after drawing once
            //textIndex++;
            // Clean up dynamically allocated memory
            delete[] combinedWord;
        }else if (textMode == WORDy && textIndex < v.charInsert.size()) {
            
            float lineHeight = fontSize * 1.2f; // Adjust line spacing as needed
            Vector2 origin;
            // Calculate the vertical starting position for this word to be centered within the current tab
            float totalTextHeight = 0;
            int wordLength = 0;
            // Calculate the total height of the word by iterating over letters until a space or end
            int tempIndex = textIndex;
            while (tempIndex < v.charInsert.size() && *v.charInsert[tempIndex] != ' ') {
                totalTextHeight += lineHeight;
                wordLength++;
                tempIndex++;
            }
            // Calculate the initial centered position for the word in the current tab
            Vector2 position = {
                GetCentre().x + (P_passRect.width) / 2,
                tabRec.y + (tabRec.height - totalTextHeight) / 2  // Center the word vertically
            };
            // Draw each letter in the word within the current tab
            for (int i = 0; i < wordLength && textIndex < v.charInsert.size(); i++) {
                float textSizeX = MeasureTextEx(GetFontDefault(), v.charInsert[textIndex], fontSize, 1.0f).x;
                origin = {textSizeX / 2.0f, 0};  // Center each letter horizontally
                // Draw the current letter
                DrawTextPro(GetFontDefault(), v.charInsert[textIndex], position, origin, rotation, fontSize, 1.0f, BLACK);
                // Move position down for the next letter in the word
                position.y += lineHeight;
                textIndex++;
            }
            // Move past the space character, if any, to start the next word on the next tab
            if (textIndex < v.charInsert.size() && *v.charInsert[textIndex] == ' ') {
                textIndex++;
            }
        }else if (textMode == LETTERx && textIndex < v.charInsert.size()) {
            float fontSize = 25.0f;
            float rotation = 0.0f;
    
            // Iterate over stored rectangles and draw characters
            for (const auto& rect : tabRects) {
                if (textIndex >= v.charInsert.size()) break;
    
                // Measure the size of the current character
                float textSizeX = MeasureTextEx(GetFontDefault(), v.charInsert[textIndex], fontSize, 1.0f).x;
                float textSizeY = MeasureTextEx(GetFontDefault(), v.charInsert[textIndex], fontSize, 1.0f).y;
    
                // Center the character within the current tab
                Vector2 position = {
                    rect.x + (rect.width - textSizeX) / 2.0f,
                    rect.y + (rect.height - textSizeY) / 2.0f
                };
    
                // Draw the character
                DrawTextPro(GetFontDefault(), v.charInsert[textIndex], position, {0.0f, 0.0f}, rotation, fontSize, 1.0f, BLACK);
    
                // Move to the next character
                textIndex++;
            }
        } else if (textMode == LETTERy && textIndex < v.charInsert.size()) {
            float fontSize = 25.0f;
            float rotation = 0.0f;
            Vector2 origin;
            // Get text size for centering each character within the tab
            
            origin = {textSizeX / 2.0f, textSizeY / 2.0f};
            // Calculate position based on the current grid index (X, Y)
            position = {
                tabRec.x + (tabRec.width - textSizeX) / 2,
                tabRec.y + (tabRec.height - textSizeY) / 2
            };
            // Draw the current letter centered within its tab cell
            DrawTextPro(GetFontDefault(), v.charInsert[textIndex], position, origin, rotation, fontSize, 1.0f, BLACK);
            // Move to the next letter in v.charInsert
            textIndex++;
        }
    }

    return tabRec;
}