diff --git a/examples/UWord/UWord.cpp b/examples/UWord/UWord.cpp index a4c69d688..af882440d 100644 --- a/examples/UWord/UWord.cpp +++ b/examples/UWord/UWord.cpp @@ -258,6 +258,7 @@ UWord::UWord() static int doc; Title(Format("Document%d", ++doc)); Icon(CtrlImg::File()); + editor.AllowDarkContent(); editor.ClearModify(); SetBar(); editor.WhenRefreshBar = THISBACK(SetBar); diff --git a/uppsrc/CtrlLib/ColorPopup.cpp b/uppsrc/CtrlLib/ColorPopup.cpp index 5832c4db9..323553137 100644 --- a/uppsrc/CtrlLib/ColorPopup.cpp +++ b/uppsrc/CtrlLib/ColorPopup.cpp @@ -50,9 +50,17 @@ Color ColorPopUp::hint[18]; ColorPopUp& ColorPopUp::DarkContent(bool b) { - dark = b; wheel.DarkContent(b); ramp.DarkContent(b); + Refresh(); + return *this; +} + +ColorPopUp& ColorPopUp::AllowDarkContent(bool b) +{ + wheel.AllowDarkContent(b); + ramp.AllowDarkContent(b); + Refresh(); return *this; } @@ -201,6 +209,7 @@ void ColorPopUp::Paint(Draw& w) } int i = 0; + bool dark = IsDarkContent(); for(;;) { for(int x = 0; x < 18 * DPI(16); x += DPI(16)) { if(i >= GetColorCount()) { diff --git a/uppsrc/CtrlLib/ColorPusher.cpp b/uppsrc/CtrlLib/ColorPusher.cpp index f276775b5..d57f0f632 100644 --- a/uppsrc/CtrlLib/ColorPusher.cpp +++ b/uppsrc/CtrlLib/ColorPusher.cpp @@ -24,7 +24,7 @@ void ColorPusher::Paint(Draw& w) w.DrawRect(x + cx / 2, y, cx - cx / 2, cy, DarkTheme(c)); } else - w.DrawRect(x, y, cx, cy, color); + w.DrawRect(x, y, cx, cy, colors.IsDarkContent() ? DarkTheme(color) : color); }; if(withtext || withhex) { DrawColor(2, 2, sz.cy - 4, sz.cy - 4); @@ -145,7 +145,7 @@ void ColorButton::Paint(Draw& w) if(IsNull(color)) w.DrawImage(center.x + push, center.y + push, nullimage); else - w.DrawImage(center.x + push, center.y + push, image, color); + w.DrawImage(center.x + push, center.y + push, image, colors.IsDarkContent() ? DarkTheme(color) : color); w.DrawImage(center.x + push, center.y + push, staticimage); } diff --git a/uppsrc/CtrlLib/Ctrl.lay b/uppsrc/CtrlLib/Ctrl.lay index d0938d930..9162b5dbd 100644 --- a/uppsrc/CtrlLib/Ctrl.lay +++ b/uppsrc/CtrlLib/Ctrl.lay @@ -65,41 +65,6 @@ LAYOUT(FileSelectorLayout, 652, 488) ITEM(Upp::Button, cancel, SetLabel(t_("Cancel")).RightPosZ(8, 68).BottomPosZ(8, 24)) END_LAYOUT -LAYOUT(PaletteLayout, 216, 332) - ITEM(Upp::TabCtrl, palette_tab, HSizePosZ(4, 4).VSizePosZ(4, 82)) - ITEM(Upp::Label, dv___1, SetLabel(t_("Color:")).LeftPosZ(4, 36).BottomPosZ(52, 24)) - ITEM(Upp::DataPusher, color, HSizePosZ(68, 32).BottomPosZ(54, 22)) - ITEM(Upp::Button, set_transparent, RightPosZ(4, 18).BottomPosZ(56, 18)) - UNTYPED(fetch_color, RightPosZ(4, 18).BottomPosZ(30, 18)) - ITEM(Upp::Label, dv___5, SetLabel(t_("R:")).LeftPosZ(4, 12).BottomPosZ(29, 19)) - ITEM(Upp::EditIntSpin, r, LeftPosZ(16, 44).BottomPosZ(29, 19)) - ITEM(Upp::Label, dv___7, SetLabel(t_("G:")).LeftPosZ(72, 12).BottomPosZ(29, 19)) - ITEM(Upp::EditIntSpin, g, LeftPosZ(84, 44).BottomPosZ(29, 19)) - ITEM(Upp::Label, dv___9, SetLabel(t_("B:")).LeftPosZ(136, 12).BottomPosZ(29, 19)) - ITEM(Upp::EditIntSpin, b, LeftPosZ(150, 42).BottomPosZ(29, 19)) - ITEM(Upp::Label, dv___11, SetLabel(t_("H:")).LeftPosZ(4, 12).BottomPosZ(5, 19)) - ITEM(Upp::EditIntSpin, h, LeftPosZ(16, 44).BottomPosZ(5, 19)) - ITEM(Upp::Label, dv___13, SetLabel(t_("S:")).LeftPosZ(72, 12).BottomPosZ(5, 19)) - ITEM(Upp::EditIntSpin, s, LeftPosZ(84, 44).BottomPosZ(5, 19)) - ITEM(Upp::Label, dv___15, SetLabel(t_("V:")).LeftPosZ(136, 12).BottomPosZ(5, 19)) - ITEM(Upp::EditIntSpin, v, LeftPosZ(148, 44).BottomPosZ(5, 19)) -END_LAYOUT - -LAYOUT(PaletteSelectorLayout, 248, 320) - ITEM(Ctrl, palette, HSizePosZ(4, 4).VSizePosZ(4, 32)) - ITEM(Upp::Button, ok, SetLabel(t_("OK")).RightPosZ(88, 80).BottomPosZ(4, 22)) - ITEM(Upp::Button, cancel, SetLabel(t_("Cancel")).RightPosZ(4, 80).BottomPosZ(4, 22)) -END_LAYOUT - -LAYOUT(PalCtrlSizeLayout, 206, 60) - ITEM(Upp::Label, dv___0, SetLabel(t_("&Rows:")).LeftPosZ(4, 44).TopPosZ(4, 19)) - ITEM(Upp::EditIntSpin, rows, LeftPosZ(48, 50).TopPosZ(4, 19)) - ITEM(Upp::Label, dv___2, SetLabel(t_("&Columns:")).LeftPosZ(108, 44).TopPosZ(4, 19)) - ITEM(Upp::EditIntSpin, columns, LeftPosZ(152, 50).TopPosZ(4, 19)) - ITEM(Upp::Button, ok, SetLabel(t_("OK")).RightPosZ(88, 80).BottomPosZ(4, 22)) - ITEM(Upp::Button, cancel, SetLabel(t_("Cancel")).RightPosZ(4, 80).BottomPosZ(4, 22)) -END_LAYOUT - LAYOUT(KeysLayout, 604, 500) ITEM(Upp::ArrayCtrl, group, LeftPosZ(8, 116).TopPosZ(8, 452)) ITEM(Upp::ArrayCtrl, keys, LeftPosZ(132, 464).TopPosZ(8, 452)) diff --git a/uppsrc/CtrlLib/DlgColor.cpp b/uppsrc/CtrlLib/DlgColor.cpp index 42591b44c..0d6a5eaab 100644 --- a/uppsrc/CtrlLib/DlgColor.cpp +++ b/uppsrc/CtrlLib/DlgColor.cpp @@ -1,86 +1,7 @@ #include "CtrlLib.h" namespace Upp { - -class FetchColorCtrl : public Button -{ -public: - FetchColorCtrl(); - - void SetData(const Value& v); - Value GetData() const; - - virtual void LeftDown(Point pt, dword keyflags); - virtual void MouseMove(Point pt, dword keyflags); - virtual bool Key(dword key, int repcnt); - virtual Image CursorImage(Point pt, dword keyflags); - -private: - Color user_color; - Color std_color; -}; - -FetchColorCtrl::FetchColorCtrl() -{ - SetImage(CtrlImg::fetch_color()); -} - -void FetchColorCtrl::SetData(const Value& v) -{ - if(IsNull(user_color)) - std_color = v; -} - -Value FetchColorCtrl::GetData() const -{ - return Nvl(user_color, std_color); -} - -void FetchColorCtrl::LeftDown(Point pt, dword keyflags) -{ - Button::LeftDown(pt, keyflags); - SetCapture(); - SetWantFocus(); -} - -void FetchColorCtrl::MouseMove(Point pt, dword keyflags) -{ - if(keyflags & K_MOUSELEFT) - { - if(Rect(GetSize()).Contains(pt)) - { - user_color = Null; - Action(); - } - else - { - pt += GetScreenView().TopLeft(); - user_color = GuiPlatformGetScreenPixel(pt.x, pt.y); - Action(); - } - } - Button::MouseMove(pt, keyflags); -} - -bool FetchColorCtrl::Key(dword key, int repcnt) -{ - if(key == K_ESCAPE && HasCapture()) - { - ReleaseCapture(); - user_color = Null; - Action(); - return true; - } - return Button::Key(key, repcnt); -} - -Image FetchColorCtrl::CursorImage(Point pt, dword keyflags) -{ - if(keyflags & K_MOUSELEFT) - return CtrlImg::fetch_color_cursor(); - return Button::CursorImage(pt, keyflags); -} - + const Display& StdColorDisplayNull() { static ColorDisplayNull display(t_("(no color)")); @@ -467,7 +388,6 @@ enum { PREC = 64 }; Image WheelRampCtrl::PaintRamp(Size size) { - DTIMING("Ramp"); ImageDraw iw(size); ImageBuffer ib(PREC, PREC); for(int y = 0; y < PREC; y++) { @@ -476,7 +396,7 @@ Image WheelRampCtrl::PaintRamp(Size size) for(int x = 0; x < PREC; x++) { int s16 = iscale(x, 65535, PREC - 1); Color c = HSV16toRGB(h16, s16, v16); - if(dark) + if(IsDarkContent()) c = DarkTheme(c); scan->r = GetRRaw(c); scan->g = GetGRaw(c); @@ -492,7 +412,6 @@ Image WheelRampCtrl::PaintRamp(Size size) Image WheelRampCtrl::PaintWheel(Size size) { - DTIMING("Wheel"); ImageBuffer ib(PREC, PREC); static WheelBuff wb[PREC * PREC]; ONCELOCK { @@ -515,7 +434,7 @@ Image WheelRampCtrl::PaintWheel(Size size) RGBA *scan = ib[y]; for(int x = 0; x < PREC; x++) { Color c = HSV16toRGB(cwb->arg, cwb->l, v16); - if(dark) + if(IsDarkContent()) c = DarkTheme(c); *scan++ = c; cwb++; @@ -549,7 +468,7 @@ void WheelRampCtrl::PaintColumn(Draw& draw) int factor = ClientToLevel(i); Color c = ramp ? HSV16toRGB(factor, 65535, 65535) : HSV16toRGB(h16, s16, factor); // : Color(iscale(nr, factor, 65536), iscale(ng, factor, 65536), iscale(nb, factor, 65536)); - draw.DrawRect(column_rect.left, i, size.cx, 1, dark ? DarkThemeCached(c) : c); + draw.DrawRect(column_rect.left, i, size.cx, 1, IsDarkContent() ? DarkThemeCached(c) : c); } } @@ -908,714 +827,4 @@ static void InitColor(Color *out) out[15] = Color(0x00, 0xFF, 0xFF); }; -PalCtrl::Config& PalCtrl::GlobalConfig() -{ - static PalCtrl::Config x; - return x; -} - -typedef Index PalCtrlIndex; - -PalCtrlIndex& PalCtrl::GetActive() -{ - static PalCtrlIndex x; - return x; -} - -PalCtrl::PalCtrl() -{ - color = Null; - color_index = -1; - swap_index = -1; - cellcount = Size(4, 4); - Transparent(); -} - -PalCtrl::~PalCtrl() -{ - GetActive().RemoveKey(this); -} - -Vector PalCtrl::GetPalette() -{ - int count = MAXSIZE; - while(count > 0 && IsNull(GlobalConfig().current[count - 1])) - count--; - Vector out; - out.SetCount(count); - for(int i = 0; i < count; i++) - out[i] = GlobalConfig().current[i]; - return out; -} - - -void PalCtrl::SetPalette(const Vector& pal) -{ - for(int n = min(pal.GetCount(), MAXSIZE), i = 0; i < n; i++) - GlobalConfig().current[i] = pal[i]; - PalCtrlIndex& active = GetActive(); - for(int c = 0; c < active.GetCount(); c++) - active[c]->Refresh(); -} - -void PalCtrl::SerializePalette(Stream& stream) -{ - int version = StreamHeading(stream, 1, 1, 1, "PalCtrl::Palette"); - if(version >= 1) - { - int count = cellcount.cx * cellcount.cy; - stream / count; - for(int i = 0; i < count; i++) - stream % GlobalConfig().current[i]; - } -} - -INITBLOCK -{ - RegisterGlobalConfig("PalCtrl", callback(&PalCtrl::FlushConfig)); -} - -void PalCtrl::Config::Serialize(Stream& stream) -{ - if(stream.IsLoading() && stream.IsEof()) - return; - int version = 1; - stream / version; - if(stream.IsError() || version < 1 || version > 1) - { - stream.SetError(); - return; - } - int count = __countof(current); - stream / count; - count = min(count, MAXSIZE); - for(int i = 0; i < count; i++) - stream % current[i]; - stream % lastsize % lastfile; -} - -void PalCtrl::Load() -{ - Config& gc = GlobalConfig(); - if(!gc.loaded) - { - gc.loaded = true; - InitColor(gc.current); - LoadFromGlobal(gc, "PalCtrl"); - cellcount = gc.lastsize; - Layout(); - UpdateColorIndex(); - } -} - -void PalCtrl::SetData(const Value& value) -{ - if(Color(value) != color) - { - color = value; - UpdateColorIndex(); - UpdateRefresh(); - } -} - -void PalCtrl::UpdateColorIndex() -{ - Config& gc = GlobalConfig(); - if(gc.loaded) - color_index =(int)( UPP::Find(gc.current, - gc.current + __countof(gc.current), color, StdEqual()) - gc.current); -} - -void PalCtrl::Layout() -{ - cell = max((GetSize() - 2 * OGAP - (cellcount + 1) * IGAP) / cellcount, Size(2, 2)); - step = cell + IGAP; - offset = (GetSize() - cellcount * step + IGAP) >> 1; - Refresh(); -} - -void PalCtrl::Paint(Draw& draw) -{ - Load(); - - GetActive().FindAdd(this); - draw.Begin(); - Size tcell = GetTextSize("256", StdFont()); - bool do_text = (tcell.cx <= cell.cx && tcell.cy <= cell.cy); - for(int i = 0, n = cellcount.cx * cellcount.cy; i < n; i++) - { - Rect rc = IndexToClient(i); - DrawFrame(draw, rc, White, Black); - rc.Deflate(1); - Color c = Nvl(GlobalConfig().current[i], SColorFace); - draw.DrawRect(rc, c); - if(do_text) - { - int brightness = 3 * c.GetR() + 6 * c.GetG() + 1 * c.GetB(); - Color ink = (brightness <= 1280 ? White : Black); - draw.DrawText(rc.left + 2, rc.top + 2, IntStr(i + 1), StdFont(), ink); - } - rc.Inflate(1); - draw.ExcludeClip(rc); - } - if(color_index >= 0) - { - Rect selected = IndexToClient(color_index); - selected.Inflate(3); - DrawFatFrame(draw, selected, SColorMark(), 2); - } - draw.End(); -} - -void PalCtrl::LeftDown(Point pt, dword keyflags) -{ - swap_index = ClientToIndex(pt); - if(swap_index < 0) - return; - if(keyflags & K_CTRL) - { - set_index = swap_index; - swap_index = -1; - OnSetColor(); - return; - } - color = GlobalConfig().current[swap_index]; - color_index = swap_index; - UpdateActionRefresh(); - SetCapture(); -} - -void PalCtrl::LeftUp(Point pt, dword keyflags) -{ - ReleaseCapture(); - int i = ClientToIndex(pt); - if(i < 0 || swap_index < 0 || i == swap_index) - { - swap_index = -1; - return; - } - Swap(GlobalConfig().current[swap_index], GlobalConfig().current[i]); - color_index = i; - swap_index = -1; - UpdateActionRefresh(); -} - -Image PalCtrl::CursorImage(Point pt, dword keyflags) -{ - if(swap_index >= 0) - return CtrlImg::swap_color_cursor(); - return Image::Arrow(); -} - -void PalCtrl::RightDown(Point pt, dword keyflags) -{ - MenuBar bar; - set_index = ClientToIndex(pt); - bar.Add(set_index >= 0 && !IsNull(color), t_("Set color"), THISBACK(OnSetColor)) - .Help(t_("Write current color to selected palette slot")); - bar.Add(t_("Default palette"), THISBACK(OnStandard)) - .Help(t_("Restore default system palette")); - bar.MenuSeparator(); - bar.Add(t_("Save as.."), THISBACK(OnSave)) - .Help(t_("Store palette information into a disk file")); - bar.Add(t_("Load"), THISBACK(OnLoad)) - .Help(t_("Load previously saved palette")); - bar.MenuSeparator(); - bool is_small = (cellcount.cx == 4 && cellcount.cy == 4); - bool is_medium = (cellcount.cx == 8 && cellcount.cy == 8); - bool is_large = (cellcount.cx == 16 && cellcount.cy == 16); - bar.Add(t_("Small"), THISBACK(OnSizeSmall)) - .Check(is_small) - .Help(t_("Set up palette size 4 times 4 colors")); - bar.Add(t_("Medium"), THISBACK(OnSizeMedium)) - .Check(is_medium) - .Help(t_("Set up palette size 8 times 8 colors")); - bar.Add(t_("Large"), THISBACK(OnSizeLarge)) - .Check(is_large) - .Help(t_("Set up palette size 16 times 16 colors (maximum size)")); - bar.Add(t_("Custom..."), THISBACK(OnSizeCustom)) - .Check(!is_small && !is_medium && !is_large) - .Help(t_("Select custom palette size")); - bar.Execute(); -} - -void PalCtrl::OnSetColor() -{ - if(set_index >= 0 && set_index < cellcount.cx * cellcount.cy && !IsNull(color)) - { - GlobalConfig().current[set_index] = color; - UpdateRefresh(); - PalCtrlIndex& active = GetActive(); - for(int i = 0; i < active.GetCount(); i++) - { - PalCtrl *pal = active[i]; - if(pal->color_index == set_index && pal->color != color) - { - pal->UpdateColorIndex(); - pal->Refresh(); - } - } - } -} - -void PalCtrl::OnStandard() -{ - memcpy(GlobalConfig().current, std_palette, min(sizeof(GlobalConfig().current), sizeof(std_palette))); - PalCtrlIndex& active = GetActive(); - for(int i = 0; i < active.GetCount(); i++) - { - PalCtrl *pal = active[i]; - pal->UpdateColorIndex(); - pal->Refresh(); - } -} - -void PalCtrl::OnSave() -{ - recent_file = SelectFileSaveAs("Palette (*.pal)\n*.pal"); - if(recent_file.GetCount()) - { - StringStream stream; - SerializePalette(stream); - if(!SaveFile(recent_file, stream)) - Exclamation(Format(t_("Error writing file [* \1%s\1]."), recent_file)); - } -} - -void PalCtrl::OnLoad() -{ - recent_file = SelectFileOpen("Palette (*.pal)\n*.pal"); - if(recent_file.GetCount()) - { - FileIn fi(recent_file); - if(!fi.IsOpen()) - { - Exclamation(Format(t_("Error opening file [* \1%s\1]."), recent_file)); - return; - } - SerializePalette(fi); - fi.Close(); - if(fi.IsError()) - { - Exclamation(Format(t_("Error reading palette from file [* \1%s\1]."), recent_file)); - return; - } - Refresh(); - } -} - -void PalCtrl::OnSizeSmall() -{ - GlobalConfig().lastsize = cellcount = Size(4, 4); - Layout(); -} - -void PalCtrl::OnSizeMedium() -{ - GlobalConfig().lastsize = cellcount = Size(8, 8); - Layout(); -} - -void PalCtrl::OnSizeLarge() -{ - GlobalConfig().lastsize = cellcount = Size(16, 16); - Layout(); -} - -//RegisterHelpTopicObjectTitle(DlgPalCtrlSize, s_(PalCtrlSizeCustomDlgTitle)) - -void PalCtrl::OnSizeCustom() -{ - WithPalCtrlSizeLayout dlg; -// CtrlLayoutOKCancel(dlg, DlgPalCtrlSizeHelpTitle()); - CtrlLayoutOKCancel(dlg, t_("Palette dimensions")); - dlg.HelpTopic("DlgPalCtrlSize"); - dlg.rows <<= cellcount.cy; - dlg.columns <<= cellcount.cx; - dlg.rows.NotNull().MinMax(1, 16); - dlg.columns.NotNull().MinMax(1, 16); - if(dlg.Run() == IDOK) - { - cellcount.cx = ~dlg.columns; - cellcount.cy = ~dlg.rows; - GlobalConfig().lastsize = cellcount; - Layout(); - } -} - -int PalCtrl::ClientToIndex(Point pt) const -{ - Size rel = pt - offset + (IGAP / 2); - Point div = idivfloor(rel, step); - if(div.x < 0 || div.x >= cellcount.cx || div.y < 0 || div.y >= cellcount.cy) - return -1; - return div.x * cellcount.cy + div.y; -} - -Rect PalCtrl::IndexToClient(int index) const -{ - if(index < 0 || index >= cellcount.cx * cellcount.cy) - return Rect(Null); - return Rect(Size(index / cellcount.cy, index % cellcount.cy) * step + offset, cell); -} - -////////////////////////////////////////////////////////////////////// -// ColorSelectorImpl:: - -class ColorSelectorImpl : public WithPaletteLayout, public ColorSelector::Impl -{ -public: - typedef ColorSelectorImpl CLASSNAME; - ColorSelectorImpl(ColorSelector& parent); - virtual ~ColorSelectorImpl(); - - void Attach(Ctrl& parent); - - virtual void Set(Color color); - virtual Color Get() const; - - virtual void NotNull(bool _nn = true); - virtual bool IsNotNull() const; - - virtual Vector GetPalette() const; - virtual void SetPalette(const Vector& pal); - - virtual void SerializeConfig(Stream& stream); - - virtual Ctrl& GetCtrl() { return *this; } - -public: - FetchColorCtrl fetch_color; - -private: - void SetAction(Color color); - void SetRaw(); - - void UpdateRGB(); - void UpdateHSV(); - - void OnRGB(); - void OnHSV(); - -// void OnColor(); - void OnSetTransparent(); - - void OnWheelCtrl(); - void OnRampCtrl(); - void OnRGBCtrl(); - void OnHSVCtrl(); - void OnPalCtrl(); - - void OnFetchColor(); - -protected: -// WithPaletteLayout dialog; - ColorDisplayNull paint_color_display; - -private: - ColorSelector& parent; - Color colorval; - WheelRampCtrl wheel; - WheelRampCtrl ramp; - RGBCtrl rgb; - HSVCtrl hsv; - PalCtrl pal; -}; - -ColorSelectorImpl::ColorSelectorImpl(ColorSelector& parent) -: paint_color_display(t_("(transparent)")) -, parent(parent), wheel(false), ramp(true) -{ - Transparent(); - CtrlLayout(*this); - parent << SizePos(); - -// dialog.palette_tab.Background(SLtGray); - - palette_tab.Add(pal.HSizePos(4, 4).VSizePos(4, 4), t_("Palette")); - pal <<= THISBACK(OnPalCtrl); - - palette_tab.Add(wheel.HSizePos(4, 4).VSizePos(4, 4), t_("Wheel")); - wheel <<= THISBACK(OnWheelCtrl); - - palette_tab.Add(ramp.HSizePos(4, 4).VSizePos(4, 4), t_("Hue")); - ramp <<= THISBACK(OnRampCtrl); - - palette_tab.Add(rgb.HSizePos(4, 4).VSizePos(4, 4), t_("RGB")); - rgb <<= THISBACK(OnRGBCtrl); - - palette_tab.Add(hsv.HSizePos(4, 4).VSizePos(4, 4), t_("HSV")); - hsv <<= THISBACK(OnHSVCtrl); - - color.SetFrame(InsetFrame()); - color.SetDisplay(paint_color_display); - color.NoWantFocus(); - color.SetReadOnly(); -// dialog.color.WhenAction = THISBACK(OnColor); - - fetch_color <<= THISBACK(OnFetchColor); - - set_transparent <<= THISBACK(OnSetTransparent); - set_transparent.SetImage(CtrlImg::set_transparent()); - - EditIntSpin *spins[6] = - { - &r, &g, &b, - &h, &s, &v, - }; - - int i; - for(i = 0; i < __countof(spins); i++) - { - spins[i] -> MinMax(0, 255); - spins[i]->WhenAction = (i < 3 ? THISBACK(OnRGB) : THISBACK(OnHSV)); - } - - Set(Black); -} - -ColorSelectorImpl::~ColorSelectorImpl() -{ -} - -void ColorSelectorImpl::SerializeConfig(Stream& stream) -{ - int version = 2; - stream / version; - if(version < 2) - stream % palette_tab; - else { - int t = palette_tab.Get(); - stream / t; - palette_tab.Set(t); - } - Color c = colorval; - stream % c; - if(stream.IsLoading()) - Set(c); - PalCtrl::SerializeConfig(stream); -} - -void ColorSelectorImpl::NotNull(bool _nn) -{ - set_transparent.Enable(!_nn); -} - -bool ColorSelectorImpl::IsNotNull() const -{ - return !set_transparent.IsEnabled(); -} - -Vector ColorSelectorImpl::GetPalette() const -{ - return pal.GetPalette(); -} - -void ColorSelectorImpl::SetPalette(const Vector& p) -{ - pal.SetPalette(p); -} - -void ColorSelectorImpl::SetRaw() -{ - wheel <<= ramp <<= rgb <<= hsv <<= pal <<= colorval; - parent.WhenSetColor(); -} - -void ColorSelectorImpl::SetAction(Color color) -{ - Set(color); - parent.Action(); -} - -Color ColorSelectorImpl::Get() const -{ - return colorval; -} - -void ColorSelectorImpl::Set(Color c) -{ - colorval = c; - color <<= c; - fetch_color <<= c; - UpdateRGB(); - UpdateHSV(); - wheel <<= ramp <<= rgb <<= hsv <<= pal <<= c; - parent.Update(); - parent.WhenSetColor(); -} - -void ColorSelectorImpl::UpdateRGB() -{ - Color c = Nvl(colorval, Black); - r <<= c.GetR(); - g <<= c.GetG(); - b <<= c.GetB(); -} - -void ColorSelectorImpl::UpdateHSV() -{ - Color c = Nvl(colorval, Black); - double vh, vs, vv; - RGBtoHSV(c.GetR() / 255.0, c.GetG() / 255.0, c.GetB() / 255.0, vh, vs, vv); - h <<= fround(vh * 255); - s <<= fround(vs * 255); - v <<= fround(vv * 255); -} - -void ColorSelectorImpl::OnRGB() -{ - int vr = Nvl((int)~r, 0), vg = Nvl((int)~g, 0), vb = Nvl((int)~b, 0); - colorval = Color(vr, vg, vb); - color <<= colorval; - UpdateHSV(); - SetRaw(); -} - -void ColorSelectorImpl::OnHSV() -{ - int vh = Nvl((int)~h, 0), vs = Nvl((int)~s, 0), vv = Nvl((int)~v, 0); - double r, g, b; - HSVtoRGB(vh / 255.0, vs / 255.0, vv / 255.0, r, g, b); - colorval = Color(fround(255 * r), fround(255 * g), fround(255 * b)); - color <<= colorval; - UpdateRGB(); - SetRaw(); -} - -/* -void ColorSelectorImpl::OnColor() -{ - SetColor(~dialog.color); -} -*/ - -void ColorSelectorImpl::OnSetTransparent() -{ - SetAction(Null); -} - -void ColorSelectorImpl::OnWheelCtrl() -{ - SetAction(~wheel); -} - -void ColorSelectorImpl::OnRampCtrl() -{ - SetAction(~ramp); -} - -void ColorSelectorImpl::OnRGBCtrl() -{ - SetAction(~rgb); -} - -void ColorSelectorImpl::OnHSVCtrl() -{ - SetAction(~hsv); - h <<= hsv.GetHValue(); - s <<= hsv.GetSValue(); - v <<= hsv.GetVValue(); -} - -void ColorSelectorImpl::OnPalCtrl() -{ - SetAction(~pal); -} - -void ColorSelectorImpl::OnFetchColor() -{ - SetAction(~fetch_color); -} - -////////////////////////////////////////////////////////////////////// -// ColorSelector:: - -ColorSelector::ColorSelector(bool not_null) -{ - Transparent(); - impl = new ColorSelectorImpl(*this); - impl->NotNull(not_null); -} - -class DlgSelectColor : public WithPaletteSelectorLayout -{ -public: - typedef DlgSelectColor CLASSNAME; - DlgSelectColor(); - - virtual void Serialize(Stream& stream); - - Color Run(Color init_color, bool not_null, const char *title, bool *ok); - -private: - ColorSelector selector; -}; - -Color RunDlgSelectColor(Color init_color, bool not_null, const char *title, bool *ok) -{ return DlgSelectColor().Run(init_color, not_null, title, ok); } - -//RegisterHelpTopicObjectTitle(DlgSelectColor, s_(DlgColorDefaultTitle)); - -DlgSelectColor::DlgSelectColor() -{ - palette.Transparent().NoWantFocus() << selector.SizePos(); -// CtrlLayoutOKCancel(*this, DlgSelectColorHelpTitle()); - CtrlLayoutOKCancel(*this, t_("Select color")); -//RegisterHelpTopicObjectTitle(DlgSelectColor, ); - HelpTopic("DlgSelectColor"); - Sizeable().MaximizeBox(); -} - -INITBLOCK -{ - RegisterGlobalConfig("DlgSelectColor"); -} - -void DlgSelectColor::Serialize(Stream& stream) -{ - if(stream.IsLoading() && stream.IsEof()) - return; - int version = 1; - stream / version; - if(stream.IsError() || version < 1 || version > 1) { - stream.SetError(); - return; - } - SerializePlacement(stream); -} - -Color DlgSelectColor::Run(Color init_color, bool not_null, const char *title, bool *ok) -{ - if(title) - Title(title); - selector.NotNull(not_null).Set(init_color); - LoadFromGlobal(*this, "DlgSelectColor"); - bool is_ok = (TopWindow::Run() == IDOK); - StoreToGlobal(*this, "DlgSelectColor"); - if(ok) - *ok = is_ok; - return is_ok ? selector.Get() : init_color; -} - -ColorCtrl::ColorCtrl(bool not_null) -: DataPusher(NoConvert(), StdColorDisplayNull()) -{ - SetFrame(EditFieldFrame()); - AddFrame(empty); - empty.SetImage(CtrlImg::cross()); - empty <<= THISBACK(OnClear); - NotNull(not_null); -} - -ColorCtrl::~ColorCtrl() {} - -void ColorCtrl::DoAction() -{ - bool ok; - Color new_color = RunDlgSelectColor(GetConvert().Format(GetData()), IsNotNull(), 0, &ok); - if(ok) - SetDataAction(GetConvert().Scan(new_color)); -} - } diff --git a/uppsrc/CtrlLib/DlgColor.h b/uppsrc/CtrlLib/DlgColor.h index 1b683adb1..cbe6cb461 100644 --- a/uppsrc/CtrlLib/DlgColor.h +++ b/uppsrc/CtrlLib/DlgColor.h @@ -19,7 +19,9 @@ public: Event<> WhenLeftDouble; - void DarkContent(bool b = true) { dark = b; } + WheelRampCtrl& DarkContent(bool b = true) { dark_content = b; return *this; } + WheelRampCtrl& AllowDarkContent(bool b = true) { allow_dark_content = b; return *this; } + bool IsDarkContent() const { return dark_content || allow_dark_content && IsDarkTheme(); } private: void SetColor(Color color, bool set_norm, bool set_hsv); @@ -33,7 +35,8 @@ private: private: bool ramp; - bool dark = false; + bool dark_content = false; + bool allow_dark_content = false; Color color; Color normalized_color; int h16, s16, v16; @@ -63,74 +66,6 @@ struct ColorRampCtrl : public WheelRampCtrl { ColorRampCtrl() : WheelRampCtrl(true) {} }; -class ColorSelector : public Ctrl -{ -public: - ColorSelector(bool not_null = true); - - ColorSelector& NotNull(bool nn = true) { impl->NotNull(nn); return *this; } - ColorSelector& NoNotNull() { return NotNull(false); } - bool IsNotNull() const { return impl->IsNotNull(); } - - Color Get() const { return impl->Get(); } - void Set(Color c) { impl->Set(c); } - - Vector GetPalette() const { return impl->GetPalette(); } - void SetPalette(const Vector& palette) { impl->SetPalette(palette); } - - void SerializeConfig(Stream& stream) { impl->SerializeConfig(stream); } - - virtual void SetData(const Value& color) { Set(color); } - virtual Value GetData() const { return Get(); } - - Ctrl& GetImplCtrl() { return impl->GetCtrl(); } - -public: - Event<> WhenSetColor; - -public: - class Impl - { - public: - virtual ~Impl() {} - - virtual void Set(Color c) = 0; - virtual Color Get() const = 0; - virtual void NotNull(bool nn) = 0; - virtual bool IsNotNull() const = 0; - virtual Vector GetPalette() const = 0; - virtual void SetPalette(const Vector& pal) = 0; - virtual void SerializeConfig(Stream& stream) = 0; - virtual Ctrl& GetCtrl() = 0; - - protected: - Impl() {} - }; - -protected: - One impl; -}; - -class ColorCtrl : public DataPusher -{ -public: - typedef ColorCtrl CLASSNAME; - ColorCtrl(bool not_null = true); - virtual ~ColorCtrl(); - - ColorCtrl& NotNull(bool _nn = true) { empty.Show(!_nn); return *this; } - ColorCtrl& NoNotNull() { return NotNull(false); } - bool IsNotNull() const { return !empty.IsVisible(); } - -protected: - virtual void DoAction(); - void OnClear() { SetDataAction(Null); } - -protected: - FrameRight