1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869 |
- #include "cmGlobal.h"
- #include "cmFloatTypes.h"
- #include "cmRpt.h"
- #include "cmErr.h"
- #include "cmCtx.h"
- #include "cmMem.h"
- #include "cmMallocDebug.h"
- #include "cmLinkedHeap.h"
- #include "cmGr.h"
- #include "cmGrDevCtx.h"
-
- // cmGr_t state flags
- enum
- {
- kDirtyGrFl = 0x01 // the cmGr object is dirty
- };
-
-
- typedef struct cmGrObj_str
- {
- unsigned id; //
- cmGrObjFunc_t f; //
- cmGrVExt_t wext; // world coord's contained within this object (children of this object are contained by these extents)
- unsigned wlimitFlags; // kLeftGrFl | kRightGrFl | kTopGrFl | kBottomGrFl
- cmGrVExt_t wlimitExt; // limit extents for wext (ext's with set flags always define the associated wext value)
-
- struct cmGrObj_str* parent;
- struct cmGrObj_str* children;
- struct cmGrObj_str* rsib;
- struct cmGrObj_str* lsib;
-
- } cmGrObj_t;
-
- typedef struct cmGrSync_str
- {
- cmGrH_t grH;
- unsigned flags; // kGrXXXSyncGrFl
- struct cmGrSync_str* link;
- } cmGrSync_t;
-
- typedef struct cmGrColorMap_str
- {
- const cmChar_t* label;
- unsigned id;
- cmGrColor_t* map;
- unsigned cnt;
- struct cmGrColorMap_str* link;
- } cmGrColorMap_t;
-
- typedef struct cmGrKeyMap_str
- {
- unsigned idx; // index of this keymap entry (used for debugging)
- char ascii; // printible ascii character associated with this keycode or 0 if not printable
- cmGrKeyCodeId_t keycode; // cmGr keycode
- } cmGrKeyMap_t;
-
- typedef struct
- {
- cmCtx_t ctx;
- cmErr_t err;
- unsigned id; // user definable id
- cmLHeapH_t lhH;
- unsigned cfgFlags;
- unsigned stateFlags;
-
- cmGrObj_t* msDnObj; // obj under last ms dn
- cmGrPPt_t msDnPPt; // last ms dn loc'n
- cmGrVPt_t msDnVPt; // last ms dn in same coords as msDnObj->vext (inside msDnObj->parent->wxt)
- cmGrVPt_t msVPt; // cur ms pt in same coords as msDnObj->vext (inside msDnObj->parent->wxt)
- cmGrVSz_t msDnVOffs;
-
- bool selValidFl;
- cmGrVPt_t sel0Pt;
- cmGrVPt_t sel1Pt;
-
- cmGrVPt_t localPt;
- cmGrVPt_t globalPt;
-
- cmGrVExt_t vext; // view virtual extents
- cmGrPExt_t pext; // view physical extents
- cmGrObj_t* objs; // object tree
- cmGrObj_t* rootObj; // current root object
- unsigned char* img; // temporary image inversion buffer
- cmGrCbFunc_t cbFunc; //
- void* cbArg; //
- cmGrSync_t* syncs; //
- cmGrColorMap_t* maps; // color maps
- } cmGr_t;
-
- cmGrH_t cmGrNullHandle = cmSTATIC_NULL_HANDLE;
- cmGrObjH_t cmGrObjNullHandle = cmSTATIC_NULL_HANDLE;
-
-
- cmGrKeyMap_t _cmGrKeyMap[] =
- {
- { 0, 0, 0 },
- { 1, 0, 0 },
- { 2, 0, 0 },
- { 3, 0, 0 },
- { 4, 0, 0 },
- { 5, 0, kHomeGrId},
- { 6, 0, kPageUpGrId},
- { 7, 0, kEndGrId},
- { 8, 8, kBackSpaceGrId },
- { 9, 9, kTabGrId },
-
- { 10, 0, kPageDownGrId},
- { 11, 0, kLeftGrId},
- { 12, 0, kUpGrId},
- { 13, 13, kEnterGrId },
- { 14, 0, kRightGrId},
- { 15, 0, kDownGrId},
- { 16, 0, kInsertGrId},
- { 17, 0, kPrintGrId},
- { 18, 0, kScrollLockGrId},
- { 19, 0, kPauseGrId},
- { 20, 0, kMenuGrId},
-
-
- { 21, 0, kLShiftGrId},
- { 22, 0, kRShiftGrId},
- { 23, 0, kLCtrlGrId},
- { 24, 0, kRCtrlGrId},
- { 25, 0, kLAltGrId},
- { 26, 0, kRAltGrId},
- { 27, 27, kEscapeGrId },
- { 28, 0, kLSuperGrId},
- { 29, 0, kRSuperGrId},
- { 30, 0, kNumLockGrId},
- { 31, 0, kCapsLockGrId},
-
- { 32, 32, kSpaceGrId },
- { 33, 33, kExclMarkGrId },
- { 34, 34, kDQuoteGrId },
- { 35, 35, kPoundGrId },
- { 36, 36, kDollarGrId },
- { 37, 37, kPercentGrId },
- { 38, 38, kAmpersandGrId },
- { 39, 39, kApostropheGrId },
-
- { 40, 40, kLParenGrId },
- { 41, 41, kRParenGrId },
- { 42, 42, kAsteriskGrId },
- { 43, 43, kPlusGrId },
- { 44, 44, kCommaGrId },
- { 45, 45, kHyphenGrId },
- { 46, 46, kPeriodGrId },
- { 47, 47, kForwardSlashGrId },
- { 48, 48, k0GrId },
- { 49, 49, k1GrId },
-
- { 50, 50, k2GrId },
- { 51, 51, k3GrId },
- { 52, 52, k4GrId },
- { 53, 53, k5GrId },
- { 54, 54, k6GrId },
- { 55, 55, k7GrId },
- { 56, 56, k8GrId },
- { 57, 57, k9GrId },
- { 58, 58, kColonGrId },
- { 59, 59, kSemiColonGrId },
-
- { 60, 60, kLesserGrId },
- { 61, 61, kEqualGrId },
- { 62, 62, kGreaterGrId },
- { 63, 63, kQMarkGrId },
- { 64, 64, kAtGrId },
- { 65, 65, kA_GrId },
- { 66, 66, kB_GrId },
- { 67, 67, kC_GrId },
- { 68, 68, kD_GrId },
- { 69, 69, kE_GrId },
-
- { 70, 70, kF_GrId },
- { 71, 71, kG_GrId },
- { 72, 72, kH_GrId },
- { 73, 73, kI_GrId },
- { 74, 74, kJ_GrId },
- { 75, 75, kK_GrId },
- { 76, 76, kL_GrId },
- { 77, 77, kM_GrId },
- { 78, 78, kN_GrId },
- { 79, 79, kO_GrId },
-
- { 80, 80, kP_GrId },
- { 81, 81, kQ_GrId },
- { 82, 82, kR_GrId },
- { 83, 83, kS_GrId },
- { 84, 84, kT_GrId },
- { 85, 85, kU_GrId },
- { 86, 86, kV_GrId },
- { 87, 87, kW_GrId },
- { 88, 88, kX_GrId },
- { 89, 89, kY_GrId },
-
- { 90, 90, kZ_GrId },
- { 91, 91, kLBracketGrId },
- { 92, 92, kBackSlashGrId },
- { 93, 93, kRBracketGrId },
- { 94, 94, kCaretGrId },
- { 95, 95, kUnderScoreGrId },
- { 96, 96, kAccentGrId },
- { 97, 97, ka_GrId },
- { 98, 98, kb_GrId },
- { 99, 99, kc_GrId },
-
- { 100, 100, kd_GrId },
- { 101, 101, ke_GrId },
- { 102, 102, kf_GrId },
- { 103, 103, kg_GrId },
- { 104, 104, kh_GrId },
- { 105, 105, ki_GrId },
- { 106, 106, kj_GrId },
- { 107, 107, kk_GrId },
- { 108, 108, kl_GrId },
- { 109, 109, km_GrId },
-
- { 110, 110, kn_GrId },
- { 111, 111, ko_GrId },
- { 112, 112, kp_GrId },
- { 113, 113, kq_GrId },
- { 114, 114, kr_GrId },
- { 115, 115, ks_GrId },
- { 116, 116, kt_GrId },
- { 117, 117, ku_GrId },
- { 118, 118, kv_GrId },
- { 119, 119, kw_GrId },
-
- { 120, 120, kx_GrId },
- { 121, 121, ky_GrId },
- { 122, 122, kz_GrId },
- { 123, 123, kLBraceGrId },
- { 124, 124, kPipeGrId },
- { 125, 125, kRBraceGrId },
- { 126, 126, kTildeGrId },
- { 127, 127, kDeleteGrId },
-
- { 128, 42, kNP_MultGrId },
- { 129, 43, kNP_PlusGrId },
- { 130, 45, kNP_MinusGrId },
- { 131, 46, kNP_DecPtGrId},
- { 132, 47, kNP_DivGrId},
- { 133, 48, kNP_0GrId},
- { 134, 49, kNP_1GrId},
- { 135, 50, kNP_2GrId},
- { 136, 51, kNP_3GrId},
- { 137, 52, kNP_4GrId},
- { 138, 53, kNP_5GrId},
- { 139, 54, kNP_6GrId},
- { 140, 55, kNP_7GrId},
- { 141, 56, kNP_8GrId},
- { 142, 57, kNP_9GrId},
- { 143, 61, kNP_EqualGrId},
- { 144, 13, kNP_EnterGrId},
- { 145, 0, kFunc_1GrId},
- { 146, 0, kFunc_2GrId},
- { 147, 0, kFunc_3GrId},
- { 148, 0, kFunc_4GrId},
- { 149, 0, kFunc_5GrId},
- { 150, 0, kFunc_6GrId},
- { 151, 0, kFunc_7GrId},
- { 152, 0, kFunc_8GrId},
- { 153, 0, kFunc_9GrId},
- { 154, 0, kFunc_10GrId},
- { 155, 0, kFunc_11GrId},
- { 156, 0, kFunc_12GrId},
- { 157, 0, kBrightUpGrId},
- { 158, 0, kBrightDnGrId},
- { 159, 0, kAudio_PrevGrId},
- { 160, 0, kAudio_PlayGrId},
- { 161, 0, kAudio_NextGrId},
- { 162, 0, kAudio_MuteGrId},
- { 163, 0, kAudio_DnGrId },
- { 164, 0, kAudio_UpGrId },
- { 165, 0, kEjectGrId },
- { cmInvalidIdx, 0, cmInvalidId}
- };
-
- void _cmGrKeyMapValidate()
- {
- unsigned i;
- for(i=0; _cmGrKeyMap[i].idx != cmInvalidIdx; ++i)
- {
- assert( _cmGrKeyMap[i].idx == i );
- }
- }
-
- cmGrKeyMap_t* _cmGrFindKeyMap( unsigned keycode )
- {
- // printable ascii codes match their indexes
- if( 32 <= keycode && keycode <= 126 )
- return _cmGrKeyMap + keycode;
-
- unsigned i;
- for(i=0; i<32; ++i)
- if( _cmGrKeyMap[i].keycode == keycode )
- return _cmGrKeyMap + i;
-
- for(i=127; _cmGrKeyMap[i].idx != cmInvalidIdx; ++i)
- if( _cmGrKeyMap[i].keycode == keycode )
- return _cmGrKeyMap + i;
-
- assert(0);
- return NULL;
- }
-
-
-
- bool _cmGrSetViewExtents( cmGr_t* p, cmGrV_t minx, cmGrV_t miny, cmGrV_t maxx, cmGrV_t maxy );
- bool _cmGrSetViewExtentsE( cmGr_t* p, const cmGrVExt_t* e )
- { return _cmGrSetViewExtents(p, cmGrVExtMinX(e), cmGrVExtMinY(e), cmGrVExtMaxX(e), cmGrVExtMaxY(e) ); }
-
- //====================================================================================================
- // Expand cmGrVExt_t e0 to hold e1.
- // Return true if e0 is actually changed.
- bool cmGrVExtExpandToContain(cmGrVExt_t* e0, const cmGrVExt_t* e1)
- {
- bool fl = false;
-
- if( cmGrVExtIsNullOrEmpty(e0) )
- {
- *e0 = *e1;
- return true;
- }
-
- assert( cmGrVExtIsNorm(e0) && cmGrVExtIsNorm(e1) );
-
- // min-x
- if( cmGrVExtMinX(e0) > cmGrVExtMinX(e1) )
- {
- cmGrVExtSetMinX(e0, cmGrVExtMinX(e1));
- fl = true;
- }
-
- // min-y
- if( cmGrVExtMinY(e0) > cmGrVExtMinY(e1) )
- {
- cmGrVExtSetMinY(e0, cmGrVExtMinY(e1));
- fl = true;
- }
-
- // max-y
- if( cmGrVExtMaxY(e0) < cmGrVExtMaxY(e1) )
- {
- cmGrVExtSetMaxY(e0, cmGrVExtMaxY(e1));
- fl = true;
- }
-
- // max-x
- if( cmGrVExtMaxX(e0) < cmGrVExtMaxX(e1) )
- {
- cmGrVExtSetMaxX(e0, cmGrVExtMaxX(e1));
- fl = true;
- }
-
-
- return fl;
- }
-
- bool cmGrVExtContain( const cmGrVExt_t* e0, cmGrVExt_t* e1 )
- {
- bool fl = false;
-
- assert( cmGrVExtIsNorm(e0) && cmGrVExtIsNorm(e1) );
- // e1 must be able to fit inside e0
- assert( e1->sz.w <= e0->sz.w && e1->sz.h <= e0->sz.h );
-
- // if left edge of e1 is to left of e0
- if( cmGrVExtMinX(e1) < cmGrVExtMinX(e0) )
- {
- cmGrVExtSetMinX(e1,cmGrVExtMinX(e0));
- fl = true;
- }
-
- // if right edge of e1 is to right of e0
- if( cmGrVExtMaxX(e1) > cmGrVExtMaxX(e0) )
- {
- cmGrVExtSetMaxX(e1,cmGrVExtMaxX(e0));
- fl = true;
- }
-
- // if the bottom edge of e1 is below the bottom edge of e0
- if( cmGrVExtMinY(e1) < cmGrVExtMinY(e0) )
- {
- cmGrVExtSetMinY(e1,cmGrVExtMinY(e0));
- fl = true;
- }
-
- // if top edge of e1 is above the top edge of e0
- if( cmGrVExtMaxY(e1) > cmGrVExtMaxY(e0) )
- {
- cmGrVExtSetMaxY(e1,cmGrVExtMaxY(e0));
- fl = true;
- }
-
- return fl;
- }
-
- void cmGrPExtIntersect( cmGrPExt_t* r, const cmGrPExt_t* e0, const cmGrPExt_t* e1 )
- {
- if( cmGrPExtR(e0) < cmGrPExtL(e1) || cmGrPExtL(e0) > cmGrPExtR(e1)
- || cmGrPExtB(e0) < cmGrPExtT(e1) || cmGrPExtT(e0) > cmGrPExtB(e1) )
- {
- cmGrPExtSetEmpty(r);
- return;
- }
-
- cmGrPExtSetD(r,
- cmMax( cmGrPExtL(e0), cmGrPExtL(e1) ),
- cmMax( cmGrPExtT(e0), cmGrPExtT(e1) ),
- cmMin( cmGrPExtR(e0), cmGrPExtR(e1) ),
- cmMin( cmGrPExtB(e0), cmGrPExtB(e1) ) );
-
- }
-
- void cmGrVExtIntersect( cmGrVExt_t* r, const cmGrVExt_t* e0, const cmGrVExt_t* e1 )
- {
- if( cmGrVExtMaxX(e0) < cmGrVExtMinX(e1) || cmGrVExtMinX(e0) > cmGrVExtMaxX(e1)
- || cmGrVExtMaxY(e0) < cmGrVExtMinY(e1) || cmGrVExtMinY(e0) > cmGrVExtMaxY(e1) )
- {
- cmGrVExtSetEmpty(r);
- return;
- }
-
- cmGrVExtSetD(r,
- cmMax( cmGrVExtMinX(e0), cmGrVExtMinX(e1) ),
- cmMax( cmGrVExtMinY(e0), cmGrVExtMinY(e1) ),
- cmMin( cmGrVExtMaxX(e0), cmGrVExtMaxX(e1) ),
- cmMin( cmGrVExtMaxY(e0), cmGrVExtMaxY(e1) ) );
- }
-
-
- //====================================================================================================
- //====================================================================================================
- cmGr_t* _cmGrHandleToPtr( cmGrH_t h )
- {
- cmGr_t* p = (cmGr_t*)h.h;
- assert( p != NULL );
- return p;
- }
-
-
- cmGrObj_t* _cmGrObjHandleToPtr( cmGrObjH_t h )
- {
- cmGrObj_t* p = (cmGrObj_t*)h.h;
- assert( p != NULL );
- return p;
- }
-
- //====================================================================================================
- unsigned cmGrColorMapCount( cmGrH_t grH )
- {
- cmGr_t* p = _cmGrHandleToPtr(grH);
- cmGrColorMap_t* cmp = p->maps;
- unsigned n = 0;
- for(; cmp!=NULL; cmp=cmp->link)
- ++n;
- return n;
- }
-
- cmGrColorMap_t* _cmGrColorMapFromIndex( cmGr_t* p, unsigned idx )
- {
- unsigned i = 0;
- cmGrColorMap_t* cmp = p->maps;
- for(; cmp!=NULL; ++i,cmp=cmp->link)
- if( i == idx )
- break;
- return cmp;
- }
-
- cmGrColorMap_t* _cmGrColorMapFromId( cmGr_t* p, unsigned id )
- {
- cmGrColorMap_t* cmp = p->maps;
- for(; cmp!=NULL; cmp=cmp->link)
- if( cmp->id == id )
- break;
- return cmp;
- }
-
- unsigned cmGrColorMapId( cmGrH_t grH, unsigned mapIdx )
- {
- cmGr_t* p = _cmGrHandleToPtr(grH);
- cmGrColorMap_t* cmp;
- if((cmp = _cmGrColorMapFromIndex(p,mapIdx)) == NULL )
- return cmInvalidId;
- return cmp->id;
- }
-
- const cmChar_t* cmGrColorMapLabel( cmGrH_t grH, unsigned id )
- {
- cmGr_t* p = _cmGrHandleToPtr(grH);
- cmGrColorMap_t* cmp;
- if((cmp = _cmGrColorMapFromId(p,id)) == NULL )
- return NULL;
- return cmp->label;
- }
-
- unsigned _cmGrColorMapRegister( cmGr_t* p, cmChar_t* label, const cmGrColor_t* array, unsigned cnt )
- {
- // locate an available id
- unsigned id = 0;
- while( _cmGrColorMapFromId(p,id)!=NULL )
- ++id;
-
- cmGrColorMap_t* cmp = cmLhAllocZ(p->lhH,cmGrColorMap_t,1);
- cmp->label = cmLhAllocStr(p->lhH,label);
- cmp->id = id;
- cmp->map = cmLhAllocZ(p->lhH,cmGrColor_t,cnt);
- cmp->cnt = cnt;
- cmp->link = p->maps;
- p->maps = cmp;
-
- memcpy(cmp->map,array,sizeof(cmGrColor_t)*cnt);
-
- return id;
- }
-
- unsigned cmGrColorMapRegister( cmGrH_t grH, cmChar_t* label, const cmGrColor_t* array, unsigned cnt )
- { return _cmGrColorMapRegister( _cmGrHandleToPtr(grH),label, array, cnt ); }
-
- cmGrColor_t* cmGrColorMap( cmGrH_t grH, unsigned mapId )
- {
- cmGr_t* p = _cmGrHandleToPtr(grH);
- cmGrColorMap_t* cmp;
- if((cmp = _cmGrColorMapFromId(p,mapId)) == NULL )
- return NULL;
- return cmp->map;
- }
-
- unsigned cmGrColorMapEleCount( cmGrH_t grH, unsigned mapId )
- {
- cmGr_t* p = _cmGrHandleToPtr(grH);
- cmGrColorMap_t* cmp;
- if((cmp = _cmGrColorMapFromId(p,mapId)) == NULL )
- return cmInvalidId;
- return cmp->cnt;
- }
-
- void _cmGrRgbInitDefaultColorMap( cmGr_t* p )
- {
- unsigned map[] =
- {
- 0x000000, // black
- 0x00008b, // dark blue
- 0x0000ff, // blue
- 0x008080, // teal
- 0x00ffff, // cyan
- 0x00ff7f, // spring green
- 0x00ff00, // green
- 0x7cfc00, // lawn green
- 0xffff00, // yellow
- 0xff7f7f, // pink
- 0xff00ff, // magenta
- 0xff007f, //
- 0xff0000, // red
- 0x7f0000, //
- 0xffffff // white
- };
-
- unsigned n = sizeof(map)/sizeof(unsigned);
-
- _cmGrColorMapRegister(p,"default",map,n);
- }
-
-
-
- //====================================================================================================
- // Object Callback Functions
- //====================================================================================================
-
- void _cmGrObjSetupFuncArgs( cmGrObjFuncArgs_t* a, cmGr_t* p, cmGrObj_t* op )
- {
- cmGrH_t h;
- cmGrObjH_t oh;
-
- h.h = p;
- oh.h = op;
-
- a->ctx = &p->ctx;
- a->grH = h;
- a->objH = oh;
- a->msDnPPt = p->msDnPPt;
- a->msDnVPt = p->msDnVPt;
- a->msDnVOffs = p->msDnVOffs;
- a->msVPt = p->msVPt;
-
- oh.h = p->msDnObj;
- a->msDnObjH = oh;
- }
-
- cmGrRC_t _cmGrObjCbCreate( cmGr_t* p, cmGrObj_t* op )
- {
- cmGrRC_t rc = kOkGrRC;
-
- if( op->f.createCbFunc != NULL )
- {
- cmGrObjFuncArgs_t a;
- _cmGrObjSetupFuncArgs(&a,p,op);
-
- a.cbArg = op->f.createCbArg;
- if((rc = op->f.createCbFunc(&a)) != kOkGrRC )
- rc = cmErrMsg(&p->err,kAppErrGrRC,"An application object (id=%i) failed on 'create'",op->id);
- }
-
- return rc;
- }
-
- void _cmGrObjCbDestroy( cmGr_t* p, cmGrObj_t* op )
- {
- if( op->f.destroyCbFunc != NULL )
- {
- cmGrObjFuncArgs_t a;
-
- _cmGrObjSetupFuncArgs(&a,p,op);
- a.cbArg = op->f.destroyCbArg;
-
- op->f.destroyCbFunc(&a);
- }
- }
-
- void _cmGrObjCbRender( cmGr_t* p, cmGrDcH_t dcH, const cmGrObj_t* op )
- {
- if( op->f.renderCbFunc != NULL )
- {
- cmGrObjFuncArgs_t a;
- _cmGrObjSetupFuncArgs(&a,p,(cmGrObj_t*)op);
- a.cbArg = op->f.renderCbArg;
-
- op->f.renderCbFunc(&a, dcH );
- }
- }
-
- int _cmGrObjCbDistance( cmGr_t* p, const cmGrObj_t* op, int px, int py )
- {
- int d = INT_MAX;
-
- if( op->f.distanceCbFunc != NULL )
- {
- cmGrObjFuncArgs_t a;
- _cmGrObjSetupFuncArgs(&a,p,(cmGrObj_t*)op);
- a.cbArg = op->f.distanceCbArg;
-
- d = op->f.distanceCbFunc(&a,px,py);
- }
- return d;
- }
-
- bool _cmGrObjCbEvent( cmGr_t* p, cmGrObj_t* op, unsigned flags, unsigned key, int px, int py )
- {
- bool fl = false;
- if( op->f.eventCbFunc != NULL )
- {
- cmGrObjFuncArgs_t a;
- _cmGrObjSetupFuncArgs(&a,p,op);
- a.cbArg = op->f.eventCbArg;
-
- fl = op->f.eventCbFunc(&a,flags,key,px,py);
- }
- return fl;
- }
-
- void _cmGrObjCbVExt( cmGr_t* p, const cmGrObj_t* op, cmGrVExt_t* vext )
- {
- if( op->f.vextCbFunc == NULL )
- cmGrVExtSetEmpty(vext);
- else
- {
- cmGrObjFuncArgs_t a;
- _cmGrObjSetupFuncArgs(&a,p,(cmGrObj_t*)op);
- a.cbArg = op->f.vextCbArg;
-
- op->f.vextCbFunc(&a,vext);
- }
- }
-
- bool _cmGrObjCbIsInside( cmGr_t* p, const cmGrObj_t* op, unsigned evtFlags, int px, int py, cmGrV_t vx, cmGrV_t vy )
- {
- bool fl = false;
- if( op->f.isInsideCbFunc != NULL )
- {
- cmGrObjFuncArgs_t a;
- _cmGrObjSetupFuncArgs(&a,p,(cmGrObj_t*)op);
- a.cbArg = op->f.isInsideCbArg;
- fl = op->f.isInsideCbFunc(&a,evtFlags,px,py,vx,vy);
- }
- return fl;
- }
-
- //====================================================================================================
- // Object Private Functions
- //====================================================================================================
- // Return true if pp is an ancestor (parent,grand-parent,great-grand-parent,...) of cp.
- bool _cmGrObjIsAncestor( cmGrObj_t* pp, cmGrObj_t* cp )
- {
- cmGrObj_t* tp = cp->parent;
- for(; tp != NULL; tp=tp->parent )
- if( tp == pp )
- break;
-
- return tp!=NULL;
- }
-
- // Append 'op' as the right-most child of 'pp'.
- void _cmGrObjAppendChild( cmGrObj_t* pp, cmGrObj_t* cp)
- {
- cp->parent = pp;
-
- if( pp->children == NULL )
- {
- pp->children = cp;
- cp->lsib = NULL;
- cp->rsib = NULL;
- }
- else
- {
- cmGrObj_t* op = pp->children;
- while( op->rsib != NULL )
- op = op->rsib;
-
- op->rsib = cp;
- cp->rsib = NULL;
- cp->lsib = op;
- }
- }
-
- // Insert 'op' on the left of 'rp'.
- void _cmGrObjInsertOnLeft( cmGrObj_t* op, cmGrObj_t* rp )
- {
- op->parent = rp->parent;
- op->rsib = rp;
- op->lsib = rp->lsib;
-
- if( rp->lsib == NULL )
- {
- assert( rp->parent == rp->parent->children);
- rp->parent->children = op;
- }
- else
- {
- rp->lsib->rsib = op;
- }
-
- rp->lsib = op;
- }
-
- // Insert 'op' on the right of 'lp'.
- // 'pp' is the parent of 'lp'. 'pp' must be given explicitely to cover
- // the case where lp is NULL - in which case the new parent for op
- // cannot be determined from lp.
- void _cmGrObjInsertOnRight( cmGrObj_t* op, cmGrObj_t* lp, cmGrObj_t* pp )
- {
- op->parent = pp;
-
- if( lp == NULL )
- {
- assert( pp != NULL && pp->children==NULL );
- pp->children = op;
- op->lsib = NULL;
- op->rsib = NULL;
- return;
- }
-
- assert( lp->parent == pp );
-
- op->lsib = lp;
- op->rsib = lp->rsib;
-
-
- if( lp->rsib != NULL )
- lp->rsib->lsib = op;
-
- lp->rsib = op;
-
- }
-
- // Unlink 'op' from the tree but leave it's children attached.
- void _cmGrObjUnlink( cmGrObj_t * op )
- {
- if( op->parent != NULL && op->parent->children == op )
- op->parent->children = op->parent->children->rsib;
-
- if( op->rsib != NULL )
- op->rsib->lsib = op->lsib;
-
- if( op->lsib != NULL )
- op->lsib->rsib = op->rsib;
-
- op->parent = NULL;
- op->rsib = NULL;
- op->lsib = NULL;
- }
-
- // Free 'op' and all of its children.
- // 'op' must be unlinked before calling this function
- cmGrRC_t _cmGrObjFree( cmGr_t* p, cmGrObj_t* op )
- {
- cmGrRC_t rc = kOkGrRC;
-
- // go to the deepest child
- if( op->children != NULL )
- if((rc = _cmGrObjFree(p,op->children)) != kOkGrRC )
- return rc;
-
- // go right
- if( op->rsib != NULL )
- if((rc = _cmGrObjFree(p,op->rsib)) != kOkGrRC )
- return rc;
-
- // inform the application that we are destroying this object
- _cmGrObjCbDestroy(p,op);
-
- _cmGrObjUnlink(op);
-
- cmMemFree(op);
-
- return rc;
- }
-
- cmGrRC_t _cmGrObjUnlinkAndFree( cmGr_t* p, cmGrObj_t* op )
- {
- cmGrRC_t rc = kOkGrRC;
- cmGrObj_t* rsib = op->rsib;
- cmGrObj_t* par = op->parent;
-
- _cmGrObjUnlink(op);
-
- // if the free fails ...
- if((rc = _cmGrObjFree(p,op)) != kOkGrRC )
- {
- // ... then restore the objects position
- if( rsib == NULL )
- _cmGrObjInsertOnLeft(op,rsib);
- else
- _cmGrObjAppendChild(par,op);
- }
-
- return rc;
- }
-
- // Return kL,T,R,BGrFl indicating which directions of wext are in violation of op->wlimitExt.
- // Return's 0 if no limits are in violation
- unsigned _cmGrObjWorldLimitsTestViolation( const cmGrObj_t* op, const cmGrVExt_t* wext )
- {
- unsigned violFlags = 0;
-
- if( cmIsFlag( op->wlimitFlags, kLeftGrFl) )
- if( cmGrVExtMinX(wext) < cmGrVExtMinX(&op->wlimitExt) )
- violFlags = kLeftGrFl;
-
- if( cmIsFlag( op->wlimitFlags, kTopGrFl) )
- if( cmGrVExtMaxY(wext) < cmGrVExtMaxY(&op->wlimitExt) )
- violFlags = kTopGrFl;
-
- if( cmIsFlag( op->wlimitFlags, kRightGrFl) )
- if( cmGrVExtMaxX(wext) > cmGrVExtMaxX(&op->wlimitExt) )
- violFlags = kRightGrFl;
-
- if( cmIsFlag( op->wlimitFlags, kBottomGrFl) )
- if( cmGrVExtMinY(wext) > cmGrVExtMinY(&op->wlimitExt) )
- violFlags = kBottomGrFl;
-
- return violFlags;
- }
-
- // If op has world extent limits then apply them to 'ext'.
- // Extent directions in 'ext' which do not have limits in 'op' are unchanged.
- // Extent directions in 'ext' which have limits are set to the limit.
- void _cmGrObjApplyExtLimits( cmGrObj_t* op, cmGrVExt_t* ext )
- {
- if( cmIsFlag(op->wlimitFlags,kLeftGrFl) )
- cmGrVExtSetMinX(ext,cmGrVExtMinX(&op->wlimitExt));
-
- if( cmIsFlag(op->wlimitFlags,kBottomGrFl) )
- cmGrVExtSetMinY(ext,cmGrVExtMinY(&op->wlimitExt));
-
- if( cmIsFlag(op->wlimitFlags,kTopGrFl) )
- cmGrVExtSetMaxY(ext,cmGrVExtMaxY(&op->wlimitExt));
-
- if( cmIsFlag(op->wlimitFlags,kRightGrFl) )
- cmGrVExtSetMaxX(ext,cmGrVExtMaxX(&op->wlimitExt));
-
- }
-
-
- // Return the outside extents of the children of 'op'.
- // Returns false if there are no children and leaves wext set to NULL.
- bool _cmGrObjChildExts( cmGr_t* p, const cmGrObj_t* op, cmGrVExt_t* ext )
- {
- cmGrVExtSetNull(ext);
-
- op = op->children;
-
- if( op == NULL )
- return false;
-
- _cmGrObjCbVExt(p,op,ext);
-
- for(op=op->rsib; op!=NULL; op=op->rsib)
- {
- cmGrVExt_t e;
- _cmGrObjCbVExt(p,op,&e);
- cmGrVExtExpandToContain(ext,&e);
- }
-
- return true;
- }
-
- cmGrRC_t _cmGrObjSetWorldExt( cmGr_t* p, cmGrObj_t* op, const cmGrVExt_t* wext );
-
- // The world extents changed to 'ref_wext' on an object.
- // Examine all 'sync'ed' objects and expand them to be contained by 'ref_wext'.
- cmGrRC_t _cmGrSyncWorldExtentsExpand( cmGr_t* p, const cmGrVExt_t* ref_wext )
- {
- cmGrRC_t rc = kOkGrRC;
-
- // apply changes to synch targets
- cmGrSync_t* sp = p->syncs;
- for(; sp!=NULL; sp=sp->link)
- if( cmIsFlag(sp->flags,kWorldSyncGrFl) )
- {
- // get the target ROOT object
- cmGrObj_t* top = _cmGrObjHandleToPtr(cmGrRootObjH(sp->grH));
- bool fl = false;
- cmGrVExt_t top_wext = top->wext;
-
- //printf("sync %i ",top->id);
- //cmGrVExtPrint("top_wext",&top_wext);
- //cmGrVExtPrint("ref_wext",ref_wext);
-
- // if horz sync was requested ...
- if( !fl && cmIsFlag(sp->flags,kHorzSyncGrFl) )
- {
- if( cmGrVExtIsNullOrEmpty(&top_wext) )
- {
- cmGrVExtSetMinX(&top_wext,cmGrVExtMinX(ref_wext));
- cmGrVExtSetMaxX(&top_wext,cmGrVExtMaxX(ref_wext));
- fl = true;
- }
- else
- {
- // ... and the target needs to expand and can expand
- if( cmGrVExtMinX(&top_wext) > cmGrVExtMinX(ref_wext) && cmIsNotFlag(top->wlimitFlags,kLeftGrFl) )
- {
- cmGrVExtSetMinX(&top_wext,cmGrVExtMinX(ref_wext)); // .. expand the view
- fl = true;
- }
-
- if( cmGrVExtMaxX(&top_wext) < cmGrVExtMaxX(ref_wext) && cmIsNotFlag(top->wlimitFlags,kRightGrFl) )
- {
- cmGrVExtSetMaxX(&top_wext,cmGrVExtMaxX(ref_wext));
- fl = true;
- }
- }
- }
-
- // if vert sync was requested ...
- if( !fl && cmIsFlag(sp->flags,kVertSyncGrFl) )
- {
- if( cmGrVExtIsNullOrEmpty(&top_wext) )
- {
- cmGrVExtSetMinY(&top_wext,cmGrVExtMinY(ref_wext));
- cmGrVExtSetMaxY(&top_wext,cmGrVExtMaxY(ref_wext));
- fl = true;
- }
- else
- {
- if( cmGrVExtMinY(&top_wext) > cmGrVExtMinY(ref_wext) && cmIsNotFlag(top->wlimitFlags,kBottomGrFl))
- {
- cmGrVExtSetMinY(&top_wext,cmGrVExtMinY(ref_wext));
- fl = true;
- }
-
- if( cmGrVExtMaxY(&top_wext) < cmGrVExtMaxY(ref_wext) && cmIsNotFlag(top->wlimitFlags,kTopGrFl) )
- {
- cmGrVExtSetMaxY(&top_wext,cmGrVExtMaxY(ref_wext));
- fl = true;
- }
-
- }
- }
-
- // If fl is set then top_wext contains an expanded world view
- if( fl )
- {
- //cmGrVExtPrint("out top_wext",&top_wext);
- // this call may result in a recursion back into this function
- if((rc = _cmGrObjSetWorldExt( _cmGrHandleToPtr(sp->grH), top, &top_wext )) != kOkGrRC )
- goto errLabel;
- }
- }
-
- errLabel:
- return rc;
- }
-
- cmGrRC_t _cmGrObjSetWorldExt( cmGr_t* p, cmGrObj_t* op, const cmGrVExt_t* wext )
- {
- cmGrRC_t rc = kOkGrRC;
- cmGrVExt_t ce;
- cmGrVExt_t we = *wext; // make a copy of the new extents to override the 'const' on 'wext'.
-
- // apply the world ext limits to 'we'.
- _cmGrObjApplyExtLimits(op,&we);
-
- assert(cmGrVExtIsNorm(&we)); // assert w/h are positive
-
- // get the extents around all children
- if( _cmGrObjChildExts(p, op, &ce ) )
- {
- // if 'ce' is not entirely inside 'we'
- if( cmGrVExtIsExtInside(&we,&ce) == false )
- return cmErrMsg(&p->err,kExtsErrGrRC,"The change in world extents would have resulted in child objects outside the requested world extents.");
- }
-
- // if world extents are actually changing
- if( !cmGrVExtIsEqual(&op->wext,&we) )
- {
- // update the world extents for this object
- op->wext = we;
-
- //op->stateFlags = cmSetFlag(op->stateFlags,kDirtyObjFl);
-
- //cmGrVExtPrint(cmTsPrintf("set w: %i ",op->id),&we);
-
- // if this is the root object
- if( p->rootObj == op )
- {
- // this call may result in recursion back into this function
- // if two cmGr's are mutual sync targets - an infinite loop
- // should be avoided by the cmGrVExtIsEqual() test above.
- rc = _cmGrSyncWorldExtentsExpand(p, wext );
- }
- }
-
- return rc;
- }
-
- void _cmGrObjReport( cmGr_t* p, cmGrObj_t* op, cmRpt_t* rpt )
- {
- cmGrVExt_t vext;
- cmRptPrintf(rpt,"id:0x%x \n",op->id);
-
- _cmGrObjCbVExt( p, op, &vext);
- cmGrVExtRpt(&vext,rpt);
- cmRptPrintf(rpt,"\n");
- }
-
- void _cmGrObjReportR( cmGr_t* p, cmGrObj_t* op, cmRpt_t* rpt)
- {
- _cmGrObjReport(p, op,rpt);
-
- if( op->children != NULL )
- _cmGrObjReport(p,op->children,rpt);
-
- if( op->rsib != NULL )
- _cmGrObjReport(p,op->rsib,rpt);
- }
-
-
- //====================================================================================================
- cmGrRC_t cmGrObjCreate( cmGrH_t h, cmGrObjH_t* ohp, cmGrObjH_t parentH, cmGrObjFunc_t* f, unsigned id, unsigned flags, const cmGrVExt_t* wext )
- {
- cmGrRC_t rc;
-
- if((rc = cmGrObjDestroy(h,ohp)) != kOkGrRC )
- return rc;
-
- cmGr_t* p = _cmGrHandleToPtr(h);
-
- // allocate the new object
- cmGrObj_t* op = cmMemAllocZ(cmGrObj_t,1);
-
- op->id = id;
- op->f = *f;
-
- if( wext != NULL )
- {
- op->wext = *wext;
-
- if( cmGrVExtIsNotNull(wext) )
- cmGrVExtNorm(&op->wext);
- }
-
- // if an explicit parent was not provided
- // then assign the root object as the parent
- if( cmGrObjIsValid(h,parentH) == false )
- parentH.h = p->rootObj;
-
- // insert the object into the tree
- if( cmGrObjIsValid(h,parentH) )
- _cmGrObjAppendChild(_cmGrObjHandleToPtr(parentH),op);
- else
- {
- // no root object exits - so make this obj the root
- assert(p->objs == NULL );
- p->objs = op;
- }
-
- ohp->h = op;
-
- // Notify the application that an object was created.
- if((rc = _cmGrObjCbCreate(p,op)) != kOkGrRC )
- goto errLabel;
-
-
- if( f->vextCbFunc != NULL )
- {
- cmGrVExt_t vext;
- cmGrVExtSetEmpty(&vext);
-
- // get the local virtual extents for the new object
- cmGrObjLocalVExt(h, *ohp, &vext );
-
- if( cmGrVExtIsNotNullOrEmpty(&vext) )
- {
- // expand the parents world to contain the new object
- if( op->parent != NULL )
- {
- cmGrVExt_t parent_wext = op->parent->wext;
- if( cmGrVExtExpandToContain(&parent_wext,&vext) )
- _cmGrObjSetWorldExt(p,op->parent,&parent_wext);
-
- assert( cmGrVExtIsExtInside(&op->parent->wext,&vext) );
- }
-
- // if cfg'd to expand the view to contain new objects then do so here
- if( op->parent!=NULL && op->parent->parent==NULL && cmIsFlag(p->cfgFlags,kExpandViewGrFl) && cmGrVExtIsExtInside(&p->vext,&vext) == false )
- {
- cmGrVExt_t v = p->vext;
- if( cmGrVExtExpandToContain(&v,&vext) )
- _cmGrSetViewExtentsE(p,&v);
-
- assert( cmGrVExtIsExtInside(&op->parent->wext,&p->vext));
- }
-
- // if the new object is inside the view extents then mark
- // the object as dirty
- //if( cmGrVExtIsExtInside(&p->vext,&vext) )
- // op->stateFlags = cmSetFlag(op->stateFlags,kDirtyObjFl);
- }
- }
-
- errLabel:
- if( rc != kOkGrRC )
- cmGrObjDestroy(h,ohp);
-
- return rc;
- }
-
- cmGrRC_t _cmGrObjDestroy( cmGr_t* p, cmGrObj_t* op )
- {
- if( op == NULL )
- return kOkGrRC;
-
- return _cmGrObjUnlinkAndFree( p, op);
- }
-
- cmGrRC_t cmGrObjDestroy( cmGrH_t h, cmGrObjH_t* ohp )
- {
- cmGrRC_t rc = kOkGrRC;
-
- if( ohp==NULL || cmGrObjIsValid(h,*ohp) == false )
- return kOkGrRC;
-
- cmGrObj_t* op = _cmGrObjHandleToPtr(*ohp);
-
- if((rc = _cmGrObjDestroy(_cmGrHandleToPtr(h),op)) != kOkGrRC )
- return rc;
-
- ohp->h = NULL;
-
- return rc;
- }
-
- cmGrRC_t cmGrObjIsValid( cmGrH_t h, cmGrObjH_t oh )
- { return oh.h != NULL; }
-
-
- unsigned cmGrObjId( cmGrObjH_t oh )
- { return _cmGrObjHandleToPtr(oh)->id; }
-
- void cmGrObjSetId( cmGrObjH_t oh, unsigned id )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- op->id = id;
- }
-
- cmGrObjH_t cmGrObjParent( cmGrObjH_t oh )
- {
- cmGrObjH_t poh;
- poh.h = _cmGrObjHandleToPtr(oh)->parent;
- return poh;
- }
-
-
- cmGrRC_t cmGrObjSetWorldExt( cmGrH_t h, cmGrObjH_t oh, const cmGrVExt_t* wext )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return _cmGrObjSetWorldExt(p,op,wext);
- }
-
- void cmGrObjWorldExt( cmGrObjH_t oh, cmGrVExt_t* wext )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- *wext = op->wext;
- }
-
- cmGrRC_t cmGrObjSetWorldLimitExt( cmGrH_t h, cmGrObjH_t oh, const cmGrVExt_t* vext, unsigned limitFlags )
- {
- cmGrRC_t rc;
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
-
- // store the current world extents
- cmGrVExt_t lext = op->wlimitExt;
- unsigned lfl = op->wlimitFlags;
-
- // set the new limits
- op->wlimitExt = *vext;
- op->wlimitFlags = limitFlags;
-
- cmGrVExtNorm(&op->wlimitExt);
-
- // attempt to apply the current world extents with the new limits
- // (this may fail if their are child objects out of range of the new extents)
- if( cmGrVExtIsNotNull(&op->wext ) )
- {
- if((rc = cmGrObjSetWorldExt(h,oh,&op->wext)) != kOkGrRC )
- {
- // we failed - restore the old limits
- op->wlimitExt = lext;
- op->wlimitFlags = lfl;
- }
- }
-
- return rc;
- }
-
- void cmGrObjWorldLimitExt( cmGrObjH_t oh, cmGrVExt_t* vext, unsigned* limitFlags )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- if( vext != NULL )
- *vext = op->wlimitExt;
-
- if( limitFlags != NULL )
- *limitFlags = op->wlimitFlags;
- }
-
-
- void cmGrObjSetCreateCb( cmGrObjH_t oh, cmGrCreateObjCb_t cbFunc, void* cbArg )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- op->f.createCbFunc = cbFunc;
- op->f.createCbArg = cbArg;
- }
-
- void cmGrObjSetDestroyCb( cmGrObjH_t oh, cmGrDestroyObjCb_t cbFunc, void* cbArg )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- op->f.destroyCbFunc = cbFunc;
- op->f.destroyCbArg = cbArg;
- }
-
- void cmGrObjSetRenderCb( cmGrObjH_t oh, cmGrRenderObjCb_t cbFunc, void* cbArg )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- op->f.renderCbFunc = cbFunc;
- op->f.renderCbArg = cbArg;
- }
-
- void cmGrObjSetDistanceCb( cmGrObjH_t oh, cmGrDistanceObjCb_t cbFunc, void* cbArg )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- op->f.distanceCbFunc = cbFunc;
- op->f.distanceCbArg = cbArg;
- }
-
- void cmGrObjSetEventCb( cmGrObjH_t oh, cmGrEventObjCb_t cbFunc, void* cbArg )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- op->f.eventCbFunc = cbFunc;
- op->f.eventCbArg = cbArg;
- }
-
- void cmGrObjSetVExtCb( cmGrObjH_t oh, cmGrVExtObjCb_t cbFunc, void* cbArg )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- op->f.vextCbFunc = cbFunc;
- op->f.vextCbArg = cbArg;
- }
-
- void cmGrObjSetIsInsideCb( cmGrObjH_t oh, cmGrIsInsideObjCb_t cbFunc, void* cbArg )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- op->f.isInsideCbFunc = cbFunc;
- op->f.isInsideCbArg = cbArg;
- }
-
- cmGrCreateObjCb_t cmGrObjCreateCbFunc( cmGrObjH_t oh )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return op->f.createCbFunc;
- }
-
- cmGrDestroyObjCb_t cmGrObjDestroyCbFunc( cmGrObjH_t oh )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return op->f.destroyCbFunc;
- }
-
- cmGrRenderObjCb_t cmGrObjRenderCbFunc( cmGrObjH_t oh )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return op->f.renderCbFunc;
- }
-
- cmGrDistanceObjCb_t cmGrObjDistanceCbFunc( cmGrObjH_t oh )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return op->f.distanceCbFunc;
- }
-
- cmGrEventObjCb_t cmGrObjEventCbFunc( cmGrObjH_t oh )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return op->f.eventCbFunc;
- }
-
- cmGrVExtObjCb_t cmGrObjVExtCbFunc( cmGrObjH_t oh )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return op->f.vextCbFunc;
- }
-
- cmGrIsInsideObjCb_t cmGrObjIsInsideCbFunc( cmGrObjH_t oh )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return op->f.isInsideCbFunc;
- }
-
- void* cmGrObjCreateCbArg( cmGrObjH_t oh )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return op->f.createCbArg;
- }
-
- void* cmGrObjDestroyCbArg( cmGrObjH_t oh )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return op->f.createCbArg;
- }
-
- void* cmGrObjRenderCbArg( cmGrObjH_t oh )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return op->f.destroyCbArg;
- }
-
- void* cmGrObjDistanceCbArg( cmGrObjH_t oh )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return op->f.distanceCbArg;
- }
-
- void* cmGrObjEventCbArg( cmGrObjH_t oh )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return op->f.eventCbArg;
- }
-
- void* cmGrObjVExtCbArg( cmGrObjH_t oh )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return op->f.vextCbArg;
- }
-
- void* cmGrObjIsInsideCbArg( cmGrObjH_t oh )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return op->f.isInsideCbArg;
- }
-
- void cmGrObjLocalVExt( cmGrH_t h, cmGrObjH_t oh, cmGrVExt_t* vext )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return _cmGrObjCbVExt( _cmGrHandleToPtr(h), op, vext);
- }
-
- cmGrObj_t* _cmGrObjIdToHandle( cmGrObj_t* op, unsigned id )
- {
- cmGrObj_t* rp = NULL;
- if( op->id == id )
- return op;
-
- if( op->children != NULL )
- if((rp = _cmGrObjIdToHandle(op->children,id)) != NULL )
- return rp;
-
- if( op->rsib != NULL )
- if((rp = _cmGrObjIdToHandle(op->rsib,id)) != NULL )
- return rp;
-
- return NULL;
- }
-
- cmGrObjH_t cmGrObjIdToHandle( cmGrH_t h, unsigned id )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
- cmGrObjH_t oh = cmGrObjNullHandle;
- cmGrObj_t* op;
-
- if((op = _cmGrObjIdToHandle(p->objs,id)) != NULL )
- oh.h = op;
- return oh;
- }
-
- // Move 'aoH' such that it is above 'boH' in the z-order.
- // This means that 'boH' must be drawn before 'aoH'.
- // This algorithm is designed to not break object hierarchies
- // when moving objects. It achieves this by only moving
- // the ancestor objects of boH and aoH at the level where
- // they do not share a common ancestor.
- void cmGrObjDrawAbove( cmGrObjH_t boH, cmGrObjH_t aoH )
- {
- cmGrObj_t* bp = _cmGrObjHandleToPtr(boH);
- cmGrObj_t* ap = _cmGrObjHandleToPtr(aoH);
- cmGrObj_t* rp = bp;
-
- // set rp to the root object
- while( rp->parent != NULL )
- rp=rp->parent;
-
- while(1)
- {
-
- cmGrObj_t* a[] = {NULL,NULL};
- cmGrObj_t* bpp = NULL;
- cmGrObj_t* app = NULL;
- unsigned i = 0;
-
- rp = rp->children;
- for(; rp!=NULL; rp=rp->rsib)
- {
- if( bp==rp || _cmGrObjIsAncestor(rp,bp) )
- {
- assert( a[i]==NULL );
- bpp = rp;
- a[i++] = rp;
-
- if( i==2 )
- break;
- }
-
- if( ap==rp || _cmGrObjIsAncestor(rp,ap) )
- {
- assert( a[i]==NULL );
- app = rp;
- a[i++] = rp;
-
- if( i==2 )
- break;
- }
- }
-
- assert( rp != NULL && i==2 );
-
- // bpp and app share the same ancestor - keep looking
- // for the level where the they do not share same ancestor
- if( bpp == app )
- rp = bpp;
- else
- {
- // if bp is not already being drawn before ap
- if( a[0] != bpp )
- {
- _cmGrObjUnlink(app);
- _cmGrObjInsertOnRight(app,bpp,bpp->parent);
- }
- return;
- }
-
- }
-
- }
-
- void cmGrObjReport( cmGrH_t h, cmGrObjH_t oh, cmRpt_t* rpt )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- _cmGrObjReport(_cmGrHandleToPtr(h),op,rpt);
- }
-
- void cmGrObjReportR( cmGrH_t h, cmGrObjH_t oh, cmRpt_t* rpt )
- {
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- _cmGrObjReportR(_cmGrHandleToPtr(h),op,rpt);
- }
-
-
- //====================================================================================================
- //====================================================================================================
- #define _cmGr_X_VperP(p) (p->vext.sz.w / (p->pext.sz.w-1) )
- #define _cmGr_Y_VperP(p) (p->vext.sz.h / (p->pext.sz.h-1) )
- #define _cmGr_X_PperV(p) ((p->pext.sz.w-1) / p->vext.sz.w )
- #define _cmGr_Y_PperV(p) ((p->pext.sz.h-1) / p->vext.sz.h )
-
- int _cmGr_X_VtoP( cmGr_t* p, cmGrV_t vx )
- { return p->pext.loc.x + lround( (vx - p->vext.loc.x) * _cmGr_X_PperV(p)); }
-
- int _cmGr_Y_VtoP(cmGr_t* p, cmGrV_t vy )
- { return p->pext.loc.y + (p->pext.sz.h-1) + lround(-(vy - p->vext.loc.y) * _cmGr_Y_PperV(p) ); }
-
- cmGrV_t _cmGr_X_PtoV( cmGr_t* p, int px )
- { return p->vext.loc.x + (px - p->pext.loc.x) * _cmGr_X_VperP(p); }
-
- cmGrV_t _cmGr_Y_PtoV( cmGr_t* p, int py )
- { return p->vext.loc.y + (p->pext.loc.y + (p->pext.sz.h-1) - py) * _cmGr_Y_VperP(p); }
-
-
-
- #define _cmGrParentToLocalX( op, vext, x ) (op)->wext.loc.x + ((x) - (vext).loc.x) * (op)->wext.sz.w / (vext).sz.w
- #define _cmGrParentToLocalY( op, vext, y ) (op)->wext.loc.y + ((y) - (vext).loc.y) * (op)->wext.sz.h / (vext).sz.h
-
- #define _cmGrLocalToParentX( op, vext, x ) (vext).loc.x + ((x) - (op)->wext.loc.x) * (vext).sz.w / (op)->wext.sz.w
- #define _cmGrLocalToParentY( op, vext, y ) (vext).loc.y + ((y) - (op)->wext.loc.y) * (vext).sz.h / (op)->wext.sz.h
-
-
- // On input x,y are in the same coord's as op->vext.
- // On output pt is converted to op's internal coord system (i.e. the pt is inside op->wext)
- // Using pt as the src and dst is always safe. (i.e. _cmGrLocalToParent(p,op,pt->x,pt->y,pt) is safe.)
- bool _cmGrParentToLocal( cmGr_t* p, cmGrObj_t* op, cmGrV_t x, cmGrV_t y, cmGrVPt_t* pt )
- {
- cmGrVExt_t vext;
- _cmGrObjCbVExt(p,op,&vext);
-
- if( cmGrVExtIsNullOrEmpty(&vext) )
- return false;
-
- pt->x = _cmGrParentToLocalX( op, vext, x);
- pt->y = _cmGrParentToLocalY( op, vext, y );
-
- //pt->x = op->wext.loc.x + (x - vext.loc.x) * op->wext.sz.w / vext.sz.w;
- //pt->y = op->wext.loc.y + (y - vext.loc.y) * op->wext.sz.h / vext.sz.h;
- return true;
- }
-
-
- // On input x,y are in the same coords as op->wext.
- // On output pt is converted to be in the same coord's as op->vext (i.e.the pt is inside op->parent->wext)
- // Using pt as the src and dst is always safe. (i.e. _cmGrLocalToParent(p,op,pt->x,pt->y,pt) is safe.)
- void _cmGrLocalToParent( cmGr_t* p, cmGrObj_t* op, cmGrV_t x, cmGrV_t y, cmGrVPt_t* pt )
- {
- cmGrVExt_t vext;
- _cmGrObjCbVExt(p,op,&vext);
-
- pt->x = _cmGrLocalToParentX(op,vext, x);
- pt->y = _cmGrLocalToParentY(op,vext, y);
-
- //pt->x = vext.loc.x + (x - op->wext.loc.x) * vext.sz.w / op->wext.sz.w;
- //pt->y = vext.loc.y + (y - op->wext.loc.y) * vext.sz.h / op->wext.sz.h;
- }
-
- // On input x is in coord's inside op->wext.
- // Return is in phys coord's
- int _cmGrX_VtoP( cmGr_t* p, cmGrObj_t* op, cmGrV_t x )
- {
- cmGrVExt_t vext;
-
- for(; op->parent != NULL; op=op->parent )
- {
- _cmGrObjCbVExt(p,op,&vext);
- x = _cmGrLocalToParentX(op->parent,vext,x);
- }
-
- return _cmGr_X_VtoP(p,x);
- }
-
- // On input y is in coord's inside op->wext.
- // Return is in phys coord's
- int _cmGrY_VtoP( cmGr_t* p, cmGrObj_t* op, cmGrV_t y )
- {
- cmGrVExt_t vext;
-
- for(; op->parent != NULL; op=op->parent )
- {
- _cmGrObjCbVExt(p,op,&vext);
- y = _cmGrLocalToParentY(op->parent,vext,y);
- }
-
- return _cmGr_Y_VtoP(p,y);
- }
-
- // On input x,y are coord's inside op->wext.
- // On output rp is in physical coord's
- void _cmGrXY_VtoP( cmGr_t* p, cmGrObj_t* op, cmGrV_t x, cmGrV_t y, cmGrPPt_t* rp )
- {
- cmGrVPt_t pt;
- cmGrVPtSet(&pt,x,y);
-
- for(; op->parent != NULL; op=op->parent )
- _cmGrLocalToParent(p,op->parent,pt.x,pt.y,&pt);
-
- rp->x = _cmGr_X_VtoP(p,pt.x);
- rp->y = _cmGr_Y_VtoP(p,pt.y);
- }
-
- // pt is converted from the root obj coord system to be in the same coord's
- // as op->vext (inside of op->parent->wext)
- void _cmGrXY_GlobalToLocal( cmGr_t* p, cmGrObj_t* op, cmGrVPt_t* pt )
- {
- if( op->parent != NULL )
- _cmGrXY_GlobalToLocal(p,op->parent,pt);
-
- // convert from parent coord to child coords
- _cmGrParentToLocal(p,op,pt->x,pt->y,pt);
-
- }
-
-
- // On input x,y are in physical coordindates.
- // On output rp is inside op->parent->wext (the same coord's as op->vext)
- void _cmGrXY_PtoV( cmGr_t* p, cmGrObj_t* op, int px, int py, cmGrVPt_t* rp )
- {
- // convert the phys points to points in the root coord system
- rp->x = _cmGr_X_PtoV(p,px);
- rp->y = _cmGr_Y_PtoV(p,py);
-
- _cmGrXY_GlobalToLocal(p, op, rp );
- }
-
- int cmGrX_VtoP( cmGrH_t hh, cmGrObjH_t oh, cmGrV_t x )
- {
- cmGr_t* p = _cmGrHandleToPtr(hh);
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return _cmGrX_VtoP(p,op,x);
- }
-
- int cmGrY_VtoP( cmGrH_t hh, cmGrObjH_t oh, cmGrV_t y )
- {
- cmGr_t* p = _cmGrHandleToPtr(hh);
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- return _cmGrY_VtoP(p,op,y);
- }
-
-
- void cmGrXY_VtoP( cmGrH_t h, cmGrObjH_t oh, cmGrV_t x, cmGrV_t y, cmGrPPt_t* rp )
- { _cmGrXY_VtoP(_cmGrHandleToPtr(h), _cmGrObjHandleToPtr(oh), x, y, rp ); }
-
- void cmGrXYWH_VtoP( cmGrH_t hh, cmGrObjH_t oh, cmGrV_t x, cmGrV_t y, cmGrV_t w, cmGrV_t h, cmGrPExt_t* pext )
- {
- cmGr_t* p = _cmGrHandleToPtr(hh);
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- cmGrPPt_t pt0,pt1;
- _cmGrXY_VtoP(p,op,x, y, &pt0);
- _cmGrXY_VtoP(p,op,x+w,y+h,&pt1);
- cmGrPExtSetD(pext,pt0.x,pt0.y,pt1.x,pt1.y);
- }
-
- void cmGrVExt_VtoP( cmGrH_t hh, cmGrObjH_t oh, const cmGrVExt_t* vext, cmGrPExt_t* pext )
- { cmGrXYWH_VtoP(hh,oh,vext->loc.x,vext->loc.y,vext->sz.w,vext->sz.h,pext); }
-
- void cmGrXY_PtoV( cmGrH_t h, cmGrObjH_t oh, int x, int y, cmGrVPt_t* rp )
- { _cmGrXY_PtoV(_cmGrHandleToPtr(h), _cmGrObjHandleToPtr(oh), x, y, rp ); }
-
- void cmGrXYWH_PtoV( cmGrH_t hh, cmGrObjH_t oh, int x, int y, int w, int h, cmGrVExt_t* vext )
- {
- cmGr_t* p = _cmGrHandleToPtr(hh);
- cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
- cmGrVPt_t pt0,pt1;
- _cmGrXY_PtoV(p,op,x,y,&pt0);
- _cmGrXY_PtoV(p,op,x+w-1,y+h-1,&pt1);
- cmGrVExtSetD(vext,pt0.x,pt0.y,pt1.x,pt1.y);
- }
-
- void cmGrPExt_PtoV( cmGrH_t hh, cmGrObjH_t oh, const cmGrPExt_t* pext, cmGrVExt_t* vext )
- { cmGrXYWH_PtoV(hh,oh,pext->loc.x,pext->loc.y,pext->sz.w,pext->sz.h,vext); }
-
-
- void cmGrDrawVLine( cmGrH_t h, cmGrDcH_t dcH, cmGrObjH_t oh, cmGrV_t x0, cmGrV_t y0, cmGrV_t x1, cmGrV_t y1 )
- {
- cmGrPPt_t loc0;
- cmGrPPt_t loc1;
-
- cmGrXY_VtoP(h,oh,x0,y0,&loc0);
- cmGrXY_VtoP(h,oh,x1,y1,&loc1);
- cmGrDcDrawLine(dcH,loc0.x,loc0.y,loc1.x,loc1.y);
- }
-
- void cmGrDrawVRect( cmGrH_t hh, cmGrDcH_t dcH, cmGrObjH_t oh, cmGrV_t x, cmGrV_t y, cmGrV_t w, cmGrV_t h )
- {
- cmGrDrawVLine(hh,dcH,oh,x, y, x, y+h);
- cmGrDrawVLine(hh,dcH,oh,x, y+h,x+w,y+h);
- cmGrDrawVLine(hh,dcH,oh,x+w,y+h,x+w,y);
- cmGrDrawVLine(hh,dcH,oh,x+w,y, x, y);
- }
-
-
- //====================================================================================================
- //====================================================================================================
-
- cmGrRC_t _cmGrDestroy( cmGr_t* p )
- {
- cmGrRC_t rc = kOkGrRC;
-
- if( p->objs != NULL )
- {
- // destroy the root object and all of it's children
- if((rc = _cmGrObjDestroy(p,p->objs)) != kOkGrRC )
- return rc;
- }
-
- cmGrSync_t* sp = p->syncs;
- while( sp != NULL )
- {
- cmGrH_t h;
- h.h = p;
-
- cmGrSync_t* np = sp->link;
- // break sync with any mutually sync'ed gr's to prevent
- // attempted calls to this gr.
- cmGrSetSync( sp->grH, h, 0 );
- cmMemFree(sp);
- sp = np;
- }
-
- p->syncs = NULL;
-
- cmLHeapDestroy(&p->lhH);
-
- cmMemPtrFree(&p->img);
-
- cmMemFree(p);
-
- return rc;
- }
-
- void _cmGrObjRootVExt( cmGrObjFuncArgs_t* args, cmGrVExt_t* vext )
- {
- // the root object's virtual extent is the same as it's world extent
- cmGrObjWorldExt( args->objH, vext );
- }
-
- bool _cmGrObjRootIsInside( cmGrObjFuncArgs_t* args, unsigned evtFlags, int px, int py, cmGrV_t vx, cmGrV_t vy )
- {
- cmGrVExt_t vext;
- _cmGrObjRootVExt(args,&vext);
- return cmGrVExtIsXyInside(&vext,vx,vy);
- }
-
- void _cmGrSetCfgFlags( cmGr_t* p, unsigned flags )
- {
- p->cfgFlags = flags;
-
- // kSelectHorzFl and kSelectVertFl are mutually exclusive
- if( cmIsFlag(p->cfgFlags,kSelectHorzGrFl) )
- p->cfgFlags = cmClrFlag(p->cfgFlags,kSelectVertGrFl);
-
- }
-
- void _cmGrCallback( cmGr_t* p, cmGrCbId_t id, unsigned eventFlags, cmGrKeyCodeId_t keycode )
- {
- if( p->cbFunc != NULL )
- {
- cmGrH_t h;
- h.h = p;
- p->cbFunc(p->cbArg,h,id,eventFlags,keycode);
- }
- }
-
-
- cmGrRC_t cmGrCreate( cmCtx_t* ctx, cmGrH_t* hp, unsigned id, unsigned cfgFlags, cmGrCbFunc_t cbFunc, void* cbArg, const cmGrVExt_t* wext )
- {
- cmGrRC_t rc = kOkGrRC;
- cmGrVExt_t null_ext;
- cmGrObjH_t rootObjH = cmGrObjNullHandle;
-
- if(( rc = cmGrDestroy(hp)) != kOkGrRC )
- return rc;
-
- // create the cmGr_t
- cmGr_t* p = cmMemAllocZ(cmGr_t,1);
- p->ctx = *ctx;
- p->id = id;
- p->stateFlags = kDirtyGrFl;
- p->cbFunc = cbFunc;
- p->cbArg = cbArg;
-
- _cmGrSetCfgFlags(p,cfgFlags);
-
- if( wext == NULL )
- {
- cmGrVExtSetNull(&null_ext);
- wext = &null_ext;
- }
-
- cmErrSetup(&p->err,&ctx->rpt,"cmGr");
-
- if( cmLHeapIsValid( p->lhH = cmLHeapCreate(8192,ctx)) == false )
- {
- rc = cmErrMsg(&p->err,kLHeapFailGrRC,"Linked heap create failed.");
- goto errLabel;
- }
-
- cmGrVExtSetEmpty(&p->vext);
- cmGrPExtSetEmpty(&p->pext);
-
- hp->h = p;
-
- // create the root object
- cmGrObjFunc_t funcs;
- memset(&funcs,0,sizeof(funcs));
- funcs.vextCbFunc = _cmGrObjRootVExt;
- funcs.isInsideCbFunc = _cmGrObjRootIsInside;
-
- if((rc = cmGrObjCreate(*hp, &rootObjH, cmGrObjNullHandle, &funcs, cmInvalidId, 0, wext )) != kOkGrRC )
- {
- rc = cmErrMsg(&p->err,kRootObjCreateFailGrRC,"The root object creation failed.");
- goto errLabel;
- }
-
- // create the default color map
- _cmGrRgbInitDefaultColorMap(p);
-
- p->objs = _cmGrObjHandleToPtr(rootObjH);
- p->rootObj = p->objs;
-
- _cmGrCallback(p,kCreateCbGrId,0,kInvalidKeyCodeGrId);
-
- errLabel:
- if( rc != kOkGrRC )
- {
- _cmGrDestroy(p);
- hp->h = NULL;
- }
-
- return rc;
- }
-
- cmGrRC_t cmGrDestroy( cmGrH_t* hp )
- {
- cmGrRC_t rc = kOkGrRC;
- if( hp==NULL || cmGrIsValid(*hp) == false )
- return kOkGrRC;
-
- cmGr_t* p = _cmGrHandleToPtr(*hp);
-
- if((rc = _cmGrDestroy(p)) != kOkGrRC )
- goto errLabel;
-
- hp->h = NULL;
-
- errLabel:
- return rc;
- }
-
- // Destroy all objects (except the root object)
- cmGrRC_t _cmGrObjDestroyAll( cmGr_t* p )
- {
- cmGrObj_t* op = p->objs;
- if( op != NULL )
- {
- op = op->children;
- while( op != NULL )
- {
- cmGrObj_t* np = op->rsib;
- _cmGrObjUnlinkAndFree(p,op);
- op = np;
- }
- }
- return kOkGrRC;
- }
-
-
- cmGrRC_t cmGrClear( cmGrH_t h )
- {
- cmGrRC_t rc;
- cmGr_t* p = _cmGrHandleToPtr(h);
-
- if((rc = _cmGrObjDestroyAll(p)) != kOkGrRC )
- return rc;
-
- p->rootObj = p->objs;
- p->stateFlags = kDirtyGrFl;
-
- cmGrVExtSetEmpty(&p->vext);
-
- p->msDnObj = NULL;
- cmGrPPtSet(&p->msDnPPt,0,0);
- cmGrVPtSet(&p->msDnVPt,0,0);
- cmGrVPtSet(&p->msVPt,0,0);
- cmGrVSzSet(&p->msDnVOffs,0,0);
-
- p->selValidFl = false;
- cmGrVPtSet(&p->sel0Pt,0,0);
- cmGrVPtSet(&p->sel1Pt,0,0);
-
- cmGrVPtSet(&p->localPt,0,0);
- cmGrVPtSet(&p->globalPt,0,0);
-
- cmGrVExtSetNull(&p->rootObj->wext);
- return rc;
- }
-
-
- cmGrObjH_t cmGrRootObjH( cmGrH_t h )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
- cmGrObjH_t oh;
- oh.h = p->rootObj;
- return oh;
- }
-
- unsigned cmGrCfgFlags( cmGrH_t h )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
- return p->cfgFlags;
- }
-
- void cmGrSetCfgFlags( cmGrH_t h, unsigned cfgFlags )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
- _cmGrSetCfgFlags(p,cfgFlags);
- }
-
-
- void _cmGrObjDraw( cmGr_t* p, cmGrObj_t* op, cmGrDcH_t dcH )
- {
- _cmGrObjCbRender(p,dcH,op);
-
- if( op->children != NULL )
- _cmGrObjDraw(p,op->children,dcH);
-
- if( op->rsib != NULL )
- _cmGrObjDraw(p,op->rsib,dcH);
-
- }
-
- void _cmInvertImage( cmGr_t* p, cmGrDcH_t dcH, const cmGrPExt_t* pext )
- {
- if( cmGrPExtIsNullOrEmpty(pext) )
- return;
-
- cmGrPExt_t r;
- cmGrPExtIntersect( &r, pext, &p->pext );
-
- unsigned n = r.sz.w * r.sz.h * 3;
-
- if( n > 0 )
- {
- unsigned i;
- p->img = cmMemResizeZ(unsigned char,p->img,n);
-
- //p->dd.read_image(p->ddUser, p->img, r.loc.x, r.loc.y, r.sz.w, r.sz.h );
- cmGrDcReadImage( dcH, p->img, &r );
-
- for(i=0; i<n; ++i)
- p->img[i] = 255 - p->img[i];
-
- //p->dd.draw_image(p->ddUser, p->img, r.loc.x, r.loc.y, r.sz.w, r.sz.h);
- cmGrDcDrawImage( dcH, p->img, &r);
- }
- }
-
- cmGrRC_t cmGrDraw( cmGrH_t h, cmGrDcH_t dcH )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
- _cmGrObjDraw(p,p->rootObj,dcH);
-
- cmGrPExt_t pext;
- cmGrVExt_t vext;
- cmGrSelectExtents(h,&vext,&pext);
-
- if( cmGrPExtIsNotNull(&pext))
- {
-
- if( pext.sz.w<=1 && pext.sz.h<=1 )
- {
- cmGrPExt_t ipext;
-
- if( !cmIsFlag(p->cfgFlags,kSelectHorzGrFl) )
- {
- cmGrPExtSet(&ipext,p->pext.loc.x,pext.loc.y,p->pext.sz.w,1);
- _cmInvertImage(p,dcH,&ipext);
- }
-
- if( !cmIsFlag(p->cfgFlags,kSelectVertGrFl) )
- {
- cmGrPExtSet(&ipext,pext.loc.x,p->pext.loc.y,1,p->pext.sz.h);
- _cmInvertImage(p,dcH,&ipext);
- }
- }
- else
- {
- _cmInvertImage(p,dcH,&pext);
- }
- }
-
- return kOkGrRC;
- }
-
- bool _cmGrSetViewExtents( cmGr_t* p, cmGrV_t minx, cmGrV_t miny, cmGrV_t maxx, cmGrV_t maxy )
- {
- cmGrVExt_t vext;
-
- // load a vext with the new view extents
- cmGrVExtSetD(&vext,minx,miny,maxx,maxy);
-
- // if the view ext is not changing
- if( cmGrVExtIsEqual(&p->vext,&vext) )
- return false;
-
- // the root object must exist
- assert( p->rootObj != NULL );
-
- // the view extents must be in the world extents
- if( cmGrVExtIsExtInside(&p->rootObj->wext,&vext)==false )
- {
- cmErrMsg(&p->err,kInvalidCoordsGrRC,"View extent is not inside the world extents.");
- return false;
- }
-
- //cmGrVExtPrint("set vw:",&vext);
-
- p->vext = vext;
- p->stateFlags = cmSetFlag(p->stateFlags,kDirtyGrFl);
-
- _cmGrCallback(p, kViewExtCbGrId,0,kInvalidKeyCodeGrId );
-
- // apply changes to synch target
- cmGrSync_t* sp = p->syncs;
- for(; sp!=NULL; sp=sp->link)
- if( cmIsFlag(sp->flags,kViewSyncGrFl) )
- {
- cmGrViewExtents( sp->grH, &vext );
- bool fl = false;
-
- if( cmIsFlag(sp->flags,kHorzSyncGrFl) )
- {
- fl = true;
- vext.loc.x = p->vext.loc.x;
- vext.sz.w = p->vext.sz.w;
- }
-
-
- if( cmIsFlag(sp->flags,kVertSyncGrFl) )
- {
- fl = true;
- vext.loc.y = p->vext.loc.y;
- vext.sz.h = p->vext.sz.h;
- }
-
- if( fl )
- cmGrSetViewExtentsE( sp->grH, &vext );
- }
- //printf("s:%p %f %f %f %f\n",p,p->vext.loc.x, p->vext.loc.y, p->vext.sz.w, p->vext.sz.h );
-
- return true;
- }
-
- bool _cmGrSetScrollH( cmGr_t* p, int x )
- {
- assert( p->rootObj != NULL );
- cmGrV_t vx = p->rootObj->wext.loc.x + (x * p->vext.sz.w / p->pext.sz.w);
-
- return _cmGrSetViewExtents(p,vx, p->vext.loc.y, vx+p->vext.sz.w, p->vext.loc.y+p->vext.sz.h);
- }
-
- int _cmGrScrollH( cmGr_t* p )
- { return round((p->vext.loc.x - p->rootObj->wext.loc.x) * p->pext.sz.w / p->vext.sz.w); }
-
- bool _cmGrSetScrollV( cmGr_t* p, int y )
- {
- assert( p->rootObj != NULL );
- cmGrV_t vy = p->rootObj->wext.loc.y + (y * p->vext.sz.h / p->pext.sz.h);
-
- return _cmGrSetViewExtents(p, p->vext.loc.x, vy, p->vext.loc.x+p->vext.sz.w, vy + p->vext.sz.h);
- }
-
- int _cmGrScrollV( cmGr_t* p )
- { return round((p->vext.loc.y - p->rootObj->wext.loc.y) * p->pext.sz.h / p->vext.sz.h); }
-
- void _cmGrSetSelectPoints(cmGr_t* p, const cmGrVPt_t* pt0, const cmGrVPt_t* pt1)
- {
- bool deltaFl = false;
-
- p->selValidFl = true;
-
- if( pt0 != NULL )
- {
- if( cmGrVPtIsNotEqual(&p->sel0Pt,pt0) )
- {
- p->sel0Pt = *pt0;
- deltaFl = true;
- }
-
- if( pt1 == NULL && cmGrVPtIsNotEqual(&p->sel1Pt,pt0) )
- {
- p->sel1Pt = *pt0;
- deltaFl = true;
- }
- }
-
- if( pt1 != NULL && cmGrVPtIsNotEqual(&p->sel1Pt,pt1) )
- {
- p->sel1Pt = *pt1;
- deltaFl = true;
- }
-
- if( !deltaFl )
- return;
-
- _cmGrCallback(p,kSelectExtCbGrId,0,kInvalidKeyCodeGrId);
-
- // apply changes to synch target
- cmGrSync_t* sp = p->syncs;
- for(; sp!=NULL; sp=sp->link)
- if( cmIsFlag(sp->flags,kSelectSyncGrFl) )
- {
- cmGrVPt_t pt0, pt1;
- cmGrSelectPoints(sp->grH, &pt0, &pt1 );
-
-
- bool fl = false;
-
- if( cmIsFlag(sp->flags,kHorzSyncGrFl) )
- {
- fl = true;
- pt0.x = p->sel0Pt.x;
- pt1.x = p->sel1Pt.x;
- }
-
- if( cmIsFlag(sp->flags,kVertSyncGrFl) )
- {
- fl = true;
- pt0.y = p->sel0Pt.y;
- pt1.y = p->sel1Pt.y;
- }
-
- if( fl )
- cmGrSetSelectPoints( sp->grH, &pt0, &pt1 );
- }
-
-
- }
-
- void _cmGrScrollExtents( cmGr_t* p, cmGrPSz_t* tot, cmGrPSz_t* vis, cmGrPSz_t* max, cmGrPPt_t* pos )
- {
-
- assert( p->rootObj != NULL );
-
- if( tot != NULL )
- {
- tot->w = round(p->rootObj->wext.sz.w * p->pext.sz.w / p->vext.sz.w);
- tot->h = round(p->rootObj->wext.sz.h * p->pext.sz.h / p->vext.sz.h);
- }
-
- if( vis != NULL )
- {
- vis->w = round(p->vext.sz.w * p->pext.sz.w / p->vext.sz.w);
- vis->h = round(p->vext.sz.h * p->pext.sz.h / p->vext.sz.h);
- }
-
- if( max != NULL )
- {
- max->w = tot->w - vis->w;
- max->h = tot->h - vis->h;
- }
-
- if( pos != NULL )
- {
- pos->x = _cmGrScrollH(p);
- pos->y = _cmGrScrollV(p);
- }
- }
-
- // vx,vy are in the same coord's as op->vext
- cmGrObj_t* _cmGrFindObjRec( cmGr_t* p, cmGrObj_t* op, unsigned evtFlags, int px, int py, cmGrV_t vx, cmGrV_t vy )
- {
- cmGrObj_t* rop = NULL;
- cmGrObj_t* top;
- cmGrVExt_t vext;
-
- // get the location of op inside op->parent->wext
- _cmGrObjCbVExt(p,op,&vext);
-
- // is vx,vy inside op - this is equiv to: cmGrVExtIsXyInside(&vext,vx,vy)
- if( _cmGrObjCbIsInside(p,op,evtFlags,px,py,vx,vy) )
- rop = op;
-
- if( op->children != NULL )
- {
- cmGrVPt_t pt;
- if( _cmGrParentToLocal(p,op,vx,vy,&pt) )
- if((top = _cmGrFindObjRec(p,op->children,evtFlags,px,py,vx,vy)) != NULL )
- rop = top;
- }
-
- if( op->rsib != NULL )
- if((top = _cmGrFindObjRec(p,op->rsib,evtFlags,px,py,vx,vy)) != NULL )
- rop = top;
-
-
- return rop;
- }
-
- cmGrObj_t* _cmGrEventMsDown( cmGr_t* p, unsigned evtFlags, cmGrKeyCodeId_t key, int px, int py, cmGrV_t gx, cmGrV_t gy )
- {
- // store the phys loc. of the ms dn event
- cmGrPPtSet(&p->msDnPPt,px,py);
-
- // get a pointer to an object
- cmGrObj_t* op = _cmGrFindObjRec(p, p->rootObj, evtFlags, px, py, gx, gy );
-
- // if the mouse did not go down in an object that accepts mouse down events
- // or the object was a root object
- if( op == NULL || op->parent == NULL )
- return NULL;
-
- // store the object and coord's where the mouse went down.
- cmGrVExt_t vext;
- p->msDnObj = op; // set the msDnObj
-
- // convert the phys ms dn point to the virt point inside op->parent.wext
- _cmGrXY_PtoV(p, op->parent, px, py, &p->msDnVPt );
-
- p->msVPt = p->msDnVPt; // set the current ms virt pt
- p->localPt = p->msDnVPt; // set the current local pt
-
- // notifiy the app of the local coord's
- _cmGrCallback(p,kLocalPtCbGrId,0,kInvalidKeyCodeGrId);
-
- // get the ms down obj virt extents
- _cmGrObjCbVExt(p,op,&vext);
-
- // store the offset from the lower left to the ms dn point
- p->msDnVOffs.w = p->msDnVPt.x - vext.loc.x;
- p->msDnVOffs.h = p->msDnVPt.y - vext.loc.y;
-
- // notify the object that the mouse went down
- if(_cmGrObjCbEvent(p,op,evtFlags,key,px,py))
- return op;
- return NULL;
- }
-
- cmGrObj_t* _cmGrEventMsUp( cmGr_t* p, unsigned evtFlags, cmGrKeyCodeId_t key, int px, int py, cmGrV_t gx, cmGrV_t gy )
- {
- bool fl = false;
- cmGrObj_t* op = NULL;
-
- cmGrPPt_t upPPt;
- cmGrPPtSet(&upPPt,px,py);
-
- // if a click occured ...
- if( cmGrPPtIsEqual(&p->msDnPPt,&upPPt ) )
- {
- // ... and the click is in a non-root object which accepts click events ...
- if( p->msDnObj!= NULL && p->msDnObj->parent!=NULL && _cmGrObjCbIsInside(p,p->msDnObj,evtFlags | kMsClickGrFl, px, py, gx, gy) )
- {
- // ... then generate a click event
- unsigned newEvtFlags = cmClrFlag(evtFlags,kMsUpGrFl) | kMsClickGrFl;
- fl = _cmGrObjCbEvent(p,p->msDnObj,newEvtFlags,key,px,py);
- op = p->msDnObj;
- }
- else // ... else if the click occurred in the root object
- //if( p->msDnObj==NULL || p->msDnObj->parent==NULL)
- {
- cmGrVPt_t pt;
- cmGrVPtSet(&pt,gx,gy);
-
- bool shFl = cmIsFlag(evtFlags,kShiftKeyGrFl);
- _cmGrSetSelectPoints( p, shFl ? NULL : &pt, shFl ? &pt : NULL );
-
- }
- }
-
- // if op is NULL then there was no-mouse down object to match with
- // this mouse-up event - find an object to match with the mouse-up event
- if( op == NULL )
- op = _cmGrFindObjRec(p, p->rootObj, evtFlags, px, py, gx, gy );
-
- // if a mouse-up object was found then
- if( op != NULL && op->parent != NULL)
- {
-
- // notify the object under the mouse that the mouse went up
- if( _cmGrObjCbEvent(p,op,evtFlags,key,px,py) )
- fl = true;
-
- // convert the phys ms dn point to the virt point inside op->parent.wext
- _cmGrXY_PtoV(p, op->parent, px, py, &p->msVPt );
-
- }
-
- _cmGrCallback(p,kFocusCbGrId,0,kInvalidKeyCodeGrId);
-
- p->msDnObj = NULL;
-
- return fl ? op : NULL;
- }
-
- cmGrObj_t* _cmGrEventMsMove( cmGr_t* p, unsigned evtFlags, cmGrKeyCodeId_t key, int px, int py, cmGrV_t gx, cmGrV_t gy )
- {
- bool fl = false;
- cmGrObj_t* op = _cmGrFindObjRec(p, p->rootObj, evtFlags, px, py, gx, gy );
-
- if( op != NULL && op->parent != NULL )
- {
-
- fl = _cmGrObjCbEvent(p,op,evtFlags,key,px,py);
- }
-
- return fl ? op : NULL;
- }
-
- cmGrObj_t* _cmGrEventMsDrag( cmGr_t* p, unsigned evtFlags, cmGrKeyCodeId_t key, int px, int py, cmGrV_t gx, cmGrV_t gy )
- {
- bool fl = false;
- cmGrObj_t* op = _cmGrFindObjRec(p, p->rootObj, evtFlags, px, py, gx, gy );
-
- if( op != NULL && op->parent != NULL )
- {
- // set the current virtual point
- _cmGrXY_PtoV(p, p->msDnObj->parent, px, py, &p->msVPt );
-
- // callback the dragged object
- fl = _cmGrObjCbEvent(p,p->msDnObj,evtFlags,key,px,py);
-
- // if the px,py is outside the root phys extents then scroll
- if( !cmGrPExtIsXyInside(&p->pext,px,py) )
- {
- bool hFl = false, vFl=false;
- cmGrPSz_t tot,vis,max;
- cmGrPPt_t pos;
- _cmGrScrollExtents(p, &tot, &vis, &max, &pos );
-
- if( px < cmGrPExtL(&p->pext) )
- hFl = _cmGrSetScrollH(p, cmMax(0, _cmGrScrollH(p) - (cmGrPExtL(&p->pext) - px)));
- else
- if( px > cmGrPExtR(&p->pext) )
- hFl = _cmGrSetScrollH(p, cmMin(max.w, _cmGrScrollH(p) + (px - cmGrPExtR(&p->pext))));
-
- if( py < cmGrPExtT(&p->pext) )
- vFl = _cmGrSetScrollV(p, cmMax(0, _cmGrScrollV(p) - (cmGrPExtT(&p->pext) - py)));
- else
- if( py > cmGrPExtB(&p->pext) )
- vFl = _cmGrSetScrollV(p, cmMin(max.h, _cmGrScrollV(p) + (py - cmGrPExtB(&p->pext))));
-
-
- fl = fl || vFl || hFl;
- }
- }
- return fl ? op : NULL;
- }
-
- bool cmGrEvent( cmGrH_t h, unsigned evtFlags, cmGrKeyCodeId_t key, int px, int py )
- {
- bool fl = false;
- cmGrObj_t* op = NULL;
- cmGr_t* p = _cmGrHandleToPtr(h);
- cmGrVPtSet(&p->localPt,0,0);
-
- // convert the phys points to points in the root coord system
- cmGrV_t gx = _cmGr_X_PtoV(p,px);
- cmGrV_t gy = _cmGr_Y_PtoV(p,py);
-
- // set the global point
- cmGrVPtSet(&p->globalPt,gx,gy);
-
- // inform the app of the possibly new global point
- _cmGrCallback(p,kGlobalPtCbGrId,0,kInvalidKeyCodeGrId);
-
- switch( evtFlags & kEvtMask)
- {
- case kKeyUpGrFl:
- _cmGrCallback(p,kKeyUpCbGrId,evtFlags,key);
- break;
-
- case kKeyDnGrFl:
- _cmGrCallback(p,kKeyDnCbGrId,evtFlags,key);
- break;
-
- case kMsDownGrFl:
- op = _cmGrEventMsDown(p,evtFlags,key,px,py,gx,gy);
- break;
-
- case kMsUpGrFl: // handle mouse-up, mouse-click, and focus events
- op = _cmGrEventMsUp(p,evtFlags,key,px,py,gx,gy);
- break;
-
- case kMsMoveGrFl:
- op = _cmGrEventMsMove(p,evtFlags,key,px,py,gx,gy);
- break;
-
- case kMsDragGrFl:
- op = _cmGrEventMsDrag(p,evtFlags,key,px,py,gx,gy);
- break;
-
- }
-
- if( op != NULL )
- {
- // convert gx,gy to be inside op->wext
- cmGrVPtSet(&p->localPt,gx,gy);
- _cmGrXY_GlobalToLocal(p,op,&p->localPt);
-
- _cmGrCallback(p,kLocalPtCbGrId,0,kInvalidKeyCodeGrId);
-
- fl = true;
- }
-
- return fl;
- }
-
- bool cmGrEvent1( cmGrH_t h, unsigned flags, cmGrKeyCodeId_t key, int px, int py )
- {
- bool fl = false;
- cmGr_t* p = _cmGrHandleToPtr(h);
- cmGrObj_t* op;
- cmGrVPtSet(&p->localPt,0,0);
-
- // convert the phys points to points in the root coord system
- cmGrV_t gx = _cmGr_X_PtoV(p,px);
- cmGrV_t gy = _cmGr_Y_PtoV(p,py);
-
- cmGrVPtSet(&p->globalPt,gx,gy);
-
- _cmGrCallback(p,kGlobalPtCbGrId,0,kInvalidKeyCodeGrId);
-
- // find the obj under the mouse
- if((op = _cmGrFindObjRec(p,p->rootObj,flags&kEvtMask,px,py,gx,gy)) != NULL )
- {
- // convert gx,gy to be inside op->wext
- cmGrVPtSet(&p->localPt,gx,gy);
- _cmGrXY_GlobalToLocal(p,op,&p->localPt);
-
- _cmGrCallback(p,kLocalPtCbGrId,0,kInvalidKeyCodeGrId);
-
- }
-
- if( (flags&kEvtMask)==kKeyUpGrFl )
- _cmGrCallback(p,kKeyUpCbGrId,flags,key);
-
- if( (flags&kEvtMask)==kKeyDnGrFl )
- _cmGrCallback(p,kKeyDnCbGrId,flags,key);
-
- if( op != NULL )
- {
- switch( flags & kEvtMask )
- {
- case kMsDownGrFl:
-
- // store the phys loc. of the ms dn event
- cmGrPPtSet(&p->msDnPPt,px,py);
-
- if( op != NULL )
- {
- // if the click was in not in the root object
- if( op->parent != NULL )
- {
- // store the object and coord's where the mouse went down.
-
- cmGrVExt_t vext;
- p->msDnObj = op; // set the msDnObj
-
- // convert the phys ms dn point to the virt point inside op->parent.wext
- _cmGrXY_PtoV(p, op->parent, px, py, &p->msDnVPt );
-
- // set the current ms virt pt
- p->msVPt = p->msDnVPt;
-
- // get the ms down obj virt extents
- _cmGrObjCbVExt(p,op,&vext);
-
- // store the offset from the lower left to the ms dn point
- p->msDnVOffs.w = p->msDnVPt.x - vext.loc.x;
- p->msDnVOffs.h = p->msDnVPt.y - vext.loc.y;
-
- // notify the object that the mouse went down
- fl = _cmGrObjCbEvent(p,op,flags,key,px,py);
- }
- }
- break;
-
- case kMsUpGrFl:
- {
- cmGrPPt_t upPPt;
- cmGrPPtSet(&upPPt,px,py);
-
- bool clickFl = cmGrPPtIsEqual(&p->msDnPPt,&upPPt );
-
- // if a click occured ...
- if( clickFl )
- {
- // ... and the click is in a non-root object ...
- if( p->msDnObj!= NULL && p->msDnObj->parent!=NULL )
- {
- // ... then generate a click event
- unsigned evtFlags = cmClrFlag(flags,kMsUpGrFl) | kMsClickGrFl;
- fl = _cmGrObjCbEvent(p,op,evtFlags,key,px,py);
- }
- else // ... else if the click occurred in the root object
- {
- cmGrVPt_t pt;
- cmGrVPtSet(&pt,gx,gy);
-
- bool shFl = cmIsFlag(flags,kShiftKeyGrFl);
- _cmGrSetSelectPoints( p, shFl ? NULL : &pt, shFl ? &pt : NULL );
-
- }
-
- }
-
- // notify the object under the mouse that the mouse went up
- if( _cmGrObjCbEvent(p,op,flags,key,px,py) )
- fl = true;
-
- _cmGrCallback(p,kFocusCbGrId,0,kInvalidKeyCodeGrId);
-
- p->msDnObj = NULL;
- }
- break;
-
- case kMsMoveGrFl:
- fl = _cmGrObjCbEvent(p,op,flags,key,px,py);
- break;
-
- case kMsWheelGrFl:
- break;
-
- case kMsDragGrFl:
- if( p->msDnObj != NULL )
- {
- // set the current virtual point
- _cmGrXY_PtoV(p, p->msDnObj->parent, px, py, &p->msVPt );
-
- // callback the dragged object
- fl = _cmGrObjCbEvent(p,p->msDnObj,flags,key,px,py);
-
- // if the px,py is outside the root phys extents then scroll
- if( !cmGrPExtIsXyInside(&p->pext,px,py) )
- {
- bool hFl = false, vFl=false;
- cmGrPSz_t tot,vis,max;
- cmGrPPt_t pos;
- cmGrScrollExtents(h, &tot, &vis, &max, &pos );
-
- if( px < cmGrPExtL(&p->pext) )
- hFl = _cmGrSetScrollH(p, cmMax(0, _cmGrScrollH(p) - (cmGrPExtL(&p->pext) - px)));
- else
- if( px > cmGrPExtR(&p->pext) )
- hFl = _cmGrSetScrollH(p, cmMin(max.w, _cmGrScrollH(p) + (px - cmGrPExtR(&p->pext))));
-
- if( py < cmGrPExtT(&p->pext) )
- vFl = _cmGrSetScrollV(p, cmMax(0, _cmGrScrollV(p) - (cmGrPExtT(&p->pext) - py)));
- else
- if( py > cmGrPExtB(&p->pext) )
- vFl = _cmGrSetScrollV(p, cmMin(max.h, _cmGrScrollV(p) + (py - cmGrPExtB(&p->pext))));
-
-
- fl = fl || vFl || hFl;
- }
-
- }
- break;
-
- case kKeyDnGrFl:
- case kKeyUpGrFl:
- //fl = _cmGrObjCbEvent(p,p->msDnObj,flags,key,px,py);
- break;
- }
- }
-
- return fl;
- }
-
-
- bool cmGrIsValid( cmGrH_t h )
- { return h.h != NULL; }
-
- unsigned cmGrId( cmGrH_t h )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
- return p->id;
- }
-
- const cmGrVPt_t* cmGrGlobalPt( cmGrH_t h )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
- return &p->globalPt;
- }
-
- const cmGrVPt_t* cmGrLocalPt( cmGrH_t h )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
- return &p->localPt;
- }
-
-
- bool cmGrSetViewExtents( cmGrH_t h, cmGrV_t minx, cmGrV_t miny, cmGrV_t maxx, cmGrV_t maxy )
- { return _cmGrSetViewExtents( _cmGrHandleToPtr(h), minx,miny,maxx,maxy); }
-
- bool cmGrSetViewExtentsE( cmGrH_t h, const cmGrVExt_t* e )
- { return cmGrSetViewExtents( h, cmGrVExtMinX(e), cmGrVExtMinY(e), cmGrVExtMaxX(e), cmGrVExtMaxY(e) ); }
-
- void cmGrViewExtents( cmGrH_t h, cmGrVExt_t* vext )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
- *vext = p->vext;
-
- //printf("g0:%p %f %f %f %f\n",p,p->vext.loc.x, p->vext.loc.y, p->vext.sz.w, p->vext.sz.h );
- //printf("g1:%p %f %f %f %f\n",p,vext->loc.x, vext->loc.y, vext->sz.w, vext->sz.h );
- }
-
-
- bool cmGrSetPhysExtents( cmGrH_t hh, int x, int y, int w, int h )
- {
- cmGr_t* p = _cmGrHandleToPtr(hh);
- cmGrPExt_t pext;
- cmGrPExtSet(&pext,x,y,w,h);
-
- assert( cmGrPExtIsNull(&pext)==false );
-
- if( cmGrPExtIsEqual(&pext,&p->pext) )
- return false;
-
- p->pext = pext;
- p->stateFlags = cmSetFlag(p->stateFlags,kDirtyGrFl);
-
- _cmGrCallback(p,kPhysExtCbGrId,0,kInvalidKeyCodeGrId);
-
- //cmGrPExtPrint("pext",&p->pext);
- return true;
- }
-
- bool cmGrSetPhysExtentsE(cmGrH_t h, const cmGrPExt_t* e )
- { return cmGrSetPhysExtents(h, cmGrPExtL(e), cmGrPExtT(e), cmGrPExtW(e), cmGrPExtH(e) ); }
-
- void cmGrPhysExtents( cmGrH_t h, cmGrPExt_t* exts )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
- *exts = p->pext;
- }
- void cmGrScrollExtents( cmGrH_t h, cmGrPSz_t* tot, cmGrPSz_t* vis, cmGrPSz_t* max, cmGrPPt_t* pos )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
- _cmGrScrollExtents(p,tot,vis,max,pos);
- }
-
-
- bool cmGrSetScrollH( cmGrH_t h, int x )
- { return _cmGrSetScrollH( _cmGrHandleToPtr(h), x ); }
-
- int cmGrScrollH( cmGrH_t h )
- { return _cmGrScrollH( _cmGrHandleToPtr(h) ); }
-
- bool cmGrSetScrollV( cmGrH_t h, int y )
- { return _cmGrSetScrollV( _cmGrHandleToPtr(h), y ); }
-
- int cmGrScrollV( cmGrH_t h )
- { return _cmGrScrollV( _cmGrHandleToPtr(h) ); }
-
- bool cmGrSelectExtents( cmGrH_t h, cmGrVExt_t* vext, cmGrPExt_t* pext )
- {
- cmGrPPt_t pt0,pt1;
- cmGr_t* p = _cmGrHandleToPtr(h);
-
- if( !p->selValidFl )
- {
- cmGrVExtSetNull(vext);
- cmGrPExtSetNull(pext);
- return false;
- }
-
- if( p->sel0Pt.x == p->sel1Pt.x && p->sel0Pt.y == p->sel1Pt.y )
- cmGrVExtSet(vext, p->sel0Pt.x, p->sel0Pt.y, 0, 0);
- else
- {
- cmGrV_t gx0=p->sel0Pt.x, gy0=p->sel0Pt.y;
- cmGrV_t gx1=p->sel1Pt.x, gy1=p->sel1Pt.y;
-
- if( cmIsFlag(p->cfgFlags,kSelectHorzGrFl) )
- {
- gy0 = cmGrVExtMaxY(&p->rootObj->wext);
- gy1 = cmGrVExtMinY(&p->rootObj->wext);
- }
- else
- if( cmIsFlag(p->cfgFlags,kSelectVertGrFl ) )
- {
- gx0 = cmGrVExtMinX(&p->rootObj->wext);
- gx1 = cmGrVExtMaxX(&p->rootObj->wext);
- }
-
- cmGrVExtSetD(vext,cmMin(gx0,gx1),cmMin(gy0,gy1),cmMax(gx0,gx1),cmMax(gy0,gy1));
-
- }
-
- _cmGrXY_VtoP(p, p->rootObj, vext->loc.x, vext->loc.y, &pt0 );
- _cmGrXY_VtoP(p, p->rootObj, vext->loc.x + vext->sz.w, vext->loc.y + vext->sz.h, &pt1 );
-
- cmGrPExtSetD(pext,cmMin(pt0.x,pt1.x),cmMin(pt0.y,pt1.y),cmMax(pt0.x,pt1.x),cmMax(pt0.y,pt1.y));
-
- //printf("%f %f %f %f\n",vext->loc.x,vext->loc.y,vext->sz.w,vext->sz.h);
- //printf("%i %i %i %i\n",pext->loc.x,pext->loc.y,pext->sz.w,pext->sz.h);
- return true;
- }
-
- void cmGrSetSelectPoints( cmGrH_t h, const cmGrVPt_t* pt0, const cmGrVPt_t* pt1 )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
- _cmGrSetSelectPoints(p,pt0,pt1);
- }
-
- void cmGrSelectPoints( cmGrH_t h, cmGrVPt_t* pt0, cmGrVPt_t* pt1 )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
-
- if( pt0 != NULL )
- *pt0 = p->sel0Pt;
-
- if( pt1 != NULL )
- *pt1 = p->sel1Pt;
- }
-
-
- void cmGrZoom( cmGrH_t h, unsigned flags )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
- bool hfl = cmIsFlag(flags,kXAxisGrFl);
- bool vfl = cmIsFlag(flags,kYAxisGrFl);
- bool inFl = cmIsFlag(flags,kZoomInGrFl);
- bool allFl = cmIsFlag(flags,kShowAllGrFl);
- double magn = 3.0;
- double ratio = inFl ? 1.0/magn : magn;
- cmGrVPt_t c;
- cmGrVExt_t z;
- cmGrVExt_t v;
- cmGrVExt_t w;
-
- cmGrVExtSetNull(&z);
- cmGrViewExtents( h,&v); // get the current view extents
- cmGrObjWorldExt( cmGrRootObjH(h),&w); // get the current world extents
-
-
- // if show all was requested ...
- if( allFl )
- {
-
- // .. then the world ext's become the view extents.
- cmGrVExtSetD(&z,
- hfl ? cmGrVExtMinX(&w) : cmGrVExtMinX(&v),
- vfl ? cmGrVExtMinY(&w) : cmGrVExtMinY(&v),
- hfl ? cmGrVExtMaxX(&w) : cmGrVExtMaxX(&v),
- vfl ? cmGrVExtMaxY(&w) : cmGrVExtMaxY(&v));
- }
- else
- {
-
- // if the selection flag is not set or the selection pt/area is not valid
- if( cmIsNotFlag(flags,kSelectGrFl) || p->selValidFl==false )
- {
- // center the zoom on the current view
- c.x = v.loc.x + v.sz.w/2;
- c.y = v.loc.y + v.sz.h/2;
- }
- else
- {
- // if the selection area is a point
- if( p->sel0Pt.x == p->sel1Pt.x && p->sel0Pt.y == p->sel1Pt.y )
- {
- // center the zoom on the current view
- c.x = p->sel0Pt.x;
- c.y = p->sel1Pt.y;
- }
- else
- {
- cmGrPExt_t dum;
-
- // The selection area exist - make it the new view area
- // Note that f the selection area is greater than the
- // current view area then this may not be a magnification.
- cmGrSelectExtents(h,&z,&dum);
- }
- }
-
- // if no zoom area has been created then create
- // one centered on 'c'.
- if( cmGrVExtIsNull(&z) )
- {
- cmGrVExt_t t;
- cmGrV_t zw = v.sz.w * ratio / 2;
- cmGrV_t zh = v.sz.h * ratio / 2;
-
- cmGrVExtSetD(&t,
- hfl ? c.x-zw : cmGrVExtMinX(&v),
- vfl ? c.y-zh : cmGrVExtMinY(&v),
- hfl ? c.x+zw : cmGrVExtMaxX(&v),
- vfl ? c.y+zh : cmGrVExtMaxY(&v));
-
- cmGrVExtIntersect(&z,&t,&w);
-
- }
- }
-
- //cmGrVExtPrint("w:",&w);
- //cmGrVExtPrint("z:",&z);
- //cmGrVExtPrint("v:",&v);
- assert( cmGrVExtIsNorm(&z));
-
- cmGrSetViewExtentsE(h,&z);
- }
-
- void cmGrSetSync( cmGrH_t h, cmGrH_t syncGrH, unsigned flags )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
-
- // attempt to locate syncGrH on the sync list
- cmGrSync_t* pp = NULL;
- cmGrSync_t* sp = p->syncs;
- for(; sp != NULL; sp=sp->link)
- {
- if( cmHandlesAreEqual(sp->grH,syncGrH) )
- break;
-
- pp = sp;
- }
-
- // if the handle was not found ...
- if( sp == NULL )
- {
- // ... and a valid flags value was given ...
- if( flags != 0 )
- {
- // ... then create a new sync target.
- sp = cmMemAllocZ(cmGrSync_t,1);
- sp->grH = syncGrH;
- sp->flags = flags;
- sp->link = p->syncs;
- p->syncs = sp;
- }
- }
- else // ... otherwise syncGrH is already a sync target
- {
- // if flags is non-zero then update the target sync flags
- if( flags != 0 )
- sp->flags = flags;
- else
- {
- // otherwise delete the sync recd assoc'd with syncGrH
- if( pp == NULL )
- p->syncs = sp->link;
- else
- pp->link = sp->link;
-
- cmMemFree(sp);
- }
- }
- }
-
- void cmGrReport( cmGrH_t h,cmRpt_t* r )
- {
- cmGr_t* p = _cmGrHandleToPtr(h);
- cmRpt_t* rpt = r==NULL ? p->err.rpt : r;
-
- cmRptPrintf(rpt,"cfg:0x%x state:0x%x\n",p->cfgFlags, p->stateFlags);
- //cmRptPrintf(rpt,"World: "); cmGrVExtMaxXpt(&p->wext,rpt); cmRptPrintf(rpt,"\n");
- cmRptPrintf(rpt,"View: "); cmGrVExtRpt(&p->vext,rpt); cmRptPrintf(rpt,"\n");
- cmRptPrintf(rpt,"Phys: "); cmGrPExtRpt(&p->pext,rpt); cmRptPrintf(rpt,"\n");
-
- _cmGrObjReportR(p,p->rootObj,rpt);
- }
|