home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / d / dir_nm20.zip / Dir_NM_2.0 / NM-Button.c < prev    next >
C/C++ Source or Header  |  1992-07-16  |  16KB  |  485 lines

  1. /*                         ===  NM-Button.c  ===
  2.  *
  3.  * void SetUpCoordSystem(width,height)
  4.  * void DoMotionEvent(report,mouse_x,mouse_y,dx,dy)
  5.  * int  DoButtonEvent(report,button,Mx,My,Mdy_Id,nx,ny)
  6.  * void DoButtonReleaseEvent(report,button,mouse_x,mouse_y,dx,dy)
  7.  * void ProcessKeyModifiers(report,modifier_state,button_state)
  8.  * void RemoveKeyModifiers(report,modifier_state,button_state)
  9.  * void DoKeyPressEvent(report)
  10.  * void DoKeyReleaseEvent(report)
  11.  * void draw_modifier_state(modifier_state)
  12.  * void print_mouse_coords(mouse_x,mouse_y,button)
  13.  * void draw_mouse_coords(win,gc,font_info,mouse_x,mouse_y,button,state)
  14.  *
  15.  */
  16.  
  17. #include "NM.h" 
  18.  
  19. GC saveGC;            /* Use this to save context.
  20.                  * See Menu.c   */
  21. Colormap theCmap,cmap;
  22. XColor mycolor,exactcolor,mine;
  23.  
  24. int FirstTime=True;
  25. extern int lit,IN;
  26.  
  27. void SetUpCoordSystem(width,height)
  28.   int width,height;
  29.   saveGC=XCreateGC(display,win,0,0);sleep(1);XSync(display,False);Init();
  30.   RefreshMenu();    /* Add the color allocation; should be the same as theCmap
  31.                        cmap=DefaultColormap(display,screen); */
  32. }
  33.  
  34. void DoMotionEvent(report,mouse_x,mouse_y,dx,dy)
  35.   XEvent report;
  36.   int mouse_x,mouse_y,dx,dy;
  37.   int state,button,n;
  38.  
  39.   state=report.xbutton.state;button=state&(Button1Mask|Button2Mask|Button3Mask);
  40.  
  41.   if (Motion==Piano_On) n=Piano_Note(Piano_X,Piano_Y,mouse_x,mouse_y);
  42.   switch(state) 
  43.     {
  44.       case False:if (On_Text==1) Hi_light_Text(mouse_x,mouse_y);
  45.              else
  46.            {
  47.              HighlightMenu(mouse_x,mouse_y);
  48.              Hi_light_Map(mouse_x,mouse_y);
  49.              if (Motion==List_On || (Motion<0 && Action==List_On))
  50.                Hi_light_Table(mouse_x,mouse_y);
  51.              if (Host==1) Hi_light_host(mouse_x,mouse_y);
  52.            }
  53.     break;
  54.  
  55.           /* Put a SWITCH here to select among different command modes,
  56.            *   do the same in the button press/release processing.
  57.            */
  58.       case Button1Mask:
  59.     break;         
  60.       case Button2Mask:if (In_Map(mouse_x,mouse_y)==1) 
  61.                      { Map_Dx+=dx;Map_Dy+=dy; }
  62.     break;
  63.       case (Button3Mask|ControlMask): break;
  64.       default:/* print_mouse_coords(button,mouse_x,mouse_y);  */ break;
  65.     }     /* end  switch(state) */
  66.   switch(IN)
  67.     {
  68.       case 2:break;
  69.  
  70.       case 7:break;
  71.     }
  72. }
  73.  
  74. int DoButtonEvent(report,button,Mx,My,Mdy_Id,nx,ny)
  75.   XEvent report;
  76.   int button,Mx,My,Mdy_Id,nx,ny;
  77. {
  78.   int state,Pos;
  79.  
  80.   state=report.xbutton.state;Pos=0;
  81.  
  82. /* printf("\n press IN=%i\n",IN); */
  83.   switch (button) 
  84.     {              /*  Process Menu Selection if Button Pressed */
  85.       case Button1:if (Motion==Input_On)
  86.              {
  87.                if (five && now>0)
  88.              {
  89.                if (922<Mx && Mx<962 && 781<My && My<818)
  90.                  { 
  91.                    Adjust_Text(166,737,1);Pos=-2;
  92.                    if (On_Text==1) 
  93.                  {
  94.                    Show_Text_Table(Map_SX,Text_SY);
  95.                    High_Light_Char(Word_X,Word_Y);
  96.                  }
  97.                    else 
  98.                  {
  99.                    High_Light_Char(Word_X,Word_Y);
  100.                    Showup(166,737,five);
  101.                  }
  102.                  }
  103.                else if (On_Text==0)
  104.                       {
  105.                     if (922<Mx && Mx<962 && 742<My && My<779)
  106.                       { 
  107.                     SetColor("cyan");
  108.                     XSetLineAttributes(display,gc,2,0,0,0);
  109.                     Use_Font("7x14");
  110.                     DrawRectangle(922,743,38,35);
  111.                     SetColor("magenta");
  112.                     DrawRectangle(924,745,34,31);
  113.                     Rub(927,749,28,23,"NavyBlue");
  114.                     SetColor("cyan");
  115.                     Adjust_High_Low(166,737,1);Pos=-1;
  116.                       }
  117.                     else if (40<Mx&&Mx<1120&&32<My&&My<732)
  118.                            Pos=MS_Input(button,Mx,My,Mdy_Id,
  119.                             nx,ny,On_High);
  120.                   }
  121.                     else if (On_Text==1) Edit_Word(button,Mx,My);
  122.              }
  123.              }
  124.                else if (Motion==Output_On)
  125.                   {
  126.                 if (548<Mx && Mx<585 && 2<My && My<28)
  127.                   { 
  128.                 Display_Array(Mdy_Id,Print_pt,Print_pt+50,now);
  129.                 Print_pt+=50;Pos=1;
  130.                 if (Print_pt>906 || now==0) Print_pt=0;
  131.                   }
  132.                     else if (Tour_On==0 && 738<My && My<738+122 &&
  133.                          ((2<Mx&&Mx<2+160) || (969<Mx&&Mx<969+160)))
  134.                        See_Tempo_Set(Mdy_Id);
  135.                          else if (Tour_On==1) 
  136.                             { 
  137.                           now=next=Now_Bank;Tour_On=0;
  138.                           Output(Back_Id);
  139.                         }
  140.                           else ch='*';
  141.               }
  142.                         else if (Motion==Piano_On) 
  143.                    Show_Piano(Piano_X,Piano_Y,button);
  144.                  else if (NewMenuSelection())
  145.                         {
  146.                       if ((2<IN && IN<9) || IN==12 || IN==22)
  147.                     Action=IN+1;
  148.                       else { Old_Motion=Motion;Motion=IN+1; }
  149.                       Renew_Notice();
  150.                       switch (IN)
  151.                         {
  152.                           case 0:Show_host(IN);break;
  153.                       case 1:if (total>0)
  154.                                {
  155.                              Column_Pointer=1;Host=0;
  156.                              Row_Pointer=0;List();
  157.                                }
  158.                         break;
  159.                       case 2:Show_host(IN);break;
  160.                       case 3: /*   Re_Name    */
  161.                       case 4: /*   Move_To    */
  162.                       case 5: /*   Delete     */
  163.                       case 6: /*   Value      */
  164.                       case 7: /*   Check_Tmp  */
  165.                       case 8: /*   Switch     */
  166.                              if (total>0 && Motion!=List_On)
  167.                                {
  168.                              Host=0;List();
  169.                              Motion=List_On;
  170.                                }
  171.                         break;
  172.                       case 9:Show_host(IN);break;
  173.                       case 12: /*   Load Text Files*/
  174.                               if (total>0&&Motion!=List_On)
  175.                                 {
  176.                                Host=0;List();
  177.                                Motion=List_On;
  178.                                 }
  179.                         break;
  180.                       case 13:Show_host(IN);break;
  181.  
  182.                       case 15:In_Menu=0;Timbre();
  183.                               RefreshMenu();
  184.                         break;
  185.                       case 16:In_Menu=0;Word_I_O('I');
  186.                               RefreshMenu();
  187.                         break;
  188.                       case 17: /*  In_Menu=0;Dance();  */
  189.                               RefreshMenu();
  190.                         break;
  191.                       case 18:In_Menu=0;Sound('I');
  192.                               RefreshMenu();
  193.                         break;
  194.                       case 19:In_Menu=0;Voice('P');
  195.                               RefreshMenu();
  196.                         break;
  197.                       case 20:Show_Piano(Piano_X,Piano_Y,
  198.                                  button);
  199.                         break;
  200.                       case 21:Show_host(IN);break;
  201.  
  202.                       case 23:Quit();break;
  203.                     }
  204.                     }
  205.                       else if (Host==1 && (Column_Id>0 || (-1<Host_X
  206.                            && Host_X<6 && -1<Host_Y&&Host_Y<6)))
  207.                              Finish_host();
  208.                        else if (Motion==List_On)
  209.                           {
  210.                         if (In_Table(Mx,My)==1) 
  211.                           Edit_Table(Mx,My);
  212.                         else if (In_Map(Mx,My)==1) 
  213.                                    Do_Re_Name(Mx,My,button);
  214.                           }
  215.     break;
  216.       case Button2:if (Motion==Input_On)
  217.                  {
  218.                if (On_Text==1) Edit_Word(button,Mx,My);
  219.                else if (five&&now>0&&40<Mx&&Mx<1120&&32<My&&My<732)
  220.                   Pos=MS_Input(button,Mx,My,Mdy_Id,nx,ny,On_High);
  221.              }
  222.                else if (Motion==Output_On) ch='!';
  223.                         else if (Motion==List_On && In_Map(Mx,My)==1)
  224.                        Do_Re_Name(Mx,My,button);
  225.                          else if (Motion==Piano_On) 
  226.                         Show_Piano(Piano_X,Piano_Y,button);
  227.                               else if (NewMenuSelection())
  228.                              {
  229.                            switch (IN)
  230.                          {
  231.                            case 2:Update_Text_Tar();break;
  232.                            case 3:break;
  233.                          }      
  234.                      }
  235.     break;
  236.       case Button3:if (In_Menu==1) RefreshMenu();
  237.                    else if (Motion==Input_On)
  238.                   {
  239.                 if (On_Text==1) Edit_Word(button,Mx,My);
  240.                 else if (five && now>0 && 40<Mx && Mx<1120 && 
  241.                      33<My && My<732) 
  242.                    Pos=MS_Input(button,Mx,My,Mdy_Id,nx,ny,
  243.                         On_High);
  244.                      else if (548<Mx && Mx<585 && 2<My && My<34) 
  245.                         {         /* Save  &  Exit */
  246.                       if (now==0) tmp[M_Id][0].note+=1;
  247.                       else St[M_Id][0][On_High].nt[0]+=1;
  248.                       head=M_Id+1;Save(M_Id+1,tail);ch='&';
  249.                     }
  250.               }
  251.                     else if (Motion==Output_On)
  252.                        {
  253.                  if (548<Mx && Mx<585 && 2<My && My<28) ch='&';
  254.                  else if (Tour_On==0) ch='/';
  255.                    }
  256.     break;
  257.     }            /* end switch(button) */
  258.   fflush(stdout);return Pos;    /* This makes the screen printing happen... */
  259. }
  260.  
  261. void DoButtonReleaseEvent(report,button,mouse_x,mouse_y,dx,dy)
  262.   XEvent report;
  263.   int button,mouse_x,mouse_y,dx,dy;
  264. {
  265.   int state,Wwidth,Wheight;
  266.  
  267.   state=report.xbutton.state;  /*  printf("\n release");  */
  268.   switch (button)  
  269.     {
  270.       case Button1:switch (IN)
  271.                  {
  272.                case 3:if (FirstTime==False )
  273.                     {
  274.                   XSetFunction(display,gc,GXxor);
  275.                   XSetFunction(display,gc,GXcopy);
  276.                 }
  277.                       else FirstTime=False;
  278.              break;
  279.                default:break;
  280.              }
  281.     break;
  282.       case Button2:if (In_Map(mouse_x,mouse_y)==1 && (Map_Dx!=0 || Map_Dy!=0)) 
  283.                  Move_Table(mouse_x,mouse_y,Map_Dx/6,Map_Dy/3);
  284.                Map_Dx=0;Map_Dy=0;
  285.                if (Motion==-Input_On || Motion==-Output_On)
  286.              Show_Preface(139);
  287.     break;
  288.       case Button3: break;
  289.     }
  290.   fflush(stdout);
  291. }
  292.  
  293.  
  294. /* Process key modifiers to make new mask 
  295.  * construct a state with only modifier keys */
  296.  
  297. void ProcessKeyModifiers(report,modifier_state,button_state)
  298.   XEvent *report;
  299.   unsigned int *modifier_state, *button_state;
  300. {
  301.   int state;
  302.   KeySym ks,sym;
  303.   XComposeStatus status;
  304.  
  305.   state=report->xkey.state;
  306.   *modifier_state=state & (ShiftMask | ControlMask | Mod1Mask);
  307.   /* construct a state with only button presses */
  308.   *button_state= state &( Button1Mask | Button2Mask | Button3Mask);
  309.   sym = XLookupKeysym(report,0); /* 0 = lower case, 1 = upper case */
  310.  /* keycode=report->xkey.keycode;KeycodeToKeysm(display,code,index);
  311.     printf("\n1. code  %d, sym %d, state %x, mod-mask %x,\n
  312.     Cs %d %d : %d %d ; %d %d \n",report->xkey.keycode,sym,state,*modifier_state,
  313.     XK_Shift_L,XK_Shift_R,XK_Control_L,XK_Control_R, XK_Meta_L, XK_Meta_R );
  314.   */
  315.   switch (sym)
  316.     {  
  317.       case  XK_Shift_L:
  318.       case  XK_Shift_R:/* printf("--bare ShiftMask");Force addition of shift 
  319.                           mask */
  320.                        *modifier_state = *modifier_state | ShiftMask;
  321.         break;
  322.       case XK_Control_L:
  323.       case XK_Control_R:/* printf("-- bare ControlMask");Force addition of 
  324.                            control mask */
  325.                         *modifier_state=*modifier_state | ControlMask;
  326.         break;
  327.       case XK_Meta_L:
  328.       case XK_Meta_R:/* printf("-- bare Mod1Mask");Force addition of Meta mask*/
  329.                      *modifier_state=*modifier_state | Mod1Mask;
  330.         break;
  331.         /* Process non-modifier keypresses */
  332.       default:XLookupString(report,tmpstr,128,&ks,&status);
  333.               if (tmpstr[0]=='q' || tmpstr[0]=='Q') Quit();
  334.                 /* here is where other letter keys hit */
  335.               if (tmpstr[0]=='a' || tmpstr[0]=='A') Quit();
  336.         break;
  337.     }
  338. }
  339.  
  340. /* Remove inserted key modifiers from mask */
  341.  
  342. void RemoveKeyModifiers(report,modifier_state,button_state)
  343.   XEvent *report;
  344.   unsigned int *modifier_state, *button_state;
  345. {
  346.   int state;
  347.   KeySym ks,sym;
  348.   XComposeStatus status;
  349.  
  350.   state=report->xkey.state;
  351.   *modifier_state= state & ( ShiftMask | ControlMask | Mod1Mask);
  352.   /* construct a state with only button presses */
  353.   *button_state= state & ( Button1Mask | Button2Mask | Button3Mask);
  354.   sym=XLookupKeysym(report,0); /* 0 = lower case, 1 = upper case */
  355.   switch (sym)
  356.     {  
  357.       case  XK_Shift_L:
  358.       case  XK_Shift_R:/* printf("--bare  ShiftMask");Force deletion of shift 
  359.                           mask by XOR operator if it is present */
  360.                        *modifier_state=*modifier_state ^ ShiftMask;
  361.         break;
  362.       case XK_Control_L:
  363.       case XK_Control_R:/* printf("-- bare ControlMask");Force addition of 
  364.                            control mask */
  365.                         *modifier_state=*modifier_state ^ ControlMask;
  366.         break;
  367.       case XK_Meta_L:
  368.       case XK_Meta_R:/* printf("-- bare Mod1Mask");Force addition of Meta mask*/
  369.                      *modifier_state=*modifier_state ^ Mod1Mask;
  370.         break;
  371.         /* Process non-modifier keypresses */
  372.       default:XLookupString(report,tmpstr,128,&ks,&status);
  373.               if (tmpstr[0]=='q' || tmpstr[0]=='Q') Quit();
  374.         break;
  375.     }
  376. }
  377.  
  378. void DoKeyPressEvent(report)
  379.   XEvent report;
  380. {
  381.   unsigned int state,modifier_state,button_state;
  382.   ProcessKeyModifiers(&report,&modifier_state,&button_state);
  383. /*  draw_modifier_state(modifier_state);
  384.   switch (modifier_state) 
  385.     {
  386.       case ShiftMask: printf("--  Shift");break;
  387.       case ControlMask: printf("-- Control");break;
  388.       case Mod1Mask: printf("-- Mod1");break;
  389.       case ShiftMask|ControlMask: printf("-- Shift Control");break;
  390.       case ShiftMask|Mod1Mask: printf("-- Shift Mod1");break;
  391.       case Mod1Mask|ControlMask: printf("--  Control Mod1");break;
  392.       case ShiftMask|Mod1Mask|ControlMask: printf("--  Shift Control Mod1"); 
  393.         break;
  394.       default:printf("-- no modifiers");break;
  395.     }
  396.   switch(button_state)
  397.     {
  398.       case Button1Mask:printf("-- Button1Mask");break;
  399.       case (Button1Mask|ShiftMask):printf("-- Button1Mask | ShiftMask");break;
  400.     }
  401. */        /* end  switch(state) */
  402.   fflush(stdout);
  403. }
  404.  
  405. void DoKeyReleaseEvent(report)
  406.   XEvent report;
  407. {
  408.   unsigned int state,new_modifier_state,old_modifier_state,new_button_state, 
  409.                 old_button_state;
  410.    /* printf("\n keyrelease");*/
  411.  
  412.   ProcessKeyModifiers(&report,&old_modifier_state,&old_button_state);
  413.   RemoveKeyModifiers(&report,&new_modifier_state,&new_button_state);
  414.   /* The idea here is that if SHIFT WAS down, the things that
  415.      ran when shift was down need to know it was down in the old state.
  416.      But things that depend on the CURRENT state, need to have the modifier
  417.      deleted so the new state appears immediately updated. */
  418. /*draw_modifier_state(new_modifier_state);
  419.   switch (old_modifier_state) 
  420.     {
  421.       case ShiftMask: printf("--  Shift");break;
  422.       case ControlMask: printf("-- Control"); break;
  423.       case Mod1Mask: printf("-- Mod1"); break;
  424.       case ShiftMask|ControlMask: printf("-- Shift Control"); break;
  425.       case ShiftMask|Mod1Mask: printf("-- Shift Mod1"); break;
  426.       case Mod1Mask|ControlMask: printf("--  Control Mod1"); break;
  427.       case ShiftMask|Mod1Mask|ControlMask:printf("-- Shift Control Mod1");break;
  428.       default:printf("-- no modifiers");break;
  429.     }
  430.   switch(old_button_state)
  431.     {
  432.       case Button1Mask:printf("-- Button1Mask");break;
  433.       case (Button1Mask|ShiftMask):printf("-- Button1Mask | ShiftMask");break;
  434.     }
  435.        */        /* end  switch(state) */
  436.   fflush(stdout);
  437. }
  438.  
  439. /* Announce Modifier State so you know what Mouse Button will do */
  440. /* Mouse position drawn to window stdio  */
  441. void draw_modifier_state(modifier_state)
  442.   unsigned int modifier_state;
  443.   static char MsgText[255];
  444.   switch (modifier_state) 
  445.     {
  446.       case ShiftMask:(void) sprintf(MsgText," Shift ");break;
  447.       case ControlMask:(void) sprintf(MsgText," Control ");break;
  448.       case Mod1Mask:(void) sprintf(MsgText," Mod1 ");break;
  449.       case (ShiftMask|ControlMask):(void) sprintf(MsgText, " Shift Control "); 
  450.         break;
  451.       case ShiftMask|Mod1Mask:(void) sprintf(MsgText," Shift Mod1 ");break;
  452.       case Mod1Mask|ControlMask:(void) sprintf(MsgText," Control Mod1 ");break;
  453.       case ShiftMask|Mod1Mask|ControlMask:(void) sprintf(MsgText,
  454.                                                  "Shift Control Mod1 "); 
  455.     break;
  456.       default:(void) sprintf(MsgText,"-- no modifiers");break;
  457.     }
  458.   PrintMsgText(win,gc,font_info,MsgText);
  459. }
  460.  
  461. /* Mouse position printed to stdio  */
  462. void print_mouse_coords(mouse_x,mouse_y,button)
  463.   int button,mouse_x,mouse_y;
  464. {
  465.   printf("(%d, %d: %d) ",  mouse_x, mouse_y, button);
  466. }
  467.  
  468. /* Mouse position drawn to window stdio  */
  469. void draw_mouse_coords(win,gc,font_info,mouse_x,mouse_y,button,state)
  470.   Window win;
  471.   GC gc;
  472.   XFontStruct *font_info;
  473.   int button,state,mouse_x,mouse_y;
  474.   static char MsgText[255];
  475.   
  476.   (void) sprintf(MsgText,"(x,y:State:Button)=(%d,%d: %d:%d).",mouse_x,mouse_y, 
  477.                  state, button);
  478.   PrintMsgText(win,gc,font_info,MsgText);
  479. }
  480.  
  481.