Ticket #2389: rename-tab.diff

File rename-tab.diff, 7.3 KB (added by heto, 15 years ago)

implement in-place renaming of tabs by double-clicking the tab

  • src/apps/terminal/TermWindow.cpp

     
    125125        fWindow->_RemoveTab(index);
    126126    }
    127127
     128    virtual void TabRenameFinished(int32 index)
     129    {
     130        SmartTabView::TabRenameFinished(index);
     131        fWindow->UpdateSessionWindowTitle(index);
     132        TabAt(Selection())->View()->MakeFocus();
     133    }
     134
    128135private:
    129136    TermWindow* fWindow;
    130137};
     
    184191        session->windowTitle = title;
    185192        BTab* tab = fTabView->TabAt(index);
    186193        tab->SetLabel(session->windowTitle.String());
     194        fTabView->Invalidate(fTabView->TabFrame(index));
    187195        if (index == fTabView->Selection())
    188196            SetTitle(session->windowTitle.String());
    189197    }
     
    200208
    201209
    202210void
     211TermWindow::UpdateSessionWindowTitle(int32 index)
     212{
     213    Session* session = (Session*) fSessions.ItemAt(index);
     214    if (session != NULL) {
     215        session->windowTitle = fTabView->TabAt(index)->Label();
     216        if (index == fTabView->Selection())
     217            SetTitle(session->windowTitle.String());
     218    }
     219}
     220
     221
     222void
    203223TermWindow::_InitWindow()
    204224{
    205225    // make menu bar
  • src/apps/terminal/SmartTabView.h

     
    1111#include <TabView.h>
    1212
    1313class BPopUpMenu;
     14class BTextControl;
    1415class SmartTabView : public BTabView {
    1516public:
    16     SmartTabView(BRect frame, const char *name,
    17             button_width width = B_WIDTH_AS_USUAL,
    18             uint32 resizingMode = B_FOLLOW_ALL,
    19             uint32 flags = B_FULL_UPDATE_ON_RESIZE |
    20                     B_WILL_DRAW | B_NAVIGABLE_JUMP |
    21                     B_FRAME_EVENTS | B_NAVIGABLE);
    22     virtual ~SmartTabView();
     17                            SmartTabView(BRect frame, const char *name,
     18                                button_width width = B_WIDTH_AS_USUAL,
     19                                uint32 resizingMode = B_FOLLOW_ALL,
     20                                uint32 flags = B_FULL_UPDATE_ON_RESIZE |
     21                                    B_WILL_DRAW | B_NAVIGABLE_JUMP |
     22                                    B_FRAME_EVENTS | B_NAVIGABLE);
     23    virtual                 ~SmartTabView();
    2324
    24             void SetInsets(float left, float top, float right, float bottom);
     25            void            SetInsets(float left, float top, float right, float bottom);
    2526
    26     virtual void MouseDown(BPoint where);
     27    virtual void            MouseDown(BPoint where);
    2728
    28     virtual void AttachedToWindow();
    29     virtual void AllAttached();
     29    virtual void            AttachedToWindow();
     30    virtual void            AllAttached();
    3031
    31     virtual void MessageReceived(BMessage *message);
     32    virtual void            MessageReceived(BMessage *message);
    3233
    33     virtual void Select(int32 tab);
     34    virtual void            Select(int32 tab);
    3435
    35     virtual void RemoveAndDeleteTab(int32 index);
     36    virtual void            AddTab(BView *target, BTab *tab = NULL);
     37    virtual void            RenameTab(int32 index);
     38    virtual void            RemoveAndDeleteTab(int32 index);
     39    virtual BTab*           RemoveTab(int32 index);
    3640
    37     virtual void AddTab(BView *target, BTab *tab = NULL);
    38     virtual BTab* RemoveTab(int32 index);
     41    virtual BRect           DrawTabs();
    3942
    40     virtual BRect DrawTabs();
     43protected:
     44    virtual void            TabRenameFinished(int32 index);
    4145
    4246private:
    43         int32 _ClickedTabIndex(const BPoint &point);   
     47            int32           _ClickedTabIndex(const BPoint &point); 
    4448
    45 private:
    46     BRect   fInsets;
     49            BRect           fInsets;
     50            BTextControl*   fRenameTabControl;
    4751};
    4852
    4953#endif // __SMARTTABVIEW_H
  • src/apps/terminal/TermScrollView.cpp

     
    5858TermScrollView::~TermScrollView()
    5959{
    6060}
     61
     62
     63void
     64TermScrollView::MakeFocus(bool focused)
     65{
     66    BScrollView::MakeFocus(focused);
     67    if (focused && fVerticalScrollBar != NULL
     68        && fVerticalScrollBar->Target() != NULL)
     69        fVerticalScrollBar->Target()->MakeFocus(focused);
     70}
  • src/apps/terminal/TermWindow.h

     
    5454            void    SetSessionWindowTitle(TermView* termView,
    5555                        const char* title);
    5656            void    SessionChanged();
     57            void    UpdateSessionWindowTitle(int32 index);
    5758
    5859protected:
    5960    virtual void    MessageReceived(BMessage *message);
  • src/apps/terminal/SmartTabView.cpp

     
    1313#include <Messenger.h>
    1414#include <PopUpMenu.h>
    1515#include <Screen.h>
     16#include <TextControl.h>
    1617#include <Window.h>
    1718
    1819#include <stdio.h>
    1920
    2021const static uint32 kCloseTab = 'ClTb';
     22const static uint32 kTabRenamed = 'TbRn';
    2123
    2224SmartTabView::SmartTabView(BRect frame, const char *name, button_width width,
    2325                uint32 resizingMode, uint32 flags)
    2426    :
    2527    BTabView(frame, name, width, resizingMode, flags),
    26     fInsets(0, 0, 0, 0)
     28    fInsets(0, 0, 0, 0),
     29    fRenameTabControl(NULL)
    2730{
    2831    // Resize the container view to fill the complete tab view for single-tab
    2932    // mode. Later, when more than one tab is added, we shrink the container
     
    5962        if (tabIndex >= 0) {       
    6063            int32 buttons;
    6164            Window()->CurrentMessage()->FindInt32("buttons", &buttons);
    62             if (buttons & B_SECONDARY_MOUSE_BUTTON) {   
     65            if (buttons & B_PRIMARY_MOUSE_BUTTON) {
     66                int32 clicks;
     67                Window()->CurrentMessage()->FindInt32("clicks", &clicks);
     68                if (clicks == 2) {
     69                    RenameTab(tabIndex);
     70                    handled = true;
     71                }
     72            } else if (buttons & B_SECONDARY_MOUSE_BUTTON) {   
    6373                BMessage *message = new BMessage(kCloseTab);
    6474                message->AddInt32("index", tabIndex);
    6575               
     
    108118                RemoveAndDeleteTab(tabIndex);
    109119            break;
    110120        }       
     121        case kTabRenamed:
     122        {
     123            int32 tabIndex;
     124            if (message->FindInt32("index", &tabIndex) != B_OK)
     125                tabIndex = -1;
     126            TabRenameFinished(tabIndex);
     127        }
    111128        default:
    112129            BTabView::MessageReceived(message);
    113130            break; 
     
    200217}
    201218
    202219
     220void
     221SmartTabView::RenameTab(int32 index)
     222{
     223    if (!Window()->LockLooper())
     224        return;
     225    if (fRenameTabControl != NULL)
     226        TabRenameFinished(-1);
     227
     228    BMessage *renamedMessage = new BMessage(kTabRenamed);
     229    if (renamedMessage != NULL
     230        && renamedMessage->AddInt32("index", index) == B_OK) {
     231        fRenameTabControl = new BTextControl(TabFrame(index), "renameTab", NULL,
     232            TabAt(index)->Label(), renamedMessage);
     233        if (fRenameTabControl != NULL) {
     234            fRenameTabControl->SetTarget(BMessenger(this));
     235            AddChild(fRenameTabControl);
     236            fRenameTabControl->MakeFocus();
     237        }
     238    }
     239
     240    Window()->UnlockLooper();
     241}
     242
     243
     244void
     245SmartTabView::TabRenameFinished(int32 index)
     246{
     247    if (!Window()->LockLooper())
     248        return;
     249   
     250    if (fRenameTabControl != NULL) {
     251        if (index >= 0) {
     252            BTab* tab = TabAt(index);
     253            if (tab != NULL)
     254                tab->SetLabel(fRenameTabControl->Text());
     255        }
     256        RemoveChild(fRenameTabControl);
     257        delete fRenameTabControl;
     258        fRenameTabControl = NULL;
     259    }
     260
     261    Window()->UnlockLooper();
     262}
     263
     264
    203265BRect
    204266SmartTabView::DrawTabs()
    205267{
  • src/apps/terminal/TermScrollView.h

     
    1414                                    BView* target,
    1515                                    uint32 resizingMode = B_FOLLOW_ALL);
    1616                                ~TermScrollView();
     17
     18        virtual void            MakeFocus(bool focused = true);
    1719};
    1820
    1921