1 /* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */
2 
3 #include "../ui_gc.h"
4 
5 #include <pobl/bl_mem.h> /* malloc */
6 
7 #include "../ui_color.h"
8 
9 #define ARGB_TO_RGB(pixel) ((pixel)&0x00ffffff)
10 
11 /* --- global functions --- */
12 
ui_gc_new(Display * display,Drawable drawable)13 ui_gc_t *ui_gc_new(Display *display, Drawable drawable) {
14   ui_gc_t *gc;
15 
16   if ((gc = calloc(1, sizeof(ui_gc_t))) == NULL) {
17     return NULL;
18   }
19 
20   gc->display = display;
21 
22   /* Default value of GC. */
23   gc->fg_color = RGB(0, 0, 0);
24   gc->bg_color = RGB(0xff, 0xff, 0xff);
25 
26   return gc;
27 }
28 
ui_gc_destroy(ui_gc_t * gc)29 void ui_gc_destroy(ui_gc_t *gc) {
30   free(gc);
31 }
32 
ui_set_gc(ui_gc_t * gc,GC _gc)33 void ui_set_gc(ui_gc_t *gc, GC _gc) {
34   gc->gc = _gc;
35 
36   SetTextAlign(gc->gc, TA_LEFT | TA_BASELINE);
37 
38   gc->fg_color = RGB(0, 0, 0); /* black */
39 #if 0
40   /* black is default value */
41   SetTextColor(gc->gc, gc->fg_color);
42 #endif
43 
44   gc->bg_color = RGB(0xff, 0xff, 0xff); /* white */
45 #if 0
46   /* white is default value */
47   SetBkColor(gc->gc, gc->bg_color);
48 #endif
49 
50   gc->fid = None;
51   gc->pen = None;
52   gc->brush = None;
53 }
54 
ui_gc_set_fg_color(ui_gc_t * gc,u_long fg_color)55 void ui_gc_set_fg_color(ui_gc_t *gc, u_long fg_color) {
56   if (ARGB_TO_RGB(fg_color) != gc->fg_color) {
57     SetTextColor(gc->gc, (gc->fg_color = ARGB_TO_RGB(fg_color)));
58   }
59 }
60 
ui_gc_set_bg_color(ui_gc_t * gc,u_long bg_color)61 void ui_gc_set_bg_color(ui_gc_t *gc, u_long bg_color) {
62   if (ARGB_TO_RGB(bg_color) != gc->bg_color) {
63     SetBkColor(gc->gc, (gc->bg_color = ARGB_TO_RGB(bg_color)));
64   }
65 }
66 
ui_gc_set_fid(ui_gc_t * gc,Font fid)67 void ui_gc_set_fid(ui_gc_t *gc, Font fid) {
68   if (gc->fid != fid) {
69     SelectObject(gc->gc, fid);
70     gc->fid = fid;
71   }
72 }
73 
ui_gc_set_pen(ui_gc_t * gc,HPEN pen)74 HPEN ui_gc_set_pen(ui_gc_t *gc, HPEN pen) {
75   if (gc->pen != pen) {
76     gc->pen = pen;
77 
78     return SelectObject(gc->gc, pen);
79   }
80 
81   return None;
82 }
83 
84 HBRUSH
ui_gc_set_brush(ui_gc_t * gc,HBRUSH brush)85 ui_gc_set_brush(ui_gc_t *gc, HBRUSH brush) {
86   if (gc->brush != brush) {
87     gc->brush = brush;
88 
89     return SelectObject(gc->gc, brush);
90   }
91 
92   return None;
93 }
94