dtinfo subtree dtinfo
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / Agents / AddLibraryAgentMotif.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  *  $TOG: AddLibraryAgentMotif.C /main/10 1998/08/05 11:46:23 mgreess $
25  *
26  * (c) Copyright 1996 Digital Equipment Corporation.
27  * (c) Copyright 1996 Hewlett-Packard Company.
28  * (c) Copyright 1996 International Business Machines Corp.
29  * (c) Copyright 1996 Sun Microsystems, Inc.
30  * (c) Copyright 1996 Novell, Inc. 
31  * (c) Copyright 1996 FUJITSU LIMITED.
32  * (c) Copyright 1996 Hitachi.
33  *
34  */
35
36 #define C_List
37 #define C_TOC_Element
38 #define L_Basic
39
40 #define C_EnvMgr
41 #define L_Managers
42
43 #define C_WindowSystem
44 #define L_Other
45
46 #define C_AddLibraryAgent
47 #define C_HelpAgent
48 #define L_Agents
49 #include <sstream>
50 using namespace std;
51
52 #include "Other/XmStringLocalized.hh"
53 #include "Managers/CatMgr.hh"
54
55 #include <Prelude.h>
56
57 #include "Registration.hh"
58
59 #define CLASS AddLibraryAgent
60 #include "create_macros.hh"
61
62 #include <WWL/WComposite.h>
63 #include <WWL/WXmSeparator.h>
64
65 AddLibraryAgent::AddLibraryAgent (Widget parent):
66         f_shell (parent, True, "add_library_agent"),
67         f_done (False), f_cancelled (False), f_file_sel (NULL) {
68
69     create_ui (parent);
70 }
71
72 AddLibraryAgent::~AddLibraryAgent() {
73     f_form.Unmanage ();
74     XtDestroyWidget (XtParent(f_form));
75 }
76
77 void
78 AddLibraryAgent::create_ui (Widget parent) {
79
80     XmStringLocalized mtfstring;
81     String            string;
82
83     window_system().register_full_modal_shell (&f_shell);
84
85     string = msg_catalog_mgr().catgets(Set_AddLibraryAgent, 1);
86     XtVaSetValues((Widget)f_shell, XmNtitle, string, NULL);
87
88     // order of these macros must match the .res file occurences for same
89     ASSN (WXmForm,              f_form,         f_shell,        "form");
90     DECLM (WXmForm,             sub_form,       f_form,         "sub_form");
91     DECLM (WXmLabel,            label,          sub_form,       "theLabel");
92     DECLM (WXmPushButton,       pick,           sub_form,       "pick");
93     ASSNM (WXmTextField,        f_text,         sub_form,       "text");
94     DECLM (WXmSeparator,        sep,            f_form,         "separator");
95     ASSNM (WXmPushButton,       f_ok,           f_form,         "ok");
96     ASSNM (WXmPushButton,       f_clr,          f_form,         "clear");
97     DECLM (WXmPushButton,       canc,           f_form,         "cancel");
98     DECLM (WXmPushButton,       help,           f_form,         "help");
99
100     mtfstring =  msg_catalog_mgr().catgets(Set_AgentLabel, 160);
101     XtVaSetValues(label, XmNlabelString, (XmString)mtfstring, NULL);
102     mtfstring =  msg_catalog_mgr().catgets(Set_AgentLabel, 161);
103     XtVaSetValues(f_ok, XmNlabelString, (XmString)mtfstring, NULL);
104     mtfstring =  msg_catalog_mgr().catgets(Set_AgentLabel, 162);
105     XtVaSetValues(canc, XmNlabelString, (XmString)mtfstring, NULL);
106
107     mtfstring =  CATGETS(Set_AgentLabel, 223, "Clear");
108     XtVaSetValues(f_clr, XmNlabelString, (XmString)mtfstring, NULL);
109     mtfstring =  CATGETS(Set_AgentLabel, 270, "...");
110     XtVaSetValues(pick, XmNlabelString, (XmString)mtfstring, NULL);
111     mtfstring = CATGETS(Set_AgentLabel, 48, "Help");
112     XtVaSetValues(help, XmNlabelString, (XmString)mtfstring, NULL);
113
114
115     f_form.DefaultButton (f_ok);
116     SET_CALLBACK (f_text, ValueChanged, textChanged);
117     SET_CALLBACK (f_text, ModifyVerify, verifyNoSpace);
118     ON_ACTIVATE (pick, pick_dir);
119     ON_ACTIVATE (f_ok, ok);
120     ON_ACTIVATE (f_clr, clear);
121     ON_ACTIVATE (canc, cancel);
122     help_agent().add_activate_help (help, (char*)"add_infolib_help");
123
124     f_shell.Realize ();
125     // XtVaSetValues(f_form, XmNinitialFocus, (Widget)f_text, NULL);
126 }
127
128 void
129 AddLibraryAgent::ok () {
130     f_done = TRUE;
131     f_form.Unmanage ();
132 }
133
134 void
135 AddLibraryAgent::clear () {
136  // autoUnmanage must be set to False for this button to work properly
137
138     XmTextFieldSetString( f_text, (char*)"" );
139 }
140
141 void
142 AddLibraryAgent::cancel () {
143     f_done = TRUE;
144     f_cancelled = TRUE;
145     f_form.Unmanage ();
146 }
147
148 void
149 AddLibraryAgent::textChanged (WCallback *wcb) {
150     char *theText = XmTextFieldGetString (wcb->GetWidget());
151     if( theText && *theText ) {
152         f_ok.SetSensitive (True);
153         f_clr.SetSensitive (True);
154     }
155     else
156     {
157         f_ok.SetSensitive (False);
158         f_clr.SetSensitive (False);
159     }
160     XtFree (theText);
161 }
162
163 void
164 AddLibraryAgent::verifyNoSpace (WCallback *wcb) {
165     CALL_DATA (XmTextVerifyCallbackStruct, tvp);
166     tvp->doit = True;
167     if (tvp->text != NULL) {
168         int length = tvp->text->length;
169         char *cp = tvp->text->ptr;
170         for ( ; length > 0; cp ++, length --) {
171             //if (*cp == ' ' || *cp == '\t' || *cp == '\n') {
172             if (*cp == '\t' || *cp == '\n') {
173                 tvp->doit = False;
174                 return;
175             }
176         }
177     }
178 }
179
180 char *
181 AddLibraryAgent::getInfolibName () {
182
183     f_done = f_cancelled = False;
184     f_form.Manage();
185     XmProcessTraversal( (Widget)f_text, XmTRAVERSE_CURRENT );
186     // XtSetKeyboardFocus( (Widget)f_form, (Widget)f_text );
187     // ((WXmForm *) XtParent(XtParent (f_text)))->InitialFocus (f_text);
188
189     f_shell.MinWidth (f_shell.Width());
190     f_shell.MinHeight (f_shell.Height());
191     f_shell.MaxHeight (f_shell.Height());
192
193     XtAppContext app_context = window_system().app_context ();
194     XEvent event;
195     while (!f_done) {
196         XtAppNextEvent (app_context, &event);
197         XtDispatchEvent (&event);
198     }
199     if (f_cancelled)
200         return NULL;
201     return XmTextFieldGetString (f_text);
202 }
203
204 void
205 AddLibraryAgent::pick_dir () {
206
207   if( !f_file_sel )
208   {
209     Arg args[20];
210     int n;
211
212     XmString title_str = XmStringCreateLocalized(
213           CATGETS(Set_AddLibraryAgent, 10, "Dtinfo: Infolib Selection"));
214     XmString no_libs_str = XmStringCreateLocalized(
215           CATGETS(Set_AddLibraryAgent, 11, "No Infolibs Present"));
216     XmString patt_str = XmStringCreateLocalized( (char*)"*.dti" );
217     XmString infolibs_str = XmStringCreateLocalized( (char*)"Infolibs" );
218
219     // default initial directory on first entry
220     char *buf = new char[256];
221
222 // Note: infolibs need to be placed in platform-portably-named
223 //       LANG-type subdirectories, so the normalized CDE language
224 //       string can be used for lookup. For now, must assume the
225 //       user environment LANG is OK ...
226     char *tmpstr;
227     if( tmpstr = getenv("LANG") )
228         sprintf( buf, "/usr/dt/infolib/%s/", tmpstr );
229     else
230         sprintf( buf, "/usr/dt/infolib/%s/", env().lang() );
231 //
232     XmString basedir_str = XmStringCreateLocalized( buf );
233     strcat( buf, "*.dti" );
234     XmString initdir_str = XmStringCreateLocalized( buf );
235     delete [] buf;
236
237     n = 0;
238     XtSetArg(args[n], XmNdialogTitle, title_str); n++;
239     XtSetArg(args[n], XmNdirectory, basedir_str); n++;
240     XtSetArg(args[n], XmNdirMask, initdir_str); n++;
241     XtSetArg(args[n], XmNfileListLabelString, infolibs_str); n++;
242     XtSetArg(args[n], XmNfileTypeMask, XmFILE_DIRECTORY);  n++;
243     XtSetArg(args[n], XmNnoMatchString, no_libs_str); n++;
244     XtSetArg(args[n], XmNpattern, patt_str); n++;
245     XtSetArg(args[n], XmNpathMode, XmPATH_MODE_RELATIVE); n++;
246
247     f_file_sel = XmCreateFileSelectionDialog(
248                       f_shell, (char*)"pick_dir_file", args, n );
249
250     XmStringFree(basedir_str);
251     XmStringFree(initdir_str);
252     XmStringFree(infolibs_str);
253     XmStringFree(patt_str);
254     XmStringFree(no_libs_str);
255     XmStringFree(title_str);
256
257     XtAddCallback ( f_file_sel,
258                       XmNokCallback,
259                       &AddLibraryAgent::fileSelectedCallback,
260                       (XtPointer) this );
261     XtAddCallback ( f_file_sel,
262                       XmNcancelCallback,
263                       &AddLibraryAgent::cancelCallback,
264                       (XtPointer) this );
265   }
266   XtManageChild( f_file_sel );
267 }
268
269 void
270 AddLibraryAgent::cancelCallback ( Widget w,
271                                      XtPointer,
272                                      XtPointer )
273 {
274     XtUnmanageChild ( w );   // Bring the file selection dialog down.
275 }
276
277 void
278 AddLibraryAgent::fileSelectedCallback ( Widget    w,
279                                           XtPointer client_data,
280                                           XtPointer call_data )
281 {
282     AddLibraryAgent * obj = (AddLibraryAgent *) client_data;
283
284     XmFileSelectionBoxCallbackStruct *cb =
285         (XmFileSelectionBoxCallbackStruct *) call_data;
286     char     *name   = NULL;
287     XmString  xmstr  = cb->value;  // The selected file
288     int       status = 0;
289
290     if ( xmstr )   // Make sure a file was selected
291     {
292         // Extract the first character string matching the default
293         // character set from the compound string
294
295         status = XmStringGetLtoR ( xmstr, XmFONTLIST_DEFAULT_TAG, &name );
296          if(status && strlen(name)<1)
297          {
298             XtFree(name);
299             return;
300          }
301          if( status )
302          {
303             XmTextFieldSetString( (Widget) obj->f_text, name );
304             // XtSetKeyboardFocus( (Widget) obj->f_form, (Widget) obj->f_ok );
305             XmProcessTraversal( (Widget) obj->f_ok, XmTRAVERSE_CURRENT );
306          }
307     }
308     XtUnmanageChild ( w );   // Bring the file selection dialog down.
309 }
310
311