/src/ftk_status_item.c

http://ftk.googlecode.com/ · C · 171 lines · 119 code · 23 blank · 29 comment · 16 complexity · 6c32520e51c59c1254db082e83412df8 MD5 · raw file

  1. /*
  2. * File: ftk_status_item.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: item on the status panel.
  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-10-03 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_log.h"
  31. #include "ftk_window.h"
  32. #include "ftk_globals.h"
  33. #include "ftk_status_item.h"
  34. #include "ftk_status_panel.h"
  35. typedef struct _StutusItemPrivInfo
  36. {
  37. int pos;
  38. FtkListener listener;
  39. void* listener_ctx;
  40. }PrivInfo;
  41. static Ret ftk_status_item_on_event(FtkWidget* thiz, FtkEvent* event)
  42. {
  43. Ret ret = RET_OK;
  44. DECL_PRIV0(thiz, priv);
  45. switch(event->type)
  46. {
  47. case FTK_EVT_MOUSE_DOWN:
  48. {
  49. ftk_widget_set_active(thiz, 1);
  50. ftk_window_grab(ftk_widget_toplevel(thiz), thiz);
  51. break;
  52. }
  53. case FTK_EVT_MOUSE_UP:
  54. {
  55. ftk_widget_set_active(thiz, 0);
  56. ftk_window_ungrab(ftk_widget_toplevel(thiz), thiz);
  57. ret = FTK_CALL_LISTENER(priv->listener, priv->listener_ctx, thiz);
  58. break;
  59. }
  60. case FTK_EVT_KEY_DOWN:
  61. {
  62. if(FTK_IS_ACTIVE_KEY(event->u.key.code))
  63. {
  64. ftk_widget_set_active(thiz, 1);
  65. }
  66. break;
  67. }
  68. case FTK_EVT_KEY_UP:
  69. {
  70. if(FTK_IS_ACTIVE_KEY(event->u.key.code) && ftk_widget_is_active(thiz))
  71. {
  72. ret = FTK_CALL_LISTENER(priv->listener, priv->listener_ctx, thiz);
  73. ftk_widget_set_active(thiz, 0);
  74. }
  75. break;
  76. }
  77. default:break;
  78. }
  79. return ret;
  80. }
  81. static Ret ftk_status_item_on_paint(FtkWidget* thiz)
  82. {
  83. FTK_BEGIN_PAINT(x, y, width, height, canvas);
  84. ftk_canvas_reset_gc(canvas, ftk_widget_get_gc(thiz));
  85. if(ftk_widget_get_text(thiz) != NULL)
  86. {
  87. int yoffset = FTK_HALF(height);
  88. FtkTextLine line = {0};
  89. const char* text = ftk_widget_get_text(thiz);
  90. FtkTextLayout* text_layout = ftk_default_text_layout();
  91. ftk_text_layout_init(text_layout, text, -1, canvas, width);
  92. if(ftk_text_layout_get_visual_line(text_layout, &line) == RET_OK)
  93. {
  94. ftk_canvas_draw_string(canvas, x, y + yoffset, line.text, line.len, 1);
  95. }
  96. }
  97. FTK_END_PAINT();
  98. }
  99. static void ftk_status_item_destroy(FtkWidget* thiz)
  100. {
  101. if(thiz != NULL)
  102. {
  103. DECL_PRIV0(thiz, priv);
  104. FTK_FREE(priv);
  105. }
  106. return;
  107. }
  108. FtkWidget* ftk_status_item_create(FtkWidget* parent, int pos, int width)
  109. {
  110. FtkWidget* thiz = (FtkWidget*)FTK_ZALLOC(sizeof(FtkWidget));
  111. return_val_if_fail(thiz != NULL, NULL);
  112. thiz->priv_subclass[0] = (PrivInfo*)FTK_ZALLOC(sizeof(PrivInfo));
  113. if(thiz->priv_subclass[0] != NULL)
  114. {
  115. thiz->on_event = ftk_status_item_on_event;
  116. thiz->on_paint = ftk_status_item_on_paint;
  117. thiz->destroy = ftk_status_item_destroy;
  118. ftk_widget_init(thiz, FTK_STATUS_ITEM, 0, 0, 0, width, 0, FTK_ATTR_TRANSPARENT);
  119. ftk_status_item_set_position(thiz, pos);
  120. ftk_status_panel_add(parent, thiz);
  121. }
  122. else
  123. {
  124. FTK_FREE(thiz);
  125. }
  126. return thiz;
  127. }
  128. Ret ftk_status_item_set_position(FtkWidget* thiz, int pos)
  129. {
  130. DECL_PRIV0(thiz, priv);
  131. return_val_if_fail(thiz != NULL, RET_OK);
  132. priv->pos = pos;
  133. return RET_OK;
  134. }
  135. int ftk_status_item_get_position(FtkWidget* thiz)
  136. {
  137. DECL_PRIV0(thiz, priv);
  138. return_val_if_fail(thiz != NULL, 0);
  139. return priv->pos;
  140. }
  141. Ret ftk_status_item_set_clicked_listener(FtkWidget* thiz, FtkListener listener, void* ctx)
  142. {
  143. DECL_PRIV0(thiz, priv);
  144. return_val_if_fail(thiz != NULL, RET_FAIL);
  145. priv->listener_ctx = ctx;
  146. priv->listener = listener;
  147. return RET_OK;
  148. }