Add GNU LGPL headers to all .c .C and .h files
[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 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 /*
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_shell (NULL),
85   f_popped_up (FALSE)
86 {
87 }
88
89
90 // /////////////////////////////////////////////////////////////////
91 // class destructor
92 // /////////////////////////////////////////////////////////////////
93
94 MarkListView::~MarkListView()
95 {
96   if (f_shell != NULL)
97     f_shell.Destroy();
98   free_mark_list();
99 }
100
101
102 // /////////////////////////////////////////////////////////////////
103 // display - display the dialog and update the mark list
104 // /////////////////////////////////////////////////////////////////
105
106 void
107 MarkListView::display()
108 {
109   Wait_Cursor bob;
110   if (f_shell == NULL)
111     create_ui();
112
113   if (!f_popped_up)
114     {
115       refresh_mark_list();
116       f_popped_up = TRUE;
117     }
118
119   f_shell.Popup();
120   f_shell.DeIconify();
121 }
122
123 // /////////////////////////////////////////////////////////////////
124 // create_ui
125 // /////////////////////////////////////////////////////////////////
126
127 void
128 MarkListView::create_ui()
129 {
130   XmStringLocalized mtfstring;
131   String            string;
132
133   f_shell = WTopLevelShell (window_system().toplevel(), WPopup, "mark_list");
134   window_system().register_shell(&f_shell);
135
136   string = CATGETS(Set_MarkList, 1, "Dtinfo: User Marks List");
137   XtVaSetValues((Widget)f_shell, XmNtitle, string, NULL);
138
139   DECL  (WXmForm,         form,         f_shell,      "form");
140   ASSNM (WXmPushButton,   f_view,       form,         "view");
141   ASSNM (WXmPushButton,   f_edit,       form,         "edit");
142   ASSNM (WXmPushButton,   f_delete,     form,         "delete"); // ross bug #14441
143   DECLM (WXmPushButton,   close,        form,         "close");
144   DECLM (WXmPushButton,   help,         form,         "help");
145   DECLM (WXmSeparator,    sep,          form,         "separator");
146   Widget list = XmCreateScrolledList (form, "list", NULL, 0);
147   f_list = WXmList (list);
148   f_list.Manage();
149
150   mtfstring = CATGETS(Set_AgentLabel, 165, "Display");
151   XtVaSetValues(f_view, XmNlabelString, (XmString)mtfstring, NULL);
152   mtfstring = CATGETS(Set_AgentLabel, 16, "Edit");
153   XtVaSetValues(f_edit, XmNlabelString, (XmString)mtfstring, NULL);
154   mtfstring = CATGETS(Set_AgentLabel, 183, "Delete");
155   XtVaSetValues(f_delete, XmNlabelString, (XmString)mtfstring, NULL);
156   mtfstring = CATGETS(Set_AgentLabel, 12, "Close");
157   XtVaSetValues(close, XmNlabelString, (XmString)mtfstring, NULL);
158   mtfstring = CATGETS(Set_AgentLabel, 48, "Help");
159   XtVaSetValues(help, XmNlabelString, (XmString)mtfstring, NULL);
160
161   SET_CALLBACK (f_list,SingleSelection,select);
162   SET_CALLBACK (f_list,BrowseSelection,select);
163   SET_CALLBACK (f_list,DefaultAction,view);
164   ON_ACTIVATE (f_view,view);
165   ON_ACTIVATE (f_edit,edit);
166   ON_ACTIVATE (f_delete,remove);
167   ON_ACTIVATE (close,close);
168   help_agent().add_activate_help (help, "mark_list_help");
169
170   SET_CALLBACK (f_shell,Popdown,popdown);
171
172   form.DefaultButton (f_view);
173   form.ShadowThickness (0);
174   form.Manage();
175
176   f_shell.Realize();
177   f_shell.MinWidth (f_shell.Width());
178   f_shell.MinHeight (f_shell.Height());
179
180   MarkMgr::request (this);
181   Mark::request ((UAS_Receiver<MarkDeleted> *) this);
182   Mark::request ((UAS_Receiver<MarkChanged> *) this);
183 }
184
185
186 // /////////////////////////////////////////////////////////////////
187 // get_list - get the list of user marks
188 // /////////////////////////////////////////////////////////////////
189
190 void
191 MarkListView::refresh_mark_list()
192 {
193   mark_mgr().get_all_marks (f_mark_list);
194
195   unsigned int length = f_mark_list.length();
196   // Convert the list to XmString values.
197   XmStringTable table = new XmString[length];
198   
199   List_Iterator<UAS_Pointer<Mark> > m (f_mark_list);
200
201   int i = 0;
202   while (m)
203     {
204       table[i] = XmStringCreateLocalized ((String) m.item()->name());
205       m++;
206       i++;
207     }
208
209   // Update the list to display those items. 
210   f_list.Set (WArgList (XmNitemCount, (XtArgVal) length,
211                         XmNitems, table,
212                         NULL));
213
214   // Free the strings since the list widget keeps its own copy. 
215   while (i)
216     XmStringFree (table[--i]);
217   delete [] table;
218 }
219
220
221 // /////////////////////////////////////////////////////////////////
222 // free_list - release the mark ptrs in the list
223 // /////////////////////////////////////////////////////////////////
224
225 void
226 MarkListView::free_mark_list()
227 {
228   List_Iterator<UAS_Pointer<Mark> > m (f_mark_list);
229
230   while (m)
231     f_mark_list.remove (m);
232 }
233
234
235 // /////////////////////////////////////////////////////////////////
236 // popdown
237 // /////////////////////////////////////////////////////////////////
238
239 void
240 MarkListView::popdown()
241 {
242   f_popped_up = FALSE;
243   // Forget about everything while the dialog isn't visible. 
244   free_mark_list();
245   f_list.DeleteAllItems();
246   // Current we forget about the selected item.
247   // This is fixable by remembering the selected mark pointer. 
248   f_selected_item = 0;
249   f_view.SetSensitive (False);
250   f_edit.SetSensitive (False);
251   f_delete.SetSensitive (False);        // ross for bug #14441
252 }
253
254 // /////////////////////////////////////////////////////////////////
255 // select - handle item selection
256 // /////////////////////////////////////////////////////////////////
257
258 void
259 MarkListView::select (WCallback *wcb)
260 {
261   CALL_DATA (XmListCallbackStruct,lcs);
262
263   f_selected_item = lcs->item_position;
264
265   ON_DEBUG (printf ("Selected item #%d\n", f_selected_item);)
266
267   f_view.SetSensitive (True);
268   f_edit.SetSensitive (True);
269   f_delete.SetSensitive (True);         // ross for bug #14441
270 }
271
272
273 // /////////////////////////////////////////////////////////////////
274 // view - view a mark
275 // /////////////////////////////////////////////////////////////////
276
277 void
278 MarkListView::view()
279 {
280   Wait_Cursor bob_dobbs;
281
282   try
283     {
284
285       Xassert (f_selected_item > 0);
286       Xassert (f_selected_item <= f_mark_list.length());
287
288       List_Iterator<UAS_Pointer<Mark> > m (f_mark_list);
289       int i  = 1;
290
291       // Skip ahead to the selected item in the list. 
292       while (i < f_selected_item)
293         i++, m++;
294
295       // Now `m' points to the selected Mark.
296       m.item()->view();
297     }
298   catch(demoException &, demo)
299     {
300       message_mgr().demo_failure(demo);
301     }
302   end_try;
303 }
304
305
306 // /////////////////////////////////////////////////////////////////
307 // remove - delete a mark
308 // /////////////////////////////////////////////////////////////////
309
310 void
311 MarkListView::remove()
312 {
313   Xassert (f_selected_item > 0);
314   Xassert (f_selected_item <= f_mark_list.length());
315
316   List_Iterator<UAS_Pointer<Mark> > m (f_mark_list);
317   int i  = 1;
318
319   // Skip ahead to the selected item in the list. 
320   while (i < f_selected_item)
321     i++, m++;
322
323   // Now `m' points to the selected Mark.
324   m.item()->remove();           // ross bug #14441
325 }
326
327
328
329
330 // /////////////////////////////////////////////////////////////////
331 // edit - edit a mark
332 // /////////////////////////////////////////////////////////////////
333
334 void
335 MarkListView::edit()
336 {
337   Xassert (f_selected_item > 0);
338   Xassert (f_selected_item <= f_mark_list.length());
339
340   List_Iterator<UAS_Pointer<Mark> > m (f_mark_list);
341   int i  = 1;
342
343   // Skip ahead to the selected item in the list. 
344   while (i < f_selected_item)
345     i++, m++;
346
347   // Now `m' points to the selected Mark.
348   m.item()->edit();
349 }
350
351
352 // /////////////////////////////////////////////////////////////////
353 // close - popdown the the dialog
354 // /////////////////////////////////////////////////////////////////
355
356 void
357 MarkListView::close()
358 {
359   f_shell.Popdown();
360 }
361
362
363 // /////////////////////////////////////////////////////////////////
364 // receive - handle mark creation/deletion
365 // /////////////////////////////////////////////////////////////////
366
367 void
368 MarkListView::receive (MarkCreated &msg, void* /*client_data*/)
369 {
370   if (!f_popped_up)
371     return;
372
373   f_list.AddItemUnselected (msg.f_mark_ptr->name(), 0);
374   f_mark_list.append (msg.f_mark_ptr);
375 }
376
377
378 void
379 MarkListView::receive (MarkDeleted &msg, void* /*client_data*/)
380 {
381   if (!f_popped_up)
382     return;
383
384   List_Iterator<UAS_Pointer<Mark> > m (f_mark_list);
385   int i  = 1;
386
387   // Skip ahead to the deleted item item in the list. 
388   while (m && m.item() != msg.f_mark_ptr)
389     {
390       ON_DEBUG (printf ("Checking <%s> against <%s>\n",
391                         m.item()->name(), msg.f_mark_ptr->name()));
392       i++, m++;
393     }
394
395   Xassert (m != NULL);
396
397   //
398   //  XXXSWM: Need to check if this is the selected item.
399   //  if so, we gotta clear f_selected_item and Desensitize
400   //  the display and edit buttons.
401   //
402   if (XmListPosSelected (f_list, i)) {
403       f_selected_item = 0;
404       f_view.SetSensitive (False);
405       f_edit.SetSensitive (False);
406       f_delete.SetSensitive (False);
407   }
408
409   // Remove it from the list widget and our list. 
410   f_list.DeletePos (i);
411   f_mark_list.remove (m);
412 }
413
414
415 void
416 MarkListView::receive (MarkChanged &msg, void* /*client_data*/)
417 {
418   if (!f_popped_up)
419     return;
420
421   List_Iterator<UAS_Pointer<Mark> > m (f_mark_list);
422   int i  = 1;
423
424   // Skip ahead to the changed item item in the list. 
425   while (m && m.item() != msg.f_mark_ptr)
426     {
427       ON_DEBUG (printf ("Checking <%s> against <%s>\n",
428                         m.item()->name(), msg.f_mark_ptr->name()));
429       i++, m++;
430     }
431
432   Xassert (m != NULL);
433
434   // Change the title of the entry, once it is found.
435   WXmString title (msg.f_mark_ptr->name());
436   XmString  table[1];
437   table[0] = title;
438
439   f_list.ReplaceItemsPosUnselected (table, 1, i);
440   if (f_selected_item == i)
441     f_list.SelectPos (i, False);
442 }