root/_TextViewer/BF_GUI_TextViewer.cpp

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

DEFINITIONS

This source file includes following definitions.
  1. SetFileNameTo
  2. FileSize
  3. CloseFile
  4. OpenFile
  5. LoadText_BeforePos
  6. LoadText_FromNextLine
  7. LoadText
  8. Draw
  9. SetVarsTo
  10. CalcVars
  11. CloseViewer
  12. KeyDown
  13. Search_Step0
  14. Search_Step1
  15. Search_Run
  16. MessageReceived
  17. ChangeCodePage
  18. Draw
  19. DrawLine
  20. MouseDown
  21. MakeFocus
  22. PrepareKeysMenu
  23. LoadPage
  24. Nav_PageUp
  25. Nav_LineUp
  26. OpenFile
  27. Nav_LineDown
  28. Nav_PageDown
  29. Nav_End
  30. Nav_GoPos
  31. UpdateStatusBar
  32. ToUtf8
  33. FromUtf8
  34. GetConversion
  35. Nav_Right
  36. Nav_Left
  37. BF_GUI_TextViewer_Run

   1 /*
   2 ===============================================
   3 Project:        BeFAR
   4 File:           BF_GUI_TextViewer.cpp   
   5 Desc:           Provides viewing textfile.
   6 Author:         by Baza, special thanks to Pavel Popov
   7 Created:        06.01.2000
   8 Modified:       16 jul 2000
   9 ===============================================
  10 */
  11 
  12 #include <stdio.h>
  13 #include <UTF8.h>
  14 
  15 #include "BL_Tools.h"
  16 #include "BL_File.h"
  17 
  18 #include "BF_Msg.h"
  19 #include "BF_Dict.h"
  20 #include "BF_GUI_KeysMenu.h"
  21 #include "BF_GUI_DlgViews.h"
  22 #include "BF_GUI_TextViewer.h"
  23 #include "BF_GUI_Func.h"
  24 #include "BF_GUI_WinMain.h"
  25 
  26 ///////////////////////////////////////////////////////////////////////////////////
  27 BF_TextFile_Line::BF_TextFile_Line(const BL_String & s_Src,off_t i_Pos,int32 i_Bytes)
  28 {
  29         s = s_Src;
  30         iBytes=i_Bytes;
  31         iPos = i_Pos;
  32 }       
  33 
  34 ///////////////////////////////////////////////////////////////////////////////////
  35 BF_TextFile_Buffer::BF_TextFile_Buffer()
  36 {       
  37         iFileSize = 0;
  38 }
  39 
  40 void                            
  41 BF_TextFile_Buffer::SetFileNameTo(const char *pc_FileName)    /* [previous][next][first][last][top][bottom][index][help] */
  42 {
  43         ASSERT(pc_FileName);
  44         sFileName = pc_FileName;
  45 }
  46 
  47 off_t                           
  48 BF_TextFile_Buffer::FileSize()    /* [previous][next][first][last][top][bottom][index][help] */
  49 {
  50         if(iFileSize>0) return iFileSize;
  51         
  52         OpenFile();     
  53         if(B_OK!=oFile.GetSize(&iFileSize)) return 0;
  54         CloseFile();
  55         return iFileSize;
  56 }
  57 
  58 void
  59 BF_TextFile_Buffer::CloseFile()    /* [previous][next][first][last][top][bottom][index][help] */
  60 {
  61         oFile.Unset();
  62 }
  63 
  64 status_t
  65 BF_TextFile_Buffer::OpenFile()    /* [previous][next][first][last][top][bottom][index][help] */
  66 {       
  67         ASSERT(sFileName!="");
  68         CloseFile();
  69         return oFile.SetTo(sFileName.String(),B_READ_ONLY);             
  70 }
  71 
  72 int32 
  73 BF_TextFile_Buffer::LoadText_BeforePos(off_t & i_LastPos,int32 i_LinesCount,BL_List & lo_Data)    /* [previous][next][first][last][top][bottom][index][help] */
  74 {
  75         off_t iLastPos = i_LastPos;
  76         lo_Data.DeleteItems();
  77 
  78 
  79         while(lo_Data.CountItems()<i_LinesCount){
  80 
  81                 bool    bFrom0 = iLastPos<=5000;
  82                 off_t   iFromPos = bFrom0?0:(iLastPos - 5000);
  83         
  84                 BL_List loData1;
  85                 int32 iReqCount = i_LinesCount - lo_Data.CountItems();
  86                 int32 iLoadedLines = LoadText(iFromPos,100000000,loData1,true,i_LastPos);
  87                 if(iLoadedLines <= iReqCount){
  88                         lo_Data.AddList(&loData1,0);
  89                         loData1.MakeEmpty();
  90                         break;
  91                 }else
  92                 if(iLoadedLines > iReqCount){
  93                         for(int i=0;i<(iLoadedLines - iReqCount);i++){
  94                                 loData1.DeleteItemAt(0);
  95                                 
  96                         }
  97                         lo_Data.AddList(&loData1,0);
  98                         loData1.MakeEmpty();
  99                         break;
 100                 }else{
 101                         iLastPos -= 5000;
 102                 }
 103         }               
 104         
 105         BF_TextFile_Line *poLine = (BF_TextFile_Line*)lo_Data.ItemAt(0);
 106         ASSERT(poLine);
 107         i_LastPos = poLine->Pos();
 108         
 109         return lo_Data.CountItems();
 110 }
 111 
 112 int32
 113 BF_TextFile_Buffer::LoadText_FromNextLine(off_t & i_StartPos,int32 i_LinesCount,BL_List & lo_Data,bool b_DeleteFirstLines)    /* [previous][next][first][last][top][bottom][index][help] */
 114 {       
 115         // calc new pos //
 116         BF_TextFile_Line *poLine = (BF_TextFile_Line*)lo_Data.LastItem();
 117         if(!poLine) return 0;
 118         off_t iNewPos = poLine->Pos()+poLine->Bytes();
 119 
 120         int32 iLinesLoaded = LoadText(iNewPos,i_LinesCount,lo_Data,false);
 121         if(iLinesLoaded>0){
 122                 i_StartPos = iNewPos;           
 123                 
 124                 if(b_DeleteFirstLines) for(int i=0;i<iLinesLoaded;i++)  lo_Data.DeleteItemAt(0);
 125                 
 126                 BF_TextFile_Line *poLine = (BF_TextFile_Line*)lo_Data.ItemAt(0);
 127                 if(!poLine) return 0;
 128                 i_StartPos = poLine->Pos();
 129                                 
 130         }
 131         
 132         return iLinesLoaded;
 133 }
 134 
 135 int32
 136 BF_TextFile_Buffer::LoadText(off_t i_StartPos,int32 i_LinesCount,BL_List & lo_Data,bool b_DeleteData,off_t i_LastPos)    /* [previous][next][first][last][top][bottom][index][help] */
 137 {
 138         status_t        uRes=B_OK;
 139         int32           iLinesLoaded=0;
 140         off_t           iPos=0;
 141         BL_String       s;
 142         // open file //
 143         uRes = OpenFile();
 144         if(B_OK!=uRes) return -1;       
 145         if(b_DeleteData) lo_Data.DeleteItems();
 146         
 147         // seek pos //  
 148         iPos = oFile.Seek(i_StartPos,SEEK_SET);
 149         if(iPos!=i_StartPos) return -1;
 150         
 151         // load lines
 152         BF_TextFile_Line        *poLine;
 153         for(int iLine=0;iLine<i_LinesCount;iLine++){
 154                 if(B_OK!=oFile.ReadString(&s)) break;
 155                 
 156                 poLine = new BF_TextFile_Line(s,iPos,oFile.Position()-iPos);
 157                 lo_Data.AddItem(poLine);
 158                 
 159                 iLinesLoaded++;
 160                 iPos = oFile.Position();
 161                 if((i_LastPos>=0) && (iPos>=i_LastPos)) break;
 162         }       
 163         oFile.GetSize(&iFileSize);
 164         CloseFile();
 165         return iLinesLoaded;
 166 }
 167 
 168 /*===========================================================================
 169                                     BF_GUI_TextViewer_StatusBar
 170 ===========================================================================*/
 171 
 172 BF_GUI_TextViewer_StatusBar::BF_GUI_TextViewer_StatusBar(BRect & o_Rect,const char *pc_FileName)
 173 :BView(o_Rect,"textviewer_status",B_FOLLOW_LEFT_RIGHT|B_FOLLOW_TOP,B_WILL_DRAW)
 174 {
 175         iFilePos = 0;
 176         iPageSize = 0;
 177         iFileSize = 0;
 178         iCodePage = CP_UTF8;
 179         ASSERT(pc_FileName);
 180         sFileName = pc_FileName;        
 181         
 182         SetViewColor(B_TRANSPARENT_COLOR);
 183         SetFont(&poSysSetup->oFontNode.oFont);  
 184         
 185 }
 186 
 187 void                    
 188 BF_GUI_TextViewer_StatusBar::Draw(BRect o_Rect)    /* [previous][next][first][last][top][bottom][index][help] */
 189 {
 190         BRect           oRect;
 191         BPoint          oPoint;
 192         BL_String       s,s1;
 193         //
 194         SetHighColor(SYS_COLOR(BF_COLOR_TEXTVIEWER_STATUS_BACK));
 195         oRect = Bounds();       
 196         FillRect(oRect);
 197         //              
 198         SetHighColor(SYS_COLOR(BF_COLOR_TEXTVIEWER_STATUS_TEXT)); 
 199         SetLowColor(SYS_COLOR(BF_COLOR_TEXTVIEWER_STATUS_BACK));        
 200         //
 201         oPoint.x = 5;
 202         oPoint.y = poSysSetup->oFontNode.fAscent;
 203         // draw file_pos/size //
 204         s=BF_DictAt(BF_DICT_TEXTVIEWER_POS);
 205         s<<": ";
 206         s1=iFilePos;            
 207         s<<*s1.SetDigits();
 208         s<<"..";
 209         s1=(iFilePos+iPageSize);
 210         s<<*s1.SetDigits();
 211         s<<"/";
 212         s1=iFileSize;
 213         s<<*s1.SetDigits();
 214         DrawString(s.String(),oPoint);
 215         oPoint.x += poSysSetup->oFontNode.oFont.StringWidth(s.String())+10;
 216         // draw startcol //     
 217         s=BF_DictAt(BF_DICT_TEXTVIEWER_COL);
 218         s<<":";
 219         s<<iStartColumn;
 220         DrawString(s.String(),oPoint);
 221         oPoint.x += poSysSetup->oFontNode.oFont.StringWidth(s.String())+10;
 222         // draw code_page //
 223         s = BF_DictAt(BF_DICT_TEXTVIEWER_CP);
 224         s<<": ";
 225         switch(iCodePage){
 226         case CP_UTF8:
 227                 s<<"UTF8";
 228                 break;
 229         case CP_DOS:
 230                 s<<"DOS866";
 231                 break;
 232         case CP_WIN:
 233                 s<<"WIN1251";
 234                 break;
 235         case CP_KOI:
 236                 s<<"KOI8R";     
 237                 break;
 238         default:
 239                 s<<"unknown";
 240         };
 241         DrawString(s.String(),oPoint);
 242         oPoint.x += poSysSetup->oFontNode.oFont.StringWidth(s.String())+10;
 243         // draw file_name //
 244         {               
 245                 s=BF_DictAt(BF_DICT_FILE);
 246                 s<<": ";
 247                 s<<sFileName;
 248                 DrawString(s.String(),oPoint);
 249         }
 250 }
 251 
 252 void                            
 253 BF_GUI_TextViewer_StatusBar::SetVarsTo(off_t i_FilePos,off_t i_PageSize,off_t i_FileSize,int32 i_CodePage,int32 i_StartColumn)    /* [previous][next][first][last][top][bottom][index][help] */
 254 {
 255         iFilePos = i_FilePos;
 256         iPageSize = i_PageSize;
 257         iFileSize = i_FileSize;
 258         iCodePage = i_CodePage;
 259         iStartColumn = i_StartColumn;
 260         if(Parent()) Invalidate(Bounds());
 261 }
 262 
 263 /*===========================================================================
 264                                     BF_GUI_TextViewer
 265 ===========================================================================*/
 266 BF_GUI_TextViewer::BF_GUI_TextViewer(   
 267         BRect o_frame,
 268         const char      *pcFileForView
 269 ):BView(o_frame,"textviewer_",B_FOLLOW_ALL_SIDES,B_WILL_DRAW|B_NAVIGABLE)
 270 {               
 271         ASSERT(pcFileForView);
 272         sFileName = pcFileForView ;
 273         
 274         poLastFocus = poWin->CurrentFocus();
 275         
 276                 
 277         iCodePage = CP_UTF8;
 278         SetViewColor(B_TRANSPARENT_COLOR);
 279         SetFont(&poSysSetup->oFontNode.oFont);  
 280         bWrapped = false;
 281         bShowSpec = false;
 282         
 283         iFilePos = 0;
 284         iFileSize = 0;
 285         iStartColumn = 0;
 286         iSearchPos = -1;
 287         
 288         oBuffer.SetFileNameTo(pcFileForView);
 289         
 290         CalcVars();
 291         
 292         LoadPage();
 293         UpdateStatusBar();
 294                 
 295 }
 296 
 297 BF_GUI_TextViewer::~BF_GUI_TextViewer()
 298 {       
 299 }
 300 
 301 
 302 void
 303 BF_GUI_TextViewer::CalcVars()    /* [previous][next][first][last][top][bottom][index][help] */
 304 {
 305         fFontHeight = poSysSetup->oFontNode.fHeight;
 306         iLinesCount = (int32) ( Bounds().Height()/fFontHeight);
 307 }
 308 
 309 void
 310 BF_GUI_TextViewer::CloseViewer()    /* [previous][next][first][last][top][bottom][index][help] */
 311 {
 312         BF_GUI_TextViewer_StatusBar *poStatus = (BF_GUI_TextViewer_StatusBar*)Parent()->FindView("textviewer_status");
 313         if(poStatus){
 314                 poStatus->RemoveSelf();
 315                 DELETE(poStatus);
 316         }
 317         
 318         if(poLastFocus){
 319                 BMessage oMessage(BF_MSG_TEXTVIEWER_CLOSED);            
 320                 BMessenger oMessenger(poLastFocus);
 321                 oMessenger.SendMessage(&oMessage);
 322         }       
 323         RemoveSelf();   
 324         delete this;
 325 }
 326 
 327 void            
 328 BF_GUI_TextViewer::KeyDown(const char *bytes, int32 numBytes)    /* [previous][next][first][last][top][bottom][index][help] */
 329 {       
 330         if ( numBytes == 1 )
 331         switch ( bytes[0] ) {
 332                 case B_RIGHT_ARROW:
 333                         Nav_Right();
 334                         return;         
 335                 case B_LEFT_ARROW:
 336                         Nav_Left();
 337                         return;                                 
 338                 case B_UP_ARROW:
 339                         Nav_LineUp();
 340                         return; 
 341                 case B_DOWN_ARROW:
 342                         Nav_LineDown();
 343                         return;
 344                 case B_PAGE_DOWN:
 345                         Nav_PageDown();
 346                         return;
 347                 case B_PAGE_UP:
 348                         Nav_PageUp();
 349                         return;                 
 350                 case B_ESCAPE:
 351                         CloseViewer();
 352                         return;
 353                 case B_HOME:{
 354                         Nav_GoPos(0);
 355                         return;}
 356                 case B_END:
 357                         Nav_End();
 358                         return;
 359         }       
 360         if(poSysKeysMenu && poSysKeysMenu->OnKeyDown(bytes,numBytes)) return;   
 361         BView::KeyDown(bytes,numBytes);
 362 }
 363 
 364 void                            
 365 BF_GUI_TextViewer::Search_Step0()    /* [previous][next][first][last][top][bottom][index][help] */
 366 {
 367         /* */
 368         BF_GUI_Func_PanelsEnable(false);
 369         /* make dialog */       
 370         BMessage oMessage(BF_MSG_TEXTVIEWER_SEARCH1);
 371         oMessage.AddPointer("bf_focus",this);
 372         oMessage.AddPointer("bf_oldfocus",poWin->CurrentFocus());
 373                         
 374         BF_GUI_Dialog *poDialog = new BF_GUI_Dialog(BRect(0,0,300,0),
 375                 BF_DictAt(BF_DICT_TEXTVIEWER_SEARCH_DLG),"dialog",oMessage,BG_GUI_DIALOG_WINRESIZE_MOVE_CENTER);
 376         BRect oRect;    
 377         /* insert edit */
 378         poDialog->LocalBounds(oRect);   
 379         oRect.bottom = oRect.top+poSysSetup->oFontToolView.fHeight;
 380         BF_GUI_ViewEdit_Create(oRect,BF_DictAt(BF_DICT_TEXTVIEWER_SEARCH_DLGTEXT),poDialog,"bf_cText",
 381                                         sSearchText.String(),B_FOLLOW_LEFT_RIGHT|B_FOLLOW_TOP,B_NAVIGABLE);
 382         /* menu */                                                              
 383         poDialog->AddOkCancelMenu(oRect);       
 384         /* finish */    
 385         poDialog->SetHeightFromLastChild();
 386         poDialog->MoveToCenter( poWinView );    
 387         BF_GUI_Func_AddChildToMainView ( poDialog );    
 388 }
 389 
 390 void
 391 BF_GUI_TextViewer::Search_Step1(BMessage & o_Message)    /* [previous][next][first][last][top][bottom][index][help] */
 392 {
 393         BF_GUI_Func_PanelsEnable(true);
 394         MakeFocus(true);
 395         
 396         const char *pcText=NULL;
 397         if(B_OK!=o_Message.FindString("bf_cText",&pcText) || !pcText) return;
 398         
 399         iSearchPos = -1;
 400         sSearchText = pcText;
 401         
 402         Search_Run();
 403 }
 404 
 405 void
 406 BF_GUI_TextViewer::Search_Run()    /* [previous][next][first][last][top][bottom][index][help] */
 407 {
 408         BF_TextFile_Line *po=NULL;
 409         int32 iLine=0;
 410         
 411         if(iSearchPos<0) iSearchPos++;
 412 
 413         int32   iOldPos = iSearchPos;
 414         int32   iCycles = 0;
 415         int32   iPosInLine = -1;
 416         BL_List lo;
 417         
 418         while(true){    
 419                 int32 iLinesLoaded = oBuffer.LoadText(iSearchPos,100,lo);
 420                 
 421                 for(iLine=0;iLine<iLinesLoaded;iLine++){
 422                         po = (BF_TextFile_Line*)lo.ItemAt(iLine);
 423                         iPosInLine = po->s.FindFirst(sSearchText.String());
 424                         if(iPosInLine>=0) break;
 425                 }               
 426                 
 427                 if(iPosInLine || (iLinesLoaded<iSearchPos))break;       
 428                 
 429                 po = (BF_TextFile_Line*)lo.LastItem();
 430                 ASSERT(po);
 431                 iSearchPos = po->Pos() + po->Bytes();                   
 432         }
 433         
 434         if(iPosInLine>=0){
 435                 loData.DeleteItems();
 436                 loData.AddList(&lo);
 437                 lo.MakeEmpty();
 438                 
 439                 po = (BF_TextFile_Line*)lo.FirstItem();
 440                 ASSERT(po);
 441                 iFilePos = po->Pos();
 442                 
 443                 iSearchLine = iLine;
 444                 iSearchPosInLine = iPosInLine;
 445         }
 446         Invalidate(Bounds());
 447 }
 448 
 449 void
 450 BF_GUI_TextViewer::MessageReceived(BMessage* po_Message)    /* [previous][next][first][last][top][bottom][index][help] */
 451 {
 452         switch(po_Message->what){
 453         case 1599425348:
 454         case 1599425365:
 455                 PrepareKeysMenu();
 456                 break;
 457         // search text //
 458         case BF_MSG_TEXTVIEWER_SEARCH:{
 459                 Search_Step0();
 460                 break;}
 461         case BF_MSG_TEXTVIEWER_SEARCH1:{
 462                 Search_Step1(*po_Message);
 463                 break;}
 464         ///
 465         case BF_MSG_TEXTVIEWER_SHOW_SPEC:{
 466                 bShowSpec = !bShowSpec;
 467                 poSysKeysMenu->SetText(9,BF_DictAt(bShowSpec?BF_DICT_TEXTVIEWER_SHOWSPEC:BF_DICT_TEXTVIEWER_HIDESPEC));
 468                 Invalidate(Bounds());
 469                 break;}
 470         case BF_MSG_TEXTVIEWER_CLOSED:
 471                 CloseViewer();
 472                 break;
 473         case BF_MSG_TEXTVIEWER_CH_CP:
 474                 ChangeCodePage();
 475                 break;
 476         default:
 477                 BView::MessageReceived(po_Message);
 478         }
 479 }
 480 
 481 void
 482 BF_GUI_TextViewer::ChangeCodePage()    /* [previous][next][first][last][top][bottom][index][help] */
 483 {
 484         // set new code page //
 485         iCodePage = (iCodePage==(CP_END-1))?0:iCodePage+1;
 486         Draw(Bounds());
 487         // update status bar
 488         UpdateStatusBar();      
 489 }
 490 
 491 void 
 492 BF_GUI_TextViewer::Draw(BRect o_Rect)    /* [previous][next][first][last][top][bottom][index][help] */
 493 {       
 494         // create render //
 495         BRect   oRect(Bounds());
 496         BView   *pv = this;
 497         
 498         BBitmap oB(oRect,B_RGBA32,TRUE);        
 499         oB.AddChild(pv = new BView(oRect,"",B_FOLLOW_ALL,B_WILL_DRAW)); 
 500         oB.Lock();              
 501         
 502         // init render //
 503         pv->SetFont(&poSysSetup->oFontNode.oFont);      
 504         pv->SetViewColor(B_TRANSPARENT_COLOR);  
 505                                                                         
 506         // draw background //
 507         pv->SetHighColor(SYS_COLOR(BF_COLOR_TEXTVIEWER_BACK));
 508         oRect = pv->Bounds();   
 509         pv->FillRect(oRect);
 510         //
 511         BL_String s;
 512         //
 513         pv->SetHighColor(SYS_COLOR(BF_COLOR_TEXTVIEWER_TEXT)); 
 514         pv->SetLowColor(SYS_COLOR(BF_COLOR_TEXTVIEWER_BACK));
 515         //
 516         for(int iLine=0;iLine<loData.CountItems();iLine++){
 517                 DrawLine(pv,iLine,false,false,s);
 518         }
 519         
 520         /* close render */      
 521         pv->Sync();
 522         oB.Unlock();
 523         DrawBitmap(&oB,oRect);
 524 }
 525 
 526 void
 527 BF_GUI_TextViewer::DrawLine(BView *po_Render,int i_Line,bool b_InitRender,bool b_DrawBackground,BL_String & s)    /* [previous][next][first][last][top][bottom][index][help] */
 528 {
 529         ASSERT(po_Render);
 530         BPoint          oPoint(5 - iStartColumn*10,i_Line * fFontHeight + poSysSetup->oFontNode.fAscent);
 531         BF_TextFile_Line        *po = (BF_TextFile_Line*)loData.ItemAt(i_Line);
 532 
 533         if(!po) return; 
 534 
 535         if(b_DrawBackground){
 536                 BRect oRect(0,i_Line*fFontHeight,Bounds().Width(),i_Line*fFontHeight+fFontHeight);
 537                 po_Render->SetHighColor(SYS_COLOR(BF_COLOR_TEXTVIEWER_BACK));           
 538                 po_Render->FillRect(oRect);
 539         }
 540 
 541         if(b_InitRender){
 542                 po_Render->SetHighColor(SYS_COLOR(BF_COLOR_TEXTVIEWER_TEXT)); 
 543                 po_Render->SetLowColor(SYS_COLOR(BF_COLOR_TEXTVIEWER_BACK));
 544         }
 545         
 546         const char *pcBuffer=pcConvertBuffer;
 547         
 548         if((po->s.Length()+1)<=0x1000){
 549                 ToUtf8(po->String(),pcConvertBuffer);           
 550         }else{          
 551                 pcBuffer = po->String();
 552         }               
 553         
 554         s=pcBuffer;     
 555         if(!bShowSpec){
 556                 s.ReplaceAll("\r","\x0");
 557                 s.ReplaceAll("\n","\x0");
 558                 s.ReplaceAll("\t","        ");
 559         }
 560         
 561         po_Render->DrawString(s.String(),oPoint);
 562         
 563         if((iSearchPos>=0) && (i_Line == iSearchLine)){
 564                 po_Render->DrawString("!!!",oPoint);
 565         }
 566 }
 567 
 568 void
 569 BF_GUI_TextViewer::MouseDown(BPoint o_point)    /* [previous][next][first][last][top][bottom][index][help] */
 570 {
 571 
 572 }
 573 void                    
 574 BF_GUI_TextViewer::MakeFocus(bool b_Focused)    /* [previous][next][first][last][top][bottom][index][help] */
 575 {
 576         BView::MakeFocus(b_Focused);
 577         PrepareKeysMenu();
 578 }
 579 
 580 void
 581 BF_GUI_TextViewer::PrepareKeysMenu()    /* [previous][next][first][last][top][bottom][index][help] */
 582 {
 583         int32 iKeysModifiers = modifiers();
 584         
 585         poSysKeysMenu->Clear();
 586         if (iKeysModifiers & B_LEFT_SHIFT_KEY || iKeysModifiers & B_RIGHT_SHIFT_KEY)
 587         {
 588                 poSysKeysMenu->SetText(7,BF_DictAt(BF_DICT_TEXTVIEWER_NEXT));           
 589                 poSysKeysMenu->SetMessage(7,new BMessage(BF_MSG_TEXTVIEWER_SEARCH_NEXT),this);
 590         }
 591         else
 592         {
 593                 poSysKeysMenu->SetText(3,BF_DictAt(BF_DICT_TEXTVIEWER_CLOSE));
 594                 poSysKeysMenu->SetText(7,BF_DictAt(BF_DICT_SEARCH));
 595                 poSysKeysMenu->SetText(8,BF_DictAt(BF_DICT_TEXTVIEWER_CODEPAGE));
 596                 poSysKeysMenu->SetText(9,BF_DictAt(BF_DICT_TEXTVIEWER_SHOWSPEC));
 597                 poSysKeysMenu->SetText(10,BF_DictAt(BF_DICT_TEXTVIEWER_CLOSE));
 598 
 599                 poSysKeysMenu->SetMessage(3,new BMessage(BF_MSG_TEXTVIEWER_CLOSED),this);
 600                 poSysKeysMenu->SetMessage(7,new BMessage(BF_MSG_TEXTVIEWER_SEARCH),this);
 601                 poSysKeysMenu->SetMessage(8,new BMessage(BF_MSG_TEXTVIEWER_CH_CP),this);
 602                 poSysKeysMenu->SetMessage(9,new BMessage(BF_MSG_TEXTVIEWER_SHOW_SPEC),this);
 603                 poSysKeysMenu->SetMessage(10,new BMessage(BF_MSG_TEXTVIEWER_CLOSED),this);
 604         };
 605 }
 606 
 607 void                    
 608 BF_GUI_TextViewer::LoadPage()    /* [previous][next][first][last][top][bottom][index][help] */
 609 {       
 610         int32 iLinesLoaded = oBuffer.LoadText(iFilePos,iLinesCount,loData);
 611         iFileSize = oBuffer.FileSize();
 612         if(Parent())    Draw(Bounds());
 613 }
 614 
 615 bool                            
 616 BF_GUI_TextViewer::Nav_PageUp()    /* [previous][next][first][last][top][bottom][index][help] */
 617 {
 618         if(iFilePos<=0) return false;   
 619         
 620         BF_TextFile_Line *po = (BF_TextFile_Line*)loData.FirstItem();
 621         if(!po) return false;
 622         
 623         iFilePos = po->Pos();
 624         int32 iLoaded = oBuffer.LoadText_BeforePos(iFilePos,iLinesCount,loData);
 625         iFileSize = oBuffer.FileSize();
 626         
 627         if(iLoaded<iLinesCount){
 628                 oBuffer.LoadText_FromNextLine(iFilePos,iLinesCount - iLoaded,loData,false);
 629         }
 630         iFileSize = oBuffer.FileSize();
 631         
 632         
 633 
 634         // redraw view
 635         Draw(Bounds());
 636         // update status_bar    
 637         UpdateStatusBar();      
 638         
 639         /*
 640         // move up
 641         off_t           iNewFilePos = iFilePos;
 642         BL_String       *ps=NULL,s;
 643                         
 644         // open file
 645         BL_File oFile;
 646         if(B_OK!=OpenFile(oFile)) return false;
 647         
 648         // load lines
 649         int iLine=0;
 650         while(iNewFilePos>0 && iLine<iLinesCount){      
 651                 // set new pos
 652                 iNewFilePos-=2;
 653                 if(iNewFilePos!=oFile.Seek(iNewFilePos,SEEK_SET)) return false;
 654                 // calc new pos //
 655                 if(B_OK!=oFile.FindPrevEOL(iNewFilePos)) return false;
 656                 if(iNewFilePos!=0) iNewFilePos++;
 657                 // set new pos
 658                 if(iNewFilePos!=oFile.Seek(iNewFilePos,SEEK_SET)) return false;
 659                 // load new line //
 660                 if(B_OK!=oFile.ReadString(&s)) return false;
 661                 // set new vars //      
 662                 if(iNewFilePos>0){
 663                         iFilePos-=s.Length()+1;
 664                         iLine++;
 665                 }else{
 666                         iNewFilePos = 0;
 667                         iFilePos = 0;
 668                 }
 669                 // delete bottom line           
 670                 lsData.DeleteItemAt(lsData.CountItems()-1);
 671                 // add new line at top
 672                 ps = new BL_String(s.String());
 673                 lsData.AddItem(ps,0);   
 674         }
 675         
 676         // redraw view
 677         Draw(Bounds());
 678         // update status_bar
 679         UpdateStatusBar();
 680         */
 681         return true;
 682 }
 683 
 684 bool
 685 BF_GUI_TextViewer::Nav_LineUp()    /* [previous][next][first][last][top][bottom][index][help] */
 686 {
 687         if(iFilePos<=0) return false;
 688         
 689         BL_List loUpLine;
 690         if(0==oBuffer.LoadText_BeforePos(iFilePos,1,loUpLine)) return false;
 691         
 692         loData.AddList(&loUpLine,0);
 693         loUpLine.MakeEmpty();
 694 
 695         if(loData.CountItems()>iLinesCount){
 696                 loData.DeleteItemAt(loData.CountItems()-1);
 697         }
 698         
 699                 
 700         // redraw view
 701         {
 702                 BL_String s;
 703                 
 704                 // move image down
 705                 BRect oRectFull(Bounds());
 706                 BRect oRectNew(0,fFontHeight,oRectFull.right,loData.CountItems()*fFontHeight),oRectOld(oRectNew);
 707                 oRectOld.OffsetBy(0,0-fFontHeight);
 708                 CopyBits(oRectOld,oRectNew);
 709                 // draw last line
 710                 DrawLine(this,0,true,true,s);
 711                 
 712         }
 713         // update status_bar
 714         UpdateStatusBar();
 715         return true;
 716 }
 717 
 718 status_t
 719 BF_GUI_TextViewer::OpenFile(BL_File & o_File)    /* [previous][next][first][last][top][bottom][index][help] */
 720 {
 721         status_t uRes=B_ERROR;
 722         o_File.SetTo(sFileName.String(),B_READ_ONLY);   
 723         uRes = o_File.InitCheck();      
 724         return uRes;
 725 }
 726 
 727 bool
 728 BF_GUI_TextViewer::Nav_LineDown()    /* [previous][next][first][last][top][bottom][index][help] */
 729 {
 730         if(1!=oBuffer.LoadText_FromNextLine(iFilePos,1,loData)) return false;
 731         iFileSize = oBuffer.FileSize();
 732         
 733         BF_TextFile_Line *po = (BF_TextFile_Line*)loData.LastItem();
 734         if(!po) return false;
 735 
 736         // redraw view
 737         BL_String s;
 738         {
 739                 // move image up
 740                 BRect oRectFull(Bounds());
 741                 BRect oRectOld(0,fFontHeight,oRectFull.right,loData.CountItems()*fFontHeight),oRectNew(oRectOld);
 742                 oRectNew.OffsetBy(0,0-fFontHeight);
 743                 CopyBits(oRectOld,oRectNew);
 744                 // draw last line
 745                 DrawLine(this,loData.CountItems()-1,true,true,s);               
 746         }       
 747         // update status_bar
 748         UpdateStatusBar();
 749         return true;
 750 }
 751 
 752 bool                            
 753 BF_GUI_TextViewer::Nav_PageDown()    /* [previous][next][first][last][top][bottom][index][help] */
 754 {
 755         if(loData.CountItems()<iLinesCount) return false;
 756         
 757         BF_TextFile_Line *po = (BF_TextFile_Line*)loData.LastItem();
 758         if(!po) return false;
 759         
 760         iFilePos = po->Pos();
 761         oBuffer.LoadText(iFilePos,iLinesCount,loData);
 762         iFileSize = oBuffer.FileSize();
 763 
 764         // redraw view
 765         Draw(Bounds());
 766         // update status_bar    
 767         UpdateStatusBar();
 768         
 769         /*
 770         if(lsData.CountItems()<iLinesCount) return false;
 771         // move down
 772         off_t           iNewFilePos = iFilePos;
 773         BL_String       *ps=NULL,s;
 774         
 775         // calc last_pos //
 776         for(int i=0;i<lsData.CountItems();i++){
 777                 ps=(BL_String*)lsData.ItemAt(i);
 778                 if(!ps) continue;
 779                 iNewFilePos+=ps->Length()+1;
 780         }
 781         // open file
 782         BL_File oFile;
 783         if(B_OK!=OpenFile(oFile)) return false;
 784         // set new pos
 785         if(iNewFilePos>oFile.Seek(iNewFilePos,SEEK_SET)) return false;
 786         // load new lines
 787         int32 iLine=0;
 788         while(iLine<iLinesCount){
 789                 // load line
 790                 if(B_OK!=oFile.ReadString(&s)) break;
 791                 // set new vars
 792                 ps=(BL_String*)lsData.ItemAt(0);
 793                 iFilePos+=ps->Length()+1;
 794                 iLine++;
 795                 // delete first line            
 796                 lsData.DeleteItemAt(0);
 797                 // add new line in the end of list
 798                 ps = new BL_String(s.String());
 799                 lsData.AddItem(ps);     
 800         }
 801         // redraw view
 802         Draw(Bounds());
 803         // update status_bar    
 804         UpdateStatusBar();*/
 805         return true;
 806 }
 807 
 808 bool                            
 809 BF_GUI_TextViewer::Nav_End()    /* [previous][next][first][last][top][bottom][index][help] */
 810 {       
 811         if(loData.CountItems()<iLinesCount) return true;
 812         
 813         iFilePos = iFileSize-1;
 814         if(0==oBuffer.LoadText_BeforePos(iFilePos,iLinesCount,loData)) return false;
 815         
 816         // redraw view
 817         Draw(Bounds());
 818         // update status_bar
 819         UpdateStatusBar();
 820         
 821         return true;
 822 
 823 /*
 824         // move up
 825         off_t           iNewFilePos = iFileSize;
 826         BL_String       *ps=NULL,s;
 827                         
 828         // open file
 829         BL_File oFile;
 830         if(B_OK!=OpenFile(oFile)) return false;
 831         // get new file_size
 832         if(B_OK!=oFile.GetSize(&iFileSize)) return false;
 833         iNewFilePos = iFileSize;
 834         
 835         lsData.DeleteItems();
 836         
 837         // load lines
 838         int iLine=0;
 839         while(iNewFilePos>0 && iLine<iLinesCount){      
 840                 // set new pos
 841                 iNewFilePos-=2;
 842                 if(iNewFilePos!=oFile.Seek(iNewFilePos,SEEK_SET)) return false;
 843                 // calc new pos //
 844                 if(B_OK!=oFile.FindPrevEOL(iNewFilePos)) return false;
 845                 if(iNewFilePos!=0) iNewFilePos++;
 846                 // set new pos
 847                 if(iNewFilePos!=oFile.Seek(iNewFilePos,SEEK_SET)) return false;
 848                 // load new line //
 849                 if(B_OK!=oFile.ReadString(&s)) break;
 850                 // set new vars //      
 851                 if(iNewFilePos>0){
 852                         iFilePos = iNewFilePos;
 853                         iLine++;
 854                 }else{
 855                         iNewFilePos = 0;
 856                         iFilePos = 0;
 857                 }               
 858                 // add new line at top
 859                 ps = new BL_String(s.String());
 860                 lsData.AddItem(ps,0);   
 861         }
 862         
 863         // redraw view
 864         Draw(Bounds());
 865         // update status_bar
 866         UpdateStatusBar();
 867         return true;
 868 */
 869 }
 870 
 871 void
 872 BF_GUI_TextViewer::Nav_GoPos(off_t i_NewFilePos)    /* [previous][next][first][last][top][bottom][index][help] */
 873 {
 874         if(i_NewFilePos==iFilePos) return;
 875         if(i_NewFilePos>iFileSize){
 876                 ASSERT(false);
 877         }
 878         //
 879         iFilePos = i_NewFilePos;
 880         LoadPage();     
 881         // update status        
 882         UpdateStatusBar();
 883 }
 884 
 885 void
 886 BF_GUI_TextViewer::UpdateStatusBar()    /* [previous][next][first][last][top][bottom][index][help] */
 887 {
 888         // calc page_size //
 889         off_t iPageSize=0;
 890         {
 891                 BF_TextFile_Line *po=NULL;
 892                 for(int i=0;i<loData.CountItems();i++){
 893                         po=(BF_TextFile_Line*)loData.ItemAt(i);
 894                         if(!po) continue;
 895                         iPageSize+=po->s.Length()+1;
 896                 }
 897         }
 898         // update statusbar //
 899         BView *poParent = Parent();
 900         if(!poParent) poParent = poWinView;
 901         if(!poParent) return;
 902         
 903         BF_GUI_TextViewer_StatusBar *poStatus = (BF_GUI_TextViewer_StatusBar*)poParent->FindView("textviewer_status");
 904         if(poStatus)    poStatus->SetVarsTo(iFilePos,iPageSize,iFileSize,iCodePage,iStartColumn);
 905 }
 906 
 907 
 908 void
 909 BF_GUI_TextViewer::ToUtf8(const char *pc_Source,char *pc_Dest)    /* [previous][next][first][last][top][bottom][index][help] */
 910 {
 911         int32   iIN;
 912         int32   iOUT;
 913         int32   iStatus;
 914         uint32  iCP = GetConversion();
 915 
 916                 if ( iCodePage == CP_UTF8 )
 917                 {
 918                         strcpy(pc_Dest,pc_Source);
 919                 }
 920                 else
 921                 {
 922                         iIN = strlen(pc_Source);
 923                         iOUT = 255;
 924                         convert_to_utf8(iCP,pc_Source,&iIN,pc_Dest,&iOUT,&iStatus);
 925                         pc_Dest[iOUT] = 0;
 926                 }
 927 }
 928 
 929 void
 930 BF_GUI_TextViewer::FromUtf8(const char *pc_Source,char *pc_Dest)    /* [previous][next][first][last][top][bottom][index][help] */
 931 {
 932         int32   iIN;
 933         int32   iOUT;
 934         int32   iStatus;
 935         uint32  iCP = GetConversion();
 936 
 937                 if ( iCodePage == CP_UTF8 )
 938                 {
 939                         strcpy(pc_Dest,pc_Source);
 940                 }
 941                 else
 942                 {
 943                         iIN = strlen(pc_Source);
 944                         iOUT = 255;
 945                         convert_from_utf8(iCP,pc_Source,&iIN,pc_Dest,&iOUT,&iStatus);
 946                         pc_Dest[iOUT] = 0;
 947                 }
 948 }
 949 
 950 
 951 uint32
 952 BF_GUI_TextViewer::GetConversion()    /* [previous][next][first][last][top][bottom][index][help] */
 953 {
 954         uint32  iCP = 0;
 955                 switch (iCodePage )
 956                 {
 957                         case CP_DOS:
 958                                 iCP = B_MS_DOS_866_CONVERSION;
 959                                 break;
 960                         case CP_WIN:
 961                                 iCP = B_MS_WINDOWS_1251_CONVERSION;
 962                                 break;
 963                         case CP_KOI:
 964                                 iCP = B_KOI8R_CONVERSION;
 965                                 break;
 966                 };
 967                 return(iCP);
 968 };
 969 
 970 bool
 971 BF_GUI_TextViewer::Nav_Right()    /* [previous][next][first][last][top][bottom][index][help] */
 972 {
 973         iStartColumn++;
 974 
 975         Draw(Bounds());
 976         UpdateStatusBar();      
 977 
 978         return true;    
 979 }
 980 
 981 bool
 982 BF_GUI_TextViewer::Nav_Left()    /* [previous][next][first][last][top][bottom][index][help] */
 983 {
 984         if(0==iStartColumn) return false;
 985         iStartColumn--;         
 986 
 987         Draw(Bounds());
 988         UpdateStatusBar();      
 989         
 990         return true;
 991 }
 992 
 993 ////////////////////////////////////////////////////////////////////////////////
 994 
 995 void  
 996 BF_GUI_TextViewer_Run(const char *pc_File)    /* [previous][next][first][last][top][bottom][index][help] */
 997 {
 998         BF_GUI_Func_PanelsShow(false);
 999 
1000         // make status_bar      
1001         BRect oRect(poWinView->Bounds());
1002         oRect.bottom = poSysSetup->oFontNode.fHeight;
1003         BF_GUI_TextViewer_StatusBar *poStatus = new BF_GUI_TextViewer_StatusBar(oRect,pc_File);
1004         BF_GUI_Func_AddChildToMainView ( poStatus );    
1005         // make viewer_view
1006         oRect = poWinView->Bounds();
1007         oRect.top += poSysSetup->oFontNode.fHeight+1;
1008         oRect.bottom-=poSysKeysMenu->Bounds().Height()+1;
1009 
1010         BF_GUI_TextViewer *po = new BF_GUI_TextViewer(oRect,pc_File);
1011         BF_GUI_Func_AddChildToMainView ( po );
1012         po->MakeFocus();

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