/src/ftk_wait_box.c

http://ftk.googlecode.com/ · C · 143 lines · 89 code · 25 blank · 29 comment · 10 complexity · 23957117847164e17bcbc2c676b1b2c2 MD5 · raw file

  1. /*
  2. * File: ftk_wait_box.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: wait box
  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-19 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_globals.h"
  31. #include "ftk_wait_box.h"
  32. #include "ftk_source_timer.h"
  33. typedef struct _WaitBoxPrivInfo
  34. {
  35. int offset;
  36. int waiting;
  37. FtkSource* timer;
  38. FtkBitmap* bitmap;
  39. }PrivInfo;
  40. static Ret ftk_wait_box_on_event(FtkWidget* thiz, FtkEvent* event)
  41. {
  42. return RET_OK;
  43. }
  44. static Ret ftk_wait_box_on_paint(FtkWidget* thiz)
  45. {
  46. DECL_PRIV0(thiz, priv);
  47. int bitmap_w = ftk_bitmap_width(priv->bitmap);
  48. int bitmap_h = ftk_bitmap_height(priv->bitmap);
  49. FTK_BEGIN_PAINT(x, y, width, height, canvas);
  50. (void)width;
  51. (void)height;
  52. if(priv->waiting)
  53. {
  54. priv->offset = priv->offset < bitmap_h ? priv->offset : 0;
  55. ftk_canvas_draw_bitmap_simple(canvas, priv->bitmap, 0, priv->offset, bitmap_w, bitmap_w, x, y);
  56. priv->offset += bitmap_w;
  57. }
  58. FTK_END_PAINT();
  59. }
  60. static void ftk_wait_box_destroy(FtkWidget* thiz)
  61. {
  62. if(thiz != NULL)
  63. {
  64. DECL_PRIV0(thiz, priv);
  65. if(priv->waiting)
  66. {
  67. ftk_wait_box_stop_waiting(thiz);
  68. }
  69. ftk_source_unref(priv->timer);
  70. ftk_bitmap_unref(priv->bitmap);
  71. FTK_ZFREE(priv, sizeof(PrivInfo));
  72. }
  73. return;
  74. }
  75. FtkWidget* ftk_wait_box_create(FtkWidget* parent, int x, int y, int w, int h)
  76. {
  77. FtkWidget* thiz = (FtkWidget*)FTK_ZALLOC(sizeof(FtkWidget));
  78. return_val_if_fail(thiz != NULL, NULL);
  79. thiz->priv_subclass[0] = (PrivInfo*)FTK_ZALLOC(sizeof(PrivInfo));
  80. if(thiz->priv_subclass[0] != NULL)
  81. {
  82. int w = 0;
  83. DECL_PRIV0(thiz, priv);
  84. thiz->on_event = ftk_wait_box_on_event;
  85. thiz->on_paint = ftk_wait_box_on_paint;
  86. thiz->destroy = ftk_wait_box_destroy;
  87. priv->bitmap = ftk_theme_load_image(ftk_default_theme(), "wait_box"FTK_STOCK_IMG_SUFFIX);
  88. assert(priv->bitmap != NULL);
  89. w = ftk_bitmap_width(priv->bitmap);
  90. ftk_widget_init(thiz, FTK_WAIT_BOX, 0, x, y, w, w, FTK_ATTR_TRANSPARENT|FTK_ATTR_INSENSITIVE);
  91. ftk_widget_set_attr(thiz, FTK_ATTR_TRANSPARENT|FTK_ATTR_INSENSITIVE);
  92. priv->timer = ftk_source_timer_create(500, (FtkTimer)ftk_widget_invalidate, thiz);
  93. ftk_widget_append_child(parent, thiz);
  94. }
  95. else
  96. {
  97. FTK_FREE(thiz);
  98. }
  99. return thiz;
  100. }
  101. Ret ftk_wait_box_start_waiting(FtkWidget* thiz)
  102. {
  103. DECL_PRIV0(thiz, priv);
  104. return_val_if_fail(priv != NULL, RET_FAIL);
  105. return_val_if_fail(!priv->waiting, RET_FAIL);
  106. priv->offset = 0;
  107. priv->waiting = 1;
  108. ftk_source_ref(priv->timer);
  109. return ftk_main_loop_add_source(ftk_default_main_loop(), priv->timer);
  110. }
  111. Ret ftk_wait_box_stop_waiting(FtkWidget* thiz)
  112. {
  113. DECL_PRIV0(thiz, priv);
  114. return_val_if_fail(priv != NULL, RET_FAIL);
  115. return_val_if_fail(priv->waiting, RET_FAIL);
  116. priv->waiting = 0;
  117. return ftk_main_loop_remove_source(ftk_default_main_loop(), priv->timer);
  118. }