This commit is contained in:
Mike Sinkovsky 2021-01-02 10:21:53 -05:00 committed by GitHub
commit 7b0691176b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 8 deletions

4
ui.h
View File

@ -305,6 +305,8 @@ typedef struct uiAreaKeyEvent uiAreaKeyEvent;
typedef struct uiDrawContext uiDrawContext; typedef struct uiDrawContext uiDrawContext;
// TO CONSIDER: the uiAreaHandler param there seems useless
// (might use individual callbacks instead of handler struct?)
struct uiAreaHandler { struct uiAreaHandler {
void (*Draw)(uiAreaHandler *, uiArea *, uiAreaDrawParams *); void (*Draw)(uiAreaHandler *, uiArea *, uiAreaDrawParams *);
// TODO document that resizes cause a full redraw for non-scrolling areas; implementation-defined for scrolling areas // TODO document that resizes cause a full redraw for non-scrolling areas; implementation-defined for scrolling areas
@ -314,6 +316,7 @@ struct uiAreaHandler {
void (*MouseCrossed)(uiAreaHandler *, uiArea *, int left); void (*MouseCrossed)(uiAreaHandler *, uiArea *, int left);
void (*DragBroken)(uiAreaHandler *, uiArea *); void (*DragBroken)(uiAreaHandler *, uiArea *);
int (*KeyEvent)(uiAreaHandler *, uiArea *, uiAreaKeyEvent *); int (*KeyEvent)(uiAreaHandler *, uiArea *, uiAreaKeyEvent *);
void (*Resize)(uiAreaHandler *, uiArea *, int, int);
}; };
// TODO RTL layouts? // TODO RTL layouts?
@ -346,6 +349,7 @@ _UI_EXTERN void uiAreaScrollTo(uiArea *a, double x, double y, double width, doub
// TODO release capture? // TODO release capture?
_UI_EXTERN void uiAreaBeginUserWindowMove(uiArea *a); _UI_EXTERN void uiAreaBeginUserWindowMove(uiArea *a);
_UI_EXTERN void uiAreaBeginUserWindowResize(uiArea *a, uiWindowResizeEdge edge); _UI_EXTERN void uiAreaBeginUserWindowResize(uiArea *a, uiWindowResizeEdge edge);
_UI_EXTERN void uiAreaSetBackgroundColor(uiArea *a, int r, int g, int b);
_UI_EXTERN uiArea *uiNewArea(uiAreaHandler *ah); _UI_EXTERN uiArea *uiNewArea(uiAreaHandler *ah);
_UI_EXTERN uiArea *uiNewScrollingArea(uiAreaHandler *ah, int width, int height); _UI_EXTERN uiArea *uiNewScrollingArea(uiAreaHandler *ah, int width, int height);

View File

@ -37,6 +37,7 @@ struct uiArea {
GtkWidget *areaWidget; GtkWidget *areaWidget;
GtkDrawingArea *drawingArea; GtkDrawingArea *drawingArea;
areaWidget *area; areaWidget *area;
int bgR, bgG, bgB;
uiAreaHandler *ah; uiAreaHandler *ah;
@ -96,6 +97,8 @@ static void areaWidget_size_allocate(GtkWidget *w, GtkAllocation *allocation)
// TODO drop this rule; it was stupid and documenting this was stupid — let platforms where it matters do it on their own // TODO drop this rule; it was stupid and documenting this was stupid — let platforms where it matters do it on their own
// TODO or do we not, for parity of performance? // TODO or do we not, for parity of performance?
gtk_widget_queue_resize(w); gtk_widget_queue_resize(w);
a->ah->Resize(a->ah, a, allocation->width, allocation->height);
} }
static void loadAreaSize(uiArea *a, double *width, double *height) static void loadAreaSize(uiArea *a, double *width, double *height)
@ -133,6 +136,11 @@ static gboolean areaWidget_draw(GtkWidget *w, cairo_t *cr)
dp.ClipWidth = clipX1 - clipX0; dp.ClipWidth = clipX1 - clipX0;
dp.ClipHeight = clipY1 - clipY0; dp.ClipHeight = clipY1 - clipY0;
if (a->bgR != -1) {
cairo_set_source_rgb(cr, a->bgR/255.0, a->bgG/255.0, a->bgB/255.0);
cairo_paint(cr);
}
// no need to save or restore the graphics state to reset transformations; GTK+ does that for us // no need to save or restore the graphics state to reset transformations; GTK+ does that for us
(*(a->ah->Draw))(a->ah, a, &dp); (*(a->ah->Draw))(a->ah, a, &dp);
@ -155,6 +163,8 @@ static void areaWidget_get_preferred_height(GtkWidget *w, gint *min, gint *nat)
*min = a->scrollHeight; *min = a->scrollHeight;
*nat = a->scrollHeight; *nat = a->scrollHeight;
} }
// TODO: min size
} }
static void areaWidget_get_preferred_width(GtkWidget *w, gint *min, gint *nat) static void areaWidget_get_preferred_width(GtkWidget *w, gint *min, gint *nat)
@ -496,6 +506,13 @@ static void areaWidget_class_init(areaWidgetClass *class)
uiUnixControlAllDefaults(uiArea) uiUnixControlAllDefaults(uiArea)
void uiAreaSetBackgroundColor(uiArea *a, int r, int g, int b)
{
a->bgR = r;
a->bgG = g;
a->bgB = b;
}
void uiAreaSetSize(uiArea *a, int width, int height) void uiAreaSetSize(uiArea *a, int width, int height)
{ {
if (!a->scrolling) if (!a->scrolling)
@ -603,6 +620,8 @@ uiArea *uiNewArea(uiAreaHandler *ah)
a->widget = a->areaWidget; a->widget = a->areaWidget;
uiAreaSetBackgroundColor(a, -1, -1, -1);
return a; return a;
} }
@ -629,6 +648,8 @@ uiArea *uiNewScrollingArea(uiAreaHandler *ah, int width, int height)
a->widget = a->swidget; a->widget = a->swidget;
uiAreaSetBackgroundColor(a, -1, -1, -1);
gtk_container_add(a->scontainer, a->areaWidget); gtk_container_add(a->scontainer, a->areaWidget);
// and make the area visible; only the scrolled window's visibility is controlled by libui // and make the area visible; only the scrolled window's visibility is controlled by libui
gtk_widget_show(a->areaWidget); gtk_widget_show(a->areaWidget);

