home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume19 / xbmbrowser / part01 / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-28  |  4.8 KB  |  217 lines

  1. /*
  2. *****************************************************************************
  3. ** xbmbrowser version 1.0  (c) Copyright Ashley Roll, 1992.
  4. ** FILE: misc.c
  5. **
  6. ** xbmbrowser is Public Domain. However it, and all the code still belong to me.
  7. ** I do, however grant permission for you to freely copy and distribute it on 
  8. ** the condition that this and all other copyright notices remain unchanged in 
  9. ** all distributions.
  10. **
  11. ** This software comes with NO warranty whatsoever. I therefore take no
  12. ** responsibility for any damages, losses or problems that the program may 
  13. ** cause.
  14. *****************************************************************************
  15. */
  16.  
  17. #include "xbmbrowser.h"
  18.  
  19.  
  20. /* 
  21. ** This function is called by the widgets and Windowmanager to quit the 
  22. ** program
  23. */
  24. void DoQuit(w, event, params, num_params)
  25. Widget w;
  26. XEvent *event;
  27. String *params;
  28. Cardinal *num_params;
  29. {
  30.   exit(0);
  31. }
  32.  
  33. /*
  34. ** Create a new menu - and put a line under the lable
  35. */
  36. Widget MakeMenu(parent,name,label)
  37. Widget parent;
  38. char *name,*label;
  39. {
  40. Widget w;
  41.  
  42.  
  43.    w = XtVaCreatePopupShell(name,simpleMenuWidgetClass,parent,
  44.                      XtNlabel,(XtArgVal)label,NULL);
  45.  
  46.    if (label != NULL)
  47.    (void) XtVaCreateManagedWidget("line",smeLineObjectClass,w,NULL);
  48.  
  49. return(w);
  50. }
  51.  
  52.  
  53. /* 
  54. ** add an smeBSBObjectClass object to a menu (widget)
  55. ** callback is the procedure that it calls or NULL
  56. */
  57. Widget AddMenuItem(menu,label,callback,cbdata,sensitive)
  58. Widget menu;
  59. char *label;
  60. XtCallbackProc callback;
  61. XtPointer cbdata;
  62. int sensitive;
  63. {
  64. Widget w;
  65.  
  66.   w = XtVaCreateManagedWidget(
  67.              label,smeBSBObjectClass,menu,
  68.              XtNlabel,(XtArgVal)label,XtNjustify,XtJustifyCenter,NULL);
  69.  
  70.   if(!sensitive)
  71.     XtVaSetValues(w,XtNsensitive,(XtArgVal)False,NULL);
  72.  
  73.   if(callback != NULL)
  74.   XtAddCallback( w, "callback",callback, cbdata);
  75.  
  76. return (w);
  77. }
  78.  
  79. /*
  80. ** these two procedures handle the help window 
  81. **
  82. ** the first is the callback for the 'Done' button the other created the 
  83. ** window and puts the stuff in it.
  84. */
  85. Widget h_shell;
  86.  
  87. void Quit_Help(w,client_data,call_data )
  88. Widget w;
  89. XtPointer client_data,call_data;
  90. {
  91.   XtDestroyWidget(h_shell);
  92. }
  93.  
  94. void Show_Help(w,client_data,call_data )
  95. Widget w;
  96. XtPointer client_data,call_data;
  97. {
  98. Widget button,about,form;
  99.  
  100.    /* create the popup shell to put the about into */
  101.    h_shell = XtVaCreatePopupShell("Help_Window",shellWidgetClass,bfw,NULL);
  102.  
  103.    form = XtVaCreateManagedWidget("Help_fw",formWidgetClass,h_shell,NULL);
  104.  
  105.   /* create the widget to put the about into */
  106.   about = XtVaCreateManagedWidget("text",asciiTextWidgetClass,form,
  107.           XtNdisplayCaret,(XtArgVal)False,
  108.           XtNscrollVertical,(XtArgVal)XawtextScrollWhenNeeded,
  109.           XtNscrollHorizontal,(XtArgVal)XawtextScrollWhenNeeded,
  110.           XtNwidth,(XtArgVal)575,
  111.           XtNheight,(XtArgVal)200,
  112.           XtNtype,(XtArgVal)XawAsciiFile,
  113.           XtNstring,(XtArgVal)HELPFILE,NULL);
  114.  
  115.  
  116.  button =  XtVaCreateManagedWidget(
  117.            "Done",commandWidgetClass,form,XtNlabel,(XtArgVal)"Done",
  118.            XtNfromVert,(XtArgVal)about,NULL);
  119.  XtAddCallback(button,"callback",Quit_Help,NULL);
  120.  
  121.  XtPopup(h_shell,XtGrabNone);
  122.  XStoreName(XtDisplay(h_shell),XtWindow(h_shell),"xbmbrowser Help");
  123.  XSetIconName(XtDisplay(h_shell),XtWindow(h_shell),"xbmbrowser Help");
  124. }
  125.  
  126. /* 
  127. ** sort a linked list 
  128. */
  129. FileList *r; /* global - pointer to the first element in the list 2 b sorted */
  130.  
  131. FileList *merge(a,b)
  132. FileList *a,*b;
  133. {
  134. FileList *temp;
  135. FileList aux;
  136.  
  137.   temp = &aux;
  138.   while(b != NULL)
  139.     if(a == NULL) { 
  140.       a = b; 
  141.       break;
  142.     }
  143.     else if(strcmp(b->fname,a->fname) > 0) {
  144.       temp = temp->next = a; 
  145.       a = a->next;
  146.     } else {
  147.       temp = temp->next = b;
  148.       b = b->next;
  149.     }
  150.  
  151.   temp->next = a;
  152. return(aux.next);
  153. }
  154.  
  155. FileList *sort(n)
  156. int n;
  157. {
  158. FileList *fi,*la,*temp;
  159.  
  160.   if(r == NULL) return(NULL);
  161.   else if(n > 1)
  162.     return(merge(sort(n/2),sort((n+1)/2)));
  163.   else {
  164.     fi = r;
  165.     la = r;
  166.     /* build list as long as possible */
  167.     for(r = r->next; r != NULL;)
  168.       if(strcmp(r->fname,la->fname) >= 0) {
  169.         la->next = r;
  170.         la = r;
  171.         r = r->next;
  172.       }
  173.       else if(strcmp(r->fname,fi->fname) <= 0) {
  174.         temp = r;
  175.         r = r->next;
  176.         temp->next = fi;
  177.         fi = temp;
  178.       }
  179.       else break;
  180.     
  181.   la->next = NULL;
  182.   return(fi);
  183.   }
  184. }
  185.  
  186.  
  187. /*
  188. ** gets all the file names from the directory 'dir'
  189. ** and puts then into a FileList linked list.
  190. */
  191. FileList *get_files(dir)
  192. char *dir;
  193. {
  194. DIR *dirp;
  195. struct dirent *dp;
  196. FileList *start=NULL,*t;
  197. int count;
  198.  
  199.   dirp = opendir(dir);
  200.   for(dp = readdir(dirp),count = 1; dp != NULL; dp = readdir(dirp),count++) {
  201.     if(start == NULL) {
  202.       start = (FileList *) malloc(sizeof(FileList));
  203.       t = start; t->next = NULL;
  204.     } else {
  205.       t->next = (FileList *) malloc(sizeof(FileList));
  206.       t = t->next;
  207.       t->next = NULL;
  208.     }
  209.     strcpy(t->fname,dp->d_name);
  210.   }
  211.   closedir(dirp);
  212.   r = start;
  213. return(sort(count));
  214. }
  215.  
  216.  
  217.