1 /*
2  * Copyright 2009 Paul Blokus <paul_pl@users.sourceforge.net>
3  *
4  * This file is part of NetSurf, http://www.netsurf-browser.org/
5  *
6  * NetSurf is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * NetSurf is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 /**
20  * \file
21  * Scrollbar widget interface.
22  *
23  * Scrollbar widgets used in frames code, not for frontend use
24  */
25 
26 #ifndef NETSURF_DESKTOP_SCROLLBAR_H
27 #define NETSURF_DESKTOP_SCROLLBAR_H
28 
29 #include <stdbool.h>
30 #include <limits.h>
31 
32 #define SCROLLBAR_WIDTH 16
33 
34 /* Region dependent values for scrollbar_scroll function */
35 #define SCROLL_TOP	 INT_MIN
36 #define SCROLL_PAGE_UP	 (INT_MIN + 1)
37 #define SCROLL_PAGE_DOWN (INT_MAX - 1)
38 #define SCROLL_BOTTOM	 INT_MAX
39 
40 struct scrollbar;
41 struct redraw_context;
42 
43 /**
44  * scrollbar message types
45  */
46 typedef enum {
47 	SCROLLBAR_MSG_MOVED,		/**< the scroll value has changed */
48 	SCROLLBAR_MSG_SCROLL_START,	/**< a scrollbar drag has started, all
49 					 * mouse events should be passed to
50 					 * the scrollbar regardless of the
51 					 * coordinates
52 					 */
53 	SCROLLBAR_MSG_SCROLL_FINISHED,	/**< cancel a scrollbar drag */
54 } scrollbar_msg;
55 
56 /**
57  * scrollbar message context data
58  */
59 struct scrollbar_msg_data {
60 	struct scrollbar *scrollbar;
61 	scrollbar_msg msg;
62 	int scroll_offset;
63 	int x0, y0, x1, y1;
64 };
65 
66 
67 /**
68  * Scrollbar mouse input status flags
69  */
70 typedef enum {
71 	SCROLLBAR_MOUSE_NONE	= 0,		/**< Not relevant */
72 	SCROLLBAR_MOUSE_USED	= (1 <<  0),	/**< Took action with input */
73 	SCROLLBAR_MOUSE_BOTH	= (1 <<  1),	/**< Scrolling both bars */
74 	SCROLLBAR_MOUSE_UP	= (1 <<  2),	/**< Hover: scroll up */
75 	SCROLLBAR_MOUSE_PUP	= (1 <<  3),	/**< Hover: scroll page up */
76 	SCROLLBAR_MOUSE_VRT	= (1 <<  4),	/**< Hover: vert. drag bar */
77 	SCROLLBAR_MOUSE_PDWN	= (1 <<  5),	/**< Hover: scroll page down */
78 	SCROLLBAR_MOUSE_DWN	= (1 <<  6),	/**< Hover: scroll down */
79 	SCROLLBAR_MOUSE_LFT	= (1 <<  7),	/**< Hover: scroll left */
80 	SCROLLBAR_MOUSE_PLFT	= (1 <<  8),	/**< Hover: scroll page left */
81 	SCROLLBAR_MOUSE_HRZ	= (1 <<  9),	/**< Hover: horiz. drag bar */
82 	SCROLLBAR_MOUSE_PRGT	= (1 << 10),	/**< Hover: scroll page right */
83 	SCROLLBAR_MOUSE_RGT	= (1 << 11)	/**< Hover: scroll right */
84 } scrollbar_mouse_status;
85 
86 
87 /**
88  * Client callback for the scrollbar.
89  *
90  * \param client_data		user data passed at scroll creation
91  * \param scrollbar_data	scrollbar message data
92  */
93 typedef void(*scrollbar_client_callback)(void *client_data,
94 		struct scrollbar_msg_data *scrollbar_data);
95 
96 
97 /**
98  * Create a scrollbar.
99  *
100  * \param horizontal		true = horizontal scrollbar, false = vertical
101  * \param length		length of scrollbar widget
102  * \param full_size		length of contained scrollable area
103  * \param visible_size		length of visible part of scrollable area
104  * \param client_data		data for the client callback
105  * \param client_callback	client callback for scrollbar events
106  * \param s			updated to point at the newly created scrollbar
107  * \return NSERROR_OK and s updated if scrollbar has been created
108  *           succesfully or eror code and s set to NULL on faliure;
109  */
110 nserror scrollbar_create(bool horizontal, int length, int full_size,
111 		int visible_size, void *client_data,
112 		scrollbar_client_callback client_callback,
113 		struct scrollbar **s);
114 
115 /**
116  * Destroy a scrollbar.
117  *
118  * \param s the scrollbar to be destroyed
119  */
120 void scrollbar_destroy(struct scrollbar *s);
121 
122 /**
123  * Redraw a part of the scrollbar.
124  *
125  * \param s	the scrollbar to be redrawn
126  * \param x	the X coordinate to draw the scrollbar at
127  * \param y	the Y coordinate to draw the scrollbar at
128  * \param clip	the clipping rectangle
129  * \param scale	scale for the redraw
130  * \param ctx	current redraw context
131  * \return	NSERROR_OK on success otherwise error code
132  */
133 nserror scrollbar_redraw(struct scrollbar *s, int x, int y,
134 		const struct rect *clip, float scale,
135 		const struct redraw_context *ctx);
136 
137 /**
138  * Set the scroll value of the scrollbar.
139  *
140  * \param s		the scrollbar to have the value set
141  * \param value		the new value to be set
142  * \param bar_pos	true if the value is for the scrollbar indication bar
143  *			position, false if it is for the scrolled area offset
144  */
145 void scrollbar_set(struct scrollbar *s, int value, bool bar_pos);
146 
147 /**
148  * Scroll the scrollbar by given amount.
149  *
150  * \param s		the scrollbar to be scrolled
151  * \param change	the change in scroll offset required (in px)
152  * \return true iff the scrollbar was moved.
153  */
154 bool scrollbar_scroll(struct scrollbar *s, int change);
155 
156 /**
157  * Get the current scroll offset to the visible part of the full area.
158  *
159  * \param s the scrollbar to get the scroll offset value from
160  * \return current scroll offset
161  */
162 int scrollbar_get_offset(struct scrollbar *s);
163 
164 /**
165  * Set the length of the scrollbar widget, the size of the visible area, and the
166  * size of the full area.
167  *
168  * \param s		the scrollbar to set the values for
169  * \param length	-1 or the new scrollbar widget length
170  * \param visible_size	-1 or the new size of the visible area
171  * \param full_size	-1 or the new size of the full contained area
172  */
173 void scrollbar_set_extents(struct scrollbar *s, int length,
174 		int visible_size, int full_size);
175 
176 /**
177  * Check orientation of the scrollbar.
178  *
179  * \param s	the scrollbar to check the orientation of
180  * \return	true for a horizontal scrollbar, else false (vertical)
181  */
182 bool scrollbar_is_horizontal(struct scrollbar *s);
183 
184 
185 /**
186  * Handle mouse actions other then drag ends.
187  *
188  * \param s	the scrollbar which gets the mouse action
189  * \param mouse	mouse state
190  * \param x	X coordinate of the mouse
191  * \param y	Y coordinate of the mouse
192  * \return	the scrollbar mouse status
193  */
194 scrollbar_mouse_status scrollbar_mouse_action(struct scrollbar *s,
195 		browser_mouse_state mouse, int x, int y);
196 
197 
198 /**
199  * Get a status bar message from a scrollbar mouse input status.
200  *
201  * \param status	Status to convert to message
202  * \return		Message for the status bar or NULL on failure
203  */
204 const char *scrollbar_mouse_status_to_message(scrollbar_mouse_status status);
205 
206 
207 /**
208  * Handle end of mouse drags.
209  *
210  * \param s	the scrollbar for which the drag ends
211  * \param mouse	mouse state
212  * \param x	X coordinate of the mouse
213  * \param y	Y coordinate of the mouse
214  */
215 void scrollbar_mouse_drag_end(struct scrollbar *s,
216 		browser_mouse_state mouse, int x, int y);
217 
218 
219 /**
220  * Called when the content is being dragged to the scrollbars have to adjust.
221  *
222  * If the content has both scrollbars, and scrollbar_make_pair has beed called
223  * before, only the one scroll which will receive further mouse events has to be
224  * passed.
225  *
226  * \param s one of the the scrollbars owned by the dragged content
227  * \param x X coordinate of mouse during drag start
228  * \param y Y coordinate of mouse during drag start
229  */
230 void scrollbar_start_content_drag(struct scrollbar *s, int x, int y);
231 
232 
233 /**
234  * Connect a horizontal and a vertical scrollbar into a pair so that they
235  * co-operate during 2D drags.
236  *
237  * \param horizontal	the scrollbar used for horizontal scrolling
238  * \param vertical	the scrollbar used for vertical scrolling
239  */
240 void scrollbar_make_pair(struct scrollbar *horizontal,
241 		struct scrollbar *vertical);
242 
243 
244 /**
245  * Get the scrollbar's client data
246  *
247  * \param s the scrollbar to get the client data from
248  * \return client data
249  */
250 void *scrollbar_get_data(struct scrollbar *s);
251 
252 #endif
253