FileTabs.cpp

IƱaki Zabala, 06/27/2020 01:27 AM

Download (4.6 KB)

 
1
#include "TabBar.h"
2

    
3
namespace Upp {
4

    
5
String FileTabs::GetFileGroup(const String &file)
6
{
7
        return GetFileDirectory(file);
8
}
9

    
10
String FileTabs::GetStackId(const Tab &a)
11
{
12
        String s = a.value;
13
        return GetFileTitle(s);
14
}
15

    
16
hash_t FileTabs::GetStackSortOrder(const Tab &a)
17
{
18
        String s = a.value;
19
        return GetFileExt(s).GetHashValue();
20
}
21

    
22
void FileTabs::ComposeTab(Tab& tab, const Font &font, Color ink, int style)
23
{
24
        if(PaintIcons() && tab.HasIcon())
25
        {
26
                tab.AddImage(tab.img);
27
                tab.AddSpace(TB_SPACEICON);
28
        }
29

    
30
        WString txt = IsString(tab.value) ? tab.value : StdConvert().Format(tab.value);
31
        int extpos = txt.ReverseFind('.');
32
        tab.AddText(extpos >= 0 ? txt.Left(extpos) : txt, font, filecolor);
33

    
34
        if (extpos >= 0) {
35
                tab.AddText(txt.Right(txt.GetLength() - extpos), font, extcolor);
36
        }
37
}
38

    
39
void FileTabs::ComposeStackedTab(Tab& tab, const Tab& stacked_tab, const Font &font, Color ink, int style)
40
{
41
        tab.AddImage(TabBarImg::STSEP);
42

    
43
        if (stackedicons && tab.HasIcon()) {
44
                tab.AddImage(style == CTRL_HOT ? stacked_tab.img : (greyedicons ? DisabledImage(stacked_tab.img) : stacked_tab.img))
45
                        .Clickable();
46
        }
47
        else {
48
                WString txt = IsString(stacked_tab.value) ? stacked_tab.value : StdConvert().Format(stacked_tab.value);
49
                int extpos = txt.ReverseFind('.');
50
        
51
                Color c = (style == CTRL_HOT) ? extcolor : SColorDisabled();
52
                if (extpos >= 0) {
53
                        tab.AddText(
54
                                txt.Mid(extpos + 1),
55
                                font,
56
                                c
57
                        ).Clickable();
58
                }
59
                else {
60
                        tab.AddText("-", font, c).Clickable();
61
                }
62
        }
63
}
64

    
65
Size FileTabs::GetStackedSize(const Tab &t)
66
{
67
        if (stackedicons && t.HasIcon())
68
                return min(t.img.GetSize(), Size(TB_ICON, TB_ICON)) + Size(TB_SPACEICON, 0) + 5;
69

    
70
        WString txt = IsString(t.value) ? t.value : StdConvert().Format(t.value);
71
        int extpos = txt.ReverseFind('.');
72
        txt = extpos >= 0 ? txt.Mid(extpos + 1) : "-";
73
        return GetTextSize(txt, GetStyle().font) + Size(TabBarImg::STSEP().GetSize().cx, 0);
74
}
75

    
76
void FileTabs::Serialize(Stream& s)
77
{
78
        TabBar::Serialize(s);
79
        if(s.IsLoading() && icons)
80
        {
81
                for(int i = 0; i < tabs.GetCount(); i++)
82
                        tabs[i].img = NativePathIcon(String(tabs[i].value));
83
        }
84
}
85

    
86
void FileTabs::AddFile(const WString &file, bool make_active)
87
{
88
        AddFile(file, NativePathIcon(file.ToString()), make_active);
89
}
90

    
91
void FileTabs::AddFile(const WString &file, Image img, bool make_active)
92
{
93
        InsertFile(GetCount(), file, img, make_active);
94
}
95

    
96
void FileTabs::InsertFile(int ix, const WString &file, bool make_active)
97
{
98
        InsertFile(ix, file, NativePathIcon(file.ToString()), make_active);
99
}
100

    
101
void FileTabs::InsertFile(int ix, const WString &file, Image img, bool make_active)
102
{
103
        String s = file.ToString();
104
        TabBar::InsertKey(ix, file, GetFileName(s), img, GetFileGroup(s), make_active);
105
}
106

    
107
void FileTabs::AddFiles(const Vector<String> &files, bool make_active)
108
{
109
        AddFiles(files, Vector<Image>(), make_active);
110
}
111

    
112
void FileTabs::AddFiles(const Vector<String> &files, const Vector<Image> &img, bool make_active)
113
{
114
        InsertFiles(GetCount(), files, img, make_active);
115
}
116

    
117
void FileTabs::InsertFiles(int ix, const Vector<String> &files, bool make_active)
118
{
119
        InsertFiles(ix, files, Vector<Image>(), make_active);
120
}
121

    
122
void FileTabs::InsertFiles(int ix, const Vector<String> &files, const Vector<Image> &img, bool make_active)
123
{
124
        if (!files.GetCount()) return;
125
        bool useimg = img.GetCount() == files.GetCount();
126
        for (int i = files.GetCount() - 1; i > 0; i--) {
127
                TabBar::InsertKey0(ix, files[i].ToWString(), GetFileName(files[i]), 
128
                        useimg ? img[i] : NativePathIcon(files[i]), GetFileGroup(files[i]));        
129
        }
130
        InsertFile(ix, files[0].ToWString(), useimg ? img[0] : NativePathIcon(files[0]), make_active);
131
}
132

    
133
void FileTabs::RenameFile(const WString &from, const WString &to, Image icon)
134
{
135
        int n = FindKey(from);
136
        if (n >= 0)
137
                Set(n, to, GetFileName(to.ToString()), IsNull(icon) ? NativePathIcon(to.ToString()) : icon);
138
}
139

    
140
FileTabs& FileTabs::FileIcons(bool normal, bool stacked, bool stacked_greyedout)
141
{
142
        stackedicons = stacked;
143
        greyedicons = stacked_greyedout;
144
        TabBar::Icons(normal); 
145
        return *this;        
146
}
147

    
148
Vector<String> FileTabs::GetFiles() const
149
{
150
        Vector<String> files;
151
        files.SetCount(tabs.GetCount());
152
        for (int i = 0; i < tabs.GetCount(); i++)
153
                files[i] = tabs[i].key;
154
        return files;        
155
}
156

    
157
FileTabs& FileTabs::operator<<(const FileTabs &src)
158
{
159
        Clear();
160
        Stacking(false);
161
        SortGroups(false);
162
        Grouping(false);
163
        AddFiles(src.GetFiles(), src.GetIcons(), false);
164
        CopySettings(src);        
165
        stackedicons = src.stackedicons;
166
        greyedicons = src.greyedicons;
167
        
168
        if (src.HasCursor())
169
                SetData(~src);
170
        sc.SetPos(src.GetScrollPos());
171
        Refresh();
172
        return *this;
173
}
174

    
175
FileTabs::FileTabs() :
176
stackedicons(false), 
177
greyedicons(true),
178
filecolor(SColorLabel),
179
extcolor(IsDark(SColorFace()) ? Blend(White, LtBlue) : LtBlue)
180
{
181

    
182
}
183

    
184
}