File: tkwidget.c

package info (click to toggle)
mc 4.1.35-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 6,924 kB
  • ctags: 9,665
  • sloc: ansic: 84,273; tcl: 1,779; makefile: 1,266; sh: 864; perl: 262; awk: 148; sed: 93; csh: 1
file content (459 lines) | stat: -rw-r--r-- 10,536 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* Widgets for the Midnight Commander, Tk interface code

   Copyright (C) 1994, 1995, 1996 Miguel de Icaza
   Copyright (C) 1995 Jakub Jelinek
   Copyright (C) 1994 Radek Doulik

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   TODO: Destroy all the command names that are created for each
   widget during runtime.

 */
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "tkmain.h"
#include "tkwidget.h"
#include "dlg.h"

static char
widget_number (Widget *w)
{
    Dlg_head *h = w->parent;
    const int count = h->count;
    Widget_Item *item;
    int i;

    item = h->first;
    for (i = 0; i < count; i++){
	if (item->widget == w)
	    return i;
	item = item->next;
    }
    /* Actually, if we get this far, this message should not appear */
    fprintf (stderr, "Inconsistent widget\n");
    exit (1);
}

/* Returns a new command prefixed with an x:
   x.query.$the_frame.b0

   for the command of the query dialog box button 0

   This allow us to control the widget with the command:
   .query.$the_frame.b0
   
   and to access the C code callback with the:
   x.query.$the_frame.b0
   command.
*/
char *
tk_new_command (widget_data parent, void *widget,
		       tcl_callback callback, char id)
{
    char buffer [60];
    Widget *w = (Widget *) widget;
    Dlg_head *h = w->parent;
    char *cmd;

    /* wdata holds the frame information, it will be replaced with the
     * the widget command name
     */
    if (h->grided && w->tkname)
	sprintf (buffer, ".widgets.%s", w->tkname);
    else {
	char *format;
	
	if (((char *) parent) [1] == 0)
	    format = "%s%c%d";
	else
	    format = ".%s%c%d";
	
	sprintf (buffer, format, w->frame, id, widget_number (w));
    }
    cmd = copy_strings ("x", (char *)parent, buffer, 0);
    w->wdata = (widget_data) cmd;
    if (callback)
	Tcl_CreateCommand (interp, cmd, callback, w, NULL);
    return cmd;
}


/* Button routines */

static int
is_default_button (WButton *b)
{
    return b->action == B_ENTER;
}

/* Called from the Tcl/Tk program when a button has been pressed */
static int
tk_button_callback (ClientData cd, Tcl_Interp *interp, int argc, char *argv [])
{
    WButton *b  = (WButton *) cd;
    Dlg_head *h = (Dlg_head *) b->widget.parent;
    int stop = 0;
    char *cmd = wtcl_cmd (b->widget);
    
    if (b->callback)
	stop = (*b->callback)(b->action, b->callback_data);
    if (!b->callback || stop){
	h->running = 0;
	h->ret_value = b->action;
    }
    return TCL_OK;
}

void
x_button_set (WButton *b, char *text)
{
    char *cmd = wtcl_cmd (b->widget);
    char *postfix = is_default_button (b) ? ".button" : "";
    
    tk_evalf ("%s%s configure -text {%s}", cmd+1, postfix, text);
}

/* Creates the button $parent.$newname and executes $newname command */
int
x_create_button (Dlg_head *h, widget_data parent, WButton *b)
{
    char *cmd;
    
    cmd = tk_new_command (parent, b, tk_button_callback, 'b');
    tk_evalf ("newbutton %s %s {%s} %d", cmd+1, cmd, b->text, is_default_button (b));
    return 1;
}



/* Radio button routines */
static int
tk_radio_callback (ClientData cd, Tcl_Interp *interp, int argc, char *argv [])
{
    WRadio   *r = (WRadio *) cd;

    if (STREQ (argv [1], "select")){
	r->pos = r->sel = atoi (argv [2]);
    }
    (r->widget.callback)(r->widget.parent, r, WIDGET_KEY, ' ');
    return TCL_OK;
}

void
x_radio_focus_item (WRadio *r)
{
    tk_evalf ("focus %s.%d", (char *)(r->widget.wdata)+1, r->pos);
}

