Ticket #7394: 0001-Fix-7394.-Add-Zoom-level-to-StatusBar.-Improve-graph.patch

File 0001-Fix-7394.-Add-Zoom-level-to-StatusBar.-Improve-graph.patch, 12.3 KB (added by Janus, 9 years ago)
  • src/apps/showimage/ShowImageStatusView.cpp

    From 02f215e9f222dbdbf923677b310c30c837fccb04 Mon Sep 17 00:00:00 2001
    From: Janus <janus2@ymail.com>
    Date: Sun, 1 Mar 2015 13:38:59 +0000
    Subject: [PATCH] Fix #7394. Add Zoom level to StatusBar. Improve graphics and
     resize the StatusBar to best fit
    
    ---
     src/apps/showimage/ShowImageStatusView.cpp | 194 ++++++++++++++++++++++-------
     src/apps/showimage/ShowImageStatusView.h   |  27 +++-
     src/apps/showimage/ShowImageView.cpp       |   4 +
     src/apps/showimage/ShowImageWindow.cpp     |  55 ++------
     src/apps/showimage/ShowImageWindow.h       |   2 +-
     5 files changed, 183 insertions(+), 99 deletions(-)
    
    diff --git a/src/apps/showimage/ShowImageStatusView.cpp b/src/apps/showimage/ShowImageStatusView.cpp
    index 0092b4e..319bab1 100644
    a b  
    1111
    1212#include "ShowImageStatusView.h"
    1313
     14#include <ControlLook.h>
    1415#include <Entry.h>
    1516#include <MenuItem.h>
    1617#include <Path.h>
    1718#include <PopUpMenu.h>
     19#include <ScrollView.h>
    1820
    1921#include <tracker_private.h>
    2022#include "DirMenu.h"
     
    2224#include "ShowImageView.h"
    2325#include "ShowImageWindow.h"
    2426
     27const float kHorzSpacing = 5.f;
    2528
    26 ShowImageStatusView::ShowImageStatusView(BRect rect, const char* name,
    27     uint32 resizingMode, uint32 flags)
     29ShowImageStatusView::ShowImageStatusView(BScrollView* scrollView)
    2830    :
    29     BView(rect, name, resizingMode, flags)
     31    BView(BRect(), "statusview", B_FOLLOW_BOTTOM | B_FOLLOW_LEFT, B_WILL_DRAW),
     32    fScrollView(scrollView),
     33    fPreferredSize(0.0, 0.0)
    3034{
    31     SetViewColor(B_TRANSPARENT_32_BIT);
    32     SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR));
    33     SetHighColor(0, 0, 0, 255);
    34 
    35     BFont font;
    36     GetFont(&font);
    37     font.SetSize(10.0);
    38     SetFont(&font);
     35    memset(fCellWidth, 0, sizeof(fCellWidth));
     36}
     37
     38
     39void
     40ShowImageStatusView::AttachedToWindow()
     41{
     42    SetFont(be_plain_font);
     43    SetFontSize(10.0);
     44
     45    BScrollBar* scrollBar = fScrollView->ScrollBar(B_HORIZONTAL);
     46    MoveTo(0.0, scrollBar->Frame().top);
     47    rgb_color color = B_TRANSPARENT_COLOR;
     48    BView* parent = Parent();
     49    if (parent != NULL)
     50        color = parent->ViewColor();
     51
     52    if (color == B_TRANSPARENT_COLOR)
     53        color = ui_color(B_PANEL_BACKGROUND_COLOR);
     54
     55    SetViewColor(color);
     56
     57    ResizeToPreferred();
     58}
     59
     60
     61void
     62ShowImageStatusView::GetPreferredSize(float* _width, float* _height)
     63{
     64    _ValidatePreferredSize();
     65
     66    if (_width)
     67        *_width = fPreferredSize.width;
     68
     69    if (_height)
     70        *_height = fPreferredSize.height;
     71}
     72
     73
     74void
     75ShowImageStatusView::ResizeToPreferred()
     76{
     77    float width, height;
     78    GetPreferredSize(&width, &height);
     79
     80    if (Bounds().Width() > width)
     81        width = Bounds().Width();
     82
     83    BView::ResizeTo(width, height);
    3984}
    4085
    4186
    4287void
    4388ShowImageStatusView::Draw(BRect updateRect)
    4489{
    45     rgb_color darkShadow = tint_color(LowColor(), B_DARKEN_2_TINT);
    46     rgb_color shadow = tint_color(LowColor(), B_DARKEN_1_TINT);
    47     rgb_color light = tint_color(LowColor(), B_LIGHTEN_MAX_TINT);
    48 
    49     BRect b(Bounds());
    50 
    51     BeginLineArray(5);
    52         AddLine(BPoint(b.left, b.top),
    53                 BPoint(b.right, b.top), darkShadow);
    54         b.top += 1.0;
    55         AddLine(BPoint(b.left, b.top),
    56                 BPoint(b.right, b.top), light);
    57         AddLine(BPoint(b.right, b.top + 1.0),
    58                 BPoint(b.right, b.bottom), shadow);
    59         AddLine(BPoint(b.right - 1.0, b.bottom),
    60                 BPoint(b.left + 1.0, b.bottom), shadow);
    61         AddLine(BPoint(b.left, b.bottom),
    62                 BPoint(b.left, b.top + 1.0), light);
    63     EndLineArray();
    64 
    65     b.InsetBy(1.0, 1.0);
    66 
    67     // Truncate and layout text
    68     BString truncated(fText);
    69     BFont font;
    70     GetFont(&font);
    71     font.TruncateString(&truncated, B_TRUNCATE_MIDDLE, b.Width() - 4.0);
    72     font_height fh;
    73     font.GetHeight(&fh);
    74 
    75     FillRect(b, B_SOLID_LOW);
    76     SetDrawingMode(B_OP_OVER);
    77     DrawString(truncated.String(), BPoint(b.left + 2.0,
    78         floorf(b.top + b.Height() / 2.0 + fh.ascent / 2.0)));
     90    if (fPreferredSize.width <= 0)
     91        return;
     92
     93    if (be_control_look != NULL) {
     94        BRect bounds(Bounds());
     95        be_control_look->DrawMenuBarBackground(this,
     96            bounds, updateRect, ViewColor());
     97    }
     98
     99    BRect bounds(Bounds());
     100    rgb_color highColor = HighColor();
     101    SetHighColor(tint_color(ViewColor(), B_DARKEN_2_TINT));
     102    StrokeLine(bounds.LeftTop(), bounds.RightTop());
     103
     104    float x = bounds.left;
     105    for (size_t i = 0; i < kStatusCellCount - 1; i++) {
     106        x += fCellWidth[i];
     107        StrokeLine(BPoint(x, bounds.top + 3), BPoint(x, bounds.bottom - 3));
     108    }
     109
     110    SetLowColor(ViewColor());
     111    SetHighColor(highColor);
     112
     113    font_height fontHeight;
     114    GetFontHeight(&fontHeight);
     115
     116    x = bounds.left;
     117    float y = (bounds.bottom + bounds.top
     118        + ceilf(fontHeight.ascent) - ceilf(fontHeight.descent)) / 2;
     119
     120    for (size_t i = 0; i < kStatusCellCount; i++) {
     121        if (fCellText[i].Length() == 0)
     122            continue;
     123        DrawString(fCellText[i], BPoint(x + kHorzSpacing, y));
     124        x += fCellWidth[i];
     125    }
    79126}
    80127
    81128
    ShowImageStatusView::MouseDown(BPoint where)  
    101148
    102149
    103150void
    104 ShowImageStatusView::Update(const entry_ref& ref, const BString& text)
     151ShowImageStatusView::Update(const entry_ref& ref, const BString& text,
     152    const BString& imageType, float zoom)
    105153{
    106     fText = text;
    107154    fRef = ref;
    108155
     156    fCellText[0] = text;
     157    fCellText[1] = "";
     158    fCellText[1] << (int) (zoom * 100 + 0.5)  << "%";
     159    fCellText[2]  = imageType;
     160
     161    _ValidatePreferredSize();
     162    Invalidate();
     163}
     164
     165
     166void
     167ShowImageStatusView::SetZoom(float zoom)
     168{
     169    fCellText[1] = "";
     170    fCellText[1] << (int) (zoom * 100 + 0.5)  << "%";
     171
     172    _ValidatePreferredSize();
    109173    Invalidate();
    110174}
    111175
     176
     177void
     178ShowImageStatusView::_ValidatePreferredSize()
     179{
     180    float orgWidth = fPreferredSize.width;
     181    // width
     182    fPreferredSize.width = 0.f;
     183    for (size_t i = 0; i < kStatusCellCount; i++) {
     184        if (fCellText[i].Length() == 0) {
     185            fCellWidth[i] = 0;
     186            continue;
     187        }
     188        float width = ceilf(StringWidth(fCellText[i]));
     189        if (width > 0)
     190            width += kHorzSpacing * 2;
     191        fCellWidth[i] = width;
     192        fPreferredSize.width += fCellWidth[i];
     193    }
     194
     195    // height
     196    font_height fontHeight;
     197    GetFontHeight(&fontHeight);
     198
     199    fPreferredSize.height = ceilf(fontHeight.ascent + fontHeight.descent
     200        + fontHeight.leading);
     201
     202    if (fPreferredSize.height < B_H_SCROLL_BAR_HEIGHT)
     203        fPreferredSize.height = B_H_SCROLL_BAR_HEIGHT;
     204
     205    float delta = fPreferredSize.width - orgWidth;
     206    ResizeBy(delta, 0);
     207    BScrollBar* scrollBar = fScrollView->ScrollBar(B_HORIZONTAL);
     208    scrollBar->ResizeBy(-delta, 0);
     209    scrollBar->MoveBy(delta, 0);
     210}
     211
  • src/apps/showimage/ShowImageStatusView.h

    diff --git a/src/apps/showimage/ShowImageStatusView.h b/src/apps/showimage/ShowImageStatusView.h
    index 05807b4..4b4e07b 100644
    a b  
    1515#include <View.h>
    1616
    1717
     18enum {
     19    kFrameSizeCell,
     20    kZoomCell,
     21    kImageTypeCell,
     22    kStatusCellCount
     23};
     24
     25
    1826class ShowImageStatusView : public BView {
    1927public:
    20                                 ShowImageStatusView(BRect rect,
    21                                     const char* name, uint32 resizingMode,
    22                                     uint32 flags);
     28                                ShowImageStatusView(BScrollView* scrollView);
    2329
     30    virtual void                AttachedToWindow();
     31    virtual void                GetPreferredSize(float* _width, float* _height);
     32    virtual void                ResizeToPreferred();
    2433    virtual void                Draw(BRect updateRect);
    2534    virtual void                MouseDown(BPoint where);
    2635
    2736            void                Update(const entry_ref& ref,
    28                                     const BString& text);
    29 
     37                                    const BString& text, const BString& imageType,
     38                                    float zoom);
     39            void                SetZoom(float zoom);
    3040private:
    31             BString             fText;
     41            void            _ValidatePreferredSize();
     42
     43            BScrollView*        fScrollView;
     44            BSize               fPreferredSize;
     45            BString             fCellText[kStatusCellCount];
     46            float               fCellWidth[kStatusCellCount];
    3247            entry_ref           fRef;
    3348};
    3449
  • src/apps/showimage/ShowImageView.cpp

    diff --git a/src/apps/showimage/ShowImageView.cpp b/src/apps/showimage/ShowImageView.cpp
    index fa55542..3279972 100644
    a b ShowImageView::SetZoom(float zoom, BPoint where)  
    15871587        offset.y = (int)(where.y * fZoom / oldZoom + 0.5) - offset.y;
    15881588        ScrollTo(offset);
    15891589    }
     1590
     1591    BMessage message(MSG_UPDATE_STATUS_ZOOM);
     1592    message.AddFloat("zoom", fZoom);
     1593    _SendMessageToWindow(&message);
    15901594}
    15911595
    15921596
  • src/apps/showimage/ShowImageWindow.cpp

    diff --git a/src/apps/showimage/ShowImageWindow.cpp b/src/apps/showimage/ShowImageWindow.cpp
    index 0015b5f..b4068f2 100644
    a b ShowImageWindow::ShowImageWindow(BRect frame, const entry_ref& ref,  
    224224            | B_FRAME_EVENTS);
    225225    // wrap a scroll view around the view
    226226    fScrollView = new BScrollView("image_scroller", fImageView,
    227         B_FOLLOW_ALL, 0, false, false, B_PLAIN_BORDER);
     227        B_FOLLOW_ALL, 0, true, true, B_PLAIN_BORDER);
    228228    contentView->AddChild(fScrollView);
    229229
    230     const int32 kstatusWidth = 190;
    231     BRect rect;
    232     rect = contentView->Bounds();
    233     rect.top = viewFrame.bottom + 1;
    234     rect.left = viewFrame.left + kstatusWidth;
    235     rect.right = viewFrame.right + 1;
    236     rect.bottom += 1;
    237     BScrollBar* horizontalScrollBar = new BScrollBar(rect, "hscroll",
    238         fImageView, 0, 150, B_HORIZONTAL);
    239     contentView->AddChild(horizontalScrollBar);
    240 
    241     rect.left = 0;
    242     rect.right = kstatusWidth - 1;
    243     rect.bottom -= 1;
    244     fStatusView = new ShowImageStatusView(rect, "status_view", B_FOLLOW_BOTTOM,
    245         B_WILL_DRAW);
    246     contentView->AddChild(fStatusView);
    247 
    248     rect = contentView->Bounds();
    249     rect.top = viewFrame.top - 1;
    250     rect.left = viewFrame.right + 1;
    251     rect.bottom = viewFrame.bottom + 1;
    252     rect.right += 1;
    253     fVerticalScrollBar = new BScrollBar(rect, "vscroll", fImageView,
    254         0, 150, B_VERTICAL);
    255     contentView->AddChild(fVerticalScrollBar);
     230    fStatusView = new ShowImageStatusView(fScrollView);
     231    fScrollView->AddChild(fStatusView);
    256232
    257233    // Update minimum window size
    258234    float toolBarMinWidth = fToolBar->MinSize().width;
    ShowImageWindow::MessageReceived(BMessage* message)  
    967943            fImageView->ZoomOut();
    968944            break;
    969945
     946        case MSG_UPDATE_STATUS_ZOOM:
     947            fStatusView->SetZoom(fImageView->Zoom());
     948            break;
     949
    970950        case kMsgOriginalSize:
    971951            if (message->FindInt32("behavior") == BButton::B_TOGGLE_BEHAVIOR) {
    972952                bool force = (message->FindInt32("be:value") == B_CONTROL_ON);
    ShowImageWindow::MessageReceived(BMessage* message)  
    10341014                fToolBar->MoveBy(0, offset);
    10351015                fScrollView->ResizeBy(0, -offset);
    10361016                fScrollView->MoveBy(0, offset);
    1037                 fVerticalScrollBar->ResizeBy(0, -offset);
    1038                 fVerticalScrollBar->MoveBy(0, offset);
    10391017                UpdateIfNeeded();
    10401018                snooze(15000);
    10411019            }
    ShowImageWindow::MessageReceived(BMessage* message)  
    10551033                frame.top = fToolBar->Frame().bottom + 1;
    10561034                fScrollView->MoveTo(fScrollView->Frame().left, frame.top);
    10571035                fScrollView->ResizeTo(fScrollView->Bounds().Width(),
    1058                     frame.Height() - B_H_SCROLL_BAR_HEIGHT + 1);
    1059                 fVerticalScrollBar->MoveTo(
    1060                     frame.right - B_V_SCROLL_BAR_WIDTH + 1, frame.top);
    1061                 fVerticalScrollBar->ResizeTo(
    1062                     fVerticalScrollBar->Bounds().Width(),
    1063                     frame.Height() - B_H_SCROLL_BAR_HEIGHT + 1);
     1036                    frame.Height() + 1);
    10641037            }
    10651038            break;
    10661039        }
    ShowImageWindow::_UpdateStatusText(const BMessage* message)  
    10791052    if (fImageView->Bitmap() != NULL) {
    10801053        BRect bounds = fImageView->Bitmap()->Bounds();
    10811054        status << bounds.IntegerWidth() + 1
    1082             << "x" << bounds.IntegerHeight() + 1 << ", " << fImageType;
    1083     }
    1084 
    1085     BString text;
    1086     if (message != NULL && message->FindString("status", &text) == B_OK
    1087         && text.Length() > 0) {
    1088         status << ", " << text;
     1055            << "x" << bounds.IntegerHeight() + 1;
    10891056    }
    10901057
    1091     fStatusView->Update(fNavigator.CurrentRef(), status);
     1058    fStatusView->Update(fNavigator.CurrentRef(), status, fImageType, fImageView->Zoom());
    10921059}
    10931060
    10941061
    ShowImageWindow::_SetToolBarVisible(bool visible, bool animate)  
    15401507    } else {
    15411508        fScrollView->ResizeBy(0, -diff);
    15421509        fScrollView->MoveBy(0, diff);
    1543         fVerticalScrollBar->ResizeBy(0, -diff);
    1544         fVerticalScrollBar->MoveBy(0, diff);
    15451510        fToolBar->MoveBy(0, diff);
    15461511        if (!visible)
    15471512            fToolBar->Hide();
  • src/apps/showimage/ShowImageWindow.h

    diff --git a/src/apps/showimage/ShowImageWindow.h b/src/apps/showimage/ShowImageWindow.h
    index ea593a3..12c960d 100644
    a b enum {  
    3535    MSG_MODIFIED                = 'mMOD',
    3636    MSG_UPDATE_STATUS           = 'mUPS',
    3737    MSG_UPDATE_STATUS_TEXT      = 'mUPT',
     38    MSG_UPDATE_STATUS_ZOOM      = 'mUPZ',
    3839    MSG_SELECTION               = 'mSEL',
    3940    MSG_FILE_NEXT               = 'mFLN',
    4041    MSG_FILE_PREV               = 'mFLP',
    private:  
    117118            BToolBar*           fToolBar;
    118119            bool                fToolBarVisible;
    119120            BScrollView*        fScrollView;
    120             BScrollBar*         fVerticalScrollBar;
    121121            ShowImageView*      fImageView;
    122122            ShowImageStatusView* fStatusView;
    123123            ProgressWindow*     fProgressWindow;