-fpermissive to allow GCC to compile old C++
[oweals/cde.git] / cde / programs / dtmail / dtmail / MoveMenuListUiItem.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 /* $TOG: MoveMenuListUiItem.C /main/5 1997/09/03 17:39:09 mgreess $ */
24 /*
25  *+SNOTICE
26  *
27  *      $TOG: MoveMenuListUiItem.C /main/5 1997/09/03 17:39:09 mgreess $
28  *
29  *      RESTRICTED CONFIDENTIAL INFORMATION:
30  *      
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
37  *      Sun's request.
38  *
39  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
40  *
41  *+ENOTICE
42  */
43 #include "RoamApp.h"
44 #include <Xm/List.h>
45 #include <DtMail/options_util.h>
46 #include "options_ui.h"
47 #include <DtMail/PropUi.hh>
48 #include <DtMail/ListUiItem.hh>
49 #include <DtMail/MoveMenuListUiItem.hh>
50
51 extern void handleIgnoreSelection(Widget, XtPointer, XtPointer );
52 extern Boolean props_changed;
53
54 // MoveMenuListUiItem::MoveMenuListUiItem
55 // MoveMenuListUiItem ctor
56 ////////////////////////////////////////////////////////////////////
57
58 MoveMenuListUiItem::MoveMenuListUiItem(Widget w, 
59                        int source, 
60                        char *search_key,
61                        Widget text_entry_widget):ListUiItem(w, source, search_key, NULL)
62 {
63   source = source; search_key = search_key;
64
65   entry_field_widget = text_entry_widget;
66
67   list_items = NULL;
68   deleted_items = NULL;
69
70   XtVaSetValues(w,
71         XmNuserData, this,
72         XmNautomaticSelection, True,
73         XmNselectionPolicy, XmBROWSE_SELECT,
74         NULL);
75
76     XtAddCallback(w,
77                 XmNbrowseSelectionCallback, 
78                 (XtCallbackProc)handleMMSelection,
79                   (XtPointer)this);
80
81
82 }
83 //---------------------------------------------------------------
84 void handleMMSelection(Widget w, XtPointer, XtPointer calldata)
85 {
86   MoveMenuListUiItem *item;
87   XmListCallbackStruct *list_info = (XmListCallbackStruct *)calldata;
88   char *selection_string = NULL;
89   DtVirtArray<PropStringPair *> *list_items;
90
91   XtVaGetValues(w, 
92         XmNuserData, &item,
93         NULL);  
94
95   list_items = item->getItemList();
96
97   if(list_items != NULL)
98     {
99       // motif index is 1 based
100       //virtarry is 0 based
101       PropStringPair *pair = (*list_items)[list_info->item_position - 1];
102
103       if(pair != NULL)
104         {
105           XtVaSetValues(item->getEntryFieldWidget(),
106                         XmNvalue,pair->label,
107                         NULL);
108
109         }       
110
111     }
112 }
113
114 // MoveMenuListUiItem::writeFromUiToSource()
115 // Takes values in the UI and puts them into the source DB
116 ///////////////////////////////////////////////////////////////////
117 void MoveMenuListUiItem::writeFromUiToSource()
118 {
119   Widget w = this->getWidget();
120   DtMailEnv error;
121   DtMail::Session * d_session = theRoamApp.session()->session();
122   DtMail::MailRc * mail_rc = d_session->mailRc(error);
123   int i, num_items, total_len = 0;
124   char *mm_str = NULL;
125
126   if(list_items != NULL)
127     {
128       if(list_items->length() > 0)
129         {
130           num_items = list_items->length();
131
132           // calc mailrc strlen...
133           total_len = 1; // add space for the terminator...
134           for(i = 0; i < num_items; i++)
135             {
136               // strlen + space
137               total_len = total_len + strlen((*list_items)[i]->label) + 1;
138             }
139
140           mm_str = (char *)malloc(total_len);
141           mm_str[0] = '\0';
142
143           for(i = 0; i < num_items; i++)
144             {
145               strcat(mm_str, (*list_items)[i]->label);
146               strcat(mm_str, " ");
147             }
148         
149           mail_rc->setValue(error, "filemenu2", mm_str);
150         }
151       else
152         {
153           mail_rc->removeValue(error, "filemenu2");
154         }
155     }                   
156   else
157     {
158       mail_rc->removeValue(error, "filemenu2");
159     }  
160      
161 }
162
163 // MoveMenuListUiItem::writeFromSourceToUi()
164 // Takes values in the UI and puts them into the source DB
165 ///////////////////////////////////////////////////////////////////
166 void MoveMenuListUiItem::writeFromSourceToUi()
167 {
168   DtMailEnv error;
169   DtMail::Session * d_session = theRoamApp.session()->session();
170   DtMail::MailRc * mail_rc = d_session->mailRc(error);
171   Widget w = this->getWidget();
172   const char *list_str = NULL;
173   char *token, *buf = NULL;
174   PropStringPair *new_pair;
175   DtVirtArray<char *> *char_list = NULL;
176
177   XmListDeleteAllItems(w);
178
179   // set up move menu list
180   mail_rc->getValue(error, "filemenu2", &list_str);
181
182   if (list_str == NULL) {
183       list_str = strdup(" ");
184
185       // set list_items to NULL, otherwise the cancel and reset will not
186       // work. The list_items will be showed up next time even the cancel
187       // or reset button is pressed
188       list_items = NULL ;
189
190       if ((buf = (char *) malloc(strlen(list_str) + 1)) == NULL)
191         return;
192       strcpy(buf, (char *)list_str);
193   }
194   else {
195         char * expanded_str = d_session->expandPath(error, list_str);
196         if ((buf = (char *) malloc(strlen(expanded_str) + 1)) == NULL) {
197                 free(expanded_str);
198                 return;
199         }
200         strcpy(buf, expanded_str);
201         free(expanded_str);
202   }
203
204   if((token = (char *) strtok(buf, " "))) 
205     {
206       list_items = new DtVirtArray<PropStringPair *>(10);
207       char_list = new DtVirtArray<char *>(10);
208
209       new_pair = new PropStringPair;
210       new_pair->label = strdup(token);
211       new_pair->value = NULL;
212       list_items->append(new_pair);
213
214       char_list->append(strdup(token));
215       
216       while(token = (char *)strtok(NULL, " "))
217         {
218           new_pair = new PropStringPair;
219           new_pair->label = strdup(token);
220           new_pair->value = NULL;
221           list_items->append(new_pair);
222
223           char_list->append(strdup(token));
224         }
225     }
226
227   options_list_init(w, char_list);
228
229   delete char_list;
230   
231   if(list_str != NULL)
232       free((void*) list_str);
233
234   if(buf != NULL)
235       free((void *)buf);
236
237 }
238
239 ///////////////////////////////////////////////////////////
240 void MoveMenuListUiItem::handleAddButtonPress()
241 {
242   Widget entry_field = this->getEntryFieldWidget();
243   char *test_str = NULL;
244   PropStringPair *new_pair = NULL;
245
246   XtVaGetValues(entry_field,
247                 XmNvalue, &test_str,
248                 NULL);
249
250   if(test_str != NULL)
251     if(strlen(test_str) > 0)
252       {
253         new_pair = new PropStringPair;
254         int *pos_list, num_pos;
255
256         new_pair->label = strdup(test_str);
257         new_pair->value = NULL;
258       
259         if(XmListGetSelectedPos(this->getWidget(),
260                                 &pos_list,
261                                 &num_pos))
262           {
263             if(list_items == NULL)
264               list_items = new DtVirtArray<PropStringPair *>(10);
265
266             list_items->insert(new_pair,pos_list[0] - 1); 
267             XmListAddItem(this->getWidget(),
268                           XmStringCreateLocalized(new_pair->label),
269                           pos_list[0]);
270             
271             XmListSelectPos(this->getWidget(),
272                             pos_list[0],
273                             TRUE);
274
275             XmListSetPos(this->getWidget(),
276                          pos_list[0]); 
277           }     
278         else
279           {
280             if(list_items == NULL)
281               list_items = new DtVirtArray<PropStringPair *>(10);
282             
283             list_items->insert(new_pair,0); 
284             XmListAddItem(this->getWidget(),
285                           XmStringCreateLocalized(new_pair->label),
286                           1);
287             XmListSelectPos(this->getWidget(),
288                             1,
289                             TRUE);
290
291             XmListSetPos(this->getWidget(),
292                          1);     
293           }
294         props_changed = TRUE;
295       }
296 }
297 ///////////////////////////////////////////////////////////
298 void MoveMenuListUiItem::handleChangeButtonPress()
299 {
300   Widget entry_field = this->getEntryFieldWidget();
301   char *test_str = NULL;
302   PropStringPair *new_pair = NULL;
303   XmString replace_string;
304   int *pos_list, num_pos;
305
306   // if nothing selected nothing to change...
307   if(XmListGetSelectedPos(this->getWidget(),
308                           &pos_list,
309                           &num_pos))
310     {
311       XtVaGetValues(entry_field,
312                     XmNvalue, &test_str,
313                     NULL);
314
315       if(test_str != NULL)
316         if(strlen(test_str) > 0)
317           {
318
319             new_pair = (*list_items)[pos_list[0] - 1];
320
321             free(new_pair->label);
322             new_pair->label = strdup(test_str);
323   
324     
325 //          list_items->insert(new_pair,pos_list[0] - 1); 
326             replace_string = XmStringCreateLocalized(new_pair->label);
327
328             XmListReplaceItemsPos(this->getWidget(),
329                                   &replace_string,
330                                   1,
331                                   pos_list[0]);
332
333             XmListSelectPos(this->getWidget(),
334                             pos_list[0],
335                             TRUE);
336           }
337         props_changed = TRUE;
338     }
339 }
340 ///////////////////////////////////////////////////////////
341 void MoveMenuListUiItem::handleDeleteButtonPress()
342 {
343   Widget list_widget = this->getWidget();
344   int *p_list, p_count;
345
346   // get the selected position
347   if(XmListGetSelectedPos(list_widget,
348                           &p_list,
349                           &p_count))
350     {
351       
352       // delete the item from our list 
353       this->list_items->remove(p_list[0] - 1); // remove only first
354
355       // delete the item from the widget
356       XmListDeletePos(list_widget, p_list[0]);
357
358       XtVaSetValues(this->getEntryFieldWidget(),
359                         XmNvalue,"",
360                         NULL);
361       XmListSelectPos(list_widget,
362                       p_list[0],
363                       TRUE);
364       props_changed = TRUE;
365     }
366
367 }
368
369