Merge ssh://git@sfinx.od.ua/home/git/Harbour/forms
[forms.git] / F / F_Text_Output.H
blobc926c8f1ed67afe803c43919f2ca094db6aacd92
2 #ifndef _F_TEXT_OUTPUT_H_
3 #define _F_TEXT_OUTPUT_H_
5 #include <F_Text_Display.H>
6 #include <F_Window.H>
7 #include <F_Scrollbar.H>
8 #include <cstdarg>
10 namespace F {
12 class F_Text_Output : public F_Widget {
14   F_Wrap_t wrap_;
15   F_Scrollbar *vs, *hs;
16   // ËÏÌÉÞÅÓÔ×Ï ÓÔÒÏË
17   unsigned int size_;
18   unsigned int topline_;
19   std::vector <std::string> lines;
20   char *line_buf;
21   unsigned int line_buf_size;
22   unsigned int max_line_size_;
23   unsigned int start_col_; // starting column
25   void draw();
26   bool handle(F_Event_t &ev);
27   void add_line(const char *str);
28   static void vs_callback(F_Scrollbar *s, void *) {
29     F_Text_Output *to = (F_Text_Output *)s->parent_wdg();
30     if (s->value() > (to->lines.size() - (to->h() - 1)))
31       to->topline(to->lines.size() - (to->h() - 1));
32     else
33       to->topline(s->value());
34   }
35   static void hs_callback(F_Scrollbar *s, void *) {
36     F_Text_Output *to = (F_Text_Output *)s->parent_wdg();
37     if (s->value() > (to->max_line_size() - (to->w() - 1)))
38       to->startcol(to->max_line_size() - (to->w() - 1));
39     else
40       to->startcol(s->value());
41   }
43   void startcol(int sc) {
44     if (sc < 0)
45       sc = 0;
46     start_col_ = sc;
47     draw();
48    }
49  public:
50   F_Text_Output(F_Box_Type_t btype, coord_t x, coord_t y, dim_t w, dim_t h,
51     const char *label = 0) : F_Widget(x, y, w, h, label) {
52       wrap_ = F_NO_WRAP;
53       size_ = 0;
54       topline_ = 0;
55       max_line_size_ = 0;
56       start_col_ = 0;
57       line_buf_size = 10240U;
58       line_buf = new char[line_buf_size];
59       // hor slider is created with max width
60       hs = new F_Scrollbar(F_NO_BOX, F_HORIZONTAL, x, y + h - 2, w, 1);
61       vs = new F_Scrollbar(F_NO_BOX, F_VERTICAL, w + x - 1, y + 1, 1, h - 2);
62       vs->callback((F_Callback *)vs_callback);
63       hs->callback((F_Callback *)hs_callback);
64       add_child(vs);
65       add_child(hs);
66  }
67   ~F_Text_Output() { delete [] line_buf; }
68   unsigned int max_line_size() { return max_line_size_; }
69   unsigned int start_col() { return start_col_; }
70   void topline(int tl) {
71     if (tl < 0)
72       tl = 0;
73     if (tl > int(lines.size()))
74       topline_ = lines.size();
75     else
76       topline_ = tl;
77     draw();
78   }
79   void wrap(F_Wrap_t w) { wrap_ = w; draw(); }
80   void clear() {
81     lines.clear();
82     topline_ = 0;
83     start_col_ = 0; // ?
84     max_line_size_ = 0;
85     vs->value(0);
86     hs->value(0);
87     draw();
88  }
89   void add(std::string &s) { add_line(s.c_str()); }
90   void add(const char *s) { add_line(s); }
91   void addf(const char *fmt, ...) {
92    ::va_list args;
93    ::va_start(args, fmt);
94    ::va_end(args);
95    ::vsnprintf(line_buf, line_buf_size, fmt, args);
96    add_line(line_buf);
97   }
98  };
101 #endif