int
x_create_radio (Dlg_head *h, widget_data parent, WRadio *r)
{
    int i;
    char *cmd;

    cmd = tk_new_command (parent, r, tk_radio_callback, 'r');
    tk_evalf ("newradio %s", cmd+1);
    
    for (i = 0; i < r->count; i++){
	tk_evalf ("radio_item %d {%s} %s %d", i, r->texts [i], cmd,
	    r->sel == i);
    }
    return 1;
}

/* Checkbuttons routines */

static int
tk_check_callback (ClientData cd, Tcl_Interp *interp, int argc, char *argv [])
{
    WCheck   *c = (WCheck *) cd;
    Dlg_head *h = c->widget.parent;

    (*c->widget.callback)(h, c, WIDGET_KEY, ' ');
    return TCL_OK;
}

int
x_create_check (Dlg_head *h, widget_data parent, WCheck *c)
{
    char *cmd;

    cmd = tk_new_command (parent, c, tk_check_callback, 'c');
    tk_evalf ("newcheck %s %s {%s} %d", cmd+1, cmd, c->text, c->state);

    return 1;
}


/* Input line */

int input_event (Gpm_Event *event, WInput *b);

/* This callback command accepts the following commands:
** 
** $win mouse x-position
** $win setmark x-position
*/
static int
tk_input_callback (ClientData cd, Tcl_Interp *interp, int argc, char *av [])
{
    Gpm_Event e;
    WInput *in = (WInput *) cd;
    
    if (strcmp (av [1], "mouse") == 0){
	e.x = atoi (av [2]) + 1;
	e.y = 0;
	e.type = GPM_DOWN;
	input_event (&e, (WInput *) cd);
	return TCL_OK;
    } else if (strcmp (av [1], "setmark") == 0){
	in->mark = in->point;
	return TCL_OK;
    }
    
    return TCL_OK;
}

void
x_update_input (WInput *in)
{
    char *name = wtcl_cmd (in->widget);
    int  p;
    
    if (!name)
	return;

    name++;
    if (in->is_password){
	tk_evalf ("%s delete 0 end", name);
	tk_evalf ("%s insert end {*secret*}", name);
	p = 9;
    } else {
	tk_evalf ("entry_save_sel %s", name);
	if (in->inserted_one && isascii (in->inserted_one) && in->inserted_one > ' '){
	    tk_evalf ("%s insert insert {%c}", name, in->inserted_one);
	    in->inserted_one = 0;
	} else {
	    tk_evalf ("%s delete 0 end", name);
	    tk_evalf ("%s insert end {%s}", name, in->buffer);
	}
	p = in->point;
    }
    tk_evalf ("%s icursor %d; entry_restore_sel %s", name, p, name);
}

int
x_create_input (Dlg_head *h, widget_data parent, WInput *in)
{
    char *cmd;

    cmd = tk_new_command (parent, in, tk_input_callback, 'i');
    tk_evalf ("newinput %s {%s}", cmd+1, in->buffer ? in->buffer: "");
    return 1;
}

void
x_listbox_select_nth (WListbox *l, int nth)
{
    if (!wtcl_cmd (l->widget))
	return;
    
    tk_evalf ("listbox_sel %s %d", wtk_win (l->widget), nth);
}

void
x_listbox_delete_nth (WListbox *l, int nth)
{
    tk_evalf ("%s delete %d", wtk_win (l->widget), nth);
}

int
x_create_listbox (Dlg_head *h, widget_data parent, WListbox *l)
{
    char *cmd;
    int  i;
    WLEntry *e;
    
    cmd = tk_new_command (parent, l, 0, 'x');
    tk_evalf ("listbox %s -width %d -selectmode single", cmd+1, l->widget.cols);

    /* As Jakub said on his very fine xvwidget.c: the user could add some
     * entries to the listbox before the tk-listbox was created, so we have
     * to add it manually :-)
     */
    i = 0;
    if ((e = l->list) == NULL)
	return 1;

    do {
	tk_evalf ("%s insert %d {%s}", cmd+1, i, e->text);
	if (e == l->current)
	    x_listbox_select_nth (l, i);
	e = e->next;
	i++;
    } while (e != l->list);
    return 1;
}

