1 /*
2  * Copyright 2004, Joe English
3  *
4  * Support routines for scrollable widgets.
5  *
6  * (This is sort of half-baked; needs some work)
7  *
8  * Scrollable interface:
9  *
10  * 	+ 'first' is controlled by [xy]view widget command
11  * 	  and other scrolling commands like 'see';
12  *      + 'total' depends on widget contents;
13  *      + 'last' depends on first, total, and widget size.
14  *
15  * Choreography (typical usage):
16  *
17  * 	1. User adjusts scrollbar, scrollbar widget calls its -command
18  * 	2. Scrollbar -command invokes the scrollee [xy]view widget method
19  * 	3. TtkScrollviewCommand calls TtkScrollTo(), which updates
20  * 	   'first' and schedules a redisplay.
21  * 	4. Once the scrollee knows 'total' and 'last' (typically in
22  * 	   the LayoutProc), call TtkScrolled(h,first,last,total) to
23  * 	   synchronize the scrollbar.
24  * 	5. The scrollee -[xy]scrollcommand is called (in an idle callback)
25  * 	6. Which calls the scrollbar 'set' method and redisplays the scrollbar.
26  *
27  * If the scrollee has internal scrolling (e.g., a 'see' method),
28  * it should TtkScrollTo() directly (step 2).
29  *
30  * If the widget value changes, it should call TtkScrolled() (step 4).
31  * (This usually happens automatically when the widget is redisplayed).
32  *
33  * If the scrollee's -[xy]scrollcommand changes, it should call
34  * TtkScrollbarUpdateRequired, which will invoke step (5) (@@@ Fix this)
35  */
36 
37 #include <tkInt.h>
38 #include "ttkTheme.h"
39 #include "ttkWidget.h"
40 
41 /* Private data:
42  */
43 #define SCROLL_UPDATE_PENDING  (0x1)
44 #define SCROLL_UPDATE_REQUIRED (0x2)
45 
46 struct ScrollHandleRec
47 {
48     unsigned 	flags;
49     WidgetCore	*corePtr;
50     Scrollable	*scrollPtr;
51 };
52 
53 /* TtkCreateScrollHandle --
54  * 	Initialize scroll handle.
55  */
TtkCreateScrollHandle(WidgetCore * corePtr,Scrollable * scrollPtr)56 ScrollHandle TtkCreateScrollHandle(WidgetCore *corePtr, Scrollable *scrollPtr)
57 {
58     ScrollHandle h = (ScrollHandle)ckalloc(sizeof(*h));
59 
60     h->flags = 0;
61     h->corePtr = corePtr;
62     h->scrollPtr = scrollPtr;
63 
64     scrollPtr->first = 0;
65     scrollPtr->last = 1;
66     scrollPtr->total = 1;
67     return h;
68 }
69 
70 /* UpdateScrollbar --
71  *	Call the -scrollcommand callback to sync the scrollbar.
72  * 	Returns: Whatever the -scrollcommand does.
73  */
UpdateScrollbar(Tcl_Interp * interp,ScrollHandle h)74 static int UpdateScrollbar(Tcl_Interp *interp, ScrollHandle h)
75 {
76     Scrollable *s = h->scrollPtr;
77     WidgetCore *corePtr = h->corePtr;
78     char arg1[TCL_DOUBLE_SPACE + 2];
79     char arg2[TCL_DOUBLE_SPACE + 2];
80     int code;
81     Tcl_DString buf;
82 
83     h->flags &= ~SCROLL_UPDATE_REQUIRED;
84 
85     if (s->scrollCmd == NULL) {
86 	return TCL_OK;
87     }
88 
89     arg1[0] = arg2[0] = ' ';
90     Tcl_PrintDouble(interp, (double)s->first / s->total, arg1+1);
91     Tcl_PrintDouble(interp, (double)s->last / s->total, arg2+1);
92     Tcl_DStringInit(&buf);
93     Tcl_DStringAppend(&buf, s->scrollCmd, -1);
94     Tcl_DStringAppend(&buf, arg1, -1);
95     Tcl_DStringAppend(&buf, arg2, -1);
96 
97     Tcl_Preserve(corePtr);
98     code = Tcl_EvalEx(interp, Tcl_DStringValue(&buf), -1, TCL_EVAL_GLOBAL);
99     Tcl_DStringFree(&buf);
100     if (WidgetDestroyed(corePtr)) {
101 	Tcl_Release(corePtr);
102 	return TCL_ERROR;
103     }
104     Tcl_Release(corePtr);
105 
106     if (code != TCL_OK && !Tcl_InterpDeleted(interp)) {
107 	/* Disable the -scrollcommand, add to stack trace:
108 	 */
109 	ckfree(s->scrollCmd);
110 	s->scrollCmd = 0;
111 
112 	Tcl_AddErrorInfo(interp, /* @@@ "horizontal" / "vertical" */
113 		"\n    (scrolling command executed by ");
114 	Tcl_AddErrorInfo(interp, Tk_PathName(h->corePtr->tkwin));
115 	Tcl_AddErrorInfo(interp, ")");
116     }
117     return code;
118 }
119 
120 /* UpdateScrollbarBG --
121  * 	Idle handler to update the scrollbar.
122  */
UpdateScrollbarBG(ClientData clientData)123 static void UpdateScrollbarBG(ClientData clientData)
124 {
125     ScrollHandle h = (ScrollHandle)clientData;
126     Tcl_Interp *interp = h->corePtr->interp;
127     int code;
128 
129     h->flags &= ~SCROLL_UPDATE_PENDING;
130     Tcl_Preserve((ClientData) interp);
131     code = UpdateScrollbar(interp, h);
132     if (code == TCL_ERROR && !Tcl_InterpDeleted(interp)) {
133 	Tcl_BackgroundError(interp);
134     }
135     Tcl_Release((ClientData) interp);
136 }
137 
138 /* TtkScrolled --
139  * 	Update scroll info, schedule scrollbar update.
140  */
TtkScrolled(ScrollHandle h,int first,int last,int total)141 void TtkScrolled(ScrollHandle h, int first, int last, int total)
142 {
143     Scrollable *s = h->scrollPtr;
144 
145     /* Sanity-check inputs:
146      */
147     if (total <= 0) {
148 	first = 0;
149 	last = 1;
150 	total = 1;
151     }
152 
153     if (last > total) {
154 	first -= (last - total);
155 	if (first < 0) first = 0;
156 	last = total;
157     }
158 
159     if (s->first != first || s->last != last || s->total != total
160 	    || (h->flags & SCROLL_UPDATE_REQUIRED))
161     {
162 	s->first = first;
163 	s->last = last;
164 	s->total = total;
165 
166 	if (!(h->flags & SCROLL_UPDATE_PENDING)) {
167 	    Tcl_DoWhenIdle(UpdateScrollbarBG, (ClientData)h);
168 	    h->flags |= SCROLL_UPDATE_PENDING;
169 	}
170     }
171 }
172 
173 /* TtkScrollbarUpdateRequired --
174  * 	Force a scrollbar update at the next call to TtkScrolled(),
175  * 	even if scroll parameters haven't changed (e.g., if
176  * 	-yscrollcommand has changed).
177  */
178 
TtkScrollbarUpdateRequired(ScrollHandle h)179 void TtkScrollbarUpdateRequired(ScrollHandle h)
180 {
181     h->flags |= SCROLL_UPDATE_REQUIRED;
182 }
183 
184 /* TtkScrollviewCommand --
185  * 	Widget [xy]view command implementation.
186  *
187  *  $w [xy]view -- return current view region
188  *  $w [xy]view $index -- set topmost item
189  *  $w [xy]view moveto $fraction
190  *  $w [xy]view scroll $number $what -- scrollbar interface
191  */
TtkScrollviewCommand(Tcl_Interp * interp,int objc,Tcl_Obj * const objv[],ScrollHandle h)192 int TtkScrollviewCommand(
193     Tcl_Interp *interp, int objc, Tcl_Obj *const objv[], ScrollHandle h)
194 {
195     Scrollable *s = h->scrollPtr;
196     int newFirst = s->first;
197 
198     if (objc == 2) {
199 	Tcl_Obj *result[2];
200 	result[0] = Tcl_NewDoubleObj((double)s->first / s->total);
201 	result[1] = Tcl_NewDoubleObj((double)s->last / s->total);
202 	Tcl_SetObjResult(interp, Tcl_NewListObj(2, result));
203 	return TCL_OK;
204     } else if (objc == 3) {
205 	if (Tcl_GetIntFromObj(interp, objv[2], &newFirst) != TCL_OK) {
206 	    return TCL_ERROR;
207 	}
208     } else {
209 	double fraction;
210 	int count;
211 
212 	switch (Tk_GetScrollInfoObj(interp, objc, objv, &fraction, &count)) {
213 	    case TK_SCROLL_ERROR:
214 		return TCL_ERROR;
215 	    case TK_SCROLL_MOVETO:
216 		newFirst = (int) ((fraction * s->total) + 0.5);
217 		break;
218 	    case TK_SCROLL_UNITS:
219 		newFirst = s->first + count;
220 		break;
221 	    case TK_SCROLL_PAGES: {
222 		int perPage = s->last - s->first;	/* @@@ */
223 		newFirst = s->first + count * perPage;
224 		break;
225 	    }
226 	}
227     }
228 
229     TtkScrollTo(h, newFirst);
230 
231     return TCL_OK;
232 }
233 
TtkScrollTo(ScrollHandle h,int newFirst)234 void TtkScrollTo(ScrollHandle h, int newFirst)
235 {
236     Scrollable *s = h->scrollPtr;
237 
238     if (newFirst >= s->total)
239 	newFirst = s->total - 1;
240     if (newFirst > s->first && s->last >= s->total) /* don't scroll past end */
241 	newFirst = s->first;
242     if (newFirst < 0)
243 	newFirst = 0;
244 
245     if (newFirst != s->first) {
246 	s->first = newFirst;
247 	TtkRedisplayWidget(h->corePtr);
248     }
249 }
250 
TtkFreeScrollHandle(ScrollHandle h)251 void TtkFreeScrollHandle(ScrollHandle h)
252 {
253     if (h->flags & SCROLL_UPDATE_PENDING) {
254 	Tcl_CancelIdleCall(UpdateScrollbarBG, (ClientData)h);
255     }
256     ckfree((ClientData)h);
257 }
258 
259