Ticket #13884: 0006-magnify-make-it-controllable-by-scripting.2.patch

File 0006-magnify-make-it-controllable-by-scripting.2.patch, 6.3 KB (added by owenca, 6 years ago)
  • src/apps/magnify/Magnify.cpp

    From b9c97d9137b770a26a686392ca7d9a5ad2184ded Mon Sep 17 00:00:00 2001
    From: Owen <owenca@users.noreply.github.com>
    Date: Tue, 19 Dec 2017 10:37:54 +0000
    Subject: [PATCH 6/6] magnify: make it controllable by scripting
    
    scripting properties:
    * Info, Grid, and Stick (true/false)
    * CopyImage
    * MakeSquare
    * Zoom (1-16)
    
    fixes #13884
    ---
     src/apps/magnify/Magnify.cpp | 179 ++++++++++++++++++++++++++++++++++++++-----
     src/apps/magnify/Magnify.h   |   5 ++
     2 files changed, 166 insertions(+), 18 deletions(-)
    
    diff --git a/src/apps/magnify/Magnify.cpp b/src/apps/magnify/Magnify.cpp
    index 02d7c4a..524ea64 100644
    a b  
    2626#include <NodeInfo.h>
    2727#include <Path.h>
    2828#include <PopUpMenu.h>
     29#include <PropertyInfo.h>
    2930#include <Screen.h>
    3031#include <ScrollView.h>
    3132#include <TextView.h>
    const int32 kDefaultPixelSize = 8;  
    9899const int32 kBorderSize = 10;
    99100
    100101
     102static property_info sProperties[] = {
     103    { "Info", { B_GET_PROPERTY, B_SET_PROPERTY, 0 },
     104        { B_DIRECT_SPECIFIER, 0 },
     105        "Gets/sets the state of 'Show info' (true/false).", 0,
     106        { B_BOOL_TYPE }
     107    },
     108    { "Grid", { B_GET_PROPERTY, B_SET_PROPERTY, 0 },
     109        { B_DIRECT_SPECIFIER, 0 },
     110        "Gets/sets the state of 'Show grid' (true/false).", 0,
     111        { B_BOOL_TYPE }
     112    },
     113    { "MakeSquare", { B_EXECUTE_PROPERTY, 0 },
     114        { B_DIRECT_SPECIFIER, 0 },
     115        "Make the view square.", 0,
     116    },
     117    { "Zoom", { B_GET_PROPERTY, B_SET_PROPERTY, 0 },
     118        { B_DIRECT_SPECIFIER, 0 },
     119        "Gets/sets the zoom factor (1-16).", 0,
     120        { B_INT32_TYPE }
     121    },
     122    { "Stick", { B_GET_PROPERTY, B_SET_PROPERTY, 0 },
     123        { B_DIRECT_SPECIFIER, 0 },
     124        "Gets/sets the state of 'Stick coordinates' (true/false).", 0,
     125        { B_BOOL_TYPE }
     126    },
     127    { "CopyImage", { B_EXECUTE_PROPERTY, 0 },
     128        { B_DIRECT_SPECIFIER, 0 },
     129        "Copy image.", 0,
     130    },
     131
     132    { 0 }
     133};
     134
     135
    101136static float
    102137FontHeight(BView* target, bool full)
    103138{
    TWindow::~TWindow()  
    304339}
    305340
    306341
     342status_t
     343TWindow::GetSupportedSuites(BMessage* msg)
     344{
     345    msg->AddString("suites", "suite/x-vnd.Haiku-Magnify");
     346
     347    BPropertyInfo propertyInfo(sProperties);
     348    msg->AddFlat("messages", &propertyInfo);
     349
     350    return BHandler::GetSupportedSuites(msg);
     351}
     352
     353
     354BHandler*
     355TWindow::ResolveSpecifier(BMessage* msg, int32 index, BMessage* specifier,
     356    int32 what, const char* property)
     357{
     358    BPropertyInfo propertyInfo(sProperties);
     359    if (propertyInfo.FindMatch(msg, index, specifier, what, property) >= 0)
     360        return this;
     361
     362    return BHandler::ResolveSpecifier(msg, index, specifier, what, property);
     363}
     364
     365
    307366void
    308367TWindow::MessageReceived(BMessage* m)
    309368{
    310369    bool active = fFatBits->Active();
    311370
    312371    switch (m->what) {
     372        case B_EXECUTE_PROPERTY:
     373        case B_GET_PROPERTY:
     374        case B_SET_PROPERTY:
     375        {
     376            int32 index;
     377            BMessage specifier;
     378            int32 what;
     379            const char* property;
     380            if (m->GetCurrentSpecifier(&index, &specifier, &what, &property)
     381                != B_OK)
     382                return BWindow::MessageReceived(m);
     383
     384            status_t result = B_OK;
     385            BMessage reply(B_REPLY);
     386
     387            BPropertyInfo propertyInfo(sProperties);
     388            switch (propertyInfo.FindMatch(m, index, &specifier, what,
     389                        property)) {
     390                case 0:
     391                    if (m->what == B_GET_PROPERTY)
     392                        result = reply.AddBool("result", fInfoBarState);
     393                    else if (m->what == B_SET_PROPERTY) {
     394                        bool showInfo;
     395                        result = m->FindBool("data", &showInfo);
     396                        if (result == B_OK) {
     397                            fInfoBarState = showInfo;
     398                            ShowInfo(fInfoBarState);
     399                        }
     400                    }
     401                    break;
     402
     403                case 1:
     404                    if (m->what == B_GET_PROPERTY)
     405                        result = reply.AddBool("result", fShowGrid);
     406                    else if (m->what == B_SET_PROPERTY) {
     407                        bool showGrid;
     408                        result = m->FindBool("data", &showGrid);
     409                        if (result == B_OK)
     410                            SetGrid(showGrid);
     411                    }
     412                    break;
     413
     414                case 2:
     415                    if (fHPixelCount != fVPixelCount) {
     416                        int32 big = fHPixelCount > fVPixelCount ? fHPixelCount
     417                                        : fVPixelCount;
     418                        ResizeWindow(big, big);
     419                    }
     420                    break;
     421
     422                case 3:
     423                    if (m->what == B_GET_PROPERTY)
     424                        result = reply.AddInt32("result", fPixelSize);
     425                    else if (m->what == B_SET_PROPERTY) {
     426                        int32 zoom;
     427                        result = m->FindInt32("data", &zoom);
     428                        if (result == B_OK)
     429                            SetPixelSize(zoom);
     430                    }
     431                    break;
     432
     433                case 4:
     434                    if (m->what == B_GET_PROPERTY)
     435                        result = reply.AddBool("result", fFatBits->Sticked());
     436                    else if (m->what == B_SET_PROPERTY) {
     437                        bool stick;
     438                        result = m->FindBool("data", &stick);
     439                        if (result == B_OK)
     440                            fFatBits->MakeSticked(stick);
     441                    }
     442                    break;
     443
     444                case 5:
     445                    fFatBits->CopyImage();
     446                    break;
     447
     448                default:
     449                    return BWindow::MessageReceived(m);
     450            }
     451
     452            if (result != B_OK) {
     453                reply.what = B_MESSAGE_NOT_UNDERSTOOD;
     454                reply.AddString("message", strerror(result));
     455                reply.AddInt32("error", result);
     456            }
     457
     458            m->SendReply(&reply);
     459            break;
     460        }
     461
    313462        case msg_show_info:
    314463            if (active) {
    315464                fInfoBarState = !fInfoBarState;
    TWindow::PixelCount(int32* h, int32 *v)  
    783932void
    784933TWindow::SetPixelSize(int32 s)
    785934{
     935    if (s > 16)
     936        s = 16;
     937    else if (s < 1)
     938        s = 1;
     939
    786940    if (s == fPixelSize)
    787941        return;
    788942
    TWindow::SetPixelSize(int32 s)  
    791945    // tell info that size has changed
    792946    // tell mag that size has changed
    793947
    794     CalcViewablePixels();
    795     ResizeWindow(fHPixelCount, fVPixelCount);
    796 }
    797 
    798 
    799 void
    800 TWindow::SetPixelSize(bool d)
    801 {
    802     if (d) {        // grow
    803         fPixelSize++;
    804         if (fPixelSize > 16)
    805             fPixelSize = 16;
    806     } else {
    807         fPixelSize--;
    808         if (fPixelSize < 1)
    809             fPixelSize = 1;
    810     }
    811 
    812948    float w = Bounds().Width();
    813949    float h = Bounds().Height();
    814950    CalcViewablePixels();
    TWindow::SetPixelSize(bool d)  
    821957}
    822958
    823959
     960void
     961TWindow::SetPixelSize(bool d)
     962{
     963    SetPixelSize(fPixelSize + (d ? 1 : -1));
     964}
     965
     966
    824967int32
    825968TWindow::PixelSize()
    826969{
  • src/apps/magnify/Magnify.h

    diff --git a/src/apps/magnify/Magnify.h b/src/apps/magnify/Magnify.h
    index 35cfa83..9b9663b 100644
    a b class TWindow : public BWindow {  
    201201        virtual void    MessageReceived(BMessage* message);
    202202        virtual bool    QuitRequested();
    203203
     204        status_t        GetSupportedSuites(BMessage* msg);
     205        BHandler*       ResolveSpecifier(BMessage* msg, int32 index,
     206                            BMessage* specifier, int32 what,
     207                            const char* property);
     208
    204209        void            GetPrefs(int32 pixelCount = -1);
    205210        void            SetPrefs();
    206211