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 /* $XConsortium: AlternatesListUiItem.C /main/3 1995/11/06 16:03:45 rswiston $ */
27 * RESTRICTED CONFIDENTIAL INFORMATION:
29 * The information in this document is subject to special
30 * restrictions in a confidential disclosure agreement bertween
31 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
32 * document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
33 * Sun's specific written approval. This documment and all copies
34 * and derivative works thereof must be returned or destroyed at
37 * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
43 #include <DtMail/options_util.h>
44 #include "options_ui.h"
45 #include <DtMail/PropUi.hh>
46 #include <DtMail/ListUiItem.hh>
47 #include <DtMail/AlternatesListUiItem.hh>
49 extern Boolean props_changed;
50 void handleAlternateSelection(Widget w, XtPointer clientdata, XtPointer calldata);
52 // AlternatesListUiItem::AlternatesListUiItem
53 // AlternatesListUiItem ctor
54 ////////////////////////////////////////////////////////////////////
56 AlternatesListUiItem::AlternatesListUiItem(Widget w,
59 Widget text_entry_widget):ListUiItem(w, source, search_key, NULL)
61 source = source; search_key = search_key;
63 entry_field_widget = text_entry_widget;
70 XmNautomaticSelection, True,
71 XmNselectionPolicy, XmBROWSE_SELECT,
75 XmNbrowseSelectionCallback,
76 (XtCallbackProc)handleAlternateSelection,
80 //---------------------------------------------------------------
81 void handleAlternateSelection(Widget w, XtPointer clientdata, XtPointer calldata)
83 AlternatesListUiItem *item;
84 XmListCallbackStruct *list_info = (XmListCallbackStruct *)calldata;
85 char *selection_string = NULL;
86 DtVirtArray<PropStringPair *> *list_items;
87 clientdata = clientdata;
93 list_items = item->getItemList();
95 if(list_items != NULL)
97 // motif index is 1 based
99 PropStringPair *pair = (*list_items)[list_info->item_position - 1];
103 XtVaSetValues(item->getEntryFieldWidget(),
104 XmNvalue,pair->label,
113 // AlternatesListUiItem::writeFromUiToSource()
114 // Takes values in the UI and puts them into the source DB
115 ///////////////////////////////////////////////////////////////////
116 void AlternatesListUiItem::writeFromUiToSource()
118 Widget w = this->getWidget();
120 DtMail::Session * d_session = theRoamApp.session()->session();
121 DtMail::MailRc * mail_rc = d_session->mailRc(error);
124 if(deleted_items != NULL)
126 num_items = deleted_items->length();
128 for(i = 0; i < num_items; i++)
130 mail_rc->removeAlternate(error,(*deleted_items)[i]);
134 if(list_items != NULL)
136 num_items = list_items->length();
137 for(i = 0; i < num_items; i++)
139 mail_rc->setAlternate(error,(*list_items)[i]->label);
143 if(deleted_items != NULL)
145 delete deleted_items;
146 deleted_items = NULL;
150 // AlternatesListUiItem::writeFromSourceToUi()
151 // Takes values in the UI and puts them into the source DB
152 ///////////////////////////////////////////////////////////////////
153 void AlternatesListUiItem::writeFromSourceToUi()
156 DtMail::Session * d_session = theRoamApp.session()->session();
157 DtMail::MailRc * mail_rc = d_session->mailRc(error);
158 Widget w = this->getWidget();
159 const char *list_str = NULL;
160 DtVirtArray<char *> *char_list = NULL;
161 PropStringPair *new_pair;
162 char *token, *buf = NULL;
164 XmListDeleteAllItems(w);
166 if(deleted_items != NULL)
168 delete deleted_items;
169 deleted_items = NULL;
172 if(list_items != NULL)
178 if ((list_str = mail_rc->getAlternates(error)) == NULL) {
179 list_str = strdup("");
182 if ((buf = (char *) malloc(strlen(list_str) + 1)) == NULL)
184 strcpy(buf, (char *)list_str);
186 if((token = (char *) strtok(buf, " ")))
188 list_items = new DtVirtArray<PropStringPair *>(10);
189 char_list = new DtVirtArray<char *>(10);
191 new_pair = new PropStringPair;
192 new_pair->label = strdup(token);
193 new_pair->value = NULL;
194 list_items->append(new_pair);
196 char_list->append(strdup(token));
198 while(token = (char *)strtok(NULL, " "))
200 new_pair = new PropStringPair;
201 new_pair->label = strdup(token);
202 new_pair->value = NULL;
203 list_items->append(new_pair);
205 char_list->append(strdup(token));
209 options_list_init(w, char_list);
214 free((void *)list_str);
221 ///////////////////////////////////////////////////////////
222 void AlternatesListUiItem::handleAddButtonPress()
224 Widget entry_field = this->getEntryFieldWidget();
225 char *test_str = NULL;
226 PropStringPair *new_pair = NULL;
228 XtVaGetValues(entry_field,
233 if(strlen(test_str) > 1)
235 new_pair = new PropStringPair;
236 int *pos_list, num_pos;
238 new_pair->label = strdup(test_str);
239 new_pair->value = NULL;
241 if(XmListGetSelectedPos(this->getWidget(),
245 if(list_items == NULL)
246 list_items = new DtVirtArray<PropStringPair *>(10);
248 list_items->insert(new_pair,pos_list[0] - 1);
249 XmListAddItem(this->getWidget(),
250 XmStringCreateLocalized(new_pair->label),
253 XmListSelectPos(this->getWidget(),
257 XmListSetPos(this->getWidget(),
262 if(list_items == NULL)
263 list_items = new DtVirtArray<PropStringPair *>(10);
265 list_items->insert(new_pair,0);
267 XmListAddItem(this->getWidget(),
268 XmStringCreateLocalized(new_pair->label),
270 XmListSelectPos(this->getWidget(),
273 XmListSetPos(this->getWidget(),
276 props_changed = TRUE;
279 ///////////////////////////////////////////////////////////
280 void AlternatesListUiItem::handleChangeButtonPress()
282 Widget entry_field = this->getEntryFieldWidget();
283 char *test_str = NULL;
284 PropStringPair *new_pair = NULL;
285 XmString replace_string;
286 int *pos_list, num_pos;
288 // if nothing selected nothing to change...
289 if(XmListGetSelectedPos(this->getWidget(),
293 XtVaGetValues(entry_field,
298 if(strlen(test_str) > 1)
301 new_pair = (*list_items)[pos_list[0] - 1];
303 if(deleted_items == NULL)
305 deleted_items = new DtVirtArray<char *>(10);
308 deleted_items->append(strdup((*list_items)[pos_list[0] -1]->label));
310 free(new_pair->label);
311 new_pair->label = strdup(test_str);
313 replace_string = XmStringCreateLocalized(new_pair->label);
315 XmListReplaceItemsPos(this->getWidget(),
320 XmListSelectPos(this->getWidget(),
324 props_changed = TRUE;
327 ///////////////////////////////////////////////////////////
328 void AlternatesListUiItem::handleDeleteButtonPress()
330 Widget list_widget = this->getWidget();
331 int *p_list, p_count;
333 // get the selected position
334 if(XmListGetSelectedPos(list_widget,
339 if(deleted_items == NULL)
341 deleted_items = new DtVirtArray<char *>(10);
344 deleted_items->append(strdup((*list_items)[p_list[0] -1]->label));
346 // delete the item from our list
347 this->list_items->remove(p_list[0] - 1); // remove only first
349 // delete the item from the widget
350 XmListDeletePos(list_widget, p_list[0]);
352 XtVaSetValues(this->getEntryFieldWidget(),
355 XmListSelectPos(list_widget,
359 props_changed = TRUE;