-fpermissive to allow GCC to compile old C++
[oweals/cde.git] / cde / programs / dtmail / dtmail / Icon.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  *+SNOTICE
25  *
26  *      $TOG: Icon.C /main/8 1998/10/27 19:42:30 mgreess $
27  *
28  *      RESTRICTED CONFIDENTIAL INFORMATION:
29  *      
30  *      The information in this document is subject to special
31  *      restrictions in a confidential disclosure agreement between
32  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
33  *      document outside HP, IBM, Sun, USL, SCO, or Univel without
34  *      Sun's specific written approval.  This document and all copies
35  *      and derivative works thereof must be returned or destroyed at
36  *      Sun's request.
37  *
38  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
39  *
40  *+ENOTICE
41  */
42
43 #ifdef DEBUG_CB_REASON
44 #define PRINTCB(str) printf("%s\n",str)
45 #else
46 #define PRINTCB(str)    
47 #endif
48
49 #include <Dt/Dnd.h>
50 #include "Icon.h"
51 #include "RoamApp.h"
52 #include "DtMailEditor.hh"
53 #include "Editor.hh"
54 #include <Dt/Editor.h>
55
56 #include <X11/keysym.h>
57 #include <X11/StringDefs.h>
58 #include <X11/Intrinsic.h>
59
60 #include <X11/Xlib.h>
61 #include <X11/Xutil.h>
62 #include <Xm/ColorObjP.h>
63
64 Icon::Icon ( 
65     Attachment *classparent, 
66     char *name, 
67     XmString label, /* label string */
68     unsigned short /* type */,
69     Widget parent,
70     int  indx
71 ) : UIComponent (name)
72 {
73 #define MAX_NUM_COLORS  8
74     DtMail::BodyPart    *bp;
75     DtMailEnv mail_error;
76     char *type = NULL, *icon_name = NULL; 
77     char *icon_filename = NULL, *host_prefix = NULL;
78     int n = 0;
79     Arg args[20];
80     int btn1_transfer = 0;
81     int         colorUse;
82     short       act, inact, prim, second, text;
83     XmPixelSet  pixels[XmCO_NUM_COLORS];
84
85     _parent = classparent; 
86     _indx = indx;
87     _is_selected = FALSE;
88     _is_armed = FALSE;
89
90     // Get the pixmap image file name for this attachment.
91     bp = _parent->getBodyPart();
92     bp->getContents(mail_error, NULL, NULL, &type, NULL, NULL, NULL);
93
94     // Retrieve the host name.
95     host_prefix = DtDtsDataTypeToAttributeValue(type,
96                                                 DtDTS_DA_DATA_HOST,
97                                                 NULL);
98
99     // Get the name of the icon.
100     icon_name = (char *) DtDtsDataTypeToAttributeValue(type,
101                                                     DtDTS_DA_ICON,
102                                                     NULL);
103     // Retrieve icon file name
104     icon_filename = XmGetIconFileName(XtScreen(parent),
105                                         NULL,
106                                         icon_name,
107                                         host_prefix,
108                                         DtMEDIUM);
109
110     // Get pixel data.
111     XmeGetColorObjData(XtScreen(parent), &colorUse, pixels, XmCO_NUM_COLORS,
112                        &act, &inact, &prim, &second, &text);
113     _cur_fg = pixels[text].fg;
114     _cur_bg = pixels[text].sc;
115
116     n = 0;
117     XtSetArg (args[n], XmNshadowThickness, 0);          n++;
118     XtSetArg (args[n], XmNfillOnArm, FALSE);             n++;
119     XtSetArg (args[n], XmNhighlightThickness, 2);       n++;
120     XtSetArg (args[n], XmNimageName, icon_filename);    n++;
121     XtSetArg (args[n], XmNstring, label);               n++;
122     XtSetArg( args[n], XmNalignment, XmALIGNMENT_CENTER); n++;
123     XtSetArg( args[n], XmNpixmapPosition, XmPIXMAP_TOP); n++;
124     XtSetArg( args[n], XmNbehavior, XmICON_DRAG); n++;
125     XtSetArg( args[n], XmNfillMode, XmFILL_PARENT); n++;
126     XtSetArg( args[n], XmNbackground, _cur_bg); n++;
127     XtSetArg( args[n], XmNforeground, _cur_fg); n++;
128     
129     _w = _DtCreateIcon(parent,"iconGadget", args,n);
130
131     XtAddEventHandler(XtParent(_w), Button1MotionMask, FALSE,
132                 (XtEventHandler)&Icon::dragMotionHandler,
133                 (XtPointer)this);
134     
135     XtVaGetValues((Widget)XmGetXmDisplay(XtDisplay(XtParent(_w))),
136             "enableBtn1Transfer", &btn1_transfer,
137             NULL);
138
139     if (btn1_transfer != True) {
140         XtAddEventHandler(XtParent(_w), Button2MotionMask, FALSE,
141                 (XtEventHandler)&Icon::dragMotionHandler,
142                 (XtPointer)this);
143     }
144
145     XtAddCallback(
146         _w, 
147         XmNcallback, 
148         (XtCallbackProc)&Icon::iconCallback,
149         (XtPointer)this
150     );
151
152     if (type) {
153         free(type);
154         type = NULL;
155     }
156     
157     if (host_prefix) {
158         DtDtsFreeAttributeValue(host_prefix);
159         host_prefix = NULL;
160     }
161     if (icon_name) {
162         DtDtsFreeAttributeValue(icon_name);
163         icon_name = NULL;
164     }
165     if (icon_filename) {
166         XtFree(icon_filename);
167         icon_filename = NULL;
168     }
169 }
170
171 Icon::~Icon() 
172 {
173     int btn1_transfer = 0;
174
175     XtRemoveCallback(
176         _w, 
177         XmNcallback,
178         (XtCallbackProc)&Icon::iconCallback,
179         (XtPointer)this
180     );
181     XtRemoveEventHandler(XtParent(_w), Button1MotionMask, FALSE,
182                 (XtEventHandler)&Icon::dragMotionHandler,
183                 (XtPointer)this);
184     
185     XtVaGetValues((Widget)XmGetXmDisplay(XtDisplay(XtParent(_w))),
186             "enableBtn1Transfer", &btn1_transfer,
187             NULL);
188
189     if (btn1_transfer != True) {
190         XtRemoveEventHandler(XtParent(_w), Button2MotionMask, FALSE,
191                 (XtEventHandler)&Icon::dragMotionHandler,
192                 (XtPointer)this);
193     }
194
195 }
196
197 void
198 Icon::iconCallback(Widget , XtPointer client_data, XtPointer call_data)
199 {
200         DtIconCallbackStruct *cb = (DtIconCallbackStruct *)call_data;
201         Icon *obj = (Icon *)client_data;
202         XEvent *event;
203
204         if ((event = cb->event) != (XEvent *) NULL) {
205             /*
206              * The following seems rather bogus....   to make assumptions
207              * about the context of a ButtonPress before decoding
208              * the reason for the callback.
209              */
210             if ((event->type == ButtonPress) &&
211                 (! IsModifierKey(XLookupKeysym((XKeyEvent *) event, 0)))) {
212                 if (event->xbutton.state & ControlMask)
213                     {
214                         /* Cntrl Button press processing*/
215                         // Need to handle it.  Postponed...
216                         return;
217                     }
218             }
219         }
220
221         switch(cb->reason) {
222                 case XmCR_UNHIGHLIGHT:
223                         PRINTCB("unhighlight");
224                         break;
225                 case XmCR_HIGHLIGHT:
226                         PRINTCB("highlight");
227                         break;
228                 case XmCR_ACTIVATE:
229                         PRINTCB("activate");
230                         break;
231                 case XmCR_DISARM:
232                         obj->disarm();
233                         PRINTCB("disarm");
234                         break;
235                 case XmCR_ARM:
236                         obj->arm();
237                         PRINTCB("arm");
238                         break;
239                 case XmCR_SELECT:
240                         PRINTCB("select");
241                         obj->select();
242                         break;
243                 case XmCR_DEFAULT_ACTION:
244                         PRINTCB("default_action");
245                         obj->defaultAction();
246                         break;
247                 case XmCR_DRAG:
248                         PRINTCB("drag");
249                         break;
250                 default:
251                         break;
252         }       
253 }
254
255 void
256 Icon::dragMotionHandler(
257     Widget      widget,
258     XtPointer   clientData,
259     XEvent      *event)
260 {
261     Icon        *icon = (Icon *) clientData;
262
263     icon->parent()->parent()->owner()->attachDragMotionHandler(widget, event);
264 }
265
266 void
267 Icon::invert()
268 {
269         Arg args[2];
270         int n;
271         Pixel swap;
272
273         n = 0;
274         XtSetArg( args[n], XmNbackground, _cur_fg); n++;
275         XtSetArg( args[n], XmNforeground, _cur_bg); n++;
276         XtSetValues(_w, args, n);
277         
278         swap = _cur_fg;
279         _cur_fg = _cur_bg;
280         _cur_bg = swap;
281 }
282
283
284 void
285 Icon::arm()
286 {
287         // deselect others in all cases
288         _parent->set_selected();
289
290         if(_is_armed == TRUE)
291                 return;
292
293         _is_selected = TRUE;
294
295         invert();
296
297         _is_armed = TRUE;
298 }
299         
300 void
301 Icon::disarm()
302 {
303         if(_is_armed == FALSE || _is_selected == TRUE )
304                 return;
305
306         invert();
307
308         _is_armed = FALSE;
309 }
310         
311 void
312 Icon::unselect()
313 {
314         if(_is_selected == FALSE)
315                 return;
316
317         _is_selected = FALSE;
318
319         disarm();
320 }
321
322
323 void
324 Icon::primitiveSelect()
325 {
326         if(_is_selected == TRUE || _is_armed == TRUE)
327                 return;
328
329         _is_selected = TRUE;
330
331         invert();
332
333         _is_armed = TRUE;
334 }
335
336 void
337 Icon::select()
338 {
339         if(_is_selected == TRUE)
340                 return;
341
342         arm();
343 }
344
345 void
346 Icon::defaultAction()
347 {
348         select();
349         _parent->handleDoubleClick();
350 }