Skip to content

Commit

Permalink
use functions instead of referencing gCurrentTheme->
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Nov 1, 2023
1 parent fed4cfb commit 8d9b6b1
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ static void DrawDocument(MainWindow* win, HDC hdc, RECT* rcArea) {
if (renderDelay != 0) {
HFONT fontRightTxt = CreateSimpleFont(hdc, "MS Shell Dlg", 14);
HGDIOBJ hPrevFont = SelectObject(hdc, fontRightTxt);
auto col = gCurrentTheme->window.textColor;
auto col = ThemeWindowTextColor();
SetTextColor(hdc, col);
if (renderDelay != RENDER_DELAY_FAILED) {
if (renderDelay < REPAINT_MESSAGE_DELAY_IN_MS) {
Expand Down
2 changes: 1 addition & 1 deletion src/CanvasAboutUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static void OnPaintAbout(MainWindow* win) {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(win->hwndCanvas, &ps);

auto txtCol = gCurrentTheme->window.textColor;
auto txtCol = ThemeWindowTextColor();
auto bgCol = GetMainWindowBackgroundColor();
if (HasPermission(Perm::SavePreferences | Perm::DiskAccess) && gGlobalPrefs->rememberOpenedFiles &&
gGlobalPrefs->showStartPage) {
Expand Down
2 changes: 1 addition & 1 deletion src/Caption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ static void DrawCaptionButton(DRAWITEMSTRUCT* item, MainWindow* win) {
}
// draw the three lines
// COLORREF c = win->caption->textColor;
COLORREF c = gCurrentTheme->window.textColor;
COLORREF c = ThemeWindowTextColor();
u8 r, g, b;
UnpackColor(c, r, g, b);
float width = floor((float)rc.dy / 8.0f);
Expand Down
24 changes: 12 additions & 12 deletions src/HomePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ static void DrawSumatraVersion(HDC hdc, Rect rect) {
pt.x += txtSize.dx;
}

SetTextColor(hdc, gCurrentTheme->window.textColor);
SetTextColor(hdc, ThemeWindowTextColor());
int x = mainRect.x + mainRect.dx + DpiScale(hdc, kInnerPadding);
int y = mainRect.y;

Expand All @@ -163,7 +163,7 @@ static Rect DrawHideFrequentlyReadLink(HWND hwnd, HDC hdc, const char* txt) {
w.withUnderline = true;
Size txtSize = w.Measure(true);

auto col = gCurrentTheme->window.linkColor;
auto col = ThemeWindowLinkColor();
ScopedSelectObject pen(hdc, CreatePen(PS_SOLID, 1, col), true);

SetTextColor(hdc, col);
Expand Down Expand Up @@ -212,10 +212,10 @@ static TempStr TrimGitTemp(char* s) {
It transcribes the design I did in graphics software - hopeless
to understand without seeing the design. */
static void DrawAbout(HWND hwnd, HDC hdc, Rect rect, Vec<StaticLinkInfo*>& staticLinks) {
auto col = gCurrentTheme->window.textColor;
auto col = ThemeWindowTextColor();
AutoDeletePen penBorder(CreatePen(PS_SOLID, ABOUT_LINE_OUTER_SIZE, col));
AutoDeletePen penDivideLine(CreatePen(PS_SOLID, ABOUT_LINE_SEP_SIZE, col));
col = gCurrentTheme->window.linkColor;
col = ThemeWindowLinkColor();
AutoDeletePen penLinkLine(CreatePen(PS_SOLID, ABOUT_LINE_SEP_SIZE, col));

HFONT fontLeftTxt = CreateSimpleFont(hdc, kLeftTextFont, kLeftTextFontSize);
Expand Down Expand Up @@ -248,7 +248,7 @@ static void DrawAbout(HWND hwnd, HDC hdc, Rect rect, Vec<StaticLinkInfo*>& stati
DrawSumatraVersion(hdc, titleRect);

/* render attribution box */
col = gCurrentTheme->window.textColor;
col = ThemeWindowTextColor();
SetTextColor(hdc, col);
SetBkMode(hdc, TRANSPARENT);

Expand All @@ -271,9 +271,9 @@ static void DrawAbout(HWND hwnd, HDC hdc, Rect rect, Vec<StaticLinkInfo*>& stati
for (AboutLayoutInfoEl* el = gAboutLayoutInfo; el->leftTxt; el++) {
bool hasUrl = HasPermission(Perm::DiskAccess) && el->url;
if (hasUrl) {
col = gCurrentTheme->window.linkColor;
col = ThemeWindowLinkColor();
} else {
col = gCurrentTheme->window.textColor;
col = ThemeWindowTextColor();
}
SetTextColor(hdc, col);
char* s = (char*)el->rightTxt;
Expand Down Expand Up @@ -642,7 +642,7 @@ void DrawHomePage(HDC hdc, const HomePageLayout& l) {
const Rect& r = l.bAppWithVer;
DrawSumatraVersion(hdc, r);

auto color = gCurrentTheme->window.textColor;
auto color = ThemeWindowTextColor();
ScopedSelectObject pen(hdc, CreatePen(PS_SOLID, 1, color), true);
DrawLine(hdc, l.bLine);
}
Expand All @@ -658,10 +658,10 @@ void DrawHomePage(MainWindow* win, HDC hdc, const FileHistory& fileHistory, COLO
LayoutHomePage(hdc, rc, layout);

HWND hwnd = win->hwndFrame;
auto color = gCurrentTheme->window.textColor;
auto color = ThemeWindowTextColor();

AutoDeletePen penThumbBorder(CreatePen(PS_SOLID, kThumbsBorderDx, color));
color = gCurrentTheme->window.linkColor;
color = ThemeWindowLinkColor();
AutoDeletePen penLinkLine(CreatePen(PS_SOLID, 1, color));

HFONT fontText = CreateSimpleFont(hdc, "MS Shell Dlg", 14);
Expand All @@ -678,7 +678,7 @@ void DrawHomePage(MainWindow* win, HDC hdc, const FileHistory& fileHistory, COLO
/* render recent files list */
SelectObject(hdc, penThumbBorder);
SetBkMode(hdc, TRANSPARENT);
color = gCurrentTheme->window.textColor;
color = ThemeWindowTextColor();
SetTextColor(hdc, color);

Rect& titleBox = layout.bAppWithVer;
Expand Down Expand Up @@ -785,7 +785,7 @@ void DrawHomePage(MainWindow* win, HDC hdc, const FileHistory& fileHistory, COLO
kThumbsMarginTop + thumbsRows * kThumbnailDy + (thumbsRows - 1) * kThumbsSpaceBetweenY + kThumbsMarginBottom;
rc.dy = kThumbsBottomBoxDy;

color = gCurrentTheme->window.linkColor;
color = ThemeWindowLinkColor();
SetTextColor(hdc, color);
SelectObject(hdc, penLinkLine);

Expand Down
2 changes: 1 addition & 1 deletion src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ void LinkHandler::GotoNamedDest(const char* name) {

void UpdateControlsColors(MainWindow* win) {
COLORREF bgCol = GetControlBackgroundColor();
COLORREF txtCol = gCurrentTheme->window.textColor;
COLORREF txtCol = ThemeWindowTextColor();

// logfa("retrieved doc colors in tree control: 0x%x 0x%x\n", treeTxtCol, treeBgCol);

Expand Down
4 changes: 2 additions & 2 deletions src/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1918,7 +1918,7 @@ void FreeMenuOwnerDrawInfoData(HMENU hmenu) {
};
}
void MarkMenuOwnerDraw(HMENU hmenu) {
if (!gCurrentTheme->colorizeControls) {
if (!ThemeColorizeControls()) {
return;
}
WCHAR buf[1024];
Expand Down Expand Up @@ -2052,7 +2052,7 @@ void MenuCustomDrawItem(HWND hwnd, DRAWITEMSTRUCT* dis) {
ScopedSelectFont restoreFont(hdc, font);

COLORREF bgCol = GetMainWindowBackgroundColor();
COLORREF txtCol = gCurrentTheme->window.textColor;
COLORREF txtCol = ThemeWindowTextColor();
// TODO: if isDisabled, pick a color that represents disabled
// either add it to theme definition or auto-generate
// (lighter if dark color, darker if light color)
Expand Down
10 changes: 5 additions & 5 deletions src/Notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,13 @@ void NotificationWnd::OnPaint(HDC hdcIn, PAINTSTRUCT* ps) {

ScopedSelectObject fontPrev(hdc, font);

COLORREF colBg = gCurrentTheme->notifications.backgroundColor;
COLORREF colBg = ThemeNotificationsBackgroundColor();
COLORREF colBorder = MkGray(0xdd);
COLORREF colTxt = gCurrentTheme->notifications.textColor;
COLORREF colTxt = ThemeNotificationsTextColor();
if (highlight) {
colBg = gCurrentTheme->notifications.highlightColor;
colBg = ThemeNotificationsHighlightColor();
colBorder = colBg;
colTxt = gCurrentTheme->notifications.highlightTextColor;
colTxt = ThemeNotificationsHighlightTextColor();
}
// COLORREF colBg = MkRgb(0xff, 0xff, 0x5c);
// COLORREF colBg = MkGray(0xff);
Expand Down Expand Up @@ -374,7 +374,7 @@ void NotificationWnd::OnPaint(HDC hdcIn, PAINTSTRUCT* ps) {
rc = rProgress;
int progressWidth = rc.dx;

COLORREF col = gCurrentTheme->notifications.progressColor;
COLORREF col = ThemeNotificationsProgressColor();
Pen pen(GdiRgbFromCOLORREF(col));
grc = {rc.x, rc.y, rc.dx, rc.dy};
graphics.DrawRectangle(&pen, grc);
Expand Down
4 changes: 2 additions & 2 deletions src/SumatraPDF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5497,14 +5497,14 @@ LRESULT CALLBACK WndProcSumatraFrame(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
return FrameOnCommand(win, hwnd, msg, wp, lp);

case WM_MEASUREITEM:
if (gCurrentTheme->colorizeControls) {
if (ThemeColorizeControls()) {
MenuCustomDrawMesureItem(hwnd, (MEASUREITEMSTRUCT*)lp);
return TRUE;
}
break;

case WM_DRAWITEM:
if (gCurrentTheme->colorizeControls) {
if (ThemeColorizeControls()) {
MenuCustomDrawItem(hwnd, (DRAWITEMSTRUCT*)lp);
return TRUE;
}
Expand Down
2 changes: 1 addition & 1 deletion src/SumatraProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ static void DrawProperties(HWND hwnd, HDC hdc) {
auto col = GetMainWindowBackgroundColor();
FillRect(hdc, rcClient, col);

col = gCurrentTheme->window.textColor;
col = ThemeWindowTextColor();
SetTextColor(hdc, col);

/* render text on the left*/
Expand Down
79 changes: 78 additions & 1 deletion src/Theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,43 @@ constexpr COLORREF kColWhite = 0xFFFFFF;
// #define kColWhiteish 0xEBEBF9
// #define kColDarkGray 0x424242

constexpr const int kThemeCount = 3;

struct MainWindowStyle {
// Background color of recently added, about, and properties menus
COLORREF backgroundColor;
// Background color of controls, menus, non-client areas, etc.
COLORREF controlBackgroundColor;
// Text color of recently added, about, and properties menus
COLORREF textColor;
// Link color on recently added, about, and properties menus
COLORREF linkColor;
};

struct NotificationStyle {
// Background color of the notification window
COLORREF backgroundColor;
// Text color of the notification window
COLORREF textColor;
// Color of the highlight box that surrounds the text when a notification is highlighted
COLORREF highlightColor;
// Color of the text when a notification is highlighted
COLORREF highlightTextColor;
// Background color of the progress bar in the notification window
COLORREF progressColor;
};

struct Theme {
// Name of the theme
const char* name;
// Style of the main window
MainWindowStyle window;
// Style of notifications
NotificationStyle notifications;
// Whether or not we colorize standard Windows controls and window areas
bool colorizeControls;
};

// clang-format off
static Theme gThemeLight = {
// Theme Name
Expand Down Expand Up @@ -234,7 +271,7 @@ void GetDocumentColors(COLORREF& text, COLORREF& bg) {
// if we're inverting in non-default themes, the colors
// should match the colors of the window
// TODO: this probably only makes sense for dark themes
text = gCurrentTheme->window.textColor;
text = ThemeWindowTextColor();
bg = gCurrentTheme->window.backgroundColor;
if (IsLightColor(bg)) {
bg = AdjustLightness2(bg, -8);
Expand All @@ -261,3 +298,43 @@ COLORREF GetMainWindowBackgroundColor() {
}
return bgColor;
}

COLORREF ThemeWindowBackgroundColor() {
return gCurrentTheme->window.backgroundColor;
}

COLORREF ThemeWindowTextColor() {
return gCurrentTheme->window.textColor;
}

COLORREF ThemeWindowControlBackgroundColor() {
return gCurrentTheme->window.controlBackgroundColor;
}

COLORREF ThemeWindowLinkColor() {
return gCurrentTheme->window.linkColor;
}

COLORREF ThemeNotificationsBackgroundColor() {
return gCurrentTheme->notifications.backgroundColor;
}

COLORREF ThemeNotificationsTextColor() {
return gCurrentTheme->notifications.textColor;
}

COLORREF ThemeNotificationsHighlightColor() {
return gCurrentTheme->notifications.highlightColor;
}

COLORREF ThemeNotificationsHighlightTextColor() {
return gCurrentTheme->notifications.highlightTextColor;
}

COLORREF ThemeNotificationsProgressColor() {
return gCurrentTheme->notifications.progressColor;
}

bool ThemeColorizeControls() {
return gCurrentTheme->colorizeControls;
}
50 changes: 11 additions & 39 deletions src/Theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,6 @@ License: GPLv3 */

#include "utils/BaseUtil.h"

// The number of themes
constexpr const int kThemeCount = 3;

struct MainWindowStyle {
// Background color of recently added, about, and properties menus
COLORREF backgroundColor;
// Background color of controls, menus, non-client areas, etc.
COLORREF controlBackgroundColor;
// Text color of recently added, about, and properties menus
COLORREF textColor;
// Link color on recently added, about, and properties menus
COLORREF linkColor;
};

struct NotificationStyle {
// Background color of the notification window
COLORREF backgroundColor;
// Text color of the notification window
COLORREF textColor;
// Color of the highlight box that surrounds the text when a notification is highlighted
COLORREF highlightColor;
// Color of the text when a notification is highlighted
COLORREF highlightTextColor;
// Background color of the progress bar in the notification window
COLORREF progressColor;
};

struct Theme {
// Name of the theme
const char* name;
// Style of the main window
MainWindowStyle window;
// Style of notifications
NotificationStyle notifications;
// Whether or not we colorize standard Windows controls and window areas
bool colorizeControls;
};

extern Theme* gCurrentTheme;
void SelectNextTheme();
void SetThemeByIndex(int);

Expand All @@ -55,3 +16,14 @@ int GetCurrentThemeIndex();
void GetDocumentColors(COLORREF& text, COLORREF& bg);
COLORREF GetMainWindowBackgroundColor();
COLORREF GetControlBackgroundColor();

COLORREF ThemeWindowBackgroundColor();
COLORREF ThemeWindowTextColor();
COLORREF ThemeWindowControlBackgroundColor();
COLORREF ThemeWindowLinkColor();
COLORREF ThemeNotificationsBackgroundColor();
COLORREF ThemeNotificationsTextColor();
COLORREF ThemeNotificationsHighlightColor();
COLORREF ThemeNotificationsHighlightTextColor();
COLORREF ThemeNotificationsProgressColor();
bool ThemeColorizeControls();
6 changes: 3 additions & 3 deletions src/Toolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,11 @@ void UpdateFindbox(MainWindow* win) {

LRESULT CALLBACK BgSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass,
DWORD_PTR dwRefData) {
if (WM_ERASEBKGND == uMsg && gCurrentTheme->colorizeControls) {
if (WM_ERASEBKGND == uMsg && ThemeColorizeControls()) {
HDC hdc = (HDC)wParam;
RECT rect;
GetClientRect(hWnd, &rect);
SetTextColor(hdc, gCurrentTheme->window.textColor);
SetTextColor(hdc, ThemeWindowTextColor());
SetBkColor(hdc, GetControlBackgroundColor());
auto bg = CreateSolidBrush(GetControlBackgroundColor());
FillRect(hdc, &rect, bg);
Expand Down Expand Up @@ -328,7 +328,7 @@ static LRESULT CALLBACK WndProcToolbar(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp
if ((win->hwndFindBg != hStatic && win->hwndPageBg != hStatic) || theme::IsAppThemed()) {
// Set color used in "Page:" and "Find:" labels
auto col = RGB(0x00, 0x00, 0x00);
SetTextColor(hdc, gCurrentTheme->window.textColor);
SetTextColor(hdc, ThemeWindowTextColor());
SetBkMode(hdc, TRANSPARENT);
return (LRESULT)win->brControlBgColor;
}
Expand Down
3 changes: 1 addition & 2 deletions src/wingui/WinGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3445,7 +3445,6 @@ void TabsCtrl::Paint(HDC hdc, RECT& rc) {
gfx.SetTextRenderingHint(TextRenderingHintClearTypeGridFit);
gfx.SetPageUnit(UnitPixel);

Theme* theme = gCurrentTheme;
SolidBrush br(GdipCol(GetControlBackgroundColor()));

Font f(hdc, GetDefaultGuiFont());
Expand All @@ -3463,7 +3462,7 @@ void TabsCtrl::Paint(HDC hdc, RECT& rc) {
Rect r;
Gdiplus::RectF rTxt;

COLORREF textColor = theme->window.textColor;
COLORREF textColor = ThemeWindowTextColor();
COLORREF tabBgSelected = GetControlBackgroundColor();
COLORREF tabBgHighlight;
COLORREF tabBgBackground;
Expand Down

0 comments on commit 8d9b6b1

Please sign in to comment.