Deskbar: Save String object instead of pointer

And add a SetLabel() that sets the truncated label when the label gets set.

This will be useful so we can compare the truncated label to the normal label
to tell if the label is truncated or not.
This commit is contained in:
John Scipione 2015-02-19 20:35:05 -05:00
parent e9982f6828
commit ae181f353b
2 changed files with 24 additions and 13 deletions

View File

@ -39,8 +39,6 @@ All rights reserved.
#include <stdlib.h> #include <stdlib.h>
#include <String.h>
#include "BarApp.h" #include "BarApp.h"
#include "BarView.h" #include "BarView.h"
#include "ExpandoMenuBar.h" #include "ExpandoMenuBar.h"
@ -56,31 +54,28 @@ const float kSwitchWidth = 12.0f;
TTruncatableMenuItem::TTruncatableMenuItem(const char* label, BMessage* message, TTruncatableMenuItem::TTruncatableMenuItem(const char* label, BMessage* message,
char shortcut, uint32 modifiers) char shortcut, uint32 modifiers)
: :
BMenuItem(label, message, shortcut, modifiers), BMenuItem(label, message, shortcut, modifiers)
fTruncatedString(new BString())
{ {
fTruncatedString = label;
} }
TTruncatableMenuItem::TTruncatableMenuItem(BMenu* menu, BMessage* message) TTruncatableMenuItem::TTruncatableMenuItem(BMenu* menu, BMessage* message)
: :
BMenuItem(menu, message), BMenuItem(menu, message)
fTruncatedString(new BString())
{ {
} }
TTruncatableMenuItem::TTruncatableMenuItem(BMessage* data) TTruncatableMenuItem::TTruncatableMenuItem(BMessage* data)
: :
BMenuItem(data), BMenuItem(data)
fTruncatedString(new BString())
{ {
} }
TTruncatableMenuItem::~TTruncatableMenuItem() TTruncatableMenuItem::~TTruncatableMenuItem()
{ {
delete fTruncatedString;
} }
@ -111,11 +106,22 @@ TTruncatableMenuItem::Label(float width)
float labelWidth = menu->StringWidth(label); float labelWidth = menu->StringWidth(label);
float offset = width - labelWidth; float offset = width - labelWidth;
TruncateLabel(maxWidth - offset, truncatedLabel); TruncateLabel(maxWidth - offset, truncatedLabel);
fTruncatedString->SetTo(truncatedLabel); fTruncatedString.SetTo(truncatedLabel);
free(truncatedLabel); free(truncatedLabel);
return fTruncatedString->String(); return fTruncatedString.String();
} }
} }
fTruncatedString.SetTo(label);
return label; return label;
} }
void
TTruncatableMenuItem::SetLabel(const char* label)
{
fTruncatedString.SetTo(label);
BMenuItem::SetLabel(label);
}

View File

@ -38,8 +38,8 @@ All rights reserved.
#include <MenuItem.h> #include <MenuItem.h>
#include <String.h>
class BString;
class TTruncatableMenuItem : public BMenuItem { class TTruncatableMenuItem : public BMenuItem {
public: public:
@ -52,8 +52,13 @@ public:
virtual const char* Label(); virtual const char* Label();
virtual const char* Label(float width); virtual const char* Label(float width);
virtual void SetLabel(const char* label);
const char* TruncatedLabel() const
{ return fTruncatedString.String(); };
private: private:
BString* fTruncatedString; BString fTruncatedString;
}; };