home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / educatio / xcoral16.zip / NEW_WIND.C < prev    next >
C/C++ Source or Header  |  1993-01-15  |  11KB  |  474 lines

  1. /*
  2. ** Copyright 1989, 1992 by Lionel Fournigault
  3. **
  4. ** Permission to use, copy, and distribute for non-commercial purposes,
  5. ** is hereby granted without fee, providing that the above copyright
  6. ** notice appear in all copies and that both the copyright notice and this
  7. ** permission notice appear in supporting documentation.
  8. ** The software may be modified for your own purposes, but modified versions
  9. ** may not be distributed.
  10. ** This software is provided "as is" without any expressed or implied warranty.
  11. **
  12. **
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <X11/Xlib.h>
  17. #include <X11/Xutil.h>
  18. #include <string.h>
  19.  
  20. #include "options.h"
  21. #include "xcoral.h"
  22. #include "browser.h"
  23. #include "logo.bm"
  24. #include "flist.h"
  25.  
  26. extern Trans st_initial;
  27.  
  28. static void SetProp ();
  29. static int add_window ();
  30.  
  31. /*
  32. **    Function name : CreateWindow
  33. **
  34. **    Description : Fabrication d'une fenetre d'edition.
  35. **    Input : 
  36. **    Ouput : La structure associee.
  37. */
  38. EdWin *CreateWindow ()
  39. {
  40.     EdWin        *edtmp;
  41.     Window        root;
  42.     int         screen;
  43.     char         *malloc ();
  44.     XYMenu         *MakeMenus (); 
  45.     Text        *MakeTextWindow ();
  46.     MWin        *MakeControlPanel ();
  47.     static         Atom wm_delete_window;
  48.  
  49.     screen = DefaultScreen ( dpy );
  50.     root = RootWindow ( dpy, screen );
  51.     
  52.     edtmp = ( EdWin * ) malloc ( sizeof ( EdWin ));
  53.  
  54.     if ( add_window ( edtmp ) < NULL ) {
  55.               if ( edtmp != 0 )
  56.             (void) free ( (char *) edtmp );
  57.         return NULL;
  58.     }
  59.     
  60.     edtmp -> w_frame = XCreateSimpleWindow (dpy, root, 0, 0,
  61.         GetOpGeo ( OP_WIDTH ), GetOpGeo( OP_HEIGHT ), GetOpBW (),
  62.         GetOpBD (), GetOpColor ( OP_MENU_BG ));
  63.  
  64.     (void) SetProp ( edtmp -> w_frame ); 
  65.  
  66.     XSelectInput ( dpy, edtmp->w_frame , ExposureMask |
  67.         ButtonPressMask | ButtonReleaseMask | EnterWindowMask
  68.         | LeaveWindowMask | KeyPressMask | StructureNotifyMask );
  69.  
  70.     edtmp -> menu = MakeMenus ( dpy, edtmp->w_frame, 
  71.         menu_names, item_names, func_names );
  72.  
  73.     edtmp -> mwin = MakeControlPanel ( edtmp->w_frame );
  74.  
  75.     edtmp -> scroll = MakeScroll ( dpy, edtmp->w_frame,
  76.         GetOpGeo ( OP_WIDTH ) - GetScrollWidth () - W_SPACE,
  77.         HeightMenuBar ( edtmp -> menu -> font ) + 1 + W_SPACE );
  78.  
  79.     SetScrollFont ( edtmp -> scroll, GetOpFont ( OP_TEXT_FONT) );
  80.  
  81.     edtmp -> text = MakeTextWindow ( dpy, edtmp -> w_frame, 
  82.             W_SPACE, HeightMenuBar ( edtmp -> menu -> font ) + 1 + W_SPACE );
  83.  
  84.     edtmp -> buf = GetBuffer ( (unsigned) SIZEOF_BUFFER );
  85.  
  86.     edtmp -> text -> buf = edtmp -> buf;
  87.     edtmp -> text -> swin = edtmp -> scroll;
  88.     edtmp -> text -> mwin = edtmp -> mwin;
  89.     edtmp -> scroll -> text = ( char * ) edtmp -> text;
  90.  
  91.     SetHiddenWindow ( edtmp -> menu, edtmp -> text -> window, edtmp -> text -> Cgc );
  92.  
  93.     if ( XSaveContext ( dpy, edtmp->w_frame, EdContext, edtmp ) != NULL )
  94.         ( void ) printf ("create_env XSaveContext Error\n" );
  95.  
  96.     edtmp -> stat = (ST *) &st_initial;
  97.     edtmp -> width = edtmp -> height = 0;
  98.     (void) strcpy ( edtmp -> text -> filename, "NoName" ); 
  99.  
  100.     wm_delete_window = XInternAtom( dpy, "WM_DELETE_WINDOW", False);
  101.     (void) XSetWMProtocols ( dpy, edtmp -> w_frame, &wm_delete_window, 1);
  102.  
  103.     return edtmp;
  104. }
  105.  
  106. /*
  107. **    Function name : ConfigWindow
  108. **
  109. **    Description : Positionne la geometrie des fenetres.
  110. **    Input : Le contexte d'edition, largeur er hauteur.
  111. **    Ouput :
  112. */
  113. ConfigWindow ( e, width, height )
  114.     EdWin   *e;
  115.     register width, height;
  116. {
  117.        register int hbar, hmess, wscroll, i, x;
  118.  
  119.        e -> width = width;
  120.        e -> height = height;
  121.        hbar = HeightMenuBar ( e -> menu -> font );
  122.        hmess = HeightOfMess ();
  123.        wscroll = GetScrollWidth ();
  124.  
  125. #ifdef DEBUG
  126.        fprintf ( stderr, "ConfigWindow width = %d, height = %d\n",
  127.         width, height );
  128.        fprintf ( stderr, "ConfigWindow hbar = %d, hmess = %d, wscroll = %d\n",
  129.         hbar, hmess, wscroll );
  130. #endif
  131.  
  132.        x = height - ( hbar + hmess + 2 ) - ( 2 * W_SPACE );
  133.        x -= ( 2 * MARGE );
  134.     i = x / e -> text -> font_height;
  135.  
  136.        SetScrollLinePage ( e -> scroll, i );
  137.        SetMenuBarWidth ( e -> menu, width );
  138.        ShowControlPanel ( e -> mwin, width, height );
  139.        ShowWindowText ( dpy, e -> text, width - wscroll - 1 - ( 2 * W_SPACE ) ,
  140.                height - hbar - hmess - 2 - ( 2 * W_SPACE) );
  141.  
  142.            if ( DoesSaveUnders ( DefaultScreenOfDisplay ( dpy ) ) != True )
  143.         SetMenuPixmap ( dpy, e -> menu, width - wscroll - 1 );
  144.  
  145.        i = e -> text -> no_current_line - e -> text -> n1 - 1;
  146. #ifdef DEBUG
  147.        fprintf ( stderr, "no_current = %d n1 = %d\n", 
  148.         e -> text -> no_current_line, e -> text -> n1 );
  149. #endif
  150.        RefreshScroll ( dpy, e -> scroll, width - W_SPACE,
  151.         height - hbar - hmess - 2 - ( 2 * W_SPACE) , i );
  152. }
  153.  
  154.  
  155. /*
  156. **    Function name : SetProp
  157. **
  158. **    Description : Initialisation des proprietes pour une fenetre
  159. **        d'edition.
  160. **    Input : La fenetre
  161. **    Ouput :
  162. */
  163. static void SetProp ( w )
  164.     Window     w;
  165. {
  166.     XSizeHints sizehints;
  167.     XWMHints wm_hints;
  168.     
  169.     wm_hints.icon_pixmap = XCreatePixmapFromBitmapData ( dpy, w, icon_bits,
  170.             icon_width, icon_height,
  171.             WhitePixel ( dpy, DefaultScreen ( dpy )),
  172.             BlackPixel ( dpy, DefaultScreen ( dpy )),
  173.             DefaultDepth ( dpy, DefaultScreen ( dpy )));
  174.     
  175.     sizehints.flags = PMinSize | PMaxSize | PPosition | PSize | USSize;
  176.  
  177.     sizehints.height = GetOpGeo ( OP_HEIGHT );
  178.     sizehints.width = GetOpGeo ( OP_WIDTH );
  179.     sizehints.x = GetOpGeo ( OP_X );
  180.     sizehints.y = GetOpGeo ( OP_Y );
  181.  
  182.     sizehints.min_width = DisplayWidth ( dpy,DefaultScreen ( dpy )) / 2; 
  183.     sizehints.min_height = DisplayHeight ( dpy,DefaultScreen ( dpy )) / 2; 
  184.     sizehints.max_width = (DisplayWidth (dpy,DefaultScreen (dpy))*5)/6;
  185.     sizehints.max_height = (DisplayHeight (dpy,DefaultScreen (dpy))*5)/6; 
  186.  
  187.     wm_hints.flags = InputHint | StateHint | IconPixmapHint;
  188.     wm_hints.input = True;
  189.     wm_hints.initial_state = NormalState;
  190.  
  191.     XSetWMProperties ( dpy, w, 0, 0, 0, 0, &sizehints, &wm_hints, 0 );
  192. }
  193.  
  194.  
  195. /*
  196. **    Function name : add_window
  197. **
  198. **    Description : Ajoute une fenetre d'edition dans
  199. **        la table principale.
  200. **    Input : Le contexte d'edition.
  201. **    Ouput : 0 si OK -1 sinon.
  202. */
  203. static int add_window ( e )
  204.     EdWin    *e;
  205. {
  206.     register EdWin    **t;
  207.  
  208.     for ( t = TWin; t < TWin + MAXWIN; t++ ) {
  209.         if ( *t == 0 ) {
  210.              *t  = e;
  211.              break;
  212.         }
  213.     }
  214.     if ( t == TWin + MAXWIN )
  215.         return (- 1);
  216.     else
  217.         return 0;
  218. }
  219.  
  220.  
  221. /*
  222. **    Function name : DisplayOpenFiles
  223. **
  224. **    Description : Affiche la liste des fichiers ouverts.
  225. **    Input : Le text courant.
  226. **    Ouput :
  227. */
  228. void DisplayOpenFiles ( text )
  229.     Text *text;
  230. {
  231.     register EdWin    **t;
  232.     register char *str, *tmp;
  233.     register int n;
  234.  
  235.        if ( GetVisibility ( dpy, text ) == False )
  236.         return;
  237.  
  238.     tmp = (char *) GetStringFromSelect ( text -> window, OPENFILES );
  239.     if ( tmp != 0 ) {
  240.         /* str [2] = 0; */
  241.         str = strrchr ( tmp, '#' );
  242.         str++;
  243.         str [2] = 0;
  244.         if ( (n = atoi (str)) == 0 ) {
  245.             DisplayMessage ( text -> mwin, "Abort" );
  246.             return;
  247.         }
  248.         t = TWin; 
  249.         t += (n-1);
  250.         XMapRaised ( dpy,  (*t) -> w_frame );
  251.               if ( tmp != 0 )
  252.                   (void) free ( tmp ); 
  253.               }
  254.     else
  255.         DisplayMessage ( text -> mwin, "Abort" );
  256. }
  257.  
  258.  
  259. /*
  260. **    Function name : LoadFileNames
  261. **
  262. **    Description : Construit la liste de fichiers ouverts
  263. **        en parcourant la table principale.
  264. **    Input : Un buffer pour stocker la liste.
  265. **    Ouput :
  266. */
  267. void LoadFileNames ( buf )
  268.     Buf *buf;
  269. {
  270.     register EdWin    **t;
  271.     register int i = 1;
  272.     register int j = 1;
  273.     char tmp [256];
  274.     register char *name;
  275.  
  276.     for ( t = TWin; t < TWin + MAXWIN; t++ ) {
  277.         if ( *t != NULL ) {
  278.             bzero ( tmp, 256 );
  279.             if ( (name = strrchr ( (*t) -> text -> filename, '/' )) != 0 )
  280.                          if ( (*t) -> text -> modif == True ) 
  281.                     (void) sprintf ( tmp,
  282.                         " %d  : buffer #%d  **\t%s\n", i, j,(char *) (name+1) );
  283.                 else
  284.                     (void) sprintf ( tmp,
  285.                         " %d  : buffer #%d  \t%s\n", i, j,(char *) (name+1) );
  286.             else 
  287.                 (void) sprintf ( tmp,
  288.                     " %d  : buffer #%d ...\n", i, j );
  289.             InsertNchar ( buf, tmp, strlen ( tmp ));
  290.             i++;
  291.         }
  292.         j++;
  293.     }
  294. }
  295.  
  296.  
  297. /*
  298. **    Function name : IsAlreadyLoad
  299. **
  300. **    Description : Pour eviter les ouvertures multiples
  301. **        de fichiers
  302. **    Input : Le nom du fichier.
  303. **    Ouput : Le nombre de buffer ouvert sur le fichier.
  304. */
  305. int IsAlreadyLoad ( s, text )
  306.     register char *s;
  307.     Text **text;
  308. {
  309.     register EdWin    **t;
  310.        register int n = 0;
  311.  
  312.     for ( t = TWin; t < TWin + MAXWIN; t++ ) {
  313.         if ( *t == 0 )
  314.              continue;
  315.         if ( strcmp ( s, (*t) -> text -> filename ) == 0 ) {
  316.             n++;
  317.                         *text = (*t) -> text;
  318.         }
  319.     }
  320.        return n;
  321. }
  322.  
  323.  
  324. /*
  325. **    Function name : NewWindow
  326. **
  327. **    Description : Construction d'une fenetre d'edition en
  328. **        tenant compte des proprietes de la fenetre parent.
  329. **    Input : Le text courant.
  330. **    Ouput :
  331. */
  332. void NewWindow ( text )
  333.     Text *text;
  334. {
  335.     EdWin     *ew;
  336.     ew = CreateWindow (); 
  337.  
  338.     if ( ew == NULL ) {
  339.         (void) fprintf ( stderr, "Too many open window\n" );
  340.         return;
  341.     }
  342.     ew -> text -> mode = text -> mode;
  343.     ew -> text -> mwin -> mode = text -> mode;
  344.     SetBrowserMode ( text -> mode );
  345.     
  346.     (void) strcpy ( ew -> text -> current_dir,
  347.             text -> current_dir );
  348. }
  349.  
  350.  
  351. /*
  352. **    Function name : DeleteWindow
  353. **
  354. **    Description : Detruit une fenetre d'edition.
  355. **    Input : Le text courant.
  356. **    Ouput : 0 si OK -1 sinon.
  357. */
  358. int DeleteWindow ( text )
  359. Text *text;
  360. {
  361.     register char *s;
  362.     char c = '\007';
  363.     register EdWin    **t;
  364.    
  365.        if ( GetVisibility ( dpy, text ) == False )
  366.         return -1;
  367.  
  368.     if ( GetModif ( text ) == True ) {
  369.               if ( SaveCurrentBuffer ( text, F_MENU ) != True )
  370.                   return -1;
  371.     }
  372.     if ( IsLastWindow ( 1 ) == True ) {
  373.         s = ( char * ) GetStringFromDialogBox ( text -> window, 
  374.             "Last window. Quit [y/n] : " );
  375.         if ( (s == NULL) || (strncmp(s,"y",1) == NULL) ) {
  376.             DisplayMessage ( text -> mwin, "Quit" );
  377.         }
  378.         else if ( (strncmp(s,"n",1 ) == NULL) || (strncmp(s,&c,1)==NULL) ) {
  379.             DisplayMessage ( text -> mwin, "Abort" );
  380.             return -1;
  381.         }
  382.     }
  383.  
  384.        XUnmapWindow ( dpy, edwin -> w_frame ); 
  385.  
  386.      DeleteMenu ( dpy, edwin -> menu );
  387.     DeleteBuffer ( edwin -> buf );
  388.     DeleteText ( dpy, edwin -> text );
  389.     DeleteControlPanel ( edwin -> mwin );
  390.     DeleteScroll ( dpy, edwin -> scroll );
  391.     XSync ( dpy, False );
  392.  
  393.     for ( t = TWin; t < TWin + MAXWIN; t++ ) {
  394.         if ( *t == edwin ) {
  395.                      if ( edwin != 0 )
  396.                 (void) free ( (char *) edwin ); 
  397.             *t = 0;
  398.             break;
  399.         }
  400.     }
  401.     return 0;
  402. }
  403.  
  404.  
  405. /*
  406. **    Function name : IsLastWindow
  407. **
  408. **    Description : Comme son l'indique.
  409. **    Input : Le nombre de fenetre.
  410. **    Ouput : Vrai ou faux.
  411. */
  412. int IsLastWindow ( nb )
  413.     register int nb;
  414. {
  415.     register EdWin    **t;
  416.     register int     n;
  417.     
  418.     n = 0;
  419.     for ( t = TWin; t < TWin + MAXWIN; t++ ) {
  420.         if ( *t != 0 )
  421.             n++;
  422.     }
  423.     if ( n != 0 ) {
  424.         if ( nb == n )
  425.             return True;
  426.         else
  427.             return False;
  428.     }
  429.     else
  430.         return True;
  431. }
  432.  
  433.  
  434. /*
  435. **    Function name : Version
  436. **
  437. **    Description : Affiche la version courante de xcoral.
  438. **    Input : Le text courant
  439. **    Ouput :
  440. */
  441. void Version ( text )
  442. Text *text;
  443. {
  444.        char tmp [64];
  445.    
  446.     if ( GetVisibility ( dpy, text ) != True )
  447.         return;
  448.    
  449.        (void) sprintf ( tmp, "Message : %s\n", CURRENT_VERSION );
  450.     (void) GetStringFromDialogBox ( text -> window,  tmp );
  451. }
  452.  
  453.  
  454. /*
  455. **    Function name : Help
  456. **
  457. **    Description : En attendant d'avoir un help serieux.
  458. **    Input : Le text courant. 
  459. **    Ouput :
  460. */
  461. void Help ( text )
  462. Text *text;
  463. {
  464.     if ( GetVisibility ( dpy, text ) != True )
  465.         return;
  466.     (void) GetStringFromDialogBox ( text -> window, 
  467.         "Message :  Help you yourself" );
  468. }
  469.  
  470.  
  471.  
  472.  
  473.  
  474.