Remove dependence on color constants in ServerReadOnlyMemory.

This fixes a maintainance problem where you have to update this otherwise
unrelated file to keep it in sync whenever you add a color constant.

I've added a B_COLOR_WHICH_COUNT constant to the color_which enum which should
be updated to point to the newest color constants as new ones are added. I
reworked ServerReadOnlyMemory to use this constant instead of using to the
current largest color constant directly. If you use B_COLOR_WHICH_COUNT to
refer to a color in your code expect to get unpredictable and nonsensical
results. Most likely you'll get an undefined result which will return black
but don't depend on it.

The net effect of this is that ServerReadOnlyMemory doesn't need to be updated
anymore when new color constants are introduced but will continue to produce
correct results.

Eliminate kNumColors constant, replace it with B_COLOR_WHICH_COUNT
This commit is contained in:
John Scipione
2013-04-05 22:46:22 -04:00
parent 9f24981a56
commit 3302521556
4 changed files with 24 additions and 22 deletions
+8 -2
View File
@@ -331,9 +331,15 @@ enum color_which {
B_KEYBOARD_NAVIGATION_COLOR = B_NAVIGATION_BASE_COLOR, B_KEYBOARD_NAVIGATION_COLOR = B_NAVIGATION_BASE_COLOR,
B_MENU_SELECTION_BACKGROUND_COLOR = B_MENU_SELECTED_BACKGROUND_COLOR, B_MENU_SELECTION_BACKGROUND_COLOR = B_MENU_SELECTED_BACKGROUND_COLOR,
// These are deprecated -- do not use in new code. See BScreen for // Update this constant to be the largest color constant excluding
// the replacement for B_DESKTOP_COLOR. // B_SUCCESS_COLOR and B_FAILURE_COLOR.
// If you add a constant with index greater than 100 you'll have to add
// to the second operand below and also update ServerReadOnlyMemory.h
B_COLOR_WHICH_COUNT = B_SCROLL_BAR_THUMB_COLOR + 3,
// The following constants are deprecated, do not use in new code.
B_DESKTOP_COLOR = 5 B_DESKTOP_COLOR = 5
// see BScreen class for B_DESKTOP_COLOR replacement
}; };
+6 -11
View File
@@ -13,23 +13,18 @@
#include <InterfaceDefs.h> #include <InterfaceDefs.h>
static const int32 kNumColors = 35;
struct server_read_only_memory { struct server_read_only_memory {
rgb_color colors[kNumColors]; rgb_color colors[B_COLOR_WHICH_COUNT];
}; };
// NOTE: these functions must be kept in sync with InterfaceDefs.h color_which!
static inline int32 static inline int32
color_which_to_index(color_which which) color_which_to_index(color_which which)
{ {
// NOTE: this must be kept in sync with InterfaceDefs.h color_which! if (which <= B_COLOR_WHICH_COUNT - 3)
if (which <= B_SCROLL_BAR_THUMB_COLOR)
return which - 1; return which - 1;
if (which >= B_SUCCESS_COLOR && which <= B_FAILURE_COLOR) if (which >= B_SUCCESS_COLOR && which <= B_FAILURE_COLOR)
return which - B_SUCCESS_COLOR + B_SCROLL_BAR_THUMB_COLOR; return which - B_SUCCESS_COLOR + B_COLOR_WHICH_COUNT - 3;
return -1; return -1;
} }
@@ -38,12 +33,12 @@ color_which_to_index(color_which which)
static inline color_which static inline color_which
index_to_color_which(int32 index) index_to_color_which(int32 index)
{ {
if (index >= 0 && index < kNumColors) { if (index >= 0 && index < B_COLOR_WHICH_COUNT) {
if ((color_which)index < B_SCROLL_BAR_THUMB_COLOR) if ((color_which)index < B_COLOR_WHICH_COUNT - 3)
return (color_which)(index + 1); return (color_which)(index + 1);
else { else {
return (color_which)(index + B_SUCCESS_COLOR return (color_which)(index + B_SUCCESS_COLOR
- B_SCROLL_BAR_THUMB_COLOR); - B_COLOR_WHICH_COUNT - 3);
} }
} }
+3 -3
View File
@@ -69,7 +69,7 @@ menu_info *_menu_info_ptr_;
extern "C" const char B_NOTIFICATION_SENDER[] = "be:sender"; extern "C" const char B_NOTIFICATION_SENDER[] = "be:sender";
static const rgb_color _kDefaultColors[kNumColors] = { static const rgb_color _kDefaultColors[B_COLOR_WHICH_COUNT] = {
{216, 216, 216, 255}, // B_PANEL_BACKGROUND_COLOR {216, 216, 216, 255}, // B_PANEL_BACKGROUND_COLOR
{216, 216, 216, 255}, // B_MENU_BACKGROUND_COLOR {216, 216, 216, 255}, // B_MENU_BACKGROUND_COLOR
{255, 203, 0, 255}, // B_WINDOW_TAB_COLOR {255, 203, 0, 255}, // B_WINDOW_TAB_COLOR
@@ -1071,7 +1071,7 @@ rgb_color
ui_color(color_which which) ui_color(color_which which)
{ {
int32 index = color_which_to_index(which); int32 index = color_which_to_index(which);
if (index < 0 || index >= kNumColors) { if (index < 0 || index >= B_COLOR_WHICH_COUNT) {
fprintf(stderr, "ui_color(): unknown color_which %d\n", which); fprintf(stderr, "ui_color(): unknown color_which %d\n", which);
return make_color(0, 0, 0); return make_color(0, 0, 0);
} }
@@ -1090,7 +1090,7 @@ void
set_ui_color(const color_which &which, const rgb_color &color) set_ui_color(const color_which &which, const rgb_color &color)
{ {
int32 index = color_which_to_index(which); int32 index = color_which_to_index(which);
if (index < 0 || index >= kNumColors) { if (index < 0 || index >= B_COLOR_WHICH_COUNT) {
fprintf(stderr, "set_ui_color(): unknown color_which %d\n", which); fprintf(stderr, "set_ui_color(): unknown color_which %d\n", which);
return; return;
} }
+7 -6
View File
@@ -18,6 +18,7 @@
#include <Path.h> #include <Path.h>
#include <DefaultColors.h> #include <DefaultColors.h>
#include <InterfaceDefs.h>
#include <ServerReadOnlyMemory.h> #include <ServerReadOnlyMemory.h>
#include "Desktop.h" #include "Desktop.h"
@@ -77,7 +78,7 @@ DesktopSettingsPrivate::_SetDefaults()
fWorkspacesRows = 2; fWorkspacesRows = 2;
memcpy(fShared.colors, BPrivate::kDefaultColors, memcpy(fShared.colors, BPrivate::kDefaultColors,
sizeof(rgb_color) * kNumColors); sizeof(rgb_color) * B_COLOR_WHICH_COUNT);
gSubpixelAntialiasing = false; gSubpixelAntialiasing = false;
gDefaultHintingMode = HINTING_MODE_ON; gDefaultHintingMode = HINTING_MODE_ON;
@@ -291,7 +292,7 @@ DesktopSettingsPrivate::_Load()
} }
// colors // colors
for (int32 i = 0; i < kNumColors; i++) { for (int32 i = 0; i < B_COLOR_WHICH_COUNT; i++) {
char colorName[12]; char colorName[12];
snprintf(colorName, sizeof(colorName), "color%" B_PRId32, snprintf(colorName, sizeof(colorName), "color%" B_PRId32,
(int32)index_to_color_which(i)); (int32)index_to_color_which(i));
@@ -436,7 +437,7 @@ DesktopSettingsPrivate::Save(uint32 mask)
settings.AddInt8("subpixel average weight", gSubpixelAverageWeight); settings.AddInt8("subpixel average weight", gSubpixelAverageWeight);
settings.AddBool("subpixel ordering", gSubpixelOrderingRGB); settings.AddBool("subpixel ordering", gSubpixelOrderingRGB);
for (int32 i = 0; i < kNumColors; i++) { for (int32 i = 0; i < B_COLOR_WHICH_COUNT; i++) {
char colorName[12]; char colorName[12];
snprintf(colorName, sizeof(colorName), "color%" B_PRId32, snprintf(colorName, sizeof(colorName), "color%" B_PRId32,
(int32)index_to_color_which(i)); (int32)index_to_color_which(i));
@@ -648,10 +649,10 @@ DesktopSettingsPrivate::WorkspacesMessage(int32 index) const
void void
DesktopSettingsPrivate::SetUIColor(color_which which, const rgb_color color) DesktopSettingsPrivate::SetUIColor(color_which which, const rgb_color color)
{ {
//
int32 index = color_which_to_index(which); int32 index = color_which_to_index(which);
if (index < 0 || index >= kNumColors) if (index < 0 || index >= B_COLOR_WHICH_COUNT)
return; return;
fShared.colors[index] = color; fShared.colors[index] = color;
// TODO: deprecate the background_color member of the menu_info struct, // TODO: deprecate the background_color member of the menu_info struct,
// otherwise we have to keep this duplication... // otherwise we have to keep this duplication...
@@ -666,7 +667,7 @@ DesktopSettingsPrivate::UIColor(color_which which) const
{ {
static const rgb_color invalidColor = {0, 0, 0, 0}; static const rgb_color invalidColor = {0, 0, 0, 0};
int32 index = color_which_to_index(which); int32 index = color_which_to_index(which);
if (index < 0 || index >= kNumColors) if (index < 0 || index >= B_COLOR_WHICH_COUNT)
return invalidColor; return invalidColor;
return fShared.colors[index]; return fShared.colors[index];
} }