Add Background colours to a TextArea gadget

BlitzMax Forums/BlitzMax Module Tweaks/Add Background colours to a TextArea gadget

Brucey(Posted 2007) [#1]
EDIT : Added MacOS code

This tweak enables background colouring to a TextArea gadget. Works just like FormatTextArea... where you give it a range of the text to affect the colour-change against.

Generic code changes :

Firstly, we need to update MaxGUI to add a new function.

maxgui.bmx
Add this new function
Function FormatTextAreaBackground( textarea:TGadget,r,g,b,pos=0,length=TEXTAREA_ALL,units=TEXTAREA_CHARS )
	textarea.SetBGStyle(r,g,b,pos,length,units)	
End Function


gadget.bmx
Add this method to the "textarea commands" section of the TGadget type.
	Method SetBGStyle(r,g,b,pos,length,units)
	End Method	


Part 1 - WINDOWS :-)
We'll be modifying win32maxgui module...

win32gui.bmx
Add to the Extern section
Function bbFormatTextAreaBackground( textarea,r,g,b,pos=0,length=-1,units=1 )

Add method to TWin32Gadget
	Method SetBGStyle(r,g,b,pos,length,units)
		bbFormatTextAreaBackground(handle,r,g,b,pos,length,units)
	End Method


textarea.h
Add to public def of BBTextArea
virtual void	formatBackground( int r,int g,int b,int pos,int len,int units )=0;

Add to extern "C" section
void		bbFormatTextAreaBackground( BBTextArea *t,int r,int g,int b,int pos,int len,int units );


win32textarea.cpp
Add function to BBTextArea
void		bbFormatTextAreaBackground( BBTextArea *t,int r,int g,int b,int pos,int len,int units ){
	t->debug();
	t->formatBackground( r,g,b,pos,len,units );
}


win32textarea.h
Add to public def of Win32TextArea
void	formatBackground( int r,int g,int b,int pos,int len,int units );


win32textarea2.cpp
Add new function
void Win32TextArea::formatBackground( int r,int g,int b, int pos,int len,int units ){
	if( !adjustRange( pos,len,units ) ) return;
	CHARFORMAT2 cf={sizeof(cf)};
	cf.dwMask=0x4000000; // CFM_BACKCOLOR... bug with mingw - not defined in richedit.h
	cf.crBackColor=(b<<16)|(g<<8)|r;
	lock( pos,len );
	SendMessage( _gadget.hwnd(),EM_SETCHARFORMAT,SCF_SELECTION,(DWORD)&cf );
	unlock();
}

..as noted in the code comment, richedit.h that comes with the Mingw that we use is missing CFM_BACKCOLOR.


Part 2 - MAC OS :-)
We'll be modifying cocoamaxgui module...

cocoagui.bmx
Add new Extern function
Function NSSetBGStyle(gadget:TNSGadget,r,g,b,pos,length,units)

Add new TNSGadget method
	Method SetBGStyle(r,g,b,pos,length,units) 	
?debug
		If pos<0 Or pos+length>AreaLen(units) Throw "Illegal Range"
?	
		If length NSSetBGStyle Self,r,g,b,pos,length,units
	End Method


cocoa.macos.m
Add new function
void NSSetBGStyle(nsgadget *gadget,int r,int g,int b,int pos,int length,int units)	{
	TextView			*textarea;
	NSRange			_range;
	NSColor				*color;

	textarea=(TextView*)gadget->handle;	
	_range=GetRange([textarea storage],pos,length,units);
	color=[NSColor colorWithDeviceRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0];
	[[textarea storage] addAttribute:NSBackgroundColorAttributeName value:color range:_range];

}


And there are Win32 and MacOS done.

Alas, it turns out that FLTK doesn't support range-specific background colours, only for the whole gadget. But then again, FLTK doesn't support BOLD, ITALIC, UNDERLINE or STRIKETHROUGH for text formatting, so I don't think that would be a reason not to allow this for the other platforms ;-)

I've also applied changes to the GTK GUI module, which also enables background colour changing.
All we need now is for BRL to decide if they want to add it :-)

:o)


Brucey(Posted 2007) [#2]
A test program... :-)



Brucey(Posted 2007) [#3]
Have now added MacOS changes to the first post.

What else do we need? :-)