From a73a27510a11e427a6997ff1fe88a1a66fa74690 Mon Sep 17 00:00:00 2001 From: Stefano Ceccherini Date: Thu, 11 May 2006 21:31:56 +0000 Subject: [PATCH] more cleanups. There is now a MenuSettings class which handles settings in a central way. More code simplification git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@17427 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/preferences/menu/AutoSettingsMenu.cpp | 7 +-- src/preferences/menu/FontMenu.cpp | 4 +- src/preferences/menu/FontMenu.h | 11 ---- src/preferences/menu/FontSizeMenu.cpp | 47 +++++++++------- src/preferences/menu/Jamfile | 3 +- src/preferences/menu/MenuBar.cpp | 10 ++-- src/preferences/menu/MenuBar.h | 4 +- src/preferences/menu/MenuSettings.cpp | 66 +++++++++++++++++++++++ src/preferences/menu/MenuSettings.h | 25 +++++++++ src/preferences/menu/MenuWindow.cpp | 66 +++++++---------------- src/preferences/menu/MenuWindow.h | 2 - 11 files changed, 151 insertions(+), 94 deletions(-) create mode 100644 src/preferences/menu/MenuSettings.cpp create mode 100644 src/preferences/menu/MenuSettings.h diff --git a/src/preferences/menu/AutoSettingsMenu.cpp b/src/preferences/menu/AutoSettingsMenu.cpp index c225856a78..dd45bed453 100644 --- a/src/preferences/menu/AutoSettingsMenu.cpp +++ b/src/preferences/menu/AutoSettingsMenu.cpp @@ -1,4 +1,5 @@ #include "AutoSettingsMenu.h" +#include "MenuSettings.h" AutoSettingsMenu::AutoSettingsMenu(const char *name, menu_layout layout) : @@ -11,9 +12,9 @@ void AutoSettingsMenu::AttachedToWindow() { menu_info info; - get_menu_info(&info); - BFont font; - + MenuSettings::GetInstance()->Get(info); + + BFont font; font.SetFamilyAndStyle(info.f_family, info.f_style); font.SetSize(info.font_size); SetFont(&font); diff --git a/src/preferences/menu/FontMenu.cpp b/src/preferences/menu/FontMenu.cpp index 0c1f415b4d..da8e426f1e 100644 --- a/src/preferences/menu/FontMenu.cpp +++ b/src/preferences/menu/FontMenu.cpp @@ -1,4 +1,5 @@ #include "FontMenu.h" +#include "MenuSettings.h" #include "msg.h" #include @@ -9,7 +10,6 @@ FontMenu::FontMenu() : AutoSettingsMenu("Font", B_ITEMS_IN_COLUMN) { - get_menu_info(&info); SetRadioMode(true); GetFonts(); } @@ -29,7 +29,7 @@ FontMenu::AttachedToWindow() ClearAllMarkedItems(); menu_info info; - get_menu_info(&info); + MenuSettings::GetInstance()->Get(info); PlaceCheckMarkOnFont(info.f_family, info.f_style); } diff --git a/src/preferences/menu/FontMenu.h b/src/preferences/menu/FontMenu.h index a569062ea5..24c673a71d 100644 --- a/src/preferences/menu/FontMenu.h +++ b/src/preferences/menu/FontMenu.h @@ -6,15 +6,6 @@ class FontSizeMenu : public AutoSettingsMenu { public: FontSizeMenu(); - -private: - menu_info info; - BMenuItem *fontSizeNine; - BMenuItem *fontSizeTen; - BMenuItem *fontSizeEleven; - BMenuItem *fontSizeTwelve; - BMenuItem *fontSizeFourteen; - BMenuItem *fontSizeEighteen; }; @@ -26,8 +17,6 @@ public: status_t PlaceCheckMarkOnFont(font_family family, font_style style); void ClearAllMarkedItems(); -private: - menu_info info; }; #endif diff --git a/src/preferences/menu/FontSizeMenu.cpp b/src/preferences/menu/FontSizeMenu.cpp index 9b35a6442e..7c4b472b8b 100644 --- a/src/preferences/menu/FontSizeMenu.cpp +++ b/src/preferences/menu/FontSizeMenu.cpp @@ -1,4 +1,5 @@ #include "FontMenu.h" +#include "MenuSettings.h" #include "msg.h" #include @@ -8,44 +9,50 @@ FontSizeMenu::FontSizeMenu() :AutoSettingsMenu("Font Size", B_ITEMS_IN_COLUMN) { - get_menu_info(&info); + menu_info info; + MenuSettings::GetInstance()->Get(info); BMessage *msg = new BMessage(MENU_FONT_SIZE); msg->AddFloat("size", 9); - fontSizeNine = new BMenuItem("9", msg, 0, 0); - AddItem(fontSizeNine); - if(info.font_size == 9){fontSizeNine->SetMarked(true);} + BMenuItem *item = new BMenuItem("9", msg, 0, 0); + AddItem(item); + if (info.font_size == 9) + item->SetMarked(true); msg = new BMessage(MENU_FONT_SIZE); msg->AddFloat("size", 10); - fontSizeTen = new BMenuItem("10", msg, 0, 0); - AddItem(fontSizeTen); - if(info.font_size == 10){fontSizeTen->SetMarked(true);} + item = new BMenuItem("10", msg, 0, 0); + AddItem(item); + if (info.font_size == 10) + item->SetMarked(true); msg = new BMessage(MENU_FONT_SIZE); msg->AddFloat("size", 11); - fontSizeEleven = new BMenuItem("11", msg, 0, 0); - AddItem(fontSizeEleven); - if(info.font_size == 11){fontSizeEleven->SetMarked(true);} + item = new BMenuItem("11", msg, 0, 0); + AddItem(item); + if (info.font_size == 11) + item->SetMarked(true); msg = new BMessage(MENU_FONT_SIZE); msg->AddFloat("size", 12); - fontSizeTwelve = new BMenuItem("12", msg, 0, 0); - AddItem(fontSizeTwelve); - if(info.font_size == 12){fontSizeTwelve->SetMarked(true);} + item = new BMenuItem("12", msg, 0, 0); + AddItem(item); + if (info.font_size == 12) + item->SetMarked(true); msg = new BMessage(MENU_FONT_SIZE); msg->AddFloat("size", 14); - fontSizeFourteen = new BMenuItem("14", msg, 0, 0); - AddItem(fontSizeFourteen); - if(info.font_size == 14){fontSizeFourteen->SetMarked(true);} + item = new BMenuItem("14", msg, 0, 0); + AddItem(item); + if (info.font_size == 14) + item->SetMarked(true); msg = new BMessage(MENU_FONT_SIZE); msg->AddFloat("size", 18); - fontSizeEighteen = new BMenuItem("18", msg, 0, 0); - AddItem(fontSizeEighteen); - if(info.font_size == 18){fontSizeEighteen->SetMarked(true);} + item = new BMenuItem("18", msg, 0, 0); + AddItem(item); + if (info.font_size == 18) + item->SetMarked(true); - SetTargetForItems(Window()); SetRadioMode(true); } diff --git a/src/preferences/menu/Jamfile b/src/preferences/menu/Jamfile index cbc8460c7b..388ec84d53 100644 --- a/src/preferences/menu/Jamfile +++ b/src/preferences/menu/Jamfile @@ -7,7 +7,8 @@ Preference Menu : FontMenu.cpp FontSizeMenu.cpp MenuApp.cpp - MenuBar.cpp + MenuBar.cpp + MenuSettings.cpp MenuWindow.cpp : libtranslation.so libbe.so : Menu.rdef diff --git a/src/preferences/menu/MenuBar.cpp b/src/preferences/menu/MenuBar.cpp index c09618ec6e..7d184b5f6f 100644 --- a/src/preferences/menu/MenuBar.cpp +++ b/src/preferences/menu/MenuBar.cpp @@ -1,6 +1,7 @@ #include "BitmapMenuItem.h" #include "FontMenu.h" #include "MenuBar.h" +#include "MenuSettings.h" #include "msg.h" #include @@ -14,7 +15,6 @@ MenuBar::MenuBar() :BMenuBar(BRect(40,10,10,10), "menu", B_FOLLOW_TOP|B_FRAME_EVENTS, B_ITEMS_IN_COLUMN, true) { - get_menu_info(&info); build_menu(); set_menu(); SetItemMargins(14.0, 2.0, 20.0, 0.0); @@ -88,8 +88,8 @@ MenuBar::set_menu() char *chars; bool altAsShortcut; - // get up-to-date menu info - get_menu_info(&info); + menu_info info; + MenuSettings::GetInstance()->Get(info); alwaysShowTriggersItem->SetMarked(info.triggers_always_shown); @@ -114,8 +114,8 @@ MenuBar::set_menu() void MenuBar::Update() { - // get up-to-date menu info - get_menu_info(&info); + menu_info info; + MenuSettings::GetInstance()->Get(info); // this needs to be updated in case the Defaults // were requested. if (info.separator == 0) diff --git a/src/preferences/menu/MenuBar.h b/src/preferences/menu/MenuBar.h index 7699538ad0..80bc6d710d 100644 --- a/src/preferences/menu/MenuBar.h +++ b/src/preferences/menu/MenuBar.h @@ -13,12 +13,10 @@ public: virtual void AttachedToWindow(); void set_menu(); void build_menu(); - virtual void Update(); + void Update(); virtual void FrameResized(float width, float height); private: - menu_info info; - //seperator submenu BMenu *separatorStyleMenu; BMenuItem *separatorStyleZero; diff --git a/src/preferences/menu/MenuSettings.cpp b/src/preferences/menu/MenuSettings.cpp new file mode 100644 index 0000000000..999e590ca6 --- /dev/null +++ b/src/preferences/menu/MenuSettings.cpp @@ -0,0 +1,66 @@ +#include "MenuSettings.h" + + +MenuSettings::MenuSettings() +{ + // the default settings. possibly a call to the app_server + // would provide and execute this information, as it does + // 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); + fDefaultSettings.separator = 0; + fDefaultSettings.click_to_open = true; + fDefaultSettings.triggers_always_shown = false; + + fPreviousSettings = fDefaultSettings; +} + + +MenuSettings::~MenuSettings() +{ +} + + +/* static */ +MenuSettings * +MenuSettings::GetInstance() +{ + static MenuSettings *settings = NULL; + if (settings == NULL) + settings = new MenuSettings(); + return settings; +} + + +void +MenuSettings::Get(menu_info &info) const +{ + get_menu_info(&info); +} + + +void +MenuSettings::Set(menu_info &info) +{ + get_menu_info(&fPreviousSettings); + set_menu_info(&info); +} + + +void +MenuSettings::Revert() +{ + set_menu_info(&fPreviousSettings); +} + + +void +MenuSettings::ResetToDefaults() +{ + get_menu_info(&fPreviousSettings); + set_menu_info(&fDefaultSettings); +} + diff --git a/src/preferences/menu/MenuSettings.h b/src/preferences/menu/MenuSettings.h new file mode 100644 index 0000000000..137c6cf2f1 --- /dev/null +++ b/src/preferences/menu/MenuSettings.h @@ -0,0 +1,25 @@ +#ifndef __MENUSETTINGS_H +#define __MENUSETTINGS_H + +#include + +class MenuSettings { +public: + static MenuSettings *GetInstance(); + + void Get(menu_info &info) const; + void Set(menu_info &info); + + void Revert(); + void ResetToDefaults(); + +private: + MenuSettings(); + ~MenuSettings(); + + menu_info fPreviousSettings; + menu_info fDefaultSettings; +}; + + +#endif diff --git a/src/preferences/menu/MenuWindow.cpp b/src/preferences/menu/MenuWindow.cpp index 46155c55e3..3fb4d89ff5 100644 --- a/src/preferences/menu/MenuWindow.cpp +++ b/src/preferences/menu/MenuWindow.cpp @@ -1,6 +1,7 @@ #include "ColorWindow.h" #include "MenuApp.h" #include "MenuBar.h" +#include "MenuSettings.h" #include "MenuWindow.h" #include "msg.h" @@ -17,9 +18,6 @@ MenuWindow::MenuWindow(BRect rect) : BWindow(rect, "Menu", B_TITLED_WINDOW, B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE) { - get_menu_info(&revert_info); - get_menu_info(&info); - revert = false; menuView = new BBox(Bounds(), "menuView", B_FOLLOW_ALL_SIDES, @@ -54,15 +52,19 @@ MenuWindow::MenuWindow(BRect rect) void MenuWindow::MessageReceived(BMessage *msg) { + MenuSettings *settings = MenuSettings::GetInstance(); + menu_info info; switch(msg->what) { case MENU_REVERT: - set_menu_info(&revert_info); revert = false; + settings->Revert(); Update(); break; case MENU_DEFAULT: - Defaults(); + revert = true; + settings->ResetToDefaults(); + Update(); break; case UPDATE_WINDOW: @@ -72,42 +74,40 @@ MenuWindow::MessageReceived(BMessage *msg) case MENU_FONT_FAMILY: case MENU_FONT_STYLE: { + revert = true; font_family *family; msg->FindPointer("family", (void**)&family); font_style *style; msg->FindPointer("style", (void**)&style); + settings->Get(info); memcpy(info.f_family, family, sizeof(info.f_family)); memcpy(info.f_style, style, sizeof(info.f_style)); - set_menu_info(&info); + settings->Set(info); Update(); break; } case MENU_FONT_SIZE: revert = true; - float fontSize; - msg->FindFloat("size", &fontSize); - info.font_size = fontSize; - set_menu_info(&info); + settings->Get(info); + msg->FindFloat("size", &info.font_size); + settings->Set(info); Update(); break; case MENU_SEP_TYPE: revert = true; - int32 i; - msg->FindInt32("sep", &i); - info.separator = i; - set_menu_info(&info); + settings->Get(info); + msg->FindInt32("sep", &info.separator); + settings->Set(info); Update(); break; case ALLWAYS_TRIGGERS_MSG: revert = true; - if (info.triggers_always_shown != true) - info.triggers_always_shown = true; - else - info.triggers_always_shown = false; - set_menu_info(&info); + settings->Get(info); + info.triggers_always_shown = !info.triggers_always_shown; + settings->Set(info); menuBar->set_menu(); Update(); break; @@ -141,7 +141,6 @@ MenuWindow::MessageReceived(BMessage *msg) break; case MENU_COLOR: - get_menu_info(&info); revert = true; Update(); break; @@ -161,30 +160,3 @@ MenuWindow::Update() // alert the rest of the application to update menuBar->Update(); } - - -void -MenuWindow::Defaults() -{ - // to set the default color. this should be changed - // to the system color for system wide compatability. - rgb_color color; - color.red = 219; - color.blue = 219; - color.green = 219; - color.alpha = 255; - - // the default settings. possibly a call to the app_server - // would provide and execute this information, as it does - // for get_menu_info and set_menu_info (or is this information - // coming from libbe.so? or else where?). - info.font_size = 12; - //info.f_family = "test"; - //info.f_style = "test"; - info.background_color = color; - info.separator = 0; - info.click_to_open = true; - info.triggers_always_shown = false; - set_menu_info(&info); - Update(); -} diff --git a/src/preferences/menu/MenuWindow.h b/src/preferences/menu/MenuWindow.h index aeaaea6e84..0433287e0d 100644 --- a/src/preferences/menu/MenuWindow.h +++ b/src/preferences/menu/MenuWindow.h @@ -20,8 +20,6 @@ private: bool revert; ColorWindow *colorWindow; BMenuItem *toggleItem; - menu_info info; - menu_info revert_info; BMenu *menu; MenuBar *menuBar; BBox *menuView;