Browse Source

Custom font colors history

Source commit: cc130170b9c47c420d468b72fafca3b7eb52c804
Martin Prikryl 7 years ago
parent
commit
a3dcf9495e

+ 2 - 0
source/windows/CustomWinConfiguration.cpp

@@ -120,6 +120,7 @@ void __fastcall TCustomWinConfiguration::Default()
   FConfirmExitOnCompletion = true;
   FSynchronizeSummary = true;
   FSessionColors = L"";
+  FFontColors = L"";
   FCopyShortCutHintShown = false;
   FHttpForWebDAV = false;
 
@@ -150,6 +151,7 @@ void __fastcall TCustomWinConfiguration::Saved()
     KEY(Bool,     ConfirmExitOnCompletion); \
     KEY(Bool,     SynchronizeSummary); \
     KEY(String,   SessionColors); \
+    KEY(String,   FontColors); \
     KEY(Bool,     CopyShortCutHintShown); \
     KEY(Bool,     HttpForWebDAV); \
   ) \

+ 2 - 0
source/windows/CustomWinConfiguration.h

@@ -58,6 +58,7 @@ private:
   bool FConfirmExitOnCompletion;
   bool FSynchronizeSummary;
   UnicodeString FSessionColors;
+  UnicodeString FFontColors;
   bool FCopyShortCutHintShown;
   bool FHttpForWebDAV;
   TNotifyEvent FOnMasterPasswordRecrypt;
@@ -105,6 +106,7 @@ public:
   __property bool ConfirmExitOnCompletion  = { read=FConfirmExitOnCompletion, write=SetConfirmExitOnCompletion };
   __property bool SynchronizeSummary  = { read = FSynchronizeSummary, write = SetSynchronizeSummary };
   __property UnicodeString SessionColors  = { read=FSessionColors, write=FSessionColors };
+  __property UnicodeString FontColors  = { read=FFontColors, write=FFontColors };
   __property bool CopyShortCutHintShown  = { read=FCopyShortCutHintShown, write=FCopyShortCutHintShown };
   __property bool UseMasterPassword = { read = GetUseMasterPassword };
   __property bool HttpForWebDAV = { read = FHttpForWebDAV, write = FHttpForWebDAV };

+ 97 - 63
source/windows/UserInterface.cpp

@@ -757,22 +757,30 @@ static UnicodeString __fastcall ExtractColorStr(UnicodeString & Colors)
   return CutToChar(Colors, ColorSeparator, true);
 }
 //---------------------------------------------------------------------------
-static bool __fastcall IsStandardColor(TColor Color)
+static bool __fastcall IsStandardColor(bool SessionColors, TColor Color)
 {
-  for (int Row = 0; Row < StandardColorRows; Row++)
+  if (SessionColors)
   {
-    for (int Col = 0; Col < ColorCols; Col++)
+    for (int Row = 0; Row < StandardColorRows; Row++)
     {
-      TColor StandardColor;
-      UnicodeString Name; // unused
-      GetStandardSessionColorInfo(Col, Row, StandardColor, Name);
-      if (StandardColor == Color)
+      for (int Col = 0; Col < ColorCols; Col++)
       {
-        return true;
+        TColor StandardColor;
+        UnicodeString Name; // unused
+        GetStandardSessionColorInfo(Col, Row, StandardColor, Name);
+        if (StandardColor == Color)
+        {
+          return true;
+        }
       }
     }
+    return false;
+  }
+  else
+  {
+    std::unique_ptr<TTBXColorPalette> DefaultColorPalette(new TTBXColorPalette(NULL));
+    return (DefaultColorPalette->FindCell(Color).X >= 0);
   }
-  return false;
 }
 //---------------------------------------------------------------------------
 class TColorChangeData : public TComponent
@@ -811,6 +819,23 @@ TColorChangeData * __fastcall TColorChangeData::Retrieve(TObject * Object)
   return DebugNotNull(dynamic_cast<TColorChangeData *>(ColorChangeDataComponent));
 }
 //---------------------------------------------------------------------------