View File

@ -11,6 +11,7 @@ static LRESULT CALLBACK areaWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
RECT client; RECT client;
WINDOWPOS *wp = (WINDOWPOS *) lParam; WINDOWPOS *wp = (WINDOWPOS *) lParam;
LRESULT lResult; LRESULT lResult;
double w, h;
a = (uiArea *) GetWindowLongPtrW(hwnd, GWLP_USERDATA); a = (uiArea *) GetWindowLongPtrW(hwnd, GWLP_USERDATA);
if (a == NULL) { if (a == NULL) {
@ -37,6 +38,8 @@ static LRESULT CALLBACK areaWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
uiWindowsEnsureGetClientRect(a->hwnd, &client); uiWindowsEnsureGetClientRect(a->hwnd, &client);
areaDrawOnResize(a, &client); areaDrawOnResize(a, &client);
areaScrollOnResize(a, &client); areaScrollOnResize(a, &client);
loadAreaSize(a, a->rt, &w, &h);
a->ah->Resize(a->ah, a, (int)w, (int)h);
return 0; return 0;
} }
@ -160,6 +163,13 @@ void uiAreaBeginUserWindowResize(uiArea *a, uiWindowResizeEdge edge)
wParam, 0); wParam, 0);
} }
void uiAreaSetBackgroundColor(uiArea *a, int r, int g, int b)
{
a->bgR = r;
a->bgG = g;
a->bgB = b;
}
uiArea *uiNewArea(uiAreaHandler *ah) uiArea *uiNewArea(uiAreaHandler *ah)
{ {
uiArea *a; uiArea *a;
@ -177,6 +187,8 @@ uiArea *uiNewArea(uiAreaHandler *ah)
hInstance, a, hInstance, a,
FALSE); FALSE);
uiAreaSetBackgroundColor(a, -1, -1, -1);
return a; return a;
} }
@ -199,6 +211,8 @@ uiArea *uiNewScrollingArea(uiAreaHandler *ah, int width, int height)
hInstance, a, hInstance, a,
FALSE); FALSE);
uiAreaSetBackgroundColor(a, -1, -1, -1);
// set initial scrolling parameters // set initial scrolling parameters
areaUpdateScroll(a); areaUpdateScroll(a);

View File

@ -24,6 +24,8 @@ struct uiArea {
BOOL inside; BOOL inside;
BOOL tracking; BOOL tracking;
int bgR, bgG, bgB;
ID2D1HwndRenderTarget *rt; ID2D1HwndRenderTarget *rt;
}; };

View File

@ -40,14 +40,21 @@ static HRESULT doPaint(uiArea *a, ID2D1RenderTarget *rt, RECT *clip)
// TODO only clear the clip area // TODO only clear the clip area
// TODO clear with actual background brush // TODO clear with actual background brush
bgcolorref = GetSysColor(COLOR_BTNFACE); if (a->bgR != -1) {
bgcolor.r = ((float) GetRValue(bgcolorref)) / 255.0; bgcolor.r = ((float) a->bgR) / 255.0;
// due to utter apathy on Microsoft's part, GetGValue() does not work with MSVC's Run-Time Error Checks bgcolor.g = ((float) a->bgG) / 255.0;
// it has not worked since 2008 and they have *never* fixed it bgcolor.b = ((float) a->bgB) / 255.0;
// TODO now that -RTCc has just been deprecated entirely, should we switch back? bgcolor.a = 1.0;
bgcolor.g = ((float) ((BYTE) ((bgcolorref & 0xFF00) >> 8))) / 255.0; } else {
bgcolor.b = ((float) GetBValue(bgcolorref)) / 255.0; bgcolorref = GetSysColor(COLOR_BTNFACE);
bgcolor.a = 1.0; bgcolor.r = ((float) GetRValue(bgcolorref)) / 255.0;
// due to utter apathy on Microsoft's part, GetGValue() does not work with MSVC's Run-Time Error Checks
// it has not worked since 2008 and they have *never* fixed it
// TODO now that -RTCc has just been deprecated entirely, should we switch back?
bgcolor.g = ((float) ((BYTE) ((bgcolorref & 0xFF00) >> 8))) / 255.0;
bgcolor.b = ((float) GetBValue(bgcolorref)) / 255.0;
bgcolor.a = 1.0;
}
rt->Clear(&bgcolor); rt->Clear(&bgcolor);
(*(ah->Draw))(ah, a, &dp); (*(ah->Draw))(ah, a, &dp);