home *** CD-ROM | disk | FTP | other *** search
/ M.u.C.S. Disc 2000 / MUCS2000.iso / anwend / qed453 / src / dd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-02  |  6.5 KB  |  336 lines

  1. #include <limits.h>
  2.  
  3. #include "global.h"
  4. #include "comm.h"
  5. #include "dd.h"
  6. #include "edit.h"
  7. #include "file.h"
  8. #include "find.h"
  9. #include "icon.h"
  10. #include "memory.h"
  11. #include "projekt.h"
  12. #include "rsc.h"
  13. #include "text.h"
  14. #include "window.h"
  15.  
  16.  
  17. /* exportierte Variablen *****************************************************/
  18. PATH    drag_filename;        /* Dateiname/Pfad wurde gedragged */
  19. char    *drag_data;            /* Daten wurden gedragged */
  20. long    drag_data_size;    /* Die Länge der Daten */
  21.  
  22.  
  23. /* DD Empfang ****************************************************************/
  24.  
  25. static int parse_ARGS(char *str)
  26. {
  27.     int        cnt = 1;
  28.     char     *c = str;
  29.     bool    in_quote = FALSE;
  30.  
  31.     while (*c)
  32.     {
  33.         switch (*c)
  34.         {
  35.             case ' ' :
  36.                 if (!in_quote)
  37.                 {
  38.                     *c = '\0';
  39.                     cnt++;
  40.                 }
  41.                 break;
  42.             case '\'' :
  43.                 strcpy(c, c + 1);
  44.                 if (!in_quote)
  45.                     in_quote = TRUE;
  46.                 else
  47.                 {
  48.                     if (*c != '\'')
  49.                     {
  50.                         in_quote = FALSE;
  51.                         *c = 0;
  52.                         if (c[1])
  53.                             cnt++;
  54.                     }
  55.                 }
  56.                 break;
  57.             default:
  58.                 break;
  59.         } /* switch */
  60.         c += 1;
  61.     } /* while */
  62.     return cnt;
  63. }
  64.  
  65.  
  66. static int rel_path(char *path, bool cs, char *sub_path)
  67. {
  68.     int    l;
  69.     char    p1[256];
  70.     
  71.     split_filename(path, p1, NULL);
  72.     l = (int)strlen(p1);
  73.     if (cs)
  74.     {
  75.         if (strncmp(p1, sub_path, l) == 0)
  76.             return l;
  77.     }
  78.     else
  79.     {
  80.         if (strnicmp(p1, sub_path, l) == 0)
  81.             return l;
  82.     }
  83.     return 0;
  84. }
  85.  
  86. /* Werte vom Popup */
  87. #define DRAG_PATHNAME    0
  88. #define DRAG_PATH            1
  89. #define DRAG_NAME            2
  90. #define DRAG_RELPATH        3
  91. #define DRAG_INHALT        4
  92.  
  93. static void insert(WINDOWP w, int mode, bool shift, char *filename)
  94. {
  95.     int    icon;
  96.  
  97.     make_normalpath(filename);
  98.  
  99.     if (w->class == CLASS_PROJEKT)
  100.     {
  101.         strcpy(drag_filename, filename);
  102.         if (filename[strlen(filename) - 1] == '\\')            /* Pfad */
  103.             icon_drag(w->handle, DRAGDROP_PATH);                /*  -> Datei suchen */
  104.         else                                                                /* Datei */
  105.             icon_drag(w->handle, DRAGDROP_FILE);                /*  -> einfügen */
  106.     }
  107.     else
  108.     {
  109.         if (filename[strlen(filename) - 1] == '\\')            /* Pfad */
  110.             mode = DRAG_PATHNAME;
  111.  
  112.         if (mode == DRAGDROP_PATH)
  113.         {
  114.             if (!shift && filematch(filename, "*.QPJ", -1))
  115.                 icon = load_projekt(filename);
  116.             else
  117.                 icon = load_edit(filename, FALSE);
  118.             if (icon > 0)
  119.                 send_dhst(filename);
  120.         }
  121.         else
  122.         {
  123.             TEXTP    t_ptr;
  124.             int    i;
  125.  
  126.             switch (mode)
  127.             {
  128.                 case DRAG_PATHNAME :
  129.                     strcpy(drag_filename, filename);
  130.                     icon_drag(w->handle, DRAGDROP_PATH);
  131.                     break;
  132.     
  133.                 case DRAG_PATH :
  134.                     split_filename(filename, drag_filename, NULL);
  135.                     icon_drag(w->handle, DRAGDROP_PATH);
  136.                     break;
  137.                     
  138.                 case DRAG_NAME :
  139.                     split_filename(filename, NULL, drag_filename);
  140.                     icon_drag(w->handle, DRAGDROP_PATH);
  141.                     break;
  142.     
  143.                 case DRAG_RELPATH :
  144.                     t_ptr = get_text(w->handle);
  145.                     if (t_ptr && !t_ptr->namenlos)
  146.                     {
  147.                         i = rel_path(t_ptr->filename, (t_ptr->filesys == FULL_CASE), filename);
  148.                         strcpy(drag_filename, filename + i);
  149.                         icon_drag(w->handle, DRAGDROP_PATH);
  150.                     }
  151.                     break;
  152.                     
  153.                 case DRAG_INHALT :
  154.                     strcpy(drag_filename, filename);
  155.                     icon_drag(w->handle, DRAGDROP_FILE);
  156.                     break;
  157.  
  158.                 default:
  159.                     debug("dd.c,insert: unbekanter mode %d\n", mode);
  160.                     break;
  161.             }
  162.         }
  163.     }
  164. }
  165.  
  166.  
  167. static void parse(char *cmdline, WINDOWP w, int kstate)
  168. {
  169.     int    comps = parse_ARGS(cmdline);
  170.     char    *c = cmdline;
  171.     int    i, mode = DRAGDROP_PATH;
  172.     bool    shift;
  173.     
  174.     shift = (kstate & (K_LSHIFT|K_RSHIFT));
  175.     
  176.     if (w->class == CLASS_EDIT && (kstate & K_ALT))
  177.     {
  178.         i = handle_popup(NULL, 0, popups, DRAGPOP, POP_OPEN);
  179.         switch (i)
  180.         {
  181.             case DPALL :
  182.                 mode = DRAG_PATHNAME;
  183.                 break;
  184.  
  185.             case DPPATH :
  186.                 mode = DRAG_PATH;
  187.                 break;
  188.                 
  189.             case DPNAME :
  190.                 mode = DRAG_NAME;
  191.                 break;
  192.  
  193.             case DPRELPATH :
  194.                 mode = DRAG_RELPATH;
  195.                 break;
  196.  
  197.             case DPINHALT :
  198.                 mode = DRAG_INHALT;
  199.                 break;
  200.  
  201.             default:
  202.                 Bconout(2, 7);
  203.                 return;
  204.         }
  205.     }
  206.  
  207.     drag_data_size = comps;            /* Anzahl der ARGS merken */
  208.     while (comps - 1)
  209.     {
  210.         insert(w, mode, shift, c);
  211.         c += strlen(c) +1;
  212.         comps--;
  213.     }
  214.     insert(w, mode, shift, c);
  215. }
  216.  
  217. void    handle_dd(int *msg)
  218. {
  219.     WINDOWP     w_dest = get_window(msg[3]);
  220.     int        fd;
  221.     long        size;
  222.     char         ext[5],
  223.                 fname[PATH_MAX],
  224.                 ourexts[DD_EXTSIZE] = "ARGS.TXT";
  225.  
  226.     if (w_dest != NULL)
  227.     {
  228.         fd = (int)dd_open(msg[7], ourexts);
  229.         if (fd < 0)
  230.             return ;
  231.  
  232.         do
  233.         {
  234.             if (!dd_rtry(fd, fname, ext, &size))
  235.             {
  236.                 dd_close(fd);
  237.                 return;
  238.             }
  239.             if (!strncmp(ext, "ARGS", 4))
  240.             {
  241.                 char    *cmdline = malloc(size +1);
  242.  
  243.                 if (!cmdline)
  244.                 {
  245.                     dd_reply(fd, DD_LEN);
  246.                     continue;
  247.                 }
  248.                 dd_reply(fd, DD_OK);
  249.                 Fread((int) fd, size, cmdline);
  250.                 dd_close(fd);
  251.                 cmdline[size] = 0;
  252.                 parse(cmdline, w_dest, msg[6]);
  253.                 free(cmdline);
  254.                 drag_data_size = 0;
  255.                 return ;
  256.             }
  257.             if (strncmp(ext, ".TXT", 4) == 0 )
  258.             {
  259. #if 0
  260.                 drag_data = malloc(size + 1);
  261.                 /* ^^ wird nach dem Einfügen in edit.c wieder freigegeben! */
  262.                 drag_data_size = size;
  263.                 if (drag_data == NULL)
  264.                 {
  265.                     dd_reply(fd, DD_LEN);
  266.                     continue;
  267.                 }
  268.                 dd_reply(fd, DD_OK);
  269.                 Fread((int) fd, drag_data_size, drag_data);
  270.                 dd_close(fd);
  271.                 drag_data[drag_data_size] = 0;
  272. #else
  273.                 RING    t;
  274.                 
  275.                 init_textring(&t);
  276.                 dd_reply(fd, DD_OK);
  277.                 load_from_fd(fd, "", &t, FALSE, NULL, size);            
  278.                 dd_close(fd);
  279.                 drag_data_size = DDS_RINGP;
  280.                 drag_data = (char *)&t;
  281. /*                kill_textring(&t); wird in edit.c wieder freigegeben! */
  282. #endif
  283.                 icon_drag(w_dest->handle, DRAGDROP_DATA);
  284.                 return ;
  285.             }
  286.         }
  287.         while (dd_reply(fd, DD_EXT));
  288.     }
  289. }
  290.  
  291.  
  292. void    handle_avdd(int win_handle, int kstate, char *arg)
  293. {
  294.     WINDOWP     w_dest = get_window(win_handle);
  295.     char        *cmdline;
  296.     
  297.     if (w_dest != NULL)
  298.     {
  299.         cmdline = (char *) malloc(strlen(arg));
  300.         strcpy(cmdline, arg);
  301.         parse(cmdline, w_dest, kstate);
  302.         free(cmdline);
  303.     }
  304. }
  305.  
  306.  
  307. /* DD Senden *****************************************************************/
  308.  
  309. void send_dd(int win_id, int m_x, int m_y, int kstate, RINGP t)
  310. {
  311.     int    i, d;
  312.     
  313.     /* gibts WF_OWNER? */
  314.     if ((appl_xgetinfo(11, &i, &d, &d, &d)) && (i & 16)) 
  315.     {
  316.         int    pipe, app_id;
  317.         char    ext[33];
  318.         
  319.         wind_get(win_id, WF_OWNER, &app_id, &d, &d, &d);
  320.     
  321.         pipe = dd_create(app_id, win_id, m_x, m_y, kstate, ext);
  322.         if (pipe > 0)
  323.         {
  324.             long    len;
  325.     
  326.             len = textring_bytes(t);
  327.             d = dd_stry(pipe, ".TXT", "Text", len);
  328.             if (d == DD_OK)
  329.                 save_to_fd(pipe, "", t, FALSE);
  330.             dd_close(pipe);
  331.         }
  332.     }
  333.     else
  334.         note(1, 0, WFOWNERR);
  335. }
  336.