home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / x / volume20 / xclipbrd / part02 < prev    next >
Encoding:
Text File  |  1993-08-05  |  14.9 KB  |  561 lines

  1. Newsgroups: comp.sources.x
  2. From: vafaeia@anubis.network.com (Amir Vafaei)
  3. Subject: v20i106:  xclipboard - Load, save and print the X clipboard, Part02/02
  4. Message-ID: <1993Aug5.182601.28954@sparky.sterling.com>
  5. X-Md4-Signature: b3a7e332e263408ba988a5a6cb5d2f11
  6. Sender: chris@sparky.sterling.com (Chris Olson)
  7. Organization: Sterling Software
  8. Date: Thu, 5 Aug 1993 18:26:01 GMT
  9. Approved: chris@sterling.com
  10.  
  11. Submitted-by: vafaeia@anubis.network.com (Amir Vafaei)
  12. Posting-number: Volume 20, Issue 106
  13. Archive-name: xclipboard/part02
  14. Environment: X11
  15. Supersedes: xclipboard: Volume 20, Issue 56
  16.  
  17. ---- Cut Here and feed the following to sh ----
  18. #!/bin/sh
  19. # this is clip.02 (part 2 of xclipboard)
  20. # do not concatenate these parts, unpack them in order with /bin/sh
  21. # file xclipboard/xclipboard.c continued
  22. #
  23. if test ! -r _shar_seq_.tmp; then
  24.     echo 'Please unpack part 1 first!'
  25.     exit 1
  26. fi
  27. (read Scheck
  28.  if test "$Scheck" != 2; then
  29.     echo Please unpack part "$Scheck" next!
  30.     exit 1
  31.  else
  32.     exit 0
  33.  fi
  34. ) < _shar_seq_.tmp || exit 1
  35. if test ! -f _shar_wnt_.tmp; then
  36.     echo 'x - still skipping xclipboard/xclipboard.c'
  37. else
  38. echo 'x - continuing file xclipboard/xclipboard.c'
  39. sed 's/^X//' << 'SHAR_EOF' >> 'xclipboard/xclipboard.c' &&
  40. X    system(buf);
  41. X    unlink(tmp);
  42. X
  43. }
  44. X
  45. static void
  46. Save()
  47. {
  48. X    ClipPtr    tempClip, headClip, saveClip;
  49. X    FILE    *fp;
  50. X    long    size;
  51. X    long    count = 0;
  52. X
  53. X    if (!(fp = fopen(saveFile, "w")))
  54. X    return;
  55. X
  56. X    tempClip = currentClip;
  57. X    saveClip = (ClipPtr)0;
  58. X
  59. X    if (!tempClip && TextLength(text))
  60. X    saveClip = tempClip = NewClip(text, (ClipPtr)0);
  61. X    
  62. X    if (tempClip){
  63. X    if (TextLength(text))
  64. X        SaveClip (text, tempClip);
  65. X    while (tempClip){
  66. X        count++;
  67. X        headClip = tempClip;
  68. X        tempClip = tempClip->prev;
  69. X    }
  70. X
  71. X    fwrite(&count, sizeof(long), 1, fp);
  72. X
  73. X    while (headClip){
  74. X        if (headClip->clip){
  75. X        size = strlen(headClip->clip) + 1;
  76. X        fwrite(&size, sizeof(long), 1, fp); 
  77. X        fwrite(headClip->clip, 1, size, fp);
  78. X        }
  79. X        else{
  80. X        size = 0L;
  81. X        fwrite(&size, sizeof(long), 1, fp);
  82. X        }
  83. X        headClip = headClip->next;
  84. X    }
  85. X    fclose(fp);
  86. X    if (saveClip)
  87. X        DeleteClip(text, saveClip);
  88. X    }
  89. X    else{
  90. X    fclose(fp);
  91. X    unlink(saveFile);
  92. X    }
  93. }
  94. X
  95. static void
  96. Load()
  97. {
  98. X    FILE    *fp;
  99. X    ClipPtr    tempClip, oldClip;
  100. X    long    size, count;
  101. X
  102. X    if (!(fp = fopen(saveFile, "r")))
  103. X    return;
  104. X
  105. X    if (currentClip)
  106. X    erase_all();
  107. X
  108. X
  109. X    if (fread(&count, sizeof(long), 1, fp) <= 0)
  110. X    return;
  111. X    
  112. X    oldClip = (ClipPtr)0;
  113. X    currentClip = (ClipPtr)0;
  114. X
  115. X    while (fread(&size, sizeof(long), 1, fp)){
  116. X    tempClip = NewClip(text, oldClip);
  117. X
  118. X    if (tempClip){
  119. X        oldClip = tempClip;
  120. X        if (!currentClip)
  121. X        currentClip = tempClip;
  122. X
  123. X        if (!(--count))
  124. X        currentClip = tempClip;
  125. X
  126. X        tempClip->clip = malloc(size);
  127. X        if (tempClip->clip){
  128. X        tempClip->avail = size;
  129. X        fread(tempClip->clip, 1, size, fp);
  130. X        }
  131. X        else{
  132. X        fseek(fp, size, SEEK_CUR);
  133. X        count--;
  134. X        }
  135. X    }
  136. X    else{
  137. X        fseek(fp, size, 1);
  138. X        count--;
  139. X    }
  140. X    }
  141. X
  142. X    fclose(fp);
  143. X    RestoreClip (text, currentClip);
  144. X    set_button_state();
  145. }
  146. X
  147. static void
  148. NewCurrentClip ()
  149. {
  150. X    NewCurrentClipContents ("", 0);
  151. }
  152. X
  153. NewCurrentClipContents (data, len)
  154. X    char    *data;
  155. X    int        len;
  156. {
  157. X    XawTextBlock textBlock;
  158. X    ClipPtr newCurrent;
  159. X
  160. X    if (!currentClip && TextLength (text))
  161. X    currentClip = NewClip (text, (ClipPtr) 0);
  162. X    if (currentClip)
  163. X    SaveClip (text, currentClip);
  164. X    /* append new clips at the end */
  165. X    while (currentClip && currentClip->next)
  166. X    currentClip = currentClip->next;
  167. X    newCurrent = NewClip (text, currentClip);
  168. X    
  169. X    currentClip = newCurrent;
  170. X
  171. X    textBlock.ptr = data;
  172. X    textBlock.firstPos = 0;
  173. X    textBlock.length = len;
  174. X    textBlock.format = FMT8BIT;
  175. X    if (XawTextReplace(text, 0, TextLength (text), &textBlock))
  176. X    XBell( XtDisplay(text), 0);
  177. X    set_button_state ();
  178. }
  179. X
  180. EraseTextWidget ()
  181. {
  182. X    XawTextBlock block;
  183. X
  184. X    block.ptr = "";
  185. X    block.length = 0;
  186. X    block.firstPos = 0;
  187. X    block.format = FMT8BIT;
  188. X
  189. X    XawTextReplace(text, 0, INFINITY, &block);
  190. X    /* If this fails, too bad. */
  191. }
  192. X
  193. X
  194. XXtActionsRec xclipboard_actions[] = {
  195. X    "NewClip", NewCurrentClip,
  196. X    "NextClip",    NextCurrentClip,
  197. X    "PrevClip", PrevCurrentClip,
  198. X    "DeleteClip", DeleteCurrentClip,
  199. X    "Load", Load,
  200. X    "Save", Save,
  201. X    "PrntClip", PrntClip,
  202. X    "Quit", Quit,
  203. };
  204. X
  205. static XtResource    resources[] = {
  206. {"printer_name", "Printer_name", XtRString, sizeof(char *), XtOffset(TEXT_DATA_PTR, pn_name), XtRString, ""},
  207. };
  208. X
  209. static XrmOptionDescRec table[] = {
  210. X    {"-w",        "*text*wrap",        XrmoptionNoArg,  "Word"},
  211. X    {"-nw",        "*text*wrap",        XrmoptionNoArg,  "Never"},
  212. X    {"-P",    "printer_name",            XrmoptionStickyArg, NULL},
  213. };
  214. X
  215. static void    LoseSelection ();
  216. static void    InsertClipboard ();
  217. static Boolean    ConvertSelection();
  218. X
  219. static void 
  220. InsertClipboard(w, client_data, selection, type, value, length, format)
  221. Widget w;
  222. caddr_t client_data;
  223. Atom *selection, *type;
  224. caddr_t value;
  225. unsigned long *length;
  226. int *format;
  227. {
  228. X    if (*type == 0 /*XT_CONVERT_FAIL*/ || *length == 0) {
  229. X    XBell( XtDisplay(w), 0 );
  230. X    return;
  231. X    }
  232. X    
  233. X    NewCurrentClipContents ((char *) value, *length);
  234. X
  235. X    XtOwnSelection(w, XA_CLIPBOARD(XtDisplay(w)), CurrentTime,
  236. X           ConvertSelection, LoseSelection, NULL);
  237. X
  238. X    XtFree(value);
  239. }
  240. X
  241. static Boolean ConvertSelection(w, selection, target,
  242. X                type, value, length, format)
  243. X    Widget w;
  244. X    Atom *selection, *target, *type;
  245. X    caddr_t *value;
  246. X    unsigned long *length;
  247. X    int *format;
  248. {
  249. X    Display* d = XtDisplay(w);
  250. X    XSelectionRequestEvent* req =
  251. X    XtGetSelectionRequest(w, *selection, (XtRequestId)NULL);
  252. X
  253. X    if (*target == XA_TARGETS(d)) {
  254. X    Atom* targetP;
  255. X    Atom* std_targets;
  256. X    unsigned long std_length;
  257. X    XmuConvertStandardSelection(w, req->time, selection, target, type,
  258. X                  (caddr_t*)&std_targets, &std_length, format);
  259. X    *value = XtMalloc(sizeof(Atom)*(std_length + 5));
  260. X    targetP = *(Atom**)value;
  261. X    *targetP++ = XA_STRING;
  262. X    *targetP++ = XA_TEXT(d);
  263. X    *targetP++ = XA_LENGTH(d);
  264. X    *targetP++ = XA_LIST_LENGTH(d);
  265. X    *targetP++ = XA_CHARACTER_POSITION(d);
  266. X    *length = std_length + (targetP - (*(Atom **) value));
  267. X    bcopy((char*)std_targets, (char*)targetP, sizeof(Atom)*std_length);
  268. X    XtFree((char*)std_targets);
  269. X    *type = XA_ATOM;
  270. X    *format = 32;
  271. X    return True;
  272. X    }
  273. X
  274. X    if (*target == XA_LIST_LENGTH(d) ||
  275. X    *target == XA_LENGTH(d))
  276. X    {
  277. X        long * temp;
  278. X        
  279. X        temp = (long *) XtMalloc(sizeof(long));
  280. X        if (*target == XA_LIST_LENGTH(d))
  281. X            *temp = 1L;
  282. X        else            /* *target == XA_LENGTH(d) */
  283. X            *temp = (long) TextLength (w);
  284. X        
  285. X        *value = (caddr_t) temp;
  286. X        *type = XA_INTEGER;
  287. X        *length = 1L;
  288. X        *format = 32;
  289. X        return True;
  290. X    }
  291. X    
  292. X    if (*target == XA_CHARACTER_POSITION(d))
  293. X    {
  294. X        long * temp;
  295. X        
  296. X        temp = (long *) XtMalloc(2 * sizeof(long));
  297. X        temp[0] = (long) 0;
  298. X        temp[1] = TextLength (w);
  299. X        *value = (caddr_t) temp;
  300. X        *type = XA_SPAN(d);
  301. X        *length = 2L;
  302. X        *format = 32;
  303. X        return True;
  304. X    }
  305. X    
  306. X    if (*target == XA_STRING ||
  307. X      *target == XA_TEXT(d) ||
  308. X      *target == XA_COMPOUND_TEXT(d))
  309. X    {
  310. X    extern char *_XawTextGetSTRING();
  311. X        if (*target == XA_COMPOUND_TEXT(d))
  312. X        *type = *target;
  313. X        else
  314. X        *type = XA_STRING;
  315. X    *length = TextLength (w);
  316. X        *value = _XawTextGetSTRING((TextWidget) w, 0, *length);
  317. X        *format = 8;
  318. X        return True;
  319. X    }
  320. X    
  321. X    if (XmuConvertStandardSelection(w, req->time, selection, target, type,
  322. X                    value, length, format))
  323. X    return True;
  324. X
  325. X    return False;
  326. }
  327. X
  328. static void LoseSelection(w, selection)
  329. X    Widget w;
  330. X    Atom *selection;
  331. {
  332. X    XtGetSelectionValue(w, *selection, XA_STRING, InsertClipboard,
  333. X            NULL, CurrentTime);
  334. }
  335. X
  336. /*ARGSUSED*/
  337. static Boolean RefuseSelection(w, selection, target,
  338. X                   type, value, length, format)
  339. X    Widget w;
  340. X    Atom *selection, *target, *type;
  341. X    caddr_t *value;
  342. X    unsigned long *length;
  343. X    int *format;
  344. {
  345. X    return False;
  346. }
  347. X
  348. /*ARGSUSED*/
  349. static void LoseManager(w, selection)
  350. X    Widget w;
  351. X    Atom *selection;
  352. {
  353. X    XtError("another clipboard has taken over control\n");
  354. }
  355. X
  356. static void
  357. get_savefile(s)
  358. char    *s;
  359. {
  360. X    struct passwd *pw;
  361. X
  362. X    if (!(pw = getpwuid(getuid())))
  363. X    return;
  364. X
  365. X    sprintf(s, "%s/.clipper", pw->pw_dir);
  366. }
  367. X
  368. X
  369. void
  370. main(argc, argv)
  371. int argc;
  372. char **argv;
  373. {
  374. X    Arg args[2];
  375. X    Widget top, parent, quit, delete, new, save, prnt;
  376. X    Atom manager;
  377. X
  378. X    top = XtInitialize( "xclipboard", "XClipboard", table, XtNumber(table),
  379. X              &argc, argv);
  380. X
  381. X    XtGetApplicationResources(top, &text_data, resources, XtNumber(resources), NULL, 0);
  382. X
  383. X    get_savefile(saveFile);
  384. X
  385. X    XtAddActions (xclipboard_actions, XtNumber (xclipboard_actions));
  386. X    /* CLIPBOARD_MANAGER is a non-standard mechanism */
  387. X    manager = XInternAtom(XtDisplay(top), "CLIPBOARD_MANAGER", False);
  388. X    if (XGetSelectionOwner(XtDisplay(top), manager))
  389. X    XtError("another clipboard is already running\n");
  390. X
  391. X    parent = XtCreateManagedWidget("form", formWidgetClass, top, NULL, ZERO);
  392. X    quit = XtCreateManagedWidget("quit", Command, parent, NULL, ZERO);
  393. X    delete = XtCreateManagedWidget("delete", Command, parent, NULL, ZERO);
  394. X    new = XtCreateManagedWidget("new", Command, parent, NULL, ZERO);
  395. X    loadButton = XtCreateManagedWidget("load", Command, parent, NULL, ZERO);
  396. X    save = XtCreateManagedWidget("save", Command, parent, NULL, ZERO);
  397. X    prnt = XtCreateManagedWidget("prnt", Command, parent, NULL, ZERO);
  398. X    nextButton = XtCreateManagedWidget("next", Command, parent, NULL, ZERO);
  399. X    prevButton = XtCreateManagedWidget("prev", Command, parent, NULL, ZERO);
  400. X
  401. X    XtSetArg(args[0], XtNtype, XawAsciiString);
  402. X    XtSetArg(args[1], XtNeditType, XawtextEdit);
  403. X    text = XtCreateManagedWidget( "text", Text, parent, args, TWO);
  404. X
  405. X    set_button_state ();
  406. X
  407. X    XtRealizeWidget(top);
  408. X
  409. X    XtOwnSelection(text, manager, CurrentTime,
  410. X           RefuseSelection, LoseManager, NULL);
  411. X    XtOwnSelection(text, XA_CLIPBOARD(XtDisplay(text)), CurrentTime,
  412. X           ConvertSelection, LoseSelection, NULL);
  413. X
  414. X    XtMainLoop();
  415. }
  416. SHAR_EOF
  417. echo 'File xclipboard/xclipboard.c is complete' &&
  418. chmod 0600 xclipboard/xclipboard.c ||
  419. echo 'restore of xclipboard/xclipboard.c failed'
  420. Wc_c="`wc -c < 'xclipboard/xclipboard.c'`"
  421. test 15078 -eq "$Wc_c" ||
  422.     echo 'xclipboard/xclipboard.c: original size 15078, current size' "$Wc_c"
  423. rm -f _shar_wnt_.tmp
  424. fi
  425. # ============= xclipboard/XClipboard.ad ==============
  426. if test -f 'xclipboard/XClipboard.ad' -a X"$1" != X"-c"; then
  427.     echo 'x - skipping xclipboard/XClipboard.ad (File already exists)'
  428.     rm -f _shar_wnt_.tmp
  429. else
  430. > _shar_wnt_.tmp
  431. echo 'x - extracting xclipboard/XClipboard.ad (Text)'
  432. sed 's/^X//' << 'SHAR_EOF' > 'xclipboard/XClipboard.ad' &&
  433. XXClipboard*quit.label:             Quit
  434. XXClipboard*quit.top:             ChainTop
  435. XXClipboard*quit.bottom:         ChainTop
  436. XXClipboard*quit.left:             ChainLeft
  437. XXClipboard*quit.right:             ChainLeft
  438. XXClipboard*quit.translations:        #override<BtnUp>:Quit() unset()
  439. X
  440. XXClipboard*delete.label:         Delete
  441. XXClipboard*delete.fromHoriz:         quit
  442. XXClipboard*delete.top:             ChainTop
  443. XXClipboard*delete.bottom:        ChainTop
  444. XXClipboard*delete.left:            ChainLeft
  445. XXClipboard*delete.right:        ChainLeft
  446. XXClipboard*delete.translations:        #override<BtnUp>:DeleteClip() unset()
  447. X
  448. XXClipboard*new.label:             New
  449. XXClipboard*new.fromHoriz:         delete
  450. XXClipboard*new.top:             ChainTop
  451. XXClipboard*new.bottom:            ChainTop
  452. XXClipboard*new.left:            ChainLeft
  453. XXClipboard*new.right:            ChainLeft
  454. XXClipboard*new.translations:        #override<BtnUp>:NewClip() unset()
  455. X
  456. XXClipboard*load.label:                   Load
  457. XXClipboard*load.fromHoriz:               new
  458. XXClipboard*load.top:                     ChainTop
  459. XXClipboard*load.bottom:                  ChainTop
  460. XXClipboard*load.left:                    ChainLeft
  461. XXClipboard*load.right:                   ChainLeft
  462. XXClipboard*load.translations:            #override<BtnUp>:Load() unset()
  463. X
  464. XXClipboard*save.label:                   Save
  465. XXClipboard*save.fromHoriz:               load
  466. XXClipboard*save.top:                     ChainTop
  467. XXClipboard*save.bottom:                  ChainTop
  468. XXClipboard*save.left:                    ChainLeft
  469. XXClipboard*save.right:                   ChainLeft
  470. XXClipboard*save.translations:            #override<BtnUp>:Save() unset()
  471. X
  472. XXClipboard*prnt.label:             Print
  473. XXClipboard*prnt.fromHoriz:         save
  474. XXClipboard*prnt.top:             ChainTop
  475. XXClipboard*prnt.bottom:            ChainTop
  476. XXClipboard*prnt.left:            ChainLeft
  477. XXClipboard*prnt.right:            ChainLeft
  478. XXClipboard*prnt.translations:        #override<BtnUp>:PrntClip() unset()
  479. X
  480. XXClipboard*next.label:             Next
  481. XXClipboard*next.fromHoriz:         prnt
  482. XXClipboard*next.top:             ChainTop
  483. XXClipboard*next.bottom:            ChainTop
  484. XXClipboard*next.left:            ChainLeft
  485. XXClipboard*next.right:            ChainLeft
  486. XXClipboard*next.translations:        #override<BtnUp>:NextClip() unset()
  487. X
  488. XXClipboard*prev.label:             Previous
  489. XXClipboard*prev.fromHoriz:         next
  490. XXClipboard*prev.top:             ChainTop
  491. XXClipboard*prev.bottom:            ChainTop
  492. XXClipboard*prev.left:            ChainLeft
  493. XXClipboard*prev.right:            ChainLeft
  494. XXClipboard*prev.translations:        #override<BtnUp>:PrevClip() unset()
  495. X
  496. XXClipboard*text.scrollVertical:      WhenNeeded
  497. XXClipboard*text.scrollHorizontal:      WhenNeeded
  498. XXClipboard*text.autoFill:         on
  499. X
  500. XXClipboard*text.fromVert:         quit
  501. XXClipboard*text.top:             ChainTop
  502. XXClipboard*text.bottom:         ChainBottom
  503. XXClipboard*text.left:             ChainLeft
  504. XXClipboard*text.right:             ChainRight
  505. XXClipboard*text.width:            600
  506. X
  507. XXClipboard.geometry:            600x200
  508. XXClipboard*ShapeStyle:            oval
  509. XXClipboard*printer_name:        ps
  510. SHAR_EOF
  511. chmod 0600 xclipboard/XClipboard.ad ||
  512. echo 'restore of xclipboard/XClipboard.ad failed'
  513. Wc_c="`wc -c < 'xclipboard/XClipboard.ad'`"
  514. test 2710 -eq "$Wc_c" ||
  515.     echo 'xclipboard/XClipboard.ad: original size 2710, current size' "$Wc_c"
  516. rm -f _shar_wnt_.tmp
  517. fi
  518. # ============= xclipboard/Makefile.unx ==============
  519. if test -f 'xclipboard/Makefile.unx' -a X"$1" != X"-c"; then
  520.     echo 'x - skipping xclipboard/Makefile.unx (File already exists)'
  521.     rm -f _shar_wnt_.tmp
  522. else
  523. > _shar_wnt_.tmp
  524. echo 'x - extracting xclipboard/Makefile.unx (Text)'
  525. sed 's/^X//' << 'SHAR_EOF' > 'xclipboard/Makefile.unx' &&
  526. X
  527. INCLUDES = /usr/local/X11/include
  528. X
  529. LIBS = -L/usr/local/X11/lib -lXaw -lXmu -lXt -lXext -lX11     
  530. X
  531. X
  532. all:
  533. X    cc -O -I$(INCLUDES) -o xclipboard xclipboard.c $(LIBS)
  534. SHAR_EOF
  535. chmod 0600 xclipboard/Makefile.unx ||
  536. echo 'restore of xclipboard/Makefile.unx failed'
  537. Wc_c="`wc -c < 'xclipboard/Makefile.unx'`"
  538. test 162 -eq "$Wc_c" ||
  539.     echo 'xclipboard/Makefile.unx: original size 162, current size' "$Wc_c"
  540. rm -f _shar_wnt_.tmp
  541. fi
  542. rm -f _shar_seq_.tmp
  543. echo You have unpacked the last part
  544. exit 0
  545. -- 
  546.  
  547. -Never mistake motion for action.
  548.  
  549. -Ernest Hemingway
  550. -------------------------------------------------------------------------
  551. vafaeia@anubis.network.com        Network Systems Corp.
  552.                     7600 Boone Ave N.
  553.                     Mpls, MN.    55428
  554.  
  555. exit 0 # Just in case...
  556. -- 
  557.   // chris@Sterling.COM           | Send comp.sources.x submissions to:
  558. \X/  Amiga - The only way to fly! |    sources-x@sterling.com
  559.  "It's intuitively obvious to the |
  560.   most casual observer..."        | GCS d+/-- p+ c++ l+ m+ s++/+ g+ w+ t+ r+ x+
  561.