+static void SaveCustomColors(bool SessionColors, const UnicodeString & Colors)
+{
+  if (SessionColors)
+  {
+    CustomWinConfiguration->SessionColors = Colors;
+  }
+  else
+  {
+    CustomWinConfiguration->FontColors = Colors;
+  }
+}
+//---------------------------------------------------------------------------
+static UnicodeString LoadCustomColors(bool SessionColors)
+{
+  return SessionColors ? CustomWinConfiguration->SessionColors : CustomWinConfiguration->FontColors;
+}
+//---------------------------------------------------------------------------
 void __fastcall TColorChangeData::ColorChange(TColor Color)
 {
   // Color palette returns clNone when no color is selected,
@@ -821,21 +846,20 @@ void __fastcall TColorChangeData::ColorChange(TColor Color)
     Color = TColor(0);
   }
 
-  if (SessionColors &&
-      (Color != TColor(0)) &&
-      !IsStandardColor(Color))
+  if ((Color != TColor(0)) &&
+      !IsStandardColor(SessionColors, Color))
   {
-    UnicodeString SessionColors = StoreColor(Color);
-    UnicodeString Temp = CustomWinConfiguration->SessionColors;
+    UnicodeString Colors = StoreColor(Color);
+    UnicodeString Temp = LoadCustomColors(SessionColors);
     while (!Temp.IsEmpty())
     {
       UnicodeString CStr = ExtractColorStr(Temp);
       if (RestoreColor(CStr) != Color)
       {
-        SessionColors += UnicodeString(ColorSeparator) + CStr;
+        Colors += UnicodeString(ColorSeparator) + CStr;
       }
     }
-    CustomWinConfiguration->SessionColors = SessionColors;
+    SaveCustomColors(SessionColors, Colors);
   }
 
   FOnColorChange(Color);
@@ -865,52 +889,60 @@ static void __fastcall ColorPickClick(void * /*Data*/, TObject * Sender)
   Dialog->Options = Dialog->Options << cdFullOpen << cdAnyColor;
   Dialog->Color = (ColorChangeData->Color != 0 ? ColorChangeData->Color : clSkyBlue);
 
