[haiku-commits] haiku: hrev45444 - headers/private/app headers/os/interface src/servers/app src/kits/interface

  • From: jscipione@xxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Sat, 6 Apr 2013 04:50:22 +0200 (CEST)

hrev45444 adds 1 changeset to branch 'master'
old head: 9f24981a56edba10807b550b521563b0cadd12c7
new head: 33025215566c130e9d950fc95d4605617203c248
overview: http://cgit.haiku-os.org/haiku/log/?qt=range&q=3302521+%5E9f24981

----------------------------------------------------------------------------

3302521: 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

                                     [ John Scipione <jscipione@xxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev45444
Commit:      33025215566c130e9d950fc95d4605617203c248
URL:         http://cgit.haiku-os.org/haiku/commit/?id=3302521
Author:      John Scipione <jscipione@xxxxxxxxx>
Date:        Sat Apr  6 00:15:16 2013 UTC

----------------------------------------------------------------------------

4 files changed, 24 insertions(+), 22 deletions(-)
headers/os/interface/InterfaceDefs.h       | 10 ++++++++--
headers/private/app/ServerReadOnlyMemory.h | 17 ++++++-----------
src/kits/interface/InterfaceDefs.cpp       |  6 +++---
src/servers/app/DesktopSettings.cpp        | 13 +++++++------

----------------------------------------------------------------------------

diff --git a/headers/os/interface/InterfaceDefs.h 
b/headers/os/interface/InterfaceDefs.h
index 02b9add..38587d1 100644
--- a/headers/os/interface/InterfaceDefs.h
+++ b/headers/os/interface/InterfaceDefs.h
@@ -331,9 +331,15 @@ enum color_which {
        B_KEYBOARD_NAVIGATION_COLOR = B_NAVIGATION_BASE_COLOR,
        B_MENU_SELECTION_BACKGROUND_COLOR = B_MENU_SELECTED_BACKGROUND_COLOR,
 
-       // These are deprecated -- do not use in new code.  See BScreen for
-       // the replacement for B_DESKTOP_COLOR.
+       // Update this constant to be the largest color constant excluding
+       // 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
+               // see BScreen class for B_DESKTOP_COLOR replacement
 };
 
 
diff --git a/headers/private/app/ServerReadOnlyMemory.h 
b/headers/private/app/ServerReadOnlyMemory.h
index 46f925e..1401467 100644
--- a/headers/private/app/ServerReadOnlyMemory.h
+++ b/headers/private/app/ServerReadOnlyMemory.h
@@ -13,23 +13,18 @@
 #include <InterfaceDefs.h>
 
 
-static const int32 kNumColors = 35;
-
 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
 color_which_to_index(color_which which)
 {
-       // NOTE: this must be kept in sync with InterfaceDefs.h color_which!
-       if (which <= B_SCROLL_BAR_THUMB_COLOR)
+       if (which <= B_COLOR_WHICH_COUNT - 3)
                return which - 1;
        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;
 }
@@ -38,12 +33,12 @@ color_which_to_index(color_which which)
 static inline color_which
 index_to_color_which(int32 index)
 {
-       if (index >= 0 && index < kNumColors) {
-               if ((color_which)index < B_SCROLL_BAR_THUMB_COLOR)
+       if (index >= 0 && index < B_COLOR_WHICH_COUNT) {
+               if ((color_which)index < B_COLOR_WHICH_COUNT - 3)
                        return (color_which)(index + 1);
                else {
                        return (color_which)(index + B_SUCCESS_COLOR
-                         - B_SCROLL_BAR_THUMB_COLOR);
+                               - B_COLOR_WHICH_COUNT - 3);
                }
        }
 
diff --git a/src/kits/interface/InterfaceDefs.cpp 
b/src/kits/interface/InterfaceDefs.cpp
index 0019676..7744821 100644
--- a/src/kits/interface/InterfaceDefs.cpp
+++ b/src/kits/interface/InterfaceDefs.cpp
@@ -69,7 +69,7 @@ menu_info *_menu_info_ptr_;
 
 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_MENU_BACKGROUND_COLOR
        {255, 203, 0, 255},             // B_WINDOW_TAB_COLOR
@@ -1071,7 +1071,7 @@ rgb_color
 ui_color(color_which 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);
                return make_color(0, 0, 0);
        }
@@ -1090,7 +1090,7 @@ void
 set_ui_color(const color_which &which, const rgb_color &color)
 {
        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);
                return;
        }
diff --git a/src/servers/app/DesktopSettings.cpp 
b/src/servers/app/DesktopSettings.cpp
index f5660b9..12646ae 100644
--- a/src/servers/app/DesktopSettings.cpp
+++ b/src/servers/app/DesktopSettings.cpp
@@ -18,6 +18,7 @@
 #include <Path.h>
 
 #include <DefaultColors.h>
+#include <InterfaceDefs.h>
 #include <ServerReadOnlyMemory.h>
 
 #include "Desktop.h"
@@ -77,7 +78,7 @@ DesktopSettingsPrivate::_SetDefaults()
        fWorkspacesRows = 2;
 
        memcpy(fShared.colors, BPrivate::kDefaultColors,
-               sizeof(rgb_color) * kNumColors);
+               sizeof(rgb_color) * B_COLOR_WHICH_COUNT);
 
        gSubpixelAntialiasing = false;
        gDefaultHintingMode = HINTING_MODE_ON;
@@ -291,7 +292,7 @@ DesktopSettingsPrivate::_Load()
                        }
 
                        // colors
-                       for (int32 i = 0; i < kNumColors; i++) {
+                       for (int32 i = 0; i < B_COLOR_WHICH_COUNT; i++) {
                                char colorName[12];
                                snprintf(colorName, sizeof(colorName), "color%" 
B_PRId32,
                                        (int32)index_to_color_which(i));
@@ -436,7 +437,7 @@ DesktopSettingsPrivate::Save(uint32 mask)
                        settings.AddInt8("subpixel average weight", 
gSubpixelAverageWeight);
                        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];
                                snprintf(colorName, sizeof(colorName), "color%" 
B_PRId32,
                                        (int32)index_to_color_which(i));
@@ -648,10 +649,10 @@ DesktopSettingsPrivate::WorkspacesMessage(int32 index) 
const
 void
 DesktopSettingsPrivate::SetUIColor(color_which which, const rgb_color color)
 {
-       //
        int32 index = color_which_to_index(which);
-       if (index < 0 || index >= kNumColors)
+       if (index < 0 || index >= B_COLOR_WHICH_COUNT)
                return;
+
        fShared.colors[index] = color;
        // TODO: deprecate the background_color member of the menu_info struct,
        // 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};
        int32 index = color_which_to_index(which);
-       if (index < 0 || index >= kNumColors)
+       if (index < 0 || index >= B_COLOR_WHICH_COUNT)
                return invalidColor;
        return fShared.colors[index];
 }


Other related posts: