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