Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtfile / dtcopy / utils.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 /* $XConsortium: utils.c /main/5 1996/10/17 09:36:36 mustafa $ */
24 /************************************<+>*************************************
25  ****************************************************************************
26  *
27  *   FILE:           utils.c
28  *
29  *
30  *   DESCRIPTION:    Utility routines
31  *
32  *   FUNCTIONS: Create_Action_Area
33  *              Display_Help
34  *              Get_Help_Dialog
35  *              Help
36  *              _DtChildPosition
37  *              help_callback
38  *              help_close_callback
39  *              help_hyperyperlink_callback
40  *
41  *   (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
42  *   (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
43  *   (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
44  *   (c) Copyright 1993, 1994, 1995 Novell, Inc.
45  *
46  ****************************************************************************
47  ************************************<+>*************************************/
48
49 #include <time.h>
50 #include <X11/Intrinsic.h>
51 #include <X11/StringDefs.h>
52 #include <Xm/XmP.h>
53 #include <Xm/Xm.h>
54 #include <Xm/VendorSEP.h>
55 #include <Xm/PushB.h>
56 #include <Xm/Form.h>
57 #include <Xm/BulletinB.h>
58 #include <Xm/TextF.h>
59 /* Copied from Xm/BaseClassI.h */
60 extern XmWidgetExtData _XmGetWidgetExtData( 
61                         Widget widget,
62 #if NeedWidePrototypes
63                         unsigned int extType) ;
64 #else
65                         unsigned char extType) ;
66 #endif /* NeedWidePrototypes */
67
68 #include <Dt/HelpDialog.h>
69
70 #include "dtcopy.h"
71 #include "sharedFuncs.h"
72
73
74
75 /*............... definitions for Help .............................*/
76 void
77 Help(
78         char *helpVolume,
79         char *locationId);
80 void
81 Display_Help(
82         char *helpVolume,
83         char *locationId);
84
85 /* Help volume tags used to attach a certian help volume
86  * to a particular call back.  Look to function help_callback
87  * for more code.  MJT
88  */
89 #define HELP_USING_HELP_VOLUME          "Help4Help"
90 #define HELP_USING_STR                  "_HomeTopic"
91 #define HELP_VOLUME                     "Filemgr"
92 #define HELP_HELP_MENU_STR              "Reference"
93 #define HELP_INTRODUCTION_STR           "_HomeTopic"
94 #define HELP_DIR_COPY_STR               "FMCopyDirDialogStatusDE"
95 #define HELP_DIR_ERROR_STR              "FMCopyDIrDialogErrorDE"
96 #define HELP_OVERWRITE_STR              "FMCopyDirOverDialogDE"
97 #define HELP_VERSION_STR                "FMHelpMenuDE"
98 #define STOPW_HELP_STR                  "FMCopyFoldWarnDialogDE"
99
100 /*--------------------------------------------------------------------
101  * Callback routines
102  *------------------------------------------------------------------*/
103
104 void
105 help_close_callback(
106         Widget widget,
107         XtPointer client_data,
108         XtPointer call_data)
109 {
110   XtUnmapWidget(XtParent(widget));
111 }
112
113 void
114 help_hyperyperlink_callback(
115         Widget widget,
116         XtPointer client_data,
117         XtPointer call_data)
118 {
119   DtHelpDialogCallbackStruct *pHyper = (DtHelpDialogCallbackStruct *) call_data;
120
121   switch(pHyper->hyperType)
122   {
123      case DtHELP_LINK_JUMP_NEW:
124           Display_Help(pHyper->helpVolume, pHyper->locationId);
125           break;
126      default:
127           ;
128    }
129 }
130
131  void
132 help_callback(
133         Widget widget,
134         XtPointer client_data,
135         XtPointer call_data)
136 {
137   char  *helpVolume, *locationId;
138   int   topic;
139
140   topic = (int) client_data;
141   helpVolume = HELP_VOLUME;
142
143   switch (topic)
144   {
145         case HELP_HELP_MENU:
146             locationId = HELP_HELP_MENU_STR;
147             break;
148         case HELP_INTRODUCTION:
149             locationId = HELP_INTRODUCTION_STR;
150             break;
151         case HELP_DIR_COPY:
152             locationId = HELP_DIR_COPY_STR;
153             break;
154         case HELP_DIR_ERROR:
155             locationId = HELP_DIR_ERROR_STR;
156             break;
157         case HELP_OVERWRITE:
158             locationId = HELP_OVERWRITE_STR;
159             break;
160         case HELP_USING:
161             locationId = HELP_USING_STR;
162             helpVolume = HELP_USING_HELP_VOLUME;
163             break;
164         case HELP_VERSION:
165             locationId = HELP_VERSION_STR;
166             break;
167         case STOPW_HELP_DIALOG:
168             locationId = STOPW_HELP_STR;
169             break;
170         default:
171             locationId = HELP_USING_STR;
172             helpVolume = HELP_USING_HELP_VOLUME;
173             break;
174   }
175   Help(helpVolume, locationId);
176 }
177
178
179 /*--------------------------------------------------------------------
180  * _DtChildPosition
181  *------------------------------------------------------------------*/
182
183 void
184 _DtChildPosition(
185          Widget w,
186          Widget parent,
187          Position *newX,
188          Position *newY)
189 {
190    Position pY, pX;
191    XmVendorShellExtObject vendorExt;
192    XmWidgetExtData        extData;
193    int xOffset, yOffset;
194    int pHeight, myHeight, sHeight;
195    int pWidth, myWidth, sWidth;
196    enum { posRight, posBelow, posLeft, posAbove } pos;
197    int space;
198
199    /* get x, y offsets for the parent's window frame */
200    extData = _XmGetWidgetExtData(parent, XmSHELL_EXTENSION);
201    if (extData)
202    {
203      vendorExt = (XmVendorShellExtObject)extData->widget;
204      xOffset = vendorExt->vendor.xOffset;
205      yOffset = vendorExt->vendor.yOffset;
206    }
207    else
208      xOffset = yOffset = 0;
209
210    /* get size/position of screen, parent, and widget */
211    sHeight = HeightOfScreen(XtScreen(parent));;
212    sWidth = WidthOfScreen(XtScreen(parent));
213    pX = XtX(parent) - xOffset;
214    pY = XtY(parent) - yOffset;
215    pHeight = XtHeight(parent) + yOffset + xOffset;
216    pWidth = XtWidth(parent) + 2*xOffset;
217    myHeight = XtHeight(w) + yOffset + xOffset;
218    myWidth = XtWidth(w) + 2*xOffset;
219
220    {
221      XWindowAttributes attr;
222
223      XGetWindowAttributes(XtDisplay(parent), XtWindow(parent), &attr);
224    }
225
226    /*
227     * Determine how much space would be left if the child was positioned
228     * to the right, below, left, or above the parent.  Choose the child
229     * positioning so that the maximum space is left.
230     */
231    pos = posRight;
232    space = sWidth - (pX + pWidth + myWidth);
233
234    if (sHeight - (pY + pHeight + myHeight) > space)
235    {
236       pos = posBelow;
237       space = sHeight - (pY + pHeight + myHeight);
238    }
239
240    if (pX - myWidth > space)
241    {
242       pos = posLeft;
243       space = pX - myWidth;
244    }
245
246    if (pY - myHeight > space)
247    {
248       pos = posAbove;
249       space = pY - myHeight;
250    }
251
252    /* Given relative positioning, determine x, y coordinates for the child */
253
254    switch (pos)
255    {
256      case posRight:
257        *newX = pX + pWidth + 5;
258        *newY = pY + (pHeight - myHeight)/2;
259        break;
260
261      case posBelow:
262        *newX = pX + (pWidth - myWidth)/2;
263        *newY = pY + pHeight + 5;
264        break;
265
266      case posLeft:
267        *newX = pX - myWidth - 5;
268        *newY = pY + (pHeight - myHeight)/2;
269        break;
270
271      case posAbove:
272        *newX = pX + (pWidth - myWidth)/2;
273        *newY = pY - myHeight - 5;
274        break;
275    }
276 }
277
278
279
280 static Widget
281 Get_Help_Dialog()
282 {
283     Widget dialog;
284     Arg args[5];
285     int n;
286
287         n = 0;
288         XtSetArg(args[n], XmNtitle, GETMESSAGE(6, 5, "Directory Copy Help")); n++;
289         dialog = DtCreateHelpDialog(G_toplevel, "helpDlg",  args, n);
290
291         XtAddCallback(dialog, DtNhyperLinkCallback,
292                       (XtCallbackProc)help_hyperyperlink_callback, NULL);
293         XtAddCallback(dialog, DtNcloseCallback,
294                       (XtCallbackProc)help_close_callback, NULL);
295         return dialog;
296 }
297
298 void
299 Display_Help(
300         char *helpVolume,
301         char *locationId)
302 {
303     Arg args[10];
304     int n;
305
306     G_help_dialog = Get_Help_Dialog();
307
308     n = 0;
309     XtSetArg(args[n], DtNhelpType, DtHELP_TYPE_TOPIC); n++;
310     XtSetArg(args[n], DtNhelpVolume, helpVolume);       n++;
311     XtSetArg(args[n], DtNlocationId, locationId);       n++;
312     XtSetValues(G_help_dialog, args, n);
313
314     XtManageChild(G_help_dialog);
315     XtMapWidget(XtParent(G_help_dialog));
316 }
317
318 void
319 Help(
320    char *helpVolume,
321    char *locationId)
322 {
323     Arg args[10];
324     int n;
325
326     if(G_help_dialog == NULL)
327     {
328         n = 0;
329         XtSetArg(args[n], DtNhelpType, DtHELP_TYPE_TOPIC); n++;
330         XtSetArg(args[n], DtNhelpVolume, helpVolume);   n++;
331         XtSetArg(args[n], DtNlocationId, locationId);   n++;
332         XtSetArg (args[n], XmNtitle,  GETMESSAGE(6, 5, "Directory Copy Help")); n++;
333
334         G_help_dialog = DtCreateHelpDialog(G_toplevel, "helpDlg", args, n);
335
336         XtAddCallback(G_help_dialog, DtNhyperLinkCallback,
337                       (XtCallbackProc)help_hyperyperlink_callback, NULL);
338         XtAddCallback(G_help_dialog, DtNcloseCallback,
339                       (XtCallbackProc)help_close_callback, NULL);
340
341         XtManageChild(G_help_dialog);
342     }
343     else
344     {
345         n = 0;
346         XtSetArg(args[n], DtNhelpType, DtHELP_TYPE_TOPIC); n++;
347         XtSetArg(args[n], DtNhelpVolume, helpVolume);   n++;
348         XtSetArg(args[n], DtNlocationId, locationId);   n++;
349         XtSetValues(G_help_dialog, args, n);
350     }
351
352     XtMapWidget(XtParent(G_help_dialog));
353 }
354
355
356
357 /*--------------------------------------------------------------------
358  *
359  * Create Action Area
360  *
361  * Use of this function requires #include "sharedFuncs.h"
362  * The source for Create_Action_Area is in dtfile/OverWrite.c and
363  * dtfile/dtcopy/utils.c. Because it uses GETMESSAGE, it could not
364  * be placed in dtcopy/sharedFunc.c.
365  *
366  *------------------------------------------------------------------*/
367
368 Widget
369 Create_Action_Area(
370         Widget parent_widget,
371         ActionAreaDefn actions,
372         Widget *pushbutton_array)
373 {
374   Widget action_area, widget;
375   int i;
376   int fractbase_value;
377   XmString xm_string;
378
379
380   if (actions.defaultAction < 0  ||  actions.defaultAction > actions.numActions-1)
381      actions.defaultAction = 0;
382
383   fractbase_value = (TIGHTNESS * actions.numActions) - 1;
384   action_area = XtVaCreateManagedWidget("action_area", xmFormWidgetClass ,parent_widget,
385                                 XmNfractionBase, fractbase_value,
386                                 XmNleftAttachment, XmATTACH_FORM,
387                                 XmNrightAttachment, XmATTACH_FORM,
388                                 XmNbottomAttachment, XmATTACH_FORM,
389                                 NULL);
390
391   for (i=0; i < actions.numActions ;i++ )
392     {
393         xm_string = XmStringCreateLocalized(GETMESSAGE(actions.actionList[i].msg_set,
394                                                 actions.actionList[i].msg_num,
395                                                 actions.actionList[i].label));
396       widget = XtVaCreateManagedWidget(actions.actionList[i].label,
397                xmPushButtonWidgetClass, action_area,
398                XmNleftAttachment,     (i ? XmATTACH_POSITION: XmATTACH_FORM),
399                XmNleftPosition,       (TIGHTNESS * i),
400                XmNtopAttachment,      XmATTACH_FORM,
401                XmNbottomAttachment,   XmATTACH_FORM,
402                XmNrightAttachment,
403                         ((i != (actions.numActions - 1)) ? XmATTACH_POSITION: XmATTACH_FORM),
404                XmNrightPosition,      ((TIGHTNESS*i) + (TIGHTNESS - 1)),
405                XmNshowAsDefault,      (i == actions.defaultAction),
406                XmNdefaultButtonShadowThickness, 1,
407                XmNlabelString,         xm_string,
408                NULL);
409       XmStringFree(xm_string);
410
411
412
413        if (actions.actionList[i].callback)
414          {
415            XtAddCallback(widget, XmNactivateCallback,
416                          actions.actionList[i].callback, actions.actionList[i].data);
417          }
418
419        if (i == actions.defaultAction)
420          {
421             Dimension height, h;
422             XtVaGetValues (action_area, XmNmarginHeight, &h, NULL);
423             XtVaGetValues (widget, XmNheight, &height, NULL);
424
425             height +=2 * h;
426             XtVaSetValues (action_area,
427                            XmNdefaultButton, widget,
428                            XmNpaneMaximum,   height,
429                            XmNpaneMinimum,   height,
430                            NULL);
431
432          }
433
434        if (pushbutton_array != NULL)
435            pushbutton_array[i] = widget;
436
437     } /* endfor */
438
439    XtManageChild(action_area);
440    return action_area;
441
442 }  /* end Create_Action_Area */
443