1 /*
2  * Author:      William Chia-Wei Cheng (bill.cheng@acm.org)
3  *
4  * Copyright (C) 2001-2009, William Chia-Wei Cheng.
5  *
6  * This file may be distributed under the terms of the Q Public License
7  * as defined by Trolltech AS of Norway and appearing in the file
8  * LICENSE.QPL included in the packaging of this file.
9  *
10  * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING
11  * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
13  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
14  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
16  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * @(#)$Header: /mm2/home/cvs/bc-src/tgif/testdrive.c,v 1.6 2011/05/16 16:22:00 william Exp $
19  */
20 
21 #define _INCLUDE_FROM_TESTDRIVE_C_
22 
23 #include "tgifdefs.h"
24 
25 #include "mainloop.e"
26 #include "msg.e"
27 #include "obj.e"
28 #include "setup.e"
29 
30 /*
31  * extern int	malloc_debug ARGS_DECL((int));
32  */
33 
34 int	lastFile=TRUE;
35 short	*pDrawFontAsc=NULL;
36 short	*pDrawFontDes=NULL;
37 
38 /*
39  * static
40  * void Prompt2 (PromptStr, OpName, FileName)
41  *    char	* PromptStr, * OpName, * FileName;
42  * {
43  *    char	inbuf[80];
44  *
45  *    printf (PromptStr);
46  *    fgets (inbuf, 80, stdin);
47  *    sscanf (inbuf, "%s%s", OpName, FileName);
48  * }
49  *
50  * static
51  * void Prompt3 (PromptStr, AttrName, ColorName, ValName)
52  *    char	* PromptStr, * AttrName, * ColorName, * ValName;
53  * {
54  *    char	inbuf[80];
55  *
56  *    printf (PromptStr);
57  *    fgets (inbuf, 80, stdin);
58  *    sscanf (inbuf, "%s%s%s", AttrName, ColorName, ValName);
59  * }
60  */
61 
62 static
PrintObjId(ObjPtr,Level)63 void PrintObjId(ObjPtr, Level)
64    struct ObjRec *ObjPtr;
65    int Level;
66 {
67    register int i;
68    int id=ObjPtr->id;
69    struct ObjRec *obj_ptr;
70    struct AttrRec *attr_ptr;
71 
72    for (i = 0; i < Level; i++) printf("   ");
73    switch (ObjPtr->type) {
74    /* these are all the tgif object types */
75    case OBJ_POLY: printf("poly: %1d\n", id); break;
76    case OBJ_BOX: printf("box: %1d\n", id); break;
77    case OBJ_OVAL: printf("oval: %1d\n", id); break;
78    case OBJ_TEXT: printf("text: %1d\n", id); break;
79    case OBJ_ARC: printf("arc: %1d\n", id); break;
80    case OBJ_RCBOX: printf("rcbox: %1d\n", id); break;
81    case OBJ_XBM: printf("xbm: %1d\n", id); break;
82    case OBJ_XPM: printf("xpm: %1d\n", id); break;
83    case OBJ_POLYGON: printf("polygon: %1d\n", id); break;
84    case OBJ_GROUP: printf("group: %1d\n", id); break;
85    case OBJ_SYM: printf("sym: %1d\n", id); break;
86    case OBJ_ICON: printf("icon: %1d\n", id); break;
87    case OBJ_PIN: printf("pin: %1d\n", id); break;
88    }
89    if (ObjPtr->type == OBJ_GROUP || ObjPtr->type == OBJ_SYM ||
90          ObjPtr->type == OBJ_ICON || ObjPtr->type == OBJ_PIN) {
91       /* for these composite objects, each on maintains a LIST of objects */
92       for (obj_ptr=ObjPtr->detail.r->last; obj_ptr != NULL;
93             obj_ptr=obj_ptr->prev) {
94          PrintObjId(obj_ptr, Level+1);
95       }
96    }
97    /* all object can have a list of attributes; the head    */
98    /*     of the list is pointed to by the fattr and the    */
99    /*     tail of the list is pointed to by the lattr field */
100    if ((attr_ptr=ObjPtr->lattr) != NULL) {
101       for (i = 0; i < Level+1; i++) printf("   ");
102       printf("attrs:\n");
103 
104       /* each attribute entry contains a TEXT object */
105       for ( ; attr_ptr != NULL; attr_ptr=attr_ptr->prev) {
106          PrintObjId(attr_ptr->obj, Level+2);
107       }
108    }
109 }
110 
111 #define DO_CMD(cmd) ExecACommandFromBuffer(cmd,NULL)
112 
113 static
ScriptDemo()114 void ScriptDemo()
115 {
116    MakeQuiescent(); /* select nothing, edit nothing */
117 
118    if (BeginExecCommandsFromBuffer()) {
119       int i=0;
120 
121       /* set current color to 'blue' */
122       DO_CMD("set_selected_obj_color(\"blue\")");
123       /* create a 200 pixels by 100 pixels blue box */
124       DO_CMD("create_box_obj(100,100,300,200)");
125       /* select the top object, which is the box object */
126       DO_CMD("select_top_obj()");
127       /* name the box object "the_box" */
128       DO_CMD("add_attr_to_selected_obj(\"name\",\"the_box\",100,100)");
129       /* hide all the attributes of the box object */
130       DO_CMD("call_simple_shortcut(\"HideAttr\")");
131       /* unselect everything */
132       DO_CMD("unselect_all_obj()");
133 
134       /* set current text justification to center justified */
135       DO_CMD("set_selected_text_just(\"center\")");
136       /* set current text font to Helvetica Bold */
137       DO_CMD("set_selected_text_font(\"Helvetica-Bold\")");
138       /* set current text size to 14 */
139       DO_CMD("set_selected_text_size(14)");
140       /* create a text object that sits on top the blue box */
141       DO_CMD("create_text_obj(200,100,\"My Box\")");
142 
143       /* select the box object */
144       DO_CMD("select_obj_by_name(\"the_box\")");
145       /* sleep for 500ms */
146       DO_CMD("sleep(NULL,500)");
147       /* change the box to 'green' */
148       DO_CMD("set_selected_obj_color(\"green\")");
149 
150       /* cycle through all the fill patterns */
151       for (i=31; i >= 0; i--) {
152          char buf[80];
153 
154          sprintf(buf, "set_selected_obj_fill(%1d)", i);
155          DO_CMD(buf);
156       }
157       /* sleep for another 500ms */
158       DO_CMD("sleep(NULL,500)");
159       /* change the box to 'red' */
160       DO_CMD("set_selected_obj_color(\"red\")");
161       /* sleep for another 500ms */
162       DO_CMD("sleep(NULL,500)");
163 
164       /* lock the box */
165       DO_CMD("call_simple_shortcut(\"Lock\")");
166 
167       /* select the top object, which is the text object */
168       DO_CMD("select_top_obj()");
169       /* also select the box */
170       DO_CMD("select_additional_obj(\"the_box\")");
171       /* align the text object so that it sits in the middle of the box */
172       DO_CMD("call_simple_shortcut(\"AlignObjsMiddle\")");
173       /* unlock the box */
174       DO_CMD("call_simple_shortcut(\"UnLock\")");
175       /* unselect everything */
176       DO_CMD("unselect_all_obj()");
177 
178       EndExecCommandsFromBuffer();
179    }
180 }
181 
main(argc,argv)182 int main(argc, argv)
183    int argc;
184    char *argv[];
185    /* All these strangeness with strings are related to */
186    /*    Prolog's foreign function interface. */
187 {
188    register int i;
189    char op_name[80], file_name[(MAXPATHLENGTH<<1)+1];
190    char *sp[6], *func_strp;
191 /*
192  * char	color_name[80], val_name[80];
193  * char	attr_name[80], speed_name[80], id_name[80];
194  */
195 
196 /*
197  * malloc_debug (1);
198  */
199 
200    if (!ProcessTgifOptions(argc, argv, file_name, sizeof(file_name))) return 1;
201 
202    if (file_name[0] == '\0') {
203       MainLoop("init", "", &func_strp,
204             &sp[0], &sp[1], &sp[2], &sp[3], &sp[4], &sp[5]);
205    } else {
206       MainLoop("init", file_name, &func_strp,
207             &sp[0], &sp[1], &sp[2], &sp[3], &sp[4], &sp[5]);
208    }
209 /*
210  * for (i = 0; i < 6; i++)
211  *    if (strcmp (sp[i], "") != 0)
212  *       printf ("%s ", sp[i]);
213  *    else
214  *       break;
215  * printf ("\n");
216  */
217 
218    while (TRUE) {
219       char s[80];
220 
221       strcpy(s, func_strp);
222       s[4] = '\0';
223 
224       DeallocStrings(&func_strp,&sp[0],&sp[1],&sp[2],&sp[3],&sp[4],&sp[5]);
225 
226       if (strcmp(s, "Quit") == 0) {
227          *file_name = '\0';
228          MainLoop("quit", file_name, &func_strp,
229                &sp[0], &sp[1], &sp[2], &sp[3], &sp[4], &sp[5]);
230          DeallocStrings(&func_strp,&sp[0],&sp[1],&sp[2],&sp[3],&sp[4],&sp[5]);
231          break;
232       } else if (strcmp(s, "Solv") == 0) {
233          struct ObjRec *obj_ptr;
234 
235          printf("==============\n");
236          printf("Listing IDs...\n");
237          printf("==============\n");
238          /* botObj points to the last top-level object, and */
239          /*     topObj points to the first top-level object */
240          for (obj_ptr = botObj; obj_ptr != NULL; obj_ptr = obj_ptr->prev) {
241             PrintObjId(obj_ptr, 0);
242          }
243          printf("\n");
244       } else if (strcmp(s, "Anim") == 0) {
245          ScriptDemo();
246       }
247 
248       Msg("Returned from basic driver.");
249 /*
250  *    Prompt2 ("Input an operation and a sub command.\n",op_name,file_name);
251  *
252  *    if (strcmp (op_name, "animate") == 0)
253  *    {
254  *       Prompt3 ("Input poly_id, speed, color.\n", id_name, speed_name,
255  *             color_name);
256  *       Animate (file_name, id_name, speed_name, color_name, &func_strp);
257  *       printf ("Animate RETURNs --> %s %s %s\n", func_strp, sp[0], sp[1]);
258  *    }
259  *    if (strcmp (op_name, "upd_attr_val") == 0)
260  *    {
261  *       Prompt3 ("Input attrname, color and value.\n", attr_name, color_name,
262  *             val_name);
263  *       UpdAttrVal (file_name, attr_name, color_name, val_name, &func_strp);
264  *       printf ("UpdAttrVal RETURNs --> %s %s %s\n", func_strp, sp[0], sp[1]);
265  *    }
266  */
267       *op_name = *file_name = '\0';
268       MainLoop(op_name, file_name, &func_strp,
269             &sp[0], &sp[1], &sp[2], &sp[3], &sp[4], &sp[5]);
270 /*
271  *    printf ("RETURN --> %s ", func_strp);
272  *    for (i = 0; i < 6; i++)
273  *       if (strcmp (sp[i], "") != 0)
274  *          printf ("%s ", sp[i]);
275  *       else
276  *          break;
277  *    printf ("\n");
278  */
279    }
280    return 0;
281 }
282