Table.cs 182 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733
  1. //#########################################################################################
  2. //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
  3. //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
  4. //#########################################################################################
  5. /*
  6. * Copyright ?2005, Mathew Hall
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  25. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. * OF SUCH DAMAGE.
  29. */
  30. using System;
  31. using System.Collections;
  32. using System.ComponentModel;
  33. using System.Drawing;
  34. using System.Drawing.Drawing2D;
  35. using System.Windows.Forms;
  36. using XPTable.Editors;
  37. using XPTable.Events;
  38. using XPTable.Models;
  39. using XPTable.Renderers;
  40. using XPTable.Sorting;
  41. using XPTable.Themes;
  42. using XPTable.Win32;
  43. namespace XPTable.Models
  44. {
  45. /// <summary>
  46. /// Summary description for Table.
  47. /// </summary>
  48. [DesignTimeVisible(true),
  49. ToolboxItem(true),
  50. ToolboxBitmap(typeof(Table))]
  51. public class Table : Control, ISupportInitialize
  52. {
  53. #region Event Handlers
  54. #region Cells
  55. /// <summary>
  56. /// Occurs when the value of a Cells property changes
  57. /// </summary>
  58. public event CellEventHandler CellPropertyChanged;
  59. #region Focus
  60. /// <summary>
  61. /// Occurs when a Cell gains focus
  62. /// </summary>
  63. public event CellFocusEventHandler CellGotFocus;
  64. /// <summary>
  65. /// Occurs when a Cell loses focus
  66. /// </summary>
  67. public event CellFocusEventHandler CellLostFocus;
  68. #endregion
  69. #region Keys
  70. /// <summary>
  71. /// Occurs when a key is pressed when a Cell has focus
  72. /// </summary>
  73. public event CellKeyEventHandler CellKeyDown;
  74. /// <summary>
  75. /// Occurs when a key is released when a Cell has focus
  76. /// </summary>
  77. public event CellKeyEventHandler CellKeyUp;
  78. #endregion
  79. #region Mouse
  80. /// <summary>
  81. /// Occurs when the mouse pointer enters a Cell
  82. /// </summary>
  83. public event CellMouseEventHandler CellMouseEnter;
  84. /// <summary>
  85. /// Occurs when the mouse pointer leaves a Cell
  86. /// </summary>
  87. public event CellMouseEventHandler CellMouseLeave;
  88. /// <summary>
  89. /// Occurs when a mouse pointer is over a Cell and a mouse button is pressed
  90. /// </summary>
  91. public event CellMouseEventHandler CellMouseDown;
  92. /// <summary>
  93. /// Occurs when a mouse pointer is over a Cell and a mouse button is released
  94. /// </summary>
  95. public event CellMouseEventHandler CellMouseUp;
  96. /// <summary>
  97. /// Occurs when a mouse pointer is moved over a Cell
  98. /// </summary>
  99. public event CellMouseEventHandler CellMouseMove;
  100. /// <summary>
  101. /// Occurs when the mouse pointer hovers over a Cell
  102. /// </summary>
  103. public event CellMouseEventHandler CellMouseHover;
  104. /// <summary>
  105. /// Occurs when a Cell is clicked
  106. /// </summary>
  107. public event CellMouseEventHandler CellClick;
  108. /// <summary>
  109. /// Occurs when a Cell is double-clicked
  110. /// </summary>
  111. public event CellMouseEventHandler CellDoubleClick;
  112. #endregion
  113. #region Buttons
  114. /// <summary>
  115. /// Occurs when a Cell's button is clicked
  116. /// </summary>
  117. public event CellButtonEventHandler CellButtonClicked;
  118. #endregion
  119. #region CheckBox
  120. /// <summary>
  121. /// Occurs when a Cell's Checked value changes
  122. /// </summary>
  123. public event CellCheckBoxEventHandler CellCheckChanged;
  124. #endregion
  125. #endregion
  126. #region Column
  127. /// <summary>
  128. /// Occurs when a Column's property changes
  129. /// </summary>
  130. public event ColumnEventHandler ColumnPropertyChanged;
  131. #endregion
  132. #region Column Headers
  133. /// <summary>
  134. /// Occurs when the mouse pointer enters a Column Header
  135. /// </summary>
  136. public event HeaderMouseEventHandler HeaderMouseEnter;
  137. /// <summary>
  138. /// Occurs when the mouse pointer leaves a Column Header
  139. /// </summary>
  140. public event HeaderMouseEventHandler HeaderMouseLeave;
  141. /// <summary>
  142. /// Occurs when a mouse pointer is over a Column Header and a mouse button is pressed
  143. /// </summary>
  144. public event HeaderMouseEventHandler HeaderMouseDown;
  145. /// <summary>
  146. /// Occurs when a mouse pointer is over a Column Header and a mouse button is released
  147. /// </summary>
  148. public event HeaderMouseEventHandler HeaderMouseUp;
  149. /// <summary>
  150. /// Occurs when a mouse pointer is moved over a Column Header
  151. /// </summary>
  152. public event HeaderMouseEventHandler HeaderMouseMove;
  153. /// <summary>
  154. /// Occurs when the mouse pointer hovers over a Column Header
  155. /// </summary>
  156. public event HeaderMouseEventHandler HeaderMouseHover;
  157. /// <summary>
  158. /// Occurs when a Column Header is clicked
  159. /// </summary>
  160. public event HeaderMouseEventHandler HeaderClick;
  161. /// <summary>
  162. /// Occurs when a Column Header is double-clicked
  163. /// </summary>
  164. public event HeaderMouseEventHandler HeaderDoubleClick;
  165. /// <summary>
  166. /// Occurs when the height of the Column Headers changes
  167. /// </summary>
  168. public event EventHandler HeaderHeightChanged;
  169. #endregion
  170. #region ColumnModel
  171. /// <summary>
  172. /// Occurs when the value of the Table's ColumnModel property changes
  173. /// </summary>
  174. public event EventHandler ColumnModelChanged;
  175. /// <summary>
  176. /// Occurs when a Column is added to the ColumnModel
  177. /// </summary>
  178. public event ColumnModelEventHandler ColumnAdded;
  179. /// <summary>
  180. /// Occurs when a Column is removed from the ColumnModel
  181. /// </summary>
  182. public event ColumnModelEventHandler ColumnRemoved;
  183. #endregion
  184. #region Editing
  185. /// <summary>
  186. /// Occurs when the Table begins editing a Cell
  187. /// </summary>
  188. public event CellEditEventHandler BeginEditing;
  189. /// <summary>
  190. /// Occurs when the Table stops editing a Cell
  191. /// </summary>
  192. public event CellEditEventHandler EditingStopped;
  193. /// <summary>
  194. /// Occurs when the editing of a Cell is cancelled
  195. /// </summary>
  196. public event CellEditEventHandler EditingCancelled;
  197. #endregion
  198. #region Rows
  199. /// <summary>
  200. /// Occurs when a Cell is added to a Row
  201. /// </summary>
  202. public event RowEventHandler CellAdded;
  203. /// <summary>
  204. /// Occurs when a Cell is removed from a Row
  205. /// </summary>
  206. public event RowEventHandler CellRemoved;
  207. /// <summary>
  208. /// Occurs when the value of a Rows property changes
  209. /// </summary>
  210. public event RowEventHandler RowPropertyChanged;
  211. #endregion
  212. #region Sorting
  213. /// <summary>
  214. /// Occurs when a Column is about to be sorted
  215. /// </summary>
  216. public event ColumnEventHandler BeginSort;
  217. /// <summary>
  218. /// Occurs after a Column has finished sorting
  219. /// </summary>
  220. public event ColumnEventHandler EndSort;
  221. #endregion
  222. #region Painting
  223. /// <summary>
  224. /// Occurs before a Cell is painted
  225. /// </summary>
  226. public event PaintCellEventHandler BeforePaintCell;
  227. /// <summary>
  228. /// Occurs after a Cell is painted
  229. /// </summary>
  230. public event PaintCellEventHandler AfterPaintCell;
  231. /// <summary>
  232. /// Occurs before a Column header is painted
  233. /// </summary>
  234. public event PaintHeaderEventHandler BeforePaintHeader;
  235. /// <summary>
  236. /// Occurs after a Column header is painted
  237. /// </summary>
  238. public event PaintHeaderEventHandler AfterPaintHeader;
  239. #endregion
  240. #region TableModel
  241. /// <summary>
  242. /// Occurs when the value of the Table's TableModel property changes
  243. /// </summary>
  244. public event EventHandler TableModelChanged;
  245. /// <summary>
  246. /// Occurs when a Row is added into the TableModel
  247. /// </summary>
  248. public event TableModelEventHandler RowAdded;
  249. /// <summary>
  250. /// Occurs when a Row is removed from the TableModel
  251. /// </summary>
  252. public event TableModelEventHandler RowRemoved;
  253. /// <summary>
  254. /// Occurs when the value of the TableModel Selection property changes
  255. /// </summary>
  256. public event SelectionEventHandler SelectionChanged;
  257. /// <summary>
  258. /// Occurs when the value of the RowHeight property changes
  259. /// </summary>
  260. public event EventHandler RowHeightChanged;
  261. #endregion
  262. #endregion
  263. #region Class Data
  264. /// <summary>
  265. /// Required designer variable.
  266. /// </summary>
  267. private System.ComponentModel.Container components = null;
  268. #region Border
  269. /// <summary>
  270. /// The style of the Table's border
  271. /// </summary>
  272. private BorderStyle borderStyle;
  273. #endregion
  274. #region Cells
  275. /// <summary>
  276. /// The last known cell position that the mouse was over
  277. /// </summary>
  278. private CellPos lastMouseCell;
  279. /// <summary>
  280. /// The last known cell position that the mouse's left
  281. /// button was pressed in
  282. /// </summary>
  283. private CellPos lastMouseDownCell;
  284. /// <summary>
  285. /// The position of the Cell that currently has focus
  286. /// </summary>
  287. private CellPos focusedCell;
  288. /// <summary>
  289. /// The Cell that is currently being edited
  290. /// </summary>
  291. private CellPos editingCell;
  292. /// <summary>
  293. /// The ICellEditor that is currently being used to edit a Cell
  294. /// </summary>
  295. private ICellEditor curentCellEditor;
  296. /// <summary>
  297. /// The action that must be performed on a Cell to start editing
  298. /// </summary>
  299. private EditStartAction editStartAction;
  300. /// <summary>
  301. /// The key that must be pressed for editing to start when
  302. /// editStartAction is set to EditStartAction.CustomKey
  303. /// </summary>
  304. private Keys customEditKey;
  305. /// <summary>
  306. /// The amount of time (in milliseconds) that that the
  307. /// mouse pointer must hover over a Cell or Column Header before
  308. /// a MouseHover event is raised
  309. /// </summary>
  310. private int hoverTime;
  311. /// <summary>
  312. /// A TRACKMOUSEEVENT used to set the hoverTime
  313. /// </summary>
  314. private TRACKMOUSEEVENT trackMouseEvent;
  315. #endregion
  316. #region Columns
  317. /// <summary>
  318. /// The ColumnModel of the Table
  319. /// </summary>
  320. private ColumnModel columnModel;
  321. /// <summary>
  322. /// Whether the Table supports column resizing
  323. /// </summary>
  324. private bool columnResizing;
  325. /// <summary>
  326. /// The index of the column currently being resized
  327. /// </summary>
  328. private int resizingColumnIndex;
  329. /// <summary>
  330. /// The x coordinate of the currently resizing column
  331. /// </summary>
  332. private int resizingColumnAnchor;
  333. /// <summary>
  334. /// The horizontal distance between the resize starting
  335. /// point and the right edge of the resizing column
  336. /// </summary>
  337. private int resizingColumnOffset;
  338. /// <summary>
  339. /// The width that the resizing column will be set to
  340. /// once column resizing is finished
  341. /// </summary>
  342. private int resizingColumnWidth;
  343. /// <summary>
  344. /// The index of the current pressed column
  345. /// </summary>
  346. private int pressedColumn;
  347. /// <summary>
  348. /// The index of the current "hot" column
  349. /// </summary>
  350. private int hotColumn;
  351. /// <summary>
  352. /// The index of the last sorted column
  353. /// </summary>
  354. private int lastSortedColumn;
  355. /// <summary>
  356. /// The Color of a sorted Column's background
  357. /// </summary>
  358. private Color sortedColumnBackColor;
  359. #endregion
  360. #region Grid
  361. /// <summary>
  362. /// Indicates whether grid lines appear between the rows and columns
  363. /// containing the rows and cells in the Table
  364. /// </summary>
  365. private GridLines gridLines;
  366. /// <summary>
  367. /// The color of the grid lines
  368. /// </summary>
  369. private Color gridColor;
  370. /// <summary>
  371. /// The line style of the grid lines
  372. /// </summary>
  373. private GridLineStyle gridLineStyle;
  374. #endregion
  375. #region Header
  376. /// <summary>
  377. /// The styles of the column headers
  378. /// </summary>
  379. private ColumnHeaderStyle headerStyle;
  380. /// <summary>
  381. /// The Renderer used to paint the column headers
  382. /// </summary>
  383. private HeaderRenderer headerRenderer;
  384. /// <summary>
  385. /// The font used to draw the text in the column header
  386. /// </summary>
  387. private Font headerFont;
  388. /// <summary>
  389. /// The ContextMenu for the column headers
  390. /// </summary>
  391. private HeaderContextMenu headerContextMenu;
  392. #endregion
  393. #region Items
  394. /// <summary>
  395. /// The TableModel of the Table
  396. /// </summary>
  397. private TableModel tableModel;
  398. #endregion
  399. #region Scrollbars
  400. /// <summary>
  401. /// Indicates whether the Table will allow the user to scroll to any
  402. /// columns or rows placed outside of its visible boundaries
  403. /// </summary>
  404. private bool scrollable;
  405. /// <summary>
  406. /// The Table's horizontal ScrollBar
  407. /// </summary>
  408. private HScrollBar hScrollBar;
  409. /// <summary>
  410. /// The Table's vertical ScrollBar
  411. /// </summary>
  412. private VScrollBar vScrollBar;
  413. #endregion
  414. #region Selection
  415. /// <summary>
  416. /// Specifies whether rows and cells can be selected
  417. /// </summary>
  418. private bool allowSelection;
  419. /// <summary>
  420. /// Specifies whether multiple rows and cells can be selected
  421. /// </summary>
  422. private bool multiSelect;
  423. /// <summary>
  424. /// Specifies whether clicking a row selects all its cells
  425. /// </summary>
  426. private bool fullRowSelect;
  427. /// <summary>
  428. /// Specifies whether the selected rows and cells in the Table remain
  429. /// highlighted when the Table loses focus
  430. /// </summary>
  431. private bool hideSelection;
  432. /// <summary>
  433. /// The background color of selected rows and cells
  434. /// </summary>
  435. private Color selectionBackColor;
  436. /// <summary>
  437. /// The foreground color of selected rows and cells
  438. /// </summary>
  439. private Color selectionForeColor;
  440. /// <summary>
  441. /// The background color of selected rows and cells when the Table
  442. /// doesn't have focus
  443. /// </summary>
  444. private Color unfocusedSelectionBackColor;
  445. /// <summary>
  446. /// The foreground color of selected rows and cells when the Table
  447. /// doesn't have focus
  448. /// </summary>
  449. private Color unfocusedSelectionForeColor;
  450. /// <summary>
  451. /// Determines how selected Cells are hilighted
  452. /// </summary>
  453. private SelectionStyle selectionStyle;
  454. #endregion
  455. #region Table
  456. /// <summary>
  457. /// The state of the table
  458. /// </summary>
  459. private TableState tableState;
  460. /// <summary>
  461. /// Is the Table currently initialising
  462. /// </summary>
  463. private bool init;
  464. /// <summary>
  465. /// The number of times BeginUpdate has been called
  466. /// </summary>
  467. private int beginUpdateCount;
  468. /// <summary>
  469. /// The ToolTip used by the Table to display cell and column tooltips
  470. /// </summary>
  471. private ToolTip toolTip;
  472. /// <summary>
  473. /// The alternating row background color
  474. /// </summary>
  475. private Color alternatingRowColor;
  476. /// <summary>
  477. /// The text displayed in the Table when it has no data to display
  478. /// </summary>
  479. private string noItemsText;
  480. /// <summary>
  481. /// Specifies whether the Table is being used as a preview Table
  482. /// in a ColumnColection editor
  483. /// </summary>
  484. private bool preview;
  485. /*/// <summary>
  486. /// Specifies whether pressing the Tab key while editing moves the
  487. /// editor to the next available cell
  488. /// </summary>
  489. private bool tabMovesEditor;*/
  490. #endregion
  491. #endregion
  492. #region Constructor
  493. /// <summary>
  494. /// Initializes a new instance of the Table class with default settings
  495. /// </summary>
  496. public Table()
  497. {
  498. // starting setup
  499. this.init = true;
  500. // This call is required by the Windows.Forms Form Designer.
  501. components = new System.ComponentModel.Container();
  502. //
  503. this.SetStyle(ControlStyles.UserPaint, true);
  504. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  505. this.SetStyle(ControlStyles.DoubleBuffer, true);
  506. this.SetStyle(ControlStyles.ResizeRedraw, true);
  507. this.SetStyle(ControlStyles.Selectable, true);
  508. this.TabStop = true;
  509. this.Size = new Size(150, 150);
  510. this.BackColor = Color.White;
  511. //
  512. this.columnModel = null;
  513. this.tableModel = null;
  514. // header
  515. this.headerStyle = ColumnHeaderStyle.Clickable;
  516. this.headerFont = this.Font;
  517. this.headerRenderer = new XPHeaderRenderer();
  518. //this.headerRenderer = new GradientHeaderRenderer();
  519. //this.headerRenderer = new FlatHeaderRenderer();
  520. this.headerRenderer.Font = this.headerFont;
  521. this.headerContextMenu = new HeaderContextMenu();
  522. this.columnResizing = true;
  523. this.resizingColumnIndex = -1;
  524. this.resizingColumnWidth = -1;
  525. this.hotColumn = -1;
  526. this.pressedColumn = -1;
  527. this.lastSortedColumn = -1;
  528. this.sortedColumnBackColor = Color.WhiteSmoke;
  529. // borders
  530. this.borderStyle = BorderStyle.Fixed3D;
  531. // scrolling
  532. this.scrollable = true;
  533. this.hScrollBar = new HScrollBar();
  534. this.hScrollBar.Visible = false;
  535. this.hScrollBar.Location = new Point(this.BorderWidth, this.Height - this.BorderWidth - SystemInformation.HorizontalScrollBarHeight);
  536. this.hScrollBar.Width = this.Width - (this.BorderWidth * 2) - SystemInformation.VerticalScrollBarWidth;
  537. this.hScrollBar.Scroll += new ScrollEventHandler(this.OnHorizontalScroll);
  538. this.Controls.Add(this.hScrollBar);
  539. this.vScrollBar = new VScrollBar();
  540. this.vScrollBar.Visible = false;
  541. this.vScrollBar.Location = new Point(this.Width - this.BorderWidth - SystemInformation.VerticalScrollBarWidth, this.BorderWidth);
  542. this.vScrollBar.Height = this.Height - (this.BorderWidth * 2) - SystemInformation.HorizontalScrollBarHeight;
  543. this.vScrollBar.Scroll += new ScrollEventHandler(this.OnVerticalScroll);
  544. this.Controls.Add(this.vScrollBar);
  545. //
  546. this.gridLines = GridLines.None;;
  547. this.gridColor = SystemColors.Control;
  548. this.gridLineStyle = GridLineStyle.Solid;
  549. this.allowSelection = true;
  550. this.multiSelect = false;
  551. this.fullRowSelect = false;
  552. this.hideSelection = false;
  553. this.selectionBackColor = SystemColors.Highlight;
  554. this.selectionForeColor = SystemColors.HighlightText;
  555. this.unfocusedSelectionBackColor = SystemColors.Control;
  556. this.unfocusedSelectionForeColor = SystemColors.ControlText;
  557. this.selectionStyle = SelectionStyle.ListView;
  558. this.alternatingRowColor = Color.Transparent;
  559. // current table state
  560. this.tableState = TableState.Normal;
  561. this.lastMouseCell = new CellPos(-1, -1);
  562. this.lastMouseDownCell = new CellPos(-1, -1);
  563. this.focusedCell = new CellPos(-1, -1);
  564. this.hoverTime = 1000;
  565. this.trackMouseEvent = null;
  566. this.ResetMouseEventArgs();
  567. this.toolTip = new ToolTip(this.components);
  568. this.toolTip.Active = false;
  569. this.toolTip.InitialDelay = 1000;
  570. this.noItemsText = "There are no items in this view";
  571. this.editingCell = new CellPos(-1, -1);
  572. this.curentCellEditor = null;
  573. this.editStartAction = EditStartAction.DoubleClick;
  574. this.customEditKey = Keys.F5;
  575. //this.tabMovesEditor = true;
  576. // finished setting up
  577. this.beginUpdateCount = 0;
  578. this.init = false;
  579. this.preview = false;
  580. }
  581. #endregion
  582. #region Methods
  583. #region Coordinate Translation
  584. #region ClientToDisplayRect
  585. /// <summary>
  586. /// Computes the location of the specified client point into coordinates
  587. /// relative to the display rectangle
  588. /// </summary>
  589. /// <param name="x">The client x coordinate to convert</param>
  590. /// <param name="y">The client y coordinate to convert</param>
  591. /// <returns>A Point that represents the converted coordinates (x, y),
  592. /// relative to the display rectangle</returns>
  593. public Point ClientToDisplayRect(int x, int y)
  594. {
  595. int xPos = x - this.BorderWidth;
  596. if (this.HScroll)
  597. {
  598. xPos += this.hScrollBar.Value;
  599. }
  600. int yPos = y - this.BorderWidth;
  601. if (this.VScroll)
  602. {
  603. yPos += this.TopIndex * this.RowHeight;
  604. }
  605. return new Point(xPos, yPos);
  606. }
  607. /// <summary>
  608. /// Computes the location of the specified client point into coordinates
  609. /// relative to the display rectangle
  610. /// </summary>
  611. /// <param name="p">The client coordinate Point to convert</param>
  612. /// <returns>A Point that represents the converted Point, p,
  613. /// relative to the display rectangle</returns>
  614. public Point ClientToDisplayRect(Point p)
  615. {
  616. return this.ClientToDisplayRect(p.X, p.Y);
  617. }
  618. /// <summary>
  619. /// Converts the location of the specified Rectangle into coordinates
  620. /// relative to the display rectangle
  621. /// </summary>
  622. /// <param name="rect">The Rectangle to convert whose location is in
  623. /// client coordinates</param>
  624. /// <returns>A Rectangle that represents the converted Rectangle, rect,
  625. /// relative to the display rectangle</returns>
  626. public Rectangle ClientToDisplayRect(Rectangle rect)
  627. {
  628. return new Rectangle(this.ClientToDisplayRect(rect.Location), rect.Size);
  629. }
  630. #endregion
  631. #region DisplayRectToClient
  632. /// <summary>
  633. /// Computes the location of the specified point relative to the display
  634. /// rectangle point into client coordinates
  635. /// </summary>
  636. /// <param name="x">The x coordinate to convert relative to the display rectangle</param>
  637. /// <param name="y">The y coordinate to convert relative to the display rectangle</param>
  638. /// <returns>A Point that represents the converted coordinates (x, y) relative to
  639. /// the display rectangle in client coordinates</returns>
  640. public Point DisplayRectToClient(int x, int y)
  641. {
  642. int xPos = x + this.BorderWidth;
  643. if (this.HScroll)
  644. {
  645. xPos -= this.hScrollBar.Value;
  646. }
  647. int yPos = y + this.BorderWidth;
  648. if (this.VScroll)
  649. {
  650. yPos -= this.TopIndex * this.RowHeight;
  651. }
  652. return new Point(xPos, yPos);
  653. }
  654. /// <summary>
  655. /// Computes the location of the specified point relative to the display
  656. /// rectangle into client coordinates
  657. /// </summary>
  658. /// <param name="p">The point relative to the display rectangle to convert</param>
  659. /// <returns>A Point that represents the converted Point relative to
  660. /// the display rectangle, p, in client coordinates</returns>
  661. public Point DisplayRectToClient(Point p)
  662. {
  663. return this.DisplayRectToClient(p.X, p.Y);
  664. }
  665. /// <summary>
  666. /// Converts the location of the specified Rectangle relative to the display
  667. /// rectangle into client coordinates
  668. /// </summary>
  669. /// <param name="rect">The Rectangle to convert whose location is relative to
  670. /// the display rectangle</param>
  671. /// <returns>A Rectangle that represents the converted Rectangle relative to
  672. /// the display rectangle, rect, in client coordinates</returns>
  673. public Rectangle DisplayRectToClient(Rectangle rect)
  674. {
  675. return new Rectangle(this.DisplayRectToClient(rect.Location), rect.Size);
  676. }
  677. #endregion
  678. #region Cells
  679. /// <summary>
  680. /// Returns the Cell at the specified client coordinates
  681. /// </summary>
  682. /// <param name="x">The client x coordinate of the Cell</param>
  683. /// <param name="y">The client y coordinate of the Cell</param>
  684. /// <returns>The Cell at the specified client coordinates, or
  685. /// null if it does not exist</returns>
  686. public Cell CellAt(int x, int y)
  687. {
  688. int row = this.RowIndexAt(x, y);
  689. int column = this.ColumnIndexAt(x, y);
  690. // return null if the row or column don't exist
  691. if (row == -1 || row >= this.TableModel.Rows.Count || column == -1 || column >= this.TableModel.Rows[row].Cells.Count)
  692. {
  693. return null;
  694. }
  695. return this.TableModel[row, column];
  696. }
  697. /// <summary>
  698. /// Returns the Cell at the specified client Point
  699. /// </summary>
  700. /// <param name="p">The point of interest</param>
  701. /// <returns>The Cell at the specified client Point,
  702. /// or null if not found</returns>
  703. public Cell CellAt(Point p)
  704. {
  705. return this.CellAt(p.X, p.Y);
  706. }
  707. /// <summary>
  708. /// Returns a Rectangle that specifies the size and location the cell at
  709. /// the specified row and column indexes in client coordinates
  710. /// </summary>
  711. /// <param name="row">The index of the row that contains the cell</param>
  712. /// <param name="column">The index of the column that contains the cell</param>
  713. /// <returns>A Rectangle that specifies the size and location the cell at
  714. /// the specified row and column indexes in client coordinates</returns>
  715. public Rectangle CellRect(int row, int column)
  716. {
  717. // return null if the row or column don't exist
  718. if (row == -1 || row >= this.TableModel.Rows.Count || column == -1 || column >= this.TableModel.Rows[row].Cells.Count)
  719. {
  720. return Rectangle.Empty;
  721. }
  722. Rectangle columnRect = this.ColumnRect(column);
  723. if (columnRect == Rectangle.Empty)
  724. {
  725. return columnRect;
  726. }
  727. Rectangle rowRect = this.RowRect(row);
  728. if (rowRect == Rectangle.Empty)
  729. {
  730. return rowRect;
  731. }
  732. return new Rectangle(columnRect.X, rowRect.Y, columnRect.Width, rowRect.Height);
  733. }
  734. /// <summary>
  735. /// Returns a Rectangle that specifies the size and location the cell at
  736. /// the specified cell position in client coordinates
  737. /// </summary>
  738. /// <param name="cellPos">The position of the cell</param>
  739. /// <returns>A Rectangle that specifies the size and location the cell at
  740. /// the specified cell position in client coordinates</returns>
  741. public Rectangle CellRect(CellPos cellPos)
  742. {
  743. return this.CellRect(cellPos.Row, cellPos.Column);
  744. }
  745. /// <summary>
  746. /// Returns a Rectangle that specifies the size and location of the
  747. /// specified cell in client coordinates
  748. /// </summary>
  749. /// <param name="cell">The cell whose bounding rectangle is to be retrieved</param>
  750. /// <returns>A Rectangle that specifies the size and location the specified
  751. /// cell in client coordinates</returns>
  752. public Rectangle CellRect(Cell cell)
  753. {
  754. if (cell == null || cell.Row == null || cell.InternalIndex == -1)
  755. {
  756. return Rectangle.Empty;
  757. }
  758. if (this.TableModel == null || this.ColumnModel == null)
  759. {
  760. return Rectangle.Empty;
  761. }
  762. int row = this.TableModel.Rows.IndexOf(cell.Row);
  763. int col = cell.InternalIndex;
  764. return this.CellRect(row, col);
  765. }
  766. /// <summary>
  767. /// Returns whether Cell at the specified row and column indexes
  768. /// is not null
  769. /// </summary>
  770. /// <param name="row">The row index of the cell</param>
  771. /// <param name="column">The column index of the cell</param>
  772. /// <returns>True if the cell at the specified row and column indexes
  773. /// is not null, otherwise false</returns>
  774. protected internal bool IsValidCell(int row, int column)
  775. {
  776. if (this.TableModel != null && this.ColumnModel != null)
  777. {
  778. if (row >= 0 && row < this.TableModel.Rows.Count)
  779. {
  780. if (column >= 0 && column < this.ColumnModel.Columns.Count)
  781. {
  782. return (this.TableModel.Rows[row].Cells[column] != null);
  783. }
  784. }
  785. }
  786. return false;
  787. }
  788. /// <summary>
  789. /// Returns whether Cell at the specified cell position is not null
  790. /// </summary>
  791. /// <param name="cellPos">The position of the cell</param>
  792. /// <returns>True if the cell at the specified cell position is not
  793. /// null, otherwise false</returns>
  794. protected internal bool IsValidCell(CellPos cellPos)
  795. {
  796. return this.IsValidCell(cellPos.Row, cellPos.Column);
  797. }
  798. /// <summary>
  799. /// Returns a CellPos that specifies the next Cell that is visible
  800. /// and enabled from the specified Cell
  801. /// </summary>
  802. /// <param name="start">A CellPos that specifies the Cell to start
  803. /// searching from</param>
  804. /// <param name="wrap">Specifies whether to move to the start of the
  805. /// next Row when the end of the current Row is reached</param>
  806. /// <param name="forward">Specifies whether the search should travel
  807. /// in a forward direction (top to bottom, left to right) through the Cells</param>
  808. /// <param name="includeStart">Indicates whether the specified starting
  809. /// Cell is included in the search</param>
  810. /// <param name="checkOtherCellsInRow">Specifies whether all Cells in
  811. /// the Row should be included in the search</param>
  812. /// <returns>A CellPos that specifies the next Cell that is visible
  813. /// and enabled, or CellPos.Empty if there are no Cells that are visible
  814. /// and enabled</returns>
  815. protected CellPos FindNextVisibleEnabledCell(CellPos start, bool wrap, bool forward, bool includeStart, bool checkOtherCellsInRow)
  816. {
  817. if (this.ColumnCount == 0 || this.RowCount == 0)
  818. {
  819. return CellPos.Empty;
  820. }
  821. int startRow = start.Row != -1 ? start.Row : 0;
  822. int startCol = start.Column != -1 ? start.Column : 0;
  823. bool first = true;
  824. if (forward)
  825. {
  826. for (int i=startRow; i<this.RowCount; i++)
  827. {
  828. int j = (first || !checkOtherCellsInRow ? startCol : 0);
  829. for (; j<this.TableModel.Rows[i].Cells.Count; j++)
  830. {
  831. if (i == startRow && j == startCol)
  832. {
  833. if (!first)
  834. {
  835. return CellPos.Empty;
  836. }
  837. first = false;
  838. if (!includeStart)
  839. {
  840. if (!checkOtherCellsInRow)
  841. {
  842. break;
  843. }
  844. continue;
  845. }
  846. }
  847. if (this.IsValidCell(i, j) && this.IsValidColumn(j) && this.TableModel[i, j].Enabled && this.ColumnModel.Columns[j].Enabled && this.ColumnModel.Columns[j].Visible)
  848. {
  849. return new CellPos(i, j);
  850. }
  851. if (!checkOtherCellsInRow)
  852. {
  853. continue;
  854. }
  855. }
  856. if (wrap)
  857. {
  858. if (i+1 == this.TableModel.Rows.Count)
  859. {
  860. i = -1;
  861. }
  862. }
  863. else
  864. {
  865. break;
  866. }
  867. }
  868. }
  869. else
  870. {
  871. for (int i=startRow; i>=0; i--)
  872. {
  873. int j = (first || !checkOtherCellsInRow ? startCol : this.TableModel.Rows[i].Cells.Count);
  874. for (; j>=0; j--)
  875. {
  876. if (i == startRow && j == startCol)
  877. {
  878. if (!first)
  879. {
  880. return CellPos.Empty;
  881. }
  882. first = false;
  883. if (!includeStart)
  884. {
  885. if (!checkOtherCellsInRow)
  886. {
  887. break;
  888. }
  889. continue;
  890. }
  891. }
  892. if (this.IsValidCell(i, j) && this.IsValidColumn(j) && this.TableModel[i, j].Enabled && this.ColumnModel.Columns[j].Enabled && this.ColumnModel.Columns[j].Visible)
  893. {
  894. return new CellPos(i, j);
  895. }
  896. if (!checkOtherCellsInRow)
  897. {
  898. continue;
  899. }
  900. }
  901. if (wrap)
  902. {
  903. if (i-1 == -1)
  904. {
  905. i = this.TableModel.Rows.Count;
  906. }
  907. }
  908. else
  909. {
  910. break;
  911. }
  912. }
  913. }
  914. return CellPos.Empty;
  915. }
  916. /// <summary>
  917. /// Returns a CellPos that specifies the next Cell that able to be
  918. /// edited from the specified Cell
  919. /// </summary>
  920. /// <param name="start">A CellPos that specifies the Cell to start
  921. /// searching from</param>
  922. /// <param name="wrap">Specifies whether to move to the start of the
  923. /// next Row when the end of the current Row is reached</param>
  924. /// <param name="forward">Specifies whether the search should travel
  925. /// in a forward direction (top to bottom, left to right) through the Cells</param>
  926. /// <param name="includeStart">Indicates whether the specified starting
  927. /// Cell is included in the search</param>
  928. /// <returns>A CellPos that specifies the next Cell that is able to
  929. /// be edited, or CellPos.Empty if there are no Cells that editable</returns>
  930. protected CellPos FindNextEditableCell(CellPos start, bool wrap, bool forward, bool includeStart)
  931. {
  932. if (this.ColumnCount == 0 || this.RowCount == 0)
  933. {
  934. return CellPos.Empty;
  935. }
  936. int startRow = start.Row != -1 ? start.Row : 0;
  937. int startCol = start.Column != -1 ? start.Column : 0;
  938. bool first = true;
  939. if (forward)
  940. {
  941. for (int i=startRow; i<this.RowCount; i++)
  942. {
  943. int j = (first ? startCol : 0);
  944. for (; j<this.TableModel.Rows[i].Cells.Count; j++)
  945. {
  946. if (i == startRow && j == startCol)
  947. {
  948. if (!first)
  949. {
  950. return CellPos.Empty;
  951. }
  952. first = false;
  953. if (!includeStart)
  954. {
  955. continue;
  956. }
  957. }
  958. if (this.IsValidCell(i, j) && this.IsValidColumn(j) && this.TableModel[i, j].Editable && this.ColumnModel.Columns[j].Editable)
  959. {
  960. return new CellPos(i, j);
  961. }
  962. }
  963. if (wrap)
  964. {
  965. if (i+1 == this.TableModel.Rows.Count)
  966. {
  967. i = -1;
  968. }
  969. }
  970. else
  971. {
  972. break;
  973. }
  974. }
  975. }
  976. else
  977. {
  978. for (int i=startRow; i>=0; i--)
  979. {
  980. int j = (first ? startCol : this.TableModel.Rows[i].Cells.Count);
  981. for (; j>=0; j--)
  982. {
  983. if (i == startRow && j == startCol)
  984. {
  985. if (!first)
  986. {
  987. return CellPos.Empty;
  988. }
  989. first = false;
  990. if (!includeStart)
  991. {
  992. continue;
  993. }
  994. }
  995. if (this.IsValidCell(i, j) && this.IsValidColumn(j) && this.TableModel[i, j].Editable && this.ColumnModel.Columns[j].Editable)
  996. {
  997. return new CellPos(i, j);
  998. }
  999. }
  1000. if (wrap)
  1001. {
  1002. if (i-1 == -1)
  1003. {
  1004. i = this.TableModel.Rows.Count;
  1005. }
  1006. }
  1007. else
  1008. {
  1009. break;
  1010. }
  1011. }
  1012. }
  1013. return CellPos.Empty;
  1014. }
  1015. #endregion
  1016. #region Columns
  1017. /// <summary>
  1018. /// Returns the index of the Column at the specified client coordinates
  1019. /// </summary>
  1020. /// <param name="x">The client x coordinate of the Column</param>
  1021. /// <param name="y">The client y coordinate of the Column</param>
  1022. /// <returns>The index of the Column at the specified client coordinates, or
  1023. /// -1 if it does not exist</returns>
  1024. public int ColumnIndexAt(int x, int y)
  1025. {
  1026. if (this.ColumnModel == null)
  1027. {
  1028. return -1;
  1029. }
  1030. // convert to DisplayRect coordinates before
  1031. // sending to the ColumnModel
  1032. return this.ColumnModel.ColumnIndexAtX(this.hScrollBar.Value + x - this.BorderWidth);
  1033. }
  1034. /// <summary>
  1035. /// Returns the index of the Column at the specified client point
  1036. /// </summary>
  1037. /// <param name="p">The point of interest</param>
  1038. /// <returns>The index of the Column at the specified client point, or
  1039. /// -1 if it does not exist</returns>
  1040. public int ColumnIndexAt(Point p)
  1041. {
  1042. return this.ColumnIndexAt(p.X, p.Y);
  1043. }
  1044. /// <summary>
  1045. /// Returns the bounding rectangle of the specified
  1046. /// column's header in client coordinates
  1047. /// </summary>
  1048. /// <param name="column">The index of the column</param>
  1049. /// <returns>The bounding rectangle of the specified
  1050. /// column's header</returns>
  1051. public Rectangle ColumnHeaderRect(int column)
  1052. {
  1053. if (this.ColumnModel == null)
  1054. {
  1055. return Rectangle.Empty;
  1056. }
  1057. Rectangle rect = this.ColumnModel.ColumnHeaderRect(column);
  1058. if (rect == Rectangle.Empty)
  1059. {
  1060. return rect;
  1061. }
  1062. rect.X -= this.hScrollBar.Value - this.BorderWidth;
  1063. rect.Y = this.BorderWidth;
  1064. return rect;
  1065. }
  1066. /// <summary>
  1067. /// Returns the bounding rectangle of the specified
  1068. /// column's header in client coordinates
  1069. /// </summary>
  1070. /// <param name="column">The column</param>
  1071. /// <returns>The bounding rectangle of the specified
  1072. /// column's header</returns>
  1073. public Rectangle ColumnHeaderRect(Column column)
  1074. {
  1075. if (this.ColumnModel == null)
  1076. {
  1077. return Rectangle.Empty;
  1078. }
  1079. return this.ColumnHeaderRect(this.ColumnModel.Columns.IndexOf(column));
  1080. }
  1081. /// <summary>
  1082. /// Returns the bounding rectangle of the column at the
  1083. /// specified index in client coordinates
  1084. /// </summary>
  1085. /// <param name="column">The column</param>
  1086. /// <returns>The bounding rectangle of the column at the
  1087. /// specified index</returns>
  1088. public Rectangle ColumnRect(int column)
  1089. {
  1090. if (this.ColumnModel == null)
  1091. {
  1092. return Rectangle.Empty;
  1093. }
  1094. Rectangle rect = this.ColumnHeaderRect(column);
  1095. if (rect == Rectangle.Empty)
  1096. {
  1097. return rect;
  1098. }
  1099. rect.Y += this.HeaderHeight;
  1100. rect.Height = this.TotalRowHeight;
  1101. return rect;
  1102. }
  1103. /// <summary>
  1104. /// Returns the bounding rectangle of the specified column
  1105. /// in client coordinates
  1106. /// </summary>
  1107. /// <param name="column">The column</param>
  1108. /// <returns>The bounding rectangle of the specified
  1109. /// column</returns>
  1110. public Rectangle ColumnRect(Column column)
  1111. {
  1112. if (this.ColumnModel == null)
  1113. {
  1114. return Rectangle.Empty;
  1115. }
  1116. return this.ColumnRect(this.ColumnModel.Columns.IndexOf(column));
  1117. }
  1118. #endregion
  1119. #region Rows
  1120. /// <summary>
  1121. /// Returns the index of the Row at the specified client coordinates
  1122. /// </summary>
  1123. /// <param name="x">The client x coordinate of the Row</param>
  1124. /// <param name="y">The client y coordinate of the Row</param>
  1125. /// <returns>The index of the Row at the specified client coordinates, or
  1126. /// -1 if it does not exist</returns>
  1127. public int RowIndexAt(int x, int y)
  1128. {
  1129. if (this.TableModel == null)
  1130. {
  1131. return -1;
  1132. }
  1133. if (this.HeaderStyle != ColumnHeaderStyle.None)
  1134. {
  1135. y -= this.HeaderHeight;
  1136. }
  1137. y -= this.BorderWidth;
  1138. if (y < 0)
  1139. {
  1140. return -1;
  1141. }
  1142. if (this.VScroll)
  1143. {
  1144. y += this.TopIndex * this.RowHeight;
  1145. }
  1146. return this.TableModel.RowIndexAt(y);
  1147. }
  1148. /// <summary>
  1149. /// Returns the index of the Row at the specified client point
  1150. /// </summary>
  1151. /// <param name="p">The point of interest</param>
  1152. /// <returns>The index of the Row at the specified client point, or
  1153. /// -1 if it does not exist</returns>
  1154. public int RowIndexAt(Point p)
  1155. {
  1156. return this.RowIndexAt(p.X, p.Y);
  1157. }
  1158. /// <summary>
  1159. /// Returns the bounding rectangle of the row at the
  1160. /// specified index in client coordinates
  1161. /// </summary>
  1162. /// <param name="row">The index of the row</param>
  1163. /// <returns>The bounding rectangle of the row at the
  1164. /// specified index</returns>
  1165. public Rectangle RowRect(int row)
  1166. {
  1167. if (this.TableModel == null || this.ColumnModel == null || row == -1 || row > this.TableModel.Rows.Count)
  1168. {
  1169. return Rectangle.Empty;
  1170. }
  1171. Rectangle rect = new Rectangle();
  1172. rect.X = this.DisplayRectangle.X;
  1173. rect.Y = this.BorderWidth + ((row - this.TopIndex) * this.RowHeight);
  1174. rect.Width = this.ColumnModel.VisibleColumnsWidth;
  1175. rect.Height = this.RowHeight;
  1176. if (this.HeaderStyle != ColumnHeaderStyle.None)
  1177. {
  1178. rect.Y += this.HeaderHeight;
  1179. }
  1180. return rect;
  1181. }
  1182. /// <summary>
  1183. /// Returns the bounding rectangle of the specified row
  1184. /// in client coordinates
  1185. /// </summary>
  1186. /// <param name="row">The row</param>
  1187. /// <returns>The bounding rectangle of the specified
  1188. /// row</returns>
  1189. public Rectangle RowRect(Row row)
  1190. {
  1191. if (this.TableModel == null)
  1192. {
  1193. return Rectangle.Empty;
  1194. }
  1195. return this.RowRect(this.TableModel.Rows.IndexOf(row));
  1196. }
  1197. #endregion
  1198. #region Hit Tests
  1199. /// <summary>
  1200. /// Returns a TableRegions value that represents the table region at
  1201. /// the specified client coordinates
  1202. /// </summary>
  1203. /// <param name="x">The client x coordinate</param>
  1204. /// <param name="y">The client y coordinate</param>
  1205. /// <returns>A TableRegions value that represents the table region at
  1206. /// the specified client coordinates</returns>
  1207. public TableRegion HitTest(int x, int y)
  1208. {
  1209. if (this.HeaderStyle != ColumnHeaderStyle.None && this.HeaderRectangle.Contains(x, y))
  1210. {
  1211. return TableRegion.ColumnHeader;
  1212. }
  1213. else if (this.CellDataRect.Contains(x, y))
  1214. {
  1215. return TableRegion.Cells;
  1216. }
  1217. else if (!this.Bounds.Contains(x, y))
  1218. {
  1219. return TableRegion.NoWhere;
  1220. }
  1221. return TableRegion.NonClientArea;
  1222. }
  1223. /// <summary>
  1224. /// Returns a TableRegions value that represents the table region at
  1225. /// the specified client point
  1226. /// </summary>
  1227. /// <param name="p">The point of interest</param>
  1228. /// <returns>A TableRegions value that represents the table region at
  1229. /// the specified client point</returns>
  1230. public TableRegion HitTest(Point p)
  1231. {
  1232. return this.HitTest(p.X, p.Y);
  1233. }
  1234. #endregion
  1235. #endregion
  1236. #region Dispose
  1237. /// <summary>
  1238. /// Releases the unmanaged resources used by the Control and optionally
  1239. /// releases the managed resources
  1240. /// </summary>
  1241. /// <param name="disposing">true to release both managed and unmanaged
  1242. /// resources; false to release only unmanaged resources</param>
  1243. protected override void Dispose(bool disposing)
  1244. {
  1245. if (disposing)
  1246. {
  1247. if (components != null)
  1248. {
  1249. components.Dispose();
  1250. }
  1251. }
  1252. base.Dispose(disposing);
  1253. }
  1254. /// <summary>
  1255. /// Removes the ColumnModel and TableModel from the Table
  1256. /// </summary>
  1257. public void Clear()
  1258. {
  1259. if (this.ColumnModel != null)
  1260. {
  1261. this.ColumnModel = null;
  1262. }
  1263. if (this.TableModel != null)
  1264. {
  1265. this.TableModel = null;
  1266. }
  1267. }
  1268. #endregion
  1269. #region Editing
  1270. /// <summary>
  1271. /// Records the Cell that is currently being edited and the
  1272. /// ICellEditor used to edit the Cell
  1273. /// </summary>
  1274. /// <param name="cell">The Cell that is currently being edited</param>
  1275. /// <param name="editor">The ICellEditor used to edit the Cell</param>
  1276. private void SetEditingCell(Cell cell, ICellEditor editor)
  1277. {
  1278. this.SetEditingCell(new CellPos(cell.Row.InternalIndex, cell.InternalIndex), editor);
  1279. }
  1280. /// <summary>
  1281. /// Records the Cell that is currently being edited and the
  1282. /// ICellEditor used to edit the Cell
  1283. /// </summary>
  1284. /// <param name="cellPos">The Cell that is currently being edited</param>
  1285. /// <param name="editor">The ICellEditor used to edit the Cell</param>
  1286. private void SetEditingCell(CellPos cellPos, ICellEditor editor)
  1287. {
  1288. this.editingCell = cellPos;
  1289. this.curentCellEditor = editor;
  1290. }
  1291. /// <summary>
  1292. /// Starts editing the Cell at the specified row and column indexes
  1293. /// </summary>
  1294. /// <param name="row">The row index of the Cell to be edited</param>
  1295. /// <param name="column">The column index of the Cell to be edited</param>
  1296. public void EditCell(int row, int column)
  1297. {
  1298. this.EditCell(new CellPos(row, column));
  1299. }
  1300. /// <summary>
  1301. /// Starts editing the Cell at the specified CellPos
  1302. /// </summary>
  1303. /// <param name="cellPos">A CellPos that specifies the Cell to be edited</param>
  1304. public void EditCell(CellPos cellPos)
  1305. {
  1306. // don't bother if the cell doesn't exists or the cell's
  1307. // column is not visible or the cell is not editable
  1308. if (!this.IsValidCell(cellPos) || !this.ColumnModel.Columns[cellPos.Column].Visible || !this.IsCellEditable(cellPos))
  1309. {
  1310. return;
  1311. }
  1312. // check if we're currently editing a cell
  1313. if (this.EditingCell != CellPos.Empty)
  1314. {
  1315. // don't bother if we're already editing the cell.
  1316. // if we're editing a different cell stop editing
  1317. if (this.EditingCell == cellPos)
  1318. {
  1319. return;
  1320. }
  1321. else
  1322. {
  1323. this.EditingCellEditor.StopEditing();
  1324. }
  1325. }
  1326. Cell cell = this.TableModel[cellPos];
  1327. ICellEditor editor = this.ColumnModel.GetCellEditor(cellPos.Column);
  1328. // make sure we have an editor and that the cell
  1329. // and the cell's column are editable
  1330. if (editor == null || !cell.Editable || !this.ColumnModel.Columns[cellPos.Column].Editable)
  1331. {
  1332. return;
  1333. }
  1334. if (this.EnsureVisible(cellPos))
  1335. {
  1336. this.Refresh();
  1337. }
  1338. Rectangle cellRect = this.CellRect(cellPos);
  1339. // give anyone subscribed to the table's BeginEditing
  1340. // event the first chance to cancel editing
  1341. CellEditEventArgs e = new CellEditEventArgs(cell, editor, this, cellPos.Row, cellPos.Column, cellRect);
  1342. this.OnBeginEditing(e);
  1343. //
  1344. if (!e.Cancel)
  1345. {
  1346. // get the editor ready for editing. if PrepareForEditing
  1347. // returns false, someone who subscribed to the editors
  1348. // BeginEdit event has cancelled editing
  1349. if (!editor.PrepareForEditing(cell, this, cellPos, cellRect, e.Handled))
  1350. {
  1351. return;
  1352. }
  1353. // keep track of the editing cell and editor
  1354. // and start editing
  1355. this.editingCell = cellPos;
  1356. this.curentCellEditor = editor;
  1357. editor.StartEditing();
  1358. }
  1359. }
  1360. /*/// <summary>
  1361. /// Stops editing the current Cell and starts editing the next editable Cell
  1362. /// </summary>
  1363. /// <param name="forwards">Specifies whether the editor should traverse
  1364. /// forward when looking for the next editable Cell</param>
  1365. protected internal void EditNextCell(bool forwards)
  1366. {
  1367. if (this.EditingCell == CellPos.Empty)
  1368. {
  1369. return;
  1370. }
  1371. CellPos nextCell = this.FindNextEditableCell(this.FocusedCell, true, forwards, false);
  1372. if (nextCell != CellPos.Empty && nextCell != this.EditingCell)
  1373. {
  1374. this.StopEditing();
  1375. this.EditCell(nextCell);
  1376. }
  1377. }*/
  1378. /// <summary>
  1379. /// Stops editing the current Cell and commits any changes
  1380. /// </summary>
  1381. public void StopEditing()
  1382. {
  1383. // don't bother if we're not editing
  1384. if (this.EditingCell == CellPos.Empty)
  1385. {
  1386. return;
  1387. }
  1388. this.EditingCellEditor.StopEditing();
  1389. this.Invalidate(this.RowRect(this.editingCell.Row));
  1390. this.editingCell = CellPos.Empty;
  1391. this.curentCellEditor = null;
  1392. }
  1393. /// <summary>
  1394. /// Cancels editing the current Cell and ignores any changes
  1395. /// </summary>
  1396. public void CancelEditing()
  1397. {
  1398. // don't bother if we're not editing
  1399. if (this.EditingCell == CellPos.Empty)
  1400. {
  1401. return;
  1402. }
  1403. this.EditingCellEditor.CancelEditing();
  1404. this.editingCell = CellPos.Empty;
  1405. this.curentCellEditor = null;
  1406. }
  1407. /// <summary>
  1408. /// Returns whether the Cell at the specified row and column is able
  1409. /// to be edited by the user
  1410. /// </summary>
  1411. /// <param name="row">The row index of the Cell to check</param>
  1412. /// <param name="column">The column index of the Cell to check</param>
  1413. /// <returns>True if the Cell at the specified row and column is able
  1414. /// to be edited by the user, false otherwise</returns>
  1415. public bool IsCellEditable(int row, int column)
  1416. {
  1417. return this.IsCellEditable(new CellPos(row, column));
  1418. }
  1419. /// <summary>
  1420. /// Returns whether the Cell at the specified CellPos is able
  1421. /// to be edited by the user
  1422. /// </summary>
  1423. /// <param name="cellpos">A CellPos that specifies the Cell to check</param>
  1424. /// <returns>True if the Cell at the specified CellPos is able
  1425. /// to be edited by the user, false otherwise</returns>
  1426. public bool IsCellEditable(CellPos cellpos)
  1427. {
  1428. // don't bother if the cell doesn't exists or the cell's
  1429. // column is not visible
  1430. if (!this.IsValidCell(cellpos) || !this.ColumnModel.Columns[cellpos.Column].Visible)
  1431. {
  1432. return false;
  1433. }
  1434. return (this.TableModel[cellpos].Editable &&
  1435. this.ColumnModel.Columns[cellpos.Column].Editable);
  1436. }
  1437. /// <summary>
  1438. /// Returns whether the Cell at the specified row and column is able
  1439. /// to respond to user interaction
  1440. /// </summary>
  1441. /// <param name="row">The row index of the Cell to check</param>
  1442. /// <param name="column">The column index of the Cell to check</param>
  1443. /// <returns>True if the Cell at the specified row and column is able
  1444. /// to respond to user interaction, false otherwise</returns>
  1445. public bool IsCellEnabled(int row, int column)
  1446. {
  1447. return this.IsCellEnabled(new CellPos(row, column));
  1448. }
  1449. /// <summary>
  1450. /// Returns whether the Cell at the specified CellPos is able
  1451. /// to respond to user interaction
  1452. /// </summary>
  1453. /// <param name="cellpos">A CellPos that specifies the Cell to check</param>
  1454. /// <returns>True if the Cell at the specified CellPos is able
  1455. /// to respond to user interaction, false otherwise</returns>
  1456. public bool IsCellEnabled(CellPos cellpos)
  1457. {
  1458. // don't bother if the cell doesn't exists or the cell's
  1459. // column is not visible
  1460. if (!this.IsValidCell(cellpos) || !this.ColumnModel.Columns[cellpos.Column].Visible)
  1461. {
  1462. return false;
  1463. }
  1464. return (this.TableModel[cellpos].Enabled &&
  1465. this.ColumnModel.Columns[cellpos.Column].Enabled);
  1466. }
  1467. #endregion
  1468. #region Invalidate
  1469. /// <summary>
  1470. /// Invalidates the specified Cell
  1471. /// </summary>
  1472. /// <param name="cell">The Cell to be invalidated</param>
  1473. public void InvalidateCell(Cell cell)
  1474. {
  1475. this.InvalidateCell(cell.Row.Index, cell.Index);
  1476. }
  1477. /// <summary>
  1478. /// Invalidates the Cell located at the specified row and column indicies
  1479. /// </summary>
  1480. /// <param name="row">The row index of the Cell to be invalidated</param>
  1481. /// <param name="column">The column index of the Cell to be invalidated</param>
  1482. public void InvalidateCell(int row, int column)
  1483. {
  1484. Rectangle cellRect = this.CellRect(row, column);
  1485. if (cellRect == Rectangle.Empty)
  1486. {
  1487. return;
  1488. }
  1489. if (cellRect.IntersectsWith(this.CellDataRect))
  1490. {
  1491. this.Invalidate(Rectangle.Intersect(this.CellDataRect, cellRect), false);
  1492. }
  1493. }
  1494. /// <summary>
  1495. /// Invalidates the Cell located at the specified CellPos
  1496. /// </summary>
  1497. /// <param name="cellPos">A CellPos that specifies the Cell to be invalidated</param>
  1498. public void InvalidateCell(CellPos cellPos)
  1499. {
  1500. this.InvalidateCell(cellPos.Row, cellPos.Column);
  1501. }
  1502. /// <summary>
  1503. /// Invalidates the specified Row
  1504. /// </summary>
  1505. /// <param name="row">The Row to be invalidated</param>
  1506. public void InvalidateRow(Row row)
  1507. {
  1508. this.InvalidateRow(row.Index);
  1509. }
  1510. /// <summary>
  1511. /// Invalidates the Row located at the specified row index
  1512. /// </summary>
  1513. /// <param name="row">The row index of the Row to be invalidated</param>
  1514. public void InvalidateRow(int row)
  1515. {
  1516. Rectangle rowRect = this.RowRect(row);
  1517. if (rowRect == Rectangle.Empty)
  1518. {
  1519. return;
  1520. }
  1521. if (rowRect.IntersectsWith(this.CellDataRect))
  1522. {
  1523. this.Invalidate(Rectangle.Intersect(this.CellDataRect, rowRect), false);
  1524. }
  1525. }
  1526. /// <summary>
  1527. /// Invalidates the Row located at the specified CellPos
  1528. /// </summary>
  1529. /// <param name="cellPos">A CellPos that specifies the Row to be invalidated</param>
  1530. public void InvalidateRow(CellPos cellPos)
  1531. {
  1532. this.InvalidateRow(cellPos.Row);
  1533. }
  1534. #endregion
  1535. #region Keys
  1536. /// <summary>
  1537. /// Determines whether the specified key is reserved for use by the Table
  1538. /// </summary>
  1539. /// <param name="key">One of the Keys values</param>
  1540. /// <returns>true if the specified key is reserved for use by the Table;
  1541. /// otherwise, false</returns>
  1542. protected internal bool IsReservedKey(Keys key)
  1543. {
  1544. if ((key & Keys.Alt) != Keys.Alt)
  1545. {
  1546. Keys k = key & Keys.KeyCode;
  1547. return (k == Keys.Up ||
  1548. k == Keys.Down ||
  1549. k == Keys.Left ||
  1550. k == Keys.Right ||
  1551. k == Keys.PageUp ||
  1552. k == Keys.PageDown ||
  1553. k == Keys.Home ||
  1554. k == Keys.End ||
  1555. k == Keys.Tab);
  1556. }
  1557. return false;
  1558. }
  1559. /// <summary>
  1560. /// Determines whether the specified key is a regular input key or a special
  1561. /// key that requires preprocessing
  1562. /// </summary>
  1563. /// <param name="keyData">One of the Keys values</param>
  1564. /// <returns>true if the specified key is a regular input key; otherwise, false</returns>
  1565. protected override bool IsInputKey(Keys keyData)
  1566. {
  1567. if ((keyData & Keys.Alt) != Keys.Alt)
  1568. {
  1569. Keys key = keyData & Keys.KeyCode;
  1570. switch (key)
  1571. {
  1572. case Keys.Up:
  1573. case Keys.Down:
  1574. case Keys.Left:
  1575. case Keys.Right:
  1576. case Keys.Prior:
  1577. case Keys.Next:
  1578. case Keys.End:
  1579. case Keys.Home:
  1580. {
  1581. return true;
  1582. }
  1583. }
  1584. if (base.IsInputKey(keyData))
  1585. {
  1586. return true;
  1587. }
  1588. }
  1589. return false;
  1590. }
  1591. #endregion
  1592. #region Layout
  1593. /// <summary>
  1594. /// Prevents the Table from drawing until the EndUpdate method is called
  1595. /// </summary>
  1596. public void BeginUpdate()
  1597. {
  1598. if (this.IsHandleCreated)
  1599. {
  1600. if (this.beginUpdateCount == 0)
  1601. {
  1602. NativeMethods.SendMessage(this.Handle, 11, 0, 0);
  1603. }
  1604. this.beginUpdateCount++;
  1605. }
  1606. }
  1607. /// <summary>
  1608. /// Resumes drawing of the Table after drawing is suspended by the
  1609. /// BeginUpdate method
  1610. /// </summary>
  1611. public void EndUpdate()
  1612. {
  1613. if (this.beginUpdateCount <= 0)
  1614. {
  1615. return;
  1616. }
  1617. this.beginUpdateCount--;
  1618. if (this.beginUpdateCount == 0)
  1619. {
  1620. NativeMethods.SendMessage(this.Handle, 11, -1, 0);
  1621. this.PerformLayout();
  1622. this.Invalidate(true);
  1623. }
  1624. }
  1625. /// <summary>
  1626. /// Signals the object that initialization is starting
  1627. /// </summary>
  1628. public void BeginInit()
  1629. {
  1630. this.init = true;
  1631. }
  1632. /// <summary>
  1633. /// Signals the object that initialization is complete
  1634. /// </summary>
  1635. public void EndInit()
  1636. {
  1637. this.init = false;
  1638. this.PerformLayout();
  1639. }
  1640. /// <summary>
  1641. /// Gets whether the Table is currently initializing
  1642. /// </summary>
  1643. [Browsable(false)]
  1644. public bool Initializing
  1645. {
  1646. get
  1647. {
  1648. return this.init;
  1649. }
  1650. }
  1651. #endregion
  1652. #region Mouse
  1653. /// <summary>
  1654. /// This member supports the .NET Framework infrastructure and is not
  1655. /// intended to be used directly from your code
  1656. /// </summary>
  1657. public new void ResetMouseEventArgs()
  1658. {
  1659. if (this.trackMouseEvent == null)
  1660. {
  1661. this.trackMouseEvent = new TRACKMOUSEEVENT();
  1662. this.trackMouseEvent.dwFlags = 3;
  1663. this.trackMouseEvent.hwndTrack = base.Handle;
  1664. }
  1665. this.trackMouseEvent.dwHoverTime = this.HoverTime;
  1666. NativeMethods.TrackMouseEvent(this.trackMouseEvent);
  1667. }
  1668. #endregion
  1669. #region Scrolling
  1670. /// <summary>
  1671. /// Updates the scrollbars to reflect any changes made to the Table
  1672. /// </summary>
  1673. public void UpdateScrollBars()
  1674. {
  1675. if (!this.Scrollable || this.ColumnModel == null)
  1676. {
  1677. return;
  1678. }
  1679. // fix: Add width/height check as otherwise minimize
  1680. // causes a crash
  1681. // Portia4ever (kangxj@126.com)
  1682. // 13/09/2005
  1683. // v1.0.1
  1684. if (this.Width == 0 || this.Height == 0)
  1685. {
  1686. return;
  1687. }
  1688. bool hscroll = (this.ColumnModel.VisibleColumnsWidth > this.Width - (this.BorderWidth * 2));
  1689. bool vscroll = this.TotalRowAndHeaderHeight > (this.Height - (this.BorderWidth * 2) - (hscroll ? SystemInformation.HorizontalScrollBarHeight : 0));
  1690. if (vscroll)
  1691. {
  1692. hscroll = (this.ColumnModel.VisibleColumnsWidth > this.Width - (this.BorderWidth * 2) - SystemInformation.VerticalScrollBarWidth);
  1693. }
  1694. if (hscroll)
  1695. {
  1696. Rectangle hscrollBounds = new Rectangle(this.BorderWidth,
  1697. this.Height - this.BorderWidth - SystemInformation.HorizontalScrollBarHeight,
  1698. this.Width - (this.BorderWidth * 2),
  1699. SystemInformation.HorizontalScrollBarHeight);
  1700. if (vscroll)
  1701. {
  1702. hscrollBounds.Width -= SystemInformation.VerticalScrollBarWidth;
  1703. }
  1704. this.hScrollBar.Visible = true;
  1705. this.hScrollBar.Bounds = hscrollBounds;
  1706. this.hScrollBar.Minimum = 0;
  1707. this.hScrollBar.Maximum = this.ColumnModel.VisibleColumnsWidth;
  1708. this.hScrollBar.SmallChange = Column.MinimumWidth;
  1709. this.hScrollBar.LargeChange = hscrollBounds.Width - 1;
  1710. if (this.hScrollBar.Value > this.hScrollBar.Maximum - this.hScrollBar.LargeChange)
  1711. {
  1712. this.hScrollBar.Value = this.hScrollBar.Maximum - this.hScrollBar.LargeChange;
  1713. }
  1714. }
  1715. else
  1716. {
  1717. this.hScrollBar.Visible = false;
  1718. this.hScrollBar.Value = 0;
  1719. }
  1720. if (vscroll)
  1721. {
  1722. Rectangle vscrollBounds = new Rectangle(this.Width - this.BorderWidth - SystemInformation.VerticalScrollBarWidth,
  1723. this.BorderWidth,
  1724. SystemInformation.VerticalScrollBarWidth,
  1725. this.Height - (this.BorderWidth * 2));
  1726. if (hscroll)
  1727. {
  1728. vscrollBounds.Height -= SystemInformation.HorizontalScrollBarHeight;
  1729. }
  1730. this.vScrollBar.Visible = true;
  1731. this.vScrollBar.Bounds = vscrollBounds;
  1732. this.vScrollBar.Minimum = 0;
  1733. this.vScrollBar.Maximum = (this.RowCount > this.VisibleRowCount ? this.RowCount - 1 : this.VisibleRowCount);
  1734. this.vScrollBar.SmallChange = 1;
  1735. this.vScrollBar.LargeChange = this.VisibleRowCount - 1;
  1736. if (this.vScrollBar.Value > this.vScrollBar.Maximum - this.vScrollBar.LargeChange)
  1737. {
  1738. this.vScrollBar.Value = this.vScrollBar.Maximum - this.vScrollBar.LargeChange;
  1739. }
  1740. }
  1741. else
  1742. {
  1743. this.vScrollBar.Visible = false;
  1744. this.vScrollBar.Value = 0;
  1745. }
  1746. }
  1747. /// <summary>
  1748. /// Scrolls the contents of the Table horizontally to the specified value
  1749. /// </summary>
  1750. /// <param name="value">The value to scroll to</param>
  1751. protected void HorizontalScroll(int value)
  1752. {
  1753. int scrollVal = this.hScrollBar.Value - value;
  1754. if (scrollVal != 0)
  1755. {
  1756. RECT scrollRect = RECT.FromRectangle(this.PseudoClientRect);
  1757. Rectangle invalidateRect = scrollRect.ToRectangle();
  1758. NativeMethods.ScrollWindow(this.Handle, scrollVal, 0, ref scrollRect, ref scrollRect);
  1759. if (scrollVal < 0)
  1760. {
  1761. invalidateRect.X = invalidateRect.Right + scrollVal;
  1762. }
  1763. invalidateRect.Width = Math.Abs(scrollVal);
  1764. this.Invalidate(invalidateRect, false);
  1765. if (this.VScroll)
  1766. {
  1767. this.Invalidate(new Rectangle(this.Width - this.BorderWidth - SystemInformation.VerticalScrollBarWidth,
  1768. this.Height - this.BorderWidth - SystemInformation.HorizontalScrollBarHeight,
  1769. SystemInformation.VerticalScrollBarWidth,
  1770. SystemInformation.HorizontalScrollBarHeight),
  1771. false);
  1772. }
  1773. }
  1774. }
  1775. /// <summary>
  1776. /// Scrolls the contents of the Table vertically to the specified value
  1777. /// </summary>
  1778. /// <param name="value">The value to scroll to</param>
  1779. protected void VerticalScroll(int value)
  1780. {
  1781. int scrollVal = this.vScrollBar.Value - value;
  1782. if (scrollVal != 0)
  1783. {
  1784. RECT scrollRect = RECT.FromRectangle(this.CellDataRect);
  1785. Rectangle invalidateRect = scrollRect.ToRectangle();
  1786. scrollVal *= this.RowHeight;
  1787. NativeMethods.ScrollWindow(this.Handle, 0, scrollVal, ref scrollRect, ref scrollRect);
  1788. if (scrollVal < 0)
  1789. {
  1790. invalidateRect.Y = invalidateRect.Bottom + scrollVal;
  1791. }
  1792. invalidateRect.Height = Math.Abs(scrollVal);
  1793. this.Invalidate(invalidateRect, false);
  1794. if (this.HScroll)
  1795. {
  1796. this.Invalidate(new Rectangle(this.Width - this.BorderWidth - SystemInformation.VerticalScrollBarWidth,
  1797. this.Height - this.BorderWidth - SystemInformation.HorizontalScrollBarHeight,
  1798. SystemInformation.VerticalScrollBarWidth,
  1799. SystemInformation.HorizontalScrollBarHeight),
  1800. false);
  1801. }
  1802. }
  1803. }
  1804. /// <summary>
  1805. /// Ensures that the Cell at the specified row and column is visible
  1806. /// within the Table, scrolling the contents of the Table if necessary
  1807. /// </summary>
  1808. /// <param name="row">The zero-based index of the row to scroll into view</param>
  1809. /// <param name="column">The zero-based index of the column to scroll into view</param>
  1810. /// <returns>true if the Table scrolled to the Cell at the specified row
  1811. /// and column, false otherwise</returns>
  1812. public bool EnsureVisible(int row, int column)
  1813. {
  1814. if (!this.Scrollable || (!this.HScroll && !this.VScroll) || row == -1)
  1815. {
  1816. return false;
  1817. }
  1818. if (column == -1)
  1819. {
  1820. if (this.FocusedCell.Column != -1)
  1821. {
  1822. column = this.FocusedCell.Column;
  1823. }
  1824. else
  1825. {
  1826. column = 0;
  1827. }
  1828. }
  1829. int hscrollVal = this.hScrollBar.Value;
  1830. int vscrollVal = this.vScrollBar.Value;
  1831. bool moved = false;
  1832. if (this.HScroll)
  1833. {
  1834. if (column < 0)
  1835. {
  1836. column = 0;
  1837. }
  1838. else if (column >= this.ColumnCount)
  1839. {
  1840. column = this.ColumnCount - 1;
  1841. }
  1842. if (this.ColumnModel.Columns[column].Visible)
  1843. {
  1844. if (this.ColumnModel.Columns[column].Left < this.hScrollBar.Value)
  1845. {
  1846. hscrollVal = this.ColumnModel.Columns[column].Left;
  1847. }
  1848. else if (this.ColumnModel.Columns[column].Right > this.hScrollBar.Value + this.CellDataRect.Width)
  1849. {
  1850. hscrollVal = this.ColumnModel.Columns[column].Right - this.CellDataRect.Width;
  1851. }
  1852. if (hscrollVal > this.hScrollBar.Maximum - this.hScrollBar.LargeChange)
  1853. {
  1854. hscrollVal = this.hScrollBar.Maximum - this.hScrollBar.LargeChange;
  1855. }
  1856. }
  1857. }
  1858. if (this.VScroll)
  1859. {
  1860. if (row < 0)
  1861. {
  1862. vscrollVal = 0;
  1863. }
  1864. else if (row >= this.RowCount)
  1865. {
  1866. vscrollVal = this.RowCount - 1;
  1867. }
  1868. else
  1869. {
  1870. if (row < vscrollVal)
  1871. {
  1872. vscrollVal = row;
  1873. }
  1874. else if (row > vscrollVal + this.vScrollBar.LargeChange)
  1875. {
  1876. vscrollVal += row - (vscrollVal + this.vScrollBar.LargeChange);
  1877. }
  1878. }
  1879. if (vscrollVal > this.vScrollBar.Maximum - this.vScrollBar.LargeChange)
  1880. {
  1881. vscrollVal = (this.vScrollBar.Maximum - this.vScrollBar.LargeChange) + 1;
  1882. }
  1883. }
  1884. if (this.RowRect(row).Bottom > this.CellDataRect.Bottom)
  1885. {
  1886. vscrollVal++;
  1887. }
  1888. moved = (this.hScrollBar.Value != hscrollVal || this.vScrollBar.Value != vscrollVal);
  1889. if (moved)
  1890. {
  1891. this.hScrollBar.Value = hscrollVal;
  1892. this.vScrollBar.Value = vscrollVal;
  1893. this.Invalidate(this.PseudoClientRect);
  1894. }
  1895. return moved;
  1896. }
  1897. /// <summary>
  1898. /// Ensures that the Cell at the specified CellPos is visible within
  1899. /// the Table, scrolling the contents of the Table if necessary
  1900. /// </summary>
  1901. /// <param name="cellPos">A CellPos that contains the zero-based index
  1902. /// of the row and column to scroll into view</param>
  1903. /// <returns></returns>
  1904. public bool EnsureVisible(CellPos cellPos)
  1905. {
  1906. return this.EnsureVisible(cellPos.Row, cellPos.Column);
  1907. }
  1908. /// <summary>
  1909. /// Gets the index of the first visible Column currently displayed in the Table
  1910. /// </summary>
  1911. [Browsable(false)]
  1912. public int FirstVisibleColumn
  1913. {
  1914. get
  1915. {
  1916. if (this.ColumnModel == null || this.ColumnModel.VisibleColumnCount == 0)
  1917. {
  1918. return -1;
  1919. }
  1920. return this.ColumnModel.ColumnIndexAtX(this.hScrollBar.Value);
  1921. }
  1922. }
  1923. /// <summary>
  1924. /// Gets the index of the last visible Column currently displayed in the Table
  1925. /// </summary>
  1926. [Browsable(false)]
  1927. public int LastVisibleColumn
  1928. {
  1929. get
  1930. {
  1931. if (this.ColumnModel == null || this.ColumnModel.VisibleColumnCount == 0)
  1932. {
  1933. return -1;
  1934. }
  1935. int rightEdge = this.hScrollBar.Value + this.PseudoClientRect.Right;
  1936. if (this.VScroll)
  1937. {
  1938. rightEdge -= this.vScrollBar.Width;
  1939. }
  1940. int col = this.ColumnModel.ColumnIndexAtX(rightEdge);
  1941. if (col == -1)
  1942. {
  1943. return this.ColumnModel.PreviousVisibleColumn(this.ColumnModel.Columns.Count);
  1944. }
  1945. else if (!this.ColumnModel.Columns[col].Visible)
  1946. {
  1947. return this.ColumnModel.PreviousVisibleColumn(col);
  1948. }
  1949. return col;
  1950. }
  1951. }
  1952. #endregion
  1953. #region Sorting
  1954. /// <summary>
  1955. /// Sorts the last sorted column opposite to its current sort order,
  1956. /// or sorts the currently focused column in ascending order if no
  1957. /// columns have been sorted
  1958. /// </summary>
  1959. public void Sort()
  1960. {
  1961. this.Sort(true);
  1962. }
  1963. /// <summary>
  1964. /// Sorts the last sorted column opposite to its current sort order,
  1965. /// or sorts the currently focused column in ascending order if no
  1966. /// columns have been sorted
  1967. /// </summary>
  1968. /// <param name="stable">Specifies whether a stable sorting method
  1969. /// should be used to sort the column</param>
  1970. public void Sort(bool stable)
  1971. {
  1972. // don't allow sorting if we're being used as a
  1973. // preview table in a ColumnModel editor
  1974. if (this.Preview)
  1975. {
  1976. return;
  1977. }
  1978. // if we don't have a sorted column already, check if
  1979. // we can use the column of the cell that has focus
  1980. if (!this.IsValidColumn(this.lastSortedColumn))
  1981. {
  1982. if (this.IsValidColumn(this.focusedCell.Column))
  1983. {
  1984. this.lastSortedColumn = this.focusedCell.Column;
  1985. }
  1986. }
  1987. // make sure the last sorted column exists
  1988. if (this.IsValidColumn(this.lastSortedColumn))
  1989. {
  1990. // don't bother if the column won't let us sort
  1991. if (!this.ColumnModel.Columns[this.lastSortedColumn].Sortable)
  1992. {
  1993. return;
  1994. }
  1995. // work out which direction we should sort
  1996. SortOrder newOrder = SortOrder.Ascending;
  1997. Column column = this.ColumnModel.Columns[this.lastSortedColumn];
  1998. if (column.SortOrder == SortOrder.Ascending)
  1999. {
  2000. newOrder = SortOrder.Descending;
  2001. }
  2002. this.Sort(this.lastSortedColumn, column, newOrder, stable);
  2003. }
  2004. }
  2005. /// <summary>
  2006. /// Sorts the specified column opposite to its current sort order,
  2007. /// or in ascending order if the column is not sorted
  2008. /// </summary>
  2009. /// <param name="column">The index of the column to sort</param>
  2010. public void Sort(int column)
  2011. {
  2012. this.Sort(column, true);
  2013. }
  2014. /// <summary>
  2015. /// Sorts the specified column opposite to its current sort order,
  2016. /// or in ascending order if the column is not sorted
  2017. /// </summary>
  2018. /// <param name="column">The index of the column to sort</param>
  2019. /// <param name="stable">Specifies whether a stable sorting method
  2020. /// should be used to sort the column</param>
  2021. public void Sort(int column, bool stable)
  2022. {
  2023. // don't allow sorting if we're being used as a
  2024. // preview table in a ColumnModel editor
  2025. if (this.Preview)
  2026. {
  2027. return;
  2028. }
  2029. // make sure the column exists
  2030. if (this.IsValidColumn(column))
  2031. {
  2032. // don't bother if the column won't let us sort
  2033. if (!this.ColumnModel.Columns[column].Sortable)
  2034. {
  2035. return;
  2036. }
  2037. // if we already have a different sorted column, set
  2038. // its sort order to none
  2039. if (column != this.lastSortedColumn)
  2040. {
  2041. if (this.IsValidColumn(this.lastSortedColumn))
  2042. {
  2043. this.ColumnModel.Columns[this.lastSortedColumn].InternalSortOrder = SortOrder.None;
  2044. }
  2045. }
  2046. this.lastSortedColumn = column;
  2047. // work out which direction we should sort
  2048. SortOrder newOrder = SortOrder.Ascending;
  2049. Column col = this.ColumnModel.Columns[column];
  2050. if (col.SortOrder == SortOrder.Ascending)
  2051. {
  2052. newOrder = SortOrder.Descending;
  2053. }
  2054. this.Sort(column, col, newOrder, stable);
  2055. }
  2056. }
  2057. /// <summary>
  2058. /// Sorts the specified column in the specified sort direction
  2059. /// </summary>
  2060. /// <param name="column">The index of the column to sort</param>
  2061. /// <param name="sortOrder">The direction the column is to be sorted</param>
  2062. public void Sort(int column, SortOrder sortOrder)
  2063. {
  2064. this.Sort(column, sortOrder, true);
  2065. }
  2066. /// <summary>
  2067. /// Sorts the specified column in the specified sort direction
  2068. /// </summary>
  2069. /// <param name="column">The index of the column to sort</param>
  2070. /// <param name="sortOrder">The direction the column is to be sorted</param>
  2071. /// <param name="stable">Specifies whether a stable sorting method
  2072. /// should be used to sort the column</param>
  2073. public void Sort(int column, SortOrder sortOrder, bool stable)
  2074. {
  2075. // don't allow sorting if we're being used as a
  2076. // preview table in a ColumnModel editor
  2077. if (this.Preview)
  2078. {
  2079. return;
  2080. }
  2081. // make sure the column exists
  2082. if (this.IsValidColumn(column))
  2083. {
  2084. // don't bother if the column won't let us sort
  2085. if (!this.ColumnModel.Columns[column].Sortable)
  2086. {
  2087. return;
  2088. }
  2089. // if we already have a different sorted column, set
  2090. // its sort order to none
  2091. if (column != this.lastSortedColumn)
  2092. {
  2093. if (this.IsValidColumn(this.lastSortedColumn))
  2094. {
  2095. this.ColumnModel.Columns[this.lastSortedColumn].InternalSortOrder = SortOrder.None;
  2096. }
  2097. }
  2098. this.lastSortedColumn = column;
  2099. this.Sort(column, this.ColumnModel.Columns[column], sortOrder, stable);
  2100. }
  2101. }
  2102. /// <summary>
  2103. /// Sorts the specified column in the specified sort direction
  2104. /// </summary>
  2105. /// <param name="index">The index of the column to sort</param>
  2106. /// <param name="column">The column to sort</param>
  2107. /// <param name="sortOrder">The direction the column is to be sorted</param>
  2108. /// <param name="stable">Specifies whether a stable sorting method
  2109. /// should be used to sort the column</param>
  2110. private void Sort(int index, Column column, SortOrder sortOrder, bool stable)
  2111. {
  2112. // make sure a null comparer type doesn't sneak past
  2113. ComparerBase comparer = null;
  2114. if (column.Comparer != null)
  2115. {
  2116. comparer = (ComparerBase) Activator.CreateInstance(column.Comparer, new object[] {this.TableModel, index, sortOrder});
  2117. }
  2118. else if (column.DefaultComparerType != null)
  2119. {
  2120. comparer = (ComparerBase) Activator.CreateInstance(column.DefaultComparerType, new object[] {this.TableModel, index, sortOrder});
  2121. }
  2122. else
  2123. {
  2124. return;
  2125. }
  2126. column.InternalSortOrder = sortOrder;
  2127. // create the comparer
  2128. SorterBase sorter = null;
  2129. // work out which sort method to use.
  2130. // - InsertionSort/MergeSort are stable sorts,
  2131. // whereas ShellSort/HeapSort are unstable
  2132. // - InsertionSort/ShellSort are faster than
  2133. // MergeSort/HeapSort on small lists and slower
  2134. // on large lists
  2135. // so we choose based on the size of the list and
  2136. // whether the user wants a stable sort
  2137. if (this.TableModel.Rows.Count < 1000)
  2138. {
  2139. if (stable)
  2140. {
  2141. sorter = new InsertionSorter(this.TableModel, index, comparer, sortOrder);
  2142. }
  2143. else
  2144. {
  2145. sorter = new ShellSorter(this.TableModel, index, comparer, sortOrder);
  2146. }
  2147. }
  2148. else
  2149. {
  2150. if (stable)
  2151. {
  2152. sorter = new MergeSorter(this.TableModel, index, comparer, sortOrder);
  2153. }
  2154. else
  2155. {
  2156. sorter = new HeapSorter(this.TableModel, index, comparer, sortOrder);
  2157. }
  2158. }
  2159. // don't let the table redraw
  2160. this.BeginUpdate();
  2161. this.OnBeginSort(new ColumnEventArgs(column, index, ColumnEventType.Sorting, null));
  2162. sorter.Sort();
  2163. this.OnEndSort(new ColumnEventArgs(column, index, ColumnEventType.Sorting, null));
  2164. // redraw any changes
  2165. this.EndUpdate();
  2166. }
  2167. /// <summary>
  2168. /// Returns whether a Column exists at the specified index in the
  2169. /// Table's ColumnModel
  2170. /// </summary>
  2171. /// <param name="column">The index of the column to check</param>
  2172. /// <returns>True if a Column exists at the specified index in the
  2173. /// Table's ColumnModel, false otherwise</returns>
  2174. public bool IsValidColumn(int column)
  2175. {
  2176. if (this.ColumnModel == null)
  2177. {
  2178. return false;
  2179. }
  2180. return (column >= 0 && column < this.ColumnModel.Columns.Count);
  2181. }
  2182. #endregion
  2183. #endregion
  2184. #region Properties
  2185. #region Borders
  2186. /// <summary>
  2187. /// Gets or sets the border style for the Table
  2188. /// </summary>
  2189. [Category("Appearance"),
  2190. DefaultValue(BorderStyle.Fixed3D),
  2191. Description("Indicates the border style for the Table")]
  2192. public BorderStyle BorderStyle
  2193. {
  2194. get
  2195. {
  2196. return this.borderStyle;
  2197. }
  2198. set
  2199. {
  2200. if (!Enum.IsDefined(typeof(BorderStyle), value))
  2201. {
  2202. throw new InvalidEnumArgumentException("value", (int) value, typeof(BorderStyle));
  2203. }
  2204. if (borderStyle != value)
  2205. {
  2206. this.borderStyle = value;
  2207. this.Invalidate(true);
  2208. }
  2209. }
  2210. }
  2211. /// <summary>
  2212. /// Gets the width of the Tables border
  2213. /// </summary>
  2214. protected int BorderWidth
  2215. {
  2216. get
  2217. {
  2218. if (this.BorderStyle == BorderStyle.Fixed3D)
  2219. {
  2220. return SystemInformation.Border3DSize.Width;
  2221. }
  2222. else if (this.BorderStyle == BorderStyle.FixedSingle)
  2223. {
  2224. return 1;
  2225. }
  2226. return 0;
  2227. }
  2228. }
  2229. #endregion
  2230. #region Cells
  2231. /// <summary>
  2232. /// Gets the last known cell position that the mouse was over
  2233. /// </summary>
  2234. [Browsable(false)]
  2235. public CellPos LastMouseCell
  2236. {
  2237. get
  2238. {
  2239. return this.lastMouseCell;
  2240. }
  2241. }
  2242. /// <summary>
  2243. /// Gets the last known cell position that the mouse's left
  2244. /// button was pressed in
  2245. /// </summary>
  2246. [Browsable(false)]
  2247. public CellPos LastMouseDownCell
  2248. {
  2249. get
  2250. {
  2251. return this.lastMouseDownCell;
  2252. }
  2253. }
  2254. /// <summary>
  2255. /// Gets or sets the position of the Cell that currently has focus
  2256. /// </summary>
  2257. [Browsable(false),
  2258. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2259. public CellPos FocusedCell
  2260. {
  2261. get
  2262. {
  2263. return this.focusedCell;
  2264. }
  2265. set
  2266. {
  2267. if (!this.IsValidCell(value))
  2268. {
  2269. return;
  2270. }
  2271. if (!this.TableModel[value].Enabled)
  2272. {
  2273. return;
  2274. }
  2275. if (this.focusedCell != value)
  2276. {
  2277. if (!this.focusedCell.IsEmpty)
  2278. {
  2279. this.RaiseCellLostFocus(this.focusedCell);
  2280. }
  2281. this.focusedCell = value;
  2282. if (!value.IsEmpty)
  2283. {
  2284. this.EnsureVisible(value);
  2285. this.RaiseCellGotFocus(value);
  2286. }
  2287. }
  2288. }
  2289. }
  2290. /// <summary>
  2291. /// Gets or sets the amount of time (in milliseconds) that that the
  2292. /// mouse pointer must hover over a Cell or Column Header before
  2293. /// a MouseHover event is raised
  2294. /// </summary>
  2295. [Category("Behavior"),
  2296. DefaultValue(1000),
  2297. Description("The amount of time (in milliseconds) that that the mouse pointer must hover over a Cell or Column Header before a MouseHover event is raised")]
  2298. public int HoverTime
  2299. {
  2300. get
  2301. {
  2302. return this.hoverTime;
  2303. }
  2304. set
  2305. {
  2306. if (value < 100)
  2307. {
  2308. throw new ArgumentException("HoverTime cannot be less than 100", "value");
  2309. }
  2310. if (this.hoverTime != value)
  2311. {
  2312. this.hoverTime = value;
  2313. this.ResetMouseEventArgs();
  2314. }
  2315. }
  2316. }
  2317. #endregion
  2318. #region ClientRectangle
  2319. /// <summary>
  2320. /// Gets the rectangle that represents the "client area" of the control.
  2321. /// (The rectangle excludes the borders and scrollbars)
  2322. /// </summary>
  2323. [Browsable(false)]
  2324. public Rectangle PseudoClientRect
  2325. {
  2326. get
  2327. {
  2328. Rectangle clientRect = this.InternalBorderRect;
  2329. if (this.HScroll)
  2330. {
  2331. clientRect.Height -= SystemInformation.HorizontalScrollBarHeight;
  2332. }
  2333. if (this.VScroll)
  2334. {
  2335. clientRect.Width -= SystemInformation.VerticalScrollBarWidth;
  2336. }
  2337. return clientRect;
  2338. }
  2339. }
  2340. /// <summary>
  2341. /// Gets the rectangle that represents the "cell data area" of the control.
  2342. /// (The rectangle excludes the borders, column headers and scrollbars)
  2343. /// </summary>
  2344. [Browsable(false)]
  2345. public Rectangle CellDataRect
  2346. {
  2347. get
  2348. {
  2349. Rectangle clientRect = this.PseudoClientRect;
  2350. if (this.HeaderStyle != ColumnHeaderStyle.None && this.ColumnCount > 0)
  2351. {
  2352. clientRect.Y += this.HeaderHeight;
  2353. clientRect.Height -= this.HeaderHeight;
  2354. }
  2355. return clientRect;
  2356. }
  2357. }
  2358. /// <summary>
  2359. ///
  2360. /// </summary>
  2361. private Rectangle InternalBorderRect
  2362. {
  2363. get
  2364. {
  2365. return new Rectangle(this.BorderWidth,
  2366. this.BorderWidth,
  2367. this.Width - (this.BorderWidth * 2),
  2368. this.Height - (this.BorderWidth * 2));
  2369. }
  2370. }
  2371. #endregion
  2372. #region ColumnModel
  2373. /// <summary>
  2374. /// Gets or sets the ColumnModel that contains all the Columns
  2375. /// displayed in the Table
  2376. /// </summary>
  2377. [Category("Columns"),
  2378. DefaultValue(null),
  2379. Description("Specifies the ColumnModel that contains all the Columns displayed in the Table")]
  2380. public ColumnModel ColumnModel
  2381. {
  2382. get
  2383. {
  2384. return this.columnModel;
  2385. }
  2386. set
  2387. {
  2388. if (this.columnModel != value)
  2389. {
  2390. if (this.columnModel != null && this.columnModel.Table == this)
  2391. {
  2392. this.columnModel.InternalTable = null;
  2393. }
  2394. this.columnModel = value;
  2395. if (value != null)
  2396. {
  2397. value.InternalTable = this;
  2398. }
  2399. this.OnColumnModelChanged(EventArgs.Empty);
  2400. }
  2401. }
  2402. }
  2403. /// <summary>
  2404. /// Gets or sets whether the Table allows users to resize Column widths
  2405. /// </summary>
  2406. [Category("Columns"),
  2407. DefaultValue(true),
  2408. Description("Specifies whether the Table allows users to resize Column widths")]
  2409. public bool ColumnResizing
  2410. {
  2411. get
  2412. {
  2413. return this.columnResizing;
  2414. }
  2415. set
  2416. {
  2417. if (this.columnResizing != value)
  2418. {
  2419. this.columnResizing = value;
  2420. }
  2421. }
  2422. }
  2423. /// <summary>
  2424. /// Returns the number of Columns in the Table
  2425. /// </summary>
  2426. [Browsable(false),
  2427. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2428. public int ColumnCount
  2429. {
  2430. get
  2431. {
  2432. if (this.ColumnModel == null)
  2433. {
  2434. return -1;
  2435. }
  2436. return this.ColumnModel.Columns.Count;
  2437. }
  2438. }
  2439. /// <summary>
  2440. /// Returns the index of the currently sorted Column
  2441. /// </summary>
  2442. [Browsable(false),
  2443. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2444. public int SortingColumn
  2445. {
  2446. get
  2447. {
  2448. return this.lastSortedColumn;
  2449. }
  2450. }
  2451. /// <summary>
  2452. /// Gets or sets the background Color for the currently sorted column
  2453. /// </summary>
  2454. [Category("Columns"),
  2455. Description("The background Color for a sorted Column")]
  2456. public Color SortedColumnBackColor
  2457. {
  2458. get
  2459. {
  2460. return this.sortedColumnBackColor;
  2461. }
  2462. set
  2463. {
  2464. if (this.sortedColumnBackColor != value)
  2465. {
  2466. this.sortedColumnBackColor = value;
  2467. if (this.IsValidColumn(this.lastSortedColumn))
  2468. {
  2469. Rectangle columnRect = this.ColumnRect(this.lastSortedColumn);
  2470. if (this.PseudoClientRect.IntersectsWith(columnRect))
  2471. {
  2472. this.Invalidate(Rectangle.Intersect(this.PseudoClientRect, columnRect));
  2473. }
  2474. }
  2475. }
  2476. }
  2477. }
  2478. /// <summary>
  2479. /// Specifies whether the Table's SortedColumnBackColor property
  2480. /// should be serialized at design time
  2481. /// </summary>
  2482. /// <returns>True if the SortedColumnBackColor property should be
  2483. /// serialized, False otherwise</returns>
  2484. private bool ShouldSerializeSortedColumnBackColor()
  2485. {
  2486. return this.sortedColumnBackColor != Color.WhiteSmoke;
  2487. }
  2488. #endregion
  2489. #region DisplayRectangle
  2490. /// <summary>
  2491. /// Gets the rectangle that represents the display area of the Table
  2492. /// </summary>
  2493. [Browsable(false),
  2494. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2495. public override Rectangle DisplayRectangle
  2496. {
  2497. get
  2498. {
  2499. Rectangle displayRect = this.CellDataRect;
  2500. if (!this.init)
  2501. {
  2502. displayRect.X -= this.hScrollBar.Value;
  2503. displayRect.Y -= this.vScrollBar.Value;
  2504. }
  2505. if (this.ColumnModel == null)
  2506. {
  2507. return displayRect;
  2508. }
  2509. if (this.ColumnModel.TotalColumnWidth <= this.CellDataRect.Width)
  2510. {
  2511. displayRect.Width = this.CellDataRect.Width;
  2512. }
  2513. else
  2514. {
  2515. displayRect.Width = this.ColumnModel.VisibleColumnsWidth;
  2516. }
  2517. if (this.TotalRowHeight <= this.CellDataRect.Height)
  2518. {
  2519. displayRect.Height = this.CellDataRect.Height;
  2520. }
  2521. else
  2522. {
  2523. displayRect.Height = this.TotalRowHeight;
  2524. }
  2525. return displayRect;
  2526. }
  2527. }
  2528. #endregion
  2529. #region Editing
  2530. /// <summary>
  2531. /// Gets whether the Table is currently editing a Cell
  2532. /// </summary>
  2533. [Browsable(false)]
  2534. public bool IsEditing
  2535. {
  2536. get
  2537. {
  2538. return !this.EditingCell.IsEmpty;
  2539. }
  2540. }
  2541. /// <summary>
  2542. /// Gets a CellPos that specifies the position of the Cell that
  2543. /// is currently being edited
  2544. /// </summary>
  2545. [Browsable(false)]
  2546. public CellPos EditingCell
  2547. {
  2548. get
  2549. {
  2550. return this.editingCell;
  2551. }
  2552. }
  2553. /// <summary>
  2554. /// Gets the ICellEditor that is currently being used to edit a Cell
  2555. /// </summary>
  2556. [Browsable(false)]
  2557. public ICellEditor EditingCellEditor
  2558. {
  2559. get
  2560. {
  2561. return this.curentCellEditor;
  2562. }
  2563. }
  2564. /// <summary>
  2565. /// Gets or sets the action that causes editing to be initiated
  2566. /// </summary>
  2567. [Category("Editing"),
  2568. DefaultValue(EditStartAction.DoubleClick),
  2569. Description("The action that causes editing to be initiated")]
  2570. public EditStartAction EditStartAction
  2571. {
  2572. get
  2573. {
  2574. return this.editStartAction;
  2575. }
  2576. set
  2577. {
  2578. if (!Enum.IsDefined(typeof(EditStartAction), value))
  2579. {
  2580. throw new InvalidEnumArgumentException("value", (int) value, typeof(EditStartAction));
  2581. }
  2582. if (this.editStartAction != value)
  2583. {
  2584. this.editStartAction = value;
  2585. }
  2586. }
  2587. }
  2588. /// <summary>
  2589. /// Gets or sets the custom key used to initiate Cell editing
  2590. /// </summary>
  2591. [Category("Editing"),
  2592. DefaultValue(Keys.F5),
  2593. Description("The custom key used to initiate Cell editing")]
  2594. public Keys CustomEditKey
  2595. {
  2596. get
  2597. {
  2598. return this.customEditKey;
  2599. }
  2600. set
  2601. {
  2602. if (this.IsReservedKey(value))
  2603. {
  2604. throw new ArgumentException("CustomEditKey cannot be one of the Table's reserved keys " +
  2605. "(Up arrow, Down arrow, Left arrow, Right arrow, PageUp, " +
  2606. "PageDown, Home, End, Tab)", "value");
  2607. }
  2608. if (this.customEditKey != value)
  2609. {
  2610. this.customEditKey = value;
  2611. }
  2612. }
  2613. }
  2614. /*/// <summary>
  2615. /// Gets or sets whether pressing the Tab key during editing moves
  2616. /// the editor to the next editable Cell
  2617. /// </summary>
  2618. [Category("Editing"),
  2619. DefaultValue(true),
  2620. Description("")]
  2621. public bool TabMovesEditor
  2622. {
  2623. get
  2624. {
  2625. return this.tabMovesEditor;
  2626. }
  2627. set
  2628. {
  2629. this.tabMovesEditor = value;
  2630. }
  2631. }*/
  2632. #endregion
  2633. #region Grid
  2634. /// <summary>
  2635. /// Gets or sets how grid lines are displayed around rows and columns
  2636. /// </summary>
  2637. [Category("Grid"),
  2638. DefaultValue(GridLines.None),
  2639. Description("Determines how grid lines are displayed around rows and columns")]
  2640. public GridLines GridLines
  2641. {
  2642. get
  2643. {
  2644. return this.gridLines;
  2645. }
  2646. set
  2647. {
  2648. if (!Enum.IsDefined(typeof(GridLines), value))
  2649. {
  2650. throw new InvalidEnumArgumentException("value", (int) value, typeof(GridLines));
  2651. }
  2652. if (this.gridLines != value)
  2653. {
  2654. this.gridLines = value;
  2655. this.Invalidate(this.PseudoClientRect, false);
  2656. }
  2657. }
  2658. }
  2659. /// <summary>
  2660. /// Gets or sets the style of the lines used to draw the grid
  2661. /// </summary>
  2662. [Category("Grid"),
  2663. DefaultValue(GridLineStyle.Solid),
  2664. Description("The style of the lines used to draw the grid")]
  2665. public GridLineStyle GridLineStyle
  2666. {
  2667. get
  2668. {
  2669. return this.gridLineStyle;
  2670. }
  2671. set
  2672. {
  2673. if (!Enum.IsDefined(typeof(GridLineStyle), value))
  2674. {
  2675. throw new InvalidEnumArgumentException("value", (int) value, typeof(GridLineStyle));
  2676. }
  2677. if (this.gridLineStyle != value)
  2678. {
  2679. this.gridLineStyle = value;
  2680. if (this.GridLines != GridLines.None)
  2681. {
  2682. this.Invalidate(this.PseudoClientRect, false);
  2683. }
  2684. }
  2685. }
  2686. }
  2687. /// <summary>
  2688. /// Gets or sets the Color of the grid lines
  2689. /// </summary>
  2690. [Category("Grid"),
  2691. Description("The color of the grid lines")]
  2692. public Color GridColor
  2693. {
  2694. get
  2695. {
  2696. return this.gridColor;
  2697. }
  2698. set
  2699. {
  2700. if (this.gridColor != value)
  2701. {
  2702. this.gridColor = value;
  2703. if (this.GridLines != GridLines.None)
  2704. {
  2705. this.Invalidate(this.PseudoClientRect, false);
  2706. }
  2707. }
  2708. }
  2709. }
  2710. /// <summary>
  2711. /// Specifies whether the Table's GridColor property
  2712. /// should be serialized at design time
  2713. /// </summary>
  2714. /// <returns>True if the GridColor property should be
  2715. /// serialized, False otherwise</returns>
  2716. private bool ShouldSerializeGridColor()
  2717. {
  2718. return (this.GridColor != SystemColors.Control);
  2719. }
  2720. /// <summary>
  2721. ///
  2722. /// </summary>
  2723. public override Color BackColor
  2724. {
  2725. get
  2726. {
  2727. return base.BackColor;
  2728. }
  2729. set
  2730. {
  2731. base.BackColor = value;
  2732. }
  2733. }
  2734. /// <summary>
  2735. /// Specifies whether the Table's BackColor property
  2736. /// should be serialized at design time
  2737. /// </summary>
  2738. /// <returns>True if the BackColor property should be
  2739. /// serialized, False otherwise</returns>
  2740. private bool ShouldSerializeBackColor()
  2741. {
  2742. return (this.BackColor != Color.White);
  2743. }
  2744. #endregion
  2745. #region Header
  2746. /// <summary>
  2747. /// Gets or sets the column header style
  2748. /// </summary>
  2749. [Category("Columns"),
  2750. DefaultValue(ColumnHeaderStyle.Clickable),
  2751. Description("The style of the column headers")]
  2752. public ColumnHeaderStyle HeaderStyle
  2753. {
  2754. get
  2755. {
  2756. return this.headerStyle;
  2757. }
  2758. set
  2759. {
  2760. if (!Enum.IsDefined(typeof(ColumnHeaderStyle), value))
  2761. {
  2762. throw new InvalidEnumArgumentException("value", (int) value, typeof(ColumnHeaderStyle));
  2763. }
  2764. if (this.headerStyle != value)
  2765. {
  2766. this.headerStyle = value;
  2767. this.pressedColumn = -1;
  2768. this.hotColumn = -1;
  2769. this.Invalidate();
  2770. }
  2771. }
  2772. }
  2773. /// <summary>
  2774. /// Gets the height of the column headers
  2775. /// </summary>
  2776. [Browsable(false)]
  2777. public int HeaderHeight
  2778. {
  2779. get
  2780. {
  2781. if (this.ColumnModel == null || this.HeaderStyle == ColumnHeaderStyle.None)
  2782. {
  2783. return 0;
  2784. }
  2785. return this.ColumnModel.HeaderHeight;
  2786. }
  2787. }
  2788. /// <summary>
  2789. /// Gets a Rectangle that specifies the size and location of
  2790. /// the Table's column header area
  2791. /// </summary>
  2792. [Browsable(false),
  2793. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2794. public Rectangle HeaderRectangle
  2795. {
  2796. get
  2797. {
  2798. return new Rectangle(this.BorderWidth, this.BorderWidth, this.PseudoClientRect.Width, this.HeaderHeight);
  2799. }
  2800. }
  2801. /// <summary>
  2802. /// Gets or sets the font used to draw the text in the column headers
  2803. /// </summary>
  2804. [Category("Columns"),
  2805. Description("The font used to draw the text in the column headers")]
  2806. public Font HeaderFont
  2807. {
  2808. get
  2809. {
  2810. return this.headerFont;
  2811. }
  2812. set
  2813. {
  2814. if (value == null)
  2815. {
  2816. throw new ArgumentNullException("HeaderFont cannot be null");
  2817. }
  2818. if (this.headerFont != value)
  2819. {
  2820. this.headerFont = value;
  2821. this.HeaderRenderer.Font = value;
  2822. this.Invalidate(this.HeaderRectangle, false);
  2823. }
  2824. }
  2825. }
  2826. /// <summary>
  2827. /// Specifies whether the Table's HeaderFont property
  2828. /// should be serialized at design time
  2829. /// </summary>
  2830. /// <returns>True if the HeaderFont property should be
  2831. /// serialized, False otherwise</returns>
  2832. private bool ShouldSerializeHeaderFont()
  2833. {
  2834. return this.HeaderFont != this.Font;
  2835. }
  2836. /// <summary>
  2837. /// Gets or sets the HeaderRenderer used to draw the Column headers
  2838. /// </summary>
  2839. [Browsable(false),
  2840. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2841. public HeaderRenderer HeaderRenderer
  2842. {
  2843. get
  2844. {
  2845. if (this.headerRenderer == null)
  2846. {
  2847. this.headerRenderer = new XPHeaderRenderer();
  2848. }
  2849. return this.headerRenderer;
  2850. }
  2851. set
  2852. {
  2853. if (value == null)
  2854. {
  2855. throw new ArgumentNullException("HeaderRenderer cannot be null");
  2856. }
  2857. if (this.headerRenderer != value)
  2858. {
  2859. this.headerRenderer = value;
  2860. this.headerRenderer.Font = this.HeaderFont;
  2861. this.Invalidate(this.HeaderRectangle, false);
  2862. }
  2863. }
  2864. }
  2865. /// <summary>
  2866. /// Gets the ContextMenu used for Column Headers
  2867. /// </summary>
  2868. [Browsable(false)]
  2869. public HeaderContextMenu HeaderContextMenu
  2870. {
  2871. get
  2872. {
  2873. return this.headerContextMenu;
  2874. }
  2875. }
  2876. /// <summary>
  2877. /// Gets or sets whether the HeaderContextMenu is able to be
  2878. /// displayed when the user right clicks on a Column Header
  2879. /// </summary>
  2880. [Category("Columns"),
  2881. DefaultValue(true),
  2882. Description("Indicates whether the HeaderContextMenu is able to be displayed when the user right clicks on a Column Header")]
  2883. public bool EnableHeaderContextMenu
  2884. {
  2885. get
  2886. {
  2887. return this.HeaderContextMenu.Enabled;
  2888. }
  2889. set
  2890. {
  2891. this.HeaderContextMenu.Enabled = value;
  2892. }
  2893. }
  2894. #endregion
  2895. #region Rows
  2896. /// <summary>
  2897. /// Gets or sets the height of each row
  2898. /// </summary>
  2899. [Browsable(false)]
  2900. public int RowHeight
  2901. {
  2902. get
  2903. {
  2904. if (this.TableModel == null)
  2905. {
  2906. return 0;
  2907. }
  2908. return this.TableModel.RowHeight;
  2909. }
  2910. }
  2911. /// <summary>
  2912. /// Gets the combined height of all the rows in the Table
  2913. /// </summary>
  2914. [Browsable(false)]
  2915. protected int TotalRowHeight
  2916. {
  2917. get
  2918. {
  2919. if (this.TableModel == null)
  2920. {
  2921. return 0;
  2922. }
  2923. return this.TableModel.TotalRowHeight;
  2924. }
  2925. }
  2926. /// <summary>
  2927. /// Gets the combined height of all the rows in the Table
  2928. /// plus the height of the column headers
  2929. /// </summary>
  2930. [Browsable(false)]
  2931. protected int TotalRowAndHeaderHeight
  2932. {
  2933. get
  2934. {
  2935. return this.TotalRowHeight + this.HeaderHeight;
  2936. }
  2937. }
  2938. /// <summary>
  2939. /// Returns the number of Rows in the Table
  2940. /// </summary>
  2941. [Browsable(false)]
  2942. public int RowCount
  2943. {
  2944. get
  2945. {
  2946. if (this.TableModel == null)
  2947. {
  2948. return 0;
  2949. }
  2950. return this.TableModel.Rows.Count;
  2951. }
  2952. }
  2953. /// <summary>
  2954. /// Gets the number of rows that are visible in the Table
  2955. /// </summary>
  2956. [Browsable(false)]
  2957. public int VisibleRowCount
  2958. {
  2959. get
  2960. {
  2961. int count = this.CellDataRect.Height / this.RowHeight;
  2962. if ((this.CellDataRect.Height % this.RowHeight) > 0)
  2963. {
  2964. count++;
  2965. }
  2966. return count;
  2967. }
  2968. }
  2969. /// <summary>
  2970. /// Gets the index of the first visible row in the Table
  2971. /// </summary>
  2972. [Browsable(false)]
  2973. public int TopIndex
  2974. {
  2975. get
  2976. {
  2977. if (this.TableModel == null || this.TableModel.Rows.Count == 0)
  2978. {
  2979. return -1;
  2980. }
  2981. if (this.VScroll)
  2982. {
  2983. return this.vScrollBar.Value;
  2984. }
  2985. return 0;
  2986. }
  2987. }
  2988. /// <summary>
  2989. /// Gets the first visible row in the Table
  2990. /// </summary>
  2991. [Browsable(false)]
  2992. public Row TopItem
  2993. {
  2994. get
  2995. {
  2996. if (this.TableModel == null || this.TableModel.Rows.Count == 0)
  2997. {
  2998. return null;
  2999. }
  3000. return this.TableModel.Rows[this.TopIndex];
  3001. }
  3002. }
  3003. /// <summary>
  3004. /// Gets or sets the background color of odd-numbered rows in the Table
  3005. /// </summary>
  3006. [Category("Appearance"),
  3007. DefaultValue(typeof(Color), "Transparent"),
  3008. Description("The background color of odd-numbered rows in the Table")]
  3009. public Color AlternatingRowColor
  3010. {
  3011. get
  3012. {
  3013. return this.alternatingRowColor;
  3014. }
  3015. set
  3016. {
  3017. if (this.alternatingRowColor != value)
  3018. {
  3019. this.alternatingRowColor = value;
  3020. this.Invalidate(this.CellDataRect, false);
  3021. }
  3022. }
  3023. }
  3024. #endregion
  3025. #region Scrolling
  3026. /// <summary>
  3027. /// Gets or sets a value indicating whether the Table will
  3028. /// allow the user to scroll to any columns or rows placed
  3029. /// outside of its visible boundaries
  3030. /// </summary>
  3031. [Category("Behavior"),
  3032. DefaultValue(true),
  3033. Description("Indicates whether the Table will display scroll bars if it contains more items than can fit in the client area")]
  3034. public bool Scrollable
  3035. {
  3036. get
  3037. {
  3038. return this.scrollable;
  3039. }
  3040. set
  3041. {
  3042. if (this.scrollable != value)
  3043. {
  3044. this.scrollable = value;
  3045. this.PerformLayout();
  3046. }
  3047. }
  3048. }
  3049. /// <summary>
  3050. /// Gets a value indicating whether the horizontal
  3051. /// scroll bar is visible
  3052. /// </summary>
  3053. [Browsable(false),
  3054. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  3055. public bool HScroll
  3056. {
  3057. get
  3058. {
  3059. if (this.hScrollBar == null)
  3060. {
  3061. return false;
  3062. }
  3063. return this.hScrollBar.Visible;
  3064. }
  3065. }
  3066. /// <summary>
  3067. /// Gets a value indicating whether the vertical
  3068. /// scroll bar is visible
  3069. /// </summary>
  3070. [Browsable(false),
  3071. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  3072. public bool VScroll
  3073. {
  3074. get
  3075. {
  3076. if (this.vScrollBar == null)
  3077. {
  3078. return false;
  3079. }
  3080. return this.vScrollBar.Visible;
  3081. }
  3082. }
  3083. #endregion
  3084. #region Selection
  3085. /// <summary>
  3086. /// Gets or sets whether cells are allowed to be selected
  3087. /// </summary>
  3088. [Category("Selection"),
  3089. DefaultValue(true),
  3090. Description("Specifies whether cells are allowed to be selected")]
  3091. public bool AllowSelection
  3092. {
  3093. get
  3094. {
  3095. return this.allowSelection;
  3096. }
  3097. set
  3098. {
  3099. if (this.allowSelection != value)
  3100. {
  3101. this.allowSelection = value;
  3102. if (!value && this.TableModel != null)
  3103. {
  3104. this.TableModel.Selections.Clear();
  3105. }
  3106. }
  3107. }
  3108. }
  3109. /// <summary>
  3110. /// Gets or sets how selected Cells are drawn by a Table
  3111. /// </summary>
  3112. [Category("Selection"),
  3113. DefaultValue(SelectionStyle.ListView),
  3114. Description("Determines how selected Cells are drawn by a Table")]
  3115. public SelectionStyle SelectionStyle
  3116. {
  3117. get
  3118. {
  3119. return this.selectionStyle;
  3120. }
  3121. set
  3122. {
  3123. if (!Enum.IsDefined(typeof(SelectionStyle), value))
  3124. {
  3125. throw new InvalidEnumArgumentException("value", (int) value, typeof(SelectionStyle));
  3126. }
  3127. if (this.selectionStyle != value)
  3128. {
  3129. this.selectionStyle = value;
  3130. if (this.TableModel != null)
  3131. {
  3132. //this.Invalidate(Rectangle.Intersect(this.CellDataRect, this.TableModel.Selections.SelectionBounds), false);
  3133. this.Invalidate(this.CellDataRect, false);
  3134. }
  3135. }
  3136. }
  3137. }
  3138. /// <summary>
  3139. /// Gets or sets whether multiple cells are allowed to be selected
  3140. /// </summary>
  3141. [Category("Selection"),
  3142. DefaultValue(false),
  3143. Description("Specifies whether multiple cells are allowed to be selected")]
  3144. public bool MultiSelect
  3145. {
  3146. get
  3147. {
  3148. return this.multiSelect;
  3149. }
  3150. set
  3151. {
  3152. if (this.multiSelect != value)
  3153. {
  3154. this.multiSelect = value;
  3155. }
  3156. }
  3157. }
  3158. /// <summary>
  3159. /// Gets or sets whether all other cells in the row are highlighted
  3160. /// when a cell is selected
  3161. /// </summary>
  3162. [Category("Selection"),
  3163. DefaultValue(false),
  3164. Description("Specifies whether all other cells in the row are highlighted when a cell is selected")]
  3165. public bool FullRowSelect
  3166. {
  3167. get
  3168. {
  3169. return this.fullRowSelect;
  3170. }
  3171. set
  3172. {
  3173. if (this.fullRowSelect != value)
  3174. {
  3175. this.fullRowSelect = value;
  3176. if (this.TableModel != null)
  3177. {
  3178. //this.Invalidate(Rectangle.Intersect(this.CellDataRect, this.TableModel.Selections.SelectionBounds), false);
  3179. this.Invalidate(this.CellDataRect, false);
  3180. }
  3181. }
  3182. }
  3183. }
  3184. /// <summary>
  3185. /// Gets or sets whether highlighting is removed from the selected
  3186. /// cells when the Table loses focus
  3187. /// </summary>
  3188. [Category("Selection"),
  3189. DefaultValue(false),
  3190. Description("Specifies whether highlighting is removed from the selected cells when the Table loses focus")]
  3191. public bool HideSelection
  3192. {
  3193. get
  3194. {
  3195. return this.hideSelection;
  3196. }
  3197. set
  3198. {
  3199. if (this.hideSelection != value)
  3200. {
  3201. this.hideSelection = value;
  3202. if (!this.Focused && this.TableModel != null)
  3203. {
  3204. //this.Invalidate(Rectangle.Intersect(this.CellDataRect, this.TableModel.Selections.SelectionBounds), false);
  3205. this.Invalidate(this.CellDataRect, false);
  3206. }
  3207. }
  3208. }
  3209. }
  3210. /// <summary>
  3211. /// Gets or sets the background color of a selected cell
  3212. /// </summary>
  3213. [Category("Selection"),
  3214. Description("The background color of a selected cell")]
  3215. public Color SelectionBackColor
  3216. {
  3217. get
  3218. {
  3219. return this.selectionBackColor;
  3220. }
  3221. set
  3222. {
  3223. if (this.selectionBackColor != value)
  3224. {
  3225. this.selectionBackColor = value;
  3226. if (this.TableModel != null)
  3227. {
  3228. //this.Invalidate(Rectangle.Intersect(this.CellDataRect, this.TableModel.Selections.SelectionBounds), false);
  3229. this.Invalidate(this.CellDataRect, false);
  3230. }
  3231. }
  3232. }
  3233. }
  3234. /// <summary>
  3235. /// Specifies whether the Table's SelectionBackColor property
  3236. /// should be serialized at design time
  3237. /// </summary>
  3238. /// <returns>True if the SelectionBackColor property should be
  3239. /// serialized, False otherwise</returns>
  3240. private bool ShouldSerializeSelectionBackColor()
  3241. {
  3242. return (this.selectionBackColor != SystemColors.Highlight);
  3243. }
  3244. /// <summary>
  3245. /// Gets or sets the foreground color of a selected cell
  3246. /// </summary>
  3247. [Category("Selection"),
  3248. Description("The foreground color of a selected cell")]
  3249. public Color SelectionForeColor
  3250. {
  3251. get
  3252. {
  3253. return this.selectionForeColor;
  3254. }
  3255. set
  3256. {
  3257. if (this.selectionForeColor != value)
  3258. {
  3259. this.selectionForeColor = value;
  3260. if (this.TableModel != null)
  3261. {
  3262. //this.Invalidate(Rectangle.Intersect(this.CellDataRect, this.TableModel.Selections.SelectionBounds), false);
  3263. this.Invalidate(this.CellDataRect, false);
  3264. }
  3265. }
  3266. }
  3267. }
  3268. /// <summary>
  3269. /// Specifies whether the Table's SelectionForeColor property
  3270. /// should be serialized at design time
  3271. /// </summary>
  3272. /// <returns>True if the SelectionForeColor property should be
  3273. /// serialized, False otherwise</returns>
  3274. private bool ShouldSerializeSelectionForeColor()
  3275. {
  3276. return (this.selectionForeColor != SystemColors.HighlightText);
  3277. }
  3278. /// <summary>
  3279. /// Gets or sets the background color of a selected cell when the
  3280. /// Table doesn't have the focus
  3281. /// </summary>
  3282. [Category("Selection"),
  3283. Description("The background color of a selected cell when the Table doesn't have the focus")]
  3284. public Color UnfocusedSelectionBackColor
  3285. {
  3286. get
  3287. {
  3288. return this.unfocusedSelectionBackColor;
  3289. }
  3290. set
  3291. {
  3292. if (this.unfocusedSelectionBackColor != value)
  3293. {
  3294. this.unfocusedSelectionBackColor = value;
  3295. if (!this.Focused && !this.HideSelection && this.TableModel != null)
  3296. {
  3297. //this.Invalidate(Rectangle.Intersect(this.CellDataRect, this.TableModel.Selections.SelectionBounds), false);
  3298. this.Invalidate(this.CellDataRect, false);
  3299. }
  3300. }
  3301. }
  3302. }
  3303. /// <summary>
  3304. /// Specifies whether the Table's UnfocusedSelectionBackColor property
  3305. /// should be serialized at design time
  3306. /// </summary>
  3307. /// <returns>True if the UnfocusedSelectionBackColor property should be
  3308. /// serialized, False otherwise</returns>
  3309. private bool ShouldSerializeUnfocusedSelectionBackColor()
  3310. {
  3311. return (this.unfocusedSelectionBackColor != SystemColors.Control);
  3312. }
  3313. /// <summary>
  3314. /// Gets or sets the foreground color of a selected cell when the
  3315. /// Table doesn't have the focus
  3316. /// </summary>
  3317. [Category("Selection"),
  3318. Description("The foreground color of a selected cell when the Table doesn't have the focus")]
  3319. public Color UnfocusedSelectionForeColor
  3320. {
  3321. get
  3322. {
  3323. return this.unfocusedSelectionForeColor;
  3324. }
  3325. set
  3326. {
  3327. if (this.unfocusedSelectionForeColor != value)
  3328. {
  3329. this.unfocusedSelectionForeColor = value;
  3330. if (!this.Focused && !this.HideSelection && this.TableModel != null)
  3331. {
  3332. //this.Invalidate(Rectangle.Intersect(this.CellDataRect, this.TableModel.Selections.SelectionBounds), false);
  3333. this.Invalidate(this.CellDataRect, false);
  3334. }
  3335. }
  3336. }
  3337. }
  3338. /// <summary>
  3339. /// Specifies whether the Table's UnfocusedSelectionForeColor property
  3340. /// should be serialized at design time
  3341. /// </summary>
  3342. /// <returns>True if the UnfocusedSelectionForeColor property should be
  3343. /// serialized, False otherwise</returns>
  3344. private bool ShouldSerializeUnfocusedSelectionForeColor()
  3345. {
  3346. return (this.unfocusedSelectionForeColor != SystemColors.ControlText);
  3347. }
  3348. /// <summary>
  3349. /// Gets an array that contains the currently selected Rows
  3350. /// </summary>
  3351. [Browsable(false)]
  3352. public Row[] SelectedItems
  3353. {
  3354. get
  3355. {
  3356. if (this.TableModel == null)
  3357. {
  3358. return new Row[0];
  3359. }
  3360. return this.TableModel.Selections.SelectedItems;
  3361. }
  3362. }
  3363. /// <summary>
  3364. /// Gets an array that contains the indexes of the currently selected Rows
  3365. /// </summary>
  3366. [Browsable(false)]
  3367. public int[] SelectedIndicies
  3368. {
  3369. get
  3370. {
  3371. if (this.TableModel == null)
  3372. {
  3373. return new int[0];
  3374. }
  3375. return this.TableModel.Selections.SelectedIndicies;
  3376. }
  3377. }
  3378. #endregion
  3379. #region TableModel
  3380. /// <summary>
  3381. /// Gets or sets the TableModel that contains all the Rows
  3382. /// and Cells displayed in the Table
  3383. /// </summary>
  3384. [Category("Items"),
  3385. DefaultValue(null),
  3386. Description("Specifies the TableModel that contains all the Rows and Cells displayed in the Table")]
  3387. public TableModel TableModel
  3388. {
  3389. get
  3390. {
  3391. return this.tableModel;
  3392. }
  3393. set
  3394. {
  3395. if (this.tableModel != value)
  3396. {
  3397. if (this.tableModel != null && this.tableModel.Table == this)
  3398. {
  3399. this.tableModel.InternalTable = null;
  3400. }
  3401. this.tableModel = value;
  3402. if (value != null)
  3403. {
  3404. value.InternalTable = this;
  3405. }
  3406. this.OnTableModelChanged(EventArgs.Empty);
  3407. }
  3408. }
  3409. }
  3410. /// <summary>
  3411. /// Gets or sets the text displayed by the Table when it doesn't
  3412. /// contain any items
  3413. /// </summary>
  3414. [Category("Appearance"),
  3415. DefaultValue("There are no items in this view"),
  3416. Description("Specifies the text displayed by the Table when it doesn't contain any items")]
  3417. public string NoItemsText
  3418. {
  3419. get
  3420. {
  3421. return this.noItemsText;
  3422. }
  3423. set
  3424. {
  3425. if (!this.noItemsText.Equals(value))
  3426. {
  3427. this.noItemsText = value;
  3428. if (this.ColumnModel == null || this.TableModel == null || this.TableModel.Rows.Count == 0)
  3429. {
  3430. this.Invalidate(this.PseudoClientRect);
  3431. }
  3432. }
  3433. }
  3434. }
  3435. #endregion
  3436. #region TableState
  3437. /// <summary>
  3438. /// Gets or sets the current state of the Table
  3439. /// </summary>
  3440. protected TableState TableState
  3441. {
  3442. get
  3443. {
  3444. return this.tableState;
  3445. }
  3446. set
  3447. {
  3448. this.tableState = value;
  3449. }
  3450. }
  3451. /// <summary>
  3452. /// Calculates the state of the Table at the specified
  3453. /// client coordinates
  3454. /// </summary>
  3455. /// <param name="x">The client x coordinate</param>
  3456. /// <param name="y">The client y coordinate</param>
  3457. protected void CalcTableState(int x, int y)
  3458. {
  3459. TableRegion region = this.HitTest(x, y);
  3460. // are we in the header
  3461. if (region == TableRegion.ColumnHeader)
  3462. {
  3463. int column = this.ColumnIndexAt(x, y);
  3464. // get out of here if we aren't in a column
  3465. if (column == -1)
  3466. {
  3467. this.TableState = TableState.Normal;
  3468. return;
  3469. }
  3470. // get the bounding rectangle for the column's header
  3471. Rectangle columnRect = this.ColumnModel.ColumnHeaderRect(column);
  3472. x = this.ClientToDisplayRect(x, y).X;
  3473. // are we in a resizing section on the left
  3474. if (x < columnRect.Left + Column.ResizePadding)
  3475. {
  3476. this.TableState = TableState.ColumnResizing;
  3477. while (column != 0)
  3478. {
  3479. if (this.ColumnModel.Columns[column-1].Visible)
  3480. {
  3481. break;
  3482. }
  3483. column--;
  3484. }
  3485. // if we are in the first visible column or the next column
  3486. // to the left is disabled, then we should be potentialy
  3487. // selecting instead of resizing
  3488. if (column == 0 || !this.ColumnModel.Columns[column-1].Enabled)
  3489. {
  3490. this.TableState = TableState.ColumnSelecting;
  3491. }
  3492. }
  3493. // or a resizing section on the right
  3494. else if (x > columnRect.Right - Column.ResizePadding)
  3495. {
  3496. this.TableState = TableState.ColumnResizing;
  3497. }
  3498. // looks like we're somewhere in the middle of
  3499. // the column header
  3500. else
  3501. {
  3502. this.TableState = TableState.ColumnSelecting;
  3503. }
  3504. }
  3505. else if (region == TableRegion.Cells)
  3506. {
  3507. this.TableState = TableState.Selecting;
  3508. }
  3509. else
  3510. {
  3511. this.TableState = TableState.Normal;
  3512. }
  3513. if (this.TableState == TableState.ColumnResizing && !this.ColumnResizing)
  3514. {
  3515. this.TableState = TableState.ColumnSelecting;
  3516. }
  3517. }
  3518. /// <summary>
  3519. /// Gets whether the Table is able to raise events
  3520. /// </summary>
  3521. protected internal bool CanRaiseEvents
  3522. {
  3523. get
  3524. {
  3525. return (this.IsHandleCreated && this.beginUpdateCount == 0);
  3526. }
  3527. }
  3528. /// <summary>
  3529. /// Gets or sets whether the Table is being used as a preview Table in
  3530. /// a ColumnCollectionEditor
  3531. /// </summary>
  3532. internal bool Preview
  3533. {
  3534. get
  3535. {
  3536. return this.preview;
  3537. }
  3538. set
  3539. {
  3540. this.preview = value;
  3541. }
  3542. }
  3543. #endregion
  3544. #region ToolTips
  3545. /// <summary>
  3546. /// Gets the internal tooltip component
  3547. /// </summary>
  3548. internal ToolTip ToolTip
  3549. {
  3550. get
  3551. {
  3552. return this.toolTip;
  3553. }
  3554. }
  3555. /// <summary>
  3556. /// Gets or sets whether ToolTips are currently enabled for the Table
  3557. /// </summary>
  3558. [Category("ToolTips"),
  3559. DefaultValue(false),
  3560. Description("Specifies whether ToolTips are enabled for the Table.")]
  3561. public bool EnableToolTips
  3562. {
  3563. get
  3564. {
  3565. return this.toolTip.Active;
  3566. }
  3567. set
  3568. {
  3569. this.toolTip.Active = value;
  3570. }
  3571. }
  3572. /// <summary>
  3573. /// Gets or sets the automatic delay for the Table's ToolTip
  3574. /// </summary>
  3575. [Category("ToolTips"),
  3576. DefaultValue(500),
  3577. Description("Specifies the automatic delay for the Table's ToolTip.")]
  3578. public int ToolTipAutomaticDelay
  3579. {
  3580. get
  3581. {
  3582. return this.toolTip.AutomaticDelay;
  3583. }
  3584. set
  3585. {
  3586. if (value > 0 && this.toolTip.AutomaticDelay != value)
  3587. {
  3588. this.toolTip.AutomaticDelay = value;
  3589. }
  3590. }
  3591. }
  3592. /// <summary>
  3593. /// Gets or sets the period of time the Table's ToolTip remains visible if
  3594. /// the mouse pointer is stationary within a Cell with a valid ToolTip text
  3595. /// </summary>
  3596. [Category("ToolTips"),
  3597. DefaultValue(5000),
  3598. Description("Specifies the period of time the Table's ToolTip remains visible if the mouse pointer is stationary within a cell with specified ToolTip text.")]
  3599. public int ToolTipAutoPopDelay
  3600. {
  3601. get
  3602. {
  3603. return this.toolTip.AutoPopDelay;
  3604. }
  3605. set
  3606. {
  3607. if (value > 0 && this.toolTip.AutoPopDelay != value)
  3608. {
  3609. this.toolTip.AutoPopDelay = value;
  3610. }
  3611. }
  3612. }
  3613. /// <summary>
  3614. /// Gets or sets the time that passes before the Table's ToolTip appears
  3615. /// </summary>
  3616. [Category("ToolTips"),
  3617. DefaultValue(1000),
  3618. Description("Specifies the time that passes before the Table's ToolTip appears.")]
  3619. public int ToolTipInitialDelay
  3620. {
  3621. get
  3622. {
  3623. return this.toolTip.InitialDelay;
  3624. }
  3625. set
  3626. {
  3627. if (value > 0 && this.toolTip.InitialDelay != value)
  3628. {
  3629. this.toolTip.InitialDelay = value;
  3630. }
  3631. }
  3632. }
  3633. /// <summary>
  3634. /// Gets or sets whether the Table's ToolTip window is
  3635. /// displayed even when its parent control is not active
  3636. /// </summary>
  3637. [Category("ToolTips"),
  3638. DefaultValue(false),
  3639. Description("Specifies whether the Table's ToolTip window is displayed even when its parent control is not active.")]
  3640. public bool ToolTipShowAlways
  3641. {
  3642. get
  3643. {
  3644. return this.toolTip.ShowAlways;
  3645. }
  3646. set
  3647. {
  3648. if (this.toolTip.ShowAlways != value)
  3649. {
  3650. this.toolTip.ShowAlways = value;
  3651. }
  3652. }
  3653. }
  3654. /// <summary>
  3655. ///
  3656. /// </summary>
  3657. private void ResetToolTip()
  3658. {
  3659. bool tooltipActive = this.ToolTip.Active;
  3660. if (tooltipActive)
  3661. {
  3662. this.ToolTip.Active = false;
  3663. }
  3664. this.ResetMouseEventArgs();
  3665. this.ToolTip.SetToolTip(this, null);
  3666. if (tooltipActive)
  3667. {
  3668. this.ToolTip.Active = true;
  3669. }
  3670. }
  3671. #endregion
  3672. #endregion
  3673. #region Events
  3674. #region Cells
  3675. /// <summary>
  3676. /// Raises the CellPropertyChanged event
  3677. /// </summary>
  3678. /// <param name="e">A CellEventArgs that contains the event data</param>
  3679. protected internal virtual void OnCellPropertyChanged(CellEventArgs e)
  3680. {
  3681. if (this.CanRaiseEvents)
  3682. {
  3683. this.InvalidateCell(e.Row, e.Column);
  3684. if (CellPropertyChanged != null)
  3685. {
  3686. CellPropertyChanged(this, e);
  3687. }
  3688. if (e.EventType == CellEventType.CheckStateChanged)
  3689. {
  3690. this.OnCellCheckChanged(new CellCheckBoxEventArgs(e.Cell, e.Column, e.Row));
  3691. }
  3692. }
  3693. }
  3694. /// <summary>
  3695. /// Handler for a Cells PropertyChanged event
  3696. /// </summary>
  3697. /// <param name="sender">The object that raised the event</param>
  3698. /// <param name="e">A CellEventArgs that contains the event data</param>
  3699. private void cell_PropertyChanged(object sender, CellEventArgs e)
  3700. {
  3701. this.OnCellPropertyChanged(e);
  3702. }
  3703. #region Buttons
  3704. /// <summary>
  3705. /// Raises the CellButtonClicked event
  3706. /// </summary>
  3707. /// <param name="e">A CellButtonEventArgs that contains the event data</param>
  3708. protected internal virtual void OnCellButtonClicked(CellButtonEventArgs e)
  3709. {
  3710. if (this.CanRaiseEvents)
  3711. {
  3712. if (CellButtonClicked != null)
  3713. {
  3714. CellButtonClicked(this, e);
  3715. }
  3716. }
  3717. }
  3718. #endregion
  3719. #region CheckBox
  3720. /// <summary>
  3721. /// Raises the CellCheckChanged event
  3722. /// </summary>
  3723. /// <param name="e">A CellCheckChanged that contains the event data</param>
  3724. protected internal virtual void OnCellCheckChanged(CellCheckBoxEventArgs e)
  3725. {
  3726. if (this.CanRaiseEvents)
  3727. {
  3728. if (CellCheckChanged != null)
  3729. {
  3730. CellCheckChanged(this, e);
  3731. }
  3732. }
  3733. }
  3734. #endregion
  3735. #region Focus
  3736. /// <summary>
  3737. /// Raises the CellGotFocus event
  3738. /// </summary>
  3739. /// <param name="e">A CellFocusEventArgs that contains the event data</param>
  3740. protected virtual void OnCellGotFocus(CellFocusEventArgs e)
  3741. {
  3742. if (this.CanRaiseEvents)
  3743. {
  3744. ICellRenderer renderer = this.ColumnModel.GetCellRenderer(e.Column);
  3745. if (renderer != null)
  3746. {
  3747. renderer.OnGotFocus(e);
  3748. }
  3749. if (CellGotFocus != null)
  3750. {
  3751. CellGotFocus(this, e);
  3752. }
  3753. }
  3754. }
  3755. /// <summary>
  3756. /// Raises the GotFocus event for the Cell at the specified position
  3757. /// </summary>
  3758. /// <param name="cellPos">The position of the Cell that gained focus</param>
  3759. protected void RaiseCellGotFocus(CellPos cellPos)
  3760. {
  3761. if (!this.IsValidCell(cellPos))
  3762. {
  3763. return;
  3764. }
  3765. ICellRenderer renderer = this.ColumnModel.GetCellRenderer(cellPos.Column);
  3766. if (renderer != null)
  3767. {
  3768. Cell cell = null;
  3769. if (cellPos.Column < this.TableModel.Rows[cellPos.Row].Cells.Count)
  3770. {
  3771. cell = this.TableModel.Rows[cellPos.Row].Cells[cellPos.Column];
  3772. }
  3773. CellFocusEventArgs cfea = new CellFocusEventArgs(cell, this, cellPos.Row, cellPos.Column, this.CellRect(cellPos.Row, cellPos.Column));
  3774. this.OnCellGotFocus(cfea);
  3775. }
  3776. }
  3777. /// <summary>
  3778. /// Raises the CellLostFocus event
  3779. /// </summary>
  3780. /// <param name="e">A CellFocusEventArgs that contains the event data</param>
  3781. protected virtual void OnCellLostFocus(CellFocusEventArgs e)
  3782. {
  3783. if (this.CanRaiseEvents)
  3784. {
  3785. ICellRenderer renderer = this.ColumnModel.GetCellRenderer(e.Column);
  3786. if (renderer != null)
  3787. {
  3788. renderer.OnLostFocus(e);
  3789. }
  3790. if (CellLostFocus != null)
  3791. {
  3792. CellLostFocus(this, e);
  3793. }
  3794. }
  3795. }
  3796. /// <summary>
  3797. /// Raises the LostFocus event for the Cell at the specified position
  3798. /// </summary>
  3799. /// <param name="cellPos">The position of the Cell that lost focus</param>
  3800. protected void RaiseCellLostFocus(CellPos cellPos)
  3801. {
  3802. if (!this.IsValidCell(cellPos))
  3803. {
  3804. return;
  3805. }
  3806. ICellRenderer renderer = this.ColumnModel.GetCellRenderer(cellPos.Column);
  3807. if (renderer != null)
  3808. {
  3809. Cell cell = null;
  3810. if (cellPos.Column < this.TableModel.Rows[cellPos.Row].Cells.Count)
  3811. {
  3812. cell = this.TableModel[cellPos.Row, cellPos.Column];
  3813. }
  3814. CellFocusEventArgs cfea = new CellFocusEventArgs(cell, this, cellPos.Row, cellPos.Column, this.CellRect(cellPos.Row, cellPos.Column));
  3815. this.OnCellLostFocus(cfea);
  3816. }
  3817. }
  3818. #endregion
  3819. #region Keys
  3820. /// <summary>
  3821. /// Raises the CellKeyDown event
  3822. /// </summary>
  3823. /// <param name="e">A CellKeyEventArgs that contains the event data</param>
  3824. protected virtual void OnCellKeyDown(CellKeyEventArgs e)
  3825. {
  3826. if (this.CanRaiseEvents)
  3827. {
  3828. ICellRenderer renderer = this.ColumnModel.GetCellRenderer(e.Column);
  3829. if (renderer != null)
  3830. {
  3831. renderer.OnKeyDown(e);
  3832. }
  3833. if (CellKeyDown != null)
  3834. {
  3835. CellKeyDown(e.Cell, e);
  3836. }
  3837. }
  3838. }
  3839. /// <summary>
  3840. /// Raises a KeyDown event for the Cell at the specified cell position
  3841. /// </summary>
  3842. /// <param name="cellPos">The position of the Cell</param>
  3843. /// <param name="e">A KeyEventArgs that contains the event data</param>
  3844. protected void RaiseCellKeyDown(CellPos cellPos, KeyEventArgs e)
  3845. {
  3846. if (!this.IsValidCell(cellPos))
  3847. {
  3848. return;
  3849. }
  3850. if (!this.TableModel[cellPos].Enabled)
  3851. {
  3852. return;
  3853. }
  3854. if (this.ColumnModel.GetCellRenderer(cellPos.Column) != null)
  3855. {
  3856. Cell cell = null;
  3857. if (cellPos.Column < this.TableModel.Rows[cellPos.Row].Cells.Count)
  3858. {
  3859. cell = this.TableModel.Rows[cellPos.Row].Cells[cellPos.Column];
  3860. }
  3861. CellKeyEventArgs ckea = new CellKeyEventArgs(cell, this, cellPos, this.CellRect(cellPos.Row, cellPos.Column), e);
  3862. this.OnCellKeyDown(ckea);
  3863. }
  3864. }
  3865. /// <summary>
  3866. /// Raises the CellKeyUp event
  3867. /// </summary>
  3868. /// <param name="e">A CellKeyEventArgs that contains the event data</param>
  3869. protected virtual void OnCellKeyUp(CellKeyEventArgs e)
  3870. {
  3871. if (this.CanRaiseEvents)
  3872. {
  3873. ICellRenderer renderer = this.ColumnModel.GetCellRenderer(e.Column);
  3874. if (renderer != null)
  3875. {
  3876. renderer.OnKeyUp(e);
  3877. }
  3878. if (CellKeyUp != null)
  3879. {
  3880. CellKeyUp(e.Cell, e);
  3881. }
  3882. }
  3883. }
  3884. /// <summary>
  3885. /// Raises a KeyUp event for the Cell at the specified cell position
  3886. /// </summary>
  3887. /// <param name="cellPos">The position of the Cell</param>
  3888. /// <param name="e">A KeyEventArgs that contains the event data</param>
  3889. protected void RaiseCellKeyUp(CellPos cellPos, KeyEventArgs e)
  3890. {
  3891. if (!this.IsValidCell(cellPos))
  3892. {
  3893. return;
  3894. }
  3895. if (!this.TableModel[cellPos].Enabled)
  3896. {
  3897. return;
  3898. }
  3899. if (this.ColumnModel.GetCellRenderer(cellPos.Column) != null)
  3900. {
  3901. Cell cell = null;
  3902. if (cellPos.Column < this.TableModel.Rows[cellPos.Row].Cells.Count)
  3903. {
  3904. cell = this.TableModel.Rows[cellPos.Row].Cells[cellPos.Column];
  3905. }
  3906. CellKeyEventArgs ckea = new CellKeyEventArgs(cell, this, cellPos, this.CellRect(cellPos.Row, cellPos.Column), e);
  3907. this.OnCellKeyUp(ckea);
  3908. }
  3909. }
  3910. #endregion
  3911. #region Mouse
  3912. #region MouseEnter
  3913. /// <summary>
  3914. /// Raises the CellMouseEnter event
  3915. /// </summary>
  3916. /// <param name="e">A CellMouseEventArgs that contains the event data</param>
  3917. protected virtual void OnCellMouseEnter(CellMouseEventArgs e)
  3918. {
  3919. if (this.CanRaiseEvents)
  3920. {
  3921. ICellRenderer renderer = this.ColumnModel.GetCellRenderer(e.Column);
  3922. if (renderer != null)
  3923. {
  3924. renderer.OnMouseEnter(e);
  3925. }
  3926. if (CellMouseEnter != null)
  3927. {
  3928. CellMouseEnter(e.Cell, e);
  3929. }
  3930. }
  3931. }
  3932. /// <summary>
  3933. /// Raises a MouseEnter event for the Cell at the specified cell position
  3934. /// </summary>
  3935. /// <param name="cellPos">The position of the Cell</param>
  3936. protected void RaiseCellMouseEnter(CellPos cellPos)
  3937. {
  3938. if (!this.IsValidCell(cellPos))
  3939. {
  3940. return;
  3941. }
  3942. if (this.ColumnModel.GetCellRenderer(cellPos.Column) != null)
  3943. {
  3944. Cell cell = null;
  3945. if (cellPos.Column < this.TableModel.Rows[cellPos.Row].Cells.Count)
  3946. {
  3947. cell = this.TableModel.Rows[cellPos.Row].Cells[cellPos.Column];
  3948. }
  3949. CellMouseEventArgs mcea = new CellMouseEventArgs(cell, this, cellPos.Row, cellPos.Column, this.CellRect(cellPos.Row, cellPos.Column));
  3950. this.OnCellMouseEnter(mcea);
  3951. }
  3952. }
  3953. #endregion
  3954. #region MouseLeave
  3955. /// <summary>
  3956. /// Raises the CellMouseLeave event
  3957. /// </summary>
  3958. /// <param name="e">A CellMouseEventArgs that contains the event data</param>
  3959. protected virtual void OnCellMouseLeave(CellMouseEventArgs e)
  3960. {
  3961. if (this.CanRaiseEvents)
  3962. {
  3963. ICellRenderer renderer = this.ColumnModel.GetCellRenderer(e.Column);
  3964. if (renderer != null)
  3965. {
  3966. renderer.OnMouseLeave(e);
  3967. }
  3968. if (CellMouseLeave != null)
  3969. {
  3970. CellMouseLeave(e.Cell, e);
  3971. }
  3972. }
  3973. }
  3974. /// <summary>
  3975. /// Raises a MouseLeave event for the Cell at the specified cell position
  3976. /// </summary>
  3977. /// <param name="cellPos">The position of the Cell</param>
  3978. protected internal void RaiseCellMouseLeave(CellPos cellPos)
  3979. {
  3980. if (!this.IsValidCell(cellPos))
  3981. {
  3982. return;
  3983. }
  3984. if (this.ColumnModel.GetCellRenderer(cellPos.Column) != null)
  3985. {
  3986. Cell cell = null;
  3987. if (cellPos.Column < this.TableModel.Rows[cellPos.Row].Cells.Count)
  3988. {
  3989. cell = this.TableModel.Rows[cellPos.Row].Cells[cellPos.Column];
  3990. }
  3991. CellMouseEventArgs mcea = new CellMouseEventArgs(cell, this, cellPos.Row, cellPos.Column, this.CellRect(cellPos.Row, cellPos.Column));
  3992. this.OnCellMouseLeave(mcea);
  3993. }
  3994. }
  3995. #endregion
  3996. #region MouseUp
  3997. /// <summary>
  3998. /// Raises the CellMouseUp event
  3999. /// </summary>
  4000. /// <param name="e">A CellMouseEventArgs that contains the event data</param>
  4001. protected virtual void OnCellMouseUp(CellMouseEventArgs e)
  4002. {
  4003. if (this.CanRaiseEvents)
  4004. {
  4005. ICellRenderer renderer = this.ColumnModel.GetCellRenderer(e.Column);
  4006. if (renderer != null)
  4007. {
  4008. renderer.OnMouseUp(e);
  4009. }
  4010. if (CellMouseUp != null)
  4011. {
  4012. CellMouseUp(e.Cell, e);
  4013. }
  4014. }
  4015. }
  4016. /// <summary>
  4017. /// Raises a MouseUp event for the Cell at the specified cell position
  4018. /// </summary>
  4019. /// <param name="cellPos">The position of the Cell</param>
  4020. /// <param name="e">A MouseEventArgs that contains the event data</param>
  4021. protected void RaiseCellMouseUp(CellPos cellPos, MouseEventArgs e)
  4022. {
  4023. if (!this.IsValidCell(cellPos))
  4024. {
  4025. return;
  4026. }
  4027. if (!this.TableModel[cellPos].Enabled)
  4028. {
  4029. return;
  4030. }
  4031. if (this.ColumnModel.GetCellRenderer(cellPos.Column) != null)
  4032. {
  4033. Cell cell = null;
  4034. if (cellPos.Column < this.TableModel.Rows[cellPos.Row].Cells.Count)
  4035. {
  4036. cell = this.TableModel.Rows[cellPos.Row].Cells[cellPos.Column];
  4037. }
  4038. CellMouseEventArgs mcea = new CellMouseEventArgs(cell, this, cellPos.Row, cellPos.Column, this.CellRect(cellPos.Row, cellPos.Column), e);
  4039. this.OnCellMouseUp(mcea);
  4040. }
  4041. }
  4042. #endregion
  4043. #region MouseDown
  4044. /// <summary>
  4045. /// Raises the CellMouseDown event
  4046. /// </summary>
  4047. /// <param name="e">A CellMouseEventArgs that contains the event data</param>
  4048. protected virtual void OnCellMouseDown(CellMouseEventArgs e)
  4049. {
  4050. if (this.CanRaiseEvents)
  4051. {
  4052. ICellRenderer renderer = this.ColumnModel.GetCellRenderer(e.Column);
  4053. if (renderer != null)
  4054. {
  4055. renderer.OnMouseDown(e);
  4056. }
  4057. if (CellMouseDown != null)
  4058. {
  4059. CellMouseDown(e.Cell, e);
  4060. }
  4061. }
  4062. }
  4063. /// <summary>
  4064. /// Raises a MouseDown event for the Cell at the specified cell position
  4065. /// </summary>
  4066. /// <param name="cellPos">The position of the Cell</param>
  4067. /// <param name="e">A MouseEventArgs that contains the event data</param>
  4068. protected void RaiseCellMouseDown(CellPos cellPos, MouseEventArgs e)
  4069. {
  4070. if (!this.IsValidCell(cellPos))
  4071. {
  4072. return;
  4073. }
  4074. if (!this.TableModel[cellPos].Enabled)
  4075. {
  4076. return;
  4077. }
  4078. if (this.ColumnModel.GetCellRenderer(cellPos.Column) != null)
  4079. {
  4080. Cell cell = null;
  4081. if (cellPos.Column < this.TableModel.Rows[cellPos.Row].Cells.Count)
  4082. {
  4083. cell = this.TableModel.Rows[cellPos.Row].Cells[cellPos.Column];
  4084. }
  4085. CellMouseEventArgs mcea = new CellMouseEventArgs(cell, this, cellPos.Row, cellPos.Column, this.CellRect(cellPos.Row, cellPos.Column), e);
  4086. this.OnCellMouseDown(mcea);
  4087. }
  4088. }
  4089. #endregion
  4090. #region MouseMove
  4091. /// <summary>
  4092. /// Raises the CellMouseMove event
  4093. /// </summary>
  4094. /// <param name="e">A CellMouseEventArgs that contains the event data</param>
  4095. protected virtual void OnCellMouseMove(CellMouseEventArgs e)
  4096. {
  4097. if (this.CanRaiseEvents)
  4098. {
  4099. ICellRenderer renderer = this.ColumnModel.GetCellRenderer(e.Column);
  4100. if (renderer != null)
  4101. {
  4102. renderer.OnMouseMove(e);
  4103. }
  4104. if (CellMouseMove != null)
  4105. {
  4106. CellMouseMove(e.Cell, e);
  4107. }
  4108. }
  4109. }
  4110. /// <summary>
  4111. /// Raises a MouseMove event for the Cell at the specified cell position
  4112. /// </summary>
  4113. /// <param name="cellPos">The position of the Cell</param>
  4114. /// <param name="e">A MouseEventArgs that contains the event data</param>
  4115. protected void RaiseCellMouseMove(CellPos cellPos, MouseEventArgs e)
  4116. {
  4117. if (!this.IsValidCell(cellPos))
  4118. {
  4119. return;
  4120. }
  4121. if (this.ColumnModel.GetCellRenderer(cellPos.Column) != null)
  4122. {
  4123. Cell cell = null;
  4124. if (cellPos.Column < this.TableModel.Rows[cellPos.Row].Cells.Count)
  4125. {
  4126. cell = this.TableModel.Rows[cellPos.Row].Cells[cellPos.Column];
  4127. }
  4128. CellMouseEventArgs mcea = new CellMouseEventArgs(cell, this, cellPos.Row, cellPos.Column, this.CellRect(cellPos.Row, cellPos.Column), e);
  4129. this.OnCellMouseMove(mcea);
  4130. }
  4131. }
  4132. /// <summary>
  4133. /// Resets the last known cell position that the mouse was over to empty
  4134. /// </summary>
  4135. internal void ResetLastMouseCell()
  4136. {
  4137. if (!this.lastMouseCell.IsEmpty)
  4138. {
  4139. this.ResetMouseEventArgs();
  4140. CellPos oldLastMouseCell = this.lastMouseCell;
  4141. this.lastMouseCell = CellPos.Empty;
  4142. this.RaiseCellMouseLeave(oldLastMouseCell);
  4143. }
  4144. }
  4145. #endregion
  4146. #region MouseHover
  4147. /// <summary>
  4148. /// Raises the CellHover event
  4149. /// </summary>
  4150. /// <param name="e">A CellEventArgs that contains the event data</param>
  4151. protected virtual void OnCellMouseHover(CellMouseEventArgs e)
  4152. {
  4153. if (this.CanRaiseEvents)
  4154. {
  4155. if (CellMouseHover != null)
  4156. {
  4157. CellMouseHover(e.Cell, e);
  4158. }
  4159. }
  4160. }
  4161. #endregion
  4162. #region Click
  4163. /// <summary>
  4164. /// Raises the CellClick event
  4165. /// </summary>
  4166. /// <param name="e">A CellEventArgs that contains the event data</param>
  4167. protected virtual void OnCellClick(CellMouseEventArgs e)
  4168. {
  4169. if (!this.IsCellEnabled(e.CellPos))
  4170. {
  4171. return;
  4172. }
  4173. if (this.CanRaiseEvents)
  4174. {
  4175. ICellRenderer renderer = this.ColumnModel.GetCellRenderer(this.LastMouseCell.Column);
  4176. if (renderer != null)
  4177. {
  4178. renderer.OnClick(e);
  4179. }
  4180. if (CellClick != null)
  4181. {
  4182. CellClick(e.Cell, e);
  4183. }
  4184. }
  4185. }
  4186. /// <summary>
  4187. /// Raises the CellDoubleClick event
  4188. /// </summary>
  4189. /// <param name="e">A CellEventArgs that contains the event data</param>
  4190. protected virtual void OnCellDoubleClick(CellMouseEventArgs e)
  4191. {
  4192. if (!this.IsCellEnabled(e.CellPos))
  4193. {
  4194. return;
  4195. }
  4196. if (this.CanRaiseEvents)
  4197. {
  4198. ICellRenderer renderer = this.ColumnModel.GetCellRenderer(this.LastMouseCell.Column);
  4199. if (renderer != null)
  4200. {
  4201. renderer.OnDoubleClick(e);
  4202. }
  4203. if (CellDoubleClick != null)
  4204. {
  4205. CellDoubleClick(e.Cell, e);
  4206. }
  4207. }
  4208. }
  4209. #endregion
  4210. #endregion
  4211. #endregion
  4212. #region Columns
  4213. /// <summary>
  4214. /// Raises the ColumnPropertyChanged event
  4215. /// </summary>
  4216. /// <param name="e">A ColumnEventArgs that contains the event data</param>
  4217. protected internal virtual void OnColumnPropertyChanged(ColumnEventArgs e)
  4218. {
  4219. if (this.CanRaiseEvents)
  4220. {
  4221. Rectangle columnHeaderRect;
  4222. if (e.Index != -1)
  4223. {
  4224. columnHeaderRect = this.ColumnHeaderRect(e.Index);
  4225. }
  4226. else
  4227. {
  4228. columnHeaderRect = this.ColumnHeaderRect(e.Column);
  4229. }
  4230. switch (e.EventType)
  4231. {
  4232. case ColumnEventType.VisibleChanged:
  4233. case ColumnEventType.WidthChanged:
  4234. {
  4235. if (e.EventType == ColumnEventType.VisibleChanged)
  4236. {
  4237. if (e.Column.Visible && e.Index != this.lastSortedColumn)
  4238. {
  4239. e.Column.InternalSortOrder = SortOrder.None;
  4240. }
  4241. if (e.Index == this.FocusedCell.Column && !e.Column.Visible)
  4242. {
  4243. int index = this.ColumnModel.NextVisibleColumn(e.Index);
  4244. if (index == -1)
  4245. {
  4246. index = this.ColumnModel.PreviousVisibleColumn(e.Index);
  4247. }
  4248. if (index != -1)
  4249. {
  4250. this.FocusedCell = new CellPos(this.FocusedCell.Row, index);
  4251. }
  4252. else
  4253. {
  4254. this.FocusedCell = CellPos.Empty;
  4255. }
  4256. }
  4257. }
  4258. if (columnHeaderRect.X <= 0)
  4259. {
  4260. this.Invalidate(this.PseudoClientRect);
  4261. }
  4262. else if (columnHeaderRect.Left <= this.PseudoClientRect.Right)
  4263. {
  4264. this.Invalidate(new Rectangle(columnHeaderRect.X,
  4265. this.PseudoClientRect.Top,
  4266. this.PseudoClientRect.Right-columnHeaderRect.X,
  4267. this.PseudoClientRect.Height));
  4268. }
  4269. this.UpdateScrollBars();
  4270. break;
  4271. }
  4272. case ColumnEventType.TextChanged:
  4273. case ColumnEventType.StateChanged:
  4274. case ColumnEventType.ImageChanged:
  4275. case ColumnEventType.HeaderAlignmentChanged:
  4276. {
  4277. if (columnHeaderRect.IntersectsWith(this.HeaderRectangle))
  4278. {
  4279. this.Invalidate(columnHeaderRect);
  4280. }
  4281. break;
  4282. }
  4283. case ColumnEventType.AlignmentChanged:
  4284. case ColumnEventType.RendererChanged:
  4285. case ColumnEventType.EnabledChanged:
  4286. {
  4287. if (e.EventType == ColumnEventType.EnabledChanged)
  4288. {
  4289. if (e.Index == this.FocusedCell.Column)
  4290. {
  4291. this.FocusedCell = CellPos.Empty;
  4292. }
  4293. }
  4294. if (columnHeaderRect.IntersectsWith(this.HeaderRectangle))
  4295. {
  4296. this.Invalidate(new Rectangle(columnHeaderRect.X,
  4297. this.PseudoClientRect.Top,
  4298. columnHeaderRect.Width,
  4299. this.PseudoClientRect.Height));
  4300. }
  4301. break;
  4302. }
  4303. }
  4304. if (ColumnPropertyChanged != null)
  4305. {
  4306. ColumnPropertyChanged(e.Column, e);
  4307. }
  4308. }
  4309. }
  4310. #endregion
  4311. #region Column Headers
  4312. #region MouseEnter
  4313. /// <summary>
  4314. /// Raises the HeaderMouseEnter event
  4315. /// </summary>
  4316. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  4317. protected virtual void OnHeaderMouseEnter(HeaderMouseEventArgs e)
  4318. {
  4319. if (this.CanRaiseEvents)
  4320. {
  4321. if (this.HeaderRenderer != null)
  4322. {
  4323. this.HeaderRenderer.OnMouseEnter(e);
  4324. }
  4325. if (HeaderMouseEnter != null)
  4326. {
  4327. HeaderMouseEnter(e.Column, e);
  4328. }
  4329. }
  4330. }
  4331. /// <summary>
  4332. /// Raises a MouseEnter event for the Column header at the specified colunm
  4333. /// index position
  4334. /// </summary>
  4335. /// <param name="index">The index of the column to recieve the event</param>
  4336. protected void RaiseHeaderMouseEnter(int index)
  4337. {
  4338. if (index < 0 || this.ColumnModel == null || index >= this.ColumnModel.Columns.Count)
  4339. {
  4340. return;
  4341. }
  4342. if (this.HeaderRenderer != null)
  4343. {
  4344. Column column = this.ColumnModel.Columns[index];
  4345. HeaderMouseEventArgs mhea = new HeaderMouseEventArgs(column, this, index, this.DisplayRectToClient(this.ColumnModel.ColumnHeaderRect(index)));
  4346. this.OnHeaderMouseEnter(mhea);
  4347. }
  4348. }
  4349. #endregion
  4350. #region MouseLeave
  4351. /// <summary>
  4352. /// Raises the HeaderMouseLeave event
  4353. /// </summary>
  4354. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  4355. protected virtual void OnHeaderMouseLeave(HeaderMouseEventArgs e)
  4356. {
  4357. if (this.CanRaiseEvents)
  4358. {
  4359. if (this.HeaderRenderer != null)
  4360. {
  4361. this.HeaderRenderer.OnMouseLeave(e);
  4362. }
  4363. if (HeaderMouseLeave != null)
  4364. {
  4365. HeaderMouseLeave(e.Column, e);
  4366. }
  4367. }
  4368. }
  4369. /// <summary>
  4370. /// Raises a MouseLeave event for the Column header at the specified colunm
  4371. /// index position
  4372. /// </summary>
  4373. /// <param name="index">The index of the column to recieve the event</param>
  4374. protected void RaiseHeaderMouseLeave(int index)
  4375. {
  4376. if (index < 0 || this.ColumnModel == null || index >= this.ColumnModel.Columns.Count)
  4377. {
  4378. return;
  4379. }
  4380. if (this.HeaderRenderer != null)
  4381. {
  4382. Column column = this.ColumnModel.Columns[index];
  4383. HeaderMouseEventArgs mhea = new HeaderMouseEventArgs(column, this, index, this.DisplayRectToClient(this.ColumnModel.ColumnHeaderRect(index)));
  4384. this.OnHeaderMouseLeave(mhea);
  4385. }
  4386. }
  4387. #endregion
  4388. #region MouseUp
  4389. /// <summary>
  4390. /// Raises the HeaderMouseUp event
  4391. /// </summary>
  4392. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  4393. protected virtual void OnHeaderMouseUp(HeaderMouseEventArgs e)
  4394. {
  4395. if (this.CanRaiseEvents)
  4396. {
  4397. if (this.HeaderRenderer != null)
  4398. {
  4399. this.HeaderRenderer.OnMouseUp(e);
  4400. }
  4401. if (HeaderMouseUp != null)
  4402. {
  4403. HeaderMouseUp(e.Column, e);
  4404. }
  4405. }
  4406. }
  4407. /// <summary>
  4408. /// Raises a MouseUp event for the Column header at the specified colunm
  4409. /// index position
  4410. /// </summary>
  4411. /// <param name="index">The index of the column to recieve the event</param>
  4412. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  4413. protected void RaiseHeaderMouseUp(int index, MouseEventArgs e)
  4414. {
  4415. if (index < 0 || this.ColumnModel == null || index >= this.ColumnModel.Columns.Count)
  4416. {
  4417. return;
  4418. }
  4419. if (this.HeaderRenderer != null)
  4420. {
  4421. Column column = this.ColumnModel.Columns[index];
  4422. HeaderMouseEventArgs mhea = new HeaderMouseEventArgs(column, this, index, this.DisplayRectToClient(this.ColumnModel.ColumnHeaderRect(index)), e);
  4423. this.OnHeaderMouseUp(mhea);
  4424. }
  4425. }
  4426. #endregion
  4427. #region MouseDown
  4428. /// <summary>
  4429. /// Raises the HeaderMouseDown event
  4430. /// </summary>
  4431. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  4432. protected virtual void OnHeaderMouseDown(HeaderMouseEventArgs e)
  4433. {
  4434. if (this.CanRaiseEvents)
  4435. {
  4436. if (this.HeaderRenderer != null)
  4437. {
  4438. this.HeaderRenderer.OnMouseDown(e);
  4439. }
  4440. if (HeaderMouseDown != null)
  4441. {
  4442. HeaderMouseDown(e.Column, e);
  4443. }
  4444. }
  4445. }
  4446. /// <summary>
  4447. /// Raises a MouseDown event for the Column header at the specified colunm
  4448. /// index position
  4449. /// </summary>
  4450. /// <param name="index">The index of the column to recieve the event</param>
  4451. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  4452. protected void RaiseHeaderMouseDown(int index, MouseEventArgs e)
  4453. {
  4454. if (index < 0 || this.ColumnModel == null || index >= this.ColumnModel.Columns.Count)
  4455. {
  4456. return;
  4457. }
  4458. if (this.HeaderRenderer != null)
  4459. {
  4460. Column column = this.ColumnModel.Columns[index];
  4461. HeaderMouseEventArgs mhea = new HeaderMouseEventArgs(column, this, index, this.DisplayRectToClient(this.ColumnModel.ColumnHeaderRect(index)), e);
  4462. this.OnHeaderMouseDown(mhea);
  4463. }
  4464. }
  4465. #endregion
  4466. #region MouseMove
  4467. /// <summary>
  4468. /// Raises the HeaderMouseMove event
  4469. /// </summary>
  4470. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  4471. protected virtual void OnHeaderMouseMove(HeaderMouseEventArgs e)
  4472. {
  4473. if (this.CanRaiseEvents)
  4474. {
  4475. if (this.HeaderRenderer != null)
  4476. {
  4477. this.HeaderRenderer.OnMouseMove(e);
  4478. }
  4479. if (HeaderMouseMove != null)
  4480. {
  4481. HeaderMouseMove(e.Column, e);
  4482. }
  4483. }
  4484. }
  4485. /// <summary>
  4486. /// Raises a MouseMove event for the Column header at the specified colunm
  4487. /// index position
  4488. /// </summary>
  4489. /// <param name="index">The index of the column to recieve the event</param>
  4490. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  4491. protected void RaiseHeaderMouseMove(int index, MouseEventArgs e)
  4492. {
  4493. if (index < 0 || this.ColumnModel == null || index >= this.ColumnModel.Columns.Count)
  4494. {
  4495. return;
  4496. }
  4497. if (this.HeaderRenderer != null)
  4498. {
  4499. Column column = this.ColumnModel.Columns[index];
  4500. HeaderMouseEventArgs mhea = new HeaderMouseEventArgs(column, this, index, this.DisplayRectToClient(this.ColumnModel.ColumnHeaderRect(index)), e);
  4501. this.OnHeaderMouseMove(mhea);
  4502. }
  4503. }
  4504. /// <summary>
  4505. /// Resets the current "hot" column
  4506. /// </summary>
  4507. internal void ResetHotColumn()
  4508. {
  4509. if (this.hotColumn != -1)
  4510. {
  4511. this.ResetMouseEventArgs();
  4512. int oldHotColumn = this.hotColumn;
  4513. this.hotColumn = -1;
  4514. this.RaiseHeaderMouseLeave(oldHotColumn);
  4515. }
  4516. }
  4517. #endregion
  4518. #region MouseHover
  4519. /// <summary>
  4520. /// Raises the HeaderMouseHover event
  4521. /// </summary>
  4522. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  4523. protected virtual void OnHeaderMouseHover(HeaderMouseEventArgs e)
  4524. {
  4525. if (this.CanRaiseEvents)
  4526. {
  4527. if (HeaderMouseHover != null)
  4528. {
  4529. HeaderMouseHover(e.Column, e);
  4530. }
  4531. }
  4532. }
  4533. #endregion
  4534. #region Click
  4535. /// <summary>
  4536. /// Raises the HeaderClick event
  4537. /// </summary>
  4538. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  4539. protected virtual void OnHeaderClick(HeaderMouseEventArgs e)
  4540. {
  4541. if (this.CanRaiseEvents)
  4542. {
  4543. if (this.HeaderRenderer != null)
  4544. {
  4545. this.HeaderRenderer.OnClick(e);
  4546. }
  4547. if (HeaderClick != null)
  4548. {
  4549. HeaderClick(e.Column, e);
  4550. }
  4551. }
  4552. }
  4553. /// <summary>
  4554. /// Raises the HeaderDoubleClick event
  4555. /// </summary>
  4556. /// <param name="e">A HeaderMouseEventArgs that contains the event data</param>
  4557. protected virtual void OnHeaderDoubleClick(HeaderMouseEventArgs e)
  4558. {
  4559. if (this.CanRaiseEvents)
  4560. {
  4561. if (this.HeaderRenderer != null)
  4562. {
  4563. this.HeaderRenderer.OnDoubleClick(e);
  4564. }
  4565. if (HeaderDoubleClick != null)
  4566. {
  4567. HeaderDoubleClick(e.Column, e);
  4568. }
  4569. }
  4570. }
  4571. #endregion
  4572. #endregion
  4573. #region ColumnModel
  4574. /// <summary>
  4575. /// Raises the ColumnModelChanged event
  4576. /// </summary>
  4577. /// <param name="e">An EventArgs that contains the event data</param>
  4578. protected virtual void OnColumnModelChanged(EventArgs e)
  4579. {
  4580. if (this.CanRaiseEvents)
  4581. {
  4582. this.PerformLayout();
  4583. this.Invalidate();
  4584. if (ColumnModelChanged != null)
  4585. {
  4586. ColumnModelChanged(this, e);
  4587. }
  4588. }
  4589. }
  4590. /// <summary>
  4591. /// Raises the ColumnAdded event
  4592. /// </summary>
  4593. /// <param name="e">A ColumnModelEventArgs that contains the event data</param>
  4594. protected internal virtual void OnColumnAdded(ColumnModelEventArgs e)
  4595. {
  4596. if (this.CanRaiseEvents)
  4597. {
  4598. this.PerformLayout();
  4599. this.Invalidate();
  4600. if (ColumnAdded != null)
  4601. {
  4602. ColumnAdded(this, e);
  4603. }
  4604. }
  4605. }
  4606. /// <summary>
  4607. /// Raises the ColumnRemoved event
  4608. /// </summary>
  4609. /// <param name="e">A ColumnModelEventArgs that contains the event data</param>
  4610. protected internal virtual void OnColumnRemoved(ColumnModelEventArgs e)
  4611. {
  4612. if (this.CanRaiseEvents)
  4613. {
  4614. this.PerformLayout();
  4615. this.Invalidate();
  4616. if (ColumnRemoved != null)
  4617. {
  4618. ColumnRemoved(this, e);
  4619. }
  4620. }
  4621. }
  4622. /// <summary>
  4623. /// Raises the HeaderHeightChanged event
  4624. /// </summary>
  4625. /// <param name="e">An EventArgs that contains the event data</param>
  4626. protected internal virtual void OnHeaderHeightChanged(EventArgs e)
  4627. {
  4628. if (this.CanRaiseEvents)
  4629. {
  4630. this.PerformLayout();
  4631. this.Invalidate();
  4632. if (HeaderHeightChanged != null)
  4633. {
  4634. HeaderHeightChanged(this, e);
  4635. }
  4636. }
  4637. }
  4638. #endregion
  4639. #region Editing
  4640. /// <summary>
  4641. /// Raises the BeginEditing event
  4642. /// </summary>
  4643. /// <param name="e">A CellEditEventArgs that contains the event data</param>
  4644. protected internal virtual void OnBeginEditing(CellEditEventArgs e)
  4645. {
  4646. if (this.CanRaiseEvents)
  4647. {
  4648. if (BeginEditing != null)
  4649. {
  4650. BeginEditing(e.Cell, e);
  4651. }
  4652. }
  4653. }
  4654. /// <summary>
  4655. /// Raises the EditingStopped event
  4656. /// </summary>
  4657. /// <param name="e">A CellEditEventArgs that contains the event data</param>
  4658. protected internal virtual void OnEditingStopped(CellEditEventArgs e)
  4659. {
  4660. if (this.CanRaiseEvents)
  4661. {
  4662. if (EditingStopped != null)
  4663. {
  4664. EditingStopped(e.Cell, e);
  4665. }
  4666. }
  4667. }
  4668. /// <summary>
  4669. /// Raises the EditingCancelled event
  4670. /// </summary>
  4671. /// <param name="e">A CellEditEventArgs that contains the event data</param>
  4672. protected internal virtual void OnEditingCancelled(CellEditEventArgs e)
  4673. {
  4674. if (this.CanRaiseEvents)
  4675. {
  4676. if (EditingCancelled != null)
  4677. {
  4678. EditingCancelled(e.Cell, e);
  4679. }
  4680. }
  4681. }
  4682. #endregion
  4683. #region Focus
  4684. /// <summary>
  4685. /// Raises the GotFocus event
  4686. /// </summary>
  4687. /// <param name="e">An EventArgs that contains the event data</param>
  4688. protected override void OnGotFocus(EventArgs e)
  4689. {
  4690. if (this.FocusedCell.IsEmpty)
  4691. {
  4692. CellPos p = this.FindNextVisibleEnabledCell(this.FocusedCell, true, true, true, true);
  4693. if (this.IsValidCell(p))
  4694. {
  4695. this.FocusedCell = p;
  4696. }
  4697. }
  4698. else
  4699. {
  4700. this.RaiseCellGotFocus(this.FocusedCell);
  4701. }
  4702. if (this.SelectedIndicies.Length > 0)
  4703. {
  4704. this.Invalidate(this.CellDataRect);
  4705. }
  4706. base.OnGotFocus(e);
  4707. }
  4708. /// <summary>
  4709. /// Raises the LostFocus event
  4710. /// </summary>
  4711. /// <param name="e">An EventArgs that contains the event data</param>
  4712. protected override void OnLostFocus(EventArgs e)
  4713. {
  4714. if (!this.FocusedCell.IsEmpty)
  4715. {
  4716. this.RaiseCellLostFocus(this.FocusedCell);
  4717. }
  4718. if (this.SelectedIndicies.Length > 0)
  4719. {
  4720. this.Invalidate(this.CellDataRect);
  4721. }
  4722. base.OnLostFocus(e);
  4723. }
  4724. #endregion
  4725. #region Keys
  4726. #region KeyDown
  4727. /// <summary>
  4728. /// Raises the KeyDown event
  4729. /// </summary>
  4730. /// <param name="e">A KeyEventArgs that contains the event data</param>
  4731. protected override void OnKeyDown(KeyEventArgs e)
  4732. {
  4733. base.OnKeyDown(e);
  4734. if (this.IsValidCell(this.FocusedCell))
  4735. {
  4736. if (this.IsReservedKey(e.KeyData))
  4737. {
  4738. Keys key = e.KeyData & Keys.KeyCode;
  4739. if (key == Keys.Up || key == Keys.Down || key == Keys.Left || key == Keys.Right)
  4740. {
  4741. CellPos nextCell;
  4742. if (key == Keys.Up)
  4743. {
  4744. nextCell = this.FindNextVisibleEnabledCell(this.FocusedCell, this.FocusedCell.Row > 0, false, false, false);
  4745. }
  4746. else if (key == Keys.Down)
  4747. {
  4748. nextCell = this.FindNextVisibleEnabledCell(this.FocusedCell, this.FocusedCell.Row < this.RowCount - 1, true, false, false);
  4749. }
  4750. else if (key == Keys.Left)
  4751. {
  4752. nextCell = this.FindNextVisibleEnabledCell(this.FocusedCell, false, false, false, true);
  4753. }
  4754. else
  4755. {
  4756. nextCell = this.FindNextVisibleEnabledCell(this.FocusedCell, false, true, false, true);
  4757. }
  4758. if (nextCell != CellPos.Empty)
  4759. {
  4760. this.FocusedCell = nextCell;
  4761. if ((e.KeyData & Keys.Modifiers) == Keys.Shift && this.MultiSelect)
  4762. {
  4763. this.TableModel.Selections.AddShiftSelectedCell(this.FocusedCell);
  4764. }
  4765. else
  4766. {
  4767. this.TableModel.Selections.SelectCell(this.FocusedCell);
  4768. }
  4769. }
  4770. }
  4771. else if (e.KeyData == Keys.PageUp)
  4772. {
  4773. if (this.RowCount > 0)
  4774. {
  4775. CellPos nextCell;
  4776. if (!this.VScroll)
  4777. {
  4778. nextCell = this.FindNextVisibleEnabledCell(new CellPos(0, this.FocusedCell.Column), true, true, true, false);
  4779. }
  4780. else
  4781. {
  4782. if (this.FocusedCell.Row > this.vScrollBar.Value && this.TableModel[this.vScrollBar.Value, this.FocusedCell.Column].Enabled)
  4783. {
  4784. nextCell = this.FindNextVisibleEnabledCell(new CellPos(this.vScrollBar.Value, this.FocusedCell.Column), true, true, true, false);
  4785. }
  4786. else
  4787. {
  4788. nextCell = this.FindNextVisibleEnabledCell(new CellPos(Math.Max(-1, this.vScrollBar.Value - (this.vScrollBar.LargeChange - 1)), this.FocusedCell.Column), true, true, true, false);
  4789. }
  4790. }
  4791. if (nextCell != CellPos.Empty)
  4792. {
  4793. this.FocusedCell = nextCell;
  4794. this.TableModel.Selections.SelectCell(this.FocusedCell);
  4795. }
  4796. }
  4797. }
  4798. else if (e.KeyData == Keys.PageDown)
  4799. {
  4800. if (this.RowCount > 0)
  4801. {
  4802. CellPos nextCell;
  4803. if (!this.VScroll)
  4804. {
  4805. nextCell = this.FindNextVisibleEnabledCell(new CellPos(this.RowCount - 1, this.FocusedCell.Column), true, false, true, false);
  4806. }
  4807. else
  4808. {
  4809. if (this.FocusedCell.Row < this.vScrollBar.Value + this.vScrollBar.LargeChange)
  4810. {
  4811. if (this.FocusedCell.Row == (this.vScrollBar.Value + this.vScrollBar.LargeChange) - 1 &&
  4812. this.RowRect(this.vScrollBar.Value + this.vScrollBar.LargeChange).Bottom > this.CellDataRect.Bottom)
  4813. {
  4814. nextCell = this.FindNextVisibleEnabledCell(new CellPos(Math.Min(this.RowCount - 1, this.FocusedCell.Row - 1 + this.vScrollBar.LargeChange), this.FocusedCell.Column), true, false, true, false);
  4815. }
  4816. else
  4817. {
  4818. nextCell = this.FindNextVisibleEnabledCell(new CellPos(this.vScrollBar.Value + this.vScrollBar.LargeChange - 1, this.FocusedCell.Column), true, false, true, false);
  4819. }
  4820. }
  4821. else
  4822. {
  4823. nextCell = this.FindNextVisibleEnabledCell(new CellPos(Math.Min(this.RowCount - 1, this.FocusedCell.Row + this.vScrollBar.LargeChange), this.FocusedCell.Column), true, false, true, false);
  4824. }
  4825. }
  4826. if (nextCell != CellPos.Empty)
  4827. {
  4828. this.FocusedCell = nextCell;
  4829. this.TableModel.Selections.SelectCell(this.FocusedCell);
  4830. }
  4831. }
  4832. }
  4833. else if (e.KeyData == Keys.Home || e.KeyData == Keys.End)
  4834. {
  4835. if (this.RowCount > 0)
  4836. {
  4837. CellPos nextCell;
  4838. if (e.KeyData == Keys.Home)
  4839. {
  4840. nextCell = this.FindNextVisibleEnabledCell(CellPos.Empty, true, true, true, true);
  4841. }
  4842. else
  4843. {
  4844. nextCell = this.FindNextVisibleEnabledCell(new CellPos(this.RowCount-1, this.TableModel.Rows[this.RowCount-1].Cells.Count), true, false, true, true);
  4845. }
  4846. if (nextCell != CellPos.Empty)
  4847. {
  4848. this.FocusedCell = nextCell;
  4849. this.TableModel.Selections.SelectCell(this.FocusedCell);
  4850. }
  4851. }
  4852. }
  4853. }
  4854. else
  4855. {
  4856. // check if we can start editing with the custom edit key
  4857. if (e.KeyData == this.CustomEditKey && this.EditStartAction == EditStartAction.CustomKey)
  4858. {
  4859. this.EditCell(this.FocusedCell);
  4860. return;
  4861. }
  4862. // send all other key events to the cell's renderer
  4863. // for further processing
  4864. this.RaiseCellKeyDown(this.FocusedCell, e);
  4865. }
  4866. }
  4867. else
  4868. {
  4869. if (this.FocusedCell == CellPos.Empty)
  4870. {
  4871. Keys key = e.KeyData & Keys.KeyCode;
  4872. if (this.IsReservedKey(e.KeyData))
  4873. {
  4874. if (key == Keys.Down || key == Keys.Right)
  4875. {
  4876. CellPos nextCell;
  4877. if (key == Keys.Down)
  4878. {
  4879. nextCell = this.FindNextVisibleEnabledCell(this.FocusedCell, true, true, true, false);
  4880. }
  4881. else
  4882. {
  4883. nextCell = this.FindNextVisibleEnabledCell(this.FocusedCell, false, true, true, true);
  4884. }
  4885. if (nextCell != CellPos.Empty)
  4886. {
  4887. this.FocusedCell = nextCell;
  4888. if ((e.KeyData & Keys.Modifiers) == Keys.Shift && this.MultiSelect)
  4889. {
  4890. this.TableModel.Selections.AddShiftSelectedCell(this.FocusedCell);
  4891. }
  4892. else
  4893. {
  4894. this.TableModel.Selections.SelectCell(this.FocusedCell);
  4895. }
  4896. }
  4897. }
  4898. }
  4899. }
  4900. }
  4901. }
  4902. #endregion
  4903. #region KeyUp
  4904. /// <summary>
  4905. /// Raises the KeyUp event
  4906. /// </summary>
  4907. /// <param name="e">A KeyEventArgs that contains the event data</param>
  4908. protected override void OnKeyUp(KeyEventArgs e)
  4909. {
  4910. base.OnKeyUp(e);
  4911. if (!this.IsReservedKey(e.KeyData))
  4912. {
  4913. //
  4914. if (e.KeyData == this.CustomEditKey && this.EditStartAction == EditStartAction.CustomKey)
  4915. {
  4916. return;
  4917. }
  4918. // send all other key events to the cell's renderer
  4919. // for further processing
  4920. this.RaiseCellKeyUp(this.FocusedCell, e);
  4921. }
  4922. }
  4923. #endregion
  4924. #endregion
  4925. #region Layout
  4926. /// <summary>
  4927. /// Raises the Layout event
  4928. /// </summary>
  4929. /// <param name="levent">A LayoutEventArgs that contains the event data</param>
  4930. protected override void OnLayout(LayoutEventArgs levent)
  4931. {
  4932. if (!this.IsHandleCreated || this.init)
  4933. {
  4934. return;
  4935. }
  4936. base.OnLayout(levent);
  4937. this.UpdateScrollBars();
  4938. }
  4939. #endregion
  4940. #region Mouse
  4941. #region MouseUp
  4942. /// <summary>
  4943. /// Raises the MouseUp event
  4944. /// </summary>
  4945. /// <param name="e">A MouseEventArgs that contains the event data</param>
  4946. protected override void OnMouseUp(MouseEventArgs e)
  4947. {
  4948. base.OnMouseUp(e);
  4949. if (!this.CanRaiseEvents)
  4950. {
  4951. return;
  4952. }
  4953. // work out the current state of play
  4954. this.CalcTableState(e.X, e.Y);
  4955. TableRegion region = this.HitTest(e.X, e.Y);
  4956. if (e.Button == MouseButtons.Left)
  4957. {
  4958. // if the left mouse button was down for a cell,
  4959. // Raise a mouse up for that cell
  4960. if (!this.LastMouseDownCell.IsEmpty)
  4961. {
  4962. if (this.IsValidCell(this.LastMouseDownCell))
  4963. {
  4964. this.RaiseCellMouseUp(this.LastMouseDownCell, e);
  4965. }
  4966. // reset the lastMouseDownCell
  4967. this.lastMouseDownCell = CellPos.Empty;
  4968. }
  4969. // if we have just finished resizing, it might
  4970. // be a good idea to relayout the table
  4971. if (this.resizingColumnIndex != -1)
  4972. {
  4973. if (this.resizingColumnWidth != -1)
  4974. {
  4975. this.DrawReversibleLine(this.ColumnRect(this.resizingColumnIndex).Left + this.resizingColumnWidth);
  4976. }
  4977. this.ColumnModel.Columns[this.resizingColumnIndex].Width = this.resizingColumnWidth;
  4978. this.resizingColumnIndex = -1;
  4979. this.resizingColumnWidth = -1;
  4980. this.UpdateScrollBars();
  4981. this.Invalidate(this.PseudoClientRect, true);
  4982. }
  4983. // check if the mouse was released in a column header
  4984. if (region == TableRegion.ColumnHeader)
  4985. {
  4986. int column = this.ColumnIndexAt(e.X, e.Y);
  4987. // if we are in the header, check if we are in the pressed column
  4988. if (this.pressedColumn != -1)
  4989. {
  4990. if (this.pressedColumn == column)
  4991. {
  4992. if (this.hotColumn != -1 && this.hotColumn != column)
  4993. {
  4994. this.ColumnModel.Columns[this.hotColumn].InternalColumnState = ColumnState.Normal;
  4995. }
  4996. this.ColumnModel.Columns[this.pressedColumn].InternalColumnState = ColumnState.Hot;
  4997. this.RaiseHeaderMouseUp(column, e);
  4998. }
  4999. this.pressedColumn = -1;
  5000. // only sort the column if we have rows to sort
  5001. if (this.ColumnModel.Columns[column].Sortable)
  5002. {
  5003. if (this.TableModel != null && this.TableModel.Rows.Count > 0)
  5004. {
  5005. this.Sort(column);
  5006. }
  5007. }
  5008. this.Invalidate(this.HeaderRectangle, false);
  5009. }
  5010. return;
  5011. }
  5012. // the mouse wasn't released in a column header, so if we
  5013. // have a pressed column then we need to make it unpressed
  5014. if (this.pressedColumn != -1)
  5015. {
  5016. this.pressedColumn = -1;
  5017. this.Invalidate(this.HeaderRectangle, false);
  5018. }
  5019. }
  5020. }
  5021. #endregion
  5022. #region MouseDown
  5023. /// <summary>
  5024. /// Raises the MouseDown event
  5025. /// </summary>
  5026. /// <param name="e">A MouseEventArgs that contains the event data</param>
  5027. protected override void OnMouseDown(MouseEventArgs e)
  5028. {
  5029. base.OnMouseDown(e);
  5030. if (!this.CanRaiseEvents)
  5031. {
  5032. return;
  5033. }
  5034. this.CalcTableState(e.X, e.Y);
  5035. TableRegion region = this.HitTest(e.X, e.Y);
  5036. int row = this.RowIndexAt(e.X, e.Y);
  5037. int column = this.ColumnIndexAt(e.X, e.Y);
  5038. if (this.IsEditing)
  5039. {
  5040. if (this.EditingCell.Row != row || this.EditingCell.Column != column)
  5041. {
  5042. this.Focus();
  5043. if (region == TableRegion.ColumnHeader && e.Button != MouseButtons.Right)
  5044. {
  5045. return;
  5046. }
  5047. }
  5048. }
  5049. #region ColumnHeader
  5050. if (region == TableRegion.ColumnHeader)
  5051. {
  5052. if (e.Button == MouseButtons.Right && this.HeaderContextMenu.Enabled)
  5053. {
  5054. this.HeaderContextMenu.Show(this, new Point(e.X, e.Y));
  5055. return;
  5056. }
  5057. if (column == -1 || !this.ColumnModel.Columns[column].Enabled)
  5058. {
  5059. return;
  5060. }
  5061. if (e.Button == MouseButtons.Left)
  5062. {
  5063. this.FocusedCell = new CellPos(-1, -1);
  5064. // don't bother going any further if the user
  5065. // double clicked
  5066. if (e.Clicks > 1)
  5067. {
  5068. return;
  5069. }
  5070. this.RaiseHeaderMouseDown(column, e);
  5071. if (this.TableState == TableState.ColumnResizing)
  5072. {
  5073. Rectangle columnRect = this.ColumnModel.ColumnHeaderRect(column);
  5074. int x = this.ClientToDisplayRect(e.X, e.Y).X;
  5075. if (x <= columnRect.Left + Column.ResizePadding)
  5076. {
  5077. //column--;
  5078. column = this.ColumnModel.PreviousVisibleColumn(column);
  5079. }
  5080. this.resizingColumnIndex = column;
  5081. if (this.resizingColumnIndex != -1)
  5082. {
  5083. this.resizingColumnAnchor = this.ColumnModel.ColumnHeaderRect(column).Left;
  5084. this.resizingColumnOffset = x - (this.resizingColumnAnchor + this.ColumnModel.Columns[column].Width);
  5085. }
  5086. }
  5087. else
  5088. {
  5089. if (this.HeaderStyle != ColumnHeaderStyle.Clickable || !this.ColumnModel.Columns[column].Sortable)
  5090. {
  5091. return;
  5092. }
  5093. if (column == -1)
  5094. {
  5095. return;
  5096. }
  5097. if (this.pressedColumn != -1)
  5098. {
  5099. this.ColumnModel.Columns[this.pressedColumn].InternalColumnState = ColumnState.Normal;
  5100. }
  5101. this.pressedColumn = column;
  5102. this.ColumnModel.Columns[column].InternalColumnState = ColumnState.Pressed;
  5103. }
  5104. return;
  5105. }
  5106. }
  5107. #endregion
  5108. #region Cells
  5109. if (region == TableRegion.Cells)
  5110. {
  5111. if (e.Button != MouseButtons.Left && e.Button != MouseButtons.Right)
  5112. {
  5113. return;
  5114. }
  5115. if (!this.IsValidCell(row, column) || !this.IsCellEnabled(row, column))
  5116. {
  5117. // clear selections
  5118. this.TableModel.Selections.Clear();
  5119. return;
  5120. }
  5121. this.FocusedCell = new CellPos(row, column);
  5122. // don't bother going any further if the user
  5123. // double clicked or we're not allowed to select
  5124. if (e.Clicks > 1 || !this.AllowSelection)
  5125. {
  5126. return;
  5127. }
  5128. this.lastMouseDownCell.Row = row;
  5129. this.lastMouseDownCell.Column = column;
  5130. //
  5131. this.RaiseCellMouseDown(new CellPos(row, column), e);
  5132. if (!this.ColumnModel.Columns[column].Selectable)
  5133. {
  5134. return;
  5135. }
  5136. //
  5137. if ((ModifierKeys & Keys.Shift) == Keys.Shift && this.MultiSelect)
  5138. {
  5139. if (e.Button == MouseButtons.Right)
  5140. {
  5141. return;
  5142. }
  5143. this.TableModel.Selections.AddShiftSelectedCell(row, column);
  5144. return;
  5145. }
  5146. if ((ModifierKeys & Keys.Control) == Keys.Control && this.MultiSelect)
  5147. {
  5148. if (e.Button == MouseButtons.Right)
  5149. {
  5150. return;
  5151. }
  5152. if (this.TableModel.Selections.IsCellSelected(row, column))
  5153. {
  5154. this.TableModel.Selections.RemoveCell(row, column);
  5155. }
  5156. else
  5157. {
  5158. this.TableModel.Selections.AddCell(row, column);
  5159. }
  5160. return;
  5161. }
  5162. this.TableModel.Selections.SelectCell(row, column);
  5163. }
  5164. #endregion
  5165. }
  5166. #endregion
  5167. #region MouseMove
  5168. /// <summary>
  5169. /// Raises the MouseMove event
  5170. /// </summary>
  5171. /// <param name="e">A MouseEventArgs that contains the event data</param>
  5172. protected override void OnMouseMove(MouseEventArgs e)
  5173. {
  5174. base.OnMouseMove(e);
  5175. // don't go any further if the table is editing
  5176. if (this.TableState == TableState.Editing)
  5177. {
  5178. return;
  5179. }
  5180. // if the left mouse button is down, check if the LastMouseDownCell
  5181. // references a valid cell. if it does, send the mouse move message
  5182. // to the cell and then exit (this will stop other cells/headers
  5183. // from getting the mouse move message even if the mouse is over
  5184. // them - this seems consistent with the way windows does it for
  5185. // other controls)
  5186. if (e.Button == MouseButtons.Left)
  5187. {
  5188. if (!this.LastMouseDownCell.IsEmpty)
  5189. {
  5190. if (this.IsValidCell(this.LastMouseDownCell))
  5191. {
  5192. this.RaiseCellMouseMove(this.LastMouseDownCell, e);
  5193. return;
  5194. }
  5195. }
  5196. }
  5197. // are we resizing a column?
  5198. if (this.resizingColumnIndex != -1)
  5199. {
  5200. if (this.resizingColumnWidth != -1)
  5201. {
  5202. this.DrawReversibleLine(this.ColumnRect(this.resizingColumnIndex).Left + this.resizingColumnWidth);
  5203. }
  5204. // calculate the new width for the column
  5205. int width = this.ClientToDisplayRect(e.X, e.Y).X - this.resizingColumnAnchor - this.resizingColumnOffset;
  5206. // make sure the new width isn't smaller than the minimum allowed
  5207. // column width, or larger than the maximum allowed column width
  5208. if (width < Column.MinimumWidth)
  5209. {
  5210. width = Column.MinimumWidth;
  5211. }
  5212. else if (width > Column.MaximumWidth)
  5213. {
  5214. width = Column.MaximumWidth;
  5215. }
  5216. this.resizingColumnWidth = width;
  5217. //this.ColumnModel.Columns[this.resizingColumnIndex].Width = width;
  5218. this.DrawReversibleLine(this.ColumnRect(this.resizingColumnIndex).Left + this.resizingColumnWidth);
  5219. return;
  5220. }
  5221. // work out the potential state of play
  5222. this.CalcTableState(e.X, e.Y);
  5223. TableRegion hitTest = this.HitTest(e.X, e.Y);
  5224. #region ColumnHeader
  5225. if (hitTest == TableRegion.ColumnHeader)
  5226. {
  5227. // this next bit is pretty complicated. need to work
  5228. // out which column is displayed as pressed or hot
  5229. // (so we have the same behaviour as a themed ListView
  5230. // in Windows XP)
  5231. int column = this.ColumnIndexAt(e.X, e.Y);
  5232. // if this isn't the current hot column, reset the
  5233. // hot columns state to normal and set this column
  5234. // to be the hot column
  5235. if (this.hotColumn != column)
  5236. {
  5237. if (this.hotColumn != -1)
  5238. {
  5239. this.ColumnModel.Columns[this.hotColumn].InternalColumnState = ColumnState.Normal;
  5240. this.RaiseHeaderMouseLeave(this.hotColumn);
  5241. }
  5242. if (this.TableState != TableState.ColumnResizing)
  5243. {
  5244. this.hotColumn = column;
  5245. if (this.hotColumn != -1 && this.ColumnModel.Columns[column].Enabled)
  5246. {
  5247. this.ColumnModel.Columns[column].InternalColumnState = ColumnState.Hot;
  5248. this.RaiseHeaderMouseEnter(column);
  5249. }
  5250. }
  5251. }
  5252. else
  5253. {
  5254. if (column != -1 && this.ColumnModel.Columns[column].Enabled)
  5255. {
  5256. this.RaiseHeaderMouseMove(column, e);
  5257. }
  5258. }
  5259. // if this isn't the pressed column, then the pressed columns
  5260. // state should be set back to normal
  5261. if (this.pressedColumn != -1 && this.pressedColumn != column)
  5262. {
  5263. this.ColumnModel.Columns[this.pressedColumn].InternalColumnState = ColumnState.Normal;
  5264. }
  5265. // else if this is the pressed column and its state is not
  5266. // pressed, then we had better set it
  5267. else if (column != -1 && this.pressedColumn == column && this.ColumnModel.Columns[this.pressedColumn].ColumnState != ColumnState.Pressed)
  5268. {
  5269. this.ColumnModel.Columns[this.pressedColumn].InternalColumnState = ColumnState.Pressed;
  5270. }
  5271. // set the cursor to a resizing cursor if necesary
  5272. if (this.TableState == TableState.ColumnResizing)
  5273. {
  5274. Rectangle columnRect = this.ColumnModel.ColumnHeaderRect(column);
  5275. int x = this.ClientToDisplayRect(e.X, e.Y).X;
  5276. this.Cursor = Cursors.VSplit;
  5277. // if the left mouse button is down, we don't want
  5278. // the resizing cursor so set it back to the default
  5279. if (e.Button == MouseButtons.Left)
  5280. {
  5281. this.Cursor = Cursors.Default;
  5282. }
  5283. // if the mouse is in the left side of the column,
  5284. // the first non-hidden column to the left needs to
  5285. // become the hot column (so the user knows which
  5286. // column would be resized if a resize action were
  5287. // to take place
  5288. if (x < columnRect.Left + Column.ResizePadding)
  5289. {
  5290. int col = column;
  5291. while (col != 0)
  5292. {
  5293. col--;
  5294. if (this.ColumnModel.Columns[col].Visible)
  5295. {
  5296. break;
  5297. }
  5298. }
  5299. if (col != -1)
  5300. {
  5301. if (this.ColumnModel.Columns[col].Enabled)
  5302. {
  5303. if (this.hotColumn != -1)
  5304. {
  5305. this.ColumnModel.Columns[this.hotColumn].InternalColumnState = ColumnState.Normal;
  5306. }
  5307. this.hotColumn = col;
  5308. this.ColumnModel.Columns[this.hotColumn].InternalColumnState = ColumnState.Hot;
  5309. this.RaiseHeaderMouseEnter(col);
  5310. }
  5311. else
  5312. {
  5313. this.Cursor = Cursors.Default;
  5314. }
  5315. }
  5316. }
  5317. else
  5318. {
  5319. if (this.ColumnModel.Columns[column].Enabled)
  5320. {
  5321. // this mouse is in the right side of the column,
  5322. // so this column needs to be dsiplayed hot
  5323. this.hotColumn = column;
  5324. this.ColumnModel.Columns[this.hotColumn].InternalColumnState = ColumnState.Hot;
  5325. }
  5326. else
  5327. {
  5328. this.Cursor = Cursors.Default;
  5329. }
  5330. }
  5331. }
  5332. else
  5333. {
  5334. // we're not in a resizing area, so make sure the cursor
  5335. // is the default cursor (we may have just come from a
  5336. // resizing area)
  5337. this.Cursor = Cursors.Default;
  5338. }
  5339. // reset the last cell the mouse was over
  5340. this.ResetLastMouseCell();
  5341. return;
  5342. }
  5343. #endregion
  5344. // we're outside of the header, so if there is a hot column,
  5345. // it need to be reset
  5346. if (this.hotColumn != -1)
  5347. {
  5348. this.ColumnModel.Columns[this.hotColumn].InternalColumnState = ColumnState.Normal;
  5349. this.ResetHotColumn();
  5350. }
  5351. // if there is a pressed column, its state need to beset to normal
  5352. if (this.pressedColumn != -1)
  5353. {
  5354. this.ColumnModel.Columns[this.pressedColumn].InternalColumnState = ColumnState.Normal;
  5355. }
  5356. #region Cells
  5357. if (hitTest == TableRegion.Cells)
  5358. {
  5359. // find the cell the mouse is over
  5360. CellPos cellPos = new CellPos(this.RowIndexAt(e.X, e.Y), this.ColumnIndexAt(e.X, e.Y));
  5361. if (!cellPos.IsEmpty)
  5362. {
  5363. if (cellPos != this.lastMouseCell)
  5364. {
  5365. // check if the cell exists (ie is not null)
  5366. if (this.IsValidCell(cellPos))
  5367. {
  5368. CellPos oldLastMouseCell = this.lastMouseCell;
  5369. if (!oldLastMouseCell.IsEmpty)
  5370. {
  5371. this.ResetLastMouseCell();
  5372. }
  5373. this.lastMouseCell = cellPos;
  5374. this.RaiseCellMouseEnter(cellPos);
  5375. }
  5376. else
  5377. {
  5378. this.ResetLastMouseCell();
  5379. // make sure the cursor is the default cursor
  5380. // (we may have just come from a resizing area in the header)
  5381. this.Cursor = Cursors.Default;
  5382. }
  5383. }
  5384. else
  5385. {
  5386. this.RaiseCellMouseMove(cellPos, e);
  5387. }
  5388. }
  5389. else
  5390. {
  5391. this.ResetLastMouseCell();
  5392. if (this.TableModel == null)
  5393. {
  5394. this.ResetToolTip();
  5395. }
  5396. }
  5397. return;
  5398. }
  5399. else
  5400. {
  5401. this.ResetLastMouseCell();
  5402. if (!this.lastMouseDownCell.IsEmpty)
  5403. {
  5404. this.RaiseCellMouseLeave(this.lastMouseDownCell);
  5405. }
  5406. if (this.TableModel == null)
  5407. {
  5408. this.ResetToolTip();
  5409. }
  5410. // make sure the cursor is the default cursor
  5411. // (we may have just come from a resizing area in the header)
  5412. this.Cursor = Cursors.Default;
  5413. }
  5414. #endregion
  5415. }
  5416. #endregion
  5417. #region MouseLeave
  5418. /// <summary>
  5419. /// Raises the MouseLeave event
  5420. /// </summary>
  5421. /// <param name="e">An EventArgs that contains the event data</param>
  5422. protected override void OnMouseLeave(EventArgs e)
  5423. {
  5424. base.OnMouseLeave(e);
  5425. // we're outside of the header, so if there is a hot column,
  5426. // it needs to be reset (this shouldn't happen, but better
  5427. // safe than sorry ;)
  5428. if (this.hotColumn != -1)
  5429. {
  5430. this.ColumnModel.Columns[this.hotColumn].InternalColumnState = ColumnState.Normal;
  5431. this.ResetHotColumn();
  5432. }
  5433. }
  5434. #endregion
  5435. #region MouseWheel
  5436. /// <summary>
  5437. /// Raises the MouseWheel event
  5438. /// </summary>
  5439. /// <param name="e">A MouseEventArgs that contains the event data</param>
  5440. protected override void OnMouseWheel(MouseEventArgs e)
  5441. {
  5442. base.OnMouseWheel(e);
  5443. if (!this.Scrollable || (!this.HScroll && !this.VScroll))
  5444. {
  5445. return;
  5446. }
  5447. if (this.VScroll)
  5448. {
  5449. int newVal = this.vScrollBar.Value - ((e.Delta / 120) * SystemInformation.MouseWheelScrollLines);
  5450. if (newVal < 0)
  5451. {
  5452. newVal = 0;
  5453. }
  5454. else if (newVal > this.vScrollBar.Maximum - this.vScrollBar.LargeChange + 1)
  5455. {
  5456. newVal = this.vScrollBar.Maximum - this.vScrollBar.LargeChange + 1;
  5457. }
  5458. this.VerticalScroll(newVal);
  5459. this.vScrollBar.Value = newVal;
  5460. }
  5461. else if (this.HScroll)
  5462. {
  5463. int newVal = this.hScrollBar.Value - ((e.Delta / 120) * Column.MinimumWidth);
  5464. if (newVal < 0)
  5465. {
  5466. newVal = 0;
  5467. }
  5468. else if (newVal > this.hScrollBar.Maximum - this.hScrollBar.LargeChange)
  5469. {
  5470. newVal = this.hScrollBar.Maximum - this.hScrollBar.LargeChange;
  5471. }
  5472. this.HorizontalScroll(newVal);
  5473. this.hScrollBar.Value = newVal;
  5474. }
  5475. }
  5476. #endregion
  5477. #region MouseHover
  5478. /// <summary>
  5479. /// Raises the MouseHover event
  5480. /// </summary>
  5481. /// <param name="e">An EventArgs that contains the event data</param>
  5482. protected override void OnMouseHover(EventArgs e)
  5483. {
  5484. base.OnMouseHover(e);
  5485. if (this.IsValidCell(this.LastMouseCell))
  5486. {
  5487. this.OnCellMouseHover(new CellMouseEventArgs(this.TableModel[this.LastMouseCell], this, this.LastMouseCell, this.CellRect(this.LastMouseCell)));
  5488. }
  5489. else if (this.hotColumn != -1)
  5490. {
  5491. this.OnHeaderMouseHover(new HeaderMouseEventArgs(this.ColumnModel.Columns[this.hotColumn], this, this.hotColumn, this.DisplayRectToClient(this.ColumnModel.ColumnHeaderRect(this.hotColumn))));
  5492. }
  5493. }
  5494. #endregion
  5495. #region Click
  5496. /// <summary>
  5497. /// Raises the Click event
  5498. /// </summary>
  5499. /// <param name="e">An EventArgs that contains the event data</param>
  5500. protected override void OnClick(EventArgs e)
  5501. {
  5502. base.OnClick(e);
  5503. if (this.IsValidCell(this.LastMouseCell))
  5504. {
  5505. this.OnCellClick(new CellMouseEventArgs(this.TableModel[this.LastMouseCell], this, this.LastMouseCell, this.CellRect(this.LastMouseCell)));
  5506. }
  5507. else if (this.hotColumn != -1)
  5508. {
  5509. this.OnHeaderClick(new HeaderMouseEventArgs(this.ColumnModel.Columns[this.hotColumn], this, this.hotColumn, this.DisplayRectToClient(this.ColumnModel.ColumnHeaderRect(this.hotColumn))));
  5510. }
  5511. }
  5512. /// <summary>
  5513. /// Raises the DoubleClick event
  5514. /// </summary>
  5515. /// <param name="e">An EventArgs that contains the event data</param>
  5516. protected override void OnDoubleClick(EventArgs e)
  5517. {
  5518. base.OnDoubleClick(e);
  5519. if (this.IsValidCell(this.LastMouseCell))
  5520. {
  5521. Rectangle cellRect = this.CellRect(this.LastMouseCell);
  5522. this.OnCellDoubleClick(new CellMouseEventArgs(this.TableModel[this.LastMouseCell], this, this.LastMouseCell, this.CellRect(this.LastMouseCell)));
  5523. }
  5524. else if (this.hotColumn != -1)
  5525. {
  5526. this.OnHeaderDoubleClick(new HeaderMouseEventArgs(this.ColumnModel.Columns[this.hotColumn], this, this.hotColumn, this.DisplayRectToClient(this.ColumnModel.ColumnHeaderRect(this.hotColumn))));
  5527. }
  5528. }
  5529. #endregion
  5530. #endregion
  5531. #region Paint
  5532. /// <summary>
  5533. /// Raises the PaintBackground event
  5534. /// </summary>
  5535. /// <param name="e">A PaintEventArgs that contains the event data</param>
  5536. protected override void OnPaintBackground(PaintEventArgs e)
  5537. {
  5538. base.OnPaintBackground(e);
  5539. }
  5540. /// <summary>
  5541. /// Raises the Paint event
  5542. /// </summary>
  5543. /// <param name="e">A PaintEventArgs that contains the event data</param>
  5544. protected override void OnPaint(PaintEventArgs e)
  5545. {
  5546. // we'll do our own painting thanks
  5547. //base.OnPaint(e);
  5548. // check if we actually need to paint
  5549. if (this.Width == 0 || this.Height == 0)
  5550. {
  5551. return;
  5552. }
  5553. if (this.ColumnModel != null)
  5554. {
  5555. // keep a record of the current clip region
  5556. Region clip = e.Graphics.Clip;
  5557. if (this.TableModel != null && this.TableModel.Rows.Count > 0)
  5558. {
  5559. this.OnPaintRows(e);
  5560. // reset the clipping region
  5561. e.Graphics.Clip = clip;
  5562. }
  5563. if (this.GridLines != GridLines.None)
  5564. {
  5565. this.OnPaintGrid(e);
  5566. }
  5567. if (this.HeaderStyle != ColumnHeaderStyle.None && this.ColumnModel.Columns.Count > 0)
  5568. {
  5569. if (this.HeaderRectangle.IntersectsWith(e.ClipRectangle))
  5570. {
  5571. this.OnPaintHeader(e);
  5572. }
  5573. }
  5574. // reset the clipping region
  5575. e.Graphics.Clip = clip;
  5576. }
  5577. this.OnPaintEmptyTableText(e);
  5578. this.OnPaintBorder(e);
  5579. }
  5580. /// <summary>
  5581. /// Draws a reversible line at the specified screen x-coordinate
  5582. /// that is the height of the PseudoClientRect
  5583. /// </summary>
  5584. /// <param name="x">The screen x-coordinate of the reversible line
  5585. /// to be drawn</param>
  5586. private void DrawReversibleLine(int x)
  5587. {
  5588. Point start = this.PointToScreen(new Point(x, this.PseudoClientRect.Top));
  5589. ControlPaint.DrawReversibleLine(start, new Point(start.X, start.Y + this.PseudoClientRect.Height), this.BackColor);
  5590. }
  5591. #region Border
  5592. /// <summary>
  5593. /// Paints the Table's border
  5594. /// </summary>
  5595. /// <param name="e">A PaintEventArgs that contains the event data</param>
  5596. protected void OnPaintBorder(PaintEventArgs e)
  5597. {
  5598. //e.Graphics.SetClip(e.ClipRectangle);
  5599. if (this.BorderStyle == BorderStyle.Fixed3D)
  5600. {
  5601. if (ThemeManager.VisualStylesEnabled)
  5602. {
  5603. TextBoxStates state = TextBoxStates.Normal;
  5604. if (!this.Enabled)
  5605. {
  5606. state = TextBoxStates.Disabled;
  5607. }
  5608. // draw the left border
  5609. Rectangle clipRect = new Rectangle(0, 0, SystemInformation.Border3DSize.Width, this.Height);
  5610. if (clipRect.IntersectsWith(e.ClipRectangle))
  5611. {
  5612. ThemeManager.DrawTextBox(e.Graphics, this.ClientRectangle, clipRect, state);
  5613. }
  5614. // draw the top border
  5615. clipRect = new Rectangle(0, 0, this.Width, SystemInformation.Border3DSize.Height);
  5616. if (clipRect.IntersectsWith(e.ClipRectangle))
  5617. {
  5618. ThemeManager.DrawTextBox(e.Graphics, this.ClientRectangle, clipRect, state);
  5619. }
  5620. // draw the right border
  5621. clipRect = new Rectangle(this.Width-SystemInformation.Border3DSize.Width, 0, this.Width, this.Height);
  5622. if (clipRect.IntersectsWith(e.ClipRectangle))
  5623. {
  5624. ThemeManager.DrawTextBox(e.Graphics, this.ClientRectangle, clipRect, state);
  5625. }
  5626. // draw the bottom border
  5627. clipRect = new Rectangle(0, this.Height-SystemInformation.Border3DSize.Height, this.Width, SystemInformation.Border3DSize.Height);
  5628. if (clipRect.IntersectsWith(e.ClipRectangle))
  5629. {
  5630. ThemeManager.DrawTextBox(e.Graphics, this.ClientRectangle, clipRect, state);
  5631. }
  5632. }
  5633. else
  5634. {
  5635. ControlPaint.DrawBorder3D(e.Graphics, 0, 0, this.Width, this.Height, Border3DStyle.Sunken);
  5636. }
  5637. }
  5638. else if (this.BorderStyle == BorderStyle.FixedSingle)
  5639. {
  5640. e.Graphics.DrawRectangle(Pens.Black, 0, 0, this.Width-1, this.Height-1);
  5641. }
  5642. if (this.HScroll && this.VScroll)
  5643. {
  5644. Rectangle rect = new Rectangle(this.Width - this.BorderWidth - SystemInformation.VerticalScrollBarWidth,
  5645. this.Height - this.BorderWidth - SystemInformation.HorizontalScrollBarHeight,
  5646. SystemInformation.VerticalScrollBarWidth,
  5647. SystemInformation.HorizontalScrollBarHeight);
  5648. if (rect.IntersectsWith(e.ClipRectangle))
  5649. {
  5650. e.Graphics.FillRectangle(SystemBrushes.Control, rect);
  5651. }
  5652. }
  5653. }
  5654. #endregion
  5655. #region Cells
  5656. /// <summary>
  5657. /// Paints the Cell at the specified row and column indexes
  5658. /// </summary>
  5659. /// <param name="e">A PaintEventArgs that contains the event data</param>
  5660. /// <param name="row">The index of the row that contains the cell to be painted</param>
  5661. /// <param name="column">The index of the column that contains the cell to be painted</param>
  5662. /// <param name="cellRect">The bounding Rectangle of the Cell</param>
  5663. protected void OnPaintCell(PaintEventArgs e, int row, int column, Rectangle cellRect)
  5664. {
  5665. if (row == 0 && column == 1)
  5666. {
  5667. column = 1;
  5668. }
  5669. // get the renderer for the cells column
  5670. ICellRenderer renderer = this.ColumnModel.Columns[column].Renderer;
  5671. if (renderer == null)
  5672. {
  5673. // get the default renderer for the column
  5674. renderer = this.ColumnModel.GetCellRenderer(this.ColumnModel.Columns[column].GetDefaultRendererName());
  5675. }
  5676. // if the renderer is still null (which it shouldn't)
  5677. // the get out of here
  5678. if (renderer == null)
  5679. {
  5680. return;
  5681. }
  5682. PaintCellEventArgs pcea = new PaintCellEventArgs(e.Graphics, cellRect);
  5683. pcea.Graphics.SetClip(Rectangle.Intersect(e.ClipRectangle, cellRect));
  5684. if (column < this.TableModel.Rows[row].Cells.Count)
  5685. {
  5686. // is the cell selected
  5687. bool selected = false;
  5688. if (this.FullRowSelect)
  5689. {
  5690. selected = this.TableModel.Selections.IsRowSelected(row);
  5691. }
  5692. else
  5693. {
  5694. if (this.SelectionStyle == SelectionStyle.ListView)
  5695. {
  5696. if (this.TableModel.Selections.IsRowSelected(row) && this.ColumnModel.PreviousVisibleColumn(column) == -1)
  5697. {
  5698. selected = true;
  5699. }
  5700. }
  5701. else if (this.SelectionStyle == SelectionStyle.Grid)
  5702. {
  5703. if (this.TableModel.Selections.IsCellSelected(row, column))
  5704. {
  5705. selected = true;
  5706. }
  5707. }
  5708. }
  5709. //
  5710. bool editable = this.TableModel[row, column].Editable && this.TableModel.Rows[row].Editable && this.ColumnModel.Columns[column].Editable;
  5711. bool enabled = this.TableModel[row, column].Enabled && this.TableModel.Rows[row].Enabled && this.ColumnModel.Columns[column].Enabled;
  5712. // draw the cell
  5713. pcea.SetCell(this.TableModel[row, column]);
  5714. pcea.SetRow(row);
  5715. pcea.SetColumn(column);
  5716. pcea.SetTable(this);
  5717. pcea.SetSelected(selected);
  5718. pcea.SetFocused(this.Focused && this.FocusedCell.Row == row && this.FocusedCell.Column == column);
  5719. pcea.SetSorted(column == this.lastSortedColumn);
  5720. pcea.SetEditable(editable);
  5721. pcea.SetEnabled(enabled);
  5722. pcea.SetCellRect(cellRect);
  5723. }
  5724. else
  5725. {
  5726. // there isn't a cell for this column, so send a
  5727. // null value for the cell and the renderer will
  5728. // take care of the rest (it should draw an empty cell)
  5729. pcea.SetCell(null);
  5730. pcea.SetRow(row);
  5731. pcea.SetColumn(column);
  5732. pcea.SetTable(this);
  5733. pcea.SetSelected(false);
  5734. pcea.SetFocused(false);
  5735. pcea.SetSorted(false);
  5736. pcea.SetEditable(false);
  5737. pcea.SetEnabled(false);
  5738. pcea.SetCellRect(cellRect);
  5739. }
  5740. // let the user get the first crack at painting the cell
  5741. this.OnBeforePaintCell(pcea);
  5742. // only send to the renderer if the user hasn't
  5743. // set the handled property
  5744. if (!pcea.Handled)
  5745. {
  5746. renderer.OnPaintCell(pcea);
  5747. }
  5748. // let the user have another go
  5749. this.OnAfterPaintCell(pcea);
  5750. }
  5751. /// <summary>
  5752. /// Raises the BeforePaintCell event
  5753. /// </summary>
  5754. /// <param name="e">A PaintCellEventArgs that contains the event data</param>
  5755. protected virtual void OnBeforePaintCell(PaintCellEventArgs e)
  5756. {
  5757. if (BeforePaintCell != null)
  5758. {
  5759. BeforePaintCell(this, e);
  5760. }
  5761. }
  5762. /// <summary>
  5763. /// Raises the AfterPaintCell event
  5764. /// </summary>
  5765. /// <param name="e">A PaintCellEventArgs that contains the event data</param>
  5766. protected virtual void OnAfterPaintCell(PaintCellEventArgs e)
  5767. {
  5768. if (AfterPaintCell != null)
  5769. {
  5770. AfterPaintCell(this, e);
  5771. }
  5772. }
  5773. #endregion
  5774. #region Grid
  5775. /// <summary>
  5776. /// Paints the Table's grid
  5777. /// </summary>
  5778. /// <param name="e">A PaintEventArgs that contains the event data</param>
  5779. protected void OnPaintGrid(PaintEventArgs e)
  5780. {
  5781. if (this.GridLines == GridLines.None)
  5782. {
  5783. return;
  5784. }
  5785. //
  5786. //e.Graphics.SetClip(e.ClipRectangle);
  5787. if (this.ColumnModel == null || this.ColumnModel.Columns.Count == 0)
  5788. {
  5789. return;
  5790. }
  5791. //e.Graphics.SetClip(e.ClipRectangle);
  5792. if (this.ColumnModel != null)
  5793. {
  5794. using (Pen gridPen = new Pen(this.GridColor))
  5795. {
  5796. //
  5797. gridPen.DashStyle = (DashStyle) this.GridLineStyle;
  5798. // check if we can draw column lines
  5799. if ((this.GridLines & GridLines.Columns) == GridLines.Columns)
  5800. {
  5801. int right = this.DisplayRectangle.X;
  5802. for (int i=0; i<this.ColumnModel.Columns.Count; i++)
  5803. {
  5804. if (this.ColumnModel.Columns[i].Visible)
  5805. {
  5806. right += this.ColumnModel.Columns[i].Width;
  5807. if (right >= e.ClipRectangle.Left && right <= e.ClipRectangle.Right)
  5808. {
  5809. e.Graphics.DrawLine(gridPen, right-1, e.ClipRectangle.Top, right-1, e.ClipRectangle.Bottom);
  5810. }
  5811. }
  5812. }
  5813. }
  5814. if (this.TableModel != null)
  5815. {
  5816. // check if we can draw row lines
  5817. if ((this.GridLines & GridLines.Rows) == GridLines.Rows)
  5818. {
  5819. int y = this.CellDataRect.Y + this.RowHeight - 1;
  5820. for (int i=y; i<=e.ClipRectangle.Bottom; i+=this.RowHeight)
  5821. {
  5822. if (i >= this.CellDataRect.Top)
  5823. {
  5824. e.Graphics.DrawLine(gridPen, e.ClipRectangle.Left, i, e.ClipRectangle.Right, i);
  5825. }
  5826. }
  5827. }
  5828. }
  5829. }
  5830. }
  5831. }
  5832. #endregion
  5833. #region Header
  5834. /// <summary>
  5835. /// Paints the Table's Column headers
  5836. /// </summary>
  5837. /// <param name="e">A PaintEventArgs that contains the event data</param>
  5838. protected void OnPaintHeader(PaintEventArgs e)
  5839. {
  5840. // only bother if we actually get to paint something
  5841. if (!this.HeaderRectangle.IntersectsWith(e.ClipRectangle))
  5842. {
  5843. return;
  5844. }
  5845. int xPos = this.DisplayRectangle.Left;
  5846. bool needDummyHeader = true;
  5847. //
  5848. PaintHeaderEventArgs phea = new PaintHeaderEventArgs(e.Graphics, e.ClipRectangle);
  5849. for (int i=0; i<this.ColumnModel.Columns.Count; i++)
  5850. {
  5851. // check that the column isn't hidden
  5852. if (this.ColumnModel.Columns[i].Visible)
  5853. {
  5854. Rectangle colHeaderRect = new Rectangle(xPos, this.BorderWidth, this.ColumnModel.Columns[i].Width, this.HeaderHeight);
  5855. // check that the column intersects with the clipping rect
  5856. if (e.ClipRectangle.IntersectsWith(colHeaderRect))
  5857. {
  5858. // move and resize the headerRenderer
  5859. this.headerRenderer.Bounds = new Rectangle(xPos, this.BorderWidth, this.ColumnModel.Columns[i].Width, this.HeaderHeight);
  5860. // set the clipping area to the header renderers bounds
  5861. phea.Graphics.SetClip(Rectangle.Intersect(e.ClipRectangle, this.headerRenderer.Bounds));
  5862. // draw the column header
  5863. phea.SetColumn(this.ColumnModel.Columns[i]);
  5864. phea.SetColumnIndex(i);
  5865. phea.SetTable(this);
  5866. phea.SetHeaderStyle(this.HeaderStyle);
  5867. phea.SetHeaderRect(this.headerRenderer.Bounds);
  5868. // let the user get the first crack at painting the header
  5869. this.OnBeforePaintHeader(phea);
  5870. // only send to the renderer if the user hasn't
  5871. // set the handled property
  5872. if (!phea.Handled)
  5873. {
  5874. this.headerRenderer.OnPaintHeader(phea);
  5875. }
  5876. // let the user have another go
  5877. this.OnAfterPaintHeader(phea);
  5878. }
  5879. // set the next column start position
  5880. xPos += this.ColumnModel.Columns[i].Width;
  5881. // if the next start poition is past the right edge
  5882. // of the clipping rectangle then we don't need to
  5883. // draw anymore
  5884. if (xPos >= e.ClipRectangle.Right)
  5885. {
  5886. return;
  5887. }
  5888. // check is the next column position is past the
  5889. // right edge of the table. if it is, get out of
  5890. // here as we don't need to draw anymore columns
  5891. if (xPos >= this.ClientRectangle.Width)
  5892. {
  5893. needDummyHeader = false;
  5894. break;
  5895. }
  5896. }
  5897. }
  5898. if (needDummyHeader)
  5899. {
  5900. // move and resize the headerRenderer
  5901. this.headerRenderer.Bounds = new Rectangle(xPos, this.BorderWidth, this.ClientRectangle.Width - xPos + 2, this.HeaderHeight);
  5902. phea.Graphics.SetClip(Rectangle.Intersect(e.ClipRectangle, this.headerRenderer.Bounds));
  5903. phea.SetColumn(null);
  5904. phea.SetColumnIndex(-1);
  5905. phea.SetTable(this);
  5906. phea.SetHeaderStyle(this.HeaderStyle);
  5907. phea.SetHeaderRect(this.headerRenderer.Bounds);
  5908. // let the user get the first crack at painting the header
  5909. this.OnBeforePaintHeader(phea);
  5910. // only send to the renderer if the user hasn't
  5911. // set the handled property
  5912. if (!phea.Handled)
  5913. {
  5914. this.headerRenderer.OnPaintHeader(phea);
  5915. }
  5916. // let the user have another go
  5917. this.OnAfterPaintHeader(phea);
  5918. }
  5919. }
  5920. /// <summary>
  5921. /// Raises the BeforePaintHeader event
  5922. /// </summary>
  5923. /// <param name="e">A PaintCellEventArgs that contains the event data</param>
  5924. protected virtual void OnBeforePaintHeader(PaintHeaderEventArgs e)
  5925. {
  5926. if (BeforePaintHeader != null)
  5927. {
  5928. BeforePaintHeader(this, e);
  5929. }
  5930. }
  5931. /// <summary>
  5932. /// Raises the AfterPaintHeader event
  5933. /// </summary>
  5934. /// <param name="e">A PaintHeaderEventArgs that contains the event data</param>
  5935. protected virtual void OnAfterPaintHeader(PaintHeaderEventArgs e)
  5936. {
  5937. if (AfterPaintHeader != null)
  5938. {
  5939. AfterPaintHeader(this, e);
  5940. }
  5941. }
  5942. #endregion
  5943. #region Rows
  5944. /// <summary>
  5945. /// Paints the Table's Rows
  5946. /// </summary>
  5947. /// <param name="e">A PaintEventArgs that contains the event data</param>
  5948. protected void OnPaintRows(PaintEventArgs e)
  5949. {
  5950. int xPos = this.DisplayRectangle.Left;
  5951. int yPos = this.PseudoClientRect.Top;
  5952. if (this.HeaderStyle != ColumnHeaderStyle.None)
  5953. {
  5954. yPos += this.HeaderHeight;
  5955. }
  5956. Rectangle rowRect = new Rectangle(xPos, yPos, this.ColumnModel.TotalColumnWidth, this.RowHeight);
  5957. for (int i=this.TopIndex; i<Math.Min(this.TableModel.Rows.Count, this.TopIndex+this.VisibleRowCount+1); i++)
  5958. {
  5959. if (rowRect.IntersectsWith(e.ClipRectangle))
  5960. {
  5961. this.OnPaintRow(e, i, rowRect);
  5962. }
  5963. else if (rowRect.Top > e.ClipRectangle.Bottom)
  5964. {
  5965. break;
  5966. }
  5967. // move to the next row
  5968. rowRect.Y += this.RowHeight;
  5969. }
  5970. //
  5971. if (this.IsValidColumn(this.lastSortedColumn))
  5972. {
  5973. if (rowRect.Y < this.PseudoClientRect.Bottom)
  5974. {
  5975. Rectangle columnRect = this.ColumnRect(this.lastSortedColumn);
  5976. columnRect.Y = rowRect.Y;
  5977. columnRect.Height = this.PseudoClientRect.Bottom - rowRect.Y;
  5978. if (columnRect.IntersectsWith(e.ClipRectangle))
  5979. {
  5980. columnRect.Intersect(e.ClipRectangle);
  5981. e.Graphics.SetClip(columnRect);
  5982. using (SolidBrush brush = new SolidBrush(this.SortedColumnBackColor))
  5983. {
  5984. e.Graphics.FillRectangle(brush, columnRect);
  5985. }
  5986. }
  5987. }
  5988. }
  5989. }
  5990. /// <summary>
  5991. /// Paints the Row at the specified index
  5992. /// </summary>
  5993. /// <param name="e">A PaintEventArgs that contains the event data</param>
  5994. /// <param name="row">The index of the Row to be painted</param>
  5995. /// <param name="rowRect">The bounding Rectangle of the Row to be painted</param>
  5996. protected void OnPaintRow(PaintEventArgs e, int row, Rectangle rowRect)
  5997. {
  5998. Rectangle cellRect = new Rectangle(rowRect.X, rowRect.Y, 0, rowRect.Height);
  5999. //e.Graphics.SetClip(rowRect);
  6000. for (int i=0; i<this.ColumnModel.Columns.Count; i++)
  6001. {
  6002. if (this.ColumnModel.Columns[i].Visible)
  6003. {
  6004. cellRect.Width = this.ColumnModel.Columns[i].Width;
  6005. if (cellRect.IntersectsWith(e.ClipRectangle))
  6006. {
  6007. this.OnPaintCell(e, row, i, cellRect);
  6008. }
  6009. else if (cellRect.Left > e.ClipRectangle.Right)
  6010. {
  6011. break;
  6012. }
  6013. cellRect.X += this.ColumnModel.Columns[i].Width;
  6014. }
  6015. }
  6016. }
  6017. #endregion
  6018. #region Empty Table Text
  6019. /// <summary>
  6020. /// Paints the message that is displayed when the Table doen't
  6021. /// contain any items
  6022. /// </summary>
  6023. /// <param name="e">A PaintEventArgs that contains the event data</param>
  6024. protected void OnPaintEmptyTableText(PaintEventArgs e)
  6025. {
  6026. if (this.ColumnModel == null || this.RowCount == 0)
  6027. {
  6028. Rectangle client = this.CellDataRect;
  6029. client.Y += 10;
  6030. client.Height -= 10;
  6031. StringFormat format = new StringFormat();
  6032. format.Alignment = StringAlignment.Center;
  6033. using (SolidBrush brush = new SolidBrush(this.ForeColor))
  6034. {
  6035. if (this.DesignMode)
  6036. {
  6037. if (this.ColumnModel == null || this.TableModel == null)
  6038. {
  6039. string text = null;
  6040. if (this.ColumnModel == null)
  6041. {
  6042. if (this.TableModel == null)
  6043. {
  6044. text = "Table does not have a ColumnModel or TableModel";
  6045. }
  6046. else
  6047. {
  6048. text = "Table does not have a ColumnModel";
  6049. }
  6050. }
  6051. else if (this.TableModel == null)
  6052. {
  6053. text = "Table does not have a TableModel";
  6054. }
  6055. e.Graphics.DrawString(text, this.Font, brush, client, format);
  6056. }
  6057. else if (this.TableModel != null && this.TableModel.Rows.Count == 0)
  6058. {
  6059. if (this.NoItemsText != null && this.NoItemsText.Length > 0)
  6060. {
  6061. e.Graphics.DrawString(this.NoItemsText, this.Font, brush, client, format);
  6062. }
  6063. }
  6064. }
  6065. else
  6066. {
  6067. if (this.NoItemsText != null && this.NoItemsText.Length > 0)
  6068. {
  6069. e.Graphics.DrawString(this.NoItemsText, this.Font, brush, client, format);
  6070. }
  6071. }
  6072. }
  6073. }
  6074. }
  6075. #endregion
  6076. #endregion
  6077. #region Rows
  6078. /// <summary>
  6079. /// Raises the RowPropertyChanged event
  6080. /// </summary>
  6081. /// <param name="e">A RowEventArgs that contains the event data</param>
  6082. protected internal virtual void OnRowPropertyChanged(RowEventArgs e)
  6083. {
  6084. if (this.CanRaiseEvents)
  6085. {
  6086. this.InvalidateRow(e.Index);
  6087. if (RowPropertyChanged != null)
  6088. {
  6089. RowPropertyChanged(e.Row, e);
  6090. }
  6091. }
  6092. }
  6093. /// <summary>
  6094. /// Raises the CellAdded event
  6095. /// </summary>
  6096. /// <param name="e">A RowEventArgs that contains the event data</param>
  6097. protected internal virtual void OnCellAdded(RowEventArgs e)
  6098. {
  6099. if (this.CanRaiseEvents)
  6100. {
  6101. this.InvalidateRow(e.Index);
  6102. if (CellAdded != null)
  6103. {
  6104. CellAdded(e.Row, e);
  6105. }
  6106. }
  6107. }
  6108. /// <summary>
  6109. /// Raises the CellRemoved event
  6110. /// </summary>
  6111. /// <param name="e">A RowEventArgs that contains the event data</param>
  6112. protected internal virtual void OnCellRemoved(RowEventArgs e)
  6113. {
  6114. if (this.CanRaiseEvents)
  6115. {
  6116. this.InvalidateRow(e.Index);
  6117. if (CellRemoved != null)
  6118. {
  6119. CellRemoved(this, e);
  6120. }
  6121. if (e.CellFromIndex == -1 && e.CellToIndex == -1)
  6122. {
  6123. if (this.FocusedCell.Row == e.Index)
  6124. {
  6125. this.focusedCell = CellPos.Empty;
  6126. }
  6127. }
  6128. else
  6129. {
  6130. for (int i=e.CellFromIndex; i<=e.CellToIndex; i++)
  6131. {
  6132. if (this.FocusedCell.Row == e.Index && this.FocusedCell.Column == i)
  6133. {
  6134. this.focusedCell = CellPos.Empty;
  6135. break;
  6136. }
  6137. }
  6138. }
  6139. }
  6140. }
  6141. #endregion
  6142. #region Scrollbars
  6143. /// <summary>
  6144. /// Occurs when the Table's horizontal scrollbar is scrolled
  6145. /// </summary>
  6146. /// <param name="sender">The object that Raised the event</param>
  6147. /// <param name="e">A ScrollEventArgs that contains the event data</param>
  6148. protected void OnHorizontalScroll(object sender, ScrollEventArgs e)
  6149. {
  6150. // stop editing as the editor doesn't move while
  6151. // the table scrolls
  6152. if (this.IsEditing)
  6153. {
  6154. this.StopEditing();
  6155. }
  6156. if (this.CanRaiseEvents)
  6157. {
  6158. // non-solid row lines develop artifacts while scrolling
  6159. // with the thumb so we invalidate the table once thumb
  6160. // scrolling has finished to make them look nice again
  6161. if (e.Type == ScrollEventType.ThumbPosition)
  6162. {
  6163. if (this.GridLineStyle != GridLineStyle.Solid)
  6164. {
  6165. if (this.GridLines == GridLines.Rows || this.GridLines == GridLines.Both)
  6166. {
  6167. this.Invalidate(this.CellDataRect, false);
  6168. }
  6169. }
  6170. // same with the focus rect
  6171. if (this.FocusedCell != CellPos.Empty)
  6172. {
  6173. this.Invalidate(this.CellRect(this.FocusedCell), false);
  6174. }
  6175. }
  6176. else
  6177. {
  6178. this.HorizontalScroll(e.NewValue);
  6179. }
  6180. }
  6181. }
  6182. /// <summary>
  6183. /// Occurs when the Table's vertical scrollbar is scrolled
  6184. /// </summary>
  6185. /// <param name="sender">The object that Raised the event</param>
  6186. /// <param name="e">A ScrollEventArgs that contains the event data</param>
  6187. protected void OnVerticalScroll(object sender, ScrollEventArgs e)
  6188. {
  6189. // stop editing as the editor doesn't move while
  6190. // the table scrolls
  6191. if (this.IsEditing)
  6192. {
  6193. this.StopEditing();
  6194. }
  6195. if (this.CanRaiseEvents)
  6196. {
  6197. // non-solid column lines develop artifacts while scrolling
  6198. // with the thumb so we invalidate the table once thumb
  6199. // scrolling has finished to make them look nice again
  6200. if (e.Type == ScrollEventType.ThumbPosition)
  6201. {
  6202. if (this.GridLineStyle != GridLineStyle.Solid)
  6203. {
  6204. if (this.GridLines == GridLines.Columns || this.GridLines == GridLines.Both)
  6205. {
  6206. this.Invalidate(this.CellDataRect, false);
  6207. }
  6208. }
  6209. }
  6210. else
  6211. {
  6212. this.VerticalScroll(e.NewValue);
  6213. }
  6214. }
  6215. }
  6216. /// <summary>
  6217. /// Handler for a ScrollBars GotFocus event
  6218. /// </summary>
  6219. /// <param name="sender">The object that raised the event</param>
  6220. /// <param name="e">An EventArgs that contains the event data</param>
  6221. private void scrollBar_GotFocus(object sender, EventArgs e)
  6222. {
  6223. // don't let the scrollbars have focus
  6224. // (appears to slow scroll speed otherwise)
  6225. this.Focus();
  6226. }
  6227. #endregion
  6228. #region Sorting
  6229. /// <summary>
  6230. /// Raises the BeginSort event
  6231. /// </summary>
  6232. /// <param name="e">A ColumnEventArgs that contains the event data</param>
  6233. protected virtual void OnBeginSort(ColumnEventArgs e)
  6234. {
  6235. if (BeginSort != null)
  6236. {
  6237. BeginSort(this, e);
  6238. }
  6239. }
  6240. /// <summary>
  6241. /// Raises the EndSort event
  6242. /// </summary>
  6243. /// <param name="e">A ColumnEventArgs that contains the event data</param>
  6244. protected virtual void OnEndSort(ColumnEventArgs e)
  6245. {
  6246. if (EndSort != null)
  6247. {
  6248. EndSort(this, e);
  6249. }
  6250. }
  6251. #endregion
  6252. #region TableModel
  6253. /// <summary>
  6254. /// Raises the TableModelChanged event
  6255. /// </summary>
  6256. /// <param name="e">An EventArgs that contains the event data</param>
  6257. protected internal virtual void OnTableModelChanged(EventArgs e)
  6258. {
  6259. if (this.CanRaiseEvents)
  6260. {
  6261. this.PerformLayout();
  6262. this.Invalidate();
  6263. if (TableModelChanged != null)
  6264. {
  6265. TableModelChanged(this, e);
  6266. }
  6267. }
  6268. }
  6269. /// <summary>
  6270. /// Raises the SelectionChanged event
  6271. /// </summary>
  6272. /// <param name="e">A TableModelEventArgs that contains the event data</param>
  6273. protected internal virtual void OnSelectionChanged(SelectionEventArgs e)
  6274. {
  6275. if (this.CanRaiseEvents)
  6276. {
  6277. if (e.OldSelectionBounds != Rectangle.Empty)
  6278. {
  6279. Rectangle invalidateRect = new Rectangle(this.DisplayRectToClient(e.OldSelectionBounds.Location), e.OldSelectionBounds.Size);
  6280. if (this.HeaderStyle != ColumnHeaderStyle.None)
  6281. {
  6282. invalidateRect.Y += this.HeaderHeight;
  6283. }
  6284. this.Invalidate(invalidateRect);
  6285. }
  6286. if (e.NewSelectionBounds != Rectangle.Empty)
  6287. {
  6288. Rectangle invalidateRect = new Rectangle(this.DisplayRectToClient(e.NewSelectionBounds.Location), e.NewSelectionBounds.Size);
  6289. if (this.HeaderStyle != ColumnHeaderStyle.None)
  6290. {
  6291. invalidateRect.Y += this.HeaderHeight;
  6292. }
  6293. this.Invalidate(invalidateRect);
  6294. }
  6295. if (SelectionChanged != null)
  6296. {
  6297. SelectionChanged(this, e);
  6298. }
  6299. }
  6300. }
  6301. /// <summary>
  6302. /// Raises the RowHeightChanged event
  6303. /// </summary>
  6304. /// <param name="e">An EventArgs that contains the event data</param>
  6305. protected internal virtual void OnRowHeightChanged(EventArgs e)
  6306. {
  6307. if (this.CanRaiseEvents)
  6308. {
  6309. this.PerformLayout();
  6310. this.Invalidate();
  6311. if (RowHeightChanged != null)
  6312. {
  6313. RowHeightChanged(this, e);
  6314. }
  6315. }
  6316. }
  6317. /// <summary>
  6318. /// Raises the RowAdded event
  6319. /// </summary>
  6320. /// <param name="e">A TableModelEventArgs that contains the event data</param>
  6321. protected internal virtual void OnRowAdded(TableModelEventArgs e)
  6322. {
  6323. if (this.CanRaiseEvents)
  6324. {
  6325. this.PerformLayout();
  6326. this.Invalidate();
  6327. if (RowAdded != null)
  6328. {
  6329. RowAdded(e.TableModel, e);
  6330. }
  6331. }
  6332. }
  6333. /// <summary>
  6334. /// Raises the RowRemoved event
  6335. /// </summary>
  6336. /// <param name="e">A TableModelEventArgs that contains the event data</param>
  6337. protected internal virtual void OnRowRemoved(TableModelEventArgs e)
  6338. {
  6339. if (this.CanRaiseEvents)
  6340. {
  6341. this.PerformLayout();
  6342. this.Invalidate();
  6343. if (RowRemoved != null)
  6344. {
  6345. RowRemoved(e.TableModel, e);
  6346. }
  6347. }
  6348. }
  6349. #endregion
  6350. #endregion
  6351. }
  6352. }