Ticket #3814: screenshot-wim.patch

File screenshot-wim.patch, 13.2 KB (added by Wim, 14 years ago)

Patch to have the screenshot application save it's settings. I also cleaned up some style violations, mainly lines that where longer than 80 characters.

  • ScreenshotWindow.cpp

     
    66 *      Karsten Heimrich
    77 *      Fredrik Modéen
    88 *      Christophe Huriaux
     9 *      Wim van der Meer, WPJvanderMeer@gmail.com
    910 */
    1011
    1112
     
    9697#define TR_CONTEXT "ScreenshotWindow"
    9798
    9899
     100ScreenshotWindow::ScreenshotWindow()
     101    :
     102    BWindow(BRect(0, 0, 200.0, 100.0), TR("Retake screenshot"), B_TITLED_WINDOW,
     103        B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_QUIT_ON_WINDOW_CLOSE
     104        | B_AVOID_FRONT | B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
     105    fDelayControl(NULL),
     106    fScreenshot(NULL),
     107    fOutputPathPanel(NULL),
     108    fLastSelectedPath(NULL),
     109    fDelay(0),
     110    fTabHeight(0),
     111    fIncludeBorder(false),
     112    fIncludeMouse(false),
     113    fGrabActiveWindow(false),
     114    fShowConfigWindow(false),
     115    fSaveScreenshotSilent(false),
     116    fOutputFilename(NULL),
     117    fExtension(""),
     118    fImageFileType(B_PNG_FORMAT)
     119{
     120    BMessage settings = _ReadSettings();
     121   
     122    if (settings.FindInt32("type", &fImageFileType) != B_OK)
     123        fImageFileType = B_PNG_FORMAT;
     124    settings.FindBool("includeBorder", &fIncludeBorder);
     125    settings.FindBool("includeMouse", &fIncludeMouse);
     126    settings.FindBool("grabActiveWindow", &fGrabActiveWindow);
     127    settings.FindInt64("delay", &fDelay);
     128    settings.FindString("outputFilename", &fOutputFilename);
     129
     130    _InitWindow(settings);
     131    CenterOnScreen();
     132    Show();
     133}
     134
     135
    99136ScreenshotWindow::ScreenshotWindow(bigtime_t delay, bool includeBorder,
    100137    bool includeMouse, bool grabActiveWindow, bool showConfigWindow,
    101138    bool saveScreenshotSilent, int32 imageFileType, const char* outputFilename)
    102139    :
    103140    BWindow(BRect(0, 0, 200.0, 100.0), TR("Retake screenshot"), B_TITLED_WINDOW,
    104         B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_QUIT_ON_WINDOW_CLOSE |
    105         B_AVOID_FRONT | B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
     141        B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_QUIT_ON_WINDOW_CLOSE
     142        | B_AVOID_FRONT | B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
    106143    fDelayControl(NULL),
    107144    fScreenshot(NULL),
    108145    fOutputPathPanel(NULL),
     
    119156    fImageFileType(imageFileType)
    120157{
    121158    if (fSaveScreenshotSilent) {
    122         _TakeScreenshot();
     159        _TakeScreenshot(fDelay);
    123160        _SaveScreenshot();
    124161        be_app_messenger.SendMessage(B_QUIT_REQUESTED);
    125162    } else {
    126         _InitWindow();
     163        BMessage settings = _ReadSettings();
     164        _InitWindow(settings);
    127165        CenterOnScreen();
    128166        Show();
    129167    }
     
    157195            break;
    158196
    159197        case kShowMouse:
    160             printf("kShowMouse\n");
    161198            fIncludeMouse = (fShowMouse->Value() == B_CONTROL_ON);
    162199            break;
    163200
     
    174211        }
    175212
    176213        case kTakeScreenshot:
     214            fDelay = (atoi(fDelayControl->Text()) * 1000000) + 50000;
    177215            Hide();
    178             _TakeScreenshot();
     216            _TakeScreenshot(fDelay);
    179217            _UpdatePreviewPanel();
    180218            Show();
    181219            _UpdateFilenameSelection();
     
    183221       
    184222        case kImageOutputFormat:
    185223            message->FindInt32("be:type", &fImageFileType);
    186             fNameControl->SetText(_FindValidFileName(fNameControl->Text()).String());
     224            fNameControl->SetText(_FindValidFileName(
     225                fNameControl->Text()).String());
    187226            _UpdateFilenameSelection();
    188227            break;
    189228       
     
    193232            if (message->FindPointer("source", &source) == B_OK)
    194233                fLastSelectedPath = static_cast<BMenuItem*> (source);
    195234
    196             fNameControl->SetText(_FindValidFileName(fNameControl->Text()).String());
     235            fNameControl->SetText(_FindValidFileName(
     236                fNameControl->Text()).String());
    197237            _UpdateFilenameSelection();
    198238            break;
    199239        }
     
    202242        {
    203243            if (!fOutputPathPanel) {
    204244                BMessenger target(this);
    205                 fOutputPathPanel = new BFilePanel(B_OPEN_PANEL, &target,
    206                     NULL, B_DIRECTORY_NODE, false, NULL, new DirectoryRefFilter());
     245                fOutputPathPanel = new BFilePanel(B_OPEN_PANEL, &target, NULL,
     246                    B_DIRECTORY_NODE, false, NULL, new DirectoryRefFilter());
    207247                fOutputPathPanel->Window()->SetTitle(TR("Choose folder"));
    208                 fOutputPathPanel->SetButtonLabel(B_DEFAULT_BUTTON, TR("Select"));
     248                fOutputPathPanel->SetButtonLabel(B_DEFAULT_BUTTON,
     249                    TR("Select"));
    209250                fOutputPathPanel->SetButtonLabel(B_CANCEL_BUTTON, TR("Cancel"));
    210251            }
    211252            fOutputPathPanel->Show();
     
    238279
    239280        // fall through
    240281        case B_QUIT_REQUESTED:
     282            _WriteSettings();
    241283            be_app_messenger.SendMessage(B_QUIT_REQUESTED);
    242284            break;
    243285
     
    281323
    282324
    283325void
    284 ScreenshotWindow::_InitWindow()
     326ScreenshotWindow::_InitWindow(const BMessage& settings)
    285327{
    286328    BCardLayout* layout = new BCardLayout();
    287329    SetLayout(layout);
    288330
    289331    _SetupFirstLayoutItem(layout);
    290     _SetupSecondLayoutItem(layout);
     332    _SetupSecondLayoutItem(layout, settings);
    291333
    292334    if (!fShowConfigWindow) {
    293         _TakeScreenshot();
     335        _TakeScreenshot(0);
    294336        _UpdatePreviewPanel();
    295337        layout->SetVisibleItem(1L);
    296338        fSaveScreenshot->MakeDefault(true);
     
    316358
    317359    BString delay;
    318360    delay << fDelay / 1000000;
    319     fDelayControl = new BTextControl("", TR("Take screenshot after a delay of"),
    320         delay.String(), NULL);
     361    fDelayControl = new BTextControl("",
     362        TR("Take screenshot after a delay of"), delay.String(), NULL);
    321363    _DisallowChar(fDelayControl->TextView());
    322364    fDelayControl->TextView()->SetAlignment(B_ALIGN_RIGHT);
    323365
     
    335377    BBox* divider = new BBox(B_FANCY_BORDER, NULL);
    336378    divider->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1));
    337379
    338     fBackToSave = new BButton("", TR("Back to saving"), new BMessage(kBackToSave));
     380    fBackToSave = new BButton("", TR("Back to saving"),
     381        new BMessage(kBackToSave));
    339382    fBackToSave->SetEnabled(false);
    340383
    341384    fTakeScreenshot = new BButton("", TR("Take screenshot"),
     
    387430
    388431
    389432void
    390 ScreenshotWindow::_SetupSecondLayoutItem(BCardLayout* layout)
     433ScreenshotWindow::_SetupSecondLayoutItem(BCardLayout* layout,
     434    const BMessage& settings)
    391435{
    392436    fPreview = new PreviewView();
    393437
    394438    fNameControl = new BTextControl("", TR("Name:"),
    395439        TR_CMT("screenshot1", "!! Filename of first screenshot !!"), NULL);
    396440
    397     BMessage settings(_ReadSettings());
     441    _SetupOutputPathMenu(new BMenu(TR("Please select")), settings);
    398442
    399     _SetupOutputPathMenu(new BMenu(TR("Please select")), settings);
    400443    BMenuField* menuField2 = new BMenuField(TR("Save in:"), fOutputPathMenu);
    401444   
    402445    fNameControl->SetText(_FindValidFileName(
    403446        TR_CMT("screenshot1", "!! Filename of first screenshot !!")).String());
    404447
    405     _SetupTranslatorMenu(new BMenu(TR("Please select")), settings);
     448    _SetupTranslatorMenu(new BMenu(TR("Please select")));
    406449    BMenuField* menuField = new BMenuField(TR("Save as:"), fTranslatorMenu);
    407450
    408451    BBox* divider = new BBox(B_FANCY_BORDER, NULL);
     
    454497
    455498
    456499void
    457 ScreenshotWindow::_SetupTranslatorMenu(BMenu* translatorMenu,
    458     const BMessage& settings)
     500ScreenshotWindow::_SetupTranslatorMenu(BMenu* translatorMenu)
    459501{
    460502    fTranslatorMenu = translatorMenu;
    461503
     
    469511    if (fTranslatorMenu->ItemAt(0))
    470512        fTranslatorMenu->ItemAt(0)->SetMarked(true);
    471513
    472     if (settings.FindInt32("be:type", &fImageFileType) != B_OK)
    473         fImageFileType = B_PNG_FORMAT;
    474 
    475514    int32 imageFileType;
    476515    for (int32 i = 0; i < fTranslatorMenu->CountItems(); ++i) {
    477516        BMenuItem* item = fTranslatorMenu->ItemAt(i);
     
    501540    find_directory(B_USER_DIRECTORY, &path);
    502541
    503542    BString label(TR("Home folder"));
    504     _AddItemToPathMenu(path.Path(), label, 0, (path.Path() == lastSelectedPath));
     543    _AddItemToPathMenu(path.Path(), label, 0,
     544        (path.Path() == lastSelectedPath));
    505545
    506546    path.Append("Desktop");
    507547    label.SetTo(TR("Desktop"));
    508     _AddItemToPathMenu(path.Path(), label, 0, (path.Path() == lastSelectedPath));
     548    _AddItemToPathMenu(path.Path(), label, 0, (
     549        path.Path() == lastSelectedPath));
    509550
    510551    find_directory(B_BEOS_ETC_DIRECTORY, &path);
    511552    path.Append("artwork");
    512553
    513554    label.SetTo(TR("Artwork folder"));
    514     _AddItemToPathMenu(path.Path(), label, 2, (path.Path() == lastSelectedPath));
     555    _AddItemToPathMenu(path.Path(), label, 2,
     556        (path.Path() == lastSelectedPath));
    515557
    516558    int32 i = 0;
    517559    BString userPath;
     
    559601{
    560602    float height = 150.0f;
    561603
    562     float width = (fScreenshot->Bounds().Width() /
    563         fScreenshot->Bounds().Height()) * height;
     604    float width = (fScreenshot->Bounds().Width()
     605        / fScreenshot->Bounds().Height()) * height;
    564606
    565607    // to prevent a preview way too wide
    566608    if (width > 400.0f) {
    567609        width = 400.0f;
    568         height = (fScreenshot->Bounds().Height() /
    569             fScreenshot->Bounds().Width()) * width;
     610        height = (fScreenshot->Bounds().Height()
     611            / fScreenshot->Bounds().Width()) * width;
    570612    }
    571613
    572614    fPreview->SetExplicitMinSize(BSize(width, height));
     
    591633{
    592634    fNameControl->MakeFocus(true);
    593635    fNameControl->TextView()->Select(0,
    594         fNameControl->TextView()->TextLength() -
    595         fExtension.Length());
     636        fNameControl->TextView()->TextLength()
     637            - fExtension.Length());
    596638    fNameControl->TextView()->ScrollToSelection();
    597639}
    598640
     
    627669                    BMessage msgExtensions;             
    628670                    if (mimeType.GetFileExtensions(&msgExtensions) == B_OK) {
    629671                        const char* extension;
    630                         if (msgExtensions.FindString("extensions", 0, &extension) == B_OK) {
     672                        if (msgExtensions.FindString("extensions", 0,
     673                            &extension) == B_OK) {
    631674                            fExtension.SetTo(extension);
    632675                            fExtension.Prepend(".");
    633676                        } else
     
    647690    if (!BEntry(outputPath.Path()).Exists())
    648691        return fileName;
    649692
    650     if (baseName.FindFirst(TR_CMT("screenshot", "!! Basename of screenshot files. !!")) == 0)
    651         baseName.SetTo(TR_CMT("screenshot", "!! Basename of screenshot files. !!" ));
     693    if (baseName.FindFirst(TR_CMT("screenshot",
     694        "!! Basename of screenshot files. !!")) == 0)
     695        baseName.SetTo(TR_CMT("screenshot",
     696            "!! Basename of screenshot files. !!"));
    652697
    653698    BEntry entry;
    654699    int32 index = 1;
     
    684729BMessage
    685730ScreenshotWindow::_ReadSettings() const
    686731{
     732    BMessage settings;
     733   
    687734    BPath settingsPath;
    688     find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath);
    689     settingsPath.Append("screenshot");
     735    if (find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath) != B_OK)
     736        return settings;
     737    settingsPath.Append("Screenshot_settings");
    690738
    691     BMessage settings;
    692 
    693739    BFile file(settingsPath.Path(), B_READ_ONLY);
    694740    if (file.InitCheck() == B_OK)
    695741        settings.Unflatten(&file);
     
    702748ScreenshotWindow::_WriteSettings() const
    703749{
    704750    BMessage settings;
    705     settings.AddInt32("be:type", fImageFileType);
    706751
     752    settings.AddInt32("type", fImageFileType);
     753    settings.AddBool("includeBorder", fIncludeBorder);
     754    settings.AddBool("includeMouse", fIncludeMouse);
     755    settings.AddBool("grabActiveWindow", fGrabActiveWindow);
     756    settings.AddInt64("delay", fDelay);
     757    settings.AddString("outputFilename", fOutputFilename);
     758
    707759    BString path;
    708760    int32 count = fOutputPathMenu->CountItems();
    709761    if (count > 5) {
     
    724776    }
    725777
    726778    BPath settingsPath;
    727     find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath);
    728     settingsPath.Append("screenshot");
     779    if (find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath) != B_OK)
     780        return;
     781    settingsPath.Append("Screenshot_settings");
    729782
    730     BFile file(settingsPath.Path(), B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
     783    BFile file(settingsPath.Path(), B_CREATE_FILE | B_ERASE_FILE
     784        | B_WRITE_ONLY);
    731785    if (file.InitCheck() == B_OK) {
    732786        ssize_t size;
    733787        settings.Flatten(&file, &size);
     
    736790
    737791
    738792void
    739 ScreenshotWindow::_TakeScreenshot()
     793ScreenshotWindow::_TakeScreenshot(bigtime_t delay)
    740794{
    741     if (fDelayControl)
    742         snooze((atoi(fDelayControl->Text()) * 1000000) + 50000);
    743     else if (fDelay > 0)
    744         snooze(fDelay);
     795    if (delay > 0)
     796        snooze(delay);
    745797
    746798    BRect frame;
    747799    delete fScreenshot;
  • ScreenshotWindow.h

     
    55 * Authors:
    66 *      Karsten Heimrich
    77 *      Fredrik Modéen
     8 *      Wim van der Meer, WPJvanderMeer@gmail.com
    89 */
    910#ifndef SCREENSHOT_WINDOW_H
    1011#define SCREENSHOT_WINDOW_H
     
    3132
    3233class ScreenshotWindow : public BWindow {
    3334public:
    34                             ScreenshotWindow(bigtime_t delay = 0,
    35                                 bool includeBorder = false,
    36                                 bool includeMouse = false,
    37                                 bool grabActiveWindow = false,
    38                                 bool showConfigWindow = false,
    39                                 bool saveScreenshotSilent = false,
     35                            ScreenshotWindow();
     36                            ScreenshotWindow(bigtime_t delay,
     37                                bool includeBorder,
     38                                bool includeMouse,
     39                                bool grabActiveWindow,
     40                                bool showConfigWindow,
     41                                bool saveScreenshotSilent,
    4042                                int32 imageFileType = B_PNG_FORMAT,
    4143                                const char* outputFilename = NULL);
    4244    virtual                 ~ScreenshotWindow();
     
    4446    virtual void            MessageReceived(BMessage* message);
    4547
    4648private:
    47             void            _InitWindow();
     49            void            _InitWindow(const BMessage& settings);
    4850            BPath           _GetDirectory();
    4951            void            _SetupFirstLayoutItem(BCardLayout* layout);
    50             void            _SetupSecondLayoutItem(BCardLayout* layout);
     52            void            _SetupSecondLayoutItem(BCardLayout* layout,
     53                                const BMessage& settings);
    5154            void            _DisallowChar(BTextView* textView);
    52             void            _SetupTranslatorMenu(BMenu* translatorMenu,
    53                                 const BMessage& settings);
     55            void            _SetupTranslatorMenu(BMenu* translatorMenu);
    5456            void            _SetupOutputPathMenu(BMenu* outputPathMenu,
    5557                                const BMessage& settings);
    5658            void            _AddItemToPathMenu(const char* path,
     
    6466            BMessage        _ReadSettings() const;
    6567            void            _WriteSettings() const;
    6668
    67             void            _TakeScreenshot();
     69            void            _TakeScreenshot(bigtime_t delay);
    6870            status_t        _GetActiveWindowFrame(BRect* frame);
    6971            void            _MakeTabSpaceTransparent(BRect* frame);
    7072