/src/ftk_group_box.c

http://ftk.googlecode.com/ · C · 135 lines · 80 code · 25 blank · 30 comment · 16 complexity · b6a29ae94d71dbb71bde2f6983568dc5 MD5 · raw file

  1. /*
  2. * File: ftk_group_box.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: radio group.
  5. *
  6. * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2009-11-15 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_theme.h"
  31. #include "ftk_globals.h"
  32. #include "ftk_group_box.h"
  33. #include "ftk_check_button.h"
  34. typedef struct _GroupBoxPrivInfo
  35. {
  36. FtkBitmap* bg;
  37. }PrivInfo;
  38. static Ret ftk_group_box_on_event(FtkWidget* thiz, FtkEvent* event)
  39. {
  40. return RET_OK;
  41. }
  42. static Ret ftk_group_box_on_paint(FtkWidget* thiz)
  43. {
  44. DECL_PRIV0(thiz, priv);
  45. FTK_BEGIN_PAINT(x, y, width, height, canvas);
  46. return_val_if_fail(priv->bg != NULL, RET_FAIL);
  47. ftk_canvas_reset_gc(canvas, ftk_widget_get_gc(thiz));
  48. ftk_canvas_draw_bg_image(canvas, priv->bg, FTK_BG_FOUR_CORNER, x, y, width, height);
  49. if(ftk_widget_get_text(thiz) != NULL)
  50. {
  51. int yoffset = 2;
  52. int xoffset = 10;
  53. FtkTextLine line = {0};
  54. int font_height = ftk_widget_get_font_size(thiz);
  55. const char* text = ftk_widget_get_text(thiz);
  56. FtkTextLayout* text_layout = ftk_default_text_layout();
  57. ftk_text_layout_init(text_layout, text, -1, canvas, width - 2 * xoffset);
  58. if(ftk_text_layout_get_visual_line(text_layout, &line) == RET_OK)
  59. {
  60. FtkGc gc = {0};
  61. gc.mask = FTK_GC_FG;
  62. /*fill bg with the first pixel of bg image.*/
  63. gc.fg = ftk_bitmap_get_pixel(priv->bg, 0, 0);
  64. ftk_canvas_set_gc(canvas, &gc);
  65. ftk_canvas_draw_rect(canvas, x + xoffset, y + yoffset, line.extent, font_height, 0, 1);
  66. ftk_canvas_reset_gc(canvas, ftk_widget_get_gc(thiz));
  67. ftk_canvas_draw_string(canvas, x + xoffset, y + yoffset + font_height, line.text, line.len, 0);
  68. }
  69. }
  70. FTK_END_PAINT();
  71. }
  72. static void ftk_group_box_destroy(FtkWidget* thiz)
  73. {
  74. DECL_PRIV0(thiz, priv);
  75. if(priv->bg != NULL)
  76. {
  77. ftk_bitmap_unref(priv->bg);
  78. }
  79. FTK_ZFREE(thiz->priv_subclass[0], sizeof(PrivInfo));
  80. return;
  81. }
  82. FtkWidget* ftk_group_box_create(FtkWidget* parent, int x, int y, int width, int height)
  83. {
  84. FtkWidget* thiz = (FtkWidget*)FTK_ZALLOC(sizeof(FtkWidget) + sizeof(PrivInfo));
  85. return_val_if_fail(thiz != NULL, NULL);
  86. thiz->priv_subclass[0] = (PrivInfo*)FTK_ZALLOC(sizeof(PrivInfo));
  87. if(thiz->priv_subclass[0] != NULL)
  88. {
  89. DECL_PRIV0(thiz, priv);
  90. thiz->on_event = ftk_group_box_on_event;
  91. thiz->on_paint = ftk_group_box_on_paint;
  92. thiz->destroy = ftk_group_box_destroy;
  93. priv->bg = ftk_theme_load_image(ftk_default_theme(), "groupbox_bg"FTK_STOCK_IMG_SUFFIX);
  94. ftk_widget_init(thiz, FTK_GROUP_BOX, 0, x, y, width, height, FTK_ATTR_TRANSPARENT);
  95. ftk_widget_append_child(parent, thiz);
  96. }
  97. return thiz;
  98. }
  99. Ret ftk_group_box_set_checked(FtkWidget* thiz, FtkWidget* radio)
  100. {
  101. FtkWidget* iter = NULL;
  102. return_val_if_fail(thiz != NULL && radio != NULL, RET_FAIL);
  103. iter = thiz->children;
  104. while(iter != NULL)
  105. {
  106. ftk_check_button_set_checked(iter, iter == radio);
  107. iter = iter->next;
  108. }
  109. return RET_OK;
  110. }