PageRenderTime 71ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/FvwmWinList/ButtonArray.c

https://github.com/tonnerre/fvwm
C | 253 lines | 168 code | 27 blank | 58 comment | 32 complexity | dd8ac8bcc1cc8785d8eaac5770a88b84 MD5 | raw file
  1. /* FvwmWinList Module for Fvwm.
  2. *
  3. * Copyright 1994, Mike Finger (mfinger@mermaid.micro.umn.edu or
  4. * Mike_Finger@atk.com)
  5. *
  6. * The functions in this source file that are the original work of Mike Finger.
  7. *
  8. * No guarantees or warantees or anything are provided or implied in any way
  9. * whatsoever. Use this program at your own risk. Permission to use this
  10. * program for any purpose is given, as long as the copyright is kept intact.
  11. *
  12. * Things to do: Convert to C++ (In Progress)
  13. */
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <X11/Xlib.h>
  17. #include "ButtonArray.h"
  18. #include "Mallocs.h"
  19. #ifndef min
  20. #define min(a,b) (((a)<(b)) ? (a) : (b))
  21. #define max(a,b) (((a)>(b)) ? (a) : (b))
  22. #endif
  23. extern XFontStruct *ButtonFont;
  24. extern Display *dpy;
  25. extern Window win;
  26. extern GC shadow,hilite,graph;
  27. /******************************************************************************
  28. InitArray - Initialize the arrary of buttons
  29. ******************************************************************************/
  30. void InitArray(ButtonArray *array,int x,int y,int w,int h)
  31. {
  32. array->count=0;
  33. array->head=array->tail=NULL;
  34. array->x=x;
  35. array->y=y;
  36. array->w=w;
  37. array->h=h;
  38. }
  39. /******************************************************************************
  40. UpdateArray - Update the array specifics. x,y, width, height
  41. ******************************************************************************/
  42. void UpdateArray(ButtonArray *array,int x,int y,int w, int h)
  43. {
  44. Button *temp;
  45. if (x!=-1) array->x=x;
  46. if (y!=-1) array->y=y;
  47. if (w!=-1) array->w=w;
  48. if (h!=-1) array->h=h;
  49. for(temp=array->head;temp!=NULL;temp=temp->next) temp->needsupdate=1;
  50. }
  51. /******************************************************************************
  52. AddButton - Allocate space for and add the button to the bottom
  53. ******************************************************************************/
  54. int AddButton(ButtonArray *array, char *title, int up)
  55. {
  56. Button *new;
  57. new=(Button *)safemalloc(sizeof(Button));
  58. new->title=safemalloc(strlen(title)+1);
  59. strcpy(new->title,title);
  60. new->up=up;
  61. new->tw=XTextWidth(ButtonFont,title,strlen(title));
  62. new->next=NULL;
  63. new->needsupdate=1;
  64. if (array->head==NULL) array->head=array->tail=new;
  65. else {
  66. array->tail->next=new;
  67. array->tail=new;
  68. }
  69. array->count++;
  70. return (array->count-1);
  71. }
  72. /******************************************************************************
  73. UpdateButton - Change the name/stae of a button
  74. ******************************************************************************/
  75. int UpdateButton(ButtonArray *array, int butnum, char *title, int up)
  76. {
  77. Button *temp;
  78. temp=find_n(array,butnum);
  79. if (temp!=NULL) {
  80. if (title!=NULL) {
  81. temp->title=(char *)realloc(temp->title,strlen(title)+1);
  82. strcpy(temp->title,title);
  83. temp->tw=XTextWidth(ButtonFont,title,strlen(title));
  84. }
  85. if (up!=-1) temp->up=up;
  86. } else return -1;
  87. temp->needsupdate=1;
  88. return 1;
  89. }
  90. /******************************************************************************
  91. RemoveButton - Delete a button from the list
  92. ******************************************************************************/
  93. void RemoveButton(ButtonArray *array, int butnum)
  94. {
  95. Button *temp,*temp2;
  96. if (butnum==0) {
  97. temp2=array->head;
  98. temp=array->head=array->head->next;
  99. } else {
  100. temp=find_n(array,butnum-1);
  101. if (temp==NULL) return;
  102. temp2=temp->next;
  103. temp->next=temp2->next;
  104. }
  105. if (array->tail==temp2) array->tail=temp;
  106. FreeButton(temp2);
  107. if (temp!=array->head) temp=temp->next;
  108. for(temp;temp!=NULL;temp=temp->next) temp->needsupdate=1;
  109. }
  110. /******************************************************************************
  111. find_n - Find the nth button in the list (Use internally)
  112. ******************************************************************************/
  113. Button *find_n(ButtonArray *array, int n)
  114. {
  115. Button *temp;
  116. int i;
  117. temp=array->head;
  118. for(i=0;i<n && temp!=NULL;i++,temp=temp->next);
  119. return temp;
  120. }
  121. /******************************************************************************
  122. FreeButton - Free space allocated to a button
  123. ******************************************************************************/
  124. void FreeButton(Button *ptr)
  125. {
  126. if (ptr != NULL) {
  127. if (ptr->title!=NULL) free(ptr->title);
  128. free(ptr);
  129. }
  130. }
  131. /******************************************************************************
  132. FreeAllButtons - Free the whole array of buttons
  133. ******************************************************************************/
  134. void FreeAllButtons(ButtonArray *array)
  135. {
  136. Button *temp,*temp2;
  137. for(temp=array->head;temp!=NULL;) {
  138. temp2=temp;
  139. temp=temp->next;
  140. FreeButton(temp2);
  141. }
  142. }
  143. /******************************************************************************
  144. DoButton - Draw the specified button. (Used internally)
  145. ******************************************************************************/
  146. void DoButton(Button *ptr, int x, int y, int w, int h)
  147. {
  148. int up,Fontheight,newx;
  149. up=ptr->up;
  150. Fontheight=ButtonFont->ascent+ButtonFont->descent;
  151. XClearArea(dpy,win,x,y,w,h,False);
  152. XDrawLine(dpy,win,(up) ? hilite : shadow,x,y,x+w-1,y);
  153. XDrawLine(dpy,win,(up) ? hilite : shadow,x,y+1,x+w-2,y+1);
  154. XDrawLine(dpy,win,(up) ? hilite : shadow,x,y,x,y+h-1);
  155. XDrawLine(dpy,win,(up) ? hilite : shadow,x+1,y,x+1,y+h-2);
  156. XDrawLine(dpy,win,(up) ? shadow : hilite,x,y+h,x+w,y+h);
  157. XDrawLine(dpy,win,(up) ? shadow : hilite,x+1,y+h-1,x+w,y+h-1);
  158. XDrawLine(dpy,win,(up) ? shadow : hilite,x+w,y+h,x+w,y);
  159. XDrawLine(dpy,win,(up) ? shadow : hilite,x+w-1,y+h,x+w-1,y+1);
  160. newx=(w-ptr->tw)/2;
  161. XDrawString(dpy,win,graph,x+newx,y+3+ButtonFont->ascent,ptr->title,strlen(ptr->title));
  162. ptr->needsupdate=0;
  163. }
  164. /******************************************************************************
  165. DrawButtonArray - Draw the whole array (all=1), or only those that need.
  166. ******************************************************************************/
  167. void DrawButtonArray(ButtonArray *array,int all)
  168. {
  169. Button *temp;
  170. int i;
  171. for(temp=array->head,i=0;temp!=NULL;temp=temp->next,i++)
  172. if (temp->needsupdate || all) {
  173. DoButton(temp,array->x,array->y+(i*(array->h+1)),array->w,array->h);
  174. }
  175. }
  176. /******************************************************************************
  177. SwitchButton - Alternate the state of a button
  178. ******************************************************************************/
  179. void SwitchButton(ButtonArray *array,int butnum)
  180. {
  181. Button *temp;
  182. temp=find_n(array,butnum);
  183. temp->up=!temp->up;
  184. temp->needsupdate=1;
  185. DrawButtonArray(array,0);
  186. }
  187. /******************************************************************************
  188. WhichButton - Based on x,y which button was pressed
  189. ******************************************************************************/
  190. int WhichButton(ButtonArray *array,int x, int y)
  191. {
  192. int num;
  193. num=y/(array->h+1);
  194. if (x<array->x || x>array->x+array->w || num<0 || num>array->count-1) num=-1;
  195. return(num);
  196. }
  197. /******************************************************************************
  198. ButtonName - Return the name of the button
  199. ******************************************************************************/
  200. char *ButtonName(ButtonArray *array, int butnum)
  201. {
  202. Button *temp;
  203. temp=find_n(array,butnum);
  204. return temp->title;
  205. }
  206. /******************************************************************************
  207. PrintButtons - Print the array of button names to the console. (Debugging)
  208. ******************************************************************************/
  209. void PrintButtons(ButtonArray *array)
  210. {
  211. Button *temp;
  212. ConsoleMessage("List of Buttons:\n");
  213. for(temp=array->head;temp!=NULL;temp=temp->next)
  214. ConsoleMessage(" %s is %s\n",temp->title,(temp->up) ? "Up":"Down");
  215. }
  216. /******************************************************************************
  217. ButtonArrayMaxWidth - Calculate the width needed for the widest title
  218. ******************************************************************************/
  219. int ButtonArrayMaxWidth(ButtonArray *array)
  220. {
  221. Button *temp;
  222. int x=0;
  223. for(temp=array->head;temp!=NULL;temp=temp->next)
  224. x=max(temp->tw,x);
  225. return x;
  226. }