Ticket #6831: stylededit-wordcount-v4.diff

File stylededit-wordcount-v4.diff, 2.7 KB (added by negusnyul, 13 years ago)
  • src/apps/stylededit/StyledEditWindow.cpp

     
    4141#include <TextControl.h>
    4242#include <TextView.h>
    4343#include <TranslationUtils.h>
     44#include <UnicodeChar.h>
    4445
    4546
    4647using namespace BPrivate;
     
    338339    menu->AddItem(fWrapItem = new BMenuItem(B_TRANSLATE("Wrap lines"),
    339340        new BMessage(WRAP_LINES)));
    340341    fWrapItem->SetMarked(true);
     342    menu->AddItem(menuItem = new BMenuItem(B_TRANSLATE("Statistics" B_UTF8_ELLIPSIS),
     343        new BMessage(SHOW_STATISTICS)));
    341344
    342345    fSavePanel = NULL;
    343346    fSavePanelEncodingMenu = NULL;
     
    645648            _UpdateCleanUndoRedoSaveRevert();
    646649            break;
    647650        }
     651        case SHOW_STATISTICS:
     652            ShowStatistics();
     653            break;
    648654        case ENABLE_ITEMS:
    649655            fCutItem->SetEnabled(true);
    650656            fCopyItem->SetEnabled(true);
     
    15591565    return false;
    15601566}
    15611567
     1568
     1569#undef B_TRANSLATE_CONTEXT
     1570#define B_TRANSLATE_CONTEXT "Statistics"
     1571
     1572
     1573int32
     1574StyledEditWindow::ShowStatistics()
     1575{
     1576    size_t words = 0;
     1577    bool inword = false;
     1578    size_t length = fTextView->TextLength();
     1579   
     1580    for (size_t i = 0; i < length; i++) {
     1581        if (BUnicodeChar::IsSpace(fTextView->Text()[i])) {
     1582            inword = false;
     1583        } else if (!inword) {
     1584            words++;
     1585            inword = true;
     1586        }
     1587    }
     1588   
     1589    BString result;
     1590    result << B_TRANSLATE("Document statistics") << '\n' << '\n'
     1591        << B_TRANSLATE("Lines:") << ' ' << fTextView->CountLines() << '\n'
     1592        << B_TRANSLATE("Characters:") << ' ' << length << '\n'
     1593        << B_TRANSLATE("Words:") << ' ' << words;
     1594       
     1595    BAlert* alert = new BAlert("Statistics", result, B_TRANSLATE("OK"), NULL,
     1596        NULL, B_WIDTH_AS_USUAL, B_EVEN_SPACING, B_INFO_ALERT);
     1597
     1598    return alert->Go();
     1599}
  • src/apps/stylededit/StyledEditWindow.h

     
    5959        void            SetFontSize(float fontSize);
    6060        void            SetFontColor(const rgb_color *color);
    6161        void            SetFontStyle(const char *fontFamily, const char *fontStyle);
     62        int32           ShowStatistics();
    6263        status_t        _LoadFile(entry_ref* ref);
    6364        void            RevertToSaved();
    6465        void            _UpdateCleanUndoRedoSaveRevert();
  • src/apps/stylededit/Constants.h

     
    6363const uint32 ALIGN_CENTER               = 'ALce';
    6464const uint32 ALIGN_RIGHT                = 'ALri';
    6565const uint32 WRAP_LINES                 = 'MDwr';
     66const uint32 SHOW_STATISTICS            = 'MDss';
    6667
    6768// enables "edit" menuitems
    6869const uint32 ENABLE_ITEMS               = 'ENit';