1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
|
/********************************************************************************
* *
* C o n s o l e W i d g e t *
* *
*********************************************************************************
* Copyright (C) 2006,2022 by Jeroen van der Zijp. All Rights Reserved. *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/> *
********************************************************************************/
#include <new>
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "FXElement.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXMutex.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXSize.h"
#include "FXPoint.h"
#include "FXRectangle.h"
#include "FXStringDictionary.h"
#include "FXSettings.h"
#include "FXRegistry.h"
#include "FXAccelTable.h"
#include "FXFont.h"
#include "FXEvent.h"
#include "FXWindow.h"
#include "FXDCWindow.h"
#include "FXApp.h"
#include "FXScrollBar.h"
#include "FXScrollArea.h"
#include "FXConsole.h"
/*
To do:
*/
using namespace FX;
/*******************************************************************************/
namespace FX {
FXDEFMAP(FXConsole) FXConsoleMap[]={
FXMAPFUNC(SEL_PAINT,0,FXConsole::onPaint),
FXMAPFUNC(SEL_COMMAND,FXConsole::ID_XXX,FXConsole::onXXX),
};
// Object implementation
FXIMPLEMENT(FXConsole,FXScrollArea,FXConsoleMap,ARRAYNUMBER(FXConsoleMap))
// Deserialization
FXConsole::FXConsole(){
flags|=FLAG_ENABLED|FLAG_DROPTARGET;
font=nullptr;
margintop=0;
marginbottom=0;
marginleft=0;
marginright=0;
historylines=0;
visiblelines=0;
topline=0;
vrows=0;
vcols=0;
textColor=0;
selbackColor=0;
seltextColor=0;
cursorColor=0;
}
// Construct and init
FXConsole::FXConsole(FXComposite *p,FXObject* tgt,FXSelector sel,FXuint opts,FXint x,FXint y,FXint w,FXint h,FXint pl,FXint pr,FXint pt,FXint pb):FXScrollArea(p,opts,x,y,w,h){
flags|=FLAG_ENABLED|FLAG_DROPTARGET;
defaultCursor=getApp()->getDefaultCursor(DEF_TEXT_CURSOR);
dragCursor=getApp()->getDefaultCursor(DEF_TEXT_CURSOR);
font=getApp()->getNormalFont();
textColor=getApp()->getForeColor();
selbackColor=getApp()->getSelbackColor();
seltextColor=getApp()->getSelforeColor();
cursorColor=getApp()->getForeColor();
target=tgt;
message=sel;
margintop=pt;
marginbottom=pb;
marginleft=pl;
marginright=pr;
historylines=100;
visiblelines=1;
topline=0;
vrows=0;
vcols=0;
contents.no(historylines);
style.no(historylines);
for(int i=0; i<contents.no(); i++){ contents[i].format("blablablablaaaaaaaaaaaaaaa%d\n",i); }
}
// Create window
void FXConsole::create(){
FXScrollArea::create();
font->create();
recalc();
}
// Detach window
void FXConsole::detach(){
FXScrollArea::detach();
font->detach();
}
// If window can have focus
FXbool FXConsole::canFocus() const {
return true;
}
// Get default width
FXint FXConsole::getDefaultWidth(){
return 0<vcols ? marginleft+marginright+vcols*font->getTextWidth("8",1) : FXScrollArea::getDefaultWidth();
}
// Get default height
FXint FXConsole::getDefaultHeight(){
return 0<vrows ? margintop+marginbottom+vrows*font->getFontHeight() : FXScrollArea::getDefaultHeight();
}
// Move content
void FXConsole::moveContents(FXint x,FXint y){
FXint dx=x-pos_x;
FXint dy=y-pos_y;
pos_x=x;
pos_y=y;
scroll(marginleft,margintop,getVisibleWidth()-marginleft-marginright,getVisibleHeight()-margintop-marginbottom,dx,dy);
}
// Determine minimum content width of scroll area
FXint FXConsole::getContentWidth(){
return marginleft+marginright+1; // FIXME
}
// Determine minimum content height of scroll area
FXint FXConsole::getContentHeight(){
return margintop+marginbottom+font->getFontHeight()*contents.no();
}
// Recalculate layout
void FXConsole::layout(){
FXint hh=font->getFontHeight();
FXint totallines;
// Number of visible lines has changed
visiblelines=(height-margintop-marginbottom+hh-1)/hh;
if(visiblelines<1) visiblelines=1;
// Total lines to buffer
totallines=FXMAX(visiblelines,historylines);
if(contents.no()!=totallines){
contents.no(totallines); // FIXME keep text insofar as possible!
if(style.no()){ style.no(totallines); }
}
FXTRACE((100,"visiblelines=%d historylines=%d totallines=%d hh=%d space=%d\n",visiblelines,historylines,totallines,hh,height-margintop-marginbottom));
// Scrollbars adjusted
placeScrollBars(width,height);
// No more dirty
flags&=~FLAG_DIRTY;
}
// Character width
FXint FXConsole::charWidth(FXwchar ch,FXint col) const {
if(ch<' '){
if(ch!='\t'){
return font->getCharWidth('#');
}
return font->getCharWidth(' ')*(8-col%8);
}
return font->getCharWidth(ch);
}
// Determine style
FXuint FXConsole::styleOf(FXint line,FXint index,FXint p,FXint c) const {
FXuint s=0;
FXchar ch;
// Selected part of text
// if(selstartpos<=p && p<selendpos) s|=STYLE_SELECTED;
// Highlighted part of text
// if(hilitestartpos<=p && p<hiliteendpos) s|=STYLE_HILITE;
// Current active line
// if((row==cursorrow)&&(options&TEXT_SHOWACTIVE)) s|=STYLE_ACTIVE;
// Blank part of line
if(p>=contents[index].length()) return s;
// Special style for control characters
ch=contents[index][p];
// Get value from style buffer
if(style.no()) s|=style[index][p];
// Tabs are just fill
if(ch == '\t') return s;
// Spaces are just fill
if(ch == ' ') return s;
// Newlines are just fill
if(ch == '\n') return s;
// Get special style for control codes
if((FXuchar)ch < ' ') return s|STYLE_CONTROL|STYLE_TEXT;
return s|STYLE_TEXT;
}
// Draw fragment of text in given style
void FXConsole::drawTextFragment(FXDCWindow& dc,FXint x,FXint y,FXint,FXint,const FXchar *text,FXint n,FXuint sty) const {
FXColor color=FXRGB(255,255,255);
dc.setForeground(color);
y+=font->getFontAscent();
dc.drawText(x,y,text,n);
}
// Draw text line with correct style
void FXConsole::drawTextLine(FXDCWindow& dc,FXint line,FXint left,FXint right) const {
FXint index=(topline+line)%contents.no();
FXint edge=pos_x+marginleft;
FXint h=font->getFontHeight();
FXint y=pos_y+margintop+line*h;
FXint x=0;
FXint w=0;
FXuint curstyle;
FXuint newstyle;
FXint cw,sp,ep,sc,ec;
// Scan ahead till until we hit the end or the left edge
for(sp=sc=0; sp<contents[index].length(); sp=contents[index].inc(sp),sc++){
cw=charWidth(contents[index].wc(sp),sc);
if(x+edge+cw>=left) break;
x+=cw;
}
// First style to display
curstyle=styleOf(line,index,sp,sc);
// Draw until we hit the end or the right edge
for(ep=sp,ec=sc; ep<contents[index].length(); ep=contents[index].inc(ep),ec++){
newstyle=styleOf(line,index,ep,ec);
if(newstyle!=curstyle){
// fillBufferRect(dc,edge+x,y,w,h,curstyle);
if(curstyle&STYLE_TEXT) drawTextFragment(dc,edge+x,y,w,h,&contents[index][sp],ep-sp,curstyle);
curstyle=newstyle;
sp=ep;
x+=w;
w=0;
}
cw=charWidth(contents[index].wc(ep),ec);
if(x+edge+w>=right) break;
w+=cw;
}
// Draw unfinished fragment
// fillBufferRect(dc,edge+x,y,w,h,curstyle);
if(curstyle&STYLE_TEXT) drawTextFragment(dc,edge+x,y,w,h,&contents[index][sp],ep-sp,curstyle);
x+=w;
// Fill any left-overs outside of text
if(x+edge<right){
curstyle=styleOf(line,index,ep,ec);
// fillBufferRect(dc,edge+x,y,right-edge-x,h,curstyle);
}
}
// Repaint lines of text
void FXConsole::drawContents(FXDCWindow& dc,FXint x,FXint y,FXint w,FXint h) const {
FXint hh=font->getFontHeight();
FXint yy=pos_y+margintop;
FXint tl=(y-yy)/hh;
FXint bl=(y+h-yy)/hh;
FXint ln;
if(tl<0) tl=0;
if(bl>=contents.no()) bl=contents.no()-1;
FXTRACE((100,"tl=%d bl=%d\n",tl,bl));
for(ln=tl; ln<=bl; ln++){
drawTextLine(dc,ln,x,x+w);
}
}
// Draw the text
long FXConsole::onPaint(FXObject*,FXSelector,void* ptr){
FXEvent* event=(FXEvent*)ptr;
FXDCWindow dc(this,event);
dc.setFont(font);
dc.setForeground(FXRGB(255,0,0));
dc.fillRectangle(event->rect.x,event->rect.y,event->rect.w,event->rect.h);
// Viewport
FXint vw=getVisibleWidth();
FXint vh=getVisibleHeight();
// Paint top margin
if(event->rect.y<=margintop){
dc.setForeground(backColor);
dc.fillRectangle(0,0,vw,margintop);
}
// Paint bottom margin
if(event->rect.y+event->rect.h>=vh-marginbottom){
dc.setForeground(backColor);
dc.fillRectangle(0,vh-marginbottom,vw,marginbottom);
}
// Paint left margin
if(event->rect.x<marginleft){
dc.setForeground(backColor);
dc.fillRectangle(0,margintop,marginleft,vh-margintop-marginbottom);
}
// Paint right margin
if(event->rect.x+event->rect.w>=vw-marginright){
dc.setForeground(backColor);
dc.fillRectangle(vw-marginright,margintop,marginright,vh-margintop-marginbottom);
}
// Paint text
dc.setClipRectangle(marginleft,margintop,vw-marginright-marginleft,vh-margintop-marginbottom);
drawContents(dc,event->rect.x,event->rect.y,event->rect.w,event->rect.h);
return 1;
}
// Draw the text
long FXConsole::onXXX(FXObject*,FXSelector,void*){
return 1;
}
// Change top margin
void FXConsole::setMarginTop(FXint mt){
if(margintop!=mt){
margintop=mt;
recalc();
update();
}
}
// Change bottom margin
void FXConsole::setMarginBottom(FXint mb){
if(marginbottom!=mb){
marginbottom=mb;
recalc();
update();
}
}
// Change left margin
void FXConsole::setMarginLeft(FXint ml){
if(marginleft!=ml){
marginleft=ml;
recalc();
update();
}
}
// Change right margin
void FXConsole::setMarginRight(FXint mr){
if(marginright!=mr){
marginright=mr;
recalc();
update();
}
}
// Change history lines
void FXConsole::setHistoryLines(FXint hl){
if(hl<1) hl=1;
if(historylines!=hl){
historylines=hl;
recalc();
update();
}
}
// Change number of visible rows
void FXConsole::setVisibleRows(FXint rows){
if(rows<0) rows=0;
if(vrows!=rows){
vrows=rows;
recalc();
}
}
// Change number of visible columns
void FXConsole::setVisibleColumns(FXint cols){
if(cols<0) cols=0;
if(vcols!=cols){
vcols=cols;
recalc();
}
}
// Change the font
void FXConsole::setFont(FXFont* fnt){
if(!fnt){ fxerror("%s::setFont: NULL font specified.\n",getClassName()); }
if(font!=fnt){
font=fnt;
recalc();
update();
}
}
// Save object to stream
void FXConsole::save(FXStream& store) const {
FXScrollArea::save(store);
store << margintop;
store << marginbottom;
store << marginleft;
store << marginright;
store << font;
store << textColor;
store << selbackColor;
store << seltextColor;
store << cursorColor;
store << help;
store << tip;
}
// Load object from stream
void FXConsole::load(FXStream& store){
FXScrollArea::load(store);
store >> margintop;
store >> marginbottom;
store >> marginleft;
store >> marginright;
store >> font;
store >> textColor;
store >> selbackColor;
store >> seltextColor;
store >> cursorColor;
store >> help;
store >> tip;
}
// Clean up
FXConsole::~FXConsole(){
font=(FXFont*)-1L;
}
}
|