1 /*-
2  * Copyright (c) 1999 Thomas Runge (coto@core.de)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <X11/X.h>
31 #include <X11/Xlib.h>
32 #include <X11/Intrinsic.h>
33 #include <Xm/XmAll.h>
34 
35 #include "radio.h"
36 #include "misc.h"
37 
38 static Boolean block;
39 static char *string;
40 static int yesno;
41 
42 /* caller has to XtFree() the returned string! */
stringCB(Widget widget,XtPointer clientData,XmFileSelectionBoxCallbackStruct * callData)43 static void stringCB(Widget widget, XtPointer clientData,
44                      XmFileSelectionBoxCallbackStruct *callData)
45 {
46  if(!XmStringGetLtoR(callData->value, XmSTRING_DEFAULT_CHARSET, &string))
47   string = NULL;
48 
49  block = False;
50 }
51 
cancelCB(Widget widget,XtPointer clientData,XtPointer callData)52 static void cancelCB(Widget widget, XtPointer clientData,
53                                     XtPointer callData)
54 {
55  string = NULL;
56  block = False;
57 }
58 
yesnoCB(Widget widget,XtPointer clientData,XtPointer callData)59 static void yesnoCB(Widget widget, XtPointer clientData,
60                                    XtPointer callData)
61 {
62  yesno = (int) clientData;
63  block = False;
64 }
65 
XtomWait(Widget widget)66 static void XtomWait(Widget widget)
67 {
68  XtInputMask mask;
69  XtAppContext app_con = XtWidgetToApplicationContext(widget);
70 
71  while(block)
72  {
73   while((mask = XtAppPending(app_con)) != 0)
74   {
75    XtAppProcessEvent(app_con, mask);
76   }
77  }
78 
79  XSync(XtDisplay(widget), 0);
80  XmUpdateDisplay(widget);
81 }
82 
XtomGetFileName(Widget parent,const char * title,const char * default_path,const char * filter,int width,int height)83 char *XtomGetFileName(Widget parent, const char *title,
84 						const char *default_path, const char *filter,
85 						int width, int height)
86 {
87  static Widget fsb=NULL;
88 
89  block = True;
90 
91  if(fsb==NULL)
92  {
93   Arg args[10];
94   Cardinal n;
95 
96   n=0;
97   XtSetArg(args[n], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); n++;
98   XtSetArg(args[n], XmNautoUnmanage, True);                           n++;
99   fsb = XmCreateFileSelectionDialog(parent, "fsbDialog", args, n);
100   XtUnmanageChild(XmFileSelectionBoxGetChild(fsb, XmDIALOG_HELP_BUTTON));
101 
102   XtAddCallback(fsb, XmNokCallback, (XtCallbackProc)stringCB,
103                                     (XtPointer)NULL);
104   XtAddCallback(fsb, XmNcancelCallback, (XtCallbackProc)cancelCB,
105                                     (XtPointer)NULL);
106 #ifdef HAS_XPM
107   if(skin)
108    SkinToWidgets(fsb, skin);
109 #endif
110  }
111 
112  if(default_path && strlen(default_path))
113  {
114   XmString xstr = XMSTR(default_path);
115   XtVaSetValues(fsb, XmNdirectory, xstr, NULL);
116   XmStringFree(xstr);
117  }
118 
119  if(filter && strlen(filter))
120  {
121   XmString xstr = XMSTR(filter);
122   XtVaSetValues(fsb, XmNpattern, xstr, NULL);
123   XmStringFree(xstr);
124  }
125 
126  if(width > 0 && height > 0)
127  {
128   XtVaSetValues(fsb, XmNwidth, width, XmNheight, height,
129                 XmNresizePolicy, XmRESIZE_NONE, NULL);
130  }
131 
132  if(title && strlen(title))
133  {
134   Widget shell = XtParent(fsb);
135   XtVaSetValues(shell, XmNtitle, title, NULL);
136  }
137 
138  XtManageChild(fsb);
139 
140  XtomWait(parent);
141  return(string);
142 }
143 
XtomGetChoice(Widget parent,const char * title,const char * message)144 Boolean XtomGetChoice(Widget parent, const char *title, const char *message)
145 {
146  static Widget diaW;
147  XmString xstr1, xstr2;
148  Arg args[10];
149  Cardinal n;
150 
151  block = True;
152 
153  if(diaW == NULL)
154  {
155   n=0;
156   XtSetArg(args[n], XmNautoUnmanage, True);                    n++;
157   XtSetArg(args[n], XmNdialogStyle,  XmDIALOG_FULL_APPLICATION_MODAL);
158                                                                n++;
159   XtSetArg(args[n], XmNmessageAlignment, XmALIGNMENT_CENTER);  n++;
160   diaW = XmCreateQuestionDialog(parent, "question", args, n);
161   XtUnmanageChild(XmMessageBoxGetChild(diaW, XmDIALOG_HELP_BUTTON));
162 
163   XtAddCallback(diaW, XmNokCallback, (XtCallbackProc)yesnoCB,
164                                      (XtPointer)True);
165   XtAddCallback(diaW, XmNcancelCallback, (XtCallbackProc)yesnoCB,
166                                      (XtPointer)False);
167 #ifdef HAS_XPM
168   if(skin)
169    SkinToWidgets(diaW, skin);
170 #endif
171  }
172 
173  xstr1 = XMSTR(message);
174  xstr2 = XMSTR(title);
175 
176  XtVaSetValues(diaW,
177                XmNmessageString, xstr1,
178                XmNdialogTitle,   xstr2,
179                NULL);
180  XmStringFree(xstr1);
181  XmStringFree(xstr2);
182 
183  XtManageChild(diaW);
184 
185  XtomWait(parent);
186  return yesno;
187 }
188 
XtomShowMessage(Widget parent,unsigned char dialogType,const char * title,const char * message)189 void XtomShowMessage(Widget parent, unsigned char dialogType,
190                      const char *title, const char *message)
191 {
192  static Widget diaW;
193  XmString xstr1, xstr2;
194  Arg args[10];
195  Cardinal n;
196 
197  block = True;
198 
199  if(diaW == NULL)
200  {
201   n=0;
202   XtSetArg(args[n], XmNautoUnmanage, True);                    n++;
203   XtSetArg(args[n], XmNdialogStyle,  XmDIALOG_FULL_APPLICATION_MODAL);
204                                                                n++;
205   XtSetArg(args[n], XmNmessageAlignment, XmALIGNMENT_CENTER);  n++;
206   diaW = XmCreateMessageDialog(parent, "message", args, n);
207   XtUnmanageChild(XmMessageBoxGetChild(diaW, XmDIALOG_HELP_BUTTON));
208   XtUnmanageChild(XmMessageBoxGetChild(diaW, XmDIALOG_CANCEL_BUTTON));
209 
210   XtAddCallback(diaW, XmNokCallback, (XtCallbackProc)cancelCB,
211                                                      (XtPointer)NULL);
212   XtAddCallback(diaW, XmNcancelCallback, (XtCallbackProc)cancelCB,
213                                                      (XtPointer)NULL);
214 #ifdef HAS_XPM
215   if(skin)
216    SkinToWidgets(diaW, skin);
217 #endif
218  }
219 
220  xstr1 = XMSTR(message);
221  xstr2 = XMSTR(title);
222 
223  XtVaSetValues(diaW,
224                XmNmessageString, xstr1,
225                XmNdialogTitle,   xstr2,
226                XmNdialogType,    dialogType,
227                NULL);
228  XmStringFree(xstr1);
229  XmStringFree(xstr2);
230 
231  XtManageChild(diaW);
232 
233  XtomWait(parent);
234  return;
235 }
236 
XMSTR(const char * str)237 XmString XMSTR(const char *str)
238 {
239  char *tmp = strdup(str);
240  XmString xmstr = XmStringCreateLtoR(tmp, XmSTRING_DEFAULT_CHARSET);
241 
242  free(tmp);
243  return(xmstr);
244 }
245 
246 
247