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