Blob Blame Raw

#include "app.h"



int graphInit(Graph *g, App *app) {
  LOGDBG("graph: init");
  
  const uint16_t F = 65535; // full
  const uint16_t H = 32768; // half
  const uint16_t Q = 16384; // quart
  const uint16_t colorsRgb[GS_COUNT*2][3] = { // background, foreground
    { 0, 0, Q }, { 0, 0, Q },   // window
    { 0, 0, H }, { 0, 0, H },   // inactive button
    { 0, 0, F }, { 0, 0, F },   // active button
    { 0, 0, H }, { F, F, 0 },   // inactive text
    { 0, 0, F }, { F, F, 0 } }; // active text
  
  // create palette
  g->app = app;
  g->cm = xcb_generate_id(g->app->xcb);
  xcb_create_colormap(g->app->xcb, XCB_COLORMAP_ALLOC_NONE, g->cm, g->app->win, g->app->screen->root_visual);

  // encqueue colors allocarion requests
  xcb_alloc_color_cookie_t cookies[GS_COUNT*2];
  for(int i = 0; i < GS_COUNT*2; ++i)
    cookies[i] = xcb_alloc_color(g->app->xcb, g->cm, colorsRgb[i][0], colorsRgb[i][1], colorsRgb[i][2]);

  // take color allocation replies (take color indices)
  uint32_t colors[GS_COUNT*2];
  for(int i = 0; i < GS_COUNT*2; ++i) {
    xcb_alloc_color_reply_t *reply = xcb_alloc_color_reply(g->app->xcb, cookies[i], NULL);
    colors[i] = reply->pixel;
    free(reply);
  }
  
  // associate palette with the window
  xcb_change_window_attributes(g->app->xcb, g->app->win, XCB_CW_COLORMAP, &g->cm);
  
  // create contexts
  uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES;
  uint32_t values[3] = {0, 0, 0};
  for(int i = 0; i < GS_COUNT; ++i) {
    values[0] = colors[i*2 + 1]; // foreground
    values[1] = colors[i*2];     // background
    g->gc[i] = xcb_generate_id(g->app->xcb);
    xcb_create_gc(g->app->xcb, g->gc[i], g->app->win, mask, values);
  }
  
  return 1;
}


void graphDeinit(Graph *g) {
  LOGDBG("graph: deinit");
  for(int i = 0; i < GS_COUNT; ++i)
    xcb_free_gc(g->app->xcb, g->gc[i]);
  xcb_change_window_attributes(g->app->xcb, g->app->win, XCB_CW_COLORMAP, &g->app->screen->default_colormap);
  xcb_free_colormap(g->app->xcb, g->cm);
}


void graphResize(Graph *g)
  { }


void graphDrawBackgound(Graph *g, int x, int y, int w, int h) {
  xcb_rectangle_t r = {x, y, w, h};
  xcb_poly_fill_rectangle(g->app->xcb, g->app->win, g->gc[GS_WINDOW], 1, &r);
}


void graphDrawButton(Graph *g, int x, int y, int w, int h, int active) {
  GraphStyle s = active ? GS_BUTTON_ACTIVE : GS_BUTTON_INACTIVE;
  xcb_rectangle_t r = {x, y, w, h};
  xcb_poly_fill_rectangle(g->app->xcb, g->app->win, g->gc[s], 1, &r);
}



int textLayoutInit(TextLayout *tl, Graph *g, int cx, int cy, const char *text) {
  tl->g = g;
  tl->text = text;
  return 1;
}

void textLayoutDeinit(TextLayout *tl)
  { }

void textLayoutDraw(TextLayout *tl, int active)
  { }