Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.88 KB | None | 0 0
  1. #include "common.h"
  2. #include "ui.h"
  3. #include "mouse.h"
  4. #include "han.h"
  5. #include "file.h"
  6. #include "edit.h"
  7.  
  8. void DrawTextBox(int x1, int y1, int x2, int y2)
  9. /* Draw an text box similar to Win9x */
  10. {
  11.     setwritemode(0);
  12.     setfillstyle(SOLID_FILL, 15);
  13.     bar(x1,y1,x2,y2);
  14.     SetColor(0);
  15.     line(x1-1,y1-1,x1-1,y2+1);
  16.     line(x1-1,y1-1,x2+1,y1-1);
  17.     SetColor(8);
  18.     line(x1-2,y1-2,x1-2,y2+2);
  19.     line(x1-2,y1-2,x2+2,y1-2);
  20.     SetColor(7);
  21.     line(x1-1,y2+1,x2+1,y2+1);
  22.     line(x2+1,y1-1,x2+1,y2+1);
  23.     SetColor(15);
  24.     line(x1-2,y2+2,x2+2,y2+2);
  25.     line(x2+2,y1-2,x2+2,y2+2);
  26. }
  27.  
  28. void DrawButton(void *n, int x1, int y1, int x2, int y2, char *title)
  29. /* Draw a button on a specific place */
  30. {
  31.     setfillstyle(SOLID_FILL, 7);
  32.     setwritemode(0);
  33.     bar(x1, y1, x2, y2);
  34.     /* Main background */
  35.    
  36.     SetColor(7);
  37.     rectangle(x1 + 2, y1 + 2, x2 - 2, y2 - 2);
  38.     line(x1, y1, x2, y1);
  39.     line(x1, y1, x1, y2);
  40.     SetColor(8);
  41.     line(x2 - 1, y1 + 1, x2 - 1, y2 - 1);
  42.     line(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
  43.     SetColor(0);
  44.     line(x2, y1, x2, y2);
  45.     line(x1, y2, x2, y2);
  46.     SetColor(15);
  47.     line(x1 + 1, y1 + 1, x2 - 1, y1 + 1);
  48.     line(x1 + 1, y1 + 1, x1 + 1, y2 - 1);
  49.     /* Draw four borders */
  50.    
  51.     OutTextxy(n, x1 + 4, y1 + 4, title, 0);
  52.     /* Output the title text */
  53. }
  54.  
  55. void DrawWindow(void *n, int x1, int y1, int x2, int y2, char *title)
  56. /* Draw an interface similar to Win9x */
  57. {
  58.     State *s;
  59.     int i, j, k, dx, period, active = 1;
  60.     setfillstyle(SOLID_FILL, 7);
  61.     setwritemode(0);
  62.    
  63.     s = (State *)n;
  64.     bar(x1 + 3, y1 + 21, x2 - 3, y2 - 3);
  65.     /*for(i = y1 + 21; i <= y2 - 3; i ++)
  66.         if(s -> edity1 <= i && i <= s -> edity2)
  67.         {
  68.             line(x1 + 3, i, s -> editx1 - 3, i);
  69.             line(s -> editx2 + 4, i, x2 - 3, i);
  70.         }
  71.         else
  72.             line(x1 + 3, i, x2 - 3, i);*/
  73.     /* Main background, same as button */
  74.    
  75.     SetColor(7);
  76.     rectangle(x1 + 2, y1 + 2, x2 - 2, y2 - 2);
  77.     line(x1, y1, x2, y1);
  78.     line(x1, y1, x1, y2);
  79.     SetColor(8);
  80.     line(x2 - 1, y1 + 1, x2 - 1, y2 - 1);
  81.     line(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
  82.     SetColor(0);
  83.     line(x2, y1, x2, y2);
  84.     line(x1, y2, x2, y2);
  85.     SetColor(15);
  86.     line(x1 + 1, y1 + 1, x2 - 1, y1 + 1);
  87.     line(x1 + 1, y1 + 1, x1 + 1, y2 - 1);
  88.    
  89.     if(s -> ischildwindow && (x1 != s -> childx1 || x2 != s -> childx2))
  90.     /* Inactive window, draw the title bar in gray */
  91.     {
  92.         active = 0;
  93.         setfillstyle(SOLID_FILL, 8);
  94.         bar(x1 + 3, y1 + 3, x2 - 3, y1 + 20);
  95.     }
  96.    
  97.     /* Use six colors to make the title bar background shading */
  98.     dx = (x2 - x1 - 6) / 6;
  99.     /* Divide the width to 6 parts */
  100.    
  101.     for(i = 0; active && i < 6; i ++)
  102.     /* Use 6 different colors to draw in their parts */
  103.     {
  104.         setfillstyle(SOLID_FILL, 9 + i);
  105.         /* Using color 9 - 14 */
  106.        
  107.         bar(x1 + 3 + dx * i, y1 + 3, x1 + 3 + dx * (i + 1), y1 + 20);
  108.     }
  109.     bar(x1 + 3 + dx * 6, y1 + 3, x2 - 3, y1 + 20);
  110.    
  111.     for(i = 0; active && i < 6; i ++)
  112.     /* Use random pixels to blurring the boundaries, so they look better */
  113.     {
  114.         if(i > 0)
  115.         /* Use right color to exhaust dyeing the left parts */
  116.             for(j = x1 + dx * i + 2; j > x1 + dx * i - dx * 2 / 3; j -= 1)
  117.             {
  118.                 period = rand() % (x1 + dx * i + 3 - j) + 1;
  119.                 /* Set the probability depending on how far between the pixel
  120.                  * and the boundary */
  121.                  
  122.                 for(k = 3 + y1 + period; k < y1 + 20; k += period)
  123.                 {
  124.                     period = rand() % ((x1 + dx * i - j + 3) / 2 + 1) + 1;
  125.                     putpixel(j, k, 9 + i);
  126.                 }
  127.             }
  128.        
  129.         if(i < 5)
  130.         /* Use left color to exhaust dyeing the right parts */
  131.             for(j = x1 + dx * (i + 1); j < x1 + dx * (i + 1) + dx * 2 / 3; j ++)
  132.             {
  133.                 period = rand() % (j - (x1 + dx * (i + 1)) + 1) + 1;
  134.                 for(k = 3 + y1 + period; k < y1 + 20; k += period)
  135.                 {
  136.                     period = rand() % ((j - (x1 + dx * (i + 1))) / 2 + 1) + 1;
  137.                     putpixel(j, k, 9 + i);
  138.                 }
  139.             }
  140.     }
  141.     /* Draw four borders, same as button, too */
  142.    
  143.     setfillstyle(SOLID_FILL, 7);
  144.     bar(x2 - 22, y1 + 5, x2 - 7, y1 + 17);
  145.     SetColor(15);
  146.     line(x2 - 22, y1 + 5, x2 - 22, y1 + 16);
  147.     line(x2 - 22, y1 + 5, x2 - 8, y1 + 5);
  148.     SetColor(8);
  149.     line(x2 - 8, y1 + 6, x2 - 8, y1 + 16);
  150.     line(x2 - 21, y1 + 16, x2 - 8, y1 + 16);
  151.     SetColor(0);
  152.     line(x2 - 7, y1 + 5, x2 - 7, y1 + 17);
  153.     line(x2 - 22, y1 + 17, x2 - 7, y1 + 17);
  154.     line(x2 - 18, y1 + 8, x2 - 12, y1 + 13);
  155.     line(x2 - 17, y1 + 8, x2 - 11, y1 + 13);
  156.     line(x2 - 18, y1 + 13, x2 - 12, y1 + 8);
  157.     line(x2 - 17, y1 + 13, x2 - 11, y1 + 8);
  158.     /* The exit button 'x' on top right */
  159.    
  160.     OutTextxy(n, x1 + 4, y1 + 4, title, 15 - (!active) * 8);
  161.     /* Output the title text, still same as button */
  162. }
  163.  
  164. void DrawStatusBox(void *n)
  165. /* Draw an status box similar to Win9x */
  166. {
  167.     int x1=getmaxx()-200,y1=getmaxy()-20,x2=getmaxx()-6,y2=getmaxy()-4;
  168.     DoNothing(n);
  169.     setwritemode(0);
  170.     setfillstyle(SOLID_FILL, 7);
  171.     bar(x1,y1,x2,y2);
  172.     SetColor(8);
  173.     line(x1-1,y1-1,x1-1,y2+1);
  174.     line(x1-1,y1-1,x2+1,y1-1);
  175.     SetColor(15);
  176.     line(x1-1,y2+1,x2+1,y2+1);
  177.     line(x2+1,y1-1,x2+1,y2+1);
  178. }
  179.  
  180. void DrawMainTextBox(void *n)
  181. /* The main text box */
  182. {
  183.     State *s;
  184.     s = (State *)n;
  185.     if(!s -> ischildwindow)
  186.     {
  187.         s -> editx1 = 10, s -> edity1 = 50;
  188.         s -> editx2 = getmaxx() - 10, s -> edity2 = getmaxy() - 28;
  189.         /*if(s -> filehead == NULL)
  190.         {  
  191.             s -> cursorx = s -> editx1;
  192.             s -> cursory = s -> edity1;
  193.         }*/
  194.     }
  195.    
  196.     DrawTextBox(7, 46, getmaxx() - 7, getmaxy() - 25);
  197.     OutPutMain(n);
  198.     ClrCur(n);
  199.     FlashCur(n);
  200. }
  201.  
  202. void DrawMainWindow(void *n)
  203. /* Draw main window */
  204. {
  205.     char title[30];
  206.     State *s;
  207.     s = (State *)n;
  208.     if(s -> filename[0] == 0)
  209.         strcpy(title, "无标题 - 记事本");
  210.     else
  211.         sprintf(title, "%s - 记事本", s -> filename);
  212.     setbkcolor(0);
  213.     DrawWindow(n, 0, 0, getmaxx(), getmaxy(), title);
  214. }
  215.  
  216. void IsExit(void *n)
  217. /* Check whether an window should be closed */
  218. {
  219.     State *s;
  220.     s = (State *)n;
  221.    
  222.     if(!s -> ischildwindow && s -> mouse . key && PinRA(
  223.                     s -> mouse . x, s -> mouse . y, 618, 0, 640, 17))
  224.     /* Clicked the 'x' on main window */
  225.         Exit(n);
  226.    
  227.     if(s -> ischildwindow && (s -> mouse . key && PinRA(
  228.                     s -> mouse . x, s -> mouse . y,
  229.                     s -> childx2 - 22, s -> childy1,
  230.                     s -> childx2, s -> childy1 + 17) || s -> isexit))
  231.     /* Clicked the 'x' on client window */
  232.     {
  233.         CloseChildWindow(n);
  234.         s -> isexit = 0;
  235.         s -> mouse . key = 0;
  236.     }
  237. }
  238.  
  239. int RAvsRA(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  240. /* If a RectAngle covers the other one */
  241. {
  242.     return(PinRA(x1, y1, x3, y3, x4, y4) || PinRA(x2, y2, x3, y3, x4, y4) ||
  243.            PinRA(x1, y2, x3, y3, x4, y4) || PinRA(x2, y1, x3, y3, x4, y4) ||
  244.            PinRA(x3, y3, x1, y1, x2, y2) || PinRA(x4, y4, x1, y1, x2, y2) ||
  245.            PinRA(x3, y4, x1, y1, x2, y2) || PinRA(x4, y3, x1, y1, x2, y2));
  246.     /* Just determine if the four corners of a rectangle is in the other one */
  247. }
  248.  
  249. void SetColor(unsigned int n)
  250. /* Workaround for quick access to change the function */
  251. {
  252.     setcolor(n);
  253. }
  254.  
  255. void FadingColor(void *n)
  256. /* Color fading while loading and unloading */
  257. {
  258.     int cp[16][3]={
  259.     {0x00,0x00,0x00},
  260.     {0x00,0x00,0x1f},
  261.     {0x00,0x1f,0x00},
  262.     {0x1f,0x00,0x00},
  263.     {0x00,0x1f,0x1f},
  264.     {0x1f,0x1f,0x00},
  265.     {0x1f,0x00,0x1f},
  266.     {0x27,0x27,0x27},
  267.     {0x17,0x17,0x17},
  268.     {0x06,0x0c,0x1e},
  269.     {0x0c,0x14,0x25},
  270.     {0x14,0x1b,0x29},
  271.     {0x1a,0x22,0x2f},
  272.     {0x21,0x29,0x35},
  273.     {0x28,0x30,0x3b},
  274.     {0x3f,0x3f,0x3f}
  275.     };
  276.     /* Manually set all 16 colors.
  277.      * Some colors here are fetched from Windows XP Classic Theme */
  278.    
  279.     int i, j, sum = 20;
  280.     /* Sum is the fading progression */
  281.    
  282.     int delta;
  283.     /* Workaround for different direction between loading and unloading progress */
  284.    
  285.     State *s;
  286.     s = (State *)n;
  287.     delta = s -> isexit;
  288.    
  289.     j = delta * sum;
  290.     while(1)
  291.     {
  292.         PlayMusic(n);
  293.         for(i=0;i<16;i++)
  294.         {
  295.             setrgbpalette(i, cp[i][0] * j / sum, cp[i][1] * j / sum, cp[i][2] * j / sum);
  296.             setpalette(i, i);
  297.             /* Color fading via calculating percentage of colors */
  298.         }
  299.         Delay(n, 7);
  300.         /* A small delay */
  301.        
  302.         if(j == sum - delta * sum)
  303.             break;
  304.            
  305.         j += (delta ? -1 : 1);
  306.         /* Go on! */
  307.     }
  308. }
  309.  
  310. void InitColor(void *n)
  311. /* Initialize all colors to black, so the fading could work later */
  312. {
  313.     int i;
  314.     DoNothing(n);
  315.     for(i = 0; i < 16; i ++)
  316.     /* Set all palette to black */
  317.     {
  318.         setrgbpalette(i, 0, 0, 0);
  319.         setpalette(i, i);
  320.     }
  321. }
  322.  
  323. void ChangeDotted(void *n)
  324. /* Change dotted mode */
  325. {
  326.     State *s;
  327.     s = (State *)n;
  328.     s -> dotted = !s -> dotted;
  329.    
  330.     /* FIXME: Dirty solution */
  331.     s -> menu -> next -> next -> menu -> enabled = 5 - s -> menu -> next -> next -> menu -> enabled;
  332. }
  333.  
  334. void ChangeHighlight(void *n)
  335. /* Change highlight mode */
  336. {
  337.     State *s;
  338.     s = (State *)n;
  339.     s -> highlight = !s -> highlight;
  340.    
  341.     /* FIXME: Dirty solution */
  342.     s -> menu -> next -> next -> menu -> next -> enabled = 5 - s -> menu -> next -> next -> menu -> next -> enabled;
  343. }
  344.  
  345. void CheckStatus(void *n)
  346. /* Check and display the status in the main status box */
  347. {
  348.     State *s;
  349.     static int isedit = 0, ischildwindow = 0, chinese = -1, chipunc = -1,
  350.         fullwidth = -1, cursorcolumn = -1, cursorrow = -1, ischosen = -1;
  351.     /* Memorize last statuses so it won't draw again and again */
  352.    
  353.     char location[20];
  354.    
  355.     int i, x, y;
  356.     s = (State *)n;
  357.    
  358.     if(s -> isdrag)
  359.     /* Disable editing while dragging */
  360.         s -> isedit = 0;
  361.    
  362.     x = getmaxx() - 7, y = getmaxy() - 20;
  363.     /* Define the place to draw */
  364.    
  365.     if(s -> isedit != isedit || ischildwindow != s -> ischildwindow ||
  366.        chinese != s -> chinese || chipunc != s -> chipunc || fullwidth != s -> fullwidth
  367.        || ischosen != s -> ischosen || (s -> isedit && !s -> ischildwindow
  368.        && (s -> currentrow + s -> pagemovecount != cursorrow ||
  369.        s -> currentcolumn != cursorcolumn)))
  370.     /* Status redraw for status bar */
  371.     {
  372.         CleanUp(n);
  373.        
  374.         /* Draw the status on both pages */
  375.         i = !s -> page;
  376.         do
  377.         {
  378.             setactivepage(i);
  379.            
  380.             DrawStatusBox(n);
  381.             if(s -> isedit)
  382.             {                
  383.                 setfillstyle(SOLID_FILL, 1);
  384.                 bar(x - 16, y, x + 1, y + 16);
  385.                 setwritemode(0);
  386.                 setcolor(15);
  387.                 line(x - 34, y, x - 34, y + 16);
  388.                 line(x - 34, y, x - 17, y);
  389.                 setcolor(8);
  390.                 line(x - 34, y + 16, x - 17, y + 16);
  391.                 line(x - 17, y, x - 17, y + 16);
  392.                 setcolor(15);
  393.                 line(x - 51, y, x - 51, y + 16);
  394.                 line(x - 51, y, x - 34, y);
  395.                 setcolor(8);
  396.                 line(x - 51, y + 16, x - 34, y + 16);
  397.                 line(x - 34, y, x - 34, y + 16);
  398.                 /* Draw the background */
  399.                
  400.                 if(s -> chinese)
  401.                 /* Chinese mode enabled */
  402.                 {
  403.                     OutTextxy(n, x - 15, y, "中", 15);
  404.                 }
  405.                 else
  406.                 /* Chinese mode disabled
  407.                  * It really looks like the Win9x En icon :) */
  408.                 {
  409.                     OutTextxy(n, x - 15, y, "En", 15);
  410.                 }
  411.                
  412.                 if(s -> chipunc)
  413.                 {
  414.                     OutTextxy(n, x - 32, y - 4, ",", 0);
  415.                     OutTextxy(n, x - 27, y, "。", 0);
  416.                 }
  417.                 else
  418.                 {
  419.                     OutTextxy(n, x - 30, y - 2, ",", 0);
  420.                     OutTextxy(n, x - 25, y + 1, ".", 0);
  421.                 }
  422.                
  423.                 if(s -> fullwidth)
  424.                 {
  425.                     OutTextxy(n, x - 51, y, "Ο", 0);
  426.                     setcolor(0);
  427.                     setfillstyle(SOLID_FILL, 0);
  428.                     floodfill(x - 41, y + 8, 0);
  429.                 }
  430.                 else
  431.                 {
  432.                     setcolor(0);
  433.                     arc(x - 32, y + 3, 175, 255, 14);
  434.                     arc(x - 40, y + 8, 135, 305, 8);
  435.                     setfillstyle(SOLID_FILL, 0);
  436.                     floodfill(x - 46, y + 8, 0);
  437.                 }
  438.                
  439.                 if(!s -> ischildwindow)
  440.                 {
  441.                     sprintf(location, "第%d行,第%d列", s -> currentrow/* + s -> pagemovecount*/, s -> currentcolumn);
  442.                     OutTextxy(n, x - 180, y, location, 0);
  443.                 }
  444.             }
  445.             i = !i;
  446.             /* Move to another page! */
  447.         }while(i == s -> page);
  448.        
  449.         MouseAway(n);
  450.        
  451.         ischosen = s -> ischosen;
  452.         isedit = s -> isedit;
  453.         ischildwindow = s -> ischildwindow;
  454.         chinese = s -> chinese;
  455.         chipunc = s -> chipunc;
  456.         fullwidth = s -> fullwidth;
  457.         cursorrow = s -> currentrow + s -> pagemovecount;
  458.         cursorcolumn = s -> currentcolumn;
  459.         /* Save the new statuses */
  460.     }
  461. }
  462.  
  463. void ShowHelp(void *n)
  464. /* Show Help messages */
  465. {
  466.     char *help;
  467.     //State *s;
  468.     //s = (State *)n;
  469.     DoNothing(n);
  470.     help = (char *)ALLOC(20 * sizeof(char));
  471.     CheckAlloc((void *)help);
  472.     strcpy(help, "help.txt");
  473.     OpenFile(n, help);
  474. }
  475.  
  476. void OpenFile(void *n, char *f)
  477. /* Open an file */
  478. {
  479.     State *s;
  480.     Event *Readerr;
  481.     int l;
  482.     s = (State *)n;
  483.     Readerr = ALLOC(sizeof(Event));
  484.     InitEvent(Readerr);
  485.     RegEvent(Readerr, &ReadErr);
  486.     strcpy(s -> filename, f);
  487.     CleanUp(n);
  488.     if(!s -> dotted)
  489.     {
  490.         setactivepage(!s -> page);
  491.         DrawMainWindow(n);
  492.         DrawStatusBox(n);
  493.         DoEvent(s -> redraw, n);
  494.         CleanUp(n);
  495.         setvisualpage(!s -> page);
  496.         s -> page = !s -> page;
  497.     }
  498.     else
  499.     {
  500.         DrawMainWindow(n);
  501.         DrawStatusBox(n);
  502.         DoEvent(s -> redraw, n);
  503.         CleanUp(n);
  504.     }
  505.     /* Do a redraw before draw it */
  506.     s -> isedit = 1;
  507.     FREEMEMRow(s -> filehead);
  508.     s -> filehead = NULL;
  509.     if(f != NULL)
  510.     {
  511.         s -> filehead = ReadFile(n, f, Readerr);
  512.         FREEMEM(f);
  513.     }
  514.     f = NULL;
  515.    
  516.     FreeEvent(Readerr);
  517.     s -> currentcolumn = 1;
  518.     s -> currentrow = 1;
  519.     if(s -> filehead == NULL)
  520.     {
  521.         memset(s -> filename, 0, 20);
  522.         //s -> filename[0] = 0;
  523.     }
  524.     l = strlen(s -> filename);
  525.    
  526.     if(l > 2 && Lowercase(s -> filename[l - 1]) == 'c' && s -> filename[l - 2] == '.'
  527.     || l > 2 && Lowercase(s -> filename[l - 1]) == 'h' && s -> filename[l - 2] == '.'
  528.     || l > 4 && Lowercase(s -> filename[l - 1]) == 'p' && Lowercase(s -> filename[l - 2]) == 'p'
  529.     && Lowercase(s -> filename[l - 3]) == 'c' && s -> filename[l - 4] == '.')
  530.     {
  531.         if(!s -> highlight)
  532.             ChangeHighlight(n);
  533.     }
  534.     else
  535.     {
  536.         if(s -> highlight)
  537.             ChangeHighlight(n);
  538.     }
  539.    
  540.     CleanUp(n);
  541.     if(!s -> dotted && l)
  542.     {
  543.         setactivepage(!s -> page);
  544.         DrawMainWindow(n);
  545.         DrawStatusBox(n);
  546.         DoEvent(s -> redraw, n);
  547.         CleanUp(n);
  548.         setvisualpage(!s -> page);
  549.         s -> page = !s -> page;
  550.     }
  551.     else
  552.     {
  553.         OutPutXY(n);
  554.         CleanUp(n);
  555.     }
  556. }
  557.    
  558. void CleanUp(void *n)
  559. /* Both mouse and cursor workaround */
  560. {
  561.     MouseAway(n);
  562.     ClrCur(n);
  563. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement