File: NMbutton.c

package info (click to toggle)
magic 7.5.220-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 17,860 kB
file content (509 lines) | stat: -rw-r--r-- 14,023 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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/* NMbutton.c -
 *
 *	This file contains routines that respond to button pushes
 *	in database windows when the netlist button handler is active.
 *
 *     ********************************************************************* 
 *     * Copyright (C) 1985, 1990 Regents of the University of California. * 
 *     * Permission to use, copy, modify, and distribute this              * 
 *     * software and its documentation for any purpose and without        * 
 *     * fee is hereby granted, provided that the above copyright          * 
 *     * notice appear in all copies.  The University of California        * 
 *     * makes no representations about the suitability of this            * 
 *     * software for any purpose.  It is provided "as is" without         * 
 *     * express or implied warranty.  Export of this software outside     * 
 *     * of the United States of America may require an export license.    * 
 *     *********************************************************************
 */

#ifndef lint
static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-7.5/netmenu/NMbutton.c,v 1.1.1.1 2006/04/10 22:03:14 tim Exp $";
#endif  /* not lint */

#include <stdio.h>
#include <string.h>

#include "utils/magic.h"
#include "utils/geometry.h"
#include "tiles/tile.h"
#include "utils/hash.h"
#include "database/database.h"
#include "windows/windows.h"
#include "graphics/graphics.h"
#include "dbwind/dbwind.h"
#include "textio/textio.h"
#include "textio/txcommands.h"
#include "netmenu/nmInt.h"
#include "netmenu/netmenu.h"
#include "utils/styles.h"
#include "utils/main.h"

/* The following static holds the name of the current net. */

char *NMCurNetName = NULL;

/* Maximimum amount of storage to hold terminal name: */

#define MAXTERMLENGTH 200

/*
 * ----------------------------------------------------------------------------
 *
 * NMButtonNetList --
 *
 * 	This procedure is invoked when the button to switch netlists
 *	is clicked.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The user is asked to type in a netlist name, and this name is
 *	used as the current netlist.  If the right button was clicked,
 *	use the name of the edit cell as the name of the netlist.
 *
 * ----------------------------------------------------------------------------
 */

void
NMButtonNetList(window, cmd, nmButton, point)
    MagWindow *window;		/* Where button was clicked (not used). */
    NetButton *nmButton;	/* Data structure for button (not used). */
    TxCommand *cmd;		/* Used to figure out which button it was. */
    Point *point;		/* Not used. */
{
#define MAXLENGTH 200
    char newName[MAXLENGTH];
    if (cmd->tx_button == TX_RIGHT_BUTTON)
	NMNewNetlist(EditCellUse->cu_def->cd_name);
    else
    {
	TxPrintf("New net list name: ");
	if (TxGetLine(newName, MAXLENGTH) == NULL) newName[0] == 0;
	if (newName[0] == 0) return;
	NMNewNetlist(newName);
    }
}

/*
 * ----------------------------------------------------------------------------
 *
 * nmButtonSetup --
 *
 * 	This procedure does miscellaneous button dirty work.  It
 *	makes sure that there's a current netlist (and prints an
 *	error if not), and finds the nearest terminal to the point
 *	location.
 *
 * Results:
 *	If everything is OK, the return value is a pointer to a
 *	statically-allocated string holding the nearest terminal
 *	location.  If there's any problem, NULL is returned.
 *
 * Side effects:
 *	None.
 *
 * ----------------------------------------------------------------------------
 */

char *
nmButtonSetup()
{
    Point rootPoint, editPoint;
    Rect rootArea, searchArea, tmp1, tmp2;
    int expand;
    static char termName[MAXTERMLENGTH];
    MagWindow *w;

#define SEARCHPIXELS 20

    if (NMNetListButton.nmb_text == NULL)
    {
	TxError("There's no current netlist;  please select one.\n");
	return NULL;
    }

    /* Compute an area (in edit cell coordinates) that's SEARCHPIXELS
     * around the cursor position, so that anything even close can
     * be used to find a terminal.
     */

    w = ToolGetPoint(&rootPoint, &rootArea);
    if (w == NULL) return NULL;
    if (((CellUse *)(w->w_surfaceID))->cu_def != EditRootDef)
    {
	TxError("Sorry, but you have to use a window that's being edited.\n");
	return NULL;
    }
    tmp1.r_xbot = tmp1.r_ybot = tmp1.r_ytop = 0;
    tmp1.r_xtop = SEARCHPIXELS;
    WindScreenToSurface(w, &tmp1, &tmp2);
    expand = tmp2.r_xtop - tmp2.r_xbot;
    rootArea.r_xbot -= expand;
    rootArea.r_xtop += expand;
    rootArea.r_ybot -= expand;
    rootArea.r_ytop += expand;
    GeoTransPoint(&RootToEditTransform, &rootPoint, &editPoint);
    GeoTransRect(&RootToEditTransform, &rootArea, &searchArea);

    if (!DBNearestLabel(EditCellUse, &searchArea, &editPoint, 0,
	(Rect *) NULL, termName, MAXTERMLENGTH))
    {
	TxPrintf("There's no terminal near the cursor.\n");
	return NULL;
    }

    if(index(termName, '/')==0)
    {
	TxPrintf("You can't route to a terminal in the Edit cell!");
	TxPrintf("  Please select one in a subcell.\n");
	return NULL;
    }
    return termName;
}

