/src/ftk_menu_item.c

http://ftk.googlecode.com/ · C · 153 lines · 104 code · 20 blank · 29 comment · 13 complexity · 592b1bcccd0e0ae9d75ba5122202b76d MD5 · raw file

  1. /*
  2. * File: ftk_menu_item.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: menu item.
  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-30 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_log.h"
  31. #include "ftk_canvas.h"
  32. #include "ftk_menu_item.h"
  33. #include "ftk_globals.h"
  34. #include "ftk_icon_cache.h"
  35. #include "ftk_menu_panel.h"
  36. typedef struct _MenuItemPrivInfo
  37. {
  38. FtkListener listener;
  39. void* listener_ctx;
  40. }PrivInfo;
  41. static Ret ftk_menu_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. ftk_widget_set_active(thiz, 0);
  73. ret = FTK_CALL_LISTENER(priv->listener, priv->listener_ctx, thiz);
  74. }
  75. break;
  76. }
  77. default:break;
  78. }
  79. return ret;
  80. }
  81. static Ret ftk_menu_item_on_paint(FtkWidget* thiz)
  82. {
  83. FTK_BEGIN_PAINT(x, y, width, height, canvas);
  84. return_val_if_fail(width > 0 && height > 0, RET_FAIL);
  85. ftk_canvas_reset_gc(canvas, ftk_widget_get_gc(thiz));
  86. if(ftk_widget_get_text(thiz))
  87. {
  88. const char* text = ftk_widget_get_text(thiz);
  89. int fh = ftk_widget_get_font_size(thiz);
  90. int fw = ftk_canvas_get_str_extent(canvas, text, -1);
  91. int dx = FTK_HALF(width - fw);
  92. int dy = FTK_HALF(height);
  93. assert(fh < height && fw < width);
  94. ftk_canvas_draw_string(canvas, x + dx, y + dy, text, -1, 1);
  95. }
  96. FTK_END_PAINT();
  97. }
  98. static void ftk_menu_item_destroy(FtkWidget* thiz)
  99. {
  100. if(thiz != NULL)
  101. {
  102. DECL_PRIV0(thiz, priv);
  103. FTK_FREE(priv);
  104. }
  105. return;
  106. }
  107. FtkWidget* ftk_menu_item_create(FtkWidget* parent)
  108. {
  109. FtkWidget* thiz = (FtkWidget*)FTK_ZALLOC(sizeof(FtkWidget));
  110. return_val_if_fail(thiz != NULL, NULL);
  111. thiz->priv_subclass[0] = (PrivInfo*)FTK_ZALLOC(sizeof(PrivInfo));
  112. if(thiz != NULL)
  113. {
  114. thiz->on_event = ftk_menu_item_on_event;
  115. thiz->on_paint = ftk_menu_item_on_paint;
  116. thiz->destroy = ftk_menu_item_destroy;
  117. ftk_widget_init(thiz, FTK_MENU_ITEM, 0, 0, 0, 0, 0, FTK_ATTR_TRANSPARENT|FTK_ATTR_BG_FOUR_CORNER);
  118. ftk_menu_panel_add(parent, thiz);
  119. }
  120. else
  121. {
  122. FTK_FREE(thiz);
  123. }
  124. return thiz;
  125. }
  126. Ret ftk_menu_item_set_clicked_listener(FtkWidget* thiz, FtkListener listener, void* ctx)
  127. {
  128. DECL_PRIV0(thiz, priv);
  129. return_val_if_fail(thiz != NULL, RET_FAIL);
  130. priv->listener_ctx = ctx;
  131. priv->listener = listener;
  132. return RET_OK;
  133. }