win32: Add fallback code to draw theme parts

This commit is contained in:
Benjamin Otte 2016-02-17 04:18:29 +01:00
parent 37e3ccb2f6
commit c98007f9fd
3 changed files with 156 additions and 11 deletions

View File

@ -19,6 +19,145 @@
#include "gtkwin32drawprivate.h"
static void
gtk_cairo_set_source_sys_color (cairo_t *cr,
gint id)
{
GdkRGBA rgba;
gtk_win32_get_sys_color (id, &rgba);
gdk_cairo_set_source_rgba (cr, &rgba);
}
static void
draw_button (cairo_t *cr,
int part,
int state,
int width,
int height)
{
gboolean is_down = (state == 3);
int top_color = is_down ? GTK_WIN32_SYS_COLOR_BTNSHADOW : GTK_WIN32_SYS_COLOR_BTNHIGHLIGHT;
int bot_color = is_down ? GTK_WIN32_SYS_COLOR_BTNHIGHLIGHT : GTK_WIN32_SYS_COLOR_BTNSHADOW;
gtk_cairo_set_source_sys_color (cr, top_color);
cairo_rectangle (cr, 0, 0, width - 1, 1);
cairo_rectangle (cr, 0, 1, 1, height - 1);
cairo_fill (cr);
gtk_cairo_set_source_sys_color (cr, bot_color);
cairo_rectangle (cr, width - 1, 0, 1, height -1);
cairo_rectangle (cr, 0, height - 1, width, 1);
cairo_fill (cr);
gtk_cairo_set_source_sys_color (cr, GTK_WIN32_SYS_COLOR_BTNFACE);
cairo_rectangle (cr, 1, 1, width - 2, height - 2);
cairo_fill (cr);
}
static void
draw_check (cairo_t *cr,
int part,
int state,
int width,
int height)
{
gtk_cairo_set_source_sys_color (cr, GTK_WIN32_SYS_COLOR_BTNHIGHLIGHT);
cairo_set_line_width (cr, 1.0);
cairo_rectangle (cr, 0.5, 0.5, width - 1.0, height - 1.0);
cairo_stroke (cr);
}
static void
draw_radio (cairo_t *cr,
int part,
int state,
int width,
int height)
{
gtk_cairo_set_source_sys_color (cr, GTK_WIN32_SYS_COLOR_BTNHIGHLIGHT);
cairo_set_line_width (cr, 1.0);
cairo_arc (cr, width / 2.0, height / 2.0, MIN (width, height) / 2.0 - 0.5, 0, G_PI * 2);
cairo_stroke (cr);
}
typedef struct _GtkWin32ThemePart GtkWin32ThemePart;
struct _GtkWin32ThemePart {
const char *class_name;
gint part;
gint size;
void (* draw_func) (cairo_t *cr,
int part,
int state,
int width,
int height);
};
static GtkWin32ThemePart theme_parts[] = {
{ "button", 1, 0, draw_button },
{ "button", 2, 13, draw_radio },
{ "button", 3, 13, draw_check }
};
static const GtkWin32ThemePart *
get_theme_part (const char *class_name,
gint part)
{
gsize i;
for (i = 0; i < G_N_ELEMENTS (theme_parts); i++)
{
if (g_str_equal (theme_parts[i].class_name, class_name) &&
theme_parts[i].part == part)
return &theme_parts[i];
}
return NULL;
}
void
gtk_win32_draw_theme_background (cairo_t *cr,
const char *class_name,
int part,
int state,
int width,
int height)
{
const GtkWin32ThemePart *theme_part;
theme_part = get_theme_part (class_name, part);
if (theme_part)
theme_part->draw_func (cr, part, state, width, height);
}
void
gtk_win32_get_theme_part_size (const char *class_name,
int part,
int state,
int *width,
int *height)
{
const GtkWin32ThemePart *theme_part;
theme_part = get_theme_part (class_name, part);
if (theme_part)
{
if (width)
*width = theme_part->size;
if (height)
*height = theme_part->size;
}
else
{
if (width)
*width = 1;
if (height)
*height = 1;
}
}
struct {
const char *name;
GdkRGBA rgba;

View File

@ -19,6 +19,7 @@
#define __GTK_WIN32_DRAW_H__
#include <gdk/gdk.h>
#include <cairo.h>
G_BEGIN_DECLS
@ -56,8 +57,20 @@ enum {
GTK_WIN32_SYS_COLOR_MENUBAR
};
void gtk_win32_get_sys_color (gint id,
GdkRGBA *color);
void gtk_win32_draw_theme_background (cairo_t *cr,
const char *class_name,
int part,
int state,
int width,
int height);
void gtk_win32_get_theme_part_size (const char *class_name,
int part,
int state,
int *width,
int *height);
void gtk_win32_get_sys_color (gint id,
GdkRGBA *color);
G_END_DECLS

View File

@ -310,7 +310,6 @@ gtk_win32_theme_create_surface (GtkWin32Theme *theme,
int *y_offs_out)
{
cairo_surface_t *surface;
GdkRGBA color;
cairo_t *cr;
int x_offs;
int y_offs;
@ -378,10 +377,7 @@ gtk_win32_theme_create_surface (GtkWin32Theme *theme,
cr = cairo_create (surface);
/* XXX: Do something better here (like printing the theme parts) */
gdk_rgba_parse (&color, "pink");
gdk_cairo_set_source_rgba (cr, &color);
cairo_paint (cr);
gtk_win32_draw_theme_background (cr, theme->class_name, xp_part, state, width, height);
cairo_destroy (cr);
@ -432,10 +428,7 @@ gtk_win32_theme_get_part_size (GtkWin32Theme *theme,
}
#endif
if (width)
*width = 1;
if (height)
*height = 1;
gtk_win32_get_theme_part_size (theme->class_name, part, state, width, height);
}
int