root/_BF_GUI/BF_GUI_Edit.cpp

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

DEFINITIONS

This source file includes following definitions.
  1. MessageReceived
  2. BF_GUI_ViewEdit_PulseThread
  3. AttachedToWindow
  4. Pulse
  5. Draw
  6. DrawBody
  7. SetText
  8. MakeFocus
  9. DrawCursor
  10. SaveToMessage
  11. Enable
  12. Enabled
  13. OnEnable
  14. OnKeyDown
  15. KeyDown
  16. ClearSelecting
  17. NavGo

   1 #include <stdio.h>
   2 
   3 #include "BF_Msg.h" 
   4 #include "BF_GUI_Func.h" 
   5 #include "BF_GUI_DlgViews.h"  
   6 #include "BF_GUI_WinMain.h"  
   7 
   8 #include <Clipboard.h>
   9 
  10 /*====================================================================*/
  11 BF_GUI_ViewEdit::BF_GUI_ViewEdit(
  12         const  BRect & o_Rect,
  13         const char *pc_Name,
  14         const char *pc_Value,
  15         uint32  i_FollowMode,
  16         uint32  i_Flags
  17 )
  18 :BF_GUI_DialogView(o_Rect,pc_Name,i_FollowMode,i_Flags|B_WILL_DRAW)
  19 {
  20         if(pc_Value) sValue = pc_Value;
  21         //
  22         idThreadPulse = 0;
  23         iNavIndex = sValue.LengthUTF8();
  24         iNavFirstChar = 0;
  25         bSelected = true;
  26         bSysKeysHandle = true;
  27         bCursorActive = true;
  28         //
  29         SetViewColor(B_TRANSPARENT_COLOR);      
  30         bPulseCursorShow = true;
  31         //      
  32         bParentCall = true;
  33         bAlwaysCursor = false;  
  34         oColBack = SYS_COLOR(BF_COLOR_DIALOG_BUTTON_FOCUS);
  35         oColSel = SYS_COLOR(BF_COLOR_DIALOG_BUTTON_TEXT);
  36         //
  37         iDisableCount = 0;
  38 }       
  39 
  40 BF_GUI_ViewEdit::~BF_GUI_ViewEdit()
  41 {
  42         while(B_OK!=kill_thread(idThreadPulse));
  43         printf("BF_GUI_ViewEdit::~BF_GUI_ViewEdit()\n");
  44 }
  45 
  46 void                                    
  47 BF_GUI_ViewEdit::MessageReceived(BMessage* po_Message)    /* [previous][next][first][last][top][bottom][index][help] */
  48 {
  49         switch(po_Message->what){
  50         case B_COPY:{
  51                 if(be_clipboard->Lock()){                                       
  52                         be_clipboard->Clear();                  
  53                         BMessage *po = be_clipboard->Data();
  54                         po->MakeEmpty();
  55                         po->AddData("text/plain", B_MIME_TYPE, sValue.String(),sValue.Length());                
  56                         be_clipboard->Commit();
  57                         be_clipboard->Unlock();
  58                 }                       
  59                 break;}
  60         case B_PASTE:{          
  61                 if(!be_clipboard->Lock()) return;
  62                 BMessage        *po = be_clipboard->Data();
  63                 const char      *pcData=NULL;
  64                 int32           iTextLen;               
  65                 if(!po || B_OK!=po->FindData("text/plain",B_MIME_TYPE,(const void**)&pcData,&iTextLen) || !pcData) return;
  66                 if(iTextLen<=0) return;
  67                 
  68                 // set end of line
  69                 char pcBuffer[iTextLen+1];
  70                 memcpy(pcBuffer,pcData,iTextLen);
  71                 pcBuffer[iTextLen] = '\0';              
  72                 // update screen                
  73                 SetText(pcBuffer,true);
  74                 be_clipboard->Unlock();
  75                 break;}
  76         case BF_MSG_PULSE:
  77                 Pulse();
  78                 break;
  79         default:
  80                 BView::MessageReceived(po_Message);
  81         }
  82 }
  83 
  84 int32 
  85 BF_GUI_ViewEdit_PulseThread(void *data)    /* [previous][next][first][last][top][bottom][index][help] */
  86 {
  87         ASSERT(data);
  88         BView *poView = (BView*)data;
  89         BMessage oMessage(BF_MSG_PULSE);
  90         BMessenger oMessenger(poView);
  91         
  92         while(true){
  93                 snooze(200000);
  94                 oMessenger.SendMessage(&oMessage);
  95         }
  96 }
  97 
  98 
  99 void            
 100 BF_GUI_ViewEdit::AttachedToWindow(void)    /* [previous][next][first][last][top][bottom][index][help] */
 101 {
 102         BView::AttachedToWindow();
 103         //Window()->SetPulseRate(100);  
 104         
 105         idThreadPulse = spawn_thread(BF_GUI_ViewEdit_PulseThread,"BeFar:edit_pulse",B_THREAD_SUSPENDED,(void*)this);    
 106         ASSERT(idThreadPulse>0,"can`t start thread BeFar:edit_pulse\n");        
 107         ASSERT(B_OK==resume_thread(idThreadPulse));             
 108         set_thread_priority(idThreadPulse,1);
 109 }
 110 
 111 void            
 112 BF_GUI_ViewEdit::Pulse(void)    /* [previous][next][first][last][top][bottom][index][help] */
 113 {
 114         bPulseCursorShow = !bPulseCursorShow;   
 115         if(bCursorActive) DrawCursor(bPulseCursorShow && (Window()->IsActive() || bAlwaysCursor));      
 116 }
 117 
 118 void                                    
 119 BF_GUI_ViewEdit::Draw(BRect o_Rect)    /* [previous][next][first][last][top][bottom][index][help] */
 120 {
 121         /* draw body */
 122         DrawBody(o_Rect);
 123         /* draw cursor */
 124         DrawCursor(bPulseCursorShow); 
 125 }
 126 
 127 void
 128 BF_GUI_ViewEdit::DrawBody(BRect o_Rect)    /* [previous][next][first][last][top][bottom][index][help] */
 129 {
 130         char *pcText=(char*)sValue.String()+ PCharCharPosInBytes_UTF8(sValue.String(),iNavFirstChar)  ;
 131         BRect oRect = Bounds();
 132         /* draw background */
 133         SetHighColor(oColBack);
 134         FillRect(oRect);
 135         /* draw selected background */ 
 136         if(bSelected){
 137                 BRect oRect1(oRect);
 138                 oRect1.right = oRect1.left+1+StringWidth(pcText);
 139                 SetHighColor(oColSel);
 140                 FillRect(oRect1);
 141         }
 142         /* draw text */
 143 
 144         if(bSelected){
 145                 SetHighColor(oColBack);
 146                 SetLowColor(oColSel); 
 147         }else{
 148                 SetHighColor(oColSel);
 149                 SetLowColor(oColBack); 
 150         }
 151         MovePenTo(1,oRect.Height()-poFont->fDescent);
 152         DrawString(pcText);
 153 }
 154 
 155 void                                    
 156 BF_GUI_ViewEdit::SetText(const char *pc_NewText,bool b_GoEnd)    /* [previous][next][first][last][top][bottom][index][help] */
 157 {               
 158         ASSERT(pc_NewText);
 159         iNavIndex = 0;
 160         iNavFirstChar = 0;
 161         sValue = pc_NewText;                    
 162         Invalidate(Bounds());
 163         
 164         if(b_GoEnd) NavGo(sValue.Length());
 165 }
 166 
 167 void                                    
 168 BF_GUI_ViewEdit::MakeFocus(bool focused)    /* [previous][next][first][last][top][bottom][index][help] */
 169 {       
 170         BView::MakeFocus(focused);
 171         if(focused){
 172                 resume_thread(idThreadPulse);           
 173                 DrawCursor(TRUE);
 174         }else{
 175                 suspend_thread(idThreadPulse);
 176                 if(!bAlwaysCursor) DrawCursor(FALSE);
 177         }
 178 }
 179 
 180 void 
 181 BF_GUI_ViewEdit::DrawCursor(bool b_Show)    /* [previous][next][first][last][top][bottom][index][help] */
 182 {
 183         //printf("----start --------------------------------\n");
 184         
 185         if(!bAlwaysCursor && !IsFocus()) b_Show = false;
 186         //
 187         float fWidth = 0;
 188         BRect oRect(Bounds());
 189         bool bReqFullDrawing=false;
 190         //
 191         if(iNavIndex<iNavFirstChar){
 192                 iNavFirstChar = iNavIndex;
 193                 bReqFullDrawing = true;
 194         }
 195         //
 196         if(iNavIndex>0){
 197                 BL_String s;                                            
 198                 while(TRUE){                            
 199                         s = sValue;
 200                         //printf("pre  RemoveUTF8 1\n");
 201                         s.RemoveUTF8(iNavIndex,s.LengthUTF8()-iNavIndex);
 202                         //printf("post RemoveUTF8 1 iNavIndex=%i LengthBytes=%i LengthChars=%i new LengthBytes=%i new LengthChars=%i \n",
 203                         //      iNavIndex,sValue.Length(),sValue.LengthUTF8(),s.Length(),s.LengthUTF8());
 204                         s.RemoveUTF8(0,iNavFirstChar);
 205                         //printf("RemoveUTF8 2 iNavFirstChar=%i new length=%i\n",iNavFirstChar,s.Length());
 206                         fWidth = poFont->oFont.StringWidth(s.String()); 
 207                         if((fWidth+1)>=oRect.Width()){
 208                                 iNavFirstChar++;                                
 209                                 bReqFullDrawing = true;
 210                                 continue;
 211                         }
 212                         break;                  
 213                 }                               
 214         }       
 215         
 216         if(bReqFullDrawing){
 217                 DrawBody(Bounds());                                     
 218                 return;
 219         }
 220         
 221         
 222         if(b_Show)      SetHighColor(oColSel); 
 223         else            SetHighColor(oColBack); 
 224         BPoint o1(1+fWidth,0),o2(o1.x,oRect.Height());
 225         StrokeLine(o1,o2);
 226 
 227         if(!b_Show){
 228                 SetHighColor(oColSel);
 229                 SetLowColor(oColBack);  
 230                 
 231                 BL_String s;
 232                 s.AppendUTF8( sValue.StringUTF8(iNavIndex),1);
 233                 //o1.x;
 234                 o1.y = oRect.Height()-poFont->fDescent;
 235                 
 236                 DrawString(s.String(),o1);
 237                 
 238         }
 239         //printf("----end --------------------------------\n");
 240 }
 241 
 242 void                                    
 243 BF_GUI_ViewEdit::SaveToMessage(BMessage *po_Message)    /* [previous][next][first][last][top][bottom][index][help] */
 244 {
 245         po_Message->AddString(Name(),sValue.String());  
 246 }
 247 
 248 void
 249 BF_GUI_ViewEdit::Enable(bool b_Enable)    /* [previous][next][first][last][top][bottom][index][help] */
 250 {
 251         if(b_Enable && iDisableCount==1){
 252                 SetFlags(B_WILL_DRAW|B_FRAME_EVENTS|B_NAVIGABLE);               
 253         }else
 254         if(!b_Enable && iDisableCount==0){
 255                 SetFlags(B_WILL_DRAW|B_FRAME_EVENTS);
 256         }
 257         
 258         iDisableCount += !b_Enable?+1:-1;
 259         ASSERT(iDisableCount>=0);
 260         
 261         if(iDisableCount==0 && b_Enable) OnEnable(true);
 262         if(iDisableCount==1 && !b_Enable) OnEnable(false);      
 263 }
 264 
 265 bool                                    
 266 BF_GUI_ViewEdit::Enabled()    /* [previous][next][first][last][top][bottom][index][help] */
 267 {
 268         return iDisableCount==0 ;
 269 }
 270 
 271 void
 272 BF_GUI_ViewEdit::OnEnable(bool b_Enable)    /* [previous][next][first][last][top][bottom][index][help] */
 273 {
 274 }
 275 
 276 #include <UTF8.h>
 277 
 278 bool
 279 BF_GUI_ViewEdit::OnKeyDown(const char *bytes, int32 numBytes)    /* [previous][next][first][last][top][bottom][index][help] */
 280 {
 281         printf(" chars : %i,%i  count:%i\n",(unsigned)bytes[0],bytes[1],numBytes);
 282         
 283         if(bSysKeysHandle && numBytes==1 && bytes[0]==B_RIGHT_ARROW){
 284                 NavGo(iNavIndex+1);
 285         }else   
 286         if(bSysKeysHandle && numBytes==1 && bytes[0]==B_LEFT_ARROW){
 287                 NavGo(iNavIndex-1);
 288         }else           
 289         if(bSysKeysHandle && numBytes==1 && bytes[0]==B_HOME){
 290                 NavGo(0);
 291         }else           
 292         if(bSysKeysHandle && numBytes==1 && bytes[0]==B_END){
 293                 NavGo(sValue.LengthUTF8());
 294         }else                   
 295         if(numBytes==1 && bytes[0]==B_BACKSPACE){       
 296                 ClearSelecting(true);
 297                 if(iNavIndex<=0) return false;
 298                 sValue.RemoveUTF8(iNavIndex-1,1);
 299                 NavGo(iNavIndex-1,true);
 300         }else
 301         if(numBytes==1 && bytes[0]==B_DELETE){  
 302                 if(sValue=="" || !bSysKeysHandle) return false;
 303                 ClearSelecting(true);
 304                 if(iNavIndex == sValue.Length()) return true;
 305                 sValue.RemoveUTF8(iNavIndex,1);         
 306                 Draw(Bounds());
 307         }else
 308         if(bParentCall && KeyDownExt(bytes,numBytes)) return true ;else         
 309         if((1==numBytes && bytes[0]>31) || (2==numBytes)){
 310                 ClearSelecting(true);
 311                 sValue.InsertUTF8(bytes,1,iNavIndex);
 312                 NavGo(iNavIndex+1,true);
 313         }else
 314                 return false;
 315         return true;
 316 }
 317 
 318 void                                    
 319 BF_GUI_ViewEdit::KeyDown(const char *bytes, int32 numBytes)    /* [previous][next][first][last][top][bottom][index][help] */
 320 {       
 321         if(!OnKeyDown(bytes,numBytes)) BView::KeyDown(bytes,numBytes);
 322 }
 323 
 324 void
 325 BF_GUI_ViewEdit::ClearSelecting(bool b_Remove)    /* [previous][next][first][last][top][bottom][index][help] */
 326 {
 327         if(!bSelected) return;
 328         if(b_Remove){
 329                 sValue = "";
 330                 iNavIndex = 0;
 331                 iNavFirstChar = 0;
 332         }
 333         bSelected = false;
 334         Draw(Bounds());
 335         
 336 }
 337 
 338 void
 339 BF_GUI_ViewEdit::NavGo(int32 i_NewCursor,bool b_FullRedraw)    /* [previous][next][first][last][top][bottom][index][help] */
 340 {
 341         ClearSelecting();
 342         //
 343         if(i_NewCursor>sValue.LengthUTF8()) i_NewCursor  = sValue.LengthUTF8();
 344         if(i_NewCursor<0) i_NewCursor = 0;
 345         //
 346         if(i_NewCursor==iNavIndex) return;
 347         //
 348         if(b_FullRedraw){
 349                 iNavIndex = i_NewCursor;
 350                 Draw(Bounds());
 351         }else{
 352                 DrawCursor(false);
 353                 iNavIndex = i_NewCursor;
 354                 DrawCursor(true);
 355         }
 356 }

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