File: tool_arrow.c

package info (click to toggle)
pcb-rnd 3.1.7b-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,108 kB
  • sloc: ansic: 213,400; yacc: 6,241; sh: 4,698; awk: 3,016; makefile: 2,254; lex: 1,166; python: 519; xml: 261; lisp: 154; tcl: 67; perl: 34; javascript: 6; ruby: 5
file content (304 lines) | stat: -rw-r--r-- 9,862 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
/*
 *                            COPYRIGHT
 *
 *  pcb-rnd, interactive printed circuit board design
 *  Copyright (C) 1994,1995,1996 Thomas Nau
 *  Copyright (C) 1997, 1998, 1999, 2000, 2001 Harry Eaton
 *  Copyright (C) 2017,2019 Tibor 'Igor2' Palinkas
 *
 *  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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  Contact:
 *    Project page: http://repo.hu/projects/pcb-rnd
 *    lead developer: http://repo.hu/projects/pcb-rnd/contact.html
 *    mailing list: pcb-rnd (at) list.repo.hu (send "subscribe")
 *
 *
 *  Old contact info:
 *  Harry Eaton, 6697 Buttonhole Ct, Columbia, MD 21044, USA
 *  haceaton@aplcomm.jhuapl.edu
 *
 */

#include "config.h"
#include "conf_core.h"

#include "board.h"
#include "buffer.h"
#include "crosshair.h"
#include "data.h"
#include <librnd/core/actions.h>
#include <librnd/core/hidlib.h>
#include "remove.h"
#include "move.h"
#include "search.h"
#include "select.h"
#include <librnd/hid/tool.h>
#include "undo.h"
#include "extobj.h"
#include "tool_logic.h"


void pcb_tool_arrow_uninit(void)
{
	rnd_hid_notify_crosshair_change(&PCB->hidlib, rnd_false);
	pcb_crosshair_attached_clean(&PCB->hidlib);
	pcb_crosshair.AttachedObject.Type = PCB_OBJ_VOID;
	pcb_crosshair.AttachedObject.State = PCB_CH_STATE_FIRST;
	pcb_crosshair.AttachedBox.State = PCB_CH_STATE_FIRST;
	rnd_hid_notify_crosshair_change(&PCB->hidlib, rnd_true);
}

/* Called some time after the click if there was a release but no second click
	 a.k.a. finalize single click (some things are already done in pcb_press_mode
	 at the initial click event) */
static void click_timer_cb(rnd_hidval_t hv)
{
	rnd_design_t *hl = hv.ptr;
	pcb_board_t *pcb = hv.ptr;

	if (hl != rnd_multi_get_current()) /* switched away or unloaded in the meantime */
		return;

	if (hl->tool_click) {
		rnd_hid_notify_crosshair_change(&PCB->hidlib, rnd_false);
		hl->tool_click = rnd_false;
		if (pcb_crosshair_note.Moving && !rnd_gui->shift_is_pressed(rnd_gui)) {
			if (!pcb->RatDraw) {
				hl->tool_grabbed.status = rnd_true;
				pcb_crosshair_note.Buffer = conf_core.editor.buffer_number;
				pcb_buffer_set_number(PCB_MAX_BUFFER - 1);
				pcb_buffer_clear(pcb, PCB_PASTEBUFFER);
				pcb_buffer_add_selected(pcb, PCB_PASTEBUFFER, hl->tool_x, hl->tool_y, rnd_true, rnd_true);
				pcb_undo_save_serial();
				pcb_remove_selected(PCB_OP_NO_SUBC_PART);
				rnd_tool_save(hl);
				rnd_tool_is_saved = rnd_true;
				rnd_tool_select_by_name(hl, "buffer");
			}
			else
				rnd_message(RND_MSG_WARNING, "Can not drag&drop move selected when the rats nest layer is active\n");
		}
		else if (hl->tool_hit && !rnd_gui->shift_is_pressed(rnd_gui)) {
			rnd_box_t box;

			hl->tool_grabbed.status = rnd_true;
			rnd_tool_save(hl);
			rnd_tool_is_saved = rnd_true;
			rnd_tool_select_by_name(hl, rnd_gui->control_is_pressed(rnd_gui)? "copy" : "move");
			pcb_crosshair_attached_clean(hl);
			pcb_crosshair.AttachedObject.Ptr1 = pcb_crosshair_note.ptr1;
			pcb_crosshair.AttachedObject.Ptr2 = pcb_crosshair_note.ptr2;
			pcb_crosshair.AttachedObject.Ptr3 = pcb_crosshair_note.ptr3;
			pcb_crosshair.AttachedObject.Type = hl->tool_hit;

			if (pcb_crosshair.drags != NULL) {
				free(pcb_crosshair.drags);
				pcb_crosshair.drags = NULL;
			}
			pcb_crosshair.dragx = hl->tool_x;
			pcb_crosshair.dragy = hl->tool_y;
			box.X1 = hl->tool_x + PCB_SLOP * rnd_pixel_slop;
			box.X2 = hl->tool_x - PCB_SLOP * rnd_pixel_slop;
			box.Y1 = hl->tool_y + PCB_SLOP * rnd_pixel_slop;
			box.Y2 = hl->tool_y - PCB_SLOP * rnd_pixel_slop;
			pcb_crosshair.drags = pcb_list_block(pcb, &box, &pcb_crosshair.drags_len, 0);
			pcb_crosshair.drags_current = 0;
			pcb_tool_attach_for_copy(hl, hl->tool_x, hl->tool_y, rnd_true);
		}
		else {
			rnd_box_t box;

			hl->tool_hit = 0;
			pcb_crosshair_note.Moving = rnd_false;
			pcb_undo_save_serial();
			box.X1 = -RND_MAX_COORD;
			box.Y1 = -RND_MAX_COORD;
			box.X2 = RND_MAX_COORD;
			box.Y2 = RND_MAX_COORD;
			/* unselect first if shift key not down */
			if (!rnd_gui->shift_is_pressed(rnd_gui) && pcb_select_block(pcb, &box, rnd_false, rnd_false, rnd_false))
				pcb_board_set_changed_flag(PCB, rnd_true);
			pcb_tool_notify_block();
			pcb_crosshair.AttachedBox.Point1.X = hl->tool_x;
			pcb_crosshair.AttachedBox.Point1.Y = hl->tool_y;
		}
		rnd_hid_notify_crosshair_change(&PCB->hidlib, rnd_true);
	}

	if (pcb_crosshair.extobj_edit != NULL) {
		pcb_extobj_float_geo(pcb_crosshair.extobj_edit);
		rnd_gui->invalidate_all(rnd_gui);
	}
}

