PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/frontend/gtkmm/src/osx/OSXTimeBar.m

https://github.com/zkrebs/workrave
Objective C | 276 lines | 99 code | 46 blank | 131 comment | 8 complexity | 2599fc3d6fa0dd2547c3c7308a11b7f4 MD5 | raw file
  1. #import "OSXTimeBar.h"
  2. @implementation OSXTimeBar
  3. - (void) setText: (NSString*) aText
  4. {
  5. text = aText;
  6. }
  7. - (void) setValue: (int) aValue
  8. {
  9. value = aValue;
  10. }
  11. - (void) setColor: (ColorId) aColor
  12. {
  13. color = aColor;
  14. }
  15. - (void) setMaxValue: (int) aMaxValue
  16. {
  17. max_value = aMaxValue;
  18. }
  19. - (void) setSecondaryValue: (int) aSecondaryValue
  20. {
  21. secondary_value = aSecondaryValue;
  22. }
  23. - (void) setSecondaryMaxValue: (int) aSecondaryMaxValue
  24. {
  25. secondary_max_value = aSecondaryMaxValue;
  26. }
  27. - (void) setSecondaryColor: (ColorId) aColor
  28. {
  29. secondary_color = aColor;
  30. }
  31. - (void)drawRect:(NSRect)rect
  32. {
  33. // [[NSColor whiteColor] set];
  34. // NSRectFill(NSMakeRect(1, 1, 98, 20));
  35. const int border_size = 2;
  36. int win_w = 30;
  37. int win_h = 22;
  38. // Draw background
  39. [[NSColor blackColor] set];
  40. NSFrameRect(NSMakeRect(1, 1, win_w - 2, win_h - 2));
  41. [[NSColor whiteColor] set];
  42. NSRectFill(NSMakeRect(2, 2, win_w - 4, win_h - 4));
  43. // Bar
  44. int bar_width = 0;
  45. if (max_value > 0)
  46. {
  47. bar_width = (value * (win_w - 2 * border_size)) / max_value;
  48. }
  49. // Secondary bar
  50. int sbar_width = 0;
  51. if (secondary_max_value > 0)
  52. {
  53. sbar_width = (secondary_value * (win_w - 2 * border_size)) / secondary_max_value;
  54. }
  55. int bar_height = win_h - 2 * border_size;
  56. if (sbar_width > 0)
  57. {
  58. // Overlap
  59. // We should assert() because this is not supported
  60. // but there are some weird boundary cases
  61. // in which this still happens.. need to check
  62. // this out some time.
  63. // assert(secondary_bar_color == COLOR_ID_INACTIVE);
  64. NSColor *overlap_color;
  65. switch (color)
  66. {
  67. case COLOR_ID_ACTIVE:
  68. overlap_color = [NSColor blueColor];
  69. break;
  70. case COLOR_ID_OVERDUE:
  71. overlap_color = [NSColor orangeColor];
  72. break;
  73. default:
  74. // We could abort() because this is not supported
  75. // but there are some weird boundary cases
  76. // in which this still happens.. need to check
  77. // this out some time.
  78. overlap_color = [NSColor redColor];
  79. }
  80. if (sbar_width >= bar_width)
  81. {
  82. if (bar_width)
  83. {
  84. [[NSColor redColor] set];
  85. NSRectFill(NSMakeRect(border_size, border_size, bar_width, bar_height));
  86. // window_gc->set_foreground(bar_colors[overlap_color]);
  87. // draw_bar(window, window_gc, true,
  88. // border_size, border_size,
  89. // bar_width, bar_height,
  90. // win_w, win_h);
  91. }
  92. if (sbar_width > bar_width)
  93. {
  94. [[NSColor yellowColor] set];
  95. NSRectFill(NSMakeRect(border_size + bar_width, border_size, sbar_width - bar_width, bar_height));
  96. // window_gc->set_foreground(bar_colors[secondary_bar_color]);
  97. // draw_bar(window, window_gc, true,
  98. // border_size + bar_width, border_size,
  99. // sbar_width - bar_width, bar_height,
  100. // win_w, win_h);
  101. }
  102. }
  103. else
  104. {
  105. if (sbar_width)
  106. {
  107. [[NSColor blueColor] set];
  108. NSRectFill(NSMakeRect(border_size, border_size, sbar_width, bar_height));
  109. // window_gc->set_foreground(bar_colors[overlap_color]);
  110. // draw_bar(window, window_gc, true,
  111. // border_size, border_size,
  112. // sbar_width, bar_height,
  113. // win_w, win_h);
  114. }
  115. [[NSColor greenColor] set];
  116. NSRectFill(NSMakeRect(border_size + sbar_width, border_size, bar_width - sbar_width, bar_height));
  117. // window_gc->set_foreground(bar_colors[bar_color]);
  118. // draw_bar(window, window_gc, true,
  119. // border_size + sbar_width, border_size,
  120. // bar_width - sbar_width, bar_height,
  121. // win_w, win_h);
  122. }
  123. }
  124. else
  125. {
  126. [[NSColor grayColor] set];
  127. NSRectFill(NSMakeRect(border_size, border_size, bar_width, bar_height));
  128. // No overlap
  129. // window_gc->set_foreground(bar_colors[bar_color]);
  130. // draw_bar(window, window_gc, true,
  131. // border_size, border_size,
  132. // bar_width, bar_height, win_w, win_h);
  133. }
  134. // Text
  135. // window_gc->set_foreground(bar_text_color);
  136. // Glib::RefPtr<Pango::Layout> pl1 = create_pango_layout(bar_text);
  137. // Glib::RefPtr<Pango::Context> pc1 = pl1->get_context();
  138. // Pango::Matrix matrix = PANGO_MATRIX_INIT;
  139. // pango_matrix_rotate(&matrix, 360 - rotation);
  140. // pc1->set_matrix(matrix);
  141. // int text_width, text_height;
  142. // pl1->get_pixel_size(text_width, text_height);
  143. // int text_x, text_y;
  144. // Gdk::Rectangle rect1, rect2;
  145. // if (rotation == 0 || rotation == 180)
  146. // {
  147. // if (win_w - text_width - MARGINX > 0)
  148. // {
  149. // if (bar_text_align > 0)
  150. // text_x = (win_w - text_width - MARGINX);
  151. // else if (bar_text_align < 0)
  152. // text_x = MARGINX;
  153. // else
  154. // text_x = (win_w - text_width) / 2;
  155. // }
  156. // else
  157. // {
  158. // text_x = MARGINX;
  159. // }
  160. // text_y = (win_h - text_height) / 2;
  161. // int left_width = (bar_width > sbar_width) ? bar_width : sbar_width;
  162. // left_width += border_size;
  163. // Gdk::Rectangle left_rect(0, 0, left_width, win_h);
  164. // Gdk::Rectangle right_rect(left_width, 0, win_w - left_width, win_h);
  165. // rect1 = left_rect;
  166. // rect2 = right_rect;
  167. // }
  168. // else
  169. // {
  170. // if (win_h - text_width - MARGINY > 0)
  171. // {
  172. // int a = bar_text_align;
  173. // if (rotation == 270)
  174. // {
  175. // a *= -1;
  176. // }
  177. // if (a > 0)
  178. // text_y = (win_h - text_width - MARGINY);
  179. // else if (a < 0)
  180. // text_y = MARGINY;
  181. // else
  182. // text_y = (win_h - text_width) / 2;
  183. // }
  184. // else
  185. // {
  186. // text_y = MARGINY;
  187. // }
  188. // text_x = (win_w - text_height) / 2;
  189. // int left_width = (bar_width > sbar_width) ? bar_width : sbar_width;
  190. // left_width += border_size;
  191. // Gdk::Rectangle up_rect(0, 0, win_w, left_width);
  192. // Gdk::Rectangle down_rect(0, left_width, win_w, win_h - left_width);
  193. // rect1 = up_rect;
  194. // rect2 = down_rect;
  195. // }
  196. // Gdk::Color textcolor = style->get_fg(Gtk::STATE_NORMAL);
  197. // TRACE_MSG(textcolor.get_red() << " " <<
  198. // textcolor.get_green() << " " <<
  199. // textcolor.get_blue());
  200. // Glib::RefPtr<Gdk::GC> window_gc1 = Gdk::GC::create(window);
  201. // window_gc1->set_clip_origin(0,0);
  202. // window_gc1->set_clip_rectangle(rect1);
  203. // window_gc1->set_foreground(bar_text_color);
  204. // window->draw_layout(window_gc1, text_x, text_y, pl1);
  205. // window_gc1->set_foreground(textcolor);
  206. // window_gc1->set_clip_rectangle(rect2);
  207. // window->draw_layout(window_gc1, text_x, text_y, pl1);
  208. // TRACE_EXIT();
  209. }
  210. // void
  211. // TimeBar::draw_bar(Glib::RefPtr<Gdk::Window> &window,
  212. // const Glib::RefPtr<Gdk::GC> &gc,
  213. // bool filled, int x, int y, int width, int height,
  214. // int winw, int winh)
  215. // {
  216. // (void) winh;
  217. // if (rotation == 0 || rotation == 180)
  218. // {
  219. // window->draw_rectangle(gc, filled, x, y, width, height);
  220. // }
  221. // else
  222. // {
  223. // window->draw_rectangle(gc, filled, y, winw - x - width, height, width);
  224. // }
  225. // }
  226. - (void)dealloc
  227. {
  228. [super dealloc];
  229. }
  230. @end