Update People app to use layout API. Also do some style cleanup. Removed many message constants which weren't used. The TextControls no longer have Invocation/Modification messages, as these were being ignored anyway. Replaced some uses of c-style strings with BStrings.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37823 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Alex Wilson 2010-07-30 20:05:01 +00:00
parent 24ca4df9de
commit 7d90a09a79
6 changed files with 122 additions and 137 deletions

View File

@ -31,11 +31,7 @@ extern people_field gFields[];
enum messages{ enum messages{
M_NEW = 128, M_SAVE, M_SAVE_AS, M_REVERT, M_NEW = 128, M_SAVE, M_SAVE_AS, M_REVERT,
M_UNDO, M_SELECT, M_GROUP_MENU, M_DIRTY, M_UNDO, M_SELECT, M_GROUP_MENU, M_WINDOW_QUITS
M_NAME, M_NICKNAME, M_COMPANY, M_ADDRESS,
M_CITY, M_STATE, M_ZIP, M_COUNTRY, M_HPHONE,
M_WPHONE, M_FAX, M_EMAIL, M_URL, M_GROUP,
M_WINDOW_QUITS
}; };
enum fields { enum fields {
@ -50,14 +46,14 @@ class TPeopleWindow;
class TPeopleApp : public BApplication { class TPeopleApp : public BApplication {
public: public:
TPeopleApp(void); TPeopleApp();
virtual ~TPeopleApp(void); virtual ~TPeopleApp();
virtual void AboutRequested(void); virtual void AboutRequested();
virtual void ArgvReceived(int32, char**); virtual void ArgvReceived(int32, char**);
virtual void MessageReceived(BMessage*); virtual void MessageReceived(BMessage*);
virtual void RefsReceived(BMessage*); virtual void RefsReceived(BMessage*);
virtual void ReadyToRun(void); virtual void ReadyToRun();
TPeopleWindow* FindWindow(entry_ref); TPeopleWindow* FindWindow(entry_ref);
TPeopleWindow* NewWindow(entry_ref* = NULL); TPeopleWindow* NewWindow(entry_ref* = NULL);

View File

@ -10,26 +10,31 @@
This file may be used under the terms of the Be Sample Code License. This file may be used under the terms of the Be Sample Code License.
*/ */
#include "PeopleView.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fs_attr.h> #include <fs_attr.h>
#include <Window.h>
#include <Box.h> #include <Box.h>
#include <ControlLook.h>
#include <GridLayout.h>
#include <MenuField.h> #include <MenuField.h>
#include <MenuItem.h> #include <MenuItem.h>
#include <PopUpMenu.h> #include <PopUpMenu.h>
#include <Query.h> #include <Query.h>
#include <VolumeRoster.h> #include <VolumeRoster.h>
#include <Window.h>
#include "PeopleView.h"
#include "TTextControl.h" #include "TTextControl.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
TPeopleView::TPeopleView(const char* name, entry_ref *ref)
TPeopleView::TPeopleView(BRect rect, const char *title, entry_ref *ref) :
:BView(rect, title, B_FOLLOW_NONE, B_WILL_DRAW) BGridView()
{ {
SetName(name);
if (ref) if (ref)
fFile = new BFile(ref, O_RDWR); fFile = new BFile(ref, O_RDWR);
else else
@ -46,68 +51,60 @@ TPeopleView::~TPeopleView(void)
void void
TPeopleView::AttachedToWindow(void) TPeopleView::AttachedToWindow(void)
{ {
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); BGridLayout* layout = GridLayout();
BRect bounds = Bounds();
BFont font = *be_plain_font;
int32 offset = int32(font.StringWidth(gFields[F_HPHONE].name) + 10.5);
font_height fontHeight;
font.GetHeight(&fontHeight);
int32 textHeight = int32(fontHeight.ascent + fontHeight.descent
+ fontHeight.leading + 12.5);
BRect rect;
int32 row = 0; int32 row = 0;
for (int32 i = 0; gFields[i].attribute; i++, row++) { for (int32 i = 0; gFields[i].attribute; i++, row++) {
const char *name = gFields[i].name; const char *name = gFields[i].name;
rect.Set(NAME_H, NAME_V + row * textHeight,
bounds.right - NAME_H, NAME_V + (row + 1) * textHeight);
int32 labelOffset = offset;
if (i == F_NAME) if (i == F_NAME)
name = "Name"; name = "Name";
else if (i == F_GROUP) {
name = "";
rect.left += offset;
labelOffset = 0;
} else if (i == F_STATE) {
rect.right = rect.left + STATE_WIDTH;
row--;
} else if (i == F_ZIP) {
rect.left += STATE_WIDTH + 10;
labelOffset = int32(font.StringWidth(gFields[i].name) + 10.5);
}
char *text = NULL; char *text = NULL;
attr_info info; attr_info info;
if (fFile && fFile->GetAttrInfo(gFields[i].attribute, &info) == B_OK) { if (fFile && fFile->GetAttrInfo(gFields[i].attribute, &info) == B_OK) {
text = (char *)calloc(info.size, 1); text = (char *)calloc(info.size, 1);
fFile->ReadAttr(gFields[i].attribute, B_STRING_TYPE, 0, text, info.size); fFile->ReadAttr(gFields[i].attribute, B_STRING_TYPE,
0, text, info.size);
} }
fField[i] = new TTextControl(rect, name, labelOffset, text, M_DIRTY, M_NAME); fField[i] = new TTextControl(name, text);
fField[i]->SetTarget(this);
AddChild(fField[i]);
free(text); free(text);
int32 labelColumn = 0;
int32 textViewColumn = 1;
int32 textViewWidth = 3;
if (i == F_STATE)
textViewWidth = 1;
else if (i == F_ZIP) {
row--;
labelColumn = 2;
textViewColumn = 3;
textViewWidth = 1;
} }
rect.right = NAME_H + offset; if (i != F_GROUP) {
rect.left = rect.right - font.StringWidth(gFields[F_GROUP].name) - 32; layout->AddItem(fField[i]->CreateLabelLayoutItem(),
rect.top -= 1; labelColumn, row, 1, 1);
layout->AddItem(fField[i]->CreateTextViewLayoutItem(),
textViewColumn, row, textViewWidth, 1);
} else {
fField[i]->SetLabel("");
layout->AddView(fField[i], textViewColumn, row, textViewWidth, 1);
}
}
fGroups = new BPopUpMenu(gFields[F_GROUP].name); fGroups = new BPopUpMenu(gFields[F_GROUP].name);
fGroups->SetRadioMode(false); fGroups->SetRadioMode(false);
BMenuField *field = new BMenuField(rect, "", "", fGroups); BMenuField *field = new BMenuField("", "", fGroups);
field->SetDivider(0.0);
field->SetFont(&font);
field->SetEnabled(true); field->SetEnabled(true);
AddChild(field); layout->AddView(field, 0, --row);
float spacing = be_control_look->DefaultItemSpacing();
layout->SetSpacing(spacing, spacing);
// TODO: remove this after #5614 is fixed
layout->SetInsets(spacing, spacing, spacing, spacing);
fField[F_NAME]->MakeFocus(); fField[F_NAME]->MakeFocus();
ResizeTo(bounds.right, rect.bottom - 5 + NAME_V);
} }
@ -126,7 +123,7 @@ TPeopleView::MessageReceived(BMessage* msg)
case M_SELECT: case M_SELECT:
for (int32 loop = 0; loop < F_END; loop++) { for (int32 loop = 0; loop < F_END; loop++) {
BTextView* text = (BTextView *)fField[loop]->ChildAt(0); BTextView* text = fField[loop]->TextView();
if (text->IsFocus()) { if (text->IsFocus()) {
text->Select(0, text->TextLength()); text->Select(0, text->TextLength());
break; break;
@ -279,7 +276,7 @@ TPeopleView::SetField(int32 index, char *data, bool update)
fField[index]->SetText(data); fField[index]->SetText(data);
fField[index]->Update(); fField[index]->Update();
} else { } else {
BTextView* text = (BTextView *)fField[index]->ChildAt(0); BTextView* text = fField[index]->TextView();
int32 start, end; int32 start, end;
text->GetSelection(&start, &end); text->GetSelection(&start, &end);
@ -303,7 +300,7 @@ bool
TPeopleView::TextSelected(void) TPeopleView::TextSelected(void)
{ {
for (int32 loop = 0; loop < F_END; loop++) { for (int32 loop = 0; loop < F_END; loop++) {
BTextView* text = (BTextView*)fField[loop]->ChildAt(0); BTextView* text = fField[loop]->TextView();
int32 start, end; int32 start, end;
text->GetSelection(&start, &end); text->GetSelection(&start, &end);

View File

@ -15,17 +15,16 @@
#include "PeopleApp.h" #include "PeopleApp.h"
#define NAME_H 10 #include <GridView.h>
#define NAME_V 10
#define STATE_WIDTH 175
class BPopUpMenu; class BPopUpMenu;
class TTextControl; class TTextControl;
class TPeopleView : public BView { class TPeopleView : public BGridView {
public: public:
TPeopleView(BRect rect, const char* title, entry_ref* ref); TPeopleView(const char* name, entry_ref* ref);
~TPeopleView(void); ~TPeopleView(void);
virtual void AttachedToWindow(void); virtual void AttachedToWindow(void);

View File

@ -10,46 +10,53 @@
This file may be used under the terms of the Be Sample Code License. This file may be used under the terms of the Be Sample Code License.
*/ */
#include <MenuBar.h>
#include <MenuItem.h>
#include <FilePanel.h>
#include <NodeInfo.h>
#include <Alert.h>
#include <Path.h>
#include <FindDirectory.h>
#include <Font.h>
#include <Clipboard.h>
#include <TextView.h>
#include <NodeMonitor.h>
#include <String.h>
#include <Volume.h>
#include "PeopleApp.h"
#include "PeopleView.h"
#include "PeopleWindow.h" #include "PeopleWindow.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <Alert.h>
#include <Clipboard.h>
#include <ControlLook.h>
#include <FilePanel.h>
#include <FindDirectory.h>
#include <Font.h>
#include <LayoutBuilder.h>
#include <MenuBar.h>
#include <MenuItem.h>
#include <NodeInfo.h>
#include <NodeMonitor.h>
#include <Path.h>
#include <String.h>
#include <TextView.h>
#include <Volume.h>
#include "PeopleApp.h"
#include "PeopleView.h"
TPeopleWindow::TPeopleWindow(BRect frame, const char *title, entry_ref *ref) TPeopleWindow::TPeopleWindow(BRect frame, const char *title, entry_ref *ref)
: BWindow(frame, title, B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE), :
BWindow(frame, title, B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE
| B_AUTO_UPDATE_SIZE_LIMITS),
fPanel(NULL) fPanel(NULL)
{ {
BMenu* menu; BMenu* menu;
BMenuItem* item; BMenuItem* item;
BRect rect(0, 0, 32767, 15); BMenuBar* menuBar = new BMenuBar("");
BMenuBar* menuBar = new BMenuBar(rect, "");
menu = new BMenu("File"); menu = new BMenu("File");
menu->AddItem(item = new BMenuItem("New person" B_UTF8_ELLIPSIS, new BMessage(M_NEW), 'N')); menu->AddItem(item = new BMenuItem("New person" B_UTF8_ELLIPSIS,
new BMessage(M_NEW), 'N'));
item->SetTarget(NULL, be_app); item->SetTarget(NULL, be_app);
menu->AddItem(new BMenuItem("Close", new BMessage(B_QUIT_REQUESTED), 'W')); menu->AddItem(new BMenuItem("Close", new BMessage(B_QUIT_REQUESTED), 'W'));
menu->AddSeparatorItem(); menu->AddSeparatorItem();
menu->AddItem(fSave = new BMenuItem("Save", new BMessage(M_SAVE), 'S')); menu->AddItem(fSave = new BMenuItem("Save", new BMessage(M_SAVE), 'S'));
fSave->SetEnabled(FALSE); fSave->SetEnabled(FALSE);
menu->AddItem(new BMenuItem("Save as"B_UTF8_ELLIPSIS, new BMessage(M_SAVE_AS))); menu->AddItem(new BMenuItem("Save as"B_UTF8_ELLIPSIS,
menu->AddItem(fRevert = new BMenuItem("Revert", new BMessage(M_REVERT), 'R')); new BMessage(M_SAVE_AS)));
menu->AddItem(fRevert = new BMenuItem("Revert",
new BMessage(M_REVERT), 'R'));
fRevert->SetEnabled(FALSE); fRevert->SetEnabled(FALSE);
menu->AddSeparatorItem(); menu->AddSeparatorItem();
item = new BMenuItem("Quit", new BMessage(B_QUIT_REQUESTED), 'Q'); item = new BMenuItem("Quit", new BMessage(B_QUIT_REQUESTED), 'Q');
@ -68,10 +75,10 @@ TPeopleWindow::TPeopleWindow(BRect frame, const char *title, entry_ref *ref)
fCopy->SetTarget(NULL, this); fCopy->SetTarget(NULL, this);
menu->AddItem(fPaste = new BMenuItem("Paste", new BMessage(B_PASTE), 'V')); menu->AddItem(fPaste = new BMenuItem("Paste", new BMessage(B_PASTE), 'V'));
fPaste->SetTarget(NULL, this); fPaste->SetTarget(NULL, this);
menu->AddItem(item = new BMenuItem("Select all", new BMessage(M_SELECT), 'A')); menu->AddItem(item = new BMenuItem("Select all",
new BMessage(M_SELECT), 'A'));
item->SetTarget(NULL, this); item->SetTarget(NULL, this);
menuBar->AddItem(menu); menuBar->AddItem(menu);
AddChild(menuBar);
if (ref) { if (ref) {
fRef = new entry_ref(*ref); fRef = new entry_ref(*ref);
@ -80,12 +87,12 @@ TPeopleWindow::TPeopleWindow(BRect frame, const char *title, entry_ref *ref)
} else } else
fRef = NULL; fRef = NULL;
rect = Frame(); fView = new TPeopleView("PeopleView", fRef);
rect.OffsetTo(0, menuBar->Bounds().bottom + 1);
fView = new TPeopleView(rect, "PeopleView", fRef);
AddChild(fView); BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
ResizeTo(fView->Frame().right, fView->Frame().bottom); .SetInsets(0, 0, 0, 0)
.Add(menuBar)
.Add(fView);
} }

View File

@ -17,38 +17,25 @@
#include "TTextControl.h" #include "TTextControl.h"
TTextControl::TTextControl(BRect r, const char *label, int32 offset, TTextControl::TTextControl(const char *label, const char *text)
const char *text, int32 modificationMessage, int32 msg) :
: BTextControl(r, "", "", text, new BMessage(msg)) BTextControl(NULL, text, NULL)
{ {
SetModificationMessage(new BMessage(modificationMessage));
if (label[0]) { if (label && label[0])
char newLabel[B_FILE_NAME_LENGTH]; SetLabel(BString(label).Append(":"));
int32 length = strlen(label);
memcpy(newLabel, label, length);
newLabel[length] = ':';
newLabel[length + 1] = '\0';
SetLabel(newLabel);
}
SetDivider(offset);
SetAlignment(B_ALIGN_RIGHT, B_ALIGN_LEFT); SetAlignment(B_ALIGN_RIGHT, B_ALIGN_LEFT);
fOriginal = text;
if (text == NULL)
text = "";
fOriginal = strdup(text);
} }
TTextControl::~TTextControl(void) TTextControl::~TTextControl()
{ {
free(fOriginal);
} }
void void
TTextControl::AttachedToWindow(void) TTextControl::AttachedToWindow()
{ {
BTextControl::AttachedToWindow(); BTextControl::AttachedToWindow();
@ -58,9 +45,9 @@ TTextControl::AttachedToWindow(void)
bool bool
TTextControl::Changed(void) TTextControl::Changed()
{ {
return strcmp(fOriginal, Text()); return fOriginal != Text();
} }
@ -75,6 +62,5 @@ TTextControl::Revert(void)
void void
TTextControl::Update(void) TTextControl::Update(void)
{ {
fOriginal = (char *)realloc(fOriginal, strlen(Text()) + 1); fOriginal = Text();
strcpy(fOriginal, Text());
} }

View File

@ -1,6 +1,6 @@
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// //
// TextControl.h // TTextControl.h
// //
// Written by: Robert Polic // Written by: Robert Polic
// //
@ -13,22 +13,22 @@
#ifndef TEXTCONTROL_H #ifndef TEXTCONTROL_H
#define TEXTCONTROL_H #define TEXTCONTROL_H
#include <String.h>
#include <TextControl.h> #include <TextControl.h>
class TTextControl : public BTextControl { class TTextControl : public BTextControl {
public: public:
TTextControl(BRect rect, const char* label, int32 length, const char* text, TTextControl(const char* label, const char* text);
int32 modificationMessage, int32 invokationMessage); virtual ~TTextControl();
~TTextControl();
virtual void AttachedToWindow(void); virtual void AttachedToWindow();
bool Changed(void); bool Changed();
void Revert(void); void Revert();
void Update(void); void Update();
private: private:
char *fOriginal; BString fOriginal;
}; };
#endif /* TEXTCONTROL_H */ #endif /* TEXTCONTROL_H */