GridCtrl.cpp 162 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362
  1. // GridCtrl.cpp : implementation file
  2. //
  3. // MFC Grid Control
  4. //
  5. // Written by Chris Maunder
  6. // mailto:chrismaunder@codeguru.com
  7. //
  8. // Copyright (c) 1998-1999.
  9. //
  10. // The code contained in this file is based on the original
  11. // WorldCom Grid control written by Joe Willcoxson,
  12. // mailto:chinajoe@aol.com
  13. // http://users.aol.com/chinajoe
  14. // The code has gone through so many modifications that I'm
  15. // not sure if there is even a single original line of code.
  16. // In any case Joe's code was a great framewaork on which to
  17. // build.
  18. //
  19. // This code may be used in compiled form in any way you desire. This
  20. // file may be redistributed unmodified by any means PROVIDING it is
  21. // not sold for profit without the authors written consent, and
  22. // providing that this notice and the authors name and all copyright
  23. // notices remains intact. If the source code in this file is used in
  24. // any commercial application then a statement along the lines of
  25. // "Portions copyright (c) Chris Maunder, 1998" must be included in
  26. // the startup banner, "About" box or printed documentation. An email
  27. // letting me know that you are using it would be nice as well. That's
  28. // not much to ask considering the amount of work that went into this.
  29. //
  30. // This file is provided "as is" with no expressed or implied warranty.
  31. // The author accepts no liability for any damage/loss of business that
  32. // this product may cause.
  33. //
  34. // Expect bugs!
  35. //
  36. // Please use and enjoy, and let me know of any bugs/mods/improvements
  37. // that you have found/implemented and I will fix/incorporate them into
  38. // this file.
  39. //
  40. // History:
  41. // --------
  42. // This control is constantly evolving, sometimes due to new features that I
  43. // feel are necessary, and sometimes due to existing bugs. Where possible I
  44. // have credited the changes to those who contributed code corrections or
  45. // enhancements (names in brackets) or code suggestions (suggested by...)
  46. //
  47. // 1.0 20 Feb 1998 First release version.
  48. // 1.01 24 Feb 1998 Memory leak fix (Jens Bohlmann)
  49. // Fixec typo (my fault!) in CMemDC.h - Claus Arend-Schneider)
  50. // Bug in GetSelectedCount (Lyn Newton)
  51. // 1.02 4 Mar 1998 Scrolling a little neater (less dead area)
  52. // Cell selection via OnTimer correctly updates Focus cell (Suggested by Lyn Newton)
  53. // 1.03 17 Mar 1998 Clipboard functions added, Intellimouse support
  54. // Using 32 bit scroll pos functions instead of 16 bit ("cronos")
  55. // Added OLE drag and drop.
  56. // 1.04 6 Apr 1998 Added Ctrl-A = Select All, fixed CGridDropTarget
  57. // problem, minor bug in CopyTextFromGrid (assert on
  58. // empty string). Cleaned up reponse to m_bEditable
  59. // (OnDrop and Ctrl-X disabled)
  60. // 1.05 10 May 1998 Memory leak fixed. (Yuheng Zhao)
  61. // Changed OLE initialisation (Carlo Comino)
  62. // Added separate fore + background cell colours (Suggested by John Crane)
  63. // ExpandToFit etc cleaned up - now decreases and
  64. // increases cell sizes to fit client area.
  65. // Added notification messages for the grid's parent (Suggested by
  66. // Added GVIS_READONLY state
  67. // 1.06 20 May 1998 Added TAB key handling. (Daniela Rybarova)
  68. // Intellimouse code correction for whole page scrolling (Paul Grant)
  69. // Fixed 16 bit thumb track problems (now 32 bit) (Paul Grant)
  70. // Fixed accelerator key problem in CInPlaceEdit (Matt Weagle)
  71. // Fixed Stupid ClassWizard code parsing problem (Michael A. Barnhart)
  72. // Double buffering now programmatically selectable
  73. // Workaround for win95 drag and drop registration problem
  74. // Corrected UNICODE implementation of clipboard stuff
  75. // Dragging and dropping from a selection onto itself no
  76. // no longer causes the cells to be emptied
  77. // 1.07 28 Jul 1998 Added EnsureVisible. (Roelf Werkman)
  78. // Fixed delete key problem on read-only cells. (Serge Weinstock)
  79. // OnEndInPlaceEdit sends notification AFTER storing
  80. // the modified text in the cell.
  81. // Added CreateInPlaceEditControl to make it easier to
  82. // change the way cells are edited. (suggested by Chris Clark)
  83. // Added Set/GetGridColor.
  84. // CopyTextToClipboard and PasteTextToGrid problem with
  85. // blank cells fixed, and CopyTextToClipboard tweaked.
  86. // SetModified called when cutting text or hitting DEL. (Jonathan Watters)
  87. // Focus cell made visible when editing begins.
  88. // Blank lines now treated correctly when pasting data.
  89. // Removed ES_MULTILINE style from the default edit control.
  90. // Added virtual CreateCell(row, col) function.
  91. // Fonts now specified on a per-cell basis using Get/SetItemFont.
  92. // 1.08 6 Aug 1998 Ctrl+arrows now allows cell navigation. Modified
  93. // CreateInPlaceEditControl to accept ID of control.
  94. // Added Titletips to grid cells. (Added EnableTitleTips / GetTitleTips)
  95. // 1.09 12 Sep 1998 When printing, parent window title is printed in header - Gert Rijs
  96. // GetNextItem search with GVNI_DROPHILITED now returns
  97. // cells with GVIS_DROPHILITED set, instead of GVIS_FOCUSED (Franco Bez)
  98. // (Also fixed minor bug in GetNextItem) (Franco Bez)
  99. // Cell selection using Shift+arrows works - Franco Bez
  100. // SetModified called after edits ONLY if contents changed (Franco Bez)
  101. // Cell colours now dithered in 256 colour screens.
  102. // Support for MSVC 4.2 (Graham Cheetham)
  103. // 1.10 30 Nov 1998 Titletips now disappear on a scroll event. Compiler errors
  104. // fixed. Grid lines drawing fixed (Graham Cheetham).
  105. // Cell focus fix on Isert Row/Col (Jochen Kauffmann)
  106. // Added DeleteNonFixedRows() (John Rackley)
  107. // Message #define conflict fixed (Oskar Wieland)
  108. // Titletips & cell insert/delete fix (Ramesh Dhar)
  109. // Titletips repeat-creation bug fixed.
  110. // GVN_SELCHANGED message now sends current cell ID
  111. // Font resource leak in GetTextExtent fixed (Gavin Jerman)
  112. // More TAB fixes (Andreas Ruh)
  113. // 1.11 1 Dec 1998 GetNextItem bug fix (suggested by Francis Fu)
  114. // InsertColumn (-1) fix (Roy Hopkins)
  115. // Was too liberal with the "IsEditable"'s. oops. (Michel Hete)
  116. // 1.11a 4 Jan 1999 Compiler errors in VC6 fixed.
  117. // 1.12 10 Apr 1999 Cleanup to allow GRIDCONTROL_NO_CLIPBOARD define
  118. // CE #defines added. (Thanks to Frank Uzzolino for a start on this)
  119. // TitleTip display fixed for cells with images, plus it now uses cell font
  120. // Added GetTextRect and IsCellFixed
  121. // Focus change problem when resizing columns fixed (Sergey Nikiforenko)
  122. // Grid line drawing problem in fixed cells fixed (Sergey Nikiforenko)
  123. // CreateCell format persistance bug fixed (Sergey Nikiforenko)
  124. // DeleteColumn now returns TRUE (oops) (R. Elmer)
  125. // Enter, Tab and Esc key problem (finally) fixed - Darren Webb and Koay Kah Hoe
  126. // OnSize infinite loop fixed - Steve Kowald
  127. // GVN_SELCHANGING and GVN_SELCHANGED values changed to avoid conflicts (Hiroaki Watanabe)
  128. // Added single row selection mode (Yao Cai)
  129. // Fixed image drawing clip problem
  130. // Reduced unnecessary redraws significantly
  131. // GetNextItem additions and bug fix, and GVNI_AREA search option (Franco Bez)
  132. // Added GVIS_MODIFIED style for cells, so individual cells can have their
  133. // modification status queried. (Franco Bez)
  134. // 1.12a 15 Apr 1999 Removed the SetModified/GetModified inlines (no compiler warning!)
  135. // Renamed IDC_INPLACE_CONTROL to IDC_INPLACE_CONTROL and moved
  136. // to the header
  137. //
  138. // TODO:
  139. // - OnOutOfMemory function instead of exceptions
  140. // - Decrease timer interval over time to speed up selection over time
  141. //
  142. // NOTE: Grid data is stored row-by-row, so all operations on large numbers
  143. // of cells should be done row-by-row as well.
  144. //
  145. // KNOWN ISSUES TO BE ADDRESSED (Please don't send bug reports):
  146. // * Killfocus comes to late when a command is selected by the Menu.
  147. // When you are editing a cell and choose a Menuitem that searches for all the
  148. // modified cells it is not found. When you chose the menu a second time it is
  149. // found. I assume that the Menu command is executed before the cell receives the
  150. // KillFocus event. Expect similar Problems with accelerators. (Franco Bez)
  151. // * When you select a cell and move the mouse around (with the Left button down
  152. // i.e continuing with your selection) - if the mouse is over the Fixed column
  153. // or Row the drawing of the selected region is strange - in particular as you
  154. // move up and down say the Left Fixed Column notice the behaviour of the Focus
  155. // Cell - it is out of sync. (Vinay Desai)
  156. //
  157. /////////////////////////////////////////////////////////////////////////////
  158. #include "stdafx.h"
  159. #include "MemDC.h"
  160. #include "GridCtrl.h"
  161. #include "InPlaceEdit.h"
  162. // OLE stuff for clipboard operations
  163. #include <afxadv.h> // For CSharedFile
  164. #include <afxconv.h> // For LPTSTR -> LPSTR macros
  165. #ifdef _DEBUG
  166. #define new DEBUG_NEW
  167. #undef THIS_FILE
  168. static char THIS_FILE[] = __FILE__;
  169. #endif
  170. #define HEADER_HEIGHT 2 // For printing
  171. #define FOOTER_HEIGHT 2
  172. #define LEFT_MARGIN 4
  173. #define RIGHT_MARGIN 4
  174. #define TOP_MARGIN 1
  175. #define BOTTOM_MARGIN 1
  176. #define GAP 1
  177. #define SELECTED_CELL_FONT_WEIGHT 600 // weight of text for selected items
  178. IMPLEMENT_DYNCREATE(CGridCtrl, CWnd)
  179. void AFXAPI DDX_GridControl(CDataExchange* pDX, int nIDC, CGridCtrl& rControl)
  180. {
  181. if (rControl.GetSafeHwnd() == NULL) // not subclassed yet
  182. {
  183. ASSERT(!pDX->m_bSaveAndValidate);
  184. HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
  185. if (!rControl.SubclassWindow(hWndCtrl))
  186. {
  187. ASSERT(FALSE); // possibly trying to subclass twice?
  188. AfxThrowNotSupportedException();
  189. }
  190. #ifndef _AFX_NO_OCC_SUPPORT
  191. else
  192. {
  193. // If the control has reparented itself (e.g., invisible control),
  194. // make sure that the CWnd gets properly wired to its control site.
  195. if (pDX->m_pDlgWnd->GetSafeHwnd() != ::GetParent(rControl.GetSafeHwnd()))
  196. rControl.AttachControlSite(pDX->m_pDlgWnd);
  197. }
  198. #endif //!_AFX_NO_OCC_SUPPORT
  199. }
  200. }
  201. // Get the number of lines to scroll with each mouse wheel notch
  202. // Why doesn't windows give us this function???
  203. UINT GetMouseScrollLines()
  204. {
  205. int nScrollLines = 3; // reasonable default
  206. HKEY hKey;
  207. if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("Control Panel\\Desktop"),
  208. 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
  209. {
  210. TCHAR szData[128];
  211. DWORD dwKeyDataType;
  212. DWORD dwDataBufSize = sizeof(szData);
  213. if (RegQueryValueEx(hKey, _T("WheelScrollLines"), NULL, &dwKeyDataType,
  214. (LPBYTE) &szData, &dwDataBufSize) == ERROR_SUCCESS)
  215. {
  216. nScrollLines = _tcstoul(szData, NULL, 10);
  217. }
  218. RegCloseKey(hKey);
  219. }
  220. return nScrollLines;
  221. }
  222. /////////////////////////////////////////////////////////////////////////////
  223. // CGridCtrl
  224. CGridCtrl::CGridCtrl(int nRows, int nCols, int nFixedRows, int nFixedCols)
  225. {
  226. RegisterWindowClass();
  227. // Initialize OLE libraries
  228. m_bMustUninitOLE = FALSE;
  229. #if !defined(GRIDCONTROL_NO_DRAGDROP) || !defined(GRIDCONTROL_NO_CLIPBOARD)
  230. _AFX_THREAD_STATE* pState = AfxGetThreadState();
  231. if (!pState->m_bNeedTerm)
  232. {
  233. SCODE sc = ::OleInitialize(NULL);
  234. if (FAILED(sc))
  235. AfxMessageBox(_T("OLE initialization failed. Make sure that the OLE libraries are the correct version"));
  236. else
  237. m_bMustUninitOLE = TRUE;
  238. }
  239. #endif
  240. // Store the system colours in case they change. The gridctrl uses
  241. // these colours, and in OnSysColorChange we can check to see if
  242. // the gridctrl colours have been changed from the system colours.
  243. // If they have, then leave them, otherwise change them to reflect
  244. // the new system colours.
  245. m_crWindowText = ::GetSysColor(COLOR_WINDOWTEXT);
  246. m_crWindowColour = ::GetSysColor(COLOR_WINDOW);
  247. m_cr3DFace = ::GetSysColor(COLOR_3DFACE);
  248. m_crShadow = ::GetSysColor(COLOR_3DSHADOW);
  249. m_crGridColour = RGB(192,192,192);
  250. m_nRows = 0;
  251. m_nCols = 0;
  252. m_nFixedRows = 0;
  253. m_nFixedCols = 0;
  254. m_nDefCellHeight = 10; // These will get changed to something meaningful
  255. m_nDefCellWidth = 30; // when the window is created or subclassed
  256. m_nVScrollMax = 0; // Scroll position
  257. m_nHScrollMax = 0;
  258. m_nMargin = 0; // cell padding
  259. m_nRowsPerWheelNotch = GetMouseScrollLines(); // Get the number of lines
  260. // per mouse wheel notch to scroll
  261. m_MouseMode = MOUSE_NOTHING;
  262. m_nGridLines = GVL_BOTH;
  263. m_bEditable = TRUE;
  264. m_bListMode = FALSE;
  265. m_bSingleRowSelection = FALSE;
  266. m_bAllowDraw = TRUE; // allow draw updates
  267. m_bEnableSelection = TRUE;
  268. m_bAllowRowResize = TRUE;
  269. m_bAllowColumnResize = TRUE;
  270. m_bSortOnClick = TRUE; // Sort on header row click if in list mode
  271. m_bHandleTabKey = TRUE;
  272. #ifdef _WIN32_WCE
  273. m_bDoubleBuffer = FALSE; // Use double buffering to avoid flicker?
  274. #else
  275. m_bDoubleBuffer = TRUE; // Use double buffering to avoid flicker?
  276. #endif
  277. m_bTitleTips = TRUE; // show cell title tips
  278. m_bAscending = TRUE; // sorting stuff
  279. m_SortColumn = -1;
  280. m_nTimerID = 0; // For drag-selection
  281. m_nTimerInterval = 25; // (in milliseconds)
  282. m_nResizeCaptureRange = 3; // When resizing columns/row, the cursor has to be
  283. // within +/-3 pixels of the dividing line for
  284. // resizing to be possible
  285. m_pImageList = NULL;
  286. m_bAllowDragAndDrop = FALSE; // for drag and drop
  287. #ifndef _WIN32_WCE
  288. // Initially use the system message font for the GridCtrl font
  289. NONCLIENTMETRICS ncm;
  290. ncm.cbSize = sizeof(NONCLIENTMETRICS);
  291. VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0));
  292. memcpy(&m_Logfont, &(ncm.lfMessageFont), sizeof(LOGFONT));
  293. #else
  294. LOGFONT lf;
  295. GetObject(GetStockObject(SYSTEM_FONT), sizeof(LOGFONT), &lf);
  296. memcpy(&m_Logfont, &lf, sizeof(LOGFONT));
  297. #endif
  298. // Set up the initial grid size
  299. SetRowCount(nRows);
  300. SetColumnCount(nCols);
  301. SetFixedRowCount(nFixedRows);
  302. SetFixedColumnCount(nFixedCols);
  303. // Set the colours
  304. SetTextColor(m_crWindowText);
  305. SetTextBkColor(m_crWindowColour);
  306. SetBkColor(m_crShadow);
  307. SetFixedTextColor(m_crWindowText);
  308. SetFixedBkColor(m_cr3DFace);
  309. // set initial selection range (ie. none)
  310. m_SelectedCellMap.RemoveAll();
  311. m_PrevSelectedCellMap.RemoveAll();
  312. }
  313. CGridCtrl::~CGridCtrl()
  314. {
  315. DeleteAllItems();
  316. DestroyWindow();
  317. m_Font.DeleteObject();
  318. #if !defined(GRIDCONTROL_NO_DRAGDROP) || !defined(GRIDCONTROL_NO_CLIPBOARD)
  319. // Uninitialize OLE support
  320. if (m_bMustUninitOLE)
  321. ::OleUninitialize();
  322. #endif
  323. }
  324. // Register the window class if it has not already been registered.
  325. BOOL CGridCtrl::RegisterWindowClass()
  326. {
  327. WNDCLASS wndcls;
  328. HINSTANCE hInst = AfxGetInstanceHandle();
  329. // HINSTANCE hInst = AfxGetResourceHandle();
  330. if (!(::GetClassInfo(hInst, GRIDCTRL_CLASSNAME, &wndcls)))
  331. {
  332. // otherwise we need to register a new class
  333. wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  334. wndcls.lpfnWndProc = ::DefWindowProc;
  335. wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
  336. wndcls.hInstance = hInst;
  337. wndcls.hIcon = NULL;
  338. wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
  339. wndcls.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1);
  340. wndcls.lpszMenuName = NULL;
  341. wndcls.lpszClassName = GRIDCTRL_CLASSNAME;
  342. if (!AfxRegisterClass(&wndcls)) {
  343. AfxThrowResourceException();
  344. return FALSE;
  345. }
  346. }
  347. return TRUE;
  348. }
  349. BOOL CGridCtrl::Create(const RECT& rect, CWnd* pParentWnd, UINT nID, DWORD dwStyle)
  350. {
  351. ASSERT(pParentWnd->GetSafeHwnd());
  352. if (!CWnd::Create(GRIDCTRL_CLASSNAME, NULL, dwStyle, rect, pParentWnd, nID))
  353. return FALSE;
  354. #ifndef GRIDCONTROL_NO_DRAGDROP
  355. m_DropTarget.Register(this);
  356. #endif
  357. // Create titletips
  358. #ifndef GRIDCONTROL_NO_TITLETIPS
  359. if (m_bTitleTips)
  360. m_TitleTip.Create(this);
  361. #endif
  362. // The number of rows and columns will only be non-zero if the constructor
  363. // was called with non-zero initialising parameters. If this window was created
  364. // using a dialog template then the number of rows and columns will be 0 (which
  365. // means that the code below will not be needed - which is lucky 'cause it ain't
  366. // gonna get called in a dialog-template-type-situation.
  367. TRY {
  368. m_arRowHeights.SetSize(m_nRows); // initialize row heights
  369. m_arColWidths.SetSize(m_nCols); // initialize column widths
  370. }
  371. CATCH (CMemoryException, e) {
  372. e->ReportError();
  373. e->Delete();
  374. return FALSE;
  375. }
  376. END_CATCH
  377. for (int i = 0; i < m_nRows; i++) m_arRowHeights[i] = m_nDefCellHeight;
  378. for (i = 0; i < m_nCols; i++) m_arColWidths[i] = m_nDefCellWidth;
  379. ResetScrollBars();
  380. return TRUE;
  381. }
  382. void CGridCtrl::PreSubclassWindow()
  383. {
  384. CWnd::PreSubclassWindow();
  385. HFONT hFont = ::CreateFontIndirect(&m_Logfont);
  386. OnSetFont((LPARAM)hFont, 0);
  387. DeleteObject(hFont);
  388. ResetScrollBars();
  389. }
  390. BOOL CGridCtrl::SubclassWindow(HWND hWnd)
  391. {
  392. if (!CWnd::SubclassWindow(hWnd))
  393. return FALSE;
  394. #ifndef GRIDCONTROL_NO_DRAGDROP
  395. m_DropTarget.Register(this);
  396. #endif
  397. #ifndef GRIDCONTROL_NO_TITLETIPS
  398. if (m_bTitleTips && !IsWindow(m_TitleTip.m_hWnd))
  399. m_TitleTip.Create(this);
  400. #endif
  401. return TRUE;
  402. }
  403. LRESULT CGridCtrl::SendMessageToParent(int nRow, int nCol, int nMessage)
  404. {
  405. if (!IsWindow(m_hWnd))
  406. return 0;
  407. NM_GRIDVIEW nmgv;
  408. nmgv.iRow = nRow;
  409. nmgv.iColumn = nCol;
  410. nmgv.hdr.hwndFrom = m_hWnd;
  411. nmgv.hdr.idFrom = GetDlgCtrlID();
  412. nmgv.hdr.code = nMessage;
  413. CWnd *pOwner = GetOwner();
  414. if (pOwner && IsWindow(pOwner->m_hWnd))
  415. return pOwner->SendMessage(WM_NOTIFY, nmgv.hdr.idFrom, (LPARAM)&nmgv);
  416. else
  417. return 0;
  418. }
  419. BEGIN_MESSAGE_MAP(CGridCtrl, CWnd)
  420. //{{AFX_MSG_MAP(CGridCtrl)
  421. ON_WM_PAINT()
  422. ON_WM_HSCROLL()
  423. ON_WM_VSCROLL()
  424. ON_WM_SIZE()
  425. ON_WM_LBUTTONUP()
  426. ON_WM_LBUTTONDOWN()
  427. ON_WM_MOUSEMOVE()
  428. ON_WM_TIMER()
  429. ON_WM_GETDLGCODE()
  430. ON_WM_KEYDOWN()
  431. ON_WM_CHAR()
  432. ON_WM_LBUTTONDBLCLK()
  433. ON_WM_ERASEBKGND()
  434. ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
  435. ON_UPDATE_COMMAND_UI(ID_EDIT_SELECT_ALL, OnUpdateEditSelectAll)
  436. //}}AFX_MSG_MAP
  437. #ifndef _WIN32_WCE_NO_CURSOR
  438. ON_WM_SETCURSOR()
  439. #endif
  440. #ifndef _WIN32_WCE
  441. ON_WM_SYSCOLORCHANGE()
  442. ON_WM_CAPTURECHANGED()
  443. #endif
  444. #ifndef GRIDCONTROL_NO_CLIPBOARD
  445. ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  446. ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
  447. ON_COMMAND(ID_EDIT_CUT, OnEditCut)
  448. ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
  449. ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
  450. ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
  451. #endif
  452. #if !defined(_WIN32_WCE) && (_MFC_VER >= 0x0421)
  453. ON_WM_MOUSEWHEEL()
  454. #endif
  455. #if (_WIN32_WCE >= 210)
  456. ON_WM_SETTINGCHANGE()
  457. #endif
  458. ON_MESSAGE(WM_SETFONT, OnSetFont)
  459. ON_MESSAGE(WM_GETFONT, OnGetFont)
  460. ON_NOTIFY(GVN_ENDLABELEDIT, IDC_INPLACE_CONTROL, OnEndInPlaceEdit)
  461. END_MESSAGE_MAP()
  462. /////////////////////////////////////////////////////////////////////////////
  463. // CGridCtrl message handlers
  464. void CGridCtrl::OnPaint()
  465. {
  466. CPaintDC dc(this); // device context for painting
  467. if (m_bDoubleBuffer) // Use a memory DC to remove flicker
  468. {
  469. CMemDC MemDC(&dc);
  470. OnDraw(&MemDC);
  471. }
  472. else // Draw raw - this helps in debugging vis problems.
  473. OnDraw(&dc);
  474. }
  475. BOOL CGridCtrl::OnEraseBkgnd(CDC* /*pDC*/)
  476. {
  477. return TRUE; // Don't erase the background.
  478. }
  479. // Custom background erasure. This gets called from within the OnDraw function,
  480. // since we will (most likely) be using a memory DC to stop flicker. If we just
  481. // erase the background normally through OnEraseBkgnd, and didn't fill the memDC's
  482. // selected bitmap with colour, then all sorts of vis problems would occur
  483. void CGridCtrl::EraseBkgnd(CDC* pDC)
  484. {
  485. CRect VisRect, ClipRect, rect;
  486. CBrush FixedBack(GetFixedBkColor()),
  487. TextBack(GetTextBkColor()),
  488. Back(GetBkColor());
  489. if (pDC->GetClipBox(ClipRect) == ERROR)
  490. return;
  491. GetVisibleNonFixedCellRange(VisRect);
  492. // Draw Fixed columns background
  493. int nFixedColumnWidth = GetFixedColumnWidth();
  494. if (ClipRect.left < nFixedColumnWidth && ClipRect.top < VisRect.bottom)
  495. pDC->FillRect(CRect(ClipRect.left, ClipRect.top,
  496. nFixedColumnWidth, VisRect.bottom),
  497. &FixedBack);
  498. // Draw Fixed rows background
  499. int nFixedRowHeight = GetFixedRowHeight();
  500. if (ClipRect.top < nFixedRowHeight &&
  501. ClipRect.right > nFixedColumnWidth && ClipRect.left < VisRect.right)
  502. pDC->FillRect(CRect(nFixedColumnWidth-1, ClipRect.top,
  503. VisRect.right, nFixedRowHeight),
  504. &FixedBack);
  505. // Draw non-fixed cell background
  506. if (rect.IntersectRect(VisRect, ClipRect))
  507. {
  508. CRect CellRect(max(nFixedColumnWidth, rect.left),
  509. max(nFixedRowHeight, rect.top),
  510. rect.right, rect.bottom);
  511. pDC->FillRect(CellRect, &TextBack);
  512. }
  513. // Draw right hand side of window outside grid
  514. if (VisRect.right < ClipRect.right)
  515. pDC->FillRect(CRect(VisRect.right, ClipRect.top,
  516. ClipRect.right, ClipRect.bottom),
  517. &Back);
  518. // Draw bottom of window below grid
  519. if (VisRect.bottom < ClipRect.bottom && ClipRect.left < VisRect.right)
  520. pDC->FillRect(CRect(ClipRect.left, VisRect.bottom,
  521. VisRect.right, ClipRect.bottom),
  522. &Back);
  523. }
  524. void CGridCtrl::OnSize(UINT nType, int cx, int cy)
  525. {
  526. static BOOL bAlreadyInsideThisProcedure = FALSE;
  527. if (bAlreadyInsideThisProcedure)
  528. return;
  529. if (!::IsWindow(m_hWnd))
  530. return;
  531. // Start re-entry blocking
  532. bAlreadyInsideThisProcedure = TRUE;
  533. // if (::IsWindow(GetSafeHwnd()) && GetFocus()->GetSafeHwnd() != GetSafeHwnd())
  534. SetFocus(); // Auto-destroy any InPlaceEdit's
  535. CWnd::OnSize(nType, cx, cy);
  536. ResetScrollBars();
  537. // End re-entry blocking
  538. bAlreadyInsideThisProcedure = FALSE;
  539. }
  540. UINT CGridCtrl::OnGetDlgCode()
  541. {
  542. UINT nCode = DLGC_WANTARROWS | DLGC_WANTCHARS; // DLGC_WANTALLKEYS; //
  543. if (m_bHandleTabKey && !IsCTRLpressed())
  544. nCode |= DLGC_WANTTAB;
  545. return nCode;
  546. }
  547. #ifndef _WIN32_WCE
  548. // If system colours change, then redo colours
  549. void CGridCtrl::OnSysColorChange()
  550. {
  551. CWnd::OnSysColorChange();
  552. if (GetTextColor() == m_crWindowText) // Still using system colours
  553. SetTextColor(::GetSysColor(COLOR_WINDOWTEXT)); // set to new system colour
  554. if (GetTextBkColor() == m_crWindowColour)
  555. SetTextBkColor(::GetSysColor(COLOR_WINDOW));
  556. if (GetBkColor() == m_crShadow)
  557. SetBkColor(::GetSysColor(COLOR_3DSHADOW));
  558. if (GetFixedTextColor() == m_crWindowText)
  559. SetFixedTextColor(::GetSysColor(COLOR_WINDOWTEXT));
  560. if (GetFixedBkColor() == m_cr3DFace)
  561. SetFixedBkColor(::GetSysColor(COLOR_3DFACE));
  562. m_crWindowText = ::GetSysColor(COLOR_WINDOWTEXT);
  563. m_crWindowColour = ::GetSysColor(COLOR_WINDOW);
  564. m_cr3DFace = ::GetSysColor(COLOR_3DFACE);
  565. m_crShadow = ::GetSysColor(COLOR_3DSHADOW);
  566. }
  567. #endif
  568. #ifndef _WIN32_WCE_NO_CURSOR
  569. // If we are drag-selecting cells, or drag and dropping, stop now
  570. void CGridCtrl::OnCaptureChanged(CWnd *pWnd)
  571. {
  572. if (pWnd->GetSafeHwnd() == GetSafeHwnd()) return;
  573. // kill timer if active
  574. if (m_nTimerID != 0)
  575. {
  576. KillTimer(m_nTimerID);
  577. m_nTimerID = 0;
  578. }
  579. #ifndef GRIDCONTROL_NO_DRAGDROP
  580. // Kill drag and drop if active
  581. if (m_MouseMode == MOUSE_DRAGGING)
  582. m_MouseMode = MOUSE_NOTHING;
  583. #endif
  584. }
  585. #endif
  586. #if (_MFC_VER >= 0x0421) || (_WIN32_WCE >= 210)
  587. // If system settings change, then redo colours
  588. void CGridCtrl::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
  589. {
  590. CWnd::OnSettingChange(uFlags, lpszSection);
  591. if (GetTextColor() == m_crWindowText) // Still using system colours
  592. SetTextColor(::GetSysColor(COLOR_WINDOWTEXT)); // set to new system colour
  593. if (GetTextBkColor() == m_crWindowColour)
  594. SetTextBkColor(::GetSysColor(COLOR_WINDOW));
  595. if (GetBkColor() == m_crShadow)
  596. SetBkColor(::GetSysColor(COLOR_3DSHADOW));
  597. if (GetFixedTextColor() == m_crWindowText)
  598. SetFixedTextColor(::GetSysColor(COLOR_WINDOWTEXT));
  599. if (GetFixedBkColor() == m_cr3DFace)
  600. SetFixedBkColor(::GetSysColor(COLOR_3DFACE));
  601. m_crWindowText = ::GetSysColor(COLOR_WINDOWTEXT);
  602. m_crWindowColour = ::GetSysColor(COLOR_WINDOW);
  603. m_cr3DFace = ::GetSysColor(COLOR_3DFACE);
  604. m_crShadow = ::GetSysColor(COLOR_3DSHADOW);
  605. m_nRowsPerWheelNotch = GetMouseScrollLines(); // Get the number of lines
  606. }
  607. #endif
  608. // For drag-selection. Scrolls hidden cells into view
  609. // TODO: decrease timer interval over time to speed up selection over time
  610. void CGridCtrl::OnTimer(UINT nIDEvent)
  611. {
  612. ASSERT(nIDEvent == WM_LBUTTONDOWN);
  613. if (nIDEvent != WM_LBUTTONDOWN)
  614. return;
  615. CPoint pt, origPt;
  616. #ifdef _WIN32_WCE
  617. if (m_MouseMode == MOUSE_NOTHING)
  618. return;
  619. origPt = GetMessagePos();
  620. #else
  621. if (!GetCursorPos(&origPt))
  622. return;
  623. #endif
  624. ScreenToClient(&origPt);
  625. CRect rect;
  626. GetClientRect(rect);
  627. int nFixedRowHeight = GetFixedRowHeight();
  628. int nFixedColWidth = GetFixedColumnWidth();
  629. pt = origPt;
  630. if (pt.y > rect.bottom)
  631. {
  632. //SendMessage(WM_VSCROLL, SB_LINEDOWN, 0);
  633. SendMessage(WM_KEYDOWN, VK_DOWN, 0);
  634. if (pt.x < rect.left)
  635. pt.x = rect.left;
  636. if (pt.x > rect.right)
  637. pt.x = rect.right;
  638. pt.y = rect.bottom;
  639. OnSelecting(GetCellFromPt(pt));
  640. }
  641. else if (pt.y < nFixedRowHeight)
  642. {
  643. //SendMessage(WM_VSCROLL, SB_LINEUP, 0);
  644. SendMessage(WM_KEYDOWN, VK_UP, 0);
  645. if (pt.x < rect.left)
  646. pt.x = rect.left;
  647. if (pt.x > rect.right)
  648. pt.x = rect.right;
  649. pt.y = nFixedRowHeight + 1;
  650. OnSelecting(GetCellFromPt(pt));
  651. }
  652. pt = origPt;
  653. if (pt.x > rect.right)
  654. {
  655. // SendMessage(WM_HSCROLL, SB_LINERIGHT, 0);
  656. SendMessage(WM_KEYDOWN, VK_RIGHT, 0);
  657. if (pt.y < rect.top)
  658. pt.y = rect.top;
  659. if (pt.y > rect.bottom)
  660. pt.y = rect.bottom;
  661. pt.x = rect.right;
  662. OnSelecting(GetCellFromPt(pt));
  663. }
  664. else if (pt.x < nFixedColWidth)
  665. {
  666. //SendMessage(WM_HSCROLL, SB_LINELEFT, 0);
  667. SendMessage(WM_KEYDOWN, VK_LEFT, 0);
  668. if (pt.y < rect.top)
  669. pt.y = rect.top;
  670. if (pt.y > rect.bottom)
  671. pt.y = rect.bottom;
  672. pt.x = nFixedColWidth + 1;
  673. OnSelecting(GetCellFromPt(pt));
  674. }
  675. }
  676. // move about with keyboard
  677. void CGridCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  678. {
  679. if (!IsValid(m_idCurrentCell))
  680. {
  681. CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
  682. return;
  683. }
  684. CCellID next = m_idCurrentCell;
  685. BOOL bChangeLine = FALSE;
  686. if (IsCTRLpressed())
  687. {
  688. switch (nChar)
  689. {
  690. case 'A': OnEditSelectAll(); break;
  691. #ifndef GRIDCONTROL_NO_CLIPBOARD
  692. case 'X': OnEditCut(); break;
  693. case 'C': OnEditCopy(); break;
  694. case 'V': OnEditPaste(); break;
  695. #endif
  696. }
  697. }
  698. switch (nChar)
  699. {
  700. case VK_DELETE:
  701. if (IsCellEditable(m_idCurrentCell.row, m_idCurrentCell.col))
  702. {
  703. SendMessageToParent(m_idCurrentCell.row, m_idCurrentCell.col, GVN_BEGINLABELEDIT);
  704. SetItemText(m_idCurrentCell.row, m_idCurrentCell.col, _T(""));
  705. SetModified(TRUE, m_idCurrentCell.row, m_idCurrentCell.col);
  706. SendMessageToParent(m_idCurrentCell.row, m_idCurrentCell.col, GVN_ENDLABELEDIT);
  707. RedrawCell(m_idCurrentCell);
  708. }
  709. break;
  710. case VK_TAB:
  711. if (IsSHIFTpressed())
  712. {
  713. if (next.col > m_nFixedCols)
  714. next.col--;
  715. else if (next.col == m_nFixedCols && next.row > m_nFixedRows)
  716. {
  717. next.row--;
  718. next.col = GetColumnCount() - 1;
  719. bChangeLine = TRUE;
  720. }
  721. else
  722. CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
  723. }
  724. else
  725. {
  726. if (next.col < (GetColumnCount() - 1))
  727. next.col++;
  728. else if (next.col == (GetColumnCount() - 1) &&
  729. next.row < (GetRowCount() - 1) )
  730. {
  731. next.row++;
  732. next.col = m_nFixedCols;
  733. bChangeLine = TRUE;
  734. }
  735. else
  736. CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
  737. }
  738. break;
  739. case VK_DOWN:
  740. if (next.row < (GetRowCount() - 1))
  741. next.row++;
  742. break;
  743. case VK_UP:
  744. if (next.row > m_nFixedRows)
  745. next.row--;
  746. break;
  747. case VK_RIGHT:
  748. if (next.col < (GetColumnCount() - 1))
  749. next.col++;
  750. break;
  751. case VK_LEFT:
  752. if (next.col > m_nFixedCols)
  753. next.col--;
  754. break;
  755. case VK_NEXT:
  756. {
  757. CCellID idOldTopLeft = GetTopleftNonFixedCell();
  758. SendMessage(WM_VSCROLL, SB_PAGEDOWN, 0);
  759. CCellID idNewTopLeft = GetTopleftNonFixedCell();
  760. int increment = idNewTopLeft.row - idOldTopLeft.row;
  761. if (increment) {
  762. next.row += increment;
  763. if (next.row > (GetRowCount() - 1))
  764. next.row = GetRowCount() - 1;
  765. }
  766. else
  767. next.row = GetRowCount() - 1;
  768. break;
  769. }
  770. case VK_PRIOR:
  771. {
  772. CCellID idOldTopLeft = GetTopleftNonFixedCell();
  773. SendMessage(WM_VSCROLL, SB_PAGEUP, 0);
  774. CCellID idNewTopLeft = GetTopleftNonFixedCell();
  775. int increment = idNewTopLeft.row - idOldTopLeft.row;
  776. if (increment)
  777. {
  778. next.row += increment;
  779. if (next.row < m_nFixedRows)
  780. next.row = m_nFixedRows;
  781. } else
  782. next.row = m_nFixedRows;
  783. break;
  784. }
  785. case VK_HOME:
  786. SendMessage(WM_VSCROLL, SB_TOP, 0);
  787. next.row = m_nFixedRows;
  788. break;
  789. case VK_END:
  790. SendMessage(WM_VSCROLL, SB_BOTTOM, 0);
  791. next.row = GetRowCount() - 1;
  792. break;
  793. default:
  794. CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
  795. }
  796. if (next != m_idCurrentCell)
  797. {
  798. // While moving with the Cursorkeys the current ROW/CELL will get selected
  799. // OR Selection will get expanded when SHIFT is pressed
  800. // Cut n paste from OnLButtonDown - Franco Bez
  801. // Added check for NULL mouse mode - Chris Maunder.
  802. if (m_MouseMode == MOUSE_NOTHING)
  803. {
  804. m_PrevSelectedCellMap.RemoveAll();
  805. m_MouseMode = m_bListMode? MOUSE_SELECT_ROW : MOUSE_SELECT_CELLS;
  806. if (!IsSHIFTpressed() || nChar == VK_TAB)
  807. m_SelectionStartCell = next;
  808. OnSelecting(next);
  809. m_MouseMode = MOUSE_NOTHING;
  810. }
  811. SetFocusCell(next);
  812. if (!IsCellVisible(next))
  813. {
  814. EnsureVisible(next); // Make sure cell is visible
  815. switch (nChar) {
  816. case VK_RIGHT:
  817. SendMessage(WM_HSCROLL, SB_LINERIGHT, 0);
  818. break;
  819. case VK_LEFT:
  820. SendMessage(WM_HSCROLL, SB_LINELEFT, 0);
  821. break;
  822. case VK_DOWN:
  823. SendMessage(WM_VSCROLL, SB_LINEDOWN, 0);
  824. break;
  825. case VK_UP:
  826. SendMessage(WM_VSCROLL, SB_LINEUP, 0);
  827. break;
  828. case VK_TAB:
  829. if (IsSHIFTpressed())
  830. {
  831. if (bChangeLine)
  832. {
  833. SendMessage(WM_VSCROLL, SB_LINEUP, 0);
  834. SetScrollPos32(SB_HORZ, m_nHScrollMax);
  835. break;
  836. }
  837. else
  838. SendMessage(WM_HSCROLL, SB_LINELEFT, 0);
  839. }
  840. else
  841. {
  842. if (bChangeLine)
  843. {
  844. SendMessage(WM_VSCROLL, SB_LINEDOWN, 0);
  845. SetScrollPos32(SB_HORZ, 0);
  846. break;
  847. }
  848. else
  849. SendMessage(WM_HSCROLL, SB_LINERIGHT, 0);
  850. }
  851. break;
  852. }
  853. Invalidate();
  854. }
  855. }
  856. }
  857. // Instant editing of cells when keys are pressed
  858. void CGridCtrl::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  859. {
  860. if (!IsCTRLpressed() && m_MouseMode == MOUSE_NOTHING)
  861. {
  862. if (!m_bHandleTabKey || (m_bHandleTabKey && nChar != VK_TAB))
  863. OnEditCell(m_idCurrentCell.row, m_idCurrentCell.col, nChar);
  864. }
  865. CWnd::OnChar(nChar, nRepCnt, nFlags);
  866. }
  867. // Callback from any CInPlaceEdits that ended. This just calls OnEndEditCell,
  868. // refreshes the edited cell and moves onto next cell if the return character
  869. // from the edit says we should.
  870. void CGridCtrl::OnEndInPlaceEdit(NMHDR* pNMHDR, LRESULT* pResult)
  871. {
  872. GV_DISPINFO *pgvDispInfo = (GV_DISPINFO *)pNMHDR;
  873. GV_ITEM *pgvItem = &pgvDispInfo->item;
  874. // In case OnEndInPlaceEdit called as window is being destroyed
  875. if (!IsWindow(GetSafeHwnd()))
  876. return;
  877. OnEndEditCell(pgvItem->row, pgvItem->col, pgvItem->szText);
  878. InvalidateCellRect(CCellID(pgvItem->row, pgvItem->col));
  879. SendMessageToParent(pgvItem->row, pgvItem->col, GVN_ENDLABELEDIT);
  880. switch (pgvItem->lParam)
  881. {
  882. case VK_TAB:
  883. case VK_DOWN:
  884. case VK_UP:
  885. case VK_RIGHT:
  886. case VK_LEFT:
  887. case VK_NEXT:
  888. case VK_PRIOR:
  889. case VK_HOME:
  890. case VK_END:
  891. OnKeyDown(pgvItem->lParam, 0, 0);
  892. OnEditCell(m_idCurrentCell.row, m_idCurrentCell.col, pgvItem->lParam);
  893. }
  894. *pResult = 0;
  895. }
  896. // Handle horz scrollbar notifications
  897. void CGridCtrl::OnHScroll(UINT nSBCode, UINT /*nPos*/, CScrollBar* /*pScrollBar*/)
  898. {
  899. if (GetFocus()->GetSafeHwnd() != GetSafeHwnd())
  900. SetFocus(); // Auto-destroy any InPlaceEdit's
  901. #ifndef GRIDCONTROL_NO_TITLETIPS
  902. m_TitleTip.Hide(); // hide any titletips
  903. #endif
  904. int scrollPos = GetScrollPos32(SB_HORZ);
  905. CCellID idTopLeft = GetTopleftNonFixedCell();
  906. CRect rect;
  907. GetClientRect(rect);
  908. switch (nSBCode)
  909. {
  910. case SB_LINERIGHT:
  911. if (scrollPos < m_nHScrollMax)
  912. {
  913. int xScroll = GetColumnWidth(idTopLeft.col);
  914. SetScrollPos32(SB_HORZ, scrollPos + xScroll);
  915. if (GetScrollPos32(SB_HORZ) == scrollPos) break; // didn't work
  916. rect.left = GetFixedColumnWidth() + xScroll;
  917. ScrollWindow(-xScroll, 0, rect);
  918. rect.left = rect.right - xScroll;
  919. InvalidateRect(rect);
  920. }
  921. break;
  922. case SB_LINELEFT:
  923. if (scrollPos > 0 && idTopLeft.col > GetFixedColumnCount())
  924. {
  925. int xScroll = GetColumnWidth(idTopLeft.col-1);
  926. SetScrollPos32(SB_HORZ, max(0,scrollPos - xScroll));
  927. rect.left = GetFixedColumnWidth();
  928. ScrollWindow(xScroll, 0, rect);
  929. rect.right = rect.left + xScroll;
  930. InvalidateRect(rect);
  931. }
  932. break;
  933. case SB_PAGERIGHT:
  934. if (scrollPos < m_nHScrollMax)
  935. {
  936. rect.left = GetFixedColumnWidth();
  937. int offset = rect.Width();
  938. int pos = min(m_nHScrollMax, scrollPos + offset);
  939. SetScrollPos32(SB_HORZ, pos);
  940. rect.left = GetFixedColumnWidth();
  941. InvalidateRect(rect);
  942. }
  943. break;
  944. case SB_PAGELEFT:
  945. if (scrollPos > 0)
  946. {
  947. rect.left = GetFixedColumnWidth();
  948. int offset = -rect.Width();
  949. int pos = max(0, scrollPos + offset);
  950. SetScrollPos32(SB_HORZ, pos);
  951. rect.left = GetFixedColumnWidth();
  952. InvalidateRect(rect);
  953. }
  954. break;
  955. case SB_THUMBPOSITION:
  956. case SB_THUMBTRACK:
  957. {
  958. SetScrollPos32(SB_HORZ, GetScrollPos32(SB_HORZ, TRUE));
  959. CCellID idNewTopLeft = GetTopleftNonFixedCell();
  960. if (idNewTopLeft != idTopLeft)
  961. {
  962. rect.left = GetFixedColumnWidth();
  963. InvalidateRect(rect);
  964. }
  965. }
  966. break;
  967. case SB_LEFT:
  968. if (scrollPos > 0)
  969. {
  970. SetScrollPos32(SB_HORZ, 0);
  971. Invalidate();
  972. }
  973. break;
  974. case SB_RIGHT:
  975. if (scrollPos < m_nHScrollMax)
  976. {
  977. SetScrollPos32(SB_HORZ, m_nHScrollMax);
  978. Invalidate();
  979. }
  980. break;
  981. default: break;
  982. }
  983. }
  984. // Handle vert scrollbar notifications
  985. void CGridCtrl::OnVScroll(UINT nSBCode, UINT /*nPos*/, CScrollBar* /*pScrollBar*/)
  986. {
  987. if (GetFocus()->GetSafeHwnd() != GetSafeHwnd())
  988. SetFocus(); // Auto-destroy any InPlaceEdit's
  989. #ifndef GRIDCONTROL_NO_TITLETIPS
  990. m_TitleTip.Hide(); // hide any titletips
  991. #endif
  992. // Get the scroll position ourselves to ensure we get a 32 bit value
  993. int scrollPos = GetScrollPos32(SB_VERT);
  994. CCellID idTopLeft = GetTopleftNonFixedCell();
  995. CRect rect;
  996. GetClientRect(rect);
  997. switch (nSBCode)
  998. {
  999. case SB_LINEDOWN:
  1000. if (scrollPos < m_nVScrollMax)
  1001. {
  1002. int yScroll = GetRowHeight(idTopLeft.row);
  1003. SetScrollPos32(SB_VERT, scrollPos + yScroll);
  1004. if (GetScrollPos32(SB_VERT) == scrollPos)
  1005. break; // didn't work
  1006. rect.top = GetFixedRowHeight() + yScroll;
  1007. ScrollWindow( 0, -yScroll, rect);
  1008. rect.top = rect.bottom - yScroll;
  1009. InvalidateRect(rect);
  1010. }
  1011. break;
  1012. case SB_LINEUP:
  1013. if (scrollPos > 0 && idTopLeft.row > GetFixedRowCount())
  1014. {
  1015. int yScroll = GetRowHeight(idTopLeft.row-1);
  1016. SetScrollPos32(SB_VERT, max(0, scrollPos - yScroll));
  1017. rect.top = GetFixedRowHeight();
  1018. ScrollWindow(0, yScroll, rect);
  1019. rect.bottom = rect.top + yScroll;
  1020. InvalidateRect(rect);
  1021. }
  1022. break;
  1023. case SB_PAGEDOWN:
  1024. if (scrollPos < m_nVScrollMax)
  1025. {
  1026. rect.top = GetFixedRowHeight();
  1027. scrollPos = min(m_nVScrollMax, scrollPos + rect.Height());
  1028. SetScrollPos32(SB_VERT, scrollPos);
  1029. rect.top = GetFixedRowHeight();
  1030. InvalidateRect(rect);
  1031. }
  1032. break;
  1033. case SB_PAGEUP:
  1034. if (scrollPos > 0)
  1035. {
  1036. rect.top = GetFixedRowHeight();
  1037. int offset = -rect.Height();
  1038. int pos = max(0, scrollPos + offset);
  1039. SetScrollPos32(SB_VERT, pos);
  1040. rect.top = GetFixedRowHeight();
  1041. InvalidateRect(rect);
  1042. }
  1043. break;
  1044. case SB_THUMBPOSITION:
  1045. case SB_THUMBTRACK:
  1046. {
  1047. SetScrollPos32(SB_VERT, GetScrollPos32(SB_VERT, TRUE));
  1048. CCellID idNewTopLeft = GetTopleftNonFixedCell();
  1049. if (idNewTopLeft != idTopLeft)
  1050. {
  1051. rect.top = GetFixedRowHeight();
  1052. InvalidateRect(rect);
  1053. }
  1054. }
  1055. break;
  1056. case SB_TOP:
  1057. if (scrollPos > 0)
  1058. {
  1059. SetScrollPos32(SB_VERT, 0);
  1060. Invalidate();
  1061. }
  1062. break;
  1063. case SB_BOTTOM:
  1064. if (scrollPos < m_nVScrollMax)
  1065. {
  1066. SetScrollPos32(SB_VERT, m_nVScrollMax);
  1067. Invalidate();
  1068. }
  1069. default: break;
  1070. }
  1071. }
  1072. /////////////////////////////////////////////////////////////////////////////
  1073. // CGridCtrl implementation functions
  1074. void CGridCtrl::OnDraw(CDC* pDC)
  1075. {
  1076. CRect rect;
  1077. int row,col;
  1078. CRect clipRect;
  1079. if (pDC->GetClipBox(&clipRect) == ERROR)
  1080. return;
  1081. EraseBkgnd(pDC); // OnEraseBkgnd does nothing, so erase bkgnd here.
  1082. // This necessary since we may be using a Memory DC.
  1083. int nFixedRowHeight = GetFixedRowHeight();
  1084. int nFixedColWidth = GetFixedColumnWidth();
  1085. CCellID idTopLeft = GetTopleftNonFixedCell();
  1086. int minVisibleRow = idTopLeft.row,
  1087. minVisibleCol = idTopLeft.col;
  1088. CRect VisRect;
  1089. CCellRange VisCellRange = GetVisibleNonFixedCellRange(VisRect);
  1090. int maxVisibleRow = VisCellRange.GetMaxRow(),
  1091. maxVisibleCol = VisCellRange.GetMaxCol();
  1092. // draw top-left cells 0..m_nFixedRows-1, 0..m_nFixedCols-1
  1093. rect.bottom = -1;
  1094. for (row = 0; row < m_nFixedRows; row++)
  1095. {
  1096. rect.top = rect.bottom+1;
  1097. rect.bottom = rect.top + GetRowHeight(row)-1;
  1098. rect.right = -1;
  1099. for (col = 0; col < m_nFixedCols; col++)
  1100. {
  1101. rect.left = rect.right+1;
  1102. rect.right = rect.left + GetColumnWidth(col)-1;
  1103. DrawFixedCell(pDC, row, col, rect);
  1104. }
  1105. }
  1106. // draw fixed column cells: m_nFixedRows..n, 0..m_nFixedCols-1
  1107. rect.bottom = nFixedRowHeight-1;
  1108. for (row = minVisibleRow; row <= maxVisibleRow; row++)
  1109. {
  1110. rect.top = rect.bottom+1;
  1111. rect.bottom = rect.top + GetRowHeight(row)-1;
  1112. // rect.bottom = bottom pixel of previous row
  1113. if (rect.top > clipRect.bottom)
  1114. break; // Gone past cliprect
  1115. if (rect.bottom < clipRect.top)
  1116. continue; // Reached cliprect yet?
  1117. rect.right = -1;
  1118. for (col = 0; col < m_nFixedCols; col++)
  1119. {
  1120. rect.left = rect.right+1;
  1121. rect.right = rect.left + GetColumnWidth(col)-1;
  1122. if (rect.left > clipRect.right)
  1123. break; // gone past cliprect
  1124. if (rect.right < clipRect.left)
  1125. continue; // Reached cliprect yet?
  1126. DrawFixedCell(pDC, row, col, rect);
  1127. }
  1128. }
  1129. // draw fixed row cells 0..m_nFixedRows, m_nFixedCols..n
  1130. rect.bottom = -1;
  1131. for (row = 0; row < m_nFixedRows; row++)
  1132. {
  1133. rect.top = rect.bottom+1;
  1134. rect.bottom = rect.top + GetRowHeight(row)-1;
  1135. // rect.bottom = bottom pixel of previous row
  1136. if (rect.top > clipRect.bottom)
  1137. break; // Gone past cliprect
  1138. if (rect.bottom < clipRect.top)
  1139. continue; // Reached cliprect yet?
  1140. rect.right = nFixedColWidth-1;
  1141. for (col = minVisibleCol; col <= maxVisibleCol; col++)
  1142. {
  1143. rect.left = rect.right+1;
  1144. rect.right = rect.left + GetColumnWidth(col)-1;
  1145. if (rect.left > clipRect.right)
  1146. break; // gone past cliprect
  1147. if (rect.right < clipRect.left)
  1148. continue; // Reached cliprect yet?
  1149. DrawFixedCell(pDC, row, col, rect);
  1150. }
  1151. }
  1152. // draw rest of non-fixed cells
  1153. rect.bottom = nFixedRowHeight-1;
  1154. for (row = minVisibleRow; row <= maxVisibleRow; row++)
  1155. {
  1156. rect.top = rect.bottom+1;
  1157. rect.bottom = rect.top + GetRowHeight(row)-1;
  1158. // rect.bottom = bottom pixel of previous row
  1159. if (rect.top > clipRect.bottom)
  1160. break; // Gone past cliprect
  1161. if (rect.bottom < clipRect.top)
  1162. continue; // Reached cliprect yet?
  1163. rect.right = nFixedColWidth-1;
  1164. for (col = minVisibleCol; col <= maxVisibleCol; col++)
  1165. {
  1166. rect.left = rect.right+1;
  1167. rect.right = rect.left + GetColumnWidth(col)-1;
  1168. if (rect.left > clipRect.right)
  1169. break; // gone past cliprect
  1170. if (rect.right < clipRect.left)
  1171. continue; // Reached cliprect yet?
  1172. DrawCell(pDC, row, col, rect);
  1173. }
  1174. }
  1175. CPen pen;
  1176. TRY {
  1177. pen.CreatePen(PS_SOLID, 0, m_crGridColour);
  1178. }
  1179. CATCH (CResourceException, e)
  1180. {
  1181. e->Delete();
  1182. return;
  1183. }
  1184. END_CATCH
  1185. pDC->SelectObject(&pen);
  1186. // draw vertical lines (drawn at ends of cells)
  1187. if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_VERT)
  1188. {
  1189. int x = nFixedColWidth;
  1190. for (col = minVisibleCol; col <= maxVisibleCol; col++) {
  1191. x += GetColumnWidth(col);
  1192. pDC->MoveTo(x-1, nFixedRowHeight);
  1193. pDC->LineTo(x-1, VisRect.bottom);
  1194. }
  1195. }
  1196. // draw horizontal lines (drawn at bottom of each cell)
  1197. if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_HORZ)
  1198. {
  1199. int y = nFixedRowHeight;
  1200. for (row = minVisibleRow; row <= maxVisibleRow; row++) {
  1201. y += GetRowHeight(row);
  1202. pDC->MoveTo(nFixedColWidth, y-1);
  1203. pDC->LineTo(VisRect.right, y-1);
  1204. }
  1205. }
  1206. pDC->SelectStockObject(NULL_PEN);
  1207. #ifdef USE_MEMDC // Use a memDC for flicker free update
  1208. }
  1209. #else // Use normal DC - this helps in debugging
  1210. }
  1211. #endif
  1212. ////////////////////////////////////////////////////////////////////////////////////////
  1213. // CGridCtrl Cell selection stuff
  1214. BOOL CGridCtrl::IsValid(int nRow, int nCol) const
  1215. {
  1216. return (nRow >= 0 && nRow < m_nRows && nCol >= 0 && nCol < m_nCols);
  1217. }
  1218. BOOL CGridCtrl::IsValid(const CCellID& cell) const
  1219. {
  1220. return IsValid(cell.row, cell.col);
  1221. }
  1222. BOOL CGridCtrl::IsValid(const CCellRange& range) const
  1223. {
  1224. return (range.GetMinRow() >= 0 && range.GetMinCol() >= 0 &&
  1225. range.GetMaxRow() >= 0 && range.GetMaxCol() >= 0 &&
  1226. range.GetMaxRow() < m_nRows && range.GetMaxCol() < m_nCols &&
  1227. range.GetMinRow() <= range.GetMaxRow() && range.GetMinCol() <= range.GetMaxCol());
  1228. }
  1229. // Enables/Disables redraw for certain operations like columns auto-sizing etc,
  1230. // but not for user caused things such as selection changes.
  1231. void CGridCtrl::SetRedraw(BOOL bAllowDraw, BOOL bResetScrollBars /* = FALSE */)
  1232. {
  1233. TRACE(_T("%s: Setting redraw to %s\n"),
  1234. GetRuntimeClass()->m_lpszClassName, bAllowDraw? _T("TRUE") : _T("FALSE"));
  1235. if (bAllowDraw && !m_bAllowDraw)
  1236. Invalidate();
  1237. m_bAllowDraw = bAllowDraw;
  1238. if (bResetScrollBars)
  1239. ResetScrollBars();
  1240. }
  1241. // Forces a redraw of a cell immediately (using a direct DC construction,
  1242. // or the supplied dc)
  1243. BOOL CGridCtrl::RedrawCell(const CCellID& cell, CDC* pDC /* = NULL */)
  1244. {
  1245. return RedrawCell(cell.row, cell.col, pDC);
  1246. }
  1247. BOOL CGridCtrl::RedrawCell(int nRow, int nCol, CDC* pDC /* = NULL */)
  1248. {
  1249. BOOL bResult = TRUE;
  1250. BOOL bMustReleaseDC = FALSE;
  1251. if (!m_bAllowDraw || !IsCellVisible(nRow, nCol))
  1252. return FALSE;
  1253. CRect rect;
  1254. if (!GetCellRect(nRow, nCol, rect))
  1255. return FALSE;
  1256. if (!pDC) {
  1257. pDC = GetDC();
  1258. if (pDC)
  1259. bMustReleaseDC = TRUE;
  1260. }
  1261. if (pDC)
  1262. {
  1263. // Redraw cells directly
  1264. if (nRow < m_nFixedRows || nCol < m_nFixedCols)
  1265. {
  1266. bResult = DrawFixedCell(pDC, nRow, nCol, rect, TRUE);
  1267. }
  1268. else
  1269. {
  1270. bResult = DrawCell(pDC, nRow, nCol, rect, TRUE);
  1271. // Since we have erased the background, we will need to redraw the gridlines
  1272. CPen pen;
  1273. TRY {
  1274. pen.CreatePen(PS_SOLID, 0, m_crGridColour);
  1275. } CATCH (CException, e) {e->Delete();} END_CATCH
  1276. CPen* pOldPen = (CPen*) pDC->SelectObject(&pen);
  1277. if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_HORZ)
  1278. {
  1279. pDC->MoveTo(rect.left, rect.bottom);
  1280. pDC->LineTo(rect.right+1, rect.bottom);
  1281. }
  1282. if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_VERT)
  1283. {
  1284. pDC->MoveTo(rect.right, rect.top);
  1285. pDC->LineTo(rect.right, rect.bottom+1);
  1286. }
  1287. pDC->SelectObject(pOldPen);
  1288. }
  1289. } else
  1290. InvalidateRect(rect, TRUE); // Could not get a DC - invalidate it anyway
  1291. // and hope that OnPaint manages to get one
  1292. if (bMustReleaseDC)
  1293. ReleaseDC(pDC);
  1294. return bResult;
  1295. }
  1296. // redraw a complete row
  1297. BOOL CGridCtrl::RedrawRow(int row)
  1298. {
  1299. BOOL bResult = TRUE;
  1300. CDC* pDC = GetDC();
  1301. for (int col = 0; col < GetColumnCount(); col++)
  1302. bResult = RedrawCell(row, col, pDC) && bResult;
  1303. if (pDC)
  1304. ReleaseDC(pDC);
  1305. return bResult;
  1306. }
  1307. // redraw a complete column
  1308. BOOL CGridCtrl::RedrawColumn(int col)
  1309. {
  1310. BOOL bResult = TRUE;
  1311. CDC* pDC = GetDC();
  1312. for (int row = 0; row < GetRowCount(); row++)
  1313. bResult = RedrawCell(row, col, pDC) && bResult;
  1314. if (pDC)
  1315. ReleaseDC(pDC);
  1316. return bResult;
  1317. }
  1318. // Sets the currently selected cell, returning the previous current cell
  1319. CCellID CGridCtrl::SetFocusCell(int nRow, int nCol)
  1320. {
  1321. return SetFocusCell(CCellID(nRow, nCol));
  1322. }
  1323. CCellID CGridCtrl::SetFocusCell(CCellID cell)
  1324. {
  1325. if (cell == m_idCurrentCell)
  1326. return m_idCurrentCell;
  1327. CCellID idPrev = m_idCurrentCell;
  1328. m_idCurrentCell = cell;
  1329. if (IsValid(idPrev))
  1330. {
  1331. SendMessageToParent(idPrev.row, idPrev.col, GVN_SELCHANGING);
  1332. SetItemState(idPrev.row, idPrev.col,
  1333. GetItemState(idPrev.row, idPrev.col) & ~GVIS_FOCUSED);
  1334. RedrawCell(idPrev);
  1335. if (idPrev.col != m_idCurrentCell.col)
  1336. for (int row = 0; row < m_nFixedRows; row++)
  1337. RedrawCell(row, idPrev.col);
  1338. if (idPrev.row != m_idCurrentCell.row)
  1339. for (int col = 0; col < m_nFixedCols; col++)
  1340. RedrawCell(idPrev.row, col);
  1341. }
  1342. if (IsValid(m_idCurrentCell)) {
  1343. SetItemState(m_idCurrentCell.row, m_idCurrentCell.col,
  1344. GetItemState(m_idCurrentCell.row, m_idCurrentCell.col) | GVIS_FOCUSED);
  1345. RedrawCell(m_idCurrentCell);
  1346. if (idPrev.col != m_idCurrentCell.col)
  1347. for (int row = 0; row < m_nFixedRows; row++)
  1348. RedrawCell(row, m_idCurrentCell.col);
  1349. if (idPrev.row != m_idCurrentCell.row)
  1350. for (int col = 0; col < m_nFixedCols; col++)
  1351. RedrawCell(m_idCurrentCell.row, col);
  1352. SendMessageToParent(m_idCurrentCell.row, m_idCurrentCell.col, GVN_SELCHANGED);
  1353. }
  1354. return idPrev;
  1355. }
  1356. // Sets the range of currently selected cells
  1357. void CGridCtrl::SetSelectedRange(const CCellRange& Range,
  1358. BOOL bForceRepaint /* = FALSE */)
  1359. {
  1360. SetSelectedRange(Range.GetMinRow(), Range.GetMinCol(),
  1361. Range.GetMaxRow(), Range.GetMaxCol(),
  1362. bForceRepaint);
  1363. }
  1364. void CGridCtrl::SetSelectedRange(int nMinRow, int nMinCol, int nMaxRow, int nMaxCol,
  1365. BOOL bForceRepaint /* = FALSE */)
  1366. {
  1367. if (!m_bEnableSelection) return;
  1368. CDC* pDC = NULL;
  1369. if (bForceRepaint) pDC = GetDC();
  1370. // Unselect all previously selected cells
  1371. for (POSITION pos = m_SelectedCellMap.GetStartPosition(); pos != NULL; )
  1372. {
  1373. DWORD key;
  1374. CCellID cell;
  1375. m_SelectedCellMap.GetNextAssoc(pos, key, (CCellID&)cell);
  1376. // Reset the selection flag on the cell
  1377. if (IsValid(cell)) {
  1378. SetItemState(cell.row, cell.col,
  1379. GetItemState(cell.row, cell.col) & ~GVIS_SELECTED);
  1380. // If this is to be reselected, continue on past the redraw
  1381. if (nMinRow <= cell.row && cell.row <= nMaxRow &&
  1382. nMinCol <= cell.col && cell.col <= nMaxCol)
  1383. continue;
  1384. if (bForceRepaint && pDC) // Redraw NOW
  1385. RedrawCell(cell.row, cell.col, pDC);
  1386. else
  1387. InvalidateCellRect(cell); // Redraw at leisure
  1388. }
  1389. }
  1390. // if any previous selected cells are to be retained (eg Ctrl is being held down)
  1391. // then copy them to the newly created list, and mark all these cells as
  1392. // selected
  1393. if (!GetSingleRowSelection())
  1394. for (pos = m_PrevSelectedCellMap.GetStartPosition(); pos != NULL; /* nothing */)
  1395. {
  1396. DWORD key;
  1397. CCellID cell;
  1398. m_PrevSelectedCellMap.GetNextAssoc(pos, key, (CCellID&)cell);
  1399. if (!IsValid(cell))
  1400. continue;
  1401. int nState = GetItemState(cell.row, cell.col);
  1402. // Set state as Selected. This will add the cell to m_SelectedCells[]
  1403. SetItemState(cell.row, cell.col, nState | GVIS_SELECTED);
  1404. // Redraw (immediately or at leisure)
  1405. if (bForceRepaint && pDC)
  1406. RedrawCell(cell.row, cell.col, pDC);
  1407. else
  1408. InvalidateCellRect(cell);
  1409. }
  1410. // Now select all cells in the cell range specified. If the cell has already
  1411. // been marked as selected (above) then ignore it.
  1412. if (nMinRow >= 0 && nMinCol >= 0 && nMaxRow >= 0 && nMaxCol >= 0 &&
  1413. nMaxRow < m_nRows && nMaxCol < m_nCols &&
  1414. nMinRow <= nMaxRow && nMinCol <= nMaxCol)
  1415. {
  1416. for (int row = nMinRow; row <= nMaxRow; row++)
  1417. for (int col = nMinCol; col <= nMaxCol; col++)
  1418. {
  1419. int nState = GetItemState(row, col);
  1420. if (nState & GVIS_SELECTED)
  1421. continue; // Already selected - ignore
  1422. // Add to list of selected cells
  1423. CCellID cell(row, col);
  1424. // Set state as Selected. This will add the cell to m_SelectedCells[]
  1425. SetItemState(row, col, nState | GVIS_SELECTED);
  1426. // Redraw (immediately or at leisure)
  1427. if (bForceRepaint && pDC)
  1428. RedrawCell(row, col, pDC);
  1429. else
  1430. InvalidateCellRect(cell);
  1431. }
  1432. }
  1433. // TRACE(_T("%d cells selected.\n"), m_SelectedCellMap.GetCount());
  1434. if (pDC != NULL)
  1435. ReleaseDC(pDC);
  1436. }
  1437. // selects all cells
  1438. void CGridCtrl::SelectAllCells()
  1439. {
  1440. if (!m_bEnableSelection)
  1441. return;
  1442. SetSelectedRange(m_nFixedRows, m_nFixedCols, GetRowCount()-1, GetColumnCount()-1);
  1443. }
  1444. // selects columns
  1445. void CGridCtrl::SelectColumns(CCellID currentCell)
  1446. {
  1447. if (!m_bEnableSelection)
  1448. return;
  1449. //if (currentCell.col == m_idCurrentCell.col) return;
  1450. if (currentCell.col < m_nFixedCols)
  1451. return;
  1452. if (!IsValid(currentCell))
  1453. return;
  1454. SetSelectedRange(GetFixedRowCount(),
  1455. min(m_SelectionStartCell.col, currentCell.col),
  1456. GetRowCount()-1,
  1457. max(m_SelectionStartCell.col, currentCell.col));
  1458. }
  1459. // selects rows
  1460. void CGridCtrl::SelectRows(CCellID currentCell)
  1461. {
  1462. if (!m_bEnableSelection)
  1463. return;
  1464. //if (currentCell.row; == m_idCurrentCell.row) return;
  1465. if (currentCell.row < m_nFixedRows)
  1466. return;
  1467. if (!IsValid(currentCell))
  1468. return;
  1469. if (GetListMode() && GetSingleRowSelection())
  1470. SetSelectedRange(currentCell.row, GetFixedColumnCount(),
  1471. currentCell.row, GetColumnCount()-1);
  1472. else
  1473. SetSelectedRange(min(m_SelectionStartCell.row, currentCell.row),
  1474. GetFixedColumnCount(),
  1475. max(m_SelectionStartCell.row, currentCell.row),
  1476. GetColumnCount()-1);
  1477. }
  1478. // selects cells
  1479. void CGridCtrl::SelectCells(CCellID currentCell)
  1480. {
  1481. if (!m_bEnableSelection)
  1482. return;
  1483. int row = currentCell.row;
  1484. int col = currentCell.col;
  1485. if (row < m_nFixedRows || col < m_nFixedCols)
  1486. return;
  1487. if (!IsValid(currentCell))
  1488. return;
  1489. // Prevent unnecessary redraws
  1490. //if (currentCell == m_LeftClickDownCell) return;
  1491. //else if (currentCell == m_idCurrentCell) return;
  1492. SetSelectedRange(min(m_SelectionStartCell.row, row),
  1493. min(m_SelectionStartCell.col, col),
  1494. max(m_SelectionStartCell.row, row),
  1495. max(m_SelectionStartCell.col, col));
  1496. }
  1497. void CGridCtrl::OnSelecting(const CCellID& currentCell)
  1498. {
  1499. if (!m_bEnableSelection)
  1500. return;
  1501. switch(m_MouseMode)
  1502. {
  1503. case MOUSE_SELECT_ALL: SelectAllCells(); break;
  1504. case MOUSE_SELECT_COL: SelectColumns(currentCell); break;
  1505. case MOUSE_SELECT_ROW: SelectRows(currentCell); break;
  1506. case MOUSE_SELECT_CELLS: SelectCells(currentCell); break;
  1507. }
  1508. }
  1509. #ifndef GRIDCONTROL_NO_CLIPBOARD
  1510. ////////////////////////////////////////////////////////////////////////////////////////
  1511. // Clipboard functions
  1512. void CGridCtrl::CutSelectedText()
  1513. {
  1514. if (!IsEditable())
  1515. return;
  1516. // Clear contents of selected cells.
  1517. for (POSITION pos = m_SelectedCellMap.GetStartPosition(); pos != NULL; )
  1518. {
  1519. DWORD key;
  1520. CCellID cell;
  1521. m_SelectedCellMap.GetNextAssoc(pos, key, (CCellID&)cell);
  1522. if (!IsCellEditable(cell))
  1523. continue;
  1524. CGridCell* pCell = GetCell(cell.row, cell.col);
  1525. if (pCell)
  1526. {
  1527. SendMessageToParent(cell.row, cell.col, GVN_BEGINLABELEDIT);
  1528. EmptyCell(pCell, cell.row, cell.col);
  1529. SetModified(TRUE, cell.row, cell.col);
  1530. SendMessageToParent(cell.row, cell.col, GVN_ENDLABELEDIT);
  1531. }
  1532. }
  1533. Invalidate();
  1534. }
  1535. COleDataSource* CGridCtrl::CopyTextFromGrid()
  1536. {
  1537. USES_CONVERSION;
  1538. CCellRange Selection = GetSelectedCellRange();
  1539. if (!IsValid(Selection)) return NULL;
  1540. // Write to shared file (REMEBER: CF_TEXT is ANSI, not UNICODE, so we need to convert)
  1541. CSharedFile sf(GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT);
  1542. // Get a tab delimited string to copy to cache
  1543. CString str;
  1544. CGridCell *pCell;
  1545. for (int row = Selection.GetMinRow(); row <= Selection.GetMaxRow(); row++)
  1546. {
  1547. str.Empty();
  1548. for (int col = Selection.GetMinCol(); col <= Selection.GetMaxCol(); col++)
  1549. {
  1550. pCell = GetCell(row,col);
  1551. if (pCell && (pCell->state & GVIS_SELECTED))
  1552. {
  1553. if (pCell->szText.IsEmpty())
  1554. str += _T(" ");
  1555. else
  1556. str += pCell->szText;
  1557. }
  1558. if (col != Selection.GetMaxCol())
  1559. str += _T("\t");
  1560. }
  1561. if (row != Selection.GetMaxRow())
  1562. str += _T("\n");
  1563. sf.Write(T2A(str.GetBuffer(1)), str.GetLength());
  1564. str.ReleaseBuffer();
  1565. }
  1566. char c = '\0';
  1567. sf.Write(&c, 1);
  1568. DWORD dwLen = sf.GetLength();
  1569. HGLOBAL hMem = sf.Detach();
  1570. if (!hMem)
  1571. return NULL;
  1572. hMem = ::GlobalReAlloc(hMem, dwLen, GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT);
  1573. if (!hMem)
  1574. return NULL;
  1575. // Cache data
  1576. COleDataSource* pSource = new COleDataSource();
  1577. pSource->CacheGlobalData(CF_TEXT, hMem);
  1578. return pSource;
  1579. }
  1580. BOOL CGridCtrl::PasteTextToGrid(CCellID cell, COleDataObject* pDataObject)
  1581. {
  1582. if (!IsValid(cell) || !IsCellEditable(cell) || !pDataObject->IsDataAvailable(CF_TEXT))
  1583. return FALSE;
  1584. // Get the text from the COleDataObject
  1585. HGLOBAL hmem = pDataObject->GetGlobalData(CF_TEXT);
  1586. CMemFile sf((BYTE*) ::GlobalLock(hmem), ::GlobalSize(hmem));
  1587. // CF_TEXT is ANSI text, so we need to allocate a char* buffer
  1588. // to hold this.
  1589. LPSTR szBuffer = new char[::GlobalSize(hmem)];
  1590. if (!szBuffer)
  1591. return FALSE;
  1592. sf.Read(szBuffer, ::GlobalSize(hmem));
  1593. ::GlobalUnlock(hmem);
  1594. // Now store in generic TCHAR form so we no longer have to deal with
  1595. // ANSI/UNICODE problems
  1596. CString strText = szBuffer;
  1597. delete szBuffer;
  1598. // Parse text data and set in cells...
  1599. strText.LockBuffer();
  1600. CString strLine = strText;
  1601. int nLine = 0;
  1602. // Find the end of the first line
  1603. int nIndex;
  1604. do {
  1605. int nColumn = 0;
  1606. nIndex = strLine.Find(_T("\n"));
  1607. // Store the remaining chars after the newline
  1608. CString strNext = (nIndex < 0)? _T("") : strLine.Mid(nIndex+1);
  1609. // Remove all chars after the newline
  1610. if (nIndex >= 0)
  1611. strLine = strLine.Left(nIndex);
  1612. // Make blank entries a "space"
  1613. if (strLine.IsEmpty() && nIndex >= 0)
  1614. strLine = _T(" ");
  1615. LPTSTR szLine = strLine.GetBuffer(1);
  1616. // Break the current line into tokens (tab or comma delimited)
  1617. LPTSTR pszCellText = _tcstok(szLine, _T("\t,\n"));
  1618. while (pszCellText != NULL)
  1619. {
  1620. CCellID TargetCell(cell.row + nLine, cell.col + nColumn);
  1621. if (IsValid(TargetCell))
  1622. {
  1623. CString strCellText = pszCellText;
  1624. strCellText.TrimLeft(); strCellText.TrimRight();
  1625. SendMessageToParent(TargetCell.row, TargetCell.col, GVN_BEGINLABELEDIT);
  1626. SetItemText(TargetCell.row, TargetCell.col, strCellText);
  1627. SetModified(TRUE, TargetCell.row, TargetCell.col);
  1628. SendMessageToParent(TargetCell.row, TargetCell.col, GVN_ENDLABELEDIT);
  1629. // Make sure cell is not selected to avoid data loss
  1630. SetItemState(TargetCell.row, TargetCell.col,
  1631. GetItemState(TargetCell.row, TargetCell.col) & ~GVIS_SELECTED);
  1632. }
  1633. pszCellText = _tcstok(NULL, _T("\t,\n"));
  1634. nColumn++;
  1635. }
  1636. strLine.ReleaseBuffer();
  1637. strLine = strNext;
  1638. nLine++;
  1639. } while (nIndex >= 0);
  1640. strText.UnlockBuffer();
  1641. Invalidate();
  1642. return TRUE;
  1643. }
  1644. #endif
  1645. #ifndef GRIDCONTROL_NO_DRAGDROP
  1646. void CGridCtrl::OnBeginDrag()
  1647. {
  1648. if (!m_bAllowDragAndDrop) return;
  1649. COleDataSource* pSource = CopyTextFromGrid();
  1650. if (pSource)
  1651. {
  1652. SendMessageToParent(GetSelectedCellRange().GetTopLeft().row,
  1653. GetSelectedCellRange().GetTopLeft().col,
  1654. GVN_BEGINDRAG);
  1655. m_MouseMode = MOUSE_DRAGGING;
  1656. DROPEFFECT dropEffect = pSource->DoDragDrop(DROPEFFECT_COPY|DROPEFFECT_MOVE);
  1657. if (dropEffect & DROPEFFECT_MOVE)
  1658. CutSelectedText();
  1659. if (pSource)
  1660. delete pSource; // Did not pass source to clipboard, so must delete
  1661. }
  1662. }
  1663. DROPEFFECT CGridCtrl::OnDragOver(COleDataObject* pDataObject, DWORD dwKeyState,
  1664. CPoint point)
  1665. {
  1666. // Any text data available for us?
  1667. if (!m_bAllowDragAndDrop || !IsEditable() || !pDataObject->IsDataAvailable(CF_TEXT))
  1668. return DROPEFFECT_NONE;
  1669. // Find which cell we are over and drop-highlight it
  1670. CCellID cell = GetCellFromPt(point, FALSE);
  1671. // If not valid, set the previously drop-highlighted cell as no longer drop-highlighted
  1672. if (!IsValid(cell))
  1673. {
  1674. OnDragLeave();
  1675. m_LastDragOverCell = CCellID(-1,-1);
  1676. return DROPEFFECT_NONE;
  1677. }
  1678. if (!IsCellEditable(cell))
  1679. return DROPEFFECT_NONE;
  1680. // Have we moved over a different cell than last time?
  1681. if (cell != m_LastDragOverCell)
  1682. {
  1683. // Set the previously drop-highlighted cell as no longer drop-highlighted
  1684. if (IsValid(m_LastDragOverCell)) {
  1685. UINT nState = GetItemState(m_LastDragOverCell.row, m_LastDragOverCell.col);
  1686. SetItemState(m_LastDragOverCell.row, m_LastDragOverCell.col,
  1687. nState & ~GVIS_DROPHILITED);
  1688. RedrawCell(m_LastDragOverCell);
  1689. }
  1690. m_LastDragOverCell = cell;
  1691. // Set the new cell as drop-highlighted
  1692. if (IsValid(m_LastDragOverCell)) {
  1693. UINT nState = GetItemState(m_LastDragOverCell.row, m_LastDragOverCell.col);
  1694. SetItemState(m_LastDragOverCell.row, m_LastDragOverCell.col,
  1695. nState | GVIS_DROPHILITED);
  1696. RedrawCell(m_LastDragOverCell);
  1697. }
  1698. }
  1699. // Return an appropraite value of DROPEFFECT so mouse cursor is set properly
  1700. if (dwKeyState & MK_CONTROL)
  1701. return DROPEFFECT_COPY;
  1702. else
  1703. return DROPEFFECT_MOVE;
  1704. }
  1705. DROPEFFECT CGridCtrl::OnDragEnter(COleDataObject* pDataObject, DWORD dwKeyState,
  1706. CPoint point)
  1707. {
  1708. // Any text data available for us?
  1709. if (!m_bAllowDragAndDrop || !pDataObject->IsDataAvailable(CF_TEXT))
  1710. return DROPEFFECT_NONE;
  1711. // Find which cell we are over and drop-highlight it
  1712. m_LastDragOverCell = GetCellFromPt(point, FALSE);
  1713. if (!IsValid(m_LastDragOverCell))
  1714. return DROPEFFECT_NONE;
  1715. if (!IsCellEditable(m_LastDragOverCell))
  1716. return DROPEFFECT_NONE;
  1717. if (IsValid(m_LastDragOverCell))
  1718. {
  1719. UINT nState = GetItemState(m_LastDragOverCell.row, m_LastDragOverCell.col);
  1720. SetItemState(m_LastDragOverCell.row, m_LastDragOverCell.col,
  1721. nState | GVIS_DROPHILITED);
  1722. RedrawCell(m_LastDragOverCell);
  1723. }
  1724. // Return an appropraite value of DROPEFFECT so mouse cursor is set properly
  1725. if (dwKeyState & MK_CONTROL)
  1726. return DROPEFFECT_COPY;
  1727. else
  1728. return DROPEFFECT_MOVE;
  1729. }
  1730. void CGridCtrl::OnDragLeave()
  1731. {
  1732. // Set the previously drop-highlighted cell as no longer drop-highlighted
  1733. if (IsValid(m_LastDragOverCell)) {
  1734. UINT nState = GetItemState(m_LastDragOverCell.row, m_LastDragOverCell.col);
  1735. SetItemState(m_LastDragOverCell.row, m_LastDragOverCell.col,
  1736. nState & ~GVIS_DROPHILITED);
  1737. RedrawCell(m_LastDragOverCell);
  1738. }
  1739. }
  1740. BOOL CGridCtrl::OnDrop(COleDataObject* pDataObject, DROPEFFECT /*dropEffect*/,
  1741. CPoint /* point */)
  1742. {
  1743. BOOL bResult = FALSE;
  1744. if (!m_bAllowDragAndDrop || !IsCellEditable(m_LastDragOverCell))
  1745. return bResult;
  1746. m_MouseMode = MOUSE_NOTHING;
  1747. OnDragLeave();
  1748. return PasteTextToGrid(m_LastDragOverCell, pDataObject);
  1749. }
  1750. #endif
  1751. #ifndef GRIDCONTROL_NO_CLIPBOARD
  1752. void CGridCtrl::OnEditCut()
  1753. {
  1754. if (!IsEditable())
  1755. return;
  1756. COleDataSource* pSource = CopyTextFromGrid();
  1757. if (!pSource) return;
  1758. pSource->SetClipboard();
  1759. CutSelectedText();
  1760. }
  1761. void CGridCtrl::OnEditCopy()
  1762. {
  1763. COleDataSource* pSource = CopyTextFromGrid();
  1764. if (!pSource) return;
  1765. pSource->SetClipboard();
  1766. }
  1767. void CGridCtrl::OnEditPaste()
  1768. {
  1769. if (!IsEditable())
  1770. return;
  1771. // Get the Focus cell, or if none, get the topleft (non-fixed) cell
  1772. CCellID cell = GetFocusCell();
  1773. if (!IsValid(cell)) cell = GetTopleftNonFixedCell();
  1774. if (!IsValid(cell)) return;
  1775. // Attach a COleDataObject to the clipboard and paste the data to the grid
  1776. COleDataObject obj;
  1777. if (obj.AttachClipboard())
  1778. PasteTextToGrid(cell, &obj);
  1779. }
  1780. #endif
  1781. void CGridCtrl::OnEditSelectAll()
  1782. {
  1783. SelectAllCells();
  1784. }
  1785. #ifndef GRIDCONTROL_NO_CLIPBOARD
  1786. void CGridCtrl::OnUpdateEditCopy(CCmdUI* pCmdUI)
  1787. {
  1788. CCellRange Selection = GetSelectedCellRange();
  1789. pCmdUI->Enable(Selection.Count() && IsValid(Selection));
  1790. }
  1791. void CGridCtrl::OnUpdateEditCut(CCmdUI* pCmdUI)
  1792. {
  1793. CCellRange Selection = GetSelectedCellRange();
  1794. pCmdUI->Enable(IsEditable() && Selection.Count() && IsValid(Selection));
  1795. }
  1796. void CGridCtrl::OnUpdateEditPaste(CCmdUI* pCmdUI)
  1797. {
  1798. CCellID cell = GetFocusCell();
  1799. BOOL bCanPaste = IsValid(cell) && IsCellEditable(cell) &&
  1800. ::IsClipboardFormatAvailable(CF_TEXT);
  1801. pCmdUI->Enable(bCanPaste);
  1802. }
  1803. #endif
  1804. void CGridCtrl::OnUpdateEditSelectAll(CCmdUI* pCmdUI)
  1805. {
  1806. pCmdUI->Enable(m_bEnableSelection);
  1807. }
  1808. ////////////////////////////////////////////////////////////////////////////////////////
  1809. // hittest-like functions
  1810. BOOL CGridCtrl::MouseOverRowResizeArea(CPoint& point) const
  1811. {
  1812. if (point.x >= GetFixedColumnWidth())
  1813. return FALSE;
  1814. CCellID idCurrentCell = GetCellFromPt(point);
  1815. CPoint start;
  1816. if (!GetCellOrigin(idCurrentCell, &start))
  1817. return FALSE;
  1818. int endy = start.y + GetRowHeight(idCurrentCell.row);
  1819. if ((point.y - start.y <= m_nResizeCaptureRange && idCurrentCell.row != 0) ||
  1820. endy - point.y <= m_nResizeCaptureRange)
  1821. {
  1822. return TRUE;
  1823. }
  1824. else
  1825. return FALSE;
  1826. }
  1827. BOOL CGridCtrl::MouseOverColumnResizeArea(CPoint& point) const
  1828. {
  1829. if (point.y >= GetFixedRowHeight())
  1830. return FALSE;
  1831. CCellID idCurrentCell = GetCellFromPt(point);
  1832. CPoint start;
  1833. if (!GetCellOrigin(idCurrentCell, &start))
  1834. return FALSE;
  1835. int endx = start.x + GetColumnWidth(idCurrentCell.col);
  1836. if ((point.x - start.x <= m_nResizeCaptureRange && idCurrentCell.col != 0) ||
  1837. endx - point.x <= m_nResizeCaptureRange)
  1838. {
  1839. return TRUE;
  1840. }
  1841. else
  1842. return FALSE;
  1843. }
  1844. // Get cell from point
  1845. CCellID CGridCtrl::GetCellFromPt(CPoint point, BOOL bAllowFixedCellCheck /*=TRUE*/) const
  1846. {
  1847. CCellID idTopLeft = GetTopleftNonFixedCell();
  1848. CCellID cellID; // return value
  1849. // calculate column index
  1850. int fixedColWidth = GetFixedColumnWidth();
  1851. if (point.x < 0 || (!bAllowFixedCellCheck && point.x < fixedColWidth)) // not in window
  1852. cellID.col = -1;
  1853. else if (point.x < fixedColWidth) // in fixed col
  1854. {
  1855. int xpos = 0;
  1856. for (int col = 0; col < m_nFixedCols; col++)
  1857. {
  1858. xpos += GetColumnWidth(col);
  1859. if (xpos > point.x) break;
  1860. }
  1861. cellID.col = col;
  1862. }
  1863. else // in non-fixed col
  1864. {
  1865. int xpos = fixedColWidth;
  1866. for (int col = idTopLeft.col; col < GetColumnCount(); col++)
  1867. {
  1868. xpos += GetColumnWidth(col);
  1869. if (xpos > point.x) break;
  1870. }
  1871. if (col >= GetColumnCount())
  1872. cellID.col = -1;
  1873. else
  1874. cellID.col = col;
  1875. }
  1876. // calculate row index
  1877. int fixedRowHeight = GetFixedRowHeight();
  1878. if (point.y < 0 || (!bAllowFixedCellCheck && point.y < fixedRowHeight)) // not in window
  1879. cellID.row = -1;
  1880. else if (point.y < fixedRowHeight) // in fixed col
  1881. {
  1882. int ypos = 0;
  1883. for (int row = 0; row < m_nFixedRows; row++)
  1884. {
  1885. ypos += GetRowHeight(row);
  1886. if (ypos > point.y) break;
  1887. }
  1888. cellID.row = row;
  1889. }
  1890. else
  1891. {
  1892. int ypos = fixedRowHeight;
  1893. for (int row = idTopLeft.row; row < GetRowCount(); row++)
  1894. {
  1895. ypos += GetRowHeight(row);
  1896. if (ypos > point.y) break;
  1897. }
  1898. if (row >= GetRowCount())
  1899. cellID.row = -1;
  1900. else
  1901. cellID.row = row;
  1902. }
  1903. return cellID;
  1904. }
  1905. ////////////////////////////////////////////////////////////////////////////////
  1906. // CGridCtrl cellrange functions
  1907. CCellID CGridCtrl::GetTopleftNonFixedCell() const
  1908. {
  1909. int nVertScroll = GetScrollPos(SB_VERT),
  1910. nHorzScroll = GetScrollPos(SB_HORZ);
  1911. int nColumn = m_nFixedCols, nRight = 0;
  1912. while (nRight < nHorzScroll && nColumn < (GetColumnCount()-1))
  1913. nRight += GetColumnWidth(nColumn++);
  1914. int nRow = m_nFixedRows, nTop = 0;
  1915. while (nTop < nVertScroll && nRow < (GetRowCount()-1))
  1916. nTop += GetRowHeight(nRow++);
  1917. //TRACE("TopLeft cell is row %d, col %d\n",nRow, nColumn);
  1918. return CCellID(nRow, nColumn);
  1919. }
  1920. // This gets even partially visible cells
  1921. CCellRange CGridCtrl::GetVisibleNonFixedCellRange(LPRECT pRect /*=NULL*/) const
  1922. {
  1923. CRect rect;
  1924. GetClientRect(rect);
  1925. CCellID idTopLeft = GetTopleftNonFixedCell();
  1926. // calc bottom
  1927. int bottom = GetFixedRowHeight();
  1928. for (int i = idTopLeft.row; i < GetRowCount(); i++)
  1929. {
  1930. bottom += GetRowHeight(i);
  1931. if (bottom >= rect.bottom) {
  1932. bottom = rect.bottom;
  1933. break;
  1934. }
  1935. }
  1936. int maxVisibleRow = min(i, GetRowCount() - 1);
  1937. // calc right
  1938. int right = GetFixedColumnWidth();
  1939. for (i = idTopLeft.col; i < GetColumnCount(); i++)
  1940. {
  1941. right += GetColumnWidth(i);
  1942. if (right >= rect.right) {
  1943. right = rect.right;
  1944. break;
  1945. }
  1946. }
  1947. int maxVisibleCol = min(i, GetColumnCount() - 1);
  1948. if (pRect) {
  1949. pRect->left = pRect->top = 0;
  1950. pRect->right = right;
  1951. pRect->bottom = bottom;
  1952. }
  1953. return CCellRange(idTopLeft.row, idTopLeft.col, maxVisibleRow, maxVisibleCol);
  1954. }
  1955. // used by ResetScrollBars() - This gets only fully visible cells
  1956. CCellRange CGridCtrl::GetUnobstructedNonFixedCellRange() const
  1957. {
  1958. CRect rect;
  1959. GetClientRect(rect);
  1960. CCellID idTopLeft = GetTopleftNonFixedCell();
  1961. // calc bottom
  1962. int bottom = GetFixedRowHeight();
  1963. for (int i = idTopLeft.row; i < GetRowCount(); i++)
  1964. {
  1965. bottom += GetRowHeight(i);
  1966. if (bottom >= rect.bottom) break;
  1967. }
  1968. int maxVisibleRow = min(i, GetRowCount() - 1);
  1969. if (maxVisibleRow > 0 && bottom > rect.bottom) maxVisibleRow--;
  1970. // calc right
  1971. int right = GetFixedColumnWidth();
  1972. for (i = idTopLeft.col; i < GetColumnCount(); i++)
  1973. {
  1974. right += GetColumnWidth(i);
  1975. if (right >= rect.right) break;
  1976. }
  1977. int maxVisibleCol = min(i, GetColumnCount() - 1);
  1978. if (maxVisibleCol > 0 && right > rect.right) maxVisibleCol--;
  1979. return CCellRange(idTopLeft.row, idTopLeft.col, maxVisibleRow, maxVisibleCol);
  1980. }
  1981. // Returns the minimum bounding range of the current selection
  1982. // If no selection, then the returned CCellRange will be invalid
  1983. CCellRange CGridCtrl::GetSelectedCellRange() const
  1984. {
  1985. CCellRange Selection(GetRowCount(), GetColumnCount(), -1,-1);
  1986. for (POSITION pos = m_SelectedCellMap.GetStartPosition(); pos != NULL; )
  1987. {
  1988. DWORD key;
  1989. CCellID cell;
  1990. m_SelectedCellMap.GetNextAssoc(pos, key, (CCellID&)cell);
  1991. Selection.SetMinRow( min(Selection.GetMinRow(), cell.row) );
  1992. Selection.SetMinCol( min(Selection.GetMinCol(), cell.col) );
  1993. Selection.SetMaxRow( max(Selection.GetMaxRow(), cell.row) );
  1994. Selection.SetMaxCol( max(Selection.GetMaxCol(), cell.col) );
  1995. }
  1996. return Selection;
  1997. }
  1998. // Returns ALL the cells in the grid
  1999. CCellRange CGridCtrl::GetCellRange() const
  2000. {
  2001. return CCellRange(0, 0, GetRowCount() - 1, GetColumnCount() - 1);
  2002. }
  2003. void CGridCtrl::ResetSelectedRange()
  2004. {
  2005. SetSelectedRange(-1,-1,-1,-1);
  2006. SetFocusCell(-1,-1);
  2007. }
  2008. // Get/Set scroll position using 32 bit functions
  2009. int CGridCtrl::GetScrollPos32(int nBar, BOOL bGetTrackPos /* = FALSE */)
  2010. {
  2011. SCROLLINFO si;
  2012. si.cbSize = sizeof(SCROLLINFO);
  2013. if (bGetTrackPos)
  2014. {
  2015. if (GetScrollInfo(nBar, &si, SIF_TRACKPOS))
  2016. return si.nTrackPos;
  2017. }
  2018. else
  2019. {
  2020. if (GetScrollInfo(nBar, &si, SIF_POS))
  2021. return si.nPos;
  2022. }
  2023. return 0;
  2024. }
  2025. BOOL CGridCtrl::SetScrollPos32(int nBar, int nPos, BOOL bRedraw /* = TRUE */)
  2026. {
  2027. SCROLLINFO si;
  2028. si.cbSize = sizeof(SCROLLINFO);
  2029. si.fMask = SIF_POS;
  2030. si.nPos = nPos;
  2031. return SetScrollInfo(nBar, &si, bRedraw);
  2032. }
  2033. void CGridCtrl::ResetScrollBars()
  2034. {
  2035. if (!m_bAllowDraw || !::IsWindow(GetSafeHwnd()))
  2036. return;
  2037. CRect rect;
  2038. GetClientRect(rect);
  2039. rect.left += GetFixedColumnWidth();
  2040. rect.top += GetFixedRowHeight();
  2041. if (rect.left >= rect.right || rect.top >= rect.bottom) return;
  2042. CRect VisibleRect(GetFixedColumnWidth(), GetFixedRowHeight(), rect.right, rect.bottom);
  2043. CRect VirtualRect(GetFixedColumnWidth(), GetFixedRowHeight(), GetVirtualWidth(), GetVirtualHeight());
  2044. CCellRange visibleCells = GetUnobstructedNonFixedCellRange();
  2045. if (!IsValid(visibleCells)) return;
  2046. SCROLLINFO si;
  2047. si.cbSize = sizeof(SCROLLINFO);
  2048. si.fMask = SIF_PAGE;
  2049. si.nPage = VisibleRect.Width(); SetScrollInfo(SB_HORZ, &si, FALSE);
  2050. si.nPage = VisibleRect.Height(); SetScrollInfo(SB_VERT, &si, FALSE);
  2051. if (VisibleRect.Height() < VirtualRect.Height())
  2052. m_nVScrollMax = VirtualRect.Height()-1; //+ GetRowHeight(visibleCells.GetTopLeft().row);
  2053. else
  2054. m_nVScrollMax = 0;
  2055. if (VisibleRect.Width() < VirtualRect.Width())
  2056. m_nHScrollMax = VirtualRect.Width()-1; //+ GetColumnWidth(visibleCells.GetTopLeft().col);
  2057. else
  2058. m_nHScrollMax = 0;
  2059. ASSERT(m_nVScrollMax < INT_MAX && m_nHScrollMax < INT_MAX); // This should be fine :)
  2060. SetScrollRange(SB_VERT, 0, m_nVScrollMax, TRUE);
  2061. SetScrollRange(SB_HORZ, 0, m_nHScrollMax, TRUE);
  2062. }
  2063. ////////////////////////////////////////////////////////////////////////////////////
  2064. // Row/Column position functions
  2065. // returns the top left point of the cell. Returns FALSE if cell not visible.
  2066. BOOL CGridCtrl::GetCellOrigin(int nRow, int nCol, LPPOINT p) const
  2067. {
  2068. int i;
  2069. if (!IsValid(nRow, nCol)) return FALSE;
  2070. CCellID idTopLeft;
  2071. if (nCol >= m_nFixedCols || nRow >= m_nFixedRows)
  2072. idTopLeft = GetTopleftNonFixedCell();
  2073. if ((nRow >= m_nFixedRows && nRow < idTopLeft.row) ||
  2074. (nCol>= m_nFixedCols && nCol < idTopLeft.col))
  2075. return FALSE;
  2076. p->x = 0;
  2077. if (nCol < m_nFixedCols) // is a fixed column
  2078. for (i = 0; i < nCol; i++)
  2079. p->x += GetColumnWidth(i);
  2080. else { // is a scrollable data column
  2081. for (i = 0; i < m_nFixedCols; i++)
  2082. p->x += GetColumnWidth(i);
  2083. for (i = idTopLeft.col; i < nCol; i++)
  2084. p->x += GetColumnWidth(i);
  2085. }
  2086. p->y = 0;
  2087. if (nRow < m_nFixedRows) // is a fixed row
  2088. for (i = 0; i < nRow; i++)
  2089. p->y += GetRowHeight(i);
  2090. else { // is a scrollable data row
  2091. for (i = 0; i < m_nFixedRows; i++)
  2092. p->y += GetRowHeight(i);
  2093. for (i = idTopLeft.row; i < nRow; i++)
  2094. p->y += GetRowHeight(i);
  2095. }
  2096. return TRUE;
  2097. }
  2098. BOOL CGridCtrl::GetCellOrigin(const CCellID& cell, LPPOINT p) const
  2099. {
  2100. return GetCellOrigin(cell.row, cell.col, p);
  2101. }
  2102. // Returns the bounding box of the cell
  2103. BOOL CGridCtrl::GetCellRect(const CCellID& cell, LPRECT pRect) const
  2104. {
  2105. return GetCellRect(cell.row, cell.col, pRect);
  2106. }
  2107. BOOL CGridCtrl::GetCellRect(int nRow, int nCol, LPRECT pRect) const
  2108. {
  2109. CPoint CellOrigin;
  2110. if (!GetCellOrigin(nRow, nCol, &CellOrigin)) return FALSE;
  2111. pRect->left = CellOrigin.x;
  2112. pRect->top = CellOrigin.y;
  2113. pRect->right = CellOrigin.x + GetColumnWidth(nCol)-1;
  2114. pRect->bottom = CellOrigin.y + GetRowHeight(nRow)-1;
  2115. //TRACE("Row %d, col %d: L %d, T %d, W %d, H %d: %d,%d - %d,%d\n",
  2116. // nRow,nCol, CellOrigin.x, CellOrigin.y, GetColumnWidth(nCol), GetRowHeight(nRow),
  2117. // pRect->left, pRect->top, pRect->right, pRect->bottom);
  2118. return TRUE;
  2119. }
  2120. BOOL CGridCtrl::GetTextRect(const CCellID& cell, LPRECT pRect)
  2121. {
  2122. return GetTextRect(cell.row, cell.col, pRect);
  2123. }
  2124. BOOL CGridCtrl::GetTextRect(int nRow, int nCol, LPRECT pRect)
  2125. {
  2126. GV_ITEM Item;
  2127. Item.mask = GVIF_IMAGE;
  2128. Item.row = nRow;
  2129. Item.col = nCol;
  2130. if (!GetItem(&Item))
  2131. return FALSE;
  2132. if (!GetCellRect(nRow, nCol, pRect))
  2133. return FALSE;
  2134. //pRect->left += m_nMargin;
  2135. //pRect->right -= m_nMargin;
  2136. if (m_pImageList && Item.iImage >= 0) {
  2137. IMAGEINFO Info;
  2138. if (m_pImageList->GetImageInfo(Item.iImage, &Info)) {
  2139. int nImageWidth = Info.rcImage.right-Info.rcImage.left+1;
  2140. pRect->left += nImageWidth+m_nMargin;
  2141. }
  2142. }
  2143. return TRUE;
  2144. }
  2145. // Returns the bounding box of a range of cells
  2146. BOOL CGridCtrl::GetCellRangeRect(const CCellRange& cellRange, LPRECT lpRect) const
  2147. {
  2148. CPoint MinOrigin,MaxOrigin;
  2149. if (!GetCellOrigin(cellRange.GetMinRow(), cellRange.GetMinCol(), &MinOrigin))
  2150. return FALSE;
  2151. if (!GetCellOrigin(cellRange.GetMaxRow(), cellRange.GetMaxCol(), &MaxOrigin))
  2152. return FALSE;
  2153. lpRect->left = MinOrigin.x;
  2154. lpRect->top = MinOrigin.y;
  2155. lpRect->right = MaxOrigin.x + GetColumnWidth(cellRange.GetMaxCol()-1);
  2156. lpRect->bottom = MaxOrigin.y + GetRowHeight(cellRange.GetMaxRow()-1);
  2157. return TRUE;
  2158. }
  2159. ////////////////////////////////////////////////////////////////////////////////////
  2160. // Grid attribute functions
  2161. LRESULT CGridCtrl::OnSetFont(WPARAM hFont, LPARAM /*lParam */)
  2162. {
  2163. LRESULT result = Default();
  2164. // Get the logical font
  2165. LOGFONT lf;
  2166. if (!GetObject((HFONT) hFont, sizeof(LOGFONT), &lf))
  2167. return result;
  2168. // Store font as the global default
  2169. memcpy(&m_Logfont, &lf, sizeof(LOGFONT));
  2170. // reset all cells' fonts
  2171. for (int row = 0; row < GetRowCount(); row++)
  2172. for (int col = 0; col < GetColumnCount(); col++)
  2173. SetItemFont(row, col, &lf);
  2174. // Get the font size and hence the default cell size
  2175. CDC* pDC = GetDC();
  2176. if (pDC)
  2177. {
  2178. m_Font.DeleteObject();
  2179. m_Font.CreateFontIndirect(&m_Logfont);
  2180. CFont* pOldFont = pDC->SelectObject(&m_Font);
  2181. TEXTMETRIC tm;
  2182. pDC->GetTextMetrics(&tm);
  2183. m_nMargin = pDC->GetTextExtent(_T(" "),1).cx;
  2184. pDC->SelectObject(pOldFont);
  2185. ReleaseDC(pDC);
  2186. m_nDefCellHeight = tm.tmHeight+tm.tmExternalLeading + 2*m_nMargin;
  2187. m_nDefCellWidth = tm.tmAveCharWidth*12 + 2*m_nMargin;
  2188. }
  2189. if (::IsWindow(GetSafeHwnd()))
  2190. Invalidate();
  2191. return result;
  2192. }
  2193. LRESULT CGridCtrl::OnGetFont(WPARAM /*wParam*/, LPARAM /*lParam*/)
  2194. {
  2195. return (LRESULT) (HFONT) m_Font;
  2196. }
  2197. #ifndef _WIN32_WCE_NO_CURSOR
  2198. BOOL CGridCtrl::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  2199. {
  2200. if (nHitTest == HTCLIENT)
  2201. {
  2202. switch (m_MouseMode)
  2203. {
  2204. case MOUSE_OVER_COL_DIVIDE: SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZEWE)); break;
  2205. case MOUSE_OVER_ROW_DIVIDE: SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZENS)); break;
  2206. #ifndef GRIDCONTROL_NO_DRAGDROP
  2207. case MOUSE_DRAGGING: break;
  2208. #endif
  2209. default: SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
  2210. }
  2211. return TRUE;
  2212. }
  2213. return CWnd::OnSetCursor(pWnd, nHitTest, message);
  2214. }
  2215. #endif
  2216. ////////////////////////////////////////////////////////////////////////////////////
  2217. // Row/Column count functions
  2218. BOOL CGridCtrl::SetFixedRowCount(int nFixedRows)
  2219. {
  2220. ASSERT(nFixedRows >= 0);
  2221. if (nFixedRows > GetRowCount())
  2222. if (!SetRowCount(nFixedRows)) return FALSE;
  2223. if (m_idCurrentCell.row < nFixedRows)
  2224. SetFocusCell(-1,-1);
  2225. m_nFixedRows = nFixedRows;
  2226. if (GetSafeHwnd() && m_bAllowDraw)
  2227. Invalidate();
  2228. return TRUE;
  2229. }
  2230. BOOL CGridCtrl::SetFixedColumnCount(int nFixedCols)
  2231. {
  2232. ASSERT(nFixedCols >= 0);
  2233. if (nFixedCols > GetColumnCount())
  2234. if (!SetColumnCount(nFixedCols)) return FALSE;
  2235. if (m_idCurrentCell.col < nFixedCols)
  2236. SetFocusCell(-1,-1);
  2237. m_nFixedCols = nFixedCols;
  2238. if (GetSafeHwnd() && m_bAllowDraw)
  2239. Invalidate();
  2240. return TRUE;
  2241. }
  2242. BOOL CGridCtrl::SetRowCount(int nRows)
  2243. {
  2244. ASSERT(nRows >= 0);
  2245. if (nRows == GetRowCount()) return TRUE;
  2246. if (nRows < m_nFixedRows)
  2247. m_nFixedRows = nRows;
  2248. if (m_idCurrentCell.row >= nRows)
  2249. SetFocusCell(-1,-1);
  2250. int addedRows = nRows - GetRowCount();
  2251. // If we are about to lose rows, then we need to delete the GridCell objects
  2252. // in each column within each row
  2253. if (addedRows < 0) {
  2254. for (int row = nRows; row < m_nRows; row++)
  2255. {
  2256. // Delete cells
  2257. for (int col = 0; col < m_nCols; col++)
  2258. {
  2259. CGridCell* pCell = GetCell(row, col);
  2260. if (pCell) {
  2261. EmptyCell(pCell, row, col);
  2262. delete pCell;
  2263. }
  2264. }
  2265. // Delete rows
  2266. GRID_ROW* pRow = m_RowData[row];
  2267. if (pRow) delete pRow;
  2268. }
  2269. }
  2270. // Change the number of rows.
  2271. m_nRows = nRows;
  2272. m_RowData.SetSize(m_nRows);
  2273. m_arRowHeights.SetSize(nRows);
  2274. // If we have just added rows, we need to construct new elements for each cell
  2275. // and set the default row height
  2276. if (addedRows > 0) {
  2277. // initialize row heights and data
  2278. int startRow = nRows - addedRows;
  2279. for (int row = startRow; row < GetRowCount(); row++) {
  2280. m_arRowHeights[row] = m_nDefCellHeight;
  2281. m_RowData[row] = new GRID_ROW;
  2282. m_RowData[row]->SetSize(m_nCols);
  2283. for (int col = 0; col < m_nCols; col++)
  2284. {
  2285. GRID_ROW* pRow = m_RowData[row];
  2286. if (pRow) pRow->SetAt(col, CreateCell(row, col));
  2287. }
  2288. }
  2289. }
  2290. //else
  2291. // ResetSelectedRange();
  2292. if (GetSafeHwnd() && m_bAllowDraw)
  2293. {
  2294. ResetScrollBars();
  2295. Invalidate();
  2296. }
  2297. return TRUE;
  2298. }
  2299. BOOL CGridCtrl::SetColumnCount(int nCols)
  2300. {
  2301. ASSERT(nCols >= 0);
  2302. if (nCols == GetColumnCount()) return TRUE;
  2303. if (nCols < m_nFixedCols)
  2304. m_nFixedCols = nCols;
  2305. if (m_idCurrentCell.col >= nCols)
  2306. SetFocusCell(-1,-1);
  2307. int addedCols = nCols - GetColumnCount();
  2308. // If we are about to lose columns, then we need to delete the GridCell objects
  2309. // within each column
  2310. if (addedCols < 0) {
  2311. for (int row = 0; row < m_nRows; row++)
  2312. for (int col = nCols; col < GetColumnCount(); col++)
  2313. {
  2314. CGridCell* pCell = GetCell(row, col);
  2315. if (pCell) {
  2316. EmptyCell(pCell, row, col);
  2317. delete pCell;
  2318. }
  2319. }
  2320. }
  2321. // Change the number of columns.
  2322. m_nCols = nCols;
  2323. m_arColWidths.SetSize(nCols);
  2324. // Change the number of columns in each row.
  2325. for (int i = 0; i < m_nRows; i++)
  2326. if (m_RowData[i]) m_RowData[i]->SetSize(nCols);
  2327. // If we have just added columns, we need to construct new elements for each cell
  2328. // and set the default column width
  2329. if (addedCols > 0)
  2330. {
  2331. // initialized column widths
  2332. int startCol = nCols - addedCols;
  2333. for (int col = startCol; col < GetColumnCount(); col++)
  2334. m_arColWidths[col] = m_nDefCellWidth;
  2335. // initialise column data
  2336. for (int row = 0; row < m_nRows; row++)
  2337. for (col = startCol; col < GetColumnCount(); col++)
  2338. {
  2339. GRID_ROW* pRow = m_RowData[row];
  2340. if (pRow) pRow->SetAt(col, CreateCell(row,col));
  2341. }
  2342. }
  2343. //else // check for selected cell ranges
  2344. // ResetSelectedRange();
  2345. if (GetSafeHwnd() && m_bAllowDraw)
  2346. {
  2347. ResetScrollBars();
  2348. Invalidate();
  2349. }
  2350. return TRUE;
  2351. }
  2352. // Insert a column at a given position, or add to end of columns (if nColumn = -1)
  2353. int CGridCtrl::InsertColumn(LPCTSTR strHeading,
  2354. UINT nFormat /* = DT_CENTER|DT_VCENTER|DT_SINGLELINE */,
  2355. int nColumn /* = -1 */)
  2356. {
  2357. // If the insertion is for a specific column, check it's within range.
  2358. if (nColumn >= 0 && nColumn >= GetColumnCount())
  2359. return -1;
  2360. ResetSelectedRange();
  2361. // Gotta be able to at least _see_ some of the column.
  2362. if (m_nRows < 1)
  2363. SetRowCount(1);
  2364. if (nColumn < 0)
  2365. {
  2366. nColumn = m_nCols;
  2367. m_arColWidths.Add(0);
  2368. for (int row = 0; row < m_nRows; row++)
  2369. {
  2370. GRID_ROW* pRow = m_RowData[row];
  2371. if (!pRow) return -1;
  2372. pRow->Add(CreateCell(row, nColumn));
  2373. }
  2374. }
  2375. else
  2376. {
  2377. m_arColWidths.InsertAt(nColumn, (int)0);
  2378. for (int row = 0; row < m_nRows; row++)
  2379. {
  2380. GRID_ROW* pRow = m_RowData[row];
  2381. if (!pRow) return -1;
  2382. pRow->InsertAt(nColumn, CreateCell(row, nColumn));
  2383. }
  2384. }
  2385. m_nCols++;
  2386. // Initialise column data
  2387. SetItemText(0, nColumn, strHeading);
  2388. for (int row = 0; row < m_nRows; row++)
  2389. {
  2390. SetItemFormat(row, nColumn, nFormat);
  2391. }
  2392. // initialized column width
  2393. m_arColWidths[nColumn] = GetTextExtent(strHeading).cx;
  2394. if (m_idCurrentCell.col != -1 && nColumn < m_idCurrentCell.col)
  2395. m_idCurrentCell.col++;
  2396. ResetScrollBars();
  2397. return nColumn;
  2398. }
  2399. // Insert a row at a given position, or add to end of rows (if nRow = -1)
  2400. int CGridCtrl::InsertRow(LPCTSTR strHeading, int nRow /* = -1 */)
  2401. {
  2402. // If the insertion is for a specific row, check it's within range.
  2403. if (nRow >= 0 && nRow >= GetRowCount())
  2404. return -1;
  2405. ResetSelectedRange();
  2406. // Gotta be able to at least _see_ some of the row.
  2407. if (m_nCols < 1)
  2408. SetColumnCount(1);
  2409. // Adding a row to the bottom
  2410. if (nRow < 0)
  2411. {
  2412. nRow = m_nRows;
  2413. m_arRowHeights.Add(0);
  2414. m_RowData.Add(new GRID_ROW);
  2415. }
  2416. else
  2417. {
  2418. m_arRowHeights.InsertAt(nRow, (int)0);
  2419. m_RowData.InsertAt(nRow, new GRID_ROW);
  2420. }
  2421. m_nRows++;
  2422. m_RowData[nRow]->SetSize(m_nCols);
  2423. // Initialise cell data
  2424. for (int col = 0; col < m_nCols; col++)
  2425. {
  2426. GRID_ROW* pRow = m_RowData[nRow];
  2427. if (!pRow) return -1;
  2428. pRow->SetAt(col, CreateCell(nRow, col));
  2429. }
  2430. // Set row title
  2431. SetItemText(nRow, 0, strHeading);
  2432. // initialized row height
  2433. m_arRowHeights[nRow] = GetTextExtent(strHeading).cy;
  2434. if (m_idCurrentCell.row != -1 && nRow < m_idCurrentCell.row)
  2435. m_idCurrentCell.row++;
  2436. ResetScrollBars();
  2437. return nRow;
  2438. }
  2439. // Creates a new grid cell and performs any necessary initialisation
  2440. CGridCell* CGridCtrl::CreateCell(int nRow, int nCol)
  2441. {
  2442. CGridCell* pCell = new CGridCell;
  2443. if (!pCell)
  2444. return NULL;
  2445. // Make format same as cell above
  2446. if (nRow > 0 && nCol >= 0 && nCol < m_nCols)
  2447. pCell->nFormat = GetItemFormat(nRow-1, nCol);
  2448. // Make font default grid font
  2449. memcpy(&(pCell->lfFont), &m_Logfont, sizeof(LOGFONT));
  2450. return pCell;
  2451. }
  2452. // Performs any cell cleanup necessary to maintain grid integrity
  2453. void CGridCtrl::EmptyCell(CGridCell* pCell, int nRow, int nCol)
  2454. {
  2455. // Set the cells state to 0. If the cell is selected, this
  2456. // will remove the cell from the selected list.
  2457. SetItemState(nRow, nCol, 0);
  2458. // Empty strings
  2459. pCell->szText.Empty();
  2460. }
  2461. BOOL CGridCtrl::DeleteColumn(int nColumn)
  2462. {
  2463. if (nColumn < 0 || nColumn >= GetColumnCount())
  2464. return FALSE;
  2465. ResetSelectedRange();
  2466. for (int row = 0; row < GetRowCount(); row++)
  2467. {
  2468. GRID_ROW* pRow = m_RowData[row];
  2469. if (!pRow) return FALSE;
  2470. CGridCell* pCell = pRow->GetAt(nColumn);
  2471. if (pCell) {
  2472. EmptyCell(pCell, row, nColumn);
  2473. delete pCell;
  2474. }
  2475. pRow->RemoveAt(nColumn);
  2476. }
  2477. m_arColWidths.RemoveAt(nColumn);
  2478. m_nCols--;
  2479. if (nColumn < m_nFixedCols) m_nFixedCols--;
  2480. if (nColumn == m_idCurrentCell.col)
  2481. m_idCurrentCell.row = m_idCurrentCell.col = -1;
  2482. else if (nColumn < m_idCurrentCell.col)
  2483. m_idCurrentCell.col--;
  2484. ResetScrollBars();
  2485. return TRUE;
  2486. }
  2487. BOOL CGridCtrl::DeleteRow(int nRow)
  2488. {
  2489. if ( nRow < 0 || nRow >= GetRowCount())
  2490. return FALSE;
  2491. GRID_ROW* pRow = m_RowData[nRow];
  2492. if (!pRow) return FALSE;
  2493. ResetSelectedRange();
  2494. for (int col = 0; col < GetColumnCount(); col++)
  2495. {
  2496. CGridCell* pCell = pRow->GetAt(col);
  2497. if (pCell) {
  2498. EmptyCell(pCell, nRow, col);
  2499. delete pCell;
  2500. }
  2501. }
  2502. delete pRow;
  2503. m_RowData.RemoveAt(nRow);
  2504. m_arRowHeights.RemoveAt(nRow);
  2505. m_nRows--;
  2506. if (nRow < m_nFixedRows) m_nFixedRows--;
  2507. if (nRow == m_idCurrentCell.row)
  2508. m_idCurrentCell.row = m_idCurrentCell.col = -1;
  2509. else if (nRow < m_idCurrentCell.row)
  2510. m_idCurrentCell.row--;
  2511. ResetScrollBars();
  2512. return TRUE;
  2513. }
  2514. // Handy function that removes all non-fixed rows
  2515. BOOL CGridCtrl::DeleteNonFixedRows()
  2516. {
  2517. int nFixed = GetFixedRowCount();
  2518. int nCount = GetRowCount();
  2519. // Delete all data rows
  2520. for (int nRow = nCount; nRow >= nFixed; nRow--)
  2521. DeleteRow(nRow);
  2522. return TRUE;
  2523. }
  2524. // Removes all rows, columns and data from the grid.
  2525. BOOL CGridCtrl::DeleteAllItems()
  2526. {
  2527. ResetSelectedRange();
  2528. m_arColWidths.RemoveAll();
  2529. m_arRowHeights.RemoveAll();
  2530. // Delete all cells in the grid
  2531. for (int row = 0; row < m_nRows; row++)
  2532. {
  2533. GRID_ROW* pRow = m_RowData[row];
  2534. if (!pRow) continue;
  2535. for (int col = 0; col < m_nCols; col++)
  2536. {
  2537. CGridCell* pCell = pRow->GetAt(col);
  2538. if (pCell) {
  2539. EmptyCell(pCell, row, col); // TODO - this is a bit of a performance hit.
  2540. delete pCell; // better to call m_SelectedCells.RemoveAll()
  2541. } // instead. This is safer for changes though.
  2542. }
  2543. delete pRow;
  2544. }
  2545. // Remove all rows
  2546. m_RowData.RemoveAll();
  2547. m_idCurrentCell.row = m_idCurrentCell.col = -1;
  2548. m_nRows = m_nFixedRows = m_nCols = m_nFixedCols = 0;
  2549. ResetScrollBars();
  2550. return TRUE;
  2551. }
  2552. /////////////////////////////////////////////////////////////////////////////
  2553. // CGridCtrl data functions
  2554. // Set CListCtrl::GetNextItem for details
  2555. CCellID CGridCtrl::GetNextItem(CCellID& cell, int nFlags) const
  2556. {
  2557. if (nFlags & GVNI_ALL)
  2558. { // GVNI_ALL Search whole Grid beginning from cell
  2559. // First row (cell.row) -- ONLY Columns to the right of cell
  2560. // following rows -- ALL Columns
  2561. int row = cell.row , col = cell.col + 1;
  2562. if (row <= 0) row = GetFixedRowCount();
  2563. for (; row < GetRowCount(); row++)
  2564. {
  2565. if (col <= 0 ) col = GetFixedColumnCount();
  2566. for (; col < GetColumnCount(); col++)
  2567. {
  2568. int nState = GetItemState(row, col);
  2569. if (nFlags & GVNI_DROPHILITED && nState & GVIS_DROPHILITED)
  2570. return CCellID(row, col);
  2571. if (nFlags & GVNI_FOCUSED && nState & GVIS_FOCUSED)
  2572. return CCellID(row, col);
  2573. if (nFlags & GVNI_SELECTED && nState & GVIS_SELECTED)
  2574. return CCellID(row, col);
  2575. if (nFlags & GVNI_READONLY && nState & GVIS_READONLY)
  2576. return CCellID(row, col);
  2577. if (nFlags & GVNI_MODIFIED && nState & GVIS_MODIFIED)
  2578. return CCellID(row, col);
  2579. }
  2580. // go to First Column
  2581. col=GetFixedColumnCount();
  2582. }
  2583. }
  2584. else if ((nFlags & GVNI_BELOW) && (nFlags & GVNI_TORIGHT))
  2585. { // GVNI_AREA Search Grid beginning from cell to Lower-Right of Grid
  2586. // Only rows starting with cell.row and below
  2587. // All rows -- ONLY Columns to the right of cell
  2588. int row = cell.row;
  2589. if (row <= 0) row = GetFixedRowCount();
  2590. for (; row < GetRowCount(); row++)
  2591. {
  2592. int col = cell.col + 1;
  2593. if (col <= 0) col = GetFixedColumnCount();
  2594. for (; col < GetColumnCount(); col++)
  2595. {
  2596. int nState = GetItemState(row, col);
  2597. if (nFlags & GVNI_DROPHILITED && nState & GVIS_DROPHILITED)
  2598. return CCellID(row, col);
  2599. if (nFlags & GVNI_FOCUSED && nState & GVIS_FOCUSED)
  2600. return CCellID(row, col);
  2601. if (nFlags & GVNI_SELECTED && nState & GVIS_SELECTED)
  2602. return CCellID(row, col);
  2603. if (nFlags & GVNI_READONLY && nState & GVIS_READONLY)
  2604. return CCellID(row, col);
  2605. if (nFlags & GVNI_MODIFIED && nState & GVIS_MODIFIED)
  2606. return CCellID(row, col);
  2607. }
  2608. }
  2609. }
  2610. else if (nFlags & GVNI_ABOVE)
  2611. {
  2612. for (int row = cell.row-1; row >= GetFixedRowCount(); row--)
  2613. {
  2614. int nState = GetItemState(row, cell.col);
  2615. if (nFlags & GVNI_DROPHILITED && nState & GVIS_DROPHILITED)
  2616. return CCellID(row, cell.col);
  2617. if (nFlags & GVNI_FOCUSED && nState & GVIS_FOCUSED)
  2618. return CCellID(row, cell.col);
  2619. if (nFlags & GVNI_SELECTED && nState & GVIS_SELECTED)
  2620. return CCellID(row, cell.col);
  2621. if (nFlags & GVNI_READONLY && nState & GVIS_READONLY)
  2622. return CCellID(row, cell.col);
  2623. if (nFlags & GVNI_MODIFIED && nState & GVIS_MODIFIED)
  2624. return CCellID(row, cell.col);
  2625. }
  2626. }
  2627. else if (nFlags & GVNI_BELOW)
  2628. {
  2629. for (int row = cell.row+1; row < GetRowCount(); row++)
  2630. {
  2631. int nState = GetItemState(row, cell.col);
  2632. if (nFlags & GVNI_DROPHILITED && nState & GVIS_DROPHILITED)
  2633. return CCellID(row, cell.col);
  2634. if (nFlags & GVNI_FOCUSED && nState & GVIS_FOCUSED)
  2635. return CCellID(row, cell.col);
  2636. if (nFlags & GVNI_SELECTED && nState & GVIS_SELECTED)
  2637. return CCellID(row, cell.col);
  2638. if (nFlags & GVNI_READONLY && nState & GVIS_READONLY)
  2639. return CCellID(row, cell.col);
  2640. if (nFlags & GVNI_MODIFIED && nState & GVIS_MODIFIED)
  2641. return CCellID(row, cell.col);
  2642. }
  2643. }
  2644. else if (nFlags & GVNI_TOLEFT)
  2645. {
  2646. for (int col = cell.col-1; col >= GetFixedColumnCount(); col--)
  2647. {
  2648. int nState = GetItemState(cell.row, col);
  2649. if (nFlags & GVNI_DROPHILITED && nState & GVIS_DROPHILITED)
  2650. return CCellID(cell.row, col);
  2651. if (nFlags & GVNI_FOCUSED && nState & GVIS_FOCUSED)
  2652. return CCellID(cell.row, col);
  2653. if (nFlags & GVNI_SELECTED && nState & GVIS_SELECTED)
  2654. return CCellID(cell.row, col);
  2655. if (nFlags & GVNI_READONLY && nState & GVIS_READONLY)
  2656. return CCellID(cell.row, col);
  2657. if (nFlags & GVNI_MODIFIED && nState & GVIS_MODIFIED)
  2658. return CCellID(cell.row, col);
  2659. }
  2660. }
  2661. else if (nFlags & GVNI_TORIGHT)
  2662. {
  2663. for (int col = cell.col+1; col < GetColumnCount(); col++)
  2664. {
  2665. int nState = GetItemState(cell.row, col);
  2666. if (nFlags & GVNI_DROPHILITED && nState & GVIS_DROPHILITED)
  2667. return CCellID(cell.row, col);
  2668. if (nFlags & GVNI_FOCUSED && nState & GVIS_FOCUSED)
  2669. return CCellID(cell.row, col);
  2670. if (nFlags & GVNI_SELECTED && nState & GVIS_SELECTED)
  2671. return CCellID(cell.row, col);
  2672. if (nFlags & GVNI_READONLY && nState & GVIS_READONLY)
  2673. return CCellID(cell.row, col);
  2674. if (nFlags & GVNI_MODIFIED && nState & GVIS_MODIFIED)
  2675. return CCellID(cell.row, col);
  2676. }
  2677. }
  2678. return CCellID(-1, -1);
  2679. }
  2680. // Sorts on a given column using the cell text
  2681. BOOL CGridCtrl::SortTextItems(int nCol, BOOL bAscending)
  2682. {
  2683. ResetSelectedRange();
  2684. SetFocusCell(-1,-1);
  2685. return SortTextItems(nCol, bAscending, GetFixedRowCount(),-1);
  2686. }
  2687. // recursive sort implementation
  2688. BOOL CGridCtrl::SortTextItems(int nCol, BOOL bAscending, int low, int high)
  2689. {
  2690. if (nCol >= GetColumnCount()) return FALSE;
  2691. if (high == -1) high = GetRowCount() - 1;
  2692. int lo = low;
  2693. int hi = high;
  2694. if( hi <= lo ) return FALSE;
  2695. CString midItem = GetItemText( (lo+hi)/2, nCol );
  2696. // loop through the list until indices cross
  2697. while( lo <= hi )
  2698. {
  2699. // Find the first element that is greater than or equal to the partition
  2700. // element starting from the left Index.
  2701. if( bAscending )
  2702. while (lo < high && GetItemText(lo, nCol) < midItem)
  2703. ++lo;
  2704. else
  2705. while (lo < high && GetItemText(lo, nCol) > midItem)
  2706. ++lo;
  2707. // Find an element that is smaller than or equal to the partition
  2708. // element starting from the right Index.
  2709. if( bAscending )
  2710. while (hi > low && GetItemText(hi, nCol) > midItem)
  2711. --hi;
  2712. else
  2713. while (hi > low && GetItemText(hi, nCol) < midItem)
  2714. --hi;
  2715. // If the indexes have not crossed, swap if the items are not equal
  2716. if (lo <= hi)
  2717. {
  2718. // swap only if the items are not equal
  2719. if (GetItemText(lo, nCol) != GetItemText(hi, nCol))
  2720. {
  2721. for (int col = 0; col < GetColumnCount(); col++)
  2722. {
  2723. CGridCell *pCell = GetCell(lo, col);
  2724. SetCell(lo, col, GetCell(hi, col));
  2725. SetCell(hi, col, pCell);
  2726. }
  2727. UINT nRowHeight = m_arRowHeights[lo];
  2728. m_arRowHeights[lo] = m_arRowHeights[hi];
  2729. m_arRowHeights[hi] = nRowHeight;
  2730. }
  2731. ++lo;
  2732. --hi;
  2733. }
  2734. }
  2735. // If the right index has not reached the left side of array
  2736. // must now sort the left partition.
  2737. if( low < hi )
  2738. SortTextItems(nCol, bAscending, low, hi);
  2739. // If the left index has not reached the right side of array
  2740. // must now sort the right partition.
  2741. if( lo < high )
  2742. SortTextItems(nCol, bAscending, lo, high);
  2743. return TRUE;
  2744. }
  2745. // Sorts on a given column using the supplied compare function (see CListCtrl::SortItems)
  2746. BOOL CGridCtrl::SortItems(PFNLVCOMPARE pfnCompare, int nCol, BOOL bAscending,
  2747. LPARAM data /* = 0 */)
  2748. {
  2749. ResetSelectedRange();
  2750. SetFocusCell(-1,-1);
  2751. return SortItems(pfnCompare, nCol, bAscending, data, GetFixedRowCount(), -1);
  2752. }
  2753. // recursive sort implementation
  2754. BOOL CGridCtrl::SortItems(PFNLVCOMPARE pfnCompare, int nCol, BOOL bAscending, LPARAM data,
  2755. int low, int high)
  2756. {
  2757. if (nCol >= GetColumnCount()) return FALSE;
  2758. if (high == -1) high = GetRowCount() - 1;
  2759. int lo = low;
  2760. int hi = high;
  2761. if( hi <= lo ) return FALSE;
  2762. LPARAM midItem = GetItemData( (lo+hi)/2, nCol );
  2763. // loop through the list until indices cross
  2764. while( lo <= hi )
  2765. {
  2766. // Find the first element that is greater than or equal to the partition
  2767. // element starting from the left Index.
  2768. if( bAscending )
  2769. while (lo < high && pfnCompare(GetItemData(lo, nCol), midItem, data) < 0)
  2770. ++lo;
  2771. else
  2772. while (lo < high && pfnCompare(GetItemData(lo, nCol), midItem, data) > 0)
  2773. ++lo;
  2774. // Find an element that is smaller than or equal to the partition
  2775. // element starting from the right Index.
  2776. if( bAscending )
  2777. while (hi > low && pfnCompare(GetItemData(hi, nCol), midItem, data) > 0)
  2778. --hi;
  2779. else
  2780. while (hi > low && pfnCompare(GetItemData(hi, nCol), midItem, data) < 0)
  2781. --hi;
  2782. // If the indexes have not crossed, swap if the items are not equal
  2783. if (lo <= hi)
  2784. {
  2785. // swap only if the items are not equal
  2786. if (pfnCompare(GetItemData(lo, nCol), GetItemData(hi, nCol), data) != 0)
  2787. {
  2788. for (int col = 0; col < GetColumnCount(); col++)
  2789. {
  2790. CGridCell *pCell = GetCell(lo, col);
  2791. SetCell(lo, col, GetCell(hi, col));
  2792. SetCell(hi, col, pCell);
  2793. }
  2794. UINT nRowHeight = m_arRowHeights[lo];
  2795. m_arRowHeights[lo] = m_arRowHeights[hi];
  2796. m_arRowHeights[hi] = nRowHeight;
  2797. }
  2798. ++lo;
  2799. --hi;
  2800. }
  2801. }
  2802. // If the right index has not reached the left side of array
  2803. // must now sort the left partition.
  2804. if( low < hi )
  2805. SortItems(pfnCompare, nCol, bAscending, data, low, hi);
  2806. // If the left index has not reached the right side of array
  2807. // must now sort the right partition.
  2808. if( lo < high )
  2809. SortItems(pfnCompare, nCol, bAscending, data, lo, high);
  2810. return TRUE;
  2811. }
  2812. /////////////////////////////////////////////////////////////////////////////
  2813. // CGridCtrl data functions
  2814. BOOL CGridCtrl::SetItem(const GV_ITEM* pItem)
  2815. {
  2816. if (!pItem)
  2817. return FALSE;
  2818. CGridCell* pCell = GetCell(pItem->row, pItem->col);
  2819. if (!pCell)
  2820. return FALSE;
  2821. if (pItem->mask & GVIF_TEXT) pCell->szText = pItem->szText;
  2822. if (pItem->mask & GVIF_PARAM) pCell->lParam = pItem->lParam;
  2823. if (pItem->mask & GVIF_IMAGE) pCell->iImage = pItem->iImage;
  2824. if (pItem->mask & GVIF_STATE) pCell->state = pItem->state;
  2825. if (pItem->mask & GVIF_FORMAT) pCell->nFormat = pItem->nFormat;
  2826. if (pItem->mask & GVIF_BKCLR) pCell->crBkClr = pItem->crBkClr;
  2827. if (pItem->mask & GVIF_FGCLR) pCell->crFgClr = pItem->crFgClr;
  2828. if (pItem->mask & GVIF_FONT) memcpy(&(pCell->lfFont), &(pItem->lfFont), sizeof(LOGFONT));
  2829. return TRUE;
  2830. }
  2831. BOOL CGridCtrl::GetItem(GV_ITEM* pItem)
  2832. {
  2833. if (!pItem)
  2834. return FALSE;
  2835. CGridCell* pCell = GetCell(pItem->row, pItem->col);
  2836. if (!pCell)
  2837. return FALSE;
  2838. if (pItem->mask & GVIF_TEXT) pItem->szText = GetItemText(pItem->row, pItem->col);
  2839. if (pItem->mask & GVIF_PARAM) pItem->lParam = pCell->lParam;
  2840. if (pItem->mask & GVIF_IMAGE) pItem->iImage = pCell->iImage;
  2841. if (pItem->mask & GVIF_STATE) pItem->state = pCell->state;
  2842. if (pItem->mask & GVIF_FORMAT) pItem->nFormat = pCell->nFormat;
  2843. if (pItem->mask & GVIF_BKCLR) pItem->crBkClr = pCell->crBkClr;
  2844. if (pItem->mask & GVIF_FGCLR) pItem->crFgClr = pCell->crFgClr;
  2845. if (pItem->mask & GVIF_FONT) memcpy(&(pItem->lfFont), &(pCell->lfFont), sizeof(LOGFONT));
  2846. return TRUE;
  2847. }
  2848. BOOL CGridCtrl::SetItemText(int nRow, int nCol, LPCTSTR str)
  2849. {
  2850. CGridCell* pCell = GetCell(nRow, nCol);
  2851. if (!pCell)
  2852. return FALSE;
  2853. pCell->szText = str;
  2854. return TRUE;
  2855. }
  2856. BOOL CGridCtrl::SetItemData(int nRow, int nCol, LPARAM lParam)
  2857. {
  2858. CGridCell* pCell = GetCell(nRow, nCol);
  2859. if (!pCell)
  2860. return FALSE;
  2861. pCell->lParam = lParam;
  2862. return TRUE;
  2863. }
  2864. LPARAM CGridCtrl::GetItemData(int nRow, int nCol) const
  2865. {
  2866. CGridCell* pCell = GetCell(nRow, nCol);
  2867. if (!pCell)
  2868. return (LPARAM) 0;
  2869. return pCell->lParam;
  2870. }
  2871. BOOL CGridCtrl::SetItemImage(int nRow, int nCol, int iImage)
  2872. {
  2873. CGridCell* pCell = GetCell(nRow, nCol);
  2874. if (!pCell)
  2875. return FALSE;
  2876. pCell->iImage = iImage;
  2877. return TRUE;
  2878. }
  2879. int CGridCtrl::GetItemImage(int nRow, int nCol) const
  2880. {
  2881. CGridCell* pCell = GetCell(nRow, nCol);
  2882. ASSERT(pCell);
  2883. if (!pCell)
  2884. return -1;
  2885. return pCell->iImage;
  2886. }
  2887. BOOL CGridCtrl::SetItemState(int nRow, int nCol, UINT state)
  2888. {
  2889. CGridCell* pCell = GetCell(nRow, nCol);
  2890. ASSERT(pCell);
  2891. if (!pCell)
  2892. return FALSE;
  2893. // If the cell is being unselected, remove it from the selected list
  2894. if ((pCell->state & GVIS_SELECTED) && !(state & GVIS_SELECTED))
  2895. {
  2896. CCellID cell;
  2897. DWORD key = MAKELONG(nRow, nCol);
  2898. if (m_SelectedCellMap.Lookup(key, (CCellID&)cell))
  2899. m_SelectedCellMap.RemoveKey(key);
  2900. }
  2901. // If cell is being selected, add it to the list of selected cells
  2902. else if (!(pCell->state & GVIS_SELECTED) && (state & GVIS_SELECTED))
  2903. {
  2904. CCellID cell(nRow, nCol);
  2905. m_SelectedCellMap.SetAt(MAKELONG(nRow, nCol), cell);
  2906. }
  2907. // Set the cell's state
  2908. pCell->state = state;
  2909. return TRUE;
  2910. }
  2911. UINT CGridCtrl::GetItemState(int nRow, int nCol) const
  2912. {
  2913. CGridCell* pCell = GetCell(nRow, nCol);
  2914. ASSERT(pCell);
  2915. if (!pCell)
  2916. return 0;
  2917. return pCell->state;
  2918. }
  2919. BOOL CGridCtrl::SetItemFormat(int nRow, int nCol, UINT nFormat)
  2920. {
  2921. CGridCell* pCell = GetCell(nRow, nCol);
  2922. ASSERT(pCell);
  2923. if (!pCell)
  2924. return FALSE;
  2925. pCell->nFormat = nFormat;
  2926. return TRUE;
  2927. }
  2928. UINT CGridCtrl::GetItemFormat(int nRow, int nCol) const
  2929. {
  2930. CGridCell* pCell = GetCell(nRow, nCol);
  2931. ASSERT(pCell);
  2932. if (!pCell)
  2933. return 0;
  2934. return pCell->nFormat;
  2935. }
  2936. BOOL CGridCtrl::SetItemBkColour(int nRow, int nCol, COLORREF cr /* = CLR_DEFAULT */)
  2937. {
  2938. CGridCell* pCell = GetCell(nRow, nCol);
  2939. ASSERT(pCell);
  2940. if (!pCell)
  2941. return FALSE;
  2942. pCell->crBkClr = cr;
  2943. return TRUE;
  2944. }
  2945. COLORREF CGridCtrl::GetItemBkColour(int nRow, int nCol) const
  2946. {
  2947. CGridCell* pCell = GetCell(nRow, nCol);
  2948. ASSERT(pCell);
  2949. if (!pCell)
  2950. return 0;
  2951. return pCell->crBkClr;
  2952. }
  2953. BOOL CGridCtrl::SetItemFgColour(int nRow, int nCol, COLORREF cr /* = CLR_DEFAULT */)
  2954. {
  2955. CGridCell* pCell = GetCell(nRow, nCol);
  2956. ASSERT(pCell);
  2957. if (!pCell)
  2958. return FALSE;
  2959. pCell->crFgClr = cr;
  2960. return TRUE;
  2961. }
  2962. COLORREF CGridCtrl::GetItemFgColour(int nRow, int nCol) const
  2963. {
  2964. CGridCell* pCell = GetCell(nRow, nCol);
  2965. ASSERT(pCell);
  2966. if (!pCell)
  2967. return 0;
  2968. return pCell->crFgClr;
  2969. }
  2970. BOOL CGridCtrl::SetItemFont(int nRow, int nCol, LOGFONT* lf)
  2971. {
  2972. CGridCell* pCell = GetCell(nRow, nCol);
  2973. ASSERT(pCell);
  2974. if (!pCell)
  2975. return FALSE;
  2976. memcpy(&(pCell->lfFont), lf, sizeof(LOGFONT));
  2977. return TRUE;
  2978. }
  2979. LOGFONT* CGridCtrl::GetItemFont(int nRow, int nCol)
  2980. {
  2981. CGridCell* pCell = GetCell(nRow, nCol);
  2982. ASSERT(pCell);
  2983. if (!pCell)
  2984. return &m_Logfont;
  2985. return &(pCell->lfFont);
  2986. }
  2987. ////////////////////////////////////////////////////////////////////////////////////
  2988. // Row/Column size functions
  2989. long CGridCtrl::GetVirtualWidth() const
  2990. {
  2991. long lVirtualWidth = 0;
  2992. int iColCount = GetColumnCount();
  2993. for (int i = 0; i < iColCount; i++)
  2994. lVirtualWidth += m_arColWidths[i];
  2995. return lVirtualWidth;
  2996. }
  2997. long CGridCtrl::GetVirtualHeight() const
  2998. {
  2999. long lVirtualHeight = 0;
  3000. int iRowCount = GetRowCount();
  3001. for (int i = 0; i < iRowCount; i++)
  3002. lVirtualHeight += m_arRowHeights[i];
  3003. return lVirtualHeight;
  3004. }
  3005. int CGridCtrl::GetRowHeight(int nRow) const
  3006. {
  3007. ASSERT(nRow >= 0 && nRow < m_nRows);
  3008. if (nRow < 0 || nRow >= m_nRows) return -1;
  3009. return m_arRowHeights[nRow];
  3010. }
  3011. int CGridCtrl::GetColumnWidth(int nCol) const
  3012. {
  3013. ASSERT(nCol >= 0 && nCol < m_nCols);
  3014. if (nCol < 0 || nCol >= m_nCols) return -1;
  3015. return m_arColWidths[nCol];
  3016. }
  3017. BOOL CGridCtrl::SetRowHeight(int nRow, int height)
  3018. {
  3019. ASSERT(nRow >= 0 && nRow < m_nRows && height >= 0);
  3020. if (nRow < 0 || nRow >= m_nRows || height < 0) return FALSE;
  3021. m_arRowHeights[nRow] = height;
  3022. return TRUE;
  3023. }
  3024. BOOL CGridCtrl::SetColumnWidth(int nCol, int width)
  3025. {
  3026. ASSERT(nCol >= 0 && nCol < m_nCols && width >= 0);
  3027. if (nCol < 0 || nCol >= m_nCols || width < 0) return FALSE;
  3028. m_arColWidths[nCol] = width;
  3029. return TRUE;
  3030. }
  3031. int CGridCtrl::GetFixedRowHeight() const
  3032. {
  3033. int nHeight = 0;
  3034. for (int i = 0; i < m_nFixedRows; i++)
  3035. nHeight += GetRowHeight(i);
  3036. return nHeight;
  3037. }
  3038. int CGridCtrl::GetFixedColumnWidth() const
  3039. {
  3040. int nWidth = 0;
  3041. for (int i = 0; i < m_nFixedCols; i++)
  3042. nWidth += GetColumnWidth(i);
  3043. return nWidth;
  3044. }
  3045. BOOL CGridCtrl::AutoSizeColumn(int nCol)
  3046. {
  3047. ASSERT(nCol >= 0 && nCol < m_nCols);
  3048. if (nCol < 0 || nCol >= m_nCols) return FALSE;
  3049. CSize size;
  3050. CDC* pDC = GetDC();
  3051. if (!pDC) return FALSE;
  3052. int nWidth = 0;
  3053. int nNumRows = GetRowCount();
  3054. for (int nRow = 0; nRow < nNumRows; nRow++)
  3055. {
  3056. size = GetCellExtent(nRow, nCol, pDC);
  3057. if (size.cx > nWidth) nWidth = size.cx;
  3058. }
  3059. m_arColWidths[nCol] = nWidth;
  3060. ReleaseDC(pDC);
  3061. ResetScrollBars();
  3062. return TRUE;
  3063. }
  3064. BOOL CGridCtrl::AutoSizeRow(int nRow)
  3065. {
  3066. ASSERT(nRow >= 0 && nRow < m_nRows);
  3067. if (nRow < 0 || nRow >= m_nRows) return FALSE;
  3068. CSize size;
  3069. CDC* pDC = GetDC();
  3070. if (!pDC) return FALSE;
  3071. int nHeight = 0;
  3072. int nNumColumns = GetColumnCount();
  3073. for (int nCol = 0; nCol < nNumColumns; nCol++)
  3074. {
  3075. size = GetCellExtent(nRow, nCol, pDC);
  3076. if (size.cy > nHeight) nHeight = size.cy;
  3077. }
  3078. m_arRowHeights[nRow] = nHeight;
  3079. ReleaseDC(pDC);
  3080. ResetScrollBars();
  3081. return TRUE;
  3082. }
  3083. void CGridCtrl::AutoSizeColumns()
  3084. {
  3085. int nNumColumns = GetColumnCount();
  3086. for (int nCol = 0; nCol < nNumColumns; nCol++)
  3087. AutoSizeColumn(nCol);
  3088. }
  3089. void CGridCtrl::AutoSizeRows()
  3090. {
  3091. int nNumRows = GetRowCount();
  3092. for (int nRow = 0; nRow < nNumRows; nRow++)
  3093. AutoSizeRow(nRow);
  3094. }
  3095. // sizes all rows and columns
  3096. // faster than calling both AutoSizeColumns() and AutoSizeRows()
  3097. void CGridCtrl::AutoSize()
  3098. {
  3099. CDC* pDC = GetDC();
  3100. if (!pDC) return;
  3101. int nNumColumns = GetColumnCount();
  3102. int nNumRows = GetRowCount();
  3103. // initialize column widths to zero
  3104. for (int nCol = 0; nCol < nNumColumns; nCol++)
  3105. m_arColWidths[nCol] = 0;
  3106. // initialize row heights to zero
  3107. for (int nRow = 0; nRow < nNumRows; nRow++)
  3108. m_arRowHeights[nRow] = 0;
  3109. CSize size;
  3110. for (nCol = 0; nCol < nNumColumns; nCol++)
  3111. for (nRow = 0; nRow < nNumRows; nRow++)
  3112. {
  3113. size = GetCellExtent(nRow, nCol, pDC);
  3114. if (size.cx > (int) m_arColWidths[nCol]) m_arColWidths[nCol] = size.cx;
  3115. if (size.cy > (int) m_arRowHeights[nRow]) m_arRowHeights[nRow] = size.cy;
  3116. }
  3117. ReleaseDC(pDC);
  3118. if (m_bAllowDraw) {
  3119. ResetScrollBars();
  3120. Invalidate();
  3121. }
  3122. }
  3123. void CGridCtrl::ExpandColumnsToFit()
  3124. {
  3125. if (GetColumnCount() <= 0) return;
  3126. CRect rect;
  3127. GetClientRect(rect);
  3128. long virtualWidth = GetVirtualWidth();
  3129. int nDifference = rect.Width() - (int) virtualWidth;
  3130. int nColumnAdjustment = nDifference / GetColumnCount();
  3131. for (int i = 0; i < GetColumnCount(); i++)
  3132. m_arColWidths[i] += nColumnAdjustment;
  3133. if (nDifference > 0)
  3134. {
  3135. int leftOver = nDifference % GetColumnCount();
  3136. for (i = 0; i < leftOver; i++)
  3137. m_arColWidths[i] += 1;
  3138. }
  3139. else
  3140. {
  3141. int leftOver = (-nDifference) % GetColumnCount();
  3142. for (i = 0; i < leftOver; i++)
  3143. m_arColWidths[i] -= 1;
  3144. }
  3145. if (m_bAllowDraw)
  3146. Invalidate();
  3147. }
  3148. void CGridCtrl::ExpandRowsToFit()
  3149. {
  3150. if (GetRowCount() <= 0) return;
  3151. CRect rect;
  3152. GetClientRect(rect);
  3153. long virtualHeight = GetVirtualHeight();
  3154. int nDifference = rect.Height() - (int) virtualHeight;
  3155. int nRowAdjustment = nDifference / GetRowCount();
  3156. for (int i = 0; i < GetRowCount(); i++)
  3157. m_arRowHeights[i] += nRowAdjustment;
  3158. if (nDifference > 0)
  3159. {
  3160. int leftOver = nDifference % GetRowCount();
  3161. for (i = 0; i < leftOver; i++)
  3162. m_arRowHeights[i] += 1;
  3163. }
  3164. else
  3165. {
  3166. int leftOver = (-nDifference) % GetRowCount();
  3167. for (i = 0; i < leftOver; i++)
  3168. m_arRowHeights[i] -= 1;
  3169. }
  3170. if (m_bAllowDraw)
  3171. Invalidate();
  3172. }
  3173. void CGridCtrl::ExpandToFit()
  3174. {
  3175. ExpandColumnsToFit(); // This will remove any existing horz scrollbar
  3176. ExpandRowsToFit(); // This will remove any existing vert scrollbar
  3177. ExpandColumnsToFit(); // Just in case the first adjustment was with a vert
  3178. // scrollbar in place
  3179. }
  3180. BOOL CGridCtrl::IsCellFixed(int nRow, int nCol)
  3181. {
  3182. return (nRow < GetFixedRowCount() || nCol < GetFixedColumnCount());
  3183. }
  3184. void CGridCtrl::SetModified(BOOL bModified /*=TRUE*/, int nRow /*=-1*/, int nCol /*=-1*/)
  3185. {
  3186. if (nRow > 0 && nCol > 0)
  3187. {
  3188. if (bModified)
  3189. {
  3190. SetItemState(nRow, nCol, GetItemState(nRow, nCol) | GVIS_MODIFIED);
  3191. m_bModified = TRUE;
  3192. }
  3193. else
  3194. SetItemState(nRow, nCol, GetItemState(nRow, nCol) & ~GVIS_MODIFIED);
  3195. }
  3196. else
  3197. m_bModified = bModified;
  3198. if (!m_bModified)
  3199. {
  3200. for (int row = 0; row < GetRowCount(); row++)
  3201. for (int col = 0; col < GetColumnCount(); col++)
  3202. SetItemState(row, col, GetItemState(row, col) & ~GVIS_MODIFIED);
  3203. }
  3204. }
  3205. BOOL CGridCtrl::GetModified(int nRow /*=-1*/, int nCol /*=-1*/)
  3206. {
  3207. if (nRow > 0 && nCol > 0)
  3208. return ( (GetItemState(nRow, nCol) & GVIS_MODIFIED) == GVIS_MODIFIED );
  3209. else
  3210. return m_bModified;
  3211. }
  3212. /////////////////////////////////////////////////////////////////////////////////////
  3213. // GridCtrl cell visibility tests and invalidation/redraw functions
  3214. // EnsureVisible supplied by Roelf Werkman
  3215. void CGridCtrl::EnsureVisible(int nRow, int nCol)
  3216. {
  3217. CCellRange VisibleCells = GetVisibleNonFixedCellRange();
  3218. int right = nCol - VisibleCells.GetMaxCol();
  3219. int left = VisibleCells.GetMinCol() - nCol;
  3220. int down = nRow - VisibleCells.GetMaxRow();
  3221. int up = VisibleCells.GetMinRow() - nRow;
  3222. while (right > 0)
  3223. {
  3224. SendMessage(WM_HSCROLL, SB_LINERIGHT, 0);
  3225. right--;
  3226. }
  3227. while (left > 0)
  3228. {
  3229. SendMessage(WM_HSCROLL, SB_LINELEFT, 0);
  3230. left--;
  3231. }
  3232. while (down > 0)
  3233. {
  3234. SendMessage(WM_VSCROLL, SB_LINEDOWN, 0);
  3235. down--;
  3236. }
  3237. while (up > 0)
  3238. {
  3239. SendMessage(WM_VSCROLL, SB_LINEUP, 0);
  3240. up--;
  3241. }
  3242. // Move one more if we only see a snall bit of the cell
  3243. CRect rectCell, rectWindow;
  3244. GetCellRect(nRow, nCol, rectCell);
  3245. GetClientRect(rectWindow);
  3246. if (rectCell.right > rectWindow.right)
  3247. SendMessage(WM_HSCROLL, SB_LINERIGHT, 0);
  3248. if (rectCell.bottom > rectWindow.bottom)
  3249. SendMessage(WM_VSCROLL, SB_LINEDOWN, 0);
  3250. }
  3251. BOOL CGridCtrl::IsCellEditable(CCellID &cell) const
  3252. {
  3253. return IsCellEditable(cell.row, cell.col);
  3254. }
  3255. BOOL CGridCtrl::IsCellEditable(int nRow, int nCol) const
  3256. {
  3257. return IsEditable() && ((GetItemState(nRow, nCol) & GVIS_READONLY) != GVIS_READONLY);
  3258. }
  3259. BOOL CGridCtrl::IsCellVisible(CCellID cell) const
  3260. {
  3261. return IsCellVisible(cell.row, cell.col);
  3262. }
  3263. BOOL CGridCtrl::IsCellVisible(int nRow, int nCol) const
  3264. {
  3265. if (!IsWindow(m_hWnd))
  3266. return FALSE;
  3267. int x,y;
  3268. CCellID TopLeft;
  3269. if (nCol >= GetFixedColumnCount() || nRow >= GetFixedRowCount())
  3270. {
  3271. TopLeft = GetTopleftNonFixedCell();
  3272. if (nCol >= GetFixedColumnCount() && nCol < TopLeft.col) return FALSE;
  3273. if (nRow >= GetFixedRowCount() && nRow < TopLeft.row) return FALSE;
  3274. }
  3275. CRect rect;
  3276. GetClientRect(rect);
  3277. if (nCol < GetFixedColumnCount())
  3278. {
  3279. x = 0;
  3280. for (int i = 0; i <= nCol; i++)
  3281. {
  3282. if (x >= rect.right) return FALSE;
  3283. x += GetColumnWidth(i);
  3284. }
  3285. }
  3286. else
  3287. {
  3288. x = GetFixedColumnWidth();
  3289. for (int i = TopLeft.col; i <= nCol; i++)
  3290. {
  3291. if (x >= rect.right) return FALSE;
  3292. x += GetColumnWidth(i);
  3293. }
  3294. }
  3295. if (nRow < GetFixedRowCount())
  3296. {
  3297. y = 0;
  3298. for (int i = 0; i <= nRow; i++)
  3299. {
  3300. if (y >= rect.bottom) return FALSE;
  3301. y += GetRowHeight(i);
  3302. }
  3303. }
  3304. else
  3305. {
  3306. if (nRow < TopLeft.row) return FALSE;
  3307. y = GetFixedRowHeight();
  3308. for (int i = TopLeft.row; i <= nRow; i++)
  3309. {
  3310. if (y >= rect.bottom) return FALSE;
  3311. y += GetRowHeight(i);
  3312. }
  3313. }
  3314. return TRUE;
  3315. }
  3316. BOOL CGridCtrl::InvalidateCellRect(const CCellID& cell)
  3317. {
  3318. if (!::IsWindow(GetSafeHwnd()) || !m_bAllowDraw)
  3319. return FALSE;
  3320. ASSERT(IsValid(cell));
  3321. if (!IsCellVisible(cell.row, cell.col)) return FALSE;
  3322. CRect rect;
  3323. if (!GetCellRect(cell, rect)) return FALSE;
  3324. rect.right++; rect.bottom++;
  3325. InvalidateRect(rect, TRUE);
  3326. return TRUE;
  3327. }
  3328. BOOL CGridCtrl::InvalidateCellRect(const CCellRange& cellRange)
  3329. {
  3330. ASSERT(IsValid(cellRange));
  3331. if (!::IsWindow(GetSafeHwnd()) || !m_bAllowDraw) return FALSE;
  3332. CCellRange visibleCellRange = GetVisibleNonFixedCellRange().Intersect(cellRange);
  3333. CRect rect;
  3334. if (!GetCellRangeRect(visibleCellRange, rect)) return FALSE;
  3335. rect.right++; rect.bottom++;
  3336. InvalidateRect(rect, TRUE);
  3337. return TRUE;
  3338. }
  3339. /////////////////////////////////////////////////////////////////////////////
  3340. // CGridCtrl Mouse stuff
  3341. // Handles mouse wheel notifications
  3342. // Note - if this doesn't work for win95 then use OnRegisteredMouseWheel instead
  3343. #if !defined(_WIN32_WCE) && (_MFC_VER >= 0x0421)
  3344. BOOL CGridCtrl::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
  3345. {
  3346. // A m_nRowsPerWheelNotch value less than 0 indicates that the mouse
  3347. // wheel scrolls whole pages, not just lines.
  3348. if (m_nRowsPerWheelNotch == -1)
  3349. {
  3350. int nPagesScrolled = zDelta / 120;
  3351. if (nPagesScrolled > 0)
  3352. for (int i = 0; i < nPagesScrolled; i++)
  3353. PostMessage(WM_VSCROLL, SB_PAGEUP, 0);
  3354. else
  3355. for (int i = 0; i > nPagesScrolled; i--)
  3356. PostMessage(WM_VSCROLL, SB_PAGEDOWN, 0);
  3357. }
  3358. else
  3359. {
  3360. int nRowsScrolled = m_nRowsPerWheelNotch * zDelta / 120;
  3361. if (nRowsScrolled > 0)
  3362. for (int i = 0; i < nRowsScrolled; i++)
  3363. PostMessage(WM_VSCROLL, SB_LINEUP, 0);
  3364. else
  3365. for (int i = 0; i > nRowsScrolled; i--)
  3366. PostMessage(WM_VSCROLL, SB_LINEDOWN, 0);
  3367. }
  3368. return CWnd::OnMouseWheel(nFlags, zDelta, pt);
  3369. }
  3370. #endif // !defined(_WIN32_WCE) && (_MFC_VER >= 0x0421)
  3371. void CGridCtrl::OnMouseMove(UINT nFlags, CPoint point)
  3372. {
  3373. CRect rect;
  3374. GetClientRect(rect);
  3375. #ifndef GRIDCONTROL_NO_DRAGDROP
  3376. // If outside client area, return (unless we are drag n dropping)
  3377. if (m_MouseMode != MOUSE_DRAGGING && !rect.PtInRect(point))
  3378. return;
  3379. #endif
  3380. // If the left mouse button is up, then test to see if row/column sizing is imminent
  3381. if (!(nFlags & MK_LBUTTON)
  3382. || (m_MouseMode == MOUSE_NOTHING && (nFlags & MK_LBUTTON)))
  3383. {
  3384. if (m_bAllowColumnResize && MouseOverColumnResizeArea(point))
  3385. {
  3386. if (m_MouseMode != MOUSE_OVER_COL_DIVIDE)
  3387. {
  3388. SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZEWE));
  3389. m_MouseMode = MOUSE_OVER_COL_DIVIDE;
  3390. }
  3391. }
  3392. else if (m_bAllowRowResize && MouseOverRowResizeArea(point))
  3393. {
  3394. if (m_MouseMode != MOUSE_OVER_ROW_DIVIDE)
  3395. {
  3396. SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZENS));
  3397. m_MouseMode = MOUSE_OVER_ROW_DIVIDE;
  3398. }
  3399. }
  3400. else if (m_MouseMode != MOUSE_NOTHING)
  3401. {
  3402. SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
  3403. m_MouseMode = MOUSE_NOTHING;
  3404. }
  3405. #ifndef GRIDCONTROL_NO_TITLETIPS
  3406. if (m_MouseMode == MOUSE_NOTHING && m_bTitleTips)
  3407. {
  3408. CCellID idCurrentCell = GetCellFromPt(point);
  3409. CRect TextRect, CellRect;
  3410. if (GetTextRect(idCurrentCell.row, idCurrentCell.col, TextRect) &&
  3411. GetCellRect(idCurrentCell.row, idCurrentCell.col, CellRect))
  3412. m_TitleTip.Show(TextRect,
  3413. GetItemText(idCurrentCell.row, idCurrentCell.col),
  3414. 0,
  3415. CellRect,
  3416. GetItemFont(idCurrentCell.row, idCurrentCell.col));
  3417. }
  3418. #endif
  3419. m_LastMousePoint = point;
  3420. return;
  3421. }
  3422. if (!IsValid(m_LeftClickDownCell))
  3423. {
  3424. m_LastMousePoint = point;
  3425. return;
  3426. }
  3427. // If the left mouse button is down, then process appropriately
  3428. if (nFlags & MK_LBUTTON)
  3429. {
  3430. switch(m_MouseMode)
  3431. {
  3432. case MOUSE_SELECT_ALL:
  3433. break;
  3434. case MOUSE_SELECT_COL:
  3435. case MOUSE_SELECT_ROW:
  3436. case MOUSE_SELECT_CELLS:
  3437. {
  3438. CCellID idCurrentCell = GetCellFromPt(point);
  3439. if (!IsValid(idCurrentCell))
  3440. return;
  3441. if (idCurrentCell != GetFocusCell())
  3442. {
  3443. OnSelecting(idCurrentCell);
  3444. //SetFocusCell(max(idCurrentCell.row, m_nFixedRows),
  3445. // max(idCurrentCell.col, m_nFixedCols));
  3446. if (idCurrentCell.row >= m_nFixedRows &&
  3447. idCurrentCell.col >= m_nFixedCols)
  3448. {
  3449. SetFocusCell(idCurrentCell);
  3450. }
  3451. }
  3452. break;
  3453. }
  3454. case MOUSE_SIZING_COL:
  3455. {
  3456. CDC* pDC = GetDC();
  3457. if (!pDC) break;
  3458. CRect oldInvertedRect(m_LastMousePoint.x, rect.top,
  3459. m_LastMousePoint.x + 2, rect.bottom);
  3460. pDC->InvertRect(&oldInvertedRect);
  3461. CRect newInvertedRect(point.x, rect.top,
  3462. point.x + 2, rect.bottom);
  3463. pDC->InvertRect(&newInvertedRect);
  3464. ReleaseDC(pDC);
  3465. }
  3466. break;
  3467. case MOUSE_SIZING_ROW:
  3468. {
  3469. CDC* pDC = GetDC();
  3470. if (!pDC) break;
  3471. CRect oldInvertedRect(rect.left, m_LastMousePoint.y,
  3472. rect.right, m_LastMousePoint.y + 2);
  3473. pDC->InvertRect(&oldInvertedRect);
  3474. CRect newInvertedRect(rect.left, point.y,
  3475. rect.right, point.y + 2);
  3476. pDC->InvertRect(&newInvertedRect);
  3477. ReleaseDC(pDC);
  3478. }
  3479. break;
  3480. #ifndef GRIDCONTROL_NO_DRAGDROP
  3481. case MOUSE_PREPARE_DRAG:
  3482. OnBeginDrag();
  3483. break;
  3484. #endif
  3485. }
  3486. }
  3487. m_LastMousePoint = point;
  3488. }
  3489. void CGridCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
  3490. {
  3491. if (m_MouseMode == MOUSE_OVER_COL_DIVIDE)
  3492. {
  3493. CCellID cell = GetCellFromPt(point);
  3494. ASSERT(IsValid(cell));
  3495. CPoint start;
  3496. if (!GetCellOrigin(0, cell.col, &start)) return;
  3497. if (point.x - start.x <= m_nResizeCaptureRange) // Clicked right of border
  3498. cell.col--;
  3499. AutoSizeColumn(cell.col);
  3500. Invalidate();
  3501. }
  3502. else if (m_MouseMode == MOUSE_OVER_ROW_DIVIDE)
  3503. {
  3504. CCellID cell = GetCellFromPt(point);
  3505. ASSERT(IsValid(cell));
  3506. CPoint start;
  3507. if (!GetCellOrigin(0, cell.col, &start)) return;
  3508. if (point.y - start.y <= m_nResizeCaptureRange) // Clicked below border
  3509. cell.row--;
  3510. AutoSizeRow(cell.row);
  3511. Invalidate();
  3512. }
  3513. else if (m_MouseMode == MOUSE_NOTHING)
  3514. {
  3515. if (m_LeftClickDownCell.row >= m_nFixedRows &&
  3516. IsValid(m_LeftClickDownCell) &&
  3517. m_LeftClickDownCell.col >= m_nFixedCols)
  3518. {
  3519. OnEditCell(m_idCurrentCell.row, m_idCurrentCell.col, VK_LBUTTON);
  3520. }
  3521. else if (m_bListMode)
  3522. {
  3523. CCellID cell = GetCellFromPt(point);
  3524. if (!IsValid(cell)) return;
  3525. if (cell.row >= m_nFixedRows && cell.col < GetFixedColumnCount())
  3526. OnEditCell(cell.row, cell.col, VK_LBUTTON);
  3527. }
  3528. }
  3529. CWnd::OnLButtonDblClk(nFlags, point);
  3530. }
  3531. void CGridCtrl::OnLButtonDown(UINT nFlags, CPoint point)
  3532. {
  3533. HWND hOldFocusWnd = ::GetFocus();
  3534. m_LeftClickDownPoint = point;
  3535. m_LeftClickDownCell = GetCellFromPt(point);
  3536. if (!IsValid(m_LeftClickDownCell)) return;
  3537. m_SelectionStartCell = (nFlags & MK_SHIFT)? m_idCurrentCell : m_LeftClickDownCell;
  3538. SetFocus(); // Auto-destroy any InPlaceEdit's
  3539. // If the user clicks on the current cell, then prepare to edit it.
  3540. // (If the user moves the mouse, then dragging occurs)
  3541. if (m_LeftClickDownCell == m_idCurrentCell)
  3542. {
  3543. m_MouseMode = MOUSE_PREPARE_EDIT;
  3544. return;
  3545. }
  3546. else if (m_MouseMode != MOUSE_OVER_COL_DIVIDE &&
  3547. m_MouseMode != MOUSE_OVER_ROW_DIVIDE)
  3548. {
  3549. SetFocusCell(-1,-1);
  3550. SetFocusCell(max(m_LeftClickDownCell.row, m_nFixedRows),
  3551. max(m_LeftClickDownCell.col, m_nFixedCols));
  3552. }
  3553. #ifndef GRIDCONTROL_NO_DRAGDROP
  3554. // If the user clicks on a selected cell, then prepare to drag it.
  3555. // (If the user moves the mouse, then dragging occurs)
  3556. if (m_bAllowDragAndDrop && hOldFocusWnd == GetSafeHwnd() &&
  3557. GetItemState(m_LeftClickDownCell.row, m_LeftClickDownCell.col) & GVNI_SELECTED)
  3558. {
  3559. m_MouseMode = MOUSE_PREPARE_DRAG;
  3560. return;
  3561. }
  3562. #endif
  3563. SetCapture();
  3564. if (m_MouseMode == MOUSE_NOTHING)
  3565. {
  3566. if (m_bAllowColumnResize && MouseOverColumnResizeArea(point))
  3567. {
  3568. if (m_MouseMode != MOUSE_OVER_COL_DIVIDE)
  3569. {
  3570. SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZEWE));
  3571. m_MouseMode = MOUSE_OVER_COL_DIVIDE;
  3572. }
  3573. }
  3574. else if (m_bAllowRowResize && MouseOverRowResizeArea(point))
  3575. {
  3576. if (m_MouseMode != MOUSE_OVER_ROW_DIVIDE)
  3577. {
  3578. SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZENS));
  3579. m_MouseMode = MOUSE_OVER_ROW_DIVIDE;
  3580. }
  3581. }
  3582. //else if (m_MouseMode != MOUSE_NOTHING)
  3583. //{
  3584. // SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
  3585. // m_MouseMode = MOUSE_NOTHING;
  3586. //}
  3587. }
  3588. if (m_MouseMode == MOUSE_OVER_COL_DIVIDE) // sizing column
  3589. {
  3590. m_MouseMode = MOUSE_SIZING_COL;
  3591. CPoint start;
  3592. if (!GetCellOrigin(0, m_LeftClickDownCell.col, &start)) return;
  3593. CRect rect;
  3594. GetClientRect(rect);
  3595. CRect invertedRect(point.x, rect.top, point.x + 2, rect.bottom);
  3596. CDC* pDC = GetDC();
  3597. if (pDC) {
  3598. pDC->InvertRect(&invertedRect);
  3599. ReleaseDC(pDC);
  3600. }
  3601. if (point.x - start.x <= m_nResizeCaptureRange) // clicked right of border
  3602. if (!GetCellOrigin(0, --m_LeftClickDownCell.col, &start)) return;
  3603. rect.left = start.x;
  3604. ClientToScreen(rect);
  3605. #ifndef _WIN32_WCE_NO_CURSOR
  3606. ClipCursor(rect);
  3607. #endif
  3608. }
  3609. else if (m_MouseMode == MOUSE_OVER_ROW_DIVIDE) // sizing row
  3610. {
  3611. m_MouseMode = MOUSE_SIZING_ROW;
  3612. CPoint start;
  3613. if (!GetCellOrigin(m_LeftClickDownCell, &start)) return;
  3614. CRect rect;
  3615. GetClientRect(rect);
  3616. CRect invertedRect(rect.left, point.y, rect.right, point.y + 2);
  3617. CDC* pDC = GetDC();
  3618. if (pDC) {
  3619. pDC->InvertRect(&invertedRect);
  3620. ReleaseDC(pDC);
  3621. }
  3622. if (point.y - start.y <= m_nResizeCaptureRange) // clicked below border
  3623. if (!GetCellOrigin(--m_LeftClickDownCell.row, 0, &start)) return;
  3624. rect.top = start.y;
  3625. ClientToScreen(rect);
  3626. #ifndef _WIN32_WCE_NO_CURSOR
  3627. ClipCursor(rect);
  3628. #endif
  3629. }
  3630. else // not sizing or editing -- selecting
  3631. {
  3632. // If Ctrl pressed, save the current cell selection. This will get added
  3633. // to the new cell selection at the end of the cell selection process
  3634. m_PrevSelectedCellMap.RemoveAll();
  3635. if (nFlags & MK_CONTROL) {
  3636. for (POSITION pos = m_SelectedCellMap.GetStartPosition(); pos != NULL; )
  3637. {
  3638. DWORD key;
  3639. CCellID cell;
  3640. m_SelectedCellMap.GetNextAssoc(pos, key, (CCellID&)cell);
  3641. m_PrevSelectedCellMap.SetAt(key, cell);
  3642. }
  3643. }
  3644. if (m_LeftClickDownCell.row < GetFixedRowCount())
  3645. OnFixedRowClick(m_LeftClickDownCell);
  3646. else if (m_LeftClickDownCell.col < GetFixedColumnCount())
  3647. OnFixedColumnClick(m_LeftClickDownCell);
  3648. else
  3649. {
  3650. m_MouseMode = m_bListMode? MOUSE_SELECT_ROW : MOUSE_SELECT_CELLS;
  3651. OnSelecting(m_LeftClickDownCell);
  3652. }
  3653. m_nTimerID = SetTimer(WM_LBUTTONDOWN, m_nTimerInterval, 0);
  3654. }
  3655. m_LastMousePoint = point;
  3656. }
  3657. void CGridCtrl::OnLButtonUp(UINT nFlags, CPoint point)
  3658. {
  3659. CWnd::OnLButtonUp(nFlags, point);
  3660. #ifndef _WIN32_WCE_NO_CURSOR
  3661. ClipCursor(NULL);
  3662. #endif
  3663. if (GetCapture()->GetSafeHwnd() == GetSafeHwnd())
  3664. {
  3665. ReleaseCapture();
  3666. KillTimer(m_nTimerID);
  3667. m_nTimerID = 0;
  3668. }
  3669. // m_MouseMode == MOUSE_PREPARE_EDIT only if user clicked down on current cell
  3670. // and then didn't move mouse before clicking up (releasing button)
  3671. if (m_MouseMode == MOUSE_PREPARE_EDIT)
  3672. {
  3673. OnEditCell(m_idCurrentCell.row, m_idCurrentCell.col, VK_LBUTTON);
  3674. }
  3675. #ifndef GRIDCONTROL_NO_DRAGDROP
  3676. // m_MouseMode == MOUSE_PREPARE_DRAG only if user clicked down on a selected cell
  3677. // and then didn't move mouse before clicking up (releasing button)
  3678. else if (m_MouseMode == MOUSE_PREPARE_DRAG)
  3679. {
  3680. ResetSelectedRange();
  3681. }
  3682. #endif
  3683. else if (m_MouseMode == MOUSE_SIZING_COL)
  3684. {
  3685. CRect rect;
  3686. GetClientRect(rect);
  3687. CRect invertedRect(m_LastMousePoint.x, rect.top, m_LastMousePoint.x + 2, rect.bottom);
  3688. CDC* pDC = GetDC();
  3689. if (pDC) {
  3690. pDC->InvertRect(&invertedRect);
  3691. ReleaseDC(pDC);
  3692. }
  3693. if (m_LeftClickDownPoint != point)
  3694. {
  3695. CPoint start;
  3696. if (!GetCellOrigin(m_LeftClickDownCell, &start)) return;
  3697. SetColumnWidth(m_LeftClickDownCell.col, point.x - start.x);
  3698. ResetScrollBars();
  3699. Invalidate();
  3700. }
  3701. }
  3702. else if (m_MouseMode == MOUSE_SIZING_ROW)
  3703. {
  3704. CRect rect;
  3705. GetClientRect(rect);
  3706. CRect invertedRect(rect.left, m_LastMousePoint.y, rect.right, m_LastMousePoint.y + 2);
  3707. CDC* pDC = GetDC();
  3708. if (pDC) {
  3709. pDC->InvertRect(&invertedRect);
  3710. ReleaseDC(pDC);
  3711. }
  3712. if (m_LeftClickDownPoint != point)
  3713. {
  3714. CPoint start;
  3715. if (!GetCellOrigin(m_LeftClickDownCell, &start)) return;
  3716. SetRowHeight(m_LeftClickDownCell.row, point.y - start.y);
  3717. ResetScrollBars();
  3718. Invalidate();
  3719. }
  3720. }
  3721. m_MouseMode = MOUSE_NOTHING;
  3722. #ifndef _WIN32_WCE_NO_CURSOR
  3723. SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
  3724. #endif
  3725. if (!IsValid(m_LeftClickDownCell)) return;
  3726. CWnd *pOwner = GetOwner();
  3727. if (pOwner && IsWindow(pOwner->m_hWnd))
  3728. pOwner->PostMessage(WM_COMMAND, MAKELONG(GetDlgCtrlID(), BN_CLICKED),
  3729. (LPARAM) GetSafeHwnd());
  3730. }
  3731. #if !defined(_WIN32_WCE_NO_PRINTING) && !defined(GRIDCONTROL_NO_PRINTING)
  3732. /////////////////////////////////////////////////////////////////////////////
  3733. // CGridCtrl printing
  3734. void CGridCtrl::Print()
  3735. {
  3736. CDC dc;
  3737. CPrintDialog printDlg(FALSE);
  3738. if (printDlg.DoModal() != IDOK) // Get printer settings from user
  3739. return;
  3740. dc.Attach(printDlg.GetPrinterDC()); // attach a printer DC
  3741. dc.m_bPrinting = TRUE;
  3742. CString strTitle;
  3743. strTitle.LoadString(AFX_IDS_APP_TITLE);
  3744. DOCINFO di; // Initialise print doc details
  3745. memset(&di, 0, sizeof (DOCINFO));
  3746. di.cbSize = sizeof (DOCINFO);
  3747. di.lpszDocName = strTitle;
  3748. BOOL bPrintingOK = dc.StartDoc(&di); // Begin a new print job
  3749. CPrintInfo Info;
  3750. Info.m_rectDraw.SetRect(0,0, dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES));
  3751. OnBeginPrinting(&dc, &Info); // Initialise printing
  3752. for (UINT page = Info.GetMinPage(); page <= Info.GetMaxPage() && bPrintingOK; page++)
  3753. {
  3754. dc.StartPage(); // begin new page
  3755. Info.m_nCurPage = page;
  3756. OnPrint(&dc, &Info); // Print page
  3757. bPrintingOK = (dc.EndPage() > 0); // end page
  3758. }
  3759. OnEndPrinting(&dc, &Info); // Clean up after printing
  3760. if (bPrintingOK)
  3761. dc.EndDoc(); // end a print job
  3762. else
  3763. dc.AbortDoc(); // abort job.
  3764. dc.Detach(); // detach the printer DC
  3765. }
  3766. /////////////////////////////////////////////////////////////////////////////
  3767. // CGridCtrl printing overridables - for Doc/View print/print preview framework
  3768. void CGridCtrl::OnBeginPrinting(CDC *pDC, CPrintInfo *pInfo)
  3769. {
  3770. // OnBeginPrinting() is called after the user has committed to
  3771. // printing by OK'ing the Print dialog, and after the framework
  3772. // has created a CDC object for the printer or the preview view.
  3773. // This is the right opportunity to set up the page range.
  3774. // Given the CDC object, we can determine how many rows will
  3775. // fit on a page, so we can in turn determine how many printed
  3776. // pages represent the entire document.
  3777. ASSERT(pDC && pInfo);
  3778. if (!pDC || !pInfo) return;
  3779. int nMaxRowCount = GetRowCount() - GetFixedRowCount();
  3780. if (!nMaxRowCount) return;
  3781. // Get a DC for the current window (will be a screen DC for print previewing)
  3782. CDC *pCurrentDC = GetDC(); // will have dimensions of the client area
  3783. if (!pCurrentDC) return;
  3784. CSize PaperPixelsPerInch(pDC->GetDeviceCaps(LOGPIXELSX), pDC->GetDeviceCaps(LOGPIXELSY));
  3785. CSize ScreenPixelsPerInch(pCurrentDC->GetDeviceCaps(LOGPIXELSX), pCurrentDC->GetDeviceCaps(LOGPIXELSY));
  3786. // Create the printer font
  3787. int nFontSize = -9;
  3788. CString strFontName = "Times New Roman";
  3789. m_PrinterFont.CreateFont(nFontSize, 0,0,0, FW_NORMAL, 0,0,0, DEFAULT_CHARSET,
  3790. OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY,
  3791. DEFAULT_PITCH | FF_DONTCARE, strFontName);
  3792. CFont *pOldFont = pDC->SelectObject(&m_PrinterFont);
  3793. // Get the average character width (in GridCtrl units) and hence the margins
  3794. m_CharSize = pDC->GetTextExtent(_T("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSATUVWXYZ"),52);
  3795. m_CharSize.cx /= 52;
  3796. int nMargins = (LEFT_MARGIN+RIGHT_MARGIN)*m_CharSize.cx;
  3797. // Get the page sizes (physical and logical)
  3798. m_PaperSize = CSize(pDC->GetDeviceCaps(HORZRES), pDC->GetDeviceCaps(VERTRES));
  3799. m_LogicalPageSize.cx = GetVirtualWidth()+nMargins;
  3800. #ifdef _WIN32_WCE
  3801. m_LogicalPageSize.cy = (m_LogicalPageSize.cx * m_PaperSize.cy) / m_PaperSize.cx;
  3802. #else
  3803. m_LogicalPageSize.cy = MulDiv(m_LogicalPageSize.cx, m_PaperSize.cy, m_PaperSize.cx);
  3804. #endif
  3805. m_nPageHeight = m_LogicalPageSize.cy - GetFixedRowHeight()
  3806. - (HEADER_HEIGHT+FOOTER_HEIGHT + 2*GAP)*m_CharSize.cy;
  3807. // Get the number of pages. Assumes no row is bigger than the page size.
  3808. int nTotalRowHeight = 0;
  3809. int nNumPages = 1;
  3810. for (int row = GetFixedRowCount(); row < GetRowCount(); row++)
  3811. {
  3812. nTotalRowHeight += GetRowHeight(row);
  3813. if (nTotalRowHeight > m_nPageHeight) {
  3814. nNumPages++;
  3815. nTotalRowHeight = GetRowHeight(row);
  3816. }
  3817. }
  3818. // Set up the print info
  3819. pInfo->SetMaxPage(nNumPages);
  3820. pInfo->m_nCurPage = 1; // start printing at page# 1
  3821. ReleaseDC(pCurrentDC);
  3822. pDC->SelectObject(pOldFont);
  3823. }
  3824. void CGridCtrl::OnPrint(CDC *pDC, CPrintInfo *pInfo)
  3825. {
  3826. if (!pDC || !pInfo) return;
  3827. //CRect rcPage(pInfo->m_rectDraw);
  3828. CFont *pOldFont = pDC->SelectObject(&m_PrinterFont);
  3829. // Set the page map mode to use GridCtrl units, and setup margin
  3830. pDC->SetMapMode(MM_ANISOTROPIC);
  3831. pDC->SetWindowExt(m_LogicalPageSize);
  3832. pDC->SetViewportExt(m_PaperSize);
  3833. pDC->SetWindowOrg(-LEFT_MARGIN*m_CharSize.cx, 0);
  3834. // Header
  3835. pInfo->m_rectDraw.top = 0;
  3836. pInfo->m_rectDraw.left = 0;
  3837. pInfo->m_rectDraw.right = m_LogicalPageSize.cx - (LEFT_MARGIN+RIGHT_MARGIN)*m_CharSize.cx;
  3838. pInfo->m_rectDraw.bottom = HEADER_HEIGHT*m_CharSize.cy;
  3839. PrintHeader(pDC, pInfo);
  3840. pDC->OffsetWindowOrg(0, -HEADER_HEIGHT*m_CharSize.cy);
  3841. // Gap between header and column headings
  3842. pDC->OffsetWindowOrg(0, -GAP*m_CharSize.cy);
  3843. // Print the column headings
  3844. pInfo->m_rectDraw.bottom = GetFixedRowHeight();
  3845. PrintColumnHeadings(pDC, pInfo);
  3846. pDC->OffsetWindowOrg(0, -GetFixedRowHeight());
  3847. // We need to find out which row to start printing for this page.
  3848. int nTotalRowHeight = 0;
  3849. UINT nNumPages = 1;
  3850. int nCurrPrintRow = GetFixedRowCount();
  3851. while (nCurrPrintRow < GetRowCount() && nNumPages < pInfo->m_nCurPage)
  3852. {
  3853. nTotalRowHeight += GetRowHeight(nCurrPrintRow);
  3854. if (nTotalRowHeight > m_nPageHeight) {
  3855. nNumPages++;
  3856. if (nNumPages == pInfo->m_nCurPage) break;
  3857. nTotalRowHeight = GetRowHeight(nCurrPrintRow);
  3858. }
  3859. nCurrPrintRow++;
  3860. }
  3861. if (nCurrPrintRow >= GetRowCount()) return;
  3862. // Draw as many rows as will fit on the printed page.
  3863. // Clip the printed page so that there is no partially shown
  3864. // row at the bottom of the page (the same row which will be fully
  3865. // shown at the top of the next page).
  3866. BOOL bFirstPrintedRow = TRUE;
  3867. CRect rect;
  3868. rect.bottom = -1;
  3869. while (nCurrPrintRow < GetRowCount())
  3870. {
  3871. rect.top = rect.bottom+1;
  3872. rect.bottom = rect.top + GetRowHeight(nCurrPrintRow) - 1;
  3873. if (rect.bottom > m_nPageHeight) break; // Gone past end of page
  3874. rect.right = -1;
  3875. for (int col = 0; col < GetColumnCount(); col++)
  3876. {
  3877. rect.left = rect.right+1;
  3878. rect.right = rect.left + GetColumnWidth(col) - 1;
  3879. DrawCell(pDC, nCurrPrintRow, col, rect);
  3880. if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_HORZ)
  3881. {
  3882. int Overlap = (col == 0)? 0:1;
  3883. pDC->MoveTo(rect.left-Overlap, rect.bottom);
  3884. pDC->LineTo(rect.right, rect.bottom);
  3885. if (nCurrPrintRow == 0) {
  3886. pDC->MoveTo(rect.left-Overlap, rect.top);
  3887. pDC->LineTo(rect.right, rect.top);
  3888. }
  3889. }
  3890. if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_VERT)
  3891. {
  3892. int Overlap = (bFirstPrintedRow)? 0:1;
  3893. pDC->MoveTo(rect.right, rect.top-Overlap);
  3894. pDC->LineTo(rect.right, rect.bottom);
  3895. if (col == 0) {
  3896. pDC->MoveTo(rect.left, rect.top-Overlap);
  3897. pDC->LineTo(rect.left, rect.bottom);
  3898. }
  3899. }
  3900. }
  3901. nCurrPrintRow++;
  3902. bFirstPrintedRow = FALSE;
  3903. }
  3904. // Footer
  3905. pInfo->m_rectDraw.bottom = FOOTER_HEIGHT*m_CharSize.cy;
  3906. pDC->SetWindowOrg(-LEFT_MARGIN*m_CharSize.cx, -m_LogicalPageSize.cy + FOOTER_HEIGHT*m_CharSize.cy);
  3907. PrintFooter(pDC, pInfo);
  3908. // SetWindowOrg back for next page
  3909. pDC->SetWindowOrg(0,0);
  3910. pDC->SelectObject(pOldFont);
  3911. }
  3912. void CGridCtrl::PrintColumnHeadings(CDC *pDC, CPrintInfo* /*pInfo*/)
  3913. {
  3914. CFont *pOldFont = pDC->SelectObject(&m_PrinterFont);
  3915. CRect rect;
  3916. rect.bottom = -1;
  3917. for (int row = 0; row < GetFixedRowCount(); row++)
  3918. {
  3919. rect.top = rect.bottom+1;
  3920. rect.bottom = rect.top + GetRowHeight(row) - 1;
  3921. rect.right = -1;
  3922. for (int col = 0; col < GetColumnCount(); col++)
  3923. {
  3924. rect.left = rect.right+1;
  3925. rect.right = rect.left + GetColumnWidth(col) - 1;
  3926. DrawFixedCell(pDC, row, col, rect);
  3927. if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_HORZ)
  3928. {
  3929. int Overlap = (col == 0)? 0:1;
  3930. pDC->MoveTo(rect.left-Overlap, rect.bottom);
  3931. pDC->LineTo(rect.right, rect.bottom);
  3932. if (row == 0) {
  3933. pDC->MoveTo(rect.left-Overlap, rect.top);
  3934. pDC->LineTo(rect.right, rect.top);
  3935. }
  3936. }
  3937. if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_VERT)
  3938. {
  3939. int Overlap = (row == 0)? 0:1;
  3940. pDC->MoveTo(rect.right, rect.top-Overlap);
  3941. pDC->LineTo(rect.right, rect.bottom);
  3942. if (col == 0) {
  3943. pDC->MoveTo(rect.left, rect.top-Overlap);
  3944. pDC->LineTo(rect.left, rect.bottom);
  3945. }
  3946. }
  3947. }
  3948. }
  3949. pDC->SelectObject(pOldFont);
  3950. }
  3951. void CGridCtrl::PrintHeader(CDC *pDC, CPrintInfo *pInfo)
  3952. {
  3953. CRect rc(pInfo->m_rectDraw);
  3954. CString strHeaderString;
  3955. CFont BoldFont;
  3956. LOGFONT lf;
  3957. //create bold font for header and footer
  3958. VERIFY(m_PrinterFont.GetLogFont(&lf));
  3959. lf.lfWeight = FW_BOLD;
  3960. VERIFY(BoldFont.CreateFontIndirect(&lf));
  3961. CFont *pNormalFont = pDC->SelectObject(&BoldFont);
  3962. int nPrevBkMode = pDC->SetBkMode(TRANSPARENT);
  3963. // print App title on top right margin
  3964. strHeaderString.LoadString(AFX_IDS_APP_TITLE);
  3965. pDC->DrawText(strHeaderString, &rc, DT_RIGHT | DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER);
  3966. // print parent window title in the centre (Gert Rijs)
  3967. CWnd *pParentWnd = GetParent();
  3968. while (pParentWnd)
  3969. {
  3970. pParentWnd->GetWindowText(strHeaderString);
  3971. if (strHeaderString.GetLength()) // can happen if it is a CView, CChildFrm has the title
  3972. break;
  3973. pParentWnd = pParentWnd->GetParent();
  3974. }
  3975. pDC->DrawText(strHeaderString, &rc, DT_CENTER | DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER);
  3976. pDC->SetBkMode(nPrevBkMode);
  3977. pDC->SelectObject(pNormalFont);
  3978. BoldFont.DeleteObject();
  3979. pDC->SelectStockObject(BLACK_PEN);
  3980. pDC->MoveTo(rc.left, rc.bottom);
  3981. pDC->LineTo(rc.right, rc.bottom);
  3982. }
  3983. //print footer with a line and date, and page number
  3984. void CGridCtrl::PrintFooter(CDC *pDC, CPrintInfo *pInfo)
  3985. {
  3986. CRect rc(pInfo->m_rectDraw);
  3987. CFont BoldFont;
  3988. LOGFONT lf;
  3989. //draw line
  3990. pDC->MoveTo(rc.left, rc.top);
  3991. pDC->LineTo(rc.right, rc.top);
  3992. //create bold font for header and footer
  3993. m_PrinterFont.GetLogFont(&lf);
  3994. lf.lfWeight = FW_BOLD;
  3995. BoldFont.CreateFontIndirect(&lf);
  3996. CFont *pNormalFont = pDC->SelectObject(&BoldFont);
  3997. int nPrevBkMode = pDC->SetBkMode(TRANSPARENT);
  3998. // draw page number
  3999. CString sTemp ;
  4000. rc.OffsetRect(0, m_CharSize.cy/2);
  4001. sTemp.Format(_T("Page %d of %d"), pInfo->m_nCurPage, pInfo->GetMaxPage());
  4002. pDC->DrawText(sTemp,-1,rc, DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER);
  4003. CTime t = CTime::GetCurrentTime();
  4004. sTemp = t.Format(_T("%c"));
  4005. pDC->DrawText(sTemp,-1,rc, DT_RIGHT | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER);
  4006. pDC->SetBkMode(nPrevBkMode);
  4007. pDC->SelectObject(pNormalFont);
  4008. BoldFont.DeleteObject();
  4009. }
  4010. void CGridCtrl::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  4011. {
  4012. m_PrinterFont.DeleteObject();
  4013. }
  4014. #endif // !defined(_WIN32_WCE_NO_PRINTING) && !defined(GRIDCONTROL_NO_PRINTING)
  4015. #ifndef _WIN32_WCE
  4016. /////////////////////////////////////////////////////////////////////////////
  4017. // CGridCtrl persistance
  4018. BOOL CGridCtrl::Save(LPCTSTR filename)
  4019. {
  4020. CStdioFile File;
  4021. CFileException ex;
  4022. if (!File.Open(filename, CFile::modeWrite | CFile::modeCreate| CFile::typeText, &ex)) {
  4023. ex.ReportError();
  4024. return FALSE;
  4025. }
  4026. TRY {
  4027. int nNumColumns = GetColumnCount();
  4028. for (int i = 0; i < nNumColumns; i++) {
  4029. File.WriteString(GetItemText(0,i));
  4030. File.WriteString((i==(nNumColumns-1))? _T("\n"):_T(","));
  4031. }
  4032. for (i = 0; i < GetRowCount(); i++) {
  4033. for (int j = 0; j < nNumColumns; j++) {
  4034. File.WriteString(GetItemText(i,j));
  4035. File.WriteString((j==(nNumColumns-1))? _T("\n"):_T(","));
  4036. }
  4037. }
  4038. File.Close();
  4039. }
  4040. CATCH (CFileException, e) {
  4041. AfxMessageBox(_T("Unable to save grid list"));
  4042. e->Delete();
  4043. return FALSE;
  4044. }
  4045. END_CATCH
  4046. return TRUE;
  4047. }
  4048. BOOL CGridCtrl::Load(LPCTSTR filename)
  4049. {
  4050. TCHAR *token, *end;
  4051. TCHAR buffer[1024];
  4052. CStdioFile File;
  4053. CFileException ex;
  4054. if (!File.Open(filename, CFile::modeRead | CFile::typeText)) {
  4055. ex.ReportError();
  4056. return FALSE;
  4057. }
  4058. DeleteAllItems();
  4059. TRY {
  4060. // Read Header off file
  4061. File.ReadString(buffer, 1024);
  4062. // Get first token
  4063. for (token=buffer, end=buffer;
  4064. *end && (*end != _T(',')) && (*end != _T('\n')); end++);
  4065. if ((*end == _T('\0')) && (token == end)) token = NULL;
  4066. *end = _T('\0');
  4067. while (token)
  4068. {
  4069. InsertColumn(token);
  4070. // Get next token
  4071. for (token=++end; *end && (*end != _T(',')) && (*end != _T('\n'));
  4072. end++);
  4073. if ((*end == _T('\0')) && (token == end)) token = NULL;
  4074. *end = _T('\0');
  4075. }
  4076. // Read in rest of data
  4077. int nItem = 0;
  4078. while (File.ReadString(buffer, 1024)) {
  4079. // Get first token
  4080. for (token=buffer, end=buffer;
  4081. *end && (*end != _T(',')) && (*end != _T('\n')); end++);
  4082. if ((*end == _T('\0')) && (token == end)) token = NULL;
  4083. *end = _T('\0');
  4084. int nSubItem = 0;
  4085. while (token) {
  4086. if (!nSubItem)
  4087. InsertRow(token);
  4088. else
  4089. SetItemText(nItem, nSubItem, token);
  4090. // Get next token
  4091. for (token=++end; *end && (*end != _T(',')) && (*end != _T('\n'));
  4092. end++);
  4093. if ((*end == _T('\0')) && (token == end)) token = NULL;
  4094. *end = _T('\0');
  4095. nSubItem++;
  4096. }
  4097. nItem++;
  4098. }
  4099. AutoSizeColumns();
  4100. File.Close();
  4101. }
  4102. CATCH (CFileException, e) {
  4103. AfxMessageBox(_T("Unable to load grid data"));
  4104. e->Delete();
  4105. return FALSE;
  4106. }
  4107. END_CATCH
  4108. return TRUE;
  4109. }
  4110. #endif
  4111. /////////////////////////////////////////////////////////////////////////////
  4112. // CGridCtrl overrideables
  4113. #ifndef GRIDCONTROL_NO_DRAGDROP
  4114. // This is no longer needed since I've changed to OLE drag and drop - but it's
  4115. // still cool code. :)
  4116. CImageList* CGridCtrl::CreateDragImage(CPoint *pHotSpot)
  4117. {
  4118. CDC* pDC = GetDC();
  4119. if (!pDC) return NULL;
  4120. CRect rect;
  4121. CCellID cell = GetFocusCell();
  4122. if (!GetCellRect(cell.row, cell.col, rect)) return NULL;
  4123. // Translate coordinate system
  4124. rect.BottomRight() = CPoint(rect.Width(), rect.Height());
  4125. rect.TopLeft() = CPoint(0,0);
  4126. *pHotSpot = rect.BottomRight();
  4127. // Create a new imagelist (the caller of this function has responsibility
  4128. // for deleting this list)
  4129. CImageList* pList = new CImageList;
  4130. if (!pList || !pList->Create(rect.Width(), rect.Height(), ILC_MASK, 1,1))
  4131. {
  4132. if (pList) delete pList;
  4133. return NULL;
  4134. }
  4135. // Create mem DC and bitmap
  4136. CDC MemDC;
  4137. CBitmap bm;
  4138. MemDC.CreateCompatibleDC(pDC);
  4139. bm.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
  4140. CBitmap* pOldBitmap = MemDC.SelectObject(&bm);
  4141. MemDC.SetWindowOrg(0,0);
  4142. // Draw cell onto bitmap in memDC
  4143. DrawCell(&MemDC, cell.row, cell.col, rect, TRUE);
  4144. // Clean up
  4145. MemDC.SelectObject(pOldBitmap);
  4146. ReleaseDC(pDC);
  4147. // Add the bitmap we just drew to the image list.
  4148. pList->Add(&bm, GetTextBkColor());
  4149. bm.DeleteObject();
  4150. return pList;
  4151. }
  4152. #endif
  4153. void CGridCtrl::OnFixedRowClick(CCellID& cell)
  4154. {
  4155. if (!IsValid(cell))
  4156. return;
  4157. if (m_bListMode)
  4158. {
  4159. if (!m_bSortOnClick)
  4160. return;
  4161. CWaitCursor waiter;
  4162. if (cell.col == m_SortColumn)
  4163. m_bAscending = !m_bAscending;
  4164. else
  4165. {
  4166. m_bAscending = TRUE;
  4167. m_SortColumn = cell.col;
  4168. }
  4169. SortTextItems(m_SortColumn, m_bAscending);
  4170. Invalidate();
  4171. }
  4172. else if (cell.col < GetFixedColumnCount())
  4173. {
  4174. m_MouseMode = MOUSE_SELECT_ALL;
  4175. OnSelecting(cell);
  4176. }
  4177. else
  4178. {
  4179. m_MouseMode = MOUSE_SELECT_COL;
  4180. OnSelecting(cell);
  4181. }
  4182. }
  4183. void CGridCtrl::OnFixedColumnClick(CCellID& cell)
  4184. {
  4185. if (!IsValid(cell))
  4186. return;
  4187. // if (m_bListMode && (GetItemState(cell.row, m_nFixedCols) & GVNI_SELECTED))
  4188. // {
  4189. // OnEditCell(cell.row, cell.col, VK_LBUTTON);
  4190. // return;
  4191. // }
  4192. if (cell.row < GetFixedRowCount())
  4193. {
  4194. m_MouseMode = MOUSE_SELECT_ALL;
  4195. OnSelecting(cell);
  4196. }
  4197. else
  4198. {
  4199. m_MouseMode = MOUSE_SELECT_ROW;
  4200. OnSelecting(cell);
  4201. }
  4202. }
  4203. // Gets the extent of the text pointed to by str (no CDC needed)
  4204. // By default this uses the selected font (which is a bigger font)
  4205. CSize CGridCtrl::GetTextExtent(LPCTSTR str, BOOL bUseSelectedFont /* = TRUE */)
  4206. {
  4207. CDC* pDC = GetDC();
  4208. if (!pDC) return CSize(0,0);
  4209. CFont *pOldFont, font;
  4210. if (bUseSelectedFont)
  4211. {
  4212. LOGFONT lf;
  4213. memcpy(&lf, &m_Logfont, sizeof(LOGFONT));
  4214. lf.lfWeight = SELECTED_CELL_FONT_WEIGHT;
  4215. font.CreateFontIndirect(&lf);
  4216. pOldFont = pDC->SelectObject(&font);
  4217. }
  4218. else
  4219. pOldFont = pDC->SelectObject(&m_Font);
  4220. CSize size = pDC->GetTextExtent(str);
  4221. pDC->SelectObject(pOldFont);
  4222. ReleaseDC(pDC);
  4223. return size + CSize(2*m_nMargin, 2*m_nMargin);
  4224. }
  4225. CSize CGridCtrl::GetCellExtent(int nRow, int nCol, CDC* pDC)
  4226. {
  4227. LOGFONT *pLF = GetItemFont(nRow, nCol);
  4228. // use selected font since it's thicker
  4229. LOGFONT lf;
  4230. memcpy(&lf, pLF, sizeof(LOGFONT));
  4231. if (nRow < m_nFixedRows || nCol < m_nFixedCols)
  4232. lf.lfWeight = SELECTED_CELL_FONT_WEIGHT;
  4233. CFont font;
  4234. font.CreateFontIndirect(&lf);
  4235. CFont* pOldFont = pDC->SelectObject(&font);
  4236. CSize size = pDC->GetTextExtent(GetItemText(nRow, nCol));
  4237. pDC->SelectObject(pOldFont);
  4238. size += CSize(4*m_nMargin, 2*m_nMargin);
  4239. CSize ImageSize(0,0);
  4240. if (m_pImageList) {
  4241. int nImage = GetItemImage(nRow, nCol);
  4242. if (nImage >= 0) {
  4243. IMAGEINFO Info;
  4244. if (m_pImageList->GetImageInfo(nImage, &Info))
  4245. ImageSize = CSize(Info.rcImage.right-Info.rcImage.left+1,
  4246. Info.rcImage.bottom-Info.rcImage.top+1);
  4247. }
  4248. }
  4249. return CSize(size.cx + ImageSize.cx, max(size.cy, ImageSize.cy));
  4250. }
  4251. BOOL CGridCtrl::DrawFixedCell(CDC* pDC, int nRow, int nCol, CRect rect, BOOL bEraseBk)
  4252. {
  4253. if (!m_bAllowDraw)
  4254. return FALSE;
  4255. GV_ITEM Item;
  4256. Item.mask = GVIF_TEXT | GVIF_FORMAT | GVIF_IMAGE | GVIF_BKCLR | GVIF_FGCLR;
  4257. Item.row = nRow;
  4258. Item.col = nCol;
  4259. if (!GetItem(&Item))
  4260. return FALSE;
  4261. // Force redraw of background if custom colour
  4262. //if (Item.crBkClr != CLR_DEFAULT)
  4263. bEraseBk = TRUE;
  4264. if (bEraseBk
  4265. #if !defined(_WIN32_WCE_NO_PRINTING) && !defined(GRIDCONTROL_NO_PRINTING)
  4266. && !pDC->IsPrinting()
  4267. #endif
  4268. )
  4269. {
  4270. CBrush brush((Item.crBkClr == CLR_DEFAULT)? GetFixedBkColor() : Item.crBkClr);
  4271. pDC->FillRect(rect, &brush);
  4272. }
  4273. pDC->SetTextColor((Item.crFgClr == CLR_DEFAULT)? GetFixedTextColor() : Item.crFgClr);
  4274. int nSavedDC = pDC->SaveDC();
  4275. // Create the appropriate font and select into DC
  4276. LOGFONT *pLF = GetItemFont(nRow, nCol);
  4277. CCellID FocusCell = GetFocusCell();
  4278. // if (IsCellFixed(nRow, nCol) && (FocusCell.row == nRow || FocusCell.col == nCol))
  4279. //{
  4280. if (FocusCell.row == nRow || FocusCell.col == nCol)
  4281. {
  4282. static LOGFONT lf;
  4283. memcpy(&lf, pLF, sizeof(LOGFONT));
  4284. lf.lfWeight = SELECTED_CELL_FONT_WEIGHT;
  4285. pLF = &lf;
  4286. }
  4287. CFont Font;
  4288. Font.CreateFontIndirect(pLF);
  4289. pDC->SelectObject(&Font);
  4290. if (IsValid(FocusCell) && (FocusCell.row == nRow || FocusCell.col == nCol))
  4291. {
  4292. rect.right++; rect.bottom++;
  4293. pDC->DrawEdge(rect, BDR_SUNKENINNER /*EDGE_RAISED*/, BF_RECT);
  4294. rect.DeflateRect(1,1);
  4295. }
  4296. else
  4297. {
  4298. CPen lightpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DHIGHLIGHT)),
  4299. darkpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DDKSHADOW)),
  4300. *pOldPen = pDC->GetCurrentPen();
  4301. pDC->SelectObject(&lightpen);
  4302. pDC->MoveTo(rect.right, rect.top);
  4303. pDC->LineTo(rect.left, rect.top);
  4304. pDC->LineTo(rect.left, rect.bottom);
  4305. pDC->SelectObject(&darkpen);
  4306. pDC->MoveTo(rect.right, rect.top);
  4307. pDC->LineTo(rect.right, rect.bottom);
  4308. pDC->LineTo(rect.left, rect.bottom);
  4309. pDC->SelectObject(pOldPen);
  4310. rect.DeflateRect(1,1);
  4311. }
  4312. pDC->SetBkMode(TRANSPARENT);
  4313. rect.DeflateRect(m_nMargin, 0);
  4314. if (m_pImageList && Item.iImage >= 0)
  4315. {
  4316. IMAGEINFO Info;
  4317. if (m_pImageList->GetImageInfo(Item.iImage, &Info))
  4318. {
  4319. //CRgn rgn;
  4320. //rgn.CreateRectRgnIndirect(rect);
  4321. //pDC->SelectClipRgn(&rgn);
  4322. //rgn.DeleteObject();
  4323. int nImageWidth = Info.rcImage.right-Info.rcImage.left+1;
  4324. m_pImageList->Draw(pDC, Item.iImage, rect.TopLeft(), ILD_NORMAL);
  4325. rect.left += nImageWidth+m_nMargin;
  4326. }
  4327. }
  4328. DrawText(pDC->m_hDC, Item.szText, -1, rect, Item.nFormat);
  4329. pDC->RestoreDC(nSavedDC);
  4330. Font.DeleteObject();
  4331. return TRUE;
  4332. }
  4333. BOOL CGridCtrl::DrawCell(CDC* pDC, int nRow, int nCol, CRect rect, BOOL bEraseBk)
  4334. {
  4335. if (!m_bAllowDraw)
  4336. return FALSE;
  4337. GV_ITEM Item;
  4338. Item.mask = GVIF_TEXT | GVIF_FORMAT | GVIF_STATE | GVIF_IMAGE | GVIF_BKCLR | GVIF_FGCLR;
  4339. Item.row = nRow;
  4340. Item.col = nCol;
  4341. if (!GetItem(&Item))
  4342. return FALSE;
  4343. COLORREF TextBkClr = (Item.crBkClr == CLR_DEFAULT)? GetTextBkColor() : Item.crBkClr;
  4344. COLORREF TextClr = (Item.crFgClr == CLR_DEFAULT)? GetTextColor() : Item.crFgClr;
  4345. // Force redraw of background if custom colour
  4346. //if (Item.crBkClr != CLR_DEFAULT)
  4347. bEraseBk = TRUE;
  4348. int nSavedDC = pDC->SaveDC();
  4349. pDC->SetBkMode(TRANSPARENT);
  4350. if (Item.state & GVIS_FOCUSED
  4351. #if !defined(_WIN32_WCE_NO_PRINTING) && !defined(GRIDCONTROL_NO_PRINTING)
  4352. && !pDC->IsPrinting()
  4353. #endif
  4354. )
  4355. {
  4356. rect.right++; rect.bottom++; // FillRect doesn't draw RHS or bottom
  4357. if (bEraseBk)
  4358. {
  4359. CBrush brush(TextBkClr);
  4360. pDC->FillRect(rect, &brush);
  4361. }
  4362. rect.right--; rect.bottom--;
  4363. pDC->SelectStockObject(BLACK_PEN);
  4364. pDC->SelectStockObject(NULL_BRUSH);
  4365. pDC->Rectangle(rect);
  4366. pDC->SetTextColor(TextClr);
  4367. rect.DeflateRect(1,1);
  4368. }
  4369. else if (Item.state & GVIS_SELECTED
  4370. #if !defined(_WIN32_WCE_NO_PRINTING) && !defined(GRIDCONTROL_NO_PRINTING)
  4371. && !pDC->IsPrinting()
  4372. #endif
  4373. )
  4374. {
  4375. rect.right++; rect.bottom++; // FillRect doesn't draw RHS or bottom
  4376. pDC->FillSolidRect(rect, ::GetSysColor(COLOR_HIGHLIGHT));
  4377. rect.right--; rect.bottom--;
  4378. pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
  4379. } else {
  4380. rect.right++; rect.bottom++; // FillRect doesn't draw RHS or bottom
  4381. if (bEraseBk)
  4382. {
  4383. CBrush brush(TextBkClr);
  4384. pDC->FillRect(rect, &brush);
  4385. }
  4386. rect.right--; rect.bottom--;
  4387. pDC->SetTextColor(TextClr);
  4388. }
  4389. if (Item.state & GVIS_DROPHILITED
  4390. #if !defined(_WIN32_WCE_NO_PRINTING) && !defined(GRIDCONTROL_NO_PRINTING)
  4391. && !pDC->IsPrinting()
  4392. #endif
  4393. )
  4394. {
  4395. pDC->SelectStockObject(BLACK_PEN);
  4396. pDC->SelectStockObject(NULL_BRUSH);
  4397. pDC->Rectangle(rect);
  4398. }
  4399. // Create the appropriate font and select into DC
  4400. LOGFONT *pLF = GetItemFont(nRow, nCol);
  4401. //CCellID FocusCell = GetFocusCell();
  4402. //if (IsCellFixed(nRow, nCol) && (FocusCell.row == nRow || FocusCell.col == nCol))
  4403. //{
  4404. // static LOGFONT lf;
  4405. // memcpy(&lf, pLF, sizeof(LOGFONT));
  4406. // lf.lfWeight = SELECTED_CELL_FONT_WEIGHT;
  4407. // pLF = &lf;
  4408. //}
  4409. CFont Font;
  4410. Font.CreateFontIndirect(pLF);
  4411. pDC->SelectObject(&Font);
  4412. rect.DeflateRect(m_nMargin, 0);
  4413. if (m_pImageList && Item.iImage >= 0)
  4414. {
  4415. IMAGEINFO Info;
  4416. if (m_pImageList->GetImageInfo(Item.iImage, &Info))
  4417. {
  4418. //CRgn rgn;
  4419. //rgn.CreateRectRgnIndirect(rect);
  4420. //pDC->SelectClipRgn(&rgn);
  4421. //rgn.DeleteObject();
  4422. int nImageWidth = Info.rcImage.right-Info.rcImage.left+1;
  4423. m_pImageList->Draw(pDC, Item.iImage, rect.TopLeft(), ILD_NORMAL);
  4424. rect.left += nImageWidth+m_nMargin;
  4425. }
  4426. }
  4427. DrawText(pDC->m_hDC, Item.szText, -1, rect, Item.nFormat);
  4428. pDC->RestoreDC(nSavedDC);
  4429. Font.DeleteObject();
  4430. return TRUE;
  4431. }
  4432. void CGridCtrl::OnEditCell(int nRow, int nCol, UINT nChar)
  4433. {
  4434. EnsureVisible(nRow, nCol);
  4435. CCellID cell(nRow, nCol);
  4436. if (!IsValid(cell) || !IsCellEditable(nRow, nCol) || !IsCellVisible(nRow, nCol))
  4437. return;
  4438. CRect rect;
  4439. if (!GetCellRect(cell, rect)) return;
  4440. SendMessageToParent(nRow, nCol, GVN_BEGINLABELEDIT);
  4441. GV_ITEM Item;
  4442. Item.mask = GVIF_TEXT | GVIF_FORMAT;
  4443. Item.row = nRow;
  4444. Item.col = nCol;
  4445. if (!GetItem(&Item)) return;
  4446. DWORD dwStyle = ES_LEFT;
  4447. if (Item.nFormat & DT_RIGHT) dwStyle = ES_RIGHT;
  4448. else if (Item.nFormat & DT_CENTER) dwStyle = ES_CENTER;
  4449. CreateInPlaceEditControl(rect, dwStyle, IDC_INPLACE_CONTROL,
  4450. nRow, nCol, Item.szText, nChar);
  4451. }
  4452. void CGridCtrl::CreateInPlaceEditControl(CRect& rect, DWORD dwStyle, UINT nID,
  4453. int nRow, int nCol,
  4454. LPCTSTR szText, int nChar)
  4455. {
  4456. // InPlaceEdit auto-deletes itself
  4457. new CInPlaceEdit(this, rect, dwStyle, nID, nRow, nCol, szText, nChar);
  4458. }
  4459. void CGridCtrl::OnEndEditCell(int nRow, int nCol, CString str)
  4460. {
  4461. CString strCurrent = GetItemText(nRow,nCol);
  4462. if (strCurrent != str)
  4463. {
  4464. SetModified(TRUE, nRow, nCol);
  4465. SetItemText(nRow, nCol, str);
  4466. }
  4467. }
  4468. CString CGridCtrl::GetItemText(int nRow, int nCol)
  4469. {
  4470. if (nRow < 0 || nRow >= m_nRows || nCol < 0 || nCol >= m_nCols) return "";
  4471. CGridCell* pCell = GetCell(nRow, nCol);
  4472. ASSERT(pCell);
  4473. if (!pCell) return "";
  4474. return pCell->szText;
  4475. }