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
combobex.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1998 by Bidus Yura, All Rights Reserved
4//
5/// \file
6/// Implementation of the TComboBoxEx class
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9
10#include <owl/combobex.h>
11
12#include <owl/imagelst.h>
13
14#include <tchar.h>
15
16using namespace std;
17
18namespace owl {
19
21DIAG_DECLARE_GROUP(OwlCommCtrl); // CommonCtrl Diagnostic group
22
23
24DEFINE_RESPONSE_TABLE1(TComboBoxEx, TComboBox)
30
31
32//
33//
34//
35TComboBoxEx::TComboBoxEx(TWindow* parent, int id, int x, int y, int w, int h,
36 uint32 style, uint textLimit, TModule* module)
37:
38 TComboBox(parent, id, x, y, w, h,style, textLimit, module),
39 ImageList(nullptr),
40 ShouldDelete(false)
41{
43 ModifyExStyle(WS_EX_CLIENTEDGE, 0); // Creates 3d sunken inside edge
44}
45
46//
47//
48
50 TModule* module)
51:
52 TComboBox(parent, resourceId, textLen, module),
53 ImageList(nullptr),
54 ShouldDelete(false)
55{
57}
58
59//
60/// Constructs an extended combo box object to encapsulate (alias) an existing control.
61//
63:
64 TComboBox(hWnd, module),
65 ImageList(nullptr),
66 ShouldDelete(false)
67{
69}
70
71//
72//
73//
75{
76 if(ShouldDelete)
77 delete ImageList;
78}
79
80//
81/// Return the proper class name.
82/// Windows class: WC_COMBOBOXEX is defined in commctrl.h
87
88//
89//
90//
91void
93{
95 if(ImageList)
96 SendMessage(CBEM_SETIMAGELIST, 0, reinterpret_cast<LPARAM>(static_cast<HIMAGELIST>(*ImageList)));
97}
98
99//
100//
101//
102bool
104{
106 ((tstring&)item.Text()).reserve(MAX_PATH);
107 COMBOBOXEXITEM exitem;
108 item.InitItem(exitem, MAX_PATH);
109 if (index != -1)
110 exitem.iItem = index; // Use provided index.
111 if(SendMessage(CBEM_GETITEM, 0, TParam2(&exitem)) != 0){
112 item.ReFillItem(exitem);
113 return true;
114 }
115 return false;
116}
117
118//
119/// Functional style overload
120//
123{
124 TComboBoxExItem item;
125 GetItem(item, index);
126 return item;
127}
128
129
130//
131//
132//
133int
135{
137 COMBOBOXEXITEM exitem;
138 item.InitItem(exitem);
139 return static_cast<int>(SendMessage(CBEM_INSERTITEM, 0, TParam2(&exitem)));
140}
141
142//
143//
144//
145bool
147{
149 COMBOBOXEXITEM exitem;
150 item.InitItem(exitem);
151 if (index != -1)
152 exitem.iItem = index; // Use provided index.
153 return SendMessage(CBEM_SETITEM, 0, TParam2(&exitem)) != 0;
154}
155
156//
157//
158//
159void
161{
162 if(GetHandle())
163 SendMessage(CBEM_SETIMAGELIST, 0, reinterpret_cast<LPARAM>(static_cast<HIMAGELIST>(*list)));
164
165 if(ShouldDelete)
166 delete ImageList;
167 ShouldDelete = del == AutoDelete;
168 ImageList = list;
169}
170
171
172//
173/// Transfers the items and selection of the combo box to or from a transfer buffer
174/// if tdSetData or tdGetData, respectively, is passed as the direction. buffer is
175/// expected to point to a TComboBoxExData structure.
176///
177/// Transfer returns the size of the TComboBoxExData structure. To retrieve the size
178/// without transferring data, your application must pass tdSizeData as the
179/// direction.
180//
181uint
183{
184 if (!buffer && direction != tdSizeData) return 0;
185 TComboBoxExData& data = *static_cast<TComboBoxExData*>(buffer);
186
187 if (direction == tdGetData)
188 {
189 // Clear out data and fill with contents of list box part.
190 //
191 data.Clear();
192 const int count = GetCount();
193 for(int i = 0; i != count; ++i)
194 data.AddItem(GetItem(i));
195
196 // Get the selected string from the list by index, or if no selection,
197 // get the selection string from the edit box.
198 //
199 int selIndex = GetSelIndex();
200 if (selIndex >= 0)
201 data.Select(selIndex);
202 else
203 data.SelectString(GetText());
204 }
205 else if (direction == tdSetData)
206 {
207 // Fill the list box part.
208 //
209 ClearList();
210 const int count = static_cast<int>(data.Size());
211 for (int i = 0; i != count; ++i)
212 InsertItem(data.GetItem(i));
213
214 // Set the selected item, if any, otherwise set the edit part.
215 //
216 if (data.GetSelIndex() >= 0)
217 SetSelIndex(data.GetSelIndex());
218 else
219 SetText(data.GetSelection());
220 }
221 return sizeof(TComboBoxExData);
222}
223
224
225////////////////////////////////////////////////////////////////////////////////
226//
227//
228//
233
234//
235//
236//
238{
239 Init();
240 Item = item;
241 Text = str;
242 Image = image;
243 Selected = image; // By default use same image for both selected and unselected items
244}
245
247{
248 Init();
249 Item = item;
250 Text = str;
251 Image = image;
254 Indent = indent;
255 Param = param;
256
257}
258
259//
260/// Copies the property data, not the properties themselves.
261//
266
267//
268/// Assigns the property data, not the properties themselves.
269//
272{
273 Mask = item.Mask();
274 Item = item.Item();
275 Text = item.Text();
276 Image = item.Image();
277 Selected = item.Selected();
278 Overlay = item.Overlay();
279 Indent = item.Indent();
280 Param = item.Param();
281 return *this;
282}
283
284//
285//
286//
287void
288TComboBoxExItem::InitItem(COMBOBOXEXITEM& item, uint tsize) const
289{
290 memset(&item,0,sizeof(item));
291 item.mask = Mask();
292 item.iItem = Item();
293 if(Image() != -1){
294 item.mask |= CBEIF_IMAGE;
295 item.iImage |= Image();
296 }
297 if(Indent() != -1){
298 item.mask |= CBEIF_INDENT;
299 item.iIndent = Indent();
300 }
301 if(Param() != 0){
302 item.mask |= CBEIF_LPARAM;
303 item.lParam = Param();
304 }
305 if(Overlay() != -1){
306 item.mask |= CBEIF_OVERLAY;
307 item.iOverlay = Overlay();
308 }
309 if(Selected() != -1){
310 item.mask |= CBEIF_SELECTEDIMAGE;
311 item.iSelectedImage = Selected();
312 }
313 if(Text().length() || tsize){
314 item.mask |= CBEIF_TEXT;
315 item.pszText = (LPTSTR)Text().c_str();
316 item.cchTextMax = static_cast<int>(Text().length() > tsize ? Text().length() : tsize);
317 }
318}
319
320//
321//
322//
323void
324TComboBoxExItem::ReFillItem(const COMBOBOXEXITEM& item)
325{
326 Mask = item.mask;
327 Item = item.iItem;
328
329 if(item.mask & CBEIF_IMAGE)
330 Image = item.iImage;
331 else
332 Image = -1;
333
334 if(item.mask & CBEIF_INDENT)
335 Indent = item.iIndent;
336 else
337 Indent = -1;
338
339 if(item.mask & CBEIF_LPARAM)
340 Param = item.lParam;
341 else
342 Param = 0;
343
344 if(item.mask & CBEIF_OVERLAY)
345 Overlay = item.iOverlay;
346 else
347 Overlay = -1;
348
349 if(item.mask & CBEIF_SELECTEDIMAGE)
350 Selected = item.iSelectedImage;
351 else
352 Selected = -1;
353
354 if(item.mask & CBEIF_TEXT)
355 Text = tstring(item.pszText);
356 else
357 Text = tstring(_T(""));
358}
359
360
361//
362//
363//
364void
366{
367 Mask = 0; //
368 Item = -1; //
369 Text = tstring(_T(""));
370 Image = -1;
371 Selected = -1;
372 Overlay = -1;
373 Indent = -1;
374 Param = 0;
375}
376
377// class TComboBoxExData
378// ----- ---------------
379//
381:
382 Selection(_T("")),
383 SelIndex(CB_ERR)
384{
386}
387
388//
389//
390//
395
396//
397//
398//
399int
401{
402 return Items->Destroy(index);
403}
404
405//
406//
407//
408int
410{
411 return Items->Add(item);
412}
413
414//
415//
416//
419{
420 PRECONDITION(Items->Size() > static_cast<uint>(index));
421 return (*Items)[index];
422}
423
424//
425//
426//
427void
429{
430 Items->Flush();
431}
432
433//
434//
435//
436uint
438{
439 return Items->Size();
440}
441//
442/// Copies the selected string into Buffer. BufferSize includes the terminating 0
443//
444void
449//
450/// Selects "str", marking the matching String entry (if any) as selected
451//
453{
454 int numStrings = Items->Size();
456 for (int i = 0; i < numStrings; i++)
457 if (_tcscmp((*Items)[i].Text().c_str(), str) == 0)
458 {
459 SelIndex = i;
460 break;
461 }
462 if (Selection != str)
463 Selection = str;
464}
465
466
467} // OWL namespace
468/* ========================================================================== */
#define PRECONDITION(condition)
Definition checks.h:227
#define DIAG_DECLARE_GROUP(group)
Definition checks.h:404
int AddItem(const TComboBoxExItem &item)
Definition combobex.cpp:409
TComboBoxExItemArray * Items
Definition combobex.h:188
TComboBoxExItem & GetItem(int index)
Definition combobex.cpp:418
void SelectString(LPCTSTR str)
Selects "str", marking the matching String entry (if any) as selected.
Definition combobex.cpp:452
const tstring & GetSelString() const
Definition combobex.h:185
int DeleteItem(int index)
Definition combobex.cpp:400
ComboBoxEx controls are combo box controls that provide native support for item images.
Definition combobex.h:89
int InsertItem(const TComboBoxExItem &item)
Definition combobex.cpp:134
void Transfer(TComboBoxExData &data, TTransferDirection op)
Safe overload.
Definition combobex.h:118
~TComboBoxEx() override
Definition combobex.cpp:74
void SetupWindow() override
Definition combobex.cpp:92
void SetImageList(TImageList *list, TAutoDelete=AutoDelete)
Definition combobex.cpp:160
bool SetItem(const TComboBoxExItem &item, INT_PTR index=-1)
Definition combobex.cpp:146
TComboBoxEx(TWindow *parent, int id, int x, int y, int w, int h, uint32 style, uint textLimit, TModule *module=0)
Definition combobex.cpp:35
auto GetWindowClassName() -> TWindowClassName override
Return the proper class name.
Definition combobex.cpp:83
bool GetItem(TComboBoxExItem &item, INT_PTR index=-1)
Definition combobex.cpp:103
Encapsulates an item in an extended combo box (COMBOBOXEXITEM).
Definition combobex.h:38
TProperty< int > Selected
The item selected image.
Definition combobex.h:59
void Init()
Initialises all data members to zero.
Definition combobex.cpp:365
TObjProperty< tstring > Text
Band text label.
Definition combobex.h:57
TProperty< int > Mask
Band image index (into rebar image list): don't use -1.
Definition combobex.h:55
TProperty< int > Overlay
Band colors.
Definition combobex.h:60
TProperty< LPARAM > Param
Additional data.
Definition combobex.h:62
void ReFillItem(const COMBOBOXEXITEM &item)
Initializes this with native item.
Definition combobex.cpp:324
TComboBoxExItem & operator=(const TComboBoxExItem &item)
Assigns the property data, not the properties themselves.
Definition combobex.cpp:271
TProperty< int > Indent
Band colors.
Definition combobex.h:61
TProperty< INT_PTR > Item
== -1 to add at end
Definition combobex.h:56
void InitItem(COMBOBOXEXITEM &item, uint tsize=0) const
Initializes native item with this.
Definition combobex.cpp:288
TProperty< int > Image
The item image.
Definition combobex.h:58
You can use TComboBox to create a combo box or a combo box control in a parent TWindow,...
Definition combobox.h:47
tstring GetText() const
Definition combobox.h:64
virtual void ClearList()
Clear all the entries in list part of the associated combobox.
Definition combobox.h:410
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
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
void SetText(LPCTSTR str)
Selects the first string in the associated list box that begins with the supplied str.
Definition combobox.cpp:249
TImageList is a wrapper class for the ImageList common "control".
Definition imagelst.h:64
ObjectWindows dynamic-link libraries (DLLs) construct an instance of TModule, which acts as an object...
Definition module.h:75
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
TResult SendMessage(TMsgId, TParam1=0, TParam2=0) const
Sends a message (msg) to a specified window or windows.
Definition window.cpp:3288
HWND THandle
TWindow encapsulates an HWND.
Definition window.h:418
HWND GetHandle() const
Returns the handle of the window.
Definition window.h:2020
Definition of class TComboBoxEx.
#define _tcscmp
Definition cygwin.h:75
#define _tcsncpy
Definition cygwin.h:80
#define MAX_PATH
Definition cygwin.h:98
#define _T(x)
Definition cygwin.h:51
#define DEFINE_RESPONSE_TABLE1(cls, base)
Macro to define a response table for a class with one base.
Definition eventhan.h:492
TAutoDelete
Flag for Handle ctors to control Handle deletion in dtor.
Definition gdibase.h:70
@ AutoDelete
Definition gdibase.h:70
TObjectArray< TComboBoxExItem > TComboBoxExItemArray
Definition combobex.h:156
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
Definition of class TImageList, an ImageList Common Control wrapper.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
void InitializeCommonControls(uint controlFlags)
Wrapper for the Windows API function InitCommmonControlsEx.
Definition commctrl.cpp:19
EV_WM_DELETEITEM
Definition combobex.cpp:26
unsigned long uint32
Definition number.h:34
LPARAM TParam2
Second parameter type.
Definition dispatch.h:55
OWL_DIAGINFO
Definition animctrl.cpp:14
END_RESPONSE_TABLE
Definition button.cpp:26
EV_WM_COMPAREITEM
Definition combobex.cpp:25
std::string tstring
Definition defs.h:79
unsigned int uint
Definition number.h:25
EV_WM_DRAWITEM
Definition combobex.cpp:27
EV_WM_MEASUREITEM
Definition combobex.cpp:28
#define WS_EX_CLIENTEDGE
Definition wsysinc.h:33