123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670 |
- #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
- };
-
- // cmGrObj_t state flags
- enum
- {
- kDirtyObjFl = 0x0001, // this object is dirty
- kFocusObjFl = 0x0002, // this object has focus
- kSelectObjFl = 0x0004, // this object is selected
- kPhysRelObjFl = 0x0008, // use physical instead of virtual offset
-
- };
-
- typedef struct cmGrObj_str
- {
- unsigned id; //
- unsigned cfgFlags; //
- unsigned stateFlags; //
- 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, 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,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 cfg:0x%x state:0x%x\n",op->id,op->cfgFlags,op->stateFlags);
-
- _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->cfgFlags = flags;
- op->stateFlags = 0;
- 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, 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 );
- }
-
-
- }
-
- // vx,vy are in the same coord's as op->vext
- cmGrObj_t* _cmGrFindObjRec( cmGr_t* p, cmGrObj_t* op, 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,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,px,py,vx,vy)) != NULL )
- rop = top;
- }
-
- if( op->rsib != NULL )
- if((top = _cmGrFindObjRec(p,op->rsib,px,py,vx,vy)) != NULL )
- rop = top;
-
-
- return rop;
- }
-
-
- bool cmGrEvent( 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,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);
-
- 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);
- }
- }
-
- 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);
- }
|