libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cmGr.c 73KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881
  1. #include "cmGlobal.h"
  2. #include "cmFloatTypes.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmCtx.h"
  6. #include "cmMem.h"
  7. #include "cmMallocDebug.h"
  8. #include "cmLinkedHeap.h"
  9. #include "cmGr.h"
  10. #include "cmGrDevCtx.h"
  11. // cmGr_t state flags
  12. enum
  13. {
  14. kDirtyGrFl = 0x01 // the cmGr object is dirty
  15. };
  16. // cmGrObj_t state flags
  17. enum
  18. {
  19. kDirtyObjFl = 0x0001, // this object is dirty
  20. kFocusObjFl = 0x0002, // this object has focus
  21. kSelectObjFl = 0x0004, // this object is selected
  22. kPhysRelObjFl = 0x0008, // use physical instead of virtual offset
  23. };
  24. typedef struct cmGrObj_str
  25. {
  26. unsigned id; //
  27. unsigned cfgFlags; //
  28. unsigned stateFlags; //
  29. cmGrObjFunc_t f; //
  30. cmGrVExt_t wext; // world coord's contained within this object (children of this object are contained by these extents)
  31. unsigned wlimitFlags; // kLeftGrFl | kRightGrFl | kTopGrFl | kBottomGrFl
  32. cmGrVExt_t wlimitExt; // limit extents for wext (ext's with set flags always define the associated wext value)
  33. struct cmGrObj_str* parent;
  34. struct cmGrObj_str* children;
  35. struct cmGrObj_str* rsib;
  36. struct cmGrObj_str* lsib;
  37. } cmGrObj_t;
  38. typedef struct cmGrSync_str
  39. {
  40. cmGrH_t grH;
  41. unsigned flags; // kGrXXXSyncGrFl
  42. struct cmGrSync_str* link;
  43. } cmGrSync_t;
  44. typedef struct cmGrColorMap_str
  45. {
  46. const cmChar_t* label;
  47. unsigned id;
  48. cmGrColor_t* map;
  49. unsigned cnt;
  50. struct cmGrColorMap_str* link;
  51. } cmGrColorMap_t;
  52. typedef struct cmGrKeyMap_str
  53. {
  54. unsigned idx; // index of this keymap entry (used for debugging)
  55. char ascii; // printible ascii character associated with this keycode or 0 if not printable
  56. cmGrKeyCodeId_t keycode; // cmGr keycode
  57. } cmGrKeyMap_t;
  58. typedef struct
  59. {
  60. cmCtx_t ctx;
  61. cmErr_t err;
  62. unsigned id; // user definable id
  63. cmLHeapH_t lhH;
  64. unsigned cfgFlags;
  65. unsigned stateFlags;
  66. cmGrObj_t* msDnObj; // obj under last ms dn
  67. cmGrPPt_t msDnPPt; // last ms dn loc'n
  68. cmGrVPt_t msDnVPt; // last ms dn in same coords as msDnObj->vext (inside msDnObj->parent->wxt)
  69. cmGrVPt_t msVPt; // cur ms pt in same coords as msDnObj->vext (inside msDnObj->parent->wxt)
  70. cmGrVSz_t msDnVOffs;
  71. bool selValidFl;
  72. cmGrVPt_t sel0Pt;
  73. cmGrVPt_t sel1Pt;
  74. cmGrVPt_t localPt;
  75. cmGrVPt_t globalPt;
  76. cmGrVExt_t vext; // view virtual extents
  77. cmGrPExt_t pext; // view physical extents
  78. cmGrObj_t* objs; // object tree
  79. cmGrObj_t* rootObj; // current root object
  80. unsigned char* img; // temporary image inversion buffer
  81. cmGrCbFunc_t cbFunc; //
  82. void* cbArg; //
  83. cmGrSync_t* syncs; //
  84. cmGrColorMap_t* maps; // color maps
  85. } cmGr_t;
  86. cmGrH_t cmGrNullHandle = cmSTATIC_NULL_HANDLE;
  87. cmGrObjH_t cmGrObjNullHandle = cmSTATIC_NULL_HANDLE;
  88. cmGrKeyMap_t _cmGrKeyMap[] =
  89. {
  90. { 0, 0, 0 },
  91. { 1, 0, 0 },
  92. { 2, 0, 0 },
  93. { 3, 0, 0 },
  94. { 4, 0, 0 },
  95. { 5, 0, kHomeGrId},
  96. { 6, 0, kPageUpGrId},
  97. { 7, 0, kEndGrId},
  98. { 8, 8, kBackSpaceGrId },
  99. { 9, 9, kTabGrId },
  100. { 10, 0, kPageDownGrId},
  101. { 11, 0, kLeftGrId},
  102. { 12, 0, kUpGrId},
  103. { 13, 13, kEnterGrId },
  104. { 14, 0, kRightGrId},
  105. { 15, 0, kDownGrId},
  106. { 16, 0, kInsertGrId},
  107. { 17, 0, kPrintGrId},
  108. { 18, 0, kScrollLockGrId},
  109. { 19, 0, kPauseGrId},
  110. { 20, 0, kMenuGrId},
  111. { 21, 0, kLShiftGrId},
  112. { 22, 0, kRShiftGrId},
  113. { 23, 0, kLCtrlGrId},
  114. { 24, 0, kRCtrlGrId},
  115. { 25, 0, kLAltGrId},
  116. { 26, 0, kRAltGrId},
  117. { 27, 27, kEscapeGrId },
  118. { 28, 0, kLSuperGrId},
  119. { 29, 0, kRSuperGrId},
  120. { 30, 0, kNumLockGrId},
  121. { 31, 0, kCapsLockGrId},
  122. { 32, 32, kSpaceGrId },
  123. { 33, 33, kExclMarkGrId },
  124. { 34, 34, kDQuoteGrId },
  125. { 35, 35, kPoundGrId },
  126. { 36, 36, kDollarGrId },
  127. { 37, 37, kPercentGrId },
  128. { 38, 38, kAmpersandGrId },
  129. { 39, 39, kApostropheGrId },
  130. { 40, 40, kLParenGrId },
  131. { 41, 41, kRParenGrId },
  132. { 42, 42, kAsteriskGrId },
  133. { 43, 43, kPlusGrId },
  134. { 44, 44, kCommaGrId },
  135. { 45, 45, kHyphenGrId },
  136. { 46, 46, kPeriodGrId },
  137. { 47, 47, kForwardSlashGrId },
  138. { 48, 48, k0GrId },
  139. { 49, 49, k1GrId },
  140. { 50, 50, k2GrId },
  141. { 51, 51, k3GrId },
  142. { 52, 52, k4GrId },
  143. { 53, 53, k5GrId },
  144. { 54, 54, k6GrId },
  145. { 55, 55, k7GrId },
  146. { 56, 56, k8GrId },
  147. { 57, 57, k9GrId },
  148. { 58, 58, kColonGrId },
  149. { 59, 59, kSemiColonGrId },
  150. { 60, 60, kLesserGrId },
  151. { 61, 61, kEqualGrId },
  152. { 62, 62, kGreaterGrId },
  153. { 63, 63, kQMarkGrId },
  154. { 64, 64, kAtGrId },
  155. { 65, 65, kA_GrId },
  156. { 66, 66, kB_GrId },
  157. { 67, 67, kC_GrId },
  158. { 68, 68, kD_GrId },
  159. { 69, 69, kE_GrId },
  160. { 70, 70, kF_GrId },
  161. { 71, 71, kG_GrId },
  162. { 72, 72, kH_GrId },
  163. { 73, 73, kI_GrId },
  164. { 74, 74, kJ_GrId },
  165. { 75, 75, kK_GrId },
  166. { 76, 76, kL_GrId },
  167. { 77, 77, kM_GrId },
  168. { 78, 78, kN_GrId },
  169. { 79, 79, kO_GrId },
  170. { 80, 80, kP_GrId },
  171. { 81, 81, kQ_GrId },
  172. { 82, 82, kR_GrId },
  173. { 83, 83, kS_GrId },
  174. { 84, 84, kT_GrId },
  175. { 85, 85, kU_GrId },
  176. { 86, 86, kV_GrId },
  177. { 87, 87, kW_GrId },
  178. { 88, 88, kX_GrId },
  179. { 89, 89, kY_GrId },
  180. { 90, 90, kZ_GrId },
  181. { 91, 91, kLBracketGrId },
  182. { 92, 92, kBackSlashGrId },
  183. { 93, 93, kRBracketGrId },
  184. { 94, 94, kCaretGrId },
  185. { 95, 95, kUnderScoreGrId },
  186. { 96, 96, kAccentGrId },
  187. { 97, 97, ka_GrId },
  188. { 98, 98, kb_GrId },
  189. { 99, 99, kc_GrId },
  190. { 100, 100, kd_GrId },
  191. { 101, 101, ke_GrId },
  192. { 102, 102, kf_GrId },
  193. { 103, 103, kg_GrId },
  194. { 104, 104, kh_GrId },
  195. { 105, 105, ki_GrId },
  196. { 106, 106, kj_GrId },
  197. { 107, 107, kk_GrId },
  198. { 108, 108, kl_GrId },
  199. { 109, 109, km_GrId },
  200. { 110, 110, kn_GrId },
  201. { 111, 111, ko_GrId },
  202. { 112, 112, kp_GrId },
  203. { 113, 113, kq_GrId },
  204. { 114, 114, kr_GrId },
  205. { 115, 115, ks_GrId },
  206. { 116, 116, kt_GrId },
  207. { 117, 117, ku_GrId },
  208. { 118, 118, kv_GrId },
  209. { 119, 119, kw_GrId },
  210. { 120, 120, kx_GrId },
  211. { 121, 121, ky_GrId },
  212. { 122, 122, kz_GrId },
  213. { 123, 123, kLBraceGrId },
  214. { 124, 124, kPipeGrId },
  215. { 125, 125, kRBraceGrId },
  216. { 126, 126, kTildeGrId },
  217. { 127, 127, kDeleteGrId },
  218. { 128, 42, kNP_MultGrId },
  219. { 129, 43, kNP_PlusGrId },
  220. { 130, 45, kNP_MinusGrId },
  221. { 131, 46, kNP_DecPtGrId},
  222. { 132, 47, kNP_DivGrId},
  223. { 133, 48, kNP_0GrId},
  224. { 134, 49, kNP_1GrId},
  225. { 135, 50, kNP_2GrId},
  226. { 136, 51, kNP_3GrId},
  227. { 137, 52, kNP_4GrId},
  228. { 138, 53, kNP_5GrId},
  229. { 139, 54, kNP_6GrId},
  230. { 140, 55, kNP_7GrId},
  231. { 141, 56, kNP_8GrId},
  232. { 142, 57, kNP_9GrId},
  233. { 143, 61, kNP_EqualGrId},
  234. { 144, 13, kNP_EnterGrId},
  235. { 145, 0, kFunc_1GrId},
  236. { 146, 0, kFunc_2GrId},
  237. { 147, 0, kFunc_3GrId},
  238. { 148, 0, kFunc_4GrId},
  239. { 149, 0, kFunc_5GrId},
  240. { 150, 0, kFunc_6GrId},
  241. { 151, 0, kFunc_7GrId},
  242. { 152, 0, kFunc_8GrId},
  243. { 153, 0, kFunc_9GrId},
  244. { 154, 0, kFunc_10GrId},
  245. { 155, 0, kFunc_11GrId},
  246. { 156, 0, kFunc_12GrId},
  247. { 157, 0, kBrightUpGrId},
  248. { 158, 0, kBrightDnGrId},
  249. { 159, 0, kAudio_PrevGrId},
  250. { 160, 0, kAudio_PlayGrId},
  251. { 161, 0, kAudio_NextGrId},
  252. { 162, 0, kAudio_MuteGrId},
  253. { 163, 0, kAudio_DnGrId },
  254. { 164, 0, kAudio_UpGrId },
  255. { 165, 0, kEjectGrId },
  256. { cmInvalidIdx, 0, cmInvalidId}
  257. };
  258. void _cmGrKeyMapValidate()
  259. {
  260. unsigned i;
  261. for(i=0; _cmGrKeyMap[i].idx != cmInvalidIdx; ++i)
  262. {
  263. assert( _cmGrKeyMap[i].idx == i );
  264. }
  265. }
  266. cmGrKeyMap_t* _cmGrFindKeyMap( unsigned keycode )
  267. {
  268. // printable ascii codes match their indexes
  269. if( 32 <= keycode && keycode <= 126 )
  270. return _cmGrKeyMap + keycode;
  271. unsigned i;
  272. for(i=0; i<32; ++i)
  273. if( _cmGrKeyMap[i].keycode == keycode )
  274. return _cmGrKeyMap + i;
  275. for(i=127; _cmGrKeyMap[i].idx != cmInvalidIdx; ++i)
  276. if( _cmGrKeyMap[i].keycode == keycode )
  277. return _cmGrKeyMap + i;
  278. assert(0);
  279. return NULL;
  280. }
  281. bool _cmGrSetViewExtents( cmGr_t* p, cmGrV_t minx, cmGrV_t miny, cmGrV_t maxx, cmGrV_t maxy );
  282. bool _cmGrSetViewExtentsE( cmGr_t* p, const cmGrVExt_t* e )
  283. { return _cmGrSetViewExtents(p, cmGrVExtMinX(e), cmGrVExtMinY(e), cmGrVExtMaxX(e), cmGrVExtMaxY(e) ); }
  284. //====================================================================================================
  285. // Expand cmGrVExt_t e0 to hold e1.
  286. // Return true if e0 is actually changed.
  287. bool cmGrVExtExpandToContain(cmGrVExt_t* e0, const cmGrVExt_t* e1)
  288. {
  289. bool fl = false;
  290. if( cmGrVExtIsNullOrEmpty(e0) )
  291. {
  292. *e0 = *e1;
  293. return true;
  294. }
  295. assert( cmGrVExtIsNorm(e0) && cmGrVExtIsNorm(e1) );
  296. // min-x
  297. if( cmGrVExtMinX(e0) > cmGrVExtMinX(e1) )
  298. {
  299. cmGrVExtSetMinX(e0, cmGrVExtMinX(e1));
  300. fl = true;
  301. }
  302. // min-y
  303. if( cmGrVExtMinY(e0) > cmGrVExtMinY(e1) )
  304. {
  305. cmGrVExtSetMinY(e0, cmGrVExtMinY(e1));
  306. fl = true;
  307. }
  308. // max-y
  309. if( cmGrVExtMaxY(e0) < cmGrVExtMaxY(e1) )
  310. {
  311. cmGrVExtSetMaxY(e0, cmGrVExtMaxY(e1));
  312. fl = true;
  313. }
  314. // max-x
  315. if( cmGrVExtMaxX(e0) < cmGrVExtMaxX(e1) )
  316. {
  317. cmGrVExtSetMaxX(e0, cmGrVExtMaxX(e1));
  318. fl = true;
  319. }
  320. return fl;
  321. }
  322. bool cmGrVExtContain( const cmGrVExt_t* e0, cmGrVExt_t* e1 )
  323. {
  324. bool fl = false;
  325. assert( cmGrVExtIsNorm(e0) && cmGrVExtIsNorm(e1) );
  326. // e1 must be able to fit inside e0
  327. assert( e1->sz.w <= e0->sz.w && e1->sz.h <= e0->sz.h );
  328. // if left edge of e1 is to left of e0
  329. if( cmGrVExtMinX(e1) < cmGrVExtMinX(e0) )
  330. {
  331. cmGrVExtSetMinX(e1,cmGrVExtMinX(e0));
  332. fl = true;
  333. }
  334. // if right edge of e1 is to right of e0
  335. if( cmGrVExtMaxX(e1) > cmGrVExtMaxX(e0) )
  336. {
  337. cmGrVExtSetMaxX(e1,cmGrVExtMaxX(e0));
  338. fl = true;
  339. }
  340. // if the bottom edge of e1 is below the bottom edge of e0
  341. if( cmGrVExtMinY(e1) < cmGrVExtMinY(e0) )
  342. {
  343. cmGrVExtSetMinY(e1,cmGrVExtMinY(e0));
  344. fl = true;
  345. }
  346. // if top edge of e1 is above the top edge of e0
  347. if( cmGrVExtMaxY(e1) > cmGrVExtMaxY(e0) )
  348. {
  349. cmGrVExtSetMaxY(e1,cmGrVExtMaxY(e0));
  350. fl = true;
  351. }
  352. return fl;
  353. }
  354. void cmGrPExtIntersect( cmGrPExt_t* r, const cmGrPExt_t* e0, const cmGrPExt_t* e1 )
  355. {
  356. if( cmGrPExtR(e0) < cmGrPExtL(e1) || cmGrPExtL(e0) > cmGrPExtR(e1)
  357. || cmGrPExtB(e0) < cmGrPExtT(e1) || cmGrPExtT(e0) > cmGrPExtB(e1) )
  358. {
  359. cmGrPExtSetEmpty(r);
  360. return;
  361. }
  362. cmGrPExtSetD(r,
  363. cmMax( cmGrPExtL(e0), cmGrPExtL(e1) ),
  364. cmMax( cmGrPExtT(e0), cmGrPExtT(e1) ),
  365. cmMin( cmGrPExtR(e0), cmGrPExtR(e1) ),
  366. cmMin( cmGrPExtB(e0), cmGrPExtB(e1) ) );
  367. }
  368. void cmGrVExtIntersect( cmGrVExt_t* r, const cmGrVExt_t* e0, const cmGrVExt_t* e1 )
  369. {
  370. if( cmGrVExtMaxX(e0) < cmGrVExtMinX(e1) || cmGrVExtMinX(e0) > cmGrVExtMaxX(e1)
  371. || cmGrVExtMaxY(e0) < cmGrVExtMinY(e1) || cmGrVExtMinY(e0) > cmGrVExtMaxY(e1) )
  372. {
  373. cmGrVExtSetEmpty(r);
  374. return;
  375. }
  376. cmGrVExtSetD(r,
  377. cmMax( cmGrVExtMinX(e0), cmGrVExtMinX(e1) ),
  378. cmMax( cmGrVExtMinY(e0), cmGrVExtMinY(e1) ),
  379. cmMin( cmGrVExtMaxX(e0), cmGrVExtMaxX(e1) ),
  380. cmMin( cmGrVExtMaxY(e0), cmGrVExtMaxY(e1) ) );
  381. }
  382. //====================================================================================================
  383. //====================================================================================================
  384. cmGr_t* _cmGrHandleToPtr( cmGrH_t h )
  385. {
  386. cmGr_t* p = (cmGr_t*)h.h;
  387. assert( p != NULL );
  388. return p;
  389. }
  390. cmGrObj_t* _cmGrObjHandleToPtr( cmGrObjH_t h )
  391. {
  392. cmGrObj_t* p = (cmGrObj_t*)h.h;
  393. assert( p != NULL );
  394. return p;
  395. }
  396. //====================================================================================================
  397. unsigned cmGrColorMapCount( cmGrH_t grH )
  398. {
  399. cmGr_t* p = _cmGrHandleToPtr(grH);
  400. cmGrColorMap_t* cmp = p->maps;
  401. unsigned n = 0;
  402. for(; cmp!=NULL; cmp=cmp->link)
  403. ++n;
  404. return n;
  405. }
  406. cmGrColorMap_t* _cmGrColorMapFromIndex( cmGr_t* p, unsigned idx )
  407. {
  408. unsigned i = 0;
  409. cmGrColorMap_t* cmp = p->maps;
  410. for(; cmp!=NULL; ++i,cmp=cmp->link)
  411. if( i == idx )
  412. break;
  413. return cmp;
  414. }
  415. cmGrColorMap_t* _cmGrColorMapFromId( cmGr_t* p, unsigned id )
  416. {
  417. cmGrColorMap_t* cmp = p->maps;
  418. for(; cmp!=NULL; cmp=cmp->link)
  419. if( cmp->id == id )
  420. break;
  421. return cmp;
  422. }
  423. unsigned cmGrColorMapId( cmGrH_t grH, unsigned mapIdx )
  424. {
  425. cmGr_t* p = _cmGrHandleToPtr(grH);
  426. cmGrColorMap_t* cmp;
  427. if((cmp = _cmGrColorMapFromIndex(p,mapIdx)) == NULL )
  428. return cmInvalidId;
  429. return cmp->id;
  430. }
  431. const cmChar_t* cmGrColorMapLabel( cmGrH_t grH, unsigned id )
  432. {
  433. cmGr_t* p = _cmGrHandleToPtr(grH);
  434. cmGrColorMap_t* cmp;
  435. if((cmp = _cmGrColorMapFromId(p,id)) == NULL )
  436. return NULL;
  437. return cmp->label;
  438. }
  439. unsigned _cmGrColorMapRegister( cmGr_t* p, cmChar_t* label, const cmGrColor_t* array, unsigned cnt )
  440. {
  441. // locate an available id
  442. unsigned id = 0;
  443. while( _cmGrColorMapFromId(p,id)!=NULL )
  444. ++id;
  445. cmGrColorMap_t* cmp = cmLhAllocZ(p->lhH,cmGrColorMap_t,1);
  446. cmp->label = cmLhAllocStr(p->lhH,label);
  447. cmp->id = id;
  448. cmp->map = cmLhAllocZ(p->lhH,cmGrColor_t,cnt);
  449. cmp->cnt = cnt;
  450. cmp->link = p->maps;
  451. p->maps = cmp;
  452. memcpy(cmp->map,array,sizeof(cmGrColor_t)*cnt);
  453. return id;
  454. }
  455. unsigned cmGrColorMapRegister( cmGrH_t grH, cmChar_t* label, const cmGrColor_t* array, unsigned cnt )
  456. { return _cmGrColorMapRegister( _cmGrHandleToPtr(grH),label, array, cnt ); }
  457. cmGrColor_t* cmGrColorMap( cmGrH_t grH, unsigned mapId )
  458. {
  459. cmGr_t* p = _cmGrHandleToPtr(grH);
  460. cmGrColorMap_t* cmp;
  461. if((cmp = _cmGrColorMapFromId(p,mapId)) == NULL )
  462. return NULL;
  463. return cmp->map;
  464. }
  465. unsigned cmGrColorMapEleCount( cmGrH_t grH, unsigned mapId )
  466. {
  467. cmGr_t* p = _cmGrHandleToPtr(grH);
  468. cmGrColorMap_t* cmp;
  469. if((cmp = _cmGrColorMapFromId(p,mapId)) == NULL )
  470. return cmInvalidId;
  471. return cmp->cnt;
  472. }
  473. void _cmGrRgbInitDefaultColorMap( cmGr_t* p )
  474. {
  475. unsigned map[] =
  476. {
  477. 0x000000, // black
  478. 0x00008b, // dark blue
  479. 0x0000ff, // blue
  480. 0x008080, // teal
  481. 0x00ffff, // cyan
  482. 0x00ff7f, // spring green
  483. 0x00ff00, // green
  484. 0x7cfc00, // lawn green
  485. 0xffff00, // yellow
  486. 0xff7f7f, // pink
  487. 0xff00ff, // magenta
  488. 0xff007f, //
  489. 0xff0000, // red
  490. 0x7f0000, //
  491. 0xffffff // white
  492. };
  493. unsigned n = sizeof(map)/sizeof(unsigned);
  494. _cmGrColorMapRegister(p,"default",map,n);
  495. }
  496. //====================================================================================================
  497. // Object Callback Functions
  498. //====================================================================================================
  499. void _cmGrObjSetupFuncArgs( cmGrObjFuncArgs_t* a, cmGr_t* p, cmGrObj_t* op )
  500. {
  501. cmGrH_t h;
  502. cmGrObjH_t oh;
  503. h.h = p;
  504. oh.h = op;
  505. a->ctx = &p->ctx;
  506. a->grH = h;
  507. a->objH = oh;
  508. a->msDnPPt = p->msDnPPt;
  509. a->msDnVPt = p->msDnVPt;
  510. a->msDnVOffs = p->msDnVOffs;
  511. a->msVPt = p->msVPt;
  512. oh.h = p->msDnObj;
  513. a->msDnObjH = oh;
  514. }
  515. cmGrRC_t _cmGrObjCbCreate( cmGr_t* p, cmGrObj_t* op )
  516. {
  517. cmGrRC_t rc = kOkGrRC;
  518. if( op->f.createCbFunc != NULL )
  519. {
  520. cmGrObjFuncArgs_t a;
  521. _cmGrObjSetupFuncArgs(&a,p,op);
  522. a.cbArg = op->f.createCbArg;
  523. if((rc = op->f.createCbFunc(&a)) != kOkGrRC )
  524. rc = cmErrMsg(&p->err,kAppErrGrRC,"An application object (id=%i) failed on 'create'",op->id);
  525. }
  526. return rc;
  527. }
  528. void _cmGrObjCbDestroy( cmGr_t* p, cmGrObj_t* op )
  529. {
  530. if( op->f.destroyCbFunc != NULL )
  531. {
  532. cmGrObjFuncArgs_t a;
  533. _cmGrObjSetupFuncArgs(&a,p,op);
  534. a.cbArg = op->f.destroyCbArg;
  535. op->f.destroyCbFunc(&a);
  536. }
  537. }
  538. void _cmGrObjCbRender( cmGr_t* p, cmGrDcH_t dcH, const cmGrObj_t* op )
  539. {
  540. if( op->f.renderCbFunc != NULL )
  541. {
  542. cmGrObjFuncArgs_t a;
  543. _cmGrObjSetupFuncArgs(&a,p,(cmGrObj_t*)op);
  544. a.cbArg = op->f.renderCbArg;
  545. op->f.renderCbFunc(&a, dcH );
  546. }
  547. }
  548. int _cmGrObjCbDistance( cmGr_t* p, const cmGrObj_t* op, int px, int py )
  549. {
  550. int d = INT_MAX;
  551. if( op->f.distanceCbFunc != NULL )
  552. {
  553. cmGrObjFuncArgs_t a;
  554. _cmGrObjSetupFuncArgs(&a,p,(cmGrObj_t*)op);
  555. a.cbArg = op->f.distanceCbArg;
  556. d = op->f.distanceCbFunc(&a,px,py);
  557. }
  558. return d;
  559. }
  560. bool _cmGrObjCbEvent( cmGr_t* p, cmGrObj_t* op, unsigned flags, unsigned key, int px, int py )
  561. {
  562. bool fl = false;
  563. if( op->f.eventCbFunc != NULL )
  564. {
  565. cmGrObjFuncArgs_t a;
  566. _cmGrObjSetupFuncArgs(&a,p,op);
  567. a.cbArg = op->f.eventCbArg;
  568. fl = op->f.eventCbFunc(&a,flags,key,px,py);
  569. }
  570. return fl;
  571. }
  572. void _cmGrObjCbVExt( cmGr_t* p, const cmGrObj_t* op, cmGrVExt_t* vext )
  573. {
  574. if( op->f.vextCbFunc == NULL )
  575. cmGrVExtSetEmpty(vext);
  576. else
  577. {
  578. cmGrObjFuncArgs_t a;
  579. _cmGrObjSetupFuncArgs(&a,p,(cmGrObj_t*)op);
  580. a.cbArg = op->f.vextCbArg;
  581. op->f.vextCbFunc(&a,vext);
  582. }
  583. }
  584. bool _cmGrObjCbIsInside( cmGr_t* p, const cmGrObj_t* op, unsigned evtFlags, int px, int py, cmGrV_t vx, cmGrV_t vy )
  585. {
  586. bool fl = false;
  587. if( op->f.isInsideCbFunc != NULL )
  588. {
  589. cmGrObjFuncArgs_t a;
  590. _cmGrObjSetupFuncArgs(&a,p,(cmGrObj_t*)op);
  591. a.cbArg = op->f.isInsideCbArg;
  592. fl = op->f.isInsideCbFunc(&a,evtFlags,px,py,vx,vy);
  593. }
  594. return fl;
  595. }
  596. //====================================================================================================
  597. // Object Private Functions
  598. //====================================================================================================
  599. // Return true if pp is an ancestor (parent,grand-parent,great-grand-parent,...) of cp.
  600. bool _cmGrObjIsAncestor( cmGrObj_t* pp, cmGrObj_t* cp )
  601. {
  602. cmGrObj_t* tp = cp->parent;
  603. for(; tp != NULL; tp=tp->parent )
  604. if( tp == pp )
  605. break;
  606. return tp!=NULL;
  607. }
  608. // Append 'op' as the right-most child of 'pp'.
  609. void _cmGrObjAppendChild( cmGrObj_t* pp, cmGrObj_t* cp)
  610. {
  611. cp->parent = pp;
  612. if( pp->children == NULL )
  613. {
  614. pp->children = cp;
  615. cp->lsib = NULL;
  616. cp->rsib = NULL;
  617. }
  618. else
  619. {
  620. cmGrObj_t* op = pp->children;
  621. while( op->rsib != NULL )
  622. op = op->rsib;
  623. op->rsib = cp;
  624. cp->rsib = NULL;
  625. cp->lsib = op;
  626. }
  627. }
  628. // Insert 'op' on the left of 'rp'.
  629. void _cmGrObjInsertOnLeft( cmGrObj_t* op, cmGrObj_t* rp )
  630. {
  631. op->parent = rp->parent;
  632. op->rsib = rp;
  633. op->lsib = rp->lsib;
  634. if( rp->lsib == NULL )
  635. {
  636. assert( rp->parent == rp->parent->children);
  637. rp->parent->children = op;
  638. }
  639. else
  640. {
  641. rp->lsib->rsib = op;
  642. }
  643. rp->lsib = op;
  644. }
  645. // Insert 'op' on the right of 'lp'.
  646. // 'pp' is the parent of 'lp'. 'pp' must be given explicitely to cover
  647. // the case where lp is NULL - in which case the new parent for op
  648. // cannot be determined from lp.
  649. void _cmGrObjInsertOnRight( cmGrObj_t* op, cmGrObj_t* lp, cmGrObj_t* pp )
  650. {
  651. op->parent = pp;
  652. if( lp == NULL )
  653. {
  654. assert( pp != NULL && pp->children==NULL );
  655. pp->children = op;
  656. op->lsib = NULL;
  657. op->rsib = NULL;
  658. return;
  659. }
  660. assert( lp->parent == pp );
  661. op->lsib = lp;
  662. op->rsib = lp->rsib;
  663. if( lp->rsib != NULL )
  664. lp->rsib->lsib = op;
  665. lp->rsib = op;
  666. }
  667. // Unlink 'op' from the tree but leave it's children attached.
  668. void _cmGrObjUnlink( cmGrObj_t * op )
  669. {
  670. if( op->parent != NULL && op->parent->children == op )
  671. op->parent->children = op->parent->children->rsib;
  672. if( op->rsib != NULL )
  673. op->rsib->lsib = op->lsib;
  674. if( op->lsib != NULL )
  675. op->lsib->rsib = op->rsib;
  676. op->parent = NULL;
  677. op->rsib = NULL;
  678. op->lsib = NULL;
  679. }
  680. // Free 'op' and all of its children.
  681. // 'op' must be unlinked before calling this function
  682. cmGrRC_t _cmGrObjFree( cmGr_t* p, cmGrObj_t* op )
  683. {
  684. cmGrRC_t rc = kOkGrRC;
  685. // go to the deepest child
  686. if( op->children != NULL )
  687. if((rc = _cmGrObjFree(p,op->children)) != kOkGrRC )
  688. return rc;
  689. // go right
  690. if( op->rsib != NULL )
  691. if((rc = _cmGrObjFree(p,op->rsib)) != kOkGrRC )
  692. return rc;
  693. // inform the application that we are destroying this object
  694. _cmGrObjCbDestroy(p,op);
  695. _cmGrObjUnlink(op);
  696. cmMemFree(op);
  697. return rc;
  698. }
  699. cmGrRC_t _cmGrObjUnlinkAndFree( cmGr_t* p, cmGrObj_t* op )
  700. {
  701. cmGrRC_t rc = kOkGrRC;
  702. cmGrObj_t* rsib = op->rsib;
  703. cmGrObj_t* par = op->parent;
  704. _cmGrObjUnlink(op);
  705. // if the free fails ...
  706. if((rc = _cmGrObjFree(p,op)) != kOkGrRC )
  707. {
  708. // ... then restore the objects position
  709. if( rsib == NULL )
  710. _cmGrObjInsertOnLeft(op,rsib);
  711. else
  712. _cmGrObjAppendChild(par,op);
  713. }
  714. return rc;
  715. }
  716. // Return kL,T,R,BGrFl indicating which directions of wext are in violation of op->wlimitExt.
  717. // Return's 0 if no limits are in violation
  718. unsigned _cmGrObjWorldLimitsTestViolation( const cmGrObj_t* op, const cmGrVExt_t* wext )
  719. {
  720. unsigned violFlags = 0;
  721. if( cmIsFlag( op->wlimitFlags, kLeftGrFl) )
  722. if( cmGrVExtMinX(wext) < cmGrVExtMinX(&op->wlimitExt) )
  723. violFlags = kLeftGrFl;
  724. if( cmIsFlag( op->wlimitFlags, kTopGrFl) )
  725. if( cmGrVExtMaxY(wext) < cmGrVExtMaxY(&op->wlimitExt) )
  726. violFlags = kTopGrFl;
  727. if( cmIsFlag( op->wlimitFlags, kRightGrFl) )
  728. if( cmGrVExtMaxX(wext) > cmGrVExtMaxX(&op->wlimitExt) )
  729. violFlags = kRightGrFl;
  730. if( cmIsFlag( op->wlimitFlags, kBottomGrFl) )
  731. if( cmGrVExtMinY(wext) > cmGrVExtMinY(&op->wlimitExt) )
  732. violFlags = kBottomGrFl;
  733. return violFlags;
  734. }
  735. // If op has world extent limits then apply them to 'ext'.
  736. // Extent directions in 'ext' which do not have limits in 'op' are unchanged.
  737. // Extent directions in 'ext' which have limits are set to the limit.
  738. void _cmGrObjApplyExtLimits( cmGrObj_t* op, cmGrVExt_t* ext )
  739. {
  740. if( cmIsFlag(op->wlimitFlags,kLeftGrFl) )
  741. cmGrVExtSetMinX(ext,cmGrVExtMinX(&op->wlimitExt));
  742. if( cmIsFlag(op->wlimitFlags,kBottomGrFl) )
  743. cmGrVExtSetMinY(ext,cmGrVExtMinY(&op->wlimitExt));
  744. if( cmIsFlag(op->wlimitFlags,kTopGrFl) )
  745. cmGrVExtSetMaxY(ext,cmGrVExtMaxY(&op->wlimitExt));
  746. if( cmIsFlag(op->wlimitFlags,kRightGrFl) )
  747. cmGrVExtSetMaxX(ext,cmGrVExtMaxX(&op->wlimitExt));
  748. }
  749. // Return the outside extents of the children of 'op'.
  750. // Returns false if there are no children and leaves wext set to NULL.
  751. bool _cmGrObjChildExts( cmGr_t* p, const cmGrObj_t* op, cmGrVExt_t* ext )
  752. {
  753. cmGrVExtSetNull(ext);
  754. op = op->children;
  755. if( op == NULL )
  756. return false;
  757. _cmGrObjCbVExt(p,op,ext);
  758. for(op=op->rsib; op!=NULL; op=op->rsib)
  759. {
  760. cmGrVExt_t e;
  761. _cmGrObjCbVExt(p,op,&e);
  762. cmGrVExtExpandToContain(ext,&e);
  763. }
  764. return true;
  765. }
  766. cmGrRC_t _cmGrObjSetWorldExt( cmGr_t* p, cmGrObj_t* op, const cmGrVExt_t* wext );
  767. // The world extents changed to 'ref_wext' on an object.
  768. // Examine all 'sync'ed' objects and expand them to be contained by 'ref_wext'.
  769. cmGrRC_t _cmGrSyncWorldExtentsExpand( cmGr_t* p, const cmGrVExt_t* ref_wext )
  770. {
  771. cmGrRC_t rc = kOkGrRC;
  772. // apply changes to synch targets
  773. cmGrSync_t* sp = p->syncs;
  774. for(; sp!=NULL; sp=sp->link)
  775. if( cmIsFlag(sp->flags,kWorldSyncGrFl) )
  776. {
  777. // get the target ROOT object
  778. cmGrObj_t* top = _cmGrObjHandleToPtr(cmGrRootObjH(sp->grH));
  779. bool fl = false;
  780. cmGrVExt_t top_wext = top->wext;
  781. //printf("sync %i ",top->id);
  782. //cmGrVExtPrint("top_wext",&top_wext);
  783. //cmGrVExtPrint("ref_wext",ref_wext);
  784. // if horz sync was requested ...
  785. if( !fl && cmIsFlag(sp->flags,kHorzSyncGrFl) )
  786. {
  787. if( cmGrVExtIsNullOrEmpty(&top_wext) )
  788. {
  789. cmGrVExtSetMinX(&top_wext,cmGrVExtMinX(ref_wext));
  790. cmGrVExtSetMaxX(&top_wext,cmGrVExtMaxX(ref_wext));
  791. fl = true;
  792. }
  793. else
  794. {
  795. // ... and the target needs to expand and can expand
  796. if( cmGrVExtMinX(&top_wext) > cmGrVExtMinX(ref_wext) && cmIsNotFlag(top->wlimitFlags,kLeftGrFl) )
  797. {
  798. cmGrVExtSetMinX(&top_wext,cmGrVExtMinX(ref_wext)); // .. expand the view
  799. fl = true;
  800. }
  801. if( cmGrVExtMaxX(&top_wext) < cmGrVExtMaxX(ref_wext) && cmIsNotFlag(top->wlimitFlags,kRightGrFl) )
  802. {
  803. cmGrVExtSetMaxX(&top_wext,cmGrVExtMaxX(ref_wext));
  804. fl = true;
  805. }
  806. }
  807. }
  808. // if vert sync was requested ...
  809. if( !fl && cmIsFlag(sp->flags,kVertSyncGrFl) )
  810. {
  811. if( cmGrVExtIsNullOrEmpty(&top_wext) )
  812. {
  813. cmGrVExtSetMinY(&top_wext,cmGrVExtMinY(ref_wext));
  814. cmGrVExtSetMaxY(&top_wext,cmGrVExtMaxY(ref_wext));
  815. fl = true;
  816. }
  817. else
  818. {
  819. if( cmGrVExtMinY(&top_wext) > cmGrVExtMinY(ref_wext) && cmIsNotFlag(top->wlimitFlags,kBottomGrFl))
  820. {
  821. cmGrVExtSetMinY(&top_wext,cmGrVExtMinY(ref_wext));
  822. fl = true;
  823. }
  824. if( cmGrVExtMaxY(&top_wext) < cmGrVExtMaxY(ref_wext) && cmIsNotFlag(top->wlimitFlags,kTopGrFl) )
  825. {
  826. cmGrVExtSetMaxY(&top_wext,cmGrVExtMaxY(ref_wext));
  827. fl = true;
  828. }
  829. }
  830. }
  831. // If fl is set then top_wext contains an expanded world view
  832. if( fl )
  833. {
  834. //cmGrVExtPrint("out top_wext",&top_wext);
  835. // this call may result in a recursion back into this function
  836. if((rc = _cmGrObjSetWorldExt( _cmGrHandleToPtr(sp->grH), top, &top_wext )) != kOkGrRC )
  837. goto errLabel;
  838. }
  839. }
  840. errLabel:
  841. return rc;
  842. }
  843. cmGrRC_t _cmGrObjSetWorldExt( cmGr_t* p, cmGrObj_t* op, const cmGrVExt_t* wext )
  844. {
  845. cmGrRC_t rc = kOkGrRC;
  846. cmGrVExt_t ce;
  847. cmGrVExt_t we = *wext; // make a copy of the new extents to override the 'const' on 'wext'.
  848. // apply the world ext limits to 'we'.
  849. _cmGrObjApplyExtLimits(op,&we);
  850. assert(cmGrVExtIsNorm(&we)); // assert w/h are positive
  851. // get the extents around all children
  852. if( _cmGrObjChildExts(p, op, &ce ) )
  853. {
  854. // if 'ce' is not entirely inside 'we'
  855. if( cmGrVExtIsExtInside(&we,&ce) == false )
  856. return cmErrMsg(&p->err,kExtsErrGrRC,"The change in world extents would have resulted in child objects outside the requested world extents.");
  857. }
  858. // if world extents are actually changing
  859. if( !cmGrVExtIsEqual(&op->wext,&we) )
  860. {
  861. // update the world extents for this object
  862. op->wext = we;
  863. op->stateFlags = cmSetFlag(op->stateFlags,kDirtyObjFl);
  864. //cmGrVExtPrint(cmTsPrintf("set w: %i ",op->id),&we);
  865. // if this is the root object
  866. if( p->rootObj == op )
  867. {
  868. // this call may result in recursion back into this function
  869. // if two cmGr's are mutual sync targets - an infinite loop
  870. // should be avoided by the cmGrVExtIsEqual() test above.
  871. rc = _cmGrSyncWorldExtentsExpand(p, wext );
  872. }
  873. }
  874. return rc;
  875. }
  876. void _cmGrObjReport( cmGr_t* p, cmGrObj_t* op, cmRpt_t* rpt )
  877. {
  878. cmGrVExt_t vext;
  879. cmRptPrintf(rpt,"id:0x%x cfg:0x%x state:0x%x\n",op->id,op->cfgFlags,op->stateFlags);
  880. _cmGrObjCbVExt( p, op, &vext);
  881. cmGrVExtRpt(&vext,rpt);
  882. cmRptPrintf(rpt,"\n");
  883. }
  884. void _cmGrObjReportR( cmGr_t* p, cmGrObj_t* op, cmRpt_t* rpt)
  885. {
  886. _cmGrObjReport(p, op,rpt);
  887. if( op->children != NULL )
  888. _cmGrObjReport(p,op->children,rpt);
  889. if( op->rsib != NULL )
  890. _cmGrObjReport(p,op->rsib,rpt);
  891. }
  892. //====================================================================================================
  893. cmGrRC_t cmGrObjCreate( cmGrH_t h, cmGrObjH_t* ohp, cmGrObjH_t parentH, cmGrObjFunc_t* f, unsigned id, unsigned flags, const cmGrVExt_t* wext )
  894. {
  895. cmGrRC_t rc;
  896. if((rc = cmGrObjDestroy(h,ohp)) != kOkGrRC )
  897. return rc;
  898. cmGr_t* p = _cmGrHandleToPtr(h);
  899. // allocate the new object
  900. cmGrObj_t* op = cmMemAllocZ(cmGrObj_t,1);
  901. op->id = id;
  902. op->cfgFlags = flags;
  903. op->stateFlags = 0;
  904. op->f = *f;
  905. if( wext != NULL )
  906. {
  907. op->wext = *wext;
  908. if( cmGrVExtIsNotNull(wext) )
  909. cmGrVExtNorm(&op->wext);
  910. }
  911. // if an explicit parent was not provided
  912. // then assign the root object as the parent
  913. if( cmGrObjIsValid(h,parentH) == false )
  914. parentH.h = p->rootObj;
  915. // insert the object into the tree
  916. if( cmGrObjIsValid(h,parentH) )
  917. _cmGrObjAppendChild(_cmGrObjHandleToPtr(parentH),op);
  918. else
  919. {
  920. // no root object exits - so make this obj the root
  921. assert(p->objs == NULL );
  922. p->objs = op;
  923. }
  924. ohp->h = op;
  925. // Notify the application that an object was created.
  926. if((rc = _cmGrObjCbCreate(p,op)) != kOkGrRC )
  927. goto errLabel;
  928. if( f->vextCbFunc != NULL )
  929. {
  930. cmGrVExt_t vext;
  931. cmGrVExtSetEmpty(&vext);
  932. // get the local virtual extents for the new object
  933. cmGrObjLocalVExt(h, *ohp, &vext );
  934. if( cmGrVExtIsNotNullOrEmpty(&vext) )
  935. {
  936. // expand the parents world to contain the new object
  937. if( op->parent != NULL )
  938. {
  939. cmGrVExt_t parent_wext = op->parent->wext;
  940. if( cmGrVExtExpandToContain(&parent_wext,&vext) )
  941. _cmGrObjSetWorldExt(p,op->parent,&parent_wext);
  942. assert( cmGrVExtIsExtInside(&op->parent->wext,&vext) );
  943. }
  944. // if cfg'd to expand the view to contain new objects then do so here
  945. if( op->parent!=NULL && op->parent->parent==NULL && cmIsFlag(p->cfgFlags,kExpandViewGrFl) && cmGrVExtIsExtInside(&p->vext,&vext) == false )
  946. {
  947. cmGrVExt_t v = p->vext;
  948. if( cmGrVExtExpandToContain(&v,&vext) )
  949. _cmGrSetViewExtentsE(p,&v);
  950. assert( cmGrVExtIsExtInside(&op->parent->wext,&p->vext));
  951. }
  952. // if the new object is inside the view extents then mark
  953. // the object as dirty
  954. if( cmGrVExtIsExtInside(&p->vext,&vext) )
  955. op->stateFlags = cmSetFlag(op->stateFlags,kDirtyObjFl);
  956. }
  957. }
  958. errLabel:
  959. if( rc != kOkGrRC )
  960. cmGrObjDestroy(h,ohp);
  961. return rc;
  962. }
  963. cmGrRC_t _cmGrObjDestroy( cmGr_t* p, cmGrObj_t* op )
  964. {
  965. if( op == NULL )
  966. return kOkGrRC;
  967. return _cmGrObjUnlinkAndFree( p, op);
  968. }
  969. cmGrRC_t cmGrObjDestroy( cmGrH_t h, cmGrObjH_t* ohp )
  970. {
  971. cmGrRC_t rc = kOkGrRC;
  972. if( ohp==NULL || cmGrObjIsValid(h,*ohp) == false )
  973. return kOkGrRC;
  974. cmGrObj_t* op = _cmGrObjHandleToPtr(*ohp);
  975. if((rc = _cmGrObjDestroy(_cmGrHandleToPtr(h),op)) != kOkGrRC )
  976. return rc;
  977. ohp->h = NULL;
  978. return rc;
  979. }
  980. cmGrRC_t cmGrObjIsValid( cmGrH_t h, cmGrObjH_t oh )
  981. { return oh.h != NULL; }
  982. unsigned cmGrObjId( cmGrObjH_t oh )
  983. { return _cmGrObjHandleToPtr(oh)->id; }
  984. void cmGrObjSetId( cmGrObjH_t oh, unsigned id )
  985. {
  986. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  987. op->id = id;
  988. }
  989. cmGrObjH_t cmGrObjParent( cmGrObjH_t oh )
  990. {
  991. cmGrObjH_t poh;
  992. poh.h = _cmGrObjHandleToPtr(oh)->parent;
  993. return poh;
  994. }
  995. cmGrRC_t cmGrObjSetWorldExt( cmGrH_t h, cmGrObjH_t oh, const cmGrVExt_t* wext )
  996. {
  997. cmGr_t* p = _cmGrHandleToPtr(h);
  998. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  999. return _cmGrObjSetWorldExt(p,op,wext);
  1000. }
  1001. void cmGrObjWorldExt( cmGrObjH_t oh, cmGrVExt_t* wext )
  1002. {
  1003. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1004. *wext = op->wext;
  1005. }
  1006. cmGrRC_t cmGrObjSetWorldLimitExt( cmGrH_t h, cmGrObjH_t oh, const cmGrVExt_t* vext, unsigned limitFlags )
  1007. {
  1008. cmGrRC_t rc;
  1009. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1010. // store the current world extents
  1011. cmGrVExt_t lext = op->wlimitExt;
  1012. unsigned lfl = op->wlimitFlags;
  1013. // set the new limits
  1014. op->wlimitExt = *vext;
  1015. op->wlimitFlags = limitFlags;
  1016. cmGrVExtNorm(&op->wlimitExt);
  1017. // attempt to apply the current world extents with the new limits
  1018. // (this may fail if their are child objects out of range of the new extents)
  1019. if( cmGrVExtIsNotNull(&op->wext ) )
  1020. {
  1021. if((rc = cmGrObjSetWorldExt(h,oh,&op->wext)) != kOkGrRC )
  1022. {
  1023. // we failed - restore the old limits
  1024. op->wlimitExt = lext;
  1025. op->wlimitFlags = lfl;
  1026. }
  1027. }
  1028. return rc;
  1029. }
  1030. void cmGrObjWorldLimitExt( cmGrObjH_t oh, cmGrVExt_t* vext, unsigned* limitFlags )
  1031. {
  1032. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1033. if( vext != NULL )
  1034. *vext = op->wlimitExt;
  1035. if( limitFlags != NULL )
  1036. *limitFlags = op->wlimitFlags;
  1037. }
  1038. void cmGrObjSetCreateCb( cmGrObjH_t oh, cmGrCreateObjCb_t cbFunc, void* cbArg )
  1039. {
  1040. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1041. op->f.createCbFunc = cbFunc;
  1042. op->f.createCbArg = cbArg;
  1043. }
  1044. void cmGrObjSetDestroyCb( cmGrObjH_t oh, cmGrDestroyObjCb_t cbFunc, void* cbArg )
  1045. {
  1046. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1047. op->f.destroyCbFunc = cbFunc;
  1048. op->f.destroyCbArg = cbArg;
  1049. }
  1050. void cmGrObjSetRenderCb( cmGrObjH_t oh, cmGrRenderObjCb_t cbFunc, void* cbArg )
  1051. {
  1052. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1053. op->f.renderCbFunc = cbFunc;
  1054. op->f.renderCbArg = cbArg;
  1055. }
  1056. void cmGrObjSetDistanceCb( cmGrObjH_t oh, cmGrDistanceObjCb_t cbFunc, void* cbArg )
  1057. {
  1058. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1059. op->f.distanceCbFunc = cbFunc;
  1060. op->f.distanceCbArg = cbArg;
  1061. }
  1062. void cmGrObjSetEventCb( cmGrObjH_t oh, cmGrEventObjCb_t cbFunc, void* cbArg )
  1063. {
  1064. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1065. op->f.eventCbFunc = cbFunc;
  1066. op->f.eventCbArg = cbArg;
  1067. }
  1068. void cmGrObjSetVExtCb( cmGrObjH_t oh, cmGrVExtObjCb_t cbFunc, void* cbArg )
  1069. {
  1070. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1071. op->f.vextCbFunc = cbFunc;
  1072. op->f.vextCbArg = cbArg;
  1073. }
  1074. void cmGrObjSetIsInsideCb( cmGrObjH_t oh, cmGrIsInsideObjCb_t cbFunc, void* cbArg )
  1075. {
  1076. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1077. op->f.isInsideCbFunc = cbFunc;
  1078. op->f.isInsideCbArg = cbArg;
  1079. }
  1080. cmGrCreateObjCb_t cmGrObjCreateCbFunc( cmGrObjH_t oh )
  1081. {
  1082. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1083. return op->f.createCbFunc;
  1084. }
  1085. cmGrDestroyObjCb_t cmGrObjDestroyCbFunc( cmGrObjH_t oh )
  1086. {
  1087. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1088. return op->f.destroyCbFunc;
  1089. }
  1090. cmGrRenderObjCb_t cmGrObjRenderCbFunc( cmGrObjH_t oh )
  1091. {
  1092. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1093. return op->f.renderCbFunc;
  1094. }
  1095. cmGrDistanceObjCb_t cmGrObjDistanceCbFunc( cmGrObjH_t oh )
  1096. {
  1097. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1098. return op->f.distanceCbFunc;
  1099. }
  1100. cmGrEventObjCb_t cmGrObjEventCbFunc( cmGrObjH_t oh )
  1101. {
  1102. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1103. return op->f.eventCbFunc;
  1104. }
  1105. cmGrVExtObjCb_t cmGrObjVExtCbFunc( cmGrObjH_t oh )
  1106. {
  1107. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1108. return op->f.vextCbFunc;
  1109. }
  1110. cmGrIsInsideObjCb_t cmGrObjIsInsideCbFunc( cmGrObjH_t oh )
  1111. {
  1112. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1113. return op->f.isInsideCbFunc;
  1114. }
  1115. void* cmGrObjCreateCbArg( cmGrObjH_t oh )
  1116. {
  1117. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1118. return op->f.createCbArg;
  1119. }
  1120. void* cmGrObjDestroyCbArg( cmGrObjH_t oh )
  1121. {
  1122. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1123. return op->f.createCbArg;
  1124. }
  1125. void* cmGrObjRenderCbArg( cmGrObjH_t oh )
  1126. {
  1127. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1128. return op->f.destroyCbArg;
  1129. }
  1130. void* cmGrObjDistanceCbArg( cmGrObjH_t oh )
  1131. {
  1132. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1133. return op->f.distanceCbArg;
  1134. }
  1135. void* cmGrObjEventCbArg( cmGrObjH_t oh )
  1136. {
  1137. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1138. return op->f.eventCbArg;
  1139. }
  1140. void* cmGrObjVExtCbArg( cmGrObjH_t oh )
  1141. {
  1142. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1143. return op->f.vextCbArg;
  1144. }
  1145. void* cmGrObjIsInsideCbArg( cmGrObjH_t oh )
  1146. {
  1147. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1148. return op->f.isInsideCbArg;
  1149. }
  1150. void cmGrObjLocalVExt( cmGrH_t h, cmGrObjH_t oh, cmGrVExt_t* vext )
  1151. {
  1152. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1153. return _cmGrObjCbVExt( _cmGrHandleToPtr(h), op, vext);
  1154. }
  1155. cmGrObj_t* _cmGrObjIdToHandle( cmGrObj_t* op, unsigned id )
  1156. {
  1157. cmGrObj_t* rp = NULL;
  1158. if( op->id == id )
  1159. return op;
  1160. if( op->children != NULL )
  1161. if((rp = _cmGrObjIdToHandle(op->children,id)) != NULL )
  1162. return rp;
  1163. if( op->rsib != NULL )
  1164. if((rp = _cmGrObjIdToHandle(op->rsib,id)) != NULL )
  1165. return rp;
  1166. return NULL;
  1167. }
  1168. cmGrObjH_t cmGrObjIdToHandle( cmGrH_t h, unsigned id )
  1169. {
  1170. cmGr_t* p = _cmGrHandleToPtr(h);
  1171. cmGrObjH_t oh = cmGrObjNullHandle;
  1172. cmGrObj_t* op;
  1173. if((op = _cmGrObjIdToHandle(p->objs,id)) != NULL )
  1174. oh.h = op;
  1175. return oh;
  1176. }
  1177. // Move 'aoH' such that it is above 'boH' in the z-order.
  1178. // This means that 'boH' must be drawn before 'aoH'.
  1179. // This algorithm is designed to not break object hierarchies
  1180. // when moving objects. It achieves this by only moving
  1181. // the ancestor objects of boH and aoH at the level where
  1182. // they do not share a common ancestor.
  1183. void cmGrObjDrawAbove( cmGrObjH_t boH, cmGrObjH_t aoH )
  1184. {
  1185. cmGrObj_t* bp = _cmGrObjHandleToPtr(boH);
  1186. cmGrObj_t* ap = _cmGrObjHandleToPtr(aoH);
  1187. cmGrObj_t* rp = bp;
  1188. // set rp to the root object
  1189. while( rp->parent != NULL )
  1190. rp=rp->parent;
  1191. while(1)
  1192. {
  1193. cmGrObj_t* a[] = {NULL,NULL};
  1194. cmGrObj_t* bpp = NULL;
  1195. cmGrObj_t* app = NULL;
  1196. unsigned i = 0;
  1197. rp = rp->children;
  1198. for(; rp!=NULL; rp=rp->rsib)
  1199. {
  1200. if( bp==rp || _cmGrObjIsAncestor(rp,bp) )
  1201. {
  1202. assert( a[i]==NULL );
  1203. bpp = rp;
  1204. a[i++] = rp;
  1205. if( i==2 )
  1206. break;
  1207. }
  1208. if( ap==rp || _cmGrObjIsAncestor(rp,ap) )
  1209. {
  1210. assert( a[i]==NULL );
  1211. app = rp;
  1212. a[i++] = rp;
  1213. if( i==2 )
  1214. break;
  1215. }
  1216. }
  1217. assert( rp != NULL && i==2 );
  1218. // bpp and app share the same ancestor - keep looking
  1219. // for the level where the they do not share same ancestor
  1220. if( bpp == app )
  1221. rp = bpp;
  1222. else
  1223. {
  1224. // if bp is not already being drawn before ap
  1225. if( a[0] != bpp )
  1226. {
  1227. _cmGrObjUnlink(app);
  1228. _cmGrObjInsertOnRight(app,bpp,bpp->parent);
  1229. }
  1230. return;
  1231. }
  1232. }
  1233. }
  1234. void cmGrObjReport( cmGrH_t h, cmGrObjH_t oh, cmRpt_t* rpt )
  1235. {
  1236. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1237. _cmGrObjReport(_cmGrHandleToPtr(h),op,rpt);
  1238. }
  1239. void cmGrObjReportR( cmGrH_t h, cmGrObjH_t oh, cmRpt_t* rpt )
  1240. {
  1241. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1242. _cmGrObjReportR(_cmGrHandleToPtr(h),op,rpt);
  1243. }
  1244. //====================================================================================================
  1245. //====================================================================================================
  1246. #define _cmGr_X_VperP(p) (p->vext.sz.w / (p->pext.sz.w-1) )
  1247. #define _cmGr_Y_VperP(p) (p->vext.sz.h / (p->pext.sz.h-1) )
  1248. #define _cmGr_X_PperV(p) ((p->pext.sz.w-1) / p->vext.sz.w )
  1249. #define _cmGr_Y_PperV(p) ((p->pext.sz.h-1) / p->vext.sz.h )
  1250. int _cmGr_X_VtoP( cmGr_t* p, cmGrV_t vx )
  1251. { return p->pext.loc.x + lround( (vx - p->vext.loc.x) * _cmGr_X_PperV(p)); }
  1252. int _cmGr_Y_VtoP(cmGr_t* p, cmGrV_t vy )
  1253. { return p->pext.loc.y + (p->pext.sz.h-1) + lround(-(vy - p->vext.loc.y) * _cmGr_Y_PperV(p) ); }
  1254. cmGrV_t _cmGr_X_PtoV( cmGr_t* p, int px )
  1255. { return p->vext.loc.x + (px - p->pext.loc.x) * _cmGr_X_VperP(p); }
  1256. cmGrV_t _cmGr_Y_PtoV( cmGr_t* p, int py )
  1257. { return p->vext.loc.y + (p->pext.loc.y + (p->pext.sz.h-1) - py) * _cmGr_Y_VperP(p); }
  1258. #define _cmGrParentToLocalX( op, vext, x ) (op)->wext.loc.x + ((x) - (vext).loc.x) * (op)->wext.sz.w / (vext).sz.w
  1259. #define _cmGrParentToLocalY( op, vext, y ) (op)->wext.loc.y + ((y) - (vext).loc.y) * (op)->wext.sz.h / (vext).sz.h
  1260. #define _cmGrLocalToParentX( op, vext, x ) (vext).loc.x + ((x) - (op)->wext.loc.x) * (vext).sz.w / (op)->wext.sz.w
  1261. #define _cmGrLocalToParentY( op, vext, y ) (vext).loc.y + ((y) - (op)->wext.loc.y) * (vext).sz.h / (op)->wext.sz.h
  1262. // On input x,y are in the same coord's as op->vext.
  1263. // On output pt is converted to op's internal coord system (i.e. the pt is inside op->wext)
  1264. // Using pt as the src and dst is always safe. (i.e. _cmGrLocalToParent(p,op,pt->x,pt->y,pt) is safe.)
  1265. bool _cmGrParentToLocal( cmGr_t* p, cmGrObj_t* op, cmGrV_t x, cmGrV_t y, cmGrVPt_t* pt )
  1266. {
  1267. cmGrVExt_t vext;
  1268. _cmGrObjCbVExt(p,op,&vext);
  1269. if( cmGrVExtIsNullOrEmpty(&vext) )
  1270. return false;
  1271. pt->x = _cmGrParentToLocalX( op, vext, x);
  1272. pt->y = _cmGrParentToLocalY( op, vext, y );
  1273. //pt->x = op->wext.loc.x + (x - vext.loc.x) * op->wext.sz.w / vext.sz.w;
  1274. //pt->y = op->wext.loc.y + (y - vext.loc.y) * op->wext.sz.h / vext.sz.h;
  1275. return true;
  1276. }
  1277. // On input x,y are in the same coords as op->wext.
  1278. // On output pt is converted to be in the same coord's as op->vext (i.e.the pt is inside op->parent->wext)
  1279. // Using pt as the src and dst is always safe. (i.e. _cmGrLocalToParent(p,op,pt->x,pt->y,pt) is safe.)
  1280. void _cmGrLocalToParent( cmGr_t* p, cmGrObj_t* op, cmGrV_t x, cmGrV_t y, cmGrVPt_t* pt )
  1281. {
  1282. cmGrVExt_t vext;
  1283. _cmGrObjCbVExt(p,op,&vext);
  1284. pt->x = _cmGrLocalToParentX(op,vext, x);
  1285. pt->y = _cmGrLocalToParentY(op,vext, y);
  1286. //pt->x = vext.loc.x + (x - op->wext.loc.x) * vext.sz.w / op->wext.sz.w;
  1287. //pt->y = vext.loc.y + (y - op->wext.loc.y) * vext.sz.h / op->wext.sz.h;
  1288. }
  1289. // On input x is in coord's inside op->wext.
  1290. // Return is in phys coord's
  1291. int _cmGrX_VtoP( cmGr_t* p, cmGrObj_t* op, cmGrV_t x )
  1292. {
  1293. cmGrVExt_t vext;
  1294. for(; op->parent != NULL; op=op->parent )
  1295. {
  1296. _cmGrObjCbVExt(p,op,&vext);
  1297. x = _cmGrLocalToParentX(op->parent,vext,x);
  1298. }
  1299. return _cmGr_X_VtoP(p,x);
  1300. }
  1301. // On input y is in coord's inside op->wext.
  1302. // Return is in phys coord's
  1303. int _cmGrY_VtoP( cmGr_t* p, cmGrObj_t* op, cmGrV_t y )
  1304. {
  1305. cmGrVExt_t vext;
  1306. for(; op->parent != NULL; op=op->parent )
  1307. {
  1308. _cmGrObjCbVExt(p,op,&vext);
  1309. y = _cmGrLocalToParentY(op->parent,vext,y);
  1310. }
  1311. return _cmGr_Y_VtoP(p,y);
  1312. }
  1313. // On input x,y are coord's inside op->wext.
  1314. // On output rp is in physical coord's
  1315. void _cmGrXY_VtoP( cmGr_t* p, cmGrObj_t* op, cmGrV_t x, cmGrV_t y, cmGrPPt_t* rp )
  1316. {
  1317. cmGrVPt_t pt;
  1318. cmGrVPtSet(&pt,x,y);
  1319. for(; op->parent != NULL; op=op->parent )
  1320. _cmGrLocalToParent(p,op->parent,pt.x,pt.y,&pt);
  1321. rp->x = _cmGr_X_VtoP(p,pt.x);
  1322. rp->y = _cmGr_Y_VtoP(p,pt.y);
  1323. }
  1324. // pt is converted from the root obj coord system to be in the same coord's
  1325. // as op->vext (inside of op->parent->wext)
  1326. void _cmGrXY_GlobalToLocal( cmGr_t* p, cmGrObj_t* op, cmGrVPt_t* pt )
  1327. {
  1328. if( op->parent != NULL )
  1329. _cmGrXY_GlobalToLocal(p,op->parent,pt);
  1330. // convert from parent coord to child coords
  1331. _cmGrParentToLocal(p,op,pt->x,pt->y,pt);
  1332. }
  1333. // On input x,y are in physical coordindates.
  1334. // On output rp is inside op->parent->wext (the same coord's as op->vext)
  1335. void _cmGrXY_PtoV( cmGr_t* p, cmGrObj_t* op, int px, int py, cmGrVPt_t* rp )
  1336. {
  1337. // convert the phys points to points in the root coord system
  1338. rp->x = _cmGr_X_PtoV(p,px);
  1339. rp->y = _cmGr_Y_PtoV(p,py);
  1340. _cmGrXY_GlobalToLocal(p, op, rp );
  1341. }
  1342. int cmGrX_VtoP( cmGrH_t hh, cmGrObjH_t oh, cmGrV_t x )
  1343. {
  1344. cmGr_t* p = _cmGrHandleToPtr(hh);
  1345. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1346. return _cmGrX_VtoP(p,op,x);
  1347. }
  1348. int cmGrY_VtoP( cmGrH_t hh, cmGrObjH_t oh, cmGrV_t y )
  1349. {
  1350. cmGr_t* p = _cmGrHandleToPtr(hh);
  1351. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1352. return _cmGrY_VtoP(p,op,y);
  1353. }
  1354. void cmGrXY_VtoP( cmGrH_t h, cmGrObjH_t oh, cmGrV_t x, cmGrV_t y, cmGrPPt_t* rp )
  1355. { _cmGrXY_VtoP(_cmGrHandleToPtr(h), _cmGrObjHandleToPtr(oh), x, y, rp ); }
  1356. void cmGrXYWH_VtoP( cmGrH_t hh, cmGrObjH_t oh, cmGrV_t x, cmGrV_t y, cmGrV_t w, cmGrV_t h, cmGrPExt_t* pext )
  1357. {
  1358. cmGr_t* p = _cmGrHandleToPtr(hh);
  1359. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1360. cmGrPPt_t pt0,pt1;
  1361. _cmGrXY_VtoP(p,op,x, y, &pt0);
  1362. _cmGrXY_VtoP(p,op,x+w,y+h,&pt1);
  1363. cmGrPExtSetD(pext,pt0.x,pt0.y,pt1.x,pt1.y);
  1364. }
  1365. void cmGrVExt_VtoP( cmGrH_t hh, cmGrObjH_t oh, const cmGrVExt_t* vext, cmGrPExt_t* pext )
  1366. { cmGrXYWH_VtoP(hh,oh,vext->loc.x,vext->loc.y,vext->sz.w,vext->sz.h,pext); }
  1367. void cmGrXY_PtoV( cmGrH_t h, cmGrObjH_t oh, int x, int y, cmGrVPt_t* rp )
  1368. { _cmGrXY_PtoV(_cmGrHandleToPtr(h), _cmGrObjHandleToPtr(oh), x, y, rp ); }
  1369. void cmGrXYWH_PtoV( cmGrH_t hh, cmGrObjH_t oh, int x, int y, int w, int h, cmGrVExt_t* vext )
  1370. {
  1371. cmGr_t* p = _cmGrHandleToPtr(hh);
  1372. cmGrObj_t* op = _cmGrObjHandleToPtr(oh);
  1373. cmGrVPt_t pt0,pt1;
  1374. _cmGrXY_PtoV(p,op,x,y,&pt0);
  1375. _cmGrXY_PtoV(p,op,x+w-1,y+h-1,&pt1);
  1376. cmGrVExtSetD(vext,pt0.x,pt0.y,pt1.x,pt1.y);
  1377. }
  1378. void cmGrPExt_PtoV( cmGrH_t hh, cmGrObjH_t oh, const cmGrPExt_t* pext, cmGrVExt_t* vext )
  1379. { cmGrXYWH_PtoV(hh,oh,pext->loc.x,pext->loc.y,pext->sz.w,pext->sz.h,vext); }
  1380. void cmGrDrawVLine( cmGrH_t h, cmGrDcH_t dcH, cmGrObjH_t oh, cmGrV_t x0, cmGrV_t y0, cmGrV_t x1, cmGrV_t y1 )
  1381. {
  1382. cmGrPPt_t loc0;
  1383. cmGrPPt_t loc1;
  1384. cmGrXY_VtoP(h,oh,x0,y0,&loc0);
  1385. cmGrXY_VtoP(h,oh,x1,y1,&loc1);
  1386. cmGrDcDrawLine(dcH,loc0.x,loc0.y,loc1.x,loc1.y);
  1387. }
  1388. void cmGrDrawVRect( cmGrH_t hh, cmGrDcH_t dcH, cmGrObjH_t oh, cmGrV_t x, cmGrV_t y, cmGrV_t w, cmGrV_t h )
  1389. {
  1390. cmGrDrawVLine(hh,dcH,oh,x, y, x, y+h);
  1391. cmGrDrawVLine(hh,dcH,oh,x, y+h,x+w,y+h);
  1392. cmGrDrawVLine(hh,dcH,oh,x+w,y+h,x+w,y);
  1393. cmGrDrawVLine(hh,dcH,oh,x+w,y, x, y);
  1394. }
  1395. //====================================================================================================
  1396. //====================================================================================================
  1397. cmGrRC_t _cmGrDestroy( cmGr_t* p )
  1398. {
  1399. cmGrRC_t rc = kOkGrRC;
  1400. if( p->objs != NULL )
  1401. {
  1402. // destroy the root object and all of it's children
  1403. if((rc = _cmGrObjDestroy(p,p->objs)) != kOkGrRC )
  1404. return rc;
  1405. }
  1406. cmGrSync_t* sp = p->syncs;
  1407. while( sp != NULL )
  1408. {
  1409. cmGrH_t h;
  1410. h.h = p;
  1411. cmGrSync_t* np = sp->link;
  1412. // break sync with any mutually sync'ed gr's to prevent
  1413. // attempted calls to this gr.
  1414. cmGrSetSync( sp->grH, h, 0 );
  1415. cmMemFree(sp);
  1416. sp = np;
  1417. }
  1418. p->syncs = NULL;
  1419. cmLHeapDestroy(&p->lhH);
  1420. cmMemPtrFree(&p->img);
  1421. cmMemFree(p);
  1422. return rc;
  1423. }
  1424. void _cmGrObjRootVExt( cmGrObjFuncArgs_t* args, cmGrVExt_t* vext )
  1425. {
  1426. // the root object's virtual extent is the same as it's world extent
  1427. cmGrObjWorldExt( args->objH, vext );
  1428. }
  1429. bool _cmGrObjRootIsInside( cmGrObjFuncArgs_t* args, unsigned evtFlags, int px, int py, cmGrV_t vx, cmGrV_t vy )
  1430. {
  1431. cmGrVExt_t vext;
  1432. _cmGrObjRootVExt(args,&vext);
  1433. return cmGrVExtIsXyInside(&vext,vx,vy);
  1434. }
  1435. void _cmGrSetCfgFlags( cmGr_t* p, unsigned flags )
  1436. {
  1437. p->cfgFlags = flags;
  1438. // kSelectHorzFl and kSelectVertFl are mutually exclusive
  1439. if( cmIsFlag(p->cfgFlags,kSelectHorzGrFl) )
  1440. p->cfgFlags = cmClrFlag(p->cfgFlags,kSelectVertGrFl);
  1441. }
  1442. void _cmGrCallback( cmGr_t* p, cmGrCbId_t id, unsigned eventFlags, cmGrKeyCodeId_t keycode )
  1443. {
  1444. if( p->cbFunc != NULL )
  1445. {
  1446. cmGrH_t h;
  1447. h.h = p;
  1448. p->cbFunc(p->cbArg,h,id,eventFlags,keycode);
  1449. }
  1450. }
  1451. cmGrRC_t cmGrCreate( cmCtx_t* ctx, cmGrH_t* hp, unsigned id, unsigned cfgFlags, cmGrCbFunc_t cbFunc, void* cbArg, const cmGrVExt_t* wext )
  1452. {
  1453. cmGrRC_t rc = kOkGrRC;
  1454. cmGrVExt_t null_ext;
  1455. cmGrObjH_t rootObjH = cmGrObjNullHandle;
  1456. if(( rc = cmGrDestroy(hp)) != kOkGrRC )
  1457. return rc;
  1458. // create the cmGr_t
  1459. cmGr_t* p = cmMemAllocZ(cmGr_t,1);
  1460. p->ctx = *ctx;
  1461. p->id = id;
  1462. p->stateFlags = kDirtyGrFl;
  1463. p->cbFunc = cbFunc;
  1464. p->cbArg = cbArg;
  1465. _cmGrSetCfgFlags(p,cfgFlags);
  1466. if( wext == NULL )
  1467. {
  1468. cmGrVExtSetNull(&null_ext);
  1469. wext = &null_ext;
  1470. }
  1471. cmErrSetup(&p->err,&ctx->rpt,"cmGr");
  1472. if( cmLHeapIsValid( p->lhH = cmLHeapCreate(8192,ctx)) == false )
  1473. {
  1474. rc = cmErrMsg(&p->err,kLHeapFailGrRC,"Linked heap create failed.");
  1475. goto errLabel;
  1476. }
  1477. cmGrVExtSetEmpty(&p->vext);
  1478. cmGrPExtSetEmpty(&p->pext);
  1479. hp->h = p;
  1480. // create the root object
  1481. cmGrObjFunc_t funcs;
  1482. memset(&funcs,0,sizeof(funcs));
  1483. funcs.vextCbFunc = _cmGrObjRootVExt;
  1484. funcs.isInsideCbFunc = _cmGrObjRootIsInside;
  1485. if((rc = cmGrObjCreate(*hp, &rootObjH, cmGrObjNullHandle, &funcs, cmInvalidId, 0, wext )) != kOkGrRC )
  1486. {
  1487. rc = cmErrMsg(&p->err,kRootObjCreateFailGrRC,"The root object creation failed.");
  1488. goto errLabel;
  1489. }
  1490. // create the default color map
  1491. _cmGrRgbInitDefaultColorMap(p);
  1492. p->objs = _cmGrObjHandleToPtr(rootObjH);
  1493. p->rootObj = p->objs;
  1494. _cmGrCallback(p,kCreateCbGrId,0,kInvalidKeyCodeGrId);
  1495. errLabel:
  1496. if( rc != kOkGrRC )
  1497. {
  1498. _cmGrDestroy(p);
  1499. hp->h = NULL;
  1500. }
  1501. return rc;
  1502. }
  1503. cmGrRC_t cmGrDestroy( cmGrH_t* hp )
  1504. {
  1505. cmGrRC_t rc = kOkGrRC;
  1506. if( hp==NULL || cmGrIsValid(*hp) == false )
  1507. return kOkGrRC;
  1508. cmGr_t* p = _cmGrHandleToPtr(*hp);
  1509. if((rc = _cmGrDestroy(p)) != kOkGrRC )
  1510. goto errLabel;
  1511. hp->h = NULL;
  1512. errLabel:
  1513. return rc;
  1514. }
  1515. // Destroy all objects (except the root object)
  1516. cmGrRC_t _cmGrObjDestroyAll( cmGr_t* p )
  1517. {
  1518. cmGrObj_t* op = p->objs;
  1519. if( op != NULL )
  1520. {
  1521. op = op->children;
  1522. while( op != NULL )
  1523. {
  1524. cmGrObj_t* np = op->rsib;
  1525. _cmGrObjUnlinkAndFree(p,op);
  1526. op = np;
  1527. }
  1528. }
  1529. return kOkGrRC;
  1530. }
  1531. cmGrRC_t cmGrClear( cmGrH_t h )
  1532. {
  1533. cmGrRC_t rc;
  1534. cmGr_t* p = _cmGrHandleToPtr(h);
  1535. if((rc = _cmGrObjDestroyAll(p)) != kOkGrRC )
  1536. return rc;
  1537. p->rootObj = p->objs;
  1538. p->stateFlags = kDirtyGrFl;
  1539. cmGrVExtSetEmpty(&p->vext);
  1540. p->msDnObj = NULL;
  1541. cmGrPPtSet(&p->msDnPPt,0,0);
  1542. cmGrVPtSet(&p->msDnVPt,0,0);
  1543. cmGrVPtSet(&p->msVPt,0,0);
  1544. cmGrVSzSet(&p->msDnVOffs,0,0);
  1545. p->selValidFl = false;
  1546. cmGrVPtSet(&p->sel0Pt,0,0);
  1547. cmGrVPtSet(&p->sel1Pt,0,0);
  1548. cmGrVPtSet(&p->localPt,0,0);
  1549. cmGrVPtSet(&p->globalPt,0,0);
  1550. cmGrVExtSetNull(&p->rootObj->wext);
  1551. return rc;
  1552. }
  1553. cmGrObjH_t cmGrRootObjH( cmGrH_t h )
  1554. {
  1555. cmGr_t* p = _cmGrHandleToPtr(h);
  1556. cmGrObjH_t oh;
  1557. oh.h = p->rootObj;
  1558. return oh;
  1559. }
  1560. unsigned cmGrCfgFlags( cmGrH_t h )
  1561. {
  1562. cmGr_t* p = _cmGrHandleToPtr(h);
  1563. return p->cfgFlags;
  1564. }
  1565. void cmGrSetCfgFlags( cmGrH_t h, unsigned cfgFlags )
  1566. {
  1567. cmGr_t* p = _cmGrHandleToPtr(h);
  1568. _cmGrSetCfgFlags(p,cfgFlags);
  1569. }
  1570. void _cmGrObjDraw( cmGr_t* p, cmGrObj_t* op, cmGrDcH_t dcH )
  1571. {
  1572. _cmGrObjCbRender(p,dcH,op);
  1573. if( op->children != NULL )
  1574. _cmGrObjDraw(p,op->children,dcH);
  1575. if( op->rsib != NULL )
  1576. _cmGrObjDraw(p,op->rsib,dcH);
  1577. }
  1578. void _cmInvertImage( cmGr_t* p, cmGrDcH_t dcH, const cmGrPExt_t* pext )
  1579. {
  1580. if( cmGrPExtIsNullOrEmpty(pext) )
  1581. return;
  1582. cmGrPExt_t r;
  1583. cmGrPExtIntersect( &r, pext, &p->pext );
  1584. unsigned n = r.sz.w * r.sz.h * 3;
  1585. if( n > 0 )
  1586. {
  1587. unsigned i;
  1588. p->img = cmMemResizeZ(unsigned char,p->img,n);
  1589. //p->dd.read_image(p->ddUser, p->img, r.loc.x, r.loc.y, r.sz.w, r.sz.h );
  1590. cmGrDcReadImage( dcH, p->img, &r );
  1591. for(i=0; i<n; ++i)
  1592. p->img[i] = 255 - p->img[i];
  1593. //p->dd.draw_image(p->ddUser, p->img, r.loc.x, r.loc.y, r.sz.w, r.sz.h);
  1594. cmGrDcDrawImage( dcH, p->img, &r);
  1595. }
  1596. }
  1597. cmGrRC_t cmGrDraw( cmGrH_t h, cmGrDcH_t dcH )
  1598. {
  1599. cmGr_t* p = _cmGrHandleToPtr(h);
  1600. _cmGrObjDraw(p,p->rootObj,dcH);
  1601. cmGrPExt_t pext;
  1602. cmGrVExt_t vext;
  1603. cmGrSelectExtents(h,&vext,&pext);
  1604. if( cmGrPExtIsNotNull(&pext))
  1605. {
  1606. if( pext.sz.w<=1 && pext.sz.h<=1 )
  1607. {
  1608. cmGrPExt_t ipext;
  1609. if( !cmIsFlag(p->cfgFlags,kSelectHorzGrFl) )
  1610. {
  1611. cmGrPExtSet(&ipext,p->pext.loc.x,pext.loc.y,p->pext.sz.w,1);
  1612. _cmInvertImage(p,dcH,&ipext);
  1613. }
  1614. if( !cmIsFlag(p->cfgFlags,kSelectVertGrFl) )
  1615. {
  1616. cmGrPExtSet(&ipext,pext.loc.x,p->pext.loc.y,1,p->pext.sz.h);
  1617. _cmInvertImage(p,dcH,&ipext);
  1618. }
  1619. }
  1620. else
  1621. {
  1622. _cmInvertImage(p,dcH,&pext);
  1623. }
  1624. }
  1625. return kOkGrRC;
  1626. }
  1627. bool _cmGrSetViewExtents( cmGr_t* p, cmGrV_t minx, cmGrV_t miny, cmGrV_t maxx, cmGrV_t maxy )
  1628. {
  1629. cmGrVExt_t vext;
  1630. // load a vext with the new view extents
  1631. cmGrVExtSetD(&vext,minx,miny,maxx,maxy);
  1632. // if the view ext is not changing
  1633. if( cmGrVExtIsEqual(&p->vext,&vext) )
  1634. return false;
  1635. // the root object must exist
  1636. assert( p->rootObj != NULL );
  1637. // the view extents must be in the world extents
  1638. if( cmGrVExtIsExtInside(&p->rootObj->wext,&vext)==false )
  1639. {
  1640. cmErrMsg(&p->err,kInvalidCoordsGrRC,"View extent is not inside the world extents.");
  1641. return false;
  1642. }
  1643. //cmGrVExtPrint("set vw:",&vext);
  1644. p->vext = vext;
  1645. p->stateFlags = cmSetFlag(p->stateFlags,kDirtyGrFl);
  1646. _cmGrCallback(p, kViewExtCbGrId,0,kInvalidKeyCodeGrId );
  1647. // apply changes to synch target
  1648. cmGrSync_t* sp = p->syncs;
  1649. for(; sp!=NULL; sp=sp->link)
  1650. if( cmIsFlag(sp->flags,kViewSyncGrFl) )
  1651. {
  1652. cmGrViewExtents( sp->grH, &vext );
  1653. bool fl = false;
  1654. if( cmIsFlag(sp->flags,kHorzSyncGrFl) )
  1655. {
  1656. fl = true;
  1657. vext.loc.x = p->vext.loc.x;
  1658. vext.sz.w = p->vext.sz.w;
  1659. }
  1660. if( cmIsFlag(sp->flags,kVertSyncGrFl) )
  1661. {
  1662. fl = true;
  1663. vext.loc.y = p->vext.loc.y;
  1664. vext.sz.h = p->vext.sz.h;
  1665. }
  1666. if( fl )
  1667. cmGrSetViewExtentsE( sp->grH, &vext );
  1668. }
  1669. //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 );
  1670. return true;
  1671. }
  1672. bool _cmGrSetScrollH( cmGr_t* p, int x )
  1673. {
  1674. assert( p->rootObj != NULL );
  1675. cmGrV_t vx = p->rootObj->wext.loc.x + (x * p->vext.sz.w / p->pext.sz.w);
  1676. return _cmGrSetViewExtents(p,vx, p->vext.loc.y, vx+p->vext.sz.w, p->vext.loc.y+p->vext.sz.h);
  1677. }
  1678. int _cmGrScrollH( cmGr_t* p )
  1679. { return round((p->vext.loc.x - p->rootObj->wext.loc.x) * p->pext.sz.w / p->vext.sz.w); }
  1680. bool _cmGrSetScrollV( cmGr_t* p, int y )
  1681. {
  1682. assert( p->rootObj != NULL );
  1683. cmGrV_t vy = p->rootObj->wext.loc.y + (y * p->vext.sz.h / p->pext.sz.h);
  1684. return _cmGrSetViewExtents(p, p->vext.loc.x, vy, p->vext.loc.x+p->vext.sz.w, vy + p->vext.sz.h);
  1685. }
  1686. int _cmGrScrollV( cmGr_t* p )
  1687. { return round((p->vext.loc.y - p->rootObj->wext.loc.y) * p->pext.sz.h / p->vext.sz.h); }
  1688. void _cmGrSetSelectPoints(cmGr_t* p, const cmGrVPt_t* pt0, const cmGrVPt_t* pt1)
  1689. {
  1690. bool deltaFl = false;
  1691. p->selValidFl = true;
  1692. if( pt0 != NULL )
  1693. {
  1694. if( cmGrVPtIsNotEqual(&p->sel0Pt,pt0) )
  1695. {
  1696. p->sel0Pt = *pt0;
  1697. deltaFl = true;
  1698. }
  1699. if( pt1 == NULL && cmGrVPtIsNotEqual(&p->sel1Pt,pt0) )
  1700. {
  1701. p->sel1Pt = *pt0;
  1702. deltaFl = true;
  1703. }
  1704. }
  1705. if( pt1 != NULL && cmGrVPtIsNotEqual(&p->sel1Pt,pt1) )
  1706. {
  1707. p->sel1Pt = *pt1;
  1708. deltaFl = true;
  1709. }
  1710. if( !deltaFl )
  1711. return;
  1712. _cmGrCallback(p,kSelectExtCbGrId,0,kInvalidKeyCodeGrId);
  1713. // apply changes to synch target
  1714. cmGrSync_t* sp = p->syncs;
  1715. for(; sp!=NULL; sp=sp->link)
  1716. if( cmIsFlag(sp->flags,kSelectSyncGrFl) )
  1717. {
  1718. cmGrVPt_t pt0, pt1;
  1719. cmGrSelectPoints(sp->grH, &pt0, &pt1 );
  1720. bool fl = false;
  1721. if( cmIsFlag(sp->flags,kHorzSyncGrFl) )
  1722. {
  1723. fl = true;
  1724. pt0.x = p->sel0Pt.x;
  1725. pt1.x = p->sel1Pt.x;
  1726. }
  1727. if( cmIsFlag(sp->flags,kVertSyncGrFl) )
  1728. {
  1729. fl = true;
  1730. pt0.y = p->sel0Pt.y;
  1731. pt1.y = p->sel1Pt.y;
  1732. }
  1733. if( fl )
  1734. cmGrSetSelectPoints( sp->grH, &pt0, &pt1 );
  1735. }
  1736. }
  1737. void _cmGrScrollExtents( cmGr_t* p, cmGrPSz_t* tot, cmGrPSz_t* vis, cmGrPSz_t* max, cmGrPPt_t* pos )
  1738. {
  1739. assert( p->rootObj != NULL );
  1740. if( tot != NULL )
  1741. {
  1742. tot->w = round(p->rootObj->wext.sz.w * p->pext.sz.w / p->vext.sz.w);
  1743. tot->h = round(p->rootObj->wext.sz.h * p->pext.sz.h / p->vext.sz.h);
  1744. }
  1745. if( vis != NULL )
  1746. {
  1747. vis->w = round(p->vext.sz.w * p->pext.sz.w / p->vext.sz.w);
  1748. vis->h = round(p->vext.sz.h * p->pext.sz.h / p->vext.sz.h);
  1749. }
  1750. if( max != NULL )
  1751. {
  1752. max->w = tot->w - vis->w;
  1753. max->h = tot->h - vis->h;
  1754. }
  1755. if( pos != NULL )
  1756. {
  1757. pos->x = _cmGrScrollH(p);
  1758. pos->y = _cmGrScrollV(p);
  1759. }
  1760. }
  1761. // vx,vy are in the same coord's as op->vext
  1762. cmGrObj_t* _cmGrFindObjRec( cmGr_t* p, cmGrObj_t* op, unsigned evtFlags, int px, int py, cmGrV_t vx, cmGrV_t vy )
  1763. {
  1764. cmGrObj_t* rop = NULL;
  1765. cmGrObj_t* top;
  1766. cmGrVExt_t vext;
  1767. // get the location of op inside op->parent->wext
  1768. _cmGrObjCbVExt(p,op,&vext);
  1769. // is vx,vy inside op - this is equiv to: cmGrVExtIsXyInside(&vext,vx,vy)
  1770. if( _cmGrObjCbIsInside(p,op,evtFlags,px,py,vx,vy) )
  1771. rop = op;
  1772. if( op->children != NULL )
  1773. {
  1774. cmGrVPt_t pt;
  1775. if( _cmGrParentToLocal(p,op,vx,vy,&pt) )
  1776. if((top = _cmGrFindObjRec(p,op->children,evtFlags,px,py,vx,vy)) != NULL )
  1777. rop = top;
  1778. }
  1779. if( op->rsib != NULL )
  1780. if((top = _cmGrFindObjRec(p,op->rsib,evtFlags,px,py,vx,vy)) != NULL )
  1781. rop = top;
  1782. return rop;
  1783. }
  1784. cmGrObj_t* _cmGrEventMsDown( cmGr_t* p, unsigned evtFlags, cmGrKeyCodeId_t key, int px, int py, cmGrV_t gx, cmGrV_t gy )
  1785. {
  1786. // store the phys loc. of the ms dn event
  1787. cmGrPPtSet(&p->msDnPPt,px,py);
  1788. // get a pointer to an object
  1789. cmGrObj_t* op = _cmGrFindObjRec(p, p->rootObj, evtFlags, px, py, gx, gy );
  1790. // if the mouse did not go down in an object that accepts mouse down events
  1791. // or the object was a root object
  1792. if( op == NULL || op->parent == NULL )
  1793. return NULL;
  1794. // store the object and coord's where the mouse went down.
  1795. cmGrVExt_t vext;
  1796. p->msDnObj = op; // set the msDnObj
  1797. // convert the phys ms dn point to the virt point inside op->parent.wext
  1798. _cmGrXY_PtoV(p, op->parent, px, py, &p->msDnVPt );
  1799. p->msVPt = p->msDnVPt; // set the current ms virt pt
  1800. p->localPt = p->msDnVPt; // set the current local pt
  1801. // notifiy the app of the local coord's
  1802. _cmGrCallback(p,kLocalPtCbGrId,0,kInvalidKeyCodeGrId);
  1803. // get the ms down obj virt extents
  1804. _cmGrObjCbVExt(p,op,&vext);
  1805. // store the offset from the lower left to the ms dn point
  1806. p->msDnVOffs.w = p->msDnVPt.x - vext.loc.x;
  1807. p->msDnVOffs.h = p->msDnVPt.y - vext.loc.y;
  1808. // notify the object that the mouse went down
  1809. if(_cmGrObjCbEvent(p,op,evtFlags,key,px,py))
  1810. return op;
  1811. return NULL;
  1812. }
  1813. cmGrObj_t* _cmGrEventMsUp( cmGr_t* p, unsigned evtFlags, cmGrKeyCodeId_t key, int px, int py, cmGrV_t gx, cmGrV_t gy )
  1814. {
  1815. bool fl = false;
  1816. cmGrObj_t* op = NULL;
  1817. cmGrPPt_t upPPt;
  1818. cmGrPPtSet(&upPPt,px,py);
  1819. // if a click occured ...
  1820. if( cmGrPPtIsEqual(&p->msDnPPt,&upPPt ) )
  1821. {
  1822. // ... and the click is in a non-root object which accepts click events ...
  1823. if( p->msDnObj!= NULL && p->msDnObj->parent!=NULL && _cmGrObjCbIsInside(p,p->msDnObj,evtFlags | kMsClickGrFl, px, py, gx, gy) )
  1824. {
  1825. // ... then generate a click event
  1826. unsigned newEvtFlags = cmClrFlag(evtFlags,kMsUpGrFl) | kMsClickGrFl;
  1827. fl = _cmGrObjCbEvent(p,p->msDnObj,newEvtFlags,key,px,py);
  1828. op = p->msDnObj;
  1829. }
  1830. else // ... else if the click occurred in the root object
  1831. //if( p->msDnObj==NULL || p->msDnObj->parent==NULL)
  1832. {
  1833. cmGrVPt_t pt;
  1834. cmGrVPtSet(&pt,gx,gy);
  1835. bool shFl = cmIsFlag(evtFlags,kShiftKeyGrFl);
  1836. _cmGrSetSelectPoints( p, shFl ? NULL : &pt, shFl ? &pt : NULL );
  1837. }
  1838. }
  1839. // if op is NULL then there was no-mouse down object to match with
  1840. // this mouse-up event - find an object to match with the mouse-up event
  1841. if( op == NULL )
  1842. op = _cmGrFindObjRec(p, p->rootObj, evtFlags, px, py, gx, gy );
  1843. // if a mouse-up object was found then
  1844. if( op != NULL && op->parent != NULL)
  1845. {
  1846. // notify the object under the mouse that the mouse went up
  1847. if( _cmGrObjCbEvent(p,op,evtFlags,key,px,py) )
  1848. fl = true;
  1849. // convert the phys ms dn point to the virt point inside op->parent.wext
  1850. _cmGrXY_PtoV(p, op->parent, px, py, &p->msVPt );
  1851. }
  1852. _cmGrCallback(p,kFocusCbGrId,0,kInvalidKeyCodeGrId);
  1853. p->msDnObj = NULL;
  1854. return fl ? op : NULL;
  1855. }
  1856. cmGrObj_t* _cmGrEventMsMove( cmGr_t* p, unsigned evtFlags, cmGrKeyCodeId_t key, int px, int py, cmGrV_t gx, cmGrV_t gy )
  1857. {
  1858. bool fl = false;
  1859. cmGrObj_t* op = _cmGrFindObjRec(p, p->rootObj, evtFlags, px, py, gx, gy );
  1860. if( op != NULL && op->parent != NULL )
  1861. {
  1862. fl = _cmGrObjCbEvent(p,op,evtFlags,key,px,py);
  1863. }
  1864. return fl ? op : NULL;
  1865. }
  1866. cmGrObj_t* _cmGrEventMsDrag( cmGr_t* p, unsigned evtFlags, cmGrKeyCodeId_t key, int px, int py, cmGrV_t gx, cmGrV_t gy )
  1867. {
  1868. bool fl = false;
  1869. cmGrObj_t* op = _cmGrFindObjRec(p, p->rootObj, evtFlags, px, py, gx, gy );
  1870. if( op != NULL && op->parent != NULL )
  1871. {
  1872. // set the current virtual point
  1873. _cmGrXY_PtoV(p, p->msDnObj->parent, px, py, &p->msVPt );
  1874. // callback the dragged object
  1875. fl = _cmGrObjCbEvent(p,p->msDnObj,evtFlags,key,px,py);
  1876. // if the px,py is outside the root phys extents then scroll
  1877. if( !cmGrPExtIsXyInside(&p->pext,px,py) )
  1878. {
  1879. bool hFl = false, vFl=false;
  1880. cmGrPSz_t tot,vis,max;
  1881. cmGrPPt_t pos;
  1882. _cmGrScrollExtents(p, &tot, &vis, &max, &pos );
  1883. if( px < cmGrPExtL(&p->pext) )
  1884. hFl = _cmGrSetScrollH(p, cmMax(0, _cmGrScrollH(p) - (cmGrPExtL(&p->pext) - px)));
  1885. else
  1886. if( px > cmGrPExtR(&p->pext) )
  1887. hFl = _cmGrSetScrollH(p, cmMin(max.w, _cmGrScrollH(p) + (px - cmGrPExtR(&p->pext))));
  1888. if( py < cmGrPExtT(&p->pext) )
  1889. vFl = _cmGrSetScrollV(p, cmMax(0, _cmGrScrollV(p) - (cmGrPExtT(&p->pext) - py)));
  1890. else
  1891. if( py > cmGrPExtB(&p->pext) )
  1892. vFl = _cmGrSetScrollV(p, cmMin(max.h, _cmGrScrollV(p) + (py - cmGrPExtB(&p->pext))));
  1893. fl = fl || vFl || hFl;
  1894. }
  1895. }
  1896. return fl ? op : NULL;
  1897. }
  1898. bool cmGrEvent( cmGrH_t h, unsigned evtFlags, cmGrKeyCodeId_t key, int px, int py )
  1899. {
  1900. bool fl = false;
  1901. cmGrObj_t* op = NULL;
  1902. cmGr_t* p = _cmGrHandleToPtr(h);
  1903. cmGrVPtSet(&p->localPt,0,0);
  1904. // convert the phys points to points in the root coord system
  1905. cmGrV_t gx = _cmGr_X_PtoV(p,px);
  1906. cmGrV_t gy = _cmGr_Y_PtoV(p,py);
  1907. // set the global point
  1908. cmGrVPtSet(&p->globalPt,gx,gy);
  1909. // inform the app of the possibly new global point
  1910. _cmGrCallback(p,kGlobalPtCbGrId,0,kInvalidKeyCodeGrId);
  1911. switch( evtFlags & kEvtMask)
  1912. {
  1913. case kKeyUpGrFl:
  1914. _cmGrCallback(p,kKeyUpCbGrId,evtFlags,key);
  1915. break;
  1916. case kKeyDnGrFl:
  1917. _cmGrCallback(p,kKeyDnCbGrId,evtFlags,key);
  1918. break;
  1919. case kMsDownGrFl:
  1920. op = _cmGrEventMsDown(p,evtFlags,key,px,py,gx,gy);
  1921. break;
  1922. case kMsUpGrFl: // handle mouse-up, mouse-click, and focus events
  1923. op = _cmGrEventMsUp(p,evtFlags,key,px,py,gx,gy);
  1924. break;
  1925. case kMsMoveGrFl:
  1926. op = _cmGrEventMsMove(p,evtFlags,key,px,py,gx,gy);
  1927. break;
  1928. case kMsDragGrFl:
  1929. op = _cmGrEventMsDrag(p,evtFlags,key,px,py,gx,gy);
  1930. break;
  1931. }
  1932. if( op != NULL )
  1933. {
  1934. // convert gx,gy to be inside op->wext
  1935. cmGrVPtSet(&p->localPt,gx,gy);
  1936. _cmGrXY_GlobalToLocal(p,op,&p->localPt);
  1937. _cmGrCallback(p,kLocalPtCbGrId,0,kInvalidKeyCodeGrId);
  1938. fl = true;
  1939. }
  1940. return fl;
  1941. }
  1942. bool cmGrEvent1( cmGrH_t h, unsigned flags, cmGrKeyCodeId_t key, int px, int py )
  1943. {
  1944. bool fl = false;
  1945. cmGr_t* p = _cmGrHandleToPtr(h);
  1946. cmGrObj_t* op;
  1947. cmGrVPtSet(&p->localPt,0,0);
  1948. // convert the phys points to points in the root coord system
  1949. cmGrV_t gx = _cmGr_X_PtoV(p,px);
  1950. cmGrV_t gy = _cmGr_Y_PtoV(p,py);
  1951. cmGrVPtSet(&p->globalPt,gx,gy);
  1952. _cmGrCallback(p,kGlobalPtCbGrId,0,kInvalidKeyCodeGrId);
  1953. // find the obj under the mouse
  1954. if((op = _cmGrFindObjRec(p,p->rootObj,flags&kEvtMask,px,py,gx,gy)) != NULL )
  1955. {
  1956. // convert gx,gy to be inside op->wext
  1957. cmGrVPtSet(&p->localPt,gx,gy);
  1958. _cmGrXY_GlobalToLocal(p,op,&p->localPt);
  1959. _cmGrCallback(p,kLocalPtCbGrId,0,kInvalidKeyCodeGrId);
  1960. }
  1961. if( (flags&kEvtMask)==kKeyUpGrFl )
  1962. _cmGrCallback(p,kKeyUpCbGrId,flags,key);
  1963. if( (flags&kEvtMask)==kKeyDnGrFl )
  1964. _cmGrCallback(p,kKeyDnCbGrId,flags,key);
  1965. if( op != NULL )
  1966. {
  1967. switch( flags & kEvtMask )
  1968. {
  1969. case kMsDownGrFl:
  1970. // store the phys loc. of the ms dn event
  1971. cmGrPPtSet(&p->msDnPPt,px,py);
  1972. if( op != NULL )
  1973. {
  1974. // if the click was in not in the root object
  1975. if( op->parent != NULL )
  1976. {
  1977. // store the object and coord's where the mouse went down.
  1978. cmGrVExt_t vext;
  1979. p->msDnObj = op; // set the msDnObj
  1980. // convert the phys ms dn point to the virt point inside op->parent.wext
  1981. _cmGrXY_PtoV(p, op->parent, px, py, &p->msDnVPt );
  1982. // set the current ms virt pt
  1983. p->msVPt = p->msDnVPt;
  1984. // get the ms down obj virt extents
  1985. _cmGrObjCbVExt(p,op,&vext);
  1986. // store the offset from the lower left to the ms dn point
  1987. p->msDnVOffs.w = p->msDnVPt.x - vext.loc.x;
  1988. p->msDnVOffs.h = p->msDnVPt.y - vext.loc.y;
  1989. // notify the object that the mouse went down
  1990. fl = _cmGrObjCbEvent(p,op,flags,key,px,py);
  1991. }
  1992. }
  1993. break;
  1994. case kMsUpGrFl:
  1995. {
  1996. cmGrPPt_t upPPt;
  1997. cmGrPPtSet(&upPPt,px,py);
  1998. bool clickFl = cmGrPPtIsEqual(&p->msDnPPt,&upPPt );
  1999. // if a click occured ...
  2000. if( clickFl )
  2001. {
  2002. // ... and the click is in a non-root object ...
  2003. if( p->msDnObj!= NULL && p->msDnObj->parent!=NULL )
  2004. {
  2005. // ... then generate a click event
  2006. unsigned evtFlags = cmClrFlag(flags,kMsUpGrFl) | kMsClickGrFl;
  2007. fl = _cmGrObjCbEvent(p,op,evtFlags,key,px,py);
  2008. }
  2009. else // ... else if the click occurred in the root object
  2010. {
  2011. cmGrVPt_t pt;
  2012. cmGrVPtSet(&pt,gx,gy);
  2013. bool shFl = cmIsFlag(flags,kShiftKeyGrFl);
  2014. _cmGrSetSelectPoints( p, shFl ? NULL : &pt, shFl ? &pt : NULL );
  2015. }
  2016. }
  2017. // notify the object under the mouse that the mouse went up
  2018. if( _cmGrObjCbEvent(p,op,flags,key,px,py) )
  2019. fl = true;
  2020. _cmGrCallback(p,kFocusCbGrId,0,kInvalidKeyCodeGrId);
  2021. p->msDnObj = NULL;
  2022. }
  2023. break;
  2024. case kMsMoveGrFl:
  2025. fl = _cmGrObjCbEvent(p,op,flags,key,px,py);
  2026. break;
  2027. case kMsWheelGrFl:
  2028. break;
  2029. case kMsDragGrFl:
  2030. if( p->msDnObj != NULL )
  2031. {
  2032. // set the current virtual point
  2033. _cmGrXY_PtoV(p, p->msDnObj->parent, px, py, &p->msVPt );
  2034. // callback the dragged object
  2035. fl = _cmGrObjCbEvent(p,p->msDnObj,flags,key,px,py);
  2036. // if the px,py is outside the root phys extents then scroll
  2037. if( !cmGrPExtIsXyInside(&p->pext,px,py) )
  2038. {
  2039. bool hFl = false, vFl=false;
  2040. cmGrPSz_t tot,vis,max;
  2041. cmGrPPt_t pos;
  2042. cmGrScrollExtents(h, &tot, &vis, &max, &pos );
  2043. if( px < cmGrPExtL(&p->pext) )
  2044. hFl = _cmGrSetScrollH(p, cmMax(0, _cmGrScrollH(p) - (cmGrPExtL(&p->pext) - px)));
  2045. else
  2046. if( px > cmGrPExtR(&p->pext) )
  2047. hFl = _cmGrSetScrollH(p, cmMin(max.w, _cmGrScrollH(p) + (px - cmGrPExtR(&p->pext))));
  2048. if( py < cmGrPExtT(&p->pext) )
  2049. vFl = _cmGrSetScrollV(p, cmMax(0, _cmGrScrollV(p) - (cmGrPExtT(&p->pext) - py)));
  2050. else
  2051. if( py > cmGrPExtB(&p->pext) )
  2052. vFl = _cmGrSetScrollV(p, cmMin(max.h, _cmGrScrollV(p) + (py - cmGrPExtB(&p->pext))));
  2053. fl = fl || vFl || hFl;
  2054. }
  2055. }
  2056. break;
  2057. case kKeyDnGrFl:
  2058. case kKeyUpGrFl:
  2059. //fl = _cmGrObjCbEvent(p,p->msDnObj,flags,key,px,py);
  2060. break;
  2061. }
  2062. }
  2063. return fl;
  2064. }
  2065. bool cmGrIsValid( cmGrH_t h )
  2066. { return h.h != NULL; }
  2067. unsigned cmGrId( cmGrH_t h )
  2068. {
  2069. cmGr_t* p = _cmGrHandleToPtr(h);
  2070. return p->id;
  2071. }
  2072. const cmGrVPt_t* cmGrGlobalPt( cmGrH_t h )
  2073. {
  2074. cmGr_t* p = _cmGrHandleToPtr(h);
  2075. return &p->globalPt;
  2076. }
  2077. const cmGrVPt_t* cmGrLocalPt( cmGrH_t h )
  2078. {
  2079. cmGr_t* p = _cmGrHandleToPtr(h);
  2080. return &p->localPt;
  2081. }
  2082. bool cmGrSetViewExtents( cmGrH_t h, cmGrV_t minx, cmGrV_t miny, cmGrV_t maxx, cmGrV_t maxy )
  2083. { return _cmGrSetViewExtents( _cmGrHandleToPtr(h), minx,miny,maxx,maxy); }
  2084. bool cmGrSetViewExtentsE( cmGrH_t h, const cmGrVExt_t* e )
  2085. { return cmGrSetViewExtents( h, cmGrVExtMinX(e), cmGrVExtMinY(e), cmGrVExtMaxX(e), cmGrVExtMaxY(e) ); }
  2086. void cmGrViewExtents( cmGrH_t h, cmGrVExt_t* vext )
  2087. {
  2088. cmGr_t* p = _cmGrHandleToPtr(h);
  2089. *vext = p->vext;
  2090. //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 );
  2091. //printf("g1:%p %f %f %f %f\n",p,vext->loc.x, vext->loc.y, vext->sz.w, vext->sz.h );
  2092. }
  2093. bool cmGrSetPhysExtents( cmGrH_t hh, int x, int y, int w, int h )
  2094. {
  2095. cmGr_t* p = _cmGrHandleToPtr(hh);
  2096. cmGrPExt_t pext;
  2097. cmGrPExtSet(&pext,x,y,w,h);
  2098. assert( cmGrPExtIsNull(&pext)==false );
  2099. if( cmGrPExtIsEqual(&pext,&p->pext) )
  2100. return false;
  2101. p->pext = pext;
  2102. p->stateFlags = cmSetFlag(p->stateFlags,kDirtyGrFl);
  2103. _cmGrCallback(p,kPhysExtCbGrId,0,kInvalidKeyCodeGrId);
  2104. //cmGrPExtPrint("pext",&p->pext);
  2105. return true;
  2106. }
  2107. bool cmGrSetPhysExtentsE(cmGrH_t h, const cmGrPExt_t* e )
  2108. { return cmGrSetPhysExtents(h, cmGrPExtL(e), cmGrPExtT(e), cmGrPExtW(e), cmGrPExtH(e) ); }
  2109. void cmGrPhysExtents( cmGrH_t h, cmGrPExt_t* exts )
  2110. {
  2111. cmGr_t* p = _cmGrHandleToPtr(h);
  2112. *exts = p->pext;
  2113. }
  2114. void cmGrScrollExtents( cmGrH_t h, cmGrPSz_t* tot, cmGrPSz_t* vis, cmGrPSz_t* max, cmGrPPt_t* pos )
  2115. {
  2116. cmGr_t* p = _cmGrHandleToPtr(h);
  2117. _cmGrScrollExtents(p,tot,vis,max,pos);
  2118. }
  2119. bool cmGrSetScrollH( cmGrH_t h, int x )
  2120. { return _cmGrSetScrollH( _cmGrHandleToPtr(h), x ); }
  2121. int cmGrScrollH( cmGrH_t h )
  2122. { return _cmGrScrollH( _cmGrHandleToPtr(h) ); }
  2123. bool cmGrSetScrollV( cmGrH_t h, int y )
  2124. { return _cmGrSetScrollV( _cmGrHandleToPtr(h), y ); }
  2125. int cmGrScrollV( cmGrH_t h )
  2126. { return _cmGrScrollV( _cmGrHandleToPtr(h) ); }
  2127. bool cmGrSelectExtents( cmGrH_t h, cmGrVExt_t* vext, cmGrPExt_t* pext )
  2128. {
  2129. cmGrPPt_t pt0,pt1;
  2130. cmGr_t* p = _cmGrHandleToPtr(h);
  2131. if( !p->selValidFl )
  2132. {
  2133. cmGrVExtSetNull(vext);
  2134. cmGrPExtSetNull(pext);
  2135. return false;
  2136. }
  2137. if( p->sel0Pt.x == p->sel1Pt.x && p->sel0Pt.y == p->sel1Pt.y )
  2138. cmGrVExtSet(vext, p->sel0Pt.x, p->sel0Pt.y, 0, 0);
  2139. else
  2140. {
  2141. cmGrV_t gx0=p->sel0Pt.x, gy0=p->sel0Pt.y;
  2142. cmGrV_t gx1=p->sel1Pt.x, gy1=p->sel1Pt.y;
  2143. if( cmIsFlag(p->cfgFlags,kSelectHorzGrFl) )
  2144. {
  2145. gy0 = cmGrVExtMaxY(&p->rootObj->wext);
  2146. gy1 = cmGrVExtMinY(&p->rootObj->wext);
  2147. }
  2148. else
  2149. if( cmIsFlag(p->cfgFlags,kSelectVertGrFl ) )
  2150. {
  2151. gx0 = cmGrVExtMinX(&p->rootObj->wext);
  2152. gx1 = cmGrVExtMaxX(&p->rootObj->wext);
  2153. }
  2154. cmGrVExtSetD(vext,cmMin(gx0,gx1),cmMin(gy0,gy1),cmMax(gx0,gx1),cmMax(gy0,gy1));
  2155. }
  2156. _cmGrXY_VtoP(p, p->rootObj, vext->loc.x, vext->loc.y, &pt0 );
  2157. _cmGrXY_VtoP(p, p->rootObj, vext->loc.x + vext->sz.w, vext->loc.y + vext->sz.h, &pt1 );
  2158. cmGrPExtSetD(pext,cmMin(pt0.x,pt1.x),cmMin(pt0.y,pt1.y),cmMax(pt0.x,pt1.x),cmMax(pt0.y,pt1.y));
  2159. //printf("%f %f %f %f\n",vext->loc.x,vext->loc.y,vext->sz.w,vext->sz.h);
  2160. //printf("%i %i %i %i\n",pext->loc.x,pext->loc.y,pext->sz.w,pext->sz.h);
  2161. return true;
  2162. }
  2163. void cmGrSetSelectPoints( cmGrH_t h, const cmGrVPt_t* pt0, const cmGrVPt_t* pt1 )
  2164. {
  2165. cmGr_t* p = _cmGrHandleToPtr(h);
  2166. _cmGrSetSelectPoints(p,pt0,pt1);
  2167. }
  2168. void cmGrSelectPoints( cmGrH_t h, cmGrVPt_t* pt0, cmGrVPt_t* pt1 )
  2169. {
  2170. cmGr_t* p = _cmGrHandleToPtr(h);
  2171. if( pt0 != NULL )
  2172. *pt0 = p->sel0Pt;
  2173. if( pt1 != NULL )
  2174. *pt1 = p->sel1Pt;
  2175. }
  2176. void cmGrZoom( cmGrH_t h, unsigned flags )
  2177. {
  2178. cmGr_t* p = _cmGrHandleToPtr(h);
  2179. bool hfl = cmIsFlag(flags,kXAxisGrFl);
  2180. bool vfl = cmIsFlag(flags,kYAxisGrFl);
  2181. bool inFl = cmIsFlag(flags,kZoomInGrFl);
  2182. bool allFl = cmIsFlag(flags,kShowAllGrFl);
  2183. double magn = 3.0;
  2184. double ratio = inFl ? 1.0/magn : magn;
  2185. cmGrVPt_t c;
  2186. cmGrVExt_t z;
  2187. cmGrVExt_t v;
  2188. cmGrVExt_t w;
  2189. cmGrVExtSetNull(&z);
  2190. cmGrViewExtents( h,&v); // get the current view extents
  2191. cmGrObjWorldExt( cmGrRootObjH(h),&w); // get the current world extents
  2192. // if show all was requested ...
  2193. if( allFl )
  2194. {
  2195. // .. then the world ext's become the view extents.
  2196. cmGrVExtSetD(&z,
  2197. hfl ? cmGrVExtMinX(&w) : cmGrVExtMinX(&v),
  2198. vfl ? cmGrVExtMinY(&w) : cmGrVExtMinY(&v),
  2199. hfl ? cmGrVExtMaxX(&w) : cmGrVExtMaxX(&v),
  2200. vfl ? cmGrVExtMaxY(&w) : cmGrVExtMaxY(&v));
  2201. }
  2202. else
  2203. {
  2204. // if the selection flag is not set or the selection pt/area is not valid
  2205. if( cmIsNotFlag(flags,kSelectGrFl) || p->selValidFl==false )
  2206. {
  2207. // center the zoom on the current view
  2208. c.x = v.loc.x + v.sz.w/2;
  2209. c.y = v.loc.y + v.sz.h/2;
  2210. }
  2211. else
  2212. {
  2213. // if the selection area is a point
  2214. if( p->sel0Pt.x == p->sel1Pt.x && p->sel0Pt.y == p->sel1Pt.y )
  2215. {
  2216. // center the zoom on the current view
  2217. c.x = p->sel0Pt.x;
  2218. c.y = p->sel1Pt.y;
  2219. }
  2220. else
  2221. {
  2222. cmGrPExt_t dum;
  2223. // The selection area exist - make it the new view area
  2224. // Note that f the selection area is greater than the
  2225. // current view area then this may not be a magnification.
  2226. cmGrSelectExtents(h,&z,&dum);
  2227. }
  2228. }
  2229. // if no zoom area has been created then create
  2230. // one centered on 'c'.
  2231. if( cmGrVExtIsNull(&z) )
  2232. {
  2233. cmGrVExt_t t;
  2234. cmGrV_t zw = v.sz.w * ratio / 2;
  2235. cmGrV_t zh = v.sz.h * ratio / 2;
  2236. cmGrVExtSetD(&t,
  2237. hfl ? c.x-zw : cmGrVExtMinX(&v),
  2238. vfl ? c.y-zh : cmGrVExtMinY(&v),
  2239. hfl ? c.x+zw : cmGrVExtMaxX(&v),
  2240. vfl ? c.y+zh : cmGrVExtMaxY(&v));
  2241. cmGrVExtIntersect(&z,&t,&w);
  2242. }
  2243. }
  2244. //cmGrVExtPrint("w:",&w);
  2245. //cmGrVExtPrint("z:",&z);
  2246. //cmGrVExtPrint("v:",&v);
  2247. assert( cmGrVExtIsNorm(&z));
  2248. cmGrSetViewExtentsE(h,&z);
  2249. }
  2250. void cmGrSetSync( cmGrH_t h, cmGrH_t syncGrH, unsigned flags )
  2251. {
  2252. cmGr_t* p = _cmGrHandleToPtr(h);
  2253. // attempt to locate syncGrH on the sync list
  2254. cmGrSync_t* pp = NULL;
  2255. cmGrSync_t* sp = p->syncs;
  2256. for(; sp != NULL; sp=sp->link)
  2257. {
  2258. if( cmHandlesAreEqual(sp->grH,syncGrH) )
  2259. break;
  2260. pp = sp;
  2261. }
  2262. // if the handle was not found ...
  2263. if( sp == NULL )
  2264. {
  2265. // ... and a valid flags value was given ...
  2266. if( flags != 0 )
  2267. {
  2268. // ... then create a new sync target.
  2269. sp = cmMemAllocZ(cmGrSync_t,1);
  2270. sp->grH = syncGrH;
  2271. sp->flags = flags;
  2272. sp->link = p->syncs;
  2273. p->syncs = sp;
  2274. }
  2275. }
  2276. else // ... otherwise syncGrH is already a sync target
  2277. {
  2278. // if flags is non-zero then update the target sync flags
  2279. if( flags != 0 )
  2280. sp->flags = flags;
  2281. else
  2282. {
  2283. // otherwise delete the sync recd assoc'd with syncGrH
  2284. if( pp == NULL )
  2285. p->syncs = sp->link;
  2286. else
  2287. pp->link = sp->link;
  2288. cmMemFree(sp);
  2289. }
  2290. }
  2291. }
  2292. void cmGrReport( cmGrH_t h,cmRpt_t* r )
  2293. {
  2294. cmGr_t* p = _cmGrHandleToPtr(h);
  2295. cmRpt_t* rpt = r==NULL ? p->err.rpt : r;
  2296. cmRptPrintf(rpt,"cfg:0x%x state:0x%x\n",p->cfgFlags, p->stateFlags);
  2297. //cmRptPrintf(rpt,"World: "); cmGrVExtMaxXpt(&p->wext,rpt); cmRptPrintf(rpt,"\n");
  2298. cmRptPrintf(rpt,"View: "); cmGrVExtRpt(&p->vext,rpt); cmRptPrintf(rpt,"\n");
  2299. cmRptPrintf(rpt,"Phys: "); cmGrPExtRpt(&p->pext,rpt); cmRptPrintf(rpt,"\n");
  2300. _cmGrObjReportR(p,p->rootObj,rpt);
  2301. }