Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtappbuilder / src / ab / brws_edit.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 /*
25  *      $XConsortium: brws_edit.c /main/3 1995/11/06 17:20:54 rswiston $
26  *
27  *      @(#)brws_edit.c 1.2 28 Jan 1994 
28  *
29  *      RESTRICTED CONFIDENTIAL INFORMATION:
30  *      
31  *      The information in this document is subject to special
32  *      restrictions in a confidential disclosure agreement between
33  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
34  *      document outside HP, IBM, Sun, USL, SCO, or Univel without
35  *      Sun's specific written approval.  This document and all copies
36  *      and derivative works thereof must be returned or destroyed at
37  *      Sun's request.
38  *
39  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
40  *
41  */
42
43
44
45 #include <sys/param.h>
46 #include <sys/types.h>
47
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <X11/Xlib.h>
51 #include <Xm/Xm.h>
52 #include <Xm/TextF.h>
53 #include <ab_private/obj.h>
54 #include <ab_private/trav.h>
55 #include <ab_private/istr.h>
56 #include <ab/util_types.h>
57 #include <ab_private/abobj.h>
58 #include <ab_private/abobj_set.h>
59 #include <ab_private/proj.h>
60 #include <ab_private/brwsP.h>
61 #include <ab_private/objxm.h>
62
63 static void     browser_textf_activate(
64                     Widget widget, 
65                     XtPointer client_data,
66                     XtPointer call_data
67                 );
68
69 static void
70 browser_textf_activate(
71     Widget widget, 
72     XtPointer client_data,
73     XtPointer call_data
74 )
75 {
76     ABObj       textf_obj = NULL;
77     VNode       textf_node = NULL;
78     char        *newName = NULL;
79
80     XtVaGetValues(widget, XmNuserData, &textf_node, NULL); 
81
82     if (!textf_node)
83         return;
84
85     textf_obj = (AB_OBJ *)textf_node->obj_data;
86
87     newName = XmTextFieldGetString(widget);
88
89     if (!newName)
90         return;
91
92     XtUnmanageChild(widget);
93     XtVaSetValues(widget, XmNuserData, NULL, NULL); 
94
95     if (!util_streq(obj_get_name(textf_obj), newName))
96     {
97         abobj_set_name(textf_obj, newName);
98         proj_update_node(textf_obj);
99         brws_update_node(textf_obj);
100     }
101
102     XtFree(newName);
103 }
104
105 void
106 brwsP_create_textf(
107     Vwr         v
108 )
109 {
110     BrowserUiObjects    *ui;
111
112     if (!v)
113         return;
114
115     ui = aob_ui_from_browser(v);
116
117     if (!ui)
118         return;
119
120     if (!ui->textf)
121     {
122         XmFontListEntry         font_list_entry;
123         XmFontList              font_list;
124         Widget                  draw_area;
125
126         draw_area = brws_draw_area(v);
127
128         if (!draw_area)
129             return;
130
131         font_list_entry = XmFontListEntryCreate(
132                                 XmFONTLIST_DEFAULT_TAG,
133                                 XmFONT_IS_FONT,
134                                 ui->sm_font);
135
136         font_list = XmFontListAppendEntry(NULL, font_list_entry);
137
138         ui->textf = XtVaCreateWidget("change_name",
139                         xmTextFieldWidgetClass, 
140                         draw_area,
141                         XmNfontList,            font_list,
142                         XmNshadowThickness,     0,
143                         XmNhighlightThickness,  0,
144                         XmNmarginWidth,         BRWS_ELM_BBOX_MARGIN,
145                         XmNmarginHeight,        BRWS_ELM_BBOX_MARGIN,
146                         XmNmaxLength,           80,
147                         XmNbackground,          
148                             BlackPixelOfScreen(XtScreen(ui->shell)),
149                         XmNforeground,          
150                             WhitePixelOfScreen(XtScreen(ui->shell)),
151                         NULL);
152                 
153         XtAddCallback(ui->textf, XmNactivateCallback,
154                 (XtCallbackProc)browser_textf_activate,
155                 (XtPointer)NULL);
156     }
157 }
158
159 void
160 brwsP_destroy_textf(
161     Vwr         v
162 )
163 {
164     BrowserUiObjects    *ui;
165
166     if (!v)
167         return;
168
169     ui = aob_ui_from_browser(v);
170
171     if (!ui)
172         return;
173
174     if (ui->textf)
175     {
176         XtDestroyWidget(ui->textf);
177         ui->textf = NULL;
178     }
179 }
180
181 void
182 brwsP_hide_textf(
183     Vwr         v
184 )
185 {
186     BrowserUiObjects    *ui;
187
188     if (!v)
189         return;
190
191     ui = aob_ui_from_browser(v);
192
193     if (!ui)
194         return;
195
196     if (ui->textf && XtIsManaged(ui->textf))
197         XtUnmanageChild(ui->textf);
198 }
199
200 /*
201  * brwsP_show_textf()
202  * Show the edit in place textfield on the node element specified
203  * by 'elm_pos'.
204  * Also, store the vnode as XmNuserData on the textfield.
205  */
206 void
207 brwsP_show_textf(
208     VNode       vnode,
209     int         elm_pos
210 )
211 {
212     Vwr                 v;
213     BrowserUiObj        ui;
214
215     if (!vnode || (elm_pos < 0) || 
216         !vnode->num_elements || 
217         (elm_pos >= vnode->num_elements))
218         return;
219
220     if (!(v = vnode->browser))
221         return;
222
223     if (!(ui = aob_ui_from_browser(v)))
224         return;
225
226     if (ui->textf)
227     {
228         VNodeElm        elm = vnode->elements;
229
230         if (!elm)
231             return;
232
233         XtVaSetValues(ui->textf,
234             XmNvalue,   (char *)elm[elm_pos].data,
235             XmNx,       vnode->x + 
236                         BRWS_ELM_BORDER_WIDTH,
237             XmNy,       elm[elm_pos].y - 
238                         BRWS_ELM_BBOX_MARGIN,
239             XmNwidth,   vnode->width - 
240                         (2 * BRWS_ELM_BORDER_WIDTH),
241             XmNheight,  elm[elm_pos].height +
242                         (2 * BRWS_ELM_BBOX_MARGIN),
243             XmNuserData,vnode,
244             NULL);
245
246         XtManageChild(ui->textf);
247     }
248 }