widget.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  16. #include <config.h>
  17. #include "widget.h"
  18. #include "mk_wcwidth.h"
  19. #include "bidi.h"
  20. #include "shaping.h"
  21. #include "my_wctob.h"
  22. Widget::Widget()
  23. {
  24. wnd = NULL;
  25. modal = false;
  26. }
  27. Widget::~Widget()
  28. {
  29. destroy_window();
  30. }
  31. bool Widget::create_window(int lines, int cols)
  32. {
  33. if (terminal::is_interactive()) {
  34. wnd = newwin(lines, cols, 0, 0);
  35. enable_keypad();
  36. }
  37. return wnd != NULL;
  38. }
  39. void Widget::resize(int lines, int columns, int y, int x)
  40. {
  41. wresize(wnd, lines, columns);
  42. mvwin(wnd, y, x);
  43. }
  44. void Widget::put_unichar(unichar ch, unichar bad_repr)
  45. {
  46. #ifdef HAVE_WIDE_CURSES
  47. if (!terminal::is_utf8 && WCTOB(ch) == EOF)
  48. ch = bad_repr;
  49. waddnwstr(wnd, (wchar_t*)&ch, 1);
  50. #else
  51. int ich = terminal::force_iso88598 ? unicode_to_iso88598(ch) : WCTOB(ch);
  52. if (ich == EOF)
  53. ich = bad_repr;
  54. waddch(wnd, (unsigned char)ich);
  55. #endif
  56. }
  57. // draw_string() - draws a UTF-8 string. This is a very simple routine and
  58. // it's used by the most simple widgets only (like Label).
  59. void Widget::draw_string(const char *u8, bool align_right)
  60. {
  61. unistring text, vis_text;
  62. text.init_from_utf8(u8);
  63. // trim string to fit window width
  64. int wnd_x, dummy;
  65. getyx(wnd, dummy, wnd_x);
  66. int swidth = wnd_x;
  67. unichar *trim_pos = text.begin();
  68. while (trim_pos < text.end()) {
  69. int char_width = mk_wcwidth(*trim_pos);
  70. if (swidth + char_width > window_width())
  71. break;
  72. swidth += char_width;
  73. trim_pos++;
  74. }
  75. text.erase(trim_pos, text.end());
  76. // convert to visual
  77. direction_t dir = BiDi::determine_base_dir(text.begin(), text.size(),
  78. algoUnicode);
  79. BiDi::simple_log2vis(text, dir, vis_text);
  80. if (terminal::do_arabic_shaping) {
  81. int new_len = shape(vis_text.begin(), vis_text.len());
  82. swidth -= vis_text.len() - new_len;
  83. vis_text.resize(new_len);
  84. }
  85. // draw the string
  86. if (align_right && dir == dirRTL)
  87. wmove(wnd, 0, window_width() - swidth);
  88. for (int i = 0; i < vis_text.len(); i++) {
  89. put_unichar(vis_text[i], '?');
  90. }
  91. // reposition the cursor
  92. if (align_right && dir == dirRTL)
  93. wmove(wnd, 0, window_width() - swidth - 1);
  94. }
  95. void Widget::signal_error()
  96. {
  97. // I don't like those awful beeps,
  98. // but people may not be familiar with flashes.
  99. //flash();
  100. beep();
  101. }