Subversion Repositories Kolibri OS

Rev

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