guiItem.cpp 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19. //
  20. // GuiItem.CPP
  21. //
  22. // History:
  23. // 08/07/96 JMI Started.
  24. //
  25. // 08/11/96 JMI When m_sBorderThickness was 0, GetClient was returning
  26. // 0 for the width and height by mistake. This was
  27. // remedied(sp?) such that it now uses the width and
  28. // height of the image.
  29. //
  30. // 08/12/96 JMI Added m_sJustification to allow the user to specify text
  31. // justification and DrawText so derived classes can
  32. // utilize this functionality.
  33. //
  34. // 08/15/96 JMI Blit() was checking dest image type for BLiT type instead
  35. // of source image type.
  36. //
  37. // 08/15/96 JMI Added TopPosToChild() complement to ChildPosToTop().
  38. //
  39. // 08/20/96 JMI SetParent now calls OffsetTopLevelPos to update top level
  40. // items.
  41. //
  42. // 08/20/96 JMI ~CGuiItem() now releases its parent and all its children.
  43. //
  44. // 09/24/96 JMI Borders are now not symmetrical. This makes the edges
  45. // appear more 3D like and causes a button's client to appear
  46. // to sink if the button has borders. This means that there
  47. // now has to be a GetTopLeftBorderThickness() and a
  48. // GetBottomRightBorderThickness() where before there was only
  49. // a GetTotalBorderThickness(). This also required a member
  50. // m_sInvertedBorder to remember whether the border was
  51. // inverted.
  52. //
  53. // 09/24/96 JMI Changed all BLU_MB?_* macros to RSP_MB?_* macros.
  54. //
  55. // 09/30/96 JMI Added SetVisible() to handle visibility. See summary
  56. // below.
  57. //
  58. // 10/01/96 JMI Added GetVal().
  59. //
  60. // 10/01/96 JMI ~CGuiItem() no longer calls ms_listguiChildren.Remove()
  61. // since SetParent(NULL) does such.
  62. //
  63. // 10/01/96 JMI Create() now calls Destroy().
  64. //
  65. // 10/22/96 JMI Changed m_print to m_pprint which defaults to new
  66. // ms_print. This should save much memory on CPRINTs. They
  67. // are reasonably large and only need to be used multiply
  68. // if one wants to use multiple fonts or other settings for
  69. // their GUIs.
  70. //
  71. // 10/27/96 MJR Fixed "unused variable" warnings.
  72. //
  73. // 10/31/96 JMI Changed:
  74. // Old label: New label:
  75. // ========= =========
  76. // CImage RImage
  77. // CGuiItem RGuiItem
  78. // CList RList
  79. // DRAWCALL DrawCall
  80. // BACKCALL BackCall
  81. // BTNUPCALL BtnUpCall
  82. // CHot RHot
  83. // LEFT Left
  84. // CENTERED Centered
  85. // CPRINT RPRINT
  86. // CFNT RFNT
  87. //
  88. // 11/01/96 JMI Changed:
  89. // Old label: New label:
  90. // ========= =========
  91. // RFNT RFontOld (soon will need to be RFont)
  92. // RPRINT RPrint
  93. // Rect RRect
  94. //
  95. // Also, changed all members referenced in RImage to
  96. // m_ and all position/dimension members referenced in
  97. // RImage to type short usage.
  98. //
  99. // 11/11/96 JMI Now m_szText is initialized to "" in CGuiItem::CGuiItem.
  100. //
  101. // 11/27/96 JMI Added initialization of m_type to identify this type
  102. // of GUI item.
  103. //
  104. // 12/04/96 JMI Now initializes RRects using new syntax.
  105. //
  106. // 12/19/96 JMI Uses new m_justification (as m_sJustification) and
  107. // upgraded to new RFont/RPrint.
  108. //
  109. // 12/22/96 JMI Added support for transparent uncompressed buffers.
  110. // Now, if m_sTransparent is set and the m_im buffer
  111. // is a type that causes ImageIsCompressed() to return
  112. // 0, rspBlitT will be used to blit the image
  113. // with transparent color m_u32TransparentColor.
  114. //
  115. // 12/28/96 JMI Added CreateGuiItem() function that will allocate,
  116. // with new, the requested 'standard' RGuiItem or
  117. // descendant.
  118. //
  119. // 12/29/96 JMI Added m_lId member used to item this RGuiItem from
  120. // others. Added GetItemFromId() to get an RGuiItem
  121. // via an ID. Added 'Justified' to the Justification
  122. // enum.
  123. //
  124. // 12/29/96 JMI Added DestroyGuiItem() function that will properly
  125. // and completely destroy a RGuiItem or descendant, if
  126. // the item is of a 'standard' type.
  127. //
  128. // 12/30/96 JMI Now SetActive() works the way SetVisible() always handled
  129. // activation. On activation, it activates its RHot only
  130. // if m_sActive is TRUE. On deactivation, it always
  131. // deactivates its RHot.
  132. // Now, if no clip rect specified to Draw(), the created
  133. // clip rect is clipped to the image dimensions.
  134. //
  135. // 12/31/96 JMI Added ms_pguiFocus static RGuiItem* and static functions
  136. // FocusNext(), FocusPrev(), and SetFocus() to standardize
  137. // focus.
  138. // Also, updated header comment regarding focus.
  139. // Note that ms_pguiFocus can be set by any other API; it
  140. // is not necessary to use FocusNext/Prev() and SetFocus(),
  141. // but for the sake of making as much work together as
  142. // possible, it would be nice if we tried to stick to
  143. // these functions.
  144. //
  145. // 01/01/97 JMI Added GetHot() which gets the area for the RHot and
  146. // now HotCall() will make your RHot as large as possible
  147. // on button down in order to increase the likelihood of
  148. // catching the button up. Also, compose now sets your
  149. // RHot to what GetHot() returns.
  150. //
  151. // 01/02/97 JMI Added DrawFocus() which draws a focus rectangle around
  152. // the border of the client area which is intended to show
  153. // which item has the focus.
  154. //
  155. // 01/03/97 JMI Removed OffsetTopLevelPos(). Now everything is truly
  156. // parent-child, including RHots. This greatly simplified
  157. // things with hotboxes.
  158. //
  159. // 01/04/97 JMI Upgraded HotCall() to new CursorEvent(). This upgrade
  160. // is in response to RGuiItem now using relative RHots.
  161. // Now m_hot.m_sX/Y is parent item relative just like
  162. // m_sX/Y. This should simplify a lot of stuff and even
  163. // fix some odd features like being able to click a GUI
  164. // item that exceeds the boundary of its parent. This fix
  165. // will be essential for the not-yet-existent RListBox since
  166. // it will most likely scroll many children through a small
  167. // client area.
  168. // Now there are two regions associated with cursor events.
  169. // The first is the 'hot' area. This is the area that m_hot
  170. // is set to include. Child items can only receive cursor
  171. // events through this area. The second is the 'event' area.
  172. // This is the area where the item really is actually con-
  173. // cerned with cursor events. Example: For a Dlg, the
  174. // entire window is the 'hot' area and the title bar is the
  175. // 'event' area.
  176. //
  177. // 01/05/97 JMI Now descends from RProps so users of this and descendant
  178. // classes can now add props. Templated to item type U32
  179. // with key type U32.
  180. // Also, added virtual destructor to guarantee that all
  181. // the destructors of the _allocated_ type get called.
  182. //
  183. // 01/06/97 JMI Now draws back to front and always clips to client area.
  184. // If a clipping rectangle is provided, Draw() intersects
  185. // it with the client area before recursing through child
  186. // items. If no clipping rectangle is provided, Draw()
  187. // intersects the destination image's rectangle with the
  188. // client area before recursing through child items.
  189. //
  190. // 01/06/97 JMI SetParent() now inserts new children at head of parent's
  191. // list.
  192. //
  193. // 01/13/97 JMI Added RListBox to CreateGuiItem() and DestroyGuiItem().
  194. //
  195. // 01/14/97 JMI Create() now calls Destroy() on failure instead of
  196. // destroying m_im manually.
  197. // SetParent() now sets the activated status to that of the
  198. // new parent if there is one. If there is none, the item
  199. // is deactivated. I'm not sure if the latter is correct.
  200. // It seems as if, now that there's this parent/child
  201. // relationship between hot boxes, that items should just
  202. // use their m_hot for their activation status.
  203. // Also, with this parent/child relationship, there is no
  204. // need to set the m_hot priority based on the child depth.
  205. // It is now up to the particular control or use of such to
  206. // utilize the m_hot.SetPriority(), which has proved useful
  207. // already for the RListBox class.
  208. //
  209. // 01/14/97 JMI Added Load and Save capabilities. Still in early stages
  210. // but hopefully I did it correctly. Seems to work fine.
  211. //
  212. // 01/15/97 JMI Now Load() and LoadInstantiate() call LoadChildren()
  213. // BEFORE Create().
  214. //
  215. // 01/16/97 JMI Added IsClicked(), SetClicked(), and m_sClicked.
  216. //
  217. // 01/18/97 JMI Added static DoFocus() to handle simple input focus.
  218. //
  219. // 01/20/97 JMI Added GetItemFromPoint() to get a GUI from the tree that
  220. // contains the specified point.
  221. //
  222. // 01/21/97 JMI Fixed comment in ReadMembers(). Added 'case' for new
  223. // version (version 1). Move future cases to the top of
  224. // the switch (they were incorrectly on the bottom).
  225. //
  226. // 01/21/97 JMI Added static array of strings describing types.
  227. //
  228. // 01/22/97 JMI GetText() had the typical strcpy args backward error.
  229. //
  230. // 01/23/97 JMI Add initialization of new member m_sFocusPos which
  231. // dictates where (relative to the client x, y) to draw
  232. // the focus.
  233. //
  234. // 02/05/97 JMI Added CreateGuiItem() and DestroyGuiItem() cases for
  235. // new PushBtn (RPushBtn).
  236. // Also, hopefully fixed thickness problem in DrawBorder()
  237. // that occurred when thickness was greater than 1.
  238. // Same for GetTopLeft/BottomRightBorderThickness().
  239. //
  240. // 02/05/97 JMI Added string in ms_apszTypes for RPushBtn.
  241. //
  242. // 02/05/97 JMI Now SetParent() calls OnLoseChild() for parent losing
  243. // child and OnGainChild() for parent gaining child.
  244. // Also, ~RGuiItem() calls Destroy() after SetParent(NULL).
  245. //
  246. // 02/25/97 JMI SaveChildren() now goes through the children in reverse
  247. // order so they, on load, get added back to their parent in
  248. // the order they were originally added to this parent.
  249. //
  250. // 03/13/97 JMI Added SetText(long lId, char* pszFrmt, ...);
  251. // Added GetVal(long lId) and GetText(long lId, char*);
  252. //
  253. // 03/19/97 JMI HotCall() now adapts sPosX,Y from hotbox coords instead
  254. // of hotbox's parent's coords.
  255. //
  256. // 03/19/97 JMI Converted to using the RHot::m_iecUser (was using
  257. // RHot::m_epcUser) so HotCall and CursorEvent now take
  258. // RInputEvent ptrs.
  259. //
  260. // 03/28/97 JMI RSP_MB0_DOUBLECLICK is now treated the same as
  261. // RSP_MB0_PRESSED.
  262. //
  263. // 04/01/97 JMI Changed short m_sCanBeFocused (TRUE, FALSE) to
  264. // m_targetFocus (Self, Parent, Sibling).
  265. // Also, changed position of default case in ReadMembers().
  266. //
  267. // 04/02/97 JMI TRACE message in GetText() was displaying a wrong value.
  268. //
  269. // 04/04/97 JMI Upped GUI_FILE_VERSION to 3 for new RScrollBar members
  270. // regarding smooth scrollage.
  271. //
  272. // 04/10/97 JMI Added components for background resource. Also, upped
  273. // file version to save these components.
  274. // Added font cell height member (loads and saves it too).
  275. //
  276. // 04/10/97 JMI Now if m_sBkdPlacement specifies Tile but not Center,
  277. // the whole background will be filled.
  278. //
  279. // 04/10/97 JMI Added case MultiBtn for RMultiBtn and string description.
  280. //
  281. // 04/24/97 JMI Added m_u32TextShadowColor.
  282. // Also, upped GUI_FILE_VERSION to 5 to read this member.
  283. //
  284. // 06/14/97 JMI ~RGuiItem() was checking this->IsProp(..) instead of
  285. // pgui->IsProp(..). Fixed.
  286. //
  287. // 06/30/97 MJR Moved SetVisible() into .cpp because the declaration of
  288. // a static inside of it was causing problems with mac
  289. // precompiled headers.
  290. //
  291. // 07/01/97 JMI Moved a bunch of functions (some that cannot be
  292. // inlined (b/c they are virtual) ) into the guiItem.cpp.
  293. //
  294. // 07/01/97 JMI Now CopyParms() now copies m_sFontCellHeight.
  295. //
  296. // 07/07/97 JMI Added TextEffectsFlags, m_sTextEffects, m_sTextShadowOffsetX,
  297. // and m_sTextShadowOffsetY.
  298. // Also, upped GUI_FILE_VERSION to 6 to save these new
  299. // members.
  300. // Also, added SetTextEffects().
  301. //
  302. // 08/22/97 JMI Now uses rspGeneralLock/Unlock() to make sure the
  303. // destination buffer to Draw() is properly locked, if
  304. // necessary.
  305. //
  306. // 09/12/97 JMI Made ReadMembers() and WriteMembers() public.
  307. //
  308. // JMI Added GetCurrentFileVersion().
  309. //
  310. // 09/25/97 JMI Upped GUI_FILE_VERSION to 7 so RMultiBtn can save its
  311. // current state which it always should've done.
  312. //
  313. // 10/06/99 JMI Added m_fnInputEvent.
  314. //
  315. //////////////////////////////////////////////////////////////////////////////
  316. //
  317. // This is intended as a base class for all RSPiX GUI objects. It does most
  318. // of the basic functionality for a gui object from CHot callbacks to drawing
  319. // borders and backgrounds. Just about all of its funcitons can be overrided.
  320. // If you want to create a totally unique GUI item, derive it from this.
  321. // If you are making a button or other item that is similar to something
  322. // already implemented, you might want to derive from a slightly higher
  323. // level such as RBtn, RTxt, RDlg, RScrollBar, etc.
  324. //
  325. // Visibility:
  326. // Visibility is handled in what may seem like a wierd way but, at this time,
  327. // to me, it seems like a useful way. I reserve the right to change my
  328. // opinion.
  329. // Effectively, calling SetVisible(TRUE) sets the visible status of this item
  330. // and calls m_hot.SetActive(m_sActive) for this item and all children.
  331. // The inverse is NOT true for SetVisible(FALSE). SetVisible(FALSE) sets
  332. // this item's m_sVisible to FALSE and calls m_hot.SetActive(FALSE) for this
  333. // and all child items. The basic idea is that hiding a gui item causes this
  334. // item and all chidren to not be displayed by setting m_sVisible, for this item
  335. // only, to FALSE and deactivating mouse input for this and all children.
  336. // Showing a gui item causes this item to be displayed by setting m_sVisible
  337. // to TRUE. All child items that have their m_sVisible set to TRUE will be
  338. // displayed through the parent's Draw(). All child items that have their
  339. // m_sActive set to TRUE will have mouse input activated.
  340. // m_sVisible is synonymous to Windows' show state (through ShowWindow(SW_HIDE) and
  341. // ShowWindow(SW_SHOW)). m_sActive is synonymous to Windows' enabled state
  342. // (through EnableWindow(FALSE | TRUE) ).
  343. //
  344. // Focus:
  345. // There is very little support for focus. It is considered a higher level
  346. // idea. What is provided is merely a static RGuiItem* that can be used to
  347. // keep track of the item in focus and three functions, FocusNext(),
  348. // FocusPrev(), and SetFocus(). Note that FocusNext/Prev() only move the focus
  349. // among children of one particular parent. Also, since their is no list of
  350. // top-level guis in this model, the changing of focus among top-level guis
  351. // must be done without the use of FocusNext/Prev(). Currently, SetFocus()
  352. // does barely anything, but it is better to use it than to set ms_pguiFocus
  353. // manually b/c SetFocus() will probably eventually do something.
  354. //
  355. // Enhancements:
  356. // To change the background of an RGuiItem, you can provide a callback in
  357. // the member m_backcall instead of actually deriving a whole new class just
  358. // for such purpose. See m_backcall.
  359. // Also, you can use a background resource name in m_szBkdResName to cause
  360. // an image to be loaded into m_pimBkdRes which will be used in composing
  361. // the item's background. See GetRes() & ReleaseRes().
  362. // Derive a class and override the DrawBorder to make different borders.
  363. // Hopefully we can have fun with these.
  364. //
  365. //////////////////////////////////////////////////////////////////////////////
  366. //////////////////////////////////////////////////////////////////////////////
  367. // C Headers.
  368. //////////////////////////////////////////////////////////////////////////////
  369. #include <stdarg.h>
  370. #include <string.h>
  371. #include <stdlib.h>
  372. #include <limits.h>
  373. //////////////////////////////////////////////////////////////////////////////
  374. // RSPiX Headers.
  375. //////////////////////////////////////////////////////////////////////////////
  376. #include "Blue.h"
  377. #ifdef PATHS_IN_INCLUDES
  378. #include "ORANGE/GUI/guiItem.h"
  379. #include "GREEN/BLiT/BLIT.H"
  380. #include "ORANGE/IFF/iff.h"
  381. // Headers for CreateGuiItem.
  382. #include "ORANGE/GUI/txt.h"
  383. #include "ORANGE/GUI/btn.h"
  384. #include "ORANGE/GUI/edit.h"
  385. #include "ORANGE/GUI/scrollbar.h"
  386. #include "ORANGE/GUI/dlg.h"
  387. #include "ORANGE/GUI/ListBox.h"
  388. #include "ORANGE/GUI/PushBtn.h"
  389. #include "ORANGE/GUI/MultiBtn.h"
  390. #else
  391. #include "BLIT.H"
  392. #include "GuiItem.h"
  393. // Headers for CreateGuiItem.
  394. #include "Txt.h"
  395. #include "Btn.h"
  396. #include "Edit.h"
  397. #include "ScrollBar.h"
  398. #include "Dlg.h"
  399. #include "ListBox.h"
  400. #include "iff.h"
  401. #include "PushBtn.h"
  402. #include "MultiBtn.h"
  403. #endif // PATHS_IN_INCLUDES
  404. //////////////////////////////////////////////////////////////////////////////
  405. // Module specific macros.
  406. //////////////////////////////////////////////////////////////////////////////
  407. #define DEF_BORDER_COLOR RSP_BLACK_INDEX
  408. #define DEF_BORDER_SHADOW_COLOR RSP_BLACK_INDEX
  409. #define DEF_BORDER_HIGHLIGHT_COLOR RSP_WHITE_INDEX
  410. #define DEF_BORDER_EDGE_COLOR RSP_WHITE_INDEX
  411. #define DEF_BORDER_THICKNESS 1
  412. #define DEF_TEXT_COLOR RSP_WHITE_INDEX
  413. #define DEF_BACK_COLOR RSP_BLACK_INDEX
  414. #define DEF_SHADOW_COLOR RSP_BLACK_INDEX
  415. #define DEF_FONT_CELL_HEIGHT 15
  416. #define DEF_FOCUS_COLOR RSP_WHITE_INDEX
  417. #define BORDER_EFFECTS_THICKNESS 2
  418. // Sets a value pointed to if ptr is not NULL.
  419. #define SET(pval, val) ((pval != NULL) ? *pval = val : val)
  420. // Finger print for GUI files.
  421. #define GUI_FINGER_PRINT MAKE_IFF_FCC('R', 'G', 'u', 'i')
  422. #define GUI_FILE_VERSION 7
  423. // If this prop is defined for a GUI item, it is 'delete'able.
  424. #define DYNAMIC_PROP_KEY 3267021
  425. //////////////////////////////////////////////////////////////////////////////
  426. // Module specific typedefs.
  427. //////////////////////////////////////////////////////////////////////////////
  428. //////////////////////////////////////////////////////////////////////////////
  429. // Module specific (static) variables.
  430. //////////////////////////////////////////////////////////////////////////////
  431. RPrint RGuiItem::ms_print; // This is the main RPrint that all
  432. // GUI items default to.
  433. RGuiItem* RGuiItem::ms_pguiFocus = NULL; // Higher level APIs can use this
  434. // as their current point of
  435. // input focus.
  436. char* RGuiItem::ms_apszTypes[NumGuiTypes] = // Array of strings
  437. // indexed by type.
  438. {
  439. "GuiItem",
  440. "Txt",
  441. "Btn",
  442. "Edit",
  443. "ScrollBar",
  444. "Dlg",
  445. "ListBox",
  446. "PushBtn",
  447. "MultiBtn",
  448. };
  449. //////////////////////////////////////////////////////////////////////////////
  450. // Construction/Destruction.
  451. //////////////////////////////////////////////////////////////////////////////
  452. //////////////////////////////////////////////////////////////////////////////
  453. //
  454. // Default constructor.
  455. //
  456. //////////////////////////////////////////////////////////////////////////////
  457. RGuiItem::RGuiItem()
  458. {
  459. m_sX = 0;
  460. m_sY = 0;
  461. m_hot.m_ulUser = (ULONG)this;
  462. m_hot.m_iecUser = HotCall;
  463. m_sEventAreaX = 0; // X coord of area in which we care
  464. // about cursor events.
  465. m_sEventAreaY = 0; // Y coord of area in which we care
  466. // about cursor events.
  467. m_sEventAreaW = 0; // Width of area in which we care
  468. // about cursor events.
  469. m_sEventAreaH = 0; // Height of area in which we care
  470. // about cursor events.
  471. m_sPressed = FALSE;
  472. m_pguiParent = NULL;
  473. m_drawcall = NULL;
  474. m_backcall = NULL;
  475. m_bcUser = NULL;
  476. m_fnInputEvent = NULL;
  477. m_ulUserInstance = NULL;
  478. m_ulUserData = 0;
  479. m_sBorderThickness = DEF_BORDER_THICKNESS;
  480. m_u32BorderColor = DEF_BORDER_COLOR;
  481. m_u32BorderShadowColor = DEF_BORDER_SHADOW_COLOR;
  482. m_u32BorderHighlightColor = DEF_BORDER_HIGHLIGHT_COLOR;
  483. m_u32BorderEdgeColor = DEF_BORDER_EDGE_COLOR;
  484. m_u32TextColor = DEF_TEXT_COLOR;
  485. m_u32BackColor = DEF_BACK_COLOR;
  486. m_u32TextShadowColor = DEF_SHADOW_COLOR;
  487. m_sTextEffects = 0; // Flags for text effects.
  488. m_sTextShadowOffsetX = 1; // Offset along X axis for text shadow.
  489. m_sTextShadowOffsetY = 1; // Offset along Y axis for text shadow.
  490. m_szText[0] = '\0';
  491. m_sFontCellHeight = DEF_FONT_CELL_HEIGHT; // Cell height for text.
  492. // Default to left justified.
  493. m_justification = RGuiItem::Left;
  494. m_sInvertedBorder = FALSE; // TRUE if border is inverted; FALSE
  495. // otherwise.
  496. m_type = GuiItem; // Indicates type of GUI item.
  497. m_sTransparent = FALSE; // TRUE, if this should be blt'ed via
  498. // a transparent blit call; FALSE,
  499. // otherwise. Note that this cannot
  500. // override transparent Image formats
  501. // (e.g., FSPR8 will always be blt'ed
  502. // with transparency).
  503. m_u32TransparentColor = 0; // Color used for transparency
  504. // when using transparent blit
  505. // call.
  506. m_lId = 0; // Default ID.
  507. m_targetFocus = Self; // Target when focus received.
  508. m_sShowFocus = TRUE; // TRUE if this item shows feedback,
  509. // usually via DrawFocus(), when it
  510. // has the focus.
  511. m_sFocusPos = 0; // Position at which DrawFocus() will
  512. // draw the focus rectangle relative
  513. // to the client area. For example,
  514. // -1 would but it just outside the
  515. // client area.
  516. m_u32FocusColor = DEF_FOCUS_COLOR; // Color to draw focus with.
  517. m_sClicked = FALSE; // TRUE if this item is considered
  518. // 'Clicked'. For example, the default
  519. // implementation makes this TRUE if
  520. // the cursor was pressed AND released
  521. // within this item's hot area.
  522. m_szBkdResName[0] = '\0'; // Name of background res
  523. // file to get into
  524. // m_pimBkdRes.
  525. m_sBkdResTransparent = FALSE; // TRUE, if m_pimBkdRes is to be
  526. // BLiT'ed transparently; FALSE,
  527. // otherwise.
  528. m_u32BkdResTransparentColor = 0; // Transparency color for
  529. // m_pimBkdRes when
  530. // BLiT'ed transparently.
  531. m_sBkdResPlacement = 0; // Combination of |'ed
  532. // Placement enum values.
  533. m_pimBkdRes = NULL; // Background resource image.
  534. m_fnGetRes = NULL; // User callback to get background res
  535. // (m_pimBkdRes).
  536. m_fnReleaseRes = NULL; // User callback to release background
  537. // res (m_pimBkdRes).
  538. m_sVisible = TRUE; // TRUE if Draw() is to draw this item
  539. // and its children; FALSE, otherwise.
  540. m_sActive = TRUE; // TRUE if the CHot is to be activated
  541. // when visible.
  542. // Assign default (lowest GUI) priority.
  543. m_hot.SetPriority(0);
  544. // Set default RPrint.
  545. m_pprint = &ms_print;
  546. }
  547. //////////////////////////////////////////////////////////////////////////////
  548. //
  549. // Destructor.
  550. //
  551. //////////////////////////////////////////////////////////////////////////////
  552. RGuiItem::~RGuiItem()
  553. {
  554. // Release all children.
  555. RGuiItem* pgui = m_listguiChildren.GetHead();
  556. while (pgui != NULL)
  557. {
  558. pgui->SetParent(NULL);
  559. // If the item has the dynamic prop set . . .
  560. if (pgui->IsDynamic() != FALSE)
  561. {
  562. // Destroy it.
  563. delete pgui;
  564. }
  565. pgui = m_listguiChildren.GetNext();
  566. }
  567. // Release parent.
  568. SetParent(NULL);
  569. // If we were the item in focus . . .
  570. if (ms_pguiFocus == this)
  571. {
  572. // Kill focus.
  573. SetFocus(NULL);
  574. }
  575. // Destroy dynamic data.
  576. Destroy();
  577. // If we had a dynamic prop . . .
  578. if (IsProp(DYNAMIC_PROP_KEY) != FALSE)
  579. {
  580. // Remove the prop.
  581. RemoveProp(DYNAMIC_PROP_KEY);
  582. }
  583. }
  584. ////////////////////////////////////////////////////////////////////////
  585. // Methods.
  586. ////////////////////////////////////////////////////////////////////////
  587. //////////////////////////////////////////////////////////////////////////////
  588. //
  589. // Creates a displayable GUI item.
  590. //
  591. //////////////////////////////////////////////////////////////////////////////
  592. short RGuiItem::Create( // Returns 0 on success.
  593. short sX, // X position relative to "parent" item.
  594. short sY, // Y position relative to "parent" item.
  595. short sW, // Width.
  596. short sH, // Height.
  597. short sDepth) // Color depth.
  598. {
  599. short sRes = 0; // Assume success.
  600. Destroy();
  601. m_sX = sX;
  602. m_sY = sY;
  603. if (m_im.CreateImage(sW, sH, RImage::BMP8, 0, sDepth) == 0)
  604. {
  605. Compose();
  606. // Done.
  607. // If there's an error after calling CreateImage, perhaps
  608. // we should destroy the RImage data here.
  609. if (sRes != 0)
  610. {
  611. Destroy();
  612. }
  613. }
  614. else
  615. {
  616. TRACE("Create(): RImage::CreateImage() failed.\n");
  617. sRes = -1;
  618. }
  619. return sRes;
  620. }
  621. ////////////////////////////////////////////////////////////////////////
  622. // Destroys dynamic display data.
  623. // (virtual)
  624. ////////////////////////////////////////////////////////////////////////
  625. void RGuiItem::Destroy(void) // Returns nothing.
  626. {
  627. m_im.DestroyData();
  628. m_im.DestroyPalette();
  629. if (m_pimBkdRes != NULL)
  630. {
  631. ReleaseRes();
  632. ASSERT(m_pimBkdRes == NULL);
  633. }
  634. }
  635. ////////////////////////////////////////////////////////////////////////
  636. //
  637. // Blit this item only into provided RImage. Used by Draw().
  638. //
  639. ////////////////////////////////////////////////////////////////////////
  640. short RGuiItem::Blit( // Returns 0 on success.
  641. RImage* pimDst, // Destination image.
  642. short sDstX, // X position in destination.
  643. short sDstY, // Y position in destination.
  644. short sSrcX /*= 0*/, // X position in source.
  645. short sSrcY /*= 0*/, // Y position in source.
  646. short sW /*= 0*/, // Amount to draw.
  647. short sH /*= 0*/, // Amount to draw.
  648. RRect* prc /*= NULL*/) // Clip to.
  649. {
  650. short sRes = 0; // Assume success.
  651. ASSERT(pimDst != NULL);
  652. // If this is a sprite . . .
  653. if (ImageIsCompressed(m_im.m_type) != 0)
  654. {
  655. if (rspBlit(&m_im, pimDst, sDstX, sDstY, prc) == 0)
  656. {
  657. // Success.
  658. }
  659. else
  660. {
  661. TRACE("Blit(): rspBlit() failed.\n");
  662. sRes = -1;
  663. }
  664. }
  665. else
  666. {
  667. if (sW == 0)
  668. {
  669. sW = m_im.m_sWidth;
  670. }
  671. if (sH == 0)
  672. {
  673. sH = m_im.m_sHeight;
  674. }
  675. if (m_sTransparent == FALSE)
  676. {
  677. if (rspBlit(&m_im, pimDst, sSrcX, sSrcY,
  678. sDstX, sDstY,
  679. sW, sH, prc) == 0)
  680. {
  681. // Success.
  682. }
  683. #if 0 // Blit returns the same error for just about everything (including
  684. // source clipped out).
  685. else
  686. {
  687. TRACE("Blit(): rspBlit() failed.\n");
  688. sRes = -1;
  689. }
  690. #endif
  691. }
  692. else
  693. {
  694. rspBlitT(
  695. m_u32TransparentColor, // Transparent color.
  696. &m_im, pimDst, // Src, Dst.
  697. sSrcX, sSrcY,
  698. sDstX, sDstY,
  699. sW, sH,
  700. prc, // Dst clip.
  701. NULL); // Src clip.
  702. }
  703. }
  704. return sRes;
  705. }
  706. ////////////////////////////////////////////////////////////////////////
  707. //
  708. // Draw this item and all its subitems into the provided RImage.
  709. //
  710. ////////////////////////////////////////////////////////////////////////
  711. short RGuiItem::Draw( // Returns 0 on success.
  712. RImage* pimDst, // Destination image.
  713. short sDstX /*= 0*/, // X position in destination.
  714. short sDstY /*= 0*/, // Y position in destination.
  715. short sSrcX /*= 0*/, // X position in source.
  716. short sSrcY /*= 0*/, // Y position in source.
  717. short sW /*= 0*/, // Amount to draw.
  718. short sH /*= 0*/, // Amount to draw.
  719. RRect* prc /*= NULL*/) // Clip to.
  720. {
  721. short sRes = 0; // Assume success.
  722. // If visible . . .
  723. if (m_sVisible != FALSE)
  724. {
  725. sDstX += m_sX;
  726. sDstY += m_sY;
  727. // Note that we lock this here and don't unlock it until after we
  728. // have processed our children. Although and because Draw()
  729. // is recursive, this is the most efficient way to do this lock.
  730. // Since rspGeneralLock() (or, at least, the functions it calls)
  731. // do not do anything if the buffer is already locked, there is
  732. // little efficiency lost in calling this multiple times but plenty
  733. // gained by not locking/unlocking and locking/unlocking again and
  734. // again (the exception being the flip page).
  735. rspGeneralLock(pimDst);
  736. if (Blit(pimDst, sDstX, sDstY, sSrcX, sSrcY, sW, sH, prc) == 0)
  737. {
  738. // An original clippage should be passed to all these children.
  739. // Set up default clip rect.
  740. RRect rc;
  741. // Get client.
  742. GetClient(&rc.sX, &rc.sY, &rc.sW, &rc.sH);
  743. // Affect by draw position.
  744. rc.sX += sDstX;
  745. rc.sY += sDstY;
  746. // Clip to destination.
  747. RRect rcDst;
  748. // If no rect . . .
  749. if (prc == NULL)
  750. {
  751. // Provide one using destination image.
  752. rcDst.sX = 0;
  753. rcDst.sY = 0;
  754. rcDst.sW = pimDst->m_sWidth;
  755. rcDst.sH = pimDst->m_sHeight;
  756. prc = &rcDst;
  757. }
  758. // Clip default clipper to provided or destination.
  759. rc.ClipTo(prc);
  760. // Draw back to front!
  761. RGuiItem* pgui = m_listguiChildren.GetTail();
  762. while (sRes == 0 && pgui != NULL)
  763. {
  764. // Draw subitem and all its subitems.
  765. sRes = pgui->Draw(pimDst, sDstX, sDstY, 0, 0, 0, 0, &rc);
  766. pgui = m_listguiChildren.GetPrev();
  767. }
  768. // If this item has the focus . . .
  769. if (ms_pguiFocus == this)
  770. {
  771. // Draw focus feedback, if this item shows the focus.
  772. DrawFocus(pimDst, sDstX, sDstY, &rc);
  773. }
  774. }
  775. else
  776. {
  777. TRACE("Draw(): Blit() failed.\n");
  778. sRes = -1;
  779. }
  780. // Unlock the destination buffer.
  781. rspGeneralUnlock(pimDst);
  782. }
  783. return sRes;
  784. }
  785. ////////////////////////////////////////////////////////////////////////
  786. //
  787. // Pass a message up to the highest level that we need to draw.
  788. //
  789. ////////////////////////////////////////////////////////////////////////
  790. short RGuiItem::Redraw( // Returns 0 on success.
  791. short sSrcX /*= 0*/, // X position to start drawing from.
  792. short sSrcY /*= 0*/, // Y position to start drawing from.
  793. short sW /*= 0*/, // Amount to draw.
  794. short sH /*= 0*/) // Amount to draw.
  795. {
  796. short sRes = 0; // Assume success.
  797. // If there is a user callback . . .
  798. if (m_drawcall != NULL)
  799. {
  800. if (sW == 0)
  801. {
  802. sW = m_im.m_sWidth;
  803. }
  804. if (sH == 0)
  805. {
  806. sH = m_im.m_sHeight;
  807. }
  808. // If we have a parent . . .
  809. if (m_pguiParent != NULL)
  810. {
  811. sRes = m_pguiParent->Redraw(m_sX + sSrcX, m_sY + sSrcY, sW, sH);
  812. }
  813. else
  814. {
  815. // Highest level. Parent of all parents.
  816. // Create a temporary image.
  817. RImage im;
  818. if (im.CreateImage(sW, sH, RImage::BMP8, 0, m_im.m_sDepth) == 0)
  819. {
  820. // Draw into image.
  821. Draw(&im, -m_sX, -m_sY, sSrcX, sSrcY, sW, sH);
  822. RRect rc(m_sX + sSrcX, m_sY + sSrcY, sW, sH);
  823. // Pass on to user callback.
  824. (*m_drawcall)(this, &im, &rc);
  825. // Done with image.
  826. im.DestroyData();
  827. }
  828. else
  829. {
  830. TRACE("Redraw(): RImage::CreateImage() failed.\n");
  831. sRes = -1;
  832. }
  833. }
  834. }
  835. else
  836. {
  837. sRes = 1;
  838. }
  839. return sRes;
  840. }
  841. ////////////////////////////////////////////////////////////////////////
  842. //
  843. // Ask user to erase area specified.
  844. //
  845. ////////////////////////////////////////////////////////////////////////
  846. void RGuiItem::Erase( // Returns nothing.
  847. short sX /*= 0*/, // X position to erase.
  848. short sY /*= 0*/, // Y position to erase.
  849. short sW /*= 0*/, // Width to erase.
  850. short sH /*= 0*/) // Height to erase.
  851. {
  852. // If there is a callback . . .
  853. if (m_drawcall != NULL)
  854. {
  855. if (sW == 0)
  856. {
  857. sW = m_im.m_sWidth;
  858. }
  859. if (sH == 0)
  860. {
  861. sH = m_im.m_sHeight;
  862. }
  863. RRect rc(m_sX + sX, m_sY + sY, sW, sH);
  864. // Ask user.
  865. (*m_drawcall)(this, NULL, &rc);
  866. }
  867. }
  868. ////////////////////////////////////////////////////////////////////////
  869. //
  870. // Set the text that represents this item.
  871. //
  872. ////////////////////////////////////////////////////////////////////////
  873. void RGuiItem::SetText(
  874. char* pszFrmt, // sprintf formatted format string.
  875. ...) // Corresponding good stuff.
  876. {
  877. va_list val;
  878. va_start(val, pszFrmt);
  879. vsprintf(m_szText, pszFrmt, val);
  880. }
  881. ////////////////////////////////////////////////////////////////////////
  882. // Set the text that represents the specified child item.
  883. ////////////////////////////////////////////////////////////////////////
  884. short RGuiItem::SetText( // Returns 0 if item found, non-zero otherwise.
  885. long lId, // Child item ID (can identify this item).
  886. char* pszFrmt, // sprintf formatted format string.
  887. ...) // Corresponding good stuff.
  888. {
  889. short sRes = 0; // Assume success.
  890. RGuiItem* pgui = GetItemFromId(lId);
  891. if (pgui != NULL)
  892. {
  893. va_list val;
  894. va_start (val, pszFrmt);
  895. vsprintf(pgui->m_szText, pszFrmt, val);
  896. }
  897. else
  898. {
  899. TRACE("SetText(): No such ID %ld.\n", lId);
  900. sRes = -1;
  901. }
  902. return sRes;
  903. }
  904. ////////////////////////////////////////////////////////////////////////
  905. // Sets up the current text effects on m_pprint to match this
  906. // GUI's settings.
  907. ////////////////////////////////////////////////////////////////////////
  908. void RGuiItem::SetTextEffects(void) // Returns nothing.
  909. {
  910. // If shadow enabled . . .
  911. if (m_sTextEffects & Shadow)
  912. {
  913. m_pprint->SetEffectAbs(RPrint::SHADOW_X, m_sTextShadowOffsetX);
  914. m_pprint->SetEffectAbs(RPrint::SHADOW_Y, m_sTextShadowOffsetY);
  915. }
  916. else
  917. {
  918. m_pprint->SetEffectAbs(RPrint::SHADOW_X, 0);
  919. m_pprint->SetEffectAbs(RPrint::SHADOW_Y, 0);
  920. }
  921. #if 0 // Not Yet Implemented -- time constraints.
  922. // If bold enabled . . .
  923. if (m_sTextEffects & Bold)
  924. {
  925. m_pprint->SetEffect(RPrint::BOLD, m_dTextBold);
  926. }
  927. else
  928. {
  929. m_pprint->SetEffect(RPrint::BOLD, 1.0);
  930. }
  931. // If italics enabled . . .
  932. if (m_sTextEffects & Italic)
  933. {
  934. m_pprint->SetEffect(RPrint::ITALIC, m_dTextItalic);
  935. }
  936. else
  937. {
  938. m_pprint->SetEffect(RPrint::ITALIC, 1.0);
  939. }
  940. #endif
  941. }
  942. ////////////////////////////////////////////////////////////////////////
  943. //
  944. // Move this item to sX, sY.
  945. //
  946. ////////////////////////////////////////////////////////////////////////
  947. void RGuiItem::Move( // Returns nothing.
  948. short sX, // New x position.
  949. short sY) // New y position.
  950. {
  951. // Erase old position.
  952. Erase();
  953. short sDifX = sX - m_sX;
  954. short sDifY = sY - m_sY;
  955. m_hot.m_sX += sDifX;
  956. m_hot.m_sY += sDifY;
  957. m_sX = sX;
  958. m_sY = sY;
  959. // Not necessary if we are updated every iteration or frame.
  960. Redraw();
  961. }
  962. ////////////////////////////////////////////////////////////////////////
  963. //
  964. // Cursor event notification.
  965. // Events in event area.
  966. // (virtual).
  967. //
  968. ////////////////////////////////////////////////////////////////////////
  969. void RGuiItem::CursorEvent( // Returns nothing.
  970. RInputEvent* pie) // In: Most recent user input event.
  971. // Out: pie->sUsed = TRUE, if used.
  972. {
  973. switch (pie->sEvent)
  974. {
  975. case RSP_MB0_DOUBLECLICK:
  976. case RSP_MB0_PRESSED:
  977. // Remember we're pressed.
  978. m_sPressed = TRUE;
  979. // Capture events.
  980. m_hot.SetCapture(TRUE);
  981. // Get focus.
  982. SetFocus();
  983. // Note that we used it.
  984. pie->sUsed = TRUE;
  985. break;
  986. case RSP_MB0_RELEASED:
  987. if (m_sPressed != FALSE)
  988. {
  989. // Stop capturing events.
  990. m_hot.SetCapture(FALSE);
  991. // If inside of item . . .
  992. if ( pie->sPosX >= m_sEventAreaX
  993. && pie->sPosX < m_sEventAreaX + m_sEventAreaW
  994. && pie->sPosY >= m_sEventAreaY
  995. && pie->sPosY < m_sEventAreaY + m_sEventAreaH)
  996. {
  997. // If there is a button up callback . . .
  998. if (m_bcUser != NULL)
  999. {
  1000. (*m_bcUser)(this);
  1001. }
  1002. // Note clickage.
  1003. SetClicked(TRUE);
  1004. }
  1005. m_sPressed = FALSE;
  1006. }
  1007. // Note that we used it.
  1008. pie->sUsed = TRUE;
  1009. break;
  1010. }
  1011. }
  1012. ////////////////////////////////////////////////////////////////////////
  1013. //
  1014. // Callback from CHot.
  1015. // Events in hot area.
  1016. //
  1017. ////////////////////////////////////////////////////////////////////////
  1018. void RGuiItem::HotCall( // Returns nothing.
  1019. RInputEvent* pie) // In: Most recent user input event.
  1020. // Out: pie->sUsed = TRUE, if used.
  1021. {
  1022. // If this is an unused mouse event . . .
  1023. if (pie->type == RInputEvent::Mouse && pie->sUsed == FALSE)
  1024. {
  1025. short sHotPosX = m_hot.m_sX;
  1026. short sHotPosY = m_hot.m_sY;
  1027. // Make relative to this GUI item (rather than hotbox).
  1028. pie->sPosX -= sHotPosX;
  1029. pie->sPosY -= sHotPosY;
  1030. // If within event area or capturing . . .
  1031. if ( ( pie->sPosX >= m_sEventAreaX && pie->sPosX < m_sEventAreaX + m_sEventAreaW
  1032. && pie->sPosY >= m_sEventAreaY && pie->sPosY < m_sEventAreaY + m_sEventAreaH)
  1033. || m_hot.IsCapturing() != FALSE)
  1034. {
  1035. // Pass item relative event.
  1036. CursorEvent(pie);
  1037. }
  1038. // If there is a button event callback . . .
  1039. if (m_fnInputEvent)
  1040. {
  1041. // Process event via callback w/ relative positioning
  1042. m_fnInputEvent(this, pie);
  1043. }
  1044. // Put position back.
  1045. pie->sPosX += sHotPosX;
  1046. pie->sPosY += sHotPosY;
  1047. }
  1048. }
  1049. ////////////////////////////////////////////////////////////////////////
  1050. // Activate or deactivate mouse reaction for this gui item only.
  1051. // If the item has m_sActive set to TRUE, it will be activated.
  1052. ////////////////////////////////////////////////////////////////////////
  1053. // virtual // If you override this, call this base if possible.
  1054. void RGuiItem::SetActive( // Returns nothing.
  1055. short sActive) // TRUE to make active, FALSE otherwise.
  1056. {
  1057. if (sActive != FALSE)
  1058. {
  1059. m_hot.SetActive(m_sActive);
  1060. }
  1061. else
  1062. {
  1063. m_hot.SetActive(FALSE);
  1064. }
  1065. }
  1066. ////////////////////////////////////////////////////////////////////////
  1067. // Hide or show GUI item and child items. Causes RHots to be
  1068. // deactivated for both this and child items.
  1069. // DOES NOT CHANGE THE VISIBLE STATUS OF CHILD ITEMS.
  1070. ////////////////////////////////////////////////////////////////////////
  1071. //virtual // If you override this, call this base if possible.
  1072. void RGuiItem::SetVisible( // Returns nothing.
  1073. short sVisible) // TRUE to make visible, FALSE otherwise.
  1074. {
  1075. static short sSem = 0;
  1076. // Only parent gains/looses its visible status . . .
  1077. if (++sSem == 1)
  1078. {
  1079. m_sVisible = sVisible;
  1080. }
  1081. // Set activation to visibility combined with visibility attribute.
  1082. SetActive(m_sVisible && sVisible);
  1083. // Enum children.
  1084. RGuiItem* pguiChild = m_listguiChildren.GetHead();
  1085. while (pguiChild != NULL)
  1086. {
  1087. pguiChild->SetVisible(m_sVisible && sVisible);
  1088. pguiChild = m_listguiChildren.GetNext();
  1089. }
  1090. // Open the gate.
  1091. sSem--;
  1092. }
  1093. ////////////////////////////////////////////////////////////////////////
  1094. //
  1095. // Change parent. Removes from old parent's list and adds to new.
  1096. // New can be NULL (so can old).
  1097. //
  1098. ////////////////////////////////////////////////////////////////////////
  1099. void RGuiItem::SetParent(RGuiItem* pguiParent)
  1100. {
  1101. short sDifX = 0; // Difference in top level x positioning.
  1102. short sDifY = 0; // Difference in top level y positioning.
  1103. // If there is an old . . .
  1104. if (m_pguiParent != NULL)
  1105. {
  1106. // Let it know it's about to lose it's child.
  1107. m_pguiParent->OnLoseChild(this);
  1108. if (m_pguiParent->m_listguiChildren.Remove(this) == 0)
  1109. {
  1110. }
  1111. else
  1112. {
  1113. TRACE("SetParent(): Unable to remove child from old parent.\n");
  1114. }
  1115. }
  1116. // Set new parent.
  1117. m_pguiParent = pguiParent;
  1118. // If there is a new . . .
  1119. if (m_pguiParent != NULL)
  1120. {
  1121. // Let it know it's about to gain a child.
  1122. m_pguiParent->OnGainChild(this);
  1123. if (m_pguiParent->m_listguiChildren.InsertHead(this) == 0)
  1124. {
  1125. }
  1126. else
  1127. {
  1128. TRACE("SetParent(): Unable to Add child to new parent.\n");
  1129. }
  1130. // Set this item's RHot as a child of its parent's.
  1131. m_hot.SetParent(&(m_pguiParent->m_hot) );
  1132. // Activate item based on new parent.
  1133. // NOTE that this can screw up the ordering.
  1134. SetActive(m_pguiParent->IsActivated());
  1135. }
  1136. else
  1137. {
  1138. // Set this item's as a global/top-level RHot.
  1139. m_hot.SetParent(NULL);
  1140. // Item becomes inactive. Or should it keep its current activation?
  1141. SetActive(FALSE);
  1142. }
  1143. }
  1144. ////////////////////////////////////////////////////////////////////////
  1145. //
  1146. // Draw border.
  1147. //
  1148. ////////////////////////////////////////////////////////////////////////
  1149. void RGuiItem::DrawBorder( // Returns nothing.
  1150. RImage* pim /*= NULL*/, // Dest image, uses m_im if NULL.
  1151. short sInvert /*= FALSE*/) // Inverts border if TRUE.
  1152. {
  1153. short sVertShadowPos;
  1154. short sHorzShadowPos;
  1155. short sVertHighlightPos;
  1156. short sHorzHighlightPos;
  1157. short sVertEdgePos;
  1158. short sHorzEdgePos;
  1159. if (pim == NULL)
  1160. {
  1161. pim = &m_im;
  1162. }
  1163. short sW = pim->m_sWidth;
  1164. short sH = pim->m_sHeight;
  1165. m_sInvertedBorder = sInvert;
  1166. if (sInvert == FALSE)
  1167. {
  1168. sVertShadowPos = sW - m_sBorderThickness;
  1169. sHorzShadowPos = sH - m_sBorderThickness;
  1170. sVertHighlightPos = 0;
  1171. sHorzHighlightPos = 0;
  1172. sVertEdgePos = sW - m_sBorderThickness * 2;
  1173. sHorzEdgePos = sH - m_sBorderThickness * 2;
  1174. }
  1175. else
  1176. {
  1177. sVertShadowPos = 0;
  1178. sHorzShadowPos = 0;
  1179. sVertHighlightPos = sW - m_sBorderThickness;
  1180. sHorzHighlightPos = sH - m_sBorderThickness;
  1181. sVertEdgePos = m_sBorderThickness;
  1182. sHorzEdgePos = m_sBorderThickness;
  1183. }
  1184. // One pixel for each edge of border gets overwritten.
  1185. rspRect(m_u32BorderHighlightColor, pim, 0, sHorzHighlightPos, sW, m_sBorderThickness);
  1186. rspRect(m_u32BorderHighlightColor, pim, sVertHighlightPos, 0, m_sBorderThickness, sH);
  1187. rspRect(m_u32BorderShadowColor, pim, 0, sHorzShadowPos, sW, m_sBorderThickness);
  1188. rspRect(m_u32BorderShadowColor, pim, sVertShadowPos, 0, m_sBorderThickness, sH);
  1189. rspRect(m_u32BorderEdgeColor, pim, m_sBorderThickness, sHorzEdgePos, sW - m_sBorderThickness * 2, m_sBorderThickness);
  1190. rspRect(m_u32BorderEdgeColor, pim, sVertEdgePos, m_sBorderThickness, m_sBorderThickness, sH - m_sBorderThickness * 2);
  1191. }
  1192. ////////////////////////////////////////////////////////////////////////
  1193. //
  1194. // Draw background. Calls user callback m_backcall if providd.
  1195. //
  1196. ////////////////////////////////////////////////////////////////////////
  1197. void RGuiItem::DrawBackground( // Returns nothing.
  1198. RImage* pim /*= NULL*/) // Dest image, uses m_im if NULL.
  1199. {
  1200. if (pim == NULL)
  1201. {
  1202. pim = &m_im;
  1203. }
  1204. RRect rc;
  1205. GetClient(&rc.sX, &rc.sY, &rc.sW, &rc.sH);
  1206. if (m_backcall != NULL)
  1207. {
  1208. (*m_backcall)(this, pim, &rc);
  1209. }
  1210. else
  1211. {
  1212. // Draw "client".
  1213. rspRect( m_u32BackColor, pim,
  1214. rc.sX, rc.sY,
  1215. rc.sW, rc.sH);
  1216. DrawBackgroundRes(pim);
  1217. }
  1218. }
  1219. ////////////////////////////////////////////////////////////////////////
  1220. // Draw background resource, if one is specified.
  1221. // Utilizes m_*BkdRes* parameters to get, place, and BLiT the resource.
  1222. // (virtual).
  1223. ////////////////////////////////////////////////////////////////////////
  1224. void RGuiItem::DrawBackgroundRes( // Returns nothing.
  1225. RImage* pim /*= NULL*/) // Dest image, uses m_im, if NULL.
  1226. {
  1227. if (pim == NULL)
  1228. {
  1229. pim = &m_im;
  1230. }
  1231. // If resource not loaded . . .
  1232. if (m_pimBkdRes == NULL)
  1233. {
  1234. // If there's a background res . . .
  1235. if (m_szBkdResName[0] != '\0')
  1236. {
  1237. if (GetRes() == 0)
  1238. {
  1239. }
  1240. else
  1241. {
  1242. TRACE("DrawBackgroundRes(): GetRes() failed.\n");
  1243. }
  1244. }
  1245. }
  1246. // If resource now available . . .
  1247. if (m_pimBkdRes != NULL)
  1248. {
  1249. RRect rc;
  1250. GetClient(&rc.sX, &rc.sY, &rc.sW, &rc.sH);
  1251. // Determine size to be drawn.
  1252. short sTotalW, sTotalH;
  1253. if (m_sBkdResPlacement & Tile)
  1254. {
  1255. // Get amount to tile multiplied by the size of that dimension.
  1256. sTotalW = (rc.sW / m_pimBkdRes->m_sWidth) * m_pimBkdRes->m_sWidth;
  1257. sTotalH = (rc.sH / m_pimBkdRes->m_sHeight) * m_pimBkdRes->m_sHeight;
  1258. // If not centered . . .
  1259. if ((m_sBkdResPlacement & Center) == 0)
  1260. {
  1261. // Allow overblit.
  1262. // If not a perfect fit . . .
  1263. if ((rc.sW % m_pimBkdRes->m_sWidth) != 0)
  1264. {
  1265. sTotalW += m_pimBkdRes->m_sWidth;
  1266. }
  1267. if ((rc.sH % m_pimBkdRes->m_sHeight) != 0)
  1268. {
  1269. sTotalH += m_pimBkdRes->m_sHeight;
  1270. }
  1271. }
  1272. }
  1273. else
  1274. {
  1275. sTotalW = m_pimBkdRes->m_sWidth;
  1276. sTotalH = m_pimBkdRes->m_sHeight;
  1277. }
  1278. // Determine place to be drawn.
  1279. short sBlitX, sBlitY;
  1280. if (m_sBkdResPlacement & Center)
  1281. {
  1282. sBlitX = rc.sX + rc.sW / 2 - sTotalW / 2;
  1283. sBlitY = rc.sY + rc.sH / 2 - sTotalH / 2;
  1284. }
  1285. else
  1286. {
  1287. sBlitX = rc.sX;
  1288. sBlitY = rc.sY;
  1289. }
  1290. // Draw.
  1291. short sX, sY;
  1292. short sMaxX = sBlitX + sTotalW;
  1293. short sMaxY = sBlitY + sTotalH;
  1294. for (sY = sBlitY; sY < sMaxY; sY += m_pimBkdRes->m_sHeight)
  1295. {
  1296. for (sX = sBlitX; sX < sMaxX; sX += m_pimBkdRes->m_sWidth)
  1297. {
  1298. if (m_sBkdResTransparent == FALSE)
  1299. {
  1300. rspBlit(
  1301. m_pimBkdRes, // Src.
  1302. pim, // Dst.
  1303. sX, // Dst.
  1304. sY, // Dst.
  1305. &rc); // Dst.
  1306. }
  1307. else
  1308. {
  1309. rspBlitT(
  1310. m_u32BkdResTransparentColor, // Src.
  1311. m_pimBkdRes, // Src.
  1312. pim, // Dst.
  1313. 0, // Src.
  1314. 0, // Src.
  1315. sX, // Dst.
  1316. sY, // Dst.
  1317. m_pimBkdRes->m_sWidth, // Both.
  1318. m_pimBkdRes->m_sHeight, // Both.
  1319. &rc, // Dst.
  1320. NULL); // Src.
  1321. }
  1322. }
  1323. }
  1324. }
  1325. }
  1326. ////////////////////////////////////////////////////////////////////////
  1327. //
  1328. // Compose item.
  1329. //
  1330. ////////////////////////////////////////////////////////////////////////
  1331. void RGuiItem::Compose( // Returns nothing.
  1332. RImage* pim /*= NULL*/) // Dest image, uses m_im if NULL.
  1333. {
  1334. if (pim == NULL)
  1335. {
  1336. pim = &m_im;
  1337. }
  1338. // If there is a border . . .
  1339. if (m_sBorderThickness > 0)
  1340. {
  1341. // Draw it.
  1342. DrawBorder(pim, m_sInvertedBorder);
  1343. }
  1344. // Draw background.
  1345. DrawBackground(pim);
  1346. // Set hot area.
  1347. SetHotArea();
  1348. // Set event area.
  1349. SetEventArea();
  1350. }
  1351. ////////////////////////////////////////////////////////////////////////
  1352. // Do one iteration of processing. This is useful on items that need to
  1353. // poll frequently or do something every once in a while that is not
  1354. // triggered by an event.
  1355. ////////////////////////////////////////////////////////////////////////
  1356. // virtual // If you override this, call this base if possible.
  1357. void RGuiItem::Do( // Returns nothing.
  1358. RInputEvent* /*pie*/) // In: Most recent user input event.
  1359. // Out: pie->sUsed = TRUE, if used.
  1360. {
  1361. // Currently, this base does nothing.
  1362. }
  1363. ////////////////////////////////////////////////////////////////////////
  1364. // Set this item's event area. This is the area where cursor events are
  1365. // interesting to the item.
  1366. ////////////////////////////////////////////////////////////////////////
  1367. // virtual // If you override this, call this base if possible.
  1368. void RGuiItem::SetEventArea(void) // Returns nothing.
  1369. {
  1370. m_sEventAreaX = 0;
  1371. m_sEventAreaY = 0;
  1372. m_sEventAreaW = m_im.m_sWidth;
  1373. m_sEventAreaH = m_im.m_sHeight;
  1374. }
  1375. ////////////////////////////////////////////////////////////////////////
  1376. // Set this item's hot area. This should be the total dimensions of this
  1377. // item.
  1378. ////////////////////////////////////////////////////////////////////////
  1379. // virtual // If you override this, call this base if possible.
  1380. void RGuiItem::SetHotArea(void) // Returns nothing.
  1381. {
  1382. // Get the hot area relative to this item.
  1383. GetHotArea(&(m_hot.m_sX), &(m_hot.m_sY), &(m_hot.m_sW), &(m_hot.m_sH));
  1384. // Convert to parent coordinates.
  1385. m_hot.m_sX += m_sX;
  1386. m_hot.m_sY += m_sY;
  1387. }
  1388. ////////////////////////////////////////////////////////////////////////
  1389. // Copy basic parameters regarding appearance and use from this item to
  1390. // the specified one.
  1391. ////////////////////////////////////////////////////////////////////////
  1392. // virtual // If you override this, call this base if possible.
  1393. void RGuiItem::CopyParms( // Returns nothing.
  1394. RGuiItem* pguiDst) // Destination for parameters from this item.
  1395. {
  1396. pguiDst->m_pprint = m_pprint;
  1397. pguiDst->m_u32BorderColor = m_u32BorderColor;
  1398. pguiDst->m_u32BorderShadowColor = m_u32BorderShadowColor;
  1399. pguiDst->m_u32BorderHighlightColor = m_u32BorderHighlightColor;
  1400. pguiDst->m_u32BorderEdgeColor = m_u32BorderEdgeColor;
  1401. pguiDst->m_u32TextColor = m_u32TextColor;
  1402. pguiDst->m_u32BackColor = m_u32BackColor;
  1403. pguiDst->m_sBorderThickness = m_sBorderThickness;
  1404. pguiDst->m_justification = m_justification;
  1405. pguiDst->m_sTransparent = m_sTransparent;
  1406. pguiDst->m_u32TransparentColor = m_u32TransparentColor;
  1407. pguiDst->m_u32FocusColor = m_u32FocusColor;
  1408. pguiDst->m_sFontCellHeight = m_sFontCellHeight;
  1409. }
  1410. ////////////////////////////////////////////////////////////////////////
  1411. // Called by SetParent() when a GUI is losing a child item.
  1412. ////////////////////////////////////////////////////////////////////////
  1413. // virtual // If you override this, call this base, if possible.
  1414. void RGuiItem::OnLoseChild( // Returns nothing.
  1415. RGuiItem* /*pguiChild*/) // Child item we're about to lose.
  1416. {
  1417. }
  1418. ////////////////////////////////////////////////////////////////////////
  1419. // Called by SetParent() when a GUI is gaining a child item.
  1420. ////////////////////////////////////////////////////////////////////////
  1421. // virtual // If you override this, call this base, if possible.
  1422. void RGuiItem::OnGainChild( // Returns nothing.
  1423. RGuiItem* /*pguiChild*/) // Child item we're about to gain.
  1424. {
  1425. }
  1426. ////////////////////////////////////////////////////////////////////////
  1427. // Sets the 'clicked' status.
  1428. ////////////////////////////////////////////////////////////////////////
  1429. void RGuiItem::SetClicked( // Returns nothing.
  1430. short sClicked) // New 'clicked' status.
  1431. {
  1432. m_sClicked = sClicked;
  1433. }
  1434. ////////////////////////////////////////////////////////////////////////
  1435. // Draws focus for item if m_sShowFocus is TRUE.
  1436. ////////////////////////////////////////////////////////////////////////
  1437. // virtual // If you override this, call this base if possible.
  1438. void RGuiItem::DrawFocus( // Returns nothing.
  1439. RImage* pimDst, // Destination image.
  1440. short sDstX /*= 0*/, // X offset in destination.
  1441. short sDstY /*= 0*/, // Y offset in destination.
  1442. RRect* prc /*= NULL*/) // Clip to.
  1443. {
  1444. // If this item shows the focus . . .
  1445. if (m_sShowFocus != FALSE)
  1446. {
  1447. short sClientX, sClientY, sClientW, sClientH;
  1448. GetClient(&sClientX, &sClientY, &sClientW, &sClientH);
  1449. rspRect(
  1450. MAX(m_sBorderThickness, (short)1),
  1451. m_u32FocusColor,
  1452. pimDst,
  1453. sDstX + sClientX + m_sFocusPos,
  1454. sDstY + sClientY + m_sFocusPos,
  1455. sClientW - m_sFocusPos * 2,
  1456. sClientH - m_sFocusPos * 2,
  1457. prc);
  1458. }
  1459. }
  1460. ////////////////////////////////////////////////////////////////////////
  1461. // Called by the static implementation of SetFocus() on the item gaining
  1462. // the focus.
  1463. // It is okay to call SetFocus() from this function.
  1464. ////////////////////////////////////////////////////////////////////////
  1465. // virtual // If you override this, call this base if possible.
  1466. void RGuiItem::OnGainFocus(void)
  1467. {
  1468. // Does this item want the focus . . .
  1469. switch (m_targetFocus)
  1470. {
  1471. case Self:
  1472. break;
  1473. case Parent:
  1474. SetFocus(GetParent() );
  1475. break;
  1476. case Sibling:
  1477. FocusNext();
  1478. break;
  1479. case Child:
  1480. SetFocus(m_listguiChildren.GetHead() );
  1481. break;
  1482. }
  1483. }
  1484. ////////////////////////////////////////////////////////////////////////
  1485. // Called by the static implementation of SetFocus() on the item losing
  1486. // the focus.
  1487. // It is okay to call SetFocus() from this function.
  1488. ////////////////////////////////////////////////////////////////////////
  1489. // virtual // If you override this, call this base if possible.
  1490. void RGuiItem::OnLoseFocus(void)
  1491. {
  1492. // Currently, this base does nothing.
  1493. }
  1494. ////////////////////////////////////////////////////////////////////////
  1495. // Sets the current focus GUI pointer to this GUI.
  1496. ////////////////////////////////////////////////////////////////////////
  1497. RGuiItem* RGuiItem::SetFocus(void) // Returns pointer to GUI losing the focus.
  1498. {
  1499. return SetFocus(this);
  1500. }
  1501. ////////////////////////////////////////////////////////////////////////
  1502. //
  1503. // Draw text in m_szText in m_u32TextColor with transparent
  1504. // background at sX, sY with sW and m_sJustification.
  1505. // Does nothing if m_szText is empty.
  1506. //
  1507. ////////////////////////////////////////////////////////////////////////
  1508. short RGuiItem::DrawText( // Returns 0 on success.
  1509. short sX, // X position in image.
  1510. short sY, // Y position in image.
  1511. short sW /*= 0*/, // Width of text area.
  1512. short sH /*= 0*/, // Height of test area.
  1513. RImage* pim /*= NULL*/) // Destination image. NULL == use m_im.
  1514. {
  1515. short sRes = 0; // Assume success.
  1516. // Draw text.
  1517. if (m_szText[0] != '\0')
  1518. {
  1519. if (pim == NULL)
  1520. {
  1521. // Use internal image.
  1522. pim = &m_im;
  1523. }
  1524. if (sW == 0)
  1525. {
  1526. GetClient(NULL, NULL, &sW, NULL);
  1527. }
  1528. if (sH == 0)
  1529. {
  1530. GetClient(NULL, NULL, NULL, &sH);
  1531. }
  1532. // Text support is currently 8 bit only.
  1533. m_pprint->SetColor((short)m_u32TextColor, (short)0, m_u32TextShadowColor);
  1534. // Set size. Hopefully this won't do too much scaling for
  1535. // caching purposes but I'm not sure.
  1536. m_pprint->SetFont(m_sFontCellHeight);
  1537. SetJustification();
  1538. SetTextEffects();
  1539. m_pprint->SetDestination(pim);
  1540. m_pprint->SetColumn(sX, sY, sW, sH);
  1541. m_pprint->print(sX, sY, m_szText);
  1542. }
  1543. return sRes;
  1544. }
  1545. ////////////////////////////////////////////////////////////////////////
  1546. // Gets the optional resource image named m_szBkdResName for this item
  1547. // into m_pimBkdRes.
  1548. // Each call to this function should have a corresponding call to
  1549. // ReleaseRes().
  1550. // This function can result in a callback.
  1551. // (virtual).
  1552. ////////////////////////////////////////////////////////////////////////
  1553. short RGuiItem::GetRes(void)
  1554. {
  1555. short sRes = 0; // Assume success.
  1556. // If there's a callback . . .
  1557. if (m_fnGetRes != NULL)
  1558. {
  1559. // Use it instead.
  1560. sRes = m_fnGetRes(this);
  1561. }
  1562. else
  1563. {
  1564. // Release current resource, if any.
  1565. // Default implementation allows 'Release' call even if no 'Get'
  1566. // call has yet occurred. This should not be relied upon when
  1567. // using user Get/Releases.
  1568. // There is, of course, the possibility that someone will define
  1569. // a Release without a Get or vice-versa...I think that's a dumb
  1570. // idea.
  1571. ReleaseRes();
  1572. m_pimBkdRes = new RImage;
  1573. if (m_pimBkdRes != NULL)
  1574. {
  1575. if (RFileEZLoad(
  1576. m_pimBkdRes,
  1577. m_szBkdResName,
  1578. "rb",
  1579. RFile::LittleEndian) == 0)
  1580. {
  1581. // Success.
  1582. }
  1583. else
  1584. {
  1585. TRACE("GetRes(): Failed to open resource \"%s\".\n", m_szBkdResName);
  1586. sRes = -2;
  1587. }
  1588. // If any errors occurred after allocation . . .
  1589. if (sRes != 0)
  1590. {
  1591. delete m_pimBkdRes;
  1592. m_pimBkdRes = NULL;
  1593. }
  1594. }
  1595. else
  1596. {
  1597. TRACE("GetRes(): Failed to allocate new RImage for "
  1598. "m_pimBkdRes.\n");
  1599. sRes = -1;
  1600. }
  1601. }
  1602. return sRes;
  1603. }
  1604. ////////////////////////////////////////////////////////////////////////
  1605. // Releases the optional resource image previously gotten by a call to
  1606. // GetRes().
  1607. // Each call to this function should correspond to a previous call to
  1608. // GetRes(). You should not call this function if the corresponding
  1609. // GetRes() failed.
  1610. // This function can result in a callback.
  1611. // (virtual).
  1612. ////////////////////////////////////////////////////////////////////////
  1613. void RGuiItem::ReleaseRes(void)
  1614. {
  1615. // If there's a callback . . .
  1616. if (m_fnReleaseRes != NULL)
  1617. {
  1618. // Use it instead.
  1619. m_fnReleaseRes(this);
  1620. }
  1621. else
  1622. {
  1623. if (m_pimBkdRes != NULL)
  1624. {
  1625. delete m_pimBkdRes;
  1626. m_pimBkdRes = NULL;
  1627. }
  1628. }
  1629. }
  1630. ////////////////////////////////////////////////////////////////////////
  1631. // File Oriented.
  1632. ////////////////////////////////////////////////////////////////////////
  1633. ////////////////////////////////////////////////////////////////////////
  1634. //
  1635. // This will open the specified file with read access in an RFile and
  1636. // pass it to Load(RFile*).
  1637. // NOTE: There is a static version of this function that will detect
  1638. // the type of the root item from the file and allocate an item of
  1639. // that type before loading. Useful for loading generic *.GUI files.
  1640. // See LoadInstance(char*).
  1641. //
  1642. ////////////////////////////////////////////////////////////////////////
  1643. short RGuiItem::Load( // Returns 0 on success.
  1644. char* pszFileName) // Name of file to load from.
  1645. {
  1646. short sRes = 0; // Assume success.
  1647. // Attempt to open specified file . . .
  1648. RFile file;
  1649. if (file.Open(pszFileName, "rb", RFile::LittleEndian) == 0)
  1650. {
  1651. // Always use your member.
  1652. sRes = Load(&file);
  1653. // Close the file.
  1654. file.Close();
  1655. }
  1656. else
  1657. {
  1658. TRACE("Load(\"%s\"): Failed to open file.\n", pszFileName);
  1659. sRes = -1;
  1660. }
  1661. return sRes;
  1662. }
  1663. ////////////////////////////////////////////////////////////////////////
  1664. //
  1665. // This will load the GUI tree (i.e., 'this' item and its children).
  1666. // If you override this function, you should call this base version
  1667. // to have it load the base class members.
  1668. // NOTE: There is a static version of this function that will detect
  1669. // the type of the root item from the file and allocate an item of
  1670. // that type before loading. Useful for loading generic *.GUI files.
  1671. // See LoadInstance(RFile*).
  1672. // (virtual).
  1673. //
  1674. ////////////////////////////////////////////////////////////////////////
  1675. short RGuiItem::Load( // Returns 0 on success.
  1676. RFile* pfile) // RFile open with read access to load from.
  1677. {
  1678. short sRes = 0; // Assume success.
  1679. ASSERT(pfile->IsOpen() != FALSE);
  1680. // Read header . . .
  1681. U32 u32Version;
  1682. Type type;
  1683. if (ReadHeader(pfile, &u32Version, &type) == 0)
  1684. {
  1685. ASSERT(type == m_type);
  1686. // Read members . . .
  1687. if (ReadMembers(pfile, u32Version) == 0)
  1688. {
  1689. // Determine current color depth.
  1690. short sDepth = 8; // Safety or something.
  1691. rspGetVideoMode(&sDepth);
  1692. if (LoadChildren(pfile) == 0)
  1693. {
  1694. // Create item . . .
  1695. if (Create(
  1696. m_sX,
  1697. m_sY,
  1698. m_im.m_sWidth,
  1699. m_im.m_sHeight,
  1700. sDepth) == 0)
  1701. {
  1702. // Success.
  1703. }
  1704. else
  1705. {
  1706. TRACE("Load(): Create() failed.\n");
  1707. sRes = -3;
  1708. }
  1709. }
  1710. else
  1711. {
  1712. TRACE("Load(): LoadChildren() failed.\n");
  1713. sRes = -4;
  1714. }
  1715. }
  1716. else
  1717. {
  1718. TRACE("Load(): ReadMembers() failed.\n");
  1719. sRes = -2;
  1720. }
  1721. }
  1722. else
  1723. {
  1724. TRACE("Load(): ReadHeader() failed.\n");
  1725. sRes = -1;
  1726. }
  1727. return sRes;
  1728. }
  1729. ////////////////////////////////////////////////////////////////////////
  1730. //
  1731. // Instantiate a GUI tree from a file. Allocates a GUI item of the
  1732. // proper (specified in the file) type and loads it using the specified
  1733. // file.
  1734. // (static).
  1735. //
  1736. ////////////////////////////////////////////////////////////////////////
  1737. RGuiItem* RGuiItem::LoadInstantiate( // Returns newly allocated GUI item
  1738. // on success or NULL on failure.
  1739. char* pszFileName) // Name of file to instantiate from.
  1740. {
  1741. RGuiItem* pgui = NULL; // Assume nothing.
  1742. // Attempt to open specified file . . .
  1743. RFile file;
  1744. if (file.Open(pszFileName, "rb", RFile::LittleEndian) == 0)
  1745. {
  1746. // Always use your member.
  1747. pgui = LoadInstantiate(&file);
  1748. // Close the file.
  1749. file.Close();
  1750. }
  1751. else
  1752. {
  1753. TRACE("LoadInstantiate(\"%s\"): Failed to open file.\n", pszFileName);
  1754. }
  1755. return pgui;
  1756. }
  1757. ////////////////////////////////////////////////////////////////////////
  1758. //
  1759. // Instantiate a GUI tree from a file. Allocates a GUI item of the
  1760. // proper (specified in the file) type and loads it using the specified
  1761. // file.
  1762. // (static).
  1763. //
  1764. ////////////////////////////////////////////////////////////////////////
  1765. RGuiItem* RGuiItem::LoadInstantiate( // Returns newly allocated GUI item
  1766. // on success or NULL on failure.
  1767. RFile* pfile) // Pointer to open GUI file.
  1768. {
  1769. short sError = 0;
  1770. RGuiItem* pgui = NULL; // Assume nothing.
  1771. ASSERT(pfile->IsOpen() != FALSE);
  1772. // Read header . . .
  1773. U32 u32Version;
  1774. Type type;
  1775. if (ReadHeader(pfile, &u32Version, &type) == 0)
  1776. {
  1777. // Allocate a GUI item of the appropriate type . . .
  1778. pgui = CreateGuiItem(type);
  1779. if (pgui != NULL)
  1780. {
  1781. // Read members . . .
  1782. if (pgui->ReadMembers(pfile, u32Version) == 0)
  1783. {
  1784. // Determine current color depth.
  1785. short sDepth = 8; // Safety or something.
  1786. rspGetVideoMode(&sDepth);
  1787. if (pgui->LoadChildren(pfile) == 0)
  1788. {
  1789. // Create item . . .
  1790. if (pgui->Create(
  1791. pgui->m_sX,
  1792. pgui->m_sY,
  1793. pgui->m_im.m_sWidth,
  1794. pgui->m_im.m_sHeight,
  1795. sDepth) == 0)
  1796. {
  1797. // Success.
  1798. }
  1799. else
  1800. {
  1801. TRACE("LoadInstantiate(): Create() failed.\n");
  1802. sError = 4;
  1803. }
  1804. }
  1805. else
  1806. {
  1807. TRACE("LoadInstantiate(): LoadChildren() failed.\n");
  1808. sError = 5;
  1809. }
  1810. }
  1811. else
  1812. {
  1813. TRACE("LoadInstantiate(): ReadMembers() failed.\n");
  1814. sError = 3;
  1815. }
  1816. // If any errors occurred after allocating GUI . . .
  1817. if (sError != 0)
  1818. {
  1819. delete pgui;
  1820. pgui = NULL;
  1821. }
  1822. }
  1823. else
  1824. {
  1825. TRACE("LoadInstantiate(): CreateGuiItem() failed.\n");
  1826. sError = 2;
  1827. }
  1828. }
  1829. else
  1830. {
  1831. TRACE("LoadInstantiate(): ReadHeader() failed.\n");
  1832. sError = 1;
  1833. }
  1834. return pgui;
  1835. }
  1836. ////////////////////////////////////////////////////////////////////////
  1837. //
  1838. // Load item's children from the specified file.
  1839. // (virtual).
  1840. // (protected).
  1841. //
  1842. ////////////////////////////////////////////////////////////////////////
  1843. short RGuiItem::LoadChildren( // Returns 0 on success.
  1844. RFile* pfile) // File to load from.
  1845. {
  1846. short sRes = 0; // Assume success.
  1847. ASSERT(pfile->IsOpen() != FALSE);
  1848. short sNum;
  1849. // Read number of children.
  1850. pfile->Read(&sNum);
  1851. // Instantiate children.
  1852. RGuiItem* pgui;
  1853. short sCurChild;
  1854. for ( sCurChild = 0;
  1855. sCurChild < sNum && sRes == 0 && pfile->Error() == FALSE;
  1856. sCurChild++)
  1857. {
  1858. pgui = LoadInstantiate(pfile);
  1859. if (pgui != NULL)
  1860. {
  1861. pgui->SetParent(this);
  1862. }
  1863. else
  1864. {
  1865. TRACE("LoadChildren(): LoadInstantiate() failed.\n");
  1866. sRes = -1;
  1867. }
  1868. }
  1869. return sRes;
  1870. }
  1871. ////////////////////////////////////////////////////////////////////////
  1872. //
  1873. // Read header for this GUI item.
  1874. // (protected).
  1875. // (static).
  1876. //
  1877. ////////////////////////////////////////////////////////////////////////
  1878. short RGuiItem::ReadHeader( // Returns 0 on success.
  1879. RFile* pfile, // In: File to read from.
  1880. U32* pu32Version, // Out: File format version.
  1881. Type* ptype) // Out: Type of GUI item stored.
  1882. {
  1883. short sRes = 0; // Assume success.
  1884. ASSERT(pfile->IsOpen() != FALSE);
  1885. // Read fingerprint.
  1886. U32 u32FingerPrint;
  1887. if (pfile->Read(&u32FingerPrint) == 1)
  1888. {
  1889. if (u32FingerPrint == GUI_FINGER_PRINT)
  1890. {
  1891. // Read file format version.
  1892. if (pfile->Read(pu32Version) == 1)
  1893. {
  1894. // Read type.
  1895. U32 u32Type;
  1896. if (pfile->Read(&u32Type) == 1)
  1897. {
  1898. // Store type for caller.
  1899. *ptype = (Type)u32Type;
  1900. }
  1901. else
  1902. {
  1903. TRACE("ReadHeader(): Error reading GUI item type from file.\n");
  1904. sRes = -4;
  1905. }
  1906. }
  1907. else
  1908. {
  1909. TRACE("ReadHeader(): Error reading file format version number.\n");
  1910. sRes = -3;
  1911. }
  1912. }
  1913. else
  1914. {
  1915. TRACE("ReadHeader(): Invalid finger print. Not a GUI file.\n");
  1916. sRes = -2;
  1917. }
  1918. }
  1919. else
  1920. {
  1921. TRACE("ReadHeader(): Error reading finger print.\n");
  1922. sRes = -1;
  1923. }
  1924. return sRes;
  1925. }
  1926. ////////////////////////////////////////////////////////////////////////
  1927. //
  1928. // Read item's members from file.
  1929. // (virtual).
  1930. //
  1931. ////////////////////////////////////////////////////////////////////////
  1932. short RGuiItem::ReadMembers( // Returns 0 on success.
  1933. RFile* pfile, // File to read from.
  1934. U32 u32Version) // File format version to use.
  1935. {
  1936. short sRes = 0; // Assume success.
  1937. // Read members. Type must always be first becuase
  1938. // LoadInstantiate() utilizes this fact to determine the type of item
  1939. // that should be instantiated.
  1940. U32 u32Temp;
  1941. switch (u32Version)
  1942. {
  1943. default:
  1944. // Insert additional version numbers here!
  1945. case 6: // Version 6 stuff.
  1946. pfile->Read(&m_sTextEffects);
  1947. pfile->Read(&m_sTextShadowOffsetX);
  1948. pfile->Read(&m_sTextShadowOffsetY);
  1949. case 5: // Version 5 stuff.
  1950. pfile->Read(&m_u32TextShadowColor);
  1951. case 4: // Version 4 stuff.
  1952. // New members regarding background resources.
  1953. pfile->Read(m_szBkdResName);
  1954. pfile->Read(&m_sBkdResTransparent);
  1955. pfile->Read(&m_u32BkdResTransparentColor);
  1956. pfile->Read(&m_sBkdResPlacement);
  1957. // New member regarding font size (height).
  1958. pfile->Read(&m_sFontCellHeight);
  1959. case 3: // Version 3 stuff.
  1960. // No new stuff here. RScrollBar has some new members to load.
  1961. case 2: // Version 2 stuff.
  1962. // No new stuff here. RListBox now saves its m_typeEncapsulator.
  1963. case 1: // Version 1 stuff.
  1964. // No new stuff here. Derived types RListBox, RScrollBar, and
  1965. // REdit load their members in version 1.
  1966. case 0: // Version 0 stuff.
  1967. // Base class members.
  1968. pfile->Read(&m_sX);
  1969. pfile->Read(&m_sY);
  1970. pfile->Read(&m_im.m_sWidth);
  1971. pfile->Read(&m_im.m_sHeight);
  1972. pfile->Read(m_szText);
  1973. pfile->Read(&m_u32BorderColor);
  1974. pfile->Read(&m_u32BorderShadowColor);
  1975. pfile->Read(&m_u32BorderHighlightColor);
  1976. pfile->Read(&m_u32BorderEdgeColor);
  1977. pfile->Read(&m_u32TextColor);
  1978. pfile->Read(&m_u32BackColor);
  1979. pfile->Read(&m_sBorderThickness);
  1980. pfile->Read(&u32Temp);
  1981. m_justification = (Justification)u32Temp;
  1982. pfile->Read(&m_sInvertedBorder);
  1983. pfile->Read(&m_sTransparent);
  1984. pfile->Read(&m_u32TransparentColor);
  1985. pfile->Read(&m_lId);
  1986. // Used to load m_sCanBeFocused (TRUE, FALSE); now load
  1987. // m_targetFocus.
  1988. short sFocusTarget = Parent;
  1989. pfile->Read(&sFocusTarget);
  1990. m_targetFocus = (Target)sFocusTarget;
  1991. pfile->Read(&m_sShowFocus);
  1992. pfile->Read(&m_u32FocusColor);
  1993. pfile->Read(&m_sVisible);
  1994. pfile->Read(&m_sActive);
  1995. // If successful . . .
  1996. if (pfile->Error() == FALSE)
  1997. {
  1998. // Success.
  1999. }
  2000. else
  2001. {
  2002. TRACE("ReadMembers(): Error reading RGuiItem members.\n");
  2003. sRes = -1;
  2004. }
  2005. break;
  2006. }
  2007. return sRes;
  2008. }
  2009. ////////////////////////////////////////////////////////////////////////
  2010. //
  2011. // This will open the specified file with write access in an RFile and
  2012. // pass it to Save(RFile*).
  2013. //
  2014. ////////////////////////////////////////////////////////////////////////
  2015. short RGuiItem::Save( // Returns 0 on success.
  2016. char* pszFileName) // Name of file to save to.
  2017. {
  2018. short sRes = 0; // Assume success.
  2019. // Attempt to open specified file . . .
  2020. RFile file;
  2021. if (file.Open(pszFileName, "wb", RFile::LittleEndian) == 0)
  2022. {
  2023. // Always use your member.
  2024. sRes = Save(&file);
  2025. // Close the file.
  2026. file.Close();
  2027. }
  2028. else
  2029. {
  2030. TRACE("Save(\"%s\"): Failed to open file.\n", pszFileName);
  2031. sRes = -1;
  2032. }
  2033. return sRes;
  2034. }
  2035. ////////////////////////////////////////////////////////////////////////
  2036. //
  2037. // This will save the GUI tree (i.e., 'this' item and its children).
  2038. // If you override this function, you should call this base version
  2039. // to have it save the base class members.
  2040. // (virtual).
  2041. //
  2042. ////////////////////////////////////////////////////////////////////////
  2043. short RGuiItem::Save( // Returns 0 on success.
  2044. RFile* pfile) // RFile open with write access to save to.
  2045. {
  2046. short sRes = 0; // Assume success.
  2047. ASSERT(pfile->IsOpen() != FALSE);
  2048. // Write fingerprint.
  2049. pfile->Write((U32)GUI_FINGER_PRINT);
  2050. // Write file format version.
  2051. pfile->Write((U32)GUI_FILE_VERSION);
  2052. // Write member variables for RGuiItem . . .
  2053. if (WriteMembers(pfile) == 0)
  2054. {
  2055. // Write all children.
  2056. sRes = SaveChildren(pfile);
  2057. }
  2058. else
  2059. {
  2060. TRACE("Save(): Error writing RGuiItem members.\n");
  2061. sRes = -1;
  2062. }
  2063. return sRes;
  2064. }
  2065. ////////////////////////////////////////////////////////////////////////
  2066. //
  2067. // Save item's children to the specified file.
  2068. // (virtual).
  2069. // (protected).
  2070. //
  2071. ////////////////////////////////////////////////////////////////////////
  2072. short RGuiItem::SaveChildren( // Returns 0 on success.
  2073. RFile* pfile) // File to save to.
  2074. {
  2075. short sRes = 0; // Assume success.
  2076. ASSERT(pfile->IsOpen() != FALSE);
  2077. // Determine number of child items.
  2078. short sNum = 0;
  2079. RGuiItem* pgui = m_listguiChildren.GetHead();
  2080. while (pgui != NULL)
  2081. {
  2082. sNum++;
  2083. pgui = m_listguiChildren.GetNext();
  2084. }
  2085. // Write number of children.
  2086. pfile->Write(sNum);
  2087. // Save children. Note that we go through the children in reverse
  2088. // order so they, on load, get added back to their parent in the
  2089. // order they were originally added to this parent.
  2090. pgui = m_listguiChildren.GetTail();
  2091. while (pgui != NULL && sRes == 0 && pfile->Error() == FALSE)
  2092. {
  2093. sRes = pgui->Save(pfile);
  2094. pgui = m_listguiChildren.GetPrev();
  2095. }
  2096. return sRes;
  2097. }
  2098. ////////////////////////////////////////////////////////////////////////
  2099. //
  2100. // Write item's members to file.
  2101. // (virtual).
  2102. //
  2103. ////////////////////////////////////////////////////////////////////////
  2104. short RGuiItem::WriteMembers( // Returns 0 on success.
  2105. RFile* pfile) // File to write to.
  2106. {
  2107. short sRes = 0; // Assume success.
  2108. // Write members. Type must always be first becuase
  2109. // LoadInstantiate() utilizes this fact to determine the type of item
  2110. // that should be instantiated.
  2111. pfile->Write((U32)m_type);
  2112. // Insert new members here.
  2113. pfile->Write(m_sTextEffects);
  2114. pfile->Write(m_sTextShadowOffsetX);
  2115. pfile->Write(m_sTextShadowOffsetY);
  2116. pfile->Write(m_u32TextShadowColor);
  2117. pfile->Write(m_szBkdResName);
  2118. pfile->Write(m_sBkdResTransparent);
  2119. pfile->Write(m_u32BkdResTransparentColor);
  2120. pfile->Write(m_sBkdResPlacement);
  2121. pfile->Write(m_sFontCellHeight);
  2122. pfile->Write(m_sX);
  2123. pfile->Write(m_sY);
  2124. pfile->Write(m_im.m_sWidth);
  2125. pfile->Write(m_im.m_sHeight);
  2126. pfile->Write(m_szText);
  2127. pfile->Write(m_u32BorderColor);
  2128. pfile->Write(m_u32BorderShadowColor);
  2129. pfile->Write(m_u32BorderHighlightColor);
  2130. pfile->Write(m_u32BorderEdgeColor);
  2131. pfile->Write(m_u32TextColor);
  2132. pfile->Write(m_u32BackColor);
  2133. pfile->Write(m_sBorderThickness);
  2134. pfile->Write((U32)m_justification);
  2135. pfile->Write(m_sInvertedBorder);
  2136. pfile->Write(m_sTransparent);
  2137. pfile->Write(m_u32TransparentColor);
  2138. pfile->Write(m_lId);
  2139. pfile->Write((short)m_targetFocus);
  2140. pfile->Write(m_sShowFocus);
  2141. pfile->Write(m_u32FocusColor);
  2142. pfile->Write(m_sVisible);
  2143. pfile->Write(m_sActive);
  2144. // If successful . . .
  2145. if (pfile->Error() == FALSE)
  2146. {
  2147. // Success.
  2148. }
  2149. else
  2150. {
  2151. TRACE("WriteMembers(): Error writing RGuiItem members.\n");
  2152. sRes = -1;
  2153. }
  2154. return sRes;
  2155. }
  2156. ////////////////////////////////////////////////////////////////////////
  2157. // Querries.
  2158. ////////////////////////////////////////////////////////////////////////
  2159. ////////////////////////////////////////////////////////////////////////
  2160. //
  2161. // Determine if the position is inside the item.
  2162. //
  2163. ////////////////////////////////////////////////////////////////////////
  2164. inline short IsInside( // Returns TRUE, if inside; FALSE otherwise.
  2165. RGuiItem* pgui, // GUI in question.
  2166. short sPosX, // X coord of position in question.
  2167. short sPosY, // Y coord of position in question.
  2168. short sActive, // If TRUE, only searches active items.
  2169. // If FALSE, searches all items.
  2170. short sEventArea) // If TRUE, only checks items' event areas.
  2171. // If FALSE, checks items' entire hot regions.
  2172. {
  2173. short sRes = TRUE; // Assume inside.
  2174. // If activated is required . . .
  2175. if (sActive != FALSE)
  2176. {
  2177. // Get activation status.
  2178. sRes = pgui->IsActivated();
  2179. }
  2180. // If okay so far . . .
  2181. if (sRes != FALSE)
  2182. {
  2183. // If using event area . . .
  2184. if (sEventArea != FALSE)
  2185. {
  2186. if ( sPosX < pgui->m_sEventAreaX
  2187. || sPosX >= pgui->m_sEventAreaX + pgui->m_sEventAreaW
  2188. || sPosY < pgui->m_sEventAreaY
  2189. || sPosY >= pgui->m_sEventAreaY + pgui->m_sEventAreaH)
  2190. {
  2191. sRes = FALSE;
  2192. }
  2193. }
  2194. else // Entire hot area.
  2195. {
  2196. if ( sPosX < pgui->m_hot.m_sX
  2197. || sPosX >= pgui->m_hot.m_sX + pgui->m_hot.m_sW
  2198. || sPosY < pgui->m_hot.m_sY
  2199. || sPosY >= pgui->m_hot.m_sY + pgui->m_hot.m_sH)
  2200. {
  2201. sRes = FALSE;
  2202. }
  2203. }
  2204. }
  2205. return sRes;
  2206. }
  2207. ////////////////////////////////////////////////////////////////////////
  2208. //
  2209. // Get the item at the specified point that has the
  2210. // specified parameters.
  2211. //
  2212. ////////////////////////////////////////////////////////////////////////
  2213. RGuiItem* RGuiItem::GetItemFromPoint( // Returns item ptr, if item found;
  2214. // NULL on failure.
  2215. short sPosX, // X position.
  2216. short sPosY, // Y position.
  2217. short sActive /*= TRUE*/, // If TRUE, only searches active items.
  2218. // If FALSE, searches all items.
  2219. short sEventArea /*= TRUE*/) // If TRUE, only checks items' event areas.
  2220. // If FALSE, checks items' entire hot regions.
  2221. {
  2222. RGuiItem* pguiRes = NULL; // Assume nothing.
  2223. // If inside this item . . .
  2224. if (IsInside(this, sPosX, sPosY, sActive, sEventArea) != FALSE)
  2225. {
  2226. RGuiItem* pguiCur;
  2227. pguiRes = this; // Start with this.
  2228. do
  2229. {
  2230. // Adapt coords to current item's system.
  2231. sPosX -= pguiRes->m_sX;
  2232. sPosY -= pguiRes->m_sY;
  2233. // Check children.
  2234. pguiCur = pguiRes->m_listguiChildren.GetHead();
  2235. while (pguiCur != NULL)
  2236. {
  2237. if (IsInside(pguiCur, sPosX, sPosY, sActive, sEventArea) != FALSE)
  2238. {
  2239. // Found one.
  2240. pguiRes = pguiCur;
  2241. break;
  2242. }
  2243. pguiCur = pguiRes->m_listguiChildren.GetNext();
  2244. }
  2245. // until we find no child item containing point.
  2246. } while (pguiCur != NULL);
  2247. }
  2248. return pguiRes;
  2249. }
  2250. ////////////////////////////////////////////////////////////////////////
  2251. //
  2252. // Returns TRUE if this item was allocated with CreateGuiItem().
  2253. //
  2254. ////////////////////////////////////////////////////////////////////////
  2255. short RGuiItem::IsDynamic(void) // Returns TRUE if this item was allocated with
  2256. // CreateGuiItem().
  2257. {
  2258. return IsProp(DYNAMIC_PROP_KEY);
  2259. }
  2260. ////////////////////////////////////////////////////////////////////////
  2261. //
  2262. // Get the text that represents this item.
  2263. //
  2264. ////////////////////////////////////////////////////////////////////////
  2265. short RGuiItem::GetText( // Returns 0 on success.
  2266. char* pszText, // Location to copy text to.
  2267. short sMax) // Total memory pointed to by pszText.
  2268. {
  2269. short sRes = 0; // Assume success.
  2270. if ((short)strlen(m_szText) < sMax)
  2271. {
  2272. strcpy(pszText, m_szText);
  2273. }
  2274. else
  2275. {
  2276. TRACE("GetText(): Not enough room to copy text (provided: %d, "
  2277. "needed: %d).\n", sMax, strlen(m_szText) + 1);
  2278. sRes = -1;
  2279. }
  2280. return sRes;
  2281. }
  2282. //////////////////////////////////////////////////////////////////////////////
  2283. //
  2284. // Get the text that represents the specified item.
  2285. //
  2286. //////////////////////////////////////////////////////////////////////////////
  2287. short RGuiItem::GetText( // Returns 0 on success.
  2288. long lId, // In: Child item ID (can identify this item).
  2289. char* pszText, // Out: Location to copy text to.
  2290. short sMax) // In: Total memory pointed to by pszText.
  2291. {
  2292. short sRes = 0; // Assume success.
  2293. RGuiItem* pgui = GetItemFromId(lId);
  2294. if (pgui != NULL)
  2295. {
  2296. sRes = pgui->GetText(pszText, sMax);
  2297. }
  2298. else
  2299. {
  2300. TRACE("GetText(): No such ID %ld.\n", lId);
  2301. sRes = -1;
  2302. }
  2303. return sRes;
  2304. }
  2305. //////////////////////////////////////////////////////////////////////////////
  2306. //
  2307. // Get the number represented by the text in this item.
  2308. //
  2309. //////////////////////////////////////////////////////////////////////////////
  2310. long RGuiItem::GetVal(void) // Returns value.
  2311. {
  2312. return atol(m_szText);
  2313. }
  2314. //////////////////////////////////////////////////////////////////////////////
  2315. //
  2316. // Get the number represented by the text in the specified item.
  2317. //
  2318. //////////////////////////////////////////////////////////////////////////////
  2319. long RGuiItem::GetVal( // Returns value.
  2320. long lId) // In: Child item ID (can identify this item).
  2321. {
  2322. long lRes = 0; // Assume value. Ayuh.
  2323. RGuiItem* pgui = GetItemFromId(lId);
  2324. if (pgui != NULL)
  2325. {
  2326. lRes = pgui->GetVal();
  2327. }
  2328. else
  2329. {
  2330. TRACE("GetVal(): No such ID %ld.\n", lId);
  2331. }
  2332. return lRes;
  2333. }
  2334. //////////////////////////////////////////////////////////////////////////////
  2335. //
  2336. // Gets the thickness of the top/left border (including border edge effect).
  2337. //
  2338. //////////////////////////////////////////////////////////////////////////////
  2339. short RGuiItem::GetTopLeftBorderThickness(void) // Returns border thickness
  2340. // including edge effect.
  2341. {
  2342. if (m_sBorderThickness == 0)
  2343. return 0;
  2344. else
  2345. return m_sBorderThickness + ((m_sInvertedBorder == FALSE) ? 0 : m_sBorderThickness);
  2346. }
  2347. //////////////////////////////////////////////////////////////////////////////
  2348. //
  2349. // Gets the thickness of the bottom/right border (including border edge effect).
  2350. //
  2351. //////////////////////////////////////////////////////////////////////////////
  2352. short RGuiItem::GetBottomRightBorderThickness(void) // Returns border thickness
  2353. // including edge effect.
  2354. {
  2355. if (m_sBorderThickness == 0)
  2356. return 0;
  2357. else
  2358. return m_sBorderThickness + ((m_sInvertedBorder == FALSE) ? m_sBorderThickness : 0);
  2359. }
  2360. //////////////////////////////////////////////////////////////////////////////
  2361. //
  2362. // Get the "client" area (i.e., non-border/title area) relative to this
  2363. // item.
  2364. // (virtual).
  2365. //
  2366. //////////////////////////////////////////////////////////////////////////////
  2367. void RGuiItem::GetClient( // Returns nothing.
  2368. short* psX, // Out: X position unless NULL.
  2369. short* psY, // Out: Y position unless NULL.
  2370. short* psW, // Out: Width unless NULL.
  2371. short* psH) // Out: Height unless NULL.
  2372. {
  2373. if (m_sBorderThickness == 0)
  2374. {
  2375. SET(psX, 0);
  2376. SET(psY, 0);
  2377. SET(psW, m_im.m_sWidth);
  2378. SET(psH, m_im.m_sHeight);
  2379. }
  2380. else
  2381. {
  2382. short sLeftTop = GetTopLeftBorderThickness();
  2383. short sRightBottom = GetBottomRightBorderThickness();
  2384. SET(psX, sLeftTop);
  2385. SET(psY, sLeftTop);
  2386. SET(psW, m_im.m_sWidth - sLeftTop - sRightBottom);
  2387. SET(psH, m_im.m_sHeight - sLeftTop - sRightBottom);
  2388. }
  2389. }
  2390. //////////////////////////////////////////////////////////////////////////////
  2391. //
  2392. // Get the "hot" area (i.e., clickable area) relative to this item.
  2393. // Only children within this area will be able to get mouse events.
  2394. // (virtual).
  2395. //
  2396. //////////////////////////////////////////////////////////////////////////////
  2397. void RGuiItem::GetHotArea( // Returns nothing.
  2398. short* psX, // Out: X position unless NULL.
  2399. short* psY, // Out: Y position unless NULL.
  2400. short* psW, // Out: Width unless NULL.
  2401. short* psH) // Out: Height unless NULL.
  2402. {
  2403. // Base implementation uses entire area.
  2404. SET(psX, 0);
  2405. SET(psY, 0);
  2406. SET(psW, m_im.m_sWidth);
  2407. SET(psH, m_im.m_sHeight);
  2408. }
  2409. //////////////////////////////////////////////////////////////////////////////
  2410. //
  2411. // Change the position specified to a top-level coord.
  2412. //
  2413. //////////////////////////////////////////////////////////////////////////////
  2414. void RGuiItem::ChildPosToTop( // Returns nothing.
  2415. short* psX, // In: Child pos, Out: Top level pos.
  2416. short* psY) // In: Child pos, Out: Top level pos.
  2417. {
  2418. // Offset current pos.
  2419. *psX += m_sX;
  2420. *psY += m_sY;
  2421. // If not yet the top level . . .
  2422. if (m_pguiParent != NULL)
  2423. {
  2424. // Pass it on.
  2425. m_pguiParent->ChildPosToTop(psX, psY);
  2426. }
  2427. }
  2428. //////////////////////////////////////////////////////////////////////////////
  2429. //
  2430. // Change the position specified to a child coord.
  2431. //
  2432. //////////////////////////////////////////////////////////////////////////////
  2433. void RGuiItem::TopPosToChild( // Returns nothing.
  2434. short* psX, // In: Top level pos, Out: Child pos.
  2435. short* psY) // In: Top level pos, Out: Child pos.
  2436. {
  2437. // Offset current pos.
  2438. *psX -= m_sX;
  2439. *psY -= m_sY;
  2440. // If not yet the top level . . .
  2441. if (m_pguiParent != NULL)
  2442. {
  2443. // Pass it on.
  2444. m_pguiParent->TopPosToChild(psX, psY);
  2445. }
  2446. }
  2447. //////////////////////////////////////////////////////////////////////////////
  2448. //
  2449. // Get the current file version (that is, the file version a file
  2450. // written from this GUI right now would be).
  2451. // (static).
  2452. //
  2453. //////////////////////////////////////////////////////////////////////////////
  2454. U32 RGuiItem::GetCurrentFileVersion(void)
  2455. {
  2456. return GUI_FILE_VERSION;
  2457. }
  2458. //////////////////////////////////////////////////////////////////////////////
  2459. // Statics.
  2460. //////////////////////////////////////////////////////////////////////////////
  2461. //////////////////////////////////////////////////////////////////////////////
  2462. // Sets the current focus GUI pointer to the supplied GUI.
  2463. // NOTE: Eventually, this function may do more, so it is better to call
  2464. // it than simply setting ms_pguiFocus to your gui.
  2465. //////////////////////////////////////////////////////////////////////////////
  2466. // static
  2467. RGuiItem* RGuiItem::SetFocus( // Returns pointer to GUI losing the focus.
  2468. RGuiItem* pguiNewFocus) // New item to focus or NULL for none.
  2469. {
  2470. // Store loser of focus.
  2471. RGuiItem* pguiLoseFocus = ms_pguiFocus;
  2472. // If this item does not already have the focus . . .
  2473. if (pguiNewFocus != ms_pguiFocus)
  2474. {
  2475. // Set new focus.
  2476. ms_pguiFocus = pguiNewFocus;
  2477. // If there is a loser . . .
  2478. if (pguiLoseFocus != NULL)
  2479. {
  2480. // Let the loser know.
  2481. pguiLoseFocus->OnLoseFocus();
  2482. }
  2483. // If the focus has not already changed and is an item . . .
  2484. if (ms_pguiFocus == pguiNewFocus && pguiNewFocus != NULL)
  2485. {
  2486. // Let the gainer know.
  2487. pguiNewFocus->OnGainFocus();
  2488. }
  2489. }
  2490. // Return loser of focus.
  2491. return pguiLoseFocus;
  2492. }
  2493. //////////////////////////////////////////////////////////////////////////////
  2494. // Moves the focus to the next GUI at the same level as the current
  2495. // ms_pguiFocus. Does not affect current focus for top-level guis.
  2496. //////////////////////////////////////////////////////////////////////////////
  2497. // static
  2498. RGuiItem* RGuiItem::FocusNext(void) // Returns new item with focus or NULL,
  2499. // if none.
  2500. {
  2501. // If there is a current . . .
  2502. if (ms_pguiFocus != NULL)
  2503. {
  2504. // If it has a parent . . .
  2505. RGuiItem* pguiParent = ms_pguiFocus->GetParent();
  2506. if (pguiParent != NULL)
  2507. {
  2508. // Get next . . .
  2509. RGuiItem* pguiFocus = pguiParent->m_listguiChildren.GetLogicalNext(ms_pguiFocus);
  2510. // Remember where we started so we can detect complete circle.
  2511. RGuiItem* pguiStart = pguiFocus;
  2512. while (pguiFocus != NULL)
  2513. {
  2514. // If visible, activated, and doesn't focus siblings . . .
  2515. if ( pguiFocus->m_sVisible != FALSE
  2516. && pguiFocus->IsActivated() != FALSE
  2517. && pguiFocus->m_targetFocus != Sibling)
  2518. {
  2519. break;
  2520. }
  2521. // Try next.
  2522. pguiFocus = pguiParent->m_listguiChildren.GetLogicalNext();
  2523. // If we wrapped . . .
  2524. if (pguiFocus == pguiStart)
  2525. {
  2526. pguiFocus = NULL;
  2527. }
  2528. }
  2529. // Set focus to the new item.
  2530. SetFocus(pguiFocus);
  2531. }
  2532. }
  2533. return ms_pguiFocus;
  2534. }
  2535. //////////////////////////////////////////////////////////////////////////////
  2536. // Moves the focus to the previous GUI at the same level as the current
  2537. // ms_pguiFocus. Does not affect current focus for top-level guis.
  2538. //////////////////////////////////////////////////////////////////////////////
  2539. // static
  2540. RGuiItem* RGuiItem::FocusPrev(void) // Returns new item with focus or NULL,
  2541. // if none.
  2542. {
  2543. // If there is a current . . .
  2544. if (ms_pguiFocus != NULL)
  2545. {
  2546. // If it has a parent . . .
  2547. RGuiItem* pguiParent = ms_pguiFocus->GetParent();
  2548. if (pguiParent != NULL)
  2549. {
  2550. // Get previous . . .
  2551. RGuiItem* pguiFocus = pguiParent->m_listguiChildren.GetLogicalPrev(ms_pguiFocus);
  2552. // Remember where we started so we can detect complete circle.
  2553. RGuiItem* pguiStart = pguiFocus;
  2554. while (pguiFocus != NULL)
  2555. {
  2556. // If visible, activated, and doesn't focus siblings . . .
  2557. if ( pguiFocus->m_sVisible != FALSE
  2558. && pguiFocus->IsActivated() != FALSE
  2559. && pguiFocus->m_targetFocus != Sibling)
  2560. {
  2561. break;
  2562. }
  2563. // Try prev.
  2564. pguiFocus = pguiParent->m_listguiChildren.GetLogicalPrev();
  2565. // If we wrapped . . .
  2566. if (pguiFocus == pguiStart)
  2567. {
  2568. pguiFocus = NULL;
  2569. }
  2570. }
  2571. // Set focus to the new item.
  2572. SetFocus(pguiFocus);
  2573. }
  2574. }
  2575. return ms_pguiFocus;
  2576. }
  2577. //////////////////////////////////////////////////////////////////////////////
  2578. //
  2579. // Sends the event to the GUI focused via Do(), and, if event is not
  2580. // used, checks for keys that might change the focus.
  2581. // (static).
  2582. //
  2583. //////////////////////////////////////////////////////////////////////////////
  2584. void RGuiItem::DoFocus( // Returns nothing.
  2585. RInputEvent* pie) // In: Most recent user input event.
  2586. // Out: pie->sUsed = TRUE, if used.
  2587. {
  2588. // If there is an item with the focus . . .
  2589. if (ms_pguiFocus != NULL)
  2590. {
  2591. // Pass event on.
  2592. ms_pguiFocus->Do(pie);
  2593. }
  2594. // If unused key event . . .
  2595. if (pie->sUsed == FALSE && pie->type == RInputEvent::Key)
  2596. {
  2597. // Assume used. Easier.
  2598. pie->sUsed = TRUE;
  2599. switch (pie->lKey)
  2600. {
  2601. // Tab == Next item at current level.
  2602. case '\t':
  2603. FocusNext();
  2604. break;
  2605. // Shift-Tab == Prev item at current level.
  2606. case (RSP_GKF_SHIFT | '\t'):
  2607. FocusPrev();
  2608. break;
  2609. // Control-Tab == Child.
  2610. case (RSP_GKF_CONTROL | '\t'):
  2611. // If there's currently an item in focus . . .
  2612. if (ms_pguiFocus != NULL)
  2613. {
  2614. // Get first child in list of children.
  2615. SetFocus(ms_pguiFocus->m_listguiChildren.GetHead() );
  2616. }
  2617. break;
  2618. // Control-Shift-Tab == Parent.
  2619. case (RSP_GKF_CONTROL | RSP_GKF_SHIFT | '\t'):
  2620. // If there's currently an item in focus . . .
  2621. if (ms_pguiFocus != NULL)
  2622. {
  2623. // Get parent.
  2624. SetFocus(ms_pguiFocus->GetParent() );
  2625. }
  2626. break;
  2627. default: // Unused.
  2628. // Well, better set it back to unused.
  2629. pie->sUsed = FALSE;
  2630. break;
  2631. }
  2632. }
  2633. }
  2634. //////////////////////////////////////////////////////////////////////////////
  2635. //
  2636. // Creates the specified object and returns a pointer to it.
  2637. // The object is created with new, so use delete to destroy the object.
  2638. // (static).
  2639. //
  2640. //////////////////////////////////////////////////////////////////////////////
  2641. RGuiItem* RGuiItem::CreateGuiItem( // Returns the allocated type on success.
  2642. // Returns NULL on error.
  2643. Type type) // Type of GuiItem to allocate. Must be one
  2644. // of the enums that is a member of
  2645. // RGuiItem::Type.
  2646. {
  2647. RGuiItem* pgui = NULL; // Assume failure.
  2648. switch (type)
  2649. {
  2650. case GuiItem: // Generic GuiItem.
  2651. pgui = new RGuiItem;
  2652. break;
  2653. case Txt: // RTxt item.
  2654. pgui = new RTxt;
  2655. break;
  2656. case Btn: // RBtn item.
  2657. pgui = new RBtn;
  2658. break;
  2659. case Edit: // REdit item.
  2660. pgui = new REdit;
  2661. break;
  2662. case ScrollBar: // RScrollBar item.
  2663. pgui = new RScrollBar;
  2664. break;
  2665. case Dlg: // RDlg item.
  2666. pgui = new RDlg;
  2667. break;
  2668. case ListBox: // RListBox item.
  2669. pgui = new RListBox;
  2670. break;
  2671. case PushBtn: // RPushBtn item.
  2672. pgui = new RPushBtn;
  2673. break;
  2674. case MultiBtn: // RMultiBtn item.
  2675. pgui = new RMultiBtn;
  2676. break;
  2677. default:
  2678. TRACE("CreateGuiItem(): Unknown type %d.\n", type);
  2679. break;
  2680. }
  2681. // If successfully allocated . . .
  2682. if (pgui != NULL)
  2683. {
  2684. // Mark as dynamic.
  2685. pgui->SetProp(DYNAMIC_PROP_KEY, TRUE);
  2686. }
  2687. return pgui;
  2688. }
  2689. //////////////////////////////////////////////////////////////////////////////
  2690. //
  2691. // Destroys the specified object utilizing the 'type' to make sure all
  2692. // appropriate destruction for that type is done. This function is
  2693. // useful for delete'ing objects that may be descendants of RGuiItem
  2694. // when you do not know the type of the object. Note this only works
  2695. // for standard types.
  2696. // (static).
  2697. //
  2698. //////////////////////////////////////////////////////////////////////////////
  2699. void RGuiItem::DestroyGuiItem( // Returns nothing.
  2700. RGuiItem* pgui) // Pointer to gui to deallocate.
  2701. {
  2702. ASSERT(pgui != NULL);
  2703. switch (pgui->m_type)
  2704. {
  2705. case GuiItem: // Generic GuiItem.
  2706. delete pgui;
  2707. break;
  2708. case Txt: // RTxt item.
  2709. delete (RTxt*)pgui;
  2710. break;
  2711. case Btn: // RBtn item.
  2712. delete (RBtn*)pgui;
  2713. break;
  2714. case Edit: // REdit item.
  2715. delete (REdit*)pgui;
  2716. break;
  2717. case ScrollBar: // RScrollBar item.
  2718. delete (RScrollBar*)pgui;
  2719. break;
  2720. case Dlg: // RDlg item.
  2721. delete (RDlg*)pgui;
  2722. break;
  2723. case ListBox: // RListBox item.
  2724. delete (RListBox*)pgui;
  2725. break;
  2726. case PushBtn: // RPushBtn item.
  2727. delete (RPushBtn*)pgui;
  2728. break;
  2729. case MultiBtn: // RMultiBtn item.
  2730. delete (RMultiBtn*)pgui;
  2731. break;
  2732. default:
  2733. TRACE("DestroyGuiItem(): Unknown type %d.\n", pgui->m_type);
  2734. TRACE("DestroyGuiItem(): Using RGuiItem delete.\n");
  2735. delete pgui;
  2736. break;
  2737. }
  2738. }
  2739. //////////////////////////////////////////////////////////////////////////////
  2740. // EOF
  2741. //////////////////////////////////////////////////////////////////////////////