Use rxvt_temp_buf in more places.
[dana/urxvt.git] / src / scrollbar.C
1 /*----------------------------------------------------------------------*
2  * File:        scrollbar.C
3  *----------------------------------------------------------------------*
4  *
5  * Copyright (c) 1997,1998 mj olesen <olesen@me.QueensU.CA>
6  * Copyright (c) 1998      Alfredo K. Kojima <kojima@windowmaker.org>
7  *                              - N*XTstep like scrollbars
8  * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com>
9  * Copyright (c) 2004-2006 Marc Lehmann <pcg@goof.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *----------------------------------------------------------------------*/
25
26 #include "../config.h"          /* NECESSARY */
27 #include "rxvt.h"               /* NECESSARY */
28
29 /*----------------------------------------------------------------------*/
30
31 /*
32  * Map or unmap a scrollbar.  Returns non-zero upon change of state
33  */
34 int
35 scrollBar_t::map (int map)
36 {
37   int change = 0;
38
39   if (map)
40     {
41       state = STATE_IDLE;
42
43       if (!win)
44         resize ();
45
46       if (win)
47         {
48           XMapWindow (term->dpy, win);
49           change = 1;
50         }
51     }
52   else
53     {
54       state = 0;
55       XUnmapWindow (term->dpy, win);
56       change = 1;
57     }
58
59   return change;
60 }
61
62 void
63 scrollBar_t::resize ()
64 {
65   int delayed_init = 0;
66   int window_sb_x = 0;
67
68   if (term->option (Opt_scrollBar_right))
69     window_sb_x = term->szHint.width - total_width ();
70
71   update_data ();
72
73   if (!win)
74     {
75       /* create the scrollbar window */
76       win = XCreateSimpleWindow (term->dpy,
77                                  term->parent[0],
78                                  window_sb_x, 0,
79                                  total_width (),
80                                  term->szHint.height,
81                                  0,
82                                  term->pix_colors[Color_fg],
83                                  term->pix_colors[Color_border]);
84       XDefineCursor (term->dpy, win, leftptr_cursor);
85
86       XSelectInput (term->dpy, win,
87                    ExposureMask | ButtonPressMask | ButtonReleaseMask
88                    | Button1MotionMask | Button2MotionMask
89                    | Button3MotionMask);
90       term->scrollbar_ev.start (term->display, win);
91
92       delayed_init = 1;
93     }
94   else
95     XMoveResizeWindow (term->dpy, win,
96                        window_sb_x, 0,
97                        total_width (), term->szHint.height);
98
99   show (1);
100
101   if (delayed_init)
102     XMapWindow (term->dpy, win);
103 }
104
105 /*
106  * Update current scrollbar view w.r.t. slider heights, etc.
107  */
108 int
109 scrollBar_t::show (int refresh)
110 {
111   int ret;
112
113   if (!state)
114     return 0;
115
116   if (refresh)
117     {
118       int sb_top = term->view_start - term->top_row;
119       int sb_bot = sb_top + (term->nrow - 1);
120       int sb_len = max (term->nrow - 1 - term->top_row, 1);
121       int sb_size = (sb_bot - sb_top) * size ();
122
123       top = beg + (sb_top * size ()) / sb_len;
124       bot = top + sb_size / sb_len + min_height () + (sb_size % sb_len > 0);
125       /* no change */
126       if (top == last_top
127           && bot == last_bot
128           && (state == last_state
129               || !(state == STATE_UP || state == STATE_DOWN)))
130         return 0;
131     }
132
133   ret = (this->*update) (refresh);
134
135   last_top = top;
136   last_bot = bot;
137   last_state = state;
138
139   return ret;
140 }
141
142 void
143 scrollBar_t::setup (rxvt_term *term)
144 {
145   int             i;
146   short           style, width;
147   const char *scrollalign, *scrollstyle, *thickness;
148
149   this->term = term;
150   scrollalign = term->rs[Rs_scrollBar_align];
151   scrollstyle = term->rs[Rs_scrollstyle];
152   thickness = term->rs[Rs_scrollBar_thickness];
153
154 # if defined(RXVT_SCROLLBAR)
155   style = R_SB_RXVT;
156 # elif defined(XTERM_SCROLLBAR)
157   style = R_SB_XTERM;
158 # elif defined(NEXT_SCROLLBAR)
159   style = R_SB_NEXT;
160 # elif defined(PLAIN_SCROLLBAR)
161   style = R_SB_PLAIN;
162 #else
163   style = R_SB_RXVT;
164 # endif
165
166 # if (defined(NEXT_SCROLLBAR) || defined(XTERM_SCROLLBAR) || defined(PLAIN_SCROLLBAR))
167   if (scrollstyle)
168     {
169 #  ifdef NEXT_SCROLLBAR
170       if (strncasecmp (scrollstyle, "next", 4) == 0)
171         style = R_SB_NEXT;
172 #  endif
173 #  ifdef XTERM_SCROLLBAR
174       if (strncasecmp (scrollstyle, "xterm", 5) == 0)
175         style = R_SB_XTERM;
176 #  endif
177 #  ifdef PLAIN_SCROLLBAR
178       if (strncasecmp (scrollstyle, "plain", 5) == 0)
179         style = R_SB_PLAIN;
180 #  endif
181
182     }
183 # endif
184   if (style == R_SB_NEXT)
185     width = SB_WIDTH_NEXT;
186   else if (style == R_SB_XTERM)
187     width = SB_WIDTH_XTERM;
188   else if (style == R_SB_PLAIN)
189     width = SB_WIDTH_PLAIN;
190   else /* if (style == R_SB_RXVT) */
191     width = SB_WIDTH_RXVT;
192
193   if (style != R_SB_NEXT)       /* dishonour request - for now */
194     if (thickness && (i = atoi (thickness)) >= SB_WIDTH_MINIMUM)
195       width = min (i, SB_WIDTH_MAXIMUM);
196
197 # ifdef RXVT_SCROLLBAR
198   if (! term->option (Opt_scrollBar_floating) && style == R_SB_RXVT)
199     shadow = SHADOW_WIDTH;
200 # endif
201
202   this->style = style;
203   this->width = width;
204
205   /* align = R_SB_ALIGN_CENTRE; */
206   if (scrollalign)
207     {
208       if (strncasecmp (scrollalign, "top", 3) == 0)
209         align = R_SB_ALIGN_TOP;
210       else if (strncasecmp (scrollalign, "bottom", 6) == 0)
211         align = R_SB_ALIGN_BOTTOM;
212     }
213   last_bot = last_state = -1;
214   /* cursor scrollBar: Black-on-White */
215   leftptr_cursor = XCreateFontCursor (term->dpy, XC_left_ptr);
216 }
217
218 void
219 scrollBar_t::destroy ()
220 {
221 #ifdef XTERM_SCROLLBAR
222   if (xscrollbarGC) XFreeGC (term->dpy, xscrollbarGC);
223   if (ShadowGC)     XFreeGC (term->dpy, ShadowGC);
224 #endif
225 #ifdef PLAIN_SCROLLBAR
226   if (pscrollbarGC) XFreeGC (term->dpy, pscrollbarGC);
227 #endif
228 #ifdef NEXT_SCROLLBAR
229   if (blackGC)      XFreeGC (term->dpy, blackGC);
230   if (whiteGC)      XFreeGC (term->dpy, whiteGC);
231   if (grayGC)       XFreeGC (term->dpy, grayGC);
232   if (darkGC)       XFreeGC (term->dpy, darkGC);
233   if (stippleGC)    XFreeGC (term->dpy, stippleGC);
234   if (dimple)       XFreePixmap (term->dpy, dimple);
235   if (upArrow)      XFreePixmap (term->dpy, upArrow);
236   if (downArrow)    XFreePixmap (term->dpy, downArrow);
237   if (upArrowHi)    XFreePixmap (term->dpy, upArrowHi);
238   if (downArrowHi)  XFreePixmap (term->dpy, downArrowHi);
239 #endif
240 #ifdef RXVT_SCROLLBAR
241   if (topShadowGC)  XFreeGC (term->dpy, topShadowGC);
242   if (botShadowGC)  XFreeGC (term->dpy, botShadowGC);
243   if (scrollbarGC)  XFreeGC (term->dpy, scrollbarGC);
244 #endif
245 }
246
247 void
248 scrollBar_t::update_data ()
249 {
250 #if defined(PLAIN_SCROLLBAR)
251   if (style == R_SB_PLAIN)
252     {
253       beg = 0;
254       end = term->szHint.height;
255       update = &scrollBar_t::show_plain;
256     }
257 #endif
258 #if defined(XTERM_SCROLLBAR)
259   if (style == R_SB_XTERM)
260     {
261       beg = 0;
262       end = term->szHint.height;
263       update = &scrollBar_t::show_xterm;
264     }
265 #endif
266 #if defined(NEXT_SCROLLBAR)
267   if (style == R_SB_NEXT)
268     {
269       beg = 0;
270       end = term->szHint.height - (SB_BUTTON_TOTAL_HEIGHT + SB_PADDING);
271       update = &scrollBar_t::show_next;
272     }
273 #endif
274 #if defined(RXVT_SCROLLBAR)
275   if (style == R_SB_RXVT)
276     {
277       beg = (width + 1) + shadow;
278       end = term->szHint.height - beg - (2 * shadow);
279       update = &scrollBar_t::show_rxvt;
280     }
281 #endif
282 }
283
284 /*----------------------- end-of-file (C source) -----------------------*/
285