static int pcb_tool_arrow_notify_mode_(rnd_design_t *hl, int type, void *ptr1, void *ptr2, void *ptr3)
{
	int otype = type;

		if (otype == PCB_OBJ_ARC_POINT) { /* ignore arc endpoints if arc radius is 0 (so arc center is grabbed) */
			pcb_arc_t *arc = (pcb_arc_t *)ptr2;
			if ((arc->Width == 0) && (arc->Height == 0))
				return 0;
		}
		if (!hl->tool_hit && (type & PCB_MOVE_TYPES) && !PCB_FLAG_TEST(PCB_FLAG_LOCK, (pcb_any_obj_t *) ptr2)) {
			hl->tool_hit = type;
			pcb_crosshair_note.ptr1 = ptr1;
			pcb_crosshair_note.ptr2 = ptr2;
			pcb_crosshair_note.ptr3 = ptr3;
			pcb_crosshair.AttachedObject.tx = hl->tool_x;
			pcb_crosshair.AttachedObject.ty = hl->tool_y;
		}
		if (!pcb_crosshair_note.Moving && (type & (PCB_SELECT_TYPES | PCB_LOOSE_SUBC(PCB))) && PCB_FLAG_TEST(PCB_FLAG_SELECTED, (pcb_any_obj_t *) ptr2)) {
			pcb_crosshair_note.Moving = rnd_true;
			/* remember where the user clicked to start this op */
			pcb_crosshair.AttachedObject.tx = pcb_crosshair.AttachedObject.X = hl->tool_x;
			pcb_crosshair.AttachedObject.ty = pcb_crosshair.AttachedObject.Y = hl->tool_y;
		}
		if ((hl->tool_hit && pcb_crosshair_note.Moving) || type == PCB_OBJ_VOID)
			return 1;

	return 0;
}

void pcb_tool_arrow_notify_mode(rnd_design_t *hl)
{
	void *ptr1, *ptr2, *ptr3;
	int type;
	int test;
	rnd_hidval_t hv;

	hl->tool_click = rnd_true;
	/* do something after click time */
	hv.ptr = hl;
	rnd_gui->add_timer(rnd_gui, click_timer_cb, conf_core.editor.click_time, hv);

	/* see if we clicked on something already selected
	 * (pcb_crosshair_note.Moving) or clicked on a MOVE_TYPE
	 * (hl->tool_hit)
	 */
	for (test = (PCB_SELECT_TYPES | PCB_MOVE_TYPES | PCB_OBJ_FLOATER | PCB_LOOSE_SUBC(PCB)) & ~PCB_OBJ_RAT; test; test &= ~type) {
		/* grab object/point (e.g. line endpoint) for edit */
		type = pcb_search_screen(hl->tool_x, hl->tool_y, test, &ptr1, &ptr2, &ptr3);
		if (pcb_tool_arrow_notify_mode_(hl, type, ptr1, ptr2, ptr3))
			return;
	}
}

