Debian Codegraph @

gdraw/growcol.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "gdraw.h"
31 #include "ggadgetP.h"
32 #include "gkeysym.h"
33 #include "gwidget.h"
34 #include "ustring.h"
35
36 static Color GRowColLabelBg = 0xc0c0c0;
37
38 static void GRowColSelected(GRowCol *l) {
39 GEvent e;
40
41 e.type = et_controlevent;
42 e.w = l->g.base;
43 e.u.control.subtype = et_listselected;
44 e.u.control.g = &l->g;
45 e.u.control.u.list.from_mouse = false;
46 if ( l->g.handle_controlevent != NULL )
47 (l->g.handle_controlevent)(&l->g,&e);
48 else
49 GDrawPostEvent(&e);
50 }
51
52 static void GRowColDoubleClick(GRowCol *l) {
53 GEvent e;
54
55 e.type = et_controlevent;
56 e.w = l->g.base;
57 e.u.control.subtype = et_listdoubleclick;
58 e.u.control.g = &l->g;
59 if ( l->g.handle_controlevent != NULL )
60 (l->g.handle_controlevent)(&l->g,&e);
61 else
62 GDrawPostEvent(&e);
63 }
64
65 static int GRowColAlphaCompare(const void *v1, const void *v2) {
66 GTextInfo * const *pt1 = v1, * const *pt2 = v2;
67 return( GTextInfoCompare(*pt1,*pt2));
68 }
69
70 static void GRowColOrderIt(GRowCol *grc) {
71 if ( grc->order_entries ) {
72 /* Sort each entry left to right then top to bottom (1 2 3/4 5 6/...) */
73 qsort(grc->ti,grc->rows*grc->cols,sizeof(GTextInfo *),grc->orderer);
74 if ( grc->backwards ) {
75 int i;
76 GTextInfo *ti;
77 for ( i=0; i<grc->ltot/2; ++i ) {
78 ti = grc->ti[i];
79 grc->ti[i] = grc->ti[grc->ltot-1-i];
80 grc->ti[grc->ltot-1-i] = ti;
81 }
82 }
83 } else {
84 /* Sort each row, top to bottom (1 8 5/2 7 3/...) */
85 qsort(grc->ti,grc->rows,sizeof(GTextInfo *)*grc->cols,grc->orderer);
86 if ( grc->backwards ) {
87 int i;
88 GTextInfo **ti;
89 ti = gmalloc(grc->cols*sizeof(GTextInfo *));
90 for ( i=0; i<grc->rows/2; ++i ) {
91 memcpy(ti,grc->ti+(i*grc->cols),grc->cols*sizeof(GTextInfo *));
92 memcpy(grc->ti+(i*grc->cols),grc->ti+((grc->rows-1-i)*grc->cols),grc->cols*sizeof(GTextInfo *));
93 memcpy(grc->ti+((grc->rows-1-i)*grc->cols),ti,grc->cols*sizeof(GTextInfo *));
94 }
95 free(ti);
96 }
97 }
98 }
99
100 static void GRowColClearSel(GRowCol *grc) {
101 int i;
102
103 for ( i=0; i<grc->ltot; ++i )
104 grc->ti[i]->selected = false;
105 }
106
107 static int GRowColAnyOtherSels(GRowCol *grc, int pos) {
108 int i;
109
110 for ( i=0; i<grc->ltot; ++i )
111 if ( grc->ti[i]->selected && i!=pos )
112 return( true );
113
114 return( false );
115 }
116
117 static int32_t GRowColGetFirstSelPos(GGadget *g) {
118 int i;
119 GRowCol *grc = (GRowCol *) g;
120
121 for ( i=0; i<grc->ltot; ++i )
122 if ( grc->ti[i]->selected )
123 return( i );
124
125 return( -1 );
126 }
127
128 static void GRowColSelect(GGadget *g, int32_t pos, int32_t sel) {
129 GRowCol *grc = (GRowCol *) g;
130
131 GRowColClearSel(grc);
132 if ( pos>=grc->ltot || pos<0 )
133 return;
134 if ( grc->ltot>0 ) {
135 grc->ti[pos]->selected = sel;
136 _ggadget_redraw(g);
137 }
138 }
139
140 static void GRowColSelectOne(GGadget *g, int32_t pos) {
141 GRowCol *grc = (GRowCol *) g;
142
143 GRowColClearSel(grc);
144 if ( pos>=grc->ltot ) pos = grc->ltot-1;
145 if ( pos<0 ) pos = 0;
146 if ( grc->ltot>0 ) {
147 grc->ti[pos]->selected = true;
148 _ggadget_redraw(g);
149 }
150 }
151
152 static int32_t GRowColIsItemSelected(GGadget *g, int32_t pos) {
153 GRowCol *grc = (GRowCol *) g;
154
155 if ( pos>=grc->ltot )
156 return( false );
157 if ( pos<0 )
158 return( false );
159 if ( grc->ltot>0 )
160 return( grc->ti[pos]->selected );
161
162 return( false );
163 }
164
165 static int GRowColIndexFromPos(GRowCol *grc,int y) {
166 int i, height;
167
168 y -= grc->g.inner.y;
169 if ( y<0 ) y=0;
170 if ( y>=grc->g.inner.height ) y = grc->g.inner.height-1;
171 for ( i=grc->loff, height=0; i<grc->ltot; ++i ) {
172 int temp = GTextInfoGetHeight(grc->g.base,grc->ti[i],grc->font);
173 if ( height+temp>y )
174 break;
175 height += temp;
176 }
177 if ( i==grc->ltot )
178 return( -1 );
179 if ( grc->ti[i]->disabled )
180 return( -1 );
181 return( i );
182 }
183
184 static void GRowColScrollBy(GRowCol *grc,int loff,int xoff) {
185 int top = GRowColTopInWindow(grc,grc->ltot-1);
186 int ydiff, i;
187
188 if ( grc->loff + loff < 0 )
189 loff = -grc->loff;
190 else if ( grc->loff + loff > top )
191 loff = top-grc->loff;
192 if ( xoff+grc->xoff<0 )
193 xoff = -grc->xoff;
194 else if ( xoff+grc->xoff+grc->g.inner.width > grc->xmax ) {
195 xoff = grc->xmax-grc->g.inner.width-grc->xoff;
196 if ( xoff<0 ) xoff = 0;
197 }
198 if ( loff == 0 && xoff==0 )
199 return;
200
201 ydiff = 0;
202 if ( loff>0 ) {
203 for ( i=0; i<loff && ydiff<grc->g.inner.height; ++i )
204 ydiff += GTextInfoGetHeight(grc->g.base,grc->ti[i+grc->loff],grc->font);
205 } else if ( loff<0 ) {
206 for ( i=loff; i<0 && -ydiff<grc->g.inner.height; ++i )
207 ydiff -= GTextInfoGetHeight(grc->g.base,grc->ti[i+grc->loff],grc->font);
208 }
209 if ( !GDrawIsVisible(grc->g.base))
210 return;
211 GDrawForceUpdate(grc->g.base);
212 grc->loff += loff; grc->xoff += xoff;
213 if ( ydiff>=grc->g.inner.height || -ydiff >= grc->g.inner.height )
214 _ggadget_redraw(&grc->g);
215 else if ( ydiff!=0 || xoff!=0 )
216 GDrawScroll(grc->g.base,&grc->g.inner,xoff,ydiff);
217 if ( loff!=0 )
218 GScrollBarSetPos(&grc->vsb->g,grc->loff);
219 }
220
221 static int GRowColFindPosition(GRowCol *grc,unichar_t *text) {
222 GTextInfo temp, *ptemp=&temp;
223 int i, order;
224
225 if ( grc->orderer!=NULL ) {
226 memset(&temp,'\0',sizeof(temp));
227 temp.text = text;
228
229 /* I don't think I need to write a binary search here... */
230 for ( i=0; i<grc->ltot; ++i ) {
231 order = (grc->orderer)(&ptemp,&grc->ti[i]);
232 if (( order<= 0 && !grc->backwards ) || ( order>=0 && grc->backwards ))
233 return( i );
234 }
235 return( 0 );
236 } else {
237 for ( i=0; i<grc->ltot; ++i ) {
238 if (u_strmatch(text,grc->ti[i]->text)==0 )
239 return( i );
240 }
241 }
242 return( 0 );
243 }
244
245 static int GRowColAdjustPos(GGadget *g,int pos) {
246 GRowCol *grc = (GRowCol *) g;
247 int newoff = grc->loff;
248
249 if ( pos<grc->loff ) {
250 if (( newoff = pos-1)<0 ) newoff = 0;
251 if ( GRowColLinesInWindow(grc,newoff)<2 )
252 newoff = pos;
253 } else if ( pos >= grc->loff + GRowColLinesInWindow(grc,grc->loff) ) {
254 newoff = GRowColTopInWindow(grc,pos);
255 if ( pos!=grc->ltot-1 && GRowColLinesInWindow(grc,newoff+1)>=2 )
256 ++newoff;
257 }
258 return( newoff );
259 }
260
261 static void GRowColShowPos(GGadget *g,int pos) {
262 GRowCol *grc = (GRowCol *) g;
263 int newoff = GRowColAdjustPos(g,pos);
264 if ( newoff!=grc->loff )
265 GRowColScrollBy(grc,newoff-grc->loff,0);
266 }
267
268 static void GRowColScrollToText(GGadget *g,unichar_t *text,int sel) {
269 GRowCol *grc = (GRowCol *) g;
270 int pos;
271
272 pos = GRowColFindPosition(grc,text);
273 if ( sel && pos<grc->ltot ) {
274 GRowColClearSel(grc);
275 if ( grc->exactly_one || u_strmatch(text,grc->ti[pos]->text)==0 )
276 grc->ti[pos]->selected = true;
277 }
278 grc->loff = GRowColAdjustPos(g,pos);
279 _ggadget_redraw(g);
280 }
281
282 static void GRowColSetOrderer(GGadget *g,int (*orderer)(const void *, const void *)) {
283 GRowCol *grc = (GRowCol *) g;
284
285 grc->orderer = orderer;
286 if ( orderer!=NULL ) {
287 GRowColOrderIt(grc);
288 GRowColScrollBy(grc,-grc->loff,-grc->xoff);
289 _ggadget_redraw(&grc->g);
290 }
291 }
292
293 static int growcol_scroll(GGadget *g, GEvent *event);
294 static void GRowColCheckSB(GRowCol *grc) {
295 if ( GRowColLinesInWindow(grc,0)<grc->ltot ) {
296 if ( grc->vsb==NULL ) {
297 GGadgetData gd;
298 memset(&gd,'\0',sizeof(gd));
299 gd.pos.y = grc->g.r.y; gd.pos.height = grc->g.r.height;
300 gd.pos.width = GDrawPointsToPixels(grc->g.base,_GScrollBar_Width);
301 gd.pos.x = grc->g.r.x+grc->g.r.width - gd.pos.width;
302 gd.flags = gg_visible|gg_enabled|gg_pos_in_pixels|gg_sb_vert|gg_pos_use0;
303 gd.handle_controlevent = growcol_scroll;
304 grc->vsb = (GScrollBar *) GScrollBarCreate(grc->g.base,&gd,grc);
305 grc->vsb->g.contained = true;
306
307 gd.pos.width += GDrawPointsToPixels(grc->g.base,1);
308 grc->g.r.width -= gd.pos.width;
309 grc->g.inner.width -= gd.pos.width;
310 }
311 GScrollBarSetBounds(&grc->vsb->g,0,grc->ltot-1,GRowColLinesInWindow(grc,0));
312 GScrollBarSetPos(&grc->vsb->g,grc->loff);
313 } else {
314 if ( grc->vsb!=NULL ) {
315 int wid = grc->vsb->g.r.width + GDrawPointsToPixels(grc->g.base,1);
316 (grc->vsb->g.funcs->destroy)(&grc->vsb->g);
317 grc->vsb = NULL;
318 grc->g.r.width += wid;
319 grc->g.inner.width += wid;
320 }
321 }
322 }
323
324 static int GRowColFindXMax(GRowCol *grc) {
325 int i, width=0, temp;
326
327 for ( i=0; i<grc->ltot; ++i ) {
328 temp = GTextInfoGetWidth(grc->g.base,grc->ti[i],grc->font);
329 if ( temp>width ) width=temp;
330 }
331 grc->xmax = width;
332 return( width );
333 }
334
335 static void GRowColSetList(GGadget *g,GTextInfo **ti,int docopy) {
336 GRowCol *grc = (GRowCol *) g;
337 int same;
338
339 GTextInfoArrayFree(grc->ti);
340 if ( docopy || ti==NULL )
341 ti = GTextInfoArrayCopy(ti);
342 grc->ti = ti;
343 grc->ltot = GTextInfoArrayCount(ti);
344 if ( grc->orderer!=NULL )
345 GRowColOrderIt(grc);
346 grc->loff = grc->xoff = 0;
347 grc->hmax = GTextInfoGetMaxHeight(g->base,ti,grc->font,&same);
348 grc->sameheight = same;
349 GRowColCheckSB(grc);
350 _ggadget_redraw(&grc->g);
351 }
352
353 static void GRowColClear(GGadget *g) {
354 GRowColSetList(g,NULL,true);
355 }
356
357 static GTextInfo **GRowColGetList(GGadget *g,int32_t *len) {
358 GRowCol *grc = (GRowCol *) g;
359 if ( len!=NULL ) *len = grc->ltot;
360 return( grc->ti );
361 }
362
363 static GTextInfo *GRowColGetListItem(GGadget *g,int32_t pos) {
364 GRowCol *grc = (GRowCol *) g;
365 if ( pos<0 || pos>=grc->ltot )
366 return( NULL );
367
368 return(grc->ti[pos]);
369 }
370
371 static int growcol_expose(GWindow pixmap, GGadget *g, GEvent *event) {
372 GRowCol *grc = (GRowCol *) g;
373 GRect old1, old2;
374 Color fg, dfg;
375 int y, l, ymax, i;
376
377 if ( g->state == gs_invisible )
378 return( false );
379
380 GDrawPushClip(pixmap,&g->r,&old1);
381
382 GBoxDrawBackground(pixmap,&g->r,g->box, g->state,false);
383 if ( g->box->border_type!=bt_none ||
384 (g->box->flags&(box_foreground_border_inner|box_foreground_border_outer|box_active_border_inner))!=0 ) {
385 GBoxDrawBorder(pixmap,&g->r,g->box,g->state,false);
386
387 GDrawPushClip(pixmap,&g->inner,&old2);
388 }
389
390 fg = g->state==gs_disabled?g->box->disabled_foreground:g->box->main_foreground;
391 dfg = g->box->disabled_foreground;
392 y = g->inner.y;
393 ymax = g->inner.y+g->inner.height;
394 if ( ymax>event->u.expose.rect.y+event->u.expose.rect.height )
395 ymax = event->u.expose.rect.y+event->u.expose.rect.height;
396 if ( grc->labels!=NULL ) {
397 if ( y+grc->fh > event->u.expose.rect.y ) {
398 GRect r;
399 r = g->inner; r.height = fh;
400 GDrawFillRect(pixmap,&r,GRowColLabelBg);
401 for ( i=0; i<grc->cols; ++i ) {
402 GTextInfoDraw(pixmap,g->inner.x+grc->colx[i]+grc->hpad-grc->xoff,y,grc->labels[i],
403 grc->font,dfg,g->box->active_border
404 g->inner.y+g->inner.height, -1, -1);
405 }
406 }
407 y += grc->fh;
408 if ( grc->hrules )
409 GDrawDrawLine(pixmap,g->inner.x,y-1,g->inner.x+g->inner.width,y-1,fg);
410 }
411 for ( l = grc->loff; y<ymax && l<grc->rows; ++l ) {
412 if ( y+grc->fh > event->u.expose.rect.y ) {
413 for ( i=0; i<grc->cols; ++i ) {
414 if ( l!=grc->tfr || i!=grc->tfc ) {
415 GTextInfo *ti = grc->ti[l*grc->cols+i];
416 GTextInfoDraw(pixmap,g->inner.x+grc->colx[i]-grc->xoff+grc->hpad,y,ti,
417 grc->font,dfg,g->box->active_border,
418 g->inner.y+g->inner.height, -1, -1);
419 } else {
420 /* now we can draw the text field */
421 grc->tf->dontdraw = false;
422 (grc->tf->g.gfuncs->handle_expose)(pixmap,&grc->tf->g,event);
423 grc->tf->dontdraw = true;
424 }
425 }
426 }
427 y += grc->fh;
428 if ( grc->hrules )
429 GDrawDrawLine(pixmap,g->inner.x,y-1,g->inner.x+g->inner.width,y-1,fg);
430 }
431 if ( grc->vrules ) {
432 for ( i=1; i<grc->cols; ++i )
433 GDrawDrawLine(pixmap,grc->colx[i]-grc->xoff,g->inner.y,
434 grc->colx[i]-grc->xoff,g->inner.y+g->inner.height,fg);
435 }
436 if ( g->box->border_type!=bt_none ||
437 (g->box->flags&(box_foreground_border_inner|box_foreground_border_outer|box_active_border_inner))!=0 )
438 GDrawPopClip(pixmap,&old2);
439 GDrawPopClip(pixmap,&old1);
440 return( true );
441 }
442
443 static void growcol_scroll_selbymouse(GGadget *g, GEvent *event) {
444 int loff=0, xoff=0, pos;
445 GRowCol *grc = (GRowCol *) (g->data);
446
447 if ( event->u.mouse.y<grc->g.inner.y ) {
448 if ( grc->loff>0 ) loff = -1;
449 } else if ( event->u.mouse.y >= grc->g.inner.y+grc->g.inner.height ) {
450 int top = GRowColTopInWindow(grc,grc->ltot-1);
451 if ( grc->loff<top ) loff = 1;
452 }
453 if ( event->u.mouse.x<grc->g.inner.x ) {
454 xoff = -GDrawPointsToPixels(grc->g.base,6);
455 } else if ( event->u.mouse.x >= grc->g.inner.x+grc->g.inner.width ) {
456 xoff = GDrawPointsToPixels(grc->g.base,6);
457 }
458 GRowColScrollBy(grc,loff,xoff);
459 pos = GRowColIndexFromPos(grc,event->u.mouse.y);
460 if ( pos==-1 || pos == grc->end )
461 /* Do Nothing, nothing selectable */;
462 else if ( !grc->multiple_sel ) {
463 GRowColClearSel(grc);
464 grc->ti[pos]->selected = true;
465 grc->start = grc->end = pos;
466 _ggadget_redraw(&grc->g);
467 } else {
468 GRowColExpandSelection(grc,pos);
469 grc->end = pos;
470 _ggadget_redraw(&grc->g);
471 }
472 }
473
474 static int growcol_mouse(GGadget *g, GEvent *event) {
475 GRowCol *grc = (GRowCol *) g;
476 int pos;
477
478 if ( !g->takes_input || (g->state!=gs_active && g->state!=gs_enabled && g->state!=gs_focused))
479 return( false );
480
481 if ( event->type == et_crossing )
482 return( false );
483 if ( event->type==et_mousemove && !grc->pressed && !grc->parentpressed ) {
484 if ( GGadgetWithin(g,event->u.mouse.x,event->u.mouse.y) && g->popup_msg )
485 GGadgetPreparePopup(g->base,g->popup_msg);
486 return( true );
487 } else if ( event->type==et_mouseup && grc->parentpressed &&
488 !GGadgetInnerWithin(&grc->g,event->u.mouse.x,event->u.mouse.y)) {
489 grc->parentpressed = false;
490 GDrawPointerUngrab(GDrawGetDisplayOfWindow(grc->g.base));
491 } else if ( event->type==et_mousemove && grc->parentpressed &&
492 GGadgetInnerWithin(&grc->g,event->u.mouse.x,event->u.mouse.y)) {
493 if ( grc->pressed == NULL )
494 grc->pressed = GDrawRequestTimer(g->base,GRowColScrollTime,GRowColScrollTime,NULL);
495 GDrawPointerUngrab(GDrawGetDisplayOfWindow(grc->g.base));
496 grc->parentpressed = false;
497 growcol_scroll_selbymouse(grc,event);
498 return( true );
499 } else if ( event->type==et_mousemove && grc->pressed ) {
500 growcol_scroll_selbymouse(grc,event);
501 return( true );
502 } else if ( event->type==et_mousedown ) {
503 if ( grc->pressed == NULL )
504 grc->pressed = GDrawRequestTimer(g->base,GRowColScrollTime,GRowColScrollTime,NULL);
505 pos = GRowColIndexFromPos(grc,event->u.mouse.y);
506 if ( pos==-1 )
507 return( true ); /* Do Nothing, nothing selectable */
508 else if ( !grc->exactly_one && grc->ti[pos]->selected &&
509 (event->u.mouse.state&(ksm_control|ksm_shift))) {
510 grc->ti[pos]->selected = false;
511 } else if ( !grc->multiple_sel ||
512 !(event->u.mouse.state&(ksm_control|ksm_shift))) {
513 GRowColClearSel(grc);
514 grc->ti[pos]->selected = true;
515 grc->start = grc->end = pos;
516 } else if ( event->u.mouse.state&ksm_control ) {
517 grc->ti[pos]->selected = !grc->ti[pos]->selected;
518 grc->start = grc->end = pos;
519 } else if ( event->u.mouse.state&ksm_shift ) {
520 GRowColExpandSelection(grc,pos);
521 }
522 _ggadget_redraw(&grc->g);
523 } else if ( event->type==et_mouseup && grc->pressed ) {
524 GDrawCancelTimer(grc->pressed); grc->pressed = NULL;
525 if ( GGadgetInnerWithin(&grc->g,event->u.mouse.x,event->u.mouse.y) ) {
526 growcol_scroll_selbymouse(grc,event);
527 if ( event->u.mouse.clicks==2 )
528 GRowColDoubleClick(grc);
529 else
530 GRowColSelected(grc);
531 }
532 } else
533 return( false );
534
535 return( true );
536 }
537
538 static int growcol_key(GGadget *g, GEvent *event) {
539 GRowCol *grc = (GRowCol *) g;
540 uint16_t keysym = event->u.chr.keysym;
541 int sofar_pos = grc->sofar_pos;
542 int loff, xoff, sel=-1;
543 int refresh = false;
544
545 if ( event->type == et_charup )
546 return( false );
547 if ( !g->takes_input || (g->state!=gs_enabled && g->state!=gs_active && g->state!=gs_focused ))
548 return(false );
549
550 if ( grc->ispopup && event->u.chr.keysym == GK_Return ) {
551 GRowColDoubleClick(grc);
552 return( true );
553 }
554
555 if ( event->u.chr.keysym == GK_Return || event->u.chr.keysym == GK_Tab ||
556 event->u.chr.keysym == GK_BackTab || event->u.chr.keysym == GK_Escape )
557 return( false );
558
559 GDrawCancelTimer(grc->enduser); grc->enduser = NULL; grc->sofar_pos = 0;
560
561 loff = 0x80000000; xoff = 0x80000000; sel = -1;
562 if ( keysym == GK_Home || keysym == GK_KP_Home || keysym == GK_Begin || keysym == GK_KP_Begin ) {
563 loff = -grc->loff;
564 xoff = -grc->xoff;
565 sel = 0;
566 } else if ( keysym == GK_End || keysym == GK_KP_End ) {
567 loff = GRowColTopInWindow(grc,grc->ltot-1)-grc->loff;
568 xoff = -grc->xoff;
569 sel = grc->ltot-1;
570 } else if ( keysym == GK_Up || keysym == GK_KP_Up ) {
571 if (( sel = GRowColGetFirstSelPos(&grc->g)-1 )<0 ) {
572 /*if ( grc->loff!=0 ) loff = -1; else loff = 0;*/
573 sel = 0;
574 }
575 } else if ( keysym == GK_Down || keysym == GK_KP_Down ) {
576 if (( sel = GRowColGetFirstSelPos(&grc->g))!= -1 )
577 ++sel;
578 else
579 /*if ( grc->loff + GRowColLinesInWindow(grc,grc->loff)<grc->ltot ) loff = 1; else loff = 0;*/
580 sel = 0;
581 } else if ( keysym == GK_Left || keysym == GK_KP_Left ) {
582 xoff = -GDrawPointsToPixels(grc->g.base,6);
583 } else if ( keysym == GK_Right || keysym == GK_KP_Right ) {
584 xoff = GDrawPointsToPixels(grc->g.base,6);
585 } else if ( keysym == GK_Page_Up || keysym == GK_KP_Page_Up ) {
586 loff = GRowColTopInWindow(grc,grc->loff);
587 if ( loff == grc->loff ) /* Normally we leave one line in window from before, except if only one line fits */
588 loff = GRowColTopInWindow(grc,grc->loff-1);
589 loff -= grc->loff;
590 if (( sel = GRowColGetFirstSelPos(&grc->g))!= -1 ) {
591 if (( sel += loff )<0 ) sel = 0;
592 }
593 } else if ( keysym == GK_Page_Down || keysym == GK_KP_Page_Down ) {
594 loff = GRowColLinesInWindow(grc,grc->loff)-1;
595 if ( loff<=0 ) loff = 1;
596 if ( loff + grc->loff >= grc->ltot )
597 loff = GRowColTopInWindow(grc,grc->ltot-1)-grc->loff;
598 if (( sel = GRowColGetFirstSelPos(&grc->g))!= -1 ) {
599 if (( sel += loff )>=grc->ltot ) sel = grc->ltot-1;
600 }
601 } else if ( keysym == GK_BackSpace && grc->orderer ) {
602 /* ordered lists may be reversed by typing backspace */
603 grc->backwards = !grc->backwards;
604 GRowColOrderIt(grc);
605 sel = GRowColGetFirstSelPos(&grc->g);
606 if ( sel!=-1 ) {
607 int top = GRowColTopInWindow(grc,grc->ltot-1);
608 grc->loff = sel-1;
609 if ( grc->loff > top )
610 grc->loff = top;
611 if ( sel-1<0 )
612 grc->loff = 0;
613 }
614 GScrollBarSetPos(&grc->vsb->g,grc->loff);
615 _ggadget_redraw(&grc->g);
616 return( true );
617 } else if ( event->u.chr.chars[0]!='\0' && grc->orderer ) {
618 int len = u_strlen(event->u.chr.chars);
619 if ( sofar_pos+len >= grc->sofar_max ) {
620 if ( grc->sofar_max == 0 )
621 grc->sofar = malloc((grc->sofar_max = len+10) * sizeof(unichar_t));
622 else
623 grc->sofar = realloc(grc->sofar,(grc->sofar_max = sofar_pos+len+10)*sizeof(unichar_t));
624 }
625 u_strcpy(grc->sofar+sofar_pos,event->u.chr.chars);
626 grc->sofar_pos = sofar_pos + len;
627 sel = GRowColFindPosition(grc,grc->sofar);
628 grc->enduser = GDrawRequestTimer(grc->g.base,GRowColTypeTime,0,NULL);
629 }
630
631 if ( loff==0x80000000 && sel>=0 ) {
632 if ( sel>=grc->ltot ) sel = grc->ltot-1;
633 if ( sel<grc->loff ) loff = sel-grc->loff;
634 else if ( sel>=grc->loff+GRowColLinesInWindow(grc,grc->loff) )
635 loff = sel-(grc->loff+GRowColLinesInWindow(grc,grc->loff)-1);
636 } else
637 sel = -1;
638 if ( sel!=-1 ) {
639 int wassel = grc->ti[sel]->selected;
640 refresh = GRowColAnyOtherSels(grc,sel) || !wassel;
641 GRowColSelectOne(&grc->g,sel);
642 if ( refresh )
643 GRowColSelected(grc);
644 }
645 if ( loff!=0x80000000 || xoff!=0x80000000 ) {
646 if ( loff==0x80000000 ) loff = 0;
647 if ( xoff==0x80000000 ) xoff = 0;
648 GRowColScrollBy(grc,loff,xoff);
649 }
650 if ( refresh )
651 _ggadget_redraw(g);
652 if ( loff!=0x80000000 || xoff!=0x80000000 || sel!=-1 )
653 return( true );
654
655 return( false );
656 }
657
658 static int growcol_timer(GGadget *g, GEvent *event) {
659 GRowCol *grc = (GRowCol *) g;
660
661 if ( event->u.timer.timer == grc->enduser ) {
662 grc->enduser = NULL;
663 grc->sofar_pos = 0;
664 return( true );
665 } else if ( event->u.timer.timer == grc->pressed ) {
666 GEvent e;
667 e.type = et_mousemove;
668 GDrawGetPointerPosition(g->base,&e);
669 if ( e.u.mouse.x<g->inner.x || e.u.mouse.y <g->inner.y ||
670 e.u.mouse.x >= g->inner.x + g->inner.width ||
671 e.u.mouse.y >= g->inner.y + g->inner.height )
672 growcol_scroll_selbymouse(grc,&e);
673 return( true );
674 }
675 return( false );
676 }
677
678 static int growcol_scroll(GGadget *g, GEvent *event) {
679 int loff = 0;
680 enum sb sbt = event->u.control.u.sb.type;
681 GRowCol *grc = (GRowCol *) g;
682
683 if ( sbt==et_sb_top )
684 loff = -grc->loff;
685 else if ( sbt==et_sb_bottom )
686 loff = GRowColTopInWindow(grc,grc->ltot-1)-grc->loff;
687 else if ( sbt==et_sb_up ) {
688 if ( grc->loff!=0 ) loff = -1; else loff = 0;
689 } else if ( sbt==et_sb_down ) {
690 if ( grc->loff + GRowColLinesInWindow(grc,grc->loff)<grc->ltot ) loff = 1; else loff = 0;
691 } else if ( sbt==et_sb_uppage ) {
692 loff = GRowColTopInWindow(grc,grc->loff);
693 if ( loff == grc->loff ) /* Normally we leave one line in window from before, except if only one line fits */
694 loff = GRowColTopInWindow(grc,grc->loff-1);
695 loff -= grc->loff;
696 } else if ( sbt==et_sb_downpage ) {
697 loff = GRowColLinesInWindow(grc,grc->loff)-1;
698 if ( loff<=0 ) loff = 1;
699 if ( loff + grc->loff >= grc->ltot )
700 loff = GRowColTopInWindow(grc,grc->ltot-1)-grc->loff;
701 } else /* if ( sbt==et_sb_thumb || sbt==et_sb_thumbrelease ) */ {
702 loff = event->u.control.u.sb.pos - grc->loff;
703 }
704 GRowColScrollBy(grc,loff,0);
705 return( true );
706 }
707
708 static void GRowCol_destroy(GGadget *g) {
709 GRowCol *grc = (GRowCol *) g;
710
711 if ( grc==NULL )
712 return;
713 GDrawCancelTimer(grc->enduser);
714 GDrawCancelTimer(grc->pressed);
715 if ( grc->freeti )
716 GTextInfoArrayFree(grc->ti);
717 free(grc->sofar);
718 if ( grc->vsb!=NULL )
719 (grc->vsb->g.funcs->destroy)(&grc->vsb->g);
720 _ggadget_destroy(g);
721 }
722
723 static void GRowColSetFont(GGadget *g,FontInstance *new) {
724 GRowCol *b = (GRowCol *) g;
725 b->font = new;
726 }
727
728 static FontInstance *GRowColGetFont(GGadget *g) {
729 GRowCol *b = (GRowCol *) g;
730 return( b->font );
731 }
732
733 static void growcol_redraw(GGadget *g) {
734 GRowCol *grc = (GRowCol *) g;
735 if ( grc->vsb!=NULL )
736 _ggadget_redraw((GGadget *) (grc->vsb));
737 _ggadget_redraw(g);
738 }
739
740 static void growcol_move(GGadget *g, int32_t x, int32_t y ) {
741 GRowCol *grc = (GRowCol *) g;
742 if ( grc->vsb!=NULL )
743 _ggadget_move((GGadget *) (grc->vsb),x+(grc->vsb->g.r.x-g->r.x),y);
744 _ggadget_move(g,x,y);
745 }
746
747 static void growcol_resize(GGadget *g, int32_t width, int32_t height ) {
748 GRowCol *grc = (GRowCol *) g;
749 if ( grc->vsb!=NULL ) {
750 int oldwidth = grc->vsb->g.r.x+grc->vsb->g.r.width-g->r.x;
751 _ggadget_move((GGadget *) (grc->vsb),grc->vsb->g.r.x+width-oldwidth,grc->vsb->g.r.y);
752 _ggadget_resize(g,width-(oldwidth-g->r.width), height);
753 _ggadget_resize((GGadget *) (grc->vsb),grc->vsb->g.r.width,height);
754 } else
755 _ggadget_resize(g,width,height);
756 }
757
758 static GRect *growcol_getsize(GGadget *g, GRect *r ) {
759 GRowCol *grc = (GRowCol *) g;
760 _ggadget_getsize(g,r);
761 if ( grc->vsb!=NULL )
762 r->width = grc->vsb->g.r.x+grc->vsb->g.r.width-g->r.x;
763 return( r );
764 }
765
766 static void growcol_setvisible(GGadget *g, int visible ) {
767 GRowCol *grc = (GRowCol *) g;
768 if ( grc->vsb!=NULL ) _ggadget_setvisible(&grc->vsb->g,visible);
769 _ggadget_setvisible(g,visible);
770 }
771
772 static void growcol_setenabled(GGadget *g, int enabled ) {
773 GRowCol *grc = (GRowCol *) g;
774 if ( grc->vsb!=NULL ) _ggadget_setenabled(&grc->vsb->g,enabled);
775 _ggadget_setenabled(g,enabled);
776 }
777
778 struct gfuncs GRowCol_funcs = {
779 0,
780 sizeof(struct gfuncs),
781
782 growcol_expose,
783 growcol_mouse,
784 growcol_key,
785 NULL,
786 NULL,
787 growcol_timer,
788 NULL,
789
790 growcol_redraw,
791 growcol_move,
792 growcol_resize,
793 growcol_setvisible,
794 growcol_setenabled,
795 growcol_getsize,
796 _ggadget_getinnersize,
797
798 GRowCol_destroy,
799
800 NULL,
801 NULL,
802 NULL,
803 NULL,
804 NULL,
805 GRowColSetFont,
806 GRowColGetFont,
807
808 GRowColClear,
809 GRowColSetList,
810 GRowColGetList,
811 GRowColGetListItem,
812 GRowColSelect,
813 GRowColSelectOne,
814 GRowColIsItemSelected,
815 GRowColGetFirstSelPos,
816 GRowColShowPos,
817 GRowColScrollToText,
818 GRowColSetOrderer,
819
820 NULL,
821 NULL,
822 NULL,
823 NULL
824 };
825
826 static GBox list_box = { bt_lowered, bs_rect, 2, 2, 3, box_foreground_border_outer, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
827 static FontInstance *list_font = NULL;
828 static int growcol_inited = false;
829
830 static void GRowColInit() {
831 list_box.main_background = 0xc0c0c0;
832 list_font = _GGadgetInitDefaultBox("GRowCol.",&list_box);
833 growcol_inited = true;
834 }
835
836 static void GRowColFit(GRowCol *grc) {
837 int width=0, height=0;
838 int bp = GBoxBorderWidth(grc->g.base,grc->g.box);
839 int i;
840
841 /* can't deal with elliptical scrolling lists nor diamond ones. Just rects and roundrects */
842 GRowColFindXMax(grc);
843 if ( grc->g.r.width==0 ) {
844 if ( width==0 ) width = GDrawPointsToPixels(grc->g.base,100);
845 width += GDrawPointsToPixels(grc->g.base,_GScrollBar_Width) +
846 GDrawPointsToPixels(grc->g.base,1);
847 grc->g.r.width = width + 2*bp;
848 }
849 if ( grc->g.r.height==0 ) {
850 for ( i=0; i<grc->ltot && i<5; ++i ) {
851 height += GTextInfoGetHeight(grc->g.base,grc->ti[i],grc->font);
852 }
853 if ( i<5 ) {
854 int as, ds, ld;
855 GDrawWindowFontMetrics(grc->g.base,grc->font,&as, &ds, &ld);
856 height += (5-i)*(as+ds);
857 }
858 grc->g.r.height = height + 2*bp;
859 }
860 grc->g.inner = grc->g.r;
861 grc->g.inner.x += bp; grc->g.inner.y += bp;
862 grc->g.inner.width -= 2*bp; grc->g.inner.height -= 2*bp;
863 GRowColCheckSB(grc);
864 }
865
866 static GRowCol *_GRowColCreate(GRowCol *grc, struct gwindow *base, GGadgetData *gd,void *data, GBox *def) {
867 int same;
868
869 if ( !growcol_inited )
870 GRowColInit();
871 grc->g.funcs = &GRowCol_funcs;
872 _GGadget_Create(&grc->g,base,gd,data,def);
873 grc->font = list_font;
874 grc->g.takes_input = grc->g.takes_keyboard = true; grc->g.focusable = true;
875
876 if ( !(gd->flags & gg_list_internal ) ) {
877 grc->ti = GTextInfoArrayFromList(gd->u.list,&grc->ltot);
878 grc->freeti = true;
879 } else {
880 grc->ti = (GTextInfo **) (gd->u.list);
881 grc->ltot = GTextInfoArrayCount(grc->ti);
882 }
883 grc->hmax = GTextInfoGetMaxHeight(grc->g.base,grc->ti,grc->font,&same);
884 grc->sameheight = same;
885 if ( gd->flags & gg_list_alphabetic ) {
886 grc->orderer = GRowColAlphaCompare;
887 GRowColOrderIt(grc);
888 }
889 grc->start = grc->end = -1;
890 if ( gd->flags & gg_list_multiplesel )
891 grc->multiple_sel = true;
892 else if ( gd->flags & gg_list_exactlyone ) {
893 int sel = GRowColGetFirstSelPos(&grc->g);
894 grc->exactly_one = true;
895 if ( sel==-1 ) sel = 0;
896 GRowColClearSel(grc);
897 if ( grc->ltot>0 ) grc->ti[sel]->selected = true;
898 }
899
900 GRowColFit(grc);
901 _GGadget_FinalPosition(&grc->g,base,gd);
902
903 if ( gd->flags & gg_group_end )
904 _GGadgetCloseGroup(&grc->g);
905 GWidgetIndicateFocusGadget(&grc->g);
906 return( grc );
907 }
908
909 GGadget *GRowColCreate(struct gwindow *base, GGadgetData *gd,void *data) {
910 GRowCol *grc = _GRowColCreate(calloc(1,sizeof(GRowCol)),base,gd,data,&list_box);
911
912 return( &grc->g );
913 }