1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 /** \file
18  * \ingroup spinfo
19  */
20 
21 #include "MEM_guardedalloc.h"
22 
23 #include "BLF_api.h"
24 
25 #include "BLI_math.h"
26 #include "BLI_string_utf8.h"
27 #include "BLI_utildefines.h"
28 
29 #include "GPU_immediate.h"
30 #include "GPU_state.h"
31 
32 #include "DNA_userdef_types.h" /* For 'U.dpi_fac' */
33 
34 #include "UI_interface.h"
35 #include "UI_interface_icons.h"
36 
37 #include "textview.h"
38 
textview_font_begin(const int font_id,const int lheight)39 static void textview_font_begin(const int font_id, const int lheight)
40 {
41   /* Font size in relation to line height. */
42   BLF_size(font_id, 0.8f * lheight, 72);
43 }
44 
45 typedef struct TextViewDrawState {
46   int font_id;
47   int cwidth;
48   int lheight;
49   /** Text vertical offset per line. */
50   int lofs;
51   int row_vpadding;
52   /** Number of characters that fit into the width of the console (fixed width). */
53   int columns;
54   /** For drawing text. */
55   const rcti *draw_rect;
56   /** For drawing backgrounds colors which may extend beyond text. */
57   const rcti *draw_rect_outer;
58   int scroll_ymin, scroll_ymax;
59   int *xy;   // [2]
60   int *sel;  // [2]
61   /* Bottom of view == 0, top of file == combine chars, end of line is lower than start. */
62   int *mval_pick_offset;
63   const int *mval;  // [2]
64   bool do_draw;
65 } TextViewDrawState;
66 
textview_step_sel(TextViewDrawState * tds,const int step)67 BLI_INLINE void textview_step_sel(TextViewDrawState *tds, const int step)
68 {
69   tds->sel[0] += step;
70   tds->sel[1] += step;
71 }
72 
textview_draw_sel(const char * str,const int xy[2],const int str_len_draw,TextViewDrawState * tds,const uchar bg_sel[4])73 static void textview_draw_sel(const char *str,
74                               const int xy[2],
75                               const int str_len_draw,
76                               TextViewDrawState *tds,
77                               const uchar bg_sel[4])
78 {
79   const int sel[2] = {tds->sel[0], tds->sel[1]};
80   const int cwidth = tds->cwidth;
81   const int lheight = tds->lheight;
82 
83   if (sel[0] <= str_len_draw && sel[1] >= 0) {
84     const int sta = BLI_str_utf8_offset_to_column(str, max_ii(sel[0], 0));
85     const int end = BLI_str_utf8_offset_to_column(str, min_ii(sel[1], str_len_draw));
86 
87     GPU_blend(GPU_BLEND_ALPHA);
88 
89     GPUVertFormat *format = immVertexFormat();
90     uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
91     immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
92 
93     immUniformColor4ubv(bg_sel);
94     immRecti(pos, xy[0] + (cwidth * sta), xy[1] + lheight, xy[0] + (cwidth * end), xy[1]);
95 
96     immUnbindProgram();
97 
98     GPU_blend(GPU_BLEND_NONE);
99   }
100 }
101 
102 /**
103  * \warning Allocated memory for 'r_offsets' must be freed by caller.
104  * \return The length in bytes.
105  */
textview_wrap_offsets(const char * str,int len,int width,int * r_lines,int ** r_offsets)106 static int textview_wrap_offsets(
107     const char *str, int len, int width, int *r_lines, int **r_offsets)
108 {
109   int i, end; /* Offset as unicode code-point. */
110   int j;      /* Offset as bytes. */
111 
112   *r_lines = 1;
113 
114   *r_offsets = MEM_callocN(
115       sizeof(**r_offsets) *
116           (len * BLI_UTF8_WIDTH_MAX / MAX2(1, width - (BLI_UTF8_WIDTH_MAX - 1)) + 1),
117       __func__);
118   (*r_offsets)[0] = 0;
119 
120   for (i = 0, end = width, j = 0; j < len && str[j]; j += BLI_str_utf8_size_safe(str + j)) {
121     int columns = BLI_str_utf8_char_width_safe(str + j);
122 
123     if (i + columns > end) {
124       (*r_offsets)[*r_lines] = j;
125       (*r_lines)++;
126 
127       end = i + width;
128     }
129     i += columns;
130   }
131   return j;
132 }
133 
134 /**
135  * return false if the last line is off the screen
136  * should be able to use this for any string type.
137  */
textview_draw_string(TextViewDrawState * tds,const char * str,int str_len,const uchar fg[4],const uchar bg[4],int icon,const uchar icon_fg[4],const uchar icon_bg[4],const uchar bg_sel[4])138 static bool textview_draw_string(TextViewDrawState *tds,
139                                  const char *str,
140                                  int str_len,
141                                  const uchar fg[4],
142                                  const uchar bg[4],
143                                  int icon,
144                                  const uchar icon_fg[4],
145                                  const uchar icon_bg[4],
146                                  const uchar bg_sel[4])
147 {
148   int tot_lines; /* Total number of lines for wrapping. */
149   int *offsets;  /* Offsets of line beginnings for wrapping. */
150 
151   str_len = textview_wrap_offsets(str, str_len, tds->columns, &tot_lines, &offsets);
152 
153   int line_height = (tot_lines * tds->lheight) + (tds->row_vpadding * 2);
154   int line_bottom = tds->xy[1];
155   int line_top = line_bottom + line_height;
156 
157   int y_next = line_top;
158 
159   /* Just advance the height. */
160   if (tds->do_draw == false) {
161     if (tds->mval_pick_offset && tds->mval[1] != INT_MAX && line_bottom <= tds->mval[1]) {
162       if (y_next >= tds->mval[1]) {
163         int ofs = 0;
164 
165         /* Wrap. */
166         if (tot_lines > 1) {
167           int iofs = (int)((float)(y_next - tds->mval[1]) / tds->lheight);
168           ofs += offsets[MIN2(iofs, tot_lines - 1)];
169         }
170 
171         /* Last part. */
172         ofs += BLI_str_utf8_offset_from_column(str + ofs,
173                                                (int)floor((float)tds->mval[0] / tds->cwidth));
174 
175         CLAMP(ofs, 0, str_len);
176         *tds->mval_pick_offset += str_len - ofs;
177       }
178       else {
179         *tds->mval_pick_offset += str_len + 1;
180       }
181     }
182 
183     tds->xy[1] = y_next;
184     MEM_freeN(offsets);
185     return true;
186   }
187   if (y_next < tds->scroll_ymin) {
188     /* Have not reached the drawable area so don't break. */
189     tds->xy[1] = y_next;
190 
191     /* Adjust selection even if not drawing. */
192     if (tds->sel[0] != tds->sel[1]) {
193       textview_step_sel(tds, -(str_len + 1));
194     }
195 
196     MEM_freeN(offsets);
197     return true;
198   }
199 
200   size_t len;
201   const char *s;
202   int i;
203 
204   int sel_orig[2];
205   copy_v2_v2_int(sel_orig, tds->sel);
206 
207   /* Invert and swap for wrapping. */
208   tds->sel[0] = str_len - sel_orig[1];
209   tds->sel[1] = str_len - sel_orig[0];
210 
211   if (bg) {
212     GPUVertFormat *format = immVertexFormat();
213     uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
214     immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
215     immUniformColor4ubv(bg);
216     immRecti(pos, tds->draw_rect_outer->xmin, line_bottom, tds->draw_rect_outer->xmax, line_top);
217     immUnbindProgram();
218   }
219 
220   if (icon_bg) {
221     float col[4];
222     int bg_size = UI_DPI_ICON_SIZE * 1.2;
223     float vpadding = (tds->lheight + (tds->row_vpadding * 2) - bg_size) / 2;
224     float hpadding = tds->draw_rect->xmin - (bg_size * 1.2f);
225 
226     rgba_uchar_to_float(col, icon_bg);
227     UI_draw_roundbox_corner_set(UI_CNR_ALL);
228     UI_draw_roundbox_aa(true,
229                         hpadding,
230                         line_top - bg_size - vpadding,
231                         bg_size + hpadding,
232                         line_top - vpadding,
233                         4 * UI_DPI_FAC,
234                         col);
235   }
236 
237   if (icon) {
238     int vpadding = (tds->lheight + (tds->row_vpadding * 2) - UI_DPI_ICON_SIZE) / 2;
239     int hpadding = tds->draw_rect->xmin - (UI_DPI_ICON_SIZE * 1.3f);
240 
241     GPU_blend(GPU_BLEND_ALPHA);
242     UI_icon_draw_ex(hpadding,
243                     line_top - UI_DPI_ICON_SIZE - vpadding,
244                     icon,
245                     (16 / UI_DPI_ICON_SIZE),
246                     1.0f,
247                     0.0f,
248                     icon_fg,
249                     false);
250     GPU_blend(GPU_BLEND_NONE);
251   }
252 
253   tds->xy[1] += tds->row_vpadding;
254 
255   /* Last part needs no clipping. */
256   const int final_offset = offsets[tot_lines - 1];
257   len = str_len - final_offset;
258   s = str + final_offset;
259   BLF_position(tds->font_id, tds->xy[0], tds->lofs + line_bottom + tds->row_vpadding, 0);
260   BLF_color4ubv(tds->font_id, fg);
261   BLF_draw_mono(tds->font_id, s, len, tds->cwidth);
262 
263   if (tds->sel[0] != tds->sel[1]) {
264     textview_step_sel(tds, -final_offset);
265     const int pos[2] = {tds->xy[0], line_bottom};
266     textview_draw_sel(s, pos, len, tds, bg_sel);
267   }
268 
269   tds->xy[1] += tds->lheight;
270 
271   BLF_color4ubv(tds->font_id, fg);
272 
273   for (i = tot_lines - 1; i > 0; i--) {
274     len = offsets[i] - offsets[i - 1];
275     s = str + offsets[i - 1];
276 
277     BLF_position(tds->font_id, tds->xy[0], tds->lofs + tds->xy[1], 0);
278     BLF_draw_mono(tds->font_id, s, len, tds->cwidth);
279 
280     if (tds->sel[0] != tds->sel[1]) {
281       textview_step_sel(tds, len);
282       textview_draw_sel(s, tds->xy, len, tds, bg_sel);
283     }
284 
285     tds->xy[1] += tds->lheight;
286 
287     /* Check if we're out of view bounds. */
288     if (tds->xy[1] > tds->scroll_ymax) {
289       MEM_freeN(offsets);
290       return false;
291     }
292   }
293 
294   tds->xy[1] = y_next;
295 
296   copy_v2_v2_int(tds->sel, sel_orig);
297   textview_step_sel(tds, -(str_len + 1));
298 
299   MEM_freeN(offsets);
300   return true;
301 }
302 
303 /**
304  * \param r_mval_pick_item: The resulting item clicked on using \a mval_init.
305  * Set from the void pointer which holds the current iterator.
306  * Its type depends on the data being iterated over.
307  * \param r_mval_pick_offset: The offset in bytes of the \a mval_init.
308  * Use for selection.
309  */
textview_draw(TextViewContext * tvc,const bool do_draw,const int mval_init[2],void ** r_mval_pick_item,int * r_mval_pick_offset)310 int textview_draw(TextViewContext *tvc,
311                   const bool do_draw,
312                   const int mval_init[2],
313                   void **r_mval_pick_item,
314                   int *r_mval_pick_offset)
315 {
316   TextViewDrawState tds = {0};
317 
318   const int x_orig = tvc->draw_rect.xmin, y_orig = tvc->draw_rect.ymin;
319   int xy[2];
320   /* Disable selection by. */
321   int sel[2] = {-1, -1};
322   uchar fg[4], bg[4], icon_fg[4], icon_bg[4];
323   int icon = 0;
324   const int font_id = blf_mono_font;
325 
326   textview_font_begin(font_id, tvc->lheight);
327 
328   xy[0] = x_orig;
329   xy[1] = y_orig;
330 
331   /* Offset and clamp the results,
332    * clamping so moving the cursor out of the bounds doesn't wrap onto the other lines. */
333   const int mval[2] = {
334       (mval_init[0] == INT_MAX) ?
335           INT_MAX :
336           CLAMPIS(mval_init[0], tvc->draw_rect.xmin, tvc->draw_rect.xmax) - tvc->draw_rect.xmin,
337       (mval_init[1] == INT_MAX) ?
338           INT_MAX :
339           CLAMPIS(mval_init[1], tvc->draw_rect.ymin, tvc->draw_rect.ymax) + tvc->scroll_ymin,
340   };
341 
342   if (r_mval_pick_offset != NULL) {
343     *r_mval_pick_offset = 0;
344   }
345 
346   /* Constants for the text-view context. */
347   tds.font_id = font_id;
348   tds.cwidth = (int)BLF_fixed_width(font_id);
349   BLI_assert(tds.cwidth > 0);
350   tds.lheight = tvc->lheight;
351   tds.row_vpadding = tvc->row_vpadding;
352   tds.lofs = -BLF_descender(font_id);
353   /* Note, scroll bar must be already subtracted. */
354   tds.columns = (tvc->draw_rect.xmax - tvc->draw_rect.xmin) / tds.cwidth;
355   /* Avoid divide by zero on small windows. */
356   if (tds.columns < 1) {
357     tds.columns = 1;
358   }
359   tds.draw_rect = &tvc->draw_rect;
360   tds.draw_rect_outer = &tvc->draw_rect_outer;
361   tds.scroll_ymin = tvc->scroll_ymin;
362   tds.scroll_ymax = tvc->scroll_ymax;
363   tds.xy = xy;
364   tds.sel = sel;
365   tds.mval_pick_offset = r_mval_pick_offset;
366   tds.mval = mval;
367   tds.do_draw = do_draw;
368 
369   if (tvc->sel_start != tvc->sel_end) {
370     sel[0] = tvc->sel_start;
371     sel[1] = tvc->sel_end;
372   }
373 
374   if (tvc->begin(tvc)) {
375     uchar bg_sel[4] = {0};
376 
377     if (do_draw && tvc->const_colors) {
378       tvc->const_colors(tvc, bg_sel);
379     }
380 
381     int iter_index = 0;
382     do {
383       const char *ext_line;
384       int ext_len;
385       int data_flag = 0;
386 
387       const int y_prev = xy[1];
388 
389       if (do_draw) {
390         data_flag = tvc->line_data(tvc, fg, bg, &icon, icon_fg, icon_bg);
391       }
392 
393       tvc->line_get(tvc, &ext_line, &ext_len);
394 
395       const bool is_out_of_view_y = !textview_draw_string(
396           &tds,
397           ext_line,
398           ext_len,
399           (data_flag & TVC_LINE_FG) ? fg : NULL,
400           (data_flag & TVC_LINE_BG) ? bg : NULL,
401           (data_flag & TVC_LINE_ICON) ? icon : 0,
402           (data_flag & TVC_LINE_ICON_FG) ? icon_fg : NULL,
403           (data_flag & TVC_LINE_ICON_BG) ? icon_bg : NULL,
404           bg_sel);
405 
406       if (do_draw) {
407         /* We always want the cursor to draw. */
408         if (tvc->draw_cursor && iter_index == 0) {
409           tvc->draw_cursor(tvc, tds.cwidth, tds.columns);
410         }
411 
412         /* When drawing, if we pass v2d->cur.ymax, then quit. */
413         if (is_out_of_view_y) {
414           break;
415         }
416       }
417 
418       if ((mval[1] != INT_MAX) && (mval[1] >= y_prev && mval[1] <= xy[1])) {
419         *r_mval_pick_item = (void *)tvc->iter;
420         break;
421       }
422 
423       iter_index++;
424 
425     } while (tvc->step(tvc));
426   }
427 
428   tvc->end(tvc);
429 
430   /* Sanity checks (bugs here can be tricky to track down). */
431   BLI_assert(tds.lheight == tvc->lheight);
432   BLI_assert(tds.row_vpadding == tvc->row_vpadding);
433   BLI_assert(tds.do_draw == do_draw);
434 
435   xy[1] += tvc->lheight * 2;
436 
437   return xy[1] - y_orig;
438 }
439