1 /********************************************************************
2 This file is part of the abs 0.907 distribution.  abs is a spreadsheet
3 with graphical user interface.
4 
5 Copyright (C) 1998-2001  Andr� Bertin (Andre.Bertin@ping.be)
6 
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version if in the same spirit as version 2.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 Concact: abs@pi.be
22          http://home.pi.be/bertin/abs.shtml
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 #include "button.h"
52 #include <math.h>
53 #include "memory.h"
54 #include "project.h"
55 #include "callback.h"
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 Button *ActiveButton = NULL;
67 
68 Button *
newbutton()69 newbutton ()
70 {
71 
72   Button *button;
73   button = (Button *) absmalloc (sizeof (Button), "newbutton:button ");
74   button->label = NULL;
75   button->macro = NULL;
76   return button;
77 
78 }
79 
80 
81 
82 
83 int
freebutton(btn)84 freebutton (btn)
85      Button *btn;
86 {
87   if (btn == NULL)
88     return 0;
89   if (btn->label != NULL)
90     absfree (btn->label, "freebutton:btn->label");
91   if (btn->macro != NULL)
92     absfree (btn->macro, "freebutton:btn->macro");
93   absfree (btn, "freebutton:button ");
94   return 0;
95 }
96 
97 int
button_setlabel(Button * button,char * label)98 button_setlabel (Button * button, char *label)
99 {
100   int len = 0;
101   if (button == NULL || label == NULL)
102     return -1;
103   len = strlen (label);
104   if (len < 1)
105     return -1;
106   button->label = absmalloc (sizeof (char) * (len + 1), "button_setlabel:button->label");
107   strcpy (button->label, label);
108   button->label[len] = '\0';
109   return 0;
110 }
111 
112 char *
button_getlabel(Button * btn)113 button_getlabel (Button * btn)
114 {
115   if (btn == NULL)
116     return NULL;
117   if (btn->label == NULL)
118     return "Button";
119   return btn->label;
120 }
121 int
button_setmacro(Button * button,char * macro)122 button_setmacro (Button * button, char *macro)
123 {
124   int len = 0;
125   if (button == NULL || macro == NULL)
126     return -1;
127   len = strlen (macro);
128   if (len < 1)
129     return -1;
130   button->macro = absmalloc (sizeof (char) * (len + 1), "button_setmacro:button->macro");
131   strcpy (button->macro, macro);
132   button->macro[len] = '\0';
133   return 0;
134 }
135 
136 char *
button_getmacro(Button * btn)137 button_getmacro (Button * btn)
138 {
139   if (btn == NULL)
140     return NULL;
141   if (btn->macro == NULL)
142     return "NoMacroAssociated";
143   return btn->macro;
144 }
145 
146 
147 
148 
149 int
button_write(btn,fp)150 button_write (btn, fp)
151      Button *btn;
152      FILE *fp;
153 {
154   fprintf (fp, "Set objbutton = ActiveSheet.Buttons.Add(%d,%d,%d,%d)\n",
155 	   btn->x1, btn->y1, btn->x2 - btn->x1, btn->y2 - btn->y1);
156   fprintf (fp, "objbutton.OnAction = \"%s\"\n", btn->macro);
157   fprintf (fp, "objbutton.Text = \"%s\"\n", btn->label);
158 
159 
160 
161 
162 
163 
164 
165 
166   return 0;
167 
168 }
169 
170 Button *
ActivateButton(button)171 ActivateButton (button)
172      Button *button;
173 {
174   ActiveButton = button;
175   return ActiveButton;
176 }
177 
178 int
button_click(btn)179 button_click (btn)
180      Button *btn;
181 {
182   if (btn == NULL)
183     return -1;
184   if (btn->macro == NULL)
185     return -2;
186   gotolabel (btn->macro);
187   xrepaint ("button_click");
188   return 0;
189 }
190 
191 int
button_pointed(btn,x,y,dx,dy)192 button_pointed (btn, x, y, dx, dy)
193      Button *btn;
194      int x, y, dx, dy;
195 {
196   int mx, my;
197   int x1, x2, y1, y2;
198 
199   x1 = btn->x1;
200   x2 = btn->x2;
201   y1 = btn->y1;
202   y2 = btn->y2;
203 
204   mx = (x1 + x2) / 2;
205   my = (y1 + y2) / 2;
206 
207   if (
208        ((x1 - dx < x && x < x1 + dx) && (y1 - dy < y && y < y2 + dy)) ||
209        ((x2 - dx < x && x < x2 + dx) && (y1 - dy < y && y < y2 + dy)) ||
210        ((y1 - dy < y && y < y1 + dy) && (x1 - dx < x && x < x2 + dx)) ||
211        ((y2 - dy < y && y < y2 + dy) && (x1 - dx < x && x < x2 + dx))
212     )
213     return 1;
214 
215 
216   return 0;
217 }
218