text.cpp

00001 /***************************************************************************
00002                           text.cpp  -  description
00003                              -------------------
00004     begin                : Mon Mar 1 2004
00005     copyright            : (C) 2004 by Dynacube Team
00006     email                :
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017  #include "gui/components/text.h"
00018 
00019  void text::text_init(DW _id,DW _parent_id,DW _cnt_type,void *_cnt_ptr,DW _alias_id,DW _x,DW _y,DW _width,DW _height,DD _style,char *_txt)
00020  {
00021     memcpy(txt,_txt,strlen(_txt));
00022     comp_init(TEXT_TYPE,_id,_parent_id,_cnt_type,_cnt_ptr,_alias_id,_x,_y,_width,_height,_style);
00023     start = 0;
00024     curs = 0;
00025     allow_chars = (width/(CHAR_WIDTH+X_OFFSET)) - 1;
00026     total_chars = strlen(_txt);
00027     is_sel = false;
00028     sel_st = 0;
00029     sel_en = 0;
00030  }
00031 
00032  void text::paint()
00033   {
00034     window *win_ptr;
00035     frame *frm_ptr;
00036     DW _x,_y,_width,_height;
00037     DB tmp[255];
00038 
00039     switch(cnt_type)
00040     {
00041       case WIND_TYPE :
00042                       win_ptr = (window *) cnt_ptr;
00043                       if(win_ptr->style & W_MAXIMIZE)
00044                        {
00045                          _x = BORDER_WIDTH;
00046                          _y = TITLE_HEIGHT + BORDER_HEIGHT + ((win_ptr->hasMenu) ? MENUBAR_HEIGHT : 0);
00047                          _width  = SCREEN_WIDTH;
00048                          _height = SCREEN_HEIGHT;
00049                        }
00050                       else
00051                        {
00052                          _x = win_ptr->x + BORDER_WIDTH;
00053                          _y = win_ptr->y + TITLE_HEIGHT + BORDER_HEIGHT + ((win_ptr->hasMenu) ? MENUBAR_HEIGHT : 0);
00054                          _width = win_ptr->width;
00055                          _height = win_ptr->height;
00056                        }
00057 
00058                       if((x + width < _width) && (y + height < _height))
00059                       {
00060 
00061        fillrect(x+_x, y+_y, width, height,TEXT_COLOR);
00062        
00063                          if(is_sel)
00064                           {
00065                            if(sel_st >=start && sel_st <= start + allow_chars)
00066                             {
00067                              if(sel_en <= start + allow_chars)
00068                                  fillrect(x+_x + X_OFFSET + (sel_st - start) * 7,y + _y + Y_OFFSET,(sel_en - sel_st) * 7,9,0x3E0);
00069                              else
00070                                  fillrect(x+_x + X_OFFSET + (sel_st - start) * 7,y + _y + Y_OFFSET,(allow_chars + start - sel_st) * 7,9,0xE300);
00071                             }
00072                            else if(sel_en >=start && sel_en <= start + allow_chars)
00073                             {
00074                                  fillrect(x+_x + X_OFFSET,y + _y + Y_OFFSET,(sel_en - start) * 7,9,0x1F);
00075                             }
00076                           }
00077                          drawstring(x+_x + X_OFFSET ,y+_y+ Y_OFFSET, (char *)strmncpy( (void *)tmp,(void *)txt,start,allow_chars));
00078                          emboss3D(x+_x, y+_y, width, height,0,_true,1);
00079                       }
00080                       else
00081                       {
00082                          rect(x+_x, y+_y, width, height);
00083                          emboss3D(x+_x, y+_y, width, height,0,_true,1);
00084                       }
00085                       break;
00086 //      case FRM_TYPE :
00087 //                      frm_ptr = (frame *) cnt_ptr;
00088 //                      if((x + width <  frm_ptr->width) && (y + height < frm_ptr->height))
00089 //                         rect(x+frm_ptr->x,y+frm_ptr->y,width,height);
00090 
00091     }
00092   }
00093 
00094 
00095    void text::react(DW _x,DW _y) //Absolute x, y
00096    {
00097     POINT pt, loc;
00098     DW visi_chars;
00099     pt = getAbsLoc(this);
00100 
00101     visi_chars = total_chars - start;
00102     is_sel = false;
00103 
00104     if(iswithin(_x,_y,pt.x,pt.y,visi_chars*(CHAR_WIDTH+X_OFFSET)+X_OFFSET,height))
00105     {
00106      curs = (_x - pt.x - X_OFFSET)/(CHAR_WIDTH+X_OFFSET);
00107      loc.x = pt.x + curs * (CHAR_WIDTH + X_OFFSET) + X_OFFSET;
00108      loc.y = pt.y + Y_OFFSET;
00109 
00110      csr.setCursor(loc);
00111      csr.show();
00112     }
00113     else
00114     {
00115         curs = visi_chars;
00116         loc.x = pt.x + (curs * (CHAR_WIDTH + X_OFFSET)) + X_OFFSET;
00117         loc.y = pt.y + Y_OFFSET;
00118 
00119         csr.setCursor(loc);
00120         csr.show();
00121     }
00122     curs = curs + start;
00123 
00124    }
00125 
00126   void text::update_cursor(DW no)
00127   {
00128      POINT pt;
00129      pt = getAbsLoc(this);
00130 
00131      DW visi_chars = total_chars - start;
00132 
00133 //     (200,100,itoa(visi_chars,10));
00134 //     ginfo(200,110,itoa(allow_chars,10));
00135 //     delay(0xFFFF);
00136 
00137      if(visi_chars > allow_chars)
00138      {
00139       visi_chars = allow_chars;
00140      }
00141      if(no > start + allow_chars)
00142         start = no - allow_chars;
00143      if(no < start)
00144         start = no;
00145 
00146      if(width > visi_chars*(CHAR_WIDTH+X_OFFSET) + X_OFFSET)
00147      {
00148       pt.x += X_OFFSET + (DW)((no-start)*(CHAR_WIDTH+X_OFFSET));
00149       pt.y += Y_OFFSET;
00150       csr.setCursor(pt);
00151       csr.show();
00152      }
00153   }
00154 
00155    void text::keyPress(DW state,DW ch)
00156    {
00157      DW i;
00158      POINT pt;
00159      DW len;
00160 
00161      pt = getAbsLoc(this);
00162 
00163     if(state & ST_ALT)
00164      return;
00165     if(state & ST_CAPS)
00166         ch = toupper(ch);
00167         
00168      if(state & ST_SHIFT)
00169         ch = toggle_shift(ch);
00170     if(state & ST_CNTL)
00171     {
00172       switch(ch)
00173        {
00174          case 'c' :
00175                     if(is_sel)
00176                         {
00177                          copy();
00178                         }
00179                     break;
00180          case 'x' :
00181                     if(is_sel)
00182                         {
00183                           cut();
00184                         }
00185                     break;
00186          case 'v' :
00187                     if(cl_pres)
00188                      {
00189                         paste();
00190                         is_sel = false;
00191                      }
00192                     break;
00193        }
00194       return;
00195     }
00196 
00197     if(ch > ESC)
00198      {
00199      if(!(state & ST_SHIFT))
00200      {
00201 
00202       switch(ch)
00203        {
00204         case DEL        :
00205                          if(!is_sel)
00206                          {
00207                          if(total_chars > 0 && curs < total_chars)
00208                          {
00209                          for(i=curs;i<total_chars - 1;i++)
00210                           txt[i] = txt[i+1];
00211 
00212                          txt[total_chars-1] = '\0';
00213                          total_chars--;
00214                          }
00215                          }
00216                          else
00217                           {
00218                            len =  sel_en - sel_st;
00219 
00220                            for(i = sel_st;i<total_chars;i++)
00221                                 txt[i] = txt[i + len];
00222 
00223                            total_chars -= len;
00224                            txt[total_chars] = '\0';
00225                            curs = sel_st;
00226                            update_cursor(curs);
00227                           }
00228                          break;
00229         case LEFT_A     :
00230                          if(curs > 0)
00231                          {
00232                          curs--;
00233                          update_cursor(curs);
00234                          }
00235                          break;
00236         case RIGHT_A    :
00237                          if(curs < total_chars)
00238                          {
00239                          curs++;
00240                          update_cursor(curs);
00241                          }
00242                          break;
00243         case HOME       :
00244                          curs = 0;
00245                          update_cursor(curs);
00246                          break;
00247         case END        :
00248                          curs = total_chars;
00249                          update_cursor(curs);
00250                          break;
00251        }
00252        is_sel = false;
00253       }
00254     else
00255      {
00256       switch(ch)
00257        {
00258         case LEFT_A     :
00259                          if(curs > 0)
00260                          {
00261                          curs--;
00262                          update_cursor(curs);
00263                          if(is_sel)
00264                           {
00265                            if(curs > sel_st)
00266                             {
00267                              sel_en = curs;
00268                             }
00269                            else if(curs == sel_st)
00270                             is_sel = false;
00271                            else
00272                             sel_st = curs;
00273                           }
00274                          else
00275                           {
00276                            is_sel = true;
00277                            sel_st = curs;
00278                            sel_en = curs + 1;
00279                           }
00280                          }
00281                          break;
00282         case RIGHT_A    :
00283                          if(curs < total_chars)
00284                          {
00285                          curs++;
00286                          update_cursor(curs);
00287                          if(is_sel)
00288                           {
00289                            if(curs > sel_en)
00290                             {
00291                              sel_en = curs;
00292                             }
00293                            else if(curs = sel_en)
00294                             is_sel = false;
00295                            else
00296                             sel_st = curs;
00297                           }
00298                          else
00299                           {
00300                            is_sel = true;
00301                            sel_st = curs - 1;
00302                            sel_en = curs;
00303                           }
00304                          }
00305                          break;
00306         case HOME       :
00307                          if(curs != 0)
00308                          {
00309                          sel_en = curs;
00310                          sel_st = 0;
00311                          is_sel = true;
00312                          curs = 0;
00313                          update_cursor(curs);
00314                          }
00315                          break;
00316         case END        :
00317                          if(curs != total_chars)
00318                          {
00319                          sel_en = total_chars;
00320                          sel_st = curs;
00321                          is_sel = true;
00322                          curs = total_chars;
00323                          update_cursor(curs);
00324                          }
00325                          break;
00326        }
00327       }
00328      }
00329 
00330  if(ch < ESC)
00331  {
00332       is_sel = false;
00333       switch(ch)
00334         {
00335          case VK_BACKSPACE :
00336                         if(!(state & ST_SHIFT))
00337                         {
00338                         if(total_chars > 0 && curs <= total_chars && curs > 0)
00339                          {
00340                          for(i=curs-1;i<total_chars - 1;i++)
00341                           txt[i] = txt[i+1];
00342 
00343                          txt[total_chars-1] = '\0';
00344                          total_chars--;
00345                          curs--;
00346                          if(curs == start)
00347                          {
00348                           if(start > allow_chars)
00349                             start = start - allow_chars;
00350                           else
00351                             start = 0;
00352                          }
00353                          update_cursor(curs);
00354                          }
00355                          }
00356                         break;
00357         case VK_ENTER:
00358                         break;
00359          default :
00360                 if(total_chars >= TEXT_MAX)
00361                         return;
00362                 for(i=total_chars;i>curs;i--)
00363                         txt[i] = txt[i-1];
00364 
00365                 txt[total_chars+1] = '\0';
00366                 //     if(state & ST_SHIFT)
00367                 txt[curs] = ch; //+(_shift?-32:0);
00368 
00369                 total_chars++;
00370 
00371                 curs++;
00372                 update_cursor(curs);
00373         }
00374  }
00375                 restoreImage(mcurx,mcury);
00376                 paint();
00377                 if(mouse_inited)
00378                         {
00379                         getimage(mcurx,mcury,16,16, &mouseimg[0][0]);
00380                         }
00381                 mvMini2Svga(pt.x,pt.y,width,height);
00382 }
00383 
00384 void text::copy()
00385  {
00386    SDW i;
00387    if(!is_sel)
00388     return;
00389    for(i = sel_st;i<sel_en;i++)
00390     clip[i - sel_st] = txt[i];
00391 
00392    clip[sel_en - sel_st] = '\0';
00393    cl_pres = true;
00394  }
00395 void text::cut()
00396  {
00397    SDW i,j,len;
00398    POINT pt;
00399    pt =getAbsLoc(this);
00400 
00401    if(!is_sel)
00402     return;
00403 
00404    len =  sel_en - sel_st;
00405 
00406    for(i = sel_st;i<sel_en;i++)
00407     clip[i - sel_st] = txt[i];
00408 
00409    clip[len] = '\0';
00410    cl_pres = true;
00411 
00412    for(i = sel_st;i<total_chars;i++)
00413      txt[i] = txt[i + len];
00414 
00415    total_chars -= len;
00416    txt[total_chars] = '\0';
00417    is_sel = false;
00418 
00419    curs = sel_st;
00420    update_cursor(curs);
00421 
00422         restoreImage(mcurx,mcury);
00423         paint();
00424         if(mouse_inited)
00425                 {
00426                 getimage(mcurx,mcury,16,16, &mouseimg[0][0]);
00427                 }
00428         mvMini2Svga(pt.x,pt.y,width,height);
00429 
00430  }
00431 void text::paste()
00432  {
00433    SDW i,j,len;
00434    POINT pt;
00435    pt =getAbsLoc(this);
00436 
00437    if(!cl_pres)
00438     return;
00439 
00440       len = strlen(clip);
00441       for(i = 0;i<len;i++)
00442          {
00443           if(clip[i] == '\n')
00444            break;
00445          }
00446         len = i;
00447 
00448     for(i = total_chars - 1; i >= curs ; i--)
00449      txt[i + len] = txt[i];
00450 
00451     for(j = 0;j <len;j++)
00452     {
00453      txt[j+curs] = clip[j];
00454     }
00455     total_chars += len;
00456     txt[total_chars] = '\0';
00457 
00458         curs = curs + len;
00459         update_cursor(curs);
00460 
00461         restoreImage(mcurx,mcury);
00462         paint();
00463         if(mouse_inited)
00464                 {
00465                 getimage(mcurx,mcury,16,16, &mouseimg[0][0]);
00466                 }
00467         mvMini2Svga(pt.x,pt.y,width,height);
00468  }
00469   void text::setText(char * text)
00470    {
00471    POINT pt;
00472    pt =getAbsLoc(this);
00473    DW i;
00474 
00475         for(i = 0;i<strlen(text);i++)
00476          {
00477           if(text[i] == '\n')
00478            break;
00479           txt[i] = text[i];
00480          }
00481          txt[i] = '\0';
00482         total_chars = strlen(txt);
00483         restoreImage(mcurx,mcury);
00484         paint();
00485         if(mouse_inited)
00486                 {
00487                 getimage(mcurx,mcury,16,16, &mouseimg[0][0]);
00488                 }
00489         mvMini2Svga(pt.x,pt.y,width,height);
00490    }
00491   void text::getText(char * text)
00492    {
00493     strcpy(text,txt);
00494   }
00495 
00496 

Generated on Thu Jul 27 23:52:27 2006 for Dynacube by  doxygen 1.4.7