1 /* $XConsortium: AlternatesListUiItem.C /main/3 1995/11/06 16:03:45 rswiston $ */
5 * RESTRICTED CONFIDENTIAL INFORMATION:
7 * The information in this document is subject to special
8 * restrictions in a confidential disclosure agreement bertween
9 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
10 * document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
11 * Sun's specific written approval. This documment and all copies
12 * and derivative works thereof must be returned or destroyed at
15 * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
21 #include <DtMail/options_util.h>
22 #include "options_ui.h"
23 #include <DtMail/PropUi.hh>
24 #include <DtMail/ListUiItem.hh>
25 #include <DtMail/AlternatesListUiItem.hh>
27 extern Boolean props_changed;
28 void handleAlternateSelection(Widget w, XtPointer clientdata, XtPointer calldata);
30 // AlternatesListUiItem::AlternatesListUiItem
31 // AlternatesListUiItem ctor
32 ////////////////////////////////////////////////////////////////////
34 AlternatesListUiItem::AlternatesListUiItem(Widget w,
37 Widget text_entry_widget):ListUiItem(w, source, search_key, NULL)
39 source = source; search_key = search_key;
41 entry_field_widget = text_entry_widget;
48 XmNautomaticSelection, True,
49 XmNselectionPolicy, XmBROWSE_SELECT,
53 XmNbrowseSelectionCallback,
54 (XtCallbackProc)handleAlternateSelection,
58 //---------------------------------------------------------------
59 void handleAlternateSelection(Widget w, XtPointer clientdata, XtPointer calldata)
61 AlternatesListUiItem *item;
62 XmListCallbackStruct *list_info = (XmListCallbackStruct *)calldata;
63 char *selection_string = NULL;
64 DtVirtArray<PropStringPair *> *list_items;
65 clientdata = clientdata;
71 list_items = item->getItemList();
73 if(list_items != NULL)
75 // motif index is 1 based
77 PropStringPair *pair = (*list_items)[list_info->item_position - 1];
81 XtVaSetValues(item->getEntryFieldWidget(),
91 // AlternatesListUiItem::writeFromUiToSource()
92 // Takes values in the UI and puts them into the source DB
93 ///////////////////////////////////////////////////////////////////
94 void AlternatesListUiItem::writeFromUiToSource()
96 Widget w = this->getWidget();
98 DtMail::Session * d_session = theRoamApp.session()->session();
99 DtMail::MailRc * mail_rc = d_session->mailRc(error);
102 if(deleted_items != NULL)
104 num_items = deleted_items->length();
106 for(i = 0; i < num_items; i++)
108 mail_rc->removeAlternate(error,(*deleted_items)[i]);
112 if(list_items != NULL)
114 num_items = list_items->length();
115 for(i = 0; i < num_items; i++)
117 mail_rc->setAlternate(error,(*list_items)[i]->label);
121 if(deleted_items != NULL)
123 delete deleted_items;
124 deleted_items = NULL;
128 // AlternatesListUiItem::writeFromSourceToUi()
129 // Takes values in the UI and puts them into the source DB
130 ///////////////////////////////////////////////////////////////////
131 void AlternatesListUiItem::writeFromSourceToUi()
134 DtMail::Session * d_session = theRoamApp.session()->session();
135 DtMail::MailRc * mail_rc = d_session->mailRc(error);
136 Widget w = this->getWidget();
137 const char *list_str = NULL;
138 DtVirtArray<char *> *char_list = NULL;
139 PropStringPair *new_pair;
140 char *token, *buf = NULL;
142 XmListDeleteAllItems(w);
144 if(deleted_items != NULL)
146 delete deleted_items;
147 deleted_items = NULL;
150 if(list_items != NULL)
156 if ((list_str = mail_rc->getAlternates(error)) == NULL) {
157 list_str = strdup("");
160 if ((buf = (char *) malloc(strlen(list_str) + 1)) == NULL)
162 strcpy(buf, (char *)list_str);
164 if((token = (char *) strtok(buf, " ")))
166 list_items = new DtVirtArray<PropStringPair *>(10);
167 char_list = new DtVirtArray<char *>(10);
169 new_pair = new PropStringPair;
170 new_pair->label = strdup(token);
171 new_pair->value = NULL;
172 list_items->append(new_pair);
174 char_list->append(strdup(token));
176 while(token = (char *)strtok(NULL, " "))
178 new_pair = new PropStringPair;
179 new_pair->label = strdup(token);
180 new_pair->value = NULL;
181 list_items->append(new_pair);
183 char_list->append(strdup(token));
187 options_list_init(w, char_list);
192 free((void *)list_str);
199 ///////////////////////////////////////////////////////////
200 void AlternatesListUiItem::handleAddButtonPress()
202 Widget entry_field = this->getEntryFieldWidget();
203 char *test_str = NULL;
204 PropStringPair *new_pair = NULL;
206 XtVaGetValues(entry_field,
211 if(strlen(test_str) > 1)
213 new_pair = new PropStringPair;
214 int *pos_list, num_pos;
216 new_pair->label = strdup(test_str);
217 new_pair->value = NULL;
219 if(XmListGetSelectedPos(this->getWidget(),
223 if(list_items == NULL)
224 list_items = new DtVirtArray<PropStringPair *>(10);
226 list_items->insert(new_pair,pos_list[0] - 1);
227 XmListAddItem(this->getWidget(),
228 XmStringCreateLocalized(new_pair->label),
231 XmListSelectPos(this->getWidget(),
235 XmListSetPos(this->getWidget(),
240 if(list_items == NULL)
241 list_items = new DtVirtArray<PropStringPair *>(10);
243 list_items->insert(new_pair,0);
245 XmListAddItem(this->getWidget(),
246 XmStringCreateLocalized(new_pair->label),
248 XmListSelectPos(this->getWidget(),
251 XmListSetPos(this->getWidget(),
254 props_changed = TRUE;
257 ///////////////////////////////////////////////////////////
258 void AlternatesListUiItem::handleChangeButtonPress()
260 Widget entry_field = this->getEntryFieldWidget();
261 char *test_str = NULL;
262 PropStringPair *new_pair = NULL;
263 XmString replace_string;
264 int *pos_list, num_pos;
266 // if nothing selected nothing to change...
267 if(XmListGetSelectedPos(this->getWidget(),
271 XtVaGetValues(entry_field,
276 if(strlen(test_str) > 1)
279 new_pair = (*list_items)[pos_list[0] - 1];
281 if(deleted_items == NULL)
283 deleted_items = new DtVirtArray<char *>(10);
286 deleted_items->append(strdup((*list_items)[pos_list[0] -1]->label));
288 free(new_pair->label);
289 new_pair->label = strdup(test_str);
291 replace_string = XmStringCreateLocalized(new_pair->label);
293 XmListReplaceItemsPos(this->getWidget(),
298 XmListSelectPos(this->getWidget(),
302 props_changed = TRUE;
305 ///////////////////////////////////////////////////////////
306 void AlternatesListUiItem::handleDeleteButtonPress()
308 Widget list_widget = this->getWidget();
309 int *p_list, p_count;
311 // get the selected position
312 if(XmListGetSelectedPos(list_widget,
317 if(deleted_items == NULL)
319 deleted_items = new DtVirtArray<char *>(10);
322 deleted_items->append(strdup((*list_items)[p_list[0] -1]->label));
324 // delete the item from our list
325 this->list_items->remove(p_list[0] - 1); // remove only first
327 // delete the item from the widget
328 XmListDeletePos(list_widget, p_list[0]);
330 XtVaSetValues(this->getEntryFieldWidget(),
333 XmListSelectPos(list_widget,
337 props_changed = TRUE;