/*
 * ----------------------------------------------------------------------------
 *
 * NMButtonRight --
 *
 * 	This procedure is invoked when the right button is pushed over
 *	a database window.  It toggles the nearest terminal into or
 *	out of the current net.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	A terminal is added to or removed from a net, and the highlight
 *	information on the display is updated.
 *
 * ----------------------------------------------------------------------------
 */

void
NMButtonRight(w, cmd)
    MagWindow *w;			/* Window in which button was pushed. */
    TxCommand *cmd;		/* Detailed information about command. */
{
    char *name;
    extern int nmButHighlightFunc(), nmButUnHighlightFunc();
    extern int nmButCheckFunc(), nmNewRefFunc(), nmFindNetNameFunc();

    name = nmButtonSetup();
    if (name == NULL) return;

    if (NMCurNetName == NULL)
    {
	TxError("Use the left button to select a net first.\n");
	return;
    }

    /* See if this terminal is already in the current net. */
    
    if (NMEnumTerms(name, nmButCheckFunc, (ClientData) NMCurNetName))
    {
	/* In the net already.  Delete it from the net.  But first,
	 * find another terminal in the net to use as a reference
	 * for the current net.  If not, then null out the current net.
	 */
	
	if (strcmp(name, NMCurNetName) == 0)
	{
	    NMSelectNet((char *) NULL);
	    (void) NMEnumTerms(name, nmNewRefFunc, (ClientData) name);
	}
	NMUndo(name, NMCurNetName, NMUE_REMOVE);
	(void) NMDeleteTerm(name);
	(void) DBSrLabelLoc(EditCellUse, name, nmButUnHighlightFunc,
	    (ClientData) NULL);
	TxPrintf("Removing \"%s\" from net.\n", name);
    }
    else
    {
	/* Not in the net already:  add it in.  But first, see if
	 * the terminal is already in a net.  If it is, then remove
	 * it from that net.
	 */
	
	if (NMTermInList(name) != NULL)
	{
	    char *netName = name;
	    (void) NMEnumTerms(name, nmFindNetNameFunc, (ClientData) &netName);
	    if (netName != name)
	    {
		TxPrintf("\"%s\" was already in a net;", name);
		TxPrintf("  I'm removing it from the old net.\n");
	    }
	    NMUndo(name, netName, NMUE_REMOVE);
	    (void) NMDeleteTerm(name);
	}
	NMUndo(name, NMCurNetName, NMUE_ADD);
	(void) NMAddTerm(name, NMCurNetName);
	(void) DBSrLabelLoc(EditCellUse, name, nmButHighlightFunc,
	    (ClientData) NULL);
	TxPrintf("Adding \"%s\" to net.\n", name);
    }
}

/* This check function merely returns TRUE if a terminal in
 * the net being enumerated matches some other given terminal.
 */

int
nmButCheckFunc(name1, name2)
    char *name1;		/* Name of terminal in net. */
    char *name2;		/* Name of other terminal. */
{
    if (strcmp(name1, name2) == 0) return 1;
    return 0;
}

/* This function looks for a name of a terminal in a net that
 * is different from a given name.  The different name, if any,
 * is stored in the clientdata.
 */

int
nmFindNetNameFunc(name1, pname2)
    char *name1;		/* Name of terminal in net. */
    char **pname2;		/* Pointer to name to be replaced with
				 * different name, if there is a different
				 * name in the net.
				 */
{
    if (strcmp(name1, *pname2) == 0) return 0;
    *pname2 = name1;
    return 1;			/* Abort search with new name. */
}

	/* ARGSUSED */
int
nmButHighlightFunc(area, name, label, pExists)
    Rect  *area;		/* Area of the label. */
    char  *name;		/* Name of label. */
    Label *label;		/* Pointer to label */
    bool  *pExists;		/* We just set this to TRUE. */
{
    Rect rootArea;
    Point point;

    GeoTransRect(&EditToRootTransform, area, &rootArea);
    point.p_x = (rootArea.r_xbot + rootArea.r_xtop)/2;
    point.p_y = (rootArea.r_ybot + rootArea.r_ytop)/2;
    NMAddPoint(&point);
    if (pExists != NULL) *pExists = TRUE;
    return 0;
}

int
nmButUnHighlightFunc(area)
    Rect *area;			/* Area of the label. */
{
    Rect rootArea;
    Point point;

    GeoTransRect(&EditToRootTransform, area, &rootArea);
    point.p_x = (rootArea.r_xbot + rootArea.r_xtop)/2;
    point.p_y = (rootArea.r_ybot + rootArea.r_ytop)/2;
    NMDeletePoint(&point);
    return 0;
}

