137ea7254SIngo Weinhold /*
27483c98dSIngo Weinhold * Copyright 2009-2012, Ingo Weinhold, ingo_weinhold@gmx.de.
337ea7254SIngo Weinhold * Distributed under the terms of the MIT License.
437ea7254SIngo Weinhold */
537ea7254SIngo Weinhold
69ea0d949SIngo Weinhold
737ea7254SIngo Weinhold #include "table/TableColumns.h"
837ea7254SIngo Weinhold
937ea7254SIngo Weinhold #include <stdio.h>
10*3aeed660SJérôme Duval #include <strings.h>
1137ea7254SIngo Weinhold
12d8d4b902SIngo Weinhold #include "util/TimeUtils.h"
13d8d4b902SIngo Weinhold
1437ea7254SIngo Weinhold
15f56fa21bSIngo Weinhold // #pragma mark - DelegateBasedTableColumn
1637ea7254SIngo Weinhold
1737ea7254SIngo Weinhold
DelegateBasedTableColumn(BColumn * columnDelegate,int32 modelIndex,float width,float minWidth,float maxWidth,alignment align)18f56fa21bSIngo Weinhold DelegateBasedTableColumn::DelegateBasedTableColumn(BColumn* columnDelegate,
1937ea7254SIngo Weinhold int32 modelIndex, float width, float minWidth, float maxWidth,
2037ea7254SIngo Weinhold alignment align)
2137ea7254SIngo Weinhold :
2237ea7254SIngo Weinhold TableColumn(modelIndex, width, minWidth, maxWidth, align),
2337ea7254SIngo Weinhold fColumnDelegate(columnDelegate)
2437ea7254SIngo Weinhold {
2537ea7254SIngo Weinhold }
2637ea7254SIngo Weinhold
2737ea7254SIngo Weinhold
~DelegateBasedTableColumn()28f56fa21bSIngo Weinhold DelegateBasedTableColumn::~DelegateBasedTableColumn()
2937ea7254SIngo Weinhold {
3037ea7254SIngo Weinhold }
3137ea7254SIngo Weinhold
3237ea7254SIngo Weinhold
3337ea7254SIngo Weinhold void
DrawTitle(BRect rect,BView * targetView)34f56fa21bSIngo Weinhold DelegateBasedTableColumn::DrawTitle(BRect rect, BView* targetView)
3537ea7254SIngo Weinhold {
3637ea7254SIngo Weinhold fColumnDelegate->DrawTitle(rect, targetView);
3737ea7254SIngo Weinhold }
3837ea7254SIngo Weinhold
3937ea7254SIngo Weinhold
4037ea7254SIngo Weinhold void
GetColumnName(BString * into) const41f56fa21bSIngo Weinhold DelegateBasedTableColumn::GetColumnName(BString* into) const
4237ea7254SIngo Weinhold {
4337ea7254SIngo Weinhold fColumnDelegate->GetColumnName(into);
4437ea7254SIngo Weinhold }
4537ea7254SIngo Weinhold
4637ea7254SIngo Weinhold
4737ea7254SIngo Weinhold void
DrawValue(const BVariant & value,BRect rect,BView * targetView)48f56fa21bSIngo Weinhold DelegateBasedTableColumn::DrawValue(const BVariant& value, BRect rect,
4937ea7254SIngo Weinhold BView* targetView)
5037ea7254SIngo Weinhold {
5137ea7254SIngo Weinhold fColumnDelegate->DrawField(PrepareField(value), rect, targetView);
5237ea7254SIngo Weinhold }
5337ea7254SIngo Weinhold
5437ea7254SIngo Weinhold
5537ea7254SIngo Weinhold float
GetPreferredWidth(const BVariant & value,BView * parent) const56f56fa21bSIngo Weinhold DelegateBasedTableColumn::GetPreferredWidth(const BVariant& value,
5737ea7254SIngo Weinhold BView* parent) const
5837ea7254SIngo Weinhold {
5937ea7254SIngo Weinhold return fColumnDelegate->GetPreferredWidth(PrepareField(value), parent);
6037ea7254SIngo Weinhold }
6137ea7254SIngo Weinhold
6237ea7254SIngo Weinhold
6337ea7254SIngo Weinhold // #pragma mark - StringTableColumn
6437ea7254SIngo Weinhold
6537ea7254SIngo Weinhold
StringTableColumn(int32 modelIndex,const char * title,float width,float minWidth,float maxWidth,uint32 truncate,alignment align)6637ea7254SIngo Weinhold StringTableColumn::StringTableColumn(int32 modelIndex, const char* title,
6737ea7254SIngo Weinhold float width, float minWidth, float maxWidth, uint32 truncate,
6837ea7254SIngo Weinhold alignment align)
6937ea7254SIngo Weinhold :
70f56fa21bSIngo Weinhold DelegateBasedTableColumn(&fColumn, modelIndex, width, minWidth, maxWidth,
7137ea7254SIngo Weinhold align),
7237ea7254SIngo Weinhold fColumn(title, width, minWidth, maxWidth, truncate, align),
7337ea7254SIngo Weinhold fField("")
7437ea7254SIngo Weinhold {
7537ea7254SIngo Weinhold }
7637ea7254SIngo Weinhold
7737ea7254SIngo Weinhold
~StringTableColumn()7837ea7254SIngo Weinhold StringTableColumn::~StringTableColumn()
7937ea7254SIngo Weinhold {
8037ea7254SIngo Weinhold }
8137ea7254SIngo Weinhold
8237ea7254SIngo Weinhold
8337ea7254SIngo Weinhold BField*
PrepareField(const BVariant & value) const84b7c5bd63SIngo Weinhold StringTableColumn::PrepareField(const BVariant& value) const
8537ea7254SIngo Weinhold {
8637ea7254SIngo Weinhold fField.SetString(value.ToString());
8737ea7254SIngo Weinhold fField.SetWidth(Width());
8837ea7254SIngo Weinhold return &fField;
8937ea7254SIngo Weinhold }
9037ea7254SIngo Weinhold
9137ea7254SIngo Weinhold
9237ea7254SIngo Weinhold int
CompareValues(const BVariant & a,const BVariant & b)93b7c5bd63SIngo Weinhold StringTableColumn::CompareValues(const BVariant& a, const BVariant& b)
9437ea7254SIngo Weinhold {
9537ea7254SIngo Weinhold return strcasecmp(a.ToString(), b.ToString());
9637ea7254SIngo Weinhold }
9737ea7254SIngo Weinhold
9837ea7254SIngo Weinhold
999ea0d949SIngo Weinhold // #pragma mark - BoolStringTableColumn
1009ea0d949SIngo Weinhold
1019ea0d949SIngo Weinhold
BoolStringTableColumn(int32 modelIndex,const char * title,float width,float minWidth,float maxWidth,const BString & trueString,const BString & falseString,uint32 truncate,alignment align)1029ea0d949SIngo Weinhold BoolStringTableColumn::BoolStringTableColumn(int32 modelIndex, const char* title,
1039ea0d949SIngo Weinhold float width, float minWidth, float maxWidth, const BString& trueString,
1049ea0d949SIngo Weinhold const BString& falseString, uint32 truncate, alignment align)
1059ea0d949SIngo Weinhold :
1069ea0d949SIngo Weinhold StringTableColumn(modelIndex, title, width, minWidth, maxWidth, truncate,
1079ea0d949SIngo Weinhold align),
1089ea0d949SIngo Weinhold fTrueString(trueString),
1099ea0d949SIngo Weinhold fFalseString(falseString)
1109ea0d949SIngo Weinhold {
1119ea0d949SIngo Weinhold }
1129ea0d949SIngo Weinhold
1139ea0d949SIngo Weinhold
1149ea0d949SIngo Weinhold BField*
PrepareField(const BVariant & value) const1159ea0d949SIngo Weinhold BoolStringTableColumn::PrepareField(const BVariant& value) const
1169ea0d949SIngo Weinhold {
1179ea0d949SIngo Weinhold return StringTableColumn::PrepareField(
1189ea0d949SIngo Weinhold BVariant(value.ToBool() ? fTrueString : fFalseString,
1199ea0d949SIngo Weinhold B_VARIANT_DONT_COPY_DATA));
1209ea0d949SIngo Weinhold }
1219ea0d949SIngo Weinhold
1229ea0d949SIngo Weinhold
1239ea0d949SIngo Weinhold int
CompareValues(const BVariant & a,const BVariant & b)1249ea0d949SIngo Weinhold BoolStringTableColumn::CompareValues(const BVariant& a, const BVariant& b)
1259ea0d949SIngo Weinhold {
1269ea0d949SIngo Weinhold bool aValue = a.ToBool();
1279ea0d949SIngo Weinhold return aValue == b.ToBool() ? 0 : (aValue ? 1 : -1);
1289ea0d949SIngo Weinhold }
1299ea0d949SIngo Weinhold
1309ea0d949SIngo Weinhold
13137ea7254SIngo Weinhold // #pragma mark - Int32TableColumn
13237ea7254SIngo Weinhold
13337ea7254SIngo Weinhold
Int32TableColumn(int32 modelIndex,const char * title,float width,float minWidth,float maxWidth,uint32 truncate,alignment align)13437ea7254SIngo Weinhold Int32TableColumn::Int32TableColumn(int32 modelIndex, const char* title,
13537ea7254SIngo Weinhold float width, float minWidth, float maxWidth, uint32 truncate,
13637ea7254SIngo Weinhold alignment align)
13737ea7254SIngo Weinhold :
13837ea7254SIngo Weinhold StringTableColumn(modelIndex, title, width, minWidth, maxWidth, truncate,
13937ea7254SIngo Weinhold align)
14037ea7254SIngo Weinhold {
14137ea7254SIngo Weinhold }
14237ea7254SIngo Weinhold
14337ea7254SIngo Weinhold
14437ea7254SIngo Weinhold BField*
PrepareField(const BVariant & value) const145b7c5bd63SIngo Weinhold Int32TableColumn::PrepareField(const BVariant& value) const
14637ea7254SIngo Weinhold {
14737ea7254SIngo Weinhold char buffer[16];
1487483c98dSIngo Weinhold snprintf(buffer, sizeof(buffer), "%" B_PRId32, value.ToInt32());
14937ea7254SIngo Weinhold return StringTableColumn::PrepareField(
150b7c5bd63SIngo Weinhold BVariant(buffer, B_VARIANT_DONT_COPY_DATA));
15137ea7254SIngo Weinhold }
15237ea7254SIngo Weinhold
15337ea7254SIngo Weinhold
15437ea7254SIngo Weinhold int
CompareValues(const BVariant & a,const BVariant & b)155b7c5bd63SIngo Weinhold Int32TableColumn::CompareValues(const BVariant& a, const BVariant& b)
15637ea7254SIngo Weinhold {
15737ea7254SIngo Weinhold return a.ToInt32() - b.ToInt32();
15837ea7254SIngo Weinhold }
15937ea7254SIngo Weinhold
16037ea7254SIngo Weinhold
16137ea7254SIngo Weinhold // #pragma mark - Int64TableColumn
16237ea7254SIngo Weinhold
16337ea7254SIngo Weinhold
Int64TableColumn(int32 modelIndex,const char * title,float width,float minWidth,float maxWidth,uint32 truncate,alignment align)16437ea7254SIngo Weinhold Int64TableColumn::Int64TableColumn(int32 modelIndex, const char* title,
16537ea7254SIngo Weinhold float width, float minWidth, float maxWidth, uint32 truncate,
16637ea7254SIngo Weinhold alignment align)
16737ea7254SIngo Weinhold :
16837ea7254SIngo Weinhold StringTableColumn(modelIndex, title, width, minWidth, maxWidth, truncate,
16937ea7254SIngo Weinhold align)
17037ea7254SIngo Weinhold {
17137ea7254SIngo Weinhold }
17237ea7254SIngo Weinhold
17337ea7254SIngo Weinhold
17437ea7254SIngo Weinhold BField*
PrepareField(const BVariant & value) const175b7c5bd63SIngo Weinhold Int64TableColumn::PrepareField(const BVariant& value) const
17637ea7254SIngo Weinhold {
17737ea7254SIngo Weinhold char buffer[32];
1787483c98dSIngo Weinhold snprintf(buffer, sizeof(buffer), "%" B_PRId64, value.ToInt64());
17937ea7254SIngo Weinhold return StringTableColumn::PrepareField(
180b7c5bd63SIngo Weinhold BVariant(buffer, B_VARIANT_DONT_COPY_DATA));
18137ea7254SIngo Weinhold }
18237ea7254SIngo Weinhold
18337ea7254SIngo Weinhold
18437ea7254SIngo Weinhold int
CompareValues(const BVariant & a,const BVariant & b)185b7c5bd63SIngo Weinhold Int64TableColumn::CompareValues(const BVariant& a, const BVariant& b)
18637ea7254SIngo Weinhold {
18737ea7254SIngo Weinhold int64 diff = a.ToInt64() - b.ToInt64();
18837ea7254SIngo Weinhold if (diff == 0)
18937ea7254SIngo Weinhold return 0;
19037ea7254SIngo Weinhold return diff < 0 ? -1 : 1;
19137ea7254SIngo Weinhold }
19237ea7254SIngo Weinhold
19337ea7254SIngo Weinhold
19437ea7254SIngo Weinhold // #pragma mark - BigtimeTableColumn
19537ea7254SIngo Weinhold
19637ea7254SIngo Weinhold
BigtimeTableColumn(int32 modelIndex,const char * title,float width,float minWidth,float maxWidth,bool invalidFirst,uint32 truncate,alignment align)19737ea7254SIngo Weinhold BigtimeTableColumn::BigtimeTableColumn(int32 modelIndex, const char* title,
19837ea7254SIngo Weinhold float width, float minWidth, float maxWidth, bool invalidFirst,
19937ea7254SIngo Weinhold uint32 truncate, alignment align)
20037ea7254SIngo Weinhold :
20137ea7254SIngo Weinhold StringTableColumn(modelIndex, title, width, minWidth, maxWidth, truncate,
20237ea7254SIngo Weinhold align),
20337ea7254SIngo Weinhold fInvalidFirst(invalidFirst)
20437ea7254SIngo Weinhold {
20537ea7254SIngo Weinhold }
20637ea7254SIngo Weinhold
20737ea7254SIngo Weinhold
20837ea7254SIngo Weinhold BField*
PrepareField(const BVariant & value) const209b7c5bd63SIngo Weinhold BigtimeTableColumn::PrepareField(const BVariant& value) const
21037ea7254SIngo Weinhold {
21137ea7254SIngo Weinhold bigtime_t time = value.ToInt64();
21237ea7254SIngo Weinhold if (time < 0) {
21337ea7254SIngo Weinhold return StringTableColumn::PrepareField(
214b7c5bd63SIngo Weinhold BVariant("-", B_VARIANT_DONT_COPY_DATA));
21537ea7254SIngo Weinhold }
21637ea7254SIngo Weinhold
21737ea7254SIngo Weinhold char buffer[64];
218d8d4b902SIngo Weinhold format_bigtime(time, buffer, sizeof(buffer));
21937ea7254SIngo Weinhold return StringTableColumn::PrepareField(
220b7c5bd63SIngo Weinhold BVariant(buffer, B_VARIANT_DONT_COPY_DATA));
22137ea7254SIngo Weinhold }
22237ea7254SIngo Weinhold
22337ea7254SIngo Weinhold
22437ea7254SIngo Weinhold int
CompareValues(const BVariant & _a,const BVariant & _b)225b7c5bd63SIngo Weinhold BigtimeTableColumn::CompareValues(const BVariant& _a, const BVariant& _b)
22637ea7254SIngo Weinhold {
22737ea7254SIngo Weinhold bigtime_t a = _a.ToInt64();
22837ea7254SIngo Weinhold bigtime_t b = _b.ToInt64();
22937ea7254SIngo Weinhold
23037ea7254SIngo Weinhold if (a == b)
23137ea7254SIngo Weinhold return 0;
23237ea7254SIngo Weinhold
23337ea7254SIngo Weinhold if (a < 0)
23437ea7254SIngo Weinhold return fInvalidFirst ? -1 : 1;
23537ea7254SIngo Weinhold if (b < 0)
23637ea7254SIngo Weinhold return fInvalidFirst ? 1 : -1;
23737ea7254SIngo Weinhold
23837ea7254SIngo Weinhold return a - b < 0 ? -1 : 1;
23937ea7254SIngo Weinhold }
240d8d4b902SIngo Weinhold
241d8d4b902SIngo Weinhold
242d8d4b902SIngo Weinhold // #pragma mark - NanotimeTableColumn
243d8d4b902SIngo Weinhold
244d8d4b902SIngo Weinhold
NanotimeTableColumn(int32 modelIndex,const char * title,float width,float minWidth,float maxWidth,bool invalidFirst,uint32 truncate,alignment align)245d8d4b902SIngo Weinhold NanotimeTableColumn::NanotimeTableColumn(int32 modelIndex, const char* title,
246d8d4b902SIngo Weinhold float width, float minWidth, float maxWidth, bool invalidFirst,
247d8d4b902SIngo Weinhold uint32 truncate, alignment align)
248d8d4b902SIngo Weinhold :
249d8d4b902SIngo Weinhold StringTableColumn(modelIndex, title, width, minWidth, maxWidth, truncate,
250d8d4b902SIngo Weinhold align),
251d8d4b902SIngo Weinhold fInvalidFirst(invalidFirst)
252d8d4b902SIngo Weinhold {
253d8d4b902SIngo Weinhold }
254d8d4b902SIngo Weinhold
255d8d4b902SIngo Weinhold
256d8d4b902SIngo Weinhold BField*
PrepareField(const BVariant & value) const257d8d4b902SIngo Weinhold NanotimeTableColumn::PrepareField(const BVariant& value) const
258d8d4b902SIngo Weinhold {
259d8d4b902SIngo Weinhold nanotime_t time = value.ToInt64();
260d8d4b902SIngo Weinhold if (time < 0) {
261d8d4b902SIngo Weinhold return StringTableColumn::PrepareField(
262d8d4b902SIngo Weinhold BVariant("-", B_VARIANT_DONT_COPY_DATA));
263d8d4b902SIngo Weinhold }
264d8d4b902SIngo Weinhold
265d8d4b902SIngo Weinhold char buffer[64];
266d8d4b902SIngo Weinhold format_nanotime(time, buffer, sizeof(buffer));
267d8d4b902SIngo Weinhold return StringTableColumn::PrepareField(
268d8d4b902SIngo Weinhold BVariant(buffer, B_VARIANT_DONT_COPY_DATA));
269d8d4b902SIngo Weinhold }
270d8d4b902SIngo Weinhold
271d8d4b902SIngo Weinhold
272d8d4b902SIngo Weinhold int
CompareValues(const BVariant & _a,const BVariant & _b)273d8d4b902SIngo Weinhold NanotimeTableColumn::CompareValues(const BVariant& _a, const BVariant& _b)
274d8d4b902SIngo Weinhold {
275d8d4b902SIngo Weinhold nanotime_t a = _a.ToInt64();
276d8d4b902SIngo Weinhold nanotime_t b = _b.ToInt64();
277d8d4b902SIngo Weinhold
278d8d4b902SIngo Weinhold if (a == b)
279d8d4b902SIngo Weinhold return 0;
280d8d4b902SIngo Weinhold
281d8d4b902SIngo Weinhold if (a < 0)
282d8d4b902SIngo Weinhold return fInvalidFirst ? -1 : 1;
283d8d4b902SIngo Weinhold if (b < 0)
284d8d4b902SIngo Weinhold return fInvalidFirst ? 1 : -1;
285d8d4b902SIngo Weinhold
286d8d4b902SIngo Weinhold return a - b < 0 ? -1 : 1;
287d8d4b902SIngo Weinhold }
288