OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
combobox.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1991, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of TComboBox & TComboBoxData.
7//----------------------------------------------------------------------------
8
9#include <owl/pch.h>
10
11#include <owl/combobox.h>
12
13using namespace std;
14
15namespace owl {
16
19
20
21//
22/// Constructs a TComboBoxData object, initializes Strings and ItemDatas to empty
23/// arrays, and initializes Selection and SelIndex to 0.
24//
26{
27 Strings = new TStringArray;
28 ItemDatas = new TLParamArray;
29 SelIndex = 0;
30}
31
32//
33/// Destructor for TComboBoxData. Deletes Strings, ItemDatas, and Selection.
34//
36{
37 delete Strings;
38 delete ItemDatas;
39}
40
41///30.05.2007 - Submitted by Frank Rast:
42///TComboBoxData needs a copy constructor because the
43///default copy constructor does not deep copy the
44///protected data of this class. For the same reason a
45///assignment operator is needed.
47{
48 Strings = new TStringArray;
49 ItemDatas = new TLParamArray;
50 SelIndex = 0;
51 *this = tCBD;
52}
53
55{
56 *Strings = *tCBD.Strings;
57 *ItemDatas = *tCBD.ItemDatas;
58 Selection = tCBD.Selection;
59 SelIndex = tCBD.SelIndex;
60 return *this;
61}
62
63
64
65//
66/// Flushes the Strings and ItemDatas members. Resets the index and selected string
67/// values.
68//
70{
71 Strings->Flush();
72 ItemDatas->Flush();
74}
75
76
77//
78/// Adds the specified string to the array of Strings. If IsSelected is true,
79/// AddString deletes Selection and copies str into Selection.
80//
81void
83{
84 Strings->Add(str); // add to end
85 if (isSelected)
86 Select(Strings->Size()-1);
87}
88
89
90//
91/// Adds a given string and uint32 item to the "Strings" and "ItemDatas"
92/// array and copies the string into "Selection" if "isSelected" is true
93//
94void
100
101
102//
103/// Selects an item at a given index.
104//
105void
107{
108 if (index != CB_ERR) {
109 SelIndex = index;
110 if (index < static_cast<int>(Strings->Size()))
111 Selection = (*Strings)[index];
112 }
113}
114
115
116//
117/// Selects "str", marking the matching String entry (if any) as selected
118//
119void
121{
122 int numStrings = Strings->Size();
123 SelIndex = CB_ERR;
124 for (int i = 0; i < numStrings; i++)
125 if (_tcscmp((*Strings)[i].c_str(), str) == 0) {
126 SelIndex = i;
127 break;
128 }
129 if (Selection != str)
130 Selection = str;
131}
132
133//
134/// Returns the length of the selection string excluding the terminating 0
135//
136int
138{
139 return static_cast<int>(Selection.length());
140}
141
142
143//
144/// Copies the selected string into Buffer. BufferSize includes the terminating 0
145//
146void
148{
149 if (bufferSize > 0) {
150 _tcsncpy(buffer, Selection.c_str(), bufferSize-1);
151 buffer[bufferSize - 1] = 0;
152 }
153}
154
155
156//----------------------------------------------------------------------------
157
158//
159// Constructors for a TComboBox object
160//
161/// Constructs a combo box object with the specified parent window (parent),
162/// control ID (Id), position (x, y) relative to the origin of the parent window's
163/// client area, width (w), height (h), style (style), and text length (textLimit).
164///
165/// Invokes the TListBox constructor with similar parameters. Then sets Attr.Style
166/// as follows:
167/// \code
168/// Attr.Style = WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP | CBS_SORT |
169/// CBS_AUTOHSCROLL | WS_VSCROLL | style;
170/// \endcode
171/// One of the following combo box style constants must be among the styles set in
172/// style: CBS_SIMPLE, CBS_DROPDOWN, CBS_DROPDOWNLIST, CBS_OWNERDRAWFIXED, or
173/// CBS_OWNERDRAWVARIABLE.
174///
175/// By default, an MS-Windows combobox associated with the TComboBox will have
176/// a vertical scrollbar and will maintain its entries in alphabetical order
177//
179 int id,
180 int x, int y, int w, int h,
181 uint32 style,
183 TModule* module)
184:
185 TListBox(parent, id, x, y, w, h, module),
186 TextLimit(textLimit)
187{
188 Attr.Style = WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP |
190 TRACEX(OwlControl, OWL_CDLEVEL, _T("TComboBox constructed @") << (void*)this);
191}
192
193
194//
195/// Constructs a default combo box with the given parent window control ID
196/// text length.
197//
199 int resourceId,
201 TModule* module)
202:
203 TListBox(parent, resourceId, module),
204 TextLimit(textLimit)
205{
206 TRACEX(OwlControl, OWL_CDLEVEL, _T("TComboBox constructed from resource @") << (void*)this);
207}
208
209//
210/// Constructs a combo box object to encapsulate (alias) an existing control.
211//
213:
214 TListBox(hWnd, module),
215 TextLimit(-1) // TODO: Retrieve the current text limit from the control (how?).
216{
217 TRACEX(OwlControl, OWL_CDLEVEL, _T("TComboBox constructed as an alias @") << (void*)this);
218}
219
220
221//
222//
223//
225{
226 TRACEX(OwlControl, OWL_CDLEVEL, _T("TComboBox destructed @") << (void*)this);
227}
228
229//
230/// Returns a COMBOBOXINFO struct containing information about the combobox.
231/// Wrapper for Windows API function GetComboBoxInfo.
232/// http://msdn.microsoft.com/en-us/library/windows/desktop/bb775939.aspx
233//
235{
237 auto i = COMBOBOXINFO{sizeof(COMBOBOXINFO)};
238 const auto ok = ::GetComboBoxInfo(GetHandle(), &i); CHECK(ok);
239 if (!ok) throw TXOwl{_T("::GetComboBoxInfo failed")};
240 return i;
241}
242
243//
244/// Selects the first string in the associated list box that begins with the
245/// supplied str. If there is no match, SetText sets the contents of the associated
246/// edit control to the supplied string and selects it.
247//
248void
250{
251 // If str is 0, then use empty str
252 //
253 if (!str)
254 str = _T("");
255
256 // If not in listbox, then set the edit/static portion
257 //
258 if (SetSelStringExact(str, -1) < 0)//DLN was SetSelString, which would use partial matches
259 {
260 SetWindowText(str);
261 SetEditSel(0, static_cast<int>(::_tcslen(str)));
262 }
263}
264
265
266//
267/// Sets the text length limit member and associated control
268//
269void
271{
272 TextLimit = textlimit;
273 if (GetHandle() && TextLimit != 0)
274 SendMessage(CB_LIMITTEXT, TextLimit-1);
275}
276
277
278//
279/// Returns, in the supplied reference parameters, the starting and
280/// ending positions of the text selected in the associated edit control
281//
282/// Returns CB_ERR is the combo box has no edit control
283//
284int
286{
288
291
292 return static_cast<int>(retValue);
293}
294
295//
296/// Functional style overload
297//
298std::pair<int, int>
300{
301 std::pair<int, int> r(0,0);
302 GetEditSel(r.first, r.second);
303 return r;
304}
305
306
307//
308/// Shows or hides the drop down or drop down list combo box depending on the value
309/// of show. If show is true, shows the list; if show is false, hides the list.
310//
311void
317
318
319//
320/// For combo boxes, gets the screen coordinates of the dropped down list box.
321//
322void
324{
326 CONST_CAST(TComboBox*,this)->
328
329 // BUG suggested by Luigi Bianchi
331 if(rect.Bottom() >= vertRes)
332 rect.Offset(0, -rect.Height());
333}
334
335
336//
337/// Transfers the items and selection of the combo box to or from a transfer buffer
338/// if tdSetData or tdGetData, respectively, is passed as the direction. buffer is
339/// expected to point to a TComboBoxData structure.
340///
341/// Transfer returns the size of the TComboBoxData structure. To retrieve the size
342/// without transferring data, your application must pass tdSizeData as the
343/// direction.
344//
345uint
347{
348 if (!buffer && direction != tdSizeData) return 0;
349 TComboBoxData& data = *static_cast<TComboBoxData*>(buffer);
350
351 if (direction == tdGetData)
352 {
353 // Get the strings and associated item data from the combo box.
354 //
355 data.Clear();
356 const int n = GetCount();
357 for (int i = 0; i != n; ++i)
358 data.AddStringItem(GetString(i), GetItemData(i), false);
359
360 // Get the selected string from the list by index, or
361 // if the combobox has no selection, get the string from the edit part.
362 //
363 int selIndex = GetSelIndex();
364 if (selIndex >= 0)
365 data.Select(selIndex);
366 else
367 data.SelectString(GetText());
368 }
369 else if (direction == tdSetData)
370 {
371 // Fill the combo box with strings and associated item data, and restore the selection, if any.
372 //
373 ClearList();
374 TStringArray& s = data.GetStrings();
375 TLParamArray& d = data.GetItemDatas();
376 const int n = s.GetItemsInContainer();
377 const int m = d.GetItemsInContainer();
378 for (int i = 0; i != n; ++i)
379 {
380 int index = AddString(s[i]);
381 if (i < m)
382 SetItemData(index, d[i]); // Set the data of the item for the inserted string.
383
384 // Note that inserted position may be different from 'i' when the combobox is sorted.
385 // So to ensure that we restore selection correctly, we select the indicated item early.
386 // The selection will automatically move with the item, if later strings are inserted above.
387 //
388 if (i == data.GetSelIndex())
389 SetSelIndex(index);
390 }
391
392 // If there is no selection, set the content of the edit part manually.
393 //
394 int selIndex = data.GetSelIndex();
395 if (selIndex < 0)
396 SetText(data.GetSelection());
397 }
398 return sizeof(TComboBoxData);
399}
400
401
402//
403/// Returns the name of TComboBox's registration class, "ComboBox."
404//
406{
407 return TWindowClassName{_T("COMBOBOX")};
408}
409
410//
411/// Limits the amount of text that the user can enter in the combo box's
412/// edit control to the value of TextLimit minus 1
413//
414/// Creates plain TWindow aliases for the children in the combo box so that
415/// TWindow can handle kill focus messages for focus support.
416//
417void
419{
420 TRACEX(OwlControl, 1, _T("TComboBox::SetupWindow() @ ") << (void*)this);
421
423
424 SetTextLimit(TextLimit);
425
427 while (hWnd) {
428 if (!GetWindowPtr(hWnd))
429 (new TWindow(hWnd))->SetParent(this);
431 }
432}
433
434//
435/// Cleanup aliases created in SetupWindow
436//
437void
439{
441 while (hWnd) {
443 delete wnd;
445 }
446
447 // call base class
448 //
450
451 TRACEX(OwlControl, 1, _T("TComboBox::CleanupWindow() @ ") << (void*)this);
452}
453
454
456
457#if OWL_PERSISTENT_STREAMS
458
459//
460/// Reads an instance of TComboBox from the supplied ipstream
461//
462void*
463TComboBox::Streamer::Read(ipstream& is, uint32 /*version*/) const
464{
465 ReadBaseObject((TListBox*)GetObject(), is);
466 is >> GetObject()->TextLimit;
467 return GetObject();
468}
469
470//
471/// Writes the TComboBox to the supplied opstream
472//
473void
474TComboBox::Streamer::Write(opstream& os) const
475{
476 WriteBaseObject((TListBox*)GetObject(), os);
477 os << GetObject()->TextLimit;
478}
479
480#endif
481
482
483} // OWL namespace
484
485
#define CHECK(condition)
Definition checks.h:239
#define PRECONDITION(condition)
Definition checks.h:227
#define DIAG_DECLARE_GROUP(group)
Definition checks.h:404
#define TRACEX(group, level, message)
Definition checks.h:263
uint Size() const
Definition template.h:672
uint GetItemsInContainer() const
Definition template.h:666
An interface object that represents a transfer buffer for a TComboBox.
Definition combobox.h:182
~TComboBoxData()
Destructor for TComboBoxData. Deletes Strings, ItemDatas, and Selection.
Definition combobox.cpp:35
void AddStringItem(LPCTSTR str, LPARAM itemData, bool isSelected=false)
Adds a given string and uint32 item to the "Strings" and "ItemDatas" array and copies the string into...
Definition combobox.cpp:95
TComboBoxData & operator=(const TComboBoxData &tCBD)
Definition combobox.cpp:54
void Select(int index)
Selects an item at a given index.
Definition combobox.cpp:106
void SelectString(LPCTSTR str)
Selects "str", marking the matching String entry (if any) as selected.
Definition combobox.cpp:120
const tstring & GetSelString() const
Definition combobox.h:214
int GetSelStringLength() const
Returns the length of the selection string excluding the terminating 0.
Definition combobox.cpp:137
TComboBoxData()
Constructs a TComboBoxData object, initializes Strings and ItemDatas to empty arrays,...
Definition combobox.cpp:25
void Clear()
Flushes the Strings and ItemDatas members.
Definition combobox.cpp:69
void ResetSelections()
Resets the index of the selected item and the currently selected string.
Definition combobox.h:694
void AddString(LPCTSTR str, bool isSelected=false)
Adds the specified string to the array of Strings.
Definition combobox.cpp:82
You can use TComboBox to create a combo box or a combo box control in a parent TWindow,...
Definition combobox.h:47
TComboBox(TWindow *parent, int id, int x, int y, int w, int h, uint32 style, uint textLimit, TModule *module=0)
Constructs a combo box object with the specified parent window (parent), control ID (Id),...
Definition combobox.cpp:178
tstring GetText() const
Definition combobox.h:64
virtual int GetString(TCHAR *str, int index) const
Retrieve the contents of the string at the passed index of list part of the associated combobox,...
Definition combobox.h:480
void ShowList(bool show=true)
Shows or hides the drop down or drop down list combo box depending on the value of show.
Definition combobox.cpp:312
virtual void ClearList()
Clear all the entries in list part of the associated combobox.
Definition combobox.h:410
std::pair< int, int > GetEditSel()
Functional style overload.
Definition combobox.cpp:299
auto GetInfo() const -> COMBOBOXINFO
Returns a COMBOBOXINFO struct containing information about the combobox.
Definition combobox.cpp:234
void SetTextLimit(uint textlimit)
Sets the text length limit member and associated control.
Definition combobox.cpp:270
int SetEditSel(int startPos, int endPos)
Selects characters that are between startPos and endPos in the edit control of the combo box.
Definition combobox.h:271
void SetupWindow()
Limits the amount of text that the user can enter in the combo box's edit control to the value of Tex...
Definition combobox.cpp:418
void GetDroppedControlRect(TRect &Rect) const
For combo boxes, gets the screen coordinates of the dropped down list box.
Definition combobox.cpp:323
virtual int SetSelStringExact(LPCTSTR find, int startIndex=-1)
DLN added.
Definition combobox.h:572
virtual int AddString(LPCTSTR str)
Adds a string to an associated list part of a combo box.
Definition combobox.h:356
virtual int SetItemData(int index, LPARAM itemData)
Sets the 32-bit value associated with the TComboBox's item.
Definition combobox.h:593
void CleanupWindow()
Cleanup aliases created in SetupWindow.
Definition combobox.cpp:438
virtual int GetCount() const
Return the number of entries in list part of the associated combobox.
Definition combobox.h:419
virtual int GetSelIndex() const
Returns the index of the list selection or a negative value if none exists.
Definition combobox.h:544
virtual int SetSelIndex(int index)
Selects a string of characters in a combo box.
Definition combobox.h:555
virtual LPARAM GetItemData(int index) const
Returns the 32-bit value associated with the combo box's item.
Definition combobox.h:584
void Transfer(TComboBoxData &data, TTransferDirection direction)
Definition combobox.h:133
void SetText(LPCTSTR str)
Selects the first string in the associated list box that begins with the supplied str.
Definition combobox.cpp:249
virtual auto GetWindowClassName() -> TWindowClassName
Returns the name of TComboBox's registration class, "ComboBox.".
Definition combobox.cpp:405
virtual int GetDeviceCaps(int index) const
Used under WIN3.1 or later, GetDeviceCaps returns capability information about this DC.
Definition dc.cpp:373
A DC class that provides access to the desktop window's client area which is the window behind all ot...
Definition dc.h:625
An interface object that represents a corresponding list box element.
Definition listbox.h:43
int Add(const T &t)
Definition template.h:1278
ObjectWindows dynamic-link libraries (DLLs) construct an instance of TModule, which acts as an object...
Definition module.h:75
TRect is a mathematical class derived from tagRect.
Definition geometry.h:308
Type-safe encapsulation of a Windows class name, a union between ATOM and LPCTSTR.
Definition module.h:47
TWindow, derived from TEventHandler and TStreamableBase, provides window-specific behavior and encaps...
Definition window.h:414
virtual void SetParent(TWindow *newParent)
Sets the parent for the specified window by setting Parent to the specified new Parent window object.
Definition window.cpp:738
TWindow()
Protected constructor for use by immediate virtually derived classes.
Definition window.cpp:392
HWND GetWindow(uint cmd) const
Returns the handle of the window that has the indicated relationship to this window.
Definition window.h:2784
long GetWindowLong(int index) const
Retrieves information about the window depending on the value stored in index.
Definition window.h:2388
virtual void CleanupWindow()
Always called immediately before the HWindow becomes invalid, CleanupWindow gives derived classes an ...
Definition window.cpp:2640
void SetWindowText(LPCTSTR str)
Sets the window's text to the given string (by copying).
Definition window.h:2669
TResult SendMessage(TMsgId, TParam1=0, TParam2=0) const
Sends a message (msg) to a specified window or windows.
Definition window.cpp:3288
virtual void SetupWindow()
Performs setup following creation of an associated MS-Windows window.
Definition window.cpp:2575
HWND THandle
TWindow encapsulates an HWND.
Definition window.h:418
HWND GetHandle() const
Returns the handle of the window.
Definition window.h:2020
TXOwl is root class of the ObjectWindows exception hierarchy.
Definition except.h:38
ipstream, a specialized input stream derivative of pstream, is the base class for reading (extracting...
Definition objstrm.h:391
Definition of class TComboBox and TComboBoxData the base class for all combobox controls.
#define _tcscmp
Definition cygwin.h:75
#define _tcsncpy
Definition cygwin.h:80
#define _tcslen
Definition cygwin.h:74
#define _T(x)
Definition cygwin.h:51
void ReadBaseObject(Base *base, ipstream &in)
Definition objstrm.h:1159
#define IMPLEMENT_STREAMABLE1(cls, base1)
Definition objstrm.h:1725
void WriteBaseObject(Base *base, opstream &out)
Definition objstrm.h:1150
TTransferDirection
The TTransferDirection enum describes the constants that the transfer function uses to determine how ...
Definition window.h:92
@ tdSizeData
Return the size of data transferred by the class.
Definition window.h:95
@ tdSetData
Set data from the buffer into the window.
Definition window.h:94
@ tdGetData
Get data from the window into the buffer.
Definition window.h:93
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
TObjectArray< tstring > TStringArray
Definition contain.h:37
uint16 HiUint16(LRESULT r)
Definition defs.h:270
unsigned long uint32
Definition number.h:34
TWindow * GetWindowPtr(HWND, const TApplication *)
Raw function to retrieve a TWindow pointer given an HWND from the a given app, or any app (app==0).
Definition window.cpp:1557
char tchar
Definition defs.h:77
LPARAM TParam2
Second parameter type.
Definition dispatch.h:55
uint16 LoUint16(LRESULT r)
Definition defs.h:264
TTypedArray< LPARAM, LPARAM, TStandardAllocator > TLParamArray
Definition contain.h:43
OWL_DIAGINFO
Definition animctrl.cpp:14
unsigned int uint
Definition number.h:25
#define OWL_CDLEVEL
Definition defs.h:171
#define CONST_CAST(targetType, object)
Definition defs.h:273