cmnutils.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. /* $XConsortium: cmnutils.c /main/4 1995/11/01 16:11:47 rswiston $ */
  24. /***************************************************************************/
  25. /* */
  26. /* Utility Functions */
  27. /* */
  28. /***************************************************************************/
  29. #include <stdio.h>
  30. #include <stdarg.h>
  31. #include <stdlib.h>
  32. #include <Xm/Xm.h>
  33. #include <Xm/RowColumnP.h>
  34. #include <Xm/MessageB.h>
  35. #include <Xm/Text.h>
  36. #include <Xm/List.h>
  37. #include "cmnutils.h"
  38. /********************************************************************************/
  39. /* countItems - counts the number of items in a null terminated array */
  40. /* INPUT: char **items - null terminated array */
  41. /* OUTPUT: int lcv - number of items in array */
  42. /********************************************************************************/
  43. int countItems (char **items)
  44. {
  45. int lcv = 0;
  46. /*
  47. while (items[lcv]) {
  48. lcv++;
  49. }
  50. */
  51. if (items) {
  52. for (lcv = 0; items[lcv]; lcv++);
  53. }
  54. return (lcv);
  55. }
  56. /********************************************************************************/
  57. /* TextStringsToXmStrings - Given an array of C text strings returns an */
  58. /* array of XmStrings. */
  59. /* INPUT: char **text_strings - array of C style strings */
  60. /* OUTPUT: XmStringTable xmstrings - an array Motif compound strings */
  61. /********************************************************************************/
  62. XmStringTable TextStringsToXmStrings (char **text_strings)
  63. {
  64. XmStringTable xmstrings = NULL;
  65. int count, lcv;
  66. if (text_strings) {
  67. count = countItems (text_strings);
  68. xmstrings = (XmStringTable) calloc (sizeof(XmString), (count));
  69. for (lcv = 0; lcv < count; lcv++)
  70. xmstrings[lcv] = (XmString) XmStringCreateSimple (text_strings[lcv]);
  71. }
  72. return ((XmStringTable)xmstrings);
  73. }
  74. /********************************************************************************/
  75. /* XmStringToText - Given an XmString returns a C character text string. */
  76. /* INPUT: XmString xmstring - a Motif compound string */
  77. /* OUTPUT: char *text_string - C style string */
  78. /********************************************************************************/
  79. char *XmStringToText (XmString xmstring)
  80. {
  81. XmStringContext context;
  82. XmStringCharSet charset;
  83. XmStringDirection direction;
  84. Boolean separator;
  85. char *text_string = NULL, *temp = NULL;
  86. text_string = (char *)calloc (1, sizeof (char));
  87. if (xmstring) {
  88. if (!XmStringInitContext (&context, xmstring)) {
  89. printf("Can't convert compound string.\n");
  90. return (NULL);
  91. }
  92. while (XmStringGetNextSegment (context, &temp, &charset,
  93. &direction, &separator)) {
  94. text_string = (char *)realloc (text_string, strlen (temp)+1);
  95. if (text_string == NULL) {
  96. printf("Can't allocate space for file name.\n");
  97. return (NULL);
  98. }
  99. text_string = strcpy(text_string, temp);
  100. }
  101. XmStringFreeContext(context);
  102. }
  103. return (text_string);
  104. }
  105. /********************************************************************************/
  106. /* delete_all_list_items - removes all items from a list box */
  107. /* INPUT: Widget list - id of list widget */
  108. /* OUTPUT: none */
  109. /********************************************************************************/
  110. void delete_all_list_items (Widget list)
  111. {
  112. XtArgVal /* int */ item_count = 0;
  113. XtVaGetValues (list, XmNitemCount, &item_count, NULL);
  114. if (item_count > 0) {
  115. XmListDeleteItemsPos (list, item_count, 1);
  116. }
  117. return;
  118. }
  119. /********************************************************************************/
  120. /* clear_text_field - removes any text from a text field */
  121. /* INPUT: Widget textfield - id of text widget */
  122. /* OUTPUT: none */
  123. /********************************************************************************/
  124. void clear_text_field (Widget textfield)
  125. {
  126. XmTextPosition last_pos;
  127. char *empty = "";
  128. last_pos = XmTextGetLastPosition (textfield);
  129. XmTextReplace (textfield, 0, last_pos, empty);
  130. return;
  131. }
  132. /********************************************************************************/
  133. /* clear_text - removes any text from a text widget */
  134. /* INPUT: Widget textwid - id of text widget */
  135. /* OUTPUT: none */
  136. /********************************************************************************/
  137. void clear_text (Widget textwid)
  138. {
  139. XmTextPosition last_pos;
  140. char *empty = "";
  141. last_pos = XmTextGetLastPosition (textwid);
  142. XmTextReplace (textwid, 0, last_pos, empty);
  143. return;
  144. }