OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
picklist.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1995, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of classes TPickListPopup & TPickListDialog.
7///
8/// Both of these modal windows allow the selction of a string from a list.
9/// TPickListPopup uses a lean popup menu for a quick selection, while
10/// TPickListDialog uses a listbox in a dialog with OK and Cancel buttons.
11//----------------------------------------------------------------------------
12#include <owl/pch.h>
13
14#include <owl/picklist.h>
15#include <owl/picklist.rh>
16
17using namespace std;
18
19namespace owl {
20
22
23//
24/// Constructs a popup menu.
25/// Adds the title at the top of the menu.
26//
28:
29 TWindow(parent, nullptr, nullptr),
30 Count(0)
31{
33
34 if (title) {
35 Popup.AppendMenu(MF_GRAYED, 0, title);
36 Popup.AppendMenu(MF_SEPARATOR);
37 }
38}
39
40//
41/// String-aware overload
42//
44 : TWindow(parent, nullptr, nullptr), Count(0)
45{
47 if (!title.empty())
48 {
49 Popup.AppendMenu(MF_GRAYED, 0, title);
50 Popup.AppendMenu(MF_SEPARATOR);
51 }
52}
53
54//
55/// Constructs a popu menu.
56/// Adds the title from resource at the top of the menu.
57//
59:
60 TWindow(parent, nullptr, nullptr),
61 Count(0)
62{
64
65 tchar buf[_MAX_PATH];
66 if (!titleId || !GetApplication()->LoadString(titleId, buf, _MAX_PATH))
67 return;
68
69 Popup.AppendMenu(MF_GRAYED, 0, buf);
70 Popup.AppendMenu(MF_SEPARATOR);
71}
72
73//
74/// Clears all strings from the pop-up.
75//
76void
78{
79 while (Count)
80 Popup.RemoveMenu(--Count, MF_BYCOMMAND);
81}
82
83//
84/// Adds a string to the pop-up.
85//
86int
88{
90 Popup.AppendMenu(MF_ENABLED, Count++, str);
91 return Count;
92}
93
94//
95/// Displays the pop-up menu and returns the command ID of the menu item the user
96/// selected.
97//
98int
100{
101 // Create the hidden window
102 //
103 Create();
104
105 // Reset the index result
106 //
107 Result = -2;
108
109 // Grab current mouse location
110 //
111 TPoint cursorLoc;
112 GetCursorPos(cursorLoc);
113
114 // Display section & cleanup
115 //
116 Popup.TrackPopupMenu(0, cursorLoc, 0, GetHandle());
117 if(!IsWindow())
118 return -2;
119// while (Popup.GetMenuItemCount()) // !CQ Menus clean up their own items
120// Popup.DeleteMenu(0, MF_BYPOSITION);
121
122 // Dispatch any pending WM_COMMAND stuck in the message queue.
123 //
126
127 return Result;
128}
129
130//
131/// Generic handler for WM_COMMAND messages.
132/// Result is set to the id of the menu item clicked.
133//
135TPickListPopup::EvCommand(uint id, THandle /*hWndCtl*/, uint /*notifyCode*/)
136{
137// WARNX(OwlDocView, id > Count, 0, "TPickListPopup index invalid");
138 Result = id;
139 return 0;
140}
141
142//----------------------------------------------------------------------------
143
144//
145//
146//
148 EV_COMMAND(IDOK, CmOK),
149 EV_COMMAND(IDCANCEL, CmCancel),
150 EV_LBN_DBLCLK(IDC_LIST, CmOK),
152
153//
154/// Initialize the dialog.
155/// Sets the initial selection of the listbox.
156/// Allocate strings if necessary.
157//
159 TStringArray* strings,
160 int initialSelection,
161 TResId templateId,
162 LPCTSTR title,
163 TModule* module)
164:
165 TDialog(parent, templateId ? templateId : TResId(IDD_PICKLISTDIALOG), module),
166 List(this, IDC_LIST, module),
167 Result(initialSelection),
168 Strings(strings ? strings : new TStringArray),
169 NewedStrings(!strings)
170{
171 if (title)
172 SetCaption(title);
173}
174
175//
176/// String-aware overload
177//
179 TWindow* parent,
180 TStringArray* strings,
181 int initialSelection,
182 TResId templateId,
183 const tstring& title,
184 TModule* module
185 )
186 : TDialog(parent, templateId ? templateId : TResId(IDD_PICKLISTDIALOG), module),
187 List(this, IDC_LIST, module),
188 Result(initialSelection),
189 Strings(strings ? strings : new TStringArray),
190 NewedStrings(!strings)
191{
192 if (!title.empty())
193 SetCaption(title);
194}
195
196//
197/// Deletes any allocated string.
198//
200{
201 if (NewedStrings)
202 delete Strings;
203}
204
205//
206/// Clears all strings from the list.
207//
208void
210{
211 Strings->Flush();
212 if (List.GetHandle())
213 List.ClearList();
214}
215
216//
217/// Adds a string to the Strings list and to the List box if it has already been
218/// created.
219//
220int
222{
223 Strings->Add(str);
224 if (List.GetHandle())
225 List.AddString(str);
226 return Strings->Size()-1;
227}
228
229//
230// Add a string to a listbox, passed via the args from ForEach.
231
232static void
233AddToList(tstring& str, void* param)
234{
235 TListBox* list = REINTERPRET_CAST(TListBox*,param);
236 if (list && list->GetHandle()) // just in case
237 list->AddString(str);
238}
239//
240/// Override from TDialog.
241/// Adds each string into the listbox.
242//
243void
245{
247
248 // Add each string in Strings to the List box
249 //
250 Strings->ForEach(owl::AddToList, &List);
251
252 // Set inital state of listbox
253 //
254 if (List.GetCount() > Result) {
255 List.SetSelIndex(Result);
256 List.SetCaretIndex(Result, false);
257 }
258}
259
260//
261/// User-selected OK. Gets selection from the listbox and returns it.
262//
263void
265{
266 int index = List.GetSelIndex();
267 if (index >= 0) {
268 // Save the selection index.
269 Result = index;
270 }
271 CloseWindow(Result);
272}
273
274//
275/// User-selected Cancel. Returns a value less than zero. Negative One (-1) cannot
276/// be used since it signals a dialog failure.
277//
278void
283
284
285} // OWL namespace
286/* ========================================================================== */
287
#define CHECK(condition)
Definition checks.h:239
#define PRECONDITION(condition)
Definition checks.h:227
Typically used to obtain information from a user, a dialog box is a window inside of which other cont...
Definition dialog.h:85
void CloseWindow(int retValue=IDCANCEL) override
Overrides the virtual function defined by TWindow and conditionally shuts down the dialog box.
Definition dialog.cpp:853
void SetupWindow() override
Overrides the virtual function defined in TWindow.
Definition dialog.cpp:825
TDialog(TWindow *parent, TResId resId, TModule *module=nullptr)
Constructor for a TDialog object.
Definition dialog.cpp:103
An interface object that represents a corresponding list box element.
Definition listbox.h:43
bool RemoveMenu(uint item, uint flags)
Removes the menu item from the menu but does not delete it if it is a submenu.
Definition menu.h:500
bool AppendMenu(uint flags, TMenuItem newItem=static_cast< unsigned int >(-1), LPCTSTR newStr=nullptr)
Adds a text menu item to the end of the menu.
Definition menu.h:342
virtual HMENU GetHandle() const
Returns the handle to the menu.
Definition menu.h:310
ObjectWindows dynamic-link libraries (DLLs) construct an instance of TModule, which acts as an object...
Definition module.h:78
bool PumpWaitingMessages()
The inner message loop.
Definition msgthred.cpp:123
The PickListDialog allows selection of an item from a list in a dialog with OK and Cancel buttons.
Definition picklist.h:75
int AddString(LPCTSTR str)
Adds a string to the Strings list and to the List box if it has already been created.
Definition picklist.cpp:221
void CmCancel()
User-selected Cancel.
Definition picklist.cpp:279
void SetupWindow() override
Override from TDialog.
Definition picklist.cpp:244
~TPickListDialog() override
Deletes any allocated string.
Definition picklist.cpp:199
void ClearStrings()
Clears all strings from the list.
Definition picklist.cpp:209
void CmOK()
User-selected OK. Gets selection from the listbox and returns it.
Definition picklist.cpp:264
TPickListDialog(TWindow *parent, TStringArray *strings=0, int initialSelection=0, TResId templateId=0, LPCTSTR title=0, TModule *module=0)
Initialize the dialog.
Definition picklist.cpp:158
int AddString(LPCTSTR str)
Adds a string to the pop-up.
Definition picklist.cpp:87
void ClearStrings()
Clears all strings from the pop-up.
Definition picklist.cpp:77
TPopupMenu Popup
Definition picklist.h:52
int Execute()
Displays the pop-up menu and returns the command ID of the menu item the user selected.
Definition picklist.cpp:99
TResult EvCommand(uint id, THandle hWndCtl, uint notifyCode)
Generic handler for WM_COMMAND messages.
Definition picklist.cpp:135
TPickListPopup(TWindow *parent, LPCTSTR title=0)
Constructs a popup menu.
Definition picklist.cpp:27
TPoint is a support class, derived from tagPOINT.
Definition geometry.h:87
int TrackPopupMenu(uint flags, int x, int y, int rsvd, HWND wnd, const TRect *rect=nullptr)
Allows the application to create a pop-up menu at the specified location in the specified window.
Definition menu.h:561
TWindow, derived from TEventHandler and TStreamableBase, provides window-specific behavior and encaps...
Definition window.h:414
static void GetCursorPos(TPoint &pos)
Retrieves the cursor's current position (in window screen coordinates) and copies the values into the...
Definition window.h:3465
tstring LoadString(uint id) const
Definition window.h:608
TApplication * GetApplication() const
Gets a pointer to the TApplication object associated with this.
Definition window.h:1855
TWindow(TWindow *parent, LPCTSTR title=nullptr, TModule *module=nullptr)
Adds this to the child list of parent if nonzero, and calls EnableAutoCreate so that this will be cre...
Definition window.cpp:341
virtual bool Create()
Creates the window interface element to be associated with this ObjectWindows interface element.
Definition window.cpp:2399
bool IsWindow() const
Returns true if an HWND is being used.
Definition window.h:2040
void SetCaption(LPCTSTR title)
Copies title to an allocated string pointed to by title.
Definition window.cpp:3410
HWND THandle
TWindow encapsulates an HWND.
Definition window.h:418
HWND GetHandle() const
Returns the handle of the window.
Definition window.h:2020
#define _MAX_PATH
Definition cygwin.h:97
#define DEFINE_RESPONSE_TABLE1(cls, base)
Macro to define a response table for a class with one base.
Definition eventhan.h:492
Object Windows Library (OWLNext Core).
Definition animctrl.h:22
TObjectArray< tstring > TStringArray
Definition contain.h:37
EV_LBN_DBLCLK(IDC_LIST, CmOK)
char tchar
Definition defs.h:77
OWL_DIAGINFO
Definition animctrl.cpp:14
END_RESPONSE_TABLE
Definition button.cpp:26
LRESULT TResult
Result type.
Definition dispatch.h:52
std::string tstring
Definition defs.h:79
unsigned int uint
Definition number.h:25
EV_COMMAND(IDOK, CmOkCancel)
#define REINTERPRET_CAST(targetType, object)
Definition defs.h:275
Definition of classes TPickListPopup & TPickListDialog.