void pcb_tool_arrow_release_mode(rnd_design_t *hl)
{
	rnd_box_t box;
	pcb_board_t *pcb = (pcb_board_t *)hl;

	if (hl->tool_click) {
		rnd_box_t box;

		box.X1 = -RND_MAX_COORD;
		box.Y1 = -RND_MAX_COORD;
		box.X2 = RND_MAX_COORD;
		box.Y2 = RND_MAX_COORD;

		hl->tool_click = rnd_false;					/* inhibit timer action */
		pcb_undo_save_serial();
		/* unselect first if shift key not down */
		if (!rnd_gui->shift_is_pressed(rnd_gui)) {
			if (pcb_select_block(pcb, &box, rnd_false, rnd_false, rnd_false))
				pcb_board_set_changed_flag(pcb, rnd_true);
			if (pcb_crosshair_note.Moving) {
				pcb_crosshair_note.Moving = 0;
				hl->tool_hit = 0;
				return;
			}
		}
		/* Restore the SN so that if we select something the deselect/select combo
		   gets the same SN. */
		pcb_undo_restore_serial();
		if (pcb_select_object(PCB))
			pcb_board_set_changed_flag(pcb, rnd_true);
		else
			pcb_undo_inc_serial(); /* We didn't select anything new, so, the deselection should get its  own SN. */
		hl->tool_hit = 0;
		pcb_crosshair_note.Moving = 0;
	}
	else if (pcb_crosshair.AttachedBox.State == PCB_CH_STATE_SECOND) {
		box.X1 = pcb_crosshair.AttachedBox.Point1.X;
		box.Y1 = pcb_crosshair.AttachedBox.Point1.Y;
		box.X2 = pcb_crosshair.AttachedBox.Point2.X;
		box.Y2 = pcb_crosshair.AttachedBox.Point2.Y;

		pcb_undo_restore_serial();
		if (pcb_select_block(pcb, &box, rnd_true, rnd_true, rnd_false)) {
			pcb_board_set_changed_flag(pcb, rnd_true);
			pcb_undo_inc_serial();
		}
		else if (pcb_bumped)
			pcb_undo_inc_serial();
		pcb_crosshair.AttachedBox.State = PCB_CH_STATE_FIRST;
	}
}

void pcb_tool_arrow_adjust_attached_objects(rnd_design_t *hl)
{
	if (pcb_crosshair.AttachedBox.State) {
		pcb_crosshair.AttachedBox.Point2.X = pcb_crosshair.X;
		pcb_crosshair.AttachedBox.Point2.Y = pcb_crosshair.Y;
	}
}

/* XPM */
static const char *arrow_icon[] = {
/* columns rows colors chars-per-pixel */
"21 21 3 1",
"  c #000000",
". c #6EA5D7",
"o c None",
/* pixels */
"oo .. ooooooooooooooo",
"oo .... ooooooooooooo",
"ooo ...... oooooooooo",
"ooo ........ oooooooo",
"ooo ....... ooooooooo",
"oooo ..... oooooooooo",
"oooo ...... ooooooooo",
"ooooo .. ... oooooooo",
"ooooo . o ... ooooooo",
"oooooooooo ... oooooo",
"ooooooooooo .. oooooo",
"oooooooooooo  ooooooo",
"ooooooooooooooooooooo",
"ooo ooo    ooo    ooo",
"oo o oo ooo oo ooo oo",
"oo o oo ooo oo ooo oo",
"o ooo o    ooo    ooo",
"o ooo o ooo oo ooo oo",
"o     o ooo oo ooo oo",
"o ooo o ooo oo ooo oo",
"o ooo o ooo oo ooo oo"
};

rnd_tool_t pcb_tool_arrow = {
	"arrow", NULL, NULL, 10, arrow_icon, RND_TOOL_CURSOR_NAMED("left_ptr"), 0,
	NULL,
	pcb_tool_arrow_uninit,
	pcb_tool_arrow_notify_mode,
	pcb_tool_arrow_release_mode,
	pcb_tool_arrow_adjust_attached_objects,
	NULL,
	NULL,
	NULL,
	NULL, /* escape */
	
	PCB_TLF_RAT | PCB_TLF_EDIT
};