root/_TextViewer/BF_ToolsView.cpp

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. MessageReceived
  2. SetPos
  3. SetCodePage
  4. Draw
  5. MouseDown
  6. ClearView
  7. DrawPage
  8. Nav_LineDown
  9. Nav_LineUp
  10. Nav_LineLeft
  11. Nav_LineRight
  12. Nav_PageUp
  13. Nav_PageDown
  14. NextCodePage
  15. PrepareKeysMenu
  16. Find
  17. FindFirst
  18. FindNext
  19. ToUtf8
  20. FromUtf8
  21. GetConversion
  22. ClearMatch
  23. GetMatch

   1 #include <InterfaceKit.h>
   2 #include "BF_ToolsView.h"
   3 #include "BF_GUI_KeysMenu.h"
   4 #include "BF_Msg.h"
   5 #include "BF_GUI_Tools.h"
   6 #include "BF_GUI_MessageBox.h"
   7 #include "BF_Viewer_SearchCMD.h"
   8 
   9 #include <stdio.h>
  10 #include <SupportKit.h>
  11 
  12 /*===========================================================================
  13                                     BF_GUI_Status
  14 ===========================================================================*/
  15 // Constructor
  16 BF_GUI_Status::BF_GUI_Status(BRect o_frame, BF_GUI_Setup *po_Setup, char *pc_FileForView) : BView(o_frame,"status_view",B_FOLLOW_LEFT_RIGHT,B_WILL_DRAW)
  17 {
  18         poSysSetup = po_Setup;
  19         SetViewColor(SYS_COLOR(BF_COLOR_CURSOR_BACK));
  20         SetFont(&poSysSetup->oFontNode.oFont);
  21         BStringView *poFileName = new BStringView(BRect(0,0,StringWidth(pc_FileForView),poSysSetup->oFontNode.fHeight),"file_name",pc_FileForView);
  22         AddChild(poFileName);
  23         
  24         o_frame.bottom = poSysSetup->oFontNode.fHeight;
  25         o_frame.left = o_frame.right - 50;
  26         o_frame.right -= 5;
  27         poPos = new BStringView(o_frame,"POS","0%",B_FOLLOW_RIGHT|B_FOLLOW_TOP);
  28         poPos->SetAlignment(B_ALIGN_RIGHT);
  29         AddChild(poPos);
  30         
  31         o_frame.left -= 50;
  32         o_frame.right -= 50;
  33         poCode = new BStringView(o_frame,"CODE","UTF8",B_FOLLOW_RIGHT|B_FOLLOW_TOP);
  34         poCode->SetAlignment(B_ALIGN_LEFT);
  35         AddChild(poCode);
  36 }
  37 
  38 
  39 void
  40 BF_GUI_Status::MessageReceived(BMessage* po_Message)    /* [previous][next][first][last][top][bottom][index][help] */
  41 {
  42         float fPOS;
  43         
  44         switch(po_Message->what)
  45         {
  46                 case BF_MSG_VIEWER_MOVE:
  47                         if(po_Message->FindFloat("Position",&fPOS) == B_OK)
  48                         {
  49                                 SetPos(fPOS);
  50                         };
  51                         break;
  52                 default:
  53                         BView::MessageReceived(po_Message);
  54         }
  55 }
  56 
  57 // display position on the status bar
  58 void
  59 BF_GUI_Status::SetPos(float f_Pos)    /* [previous][next][first][last][top][bottom][index][help] */
  60 {
  61         char cBuffer[10];
  62         int iProc = (int)(f_Pos * 100);
  63         sprintf(cBuffer,"%u%%",(iProc > 100) ? 100 : iProc);
  64         poPos->SetText(cBuffer);
  65 };
  66 
  67 
  68 // display codepage on the status bar
  69 void 
  70 BF_GUI_Status::SetCodePage(uint32 i_CodePage)    /* [previous][next][first][last][top][bottom][index][help] */
  71 {
  72         char *pcCodePage[] = {"UTF8","DOS","WIN","KOI8R"};
  73         
  74         poCode->SetText(pcCodePage[i_CodePage]);
  75 };
  76 
  77 /*===========================================================================
  78                                     BF_GUI_Text
  79 ===========================================================================*/
  80 BF_GUI_Text::BF_GUI_Text(       
  81         BRect o_frame,
  82         BList *plo_String, 
  83         BF_GUI_Status *po_Status,
  84         BHandler *po_Handler
  85 ):BView(        o_frame,"text_view",B_FOLLOW_ALL_SIDES,B_WILL_DRAW)
  86 {
  87         ploString = plo_String;
  88         poStatus = po_Status;
  89         iCodePage = CP_UTF8;
  90         SetViewColor(SYS_COLOR(BF_COLOR_BACK));
  91         SetFont(&poSysSetup->oFontNode.oFont);
  92         bFullPage = false;
  93         poHandler = po_Handler; 
  94         iStartSel = 0;
  95         iStopSel = 0;
  96         iMatchLine = 0;
  97         poMatch = NULL;
  98         bDrawSel = false;
  99 }
 100 
 101 BF_GUI_Text::~BF_GUI_Text()
 102 {
 103         if(poMatch != NULL) delete(poMatch);
 104 }
 105 
 106 void 
 107 BF_GUI_Text::Draw(BRect oRect)    /* [previous][next][first][last][top][bottom][index][help] */
 108 {
 109         DrawPage();
 110 }
 111 
 112 
 113 void
 114 BF_GUI_Text::MouseDown(BPoint o_point)    /* [previous][next][first][last][top][bottom][index][help] */
 115 {
 116         printf("BF_GUI_Text::MouseDown()\n");
 117         BRect oRect = Bounds();
 118         if (o_point.y > oRect.top && o_point.y < oRect.top + oRect.Height() / 2)
 119         {
 120                 Nav_PageUp();
 121         }
 122         else
 123         {
 124                 Nav_PageDown();
 125         }
 126 };
 127 
 128 // clear current page
 129 void BF_GUI_Text::ClearView()    /* [previous][next][first][last][top][bottom][index][help] */
 130 {
 131         BRect oRect = Bounds();
 132         SetHighColor(SYS_COLOR(BF_COLOR_BACK));
 133         FillRect(oRect);
 134 };
 135 
 136 // draw current page
 137 void 
 138 BF_GUI_Text::DrawPage()    /* [previous][next][first][last][top][bottom][index][help] */
 139 {
 140         BRect   oRect;
 141         BPoint  oPoint(3,poSysSetup->oFontNode.fHeight);
 142         int32   iCurLine;
 143         BString *psLine;
 144         char    cBuffer[256];
 145         
 146         oRect = Bounds();
 147         
 148         SetHighColor(SYS_COLOR(BF_COLOR_NODE));
 149         SetLowColor(SYS_COLOR(BF_COLOR_BACK));
 150         for(iCurLine = 0;oPoint.y < oRect.bottom + poSysSetup->oFontNode.fHeight && iCurLine < ploString->CountItems();iCurLine++)
 151         {
 152                 psLine = (BString *) ploString->ItemAt(iCurLine);
 153 
 154                 ToUtf8(psLine->String(),cBuffer);
 155 
 156                 BString oCurStr(cBuffer);
 157                 oCurStr.ReplaceAll("\r","\x0");
 158                 oCurStr.ReplaceAll("\n","\x0");
 159                 oCurStr.ReplaceAll("\t","        ");
 160                 if (bDrawSel && iMatchLine == iCurLine)
 161                 {
 162                         BString oBuffer;
 163                         // draw first part of the string
 164                         iStartSel = oCurStr.FindFirst(poMatch->String(),iStartSel);
 165                         iStopSel = poMatch->CountChars();
 166                         oCurStr.MoveInto(oBuffer,0,iStartSel);
 167                         DrawString(oBuffer.String(),oPoint);
 168                         // draw selected part of the string
 169                         SetHighColor(SYS_COLOR(BF_COLOR_CURSOR_BACK));
 170                         float fStartPoint = StringWidth(oBuffer.String());
 171                         float fWidth = StringWidth(poMatch->String());
 172                         BRect oRect(oPoint.x + fStartPoint,oPoint.y - poSysSetup->oFontNode.fHeight,oPoint.x + fStartPoint + fWidth,oPoint.y);
 173                         FillRect(oRect);
 174                         SetHighColor(0,0,0);
 175                         SetLowColor(SYS_COLOR(BF_COLOR_CURSOR_BACK));
 176                         oCurStr.MoveInto(oBuffer,0,iStopSel);
 177                         DrawString(oBuffer.String());
 178                         // draw rest part of the string
 179                         SetHighColor(SYS_COLOR(BF_COLOR_NODE));
 180                         SetLowColor(SYS_COLOR(BF_COLOR_BACK));
 181                         DrawString(oCurStr.String());
 182                 }
 183                 else
 184                 {
 185                         // draw string without selection
 186                         DrawString(oCurStr.String(),oPoint);
 187                 };
 188                 oPoint.y += poSysSetup->oFontNode.fHeight;
 189         };
 190 //-------------------------------- change display position ----------------------------
 191 /*
 192         BMessage *poMSG = new BMessage(BF_MSG_VIEWER_MOVE);
 193         float fPOS = oPoint.y / (poSysSetup->oFontNode.fHeight * ploString->CountItems());
 194         poMSG->AddFloat("Position",fPOS);
 195         BMessenger oMessenger(poStatus);
 196         oMessenger.SendMessage(poMSG);
 197 */
 198 //------------------------------ TRUE if page is full ---------------------------------
 199         bFullPage = (oPoint.y < oRect.bottom + poSysSetup->oFontNode.fHeight) ? false : true;
 200 };
 201 
 202 // Line down action
 203 void 
 204 BF_GUI_Text::Nav_LineDown()    /* [previous][next][first][last][top][bottom][index][help] */
 205 {
 206         if (bFullPage)
 207         {
 208                 ScrollBy(0,20);
 209                 ClearMatch();
 210         };              
 211 };
 212 
 213 // Line up action
 214 void 
 215 BF_GUI_Text::Nav_LineUp()    /* [previous][next][first][last][top][bottom][index][help] */
 216 {
 217         BRect oRect = Bounds();
 218         
 219         if (oRect.top > 0)
 220         {
 221                 ScrollBy(0,(oRect.top > 20) ? -20 : -oRect.top);
 222                 ClearMatch();
 223         };              
 224 };
 225 
 226 // Key left action
 227 void 
 228 BF_GUI_Text::Nav_LineLeft()    /* [previous][next][first][last][top][bottom][index][help] */
 229 {
 230         BRect oRect = Bounds();
 231         
 232         if (oRect.left > 0)
 233         {
 234                 ScrollBy(-10,0);
 235                 ClearMatch();
 236         };
 237 };
 238 
 239 // Key right action
 240 void 
 241 BF_GUI_Text::Nav_LineRight()    /* [previous][next][first][last][top][bottom][index][help] */
 242 {
 243         ScrollBy(10,0);
 244         ClearMatch();
 245 };
 246 
 247 // page up action
 248 void 
 249 BF_GUI_Text::Nav_PageUp()    /* [previous][next][first][last][top][bottom][index][help] */
 250 {
 251         BRect   oRect = Bounds();
 252         
 253         if (oRect.top <= 0) return;
 254 
 255         if (2 * oRect.top - oRect.bottom > 0)
 256         {
 257                 ScrollBy(0,-oRect.Height());
 258         }
 259         else
 260         {
 261                 ScrollBy(0,-oRect.top);
 262         };
 263         ClearMatch();
 264 }
 265 
 266 // Page down action
 267 void 
 268 BF_GUI_Text::Nav_PageDown()    /* [previous][next][first][last][top][bottom][index][help] */
 269 {
 270         if (bFullPage)
 271         {
 272                 BRect oRect = Bounds();
 273                 if (2 * oRect.bottom - oRect.top > poSysSetup->oFontNode.fHeight * ploString->CountItems())
 274                 {
 275                         ScrollTo(oRect.left,poSysSetup->oFontNode.fHeight * ploString->CountItems() - oRect.Height());
 276                 }
 277                 else
 278                 {
 279                         ScrollBy(0,oRect.Height());
 280                 }
 281                 ClearMatch();
 282         }
 283 }
 284 
 285 
 286 // set and display next in list codepage
 287 void 
 288 BF_GUI_Text::NextCodePage()    /* [previous][next][first][last][top][bottom][index][help] */
 289 {
 290         iCodePage = (iCodePage < CP_KOI) ? iCodePage + 1 : CP_UTF8;
 291         poStatus->SetCodePage(iCodePage);
 292         ClearView();
 293         DrawPage();
 294 };
 295 
 296 
 297 void
 298 BF_GUI_Text::PrepareKeysMenu()    /* [previous][next][first][last][top][bottom][index][help] */
 299 {
 300         if(!poSysKeysMenu) return;
 301         iKeysModifiers = modifiers();
 302         
 303         poSysKeysMenu->Clear();
 304         if (iKeysModifiers & B_LEFT_SHIFT_KEY || iKeysModifiers & B_RIGHT_SHIFT_KEY)
 305         {
 306                 poSysKeysMenu->SetText(7,"Next");
 307                 
 308                 BMessage *poMessage = new BMessage(BF_MSG_VIEWER_SEARCH);
 309                 poMessage->AddInt32("bf_viewer_search_cmd",BF_VIEWER_CMD_SEARCH_NEXT);
 310                 
 311                 poSysKeysMenu->SetMessage(7,poMessage,poHandler);
 312         }
 313         else
 314         {
 315                 poSysKeysMenu->SetText(3,"Close");
 316                 poSysKeysMenu->SetText(8,"CP");
 317                 poSysKeysMenu->SetText(10,"Close");
 318                 poSysKeysMenu->SetText(7,"Search");
 319 
 320                 poSysKeysMenu->SetMessage(3,new BMessage(BF_MSG_VIEWER_CLOSED),poHandler);
 321                 poSysKeysMenu->SetMessage(8,new BMessage(BF_MSG_VIEWER_CH_CP),poHandler);
 322                 poSysKeysMenu->SetMessage(10,new BMessage(BF_MSG_VIEWER_CLOSED),poHandler);
 323                 BMessage *poMessage = new BMessage(BF_MSG_VIEWER_SEARCH);
 324                 poMessage->AddInt32("bf_viewer_search_cmd",BF_VIEWER_CMD_SEARCH_DIALOG);
 325                 poSysKeysMenu->SetMessage(7,poMessage,poHandler);
 326         };
 327 }
 328 
 329 void
 330 BF_GUI_Text::Find(char *pc_Sample, uint32 i_StartLine, uint32 i_StartCol)    /* [previous][next][first][last][top][bottom][index][help] */
 331 {
 332         uint32  iColumn = i_StartCol;
 333         BString *poString;
 334         char cSample[256];
 335         bool bFound = false;
 336         BF_GUI_MessageBox *po_Message;
 337 
 338         ClearMatch();
 339         if (pc_Sample == NULL) 
 340         {
 341                 FromUtf8(poMatch->String(),cSample);
 342         }
 343         else
 344         {
 345                 FromUtf8(pc_Sample,cSample);
 346         }
 347 
 348         for(int32 i = i_StartLine; i < ploString->CountItems();i++)
 349         {
 350                 poString = (BString *) ploString->ItemAt(i);
 351                 if (poString->FindFirst(cSample,iColumn) != B_ERROR)
 352                 {
 353                         iMatchLine = i;
 354                         bFound = true;
 355                         break;
 356                 };
 357                 iColumn = 0;
 358         };
 359         if (bFound)
 360         {
 361                 if (pc_Sample != NULL)
 362                 {
 363                         if (poMatch != NULL) delete(poMatch);
 364                         poMatch = new BString(pc_Sample);
 365                 };
 366                 bDrawSel = true;
 367                 BRect oRect = Bounds();
 368                 float y = iMatchLine * poSysSetup->oFontNode.fHeight;
 369                 ScrollTo(oRect.left,y);
 370                 iStartSel = iColumn;
 371                 DrawPage();
 372         }
 373         else
 374         {
 375                 bDrawSel = false;
 376                 if (i_StartLine)
 377                 {
 378                         po_Message = new BF_GUI_MessageBox(BRect(30,30,300,150),"Error","Not more samles found!",Parent(),"Ok");
 379                 }
 380                 else
 381                 {
 382                         po_Message = new BF_GUI_MessageBox(BRect(30,30,300,150),"Error","The sample is not found in this file!",Parent(),"Ok");
 383                 }
 384                 Parent()->AddChild(po_Message);
 385         };
 386 };
 387 
 388 void
 389 BF_GUI_Text::FindFirst(char *pc_Sample)    /* [previous][next][first][last][top][bottom][index][help] */
 390 {
 391 
 392         iMatchLine = 0;
 393         iStartSel = 0;
 394         iStopSel = 0;
 395 
 396         Find(pc_Sample);
 397 };
 398 
 399 void
 400 BF_GUI_Text::FindNext()    /* [previous][next][first][last][top][bottom][index][help] */
 401 {
 402         Find(NULL,iMatchLine,iStartSel + iStopSel);
 403 };
 404 
 405 void
 406 BF_GUI_Text::ToUtf8(const char *pc_Source,char *pc_Dest)    /* [previous][next][first][last][top][bottom][index][help] */
 407 {
 408         int32   iIN;
 409         int32   iOUT;
 410         int32   iStatus;
 411         uint32  iCP = GetConversion();
 412 
 413                 if ( iCodePage == CP_UTF8 )
 414                 {
 415                         strcpy(pc_Dest,pc_Source);
 416                 }
 417                 else
 418                 {
 419                         iIN = strlen(pc_Source);
 420                         iOUT = 255;
 421                         convert_to_utf8(iCP,pc_Source,&iIN,pc_Dest,&iOUT,&iStatus);
 422                         pc_Dest[iOUT] = 0;
 423                 }
 424 }
 425 
 426 void
 427 BF_GUI_Text::FromUtf8(const char *pc_Source,char *pc_Dest)    /* [previous][next][first][last][top][bottom][index][help] */
 428 {
 429         int32   iIN;
 430         int32   iOUT;
 431         int32   iStatus;
 432         uint32  iCP = GetConversion();
 433 
 434                 if ( iCodePage == CP_UTF8 )
 435                 {
 436                         strcpy(pc_Dest,pc_Source);
 437                 }
 438                 else
 439                 {
 440                         iIN = strlen(pc_Source);
 441                         iOUT = 255;
 442                         convert_from_utf8(iCP,pc_Source,&iIN,pc_Dest,&iOUT,&iStatus);
 443                         pc_Dest[iOUT] = 0;
 444                 }
 445 }
 446 
 447 
 448 uint32
 449 BF_GUI_Text::GetConversion()    /* [previous][next][first][last][top][bottom][index][help] */
 450 {
 451         uint32  iCP = 0;
 452                 switch (iCodePage )
 453                 {
 454                         case CP_DOS:
 455                                 iCP = B_MS_DOS_866_CONVERSION;
 456                                 break;
 457                         case CP_WIN:
 458                                 iCP = B_MS_WINDOWS_1251_CONVERSION;
 459                                 break;
 460                         case CP_KOI:
 461                                 iCP = B_KOI8R_CONVERSION;
 462                                 break;
 463                 };
 464                 return(iCP);
 465 };
 466 
 467 void
 468 BF_GUI_Text::ClearMatch( void )    /* [previous][next][first][last][top][bottom][index][help] */
 469 {
 470         if(bDrawSel)
 471         {
 472                 bDrawSel = false;
 473                 ClearView();
 474                 DrawPage();
 475         };
 476 }
 477 
 478 char
 479 *BF_GUI_Text::GetMatch( void )    /* [previous][next][first][last][top][bottom][index][help] */
 480 {
 481         if(poMatch == NULL)
 482         {
 483                 return(NULL);
 484         }
 485         else
 486         {
 487                 return poMatch->String(); 
 488         };
 489 };

/* [previous][next][first][last][top][bottom][index][help] */