Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ft2build.h>
  4. #include FT_FREETYPE_H
  5. #include FT_GLYPH_H
  6. #include "winlib.h"
  7. #include "draw2.h"
  8.  
  9. int init_fontlib();
  10. font_t *create_font(FT_Face face, int size);
  11. int get_font_height(font_t *font);
  12. int get_font_width(font_t *font);
  13.  
  14. int draw_text_ext(ctx_t *ctx, font_t *font, char *text, int len, rect_t *rc, color_t color);
  15.  
  16. typedef struct
  17. {
  18.     ctx_t    *ctx;
  19.     font_t   *font;
  20.  
  21.     char    *text;
  22.     char   **line;
  23.     int      lines;
  24.  
  25.     int      startline;
  26.     int      endline;
  27.     int      pagesize;
  28.     int      tail;
  29.  
  30.     rect_t   margins;
  31.     int      leading;
  32.     int      line_height;
  33.     int      w;
  34.     int      h;
  35.  
  36. }tview_t;
  37.  
  38.  
  39.  
  40. tview_t *create_tview(ctx_t *ctx, int width, int height)
  41. {
  42.     tview_t *txv;
  43.  
  44.     txv = malloc(sizeof(*txv));
  45.     if(txv == NULL)
  46.     {
  47.         return NULL;
  48.     };
  49.  
  50.     memset(txv, 0, sizeof(*txv));
  51.  
  52.     txv->ctx = ctx;
  53.     txv->w = width;
  54.     txv->h = height;
  55.     txv->font = create_font(NULL, 18);
  56.     txv->leading = 4;
  57.     txv->line_height = txv->leading + get_font_height(txv->font);
  58.     return txv;
  59. };
  60.  
  61. void txv_get_margins(const tview_t *txv, rect_t *margins)
  62. {
  63.     *margins = txv->margins;
  64. };
  65.  
  66. void txv_set_margins(tview_t *txv, const rect_t *margins)
  67. {
  68.     txv->margins = *margins;
  69. };
  70.  
  71. static void txv_recalc(tview_t *txv)
  72. {
  73.     int full_height;
  74.     int endline;
  75.  
  76.     full_height = txv->h - (txv->margins.t + txv->margins.b);
  77.     txv->pagesize = full_height / txv->line_height;
  78.     txv->tail = full_height % txv->line_height;
  79.     endline = txv->startline + txv->pagesize;
  80.     txv->endline = endline  < txv->lines ? endline : txv->lines;
  81.  
  82. //    printf("pagesize %d visible %d endline %d\n",txv->pagesize, txv->pagesize+!!txv->tail, txv->endline );
  83. };
  84.  
  85. static void txv_redraw(tview_t *txv)
  86. {
  87.     rect_t rc;
  88.     int i;
  89.  
  90.     draw_rect(txv->ctx, 0, 0, txv->w, txv->h, 0xFFFFFFFF);
  91.  
  92.     rc.l = txv->margins.l;
  93.     rc.t = txv->margins.t;
  94.     rc.r = txv->w - txv->margins.r;
  95.     rc.b = rc.t + txv->line_height;
  96.  
  97.     for(i = txv->startline; i < txv->endline; i++)
  98.     {
  99.         draw_text_ext(txv->ctx, txv->font, txv->line[i], txv->line[i+1]-txv->line[i], &rc, 0xFF000000);
  100.         rc.t+= txv->line_height;
  101.         rc.b+= txv->line_height;
  102.     }
  103.     if(txv->tail && ( i < txv->lines))
  104.         draw_text_ext(txv->ctx, txv->font, txv->line[i], txv->line[i+1]-txv->line[i], &rc, 0xFF000000);
  105. };
  106.  
  107. void txv_set_text(tview_t *txv, char *text, int size)
  108. {
  109.     int  i = 0;
  110.     char *p, *t;
  111.  
  112.     p = text;
  113.  
  114.     while(i < size)
  115.     {
  116.         switch(*p++)
  117.         {
  118.             case '\n':
  119.                 txv->lines++;
  120.                 break;
  121.             case '\r':
  122.                 break;
  123.         }
  124.         i++;
  125.     };
  126.  
  127.     txv->line = user_alloc((txv->lines+1)*sizeof(char*));
  128.     txv->text = text;
  129.     {
  130.         int  l=0;
  131.  
  132.         i = 0;
  133.         txv->line[0] = txv->text;
  134.  
  135.         while(i < size)
  136.         {
  137.             switch(*text++)
  138.             {
  139.                 case '\n':
  140.                     l++;
  141.                     txv->line[l] = text;
  142.                     break;
  143.                 case '\r':
  144.                     txv->line[l] = text;
  145.                     break;
  146.             }
  147.             i++;
  148.         };
  149.     }
  150.  
  151.     txv_recalc(txv);
  152.     txv_redraw(txv);
  153. };
  154.  
  155. int txv_scroll_down(tview_t *txv)
  156. {
  157.     int dst, src, rows;
  158.     rect_t rc;
  159.  
  160.     if(txv->endline < txv->lines)
  161.     {
  162.         dst  = txv->margins.t;
  163.         src  = dst + txv->line_height;
  164.         rows = txv->line_height * (txv->pagesize-1);
  165.         scroll_ctx(txv->ctx, dst, src, rows );
  166.  
  167.         rc.l = txv->margins.l;
  168.         rc.t = txv->margins.t + rows;
  169.         rc.r = txv->w - txv->margins.r;
  170.         rc.b = rc.t + txv->line_height;
  171.  
  172.         draw_rect(txv->ctx, rc.l, rc.t, rc.r - rc.l, txv->line_height, 0xFFFFFFFF);
  173.  
  174.         draw_text_ext(txv->ctx, txv->font, txv->line[txv->endline],
  175.                       txv->line[txv->endline+1]-txv->line[txv->endline], &rc, 0xFF000000);
  176.  
  177.         txv->startline++;
  178.         txv->endline++;
  179.  
  180.         if( txv->tail && (txv->endline < txv->lines))
  181.         {
  182.             rc.t+= txv->line_height;
  183.             rc.b+= txv->line_height;
  184.             draw_rect(txv->ctx, rc.l, rc.t, rc.r - rc.l, txv->line_height, 0xFFFFFFFF);
  185.             draw_text_ext(txv->ctx, txv->font, txv->line[txv->endline],
  186.                           txv->line[txv->endline+1]-txv->line[txv->endline], &rc, 0xFF000000);
  187.         }
  188.         return 1;
  189.     };
  190.  
  191.     return 0;
  192. };
  193.  
  194. int txv_scroll_up(tview_t *txv)
  195. {
  196.     int dst, src, rows;
  197.     rect_t rc;
  198.  
  199.     if(txv->startline > 0)
  200.     {
  201.         rows = txv->tail + txv->line_height * (txv->pagesize-1);
  202.         src  = txv->margins.t;
  203.         dst  = src + txv->line_height;
  204.  
  205.         scroll_ctx(txv->ctx, dst, src, rows);
  206.  
  207.         rc.l = txv->margins.l;
  208.         rc.t = txv->margins.t;
  209.         rc.r = txv->w - txv->margins.r;
  210.         rc.b = rc.t + txv->line_height;
  211.  
  212.         draw_rect(txv->ctx, rc.l, rc.t, rc.r - rc.l, txv->line_height, 0xFFFFFFFF);
  213.  
  214.         txv->startline--;
  215.         txv->endline--;
  216.  
  217.         draw_text_ext(txv->ctx, txv->font, txv->line[txv->startline],
  218.                       txv->line[txv->startline+1]-txv->line[txv->startline], &rc, 0xFF000000);
  219.         return 1;
  220.     };
  221.  
  222.     return 0;
  223. };
  224.  
  225. int txv_page_up(tview_t *txv)
  226. {
  227.     int startline, endline;
  228.  
  229.     startline = txv->startline - txv->pagesize;
  230.     txv->startline = startline >= 0 ? startline : 0;
  231.  
  232.     endline = txv->startline + txv->pagesize;
  233.     txv->endline = endline  < txv->lines ? endline : txv->lines;
  234.     txv_redraw(txv);
  235.     return 1;
  236. };
  237.  
  238. int txv_page_down(tview_t *txv)
  239. {
  240.     int startline, endline;
  241.  
  242.     endline = txv->endline + txv->pagesize;
  243.     txv->endline = endline  < txv->lines ? endline : txv->lines;
  244.     startline = txv->endline - txv->pagesize;
  245.     txv->startline = startline >= 0 ? startline : 0;
  246.     txv_redraw(txv);
  247.     return 1;
  248. };
  249.  
  250. void txv_set_size(tview_t *txv, int txw, int txh)
  251. {
  252.     txv->w = txw;
  253.     txv->h = txh;
  254.  
  255.     txv_recalc(txv);
  256.     txv_redraw(txv);
  257. };
  258.  
  259. void txv_set_font_size(tview_t *txv, int size)
  260. {
  261.  
  262.     if(size > 72)
  263.         size = 72;
  264.     else if(size < 6)
  265.         size = 6;
  266.  
  267.     txv->font = create_font(NULL, size);
  268.     if ( txv->font == NULL )
  269.     {
  270.         printf("cannot create font\n");
  271.         return;
  272.     }
  273.  
  274.     txv->line_height = txv->leading + get_font_height(txv->font);
  275.     txv_recalc(txv);
  276.     txv_redraw(txv);
  277. }
  278.  
  279. int  txv_get_font_size(tview_t *txv)
  280. {
  281.     return get_font_height(txv->font);
  282. }
  283.