-  if (ColorChangeData->SessionColors)
+  UnicodeString Temp = LoadCustomColors(ColorChangeData->SessionColors);
+  int StandardColorIndex = 0;
+  for (int Index = 0; Index < MaxCustomColors; Index++)
   {
-    UnicodeString Temp = CustomWinConfiguration->SessionColors;
-    int StandardColorIndex = 0;
-    int CustomColors = Min(MaxCustomColors, StandardColorCount);
-    for (int Index = 0; Index < CustomColors; Index++)
+    TColor CustomColor;
+    if (!Temp.IsEmpty())
     {
-      TColor CustomColor;
-      if (!Temp.IsEmpty())
+      CustomColor = RestoreColor(ExtractColorStr(Temp));
+    }
+    else
+    {
+      if (ColorChangeData->SessionColors)
       {
-        CustomColor = RestoreColor(ExtractColorStr(Temp));
+        if (StandardColorIndex < StandardColorCount)
+        {
+          UnicodeString Name; // not used
+          GetStandardSessionColorInfo(
+            StandardColorIndex % ColorCols, StandardColorIndex / ColorCols,
+            CustomColor, Name);
+          StandardColorIndex++;
+        }
+        else
+        {
+          break;
+        }
       }
       else
       {
-        UnicodeString Name; // not used
-        DebugAssert(StandardColorIndex < StandardColorCount);
-        GetStandardSessionColorInfo(
-          StandardColorIndex % ColorCols, StandardColorIndex / ColorCols,
-          CustomColor, Name);
-        StandardColorIndex++;
+        // no standard font colors
+        break;
       }
-      Dialog->CustomColors->Values[CustomColorName(Index)] = StoreColor(CustomColor);
     }
+    Dialog->CustomColors->Values[CustomColorName(Index)] = StoreColor(CustomColor);
   }
 
   if (Dialog->Execute())
   {
-    if (ColorChangeData->SessionColors)
+    // so that we do not have to try to preserve the excess colors
+    DebugAssert(UserColorCount <= MaxCustomColors);
+    UnicodeString Colors;
+    for (int Index = 0; Index < MaxCustomColors; Index++)
     {
-      // so that we do not have to try to preserve the excess colors
-      DebugAssert(UserColorCount <= MaxCustomColors);
-      UnicodeString SessionColors;
-      for (int Index = 0; Index < MaxCustomColors; Index++)
+      UnicodeString CStr = Dialog->CustomColors->Values[CustomColorName(Index)];
+      if (!CStr.IsEmpty())
       {
-        UnicodeString CStr = Dialog->CustomColors->Values[CustomColorName(Index)];
-        if (!CStr.IsEmpty())
+        TColor CustomColor = RestoreColor(CStr);
+        if ((CustomColor != static_cast<TColor>(-1)) &&
+            !IsStandardColor(ColorChangeData->SessionColors, CustomColor))
         {
-          TColor CustomColor = RestoreColor(CStr);
-          if (!IsStandardColor(CustomColor))
-          {
-            AddToList(SessionColors, StoreColor(CustomColor), ColorSeparator);
-          }
+          AddToList(Colors, StoreColor(CustomColor), ColorSeparator);
         }
       }
-      CustomWinConfiguration->SessionColors = SessionColors;
     }
+    SaveCustomColors(ColorChangeData->SessionColors, Colors);
 
     // call color change only after copying custom colors back,
     // so that it can add selected color to the user list
@@ -926,11 +958,12 @@ TPopupMenu * __fastcall CreateSessionColorPopupMenu(TColor Color,
   return PopupMenu.release();
 }
 //---------------------------------------------------------------------------
-static void __fastcall UserSessionColorSetGetColorInfo(
+static void __fastcall UserCustomColorSetGetColorInfo(
   void * /*Data*/, TTBXCustomColorSet * Sender, int Col, int Row, TColor & Color, UnicodeString & /*Name*/)
 {
   int Index = (Row * Sender->ColCount) + Col;
-  UnicodeString Temp = CustomWinConfiguration->SessionColors;
+  bool SessionColors = static_cast<bool>(Sender->Tag);
+  UnicodeString Temp = LoadCustomColors(SessionColors);
   while ((Index > 0) && !Temp.IsEmpty())
   {
     ExtractColorStr(Temp);
@@ -963,6 +996,7 @@ void __fastcall CreateColorPalette(TTBCustomItem * Owner, TColor Color, int Rows
     ColorSet->ColCount = ColorCols;
     ColorSet->RowCount = Rows;
     ColorSet->OnGetColorInfo = OnGetColorInfo;
+    ColorSet->Tag = static_cast<int>(SessionColors);
   }
 
   // clNone = no selection, see also ColorChange
@@ -998,27 +1032,27 @@ static void __fastcall CreateColorMenu(TComponent * AOwner, TColor Color,
 
     Owner->Add(new TTBXSeparatorItem(Owner));
 
-    if (SessionColors)
+    int CustomColorCount = 0;
+    UnicodeString Temp = LoadCustomColors(SessionColors);
+    while (!Temp.IsEmpty())
     {
-      int SessionColorCount = 0;
-      UnicodeString Temp = CustomWinConfiguration->SessionColors;
-      while (!Temp.IsEmpty())
-      {
-        SessionColorCount++;
-        ExtractColorStr(Temp);
-      }
+      CustomColorCount++;
+      ExtractColorStr(Temp);
+    }
 
-      if (SessionColorCount > 0)
-      {
-        SessionColorCount = Min(SessionColorCount, UserColorCount);
-        int RowCount = ((SessionColorCount + ColorCols - 1) / ColorCols);
-        DebugAssert(RowCount <= UserColorRows);
+    if (CustomColorCount > 0)
+    {
+      CustomColorCount = Min(CustomColorCount, UserColorCount);
+      int RowCount = ((CustomColorCount + ColorCols - 1) / ColorCols);
+      DebugAssert(RowCount <= UserColorRows);
 
-        CreateColorPalette(Owner, Color, RowCount,
-          MakeMethod<TCSGetColorInfo>(NULL, UserSessionColorSetGetColorInfo),
-          OnColorChange, SessionColors);
-      }
+      CreateColorPalette(Owner, Color, RowCount,
+        MakeMethod<TCSGetColorInfo>(NULL, UserCustomColorSetGetColorInfo),
+        OnColorChange, SessionColors);
+    }
 
+    if (SessionColors)
+    {
       CreateColorPalette(Owner, Color, StandardColorRows,
         MakeMethod<TCSGetColorInfo>(NULL, SessionColorSetGetColorInfo),
         OnColorChange, SessionColors);