Fix warnings on FreeBSD
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / Agents / MarkListView.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 libraries 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  * $XConsortium: MarkListView.C /main/8 1996/09/13 20:45:08 cde-hal $
25  *
26  * Copyright (c) 1993 HAL Computer Systems International, Ltd.
27  * All rights reserved.  Unpublished -- rights reserved under
28  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
29  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
30  * OR DISCLOSURE.
31  * 
32  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
34  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
36  * INTERNATIONAL, LTD.
37  * 
38  *                         RESTRICTED RIGHTS LEGEND
39  * Use, duplication, or disclosure by the Government is subject
40  * to the restrictions as set forth in subparagraph (c)(l)(ii)
41  * of the Rights in Technical Data and Computer Software clause
42  * at DFARS 252.227-7013.
43  *
44  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
45  *                  1315 Dell Avenue
46  *                  Campbell, CA  95008
47  * 
48  */
49
50 #define C_TOC_Element
51 #define L_Basic
52
53 #define C_MarkListView
54 #define C_HelpAgent
55 #define L_Agents
56
57 #define C_WindowSystem
58 #define L_Other
59
60 #define C_MarkMgr
61 #define C_MessageMgr
62 #define L_Managers
63
64 #include "Other/XmStringLocalized.hh"
65 #include "Managers/CatMgr.hh"
66
67 #include <Prelude.h>
68
69 #include "Registration.hh"
70
71 #include <WWL/WXmForm.h>
72 #include <WWL/WXmSeparator.h>
73
74 #define CLASS MarkListView
75 #include "create_macros.hh"
76
77 #include "utility/mmdb_exception.h"
78
79 // /////////////////////////////////////////////////////////////////
80 // class constructor
81 // /////////////////////////////////////////////////////////////////
82
83 MarkListView::MarkListView()
84 : f_selected_item(0),
85   f_shell (NULL),
86   f_popped_up (FALSE)
87 {
88 }
89
90
91 // /////////////////////////////////////////////////////////////////
92 // class destructor
93 // /////////////////////////////////////////////////////////////////
94
95 MarkListView::~MarkListView()
96 {
97   if (f_shell != 0)
98     f_shell.Destroy();
99   free_mark_list();
100 }
101
102
103 // /////////////////////////////////////////////////////////////////
104 // display - display the dialog and update the mark list
105 // /////////////////////////////////////////////////////////////////
106
107 void
108 MarkListView::display()
109 {
110   Wait_Cursor bob;
111   if (f_shell == 0)
112     create_ui();
113
114   if (!f_popped_up)
115     {
116       refresh_mark_list();
117       f_popped_up = TRUE;
118     }
119
120   f_shell.Popup();
121   f_shell.DeIconify();
122 }
123
124 // /////////////////////////////////////////////////////////////////
125 // create_ui
126 // /////////////////////////////////////////////////////////////////
127
128 void
129 MarkListView::create_ui()
130 {
131   XmStringLocalized mtfstring;
132   String            string;
133
134   f_shell = WTopLevelShell (window_system().toplevel(), WPopup, "mark_list");
135   window_system().register_shell(&f_shell);
136
137   string = CATGETS(Set_MarkList, 1, "Dtinfo: User Marks List");
138   XtVaSetValues((Widget)f_shell, XmNtitle, string, NULL);
139
140   DECL  (WXmForm,         form,         f_shell,      "form");
141   ASSNM (WXmPushButton,   f_view,       form,         "view");
142   ASSNM (WXmPushButton,   f_edit,       form,         "edit");
143   ASSNM (WXmPushButton,   f_delete,     form,         "delete"); // ross bug #14441
144   DECLM (WXmPushButton,   close,        form,         "close");
145   DECLM (WXmPushButton,   help,         form,         "help");
146   DECLM (WXmSeparator,    sep,          form,         "separator");
147   Widget list = XmCreateScrolledList (form, (char*)"list", NULL, 0);
148   f_list = WXmList (list);
149   f_list.Manage();
150
151   mtfstring = CATGETS(Set_AgentLabel, 165, "Display");
152   XtVaSetValues(f_view, XmNlabelString, (XmString)mtfstring, NULL);
153   mtfstring = CATGETS(Set_AgentLabel, 16, "Edit");
154   XtVaSetValues(f_edit, XmNlabelString, (XmString)mtfstring, NULL);
155   mtfstring = CATGETS(Set_AgentLabel, 183, "Delete");
156   XtVaSetValues(f_delete, XmNlabelString, (XmString)mtfstring, NULL);
157   mtfstring = CATGETS(Set_AgentLabel, 12, "Close");
158   XtVaSetValues(close, XmNlabelString, (XmString)mtfstring, NULL);
159   mtfstring = CATGETS(Set_AgentLabel, 48, "Help");
160   XtVaSetValues(help, XmNlabelString, (XmString)mtfstring, NULL);
161
162   SET_CALLBACK (f_list,SingleSelection,select);
163   SET_CALLBACK (f_list,BrowseSelection,select);
164   SET_CALLBACK (f_list,DefaultAction,view);
165   ON_ACTIVATE (f_view,view);
166   ON_ACTIVATE (f_edit,edit);
167   ON_ACTIVATE (f_delete,remove);
168   ON_ACTIVATE (close,close);
169   help_agent().add_activate_help (help, (char*)"mark_list_help");
170
171   SET_CALLBACK (f_shell,Popdown,popdown);
172
173   form.DefaultButton (f_view);
174   form.ShadowThickness (0);
175   form.Manage();
176
177   f_shell.Realize();
178   f_shell.MinWidth (f_shell.Width());
179   f_shell.MinHeight (f_shell.Height());
180
181   MarkMgr::request (this);
182   Mark::request ((UAS_Receiver<MarkDeleted> *) this);
183   Mark::request ((UAS_Receiver<MarkChanged> *) this);
184 }
185
186
187 // /////////////////////////////////////////////////////////////////
188 // get_list - get the list of user marks
189 // /////////////////////////////////////////////////////////////////
190
191 void
192 MarkListView::refresh_mark_list()
193 {
194   mark_mgr().get_all_marks (f_mark_list);
195
196   unsigned int length = f_mark_list.length();
197   // Convert the list to XmString values.
198   XmStringTable table = new XmString[length];
199   
200   List_Iterator<UAS_Pointer<Mark> > m (f_mark_list);
201
202   int i = 0;
203   while (m)
204     {
205       table[i] = XmStringCreateLocalized ((String) m.item()->name());
206       m++;
207       i++;
208     }
209
210   // Update the list to display those items. 
211   f_list.Set (WArgList (XmNitemCount, (XtArgVal) length,
212                         XmNitems, table,
213                         NULL));
214
215   // Free the strings since the list widget keeps its own copy. 
216   while (i)
217     XmStringFree (table[--i]);
218   delete [] table;
219 }
220
221
222 // /////////////////////////////////////////////////////////////////
223 // free_list - release the mark ptrs in the list
224 // /////////////////////////////////////////////////////////////////
225
226 void
227 MarkListView::free_mark_list()
228 {
229   List_Iterator<UAS_Pointer<Mark> > m (f_mark_list);
230
231   while (m)
232     f_mark_list.remove (m);
233 }
234
235
236 // /////////////////////////////////////////////////////////////////
237 // popdown
238 // /////////////////////////////////////////////////////////////////
239
240 void
241 MarkListView::popdown()
242 {
243   f_popped_up = FALSE;
244   // Forget about everything while the dialog isn't visible. 
245   free_mark_list();
246   f_list.DeleteAllItems();
247   // Current we forget about the selected item.
248   // This is fixable by remembering the selected mark pointer. 
249   f_selected_item = 0;
250   f_view.SetSensitive (False);
251   f_edit.SetSensitive (False);
252   f_delete.SetSensitive (False);        // ross for bug #14441
253 }
254
255 // /////////////////////////////////////////////////////////////////
256 // select - handle item selection
257 // /////////////////////////////////////////////////////////////////
258
259 void
260 MarkListView::select (WCallback *wcb)
261 {
262   CALL_DATA (XmListCallbackStruct,lcs);
263
264   f_selected_item = lcs->item_position;
265
266   ON_DEBUG (printf ("Selected item #%d\n", f_selected_item);)
267
268   f_view.SetSensitive (True);
269   f_edit.SetSensitive (True);
270   f_delete.SetSensitive (True);         // ross for bug #14441
271 }
272
273
274 // /////////////////////////////////////////////////////////////////
275 // view - view a mark
276 // /////////////////////////////////////////////////////////////////
277
278 void
279 MarkListView::view()
280 {
281   Wait_Cursor bob_dobbs;
282
283   mtry
284     {
285
286       Xassert (f_selected_item > 0);
287       Xassert (f_selected_item <= (int) f_mark_list.length());
288
289       List_Iterator<UAS_Pointer<Mark> > m (f_mark_list);
290       int i  = 1;
291
292       // Skip ahead to the selected item in the list. 
293       while (i < f_selected_item)
294         i++, m++;
295
296       // Now `m' points to the selected Mark.
297       m.item()->view();
298     }
299   mcatch(demoException &, demo)
300     {
301       message_mgr().demo_failure(demo);
302     }
303   end_try;
304 }
305
306
307 // /////////////////////////////////////////////////////////////////
308 // remove - delete a mark
309 // /////////////////////////////////////////////////////////////////
310
311 void
312 MarkListView::remove()
313 {
314   Xassert (f_selected_item > 0);
315   Xassert (f_selected_item <= (int) f_mark_list.length());
316
317   List_Iterator<UAS_Pointer<Mark> > m (f_mark_list);
318   int i  = 1;
319
320   // Skip ahead to the selected item in the list. 
321   while (i < f_selected_item)
322     i++, m++;
323
324   // Now `m' points to the selected Mark.
325   m.item()->remove();           // ross bug #14441
326 }
327
328
329
330
331 // /////////////////////////////////////////////////////////////////
332 // edit - edit a mark
333 // /////////////////////////////////////////////////////////////////
334
335 void
336 MarkListView::edit()
337 {
338   Xassert (f_selected_item > 0);
339   Xassert (f_selected_item <= (int) f_mark_list.length());
340
341   List_Iterator<UAS_Pointer<Mark> > m (f_mark_list);
342   int i  = 1;
343
344   // Skip ahead to the selected item in the list. 
345   while (i < f_selected_item)
346     i++, m++;
347
348   // Now `m' points to the selected Mark.
349   m.item()->edit();
350 }
351
352
353 // /////////////////////////////////////////////////////////////////
354 // close - popdown the the dialog
355 // /////////////////////////////////////////////////////////////////
356
357 void
358 MarkListView::close()
359 {
360   f_shell.Popdown();
361 }
362
363
364 // /////////////////////////////////////////////////////////////////
365 // receive - handle mark creation/deletion
366 // /////////////////////////////////////////////////////////////////
367
368 void
369 MarkListView::receive (MarkCreated &msg, void* /*client_data*/)
370 {
371   if (!f_popped_up)
372     return;
373
374   f_list.AddItemUnselected (msg.f_mark_ptr->name(), 0);
375   f_mark_list.append (msg.f_mark_ptr);
376 }
377
378
379 void
380 MarkListView::receive (MarkDeleted &msg, void* /*client_data*/)
381 {
382   if (!f_popped_up)
383     return;
384
385   List_Iterator<UAS_Pointer<Mark> > m (f_mark_list);
386   int i  = 1;
387
388   // Skip ahead to the deleted item item in the list. 
389   while (m && m.item() != msg.f_mark_ptr)
390     {
391       ON_DEBUG (printf ("Checking <%s> against <%s>\n",
392                         m.item()->name(), msg.f_mark_ptr->name()));
393       i++, m++;
394     }
395
396   Xassert (m != 0);
397
398   //
399   //  XXXSWM: Need to check if this is the selected item.
400   //  if so, we gotta clear f_selected_item and Desensitize
401   //  the display and edit buttons.
402   //
403   if (XmListPosSelected (f_list, i)) {
404       f_selected_item = 0;
405       f_view.SetSensitive (False);
406       f_edit.SetSensitive (False);
407       f_delete.SetSensitive (False);
408   }
409
410   // Remove it from the list widget and our list. 
411   f_list.DeletePos (i);
412   f_mark_list.remove (m);
413 }
414
415
416 void
417 MarkListView::receive (MarkChanged &msg, void* /*client_data*/)
418 {
419   if (!f_popped_up)
420     return;
421
422   List_Iterator<UAS_Pointer<Mark> > m (f_mark_list);
423   int i  = 1;
424
425   // Skip ahead to the changed item item in the list. 
426   while (m && m.item() != msg.f_mark_ptr)
427     {
428       ON_DEBUG (printf ("Checking <%s> against <%s>\n",
429                         m.item()->name(), msg.f_mark_ptr->name()));
430       i++, m++;
431     }
432
433   Xassert (m != 0);
434
435   // Change the title of the entry, once it is found.
436   WXmString title (msg.f_mark_ptr->name());
437   XmString  table[1];
438   table[0] = title;
439
440   f_list.ReplaceItemsPosUnselected (table, 1, i);
441   if (f_selected_item == i)
442     f_list.SelectPos (i, False);
443 }