1 /* $Id$ $Revision$ */
2 /* vim:set shiftwidth=4 ts=8: */
3
4 /*************************************************************************
5 * Copyright (c) 2011 AT&T Intellectual Property
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *
11 * Contributors: See CVS logs. Details at http://www.graphviz.org/
12 *************************************************************************/
13
14 /* Lefteris Koutsofios - AT&T Labs Research */
15
16 #include "common.h"
17 #include "g.h"
18 #include "gcommon.h"
19 #include "mem.h"
20 #include "resource.h"
21
22 #define WQU widget->u.q
23
24 BOOL CALLBACK stringproc (HWND, UINT, WPARAM, LPARAM);
25 BOOL CALLBACK choiceproc (HWND, UINT, WPARAM, LPARAM);
26
GQcreatewidget(Gwidget_t * parent,Gwidget_t * widget,int attrn,Gwattr_t * attrp)27 int GQcreatewidget (
28 Gwidget_t *parent, Gwidget_t *widget, int attrn, Gwattr_t *attrp
29 ) {
30 DWORD wflags;
31 int ai;
32
33 WQU->mode = G_QWSTRING;
34 wflags = WS_OVERLAPPEDWINDOW;
35 for (ai = 0; ai < attrn; ai++) {
36 switch (attrp[ai].id) {
37 case G_ATTRMODE:
38 if (strcmp ("string", attrp[ai].u.t) == 0)
39 WQU->mode = G_QWSTRING;
40 else if (strcmp ("file", attrp[ai].u.t) == 0)
41 WQU->mode = G_QWFILE;
42 else if (strcmp ("choice", attrp[ai].u.t) == 0)
43 WQU->mode = G_QWCHOICE;
44 else {
45 Gerr (POS, G_ERRBADATTRVALUE, attrp[ai].u.t);
46 return -1;
47 }
48 break;
49 case G_ATTRUSERDATA:
50 widget->udata = attrp[ai].u.u;
51 break;
52 default:
53 Gerr (POS, G_ERRBADATTRID, attrp[ai].id);
54 return -1;
55 }
56 }
57 switch (WQU->mode) {
58 case G_QWSTRING:
59 widget->w = 0;
60 break;
61 case G_QWFILE:
62 widget->w = 0;
63 break;
64 case G_QWCHOICE:
65 widget->w = 0;
66 break;
67 }
68 return 0;
69 }
70
GQsetwidgetattr(Gwidget_t * widget,int attrn,Gwattr_t * attrp)71 int GQsetwidgetattr (Gwidget_t *widget, int attrn, Gwattr_t *attrp) {
72 int ai;
73
74 for (ai = 0; ai < attrn; ai++) {
75 switch (attrp[ai].id) {
76 case G_ATTRMODE:
77 Gerr (POS, G_ERRCANNOTSETATTR2, "mode");
78 return -1;
79 case G_ATTRUSERDATA:
80 widget->udata = attrp[ai].u.u;
81 break;
82 default:
83 Gerr (POS, G_ERRBADATTRID, attrp[ai].id);
84 return -1;
85 }
86 }
87 return 0;
88 }
89
GQgetwidgetattr(Gwidget_t * widget,int attrn,Gwattr_t * attrp)90 int GQgetwidgetattr (Gwidget_t *widget, int attrn, Gwattr_t *attrp) {
91 int ai;
92
93 for (ai = 0; ai < attrn; ai++) {
94 switch (attrp[ai].id) {
95 case G_ATTRMODE:
96 switch (WQU->mode) {
97 case G_QWSTRING:
98 attrp[ai].u.t = "string";
99 break;
100 case G_QWFILE:
101 attrp[ai].u.t = "file";
102 break;
103 case G_QWCHOICE:
104 attrp[ai].u.t = "choice";
105 break;
106 }
107 break;
108 case G_ATTRUSERDATA:
109 attrp[ai].u.u = widget->udata;
110 break;
111 default:
112 Gerr (POS, G_ERRBADATTRID, attrp[ai].id);
113 return -1;
114 }
115 }
116 return 0;
117 }
118
GQdestroywidget(Gwidget_t * widget)119 int GQdestroywidget (Gwidget_t *widget) {
120 return 0;
121 }
122
123 static Gwidget_t *choicew;
124 static char *buttons[20];
125 static char *stringp;
126 static int stringn;
127
GQqueryask(Gwidget_t * widget,char * prompt,char * args,char * responsep,int responsen)128 int GQqueryask (
129 Gwidget_t *widget, char *prompt, char *args,
130 char *responsep, int responsen
131 ) {
132 OPENFILENAME ofn;
133 char buf[256];
134 char *s1, *s2;
135 char c;
136 int i;
137
138 switch (WQU->mode) {
139 case G_QWSTRING:
140 stringp = responsep;
141 stringn = responsen;
142 buttons[0] = prompt;
143 buttons[1] = args;
144 DialogBox (hinstance, (LPCSTR) "STRINGDIALOG", (HWND) NULL, stringproc);
145 Gpopdownflag = TRUE;
146 if (stringp)
147 return 0;
148 return -1;
149 break;
150 case G_QWFILE:
151 strcpy (buf, args);
152 ofn.lStructSize = sizeof (OPENFILENAME);
153 ofn.hwndOwner = (HWND) NULL;
154 ofn.lpstrFilter = "All Files (*.*)\0*.*\0";
155 ofn.lpstrCustomFilter = NULL;
156 ofn.nMaxCustFilter = 0;
157 ofn.nFilterIndex = 1;
158 ofn.lpstrFile = buf;
159 ofn.nMaxFile = 256;
160 ofn.lpstrFileTitle = NULL;
161 ofn.nMaxFileTitle = 0;
162 ofn.lpstrInitialDir = NULL;
163 ofn.lpstrTitle = prompt;
164 ofn.Flags = 0;
165 ofn.lpstrDefExt = NULL;
166 if (!GetOpenFileName (&ofn)) {
167 Gpopdownflag = TRUE;
168 return -1;
169 }
170 strncpy (responsep, buf, responsen);
171 Gpopdownflag = TRUE;
172 break;
173 case G_QWCHOICE:
174 if (!args)
175 return -1;
176 WQU->button = 0;
177 choicew = widget;
178 buttons[0] = prompt;
179 for (s1 = args, i = 1; *s1; i++) {
180 buttons[i] = s1;
181 s2 = s1;
182 while (*s2 && *s2 != '|')
183 s2++;
184 c = *s2, *s2 = 0;
185 s1 = s2;
186 if (c)
187 s1++;
188 }
189 buttons[i] = NULL;
190 DialogBox (hinstance, "CHOICEDIALOG", (HWND) NULL, choiceproc);
191 if (WQU->button > 0)
192 strncpy (responsep, buttons[WQU->button], responsen);
193 for (s2 = args; s2 < s1; s2++)
194 if (!*s2)
195 *s2 = '|';
196 Gpopdownflag = TRUE;
197 if (WQU->button > 0)
198 return 0;
199 return -1;
200 break;
201 }
202 if (responsep[0] && responsep[strlen(responsep) - 1] == '\n')
203 responsep[strlen(responsep) - 1] = 0;
204 return 0;
205 }
206
stringproc(HWND hdlg,UINT message,WPARAM wparam,LPARAM lparam)207 BOOL CALLBACK stringproc (
208 HWND hdlg, UINT message, WPARAM wparam, LPARAM lparam
209 ) {
210 switch (message) {
211 case WM_INITDIALOG:
212 SetDlgItemText (hdlg, IDC_STATIC1, (LPCSTR) buttons[0]);
213 if (buttons[1])
214 SetDlgItemText (hdlg, IDC_EDIT1, (LPCSTR) buttons[1]);
215 return TRUE;
216 break;
217 case WM_COMMAND:
218 switch (wparam) {
219 case IDOK:
220 GetDlgItemText (hdlg, IDC_EDIT1, stringp, stringn);
221 EndDialog (hdlg, TRUE);
222 return TRUE;
223 case IDCANCEL:
224 stringp = NULL;
225 EndDialog(hdlg, TRUE);
226 return TRUE;
227 break;
228 }
229 }
230 return FALSE;
231 }
232
choiceproc(HWND hdlg,UINT message,WPARAM wparam,LPARAM lparam)233 BOOL CALLBACK choiceproc (HWND hdlg, UINT message,
234 WPARAM wparam, LPARAM lparam) {
235 int sel, i;
236
237 switch (message) {
238 case WM_INITDIALOG:
239 SetDlgItemText (hdlg, IDC_STATIC1, (LPCSTR) buttons[0]);
240 for (i = 1; buttons[i]; i++)
241 SendDlgItemMessage (
242 hdlg, IDC_LIST1, LB_ADDSTRING, 0, (LPARAM) buttons[i]
243 );
244 return TRUE;
245 break;
246 case WM_COMMAND:
247 if ((sel = (int) SendDlgItemMessage (
248 hdlg, IDC_LIST1, LB_GETCURSEL, 0, 0
249 )) >= 0) {
250 choicew->u.q->button = sel + 1;
251 EndDialog (hdlg, TRUE);
252 }
253 return TRUE;
254 break;
255 }
256 return FALSE;
257 }
258