Blender  V3.3
ScrollBar.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
4 #include <stdlib.h>
5 
6 #include <math.h>
7 
8 #include "MEM_guardedalloc.h"
9 
10 #include "Basic.h"
11 #include "ScrollBar.h"
12 
13 struct _ScrollBar {
14  int rect[2][2];
16 
17  int inset;
18  int minthumb;
19 
20  int scrolling;
21  float scrolloffs;
22 };
23 
25 {
26  int scrollable_h = rect_height(sb->rect) - 2 * sb->inset;
27 
28  return clamp_i(sb->thumbpct * scrollable_h, sb->minthumb, scrollable_h);
29 }
30 
32 {
33  int scrollable_h = rect_height(sb->rect) - 2 * sb->inset;
34  int thumb_h = scrollbar_get_thumbH(sb);
35 
36  return scrollable_h - thumb_h;
37 }
38 
39 static float scrollbar_co_to_pos(ScrollBar *sb, int yco)
40 {
41  int thumb_h = scrollbar_get_thumbH(sb);
42  int thumbable_h = scrollbar_get_thumbableH(sb);
43  int thumbable_y = (sb->rect[0][1] + sb->inset) + thumb_h / 2;
44 
45  return (float)(yco - thumbable_y) / thumbable_h;
46 }
47 
48 
49 
50 ScrollBar *scrollbar_new(int inset, int minthumb)
51 {
52  ScrollBar *sb = MEM_callocN(sizeof(*sb), "scrollbar_new");
53  sb->inset = inset;
54  sb->minthumb = minthumb;
55 
56  return sb;
57 }
58 
59 void scrollbar_get_thumb(ScrollBar *sb, int thumb_r[2][2])
60 {
61  int thumb_h = scrollbar_get_thumbH(sb);
62  int thumbable_h = scrollbar_get_thumbableH(sb);
63 
64  thumb_r[0][0] = sb->rect[0][0] + sb->inset;
65  thumb_r[1][0] = sb->rect[1][0] - sb->inset;
66 
67  thumb_r[0][1] = sb->rect[0][1] + sb->inset + sb->thumbpos * thumbable_h;
68  thumb_r[1][1] = thumb_r[0][1] + thumb_h;
69 }
70 
72 {
73  return sb->scrolling;
74 }
75 int scrollbar_contains_pt(ScrollBar *sb, int pt[2])
76 {
77  return rect_contains_pt(sb->rect, pt);
78 }
79 
81 {
82  int thumb_h_2 = scrollbar_get_thumbH(sb) / 2;
83  int thumbable_h = scrollbar_get_thumbableH(sb);
84  float npos = scrollbar_co_to_pos(sb, yco);
85 
86  sb->scrolloffs = sb->thumbpos - npos;
87  if (fabs(sb->scrolloffs) >= (float)thumb_h_2 / thumbable_h) {
88  sb->scrolloffs = 0.0;
89  }
90 
91  sb->scrolling = 1;
92  sb->thumbpos = clamp_f(npos + sb->scrolloffs, 0.0, 1.0);
93 }
95 {
96  float npos = scrollbar_co_to_pos(sb, yco);
97 
98  sb->thumbpos = clamp_f(npos + sb->scrolloffs, 0.0, 1.0);
99 }
101 {
102  sb->scrolling = 0;
103  sb->scrolloffs = 0.0;
104 }
105 
106 void scrollbar_set_thumbpct(ScrollBar *sb, float pct)
107 {
108  sb->thumbpct = pct;
109 }
111 {
112  sb->thumbpos = clamp_f(pos, 0.0, 1.0);
113 }
114 void scrollbar_set_rect(ScrollBar *sb, int rect[2][2])
115 {
116  rect_copy(sb->rect, rect);
117 }
118 
120 {
121  return sb->thumbpct;
122 }
124 {
125  return sb->thumbpos;
126 }
127 void scrollbar_get_rect(ScrollBar *sb, int rect_r[2][2])
128 {
129  rect_copy(rect_r, sb->rect);
130 }
131 
133 {
134  MEM_freeN(sb);
135 }
MINLINE float clamp_f(float value, float min, float max)
MINLINE int clamp_i(int value, int min, int max)
int rect_contains_pt(int rect[2][2], int pt[2])
Definition: Basic.c:37
int rect_height(int rect[2][2])
Definition: Basic.c:46
void rect_copy(int dst[2][2], int src[2][2])
Definition: Basic.c:32
Read Guarded memory(de)allocation.
static int scrollbar_get_thumbH(ScrollBar *sb)
Definition: ScrollBar.c:24
void scrollbar_keep_scrolling(ScrollBar *sb, int yco)
Definition: ScrollBar.c:94
void scrollbar_start_scrolling(ScrollBar *sb, int yco)
Definition: ScrollBar.c:80
int scrollbar_is_scrolling(ScrollBar *sb)
Definition: ScrollBar.c:71
static int scrollbar_get_thumbableH(ScrollBar *sb)
Definition: ScrollBar.c:31
ScrollBar * scrollbar_new(int inset, int minthumb)
Definition: ScrollBar.c:50
float scrollbar_get_thumbpos(ScrollBar *sb)
Definition: ScrollBar.c:123
static float scrollbar_co_to_pos(ScrollBar *sb, int yco)
Definition: ScrollBar.c:39
void scrollbar_get_rect(ScrollBar *sb, int rect_r[2][2])
Definition: ScrollBar.c:127
void scrollbar_get_thumb(ScrollBar *sb, int thumb_r[2][2])
Definition: ScrollBar.c:59
void scrollbar_set_rect(ScrollBar *sb, int rect[2][2])
Definition: ScrollBar.c:114
float scrollbar_get_thumbpct(ScrollBar *sb)
Definition: ScrollBar.c:119
void scrollbar_free(ScrollBar *sb)
Definition: ScrollBar.c:132
int scrollbar_contains_pt(ScrollBar *sb, int pt[2])
Definition: ScrollBar.c:75
void scrollbar_set_thumbpct(ScrollBar *sb, float pct)
Definition: ScrollBar.c:106
void scrollbar_stop_scrolling(ScrollBar *sb)
Definition: ScrollBar.c:100
void scrollbar_set_thumbpos(ScrollBar *sb, float pos)
Definition: ScrollBar.c:110
uint pos
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
ccl_device_inline float2 fabs(const float2 &a)
Definition: math_float2.h:222
int scrolling
Definition: ScrollBar.c:20
int inset
Definition: ScrollBar.c:17
float thumbpos
Definition: ScrollBar.c:15
int minthumb
Definition: ScrollBar.c:18
float thumbpct
Definition: ScrollBar.c:15
float scrolloffs
Definition: ScrollBar.c:21
int rect[2][2]
Definition: ScrollBar.c:14