applied patch by aldeck:

* the Revert button always returns to the settings which were active
  when the preflet was started
* the Defaults button always applies the default settings (the Revert
  button then still reverts to the original settings instead of those
  which were active when "Defaults" was pressed)
* the enabled state of the two buttons is correctly maintained
* code cleanup and refactoring

Thanks a lot, aldeck!


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23296 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus 2008-01-09 10:36:47 +00:00
parent afc104a083
commit 3dfc4833e3
8 changed files with 196 additions and 135 deletions

View File

@ -9,6 +9,7 @@
#include "ColorWindow.h"
#include "MenuSettings.h"
#include "msg.h"
#include <Application.h>
@ -22,9 +23,9 @@ ColorWindow::ColorWindow(BMessenger owner)
B_NOT_ZOOMABLE | B_NOT_RESIZABLE),
fOwner(owner)
{
// Set and collect the variables for revert
get_menu_info(&fInfo);
get_menu_info(&fRevertInfo);
fCurrentColor = MenuSettings::GetInstance()->BackgroundColor();
fPreviousColor = MenuSettings::GetInstance()->PreviousBackgroundColor();
fDefaultColor = MenuSettings::GetInstance()->DefaultBackgroundColor();
BView *topView = new BView(Bounds(), "topView", B_FOLLOW_ALL_SIDES, 0);
topView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
@ -32,7 +33,7 @@ ColorWindow::ColorWindow(BMessenger owner)
fColorControl = new BColorControl(BPoint(10, 10), B_CELLS_32x8,
9, "COLOR", new BMessage(MENU_COLOR), true);
fColorControl->SetValue(fInfo.background_color);
fColorControl->SetValue(fCurrentColor);
fColorControl->ResizeToPreferred();
topView->AddChild(fColorControl);
@ -44,7 +45,7 @@ ColorWindow::ColorWindow(BMessenger owner)
new BMessage(MENU_COLOR_DEFAULT), B_FOLLOW_LEFT | B_FOLLOW_TOP,
B_WILL_DRAW | B_NAVIGABLE);
fDefaultButton->ResizeToPreferred();
fDefaultButton->SetEnabled(false);
fDefaultButton->SetEnabled(fCurrentColor != fDefaultColor);
topView->AddChild(fDefaultButton);
rect.OffsetBy(fDefaultButton->Bounds().Width() + 10, 0);
@ -52,7 +53,7 @@ ColorWindow::ColorWindow(BMessenger owner)
new BMessage(MENU_REVERT), B_FOLLOW_LEFT | B_FOLLOW_TOP,
B_WILL_DRAW | B_NAVIGABLE);
fRevertButton->ResizeToPreferred();
fRevertButton->SetEnabled(false);
fRevertButton->SetEnabled(fCurrentColor != fPreviousColor);
topView->AddChild(fRevertButton);
ResizeTo(fColorControl->Bounds().Width() + 20, fRevertButton->Frame().bottom + 10);
@ -67,46 +68,42 @@ ColorWindow::Quit()
}
void
ColorWindow::_UpdateAndPost()
{
fDefaultButton->SetEnabled(fCurrentColor != fDefaultColor);
fRevertButton->SetEnabled(fCurrentColor != fPreviousColor);
fCurrentColor = fColorControl->ValueAsColor();
menu_info info;
get_menu_info(&info);
info.background_color = fCurrentColor;
set_menu_info(&info);
be_app->PostMessage(MENU_COLOR);
}
void
ColorWindow::MessageReceived(BMessage *msg)
{
switch (msg->what) {
case MENU_REVERT:
fColorControl->SetValue(fRevertInfo.background_color);
fInfo.background_color = fColorControl->ValueAsColor();
set_menu_info(&fInfo);
be_app->PostMessage(UPDATE_WINDOW);
fRevertButton->SetEnabled(false);
fColorControl->SetValue(fPreviousColor);
_UpdateAndPost();
break;
case MENU_COLOR_DEFAULT:
// change to system color for system wide
// compatability
rgb_color color;
color.red = 216;
color.blue = 216;
color.green = 216;
color.alpha = 255;
fColorControl->SetValue(color);
fDefaultButton->SetEnabled(false);
get_menu_info(&fInfo);
fInfo.background_color = fColorControl->ValueAsColor();
set_menu_info(&fInfo);
be_app->PostMessage(MENU_COLOR);
fColorControl->SetValue(fDefaultColor);
_UpdateAndPost();
break;
case MENU_COLOR:
get_menu_info(&fInfo);
fRevertInfo.background_color = fInfo.background_color;
fInfo.background_color = fColorControl->ValueAsColor();
set_menu_info(&fInfo);
be_app->PostMessage(MENU_COLOR);
fDefaultButton->SetEnabled(true);
fRevertButton->SetEnabled(true);
_UpdateAndPost();
break;
default:
be_app->PostMessage(UPDATE_WINDOW);
BWindow::MessageReceived(msg);
break;
}

View File

@ -28,11 +28,15 @@ class ColorWindow : public BWindow {
virtual void MessageReceived(BMessage* message);
private:
BColorControl* fColorControl;
void _UpdateAndPost();
private:
BColorControl* fColorControl;
BButton* fDefaultButton;
BButton* fRevertButton;
menu_info fRevertInfo;
menu_info fInfo;
rgb_color fCurrentColor;
rgb_color fPreviousColor;
rgb_color fDefaultColor;
BMessenger fOwner;
};

View File

@ -20,16 +20,12 @@
#include <TranslationUtils.h>
#include <Window.h>
#include <stdio.h>
#include <stdlib.h>
MenuBar::MenuBar()
: BMenuBar(BRect(40, 10, 10, 10), "menu", B_FOLLOW_TOP | B_FRAME_EVENTS,
B_ITEMS_IN_COLUMN, true)
{
_BuildMenu();
UpdateMenu();
SetItemMargins(14.0, 2.0, 20.0, 0.0);
}
@ -81,38 +77,19 @@ MenuBar::_BuildMenu()
void
MenuBar::UpdateMenu()
MenuBar::Update()
{
menu_info info;
MenuSettings::GetInstance()->Get(info);
fAlwaysShowTriggersItem->SetMarked(info.triggers_always_shown);
key_map *keys;
char* chars;
get_key_map(&keys, &chars);
bool altAsShortcut = (keys->left_command_key == 0x5d)
&& (keys->right_command_key == 0x5f);
free(chars);
free(keys);
bool altAsShortcut = MenuSettings::GetInstance()->AltAsShortcut();
fAltAsShortcutItem->SetMarked(altAsShortcut);
fControlAsShortcutItem->SetMarked(!altAsShortcut);
}
void
MenuBar::Update()
{
UpdateMenu();
BFont font;
if (LockLooper()) {
menu_info info;
MenuSettings::GetInstance()->Get(info);
font.SetFamilyAndStyle(info.f_family, info.f_style);
font.SetSize(info.font_size);
SetFont(&font);

View File

@ -19,7 +19,6 @@ class MenuBar : public BMenuBar {
virtual void AttachedToWindow();
void UpdateMenu();
void Update();
virtual void FrameResized(float width, float height);

View File

@ -9,6 +9,10 @@
#include "MenuSettings.h"
#include <Roster.h>
#include <Errors.h>
#include <stdio.h>
#include <stdlib.h>
MenuSettings::MenuSettings()
@ -18,14 +22,21 @@ MenuSettings::MenuSettings()
// for get_menu_info and set_menu_info (or is this information
// coming from libbe.so? or else where?).
fDefaultSettings.font_size = 12;
//info.f_family = "test";
//info.f_style = "test";
fDefaultSettings.background_color = ui_color(B_MENU_BACKGROUND_COLOR);
sprintf(fDefaultSettings.f_family,"%s","Bitstream Vera Sans");
sprintf(fDefaultSettings.f_style,"%s","Roman");
rgb_color color;
color.red = 216;
color.blue = 216;
color.green = 216;
color.alpha = 255;
fDefaultSettings.background_color = color;//ui_color(B_MENU_BACKGROUND_COLOR);
fDefaultSettings.separator = 0;
fDefaultSettings.click_to_open = true;
fDefaultSettings.triggers_always_shown = false;
fDefaultAltAsShortcut = true;
fPreviousSettings = fDefaultSettings;
get_menu_info(&fPreviousSettings);
fPreviousAltAsShortcut = AltAsShortcut();
}
@ -55,23 +66,83 @@ MenuSettings::Get(menu_info &info) const
void
MenuSettings::Set(menu_info &info)
{
get_menu_info(&fPreviousSettings);
set_menu_info(&info);
}
bool
MenuSettings::AltAsShortcut() const
{
key_map *keys;
char* chars;
get_key_map(&keys, &chars);
bool altAsShortcut = (keys->left_command_key == 0x5d)
&& (keys->right_command_key == 0x5f);
free(chars);
free(keys);
return altAsShortcut;
}
void
MenuSettings::SetAltAsShortcut(bool altAsShortcut)
{
if (altAsShortcut) {
// This might not be the same for all keyboards
set_modifier_key(B_LEFT_COMMAND_KEY, 0x5d);
set_modifier_key(B_RIGHT_COMMAND_KEY, 0x5f);
set_modifier_key(B_LEFT_CONTROL_KEY, 0x5c);
set_modifier_key(B_RIGHT_OPTION_KEY, 0x60);
} else {
// This might not be the same for all keyboards
set_modifier_key(B_LEFT_COMMAND_KEY, 0x5c);
set_modifier_key(B_RIGHT_COMMAND_KEY, 0x60);
set_modifier_key(B_LEFT_CONTROL_KEY, 0x5d);
set_modifier_key(B_RIGHT_OPTION_KEY, 0x5f);
}
be_roster->Broadcast(new BMessage(B_MODIFIERS_CHANGED));
}
void
MenuSettings::Revert()
{
set_menu_info(&fPreviousSettings);
SetAltAsShortcut(fPreviousAltAsShortcut);
}
void
MenuSettings::ResetToDefaults()
{
get_menu_info(&fPreviousSettings);
set_menu_info(&fDefaultSettings);
SetAltAsShortcut(fDefaultAltAsShortcut);
}
rgb_color
MenuSettings::BackgroundColor() const
{
menu_info info;
get_menu_info(&info);
return info.background_color;
}
rgb_color
MenuSettings::PreviousBackgroundColor() const
{
return fPreviousSettings.background_color;
}
rgb_color
MenuSettings::DefaultBackgroundColor() const
{
return fDefaultSettings.background_color;
}
@ -82,9 +153,29 @@ MenuSettings::IsDefaultable()
get_menu_info(&info);
return info.font_size != fDefaultSettings.font_size
|| strcmp(info.f_family, fDefaultSettings.f_family) != 0
|| strcmp(info.f_style, fDefaultSettings.f_style) != 0
|| info.background_color != fDefaultSettings.background_color
|| info.separator != fDefaultSettings.separator
|| info.click_to_open != fDefaultSettings.click_to_open
|| info.triggers_always_shown != fDefaultSettings.triggers_always_shown;
|| info.triggers_always_shown != fDefaultSettings.triggers_always_shown
|| AltAsShortcut() != fDefaultAltAsShortcut;
}
bool
MenuSettings::IsRevertable()
{
menu_info info;
get_menu_info(&info);
return info.font_size != fPreviousSettings.font_size
|| strcmp(info.f_family, fPreviousSettings.f_family) != 0
|| strcmp(info.f_style, fPreviousSettings.f_style) != 0
|| info.background_color != fPreviousSettings.background_color
|| info.separator != fPreviousSettings.separator
|| info.click_to_open != fPreviousSettings.click_to_open
|| info.triggers_always_shown != fPreviousSettings.triggers_always_shown
|| AltAsShortcut() != fPreviousAltAsShortcut;
}

View File

@ -19,9 +19,18 @@ class MenuSettings {
void Get(menu_info &info) const;
void Set(menu_info &info);
bool AltAsShortcut() const;
void SetAltAsShortcut(bool altAsShortcut);
rgb_color BackgroundColor() const;
rgb_color PreviousBackgroundColor() const;
rgb_color DefaultBackgroundColor() const;
void Revert();
void ResetToDefaults();
bool IsDefaultable();
bool IsRevertable();
private:
MenuSettings();
@ -29,6 +38,8 @@ class MenuSettings {
menu_info fPreviousSettings;
menu_info fDefaultSettings;
bool fPreviousAltAsShortcut;
bool fDefaultAltAsShortcut;
};
#endif // MENU_SETTINGS_H

View File

@ -20,7 +20,6 @@
#include <Box.h>
#include <Button.h>
#include <MenuItem.h>
#include <Roster.h>
#include <stdio.h>
#include <string.h>
@ -32,6 +31,8 @@ MenuWindow::MenuWindow(BRect rect)
{
fColorWindow = NULL;
fSettings = MenuSettings::GetInstance();
BView* topView = new BView(Bounds(), "menuView", B_FOLLOW_ALL_SIDES,
B_WILL_DRAW);
topView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
@ -60,34 +61,29 @@ MenuWindow::MenuWindow(BRect rect)
topView->AddChild(fRevertButton);
topView->MakeFocus();
fMenuBar->Update();
_UpdateAll();
}
void
MenuWindow::MessageReceived(BMessage *msg)
{
MenuSettings *settings = MenuSettings::GetInstance();
menu_info info;
switch (msg->what) {
case MENU_REVERT:
settings->Revert();
fRevertButton->SetEnabled(false);
fDefaultsButton->SetEnabled(settings->IsDefaultable());
fMenuBar->Update();
fSettings->Revert();
_UpdateAll();
break;
case MENU_DEFAULT:
settings->ResetToDefaults();
fDefaultsButton->SetEnabled(false);
fMenuBar->Update();
fSettings->ResetToDefaults();
_UpdateAll();
break;
case UPDATE_WINDOW:
fMenuBar->Update();
_UpdateAll();
break;
case MENU_FONT_FAMILY:
@ -98,64 +94,39 @@ MenuWindow::MessageReceived(BMessage *msg)
const font_style *style;
msg->FindString("style", (const char **)&style);
settings->Get(info);
fSettings->Get(info);
strlcpy(info.f_family, (const char *)family, B_FONT_FAMILY_LENGTH);
strlcpy(info.f_style, (const char *)style, B_FONT_STYLE_LENGTH);
settings->Set(info);
fSettings->Set(info);
fRevertButton->SetEnabled(true);
fDefaultsButton->SetEnabled(settings->IsDefaultable());
fMenuBar->Update();
_UpdateAll();
break;
}
case MENU_FONT_SIZE:
settings->Get(info);
fSettings->Get(info);
msg->FindFloat("size", &info.font_size);
settings->Set(info);
fSettings->Set(info);
fRevertButton->SetEnabled(true);
fDefaultsButton->SetEnabled(settings->IsDefaultable());
fMenuBar->Update();
_UpdateAll();
break;
case ALLWAYS_TRIGGERS_MSG:
settings->Get(info);
fSettings->Get(info);
info.triggers_always_shown = !info.triggers_always_shown;
settings->Set(info);
fSettings->Set(info);
fRevertButton->SetEnabled(true);
fDefaultsButton->SetEnabled(settings->IsDefaultable());
fMenuBar->UpdateMenu();
fMenuBar->Update();
_UpdateAll();
break;
case CTL_MARKED_MSG:
// This might not be the same for all keyboards
set_modifier_key(B_LEFT_COMMAND_KEY, 0x5c);
set_modifier_key(B_RIGHT_COMMAND_KEY, 0x60);
set_modifier_key(B_LEFT_CONTROL_KEY, 0x5d);
set_modifier_key(B_RIGHT_OPTION_KEY, 0x5f);
be_roster->Broadcast(new BMessage(B_MODIFIERS_CHANGED));
fRevertButton->SetEnabled(true);
fDefaultsButton->SetEnabled(settings->IsDefaultable());
fMenuBar->Update();
fSettings->SetAltAsShortcut(false);
_UpdateAll();
break;
case ALT_MARKED_MSG:
// This might not be the same for all keyboards
set_modifier_key(B_LEFT_COMMAND_KEY, 0x5d);
set_modifier_key(B_RIGHT_COMMAND_KEY, 0x5f);
set_modifier_key(B_LEFT_CONTROL_KEY, 0x5c);
set_modifier_key(B_RIGHT_OPTION_KEY, 0x60);
be_roster->Broadcast(new BMessage(B_MODIFIERS_CHANGED));
fRevertButton->SetEnabled(true);
fDefaultsButton->SetEnabled(settings->IsDefaultable());
fMenuBar->Update();
fSettings->SetAltAsShortcut(true);
_UpdateAll();
break;
case COLOR_SCHEME_OPEN_MSG:
@ -171,9 +142,7 @@ MenuWindow::MessageReceived(BMessage *msg)
break;
case MENU_COLOR:
fRevertButton->SetEnabled(true);
fDefaultsButton->SetEnabled(settings->IsDefaultable());
fMenuBar->Update();
_UpdateAll();
break;
default:
@ -183,6 +152,15 @@ MenuWindow::MessageReceived(BMessage *msg)
}
void
MenuWindow::_UpdateAll()
{
fRevertButton->SetEnabled(fSettings->IsRevertable());
fDefaultsButton->SetEnabled(fSettings->IsDefaultable());
fMenuBar->Update();
}
bool
MenuWindow::QuitRequested()
{

View File

@ -10,7 +10,7 @@
#ifndef MENU_WINDOW_H
#define MENU_WINDOW_H
#include "MenuSettings.h"
#include <Menu.h>
#include <Window.h>
@ -25,11 +25,15 @@ class MenuWindow : public BWindow {
virtual void MessageReceived(BMessage *message);
virtual bool QuitRequested();
private:
void _UpdateAll();
private:
ColorWindow* fColorWindow;
MenuBar* fMenuBar;
BButton* fDefaultsButton;
BButton* fRevertButton;
MenuSettings* fSettings;
};
#endif // MENU_WINDOW_H