Debian Codegraph @

gdraw/gmatrixedit.c

raw symbols SCIP
1 /* Copyright (C) 2006-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 "ffglib.h"
31 #include "gdraw.h"
32 #include "gdrawP.h"
33 #include "ggadgetP.h"
34 #include "gkeysym.h"
35 #include "gwidget.h"
36 #include "ustring.h"
37
38 #define DEL_SPACE 6
39
40 static GBox gmatrixedit_box = GBOX_EMPTY; /* Don't initialize here */
41 static GBox gmatrixedit_button_box = GBOX_EMPTY; /* Don't initialize here */
42 static GResFont gmatrixedit_font = GRESFONT_INIT("400 12pt " MONO_UI_FAMILIES);
43 static GResFont gmatrixedit_titfont = GRESFONT_INIT("400 8pt " SANS_UI_FAMILIES);
44 static Color gmatrixedit_title_bg = 0x808080, gmatrixedit_title_fg = 0x000000, gmatrixedit_title_divider = 0xffffff;
45 static Color gmatrixedit_rules = 0x000000;
46 static Color gmatrixedit_frozencol = 0xff0000,
47 gmatrixedit_activecol = 0x0000ff, gmatrixedit_activebg=0xffffc0;
48
49 static struct resed gmatrixedit_re[] = {
50 {N_("Title Background"), "TitleBG", rt_color, &gmatrixedit_title_bg, N_("Background color of column headers at the top of a matrix edit"), NULL, { 0 }, 0, 0 },
51 {N_("Title Text Color"), "TitleFG", rt_color, &gmatrixedit_title_fg, N_("Text color of column headers at the top of a matrix edit"), NULL, { 0 }, 0, 0 },
52 {N_("Title Divider Color"), "TitleDivider", rt_color, &gmatrixedit_title_divider, N_("Color of column dividers in the title section of a matrix edit"), NULL, { 0 }, 0, 0 },
53 {N_("Rule Color"), "RuleCol", rt_color, &gmatrixedit_rules, N_("Color of column dividers in the main section of a matrix edit"), NULL, { 0 }, 0, 0 },
54 {N_("Frozen Color"), "FrozenCol", rt_color, &gmatrixedit_frozencol, N_("Color of frozen (unchangeable) entries in the main section of a matrix edit"), NULL, { 0 }, 0, 0 },
55 {N_("Active Color"), "ActiveCol", rt_color, &gmatrixedit_activecol, N_("Color of the active entry in the main section of a matrix edit"), NULL, { 0 }, 0, 0 },
56 {N_("Active Background"), "ActiveBG", rt_color, &gmatrixedit_activebg, N_("Background color of the active entry in the main section of a matrix edit"), NULL, { 0 }, 0, 0 },
57 {N_("Title Font"), "TitleFont", rt_font, &gmatrixedit_titfont, N_("Font used to draw titles of a matrix edit"), NULL, { 0 }, 0, 0 },
58 RESED_EMPTY
59 };
60 static GResInfo gmatrixedit2_ri;
61 GResInfo gmatrixedit_ri = {
62 &gmatrixedit2_ri, &ggadget_ri, &gmatrixedit2_ri,NULL,
63 &gmatrixedit_box,
64 &gmatrixedit_font,
65 NULL,
66 NULL,
67 N_("Matrix Edit"),
68 N_("Matrix Edit (sort of like a spreadsheet)"),
69 "GMatrixEdit",
70 "Gdraw",
71 false,
72 false,
73 omf_border_type|omf_border_width|omf_border_shape|omf_padding|
74 omf_main_background|omf_disabled_background,
75 { bt_none, bs_rect, 0, 0, 0, 0, 0, 0, 0, 0, COLOR_TRANSPARENT, 0, COLOR_TRANSPARENT, 0, 0, 0, 0, 0, 0 },
76 GBOX_EMPTY,
77 NULL,
78 NULL,
79 NULL
80 };
81 static GResInfo gmatrixedit_button_ri;
82 static GResInfo gmatrixedit2_ri = {
83 &gmatrixedit_button_ri, &ggadget_ri, &gmatrixedit_ri,NULL,
84 NULL,
85 NULL,
86 NULL,
87 gmatrixedit_re,
88 N_("Matrix Edit Continued"),
89 N_("Matrix Edit (sort of like a spreadsheet)"),
90 "GMatrixEdit",
91 "Gdraw",
92 false,
93 false,
94 0,
95 GBOX_EMPTY,
96 GBOX_EMPTY,
97 NULL,
98 NULL,
99 NULL
100 };
101
102 static int GMatrixEditButtonRIInit(GResInfo *ri) {
103 if ( ri->is_initialized )
104 return false;
105 GResEditDoInit(&gmatrixedit_ri);
106 ri->overrides.main_background = gmatrixedit_ri.boxdata->main_background;
107 ri->overrides.disabled_background = gmatrixedit_ri.boxdata->disabled_background;
108 return _GResEditInitialize(ri);
109 }
110 extern GResInfo gdrawable_ri;
111 static GResInfo gmatrixedit_button_ri = {
112 &gdrawable_ri, &ggadget_ri, NULL,NULL,
113 &gmatrixedit_button_box,
114 NULL,
115 NULL,
116 NULL,
117 N_("Matrix Edit Button"),
118 N_("Button in Matrix Edit"),
119 "GMatrixEditButton",
120 "Gdraw",
121 false,
122 false,
123 omf_border_width|omf_main_background|omf_disabled_background|box_foreground_border_inner,
124 { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
125 GBOX_EMPTY,
126 NULL,
127 GMatrixEditButtonRIInit,
128 NULL
129 };
130
131 static void _GMatrixEdit_Init(void) {
132 GResEditDoInit(&gmatrixedit_ri);
133 GResEditDoInit(&gmatrixedit2_ri);
134 GResEditDoInit(&gmatrixedit_button_ri);
135 }
136
137 static void MatrixDataFree(GMatrixEdit *gme) {
138 int r,c;
139
140 for ( r=0; r<gme->rows; ++r ) for ( c=0; c<gme->cols; ++c ) {
141 if ( gme->col_data[c].me_type == me_string ||
142 gme->col_data[c].me_type == me_bigstr ||
143 gme->col_data[c].me_type == me_stringchoice ||
144 gme->col_data[c].me_type == me_stringchoicetrans ||
145 gme->col_data[c].me_type == me_stringchoicetag ||
146 gme->col_data[c].me_type == me_funcedit ||
147 gme->col_data[c].me_type == me_onlyfuncedit ||
148 gme->col_data[c].me_type == me_button ||
149 gme->col_data[c].me_type == me_func )
150 free( gme->data[r*gme->cols+c].u.md_str );
151 }
152 free( gme->data );
153 }
154
155 static void GMatrixEdit_destroy(GGadget *g) {
156 GMatrixEdit *gme = (GMatrixEdit *) g;
157 int c, i;
158
159 free(gme->newtext);
160 /* The textfield gme->tf lives in the nested window and doesn't need to be destroyed */
161 if ( gme->vsb!=NULL )
162 GGadgetDestroy(gme->vsb);
163 if ( gme->hsb!=NULL )
164 GGadgetDestroy(gme->hsb);
165 if ( gme->del!=NULL )
166 GGadgetDestroy(gme->del);
167 if ( gme->up!=NULL )
168 GGadgetDestroy(gme->up);
169 if ( gme->down!=NULL )
170 GGadgetDestroy(gme->down);
171 if ( gme->buttonlist!=NULL )
172 for ( i=0; gme->buttonlist[i]!=NULL; ++i )
173 GGadgetDestroy(gme->buttonlist[i]);
174 if ( gme->nested!=NULL ) {
175 GDrawSetUserData(gme->nested,NULL);
176 GDrawDestroyWindow(gme->nested);
177 }
178
179 MatrixDataFree(gme); /* Uses col data */
180
181 for ( c=0; c<gme->cols; ++c ) {
182 if ( gme->col_data[c].enum_vals!=NULL )
183 GMenuItemArrayFree(gme->col_data[c].enum_vals);
184 free( gme->col_data[c].title );
185 }
186 free( gme->col_data );
187
188 _ggadget_destroy(g);
189 }
190
191 static void GME_FixScrollBars(GMatrixEdit *gme) {
192 int width;
193 int lastc;
194 int pagesize = gme->vsb->r.height/(gme->fh+gme->vpad);
195 if ( pagesize<=0 ) pagesize=1;
196
197 /* Editable matrixedits need one extra line, for the <New> button */
198 GScrollBarSetBounds(gme->vsb,0,gme->rows+!gme->no_edit,pagesize);
199 for (lastc=gme->cols-1; lastc>=0 && gme->col_data[lastc].hidden; lastc--);
200 width = gme->col_data[lastc].x + gme->col_data[lastc].width;
201 GScrollBarSetBounds(gme->hsb,0,width,gme->hsb->r.width);
202 }
203
204 static void GMatrixEdit_Move(GGadget *g, int32_t x, int32_t y) {
205 GMatrixEdit *gme = (GMatrixEdit *) g;
206 int i;
207
208 /* I don't need to move the textfield because it is */
209 /* nested within the window, and I'm moving the window */
210 if ( gme->vsb!=NULL )
211 _ggadget_move((GGadget *) (gme->vsb),x+(gme->vsb->r.x-g->r.x),
212 y+(gme->vsb->r.y-g->r.y));
213 if ( gme->hsb!=NULL )
214 _ggadget_move((GGadget *) (gme->hsb),x+(gme->hsb->r.x-g->r.x),
215 y+(gme->hsb->r.y-g->r.y));
216 if ( gme->del!=NULL )
217 _ggadget_move((GGadget *) (gme->del),x+(gme->del->r.x-g->r.x),
218 y+(gme->del->r.y-g->r.y));
219 if ( gme->up!=NULL )
220 _ggadget_move((GGadget *) (gme->up),x+(gme->up->r.x-g->r.x),
221 y+(gme->up->r.y-g->r.y));
222 if ( gme->down!=NULL )
223 _ggadget_move((GGadget *) (gme->down),x+(gme->down->r.x-g->r.x),
224 y+(gme->down->r.y-g->r.y));
225 if ( gme->buttonlist!=NULL )
226 for ( i=0; gme->buttonlist[i]!=NULL; ++i )
227 if ( gme->buttonlist[i]!=NULL )
228 _ggadget_move((GGadget *) (gme->buttonlist[i]),x+(gme->buttonlist[i]->r.x-g->r.x),
229 y+(gme->buttonlist[i]->r.y-g->r.y));
230 GDrawMove(gme->nested,x+(g->inner.x-g->r.x),y+(g->inner.y-g->r.y+
231 (gme->has_titles?gme->fh:0)));
232 _ggadget_move(g,x,y);
233 }
234
235 static GMenuItem *FindMi(GMenuItem *mi, intptr_t val ) {
236 int i;
237
238 for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.line; ++i ) {
239 if ( mi[i].ti.userdata == (void *) (intptr_t) val && mi[i].ti.text!=NULL )
240 return( &mi[i] );
241 }
242 return( NULL );
243 }
244
245 static int GME_ColWidth(GMatrixEdit *gme, int c) {
246 int width=0, max, cur;
247 FontInstance *old = GDrawSetFont(gme->g.base,gme->font);
248 char *str, *pt;
249 int r;
250 GMenuItem *mi;
251
252 if ( gme->col_data[c].hidden )
253 return( 0 );
254 switch ( gme->col_data[c].me_type ) {
255 case me_int:
256 width = GDrawGetText8Width(gme->g.base,"1234", -1);
257 break;
258 case me_hex: case me_addr:
259 width = GDrawGetText8Width(gme->g.base,"0xFFFF", -1);
260 break;
261 case me_uhex:
262 width = GDrawGetText8Width(gme->g.base,"U+FFFF", -1);
263 break;
264 case me_real:
265 width = GDrawGetText8Width(gme->g.base,"1.234567", -1);
266 break;
267 case me_enum:
268 max = 0;
269 for ( r=0; r<gme->rows; ++r ) {
270 mi = FindMi(gme->col_data[c].enum_vals,gme->data[r*gme->cols+c].u.md_ival);
271 if ( mi!=NULL ) {
272 if ( mi->ti.text_is_1byte )
273 cur = GDrawGetText8Width(gme->g.base,(char *)mi->ti.text,-1);
274 else
275 cur = GDrawGetTextWidth(gme->g.base,mi->ti.text,-1);
276 if ( cur>max ) max = cur;
277 }
278 }
279 cur = 6 * GDrawGetText8Width(gme->g.base,"n", 1);
280 if ( max<cur )
281 max = cur;
282 width = max;
283 break;
284 case me_func:
285 case me_button:
286 case me_stringchoice: case me_stringchoicetrans: case me_stringchoicetag:
287 case me_funcedit:
288 case me_onlyfuncedit:
289 case me_string: case me_bigstr:
290 max = 0;
291 for ( r=0; r<gme->rows; ++r ) {
292 char *freeme = NULL;
293 str = gme->data[r*gme->cols+c].u.md_str;
294 if ( str==NULL && gme->col_data[c].me_type==me_func )
295 str = freeme = (gme->col_data[c].func)(&gme->g,r,c);
296 if ( str==NULL )
297 continue;
298 /* use the maximum width of 40 characters to avoid insanely wide
299 * cells and horizontal scrollbars, the magic number 40 is the max
300 * length of characters after which we use GME_StrBigEdit below */
301 char buf[1024];
302 utf8_strncpy(buf, str, 40);
303 pt = strchr(buf,'\n');
304 cur = GDrawGetText8Width(gme->g.base,buf, pt==NULL ? -1: pt-buf);
305 if ( cur>max ) max = cur;
306 free(freeme);
307 }
308 if ( max < 10*GDrawGetText8Width(gme->g.base,"n", 1) )
309 width = 10*GDrawGetText8Width(gme->g.base,"n", 1);
310 else
311 width = max;
312 if ( gme->col_data[c].me_type==me_stringchoice ||
313 gme->col_data[c].me_type==me_stringchoicetrans ||
314 gme->col_data[c].me_type==me_stringchoicetag ||
315 gme->col_data[c].me_type==me_onlyfuncedit ||
316 gme->col_data[c].me_type==me_funcedit )
317 width += gme->mark_size + gme->mark_skip;
318 break;
319 default:
320 width = 0;
321 break;
322 }
323 if ( gme->col_data[c].title!=NULL ) {
324 GDrawSetFont(gme->g.base,gme->titfont);
325 cur = GDrawGetText8Width(gme->g.base,gme->col_data[c].title, -1);
326 if ( cur>width ) width = cur;
327 }
328 GDrawSetFont(gme->g.base,old);
329 if ( width>0x7fff )
330 width = 0x7fff;
331 return( width );
332 }
333
334 static int GME_AdjustCol(GMatrixEdit *gme,int col) {
335 int new_width, x,c, changed;
336 int orig_width, min_width;
337 int lastc;
338
339 changed = false;
340 if ( col==-1 ) {
341 for ( c=0; c<gme->cols; ++c ) if ( !gme->col_data[c].fixed ) {
342 new_width = GME_ColWidth(gme,c);
343 if ( new_width!=gme->col_data[c].width ) {
344 gme->col_data[c].width = new_width;
345 changed = true;
346 }
347 }
348 col = 0;
349 } else if ( !gme->col_data[col].fixed ) {
350 new_width = GME_ColWidth(gme,col);
351 if ( new_width!=gme->col_data[col].width ) {
352 gme->col_data[col].width = new_width;
353 changed = true;
354 }
355 }
356 if ( changed ) {
357 x = gme->col_data[col].x;
358 for ( c=col; c<gme->cols; ++c ) {
359 gme->col_data[c].x = x;
360 if ( !gme->col_data[c].hidden )
361 x += gme->col_data[c].width + gme->hpad;
362 }
363 }
364 for ( lastc=gme->cols-1; lastc>0 && gme->col_data[lastc].hidden; --lastc );
365 if ( !gme->col_data[lastc].fixed ) {
366 orig_width = gme->col_data[lastc].width;
367 min_width = GME_ColWidth(gme,lastc);
368 gme->col_data[lastc].width = (gme->g.inner.width-gme->vsb->r.width-gme->hpad-gme->col_data[lastc].x);
369 if ( gme->col_data[lastc].width<min_width )
370 gme->col_data[lastc].width = min_width;
371 if ( gme->col_data[lastc].width != orig_width )
372 changed = true;
373 }
374
375 if ( changed ) {
376 GME_FixScrollBars(gme);
377 GDrawRequestExpose(gme->nested,NULL,false);
378 _ggadget_redraw((GGadget*)gme);
379 }
380 return( changed );
381 }
382
383 static void GMatrixEdit_SetDesiredSize(GGadget *g,GRect *outer,GRect *inner) {
384 GMatrixEdit *gme = (GMatrixEdit *) g;
385 int bp = GBoxBorderWidth(g->base,g->box);
386
387 if ( outer!=NULL ) {
388 g->desired_width = outer->width;
389 g->desired_height = outer->height;
390 } else if ( inner!=NULL ) {
391 int extra = 2*bp+ gme->hsb->r.height+ (gme->has_titles?gme->fh:0);
392 if ( gme->del )
393 extra += gme->del->r.height+DEL_SPACE;
394 g->desired_width = inner->width<=0 ? -1 : inner->width+2*bp+gme->vsb->r.width;
395 g->desired_height = inner->height<=0 ? -1 :
396 inner->height<10 ? inner->height*(gme->fh + gme->vpad) + extra :
397 inner->height+extra;
398 }
399 }
400
401 static void GMatrixEdit_GetDesiredSize(GGadget *g,GRect *outer,GRect *inner) {
402 GMatrixEdit *gme = (GMatrixEdit *) g;
403 int width, height;
404 int bp = GBoxBorderWidth(g->base,g->box);
405 int c, rows, i;
406 FontInstance *old = GDrawSetFont(gme->g.base,gme->font);
407 int sbwidth = GDrawPointsToPixels(g->base,_GScrollBar_Width);
408 int butwidth = 0;
409
410 width = 1;
411 for ( c=0; c<gme->cols; ++c ) {
412 width += GME_ColWidth(gme,c);
413 if ( c!=gme->cols-1 )
414 width += gme->hpad;
415 }
416 GDrawSetFont(gme->g.base,old);
417 width += sbwidth;
418
419 rows = (gme->rows<4) ? 4 : (gme->rows>20) ? 21 : gme->rows+1;
420 height = rows * (gme->fh + gme->vpad);
421
422 if ( gme->has_titles )
423 height += gme->fh;
424 height += sbwidth;
425 if ( gme->del ) {
426 height += gme->del->r.height+DEL_SPACE;
427 butwidth += gme->del->r.width + 10;
428 }
429 if ( gme->up && gme->up->state!=gs_invisible )
430 butwidth += gme->up->r.width+5;
431 if ( gme->down && gme->down->state!=gs_invisible )
432 butwidth += gme->down->r.width+5;
433 if ( gme->buttonlist!=NULL )
434 for ( i=0; gme->buttonlist[i]!=NULL; ++i )
435 if ( gme->buttonlist[i] && gme->buttonlist[i]->state!=gs_invisible )
436 butwidth += gme->buttonlist[i]->r.width+5;
437 if ( butwidth > width )
438 width = butwidth;
439
440 if ( g->desired_width>2*bp )
441 width = g->desired_width-2*bp;
442 if ( g->desired_height>2*bp )
443 height = g->desired_height-2*bp;
444 if ( inner!=NULL ) {
445 inner->x = inner->y = 0;
446 inner->width = width;
447 inner->height = height;
448 }
449 if ( outer!=NULL ) {
450 outer->x = outer->y = 0;
451 outer->width = width + 2*bp;
452 outer->height = height + 2*bp;
453 }
454 }
455
456 static void GME_PositionEdit(GMatrixEdit *gme);
457 static void GMatrixEdit_Resize(GGadget *g, int32_t width, int32_t height ) {
458 GMatrixEdit *gme = (GMatrixEdit *) g;
459 int bp = GBoxBorderWidth(g->base,g->box);
460 int subheight, subwidth;
461 /*int plus, extra,x,c;*/
462 int bcnt, i, min_width;
463 GRect wsize;
464
465 width -= 2*bp; height -= 2*bp;
466
467 subheight = height -
468 (gme->no_edit && gme->buttonlist==NULL?0:(gme->del->r.height+DEL_SPACE)) -
469 (gme->has_titles?gme->fh:0) -
470 gme->hsb->r.height;
471 subwidth = width - gme->vsb->r.width;
472 GDrawResize(gme->nested,subwidth, subheight);
473 /* Make sure the dimensions of the internal window are properly set. */
474 /* We need this because GDrawResize() just notifies WM about the size */
475 /* changes, but doesn't update the data structures used by FF internally. */
476 /* This may result into list marks/functional buttons being incorrectly */
477 /* positioned in their matrix cells when the dialog is displayed. */
478 GDrawGetSize(gme->nested, &wsize);
479 wsize.width = subwidth;
480 wsize.height = subheight;
481 gme->nested->pos = wsize;
482
483 GGadgetResize(gme->vsb,gme->vsb->r.width,subheight);
484 GGadgetMove(gme->vsb,gme->g.inner.x + width-2*bp-gme->vsb->r.width,
485 gme->vsb->r.y);
486 GGadgetResize(gme->hsb,subwidth,gme->hsb->r.height);
487 GGadgetMove(gme->hsb,gme->g.inner.x,
488 gme->g.inner.y + height - (gme->del->r.height+DEL_SPACE) - gme->hsb->r.height);
489 GME_FixScrollBars(gme);
490
491 bcnt = 1; /* delete */
492 if ( gme->up && gme->up->state!=gs_invisible )
493 bcnt += 2;
494 if ( gme->buttonlist!=NULL ) {
495 for ( i=0; gme->buttonlist[i]!=NULL; ++i )
496 if ( gme->buttonlist[i]->state!=gs_invisible )
497 ++bcnt;
498 }
499 if ( bcnt==1 && gme->no_edit ) {
500 /* No delete button to display */
501 } else if ( bcnt==1 ) {
502 GGadgetMove(gme->del,gme->g.inner.x + (width-gme->del->r.width)/2,
503 gme->g.inner.y + height - (gme->del->r.height+DEL_SPACE/2));
504 } else {
505 int y = gme->g.inner.y + height - (gme->del->r.height+DEL_SPACE/2);
506 int x = gme->g.inner.x + width-5;
507 GGadgetMove(gme->del,gme->g.inner.x + 5, y);
508 if ( gme->up && gme->up->state!=gs_invisible ) {
509 x -= gme->down->r.width;
510 GGadgetMove(gme->down, x, y);
511 x -= 5 + gme->up->r.width;
512 GGadgetMove(gme->up, x, y);
513 x -= 10;
514 }
515 if ( gme->buttonlist!=NULL ) {
516 for ( i=0; gme->buttonlist[i]!=NULL; ++i )
517 if ( gme->buttonlist[i]->state!=gs_invisible ) {
518 x -= gme->buttonlist[i]->r.width;
519 GGadgetMove(gme->buttonlist[i], x, y);
520 x -= 5;
521 }
522 }
523 }
524
525 /* I thought to leave the columns as they are, but that looks odd */
526 /* for the last column. Instead put all the extra space in the */
527 /* last column, but give it some minimal size */
528 min_width = GME_ColWidth(gme,gme->cols-1);
529 gme->col_data[gme->cols-1].width = (subwidth-gme->hpad-gme->col_data[gme->cols-1].x);
530 if ( gme->col_data[gme->cols-1].width<min_width )
531 gme->col_data[gme->cols-1].width = min_width;
532 GME_FixScrollBars(gme);
533 _ggadget_resize(g,width+2*bp, height+2*bp);
534 GME_PositionEdit(gme);
535 GDrawRequestExpose(gme->nested,NULL,false);
536 }
537
538 static int GMatrixEdit_Mouse(GGadget *g, GEvent *event) {
539 GMatrixEdit *gme = (GMatrixEdit *) g;
540 int c, nw, i, x, ex = event->u.mouse.x + gme->off_left;
541
542 if (( event->type==et_mouseup || event->type==et_mousedown ) &&
543 (event->u.mouse.button>=4 && event->u.mouse.button<=7)) {
544 int isv = event->u.mouse.button<=5;
545 if ( event->u.mouse.state&ksm_shift ) isv = !isv;
546 if ( isv && gme->vsb!=NULL )
547 return( GGadgetDispatchEvent(gme->vsb,event));
548 else if ( !isv && gme->hsb!=NULL )
549 return( GGadgetDispatchEvent(gme->hsb,event));
550 else
551 return( true );
552 }
553
554 if ( gme->pressed_col>=0 && (event->type==et_mouseup || event->type==et_mousemove)) {
555 c = gme->pressed_col;
556 nw = (ex-gme->g.inner.x-gme->col_data[c].x-gme->hpad/2);
557 if ( ex-gme->g.inner.x < gme->col_data[c].x ) {
558 if ( nw<=0 )
559 nw = 1;
560 }
561 nw = (ex-gme->g.inner.x-gme->col_data[c].x-gme->hpad/2);
562 x = gme->col_data[c].x;
563 for ( i=c; i<gme->cols; ++i ) {
564 gme->col_data[i].x = x;
565 x += gme->col_data[i].width + gme->hpad;
566 }
567 gme->col_data[c].width = nw;
568 if ( event->type==et_mouseup )
569 GME_FixScrollBars(gme);
570 _ggadget_redraw((GGadget*)gme);
571 GME_PositionEdit(gme);
572 GDrawRequestExpose(gme->nested,NULL,false);
573 if ( event->type==et_mouseup ) {
574 GDrawSetCursor(g->base,ct_pointer);
575 gme->pressed_col = -1;
576 }
577 return( true );
578 }
579
580 if ( !gme->has_titles ||
581 event->u.mouse.x< gme->hsb->r.x || event->u.mouse.x >= gme->hsb->r.x+gme->hsb->r.width ||
582 event->u.mouse.y< gme->g.inner.y || event->u.mouse.y>=gme->g.inner.y+gme->fh ) {
583 if ( gme->lr_pointer ) {
584 gme->lr_pointer = false;
585 GDrawSetCursor(g->base,ct_pointer);
586 }
587 return( false );
588 }
589 for ( c=0; c<gme->cols; ++c ) {
590 if ( ex>= gme->g.inner.x + gme->col_data[c].x+gme->col_data[c].width+gme->hpad/2-4 &&
591 ex<= gme->g.inner.x + gme->col_data[c].x+gme->col_data[c].width+gme->hpad/2+4 )
592 break;
593 }
594 if ( c==gme->cols ) {
595 if ( gme->lr_pointer ) {
596 gme->lr_pointer = false;
597 GDrawSetCursor(g->base,ct_pointer);
598 }
599 } else {
600 if ( !gme->lr_pointer ) {
601 gme->lr_pointer = true;
602 GDrawSetCursor(g->base,ct_4way);
603 }
604 if ( event->type == et_mousedown )
605 gme->pressed_col = c;
606 }
607 return( true );
608 }
609
610 static int GMatrixEdit_Expose(GWindow pixmap, GGadget *g, GEvent *event) {
611 GMatrixEdit *gme = (GMatrixEdit *) g;
612 GRect r, old, older;
613 int c, y, lastc;
614 Color fg = gmatrixedit_title_fg;
615
616 if ( gme->g.state!=gs_enabled )
617 fg = gme->g.box->disabled_foreground;
618
619 GBoxDrawBorder(pixmap,&g->r,g->box,g->state,false);
620 if ( gme->has_titles ) {
621 r = gme->g.inner;
622 r.height = gme->font_fh;
623 r.width = gme->hsb->r.width;
624 GDrawPushClip(pixmap,&r,&older);
625 GDrawFillRect(pixmap,&r,gmatrixedit_title_bg);
626 y = r.y + gme->font_as;
627 GDrawSetFont(pixmap,gme->titfont);
628 for ( lastc = gme->cols-1; lastc>0 && gme->col_data[lastc].hidden; --lastc );
629 for ( c=0; c<gme->cols; ++c ) {
630 if ( gme->col_data[c].title!=NULL &&
631 !gme->col_data[c].hidden ) {
632 r.x = gme->col_data[c].x + gme->g.inner.x - gme->off_left;
633 r.width = gme->col_data[c].width;
634 GDrawPushClip(pixmap,&r,&old);
635 GDrawDrawText8(pixmap,r.x,y,gme->col_data[c].title,-1,fg);
636 GDrawPopClip(pixmap,&old);
637 }
638 if ( c!=lastc && !gme->col_data[c].hidden)
639 GDrawDrawLine(pixmap,r.x+gme->col_data[c].width+gme->hpad/2,r.y,
640 r.x+gme->col_data[c].width+gme->hpad/2,r.y+r.height,
641 gmatrixedit_title_divider);
642 }
643 GDrawPopClip(pixmap,&older);
644 }
645 return( true );
646 }
647
648 static void GMatrixEdit_SetVisible(GGadget *g, int visible ) {
649 GMatrixEdit *gme = (GMatrixEdit *) g;
650 int i;
651
652 if ( gme->vsb!=NULL ) _ggadget_setvisible(gme->vsb,visible);
653 if ( gme->hsb!=NULL ) _ggadget_setvisible(gme->hsb,visible);
654 if ( gme->del!=NULL ) _ggadget_setvisible(gme->del,visible);
655 if ( gme->up!=NULL ) _ggadget_setvisible(gme->up,visible);
656 if ( gme->down!=NULL ) _ggadget_setvisible(gme->down,visible);
657 if ( gme->buttonlist!=NULL )
658 for ( i=0; gme->buttonlist[i]!=NULL; ++i )
659 _ggadget_setvisible(gme->buttonlist[i],visible);
660
661 GDrawSetVisible(gme->nested,visible);
662 _ggadget_setvisible(g,visible);
663 }
664
665 static void GMatrixEdit_Redraw(GGadget *g ) {
666 GMatrixEdit *gme = (GMatrixEdit *) g;
667 GDrawRequestExpose(gme->nested,NULL,false);
668 _ggadget_redraw(g);
669 }
670
671 static char *MD_Text(GMatrixEdit *gme,int r, int c ) {
672 char buffer[20], *str= NULL;
673 struct matrix_data *d = &gme->data[r*gme->cols+c];
674
675 switch ( gme->col_data[c].me_type ) {
676 case me_enum:
677 /* Fall through into next case */
678 case me_int:
679 sprintf( buffer,"%d",(int) d->u.md_ival );
680 str = buffer;
681 break;
682 case me_hex:
683 sprintf( buffer,"0x%x",(int) d->u.md_ival );
684 str = buffer;
685 break;
686 case me_uhex:
687 sprintf( buffer,"U+%04X",(int) d->u.md_ival );
688 str = buffer;
689 break;
690 case me_addr:
691 sprintf( buffer,"%p", d->u.md_addr );
692 str = buffer;
693 break;
694 case me_real:
695 sprintf( buffer,"%g",d->u.md_real );
696 str = buffer;
697 break;
698 case me_string: case me_bigstr:
699 case me_funcedit:
700 case me_onlyfuncedit:
701 case me_button:
702 case me_stringchoice: case me_stringchoicetrans: case me_stringchoicetag:
703 str = d->u.md_str;
704 break;
705 case me_func:
706 str = d->u.md_str;
707 if ( str==NULL )
708 return( (gme->col_data[c].func)(&gme->g,r,c) );
709 break;
710 }
711 if (!str) str="";
712 return( copy(str));
713 }
714
715 static int GME_RecalcFH(GMatrixEdit *gme) {
716 int r,c, as, ds;
717 int32_t end = -1;
718 char *str, *ept;
719 GTextBounds bounds;
720 GMenuItem *mi;
721
722 GDrawSetFont(gme->nested,gme->font);
723 as = gme->font_as; ds = gme->font_fh-as;
724 for ( r=0; r<gme->rows; ++r ) for ( c=0; c<gme->cols; ++c ) {
725 end = -1;
726 switch ( gme->col_data[c].me_type ) {
727 case me_enum:
728 mi = FindMi(gme->col_data[c].enum_vals,gme->data[r*gme->cols+c].u.md_ival);
729 if ( mi==NULL )
730 continue;
731 str = copy( (char *)mi->ti.text );
732 break;
733 default:
734 str = MD_Text(gme,r,c);
735 if ( str == NULL )
736 continue;
737 if ( ( ept = strchr(str,'\n') ) != NULL )
738 end = ept - str;
739 break;
740 }
741 GDrawGetText8Bounds(gme->nested, str, end, &bounds);
742 free(str);
743 if ( bounds.as>as )
744 as = bounds.as;
745 if ( bounds.ds>ds )
746 ds = bounds.ds;
747 }
748 if ( as!=gme->as || as+ds!=gme->fh ) {
749 gme->fh = as+ds;
750 gme->as = as;
751 return( true );
752 }
753 return( false );
754 }
755
756 static void GMatrixEdit_SetFont(GGadget *g,FontInstance *new) {
757 GMatrixEdit *gme = (GMatrixEdit *) g;
758 int as, ds, ld;
759 gme->font = new;
760 GDrawWindowFontMetrics(g->base,gme->font,&as, &ds, &ld);
761 gme->font_as = gme->as = as;
762 gme->font_fh = gme->fh = as+ds;
763 GME_RecalcFH(gme);
764 GME_FixScrollBars(gme);
765 GDrawRequestExpose(gme->nested,NULL,false);
766 }
767
768 static FontInstance *GMatrixEdit_GetFont(GGadget *g) {
769 GMatrixEdit *gme = (GMatrixEdit *) g;
770 return( gme->font );
771 }
772
773 static int32_t GMatrixEdit_IsSelected(GGadget *g, int32_t pos) {
774 GMatrixEdit *gme = (GMatrixEdit *) g;
775
776 return( pos==gme->active_row );
777 }
778
779 static int32_t GMatrixEdit_GetFirst(GGadget *g) {
780 GMatrixEdit *gme = (GMatrixEdit *) g;
781
782 return( gme->active_row );
783 }
784
785 static int GMatrixEdit_FillsWindow(GGadget *g) {
786 return( true );
787 }
788
789 struct gfuncs gmatrixedit_funcs = {
790 0,
791 sizeof(struct gfuncs),
792
793 GMatrixEdit_Expose,
794 GMatrixEdit_Mouse,
795 NULL,
796 NULL,
797 NULL,
798 NULL,
799 NULL,
800
801 GMatrixEdit_Redraw,
802 GMatrixEdit_Move,
803 GMatrixEdit_Resize,
804 GMatrixEdit_SetVisible,
805 _ggadget_setenabled,
806 _ggadget_getsize,
807 _ggadget_getinnersize,
808
809 GMatrixEdit_destroy,
810
811 NULL,
812 NULL,
813 NULL,
814 NULL,
815 NULL,
816 GMatrixEdit_SetFont,
817 GMatrixEdit_GetFont,
818
819 NULL,
820 NULL,
821 NULL,
822 NULL,
823 NULL,
824 NULL,
825 GMatrixEdit_IsSelected,
826 GMatrixEdit_GetFirst,
827 NULL,
828 NULL,
829 NULL,
830
831 GMatrixEdit_GetDesiredSize,
832 GMatrixEdit_SetDesiredSize,
833 GMatrixEdit_FillsWindow,
834 NULL
835 };
836
837 static void GME_PositionEdit(GMatrixEdit *gme) {
838 int x,y,end;
839 GRect wsize;
840 int c = gme->active_col, r = gme->active_row, lastc;
841
842 for ( lastc = gme->cols-1; lastc>0 && gme->col_data[lastc].hidden; --lastc );
843
844 if ( gme->edit_active ) {
845 x = gme->col_data[c].x - gme->off_left;
846 y = (r-gme->off_top)*(gme->fh+gme->vpad);
847 end = x + gme->col_data[c].width;
848
849 if ( c == lastc ) {
850 GDrawGetSize(gme->nested,&wsize);
851 if ( end>wsize.width )
852 end = wsize.width - x;
853 if ( gme->col_data[c].me_type==me_stringchoice ||
854 gme->col_data[c].me_type==me_stringchoicetrans ||
855 gme->col_data[c].me_type==me_stringchoicetag ||
856 gme->col_data[c].me_type==me_onlyfuncedit ||
857 gme->col_data[c].me_type==me_funcedit )
858 end -= gme->mark_size+gme->mark_skip;
859 }
860
861 GGadgetResize(gme->tf,end-x,gme->fh);
862 GGadgetMove(gme->tf,x,y);
863 }
864 }
865
866 static void GME_StrSmallEdit(GMatrixEdit *gme,char *str, GEvent *event) {
867 gme->edit_active = true;
868 /* Shift so all of column is in window???? */
869 GME_PositionEdit(gme);
870 GGadgetSetTitle8(gme->tf,str);
871 GGadgetSetVisible(gme->tf,true);
872 GGadgetSetEnabled(gme->tf,true);
873 GCompletionFieldSetCompletion(gme->tf,gme->col_data[gme->active_col].completer);
874 ((GTextField *) (gme->tf))->accepts_tabs = false;
875 ((GTextField *) (gme->tf))->was_completing = gme->col_data[gme->active_col].completer!=NULL;
876 GWidgetIndicateFocusGadget(gme->tf);
877 if ( event->type == et_mousedown )
878 GGadgetDispatchEvent(gme->tf,event);
879 }
880
881 static int GME_SetValue(GMatrixEdit *gme,GGadget *g ) {
882 int c = gme->active_col;
883 int r = gme->active_row;
884 intptr_t lval;
885 double dval;
886 char *end="";
887 char *str = GGadgetGetTitle8(g), *pt;
888 int kludge;
889
890 switch ( gme->col_data[c].me_type ) {
891 case me_enum:
892 {
893 const unichar_t *ustr = _GGadgetGetTitle(g), *test;
894 int i;
895 for ( i=0; (test=gme->col_data[c].enum_vals[i].ti.text)!=NULL || gme->col_data[c].enum_vals[i].ti.line ; ++i ) {
896 if ( u_strcmp(ustr,test)==0 ) {
897 if ( (intptr_t) gme->col_data[c].enum_vals[i].ti.userdata != GME_NoChange )
898 gme->data[r*gme->cols+c].u.md_ival =
899 (intptr_t) gme->col_data[c].enum_vals[i].ti.userdata;
900 free(str);
901 goto good;
902 }
903 }
904 }
905 /* Didn't match any of the enums try as a direct integer */
906 /* Fall through */
907 case me_int: case me_hex: case me_uhex: case me_addr:
908 if ( gme->validatestr!=NULL )
909 end = (gme->validatestr)(&gme->g,gme->active_row,gme->active_col,gme->wasnew,str);
910 if ( *end=='\0' ) {
911 if ( gme->col_data[c].me_type==me_hex || gme->col_data[c].me_type==me_uhex ) {
912 pt = str;
913 while ( *pt==' ' ) ++pt;
914 if ( (*pt=='u' || *pt=='U') && pt[1]=='+' )
915 pt += 2;
916 else if ( *pt=='0' && (pt[1]=='x' || pt[1]=='X'))
917 pt += 2;
918 lval = strtoul(pt,&end,16);
919 } else
920 lval = strtol(str,&end,10);
921 }
922 if ( *end!='\0' ) {
923 GTextFieldSelect(g,end-str,-1);
924 free(str);
925 GDrawBeep(NULL);
926 return( false );
927 }
928 if ( gme->col_data[c].me_type == me_addr )
929 gme->data[r*gme->cols+c].u.md_addr = (void *) lval;
930 else
931 gme->data[r*gme->cols+c].u.md_ival = lval;
932 free(str);
933 goto good;
934 case me_real:
935 if ( gme->validatestr!=NULL )
936 end = (gme->validatestr)(&gme->g,gme->active_row,gme->active_col,gme->wasnew,str);
937 if ( *end=='\0' )
938 dval = strtod(str,&end);
939 if ( *end!='\0' ) {
940 GTextFieldSelect(g,end-str,-1);
941 free(str);
942 GDrawBeep(NULL);
943 return( false );
944 }
945 gme->data[r*gme->cols+c].u.md_real = dval;
946 free(str);
947 goto good;
948 case me_stringchoice: case me_stringchoicetrans: case me_stringchoicetag:
949 case me_funcedit: case me_onlyfuncedit:
950 case me_string: case me_bigstr: case me_func: case me_button:
951 if ( gme->validatestr!=NULL )
952 end = (gme->validatestr)(&gme->g,gme->active_row,gme->active_col,gme->wasnew,str);
953 if ( *end!='\0' ) {
954 GTextFieldSelect(g,end-str,-1);
955 free(str);
956 GDrawBeep(NULL);
957 return( false );
958 }
959
960 free(gme->data[r*gme->cols+c].u.md_str);
961 gme->data[r*gme->cols+c].u.md_str = str;
962 /* Used to delete the row if this were a null string. seems extreme */
963 goto good;
964 default:
965 /* Eh? Can't happen */
966 GTextFieldSelect(g,0,-1);
967 GDrawBeep(NULL);
968 free(str);
969 return( false );
970 }
971 good:
972 kludge = gme->edit_active; gme->edit_active = false;
973 if ( gme->finishedit != NULL )
974 (gme->finishedit)(&gme->g,r,c,gme->wasnew);
975 gme->edit_active = kludge;
976 return( true );
977 }
978
979 static int GME_FinishEdit(GMatrixEdit *gme) {
980
981 if ( !gme->edit_active ) {
982 gme->wasnew = false;
983 return( true );
984 }
985 if ( !GME_SetValue(gme,gme->tf)) {
986 gme->wasnew = false;
987 return( false );
988 }
989 gme->edit_active = false;
990 GGadgetSetVisible(gme->tf,false);
991 GME_AdjustCol(gme,gme->active_col);
992 if ( GME_RecalcFH(gme) ) {
993 GME_FixScrollBars(gme);
994 GDrawRequestExpose(gme->nested,NULL,false);
995 }
996
997 gme->wasnew = false;
998 return( true );
999 }
1000
1001 /* Sometimes our data moves underneath us (if the validate function does */
1002 /* something weird). See if we can move the current row with the data */
1003 static int GME_FinishEditPreserve(GMatrixEdit *gme,int r) {
1004 int i;
1005
1006 if ( r<gme->rows ) {
1007 for ( i=0; i<gme->rows; ++i )
1008 gme->data[i*gme->cols].current = 0;
1009 gme->data[r*gme->cols].current = 1;
1010 }
1011 if ( !GME_FinishEdit(gme))
1012 return( -1 );
1013 if ( r==gme->rows )
1014 return( r );
1015 for ( i=0; i<gme->rows; ++i )
1016 if ( gme->data[i*gme->cols].current )
1017 return( i );
1018
1019 /* Quite lost */
1020 return( r );
1021 }
1022
1023 static void GME_EnableDelete(GMatrixEdit *gme) {
1024 int enabled = false;
1025
1026 if ( gme->setotherbuttons )
1027 (gme->setotherbuttons)(&gme->g,gme->active_row,gme->active_col);
1028 if ( gme->active_row>=0 && gme->active_row<gme->rows ) {
1029 enabled = true;
1030 if ( gme->candelete!=NULL && !(gme->candelete)(&gme->g,gme->active_row))
1031 enabled = false;
1032 }
1033 GGadgetSetEnabled(gme->del,enabled);
1034
1035 if ( gme->up!=NULL ) {
1036 enum gme_updown updown;
1037 if ( gme->canupdown != NULL )
1038 updown = (gme->canupdown)((GGadget *) gme,gme->active_row);
1039 else {
1040 updown = 0;
1041 if ( gme->active_row>=1 && gme->active_row<gme->rows )
1042 updown = ud_up_enabled;
1043 if ( gme->active_row>=0 && gme->active_row<gme->rows-1 )
1044 updown |= ud_down_enabled;
1045 }
1046 GGadgetSetEnabled(gme->up,updown & ud_up_enabled ? 1 : 0);
1047 GGadgetSetEnabled(gme->down,updown & ud_down_enabled ? 1 : 0);
1048 }
1049 }
1050
1051 static void GME_DeleteActive(GMatrixEdit *gme) {
1052 int r, c;
1053
1054 if ( gme->active_row==-1 || (gme->candelete && !(gme->candelete)(&gme->g,gme->active_row))) {
1055 GGadgetSetEnabled(gme->del,false);
1056 GDrawBeep(NULL);
1057 return;
1058 }
1059 if ( gme->predelete!=NULL )
1060 (gme->predelete)((GGadget *) gme, gme->active_row );
1061
1062 gme->edit_active = false;
1063 GGadgetSetVisible(gme->tf,false);
1064 for ( c=0; c<gme->cols; ++c ) {
1065 if ( gme->col_data[c].me_type == me_string || gme->col_data[c].me_type == me_bigstr ||
1066 gme->col_data[c].me_type == me_func || gme->col_data[c].me_type == me_funcedit ||
1067 gme->col_data[c].me_type == me_onlyfuncedit ||
1068 gme->col_data[c].me_type == me_button ||
1069 gme->col_data[c].me_type == me_stringchoice ||
1070 gme->col_data[c].me_type == me_stringchoicetag ||
1071 gme->col_data[c].me_type == me_stringchoicetrans ) {
1072 free(gme->data[gme->active_row*gme->cols+c].u.md_str);
1073 gme->data[gme->active_row*gme->cols+c].u.md_str = NULL;
1074 }
1075 }
1076 for ( r=gme->active_row+1; r<gme->rows; ++r )
1077 memcpy(gme->data+(r-1)*gme->cols,gme->data+r*gme->cols,
1078 gme->cols*sizeof(struct matrix_data));
1079 --gme->rows;
1080 gme->active_col = -1;
1081 if ( gme->active_row>=gme->rows ) gme->active_row = -1;
1082 GScrollBarSetBounds(gme->vsb,0,gme->rows,gme->vsb->inner.height/gme->fh);
1083 GDrawRequestExpose(gme->nested,NULL,false);
1084 GME_EnableDelete(gme);
1085 }
1086
1087 void GMatrixEditUp(GGadget *g) {
1088 GMatrixEdit *gme = (GMatrixEdit *) g;
1089 GRect r;
1090 int i;
1091
1092 if ( gme->active_row<1 || gme->active_row>=gme->rows )
1093 return;
1094 for ( i=0; i<gme->cols; ++i ) {
1095 struct matrix_data md = gme->data[gme->active_row*gme->cols+i];
1096 gme->data[gme->active_row*gme->cols+i] = gme->data[(gme->active_row-1)*gme->cols+i];
1097 gme->data[(gme->active_row-1)*gme->cols+i] = md;
1098 }
1099 --gme->active_row;
1100 GGadgetGetSize(gme->tf,&r);
1101 GGadgetMove(gme->tf,r.x,r.y-(gme->fh+1));
1102 GME_EnableDelete(gme);
1103 if ( gme->rowmotion!=NULL )
1104 (gme->rowmotion)((GGadget *) gme, gme->active_row+1,gme->active_row);
1105 GMatrixEditScrollToRowCol(&gme->g,gme->active_row,gme->active_col);
1106 }
1107
1108 static int _GME_Up(GGadget *g, GEvent *e) {
1109 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
1110 GMatrixEditUp( g->data );
1111 }
1112 return( true );
1113 }
1114
1115 void GMatrixEditDown(GGadget *g) {
1116 GMatrixEdit *gme = (GMatrixEdit *) g;
1117 GRect r;
1118 int i;
1119
1120 if ( gme->active_row<0 || gme->active_row>=gme->rows-1 )
1121 return;
1122 for ( i=0; i<gme->cols; ++i ) {
1123 struct matrix_data md = gme->data[gme->active_row*gme->cols+i];
1124 gme->data[gme->active_row*gme->cols+i] = gme->data[(gme->active_row+1)*gme->cols+i];
1125 gme->data[(gme->active_row+1)*gme->cols+i] = md;
1126 }
1127 ++gme->active_row;
1128 GGadgetGetSize(gme->tf,&r);
1129 GGadgetMove(gme->tf,r.x,r.y-(gme->fh+1));
1130 GME_EnableDelete(gme);
1131 if ( gme->rowmotion!=NULL )
1132 (gme->rowmotion)((GGadget *) gme, gme->active_row-1,gme->active_row);
1133 GMatrixEditScrollToRowCol(&gme->g,gme->active_row,gme->active_col);
1134 return;
1135 }
1136
1137 static int _GME_Down(GGadget *g, GEvent *e) {
1138 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
1139 GMatrixEditDown( g->data );
1140 }
1141 return( true );
1142 }
1143
1144 #define CID_OK 1001
1145 #define CID_Cancel 1002
1146 #define CID_EntryField 1011
1147
1148 static int big_e_h(GWindow gw, GEvent *event) {
1149 GMatrixEdit *gme = GDrawGetUserData(gw);
1150
1151 if ( event->type==et_close ) {
1152 gme->big_done = true;
1153 } else if ( event->type == et_char ) {
1154 return( false );
1155 } else if ( event->type == et_controlevent && event->u.control.subtype == et_buttonactivate ) {
1156 gme->big_done = true;
1157 if ( GGadgetGetCid(event->u.control.g)==CID_OK ) {
1158 gme->big_done = GME_SetValue(gme,GWidgetGetControl(gw,CID_EntryField) );
1159 if ( gme->big_done )
1160 GME_AdjustCol(gme,gme->active_col);
1161 } else if ( gme->wasnew ) {
1162 /* They canceled a click which produced a new row */
1163 GME_DeleteActive(gme);
1164 gme->wasnew = false;
1165 }
1166 }
1167 return( true );
1168 }
1169
1170 static void GME_StrBigEdit(GMatrixEdit *gme,char *str) {
1171 GRect pos;
1172 GWindow gw;
1173 GWindowAttrs wattrs;
1174 GGadgetCreateData mgcd[6], boxes[3], *varray[5], *harray[6];
1175 GTextInfo mlabel[6];
1176 char *title_str = NULL;
1177
1178 if ( gme->bigedittitle!=NULL )
1179 title_str = (gme->bigedittitle)(&(gme->g),gme->active_row,gme->active_col);
1180
1181 memset(&wattrs,0,sizeof(wattrs));
1182 wattrs.mask = wam_events|wam_cursor|wam_utf8_wtitle|wam_undercursor|wam_isdlg|wam_restrict;
1183 wattrs.event_masks = ~(1<<et_charup);
1184 wattrs.is_dlg = true;
1185 wattrs.restrict_input_to_me = 1;
1186 wattrs.undercursor = 1;
1187 wattrs.cursor = ct_pointer;
1188 wattrs.utf8_window_title = title_str==NULL ? "Editing..." : title_str;
1189 pos.x = pos.y = 0;
1190 pos.width =GDrawPointsToPixels(NULL,GGadgetScale(500));
1191 pos.height = GDrawPointsToPixels(NULL,400);
1192 gme->big_done = 0;
1193 gw = GDrawCreateTopWindow(NULL,&pos,big_e_h,gme,&wattrs);
1194 free(title_str);
1195
1196 memset(&mgcd,0,sizeof(mgcd));
1197 memset(&boxes,0,sizeof(boxes));
1198 memset(&mlabel,0,sizeof(mlabel));
1199 mgcd[0].gd.pos.x = 4; mgcd[0].gd.pos.y = 6;
1200 mgcd[0].gd.pos.width = 492;
1201 mgcd[0].gd.pos.height = 260;
1202 mgcd[0].gd.flags = gg_visible | gg_enabled | gg_textarea_wrap | gg_text_xim;
1203 mgcd[0].gd.cid = CID_EntryField;
1204 mgcd[0].creator = GTextAreaCreate;
1205 varray[0] = &mgcd[0]; varray[1] = NULL;
1206 varray[2] = &boxes[2]; varray[3] = NULL;
1207 varray[4] = NULL;
1208
1209 mgcd[1].gd.pos.x = 30-3; mgcd[1].gd.pos.y = GDrawPixelsToPoints(NULL,pos.height)-35-3;
1210 mgcd[1].gd.pos.width = -1; mgcd[1].gd.pos.height = 0;
1211 mgcd[1].gd.flags = gg_visible | gg_enabled | gg_but_default;
1212 mlabel[1].text = (unichar_t *) _("_OK");
1213 mlabel[1].text_is_1byte = true;
1214 mlabel[1].text_in_resource = true;
1215 mgcd[1].gd.label = &mlabel[1];
1216 mgcd[1].gd.cid = CID_OK;
1217 mgcd[1].creator = GButtonCreate;
1218
1219 mgcd[2].gd.pos.x = -30; mgcd[2].gd.pos.y = mgcd[1].gd.pos.y+3;
1220 mgcd[2].gd.pos.width = -1; mgcd[2].gd.pos.height = 0;
1221 mgcd[2].gd.flags = gg_visible | gg_enabled | gg_but_cancel;
1222 mlabel[2].text = (unichar_t *) _("_Cancel");
1223 mlabel[2].text_is_1byte = true;
1224 mlabel[2].text_in_resource = true;
1225 mgcd[2].gd.label = &mlabel[2];
1226 mgcd[2].gd.cid = CID_Cancel;
1227 mgcd[2].creator = GButtonCreate;
1228 harray[0] = GCD_Glue; harray[1] = &mgcd[1];
1229 harray[2] = GCD_Glue; harray[3] = &mgcd[2];
1230 harray[4] = GCD_Glue; harray[5] = NULL;
1231
1232 boxes[2].gd.flags = gg_visible | gg_enabled;
1233 boxes[2].gd.u.boxelements = harray;
1234 boxes[2].creator = GHBoxCreate;
1235
1236 boxes[0].gd.pos.x = boxes[0].gd.pos.y = 2;
1237 boxes[0].gd.flags = gg_visible | gg_enabled;
1238 boxes[0].gd.u.boxelements = varray;
1239 boxes[0].creator = GHVGroupCreate;
1240
1241 GGadgetsCreate(gw,boxes);
1242 GHVBoxSetExpandableRow(boxes[0].ret,0);
1243 GHVBoxSetExpandableCol(boxes[2].ret,gb_expandgluesame);
1244 GHVBoxFitWindow(boxes[0].ret);
1245 GGadgetSetTitle8(mgcd[0].ret,str);
1246 GTextFieldSelect(mgcd[0].ret,0,0);
1247 GTextFieldShow(mgcd[0].ret,0);
1248
1249 GDrawSetVisible(gw,true);
1250 while ( !gme->big_done )
1251 GDrawProcessOneEvent(NULL);
1252 GDrawDestroyWindow(gw);
1253 GDrawRequestExpose(gme->nested,NULL,false);
1254
1255 gme->wasnew = false;
1256 }
1257
1258 static void GME_EnumDispatch(GWindow gw, GMenuItem *mi, GEvent *e) {
1259 GMatrixEdit *gme = GDrawGetUserData(gw);
1260
1261 if ( (intptr_t) mi->ti.userdata == GME_NoChange )
1262 return;
1263
1264 gme->data[gme->active_row*gme->cols+gme->active_col].u.md_ival = (intptr_t) mi->ti.userdata;
1265
1266 if ( gme->finishedit != NULL )
1267 (gme->finishedit)(&gme->g,gme->active_row,gme->active_col,gme->wasnew);
1268 GME_AdjustCol(gme,gme->active_col);
1269 gme->wasnew = false;
1270 }
1271
1272 static void GME_FinishChoice(GWindow gw) {
1273 GMatrixEdit *gme = GDrawGetUserData(gw);
1274
1275 /* If wasnew is still set then they didn't pick anything, so remove the row */
1276 if ( gme->wasnew && gme->active_col==0 )
1277 GME_DeleteActive(gme);
1278 gme->wasnew = false;
1279 GDrawRequestExpose(gme->nested,NULL,false);
1280 }
1281
1282 static void GME_Choices(GMatrixEdit *gme,GEvent *event,int r,int c) {
1283 GMenuItem *mi = gme->col_data[c].enum_vals;
1284 int val = gme->data[r*gme->cols+c].u.md_ival;
1285 int i;
1286
1287 for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.line || mi[i].ti.image!=NULL; ++i )
1288 mi[i].ti.selected = mi[i].ti.checked = !gme->wasnew && (mi[i].ti.userdata == (void *) (intptr_t) val);
1289 if ( gme->col_data[c].enable_enum!=NULL )
1290 (gme->col_data[c].enable_enum)(&gme->g,mi,r,c);
1291 _GMenuCreatePopupMenu(gme->nested,event, mi, GME_FinishChoice);
1292 }
1293
1294 static void GME_EnumStringDispatch(GWindow gw, GMenuItem *mi, GEvent *e) {
1295 GMatrixEdit *gme = GDrawGetUserData(gw);
1296 int r = gme->active_row, c = gme->active_col;
1297
1298 if ( (intptr_t) mi->ti.userdata == GME_NoChange )
1299 return;
1300
1301 free(gme->data[r*gme->cols+c].u.md_str);
1302 if ( gme->col_data[c].me_type==me_stringchoicetrans )
1303 gme->data[r*gme->cols+c].u.md_str = copy( (char *) mi->ti.userdata );
1304 else if ( gme->col_data[c].me_type==me_stringchoicetag ) {
1305 char buf[8];
1306 buf[0] = ((intptr_t) mi->ti.userdata)>>24;
1307 buf[1] = ((intptr_t) mi->ti.userdata)>>16;
1308 buf[2] = ((intptr_t) mi->ti.userdata)>>8;
1309 buf[3] = ((intptr_t) mi->ti.userdata)&0xff;
1310 buf[4] = '\0';
1311 gme->data[r*gme->cols+c].u.md_str = copy( buf );
1312 } else
1313 gme->data[r*gme->cols+c].u.md_str = u2utf8_copy( mi->ti.text );
1314
1315 if ( gme->finishedit != NULL )
1316 (gme->finishedit)(&gme->g,r,c,gme->wasnew);
1317 GME_AdjustCol(gme,c);
1318 gme->wasnew = false;
1319 }
1320
1321 static void GME_StringChoices(GMatrixEdit *gme,GEvent *event,int r,int c) {
1322 GMenuItem *mi = gme->col_data[c].enum_vals;
1323 char *val = gme->data[r*gme->cols+c].u.md_str;
1324 int i;
1325
1326 if ( gme->col_data[c].me_type==me_stringchoicetag ) {
1327 char buf[8];
1328 for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.line || mi[i].ti.image!=NULL; ++i ) {
1329 buf[0] = ((intptr_t) mi[i].ti.userdata)>>24;
1330 buf[1] = ((intptr_t) mi[i].ti.userdata)>>16;
1331 buf[2] = ((intptr_t) mi[i].ti.userdata)>>8;
1332 buf[3] = ((intptr_t) mi[i].ti.userdata)&0xff;
1333 buf[4] = '\0';
1334 mi[i].ti.selected = mi[i].ti.checked = !gme->wasnew && val!=NULL &&
1335 strcmp(buf,val)==0;
1336 }
1337 } else if ( gme->col_data[c].me_type==me_stringchoicetrans ) {
1338 for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.line || mi[i].ti.image!=NULL; ++i )
1339 mi[i].ti.selected = mi[i].ti.checked = !gme->wasnew && val!=NULL &&
1340 strcmp((char *) mi[i].ti.userdata,val)==0;
1341 }
1342 if ( gme->col_data[c].enable_enum!=NULL )
1343 (gme->col_data[c].enable_enum)(&gme->g,mi,r,c);
1344 _GMenuCreatePopupMenu(gme->nested,event, mi, GME_FinishChoice);
1345 }
1346
1347 static void GMatrixEdit_StartSubGadgets(GMatrixEdit *gme,int r, int c,GEvent *event) {
1348 int i, markpos, lastc;
1349 struct matrix_data *d;
1350 int old_off_left = gme->off_left; /* We sometimes scroll */
1351 int oldr;
1352 GRect size;
1353
1354 GDrawGetSize(gme->nested,&size);
1355
1356 for ( lastc = gme->cols-1; lastc>0 && gme->col_data[lastc].hidden; --lastc );
1357 /* new row */
1358 if ( c==0 && r==gme->rows && event->type == et_mousedown &&
1359 event->u.mouse.button==1 && !gme->no_edit ) {
1360 if ( gme->rows>=gme->row_max )
1361 gme->data = realloc(gme->data,(gme->row_max+=10)*gme->cols*sizeof(struct matrix_data));
1362 ++gme->rows;
1363 for ( i=0; i<gme->cols; ++i ) {
1364 d = &gme->data[r*gme->cols+i];
1365 memset(d,0,sizeof(*d));
1366 switch ( gme->col_data[i].me_type ) {
1367 case me_string: case me_bigstr:
1368 case me_stringchoice: case me_stringchoicetrans: case me_stringchoicetag:
1369 case me_funcedit:
1370 case me_onlyfuncedit:
1371 d->u.md_str = copy("");
1372 break;
1373 case me_enum:
1374 d->u.md_ival = (int) (intptr_t) gme->col_data[i].enum_vals[0].ti.userdata;
1375 break;
1376 default: break;
1377 }
1378 }
1379 if ( gme->initrow!=NULL )
1380 (gme->initrow)(&gme->g,r);
1381 GME_FixScrollBars(gme);
1382 GDrawRequestExpose(gme->nested,NULL,false);
1383 gme->wasnew = true;
1384 }
1385
1386 if ( c<0 || r<0 || c==gme->cols || r>=gme->rows || gme->col_data[c].disabled )
1387 return;
1388 oldr = gme->active_row;
1389 gme->active_col = c; gme->active_row = r;
1390 if ( r!=oldr && oldr!=-1 ) {
1391 GRect r;
1392 r.x = gme->col_data[c].x - gme->off_left; r.width = gme->col_data[c].width;
1393 r.y = (oldr-gme->off_top)*(gme->fh+gme->vpad); r.height = gme->fh+gme->vpad;
1394 if ( r.y+r.height>0 )
1395 GDrawRequestExpose(gme->nested,&r,false);
1396 }
1397 GME_EnableDelete(gme);
1398
1399 GMatrixEditScrollToRowCol(&gme->g,r,c);
1400 d = &gme->data[r*gme->cols+c];
1401
1402 markpos = ( c == lastc && gme->col_data[c].x + gme->col_data[c].width > size.width ) ?
1403 size.width - gme->col_data[c].x : gme->col_data[c].width;
1404 markpos -= (gme->mark_size+gme->mark_skip);
1405 if ( markpos < 0 ) markpos = 0;
1406 if ( event->type==et_mousedown && event->u.mouse.button==3 ) {
1407 if ( gme->popupmenu!=NULL )
1408 (gme->popupmenu)(&gme->g,event,r,c);
1409 } else if ( d->frozen ) {
1410 GDrawBeep(NULL);
1411 } else if ( gme->no_edit ) {
1412 /* Twiddle toes */;
1413 } else if ( gme->col_data[c].me_type==me_enum ) {
1414 GME_Choices(gme,event,r,c);
1415 } else if ( gme->col_data[c].me_type==me_button ) {
1416 char *ret = (gme->col_data[c].func)(&gme->g,r,c);
1417 if ( ret!=NULL ) {
1418 /* I don't bother validating it because I expect the function to */
1419 /* do that for me */
1420 free(gme->data[r*gme->cols+c].u.md_str);
1421 gme->data[r*gme->cols+c].u.md_str = ret;
1422 GDrawRequestExpose(gme->nested,NULL,false);
1423 }
1424 } else if ( ((gme->col_data[c].me_type==me_funcedit ||
1425 gme->col_data[c].me_type==me_onlyfuncedit ) &&
1426 event->type==et_mousedown &&
1427 event->u.mouse.x>gme->col_data[c].x + markpos - old_off_left ) ||
1428 (gme->col_data[c].me_type==me_onlyfuncedit &&
1429 event->type==et_mousedown &&
1430 (gme->wasnew || event->u.mouse.clicks==2)) ) {
1431 char *ret = (gme->col_data[c].func)(&gme->g,r,c);
1432 if ( ret!=NULL ) {
1433 /* I don't bother validating it because I expect the function to */
1434 /* do that for me */
1435 free(gme->data[r*gme->cols+c].u.md_str);
1436 gme->data[r*gme->cols+c].u.md_str = ret;
1437 if ( gme->finishedit != NULL )
1438 (gme->finishedit)(&gme->g,r,c,gme->wasnew);
1439 GDrawRequestExpose(gme->nested,NULL,false);
1440 gme->wasnew = false; // This is an attempted hack by somebody (Frank) who admittedly has no idea what is happening in all of this sparsely commented code.
1441 }
1442 } else if ( gme->col_data[c].me_type==me_onlyfuncedit ) {
1443 /* Don't allow other editing */
1444 } else if ( (gme->col_data[c].me_type==me_stringchoice ||
1445 gme->col_data[c].me_type==me_stringchoicetrans ||
1446 gme->col_data[c].me_type==me_stringchoicetag) &&
1447 event->type==et_mousedown &&
1448 event->u.mouse.x>gme->col_data[c].x + markpos - old_off_left ) {
1449 GME_StringChoices(gme,event,r,c);
1450 } else {
1451 char *str = MD_Text(gme,gme->active_row,gme->active_col);
1452 if ( str==NULL )
1453 str = copy("");
1454 if ( str!=NULL &&
1455 (g_utf8_strlen(str, -1)>40 || strchr(str,'\n')!=NULL || gme->col_data[c].me_type == me_bigstr))
1456 GME_StrBigEdit(gme,str);
1457 else
1458 GME_StrSmallEdit(gme,str,event);
1459 free(str);
1460 }
1461 }
1462
1463 static void GMatrixEdit_MouseEvent(GMatrixEdit *gme,GEvent *event) {
1464 int r = event->u.mouse.y/(gme->fh+gme->vpad) + gme->off_top;
1465 int x = event->u.mouse.x + gme->off_left;
1466 int c, i;
1467
1468 if ( gme->edit_active && event->type==et_mousemove )
1469 return;
1470 for ( c=0; c<gme->cols; ++c ) {
1471 if ( gme->col_data[c].hidden )
1472 continue;
1473 if ( x>=gme->col_data[c].x && x<=gme->col_data[c].x+gme->col_data[c].width )
1474 break;
1475 }
1476 if ( event->type==et_mousemove && gme->reportmousemove!=NULL ) {
1477 (gme->reportmousemove)(&gme->g,r,c);
1478 return;
1479 }
1480 if ( gme->edit_active ) {
1481 if ( (r = GME_FinishEditPreserve(gme,r))== -1 )
1482 return;
1483 }
1484 if ( event->type==et_mousedown )
1485 GMatrixEdit_StartSubGadgets(gme,r,c,event);
1486 else if ( event->type==et_mousemove ) {
1487 if ( c<gme->cols && gme->col_data[c].me_type==me_stringchoicetrans &&
1488 gme->col_data[c].enum_vals!=NULL &&
1489 r<gme->rows ) {
1490 char *str = gme->data[r*gme->cols+c].u.md_str;
1491 GMenuItem *enums = gme->col_data[c].enum_vals;
1492 for ( i=0; enums[i].ti.text!=NULL || enums[i].ti.line ; ++i ) {
1493 if ( enums[i].ti.userdata!=NULL && strcmp(enums[i].ti.userdata,str)==0 ) {
1494 if ( enums[i].ti.text_is_1byte )
1495 GGadgetPreparePopup8(gme->nested,(char *) enums[i].ti.text);
1496 else
1497 GGadgetPreparePopup(gme->nested,enums[i].ti.text);
1498 break;
1499 }
1500 }
1501 } else if ( c<gme->cols && gme->col_data[c].me_type==me_stringchoicetag &&
1502 gme->col_data[c].enum_vals!=NULL &&
1503 r<gme->rows ) {
1504 char *str = gme->data[r*gme->cols+c].u.md_str, buf[8];
1505 GMenuItem *enums = gme->col_data[c].enum_vals;
1506 for ( i=0; enums[i].ti.text!=NULL || enums[i].ti.line ; ++i ) {
1507 buf[0] = ((intptr_t) enums[i].ti.userdata)>>24;
1508 buf[1] = ((intptr_t) enums[i].ti.userdata)>>16;
1509 buf[2] = ((intptr_t) enums[i].ti.userdata)>>8;
1510 buf[3] = ((intptr_t) enums[i].ti.userdata)&0xff;
1511 buf[4] = '\0';
1512 if ( enums[i].ti.userdata!=NULL && strcmp(buf,str)==0 ) {
1513 if ( enums[i].ti.text_is_1byte )
1514 GGadgetPreparePopup8(gme->nested,(char *) enums[i].ti.text);
1515 else
1516 GGadgetPreparePopup(gme->nested,enums[i].ti.text);
1517 break;
1518 }
1519 }
1520 } else if ( gme->g.popup_msg!=NULL )
1521 GGadgetPreparePopup(gme->nested,gme->g.popup_msg);
1522 }
1523 }
1524
1525 static void GMatrixEdit_SubExpose(GMatrixEdit *gme,GWindow pixmap,GEvent *event) {
1526 int r,c, lastc, kludge;
1527 char *buf, *str, *pt;
1528 GRect size;
1529 GRect clip, old;
1530 Color fg, mkbg;
1531 struct matrix_data *data;
1532 GMenuItem *mi;
1533
1534 GDrawGetSize(gme->nested,&size);
1535 if ( gme->g.state!=gs_enabled )
1536 GDrawFillRect(pixmap,&size,gme->g.box->disabled_background);
1537
1538 GDrawDrawLine(pixmap,0,0,0,size.height,gmatrixedit_rules);
1539 /* Make sure the last visible column ends at (or after) the edge */
1540 for ( lastc = gme->cols-1; lastc>0 && gme->col_data[lastc].hidden; --lastc );
1541 if ( lastc>=0 && gme->col_data[lastc].x+gme->col_data[lastc].width < size.width ) {
1542 gme->col_data[lastc].width = size.width - gme->col_data[lastc].x;
1543 for ( c=lastc+1; c<gme->cols; ++c )
1544 gme->col_data[c].x = size.width;
1545 }
1546 for ( c=0; c<gme->cols-1; ++c ) if ( !gme->col_data[c].hidden )
1547 GDrawDrawLine(pixmap,
1548 gme->col_data[c].x+gme->col_data[c].width+gme->hpad/2-gme->off_left,0,
1549 gme->col_data[c].x+gme->col_data[c].width+gme->hpad/2-gme->off_left,size.height,
1550 gmatrixedit_rules);
1551 GDrawDrawLine(pixmap,0,0,size.width,0,gmatrixedit_rules);
1552 for ( r=gme->off_top; r<=gme->rows && (r-gme->off_top)*(gme->fh+gme->vpad)<=gme->g.inner.height; ++r )
1553 GDrawDrawLine(pixmap,
1554 0, (r-gme->off_top)*(gme->fh+gme->vpad)-1,
1555 size.width,(r-gme->off_top)*(gme->fh+gme->vpad)-1,
1556 gmatrixedit_rules);
1557
1558
1559 GDrawSetFont(pixmap,gme->font);
1560 for ( r=event->u.expose.rect.y/(gme->fh+gme->vpad);
1561 r<=(event->u.expose.rect.y+event->u.expose.rect.height+gme->fh+gme->vpad-1)/gme->fh &&
1562 r+gme->off_top<=gme->rows;
1563 ++r ) {
1564 int y, lastc;
1565 clip.y = r*(gme->fh+gme->vpad);
1566 y = clip.y + gme->font_as; /* I know this looks odd, but it seems to work when we grab a glyph from another font with cairo */
1567 clip.height = gme->fh;
1568 for ( lastc = gme->cols-1; lastc>0 && gme->col_data[lastc].hidden; --lastc );
1569 /* Compensate for the top border line */
1570 if ( clip.y <= 0 ) {
1571 clip.y = 1; clip.height += ( clip.y - 1 );
1572 }
1573
1574 for ( c=0; c<gme->cols; ++c ) {
1575 if ( gme->col_data[c].hidden )
1576 continue;
1577 if ( gme->col_data[c].x + gme->col_data[c].width < gme->off_left && gme->col_data[c].width > gme->hpad)
1578 continue;
1579 clip.x = gme->col_data[c].x - gme->off_left;
1580 clip.width = gme->col_data[c].width;
1581 if ( gme->col_data[c].me_type==me_button ) {
1582 int temp;
1583 clip.height += 2;
1584 GBoxDrawBackground(pixmap,&clip,&gmatrixedit_button_box,
1585 gme->col_data[c].disabled ? gs_disabled : gs_enabled, false);
1586 GBoxDrawBorder(pixmap,&clip,&gmatrixedit_button_box,
1587 gme->col_data[c].disabled ? gs_disabled : gs_enabled, false);
1588 clip.height -= 2;
1589 temp = GBoxBorderWidth(pixmap,&gmatrixedit_button_box)+2;
1590 clip.x += temp;
1591 clip.width -= temp;
1592 } else if ( gme->col_data[c].disabled && gme->g.box->disabled_background!=COLOR_TRANSPARENT )
1593 GDrawFillRect(pixmap,&clip,gme->g.box->disabled_background);
1594 else if ( gme->active_row==r+gme->off_top )
1595 GDrawFillRect(pixmap,&clip,gmatrixedit_activebg);
1596 if ( gme->col_data[c].me_type == me_stringchoice ||
1597 gme->col_data[c].me_type == me_stringchoicetrans ||
1598 gme->col_data[c].me_type == me_stringchoicetag ||
1599 gme->col_data[c].me_type == me_onlyfuncedit ||
1600 gme->col_data[c].me_type == me_funcedit ) {
1601
1602 if ( c == lastc ) {
1603 if ( clip.x < size.width && clip.x + clip.width > size.width )
1604 clip.width = size.width - clip.x;
1605 else if ( clip.x >= size.width )
1606 clip.width = 0;
1607 }
1608 if ( clip.width >= (gme->mark_size+gme->mark_skip) )
1609 clip.width -= (gme->mark_size+gme->mark_skip);
1610 else
1611 clip.width = 0;
1612 }
1613 if ( clip.width>0 ) {
1614 GDrawPushClip(pixmap,&clip,&old);
1615 str = NULL;
1616 if ( r+gme->off_top==gme->rows ) {
1617 if ( !gme->no_edit ) {
1618 if ( gme->newtext!=NULL )
1619 buf = smprintf( "<%s>", gme->newtext );
1620 else
1621 buf = smprintf( "<%s>", S_("Row|New") );
1622 GDrawDrawText8( pixmap, gme->col_data[0].x - gme->off_left,y,
1623 (char *) buf, -1, gmatrixedit_activecol );
1624 free( buf ) ; buf = NULL ;
1625 }
1626 } else {
1627 data = &gme->data[(r+gme->off_top)*gme->cols+c];
1628 fg = gme->g.state==gs_disabled?gme->g.box->disabled_foreground:
1629 data->frozen ? gmatrixedit_frozencol:
1630 gme->g.box->main_foreground==COLOR_DEFAULT?GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(pixmap)):
1631 gme->g.box->main_foreground;
1632 switch ( gme->col_data[c].me_type ) {
1633 case me_enum:
1634 mi = FindMi(gme->col_data[c].enum_vals,data->u.md_ival);
1635 if ( mi!=NULL ) {
1636 if ( mi->ti.text_is_1byte )
1637 GDrawDrawText8(pixmap,clip.x,y,(char *)mi->ti.text,-1,fg);
1638 else
1639 GDrawDrawText(pixmap,clip.x,y,mi->ti.text,-1,fg);
1640 break;
1641 }
1642 /* Fall through into next case */
1643 default:
1644 kludge = gme->edit_active; gme->edit_active = false;
1645 str = MD_Text(gme,r+gme->off_top,c);
1646 if ( str==NULL && gme->col_data[c].me_type==me_button )
1647 str = copy("...");
1648 gme->edit_active = kludge;
1649 break;
1650 }
1651 if ( str!=NULL ) {
1652 pt = strchr(str,'\n');
1653 GDrawDrawText8(pixmap,clip.x,y,str,pt==NULL?-1:pt-str,fg);
1654 free(str);
1655 }
1656 }
1657 GDrawPopClip(pixmap,&old);
1658 }
1659 if ( gme->col_data[c].me_type == me_stringchoice ||
1660 gme->col_data[c].me_type == me_stringchoicetrans ||
1661 gme->col_data[c].me_type == me_stringchoicetag ||
1662 gme->col_data[c].me_type == me_onlyfuncedit ||
1663 gme->col_data[c].me_type == me_funcedit ) {
1664 GRect mr;
1665 mr.x = clip.x + clip.width; mr.width = gme->mark_size+gme->mark_skip;
1666 mr.y = clip.y; mr.height = clip.height;
1667
1668 mkbg = gme->active_row==r+gme->off_top ?
1669 gmatrixedit_activebg : GDrawGetDefaultBackground(GDrawGetDisplayOfWindow(pixmap));
1670 GDrawFillRect(pixmap,&mr,mkbg);
1671 GListMarkDraw(pixmap,
1672 mr.x + gme->mark_skip + (gme->mark_size - gme->mark_length)/2,
1673 clip.y,
1674 clip.height,
1675 gme->g.state);
1676 }
1677 if ( r+gme->off_top==gme->rows )
1678 return;
1679 }
1680 }
1681 }
1682
1683 static int matrixeditsub_e_h(GWindow gw, GEvent *event) {
1684 GMatrixEdit *gme = (GMatrixEdit *) GDrawGetUserData(gw);
1685 int r,c;
1686
1687 GGadgetPopupExternalEvent(event);
1688 if (( event->type==et_mouseup || event->type==et_mousedown ) &&
1689 (event->u.mouse.button>=4 && event->u.mouse.button<=7)) {
1690 int isv = event->u.mouse.button<=5;
1691 if ( event->u.mouse.state&ksm_shift ) isv = !isv;
1692 if ( isv && gme->vsb!=NULL )
1693 return( GGadgetDispatchEvent(gme->vsb,event));
1694 else if ( !isv && gme->hsb!=NULL )
1695 return( GGadgetDispatchEvent(gme->hsb,event));
1696 else
1697 return( true );
1698 }
1699
1700 switch ( event->type ) {
1701 case et_expose:
1702 GDrawSetLineWidth(gw,0);
1703 GMatrixEdit_SubExpose(gme,gw,event);
1704 break;
1705 case et_mousedown:
1706 case et_mouseup:
1707 if ( gme->g.state == gs_disabled )
1708 return( false );
1709 case et_mousemove:
1710 GMatrixEdit_MouseEvent(gme,event);
1711 break;
1712 case et_char:
1713 if ( gme->g.state == gs_disabled )
1714 return( false );
1715 r = gme->active_row;
1716 switch ( event->u.chr.keysym ) {
1717 case GK_Up: case GK_KP_Up:
1718 if ( (!gme->edit_active || GME_FinishEdit(gme)) &&
1719 gme->active_row>0 )
1720 GMatrixEdit_StartSubGadgets(gme,gme->active_row-1,gme->active_col,event);
1721 return( true );
1722 break;
1723 case GK_Down: case GK_KP_Down:
1724 if ( (!gme->edit_active || GME_FinishEdit(gme)) &&
1725 gme->active_row<gme->rows-(gme->active_col!=0) )
1726 GMatrixEdit_StartSubGadgets(gme,gme->active_row+1,gme->active_col,event);
1727 return( true );
1728 break;
1729 case GK_Left: case GK_KP_Left:
1730 case GK_BackTab:
1731 backtab:
1732 if ( (!gme->edit_active || (r=GME_FinishEditPreserve(gme,r))!=-1) &&
1733 gme->active_col>0 ) {
1734 for ( c = gme->active_col-1; c>=0 && gme->col_data[c].hidden; --c );
1735 if ( c>=0 )
1736 GMatrixEdit_StartSubGadgets(gme,r,c,event);
1737 }
1738 return( true );
1739 break;
1740 case GK_Tab:
1741 if ( event->u.chr.state&ksm_shift )
1742 goto backtab;
1743 /* Else fall through */
1744 case GK_Right: case GK_KP_Right:
1745 if ( (!gme->edit_active || (r=GME_FinishEditPreserve(gme,r))!=-1) &&
1746 gme->active_col<gme->cols-1 ) {
1747 for ( c = gme->active_col+1; c<gme->cols && gme->col_data[c].hidden; ++c );
1748 if ( c<gme->cols )
1749 GMatrixEdit_StartSubGadgets(gme,r,c,event);
1750 }
1751 return( true );
1752 break;
1753 case GK_Return: case GK_KP_Enter:
1754 if ( gme->edit_active && (r=GME_FinishEditPreserve(gme,r))!=-1 ) {
1755 GEvent dummy;
1756 memset(&dummy,0,sizeof(dummy));
1757 dummy.w = event->w;
1758 dummy.type = et_mousedown;
1759 dummy.u.mouse.state = event->u.chr.state;
1760 dummy.u.mouse.x = gme->off_left+gme->col_data[0].x+1;
1761 dummy.u.mouse.y = gme->off_top + (r+1)*(gme->fh+gme->vpad);
1762 dummy.u.mouse.button = 1;
1763 GMatrixEdit_StartSubGadgets(gme,r+1,0,&dummy);
1764 }
1765 return( true );
1766 }
1767 if ( gme->handle_key!=NULL )
1768 return( (gme->handle_key)(&gme->g,event) );
1769 return( false );
1770 break;
1771 case et_destroy:
1772 if ( gme!=NULL )
1773 gme->nested = NULL;
1774 break;
1775 case et_controlevent:
1776 if ( gme->reporttextchanged!=NULL )
1777 (gme->reporttextchanged)(&gme->g,gme->active_row,gme->active_col,gme->tf);
1778 break;
1779 default: break;
1780 }
1781 return( true );
1782 }
1783
1784 static void GME_HScroll(GMatrixEdit *gme,struct sbevent *sb) {
1785 int newpos = gme->off_left;
1786 GRect size;
1787 int hend = gme->col_data[gme->cols-1].x + gme->col_data[gme->cols-1].width;
1788
1789 GDrawGetSize(gme->nested,&size);
1790 switch( sb->type ) {
1791 case et_sb_top:
1792 newpos = 0;
1793 break;
1794 case et_sb_uppage:
1795 newpos -= 9*size.width/10;
1796 break;
1797 case et_sb_up:
1798 newpos -= size.width/15;
1799 break;
1800 case et_sb_down:
1801 newpos += size.width/15;
1802 break;
1803 case et_sb_downpage:
1804 newpos += 9*size.width/10;
1805 break;
1806 case et_sb_bottom:
1807 newpos = hend;
1808 break;
1809 case et_sb_thumb:
1810 case et_sb_thumbrelease:
1811 newpos = sb->pos;
1812 break;
1813 case et_sb_halfup: case et_sb_halfdown: break;
1814 }
1815
1816 if ( newpos + size.width > hend )
1817 newpos = hend - size.width;
1818 if ( newpos<0 )
1819 newpos = 0;
1820 if ( newpos!=gme->off_left ) {
1821 int lastc, diff = gme->off_left-newpos;
1822 GRect clip;
1823 gme->off_left = newpos;
1824 GScrollBarSetPos(gme->hsb,newpos);
1825
1826 clip.y = 1;
1827 clip.height = size.height - 1;
1828 for ( lastc = gme->cols-1; lastc>0 && gme->col_data[lastc].hidden; --lastc );
1829
1830 gme->off_left = newpos;
1831 GScrollBarSetPos(gme->hsb,newpos);
1832 clip.x = 1; clip.y = 1; clip.width = size.width-1; clip.height = size.height-1;
1833
1834 if (( gme->col_data[lastc].me_type == me_stringchoice ||
1835 gme->col_data[lastc].me_type == me_stringchoicetrans ||
1836 gme->col_data[lastc].me_type == me_stringchoicetag ||
1837 gme->col_data[lastc].me_type == me_onlyfuncedit ||
1838 gme->col_data[lastc].me_type == me_funcedit ) &&
1839 gme->col_data[lastc].x <= gme->off_left + size.width - (gme->mark_size + gme->mark_skip) ) {
1840 int xdiff = gme->off_left + size.width - (gme->mark_size + gme->mark_skip) - gme->col_data[lastc].x;
1841 /* Catch the moment when we should stop scrolling the list mark area */
1842 if ( xdiff + diff < 0 ) {
1843 GDrawScroll( gme->nested,&clip,xdiff + diff,0 );
1844 diff = -xdiff;
1845 }
1846 clip.width -= (gme->mark_size + gme->mark_skip);
1847 }
1848 GDrawScroll( gme->nested,&clip,diff,0 );
1849 GME_PositionEdit(gme);
1850 _ggadget_redraw((GGadget*)gme);
1851 }
1852 }
1853
1854 static void GME_VScroll(GMatrixEdit *gme,struct sbevent *sb) {
1855 int newpos = gme->off_top;
1856 int page;
1857 GRect size;
1858
1859 GDrawGetSize(gme->nested,&size);
1860 page = size.height/(gme->fh+gme->vpad);
1861
1862 switch( sb->type ) {
1863 case et_sb_top:
1864 newpos = 0;
1865 break;
1866 case et_sb_uppage:
1867 newpos -= 9*page/10;
1868 break;
1869 case et_sb_up:
1870 newpos--;
1871 break;
1872 case et_sb_down:
1873 newpos++;
1874 break;
1875 case et_sb_downpage:
1876 newpos += 9*page/10;
1877 break;
1878 case et_sb_bottom:
1879 newpos = gme->rows+1;
1880 break;
1881 case et_sb_thumb:
1882 case et_sb_thumbrelease:
1883 newpos = sb->pos;
1884 break;
1885 case et_sb_halfup: case et_sb_halfdown: break;
1886 }
1887 if ( newpos + page > gme->rows+1 )
1888 newpos = gme->rows+1 - page;
1889 if ( newpos<0 )
1890 newpos = 0;
1891 if ( newpos!=gme->off_top ) {
1892 int diff = (newpos-gme->off_top)*(gme->fh+gme->vpad);
1893 GRect r;
1894 gme->off_top = newpos;
1895 GScrollBarSetPos(gme->vsb,newpos);
1896 r.x = 1; r.y = 1; r.width = size.width-1; r.height = size.height-1;
1897 GDrawScroll(gme->nested,&r,0,diff);
1898 GME_PositionEdit(gme);
1899 GDrawRequestExpose(gme->nested,&size,false);
1900 }
1901 }
1902
1903 static int _GME_HScroll(GGadget *g, GEvent *e) {
1904 if ( e->type==et_controlevent && e->u.control.subtype == et_scrollbarchange ) {
1905 GMatrixEdit *gme = (GMatrixEdit *) g->data;
1906 GME_HScroll(gme,&e->u.control.u.sb);
1907 }
1908 return( true );
1909 }
1910
1911 static int _GME_VScroll(GGadget *g, GEvent *e) {
1912 if ( e->type==et_controlevent && e->u.control.subtype == et_scrollbarchange ) {
1913 GMatrixEdit *gme = (GMatrixEdit *) g->data;
1914 GME_VScroll(gme,&e->u.control.u.sb);
1915 }
1916 return( true );
1917 }
1918
1919 static int _GME_DeleteActive(GGadget *g, GEvent *e) {
1920 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
1921 GMatrixEdit *gme = (GMatrixEdit *) g->data;
1922 GME_DeleteActive(gme);
1923 }
1924 return( true );
1925 }
1926
1927 static GMenuItem *GMenuItemFromTI(GTextInfo *ti,int is_enum) {
1928 int cnt;
1929 GMenuItem *mi;
1930
1931 for ( cnt=0; ti[cnt].text!=NULL || ti[cnt].line; ++cnt );
1932 mi = calloc((cnt+1),sizeof(GMenuItem));
1933 for ( cnt=0; ti[cnt].text!=NULL || ti[cnt].line; ++cnt ) {
1934 mi[cnt].ti = ti[cnt];
1935 if ( ti[cnt].bg == ti[cnt].fg )
1936 mi[cnt].ti.bg = mi[cnt].ti.fg = COLOR_DEFAULT;
1937 if ( mi[cnt].ti.text!=NULL ) {
1938 if ( ti[cnt].text_is_1byte )
1939 mi[cnt].ti.text = (unichar_t *) copy( (char *) mi[cnt].ti.text );
1940 else
1941 mi[cnt].ti.text = u_copy( mi[cnt].ti.text );
1942 mi[cnt].ti.checkable = true;
1943 mi[cnt].invoke = is_enum ? GME_EnumDispatch : GME_EnumStringDispatch;
1944 }
1945 }
1946 return( mi );
1947 }
1948
1949 /* GMatrixElement: External interface *************************************** */
1950 GGadget *GMatrixEditCreate(struct gwindow *base, GGadgetData *gd,void *data) {
1951 struct matrixinit *matrix = gd->u.matrix;
1952 GMatrixEdit *gme = calloc(1,sizeof(GMatrixEdit));
1953 int r, c, bp;
1954 int x;
1955 GRect outer;
1956 GRect pos;
1957 GWindowAttrs wattrs;
1958 int sbwidth = GDrawPointsToPixels(base,_GScrollBar_Width);
1959 GGadgetData sub_gd;
1960 GTextInfo label;
1961 int as, ds, ld;
1962
1963 _GMatrixEdit_Init();
1964
1965 gme->g.funcs = &gmatrixedit_funcs;
1966 _GGadget_Create(&gme->g,base,gd,data,&gmatrixedit_box);
1967 gme->g.takes_input = true; gme->g.takes_keyboard = false; gme->g.focusable = false;
1968
1969 gme->font = gmatrixedit_font.fi;
1970 gme->titfont = gmatrixedit_titfont.fi;
1971 GDrawWindowFontMetrics(base,gme->font,&as, &ds, &ld);
1972 gme->font_as = gme->as = as;
1973 gme->font_fh = gme->fh = as+ds;
1974
1975 gme->rows = matrix->initial_row_cnt; gme->cols = matrix->col_cnt;
1976 gme->row_max = gme->rows;
1977 gme->hpad = gme->vpad = GDrawPointsToPixels(base,2);
1978
1979 gme->col_data = calloc(gme->cols,sizeof(struct col_data));
1980 for ( c=0; c<gme->cols; ++c ) {
1981 gme->col_data[c].me_type = matrix->col_init[c].me_type;
1982 gme->col_data[c].func = matrix->col_init[c].func;
1983 if ( matrix->col_init[c].enum_vals!=NULL )
1984 gme->col_data[c].enum_vals = GMenuItemFromTI(matrix->col_init[c].enum_vals,
1985 matrix->col_init[c].me_type==me_enum );
1986 else
1987 gme->col_data[c].enum_vals = NULL;
1988 gme->col_data[c].enable_enum = matrix->col_init[c].enable_enum;
1989 gme->col_data[c].title = copy( matrix->col_init[c].title );
1990 if ( gme->col_data[c].title!=NULL ) gme->has_titles = true;
1991 gme->col_data[c].fixed = false;
1992 }
1993
1994 gme->data = calloc(gme->rows*gme->cols,sizeof(struct matrix_data));
1995 memcpy(gme->data,matrix->matrix_data,gme->rows*gme->cols*sizeof(struct matrix_data));
1996 for ( c=0; c<gme->cols; ++c ) {
1997 enum me_type me_type = gme->col_data[c].me_type;
1998 if ( me_type==me_string || me_type==me_bigstr || me_type==me_func ||
1999 me_type==me_button || me_type==me_onlyfuncedit ||
2000 me_type==me_funcedit || me_type==me_stringchoice ||
2001 me_type==me_stringchoicetrans || me_type==me_stringchoicetag ) {
2002 for ( r=0; r<gme->rows; ++r )
2003 gme->data[r*gme->cols+c].u.md_str = copy(gme->data[r*gme->cols+c].u.md_str);
2004 }
2005 }
2006
2007 gme->mark_length = GDrawPointsToPixels(base,_GListMarkSize);
2008 gme->mark_size = gme->mark_length +
2009 2*GBoxBorderWidth(base,&_GListMark_Box);
2010 gme->mark_skip = GDrawPointsToPixels(base,_GGadget_TextImageSkip);
2011
2012 /* Can't do this earlier. It depends on matrix_data being set */
2013 x = 1;
2014 for ( c=0; c<gme->cols; ++c ) {
2015 gme->col_data[c].x = x;
2016 gme->col_data[c].width = GME_ColWidth(gme,c);
2017 x += gme->col_data[c].width + gme->hpad;
2018 }
2019
2020 gme->pressed_col = -1;
2021 gme->active_col = gme->active_row = -1;
2022 gme->initrow = matrix->initrow;
2023 gme->finishedit = matrix->finishedit;
2024 gme->candelete = matrix->candelete;
2025 gme->popupmenu = matrix->popupmenu;
2026 gme->handle_key = matrix->handle_key;
2027 gme->bigedittitle = matrix->bigedittitle;
2028
2029 GMatrixEdit_GetDesiredSize(&gme->g,&outer,NULL);
2030 if ( gme->g.r.width==0 )
2031 gme->g.r.width = outer.width;
2032 else
2033 gme->g.desired_width = gme->g.r.width;
2034 if ( gme->g.r.height==0 )
2035 gme->g.r.height = outer.height;
2036 else
2037 gme->g.desired_height = gme->g.r.height;
2038 bp = GBoxBorderWidth(gme->g.base,gme->g.box);
2039 gme->g.inner.x = gme->g.r.x + bp;
2040 gme->g.inner.y = gme->g.r.y + bp;
2041 gme->g.inner.width = gme->g.r.width -2*bp;
2042 gme->g.inner.height = gme->g.r.height -2*bp;
2043
2044 memset(&sub_gd,0,sizeof(sub_gd));
2045 memset(&label,0,sizeof(label));
2046 sub_gd.pos.x = sub_gd.pos.y = 1; sub_gd.pos.width = sub_gd.pos.height = 0;
2047 label.text = (unichar_t *) _("Delete");
2048 label.text_is_1byte = true;
2049 sub_gd.flags = gg_visible | gg_pos_in_pixels;
2050 sub_gd.label = &label;
2051 sub_gd.handle_controlevent = _GME_DeleteActive;
2052 gme->del = GButtonCreate(base,&sub_gd,gme);
2053 gme->del->contained = true;
2054
2055 if ( gme->g.r.height<10 ) {
2056 int extra = 2*bp+ sbwidth + (gme->has_titles?gme->fh:0) +
2057 gme->del->r.height+DEL_SPACE;
2058 gme->g.r.height = extra + gme->g.r.height*(gme->fh + gme->vpad);
2059 gme->g.inner.height = gme->g.r.height - 2*bp;
2060 }
2061
2062 memset(&wattrs,0,sizeof(wattrs));
2063 if ( gme->g.box->main_background!=COLOR_TRANSPARENT )
2064 wattrs.mask = wam_events|wam_cursor|wam_backcol;
2065 else
2066 wattrs.mask = wam_events|wam_cursor;
2067 wattrs.event_masks = ~(1<<et_charup);
2068 wattrs.cursor = ct_pointer;
2069 wattrs.background_color = gme->g.box->main_background;
2070 pos = gme->g.inner;
2071 pos.width -= sbwidth;
2072 pos.height -= sbwidth + gme->del->inner.height+DEL_SPACE;
2073 if ( gme->has_titles ) {
2074 pos.y += gme->fh;
2075 pos.height -= gme->fh;
2076 }
2077 gme->nested = GWidgetCreateSubWindow(base,&pos,matrixeditsub_e_h,gme,&wattrs);
2078
2079 GGadgetMove(gme->del,
2080 (gme->g.inner.width-gme->del->r.width)/2,
2081 gme->g.inner.height-gme->del->r.height-DEL_SPACE/2);
2082
2083 sub_gd.pos = pos;
2084 sub_gd.pos.x = pos.x+pos.width; sub_gd.pos.width = sbwidth;
2085 sub_gd.flags = (gd->flags & (gg_visible | gg_enabled)) | gg_sb_vert | gg_pos_in_pixels;
2086 sub_gd.handle_controlevent = _GME_VScroll;
2087 gme->vsb = GScrollBarCreate(base,&sub_gd,gme);
2088 gme->vsb->contained = true;
2089
2090 sub_gd.pos = pos;
2091 sub_gd.pos.y = pos.y+pos.height; sub_gd.pos.height = sbwidth;
2092 sub_gd.flags = gg_visible | gg_enabled | gg_pos_in_pixels;
2093 sub_gd.handle_controlevent = _GME_HScroll;
2094 gme->hsb = GScrollBarCreate(base,&sub_gd,gme);
2095 gme->hsb->contained = true;
2096
2097 GME_RecalcFH(gme);
2098 {
2099 static GBox small = GBOX_EMPTY;
2100 static unichar_t nullstr[1] = { 0 };
2101
2102 small.main_background = gmatrixedit_activebg;
2103 small.main_foreground = gmatrixedit_activecol;
2104 memset(&sub_gd,'\0',sizeof(sub_gd));
2105 memset(&label,'\0',sizeof(label));
2106
2107 label.text = nullstr;
2108 label.font = gme->font;
2109 sub_gd.pos.height = gme->fh;
2110 sub_gd.pos.width = 40;
2111 sub_gd.label = &label;
2112 sub_gd.box = &small;
2113 sub_gd.flags = gg_enabled | gg_pos_in_pixels | gg_dontcopybox | gg_text_xim;
2114 gme->tf = GTextCompletionCreate(gme->nested,&sub_gd,gme);
2115 ((GTextField *) (gme->tf))->accepts_tabs = false;
2116 }
2117
2118 if ( gme->g.state!=gs_invisible )
2119 GDrawSetVisible(gme->nested,true);
2120 return( &gme->g );
2121 }
2122
2123 void GMatrixEditSet(GGadget *g,struct matrix_data *data, int rows, int copy_it) {
2124 GMatrixEdit *gme = (GMatrixEdit *) g;
2125 int r,c;
2126
2127 if ( data==gme->data ) {
2128 if ( rows<gme->rows )
2129 gme->rows = rows;
2130 GME_RecalcFH(gme);
2131 } else {
2132 MatrixDataFree(gme);
2133
2134 gme->rows = gme->row_max = rows;
2135 if ( !copy_it ) {
2136 gme->data = data;
2137 } else {
2138 gme->data = calloc(rows*gme->cols,sizeof(struct matrix_data));
2139 memcpy(gme->data,data,rows*gme->cols*sizeof(struct matrix_data));
2140 for ( c=0; c<gme->cols; ++c ) {
2141 enum me_type me_type = gme->col_data[c].me_type;
2142 if ( me_type==me_string || me_type==me_bigstr || me_type==me_func ||
2143 me_type==me_button || me_type==me_onlyfuncedit ||
2144 me_type==me_funcedit || me_type==me_stringchoice ||
2145 me_type==me_stringchoicetrans || me_type==me_stringchoicetag ) {
2146 for ( r=0; r<rows; ++r )
2147 gme->data[r*gme->cols+c].u.md_str = copy(gme->data[r*gme->cols+c].u.md_str);
2148 }
2149 }
2150 }
2151 GME_RecalcFH(gme);
2152
2153 gme->active_row = gme->active_col = -1;
2154 GME_EnableDelete(gme);
2155 if ( !GME_AdjustCol(gme,-1)) {
2156 GME_FixScrollBars(gme);
2157 GDrawRequestExpose(gme->nested,NULL,false);
2158 }
2159 }
2160 }
2161
2162 void GMatrixEditDeleteRow(GGadget *g,int row) {
2163 GMatrixEdit *gme = (GMatrixEdit *) g;
2164
2165 if ( row!=-1 )
2166 gme->active_row = row;
2167 GME_DeleteActive(gme);
2168 }
2169
2170 int GMatrixEditStringDlg(GGadget *g,int row,int col) {
2171 GMatrixEdit *gme = (GMatrixEdit *) g;
2172 char *str;
2173
2174 if ( gme->edit_active ) {
2175 if ( !GME_FinishEdit(gme) )
2176 return(false);
2177 }
2178 if ( row!=-1 )
2179 gme->active_row = row;
2180 if ( col!=-1 )
2181 gme->active_col = col;
2182 str = MD_Text(gme,row,col);
2183 GME_StrBigEdit(gme,str);
2184 free(str);
2185 return( true );
2186 }
2187
2188 struct matrix_data *GMatrixEditGet(GGadget *g, int *rows) {
2189 GMatrixEdit *gme = (GMatrixEdit *) g;
2190
2191 if ( gme->edit_active && !GME_FinishEdit(gme) ) {
2192 *rows = 0;
2193 return( NULL );
2194 }
2195
2196 *rows = gme->rows;
2197 return( gme->data );
2198 }
2199
2200 struct matrix_data *_GMatrixEditGet(GGadget *g, int *rows) {
2201 GMatrixEdit *gme = (GMatrixEdit *) g;
2202
2203 /* Does not try to parse the active textfield, if any */
2204 *rows = gme->rows;
2205 return( gme->data );
2206 }
2207
2208 GGadget *_GMatrixEditGetActiveTextField(GGadget *g) {
2209 GMatrixEdit *gme = (GMatrixEdit *) g;
2210 if ( gme->edit_active )
2211 return( gme->tf );
2212
2213 return( NULL );
2214 }
2215
2216 int GMatrixEditGetActiveRow(GGadget *g) {
2217 GMatrixEdit *gme = (GMatrixEdit *) g;
2218
2219 return( gme->active_row );
2220 }
2221
2222 int GMatrixEditGetActiveCol(GGadget *g) {
2223 GMatrixEdit *gme = (GMatrixEdit *) g;
2224
2225 return( gme->active_col );
2226 }
2227
2228 void GMatrixEditSetNewText(GGadget *g, char *text) {
2229 GMatrixEdit *gme = (GMatrixEdit *) g;
2230
2231 free(gme->newtext);
2232 gme->newtext = copy(text);
2233 }
2234
2235 void GMatrixEditSetOtherButtonEnable(GGadget *g, void (*sob)(GGadget *g, int r, int c)) {
2236 GMatrixEdit *gme = (GMatrixEdit *) g;
2237
2238 gme->setotherbuttons = sob;
2239 }
2240
2241 void GMatrixEditSetMouseMoveReporter(GGadget *g, void (*rmm)(GGadget *g, int r, int c)) {
2242 GMatrixEdit *gme = (GMatrixEdit *) g;
2243
2244 gme->reportmousemove = rmm;
2245 }
2246
2247 void GMatrixEditSetTextChangeReporter(GGadget *g, void (*tcr)(GGadget *g, int r, int c, GGadget *text)) {
2248 GMatrixEdit *gme = (GMatrixEdit *) g;
2249
2250 gme->reporttextchanged = tcr;
2251 }
2252
2253 void GMatrixEditSetValidateStr(GGadget *g, char *(*validate)(GGadget *g, int r, int c, int wasnew, char *str)) {
2254 GMatrixEdit *gme = (GMatrixEdit *) g;
2255
2256 gme->validatestr = validate;
2257 }
2258
2259 void GMatrixEditSetUpDownVisible(GGadget *g, int visible) {
2260 GMatrixEdit *gme = (GMatrixEdit *) g;
2261 GGadgetCreateData gcd[3];
2262 GTextInfo label[2];
2263 int i;
2264
2265 if ( gme->up==NULL ) {
2266 if ( !visible )
2267 return;
2268
2269 memset(gcd,0,sizeof(gcd));
2270 memset(label,0,sizeof(label));
2271 i = 0;
2272
2273 /* I want the 2 pronged arrow, but gdraw can't find a nice one */
2274 /* label[i].text = (unichar_t *) "⇑"; *//* Up Arrow */
2275 label[i].text = (unichar_t *) "↑"; /* Up Arrow */
2276 label[i].text_is_1byte = true;
2277 gcd[i].gd.label = &label[i];
2278 gcd[i].gd.flags = gg_visible /*| gg_enabled*/ ;
2279 gcd[i].gd.handle_controlevent = _GME_Up;
2280 gcd[i].data = gme;
2281 gcd[i++].creator = GButtonCreate;
2282
2283 /* I want the 2 pronged arrow, but gdraw can't find a nice one */
2284 /* label[i].text = (unichar_t *) "⇓"; *//* Down Arrow */
2285 label[i].text = (unichar_t *) "↓"; /* Down Arrow */
2286 label[i].text_is_1byte = true;
2287 gcd[i].gd.label = &label[i];
2288 gcd[i].gd.flags = gg_visible /*| gg_enabled*/ ;
2289 gcd[i].gd.handle_controlevent = _GME_Down;
2290 gcd[i].data = gme;
2291 gcd[i++].creator = GButtonCreate;
2292 GGadgetsCreate(g->base,gcd);
2293
2294 gme->up = gcd[0].ret;
2295 gme->down = gcd[1].ret;
2296 gme->up->contained = gme->down->contained = true;
2297 } else {
2298 GGadgetSetVisible(gme->up,visible);
2299 GGadgetSetVisible(gme->down,visible);
2300 }
2301 }
2302
2303 void GMatrixEditAddButtons(GGadget *g, GGadgetCreateData *gcd) {
2304 GMatrixEdit *gme = (GMatrixEdit *) g;
2305 int i, base=0;
2306
2307 if ( gme->buttonlist!=NULL ) {
2308 for ( base=0; gme->buttonlist[base]!=NULL; ++base );
2309 }
2310 for ( i=0; gcd[i].creator!=NULL; ++i );
2311 gme->buttonlist = realloc(gme->buttonlist,(i+base+1)*sizeof(GGadget *));
2312 GGadgetsCreate(g->base,gcd);
2313 for ( i=0; gcd[i].creator!=NULL; ++i ) {
2314 gme->buttonlist[base+i] = gcd[i].ret;
2315 gcd[i].ret->contained = true;
2316 }
2317 gme->buttonlist[base+i] = NULL;
2318 }
2319
2320 void GMatrixEditEnableColumn(GGadget *g, int col, int enabled) {
2321 GMatrixEdit *gme = (GMatrixEdit *) g;
2322 /* User must do a refresh of the gadget. Don't want to do it always */
2323 /* because multiple calls might cause a flicker */
2324
2325 if ( col<0 || col>=gme->cols )
2326 return;
2327 gme->col_data[col].disabled = !enabled;
2328 }
2329
2330 void GMatrixEditShowColumn(GGadget *g, int col, int visible) {
2331 GMatrixEdit *gme = (GMatrixEdit *) g;
2332
2333 if ( col<0 || col>=gme->cols )
2334 return;
2335 gme->col_data[col].hidden = !visible;
2336 gme->col_data[col].fixed = false;
2337 GME_AdjustCol(gme,-1);
2338 }
2339
2340 void GMatrixEditSetColumnChoices(GGadget *g, int col, GTextInfo *ti) {
2341 GMatrixEdit *gme = (GMatrixEdit *) g;
2342
2343 if ( gme->col_data[col].enum_vals!=NULL )
2344 GMenuItemArrayFree(gme->col_data[col].enum_vals);
2345 if ( ti!=NULL )
2346 gme->col_data[col].enum_vals = GMenuItemFromTI(ti,
2347 gme->col_data[col].me_type==me_enum );
2348 else
2349 gme->col_data[col].enum_vals = NULL;
2350 }
2351
2352 GMenuItem *GMatrixEditGetColumnChoices(GGadget *g, int col) {
2353 GMatrixEdit *gme = (GMatrixEdit *) g;
2354
2355 return( gme->col_data[col].enum_vals );
2356 }
2357
2358 int GMatrixEditGetColCnt(GGadget *g) {
2359 GMatrixEdit *gme = (GMatrixEdit *) g;
2360 return( gme->cols );
2361 }
2362
2363 void GMatrixEditScrollToRowCol(GGadget *g,int r, int c) {
2364 GMatrixEdit *gme = (GMatrixEdit *) g;
2365 int rows_shown = gme->vsb->r.height/(gme->fh+gme->vpad);
2366 int context = rows_shown/3;
2367 int needs_expose = true;
2368 int width = gme->hsb->r.width;
2369 int i;
2370 GRect size;
2371
2372 if ( r<0 ) r = 0; else if ( r>=gme->rows ) r = gme->rows-1;
2373 if ( r<gme->off_top || r>=gme->off_top+rows_shown ) {
2374 gme->off_top = r-context;
2375 if ( gme->off_top<0 )
2376 gme->off_top = 0;
2377 needs_expose = true;
2378 }
2379 if ( c<0 ) c = 0; else if ( c>=gme->cols ) c = gme->cols-1;
2380 for ( i=0; i<gme->cols; ++i ) {
2381 if ( gme->col_data[i].x-gme->off_left>=0 )
2382 break;
2383 }
2384 if ( c<i ) {
2385 if ( c>0 && gme->col_data[c-1].width + gme->col_data[c].width<width )
2386 gme->off_left = gme->col_data[c-1].x;
2387 else
2388 gme->off_left = gme->col_data[c ].x;
2389 needs_expose = true;
2390 } else {
2391 for ( ; i<gme->cols; ++i ) {
2392 if ( gme->col_data[i].x+gme->col_data[i].width-gme->off_left>width )
2393 break;
2394 }
2395 if ( c>=i && gme->col_data[c].x!=gme->off_left ) {
2396 gme->off_left = gme->col_data[c].x;
2397 needs_expose = true;
2398 }
2399 }
2400 if ( needs_expose ) {
2401 int hend = gme->col_data[gme->cols-1].x + gme->col_data[gme->cols-1].width;
2402
2403 GDrawGetSize(gme->nested,&size);
2404 if ( gme->off_left>hend-size.width )
2405 gme->off_left = hend-size.width;
2406 if ( gme->off_left<0 )
2407 gme->off_left = 0;
2408 GScrollBarSetPos(gme->hsb,gme->off_left);
2409 GScrollBarSetPos(gme->vsb,gme->off_top);
2410 GGadgetRedraw(&gme->g);
2411 /* Used to request expose only if the row or column we are scrolling to */
2412 /* was outside of the visible area. However we need expose anyway, because */
2413 /* otherwise it is impossible to properly highlight fields in the active row. */
2414 /* So the rectangle associated with the expose event is now the only thing which */
2415 /* makes some difference. */
2416 } else {
2417 GGadgetGetSize(gme->tf,&size);
2418 GDrawRequestExpose(gme->nested,&size,false);
2419 }
2420 }
2421
2422 void GMatrixEditSetColumnCompletion(GGadget *g, int col,
2423 GTextCompletionHandler completion) {
2424 GMatrixEdit *gme = (GMatrixEdit *) g;
2425
2426 gme->col_data[col].completer = completion;
2427 }
2428
2429 void GMatrixEditSetBeforeDelete(GGadget *g, void (*predelete)(GGadget *g, int r)) {
2430 GMatrixEdit *gme = (GMatrixEdit *) g;
2431
2432 gme->predelete = predelete;
2433 }
2434
2435 void GMatrixEditSetRowMotionCallback(GGadget *g, void (*rowmotion)(GGadget *g, int oldr, int newr)) {
2436 GMatrixEdit *gme = (GMatrixEdit *) g;
2437
2438 gme->rowmotion = rowmotion;
2439 }
2440
2441 void GMatrixEditSetCanUpDown(GGadget *g, enum gme_updown (*canupdown)(GGadget *g, int r)) {
2442 GMatrixEdit *gme = (GMatrixEdit *) g;
2443
2444 gme->canupdown = canupdown;
2445 }
2446
2447 void GMatrixEditActivateRowCol(GGadget *g, int r, int c) {
2448 GMatrixEdit *gme = (GMatrixEdit *) g;
2449
2450 gme->active_row = r;
2451 gme->active_col = c;
2452 GME_EnableDelete(gme);
2453 GDrawRequestExpose(gme->nested,NULL,false);
2454 }
2455
2456 void GMatrixEditSetEditable(GGadget *g, int editable ) {
2457 GMatrixEdit *gme = (GMatrixEdit *) g;
2458
2459 gme->no_edit = !editable;
2460 GGadgetSetVisible(gme->del,editable);
2461 GMatrixEdit_Resize(&gme->g,gme->g.r.width,gme->g.r.height);
2462 GDrawRequestExpose(gme->nested,NULL,false);
2463 }