b9347c6c28ee56ffcb4a2766e82f07a1d068cd37
[oweals/cde.git] / cde / programs / dtprintinfo / UI / DtPrtProps.C
1 /* $TOG: DtPrtProps.C /main/4 1998/07/24 16:14:02 mgreess $ */
2 /*                                                                      *
3  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
4  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
5  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
6  * (c) Copyright 1993, 1994 Novell, Inc.                                *
7  */
8
9 #include "DtPrtProps.h"
10 #include "DtFindSet.h"
11 #include "DtMainW.h"
12 #include "DtPrinterIcon.h"
13 #include "Button.h"
14 #include "Container.h"
15 #include "Group.h"
16 #include "IconObj.h"
17 #include "LabelObj.h"
18 #include "Sep.h"
19
20 #include "Dt/Action.h"
21
22 #include "dtprintinfomsg.h"
23
24 #include <stdio.h>
25 #include <unistd.h> // This is for the getpid function
26 #include <stdlib.h> // This is for the getenv function
27
28 DtPrtProps::DtPrtProps(DtMainW *parent, char *name, DtPrinterIcon *_printer)
29         : Dialog(parent, name)
30 {
31    mainw = parent;
32    printer = _printer;
33    _iconFile = STRDUP(printer->IconFile());
34    findSetD = NULL;
35    _has_been_posted = false;
36
37    int i, captionWidth = 0, width, columns = 0;
38    int n_attributes = printer->QueueObj()->NumAttributes();
39    Attribute **attributes = printer->QueueObj()->Attributes();
40    if ((width = StringWidth(MESSAGE(IconLabelL))) > captionWidth)
41       captionWidth = width;
42    if ((width = StringWidth(MESSAGE(IconGroupTitleL))) > captionWidth)
43       captionWidth = width;
44    if ((width = StringWidth(MESSAGE(DescriptionL))) > captionWidth)
45       captionWidth = width;
46    if ((width = strlen(printer->Name())) > columns)
47       columns = width;
48    for (i = 0; i < n_attributes; i++)
49     {
50       if ((width = StringWidth(attributes[i]->DisplayName)) > captionWidth)
51          captionWidth = width;
52       if ((width = strlen(attributes[i]->DisplayValue)) > columns)
53          columns = width;
54     }
55
56    icon_prompt = new Prompt(this, MESSAGE(IconLabelL), true, STRING_PROMPT,
57                             (char *)printer->Name(), NULL,
58                             NULL, true, columns, 1, captionWidth + 8);
59    icon_prompt->AttachLeft();
60    icon_prompt->AttachRight();
61    icon_prompt->AttachTop(5);
62                        
63    Container *form = new Container(this, "form", FORM);
64    icon_group = new Group(form, NULL, FORM_BOX);
65    LabelObj *label = new LabelObj(form, MESSAGE(IconGroupTitleL),
66                                   RIGHT_JUSTIFIED);
67    label->AttachTop();
68    label->AttachBottom();
69    label->AttachLeft();
70    label->Width(captionWidth + 8);
71    icon_group->AttachTop();
72    icon_group->AttachBottom();
73    icon_group->AttachRight();
74    icon_group->AttachLeft(label);
75
76    // Creation of Large Icon
77    largeIcon = new IconObj(icon_group, NULL, printer->IconFile());
78    largeIcon->IconView(VERY_LARGE_ICON);
79    largeIcon->AttachLeft(10);
80    largeIcon->AttachTop(10);
81    largeIcon->AttachBottom(10);
82
83    // Creation of Medium Icon
84    mediumIcon = new IconObj(icon_group, NULL, printer->IconFile());
85    mediumIcon->IconView(MEDIUM_ICON);
86    mediumIcon->AttachLeft(largeIcon, 10);
87    mediumIcon->AttachBottom(10);
88
89    // Creation of Small Icon
90    smallIcon = new IconObj(icon_group, NULL, printer->IconFile());
91    smallIcon->IconView(SMALL_ICON);
92    smallIcon->AttachLeft(mediumIcon, 10);
93    smallIcon->AttachBottom(10);
94
95    // Creation of Find Set Button
96    find_set = new Button(icon_group, MESSAGE(FindSetL), PUSH_BUTTON, FindSetCB,
97                          this);
98     
99    find_set->AttachRight(10);
100    find_set->AttachLeft(smallIcon, 10);
101    find_set->AttachBottom(10);
102
103    form->AttachLeft();
104    form->AttachRight();
105    form->AttachTop(icon_prompt, 5);
106
107    Container *rc = new Container(this, "rc", VERTICAL_ROW_COLUMN);
108    for (i = 0; i < n_attributes; i++)
109     {
110       new Prompt(rc, attributes[i]->DisplayName, false, STRING_PROMPT,
111                  attributes[i]->DisplayValue, NULL,
112                  NULL, true, columns, 1, captionWidth + 8);
113       if (mainw->PrinterAppMode() != CONFIG_PRINTERS)
114        {
115          char *value;
116          if (i == 0)
117             value = _printer->PrintQueueUp() ? MESSAGE(UpL) : MESSAGE(DownL);
118          else
119 #ifdef aix
120             value = _printer->PrintDeviceUp(i - 1) ? MESSAGE(UpL) :
121                                                      MESSAGE(DownL);
122 #else
123             value = _printer->PrintDeviceUp() ? MESSAGE(UpL) : MESSAGE(DownL);
124 #endif
125          new Prompt(rc, MESSAGE(PrinterStatusL), false, STRING_PROMPT,
126                     value, NULL, NULL, true, columns, 1, captionWidth + 8);
127        }
128     }
129
130    rc->AttachRight();
131    rc->AttachLeft();
132    rc->AttachBottom();
133
134    Sep *sep = new Sep(this);
135    sep->AttachRight();
136    sep->AttachLeft();
137    sep->AttachBottom(rc, 5);
138
139    description = new Prompt(this, MESSAGE(DescriptionL), false,
140                             MULTI_LINE_STRING_PROMPT, printer->Description(),
141                             NULL, NULL, true, columns, 3, captionWidth + 8);
142    description->AttachLeft();
143    description->AttachRight();
144    description->AttachTop(form, 5);
145    description->AttachBottom(sep, 5);
146
147    ok = new Button(this, MESSAGE(OKL), PUSH_BUTTON, OkCB, this);
148    cancel = new Button(this, MESSAGE(CancelL), PUSH_BUTTON, CancelCB, this);
149    help = new Button(this, MESSAGE(HelpL), PUSH_BUTTON, HelpCB, this);
150    DefaultButton(ok);
151    CancelButton(cancel);
152 }
153
154 DtPrtProps::~DtPrtProps()
155 {
156    delete _iconFile;
157 }
158
159 boolean DtPrtProps::SetVisiblity(boolean flag)
160 {
161    Dialog::SetVisiblity(flag);
162    if (_has_been_posted == false)
163     {
164       _has_been_posted = true;
165       find_set->Height(find_set->Height() + 6);
166     }
167    else if (flag)
168       Reset();
169    return true;
170 }
171
172 void DtPrtProps::Reset()
173 {
174    if (strcmp(printer->IconFile(), _iconFile))
175       SetActionIcons(printer->IconFile());
176    icon_prompt->Value((char *)printer->Name());
177    description->Value(printer->Description());
178 }
179
180 void DtPrtProps::Apply()
181 {
182    char *buf;
183    int buf_len;
184    char *iconName = (char *)IconLabel();
185    char *iconFile = (char *)IconFileName();
186
187    if (!iconName || *iconName == '\0')
188       iconName = (char *)printer->Name();
189    if (!iconFile || *iconFile == '\0')
190       iconFile = (char *)PRINTER_ICON_FILE;
191
192    if (!strcmp(iconName, printer->Name()) &&
193        !strcmp(iconFile, printer->IconFile()))
194     {
195       // return since nothing changed
196       return;
197     }
198
199    char *save_msg = strdup(mainw->status_line->Name());
200    mainw->WorkingCursor(true);
201    mainw->status_line->Name(MESSAGE(UpdatingActionsL));
202    Refresh();
203    buf_len = strlen(printer->QueueObj()->Name()) +
204              (2 * strlen(printer->HomeDir())) + 50;
205    if (buf_len < 200)
206       buf_len = 200;
207    buf = new char [buf_len];
208    FILE *fp_src, *fp_dest;
209    char *filename = printer->CreateActionFile();
210    sprintf(buf, "%s.%ld", filename, (long)getpid());
211    if (fp_dest = fopen(buf, "w"))
212     {
213       if (fp_src = fopen(filename, "r"))
214        {
215          while (fgets(buf, buf_len, fp_src))
216           {
217             if (strstr(buf, "LABEL"))
218                fprintf(fp_dest, "        LABEL                   %s\n",
219                        iconName);
220             else if (strstr(buf, "ICON"))
221                fprintf(fp_dest, "        ICON                    %s\n",
222                        iconFile);
223             else
224                fprintf(fp_dest, "%s", buf);
225           }
226          fclose(fp_src);
227        }
228     }
229    fclose(fp_dest);
230    sprintf(buf, "%s.%ld", filename, (long)getpid());
231    rename(buf, filename);
232    DtActionInvoke(((AnyUI *)mainw->Parent())->BaseWidget(), "ReloadActions",
233                   NULL, 0, NULL, NULL, NULL, True, NULL, NULL);
234    delete [] buf;
235    mainw->WorkingCursor(false);
236    mainw->status_line->Name(save_msg);
237 }
238
239 void DtPrtProps::CloseCB()
240 {
241    Reset();
242    Visible(false);
243 }
244
245 void DtPrtProps::OkCB(void *data)
246 {
247    DtPrtProps *obj = (DtPrtProps *) data;
248    
249    obj->Apply();
250    obj->Visible(false);
251 }
252
253 void DtPrtProps::ApplyCB(void *data)
254 {
255    DtPrtProps *obj = (DtPrtProps *) data;
256    obj->Apply();
257 }
258
259 void DtPrtProps::CancelCB(void *data)
260 {
261    DtPrtProps *obj = (DtPrtProps *) data;
262    
263    obj->Reset();
264    obj->Visible(false);
265 }
266
267 void DtPrtProps::ResetCB(void *data)
268 {
269    DtPrtProps *obj = (DtPrtProps *) data;
270    
271    obj->Reset();
272 }
273
274 void DtPrtProps::HelpCB(void *data)
275 {
276    DtPrtProps *obj = (DtPrtProps *) data;
277    obj->HandleHelpRequest();
278 }
279
280 boolean DtPrtProps::HandleHelpRequest()
281 {
282    mainw->DisplayHelp((char *)PROPS_PRINTER_ID);
283    return true;
284 }
285
286 void DtPrtProps::SetActionIcons(const char *icon)
287 {
288    delete _iconFile;
289    _iconFile = STRDUP(icon);
290    largeIcon->IconFile((char*)icon);
291    mediumIcon->IconFile((char*)icon);
292    smallIcon->IconFile((char*)icon);
293 }
294
295 void DtPrtProps::CallerCB(BaseUI *caller, char *iconFile)
296 {
297    DtPrtProps *obj = (DtPrtProps *)caller;
298    delete obj->_iconFile;
299    obj->_iconFile = STRDUP(iconFile);
300    obj->smallIcon->IconFile(iconFile);
301    obj->mediumIcon->IconFile(iconFile);
302    obj->largeIcon->IconFile(iconFile);
303 }
304
305 void DtPrtProps::FindSetCB(void *data)
306 {
307    DtPrtProps *obj = (DtPrtProps *)data;
308    if (!obj->mainw->findSetD)
309     {
310       char *name = new char [strlen(obj->mainw->Name()) +
311                              strlen(MESSAGE(FindSetTitleL))];
312       sprintf(name, MESSAGE(FindSetTitleL), obj->mainw->Name());
313       obj->mainw->findSetD = new DtFindSet(obj->mainw, name,
314                                            &DtPrtProps::CallerCB);
315       delete [] name;
316     }
317    obj->mainw->findSetD->Caller(obj);
318    obj->mainw->findSetD->Visible(true);
319 }