home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / windows / x / motif / 8316 < prev    next >
Encoding:
Text File  |  1993-01-04  |  11.0 KB  |  334 lines

  1. Path: sparky!uunet!noc.near.net!hri.com!ukma!lexmark!lexmark.com!CJSENFT
  2. From: CJSENFT@lexmark.com
  3. Newsgroups: comp.windows.x.motif
  4. Subject: Core Dump when deleting from a List widget
  5. Message-ID: <16B4CC27C.CJSENFT@lexmark.com>
  6. Date: 4 Jan 93 18:49:48 GMT
  7. Sender: usenet@lexmark.com (News Dude)
  8. Organization: Lexmark International, Lexington, KY
  9. Lines: 321
  10. X-Disclaimer: These views are the poster's and not necessarily those of Lexmark
  11. Nntp-Posting-Host: lexvmlpp.lexmark.com
  12.  
  13. ******> WARNING Long (but necessary) Code Section Attached <******
  14.  
  15.      I have been trying to use a list widget in XmEXTENDED_SELECT mode.
  16. I wish to create create a list and have the user select items and then
  17. a push button which will delete the selected items.  Since the items in
  18. my actual application are not necessarily unique, I prefer to use the
  19. position list widget functions to perform the delete.  My problem is that
  20. the program core dumps.  I have written what I consider a simple test
  21. program (which follows) that also exhibits this problem.  I am using
  22. Motif v1.1 on a RISC/6000 (AIX v3.2) but I am not sure what patch level
  23. my version is at.
  24.  
  25.      The code includes alterations to the translations so that selections
  26. are always done in "add mode" (I am unsure of my terminology).  The code
  27. still core dumps if this alteration is removed and the user makes his
  28. selections manually using "add mode".  The crash occurs when a deletion
  29. has already been performed at least once and the user attempts to make
  30. another selection in the list.  The crash occurs in some internal
  31. function (AddItem) before my XmNextendedSelectionCallback is called.
  32. Although the crash is quite unpredictable it seems to occur more
  33. frequently when items are deleted from the end of the list.
  34.  
  35.      Thanks Warren A. Bullock
  36.      (via Cathy J. Senft :: Lexmark International, Inc. :: cjsenft@lexmark.com)
  37.  
  38.  
  39. # ========================================================================
  40. # BEGINNING OF SOURCE CODE
  41. # ========================================================================
  42.  
  43.  
  44. /************************************************************************/
  45. /* Name: list_test.c                                                    */
  46. /*                                                                      */
  47. /* Description: test list widget delete behavior                        */
  48. /*                                                                      */
  49. /* History:  Dec 21 1992  Warren A. Bullock                             */
  50. /************************************************************************/
  51.  
  52. /*****************/
  53. /* include files */
  54. /*****************/
  55. #include <stdio.h>
  56. #include <Xm/Xm.h>
  57. #include <Xm/List.h>
  58. #include <Xm/PushB.h>
  59. #include <Xm/Form.h>
  60.  
  61. /******************/
  62. /* local literals */
  63. /******************/
  64. #define LIST_LENGTH 100
  65.  
  66. /***********************/
  67. /* function prototypes */
  68. /***********************/
  69. void select_callback(Widget, XtPointer, XmListCallbackStruct *);
  70. void delete_callback(Widget, XtPointer, XtPointer);
  71. void quit_callback(Widget, XtPointer, XtPointer);
  72.  
  73. /********************/
  74. /* global variables */
  75. /********************/
  76. int list_num, selections[LIST_LENGTH];
  77. Widget list_w;
  78.  
  79. /*****************/
  80. /* main function */
  81. /*****************/
  82. main (int argc, char **argv)
  83.   {
  84.     XtAppContext app;
  85.     XtTranslations trans_parsed;
  86.     Widget toplevel_w, form_w, delete_button_w, quit_button_w;
  87.     XmString list_data[LIST_LENGTH], temp_xmstr;
  88.     char trans_str[] = "#replace \n\
  89.             <Btn1Down>:    ListBeginToggle()  \n\
  90.             <Btn1Motion>:  ListButtonMotion() \n\
  91.             <Btn1Up>:      ListEndToggle()    \n",
  92.          temp_str[80];
  93.     Arg args[10];
  94.     int num_args, i;
  95.  
  96.     /*************************************************/
  97.     /* initialize the toolkit; Create toplevel shell */
  98.     /*************************************************/
  99.     toplevel_w = XtVaAppInitialize(&app, "LT", NULL, 0, &argc, argv, NULL,
  100.         XmNtitle, "ListTest", NULL);
  101.  
  102.     /*************************************************************/
  103.     /* create a form widget which will contain all other widgets */
  104.     /*************************************************************/
  105.     form_w = XtVaCreateManagedWidget("form",
  106.         xmFormWidgetClass, toplevel_w, NULL);
  107.  
  108.     /***************************************/
  109.     /* initialize list widget translations */
  110.     /***************************************/
  111.     trans_parsed = XtParseTranslationTable(trans_str);
  112.  
  113.     /*******************************/
  114.     /* initialize list widget data */
  115.     /*******************************/
  116.     for (i = 0; i < LIST_LENGTH; i++)
  117.       {
  118.         sprintf(temp_str, "%d Item    ", i + 1);
  119.         list_data[i] = XmStringCreateLtoR(temp_str, XmSTRING_DEFAULT_CHARSET);
  120.         selections[i] = FALSE;
  121.       }
  122.     list_num = LIST_LENGTH;
  123.  
  124.     /**********************/
  125.     /* create list widget */
  126.     /**********************/
  127.     num_args = 0;
  128.     XtSetArg(args[num_args], XmNselectionPolicy, XmEXTENDED_SELECT);
  129.         num_args++;
  130.     XtSetArg(args[num_args], XmNitems, list_data); num_args++;
  131.     XtSetArg(args[num_args], XmNitemCount, list_num); num_args++;
  132.     XtSetArg(args[num_args], XmNvisibleItemCount, 10); num_args++;
  133.     XtSetArg(args[num_args], XmNleftAttachment, XmATTACH_FORM); num_args++;
  134.     XtSetArg(args[num_args], XmNtopAttachment, XmATTACH_FORM); num_args++;
  135.     list_w = XmCreateScrolledList(form_w, "list", args, num_args);
  136.     XtManageChild(list_w);
  137.  
  138.     /*******************************/
  139.     /* change list widget behavior */
  140.     /*******************************/
  141.     num_args = 0;
  142.     XtSetArg(args[num_args], XmNtranslations, trans_parsed); num_args++;
  143.     XtSetValues(list_w, args, num_args);
  144.     XtAddCallback(list_w, XmNextendedSelectionCallback,
  145.       select_callback, NULL);
  146.  
  147.     /************************/
  148.     /* create delete button */
  149.     /************************/
  150.     temp_xmstr = XmStringCreateLtoR("Delete", XmSTRING_DEFAULT_CHARSET);
  151.     delete_button_w = XtVaCreateManagedWidget("delete_button",
  152.         xmPushButtonWidgetClass, form_w,
  153.         XmNleftAttachment, XmATTACH_FORM,
  154.         XmNtopAttachment, XmATTACH_WIDGET,
  155.         XmNtopWidget, list_w,
  156.         XmNmultiClick, XmMULTICLICK_DISCARD,
  157.         XmNlabelString, temp_xmstr, NULL);
  158.     XtAddCallback(delete_button_w, XmNactivateCallback,
  159.       delete_callback, NULL);
  160.     XmStringFree(temp_xmstr);
  161.  
  162.     /**********************/
  163.     /* create quit button */
  164.     /**********************/
  165.     temp_xmstr = XmStringCreateLtoR("Quit", XmSTRING_DEFAULT_CHARSET);
  166.     quit_button_w = XtVaCreateManagedWidget("quit_button",
  167.         xmPushButtonWidgetClass, form_w,
  168.         XmNleftAttachment, XmATTACH_WIDGET,
  169.         XmNleftWidget, delete_button_w,
  170.         XmNtopAttachment, XmATTACH_WIDGET,
  171.         XmNtopWidget, list_w,
  172.         XmNmultiClick, XmMULTICLICK_DISCARD,
  173.         XmNlabelString, temp_xmstr, NULL);
  174.     XtAddCallback(quit_button_w, XmNactivateCallback,
  175.       quit_callback, NULL);
  176.     XmStringFree(temp_xmstr);
  177.  
  178.     /*******************************/
  179.     /* realize application widgets */
  180.     /*******************************/
  181.     XtRealizeWidget(toplevel_w);
  182.  
  183.     /********************/
  184.     /* start event loop */
  185.     /********************/
  186.     XtAppMainLoop(app);
  187.   }
  188.  
  189. /*********************************/
  190. /* callback to select list items */
  191. /*********************************/
  192. void select_callback(Widget w, XtPointer client_data,
  193.     XmListCallbackStruct *list_data)
  194.   {
  195.     int i, j;
  196.  
  197. #ifdef DEBUG
  198.       {
  199.         int num;
  200.         XmString *items;
  201.  
  202.         XtVaGetValues(list_w, XmNitemCount, &num, XmNitems, &items, NULL);
  203.         fprintf(stderr, "BEFORE SELECTION NUMBER IN LIST (%d)\n", num);
  204.         if (num > 0)
  205.           {
  206.             for(i = 0; i < num; i++)
  207.               {
  208.                 char *str;
  209.  
  210.                 XmStringGetLtoR(items[i], XmSTRING_DEFAULT_CHARSET, &str);
  211.                 fprintf(stderr, "  ITEM (%s)\n", str);
  212.               }
  213.           }
  214.       }
  215. #endif
  216.  
  217.     /*******************************************/
  218.     /* mark selected items in selections array */
  219.     /*******************************************/
  220.     for (i = 0, j = 0; i < list_num; i++)
  221.       {
  222.  
  223.         /***********************************/
  224.         /* while there are more selections */
  225.         /***********************************/
  226.         if (j < list_data->selected_item_count)
  227.           {
  228.             if (list_data->selected_item_positions[j] == i + 1)
  229.               {
  230.                 selections[i] = TRUE;
  231.                 j++;
  232.               }
  233.             else
  234.               selections[i] = FALSE;
  235.           }
  236.         else
  237.           selections[i] = FALSE;
  238.       }
  239.  
  240.     /*******************/
  241.     /* show selections */
  242.     /*******************/
  243.     fprintf(stderr, "SELECTIONS\n");
  244.     for (i = 0; i < list_num; i++)
  245.       {
  246.         if (selections[i] == TRUE)
  247.           fprintf(stderr, "  %d\n", i+1);
  248.       }
  249.     fprintf(stderr, "\n");
  250.   }
  251.  
  252. /*********************************/
  253. /* callback to delete list items */
  254. /*********************************/
  255. void delete_callback(Widget w, XtPointer client_data, XtPointer call_data)
  256.   {
  257.     int i;
  258.  
  259.     /*********************************/
  260.     /* delete all current selections */
  261.     /*********************************/
  262.     fprintf(stderr, "DELETIONS\n");
  263.     for (i = list_num - 1; i >= 0; i--)
  264.       {
  265.         if (selections[i] == TRUE)
  266.           {
  267.             XmListDeletePos(list_w, i + 1);
  268.             list_num--;
  269.             fprintf(stderr, "  %d\n", i + 1);
  270.           }
  271.       }
  272.     fprintf(stderr, "\n");
  273.  
  274.     /***********************************/
  275.     /* set all selections to all false */
  276.     /***********************************/
  277.     for (i = 0; i < list_num; i++)
  278.       selections[i] = FALSE;
  279.  
  280. #ifdef DEBUG
  281.       {
  282.         int num;
  283.         XmString *items;
  284.  
  285.         XtVaGetValues(list_w, XmNitemCount, &num, XmNitems, &items, NULL);
  286.         fprintf(stderr, "AFTER DELETE NUMBER IN LIST (%d)\n", num);
  287.         if (num > 0)
  288.           {
  289.             for(i = 0; i < num; i++)
  290.               {
  291.                 char *str;
  292.  
  293.                 XmStringGetLtoR(items[i], XmSTRING_DEFAULT_CHARSET, &str);
  294.                 fprintf(stderr, "  ITEM (%s)\n", str);
  295.               }
  296.           }
  297.       }
  298. #endif
  299.  
  300.   }
  301.  
  302. /****************************/
  303. /* callback to exit program */
  304. /****************************/
  305. void quit_callback(Widget w, XtPointer client_data, XtPointer call_data)
  306.   {
  307.     XtCloseDisplay(XtDisplay(w));
  308.     exit(0);
  309.   }
  310.  
  311. # ========================================================================
  312. # END OF SOURCE CODE
  313. # ========================================================================
  314.  
  315.  
  316. # ========================================================================
  317. # BEGINNING OF MAKEFILE
  318. # ========================================================================
  319.  
  320. list_test: list_test.o
  321.     cc -g -o list_test list_test.o -lXm -lXt -lX11
  322.  
  323. list_test.o: list_test.c
  324.     cc -g -c list_test.c
  325.  
  326.  
  327. # ========================================================================
  328. # END OF MAKEFILE
  329. # ========================================================================
  330.  
  331.  
  332. Thanks again.
  333. (Cathy J. Senft :: Lexmark International, Inc. :: cjsenft@lexmark.com)
  334.