* Use the new layout features to build the UI, prevents problems with

BMenuField being cut off. Simplifies the code a lot.
* Move message constants declaration into View, since they are private.
* Cleanup of EthernetSettingsView.h.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24854 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus 2008-04-07 10:45:50 +00:00
parent 391fdf1e99
commit 04ea9a475f
3 changed files with 136 additions and 146 deletions

View File

@ -18,9 +18,13 @@
#include <Button.h>
#include <CheckBox.h>
#include <File.h>
#include <GridView.h>
#include <GroupView.h>
#include <LayoutItem.h>
#include <Slider.h>
#include <SpaceLayoutItem.h>
#include <StringView.h>
#include <String.h>
#include <Slider.h>
#include <PopUpMenu.h>
#include <MenuItem.h>
#include <MenuField.h>
@ -56,6 +60,15 @@
#include "AutoDeleter.h"
static const uint32 kMsgApply = 'aply';
static const uint32 kMsgRevert = 'rvrt';
static const uint32 kMsgClose = 'clse';
static const uint32 kMsgField = 'fild';
static const uint32 kMsgInfo = 'info';
static const uint32 kMsgMode = 'mode';
static const uint32 kMsgChange = 'chng';
static void
SetupTextControl(BTextControl *control)
{
@ -69,8 +82,8 @@ SetupTextControl(BTextControl *control)
bool
EthernetSettingsView::_PrepareRequest(struct ifreq& request, const char* name)
{
//This function is used for talking direct to the stack.
//It´s used by _ShowConfiguration.
// This function is used for talking direct to the stack.
// It's used by _ShowConfiguration.
if (strlen(name) > IF_NAMESIZE)
return false;
@ -145,127 +158,103 @@ EthernetSettingsView::DetachedFromWindow()
}
EthernetSettingsView::EthernetSettingsView(BRect frame)
: BView(frame, "EthernetSettingsView", B_FOLLOW_ALL, B_WILL_DRAW)
EthernetSettingsView::EthernetSettingsView()
: BView("EthernetSettingsView", 0, NULL)
, fInterfaces()
, fSettings()
, fCurrentSettings(NULL)
{
float inset = ceilf(be_plain_font->Size() * 0.8);
frame.OffsetTo(inset, inset);
frame.right = StringWidth("IP Address XXX.XXX.XXX.XXX") + 50;
frame.bottom = frame.top + 15; // just a starting point
BPoint spacing(0, inset);
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
fSocket = socket(AF_INET, SOCK_DGRAM, 0);
_GatherInterfaces();
BPopUpMenu* devmenu = new BPopUpMenu("devices");
// build the GUI
BGroupLayout* rootLayout = new BGroupLayout(B_VERTICAL);
SetLayout(rootLayout);
BGridView* controlsGroup = new BGridView();
BGridLayout* layout = controlsGroup->GridLayout();
// insets
float inset = ceilf(be_plain_font->Size() * 0.7);
rootLayout->SetInsets(inset, inset, inset, inset);
rootLayout->SetSpacing(inset);
layout->SetSpacing(inset, inset);
BPopUpMenu* deviceMenu = new BPopUpMenu("devices");
for (int32 i = 0; i < fInterfaces.CountItems(); i++) {
BString& name = *fInterfaces.ItemAt(i);
BString label = name;
BMessage* info = new BMessage(kMsgInfo);
info->AddString("interface", name.String());
BMenuItem* item = new BMenuItem(label.String(), info);
devmenu->AddItem(item);
deviceMenu->AddItem(item);
}
BPopUpMenu* modemenu = new BPopUpMenu("modes");
modemenu->AddItem(new BMenuItem("Static", new BMessage(kMsgMode)));
modemenu->AddItem(new BMenuItem("DHCP", new BMessage(kMsgMode)));
//BMenuItem* offitem = new BMenuItem("Disconnected", NULL);
//modemenu->AddItem(offitem);
BPopUpMenu* modeMenu = new BPopUpMenu("modes");
modeMenu->AddItem(new BMenuItem("Static", new BMessage(kMsgMode)));
modeMenu->AddItem(new BMenuItem("DHCP", new BMessage(kMsgMode)));
//modeMenu->AddSeparatorItem();
//BMenuItem* offItem = new BMenuItem("Disabled", NULL);
//modeMenu->AddItem(offItem);
fDeviceMenuField = new BMenuField(frame, "networkcards", "Adapter:", devmenu);
AddChild(fDeviceMenuField);
fDeviceMenuField->ResizeToPreferred();
fDeviceMenuField = new BMenuField("Adapter:", deviceMenu);
layout->AddItem(fDeviceMenuField->CreateLabelLayoutItem(), 0, 0);
layout->AddItem(fDeviceMenuField->CreateMenuBarLayoutItem(), 1, 0);
fTypeMenuField = new BMenuField(frame, "type", "Mode:", modemenu);
fTypeMenuField->MoveTo(fDeviceMenuField->Frame().LeftBottom() + spacing);
AddChild(fTypeMenuField);
fTypeMenuField->ResizeToPreferred();
fTypeMenuField = new BMenuField("Mode:", modeMenu);
layout->AddItem(fTypeMenuField->CreateLabelLayoutItem(), 0, 1);
layout->AddItem(fTypeMenuField->CreateMenuBarLayoutItem(), 1, 1);
fIPTextControl = new BTextControl(frame, "ip", "IP Address:", "", NULL);
fIPTextControl->MoveTo(fTypeMenuField->Frame().LeftBottom() + spacing);
fIPTextControl->ResizeToPreferred();
fIPTextControl = new BTextControl("IP Address:", "", NULL);
SetupTextControl(fIPTextControl);
AddChild(fIPTextControl);
fNetMaskTextControl = new BTextControl(frame, "mask", "Netmask:", "", NULL);
fNetMaskTextControl->MoveTo(
fIPTextControl->Frame().LeftBottom() + spacing);
BLayoutItem* layoutItem = fIPTextControl->CreateTextViewLayoutItem();
layoutItem->SetExplicitMinSize(BSize(
fIPTextControl->StringWidth("XXX.XXX.XXX.XXX") + inset,
B_SIZE_UNSET));
layout->AddItem(fIPTextControl->CreateLabelLayoutItem(), 0, 2);
layout->AddItem(layoutItem, 1, 2);
fNetMaskTextControl = new BTextControl("Netmask:", "", NULL);
SetupTextControl(fNetMaskTextControl);
AddChild(fNetMaskTextControl);
fNetMaskTextControl->ResizeToPreferred();
layout->AddItem(fNetMaskTextControl->CreateLabelLayoutItem(), 0, 3);
layout->AddItem(fNetMaskTextControl->CreateTextViewLayoutItem(), 1, 3);
fGatewayTextControl = new BTextControl(frame, "gateway", "Gateway:", "",
NULL);
fGatewayTextControl->MoveTo(
fNetMaskTextControl->Frame().LeftBottom() + spacing);
fGatewayTextControl = new BTextControl("Gateway:", "", NULL);
SetupTextControl(fGatewayTextControl);
AddChild(fGatewayTextControl);
fGatewayTextControl->ResizeToPreferred();
layout->AddItem(fGatewayTextControl->CreateLabelLayoutItem(), 0, 4);
layout->AddItem(fGatewayTextControl->CreateTextViewLayoutItem(), 1, 4);
fPrimaryDNSTextControl = new BTextControl(frame, "dns1", "DNS #1:", "",
NULL);
fPrimaryDNSTextControl->MoveTo(
fGatewayTextControl->Frame().LeftBottom() + spacing);
// TODO: Replace the DNS text controls by a BListView with add/remove
// functionality and so on...
fPrimaryDNSTextControl = new BTextControl("DNS #1:", "", NULL);
SetupTextControl(fPrimaryDNSTextControl);
AddChild(fPrimaryDNSTextControl);
fPrimaryDNSTextControl->ResizeToPreferred();
layout->AddItem(fPrimaryDNSTextControl->CreateLabelLayoutItem(), 0, 5);
layout->AddItem(fPrimaryDNSTextControl->CreateTextViewLayoutItem(), 1, 5);
fSecondaryDNSTextControl = new BTextControl(frame, "dns2", "DNS #2:", "",
NULL);
fSecondaryDNSTextControl->MoveTo(
fPrimaryDNSTextControl->Frame().LeftBottom() + spacing);
fSecondaryDNSTextControl = new BTextControl("DNS #2:", "", NULL);
SetupTextControl(fSecondaryDNSTextControl);
AddChild(fSecondaryDNSTextControl);
fSecondaryDNSTextControl->ResizeToPreferred();
layout->AddItem(fSecondaryDNSTextControl->CreateLabelLayoutItem(), 0, 6);
layout->AddItem(fSecondaryDNSTextControl->CreateTextViewLayoutItem(), 1, 6);
fRevertButton = new BButton(frame, "revert", "Revert",
new BMessage(kMsgRevert));
fRevertButton->ResizeToPreferred();
fRevertButton->MoveTo(
fSecondaryDNSTextControl->Frame().LeftBottom() + spacing);
// button group (TODO: move to window, but take care of
// enabling/disabling)
BGroupView* buttonGroup = new BGroupView(B_HORIZONTAL);
fRevertButton = new BButton("Revert", new BMessage(kMsgRevert));
fRevertButton->SetEnabled(false);
AddChild(fRevertButton);
buttonGroup->GroupLayout()->AddView(fRevertButton);
fApplyButton = new BButton(frame, "apply", "Apply", new BMessage(kMsgApply));
fApplyButton->ResizeToPreferred();
fApplyButton->MoveTo(
fSecondaryDNSTextControl->Frame().RightBottom() + spacing
+ BPoint(-fApplyButton->Frame().Width(), 0));
AddChild(fApplyButton);
buttonGroup->GroupLayout()->AddItem(BSpaceLayoutItem::CreateGlue());
ResizeTo(frame.Width() + 2 * inset, fApplyButton->Frame().bottom + inset);
fApplyButton = new BButton("Apply", new BMessage(kMsgApply));
buttonGroup->GroupLayout()->AddView(fApplyButton);
// take care of label alignment
float maxLabelWidth
= fDeviceMenuField->StringWidth(fDeviceMenuField->Label());
maxLabelWidth = max_c(maxLabelWidth,
fTypeMenuField->StringWidth(fTypeMenuField->Label()));
maxLabelWidth = max_c(maxLabelWidth,
fIPTextControl->StringWidth(fIPTextControl->Label()));
maxLabelWidth = max_c(maxLabelWidth,
fNetMaskTextControl->StringWidth(fNetMaskTextControl->Label()));
maxLabelWidth = max_c(maxLabelWidth,
fGatewayTextControl->StringWidth(fGatewayTextControl->Label()));
maxLabelWidth = max_c(maxLabelWidth,
fPrimaryDNSTextControl->StringWidth(fPrimaryDNSTextControl->Label()));
maxLabelWidth = max_c(maxLabelWidth,
fSecondaryDNSTextControl->StringWidth(
fSecondaryDNSTextControl->Label()));
fDeviceMenuField->SetDivider(maxLabelWidth + 8);
fTypeMenuField->SetDivider(maxLabelWidth + 8);
fIPTextControl->SetDivider(maxLabelWidth + 8);
fNetMaskTextControl->SetDivider(maxLabelWidth + 8);
fGatewayTextControl->SetDivider(maxLabelWidth + 8);
fPrimaryDNSTextControl->SetDivider(maxLabelWidth + 8);
fSecondaryDNSTextControl->SetDivider(maxLabelWidth + 8);
rootLayout->AddView(controlsGroup);
rootLayout->AddView(buttonGroup);
}
EthernetSettingsView::~EthernetSettingsView()

View File

@ -22,18 +22,9 @@ class BPath;
class BTextControl;
static const uint32 kMsgApply = 'aply';
static const uint32 kMsgRevert = 'rvrt';
static const uint32 kMsgClose = 'clse';
static const uint32 kMsgField = 'fild';
static const uint32 kMsgInfo = 'info';
static const uint32 kMsgMode = 'mode';
static const uint32 kMsgChange = 'chng';
class EthernetSettingsView : public BView {
public:
EthernetSettingsView(BRect frame);
public:
EthernetSettingsView();
virtual ~EthernetSettingsView();
virtual void MessageReceived(BMessage* message);
@ -42,25 +33,11 @@ class EthernetSettingsView : public BView {
void SaveProfile(BString profileName);
void LoadProfile(BString profileName);
private:
BButton* fApplyButton;
BButton* fRevertButton;
BMenuField* fDeviceMenuField;
BMenuField* fTypeMenuField;
BTextControl* fIPTextControl;
BTextControl* fNetMaskTextControl;
BTextControl* fGatewayTextControl;
BTextControl* fPrimaryDNSTextControl;
BTextControl* fSecondaryDNSTextControl;
BObjectList<BString> fInterfaces;
BObjectList<Settings> fSettings;
Settings* fCurrentSettings;
int32 fStatus;
int fSocket;
private:
void _GatherInterfaces();
bool _PrepareRequest(struct ifreq& request, const char* name);
bool _PrepareRequest(struct ifreq& request,
const char* name);
void _ShowConfiguration(Settings* settings);
void _EnableTextControls(bool enable);
void _SaveConfiguration();
@ -68,6 +45,33 @@ class EthernetSettingsView : public BView {
void _SaveAdaptersConfiguration();
void _ApplyControlsToConfiguration();
status_t _GetPath(const char* name, BPath& path);
private:
BButton* fApplyButton;
BButton* fRevertButton;
// TODO: buttons should be moved to window instead
BMenuField* fDeviceMenuField;
BMenuField* fTypeMenuField;
BTextControl* fIPTextControl;
BTextControl* fNetMaskTextControl;
BTextControl* fGatewayTextControl;
BTextControl* fPrimaryDNSTextControl;
BTextControl* fSecondaryDNSTextControl;
// TODO: DNS settings do not belong here, do they?
BObjectList<BString> fInterfaces;
// TODO: the view should not know about the interfaces,
// it should only display the given interface, move
// one level up.
BObjectList<Settings> fSettings;
// TODO: the view should not know about a list
// of settings, instead it should be configured
// to a specific setting from the code one level up
Settings* fCurrentSettings;
int32 fStatus;
int fSocket;
};
#endif /* ETHERNET_SETTINGS_VIEW_H */

View File

@ -10,6 +10,7 @@
*/
#include <Application.h>
#include <GroupLayout.h>
#include "NetworkWindow.h"
#include "EthernetSettingsView.h"
@ -17,13 +18,12 @@
NetworkWindow::NetworkWindow()
: BWindow(BRect(50, 50, 269, 302), "Network", B_TITLED_WINDOW,
B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE)
B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE
| B_AUTO_UPDATE_SIZE_LIMITS)
{
fEthView = new EthernetSettingsView(Bounds());
ResizeTo(fEthView->Frame().Width(), fEthView->Frame().Height());
AddChild(fEthView);
SetLayout(new BGroupLayout(B_HORIZONTAL));
fEthView = new EthernetSettingsView();
GetLayout()->AddView(fEthView);
}
@ -31,9 +31,6 @@ void
NetworkWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
case kMsgInfo:
fEthView->MessageReceived(message);
break;
default:
BWindow::MessageReceived(message);
}