2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /* $TOG: TemplateListUiItem.C /main/6 1997/09/03 17:40:26 mgreess $ */
27 * $TOG: TemplateListUiItem.C /main/6 1997/09/03 17:40:26 mgreess $
29 * RESTRICTED CONFIDENTIAL INFORMATION:
31 * The information in this document is subject to special
32 * restrictions in a confidential disclosure agreement bertween
33 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
34 * document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
35 * Sun's specific written approval. This documment and all copies
36 * and derivative works thereof must be returned or destroyed at
39 * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
46 #include <DtMail/options_util.h>
47 #include "options_ui.h"
48 #include <DtMail/PropUi.hh>
49 #include <DtMail/ListUiItem.hh>
50 #include <DtMail/TemplateListUiItem.hh>
52 extern Boolean props_changed;
53 void handleTmplSelection(Widget w, XtPointer clientdata, XtPointer calldata);
54 // TemplateListUiItem::TemplateListUiItem
55 // TemplateListUiItem ctor
56 ////////////////////////////////////////////////////////////////////
58 TemplateListUiItem::TemplateListUiItem(Widget w,
61 Widget key_entry_widget,
62 Widget value_entry_widget):ListUiItem(w, source, search_key, NULL)
64 source = source; search_key = search_key;
66 key_widget = key_entry_widget;
67 value_widget = value_entry_widget;
74 XmNautomaticSelection, True,
75 XmNselectionPolicy, XmBROWSE_SELECT,
79 XmNbrowseSelectionCallback,
80 (XtCallbackProc)handleTmplSelection,
86 //-----------------======================-----------------
87 void handleTmplSelection(Widget w, XtPointer, XtPointer calldata)
89 TemplateListUiItem *item;
90 XmListCallbackStruct *list_info = (XmListCallbackStruct *)calldata;
91 char *selection_string = NULL;
92 DtVirtArray<PropStringPair *> *list_items;
99 list_items = item->getItemList();
101 if(list_items != NULL)
102 { // motif index is 1 based
103 //virtarry is 0 based
104 PropStringPair *pair = (*list_items)[list_info->item_position - 1];
108 XtVaSetValues(item->getKeyWidget(),
109 XmNvalue,pair->label,
112 XtVaSetValues(item->getValueWidget(),
113 XmNvalue,pair->value,
119 // TemplateListUiItem::writeFromUiToSource()
120 // Takes values in the UI and puts them into the source DB
121 ///////////////////////////////////////////////////////////////////
122 void TemplateListUiItem::writeFromUiToSource()
124 Widget w = this->getWidget();
126 DtMail::Session * d_session = theRoamApp.session()->session();
127 DtMail::MailRc * mail_rc = d_session->mailRc(error);
132 if(list_items != NULL) {
133 if(list_items->length() > 0)
135 num_items = list_items->length();
137 // calc mailrc strlen...
138 total_len = 1; // add space for the terminator...
139 for(i = 0; i < num_items; i++) {
142 for (ptr = (*list_items)[i]->label; ptr && *ptr; ptr++)
143 if (*ptr == ' ') num_blanks++;
144 // strlen(label) + strlen(value) + space + colon +
145 // potential \'s that precede the blank character
146 if((*list_items)[i]->label != NULL)
147 total_len = total_len +
148 strlen((*list_items)[i]->label) + (strlen("\\") * num_blanks) + 2;
150 if((*list_items)[i]->value != NULL)
151 total_len = total_len + strlen((*list_items)[i]->value);
154 cf_str = (char *)malloc(total_len);
157 for(i = 0; i < num_items; i++) {
158 if((*list_items)[i]->label != NULL) {
159 if (strchr((char*)(*list_items)[i]->label, ' ')) {
160 char *token, *tmpbuf;
161 tmpbuf = strdup((*list_items)[i]->label);
162 if (token = (char *)strtok(tmpbuf, " ")) {
163 strcat(cf_str, token);
164 while(token = (char *)strtok(NULL, " ")) {
165 strcat(cf_str, "\\ ");
166 strcat(cf_str, token);
172 strcat(cf_str, (*list_items)[i]->label);
175 if((*list_items)[i]->value != NULL)
176 strcat(cf_str, (*list_items)[i]->value);
180 mail_rc->setValue(error, "templates", cf_str);
183 mail_rc->removeValue(error, "templates");
186 mail_rc->removeValue(error, "templates");
189 // TemplateListUiItem::writeFromSourceToUi()
190 // Takes values in the UI and puts them into the source DB
191 ///////////////////////////////////////////////////////////////////
192 void TemplateListUiItem::writeFromSourceToUi()
195 DtMail::Session * d_session = theRoamApp.session()->session();
196 DtMail::MailRc * mail_rc = d_session->mailRc(error);
197 Widget w = this->getWidget();
198 const char *list_str = NULL;
199 DtVirtArray<char *> list_str_list(10);
202 const char *value = NULL;
204 XmListDeleteAllItems(w);
206 mail_rc->getValue(error, "templates", &value);
208 if(list_items != NULL)
211 char * expanded_list = d_session->expandPath(error, value);
213 list_items = new DtVirtArray<PropStringPair *>(10);
215 parsePropString(expanded_list, *list_items);
218 list_len = list_items->length();
219 for(i = 0; i < list_len; i++)
220 list_str_list.append(formatPropPair((*list_items)[i]->label,
221 (*list_items)[i]->value));
223 options_list_init(w, &list_str_list);
229 ///////////////////////////////////////////////////////////
230 void TemplateListUiItem::handleAddButtonPress()
232 char *key_str = NULL;
233 char *value_str = NULL;
234 PropStringPair *new_pair = NULL;
236 XtVaGetValues(key_widget,
240 XtVaGetValues(value_widget,
241 XmNvalue, &value_str,
245 if(strlen(key_str) > 0)
247 new_pair = new PropStringPair;
248 int *pos_list, num_pos;
250 new_pair->label = strdup(key_str);
252 if(value_str != NULL)
253 new_pair->value = strdup(value_str);
255 new_pair->value = NULL;
257 if(XmListGetSelectedPos(this->getWidget(),
261 if(list_items == NULL)
262 list_items = new DtVirtArray<PropStringPair *>(10);
264 list_items->insert(new_pair,pos_list[0] - 1);
266 XmListAddItem(this->getWidget(),
267 XmStringCreateLocalized(
273 XmListSelectPos(this->getWidget(),
277 XmListSetPos(this->getWidget(),
282 if(list_items == NULL)
283 list_items = new DtVirtArray<PropStringPair *>(10);
285 list_items->insert(new_pair,0);
286 XmListAddItem(this->getWidget(),
287 XmStringCreateLocalized(
292 XmListSelectPos(this->getWidget(),
296 XmListSetPos(this->getWidget(),
299 props_changed = TRUE;
302 ///////////////////////////////////////////////////////////
303 void TemplateListUiItem::handleChangeButtonPress()
305 char *key_str = NULL;
306 char *value_str = NULL;
307 PropStringPair *new_pair = NULL;
308 XmString replace_string;
309 int *pos_list, num_pos;
311 // if nothing selected nothing to change...
312 if(XmListGetSelectedPos(this->getWidget(),
316 XtVaGetValues(key_widget,
320 XtVaGetValues(value_widget,
321 XmNvalue, &value_str,
325 if(strlen(key_str) > 0)
328 new_pair = (*list_items)[pos_list[0] - 1];
330 free(new_pair->label);
331 new_pair->label = strdup(key_str);
333 if(new_pair->value != NULL)
335 free(new_pair->value);
336 if(value_str != NULL)
337 new_pair->value = strdup(value_str);
339 new_pair->value = NULL;
342 replace_string = XmStringCreateLocalized(
343 formatPropPair(new_pair->label,
346 XmListReplaceItemsPos(this->getWidget(),
351 XmListSelectPos(this->getWidget(),
355 props_changed = TRUE;
358 ///////////////////////////////////////////////////////////
359 void TemplateListUiItem::handleDeleteButtonPress()
361 Widget list_widget = this->getWidget();
362 int *p_list, p_count;
364 // get the selected position
365 if(XmListGetSelectedPos(list_widget,
370 // delete the item from our list
371 this->list_items->remove(p_list[0] - 1); // remove only first
373 // delete the item from the widget
374 XmListDeletePos(list_widget, p_list[0]);
376 XtVaSetValues(this->getKeyWidget(),
380 XtVaSetValues(this->getValueWidget(),
384 XmListSelectPos(list_widget,
388 props_changed = TRUE;