toolbar.cpp 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572
  1. #include "main.h"
  2. #include "./toolbar.h"
  3. #include "./graphics.h"
  4. #include "./resource.h"
  5. #include "./toolbarItem.h"
  6. #include "./toolbarStatic.h"
  7. #include "./toolbarButton.h"
  8. #include "./toolbarRating.h"
  9. #include "./toolbarProgress.h"
  10. #include "./toolbarAddress.h"
  11. #include "../winamp/wa_dlg.h"
  12. #include "../Plugins/General/gen_ml/ml_ipc_0313.h"
  13. #include "./ifc_imageloader.h"
  14. #include "./ifc_skinhelper.h"
  15. #include "./ifc_omservice.h"
  16. #include "./ifc_omservicecommand.h"
  17. #include "./menu.h"
  18. #include "./ifc_wasabihelper.h"
  19. #include "./browserUiCommon.h"
  20. #include <windows.h>
  21. #include <shlwapi.h>
  22. #include <commctrl.h>
  23. #include <strsafe.h>
  24. #include <vector>
  25. #define TOOLBAR_SPACE_LEFT 0
  26. #define TOOLBAR_SPACE_TOP 4
  27. #define TOOLBAR_SPACE_RIGHT 1
  28. #define TOOLBAR_SPACE_BOTTOM 3
  29. #define TOOLBAR_HIDDENHEIGHT 3
  30. #define TOOLBAR_ICONSTATE_NORMAL 0
  31. #define TOOLBAR_ICONSTATE_HIGHLIGHTED 1
  32. #define TOOLBAR_ICONSTATE_PRESSED 2
  33. #define TOOLBAR_ICONSTATE_DISABLED 3
  34. #define TOOLBAR_ICONSTATE_COUNT 4
  35. #define TBS_MENULOOP 0x00000001
  36. #define TBS_HIDDEN 0x00000002
  37. #define TBS_HIDETIMERSET 0x00000004
  38. #define TBS_NOFOCUSRECT 0x00000008
  39. #define ICON_NONE (-1)
  40. #define ICON_CHEVRON (-2) // - resolved on the fly
  41. #define ICON_SEPARATOR 0
  42. #define ICON_CHEVRON_BOTTOM 1
  43. #define ICON_CHEVRON_TOP 2
  44. #define ICON_BACK 3
  45. #define ICON_FORWARD 4
  46. #define ICON_REFRESH 5
  47. #define ICON_STOP 6
  48. #define ICON_HOME 7
  49. #define ICON_ERROR 8
  50. #define ICON_LOCK 9
  51. #define ICON_HISTORY 10
  52. #define ICON_ADDRESSBAR 11
  53. #define ICON_SEPARATOR_WIDTH 6
  54. #define ICON_CHEVRON_WIDTH 13
  55. #define ICON_HISTORY_WIDTH 9
  56. #define ICON_ARROW_WIDTH 17
  57. #define TOOLBRUSH_BACK 0
  58. #define TOOLBRUSH_FRAME 1
  59. #define TOOLBRUSH_ITEMBK 2
  60. #define TOOLBRUSH_LAST TOOLBRUSH_ITEMBK
  61. #define ID_CHEVRON_CLICKED 1
  62. #define TOOLBAR_BKCOLOR WADLG_LISTHEADER_BGCOLOR
  63. #define TOOLBAR_FGCOLOR WADLG_LISTHEADER_FONTCOLOR
  64. #define EDIT_ALPHA 50
  65. #define TEXT_ALPHA 127
  66. #define HIGHLIGHT_ALPHA 160
  67. #define TIMER_AUTOHIDE_ID 14
  68. #define TIMER_AUTOHIDE_DELAY 300
  69. #define AUTOHIDE_ANIMATETIME 200
  70. typedef std::vector<ToolbarItem*> ItemList;
  71. typedef struct __TOOLBAR
  72. {
  73. UINT ref;
  74. UINT flags;
  75. HBRUSH szBrushes[TOOLBRUSH_LAST + 1];
  76. HIMAGELIST imageList;
  77. HFONT textFont;
  78. ItemList *items;
  79. ToolbarItem *chevron;
  80. ToolbarItem *pressed;
  81. ToolbarItem *highlighted;
  82. UINT resolveCache;
  83. HFONT ownedFont;
  84. HWND hTooltip;
  85. COLORREF rgbBk;
  86. COLORREF rgbFg;
  87. COLORREF rgbText;
  88. COLORREF rgbHilite;
  89. COLORREF rgbFrame;
  90. COLORREF rgbEdit;
  91. COLORREF rgbEditBk;
  92. size_t iFocused;
  93. HWND hBrowser;
  94. TOOLBARTEXTMETRIC textMetric;
  95. } TOOLBAR;
  96. typedef struct __TOOLBARMOUSEHOOK
  97. {
  98. HHOOK hHook;
  99. HWND hwnd;
  100. } TOOLBARMOUSEHOOK;
  101. static size_t tlsIndex = TLS_OUT_OF_INDEXES;
  102. typedef ToolbarItem *(CALLBACK *TOOLBARITEMFACTORY)(ToolbarItem::Template * /*itemTemplate*/);
  103. typedef struct __TOOLBARITEMCLASS
  104. {
  105. LPCSTR name;
  106. TOOLBARITEMFACTORY creator;
  107. LPCWSTR text;
  108. LPCWSTR description;
  109. INT iconId;
  110. INT commandId;
  111. UINT style;
  112. } TOOLBARITEMCLASS;
  113. #define REGISTER_ITEM(__name, __creator, __textId, __descriptionId, __iconId, __commandId, __style)\
  114. { (__name), (__creator), MAKEINTRESOURCE(__textId),\
  115. MAKEINTRESOURCE(__descriptionId), (__iconId), (__commandId), (__style) }
  116. #define REGISTER_STATIC(__name, __textId, __descriptionId, __iconId, __style)\
  117. REGISTER_ITEM(__name, ToolbarStatic::CreateInstance, __textId, __descriptionId, __iconId, 0, __style)
  118. #define REGISTER_BUTTON(__name, __textId, __descriptionId, __iconId, __commandId, __style)\
  119. REGISTER_ITEM(__name, ToolbarButton::CreateInstance, __textId, __descriptionId, __iconId, __commandId, __style)
  120. #define REGISTER_RATING(__name, __textId, __descriptionId, __style)\
  121. REGISTER_ITEM(__name, ToolbarRating::CreateInstance, __textId, __descriptionId, ICON_NONE, 0, __style)
  122. #define REGISTER_PROGRESS(__name, __textId, __descriptionId, __style)\
  123. REGISTER_ITEM(__name, ToolbarProgress::CreateInstance, __textId, __descriptionId, ICON_NONE, 0, __style)
  124. #define REGISTER_ADDRESSBAR(__name, __style)\
  125. REGISTER_ITEM(__name, ToolbarAddress::CreateInstance, NULL, NULL, ICON_NONE, 0, __style)
  126. const static TOOLBARITEMCLASS szRegisteredToolItems[] =
  127. {
  128. REGISTER_STATIC(TOOLITEM_SEPARATOR, IDS_SEPARATOR, 0, ICON_SEPARATOR, ToolbarStatic::styleSeparator),
  129. REGISTER_STATIC(TOOLITEM_SPACE, IDS_SPACE, 0, ICON_NONE, ToolbarStatic::styleSpacer),
  130. REGISTER_STATIC(TOOLITEM_FLEXSPACE, IDS_FLEXSPACE, 0, ICON_NONE, ToolbarStatic::styleSpacer | ToolbarItem::styleFlexible),
  131. REGISTER_RATING(TOOLITEM_USERRATING, IDS_RATED, NULL, ToolbarItem::styleWantKey),
  132. REGISTER_PROGRESS(TOOLITEM_DOWNLOADPROGRESS, NULL, NULL, ToolbarItem::stateDisabled),
  133. REGISTER_ADDRESSBAR(TOOLITEM_ADDRESSBAR, ToolbarItem::styleFlexible | ToolbarItem::styleWantKey | ToolbarItem::styleTabstop),
  134. REGISTER_BUTTON(TOOLITEM_CHEVRON, IDS_MORE, IDS_MORE_DESCRIPTION, ICON_CHEVRON,
  135. ID_CHEVRON_CLICKED, ToolbarItem::styleWantKey),
  136. REGISTER_BUTTON(TOOLITEM_BUTTON_HOME, IDS_HOME, IDS_HOME_DESCRIPTION, ICON_HOME,
  137. ID_NAVIGATION_HOME, ToolbarItem::styleWantKey),
  138. REGISTER_BUTTON(TOOLITEM_BUTTON_BACK, IDS_BACK, IDS_BACK_DESCRIPTION, ICON_BACK,
  139. ID_NAVIGATION_BACK, ToolbarItem::styleWantKey),
  140. REGISTER_BUTTON(TOOLITEM_BUTTON_FORWARD, IDS_FORWARD, IDS_FORWARD_DESCRIPTION, ICON_FORWARD,
  141. ID_NAVIGATION_FORWARD, ToolbarItem::styleWantKey),
  142. REGISTER_BUTTON(TOOLITEM_BUTTON_STOP, IDS_STOP, IDS_STOP_DESCRIPTION, ICON_STOP,
  143. ID_NAVIGATION_STOP, ToolbarItem::styleWantKey),
  144. REGISTER_BUTTON(TOOLITEM_BUTTON_REFRESH, IDS_REFRESH, IDS_REFRESH_DESCRIPTION, ICON_REFRESH,
  145. ID_NAVIGATION_REFRESH, ToolbarItem::styleWantKey),
  146. REGISTER_BUTTON(TOOLITEM_BUTTON_HISTORY, IDS_HISTORY, IDS_HISTORY_DESCRIPTION, ICON_HISTORY,
  147. ID_NAVIGATION_HISTORY, ToolbarItem::styleWantKey),
  148. REGISTER_BUTTON(TOOLITEM_CMDLINK_INFO, IDS_SERVICE_GETINFO, IDS_SERVICE_GETINFO_DESCRIPTION, ICON_NONE,
  149. ID_SERVICE_GETINFO, ToolbarItem::styleWantKey | ToolbarButton::styleCommandLink),
  150. REGISTER_BUTTON(TOOLITEM_CMDLINK_REPORT, IDS_SERVICE_REPORT, IDS_SERVICE_REPORT_DESCRIPTION, ICON_NONE,
  151. ID_SERVICE_REPORT, ToolbarItem::styleWantKey | ToolbarButton::styleCommandLink),
  152. REGISTER_BUTTON(TOOLITEM_CMDLINK_UNSUBSCRIBE, IDS_SERVICE_UNSUBSCRIBE, IDS_SERVICE_UNSUBSCRIBE_DESCRIPTION, ICON_NONE,
  153. ID_SERVICE_UNSUBSCRIBE, ToolbarItem::styleWantKey | ToolbarButton::styleCommandLink),
  154. REGISTER_BUTTON(TOOLITEM_BUTTON_SECURECONNECTION, IDS_SECURE_CONNECTION, IDS_SECURE_CONNECTION, ICON_LOCK,
  155. ID_BROWSER_SECURECONNECTION, ToolbarItem::styleWantKey),
  156. REGISTER_BUTTON(TOOLITEM_BUTTON_SCRIPTERROR, IDS_SCRIPT_ERROR, IDS_SCRIPT_ERROR_DESCRIPTION, ICON_ERROR,
  157. ID_BROWSER_SCRIPTERROR, ToolbarItem::styleWantKey),
  158. };
  159. #define GetToolbar(__hwnd) ((TOOLBAR*)(LONG_PTR)(LONGX86)GetWindowLongPtr((__hwnd), 0))
  160. static LRESULT CALLBACK Toolbar_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  161. static LRESULT CALLBACK Toolbar_MouseHook(INT code, WPARAM wParam, LPARAM lParam);
  162. typedef struct __TOOLBARITEMINSERTREC
  163. {
  164. LPCSTR name;
  165. UINT style;
  166. } TOOLBARITEMINSERTREC;
  167. BOOL Toolbar_RegisterClass(HINSTANCE hInstance)
  168. {
  169. WNDCLASS wc;
  170. ATOM klassAtom;
  171. ifc_wasabihelper *wasabi;
  172. if (GetClassInfo(hInstance, NWC_ONLINEMEDIATOOLBAR, &wc))
  173. return TRUE;
  174. ZeroMemory(&wc, sizeof(WNDCLASS));
  175. wc.hInstance = hInstance;
  176. wc.lpszClassName = NWC_ONLINEMEDIATOOLBAR;
  177. wc.lpfnWndProc = Toolbar_WindowProc;
  178. wc.style = 0;
  179. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  180. wc.hbrBackground = NULL;
  181. wc.cbWndExtra = sizeof(TOOLBAR*);
  182. klassAtom = RegisterClassW(&wc);
  183. if (0 == klassAtom)
  184. return FALSE;
  185. if (SUCCEEDED(Plugin_GetWasabiHelper(&wasabi)))
  186. {
  187. api_application *application;
  188. if (SUCCEEDED(wasabi->GetApplicationApi(&application)))
  189. {
  190. application->DirectMouseWheel_RegisterSkipClass(klassAtom);
  191. application->Release();
  192. }
  193. wasabi->Release();
  194. }
  195. return TRUE;
  196. }
  197. static BOOL Toolbar_ClearItems(TOOLBAR *toolbar)
  198. {
  199. if (NULL == toolbar) return FALSE;
  200. if (NULL == toolbar->items) return TRUE;
  201. size_t index = toolbar->items->size();
  202. while(index--)
  203. {
  204. ToolbarItem *item = toolbar->items->at(index);
  205. if (NULL != item) item->Release();
  206. }
  207. toolbar->items->clear();
  208. return TRUE;
  209. }
  210. static ULONG Toolbar_AddRef(TOOLBAR *toolbar)
  211. {
  212. if (NULL == toolbar) return 0;
  213. return InterlockedIncrement((LONG*)&toolbar->ref);
  214. }
  215. static ULONG Toolbar_Release(TOOLBAR *toolbar)
  216. {
  217. if (0 == toolbar || 0 == toolbar->ref) return 0;
  218. LONG r = InterlockedDecrement((LONG*)&toolbar->ref);
  219. if (0 == r)
  220. {
  221. if (NULL != toolbar->hTooltip)
  222. DestroyWindow(toolbar->hTooltip);
  223. if (NULL != toolbar->chevron)
  224. toolbar->chevron->Release();
  225. if (NULL != toolbar->items)
  226. {
  227. Toolbar_ClearItems(toolbar);
  228. delete(toolbar->items);
  229. }
  230. if (NULL != toolbar->imageList)
  231. ImageList_Destroy(toolbar->imageList);
  232. if (NULL != toolbar->ownedFont)
  233. DeleteObject(toolbar->ownedFont);
  234. free(toolbar);
  235. }
  236. return r;
  237. }
  238. static const TOOLBARITEMCLASS *Toolbar_FindClass(LPCSTR pszName)
  239. {
  240. if (NULL == pszName) return NULL;
  241. for (INT i =0; i < ARRAYSIZE(szRegisteredToolItems); i++)
  242. {
  243. if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, NORM_IGNORECASE,
  244. szRegisteredToolItems[i].name, -1, pszName, -1))
  245. {
  246. return &szRegisteredToolItems[i];
  247. }
  248. }
  249. return NULL;
  250. }
  251. static ToolbarItem* Toolbar_CreateItem(LPCSTR pszName, UINT styleOverride)
  252. {
  253. const TOOLBARITEMCLASS *iClass = Toolbar_FindClass(pszName);
  254. if (NULL == iClass || NULL == iClass->creator)
  255. return NULL;
  256. ToolbarItem::Template iTemplate;
  257. ZeroMemory(&iTemplate, sizeof(ToolbarItem::Template));
  258. iTemplate.name = iClass->name;
  259. iTemplate.text = iClass->text;
  260. iTemplate.description= iClass->description;
  261. iTemplate.iconId = iClass->iconId;
  262. iTemplate.commandId = iClass->commandId;
  263. iTemplate.style = iClass->style | styleOverride;
  264. return iClass->creator(&iTemplate);
  265. }
  266. static BOOL Toolbar_GetClientRect(HWND hwnd, RECT *prc)
  267. {
  268. if (!GetClientRect(hwnd, prc))
  269. return FALSE;
  270. prc->left += TOOLBAR_SPACE_LEFT;
  271. prc->right -= TOOLBAR_SPACE_RIGHT;
  272. if (0 != (TBS_BOTTOMDOCK & GetWindowLongPtr(hwnd, GWL_STYLE)))
  273. {
  274. prc->top += TOOLBAR_SPACE_TOP;
  275. prc->bottom -= TOOLBAR_SPACE_BOTTOM;
  276. }
  277. else
  278. {
  279. prc->top += TOOLBAR_SPACE_BOTTOM;
  280. prc->bottom -= TOOLBAR_SPACE_TOP;
  281. }
  282. return TRUE;
  283. }
  284. static void Toolbar_UpdateColorTable(HWND hwnd, ifc_skinhelper *skinHelper)
  285. {
  286. TOOLBAR *toolbar = GetToolbar(hwnd);
  287. if (NULL == toolbar) return;
  288. if (NULL == skinHelper || FAILED(skinHelper->GetColor(TOOLBAR_BKCOLOR, &toolbar->rgbBk)))
  289. toolbar->rgbBk = GetSysColor(COLOR_WINDOW);
  290. if (NULL == skinHelper || FAILED(skinHelper->GetColor(TOOLBAR_FGCOLOR, &toolbar->rgbFg)))
  291. toolbar->rgbFg = GetSysColor(COLOR_WINDOWTEXT);
  292. COLORREF rgbItemBk;
  293. if (NULL == skinHelper || FAILED(skinHelper->GetColor(WADLG_ITEMBG, &rgbItemBk)))
  294. rgbItemBk = GetSysColor(COLOR_WINDOW);
  295. COLORREF rgbItem;
  296. if (NULL == skinHelper || FAILED(skinHelper->GetColor(WADLG_ITEMFG, &rgbItem)))
  297. rgbItem = GetSysColor(COLOR_WINDOWTEXT);
  298. if (NULL != skinHelper)
  299. {
  300. const INT szFrameColors[] =
  301. {
  302. WADLG_HILITE,
  303. WADLG_LISTHEADER_FRAME_MIDDLECOLOR,
  304. WADLG_LISTHEADER_FRAME_BOTTOMCOLOR,
  305. WADLG_LISTHEADER_FRAME_TOPCOLOR,
  306. };
  307. for (INT i = 0; i < ARRAYSIZE(szFrameColors); i++)
  308. {
  309. if (SUCCEEDED(skinHelper->GetColor(szFrameColors[i], &toolbar->rgbFrame)))
  310. toolbar->rgbFrame = GetSysColor(COLOR_GRAYTEXT);
  311. INT distance = GetColorDistance(toolbar->rgbFrame, rgbItemBk);
  312. if (distance < 0) distance = -distance;
  313. if (distance >= 40) break;
  314. }
  315. }
  316. else
  317. toolbar->rgbFrame = GetSysColor(COLOR_GRAYTEXT);
  318. toolbar->rgbEdit = BlendColors(toolbar->rgbBk, rgbItem, EDIT_ALPHA);
  319. toolbar->rgbEditBk = BlendColors(toolbar->rgbBk, rgbItemBk, EDIT_ALPHA);
  320. toolbar->rgbText = BlendColors(toolbar->rgbFg, toolbar->rgbBk, TEXT_ALPHA);
  321. toolbar->rgbHilite = BlendColors(toolbar->rgbFg, toolbar->rgbBk, HIGHLIGHT_ALPHA);
  322. }
  323. static void Toolbar_UpdateTextMetrics(HWND hwnd)
  324. {
  325. TOOLBAR *toolbar = GetToolbar(hwnd);
  326. if (NULL == toolbar) return;
  327. ZeroMemory(&toolbar->textMetric, sizeof(TOOLBARTEXTMETRIC));
  328. RECT clientRect;
  329. if (!Toolbar_GetClientRect(hwnd, &clientRect))
  330. return;
  331. INT iconCX, iconCY;
  332. if (NULL == toolbar->imageList ||
  333. FALSE == ImageList_GetIconSize(toolbar->imageList, &iconCX, &iconCY))
  334. {
  335. iconCX = 0;
  336. iconCY = 0;
  337. }
  338. else
  339. {
  340. iconCY = iconCY / TOOLBAR_ICONSTATE_COUNT;
  341. }
  342. HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_NORESETATTRS);
  343. if (NULL == hdc) return;
  344. HFONT originalFont = (HFONT)SelectObject(hdc, toolbar->textFont);
  345. TEXTMETRIC tm;
  346. if (GetTextMetrics(hdc, &tm))
  347. {
  348. toolbar->textMetric.height = tm.tmHeight;
  349. toolbar->textMetric.aveCharWidth = tm.tmAveCharWidth;
  350. toolbar->textMetric.overhang = tm.tmOverhang;
  351. INT clientHeight = clientRect.bottom - clientRect.top;
  352. if (tm.tmHeight >= iconCY)
  353. {
  354. toolbar->textMetric.baseY = tm.tmAscent - tm.tmInternalLeading;
  355. INT t = clientHeight - toolbar->textMetric.baseY;
  356. toolbar->textMetric.origY = clientRect.top + t/2;
  357. if (0 != t%2)
  358. {
  359. toolbar->textMetric.origY += 1;
  360. toolbar->textMetric.baseY += 1;
  361. }
  362. }
  363. else
  364. {
  365. toolbar->textMetric.baseY = (clientHeight - iconCY)/2;
  366. if (tm.tmDescent > toolbar->textMetric.baseY)
  367. toolbar->textMetric.baseY = tm.tmDescent;
  368. //toolbar->textMetric.baseY += 8;
  369. toolbar->textMetric.baseY = clientHeight - toolbar->textMetric.baseY;
  370. toolbar->textMetric.origY = (clientRect.top + toolbar->textMetric.baseY + tm.tmDescent) - tm.tmHeight;
  371. toolbar->textMetric.baseY -= toolbar->textMetric.origY;
  372. }
  373. }
  374. SelectObject(hdc, originalFont);
  375. ReleaseDC(hwnd, hdc);
  376. }
  377. static HIMAGELIST Toolbar_LoadImagelist(TOOLBAR *toolbar, HINSTANCE hInstance, LPCWSTR pszPath, INT iconWidth, INT iconStatesCount)
  378. {
  379. if (iconWidth <= 0 || iconStatesCount <= 0 || NULL == pszPath)
  380. return NULL;
  381. HBITMAP bitmap = NULL;
  382. ifc_omimageloader *loader;
  383. if (SUCCEEDED(Plugin_QueryImageLoader(hInstance, pszPath, FALSE, &loader)))
  384. {
  385. loader->LoadBitmap(&bitmap, NULL, NULL);
  386. loader->Release();
  387. }
  388. DIBSECTION dib;
  389. if (NULL == bitmap ||
  390. sizeof(DIBSECTION) != GetObject(bitmap, sizeof(DIBSECTION), &dib))
  391. {
  392. if (NULL != bitmap) DeleteObject(bitmap);
  393. return NULL;
  394. }
  395. if (dib.dsBm.bmHeight < 0) dib.dsBm.bmHeight = -dib.dsBm.bmHeight;
  396. Image_Colorize((BYTE*)dib.dsBm.bmBits, dib.dsBm.bmWidth, dib.dsBm.bmHeight, dib.dsBm.bmBitsPixel,
  397. ColorAdjustLuma(toolbar->rgbBk, -140, TRUE), toolbar->rgbFg, FALSE);
  398. Image_BlendOnColorEx((BYTE*)dib.dsBm.bmBits, dib.dsBm.bmWidth, dib.dsBm.bmHeight,
  399. 0, 0, dib.dsBm.bmWidth, dib.dsBm.bmHeight, dib.dsBm.bmBitsPixel, FALSE, toolbar->rgbBk);
  400. if (iconWidth > dib.dsBm.bmWidth) iconWidth = dib.dsBm.bmWidth;
  401. INT iconHeight = dib.dsBm.bmHeight / iconStatesCount;
  402. INT initial = dib.dsBm.bmWidth / iconWidth;
  403. HIMAGELIST himl = ImageList_Create(iconWidth, iconHeight * iconStatesCount, ILC_COLOR24, initial, 1);
  404. if (NULL != himl)
  405. {
  406. INT index = ImageList_Add(himl, bitmap, NULL);
  407. if (-1 == index)
  408. {
  409. ImageList_Destroy(himl);
  410. himl = NULL;
  411. }
  412. }
  413. DeleteObject(bitmap);
  414. return himl;
  415. }
  416. static LONG Toolbar_ShowChevron(HWND hwnd, const RECT *prcClient) // return client cx change
  417. {
  418. TOOLBAR *toolbar = GetToolbar(hwnd);
  419. if (NULL != toolbar && NULL != toolbar->chevron && toolbar->chevron->IsRectEmpty())
  420. {
  421. RECT chevronRect;
  422. CopyRect(&chevronRect, prcClient);
  423. if (toolbar->chevron->AdjustRect(hwnd, &chevronRect))
  424. {
  425. chevronRect.left = prcClient->right - (chevronRect.right - chevronRect.left);
  426. chevronRect.right = prcClient->right;
  427. if (toolbar->chevron->SetRect(&chevronRect))
  428. return (chevronRect.left - prcClient->right);
  429. }
  430. }
  431. return 0;
  432. }
  433. static ToolbarItem *Toolbar_GetItem(TOOLBAR *toolbar, size_t index)
  434. {
  435. if (NULL == toolbar || NULL == toolbar->items)
  436. return NULL;
  437. size_t count = toolbar->items->size();
  438. if (index < count)
  439. return toolbar->items->at(index);
  440. if (index == count)
  441. return toolbar->chevron;
  442. return NULL;
  443. }
  444. static BOOL Toolbar_IsItemAcceptFocus(ToolbarItem *item, BOOL fTabstopOnly)
  445. {
  446. if (NULL == item)
  447. return FALSE;
  448. UINT itemStyle = item->GetStyle();
  449. if (0 != ((ToolbarItem::stateDisabled | ToolbarItem::stateHidden) & itemStyle))
  450. return FALSE;
  451. UINT mask = ToolbarItem::styleTabstop;
  452. if (FALSE == fTabstopOnly) mask |= ToolbarItem::styleWantKey;
  453. if (0 == (mask & itemStyle))
  454. return FALSE;
  455. return (FALSE == item->IsRectEmpty());
  456. }
  457. static void Toolbar_UpdateTabstop(HWND hwnd)
  458. {
  459. BOOL fEnable = FALSE;
  460. UINT windowStyle = GetWindowStyle(hwnd);
  461. if (0 != (TBS_TABSTOP & windowStyle))
  462. {
  463. fEnable = TRUE;
  464. }
  465. else
  466. {
  467. TOOLBAR *toolbar = GetToolbar(hwnd);
  468. if (NULL != toolbar && NULL != toolbar->items)
  469. {
  470. size_t index = toolbar->items->size();
  471. while(index--)
  472. {
  473. if (FALSE != Toolbar_IsItemAcceptFocus(toolbar->items->at(index), TRUE))
  474. {
  475. fEnable = TRUE;
  476. break;
  477. }
  478. }
  479. }
  480. }
  481. if (FALSE != fEnable)
  482. {
  483. if (0 == (WS_TABSTOP & windowStyle))
  484. SetWindowLongPtr(hwnd, GWL_STYLE, windowStyle | WS_TABSTOP);
  485. }
  486. else
  487. {
  488. if (0 != (WS_TABSTOP & windowStyle))
  489. {
  490. SetWindowLongPtr(hwnd, GWL_STYLE, windowStyle & ~WS_TABSTOP);
  491. }
  492. }
  493. }
  494. static INT Toolbar_InsertItemInternal(TOOLBAR *toolbar, ToolbarItem *item, INT insertBefore, HWND hwnd)
  495. {
  496. if (NULL == toolbar || NULL == toolbar->items || NULL == item)
  497. return ITEM_ERR;
  498. INT index = ITEM_ERR;
  499. if (TBIP_FIRST == insertBefore)
  500. {
  501. toolbar->items->insert(toolbar->items->begin(), item);
  502. index = 0;
  503. }
  504. else if (TBIP_LAST == insertBefore)
  505. {
  506. toolbar->items->push_back(item);
  507. index = ((INT)toolbar->items->size() - 1);
  508. }
  509. else
  510. {
  511. if (insertBefore < 0) insertBefore = 0;
  512. else if (insertBefore >= (INT)toolbar->items->size()) insertBefore = (INT)toolbar->items->size();
  513. toolbar->items->insert(toolbar->items->begin() + insertBefore, item);
  514. index = insertBefore;
  515. }
  516. if (ITEM_ERR != index)
  517. {
  518. item->AddRef();
  519. item->UpdateSkin(hwnd);
  520. }
  521. return index;
  522. }
  523. static size_t Toolbar_GetFocusIndex(HWND hwnd, size_t focusIndex, INT searchDirection, BOOL fTabstopOnly)
  524. {
  525. TOOLBAR *toolbar = GetToolbar(hwnd);
  526. if (NULL == toolbar || NULL == toolbar->items || 0 == toolbar->items->size())
  527. return ((size_t)-1);
  528. ToolbarItem *item = Toolbar_GetItem(toolbar, focusIndex);
  529. if (NULL != item && Toolbar_IsItemAcceptFocus(item, fTabstopOnly))
  530. return focusIndex;
  531. if (0 == searchDirection)
  532. return ((size_t)-1);
  533. if (searchDirection > 0)
  534. {
  535. size_t count = toolbar->items->size();
  536. size_t i = focusIndex + 1;
  537. for (; i < count; i++)
  538. {
  539. if (Toolbar_IsItemAcceptFocus(toolbar->items->at(i), fTabstopOnly))
  540. return i;
  541. }
  542. if (i == count && Toolbar_IsItemAcceptFocus(toolbar->chevron, fTabstopOnly))
  543. return i;
  544. }
  545. else
  546. {
  547. size_t count = toolbar->items->size();
  548. if (focusIndex == count && Toolbar_IsItemAcceptFocus(toolbar->chevron, fTabstopOnly))
  549. return count;
  550. size_t i = focusIndex;
  551. if (i <= count)
  552. {
  553. while(i--)
  554. {
  555. if (Toolbar_IsItemAcceptFocus(toolbar->items->at(i), fTabstopOnly))
  556. return i;
  557. }
  558. }
  559. }
  560. return ((size_t)-1);
  561. }
  562. static void Toolbar_OffsetItems(ToolbarItem **itemList, INT count, INT offsetX, INT offsetY)
  563. {
  564. while(count-- > 0)
  565. {
  566. if (NULL != itemList[count])
  567. itemList[count]->OffsetRect(offsetX, offsetY);
  568. }
  569. }
  570. static void Toolbar_HideItems(ToolbarItem **itemList, INT count, INT *chevronItemCount)
  571. {
  572. INT chevronItems = 0;
  573. while(count-- > 0)
  574. {
  575. if (NULL != itemList[count])
  576. {
  577. if (0 == (ToolbarItem::styleNoChevron & itemList[count]->GetStyle()))
  578. chevronItems++;
  579. itemList[count]->SetRectEmpty();
  580. }
  581. }
  582. if (NULL != chevronItemCount)
  583. *chevronItemCount = chevronItems;
  584. }
  585. static LONG Toolbar_CutoffItems(ToolbarItem **itemList, INT count, LONG cutoffCX)
  586. {
  587. RECT elementRect;
  588. LONG left = cutoffCX;
  589. ToolbarItem *item;
  590. while(count-- > 0)
  591. {
  592. item = itemList[count];
  593. if (NULL == item)
  594. continue;
  595. DWORD style = item->GetStyle();
  596. if (0 == (ToolbarItem::styleChevronOnly & style) &&
  597. (0 == (ToolbarItem::stateHidden & style) || 0 == (ToolbarItem::stylePopup & style)) &&
  598. item->GetRect(&elementRect))
  599. {
  600. if (elementRect.right <= cutoffCX)
  601. return elementRect.right;
  602. if (0 == count)
  603. left = elementRect.left;
  604. item->SetRectEmpty();
  605. }
  606. }
  607. return left;
  608. }
  609. static LONG Toolbar_CalculateClient(HWND hwnd, ToolbarItem **itemList, INT count, const RECT *prcClient, INT *deltaCX)
  610. {
  611. ToolbarItem *item;
  612. RECT elementRect;
  613. INT index;
  614. UINT style;
  615. LONG elementLeft = prcClient->left;
  616. for (index = 0; index < count; index++)
  617. {
  618. item = itemList[index];
  619. if (NULL != item)
  620. {
  621. style = item->GetStyle();
  622. if (0 != (ToolbarItem::styleChevronOnly & style) ||
  623. (0 != (ToolbarItem::stateHidden & style) && 0!= (ToolbarItem::stylePopup & style)))
  624. {
  625. item->SetRectEmpty();
  626. continue;
  627. }
  628. CopyRect(&elementRect, prcClient);
  629. elementRect.left = elementLeft;
  630. if (0 != (ToolbarItem::styleFlexible & style))
  631. {
  632. RECT testRect;
  633. INT flexMinimum = 0;
  634. SetRect(&testRect, elementRect.left, elementRect.top, elementRect.left, elementRect.bottom);
  635. if (item->AdjustRect(hwnd, &testRect) && testRect.right > elementRect.left)
  636. {
  637. flexMinimum = (testRect.right - elementRect.left);
  638. }
  639. if (index != (count -1))
  640. {
  641. INT delta = 0;
  642. INT section;
  643. if ((elementRect.left + flexMinimum) < prcClient->right)
  644. {
  645. CopyRect(&testRect, &elementRect);
  646. testRect.left += flexMinimum;
  647. section = Toolbar_CalculateClient(hwnd, itemList + (index + 1),
  648. count - index - 1, &testRect, &delta);
  649. }
  650. else
  651. {
  652. section = prcClient->right - (elementRect.left + flexMinimum);
  653. }
  654. if (section < 0)
  655. { // we need to move back
  656. item->SetRectEmpty();
  657. elementLeft += (flexMinimum - section);
  658. if (index > 0)
  659. elementLeft = Toolbar_CutoffItems(itemList, index, elementLeft);
  660. return (elementLeft - prcClient->left);
  661. }
  662. if (NULL != deltaCX)
  663. *deltaCX += delta;
  664. elementRect.right = elementRect.right - section + delta;
  665. if (elementRect.right < elementRect.left)
  666. elementRect.right = elementRect.left;
  667. }
  668. if (item->AdjustRect(hwnd, &elementRect))
  669. {
  670. item->SetRect(&elementRect);
  671. Toolbar_OffsetItems(itemList + (index + 1), count - index - 1, elementRect.right - elementRect.left - flexMinimum, 0);
  672. elementLeft = prcClient->right;
  673. break;
  674. }
  675. }
  676. if (item->AdjustRect(hwnd, &elementRect))
  677. {
  678. if (elementRect.right > prcClient->right)
  679. {
  680. INT chevronItems;
  681. Toolbar_HideItems(itemList + index, count - index, &chevronItems);
  682. if (chevronItems > 0)
  683. {
  684. LONG delta = Toolbar_ShowChevron(hwnd, prcClient);
  685. if (NULL != deltaCX) *deltaCX += delta;
  686. elementLeft = (index > 0) ?
  687. Toolbar_CutoffItems(itemList, index, prcClient->right + delta) :
  688. (prcClient->left + delta);
  689. }
  690. break;
  691. }
  692. item->SetRect(&elementRect);
  693. elementLeft += (elementRect.right - elementRect.left);
  694. }
  695. }
  696. }
  697. return (elementLeft - prcClient->left);
  698. }
  699. static void Toolbar_UpdateLayout(HWND hwnd, BOOL fRedraw)
  700. {
  701. UINT windowStyle = GetWindowStyle(hwnd);
  702. if (0 != (TBS_LOCKUPDATE & windowStyle)) return;
  703. TOOLBAR *toolbar = GetToolbar(hwnd);
  704. if (NULL == toolbar) return;
  705. RECT clientRect;
  706. if (!Toolbar_GetClientRect(hwnd, &clientRect)) return;
  707. Toolbar_UpdateTextMetrics(hwnd);
  708. size_t count = (NULL != toolbar->items) ? toolbar->items->size() : 0;
  709. if (NULL != toolbar->chevron)
  710. {
  711. INT chevronOnly = 0;
  712. ToolbarItem *item;
  713. for (size_t i = 0; i < count; i++)
  714. {
  715. item = toolbar->items->at(i);
  716. UINT style = item->GetStyle();
  717. if (NULL != item &&
  718. 0 != (ToolbarItem::styleChevronOnly & style) &&
  719. 0 == (ToolbarItem::stateHidden & style))
  720. {
  721. chevronOnly++;
  722. }
  723. }
  724. toolbar->chevron->SetRectEmpty();
  725. if (0 != chevronOnly)
  726. clientRect.right += Toolbar_ShowChevron(hwnd, &clientRect);
  727. }
  728. Toolbar_CalculateClient(hwnd, toolbar->items->size() ? &toolbar->items->at(0) : nullptr, (INT)count, &clientRect, NULL);
  729. if (((size_t)-1) != toolbar->iFocused)
  730. {
  731. size_t focusIndex = Toolbar_GetFocusIndex(hwnd, toolbar->iFocused, -1, FALSE);
  732. if (focusIndex != toolbar->iFocused)
  733. {
  734. ToolbarItem *itemNew, *itemOld;
  735. itemOld = Toolbar_GetItem(toolbar, toolbar->iFocused);
  736. itemNew = Toolbar_GetItem(toolbar, focusIndex);
  737. toolbar->iFocused = focusIndex;
  738. if (NULL != itemOld)
  739. itemOld->SetFocus(hwnd, itemNew, FALSE);
  740. if (NULL != itemNew)
  741. itemNew->SetFocus(hwnd, itemOld, TRUE);
  742. }
  743. }
  744. if (FALSE != fRedraw)
  745. {
  746. RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_NOERASE);
  747. }
  748. }
  749. static ToolbarItem *Toolbar_HitTest(HWND hwnd, POINT pt)
  750. {
  751. TOOLBAR *toolbar = GetToolbar(hwnd);
  752. if (NULL != toolbar)
  753. {
  754. ToolbarItem *item;
  755. size_t count = (NULL != toolbar->items) ? toolbar->items->size() : 0;
  756. for (size_t i = 0; i <= count; i++)
  757. {
  758. item = (i < count) ? toolbar->items->at(i) : toolbar->chevron;
  759. if (NULL != item && item->PtInItem(pt))
  760. return item;
  761. }
  762. }
  763. return NULL;
  764. }
  765. static HFONT Toolbar_CreateFont(HFONT skinFont)
  766. {
  767. LOGFONT lf;
  768. if (NULL == skinFont) return NULL;
  769. INT skinHeight = (sizeof(LOGFONT) == GetObject(skinFont, sizeof(LOGFONT), &lf)) ? lf.lfHeight : -11;
  770. ZeroMemory(&lf, sizeof(LOGFONT));
  771. lf.lfHeight = skinHeight;
  772. lf.lfWeight = FW_DONTCARE;
  773. lf.lfItalic = FALSE;
  774. lf.lfUnderline = FALSE;
  775. lf.lfStrikeOut = FALSE;
  776. lf.lfCharSet = DEFAULT_CHARSET;
  777. lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
  778. lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  779. lf.lfQuality = DEFAULT_QUALITY;
  780. lf.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
  781. StringCchCopy(lf.lfFaceName, ARRAYSIZE(lf.lfFaceName), L"MS Shell Dlg 2");
  782. return CreateFontIndirect(&lf);
  783. }
  784. static HWND Toolbar_CreateTooltip(HINSTANCE hInstance, HWND hOwner)
  785. {
  786. HWND hTooltip = CreateWindowExW(WS_EX_TRANSPARENT , TOOLTIPS_CLASS, NULL,
  787. WS_POPUP | TTS_ALWAYSTIP,
  788. 0, 0, 0, 0,
  789. hOwner, NULL, hInstance, NULL);
  790. if (NULL == hTooltip)
  791. return NULL;
  792. SetWindowPos(hTooltip, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
  793. SendMessage(hTooltip, TTM_SETDELAYTIME, TTDT_INITIAL, MAKELPARAM(1000, 0));
  794. SendMessage(hTooltip, TTM_SETDELAYTIME, TTDT_RESHOW, MAKELPARAM(-2, 0));
  795. OSVERSIONINFO ov;
  796. ov.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  797. if (::GetVersionEx(&ov) &&
  798. ov.dwMajorVersion >= 6 &&
  799. VER_PLATFORM_WIN32_NT == ov.dwPlatformId)
  800. {
  801. RECT rcMargin;
  802. SetRect(&rcMargin, 3, 1, 3, 1);
  803. SendMessage(hTooltip, TTM_SETMARGIN, 0, (LPARAM)&rcMargin);
  804. }
  805. TOOLINFO ti;
  806. ZeroMemory(&ti, sizeof(TOOLINFO));
  807. ti.cbSize = sizeof(TOOLINFO);
  808. ti.hwnd = hOwner;
  809. ti.lpszText = LPSTR_TEXTCALLBACK;
  810. SendMessage(hTooltip, TTM_ADDTOOL, 0, (LPARAM)&ti);
  811. SendMessage(hTooltip, TTM_SETMAXTIPWIDTH, 0, 1000);
  812. return hTooltip;
  813. }
  814. static void Toolbar_RelayTooltipMsg(HWND hTooltip, HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  815. {
  816. MSG msg;
  817. msg.hwnd = hwnd;
  818. msg.message = uMsg;
  819. msg.wParam = wParam;
  820. msg.lParam = lParam;
  821. //DWORD pts = GetMessagePos();
  822. //POINTSTOPOINT(msg.pt, pts);
  823. //msg.time = GetMessageTime();
  824. SendMessage(hTooltip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
  825. }
  826. static void Toolbar_SetTooltipItem(HWND hwnd, ToolbarItem *item)
  827. {
  828. TOOLBAR *toolbar = GetToolbar(hwnd);
  829. if (NULL == toolbar) return;
  830. TOOLINFO ti;
  831. ti.cbSize = sizeof(TOOLINFO);
  832. ti.hwnd = hwnd;
  833. ti.uId = 0;
  834. ti.lpszText = NULL;
  835. if (SendMessage(toolbar->hTooltip, TTM_GETTOOLINFO, 0, (LPARAM)&ti))
  836. {
  837. RECT clientRect, toolRect;
  838. Toolbar_GetClientRect(hwnd, &clientRect);
  839. if (NULL == item || FALSE == item->GetRect(&toolRect))
  840. {
  841. SendMessage(toolbar->hTooltip, TTM_ACTIVATE, FALSE, 0L);
  842. return;
  843. }
  844. toolRect.top = clientRect.top;
  845. toolRect.bottom = clientRect.bottom;
  846. if (ti.lParam != (LPARAM)item || !::EqualRect(&ti.rect, &toolRect))
  847. {
  848. CopyRect(&ti.rect, &toolRect);
  849. ti.lParam = (LPARAM)item;
  850. SendMessage(toolbar->hTooltip, TTM_SETTOOLINFO, 0, (LPARAM)&ti);
  851. }
  852. SendMessage(toolbar->hTooltip, TTM_ACTIVATE, TRUE, 0L);
  853. }
  854. }
  855. static void Toolbar_ClearBrushCache(HWND hwnd)
  856. {
  857. TOOLBAR *toolbar = GetToolbar(hwnd);
  858. if (NULL == toolbar) return;
  859. for (INT i = 0; i < ARRAYSIZE(toolbar->szBrushes); i++)
  860. {
  861. if (NULL != toolbar->szBrushes[i])
  862. {
  863. DeleteObject(toolbar->szBrushes[i]);
  864. toolbar->szBrushes[i] = NULL;
  865. }
  866. }
  867. }
  868. static HBRUSH Toolbar_GetBrush(HWND hwnd, UINT brushId)
  869. {
  870. TOOLBAR *toolbar = GetToolbar(hwnd);
  871. if (NULL == toolbar) return NULL;
  872. if (brushId >= ARRAYSIZE(toolbar->szBrushes))
  873. return NULL;
  874. if (NULL == toolbar->szBrushes[brushId])
  875. {
  876. COLORREF brushColor = 0x00FF00FF;
  877. switch(brushId)
  878. {
  879. case TOOLBRUSH_BACK: brushColor = toolbar->rgbBk;break;
  880. case TOOLBRUSH_FRAME: brushColor = toolbar->rgbFrame; break;
  881. case TOOLBRUSH_ITEMBK: brushColor = toolbar->rgbEditBk; break;
  882. }
  883. toolbar->szBrushes[brushId] = CreateSolidBrush(brushColor);
  884. }
  885. return toolbar->szBrushes[brushId];
  886. }
  887. static HRGN Toolbar_GetFrameRgn(const RECT *prcToolbar, BOOL bottomDock)
  888. {
  889. if (TOOLBAR_SPACE_TOP < 1)
  890. return NULL;
  891. HRGN regionFrame;
  892. INT origY = (FALSE != bottomDock) ? prcToolbar->top : prcToolbar->bottom - 1;
  893. regionFrame = CreateRectRgn(prcToolbar->left, origY, prcToolbar->right, origY + 1);
  894. return regionFrame;
  895. }
  896. static void Toolbar_PaintBack(HWND hwnd, HDC hdc, const RECT *prcToolbar, HRGN regionPaint)
  897. {
  898. TOOLBAR *toolbar = GetToolbar(hwnd);
  899. if (NULL == toolbar) return;
  900. UINT windowStyle = GetWindowStyle(hwnd);
  901. if (0 == (TBS_HIDDEN & toolbar->flags))
  902. {
  903. HRGN regionFrame = Toolbar_GetFrameRgn(prcToolbar, (0 != (TBS_BOTTOMDOCK & windowStyle)));
  904. if (NULL != regionFrame)
  905. {
  906. if (FillRgn(hdc, regionFrame, Toolbar_GetBrush(hwnd, TOOLBRUSH_FRAME)))
  907. CombineRgn(regionPaint, regionPaint, regionFrame, RGN_DIFF);
  908. DeleteObject(regionFrame);
  909. }
  910. }
  911. FillRgn(hdc, regionPaint, Toolbar_GetBrush(hwnd, TOOLBRUSH_BACK));
  912. }
  913. static void Toolbar_Paint(HWND hwnd, HDC hdc, const RECT *prcPaint, BOOL fErase)
  914. {
  915. TOOLBAR *toolbar = GetToolbar(hwnd);
  916. if (NULL == toolbar) return;
  917. RECT clientRect;
  918. GetClientRect(hwnd, &clientRect);
  919. DWORD windowStyle = GetWindowStyle(hwnd);
  920. HRGN paintRegion = CreateRectRgnIndirect(prcPaint);
  921. if (0 == (TBS_HIDDEN & toolbar->flags))
  922. {
  923. INT savedDC = SaveDC(hdc);
  924. HRGN itemRegion = CreateRectRgn(0,0,0,0);
  925. SetBkColor(hdc, toolbar->rgbBk);
  926. SetTextColor(hdc, toolbar->rgbText);
  927. SelectObject(hdc, toolbar->textFont);
  928. ToolbarItem *item;
  929. RECT paintRect;
  930. UINT style;
  931. BOOL fFocused = (GetFocus() == hwnd);
  932. UINT commonState= 0;
  933. if (0 != (WS_DISABLED & windowStyle))
  934. commonState |= ToolbarItem::stateDisabled;
  935. size_t count = (NULL != toolbar->items) ? toolbar->items->size() : 0;
  936. for (size_t i = 0; i <= count; i++)
  937. {
  938. item = (i < count) ? toolbar->items->at(i) : toolbar->chevron;
  939. if (NULL != item && item->IntersectRect(&paintRect, prcPaint))
  940. {
  941. style = item->GetStyle() | commonState;
  942. if (i == toolbar->iFocused && fFocused)
  943. style |= ToolbarItem::stateFocused;
  944. if (0 != (TBS_NOFOCUSRECT & toolbar->flags))
  945. style |= ToolbarItem::stateNoFocusRect;
  946. if (0 == (ToolbarItem::stateHidden & style) &&
  947. item->Paint(hwnd, hdc, &paintRect, style))
  948. {
  949. SetRectRgn(itemRegion, paintRect.left, paintRect.top, paintRect.right, paintRect.bottom);
  950. CombineRgn(paintRegion, paintRegion, itemRegion, RGN_DIFF);
  951. }
  952. }
  953. }
  954. DeleteObject(itemRegion);
  955. RestoreDC(hdc, savedDC);
  956. }
  957. Toolbar_PaintBack(hwnd, hdc, &clientRect, paintRegion);
  958. DeleteObject(paintRegion);
  959. }
  960. static INT Toolbar_FindListItem(ToolbarItem **itemList, INT start, INT count, LPCSTR pszName, INT cchName)
  961. {
  962. ToolbarItem *item;
  963. for (INT i = start; i < count; i++)
  964. {
  965. item = itemList[i];
  966. if (NULL != item && item->IsEqual(pszName, cchName))
  967. {
  968. return i;
  969. }
  970. }
  971. return ITEM_ERR;
  972. }
  973. static INT Toolbar_ResolveName(HWND hwnd, LPCSTR pszName)
  974. {
  975. TOOLBAR *toolbar = GetToolbar(hwnd);
  976. if (NULL == toolbar || NULL == toolbar->items)
  977. return ITEM_ERR;
  978. UINT index;
  979. UINT count = (UINT)toolbar->items->size();
  980. if (IS_INTRESOURCE(pszName))
  981. {
  982. index = (UINT)(UINT_PTR)pszName;
  983. if (index == count && NULL != toolbar->chevron)
  984. return index;
  985. return (index < count) ? index : ITEM_ERR;
  986. }
  987. INT cchName = lstrlenA(pszName);
  988. if (0 == cchName) return ITEM_ERR;
  989. if (count == toolbar->resolveCache)
  990. {
  991. if (toolbar->chevron->IsEqual(pszName, cchName))
  992. index = count;
  993. else
  994. {
  995. index = ITEM_ERR;
  996. toolbar->resolveCache = 0;
  997. }
  998. }
  999. else
  1000. {
  1001. index = ITEM_ERR;
  1002. }
  1003. if (ITEM_ERR == index)
  1004. index = Toolbar_FindListItem(toolbar->items->size() ? & toolbar->items->at(0) : nullptr, toolbar->resolveCache, count, pszName, cchName);
  1005. if (ITEM_ERR == index && 0 != toolbar->resolveCache)
  1006. index = Toolbar_FindListItem(toolbar->items->size() ? &toolbar->items->at(0) : nullptr, 0, toolbar->resolveCache, pszName, cchName);
  1007. if (ITEM_ERR == index && NULL != toolbar->chevron && toolbar->chevron->IsEqual(pszName, cchName))
  1008. index = count;
  1009. toolbar->resolveCache = (ITEM_ERR != index) ? index : 0;
  1010. return index;
  1011. }
  1012. static BOOL Toolbar_DisplayChevronMenu(HWND hwnd)
  1013. {
  1014. TOOLBAR *toolbar = GetToolbar(hwnd);
  1015. if (NULL == toolbar || NULL == toolbar->items) return FALSE;
  1016. HMENU hMenu = CreatePopupMenu();
  1017. if (NULL == hMenu) return FALSE;
  1018. size_t count = toolbar->items->size();
  1019. UINT insertedCount = 0;
  1020. MENUITEMINFO mi;
  1021. mi.cbSize = sizeof(MENUITEMINFO);
  1022. UINT style;
  1023. WCHAR szBuffer[80] = {0};
  1024. INT commandId = 0;
  1025. ToolbarItem *item;
  1026. BOOL insertBreak = FALSE;
  1027. for (size_t i = 0; i < count; i++)
  1028. {
  1029. item = toolbar->items->at(i);
  1030. if (NULL != item)
  1031. {
  1032. style = item->GetStyle();
  1033. if(0 == ((ToolbarItem::stateHidden | ToolbarItem::styleNoChevron) & style) &&
  1034. item->IsRectEmpty() &&
  1035. FALSE != item->FillMenuInfo(hwnd, &mi, szBuffer, ARRAYSIZE(szBuffer)))
  1036. {
  1037. if (MIIM_FTYPE == mi.fMask && MFT_MENUBREAK == mi.fType)
  1038. {
  1039. if (insertedCount > 0)
  1040. insertBreak = TRUE;
  1041. }
  1042. else
  1043. {
  1044. if (FALSE != InsertMenuItem(hMenu, insertedCount, TRUE, &mi))
  1045. {
  1046. if (insertBreak)
  1047. {
  1048. mi.fMask = MIIM_FTYPE;
  1049. mi.fType = MFT_MENUBREAK;
  1050. if (InsertMenuItem(hMenu, insertedCount, TRUE, &mi))
  1051. insertedCount++;
  1052. insertBreak = FALSE;
  1053. }
  1054. insertedCount++;
  1055. }
  1056. }
  1057. }
  1058. }
  1059. }
  1060. if (NULL != hMenu && insertedCount > 0)
  1061. {
  1062. RECT windowRect;
  1063. GetWindowRect(hwnd, &windowRect);
  1064. UINT windowStyle = GetWindowStyle(hwnd);
  1065. UINT menuStyle = TPM_RIGHTALIGN | TPM_RETURNCMD;
  1066. POINT menuOrig;
  1067. menuOrig.x = windowRect.right;
  1068. if (0 != (TBS_BOTTOMDOCK & windowStyle))
  1069. {
  1070. menuOrig.y = windowRect.top;
  1071. menuStyle |= (TPM_BOTTOMALIGN | TPM_VERNEGANIMATION);
  1072. }
  1073. else
  1074. {
  1075. menuOrig.y = windowRect.bottom;
  1076. menuStyle |= (TPM_TOPALIGN | TPM_VERPOSANIMATION);
  1077. }
  1078. commandId = Menu_TrackPopup(hMenu, menuStyle, menuOrig.x, menuOrig.y, hwnd, NULL);
  1079. }
  1080. DestroyMenu(hMenu);
  1081. if (0 != commandId)
  1082. Toolbar_SendCommand(hwnd, commandId);
  1083. return TRUE;
  1084. }
  1085. static void Toolbar_DisplayContextMenu(HWND hwnd, INT x, INT y)
  1086. {
  1087. HMENU hMenu = Menu_GetMenu(MENU_TOOLBAR, 0);
  1088. if (NULL != hMenu)
  1089. {
  1090. UINT windowStyle = GetWindowStyle(hwnd);
  1091. CheckMenuRadioItem(hMenu, ID_TOOLBAR_DOCKTOP, ID_TOOLBAR_DOCKBOTTOM,
  1092. (0 != (TBS_BOTTOMDOCK & windowStyle)) ? ID_TOOLBAR_DOCKBOTTOM :ID_TOOLBAR_DOCKTOP,
  1093. MF_BYCOMMAND);
  1094. CheckMenuItem(hMenu, ID_TOOLBAR_AUTOHIDE,
  1095. MF_BYCOMMAND | ((0 != (TBS_AUTOHIDE & windowStyle)) ? MF_CHECKED :MF_UNCHECKED));
  1096. CheckMenuItem(hMenu, ID_TOOLBAR_TABSTOP,
  1097. MF_BYCOMMAND | ((0 != (TBS_TABSTOP & windowStyle)) ? MF_CHECKED :MF_UNCHECKED));
  1098. Menu_TrackPopup(hMenu, TPM_LEFTALIGN | TPM_TOPALIGN, x, y, hwnd, NULL);
  1099. Menu_ReleaseMenu(hMenu, MENU_TOOLBAR);
  1100. }
  1101. }
  1102. static BOOL Toolbar_InstallMouseHook(HWND hwnd)
  1103. {
  1104. if (TLS_OUT_OF_INDEXES == tlsIndex)
  1105. {
  1106. tlsIndex = Plugin_TlsAlloc();
  1107. if (TLS_OUT_OF_INDEXES == tlsIndex) return FALSE;
  1108. }
  1109. TOOLBARMOUSEHOOK *hook = (TOOLBARMOUSEHOOK*)calloc(1, sizeof(TOOLBARMOUSEHOOK));
  1110. if (NULL == hook) return FALSE;
  1111. hook->hwnd = hwnd;
  1112. hook->hHook = SetWindowsHookEx(WH_MOUSE, Toolbar_MouseHook, NULL, GetCurrentThreadId());
  1113. if (NULL == hook->hHook)
  1114. {
  1115. free(hook);
  1116. return FALSE;
  1117. }
  1118. Plugin_TlsSetValue(tlsIndex, hook);
  1119. return TRUE;
  1120. }
  1121. static void Toolbar_TrackMouseLeave(HWND hwnd)
  1122. {
  1123. TRACKMOUSEEVENT tm;
  1124. tm.cbSize = sizeof(TRACKMOUSEEVENT);
  1125. tm.dwFlags = TME_QUERY;
  1126. tm.hwndTrack = hwnd;
  1127. if (TrackMouseEvent(&tm) && 0 == (TME_LEAVE & tm.dwFlags))
  1128. {
  1129. tm.cbSize = sizeof(TRACKMOUSEEVENT);
  1130. tm.dwFlags = TME_LEAVE;
  1131. tm.hwndTrack = hwnd;
  1132. TrackMouseEvent(&tm);
  1133. }
  1134. }
  1135. static BOOL Toolbar_RemoveMouseHook(HWND hwnd, BOOL fOnlyMine)
  1136. {
  1137. if (TLS_OUT_OF_INDEXES == tlsIndex) return FALSE;
  1138. TOOLBARMOUSEHOOK *hook = (TOOLBARMOUSEHOOK*)Plugin_TlsGetValue(tlsIndex);
  1139. if (NULL == hook) return FALSE;
  1140. if (FALSE != fOnlyMine && hwnd != hook->hwnd)
  1141. return FALSE;
  1142. Plugin_TlsSetValue(tlsIndex, NULL);
  1143. if (NULL != hook->hHook)
  1144. UnhookWindowsHookEx(hook->hHook);
  1145. if (NULL != hook->hwnd && hwnd != hook->hwnd)
  1146. {
  1147. Toolbar_TrackMouseLeave(hook->hwnd);
  1148. }
  1149. free(hook);
  1150. return TRUE;
  1151. }
  1152. static void Toolbar_AutoShowWindow(HWND hwnd)
  1153. {
  1154. KillTimer(hwnd, TIMER_AUTOHIDE_ID);
  1155. TOOLBAR *toolbar = GetToolbar(hwnd);
  1156. if (NULL == toolbar) return;
  1157. HWND hFocus = GetFocus();
  1158. BOOL fFocused = (hFocus == hwnd);
  1159. UINT windowStyle = GetWindowStyle(hwnd);
  1160. if ((TBS_AUTOHIDE | WS_VISIBLE) != ((TBS_AUTOHIDE | WS_VISIBLE) & windowStyle))
  1161. return;
  1162. if (0 == (TBS_HIDDEN & toolbar->flags))
  1163. {
  1164. if (0 != (TBS_HIDETIMERSET & toolbar->flags))
  1165. toolbar->flags &= ~TBS_HIDETIMERSET;
  1166. if (FALSE == fFocused)
  1167. Toolbar_TrackMouseLeave(hwnd);
  1168. return;
  1169. }
  1170. Toolbar_RemoveMouseHook(hwnd, FALSE);
  1171. if (FALSE == fFocused && FALSE == Toolbar_InstallMouseHook(hwnd))
  1172. return;
  1173. toolbar->flags &= ~TBS_HIDDEN;
  1174. RECT windowRect;
  1175. if (GetWindowRect(hwnd, &windowRect))
  1176. {
  1177. HWND hParent = GetParent(hwnd);
  1178. if(NULL != hParent)
  1179. {
  1180. MapWindowPoints(HWND_DESKTOP, hParent, (POINT*)&windowRect, 2);
  1181. INT height = Toolbar_GetIdealHeight(hwnd);
  1182. INT x = windowRect.left;
  1183. INT y = windowRect.top;
  1184. UINT swpFlags = SWP_NOMOVE | SWP_NOREDRAW;
  1185. UINT animateFlags = AW_SLIDE | AW_VER_POSITIVE;
  1186. if (0 != (TBS_BOTTOMDOCK & windowStyle))
  1187. {
  1188. y = windowRect.bottom - height;
  1189. swpFlags &= ~SWP_NOMOVE;
  1190. animateFlags = AW_SLIDE | AW_VER_NEGATIVE;
  1191. }
  1192. SetWindowPos(hwnd, HWND_TOP, x, y, windowRect.right - windowRect.left, height, swpFlags);
  1193. SetWindowLongPtr(hwnd, GWL_STYLE, windowStyle & ~WS_VISIBLE);
  1194. BOOL result = AnimateWindow(hwnd, AUTOHIDE_ANIMATETIME, animateFlags);
  1195. if (!result)
  1196. {
  1197. SetWindowLongPtr(hwnd, GWL_STYLE, windowStyle | WS_VISIBLE);
  1198. InvalidateRect(hwnd, NULL, TRUE);
  1199. }
  1200. }
  1201. }
  1202. if (FALSE == fFocused)
  1203. Toolbar_TrackMouseLeave(hwnd);
  1204. }
  1205. static void CALLBACK Toolbar_AutoHideTimerProc(HWND hwnd, UINT uMsg, UINT_PTR eventId, DWORD time)
  1206. {
  1207. KillTimer(hwnd, eventId);
  1208. TOOLBAR *toolbar = GetToolbar(hwnd);
  1209. if (NULL == toolbar) return;
  1210. UINT windowStyle = GetWindowStyle(hwnd);
  1211. if (0 == (TBS_AUTOHIDE & windowStyle)) return;
  1212. toolbar->flags &= ~TBS_HIDETIMERSET;
  1213. if (0 != ((TBS_HIDDEN | TBS_MENULOOP) & toolbar->flags))
  1214. return;
  1215. HWND hFocus = ::GetFocus();
  1216. if (hwnd == hFocus || IsChild(hwnd, hFocus))
  1217. return;
  1218. Toolbar_RemoveMouseHook(hwnd, TRUE);
  1219. RECT windowRect;
  1220. if (GetWindowRect(hwnd, &windowRect))
  1221. {
  1222. HWND hParent = GetParent(hwnd);
  1223. if(NULL != hParent)
  1224. {
  1225. MapWindowPoints(HWND_DESKTOP, hParent, (POINT*)&windowRect, 2);
  1226. INT height = TOOLBAR_HIDDENHEIGHT;
  1227. INT x = windowRect.left;
  1228. INT y = windowRect.top;
  1229. UINT swpFlags = SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOREDRAW;
  1230. UINT animateFlags = AW_HIDE | AW_SLIDE | AW_VER_NEGATIVE;
  1231. HWND zOrder = HWND_TOP;
  1232. if (0 != (TBS_BOTTOMDOCK & windowStyle))
  1233. {
  1234. y = windowRect.bottom - height;
  1235. swpFlags &= ~SWP_NOMOVE;
  1236. zOrder = HWND_BOTTOM;
  1237. animateFlags = AW_HIDE | AW_SLIDE | AW_VER_POSITIVE;
  1238. }
  1239. AnimateWindow(hwnd, AUTOHIDE_ANIMATETIME, animateFlags);
  1240. if (NULL != toolbar->hBrowser)
  1241. {
  1242. RECT invalidRect;
  1243. CopyRect(&invalidRect, &windowRect);
  1244. MapWindowPoints(hParent, toolbar->hBrowser, (POINT*)&invalidRect, 2);
  1245. RedrawWindow(toolbar->hBrowser, &invalidRect, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_ALLCHILDREN);
  1246. }
  1247. SetWindowPos(hwnd, zOrder, x, y, windowRect.right - windowRect.left, height, swpFlags);
  1248. if (0 != (WS_VISIBLE & windowStyle))
  1249. {
  1250. SetWindowLongPtr(hwnd, GWL_STYLE, windowStyle | WS_VISIBLE);
  1251. InvalidateRect(hwnd, NULL, TRUE);
  1252. }
  1253. }
  1254. }
  1255. toolbar->flags |= TBS_HIDDEN;
  1256. }
  1257. static void Toolbar_AutoHideWindow(HWND hwnd, BOOL fImmediate)
  1258. {
  1259. TOOLBAR *toolbar = GetToolbar(hwnd);
  1260. if (NULL == toolbar) return;
  1261. if (0 != ((TBS_HIDDEN | TBS_MENULOOP) & toolbar->flags))
  1262. {
  1263. return;
  1264. }
  1265. UINT windowStyle = GetWindowStyle(hwnd);
  1266. if (0 == (TBS_AUTOHIDE & windowStyle)) return;
  1267. if (FALSE == fImmediate)
  1268. {
  1269. if (SetTimer(hwnd, TIMER_AUTOHIDE_ID, TIMER_AUTOHIDE_DELAY, Toolbar_AutoHideTimerProc))
  1270. toolbar->flags |= TBS_HIDETIMERSET;
  1271. }
  1272. else
  1273. {
  1274. Toolbar_AutoHideTimerProc(hwnd, WM_TIMER, TIMER_AUTOHIDE_ID, GetTickCount());
  1275. }
  1276. }
  1277. static void Toolbar_UpdateUiFlags(HWND hwnd)
  1278. {
  1279. TOOLBAR *toolbar = GetToolbar(hwnd);
  1280. if (NULL == toolbar) return;
  1281. UINT uiState = (UINT)SendMessage(hwnd, WM_QUERYUISTATE, 0, 0L);
  1282. if (0 != (UISF_HIDEFOCUS & uiState)) toolbar->flags |= TBS_NOFOCUSRECT;
  1283. else toolbar->flags &= ~TBS_NOFOCUSRECT;
  1284. }
  1285. static void Toolbar_NotifyParent(HWND hwnd, UINT eventId)
  1286. {
  1287. HWND hParent = GetParent(hwnd);
  1288. if (NULL == hParent) return;
  1289. UINT controlId = (UINT)GetWindowLongPtr(hwnd, GWLP_ID);
  1290. SENDCMD(hParent, controlId, eventId, hwnd);
  1291. }
  1292. static LRESULT Toolbar_OnCreate(HWND hwnd, CREATESTRUCT *pcs)
  1293. {
  1294. TOOLBAR *toolbar = (TOOLBAR*)calloc(1, sizeof(TOOLBAR));
  1295. if (NULL != toolbar)
  1296. {
  1297. Toolbar_AddRef(toolbar);
  1298. SetLastError(ERROR_SUCCESS);
  1299. if (!SetWindowLongPtr(hwnd, 0, (LONGX86)(LONG_PTR)toolbar) && ERROR_SUCCESS != GetLastError())
  1300. {
  1301. free(toolbar);
  1302. toolbar = NULL;
  1303. }
  1304. }
  1305. if (NULL == toolbar)
  1306. {
  1307. DestroyWindow(hwnd);
  1308. return -1;
  1309. }
  1310. toolbar->chevron = Toolbar_CreateItem(TOOLITEM_CHEVRON, 0);
  1311. toolbar->items = new ItemList();
  1312. toolbar->hTooltip = Toolbar_CreateTooltip(pcs->hInstance, hwnd);
  1313. if (NULL != toolbar->hTooltip)
  1314. {
  1315. ifc_skinhelper *skinHelper;
  1316. if (SUCCEEDED(Plugin_GetSkinHelper(&skinHelper)))
  1317. {
  1318. skinHelper->SkinControl(toolbar->hTooltip, SKINNEDWND_TYPE_TOOLTIP,
  1319. SWS_USESKINCOLORS | SWS_USESKINCURSORS | SWS_USESKINFONT);
  1320. skinHelper->Release();
  1321. }
  1322. }
  1323. if (0 != (TBS_AUTOHIDE & pcs->style))
  1324. toolbar->flags |= TBS_HIDDEN;
  1325. return 0;
  1326. }
  1327. static void Toolbar_OnDestroy(HWND hwnd)
  1328. {
  1329. Toolbar_RemoveMouseHook(hwnd, TRUE);
  1330. Toolbar_ClearBrushCache(hwnd);
  1331. TOOLBAR *toolbar = GetToolbar(hwnd);
  1332. SetWindowLongPtr(hwnd, 0, 0L);
  1333. Toolbar_Release(toolbar);
  1334. }
  1335. static void Toolbar_OnPaint(HWND hwnd)
  1336. {
  1337. PAINTSTRUCT ps;
  1338. if (BeginPaint(hwnd, &ps))
  1339. {
  1340. if (ps.rcPaint.left != ps.rcPaint.right)
  1341. Toolbar_Paint(hwnd, ps.hdc, &ps.rcPaint, ps.fErase);
  1342. EndPaint(hwnd, &ps);
  1343. }
  1344. }
  1345. static void Toolbar_OnPrintClient(HWND hwnd, HDC hdc, UINT options)
  1346. {
  1347. RECT clientRect;
  1348. if (GetClientRect(hwnd, &clientRect))
  1349. Toolbar_Paint(hwnd, hdc, &clientRect, 0 != (PRF_ERASEBKGND & options));
  1350. }
  1351. static void Toolbar_OnWindowPosChanged(HWND hwnd, WINDOWPOS *pwp)
  1352. {
  1353. if (SWP_NOSIZE == ((SWP_NOSIZE | SWP_FRAMECHANGED) & pwp->flags))
  1354. return;
  1355. Toolbar_UpdateLayout(hwnd, 0 == (SWP_NOREDRAW & pwp->flags));
  1356. }
  1357. static void Toolbar_OnSetRedraw(HWND hwnd, BOOL allowRedraw)
  1358. {
  1359. UINT windowStyle = GetWindowStyle(hwnd);
  1360. if (FALSE == allowRedraw)
  1361. {
  1362. if (0 == (TBS_LOCKUPDATE & windowStyle))
  1363. SetWindowLongPtr(hwnd, GWL_STYLE, windowStyle | TBS_LOCKUPDATE);
  1364. }
  1365. else
  1366. {
  1367. if (0 != (TBS_LOCKUPDATE & windowStyle))
  1368. {
  1369. SetWindowLongPtr(hwnd, GWL_STYLE, windowStyle & ~TBS_LOCKUPDATE);
  1370. Toolbar_UpdateTabstop(hwnd);
  1371. Toolbar_UpdateLayout(hwnd, FALSE);
  1372. InvalidateRect(hwnd, NULL, TRUE);
  1373. }
  1374. }
  1375. DefWindowProcW(hwnd, WM_SETREDRAW, (WPARAM)allowRedraw, 0L);
  1376. }
  1377. static void Toolbar_OnEnterMenuLoop(HWND hwnd, BOOL fContext)
  1378. {
  1379. TOOLBAR *toolbar = GetToolbar(hwnd);
  1380. if (NULL == toolbar) return;
  1381. toolbar->flags |= TBS_MENULOOP;
  1382. }
  1383. static void Toolbar_OnExitMenuLoop(HWND hwnd, BOOL fContext)
  1384. {
  1385. TOOLBAR *toolbar = GetToolbar(hwnd);
  1386. if (NULL == toolbar) return;
  1387. toolbar->flags &= ~TBS_MENULOOP;
  1388. Toolbar_TrackMouseLeave(hwnd);
  1389. }
  1390. static void Toolbar_OnSetFocus(HWND hwnd, HWND hFocus)
  1391. {
  1392. UINT windowStyle = GetWindowStyle(hwnd);
  1393. TOOLBAR *toolbar = GetToolbar(hwnd);
  1394. if (NULL == toolbar) return;
  1395. BOOL focusDirLeft = FALSE;
  1396. if (0 != (0x8000 & GetAsyncKeyState(VK_LEFT)))
  1397. {
  1398. focusDirLeft = TRUE;
  1399. }
  1400. else if (0 != (0x8000 & GetAsyncKeyState(VK_TAB)))
  1401. {
  1402. if (0 != (0x8000 & GetAsyncKeyState(VK_SHIFT)))
  1403. focusDirLeft = TRUE;
  1404. }
  1405. else if (0 != (0x8000 & GetAsyncKeyState(VK_LBUTTON)) || 0 != (0x8000 & GetAsyncKeyState(VK_RBUTTON)) ||
  1406. 0 != (0x8000 & GetAsyncKeyState(VK_MBUTTON)) || 0 != (0x8000 & GetAsyncKeyState(VK_XBUTTON1)) ||
  1407. 0 != (0x8000 & GetAsyncKeyState(VK_XBUTTON2)))
  1408. {
  1409. // mouse press (?)
  1410. }
  1411. if ((size_t)-1 != toolbar->iFocused)
  1412. toolbar->iFocused = Toolbar_GetFocusIndex(hwnd, toolbar->iFocused, 0, FALSE);
  1413. if ((size_t)-1 == toolbar->iFocused)
  1414. {
  1415. if (0 != (TBS_TABSTOP & windowStyle))
  1416. {
  1417. if (FALSE == focusDirLeft)
  1418. toolbar->iFocused = Toolbar_GetFocusIndex(hwnd, 0, 1, FALSE);
  1419. else
  1420. {
  1421. size_t last = (NULL != toolbar->items) ? toolbar->items->size() : 0;
  1422. if (last > 0)
  1423. toolbar->iFocused = Toolbar_GetFocusIndex(hwnd, last - 1, -1, FALSE);
  1424. }
  1425. }
  1426. else
  1427. {
  1428. size_t lim, index;
  1429. INT inc;
  1430. if (FALSE == focusDirLeft)
  1431. {
  1432. lim = toolbar->items->size();
  1433. index = 0;
  1434. inc = 1;
  1435. }
  1436. else
  1437. {
  1438. lim = (size_t)-1;
  1439. index = toolbar->items->size();
  1440. if (index > 0) index--;
  1441. inc = -1;
  1442. }
  1443. for (; index != lim; index += inc)
  1444. {
  1445. UINT itemStyle = toolbar->items->at(index)->GetStyle();
  1446. UINT mask = ToolbarItem::styleTabstop | ToolbarItem::stateDisabled | ToolbarItem::stateHidden;
  1447. if (ToolbarItem::styleTabstop == (mask & itemStyle))
  1448. {
  1449. toolbar->iFocused = index;
  1450. break;
  1451. }
  1452. }
  1453. }
  1454. }
  1455. if (((size_t)-1) != toolbar->iFocused)
  1456. {
  1457. ToolbarItem *item = Toolbar_GetItem(toolbar, toolbar->iFocused);
  1458. if (NULL != item) item->SetFocus(hwnd, NULL, TRUE);
  1459. }
  1460. if (0 != (WS_TABSTOP & windowStyle))
  1461. Toolbar_AutoShowWindow(hwnd);
  1462. }
  1463. static void Toolbar_OnKillFocus(HWND hwnd, HWND hFocus)
  1464. {
  1465. TOOLBAR *toolbar = GetToolbar(hwnd);
  1466. if (NULL != toolbar && ((size_t)-1) != toolbar->iFocused)
  1467. {
  1468. ToolbarItem *item = Toolbar_GetItem(toolbar, toolbar->iFocused);
  1469. if (NULL != item) item->SetFocus(hwnd, NULL, FALSE);
  1470. toolbar->iFocused = ((size_t)-1);
  1471. }
  1472. Toolbar_AutoHideWindow(hwnd, TRUE);
  1473. }
  1474. static void Toolbar_OnKeyDown(HWND hwnd, INT vKey, UINT flags)
  1475. {
  1476. TOOLBAR *toolbar = GetToolbar(hwnd);
  1477. if (NULL == toolbar) return;
  1478. Toolbar_AutoShowWindow(hwnd);
  1479. INT searchDirection = 0;
  1480. if (VK_LEFT == vKey) searchDirection = -1;
  1481. else if (VK_RIGHT == vKey) searchDirection = 1;
  1482. toolbar->iFocused = Toolbar_GetFocusIndex(hwnd, toolbar->iFocused, searchDirection, FALSE);
  1483. ToolbarItem *focusedItem = Toolbar_GetItem(toolbar, toolbar->iFocused);
  1484. BOOL fHandled = (NULL != focusedItem && focusedItem->KeyDown(hwnd, vKey, flags));
  1485. if (!fHandled)
  1486. {
  1487. switch(vKey)
  1488. {
  1489. case VK_LEFT:
  1490. Toolbar_NextItem(hwnd, TBNS_PREVITEM, FALSE);
  1491. break;
  1492. case VK_RIGHT:
  1493. Toolbar_NextItem(hwnd, TBNS_NEXTITEM, FALSE);
  1494. break;
  1495. case VK_HOME:
  1496. case VK_PRIOR:
  1497. {
  1498. size_t first = 0;
  1499. first = Toolbar_GetFocusIndex(hwnd, first, 1, FALSE);
  1500. if (((size_t)-1) != first)
  1501. Toolbar_NextItem(hwnd, MAKEINTRESOURCE(first), TRUE);
  1502. }
  1503. break;
  1504. case VK_END:
  1505. case VK_NEXT:
  1506. {
  1507. size_t last = toolbar->items->size();
  1508. last = Toolbar_GetFocusIndex(hwnd, last, -1, FALSE);
  1509. if (((size_t)-1) != last)
  1510. Toolbar_NextItem(hwnd, MAKEINTRESOURCE(last), TRUE);
  1511. }
  1512. break;
  1513. }
  1514. }
  1515. }
  1516. static void Toolbar_OnKeyUp(HWND hwnd, INT vKey, UINT flags)
  1517. {
  1518. TOOLBAR *toolbar = GetToolbar(hwnd);
  1519. if (NULL == toolbar) return;
  1520. toolbar->iFocused = Toolbar_GetFocusIndex(hwnd, toolbar->iFocused, FALSE, FALSE);
  1521. ToolbarItem *focusedItem = Toolbar_GetItem(toolbar, toolbar->iFocused);
  1522. BOOL fHandled = (NULL != focusedItem && focusedItem->KeyUp(hwnd, vKey, flags));
  1523. }
  1524. static void Toolbar_OnMouseMove(HWND hwnd, UINT mouseFlags, POINTS pts)
  1525. {
  1526. TOOLBAR *toolbar = GetToolbar(hwnd);
  1527. if (NULL == toolbar) return;
  1528. Toolbar_AutoShowWindow(hwnd);
  1529. POINT pt;
  1530. POINTSTOPOINT(pt, pts);
  1531. RECT clientRect;
  1532. Toolbar_GetClientRect(hwnd, &clientRect);
  1533. if (NULL != toolbar->pressed)
  1534. {
  1535. if (!PtInRect(&clientRect, pt))
  1536. {
  1537. toolbar->pressed->MouseLeave(hwnd);
  1538. }
  1539. else
  1540. {
  1541. toolbar->pressed->MouseMove(hwnd, mouseFlags, pt);
  1542. }
  1543. return;
  1544. }
  1545. ToolbarItem *item = (PtInRect(&clientRect, pt)) ? Toolbar_HitTest(hwnd, pt) : NULL;
  1546. ToolbarItem *prevHighlighted = toolbar->highlighted;
  1547. if (NULL != toolbar->highlighted)
  1548. {
  1549. if (item != toolbar->highlighted)
  1550. {
  1551. toolbar->highlighted->MouseLeave(hwnd);
  1552. toolbar->highlighted = NULL;
  1553. }
  1554. }
  1555. if (NULL != item)
  1556. {
  1557. UINT style = item->GetStyle();
  1558. if (0 == ((ToolbarItem::stateDisabled | ToolbarItem::stateHidden | ToolbarItem::styleStatic) & style))
  1559. {
  1560. toolbar->highlighted = item;
  1561. item->MouseMove(hwnd, mouseFlags, pt);
  1562. Toolbar_TrackMouseLeave(hwnd);
  1563. }
  1564. }
  1565. if (NULL != toolbar->hTooltip)
  1566. {
  1567. if (prevHighlighted == toolbar->highlighted || NULL != prevHighlighted)
  1568. Toolbar_RelayTooltipMsg(toolbar->hTooltip, hwnd, WM_MOUSEMOVE, (WPARAM)mouseFlags, *((LPARAM*)(&pts)));
  1569. if (prevHighlighted != toolbar->highlighted)
  1570. {
  1571. Toolbar_SetTooltipItem(hwnd, toolbar->highlighted);
  1572. if (NULL != toolbar->highlighted)
  1573. Toolbar_RelayTooltipMsg(toolbar->hTooltip, hwnd, WM_MOUSEMOVE, (WPARAM)mouseFlags, *((LPARAM*)(&pts)));
  1574. }
  1575. }
  1576. }
  1577. static void Toolbar_OnMouseLeave(HWND hwnd)
  1578. {
  1579. TOOLBAR *toolbar = GetToolbar(hwnd);
  1580. if (NULL == toolbar) return;
  1581. if (NULL != toolbar->highlighted)
  1582. {
  1583. toolbar->highlighted->MouseLeave(hwnd);
  1584. toolbar->highlighted = NULL;
  1585. }
  1586. Toolbar_SetTooltipItem(hwnd, NULL);
  1587. POINT cursor;
  1588. if (GetFocus() != hwnd && GetCursorPos(&cursor))
  1589. {
  1590. HWND hCursor = WindowFromPoint(cursor);
  1591. if (NULL != hCursor &&
  1592. GetWindowThreadProcessId(hwnd, NULL) != GetWindowThreadProcessId(hCursor, NULL))
  1593. {
  1594. Toolbar_AutoHideWindow(hwnd, FALSE);
  1595. }
  1596. }
  1597. }
  1598. static void Toolbar_OnLButtonDown(HWND hwnd, UINT mouseFlags, POINTS pts)
  1599. {
  1600. TOOLBAR *toolbar = GetToolbar(hwnd);
  1601. if (NULL == toolbar) return;
  1602. Toolbar_SetTooltipItem(hwnd, NULL);
  1603. if (NULL != toolbar->highlighted)
  1604. Toolbar_RelayTooltipMsg(toolbar->hTooltip, hwnd, WM_LBUTTONDOWN, (WPARAM)mouseFlags, *((LPARAM*)(&pts)));
  1605. POINT pt;
  1606. POINTSTOPOINT(pt, pts);
  1607. RECT clientRect;
  1608. Toolbar_GetClientRect(hwnd, &clientRect);
  1609. ToolbarItem *item = (PtInRect(&clientRect, pt)) ? Toolbar_HitTest(hwnd, pt) : NULL;
  1610. if (NULL != item)
  1611. {
  1612. if (0 == ((ToolbarItem::stateDisabled | ToolbarItem::stateHidden | ToolbarItem::styleStatic) & item->GetStyle()))
  1613. {
  1614. item->LButtonDown(hwnd, mouseFlags, pt);
  1615. toolbar->pressed = item;
  1616. }
  1617. }
  1618. SetCapture(hwnd);
  1619. }
  1620. static void Toolbar_OnLButtonUp(HWND hwnd, UINT mouseFlags, POINTS pts)
  1621. {
  1622. TOOLBAR *toolbar = GetToolbar(hwnd);
  1623. if (NULL == toolbar) return;
  1624. if (NULL != toolbar->highlighted)
  1625. Toolbar_RelayTooltipMsg(toolbar->hTooltip, hwnd, WM_LBUTTONUP, (WPARAM)mouseFlags, *((LPARAM*)(&pts)));
  1626. POINT pt;
  1627. POINTSTOPOINT(pt, pts);
  1628. RECT clientRect;
  1629. Toolbar_GetClientRect(hwnd, &clientRect);
  1630. ToolbarItem *item = (PtInRect(&clientRect, pt)) ? Toolbar_HitTest(hwnd, pt) : NULL;
  1631. if (NULL != toolbar->pressed)
  1632. {
  1633. ToolbarItem *pressed = toolbar->pressed;
  1634. toolbar->pressed = NULL;
  1635. Toolbar_AddRef(toolbar);
  1636. pressed->AddRef();
  1637. if (pressed == item)
  1638. pressed->Click(hwnd, mouseFlags, pt);
  1639. pressed->LButtonUp(hwnd, mouseFlags, pt);
  1640. pressed->Release();
  1641. Toolbar_Release(toolbar);
  1642. }
  1643. if (hwnd == GetCapture())
  1644. ReleaseCapture();
  1645. }
  1646. static void Toolbar_OnRButtonDown(HWND hwnd, UINT mouseFlags, POINTS pts)
  1647. {
  1648. TOOLBAR *toolbar = GetToolbar(hwnd);
  1649. if (NULL == toolbar) return;
  1650. Toolbar_SetTooltipItem(hwnd, NULL);
  1651. if (NULL != toolbar->highlighted)
  1652. Toolbar_RelayTooltipMsg(toolbar->hTooltip, hwnd, WM_RBUTTONDOWN, (WPARAM)mouseFlags, *((LPARAM*)(&pts)));
  1653. }
  1654. static void Toolbar_OnRButtonUp(HWND hwnd, UINT mouseFlags, POINTS pts)
  1655. {
  1656. TOOLBAR *toolbar = GetToolbar(hwnd);
  1657. if (NULL == toolbar) return;
  1658. if (NULL != toolbar->highlighted)
  1659. Toolbar_RelayTooltipMsg(toolbar->hTooltip, hwnd, WM_RBUTTONUP, (WPARAM)mouseFlags, *((LPARAM*)(&pts)));
  1660. }
  1661. static void Toolbar_OnMButtonDown(HWND hwnd, UINT mouseFlags, POINTS pts)
  1662. {
  1663. TOOLBAR *toolbar = GetToolbar(hwnd);
  1664. if (NULL == toolbar) return;
  1665. Toolbar_SetTooltipItem(hwnd, NULL);
  1666. if (NULL != toolbar->highlighted)
  1667. Toolbar_RelayTooltipMsg(toolbar->hTooltip, hwnd, WM_MBUTTONDOWN, (WPARAM)mouseFlags, *((LPARAM*)(&pts)));
  1668. }
  1669. static void Toolbar_OnMButtonUp(HWND hwnd, UINT mouseFlags, POINTS pts)
  1670. {
  1671. TOOLBAR *toolbar = GetToolbar(hwnd);
  1672. if (NULL == toolbar) return;
  1673. if (NULL != toolbar->highlighted)
  1674. Toolbar_RelayTooltipMsg(toolbar->hTooltip, hwnd, WM_MBUTTONUP, (WPARAM)mouseFlags, *((LPARAM*)(&pts)));
  1675. }
  1676. static void Toolbar_OnContextMenu(HWND hwnd, HWND hOwner, POINTS pts)
  1677. {
  1678. POINT pt;
  1679. POINTSTOPOINT(pt, pts);
  1680. if (-1 == pt.x || -1 == pt.y)
  1681. {
  1682. RECT windowRect;
  1683. GetWindowRect(hwnd, &windowRect);
  1684. if (!GetCursorPos(&pt) || !PtInRect(&windowRect, pt))
  1685. {
  1686. pt.x = windowRect.left + 2;
  1687. pt.y = windowRect.top + 2;
  1688. }
  1689. }
  1690. else
  1691. {
  1692. POINT localPt = pt;
  1693. MapWindowPoints(HWND_DESKTOP, hwnd, &localPt, 1);
  1694. RECT clientRect;
  1695. Toolbar_GetClientRect(hwnd, &clientRect);
  1696. ToolbarItem *item = (PtInRect(&clientRect, localPt)) ? Toolbar_HitTest(hwnd, localPt) : NULL;
  1697. if (NULL != item)
  1698. {
  1699. UINT itemStyle = item->GetStyle();
  1700. if (0 == ((ToolbarItem::stateHidden | ToolbarItem::stateDisabled) & itemStyle))
  1701. {
  1702. RECT itemRect;
  1703. if (item->GetRect(&itemRect) && PtInRect(&itemRect, localPt))
  1704. {
  1705. if (FALSE != item->DisplayContextMenu(hwnd, pt.x, pt.y))
  1706. return;
  1707. }
  1708. }
  1709. }
  1710. }
  1711. Toolbar_DisplayContextMenu(hwnd, pt.x, pt.y);
  1712. }
  1713. static void Toolbar_OnCaptureChanged(HWND hwnd, HWND hCapture)
  1714. {
  1715. TOOLBAR *toolbar = GetToolbar(hwnd);
  1716. if (NULL == toolbar) return;
  1717. Toolbar_TrackMouseLeave(hwnd);
  1718. }
  1719. static LRESULT Toolbar_OnGetFont(HWND hwnd)
  1720. {
  1721. TOOLBAR *toolbar = GetToolbar(hwnd);
  1722. if (NULL == toolbar) return NULL;
  1723. return (LRESULT)toolbar->textFont;
  1724. }
  1725. static void Toolbar_OnSetFont(HWND hwnd, HFONT hFont, BOOL fRedraw)
  1726. {
  1727. TOOLBAR *toolbar = GetToolbar(hwnd);
  1728. if (NULL == toolbar) return;
  1729. toolbar->textFont = hFont;
  1730. Toolbar_UpdateTextMetrics(hwnd);
  1731. if (FALSE != fRedraw)
  1732. InvalidateRect(hwnd, NULL, TRUE);
  1733. }
  1734. static void Toolbar_GetTootipDispInfo(HWND hwnd, NMTTDISPINFO *pdisp)
  1735. {
  1736. ToolbarItem *item = (ToolbarItem*)pdisp->lParam;
  1737. if (NULL != item)
  1738. {
  1739. item->GetTip(pdisp->szText, ARRAYSIZE(pdisp->szText));
  1740. }
  1741. }
  1742. static LRESULT Toolbar_OnTooltipNotify(HWND hwnd, NMHDR *pnmh)
  1743. {
  1744. switch(pnmh->code)
  1745. {
  1746. case TTN_GETDISPINFO:
  1747. Toolbar_GetTootipDispInfo(hwnd, (NMTTDISPINFO*)pnmh);
  1748. break;
  1749. }
  1750. return 0;
  1751. }
  1752. static LRESULT Toolbar_OnNotify(HWND hwnd, INT controlId, NMHDR *pnmh)
  1753. {
  1754. TOOLBAR *toolbar = GetToolbar(hwnd);
  1755. if (NULL == toolbar) return 0;
  1756. if (toolbar->hTooltip == pnmh->hwndFrom && NULL != toolbar->hTooltip)
  1757. return Toolbar_OnTooltipNotify(hwnd, pnmh);
  1758. return 0;
  1759. }
  1760. static void Toolbar_OnCommand(HWND hwnd, INT commandId, INT eventId, HWND hControl)
  1761. {
  1762. BOOL fEnabled;
  1763. switch(commandId)
  1764. {
  1765. case ID_TOOLBAR_DOCKTOP:
  1766. case ID_TOOLBAR_DOCKBOTTOM:
  1767. fEnabled = (ID_TOOLBAR_DOCKBOTTOM == commandId);
  1768. if (fEnabled != Toolbar_EnableBottomDock(hwnd, fEnabled))
  1769. Toolbar_NotifyParent(hwnd, TBN_DOCKCHANGED);
  1770. break;
  1771. case ID_TOOLBAR_AUTOHIDE:
  1772. fEnabled = (0 == (TBS_AUTOHIDE & GetWindowStyle(hwnd)));
  1773. if (fEnabled != Toolbar_EnableAutoHide(hwnd, fEnabled))
  1774. Toolbar_NotifyParent(hwnd, TBN_AUTOHIDECHANGED);
  1775. break;
  1776. case ID_TOOLBAR_TABSTOP:
  1777. fEnabled = (0 == (TBS_TABSTOP & GetWindowStyle(hwnd)));
  1778. if (fEnabled != Toolbar_EnableTabStop(hwnd, fEnabled))
  1779. Toolbar_NotifyParent(hwnd, TBN_TABSTOPCHANGED);
  1780. break;
  1781. }
  1782. }
  1783. static BOOL Toolbar_ProcessTabKey(HWND hwnd, UINT messageId)
  1784. {
  1785. TOOLBAR *toolbar = GetToolbar(hwnd);
  1786. if (NULL == toolbar) return FALSE;
  1787. BOOL focusDirLeft = (0 != (0x8000 & GetAsyncKeyState(VK_SHIFT)));
  1788. size_t lim, index;
  1789. INT inc;
  1790. if (FALSE == focusDirLeft)
  1791. {
  1792. inc = 1;
  1793. lim = toolbar->items->size();
  1794. index = toolbar->iFocused;
  1795. if ((size_t)-1 == index) index = 0;
  1796. index++;
  1797. if (index >= lim) return FALSE;
  1798. }
  1799. else
  1800. {
  1801. inc = -1;
  1802. lim = (size_t)-1;
  1803. index = toolbar->iFocused;
  1804. if ((size_t)-1 == index) index = toolbar->items->size();
  1805. index--;
  1806. if (index >= toolbar->items->size()) return FALSE;
  1807. }
  1808. for (; index != lim; index += inc)
  1809. {
  1810. ToolbarItem *item = toolbar->items->at(index);
  1811. if (FALSE != Toolbar_IsItemAcceptFocus(item, TRUE))
  1812. {
  1813. item->SetFocus(hwnd, NULL, TRUE);
  1814. return TRUE;
  1815. }
  1816. }
  1817. return FALSE;
  1818. }
  1819. static LRESULT Toolbar_OnGetDlgCode(HWND hwnd, INT vKey, MSG *pMsg)
  1820. {
  1821. if (NULL != pMsg)
  1822. {
  1823. switch(vKey)
  1824. {
  1825. case VK_TAB:
  1826. if (FALSE == Toolbar_ProcessTabKey(hwnd, pMsg->message))
  1827. {
  1828. if (WM_KEYDOWN == pMsg->message)
  1829. Toolbar_AutoHideWindow(hwnd, TRUE);
  1830. return 0;
  1831. }
  1832. break;
  1833. }
  1834. }
  1835. return DLGC_WANTALLKEYS;
  1836. }
  1837. static void Toolbar_OnUpdateUiState(HWND hwnd, INT action, INT state)
  1838. {
  1839. DefWindowProc(hwnd, WM_UPDATEUISTATE, MAKEWPARAM(action, state), 0L);
  1840. Toolbar_UpdateUiFlags(hwnd);
  1841. }
  1842. static void Toolbar_OnEnable(HWND hwnd, BOOL fEnable)
  1843. {
  1844. InvalidateRect(hwnd, NULL, FALSE);
  1845. }
  1846. static LRESULT Toolbar_OnSetCursor(HWND hwnd, HWND hCursor, UINT hitTest, UINT messageId)
  1847. {
  1848. TOOLBAR *toolbar = GetToolbar(hwnd);
  1849. if (NULL == toolbar) return FALSE;
  1850. if (0 == (TBS_MENULOOP & toolbar->flags) && hwnd == hCursor)
  1851. {
  1852. ToolbarItem *item = (NULL != toolbar->pressed) ? toolbar->pressed : toolbar->highlighted;
  1853. if (NULL != item && FALSE != item->SetCursor(hwnd, hCursor, hitTest, messageId))
  1854. {
  1855. return TRUE;
  1856. }
  1857. }
  1858. return DefWindowProcW(hwnd, WM_SETCURSOR, (WPARAM)hCursor, MAKELPARAM(hitTest, messageId));
  1859. }
  1860. static LRESULT Toolbar_OnColorEdit(HWND hwnd, HDC hdcCtrl, HWND hwndCtrl)
  1861. {
  1862. TOOLBAR *toolbar = GetToolbar(hwnd);
  1863. if (NULL == toolbar) return 0;
  1864. SetTextColor(hdcCtrl, toolbar->rgbEdit);
  1865. SetBkColor(hdcCtrl, toolbar->rgbEditBk);
  1866. return (LRESULT)Toolbar_GetBrush(hwnd, TOOLBRUSH_ITEMBK);
  1867. }
  1868. static LRESULT Toolbar_OnColorStatic(HWND hwnd, HDC hdcCtrl, HWND hwndCtrl)
  1869. {
  1870. TOOLBAR *toolbar = GetToolbar(hwnd);
  1871. if (NULL == toolbar) return 0;
  1872. SetTextColor(hdcCtrl, toolbar->rgbText);
  1873. SetBkColor(hdcCtrl, toolbar->rgbBk);
  1874. return (LRESULT)Toolbar_GetBrush(hwnd, TOOLBRUSH_BACK);
  1875. }
  1876. static LRESULT Toolbar_OnGetIdealHeight(HWND hwnd)
  1877. {
  1878. TOOLBAR *toolbar = GetToolbar(hwnd);
  1879. if (NULL == toolbar || NULL == toolbar->imageList) return 0;
  1880. INT toolbarHeight, iconWidth;
  1881. if (!ImageList_GetIconSize(toolbar->imageList, &iconWidth, &toolbarHeight))
  1882. toolbarHeight = 0;
  1883. else
  1884. toolbarHeight /= TOOLBAR_ICONSTATE_COUNT;
  1885. HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_NORESETATTRS);
  1886. if (NULL != hdc)
  1887. {
  1888. HFONT originalFont = (HFONT)SelectObject(hdc, toolbar->textFont);
  1889. TEXTMETRIC tm;
  1890. if (GetTextMetrics(hdc, &tm))
  1891. {
  1892. tm.tmHeight += ((tm.tmHeight - (tm.tmAscent - tm.tmInternalLeading))/2 + 2);
  1893. if (toolbarHeight <= tm.tmHeight)
  1894. toolbarHeight = tm.tmHeight + 4;
  1895. }
  1896. SelectObject(hdc, originalFont);
  1897. ReleaseDC(hwnd, hdc);
  1898. }
  1899. return toolbarHeight + TOOLBAR_SPACE_TOP + TOOLBAR_SPACE_BOTTOM;
  1900. }
  1901. static void Toolbar_OnUpdateSkin(HWND hwnd, BOOL fRedraw)
  1902. {
  1903. TOOLBAR *toolbar = GetToolbar(hwnd);
  1904. if (NULL == toolbar) return;
  1905. ifc_skinhelper *skinHelper;
  1906. if (FAILED(Plugin_GetSkinHelper(&skinHelper)))
  1907. skinHelper = NULL;
  1908. Toolbar_UpdateColorTable(hwnd, skinHelper);
  1909. Toolbar_ClearBrushCache(hwnd);
  1910. if (NULL != toolbar->imageList)
  1911. ImageList_Destroy(toolbar->imageList);
  1912. toolbar->imageList = Toolbar_LoadImagelist(toolbar, Plugin_GetInstance(),
  1913. MAKEINTRESOURCE(IDR_TOOLBARLARGE_IMAGE),
  1914. 21, TOOLBAR_ICONSTATE_COUNT);
  1915. if (NULL != toolbar->ownedFont)
  1916. {
  1917. DeleteObject(toolbar->ownedFont);
  1918. }
  1919. HFONT font = (NULL != skinHelper) ? skinHelper->GetFont() : NULL;
  1920. if (0/* create custom font */)
  1921. {
  1922. toolbar->ownedFont = Toolbar_CreateFont(font);
  1923. if (NULL != toolbar->ownedFont)
  1924. font = toolbar->ownedFont;
  1925. }
  1926. else
  1927. {
  1928. toolbar->ownedFont = NULL;
  1929. }
  1930. SendMessage(hwnd, WM_SETFONT, (WPARAM)font, FALSE);
  1931. if (NULL != toolbar->hTooltip)
  1932. {
  1933. SendMessage(toolbar->hTooltip, WM_SETFONT, (WPARAM)(toolbar->textFont), TRUE);
  1934. }
  1935. Toolbar_UpdateUiFlags(hwnd);
  1936. if (NULL != toolbar->items)
  1937. {
  1938. size_t index = toolbar->items->size();
  1939. while(index-- > 0)
  1940. {
  1941. ToolbarItem *item = toolbar->items->at(index);
  1942. if (NULL != item)
  1943. {
  1944. item->UpdateSkin(hwnd);
  1945. }
  1946. }
  1947. }
  1948. Toolbar_UpdateLayout(hwnd, FALSE);
  1949. if (FALSE != fRedraw)
  1950. InvalidateRect(hwnd, NULL, TRUE);
  1951. if (NULL != skinHelper)
  1952. skinHelper->Release();
  1953. }
  1954. static LRESULT Toolbar_OnGetIconSize(HWND hwnd, INT iconIndex, SIZE *pSize)
  1955. {
  1956. if (NULL == pSize) return FALSE;
  1957. TOOLBAR *toolbar = GetToolbar(hwnd);
  1958. if (ICON_CHEVRON == iconIndex)
  1959. {
  1960. UINT windowStyle = GetWindowStyle(hwnd);
  1961. iconIndex = (0 != (TBS_BOTTOMDOCK & windowStyle)) ? ICON_CHEVRON_BOTTOM : ICON_CHEVRON_TOP;
  1962. }
  1963. if (NULL == toolbar ||
  1964. NULL == toolbar->imageList ||
  1965. iconIndex < 0 ||
  1966. iconIndex >= ImageList_GetImageCount(toolbar->imageList))
  1967. {
  1968. ZeroMemory(pSize, sizeof(SIZE));
  1969. return FALSE;
  1970. }
  1971. INT cx, cy;
  1972. BOOL result = ImageList_GetIconSize(toolbar->imageList, &cx, &cy);
  1973. if (FALSE != result)
  1974. {
  1975. pSize->cy = cy / TOOLBAR_ICONSTATE_COUNT;
  1976. switch(iconIndex)
  1977. {
  1978. case ICON_SEPARATOR:
  1979. pSize->cx = ICON_SEPARATOR_WIDTH;
  1980. break;
  1981. case ICON_CHEVRON_TOP:
  1982. case ICON_CHEVRON_BOTTOM:
  1983. pSize->cx = ICON_CHEVRON_WIDTH;
  1984. break;
  1985. case ICON_HISTORY:
  1986. pSize->cx = ICON_HISTORY_WIDTH;
  1987. break;
  1988. case ICON_BACK:
  1989. case ICON_FORWARD:
  1990. pSize->cx =ICON_ARROW_WIDTH;
  1991. break;
  1992. default:
  1993. pSize->cx = cx;
  1994. break;
  1995. }
  1996. }
  1997. else
  1998. ZeroMemory(pSize, sizeof(SIZE));
  1999. return result;
  2000. }
  2001. static LRESULT Toolbar_OnSendCommand(HWND hwnd, INT commandId)
  2002. {
  2003. HWND hParent = GetParent(hwnd);
  2004. if (NULL == hParent) return FALSE;
  2005. if (ID_CHEVRON_CLICKED == commandId)
  2006. {
  2007. Toolbar_DisplayChevronMenu(hwnd);
  2008. return 0;
  2009. }
  2010. TOOLBAR *toolbar = GetToolbar(hwnd);
  2011. if (NULL != toolbar && NULL != toolbar->items)
  2012. {
  2013. size_t index= toolbar->items->size();
  2014. while(index--)
  2015. toolbar->items->at(index)->CommandSent(hwnd, commandId);
  2016. }
  2017. return SendMessage(hParent, WM_COMMAND, MAKEWPARAM(commandId, 0), (LPARAM)hwnd);
  2018. }
  2019. static LRESULT Toolbar_OnDrawIcon(HWND hwnd, TOOLBARDRAWICONPARAM *iconParam)
  2020. {
  2021. TOOLBAR *toolbar = GetToolbar(hwnd);
  2022. if (NULL == toolbar || NULL == iconParam) return FALSE;
  2023. INT iconIndex = iconParam->iconIndex;
  2024. if (ICON_CHEVRON == iconIndex)
  2025. {
  2026. UINT windowStyle = GetWindowStyle(hwnd);
  2027. iconIndex = (0 != (TBS_BOTTOMDOCK & windowStyle)) ? ICON_CHEVRON_BOTTOM : ICON_CHEVRON_TOP;
  2028. }
  2029. if (NULL == toolbar->imageList ||
  2030. iconIndex < 0 ||
  2031. iconIndex >= ImageList_GetImageCount(toolbar->imageList))
  2032. {
  2033. return FALSE;
  2034. }
  2035. INT frameHeight, frameWidth;
  2036. if (!ImageList_GetIconSize(toolbar->imageList, &frameWidth, &frameHeight))
  2037. {
  2038. frameHeight = 0;
  2039. frameWidth = 0;
  2040. }
  2041. INT offsetY;
  2042. if (0 != (ToolbarItem::stateDisabled & iconParam->itemState))
  2043. offsetY = TOOLBAR_ICONSTATE_DISABLED;
  2044. else if (0 != (ToolbarItem::statePressed & iconParam->itemState))
  2045. offsetY = TOOLBAR_ICONSTATE_PRESSED;
  2046. else if (0 != (ToolbarItem::stateHighlighted & iconParam->itemState))
  2047. offsetY = TOOLBAR_ICONSTATE_HIGHLIGHTED;
  2048. else
  2049. offsetY = TOOLBAR_ICONSTATE_NORMAL;
  2050. frameHeight = frameHeight / TOOLBAR_ICONSTATE_COUNT;
  2051. IMAGELISTDRAWPARAMS param;
  2052. param.cbSize = sizeof(IMAGELISTDRAWPARAMS) - sizeof(DWORD) * 3;
  2053. param.himl = toolbar->imageList;
  2054. param.i = iconIndex;
  2055. param.hdcDst = iconParam->hdcDst;
  2056. param.x = iconParam->x;
  2057. param.y = iconParam->y;
  2058. param.cx = (iconParam->cx > frameWidth) ? frameWidth : iconParam->cx;
  2059. param.cy = (iconParam->cy > frameHeight) ? frameHeight : iconParam->cy;
  2060. param.xBitmap = 0;
  2061. param.yBitmap = frameHeight * offsetY;
  2062. param.rgbBk = CLR_NONE;
  2063. param.rgbFg = CLR_NONE;
  2064. param.fStyle = ILD_NORMAL;
  2065. param.dwRop = SRCCOPY;
  2066. param.fState = ILS_NORMAL;
  2067. param.Frame = 0;
  2068. param.crEffect = 0;
  2069. return ImageList_DrawIndirect(&param);
  2070. }
  2071. static LRESULT Toolbar_OnGetItemCount(HWND hwnd)
  2072. {
  2073. TOOLBAR *toolbar = GetToolbar(hwnd);
  2074. if (NULL == toolbar || NULL == toolbar->items) return 0;
  2075. return (INT)toolbar->items->size();
  2076. }
  2077. static LRESULT Toolbar_OnClear(HWND hwnd)
  2078. {
  2079. TOOLBAR *toolbar = GetToolbar(hwnd);
  2080. if (NULL == toolbar) return FALSE;
  2081. if (NULL == toolbar->items) return TRUE;
  2082. Toolbar_ClearItems(toolbar);
  2083. UINT windowStyle = GetWindowStyle(hwnd);
  2084. if (0 == (TBS_LOCKUPDATE & windowStyle))
  2085. {
  2086. Toolbar_UpdateTabstop(hwnd);
  2087. Toolbar_UpdateLayout(hwnd, FALSE);
  2088. InvalidateRect(hwnd, NULL, TRUE);
  2089. }
  2090. return TRUE;
  2091. }
  2092. static LRESULT Toolbar_OnInsertItem(HWND hwnd, TOOLBARINSERTITEM *insertItem)
  2093. {
  2094. TOOLBAR *toolbar = GetToolbar(hwnd);
  2095. if (NULL == toolbar || NULL == toolbar->items ||
  2096. NULL == insertItem || insertItem->cbSize != sizeof(TOOLBARINSERTITEM))
  2097. {
  2098. return -1;
  2099. }
  2100. INT index = ITEM_ERR;
  2101. UINT styleOverride = 0;
  2102. if (0 != (TBIS_HIDDEN & insertItem->style)) styleOverride |= ToolbarItem::stateHidden;
  2103. if (0 != (TBIS_DISABLED & insertItem->style)) styleOverride |= ToolbarItem::stateDisabled;
  2104. if (0 != (TBIS_CHEVRONONLY & insertItem->style)) styleOverride |= ToolbarItem::styleChevronOnly;
  2105. if (0 != (TBIS_NOCHEVRON & insertItem->style)) styleOverride |= ToolbarItem::styleNoChevron;
  2106. if (0 != (TBIS_POPUP & insertItem->style)) styleOverride |= ToolbarItem::stylePopup;
  2107. ToolbarItem *item = Toolbar_CreateItem(insertItem->pszName, styleOverride);
  2108. if (NULL != item)
  2109. {
  2110. index = Toolbar_InsertItemInternal(toolbar, item, insertItem->insertBefore, hwnd);
  2111. item->Release();
  2112. if (ITEM_ERR != index)
  2113. {
  2114. UINT windowStyle = GetWindowStyle(hwnd);
  2115. if (0 == (TBS_LOCKUPDATE & windowStyle))
  2116. {
  2117. Toolbar_UpdateTabstop(hwnd);
  2118. Toolbar_UpdateLayout(hwnd, FALSE);
  2119. InvalidateRect(hwnd, NULL, TRUE);
  2120. }
  2121. }
  2122. }
  2123. return index;
  2124. }
  2125. static LRESULT Toolbar_OnFindItem(HWND hwnd, LPCSTR pszName)
  2126. {
  2127. return Toolbar_ResolveName(hwnd, pszName);
  2128. }
  2129. static LRESULT Toolbar_OnRemoveItem(HWND hwnd, LPCSTR pszName)
  2130. {
  2131. INT index = Toolbar_ResolveName(hwnd, pszName);
  2132. if (ITEM_ERR == index) return FALSE;
  2133. TOOLBAR *toolbar = GetToolbar(hwnd);
  2134. if (NULL == toolbar || NULL == toolbar->items || index == toolbar->items->size())
  2135. return FALSE;
  2136. ToolbarItem *item = toolbar->items->at(index);
  2137. toolbar->items->erase(toolbar->items->begin() + index);
  2138. if (NULL != item) item->Release();
  2139. UINT windowStyle = GetWindowStyle(hwnd);
  2140. if (0 == (TBS_LOCKUPDATE & windowStyle))
  2141. {
  2142. Toolbar_UpdateTabstop(hwnd);
  2143. Toolbar_UpdateLayout(hwnd, FALSE);
  2144. InvalidateRect(hwnd, NULL, TRUE);
  2145. }
  2146. return TRUE;
  2147. }
  2148. static LRESULT Toolbar_OnSetItemInt(HWND hwnd, LPCSTR pszName, INT value)
  2149. {
  2150. INT index = Toolbar_ResolveName(hwnd, pszName);
  2151. if (ITEM_ERR == index) return FALSE;
  2152. TOOLBAR *toolbar = GetToolbar(hwnd);
  2153. if (index == toolbar->items->size()) return FALSE;
  2154. return toolbar->items->at(index)->SetValueInt(hwnd, value);
  2155. }
  2156. static LRESULT Toolbar_OnSetItemString(HWND hwnd, LPCSTR pszName, LPCWSTR value)
  2157. {
  2158. INT index = Toolbar_ResolveName(hwnd, pszName);
  2159. if (ITEM_ERR == index) return FALSE;
  2160. TOOLBAR *toolbar = GetToolbar(hwnd);
  2161. if (index == toolbar->items->size()) return FALSE;
  2162. return toolbar->items->at(index)->SetValueStr(hwnd, value);
  2163. }
  2164. static COLORREF Toolbar_OnGetBkColor(HWND hwnd)
  2165. {
  2166. TOOLBAR *toolbar = GetToolbar(hwnd);
  2167. return (NULL != toolbar) ? toolbar->rgbBk : 0x00FF00FF;
  2168. }
  2169. static COLORREF Toolbar_OnGetFgColor(HWND hwnd)
  2170. {
  2171. TOOLBAR *toolbar = GetToolbar(hwnd);
  2172. return (NULL != toolbar) ? toolbar->rgbFg : 0x00FF00FF;
  2173. }
  2174. static COLORREF Toolbar_OnGetTextColor(HWND hwnd)
  2175. {
  2176. TOOLBAR *toolbar = GetToolbar(hwnd);
  2177. return (NULL != toolbar) ? toolbar->rgbText : 0x00FF00FF;
  2178. }
  2179. static COLORREF Toolbar_OnGetHiliteColor(HWND hwnd)
  2180. {
  2181. TOOLBAR *toolbar = GetToolbar(hwnd);
  2182. return (NULL != toolbar) ? toolbar->rgbHilite : 0x00FF00FF;
  2183. }
  2184. static COLORREF Toolbar_OnGetEditColor(HWND hwnd)
  2185. {
  2186. TOOLBAR *toolbar = GetToolbar(hwnd);
  2187. return (NULL != toolbar) ? toolbar->rgbEdit : 0x00FF00FF;
  2188. }
  2189. static COLORREF Toolbar_OnGetEditBkColor(HWND hwnd)
  2190. {
  2191. TOOLBAR *toolbar = GetToolbar(hwnd);
  2192. return (NULL != toolbar) ? toolbar->rgbEditBk : 0x00FF00FF;
  2193. }
  2194. static LRESULT Toolbar_OnEnableItem(HWND hwnd, LPCSTR pszName, BOOL fEnable)
  2195. {
  2196. INT index = Toolbar_ResolveName(hwnd, pszName);
  2197. if (ITEM_ERR == index) return FALSE;
  2198. TOOLBAR *toolbar = GetToolbar(hwnd);
  2199. if (index == toolbar->items->size()) return FALSE;
  2200. ToolbarItem *item = toolbar->items->at(index);
  2201. if (NULL == item) return FALSE;
  2202. item->SetStyle(hwnd, (0 != fEnable) ? 0 : ToolbarItem::stateDisabled, ToolbarItem::stateDisabled);
  2203. if (FALSE == fEnable && toolbar->highlighted == item)
  2204. Toolbar_SetTooltipItem(hwnd, NULL);
  2205. Toolbar_UpdateTabstop(hwnd);
  2206. return TRUE;
  2207. }
  2208. static LRESULT Toolbar_OnGetItemStyle(HWND hwnd, LPCSTR pszName, UINT fMask)
  2209. {
  2210. INT index = Toolbar_ResolveName(hwnd, pszName);
  2211. if (ITEM_ERR == index) return 0;
  2212. TOOLBAR *toolbar = GetToolbar(hwnd);
  2213. if (index == toolbar->items->size()) return 0;
  2214. UINT itemStyle = toolbar->items->at(index)->GetStyle();
  2215. return (itemStyle & fMask);
  2216. }
  2217. static LRESULT Toolbar_OnGetItemCommand(HWND hwnd, LPCSTR pszName)
  2218. {
  2219. INT index = Toolbar_ResolveName(hwnd, pszName);
  2220. if (ITEM_ERR == index) return 0;
  2221. TOOLBAR *toolbar = GetToolbar(hwnd);
  2222. if (index == toolbar->items->size()) return 0;
  2223. return toolbar->items->at(index)->GetCommandId();
  2224. }
  2225. static LRESULT Toolbar_OnShowItem(HWND hwnd, LPCSTR pszName, BOOL fShow)
  2226. {
  2227. INT index = Toolbar_ResolveName(hwnd, pszName);
  2228. if (ITEM_ERR == index) return FALSE;
  2229. TOOLBAR *toolbar = GetToolbar(hwnd);
  2230. if (index == toolbar->items->size()) return FALSE;
  2231. ToolbarItem *item = toolbar->items->at(index);
  2232. if (NULL == item) return FALSE;
  2233. UINT style = item->GetStyle();
  2234. if ((0 != (ToolbarItem::stateHidden & style)) == (FALSE == fShow))
  2235. return TRUE;
  2236. item->SetStyle(hwnd, (0 != fShow) ? 0 : ToolbarItem::stateHidden, ToolbarItem::stateHidden);
  2237. UINT windowStyle = GetWindowStyle(hwnd);
  2238. if (0 == (TBS_LOCKUPDATE & windowStyle))
  2239. {
  2240. Toolbar_UpdateTabstop(hwnd);
  2241. Toolbar_UpdateLayout(hwnd, FALSE);
  2242. InvalidateRect(hwnd, NULL, TRUE);
  2243. }
  2244. return TRUE;
  2245. }
  2246. static LRESULT Toolbar_OnSetItemDescription(HWND hwnd, LPCSTR pszName, LPCWSTR pszDescription)
  2247. {
  2248. INT index = Toolbar_ResolveName(hwnd, pszName);
  2249. if (ITEM_ERR == index) return FALSE;
  2250. TOOLBAR *toolbar = GetToolbar(hwnd);
  2251. if (index == toolbar->items->size()) return FALSE;
  2252. ToolbarItem *item = toolbar->items->at(index);
  2253. if (NULL == item) return FALSE;
  2254. if (FALSE == item->SetDescription(hwnd, pszDescription))
  2255. return FALSE;
  2256. if (IsWindowVisible(hwnd) && IsWindowEnabled(hwnd))
  2257. Toolbar_UpdateTip(hwnd);
  2258. return TRUE;
  2259. }
  2260. static void Toolbar_OnUpdateTip(HWND hwnd)
  2261. {
  2262. TOOLBAR *toolbar = GetToolbar(hwnd);
  2263. if (NULL != toolbar && NULL != toolbar->hTooltip)
  2264. {
  2265. TOOLINFO ti;
  2266. ti.cbSize = sizeof(TOOLINFO);
  2267. ti.uId = 0;
  2268. ti.hinst = NULL;
  2269. ti.hwnd = hwnd;
  2270. ti.lpszText = LPSTR_TEXTCALLBACK;
  2271. SendMessage(toolbar->hTooltip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
  2272. }
  2273. }
  2274. static LRESULT Toolbar_OnGetTextMetrics(HWND hwnd, TOOLBARTEXTMETRIC *metric)
  2275. {
  2276. TOOLBAR *toolbar = GetToolbar(hwnd);
  2277. if (NULL == toolbar) return FALSE;
  2278. CopyMemory(metric, &toolbar->textMetric, sizeof(TOOLBARTEXTMETRIC));
  2279. return TRUE;
  2280. }
  2281. static LRESULT Toolbar_OnGetBkBrush(HWND hwnd)
  2282. {
  2283. return (LRESULT)Toolbar_GetBrush(hwnd, TOOLBRUSH_BACK);
  2284. }
  2285. static LRESULT Toolbar_OnLayout(HWND hwnd, TOOLBARLAYOUT *layout)
  2286. {
  2287. if (NULL == layout)
  2288. return FALSE;
  2289. CopyRect(&layout->clientRect, layout->prcParent);
  2290. DWORD windowStyle = GetWindowStyle(hwnd);
  2291. TOOLBAR *toolbar = GetToolbar(hwnd);
  2292. if (NULL == toolbar || 0 == (WS_VISIBLE & windowStyle))
  2293. {
  2294. layout->insertAfter = HWND_BOTTOM;
  2295. SetRectEmpty(&layout->toolbarRect);
  2296. return TRUE;
  2297. }
  2298. CopyRect(&layout->toolbarRect, layout->prcParent);
  2299. INT toolbarHeight, clientInflate;
  2300. if (0 != (TBS_AUTOHIDE & windowStyle))
  2301. {
  2302. clientInflate = TOOLBAR_HIDDENHEIGHT;
  2303. toolbarHeight = (0 == (TBS_HIDDEN & toolbar->flags)) ?
  2304. Toolbar_GetIdealHeight(hwnd) : TOOLBAR_HIDDENHEIGHT;
  2305. }
  2306. else
  2307. {
  2308. toolbarHeight = Toolbar_GetIdealHeight(hwnd);
  2309. clientInflate = toolbarHeight;
  2310. }
  2311. if (0 != (TBS_BOTTOMDOCK & windowStyle))
  2312. {
  2313. layout->toolbarRect.top = layout->toolbarRect.bottom - toolbarHeight;
  2314. if (layout->toolbarRect.top < layout->prcParent->top)
  2315. layout->toolbarRect.top = layout->prcParent->top;
  2316. layout->clientRect.bottom -= clientInflate;
  2317. layout->insertAfter = (0 == (TBS_AUTOHIDE & windowStyle) || 0 != (TBS_HIDDEN & toolbar->flags)) ?
  2318. HWND_BOTTOM : HWND_TOP;
  2319. }
  2320. else
  2321. {
  2322. layout->toolbarRect.bottom = layout->toolbarRect.top + toolbarHeight;
  2323. if (layout->toolbarRect.bottom > layout->prcParent->bottom)
  2324. layout->toolbarRect.bottom = layout->prcParent->bottom;
  2325. layout->clientRect.top += clientInflate;
  2326. layout->insertAfter = HWND_TOP;
  2327. }
  2328. return TRUE;
  2329. }
  2330. static BOOL Toolbar_OnNextItem(HWND hwnd, LPCSTR pszName, BOOL fUseName)
  2331. {
  2332. TOOLBAR *toolbar = GetToolbar(hwnd);
  2333. if (NULL == toolbar) return FALSE;
  2334. size_t focusIndex = toolbar->iFocused;
  2335. if (FALSE == fUseName)
  2336. {
  2337. focusIndex = Toolbar_GetFocusIndex(hwnd, toolbar->iFocused, 0, FALSE);
  2338. if (((size_t)-1) == focusIndex)
  2339. {
  2340. if (TBNS_NEXTITEM == pszName)
  2341. {
  2342. focusIndex = toolbar->items->size();
  2343. if (focusIndex > 0) focusIndex--;
  2344. }
  2345. else
  2346. {
  2347. focusIndex = 0;
  2348. }
  2349. }
  2350. else
  2351. {
  2352. if (TBNS_NEXTITEM == pszName)
  2353. {
  2354. if (focusIndex <= toolbar->items->size())
  2355. focusIndex = Toolbar_GetFocusIndex(hwnd, focusIndex + 1, 1, FALSE);
  2356. }
  2357. else
  2358. {
  2359. if (focusIndex > 0)
  2360. focusIndex = Toolbar_GetFocusIndex(hwnd, focusIndex - 1, -1, FALSE);
  2361. }
  2362. if (((size_t)-1) == focusIndex)
  2363. focusIndex = toolbar->iFocused;
  2364. }
  2365. }
  2366. else
  2367. {
  2368. INT index = Toolbar_ResolveName(hwnd, pszName);
  2369. if (ITEM_ERR == index ||
  2370. !Toolbar_IsItemAcceptFocus(Toolbar_GetItem(toolbar, index), FALSE))
  2371. return FALSE;
  2372. focusIndex = (size_t)index;
  2373. }
  2374. BOOL focusChanged = FALSE;
  2375. if (focusIndex != toolbar->iFocused)
  2376. {
  2377. ToolbarItem *itemNew, *itemOld;
  2378. itemOld = Toolbar_GetItem(toolbar, toolbar->iFocused);
  2379. itemNew = Toolbar_GetItem(toolbar, focusIndex);
  2380. toolbar->iFocused = focusIndex;
  2381. if (NULL != itemOld) itemOld->SetFocus(hwnd, itemNew, FALSE);
  2382. if (NULL != itemNew) itemNew->SetFocus(hwnd, itemOld, TRUE);
  2383. focusChanged = TRUE;
  2384. }
  2385. HWND hFocus = GetFocus();
  2386. if (hwnd != hFocus && FALSE == IsChild(hwnd, hFocus))
  2387. {
  2388. if (IsWindowVisible(hwnd) && IsWindowEnabled(hwnd))
  2389. {
  2390. HWND hRoot = GetAncestor(hwnd, GA_ROOT);
  2391. if (NULL != hRoot)
  2392. SendMessage(hwnd, WM_NEXTDLGCTL, (WPARAM)hwnd, TRUE);
  2393. if (hwnd != GetFocus())
  2394. SetFocus(hwnd);
  2395. }
  2396. }
  2397. else if (FALSE == focusChanged)
  2398. {
  2399. ToolbarItem *item = Toolbar_GetItem(toolbar, toolbar->iFocused);
  2400. if (NULL != item) item->SetFocus(hwnd, item, TRUE);
  2401. }
  2402. return TRUE;
  2403. }
  2404. static BOOL Toolbar_OnGetItemInfo(HWND hwnd, LPCSTR pszName, TBITEMINFO *itemInfo)
  2405. {
  2406. if (NULL == itemInfo)
  2407. return FALSE;
  2408. INT index = Toolbar_ResolveName(hwnd, pszName);
  2409. if (ITEM_ERR == index) return FALSE;
  2410. TOOLBAR *toolbar = GetToolbar(hwnd);
  2411. if (NULL == toolbar || index == toolbar->items->size()) return FALSE;
  2412. ToolbarItem *item = toolbar->items->at(index);
  2413. if (NULL == item) return FALSE;
  2414. BOOL result = TRUE;
  2415. itemInfo->commandId = item->GetCommandId();
  2416. itemInfo->style = item->GetStyle();
  2417. if (NULL != itemInfo->pszText)
  2418. {
  2419. if (FAILED(item->GetText(itemInfo->pszText, itemInfo->cchText)))
  2420. result = FALSE;
  2421. }
  2422. if (NULL != itemInfo->pszDescription)
  2423. {
  2424. if (FAILED(item->GetDescription(itemInfo->pszDescription, itemInfo->cchDescription)))
  2425. result = FALSE;
  2426. }
  2427. return result;
  2428. }
  2429. static HRESULT Toolbar_GetServiceCommandState(HWND hBrowser,ifc_omservicecommand *serviceCommand, const GUID *commandGroup, UINT commandId, HRESULT defaultState)
  2430. {
  2431. HRESULT state = (NULL != serviceCommand) ? serviceCommand->QueryState(hBrowser, commandGroup, commandId) : defaultState;
  2432. if (E_NOTIMPL == state)
  2433. state = defaultState;
  2434. return state;
  2435. }
  2436. static INT Toolbar_InsertItemHelper(HWND hwnd, LPCSTR pszName, UINT overrideStyle, INT insertBefore)
  2437. {
  2438. INT index = ITEM_ERR;
  2439. TOOLBAR *toolbar = GetToolbar(hwnd);
  2440. if (NULL != toolbar && NULL != toolbar->items)
  2441. {
  2442. ToolbarItem *item = Toolbar_CreateItem(pszName, overrideStyle);
  2443. if (NULL != item)
  2444. {
  2445. index = Toolbar_InsertItemInternal(toolbar, item, insertBefore, hwnd);
  2446. item->Release();
  2447. }
  2448. }
  2449. return index;
  2450. }
  2451. static INT Toolbar_AddItemHelper(HWND hwnd, LPCSTR pszName, UINT overrideStyle)
  2452. {
  2453. return Toolbar_InsertItemHelper(hwnd, pszName, overrideStyle, TBIP_LAST);
  2454. }
  2455. static INT Toolbar_AddItemHelper2(HWND hwnd, LPCSTR pszName, UINT overrideStyle, ifc_omservicecommand *serviceCommand, const GUID *commandGroup, ULONG commandId, HRESULT defaultState)
  2456. {
  2457. HWND hBrowser = GetParent(hwnd);
  2458. HRESULT commandState = Toolbar_GetServiceCommandState(hBrowser, serviceCommand, commandGroup, commandId, defaultState);
  2459. if (CMDSTATE_ENABLED != commandState)
  2460. return ITEM_ERR;
  2461. return Toolbar_AddItemHelper(hwnd, pszName, overrideStyle);
  2462. }
  2463. static LRESULT Toolbar_OnAutoPopulate(HWND hwnd, ifc_omservice *service, UINT flags)
  2464. {
  2465. TOOLBAR *toolbar = GetToolbar(hwnd);
  2466. if (NULL == toolbar || NULL == toolbar->items)
  2467. return 0;
  2468. ifc_omservicecommand *serviceCommand;
  2469. if (NULL == service || FAILED(service->QueryInterface(IFC_OmServiceCommand, (void**)&serviceCommand)))
  2470. serviceCommand = NULL;
  2471. UINT windowStyle = GetWindowStyle(hwnd);
  2472. Toolbar_ClearItems(toolbar);
  2473. ToolbarItem *item;
  2474. HRESULT commandStatus;
  2475. size_t blockSize, currentSize;
  2476. HWND hBrowser = GetParent(hwnd);
  2477. // Back/Forward
  2478. commandStatus = Toolbar_GetServiceCommandState(hBrowser, serviceCommand, &CMDGROUP_NAVIGATION, NAVCOMMAND_BACKFORWARD, CMDSTATE_ENABLED);
  2479. if (SUCCEEDED(commandStatus))
  2480. {
  2481. Toolbar_AddItemHelper(hwnd, TOOLITEM_BUTTON_BACK, ToolbarItem::stateDisabled);
  2482. Toolbar_AddItemHelper(hwnd, TOOLITEM_BUTTON_FORWARD, ToolbarItem::stateDisabled);
  2483. // History
  2484. if (0 != toolbar->items->size())
  2485. {
  2486. Toolbar_AddItemHelper2(hwnd, TOOLITEM_BUTTON_HISTORY, ToolbarItem::stateDisabled | ToolbarItem::styleNoChevron,
  2487. serviceCommand, &CMDGROUP_NAVIGATION, NAVCOMMAND_HISTORY, CMDSTATE_ENABLED);
  2488. }
  2489. }
  2490. blockSize = toolbar->items->size();
  2491. // Home
  2492. Toolbar_AddItemHelper2(hwnd, TOOLITEM_BUTTON_HOME, ToolbarItem::stateDisabled,
  2493. serviceCommand, &CMDGROUP_NAVIGATION, NAVCOMMAND_HOME, CMDSTATE_ENABLED);
  2494. // Refresh
  2495. Toolbar_AddItemHelper2(hwnd, TOOLITEM_BUTTON_REFRESH, ToolbarItem::stateDisabled,
  2496. serviceCommand, &CMDGROUP_NAVIGATION, NAVCOMMAND_REFRESH, CMDSTATE_ENABLED);
  2497. // Stop
  2498. Toolbar_AddItemHelper2(hwnd, TOOLITEM_BUTTON_STOP, ToolbarItem::stateDisabled,
  2499. serviceCommand, &CMDGROUP_NAVIGATION, NAVCOMMAND_STOP, CMDSTATE_ENABLED);
  2500. currentSize = toolbar->items->size();
  2501. if (0 != blockSize && 0 != currentSize && blockSize != currentSize)
  2502. Toolbar_InsertItemHelper(hwnd, TOOLITEM_SPACE, ToolbarItem::styleNoChevron, (INT)blockSize);
  2503. BOOL fShowFlexSpace = TRUE;
  2504. // Addressbar
  2505. UINT style = ToolbarItem::styleNoChevron | ToolbarItem::stateDisabled | ToolbarItem::stylePopup;
  2506. if (0 == (TBS_FORCEADDRESS & windowStyle) &&
  2507. CMDSTATE_ENABLED != Toolbar_GetServiceCommandState(hBrowser, serviceCommand,
  2508. &CMDGROUP_ADDRESSBAR, ADDRESSCOMMAND_VISIBLE,
  2509. (0 != (TBS_SHOWADDRESS & windowStyle)) ? CMDSTATE_ENABLED : CMDSTATE_DISABLED))
  2510. {
  2511. style |= ToolbarItem::stateHidden;
  2512. }
  2513. if (CMDSTATE_ENABLED == Toolbar_GetServiceCommandState(hBrowser, serviceCommand,
  2514. &CMDGROUP_ADDRESSBAR, ADDRESSCOMMAND_READONLY,
  2515. (0 != (TBPF_READONLYADDRESS & flags) ? CMDSTATE_ENABLED : CMDSTATE_DISABLED)))
  2516. {
  2517. style |= ToolbarAddress::styleAddressReadonly;
  2518. }
  2519. if (0 == (TBS_FANCYADDRESS & windowStyle))
  2520. style |= ToolbarAddress::styleAddressShowReal;
  2521. if (ITEM_ERR != Toolbar_AddItemHelper(hwnd, TOOLITEM_ADDRESSBAR, style))
  2522. {
  2523. currentSize++;
  2524. if (0 == (ToolbarItem::stateHidden & style))
  2525. fShowFlexSpace = FALSE;
  2526. }
  2527. if (0 != currentSize)
  2528. {
  2529. Toolbar_AddItemHelper(hwnd, TOOLITEM_FLEXSPACE, ToolbarItem::stylePopup | ((FALSE == fShowFlexSpace) ? ToolbarItem::stateHidden : 0));
  2530. }
  2531. if (NULL != serviceCommand && 0 == (TBPF_NOSERVICECOMMANDS & flags))
  2532. {
  2533. blockSize = toolbar->items->size();
  2534. // Rating
  2535. commandStatus = Toolbar_GetServiceCommandState(hBrowser, serviceCommand, &CMDGROUP_SERVICE, SVCCOMMAND_RATE, CMDSTATE_UNKNOWN);
  2536. if (SUCCEEDED(commandStatus))
  2537. {
  2538. UINT rating;
  2539. if (NULL != service && SUCCEEDED(service->GetRating(&rating)))
  2540. {
  2541. if (ITEM_ERR != Toolbar_AddItemHelper(hwnd, TOOLITEM_USERRATING, 0))
  2542. Toolbar_SetItemInt(hwnd, TOOLITEM_USERRATING, rating);
  2543. }
  2544. }
  2545. // Show Info
  2546. Toolbar_AddItemHelper2(hwnd, TOOLITEM_CMDLINK_INFO, ToolbarItem::styleChevronOnly,
  2547. serviceCommand, &CMDGROUP_SERVICE, SVCCOMMAND_SHOWINFO, CMDSTATE_UNKNOWN);
  2548. // Report
  2549. Toolbar_AddItemHelper2(hwnd, TOOLITEM_CMDLINK_REPORT, ToolbarItem::styleChevronOnly,
  2550. serviceCommand, &CMDGROUP_SERVICE, SVCCOMMAND_REPORT, CMDSTATE_UNKNOWN);
  2551. // Remove
  2552. Toolbar_AddItemHelper2(hwnd, TOOLITEM_CMDLINK_UNSUBSCRIBE, ToolbarItem::styleChevronOnly,
  2553. serviceCommand, &CMDGROUP_SERVICE, SVCCOMMAND_UNSUBSCRIBE, CMDSTATE_UNKNOWN);
  2554. currentSize = toolbar->items->size();
  2555. if (0 != currentSize && blockSize != currentSize)
  2556. Toolbar_AddItemHelper(hwnd, TOOLITEM_SEPARATOR, ToolbarItem::styleChevronOnly);
  2557. }
  2558. const TOOLBARITEMINSERTREC szBrowserTools[] =
  2559. {
  2560. { TOOLITEM_BUTTON_SECURECONNECTION, ToolbarItem::stateHidden | ToolbarItem::stylePopup | ToolbarItem::styleNoChevron /*| TBIS_NOREMOVE*/ },
  2561. { TOOLITEM_BUTTON_SCRIPTERROR, ToolbarItem::stateHidden | ToolbarItem::stylePopup | ToolbarItem::styleNoChevron /*| TBIS_NOREMOVE*/},
  2562. { TOOLITEM_DOWNLOADPROGRESS, ToolbarItem::styleNoChevron | ToolbarItem::stateDisabled /*| TBIS_NOREMOVE*/},
  2563. };
  2564. for (INT i = 0; i < ARRAYSIZE(szBrowserTools); i++)
  2565. {
  2566. item = Toolbar_CreateItem(szBrowserTools[i].name, szBrowserTools[i].style);
  2567. if (NULL != item)
  2568. {
  2569. Toolbar_InsertItemInternal(toolbar, item, TBIP_LAST, hwnd);
  2570. item->Release();
  2571. }
  2572. }
  2573. if (0 == (TBS_LOCKUPDATE & windowStyle))
  2574. {
  2575. Toolbar_UpdateTabstop(hwnd);
  2576. Toolbar_UpdateLayout(hwnd, FALSE);
  2577. InvalidateRect(hwnd, NULL, TRUE);
  2578. }
  2579. if (NULL != serviceCommand)
  2580. serviceCommand->Release();
  2581. return (INT)(INT_PTR)toolbar->items->size();
  2582. }
  2583. static LRESULT Toolbar_OnEnableBottomDock(HWND hwnd, BOOL fEnable)
  2584. {
  2585. UINT windowStyle = GetWindowStyle(hwnd);
  2586. UINT newStyle = windowStyle;
  2587. if (FALSE == fEnable)
  2588. newStyle &= ~TBS_BOTTOMDOCK;
  2589. else
  2590. newStyle |= TBS_BOTTOMDOCK;
  2591. if(newStyle == windowStyle)
  2592. return fEnable;
  2593. if (0 != (TBS_AUTOHIDE & windowStyle))
  2594. {
  2595. HWND hFocus = GetFocus();
  2596. if (hwnd == hFocus || IsChild(hwnd, hFocus))
  2597. Toolbar_AutoHideWindow(hwnd, TRUE);
  2598. }
  2599. SetWindowLongPtr(hwnd, GWL_STYLE, newStyle);
  2600. HWND hParent = GetParent(hwnd);
  2601. if (NULL != hParent)
  2602. {
  2603. SetWindowPos(hParent, NULL, 0, 0, 0, 0,
  2604. SWP_NOSIZE |SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
  2605. }
  2606. HWND insertAfter = HWND_TOP;
  2607. if (0 != (TBS_BOTTOMDOCK & newStyle))
  2608. {
  2609. TOOLBAR *toolbar = GetToolbar(hwnd);
  2610. if (NULL == toolbar ||
  2611. 0 == (TBS_AUTOHIDE & windowStyle) ||
  2612. 0 != (TBS_HIDDEN & toolbar->flags))
  2613. {
  2614. insertAfter = HWND_BOTTOM;
  2615. }
  2616. }
  2617. SetWindowPos(hwnd, insertAfter, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE | SWP_NOREDRAW);
  2618. InvalidateRect(hwnd, NULL, TRUE);
  2619. return !fEnable;
  2620. }
  2621. static LRESULT Toolbar_OnEnableAutoHide(HWND hwnd, BOOL fEnable)
  2622. {
  2623. TOOLBAR *toolbar = GetToolbar(hwnd);
  2624. if (NULL == toolbar) return 0;
  2625. UINT windowStyle = GetWindowStyle(hwnd);
  2626. UINT newStyle = windowStyle;
  2627. KillTimer(hwnd, TIMER_AUTOHIDE_ID);
  2628. toolbar->flags &= ~TBS_HIDETIMERSET;
  2629. if (FALSE == fEnable)
  2630. {
  2631. newStyle &= ~TBS_AUTOHIDE;
  2632. toolbar->flags &= ~TBS_HIDDEN;
  2633. }
  2634. else
  2635. {
  2636. newStyle |= TBS_AUTOHIDE;
  2637. HWND hFocus = GetFocus();
  2638. if (hwnd != hFocus && FALSE == IsChild(hwnd, hFocus))
  2639. toolbar->flags |= TBS_HIDDEN;
  2640. }
  2641. if(newStyle == windowStyle)
  2642. return fEnable;
  2643. SetWindowLongPtr(hwnd, GWL_STYLE, newStyle);
  2644. HWND hParent = GetParent(hwnd);
  2645. if (NULL != hParent)
  2646. {
  2647. SetWindowPos(hParent, NULL, 0, 0, 0, 0,
  2648. SWP_NOSIZE |SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
  2649. }
  2650. InvalidateRect(hwnd, NULL, TRUE);
  2651. return !fEnable;
  2652. }
  2653. static LRESULT Toolbar_OnEnableTabStop(HWND hwnd, BOOL fEnable)
  2654. {
  2655. UINT windowStyle = GetWindowStyle(hwnd);
  2656. UINT newStyle = windowStyle;
  2657. if (FALSE == fEnable)
  2658. newStyle &= ~TBS_TABSTOP;
  2659. else
  2660. newStyle |= TBS_TABSTOP;
  2661. if(newStyle == windowStyle)
  2662. return fEnable;
  2663. SetWindowLongPtr(hwnd, GWL_STYLE, newStyle);
  2664. Toolbar_UpdateTabstop(hwnd);
  2665. return !fEnable;
  2666. }
  2667. static LRESULT Toolbar_OnEnableForceAddress(HWND hwnd, BOOL fEnable)
  2668. {
  2669. UINT windowStyle = GetWindowStyle(hwnd);
  2670. UINT newStyle = windowStyle;
  2671. if (FALSE == fEnable)
  2672. newStyle &= ~TBS_FORCEADDRESS;
  2673. else
  2674. newStyle |= TBS_FORCEADDRESS;
  2675. if(newStyle == windowStyle)
  2676. return fEnable;
  2677. SetWindowLongPtr(hwnd, GWL_STYLE, newStyle);
  2678. TOOLBAR *toolbar = GetToolbar(hwnd);
  2679. if (NULL != toolbar && NULL != toolbar->items)
  2680. {
  2681. size_t index = toolbar->items->size();
  2682. while(index--)
  2683. {
  2684. ToolbarItem *item = toolbar->items->at(index);
  2685. if (NULL != item && item->IsEqual(TOOLITEM_ADDRESSBAR, -1))
  2686. {
  2687. UINT itemStyle = item->GetStyle();
  2688. if (FALSE != fEnable)
  2689. {
  2690. if (0 != (ToolbarItem::stateHidden & itemStyle))
  2691. {
  2692. Toolbar_LockUpdate(hwnd, TRUE);
  2693. if (FALSE != Toolbar_ShowItem(hwnd, MAKEINTRESOURCE(index), TRUE))
  2694. {
  2695. size_t nextIndex = index + 1;
  2696. if (nextIndex < toolbar->items->size())
  2697. {
  2698. ToolbarItem *nextItem = toolbar->items->at(nextIndex);
  2699. if (NULL != nextItem && nextItem->IsEqual(TOOLITEM_FLEXSPACE, -1))
  2700. {
  2701. Toolbar_ShowItem(hwnd, MAKEINTRESOURCE(nextIndex), FALSE);
  2702. }
  2703. }
  2704. }
  2705. Toolbar_LockUpdate(hwnd, FALSE);
  2706. InvalidateRect(hwnd, NULL, TRUE);
  2707. }
  2708. }
  2709. else
  2710. {
  2711. HRESULT commandState = E_NOTIMPL;
  2712. HWND hBrowser = GetParent(hwnd);
  2713. ifc_omservice *service;
  2714. if (NULL != hBrowser && FALSE != BrowserControl_GetService(hBrowser, &service))
  2715. {
  2716. ifc_omservicecommand *serviceCommand;
  2717. if (SUCCEEDED(service->QueryInterface(IFC_OmServiceCommand, (void**)&serviceCommand)))
  2718. {
  2719. commandState = serviceCommand->QueryState(hBrowser, &CMDGROUP_ADDRESSBAR, ADDRESSCOMMAND_VISIBLE);
  2720. serviceCommand->Release();
  2721. }
  2722. service->Release();
  2723. }
  2724. if (E_NOTIMPL == commandState)
  2725. commandState = (0 != (TBS_SHOWADDRESS & windowStyle)) ? CMDSTATE_ENABLED : CMDSTATE_DISABLED;
  2726. if (0 == (ToolbarItem::stateHidden & itemStyle) && CMDSTATE_ENABLED != commandState)
  2727. {
  2728. Toolbar_LockUpdate(hwnd, TRUE);
  2729. if (FALSE != Toolbar_ShowItem(hwnd, MAKEINTRESOURCE(index), FALSE))
  2730. {
  2731. size_t nextIndex = index + 1;
  2732. if (nextIndex < toolbar->items->size())
  2733. {
  2734. ToolbarItem *nextItem = toolbar->items->at(nextIndex);
  2735. if (NULL != nextItem && nextItem->IsEqual(TOOLITEM_FLEXSPACE, -1))
  2736. {
  2737. Toolbar_ShowItem(hwnd, MAKEINTRESOURCE(nextIndex), TRUE);
  2738. }
  2739. }
  2740. }
  2741. Toolbar_LockUpdate(hwnd, FALSE);
  2742. InvalidateRect(hwnd, NULL, TRUE);
  2743. }
  2744. }
  2745. break;
  2746. }
  2747. }
  2748. }
  2749. return !fEnable;
  2750. }
  2751. static LRESULT Toolbar_OnEnableFancyAddress(HWND hwnd, BOOL fEnable)
  2752. {
  2753. UINT windowStyle = GetWindowStyle(hwnd);
  2754. UINT newStyle = windowStyle;
  2755. if (FALSE == fEnable)
  2756. newStyle &= ~TBS_FANCYADDRESS;
  2757. else
  2758. newStyle |= TBS_FANCYADDRESS;
  2759. if(newStyle == windowStyle)
  2760. return fEnable;
  2761. SetWindowLongPtr(hwnd, GWL_STYLE, newStyle);
  2762. TOOLBAR *toolbar = GetToolbar(hwnd);
  2763. if (NULL != toolbar && NULL != toolbar->items)
  2764. {
  2765. size_t index = toolbar->items->size();
  2766. while(index--)
  2767. {
  2768. ToolbarItem *item = toolbar->items->at(index);
  2769. if (NULL != item && item->IsEqual(TOOLITEM_ADDRESSBAR, -1))
  2770. {
  2771. UINT style = (FALSE != fEnable) ? 0 : ToolbarAddress::styleAddressShowReal;
  2772. item->SetStyle(hwnd, style, ToolbarAddress::styleAddressShowReal);
  2773. break;
  2774. }
  2775. }
  2776. }
  2777. return !fEnable;
  2778. }
  2779. static LRESULT Toolbar_OnSetBrowserHost(HWND hwnd, HWND hBrowser)
  2780. {
  2781. TOOLBAR *toolbar = GetToolbar(hwnd);
  2782. if (NULL == toolbar) return 0;
  2783. toolbar->hBrowser = hBrowser;
  2784. return TRUE;
  2785. }
  2786. static LRESULT Toolbar_OnGetImageListHeight(HWND hwnd)
  2787. {
  2788. TOOLBAR *toolbar = GetToolbar(hwnd);
  2789. INT iconCX, iconCY;
  2790. if (NULL == toolbar ||
  2791. NULL == toolbar->imageList ||
  2792. FALSE == ImageList_GetIconSize(toolbar->imageList, &iconCX, &iconCY))
  2793. {
  2794. return 0;
  2795. }
  2796. return iconCY/TOOLBAR_ICONSTATE_COUNT;
  2797. }
  2798. static LRESULT Toolbar_OnGetNextTabItem(HWND hwnd, LPCSTR pszName, BOOL fPrevious)
  2799. {
  2800. TOOLBAR *toolbar = GetToolbar(hwnd);
  2801. if (NULL == toolbar) return ITEM_ERR;
  2802. INT index = Toolbar_ResolveName(hwnd, pszName);
  2803. if (ITEM_ERR == index)
  2804. return ITEM_ERR;
  2805. if (0 == index && FALSE == fPrevious)
  2806. return ITEM_ERR;
  2807. UINT windowStyle = GetWindowStyle(hwnd);
  2808. BOOL fTabstopOnly = (0 == (TBS_TABSTOP & windowStyle));
  2809. INT direction = (FALSE == fPrevious) ? 1 : -1;
  2810. return Toolbar_GetFocusIndex(hwnd, index + direction, direction, fTabstopOnly);
  2811. }
  2812. static void Toolbar_OnCheckHide(HWND hwnd, BOOL fImmediate)
  2813. {
  2814. Toolbar_AutoHideWindow(hwnd, fImmediate);
  2815. }
  2816. static LRESULT Toolbar_OnGetTextLength(HWND hwnd, LPCSTR pszName, size_t *textLength)
  2817. {
  2818. if (NULL == textLength) return FALSE;
  2819. *textLength = 0;
  2820. TOOLBAR *toolbar = GetToolbar(hwnd);
  2821. if (NULL == toolbar) return FALSE;
  2822. size_t index = Toolbar_ResolveName(hwnd, pszName);
  2823. if (ITEM_ERR == index) return FALSE;
  2824. ToolbarItem *item = Toolbar_GetItem(toolbar, index);
  2825. if (NULL == item) return FALSE;
  2826. if (FAILED(item->GetTextLength(textLength)))
  2827. return FALSE;
  2828. return TRUE;
  2829. }
  2830. static LRESULT CALLBACK Toolbar_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  2831. {
  2832. switch (uMsg)
  2833. {
  2834. case WM_CREATE: return Toolbar_OnCreate(hwnd, (CREATESTRUCT*)lParam);
  2835. case WM_DESTROY: Toolbar_OnDestroy(hwnd); break;
  2836. case WM_PAINT: Toolbar_OnPaint(hwnd); return 0;
  2837. case WM_PRINTCLIENT: Toolbar_OnPrintClient(hwnd, (HDC)wParam, (UINT)lParam); return 0;
  2838. case WM_ERASEBKGND: return 0;
  2839. case WM_WINDOWPOSCHANGED: Toolbar_OnWindowPosChanged(hwnd, (WINDOWPOS*)lParam); return 0;
  2840. case WM_SETREDRAW: Toolbar_OnSetRedraw(hwnd, (BOOL)wParam); return 0;
  2841. case WM_MOUSEMOVE: Toolbar_OnMouseMove(hwnd, (UINT)wParam, MAKEPOINTS(lParam)); return 0;
  2842. case WM_MOUSELEAVE: Toolbar_OnMouseLeave(hwnd); return 0;
  2843. case WM_LBUTTONDOWN:
  2844. Toolbar_OnLButtonDown(hwnd, (UINT)wParam, MAKEPOINTS(lParam));
  2845. break;
  2846. case WM_LBUTTONUP: Toolbar_OnLButtonUp(hwnd, (UINT)wParam, MAKEPOINTS(lParam)); break;
  2847. case WM_RBUTTONDOWN: Toolbar_OnRButtonDown(hwnd, (UINT)wParam, MAKEPOINTS(lParam)); break;
  2848. case WM_RBUTTONUP: Toolbar_OnRButtonUp(hwnd, (UINT)wParam, MAKEPOINTS(lParam)); break;
  2849. case WM_MBUTTONDOWN: Toolbar_OnMButtonDown(hwnd, (UINT)wParam, MAKEPOINTS(lParam)); break;
  2850. case WM_MBUTTONUP: Toolbar_OnMButtonUp(hwnd, (UINT)wParam, MAKEPOINTS(lParam)); break;
  2851. case WM_CAPTURECHANGED: Toolbar_OnCaptureChanged(hwnd, (HWND)lParam); return 0;
  2852. case WM_GETFONT: return Toolbar_OnGetFont(hwnd);
  2853. case WM_SETFONT: Toolbar_OnSetFont(hwnd, (HFONT)wParam, (BOOL)LOWORD(lParam)); return 0;
  2854. case WM_NOTIFY: return Toolbar_OnNotify(hwnd, (INT)wParam, (NMHDR*)lParam);
  2855. case WM_COMMAND: Toolbar_OnCommand(hwnd, LOWORD(wParam), HIWORD(wParam), (HWND)lParam); return 0;
  2856. case WM_ENTERMENULOOP: Toolbar_OnEnterMenuLoop(hwnd, (BOOL)wParam); return 0;
  2857. case WM_EXITMENULOOP: Toolbar_OnExitMenuLoop(hwnd, (BOOL)wParam); return 0;
  2858. case WM_SETFOCUS: Toolbar_OnSetFocus(hwnd, (HWND)wParam); return 0;
  2859. case WM_KILLFOCUS: Toolbar_OnKillFocus(hwnd, (HWND)wParam); return 0;
  2860. case WM_CONTEXTMENU: Toolbar_OnContextMenu(hwnd, (HWND)wParam, MAKEPOINTS(lParam)); return 0;
  2861. case WM_KEYDOWN: Toolbar_OnKeyDown(hwnd, (INT)wParam, (UINT)lParam); return 0;
  2862. case WM_KEYUP: Toolbar_OnKeyUp(hwnd, (INT)wParam, (UINT)lParam); return 0;
  2863. case WM_GETDLGCODE: return Toolbar_OnGetDlgCode(hwnd, (INT)wParam, (MSG*)lParam);
  2864. case WM_UPDATEUISTATE: Toolbar_OnUpdateUiState(hwnd, LOWORD(wParam), HIWORD(wParam)); return 0;
  2865. case WM_ENABLE: Toolbar_OnEnable(hwnd, (BOOL)wParam); return 0;
  2866. case WM_SETCURSOR: return Toolbar_OnSetCursor(hwnd, (HWND)wParam, LOWORD(lParam), HIWORD(lParam));
  2867. case WM_CTLCOLOREDIT: return Toolbar_OnColorEdit(hwnd, (HDC)wParam, (HWND)lParam);
  2868. case WM_CTLCOLORSTATIC: return Toolbar_OnColorStatic(hwnd, (HDC)wParam, (HWND)lParam);
  2869. case TBM_UPDATESKIN: Toolbar_OnUpdateSkin(hwnd, (BOOL)lParam); return 0;
  2870. case TBM_GETIDEALHEIGHT: return Toolbar_OnGetIdealHeight(hwnd);
  2871. case TBM_GETICONSIZE: return Toolbar_OnGetIconSize(hwnd, (INT)wParam, (SIZE*)lParam);
  2872. case TBM_SENDCOMMAND: return Toolbar_OnSendCommand(hwnd, (INT)wParam);
  2873. case TBM_DRAWICON: return Toolbar_OnDrawIcon(hwnd, (TOOLBARDRAWICONPARAM*)lParam);
  2874. case TBM_GETITEMCOUNT: return Toolbar_OnGetItemCount(hwnd);
  2875. case TBM_CLEAR: return Toolbar_OnClear(hwnd);
  2876. case TBM_INSERTITEM: return Toolbar_OnInsertItem(hwnd, (TOOLBARINSERTITEM*)lParam);
  2877. case TBM_FINDITEM: return Toolbar_OnFindItem(hwnd, (LPCSTR)lParam);
  2878. case TBM_REMOVEITEM: return Toolbar_OnRemoveItem(hwnd, (LPCSTR)lParam);
  2879. case TBM_SETITEMINT: return Toolbar_OnSetItemInt(hwnd, (LPCSTR)lParam, (INT)wParam);
  2880. case TBM_SETITEMSTRING: return Toolbar_OnSetItemString(hwnd, (LPCSTR)lParam, (LPCWSTR)wParam);
  2881. case TBM_GETBKCOLOR: return Toolbar_OnGetBkColor(hwnd);
  2882. case TBM_GETFGCOLOR: return Toolbar_OnGetFgColor(hwnd);
  2883. case TBM_GETTEXTCOLOR: return Toolbar_OnGetTextColor(hwnd);
  2884. case TBM_GETHILITECOLOR: return Toolbar_OnGetHiliteColor(hwnd);
  2885. case TBM_ENABLEITEM: return Toolbar_OnEnableItem(hwnd,(LPCSTR)lParam, (BOOL)wParam);
  2886. case TBM_SHOWITEM: return Toolbar_OnShowItem(hwnd,(LPCSTR)lParam, (BOOL)wParam);
  2887. case TBM_UPDATETIP: Toolbar_OnUpdateTip(hwnd); return 0;
  2888. case TBM_GETTEXTMETRICS: return Toolbar_OnGetTextMetrics(hwnd, (TOOLBARTEXTMETRIC*)lParam);
  2889. case TBM_GETBKBRUSH: return Toolbar_OnGetBkBrush(hwnd);
  2890. case TBM_LAYOUT: return Toolbar_OnLayout(hwnd, (TOOLBARLAYOUT*)lParam);
  2891. case TBM_NEXTITEM: return Toolbar_OnNextItem(hwnd, (LPCSTR)lParam, (BOOL)wParam);
  2892. case TBM_GETITEMSTYLE: return Toolbar_OnGetItemStyle(hwnd, (LPCSTR)lParam, (UINT)wParam);
  2893. case TBM_GETITEMCOMMAND: return Toolbar_OnGetItemCommand(hwnd, (LPCSTR)lParam);
  2894. case TBM_SETITEMDESCRIPTION:return Toolbar_OnSetItemDescription(hwnd, (LPCSTR)lParam, (LPCWSTR)wParam);
  2895. case TBM_GETITEMINFO: return Toolbar_OnGetItemInfo(hwnd, (LPCSTR)lParam, (TBITEMINFO*)wParam);
  2896. case TBM_AUTOPOPULATE: return Toolbar_OnAutoPopulate(hwnd, (ifc_omservice*)lParam, (UINT)wParam);
  2897. case TBM_ENABLEBOTTOMDOCK: return Toolbar_OnEnableBottomDock(hwnd, (BOOL)lParam);
  2898. case TBM_ENABLEAUTOHIDE: return Toolbar_OnEnableAutoHide(hwnd, (BOOL)lParam);
  2899. case TBM_ENABLETABSTOP: return Toolbar_OnEnableTabStop(hwnd, (BOOL)lParam);
  2900. case TBM_ENABLEFORCEADDRESS: return Toolbar_OnEnableForceAddress(hwnd, (BOOL)lParam);
  2901. case TBM_ENABLEFANCYADDRESS: return Toolbar_OnEnableFancyAddress(hwnd, (BOOL)lParam);
  2902. case TBM_SETBROWSERHOST: return Toolbar_OnSetBrowserHost(hwnd, (HWND)lParam);
  2903. case TBM_GETEDITCOLOR: return Toolbar_OnGetEditColor(hwnd);
  2904. case TBM_GETEDITBKCOLOR: return Toolbar_OnGetEditBkColor(hwnd);
  2905. case TBM_GETIMAGELISTHEIGHT:return Toolbar_OnGetImageListHeight(hwnd);
  2906. case TBM_GETNEXTTABITEM: return Toolbar_OnGetNextTabItem(hwnd, (LPCSTR)lParam, (BOOL)wParam);
  2907. case TBM_CHECKHIDE: Toolbar_OnCheckHide(hwnd, (BOOL)lParam); return 0;
  2908. case TBM_GETTEXTLENGTH: return Toolbar_OnGetTextLength(hwnd, (LPCSTR)lParam, (size_t*)wParam);
  2909. }
  2910. return DefWindowProcW(hwnd, uMsg, wParam, lParam);
  2911. }
  2912. static LRESULT CALLBACK Toolbar_MouseHook(INT code, WPARAM wParam, LPARAM lParam)
  2913. {
  2914. TOOLBARMOUSEHOOK *hook = (TOOLBARMOUSEHOOK*)Plugin_TlsGetValue(tlsIndex);
  2915. if (NULL == hook || NULL == hook->hHook) return FALSE;
  2916. if (code >= 0)
  2917. {
  2918. MOUSEHOOKSTRUCT *mouseHook = (MOUSEHOOKSTRUCT*)lParam;
  2919. if (mouseHook->hwnd != hook->hwnd)
  2920. {
  2921. TOOLBAR *toolbar = GetToolbar(hook->hwnd);
  2922. if (NULL != toolbar)
  2923. {
  2924. if (0 != (TBS_HIDDEN & toolbar->flags))
  2925. {
  2926. Toolbar_RemoveMouseHook(hook->hwnd, TRUE);
  2927. }
  2928. else if (0 == ((TBS_MENULOOP | TBS_HIDETIMERSET) & toolbar->flags))
  2929. {
  2930. HWND hAncestor = GetAncestor(mouseHook->hwnd, GA_ROOT);
  2931. BOOL autoHide;
  2932. if (NULL == hAncestor || FALSE == IsChild(hAncestor, hook->hwnd))
  2933. {
  2934. autoHide = TRUE;
  2935. }
  2936. else
  2937. {
  2938. RECT toolbarRect;
  2939. if (!GetWindowRect(hook->hwnd, &toolbarRect))
  2940. SetRectEmpty(&toolbarRect);
  2941. toolbarRect.top -= 6;
  2942. toolbarRect.bottom += 6;
  2943. autoHide = !PtInRect(&toolbarRect, mouseHook->pt);
  2944. }
  2945. if (FALSE != autoHide)
  2946. Toolbar_AutoHideWindow(hook->hwnd, FALSE);
  2947. }
  2948. }
  2949. }
  2950. }
  2951. return CallNextHookEx(hook->hHook, code, wParam, lParam);
  2952. }