/*
 * 30.10.96 bor: x_list_insert added in reverse order as x_create_listbox
 */
void
x_list_insert (WListbox *l, WLEntry *p, WLEntry *e)
{
    int i;
    char *t = e->text;

    if (wtcl_cmd (l->widget) == NULL)
	return;

    for (i = 0; p != NULL && p != e; p = p->next, i++)
	;
    tk_evalf ("%s insert %d {%s}", wtk_win (l->widget), i, t);
}

int
x_create_label (Dlg_head *g, widget_data parent, WLabel *l)
{
    char *cmd;

    cmd = tk_new_command (parent, l, 0, 'l');
    tk_evalf ("label %s -text {%s}", cmd+1, l->text);
    return 1;
}


void
x_label_set_text (WLabel *label, char *text)
{
    char *lname;

    if (!wtcl_cmd (label->widget))
	return;
    
    tk_evalf ("%s configure -text {%s}", wtk_win (label->widget),
	      text ? text : "");
}


static int
tk_buttonbar_callback (ClientData cd, Tcl_Interp *in, int ac, char *av [])
{
    WButtonBar *bb = (WButtonBar *) cd;
    Dlg_head *h = (Dlg_head *) bb->widget.parent;
    int which = atoi (av [1]);

    (*bb->labels [which].function)(bb->labels [which].data);
    return TCL_OK;
}

int
x_create_buttonbar (Dlg_head *h, widget_data parent, WButtonBar *bb)
{
    char *cmd;
    int  i;
    
    cmd = tk_new_command (parent, bb, tk_buttonbar_callback, 'n');
    tk_evalf ("frame %s", cmd+1);
    for (i = 0; i < 10; i++){
	tk_evalf ("newbutton %s.%d {%s %d} {%d %s} 0", cmd+1, i, cmd, i, i+1,
		  bb->labels [i].text ? bb->labels [i].text : "");
	tk_evalf ("%s.%d configure -padx 1", cmd+1, i);
	tk_evalf ("pack %s.%d -side left -fill x -expand 1 -ipady 0",
	    cmd+1, i);
    }
    return 1;
}

void
x_redefine_label (WButtonBar *bb, int idx)
{
    if (!bb->widget.wdata)
	return;

    if (!*(char *) bb->widget.wdata)
	return;
    
    if (!bb->labels [idx-1].text)
	return;
    tk_evalf ("%s.%d configure -text {%d %s}",
        wtk_win (bb->widget), idx-1, idx, bb->labels [idx-1].text);
}

/* destructor for the Tk widgets */
void
x_destroy_cmd (void *w)
{
    Widget *widget = (Widget *)w;
    
    if (widget->wdata){
	Tcl_DeleteCommand (interp, wtcl_cmd (*widget));
	tk_evalf ("destroy %s", wtk_win (*widget));
	free (wtcl_cmd (*widget));
    }
}

int
x_find_buttonbar_check (WButtonBar *bb, Widget *paneletc)
{
    return (strcmp ((char *) bb->widget.wcontainer,
		    (char *)paneletc->wcontainer));
}

/* The only parameter accepted is the size of the widget width */
static int
tk_gauge_callback (ClientData cd, Tcl_Interp *in, int ac, char *av [])
{
    WGauge *g = (WGauge *) cd;

    g->pixels = atoi (av [1]);
    return TCL_OK;
}

int
x_create_gauge (Dlg_head *h, widget_data parent, WGauge *g)
{
    char *cmd;

    cmd = tk_new_command (parent, g, tk_gauge_callback, 'g');
    tk_evalf ("newgauge %s", cmd+1);
    return 1;
}

static void
x_gauge_display (WGauge *g)
{
    tk_evalf ("%s coords gauge 0 0 %d 90", wtk_win (g->widget),
	      (int) (g->current*g->pixels)/g->max);
}

void
x_gauge_show (WGauge *g)
{
    tk_evalf ("gauge_%s %s", g->shown ? "shown":"hidden", wtk_win (g->widget));
    if (g->shown)
	x_gauge_display (g);
}

void
x_gauge_set_value (WGauge *g, int max, int current)
{
    if (g->shown)
	x_gauge_display (g);
}