* 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
@@ -18,9 +18,13 @@
#include <Button.h> #include <Button.h>
#include <CheckBox.h> #include <CheckBox.h>
#include <File.h> #include <File.h>
#include <GridView.h>
#include <GroupView.h>
#include <LayoutItem.h>
#include <Slider.h>
#include <SpaceLayoutItem.h>
#include <StringView.h> #include <StringView.h>
#include <String.h> #include <String.h>
#include <Slider.h>
#include <PopUpMenu.h> #include <PopUpMenu.h>
#include <MenuItem.h> #include <MenuItem.h>
#include <MenuField.h> #include <MenuField.h>
@@ -56,6 +60,15 @@
#include "AutoDeleter.h" #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 static void
SetupTextControl(BTextControl *control) SetupTextControl(BTextControl *control)
{ {
@@ -69,8 +82,8 @@ SetupTextControl(BTextControl *control)
bool bool
EthernetSettingsView::_PrepareRequest(struct ifreq& request, const char* name) EthernetSettingsView::_PrepareRequest(struct ifreq& request, const char* name)
{ {
//This function is used for talking direct to the stack. // This function is used for talking direct to the stack.
//It´s used by _ShowConfiguration. // It's used by _ShowConfiguration.
if (strlen(name) > IF_NAMESIZE) if (strlen(name) > IF_NAMESIZE)
return false; return false;
@@ -145,127 +158,103 @@ EthernetSettingsView::DetachedFromWindow()
} }
EthernetSettingsView::EthernetSettingsView(BRect frame) EthernetSettingsView::EthernetSettingsView()
: BView(frame, "EthernetSettingsView", B_FOLLOW_ALL, B_WILL_DRAW) : BView("EthernetSettingsView", 0, NULL)
, fInterfaces() , fInterfaces()
, fSettings() , fSettings()
, fCurrentSettings(NULL) , 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)); SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
fSocket = socket(AF_INET, SOCK_DGRAM, 0); fSocket = socket(AF_INET, SOCK_DGRAM, 0);
_GatherInterfaces(); _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++) { for (int32 i = 0; i < fInterfaces.CountItems(); i++) {
BString& name = *fInterfaces.ItemAt(i); BString& name = *fInterfaces.ItemAt(i);
BString label = name; BString label = name;
BMessage* info = new BMessage(kMsgInfo); BMessage* info = new BMessage(kMsgInfo);
info->AddString("interface", name.String()); info->AddString("interface", name.String());
BMenuItem* item = new BMenuItem(label.String(), info); BMenuItem* item = new BMenuItem(label.String(), info);
devmenu->AddItem(item); deviceMenu->AddItem(item);
} }
BPopUpMenu* modemenu = new BPopUpMenu("modes"); BPopUpMenu* modeMenu = new BPopUpMenu("modes");
modemenu->AddItem(new BMenuItem("Static", new BMessage(kMsgMode))); modeMenu->AddItem(new BMenuItem("Static", new BMessage(kMsgMode)));
modemenu->AddItem(new BMenuItem("DHCP", new BMessage(kMsgMode))); modeMenu->AddItem(new BMenuItem("DHCP", new BMessage(kMsgMode)));
//BMenuItem* offitem = new BMenuItem("Disconnected", NULL); //modeMenu->AddSeparatorItem();
//modemenu->AddItem(offitem); //BMenuItem* offItem = new BMenuItem("Disabled", NULL);
//modeMenu->AddItem(offItem);
fDeviceMenuField = new BMenuField(frame, "networkcards", "Adapter:", devmenu); fDeviceMenuField = new BMenuField("Adapter:", deviceMenu);
AddChild(fDeviceMenuField); layout->AddItem(fDeviceMenuField->CreateLabelLayoutItem(), 0, 0);
fDeviceMenuField->ResizeToPreferred(); layout->AddItem(fDeviceMenuField->CreateMenuBarLayoutItem(), 1, 0);
fTypeMenuField = new BMenuField(frame, "type", "Mode:", modemenu); fTypeMenuField = new BMenuField("Mode:", modeMenu);
fTypeMenuField->MoveTo(fDeviceMenuField->Frame().LeftBottom() + spacing); layout->AddItem(fTypeMenuField->CreateLabelLayoutItem(), 0, 1);
AddChild(fTypeMenuField); layout->AddItem(fTypeMenuField->CreateMenuBarLayoutItem(), 1, 1);
fTypeMenuField->ResizeToPreferred();
fIPTextControl = new BTextControl(frame, "ip", "IP Address:", "", NULL); fIPTextControl = new BTextControl("IP Address:", "", NULL);
fIPTextControl->MoveTo(fTypeMenuField->Frame().LeftBottom() + spacing);
fIPTextControl->ResizeToPreferred();
SetupTextControl(fIPTextControl); SetupTextControl(fIPTextControl);
AddChild(fIPTextControl);
fNetMaskTextControl = new BTextControl(frame, "mask", "Netmask:", "", NULL); BLayoutItem* layoutItem = fIPTextControl->CreateTextViewLayoutItem();
fNetMaskTextControl->MoveTo( layoutItem->SetExplicitMinSize(BSize(
fIPTextControl->Frame().LeftBottom() + spacing); 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); SetupTextControl(fNetMaskTextControl);
AddChild(fNetMaskTextControl); layout->AddItem(fNetMaskTextControl->CreateLabelLayoutItem(), 0, 3);
fNetMaskTextControl->ResizeToPreferred(); layout->AddItem(fNetMaskTextControl->CreateTextViewLayoutItem(), 1, 3);
fGatewayTextControl = new BTextControl(frame, "gateway", "Gateway:", "", fGatewayTextControl = new BTextControl("Gateway:", "", NULL);
NULL);
fGatewayTextControl->MoveTo(
fNetMaskTextControl->Frame().LeftBottom() + spacing);
SetupTextControl(fGatewayTextControl); SetupTextControl(fGatewayTextControl);
AddChild(fGatewayTextControl); layout->AddItem(fGatewayTextControl->CreateLabelLayoutItem(), 0, 4);
fGatewayTextControl->ResizeToPreferred(); layout->AddItem(fGatewayTextControl->CreateTextViewLayoutItem(), 1, 4);
fPrimaryDNSTextControl = new BTextControl(frame, "dns1", "DNS #1:", "", // TODO: Replace the DNS text controls by a BListView with add/remove
NULL); // functionality and so on...
fPrimaryDNSTextControl->MoveTo( fPrimaryDNSTextControl = new BTextControl("DNS #1:", "", NULL);
fGatewayTextControl->Frame().LeftBottom() + spacing);
SetupTextControl(fPrimaryDNSTextControl); SetupTextControl(fPrimaryDNSTextControl);
AddChild(fPrimaryDNSTextControl); layout->AddItem(fPrimaryDNSTextControl->CreateLabelLayoutItem(), 0, 5);
fPrimaryDNSTextControl->ResizeToPreferred(); layout->AddItem(fPrimaryDNSTextControl->CreateTextViewLayoutItem(), 1, 5);
fSecondaryDNSTextControl = new BTextControl(frame, "dns2", "DNS #2:", "", fSecondaryDNSTextControl = new BTextControl("DNS #2:", "", NULL);
NULL);
fSecondaryDNSTextControl->MoveTo(
fPrimaryDNSTextControl->Frame().LeftBottom() + spacing);
SetupTextControl(fSecondaryDNSTextControl); SetupTextControl(fSecondaryDNSTextControl);
AddChild(fSecondaryDNSTextControl); layout->AddItem(fSecondaryDNSTextControl->CreateLabelLayoutItem(), 0, 6);
fSecondaryDNSTextControl->ResizeToPreferred(); layout->AddItem(fSecondaryDNSTextControl->CreateTextViewLayoutItem(), 1, 6);
fRevertButton = new BButton(frame, "revert", "Revert", // button group (TODO: move to window, but take care of
new BMessage(kMsgRevert)); // enabling/disabling)
fRevertButton->ResizeToPreferred(); BGroupView* buttonGroup = new BGroupView(B_HORIZONTAL);
fRevertButton->MoveTo(
fSecondaryDNSTextControl->Frame().LeftBottom() + spacing); fRevertButton = new BButton("Revert", new BMessage(kMsgRevert));
fRevertButton->SetEnabled(false); fRevertButton->SetEnabled(false);
AddChild(fRevertButton); buttonGroup->GroupLayout()->AddView(fRevertButton);
fApplyButton = new BButton(frame, "apply", "Apply", new BMessage(kMsgApply)); buttonGroup->GroupLayout()->AddItem(BSpaceLayoutItem::CreateGlue());
fApplyButton->ResizeToPreferred();
fApplyButton->MoveTo(
fSecondaryDNSTextControl->Frame().RightBottom() + spacing
+ BPoint(-fApplyButton->Frame().Width(), 0));
AddChild(fApplyButton);
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 rootLayout->AddView(controlsGroup);
float maxLabelWidth rootLayout->AddView(buttonGroup);
= 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);
} }
EthernetSettingsView::~EthernetSettingsView() EthernetSettingsView::~EthernetSettingsView()
+46 -42
View File
@@ -22,52 +22,56 @@ class BPath;
class BTextControl; 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 { class EthernetSettingsView : public BView {
public: public:
EthernetSettingsView(BRect frame); EthernetSettingsView();
virtual ~EthernetSettingsView(); virtual ~EthernetSettingsView();
virtual void MessageReceived(BMessage* message); virtual void MessageReceived(BMessage* message);
virtual void AttachedToWindow(); virtual void AttachedToWindow();
virtual void DetachedFromWindow(); virtual void DetachedFromWindow();
void SaveProfile(BString profileName); void SaveProfile(BString profileName);
void LoadProfile(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; private:
int fSocket; void _GatherInterfaces();
void _GatherInterfaces(); bool _PrepareRequest(struct ifreq& request,
bool _PrepareRequest(struct ifreq& request, const char* name); const char* name);
void _ShowConfiguration(Settings* settings); void _ShowConfiguration(Settings* settings);
void _EnableTextControls(bool enable); void _EnableTextControls(bool enable);
void _SaveConfiguration(); void _SaveConfiguration();
void _SaveDNSConfiguration(); void _SaveDNSConfiguration();
void _SaveAdaptersConfiguration(); void _SaveAdaptersConfiguration();
void _ApplyControlsToConfiguration(); void _ApplyControlsToConfiguration();
status_t _GetPath(const char* name, BPath& path); 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 */ #endif /* ETHERNET_SETTINGS_VIEW_H */
+8 -11
View File
@@ -10,6 +10,7 @@
*/ */
#include <Application.h> #include <Application.h>
#include <GroupLayout.h>
#include "NetworkWindow.h" #include "NetworkWindow.h"
#include "EthernetSettingsView.h" #include "EthernetSettingsView.h"
@@ -17,13 +18,12 @@
NetworkWindow::NetworkWindow() NetworkWindow::NetworkWindow()
: BWindow(BRect(50, 50, 269, 302), "Network", B_TITLED_WINDOW, : 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)
{ {
SetLayout(new BGroupLayout(B_HORIZONTAL));
fEthView = new EthernetSettingsView(Bounds()); fEthView = new EthernetSettingsView();
ResizeTo(fEthView->Frame().Width(), fEthView->Frame().Height()); GetLayout()->AddView(fEthView);
AddChild(fEthView);
} }
@@ -31,11 +31,8 @@ void
NetworkWindow::MessageReceived(BMessage* message) NetworkWindow::MessageReceived(BMessage* message)
{ {
switch (message->what) { switch (message->what) {
case kMsgInfo: default:
fEthView->MessageReceived(message); BWindow::MessageReceived(message);
break;
default:
BWindow::MessageReceived(message);
} }
} }