home *** CD-ROM | disk | FTP | other *** search
/ BUG 4 / BUGCD1997_05.BIN / aplic / clip4win / clip4win.exe / C4W30E.HUF / SOURCE / COMBOBOX.PRG < prev    next >
Text File  |  1993-06-23  |  12KB  |  523 lines

  1. ////////////////////////////
  2. //
  3. //    Clip-4-Win combobox demo
  4. //
  5. //    Copyright (C) 1992,1993 Skelton Software, Kendal Cottage, Hillam, Leeds, UK.
  6. //    All Rights Reserved.
  7. //
  8. //    Compile:    combobox /n /w
  9. //    Link:        /se:600 combobox,,,clip4win,clip4win.def
  10. //
  11. ////////////////////////////
  12.  
  13. #define    WIN_WANT_CB
  14. #define    WIN_WANT_CLIPBOARD
  15. #define    WIN_WANT_ALL
  16. #include "windows.ch"
  17.  
  18.  
  19. #define    MAKELPARAM(nLow, nHigh)        ((nLow) + (nHigh) * 65536)
  20.  
  21. #define    CR    chr(13)
  22.  
  23.  
  24. #ifndef    LIB_ONLY
  25.  
  26.  
  27. #define    ID_CBOX    1            // unique id (used for the combobox)
  28.  
  29. static    hWnd, hInst, cText, hCBox, cAppName := "Clip-4-Win"
  30. static    bEvent := {|nEvent| nil}    // current event handler
  31.  
  32.  
  33. function main()
  34. local    hMenu, nEvent
  35.  
  36. hWnd = WinSetup(cAppName, "Clip-4-Win Combo Box Demo")
  37. hInst = _GetInstance()
  38. hMenu = MenuSetup()
  39. HideCaret(hWnd)
  40.  
  41. do while .t.
  42.     do while (nEvent := ChkEvent()) == EVENT_NONE
  43.         // some "background" processing could go here
  44.     enddo
  45.     eval(bEvent, nEvent)        // give the event to the right handler
  46. enddo
  47.  
  48. return 0
  49.  
  50.  
  51. procedure DoAbout()
  52. MessageBox( , "Written by John Skelton.", "Info", MB_OK)
  53. return
  54.  
  55.  
  56. procedure DoExit()
  57. quit
  58. return
  59.  
  60.  
  61. // This is non-modal, but you have to send/receive messages
  62. procedure DoCBox()
  63. local    aList := {"Some strings", "for ComboBox", "as an example", "for you"}
  64. if hCBox != nil
  65.     DestroyWindow(hCBox)
  66. endif
  67. hCBox = CreateWindow("combobox",    ;    // window class
  68.             ,             ;    // caption for title bar (none)
  69.             WS_CHILD + WS_VISIBLE ;     // window style
  70.             + CBS_SIMPLE,    ;    // ... just a simple one
  71.             100,        ;    // x co-ordinate
  72.             50,            ;    // y co-ordinate
  73.             200,        ;    // width
  74.             137,        ;    // height
  75.             hWnd,        ;    // hWnd of parent
  76.             ID_CBOX,        ;    // id for child control to use
  77.             hInst)            // our own app instance
  78. aeval(aList, {|cStr| SendMessage(hCBox, CB_ADDSTRING, 0, cStr)} )
  79. bEvent = {|nEvent| CBoxEvent(nEvent)}
  80. SetFocus(hCBox)
  81. return
  82.  
  83.  
  84. // This is non-modal, but you have to send/receive messages
  85. procedure DoDropDown()
  86. local    aList := {"Item 1", "for ComboBox", "Item 3", "Item 4"}
  87. if hCBox != nil
  88.     DestroyWindow(hCBox)
  89. endif
  90. hCBox = CreateWindow("combobox",    ;    // window class
  91.             ,             ;    // caption for title bar (none)
  92.             WS_CHILD + WS_VISIBLE ;     // window style
  93.             + CBS_DROPDOWNLIST,    ;    // ... drops down when selected
  94.             100,        ;    // x co-ordinate
  95.             50,            ;    // y co-ordinate
  96.             200,        ;    // width
  97.             100,        ;    // height
  98.             hWnd,        ;    // hWnd of parent
  99.             ID_CBOX,        ;    // id for child control to use
  100.             hInst)            // our own app instance
  101. aeval(aList, {|cStr| SendMessage(hCBox, CB_ADDSTRING, 0, cStr)} )
  102. bEvent = {|nEvent| CBoxEvent(nEvent)}
  103. SetFocus(hCBox)
  104. return
  105.  
  106.  
  107. procedure CBoxEvent(nEvent)
  108. local    i, lRedraw := .f.
  109. do case
  110. case nEvent == EVENT_CONTROL
  111.     if _lastwParam() == ID_CBOX    // child id
  112.         if _lastHilParam() == CBN_SELCHANGE
  113.             i = SendMessage(hCBox, CB_GETCURSEL, 0, 0)
  114.             cText = space(SendMessage(hCBox, CB_GETLBTEXTLEN, i, 0))
  115.             SendMessage(hCBox, CB_GETLBTEXT, i, @cText)
  116. //            MessageBox( , nstr(i) + cText, "GETLBTEXT", MB_OK)
  117.             lRedraw = .t.
  118.         endif
  119.     endif
  120. case nEvent == EVENT_REDRAW
  121.     lRedraw = .t.
  122. case nEvent == EVENT_SETFOCUS
  123.     SetFocus(hCBox)
  124. endcase
  125. if lRedraw
  126.     SetPos(15, 15)
  127.     ? "Text chosen:" ; ? ; ? padr(cText, 40)
  128. endif
  129. return
  130.  
  131.  
  132. function nstr(n)
  133. return alltrim(str(n)) + " "
  134.  
  135.  
  136. #endif // !LIB_ONLY
  137.  
  138.  
  139. /////////
  140. //
  141. //    CBAddString( <hCBox>, <cNewStr> )  -->  nLine
  142. //
  143. //    Add a new string to the list box part of a combo box
  144. //
  145. //    Returns the position of the string added, or CB_ERR if an
  146. //    error occurs (but CB_ERRSPACE if not enough room)
  147. //
  148. /////////
  149.  
  150. function CBAddString(hCBox, cNewStr)
  151. return SendMessage(hCBox, CB_ADDSTRING, 0, cNewStr)
  152.  
  153.  
  154. /////////
  155. //
  156. //    CBCopy( <hCBox> )  -->  nil
  157. //
  158. //    Copy the edit control part of a combo box to the Windows clipboard
  159. //
  160. /////////
  161.  
  162. procedure CBCopy(hCBox)
  163. SendMessage(hCBox, WM_COPY, 0, 0)
  164. return
  165.  
  166.  
  167. /////////
  168. //
  169. //    CBCut( <hCBox> )  -->  nil
  170. //
  171. //    Cut the contents of the edit control part of a combo box to
  172. //    the Windows clipboard
  173. //
  174. /////////
  175.  
  176. procedure CBCut(hCBox)
  177. SendMessage(hCBox, WM_CUT, 0, 0)
  178. return
  179.  
  180.  
  181. /////////
  182. //
  183. //    CBDeleteString( <hCBox>, <nLine> )  -->  nCount
  184. //
  185. //    Deletes a string from the list box part of a combo box.
  186. //    <nLine> is the position of the string to delete
  187. //
  188. //    Returns the the number of strings left, or CB_ERR if an
  189. //    error occurs
  190. //
  191. /////////
  192.  
  193. function CBDeleteString(hCBox, nLine)
  194. return SendMessage(hCBox, CB_DELETESTRING, nLine, 0)
  195.  
  196.  
  197. /////////
  198. //
  199. //    CBDir( <hCBox>, <nAttr>, <cFileSpec> )  -->  nLine
  200. //
  201. //    Add a list of filenames to the list box part of a combo box
  202. //
  203. //    Returns the position of the last filename added, or CB_ERR if an
  204. //    error occurs (but CB_ERRSPACE if not enough room)
  205. //
  206. /////////
  207.  
  208. function CBDir(hCBox, nAttr, cFileSpec)
  209. return SendMessage(hCBox, CB_DIR, nAttr, cFileSpec)
  210.  
  211.  
  212. /////////
  213. //
  214. //    CBFindString( <hCBox>, <cStr>, [ <nStart> ] )  -->  nLine
  215. //
  216. //    Find a string in the list box part of a combo box.
  217. //    <nStart> optionally specifies the start of the search
  218. //
  219. //    Returns the position of the string found, or CB_ERR if not found
  220. //
  221. //    See Also: CBFindStrExact()
  222. //
  223. /////////
  224.  
  225. function CBFindString(hCBox, cStr, nStart)
  226. if nStart == nil
  227.     nStart = -1            // search from the start
  228. endif
  229. return SendMessage(hCBox, CB_FINDSTRING, nStart, cStr)
  230.  
  231.  
  232. /////////
  233. //
  234. //    CBFindStrExact( <hCBox>, <cStr>, [ <nStart> ] )  -->  nLine
  235. //
  236. //    Find a string in the list box part of a combo box.
  237. //    <nStart> optionally specifies the start of the search
  238. //
  239. //    Returns the position of the string found, or CB_ERR if not found
  240. //
  241. //    See Also: CBFindString()
  242. //
  243. /////////
  244.  
  245. function CBFindStrExact(hCBox, cStr, nStart)
  246. if nStart == nil
  247.     nStart = -1            // search from the start
  248. endif
  249. return SendMessage(hCBox, CB_FINDSTRINGEXACT, nStart, cStr)
  250.  
  251.  
  252. /////////
  253. //
  254. //    CBGetCount( <hCBox> )  -->  nCount
  255. //
  256. //    Returns the number of items in the list box of a combo box
  257. //
  258. /////////
  259.  
  260. function CBGetCount(hCBox)
  261. return SendMessage(hCBox, CB_GETCOUNT, 0, 0)
  262.  
  263.  
  264. /////////
  265. //
  266. //    CBGetCurSel( <hCBox> )  -->  nLine
  267. //
  268. //    Returns the position of the currently selected item in the list box
  269. //    of a combo box (or CB_ERR if nothing is selected)
  270. //
  271. /////////
  272.  
  273. function CBGetCurSel(hCBox)
  274. return SendMessage(hCBox, CB_GETCURSEL, 0, 0)
  275.  
  276.  
  277. /////////
  278. //
  279. //    CBGetDropRect( <hCBox> )  -->  aRect
  280. //
  281. //    Returns the screen (not window) co-ordinates of the drop-down
  282. //    rectangle for the list box part of a combo box, in
  283. //    the form {left, top, right, bottom}
  284. //
  285. /////////
  286.  
  287.  
  288. function CBGetDropRect(hCBox)
  289. local    cRect := space(8)        // room for int[4]
  290. SendMessage(hCBox, CB_GETDROPPEDCONTROLRECT, 0, @cRect)
  291. return bin2a(cRect, "int[4]")
  292.  
  293.  
  294. /////////
  295. //
  296. //    CBGetDropState( <hCBox> )  -->  lDroppedDown
  297. //
  298. //    Returns .t. if the list box part of a combo box is dropped down
  299. //
  300. /////////
  301.  
  302. function CBGetDropState(hCBox)
  303. return SendMessage(hCBox, CB_GETDROPPEDSTATE, 0, 0) != 0
  304.  
  305.  
  306. /////////
  307. //
  308. //    CBGetEditSel( <hCBox> )  -->  aSel
  309. //
  310. //    Returns the start and end of the selected part of a the edit
  311. //    control part of a combo box
  312. //
  313. //    aSel is in the form { start, end }
  314. //
  315. /////////
  316.  
  317. function CBGetEditSel(hCBox)
  318. return bin2a(c4w_l2bin(SendMessage(hCBox, CB_GETEDITSEL, 0, 0)), "int[2]")
  319.  
  320.  
  321. /////////
  322. //
  323. //    CBGetLBText( <hCBox>, <nLine> )  -->  cLine
  324. //
  325. //    Returns the specified line from within the list box of a combo box
  326. //    (<nLine> starts at 0)
  327. //
  328. /////////
  329.  
  330. function CBGetLBText(hCBox, nLine)
  331. local    nLen, cBuf := space(SendMessage(hCBox, CB_GETLBTEXTLEN, nLine, 0) + 1)
  332. nLen = SendMessage(hCBox, CB_GETLBTEXT, nLine, @cBuf)
  333. return iif(nLen == CB_ERR, nil, left(cBuf, nLen))
  334.  
  335.  
  336. /////////
  337. //
  338. //    CBGetLBTLen( <hCBox>, <nLine> )  -->  nLen
  339. //
  340. //    Returns the length of a line in the list box of a combo box
  341. //
  342. //    <nLine> counts from zero.
  343. //
  344. /////////
  345.  
  346. function CBGetLBTLen(hCBox, nLine)
  347. return SendMessage(hCBox, CB_GETLBTEXTLEN, nLine, 0)
  348.  
  349.  
  350. /////////
  351. //
  352. //    CBGetText( <hCBox> )  -->  cText
  353. //
  354. //    Get all the text in the edit control part of a combo box
  355. //
  356. /////////
  357.  
  358. function CBGetText(hCBox)
  359. local    nLen, cBuf
  360. // get the length, and include room for the trailing null that Windows
  361. // wants to add (even though we don't want it!)
  362. nLen = SendMessage(hCBox, WM_GETTEXTLENGTH, 0, 0) + 1
  363. cBuf = space(nLen)
  364. nLen = SendMessage(hCBox, WM_GETTEXT, nLen, @cBuf)    // note: not an EM_ msg
  365. return left(cBuf, nLen)
  366.  
  367.  
  368. /////////
  369. //
  370. //    CBInsertString( <hCBox>, <cNewStr>, <nLine> )  -->  nLen
  371. //
  372. //    Insert a new string in the list box part of a combo box
  373. //
  374. //    Returns the length of the string added, or CB_ERR if an
  375. //    error occurs (but CB_ERRSPACE if not enough room)
  376. //
  377. /////////
  378.  
  379. function CBInsertString(hCBox, cNewStr, nLine)
  380. return SendMessage(hCBox, CB_INSERTSTRING, nLine, cNewStr)
  381.  
  382.  
  383. /////////
  384. //
  385. //    CBLimitText( <hCBox>, <nMax> )  -->  nil
  386. //
  387. //    Sets the maximum number of characters allowed in the edit control
  388. //    of a combo box
  389. //    (in the range 0-nnnn, where nnnn is limited by the heapsize
  390. //    specified in the .def file for the linker)
  391. //
  392. /////////
  393.  
  394. procedure CBLimitText(hCBox, nMax)
  395. SendMessage(hCBox, CB_LIMITTEXT, nMax, 0)
  396. return
  397.  
  398.  
  399. /////////
  400. //
  401. //    CBPaste( <hCBox> )  -->  nil
  402. //
  403. //    Paste the contents of the Windows clipboard into the edit
  404. //    control part of a combo box
  405. //
  406. /////////
  407.  
  408. procedure CBPaste(hCBox)
  409. SendMessage(hCBox, WM_PASTE, 0, 0)
  410. return
  411.  
  412.  
  413. /////////
  414. //
  415. //    CBResetContent( <hCBox> )  -->  nil
  416. //
  417. //    Empty the list box and edit control of a combo box
  418. //
  419. /////////
  420.  
  421. procedure CBResetContent(hCBox)
  422. SendMessage(hCBox, CB_RESETCONTENT, 0, 0)
  423. return
  424.  
  425.  
  426. /////////
  427. //
  428. //    CBSelectString( <hCBox>, <cStr>, [ <nStart> ] )  -->  nLine
  429. //
  430. //    Find a string in the list box part of a combo box, and (if found)
  431. //    select it and copy it into the edit control.
  432. //    <nStart> optionally specifies the start of the search
  433. //
  434. //    Returns the position of the string found, or CB_ERR if not found
  435. //
  436. /////////
  437.  
  438. function CBSelectString(hCBox, cStr, nStart)
  439. if nStart == nil
  440.     nStart = -1            // search from the start
  441. endif
  442. return SendMessage(hCBox, CB_SELECTSTRING, nStart, cStr)
  443.  
  444.  
  445. /////////
  446. //
  447. //    CBSetCurSel( <hCBox>, <nLine> )  -->  nRet
  448. //
  449. //    Sets the position of the currently selected item in the list box
  450. //    of a combo box
  451. //
  452. //    <nLine> can be -1 to remove any selection, and clear the edit control
  453. //
  454. //    Returns <nLine> (or CB_ERR if <nLine> is too big or -1)
  455. //
  456. /////////
  457.  
  458. function CBSetCurSel(hCBox, nLine)
  459. return SendMessage(hCBox, CB_SETCURSEL, nLine, 0)
  460.  
  461.  
  462. /////////
  463. //
  464. //    CBSetEditSel( <hCBox>, <nStart>, <nEnd> )  -->  nil
  465. //
  466. //    Sets the start and end of the selected part of the edit control
  467. //    part of a combo box.
  468. //
  469. //    Special values:
  470. //
  471. //        nStart = 0, and nEnd = -1    all the text is selected
  472. //        nStart = -1            nothing selected
  473. //
  474. /////////
  475.  
  476. procedure CBSetEditSel(hCBox, nStart, nEnd)
  477. SendMessage(hCBox, CB_SETEDITSEL, 0, MAKELPARAM(nStart, nEnd))
  478. return
  479.  
  480.  
  481. /////////
  482. //
  483. //    CBShowDropDown( <hCBox>, <lShow> )  -->  nil
  484. //
  485. //    Shows/hides the drop-down list box part of a combo box
  486. //
  487. /////////
  488.  
  489. procedure CBShowDropDown(hCBox, lShow)
  490. SendMessage(hCBox, CB_SHOWDROPDOWN, iif(lShow, 1, 0), 0)
  491. return
  492.  
  493.  
  494. #ifndef    LIB_ONLY
  495.  
  496.  
  497. function MenuSetup()
  498. local    hWnd := SelectWindow(), hMenu, hPopupMenu
  499.  
  500. if (hMenu := GetMenu(hWnd)) != nil
  501.     DestroyMenu(hMenu)
  502. endif
  503.  
  504. // do new one (forget old value)
  505. hMenu = CreateMenu()
  506. hPopupMenu = CreatePopupMenu()
  507. AppendMenu(hMenu, "file", MF_ENABLED + MF_POPUP, "&File", hPopupMenu)
  508. AppendMenu(hPopupMenu, "exit", MF_ENABLED + MF_STRING, "E&xit", {|| Alert("Thanks for running this demo"), DoExit()})
  509. hPopupMenu = CreatePopupMenu()
  510. AppendMenu(hMenu, "demo", MF_ENABLED + MF_POPUP, "&Demo", hPopupMenu)
  511. AppendMenu(hPopupMenu, "combo1", MF_ENABLED + MF_STRING, "&Combo box", {|c| DoCBox()})
  512. AppendMenu(hPopupMenu, "combo2", MF_ENABLED + MF_STRING, "&DropDown", {|c| DoDropDown()})
  513. hPopupMenu = CreatePopupMenu()
  514. AppendMenu(hMenu, "help", MF_ENABLED + MF_POPUP, "&Help", hPopupMenu)
  515. AppendMenu(hPopupMenu, "about", MF_ENABLED + MF_STRING, "&About", {|| DoAbout()})
  516. SetMenu(hWnd, hMenu)
  517.  
  518. return hMenu
  519.  
  520.  
  521. #endif // !LIB_ONLY
  522.  
  523.