int
nmNewRefFunc(name, oldRef)
    char *name;			/* Name of a terminal in the net. */
    char *oldRef;		/* Name we don't want to use as net reference
				 * anymore.
				 */
{
    if (strcmp(name, oldRef) != 0)
    {
	NMSelectNet(name);
	return 1;		/* Abort search: we've found what we want. */
    }
    return 0;
}

/*
 * ----------------------------------------------------------------------------
 *
 * NMSelectNet --
 *
 * 	This routine selects a net and highlights it on the screen.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The net containing terminal is selected.  If there used to
 *	be a net selected, it is unselected.  If name is NULL,
 *	all this procedure does is to unselect the previous net.
 *
 * ----------------------------------------------------------------------------
 */

void
NMSelectNet(name)
    char *name;			/* Gives name of terminal in net to be
				 * be selected.
				 */
{
    extern int nmSelNetFunc();

    NMUndo(name, NMCurNetName, NMUE_SELECT);
    NMCurNetName = NULL;
    NMClearPoints();

    if (name == NULL) return;

    /* If this terminal isn't already in a net, return. */

    NMCurNetName = NMTermInList(name);
    TxPrintf("Selected net is now \"%s\".\n", NMCurNetName);
    if (NMCurNetName == NULL) return;

    /* Highlight the entire net. */

    (void) NMEnumTerms(name, nmSelNetFunc, (ClientData) NULL);
}

/* For each terminal in the net, highlight each instance of the terminal. */

int
nmSelNetFunc(name)
    char *name;
{
    bool exists;

    exists = FALSE;
    (void) DBSrLabelLoc(EditCellUse, name, nmButHighlightFunc,
	(ClientData) &exists);
    if (!exists) TxPrintf("%s: not in circuit!\n", name);
    return 0;
}

/*
 * ----------------------------------------------------------------------------
 *
 * NMButtonLeft --
 *
 * 	This procedure is invoked when the left button is pushed
 *	over a database window.  It selects a new net.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	If there is a terminal near the cursor, it is selected as the
 *	current net and is highlighted on the screen.
 *
 * ----------------------------------------------------------------------------
 */

void
NMButtonLeft(w, cmd)
    MagWindow *w;			/* Window in which button was pushed. */
    TxCommand *cmd;		/* Detailed information about the command. */
{
    char *name;

    name = nmButtonSetup();
    if (name == NULL)
    {
	NMSelectNet((char *) NULL);
	return;
    }

    /* If this terminal isn't already in a net, start a new net. */

    if (NMTermInList(name) == NULL) (void) NMAddTerm(name, name);
    NMSelectNet(name);
}

/*
 * ----------------------------------------------------------------------------
 *
 * NMButtonMiddle --
 *
 * 	This procedure is invoked when the middle button is pressed in
 *	a database window.  It joins the net of the nearest terminal
 *	into the current net.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Two nets are joined together.  The highlights on the screen
 *	reflect this.
 *
 * ----------------------------------------------------------------------------
 */

void
NMButtonMiddle(w, cmd)
    MagWindow *w;		/* Window in which button was pushed. */
    TxCommand *cmd;	/* Detailed information about command. */
{
    char *name;

    name = nmButtonSetup();
    if (name == NULL) return;

    if (NMCurNetName == NULL)
    {
	TxPrintf("Use the left button to select a name first.\n");
	return;
    }

    /* Go through the new terminal's net and add everything to
     * the list of stuff to be redisplayed.  After this is done,
     * join the nets.  Note:  if the terminal isn't in any net
     * at all, make a new terminal.
     */
    
    if (NMTermInList(name) == NULL) (void) NMAddTerm(name, name);
    (void) NMEnumTerms(name, nmSelNetFunc, (ClientData) NULL);
    NMJoinNets(name, NMCurNetName);

    TxPrintf("Merging net \"%s\" into current net.\n", name);
}

/*
 * ----------------------------------------------------------------------------
 *
 * NMButtonProc --
 *
 * 	This is an alternate handler for button pushes in database
 *	windows.  It is invoked from the dbwind module when netlist
 *	editing is in progress.  This procedure just dispatches to
 *	the correct command procedure for the button.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Whatever the command procedure for the button does.
 *
 * ----------------------------------------------------------------------------
 */

void
NMButtonProc(w, cmd)
    MagWindow *w;		/* Window in which button was pushed. */
    TxCommand *cmd;	/* Detailed information about exactly what happened. */
{
    if (cmd->tx_buttonAction != TX_BUTTON_DOWN) return;
    switch (cmd->tx_button)
    {
	case TX_LEFT_BUTTON:
	    NMButtonLeft(w, cmd);
	    break;
	case TX_RIGHT_BUTTON:
	    NMButtonRight(w, cmd);
	    break;
	case TX_MIDDLE_BUTTON:
	    NMButtonMiddle(w, cmd);
	    break;
    }
}