OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
listboxview.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1993, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of class TListBoxView
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9
10#include <owl/listboxview.h>
11#include <owl/inputdia.h>
12#include <owl/listboxview.rh>
13#include <owl/docview.rh>
14#include <owl/edit.rh>
15#include <stdio.h>
16
17using namespace std;
18
19namespace owl {
20
22DIAG_DECLARE_GROUP(OwlDocView); // General Doc/View diagnostic group
23
24// Let the compiler know that the following template instances will be defined elsewhere.
25//#pragma option -Jgx
26
27
28const tchar VirtualLastLineStr[] = _T("---"); // Last virtual line appended to list
29
31 EV_COMMAND(CM_EDITUNDO, CmEditUndo),
32 EV_COMMAND(CM_EDITCUT, CmEditCut),
33 EV_COMMAND(CM_EDITCOPY, CmEditCopy),
34 EV_COMMAND(CM_EDITPASTE, CmEditPaste),
35 EV_COMMAND(CM_EDITCLEAR, CmEditClear),
36 EV_COMMAND(CM_EDITDELETE, CmEditDelete),
38 EV_COMMAND(CM_EDITEDIT, CmEditItem),
48
49//
50/// Creates a TListBoxView object associated with the specified document and parent
51/// window. Sets Attr.AccelTable to IDA_LISTVIEW to identify the edit view. Sets the
52/// view style to WS_HSCROLL | LBS_NOINTEGRALHEIGHT.
53/// Sets TView::ViewMenu to the new TMenuDescr for this view.
54//
56:
57 TListBox(parent, GetNextViewId(), 0,0,0,0),
58 TView(doc),
59 DirtyFlag(false),
60 Origin(0),
61 MaxWidth(0)
62{
63 Attr.Style &= ~(LBS_SORT);
64 Attr.Style |= (WS_HSCROLL | LBS_NOINTEGRALHEIGHT);
65 Attr.AccelTable = IDA_LISTVIEW;
66 if (::FindResource(*GetModule(), TResId(IDM_LISTVIEW), RT_MENU))
67 SetViewMenu(new TMenuDescr(IDM_LISTVIEW, 0,1,0,0,0,1, GetModule()));
68}
69
70//
71/// Adds a string into the view. Returns the index at which the string is added.
72//
73int
75{
76 long style = GetWindowLong(GWL_STYLE);
77 if (!(style & LBS_SORT)) {
79 if (itemsInListBox > 0) {
80 // before the end of list marker
81 return InsertString(str, itemsInListBox-1);
82 }
83 }
84 return TListBox::AddString(str);
85}
86
87//
88/// Sets the maximum horizontal extent for the list view window.
89//
90void
92{
93 if (str.length() == 0)
94 return;
95
96 TClientDC clientDC(*this);
97 TSize extent = clientDC.GetTextExtent(str, static_cast<int>(str.length()));
98 extent.cx += 2; // room for focus rectangle
99
100 if (extent.cx > MaxWidth)
101 SetHorizontalExtent(MaxWidth = extent.cx);
102}
103
104//
105/// Indicates that the document has been closed.
106//
107bool
109{
110 if (DirtyFlag == 2 || !(omode & ofWrite)) // make sure someone else's write
111 return false;
112 int top = GetTopIndex();
113 int sel = GetSelIndex();
114 LoadData(top, sel);
115 return true;
116}
117
118//
119/// Reads the view from the stream and closes the file. Returns true if the view was
120/// successfully loaded.
121/// Throws an xmsg exception and displays the error message TListBoxView initial read
122/// error if the file can't be read. Returns false if the view can't be loaded.
123//
124bool
126{
127 CmEditClear(); // Clear list & remove virtual last line temporarily
128 DeleteString(0);
129
130 long style = GetWindowLong(GWL_STYLE);
131 if (!(style & LBS_SORT))
132 TListBox::AddString(VirtualLastLineStr); // Append virtual last line
133
134 DirtyFlag = false;
135
137 if ((inStream = Doc->InStream(ios::in)) == nullptr) {
138 Doc->PostError(IDS_UNABLEOPEN, MB_OK);
139 return false;
140 }
141 bool status;
142 for (;;) {
143 tchar buf[100+1];
144 inStream->getline(buf, COUNTOF(buf)-1);
145 if (!inStream->gcount() && !inStream->good()) {
146 status = ToBool(inStream->eof());
147 break;
148 }
149 AddString(buf);
150 SetExtent(buf);
151 }
152 SetTopIndex(top);
154 delete inStream; // close file in case process switch
155 if (!status)
156 Doc->PostError(IDS_READERROR, MB_OK);
157 return status;
158}
159
160//
161/// Overrides TWindow::Create and creates the view's window. Determines if the file
162/// is new or already has data. If there is data, calls LoadData to add the data to
163/// the view. If the view's window can't be created, Create throws a TXWindow
164/// exception.
165//
166bool
168{
169 try {
170 TListBox::Create(); // throws exception TWindow::TXWindow
171 }
172 catch (TXOwl& ) {
173 Doc->PostError(IDS_NOMEMORYFORVIEW, MB_OK);
174 return true; // cannot return false - throws another exception
175 }
176 if (Doc->GetDocPath() == nullptr) {
177 CmEditClear(); // perform any clearing initialization
178 return true; // new file, no data to display
179 }
180 if (!LoadData(0, 0))
181 NotOK();
182 return true;
183}
184
185//
186/// Commits changes made in the view to the document. If force is nonzero, all data,
187/// even if it's unchanged, is saved to the document.
188//
189bool
191{
192 if (!force && !DirtyFlag)
193 return true;
194
195 tostream* outStream = Doc->OutStream(ios::out);
196 if (outStream == nullptr) {
197 Doc->PostError(IDS_UNABLEOPEN, MB_OK);
198 return false;
199 }
200 outStream->seekp(Origin);
201 int count = GetCount();
202 for (int index = 0; index < count-1; index++) { // don't write last virtual line
203 int len = GetStringLen(index);
204 TAPointer<tchar> buf(new tchar[len+1]);
205 GetString(static_cast<tchar*>(buf), index);
207 //delete buf;
208 }
209 DirtyFlag = 2; // to detect our own close notification
210
211 bool status = ToBool(outStream->good());
212 delete outStream;
213 DirtyFlag = false;
214 if (!status)
215 Doc->PostError(IDS_WRITEERROR, MB_OK);
216
217 return status;
218}
219
220//
221/// Indicates if changes made to the view should be erased, and if the data from the
222/// document should be restored to the view. If clear is a nonzero value, the data
223/// is cleared instead of restored to the view.
224//
225bool
227{
228 if (!clear && Doc->GetDocPath() != nullptr)
229 return LoadData(0,0);
230 CmEditClear();
231 DirtyFlag = false;
232 return true;
233}
234
235//
236/// Overrides TWindow's response to a WM_GETDLGCODE message (an input procedure
237/// associated with a control that isn't a check box) by calling
238/// TWindow::DefaultProcessing.
239/// EvGetDlgCode returns a code that indicates how the list box control message is
240/// to be treated.
241//
242uint
244{
245 uint retVal = static_cast<uint>(DefaultProcessing());
247 return retVal;
248}
249
250//
251/// Handler to undo the last operation performed on the underlying ListBox.
252/// \note This feature is not implemented in the current version of ObjectWindows.
253//
254void
256{
257#if defined(BI_MSG_LANGUAGE) && BI_MSG_LANGUAGE == 0x0411
258 MessageBox("この機能は実装されていません", "元に戻す", MB_OK);
259#else
260 MessageBox(_T("Feature not implemented"), _T("Undo"), MB_OK);
261#endif
262}
263
264//
265/// Automatically responds to a menu selection with a menu ID of CM_EDITCUT by
266/// calling CmEditCopy and CmEditDelete to delete a text string from the list view.
267/// Sets the data member DirtyFlag to true.
268//
269void
275
276//
277/// Automatically responds to a menu selection with a menu ID of CM_EDITCOPY and
278/// copies the selected text to the Clipboard.
279//
280void
282{
283 int index = GetSelIndex();
284 int count = GetCount();
285 if (count <= 1 || index >= count)
286 return;
287
288 TClipboard cb(*this, false);
289 if (cb.EmptyClipboard()) {
290 int len = GetStringLen(index);
293 GetString(buf, index);
295#if defined(UNICODE)
296 cb.SetClipboardData(CF_UNICODETEXT, cbhdl);
297#else
298 cb.SetClipboardData(CF_TEXT, cbhdl);
299#endif
300 }
301}
302
303//
304/// Automatically responds to a menu selection with a menu ID of CM_EDITPASTE by
305/// inserting text into the list box using functions in TListBox.
306//
307void
309{
310 int index = GetSelIndex();
311 if (index < 0)
312 index = 0;
313
314 TClipboard cb(*this, false);
315 if (!cb)
316 return; // clipboard open by another program
317
318#if defined(UNICODE)
319 HANDLE cbhdl = cb.GetClipboardData(CF_UNICODETEXT);
320#else
321 HANDLE cbhdl = cb.GetClipboardData(CF_TEXT);
322#endif
323 if (cbhdl) {
325 InsertString(text, index);
326 SetSelIndex(index+1);
327 DirtyFlag = true;
329 }
330}
331
332//
333/// Automatically responds to a menu selection with a menu ID of CM_EDITDELETE by
334/// deleting the currently selected text.
335//
336void
338{
339 int count = GetCount();
340 int index = GetSelIndex();
341 if (count <= 1 || index >= count-1)
342 return;
343
344 DeleteString(index);
345 SetSelIndex(index);
346 DirtyFlag = true;
347}
348
349//
350/// Automatically responds to a menu selection with a menu ID of CM_EDITCLEAR by
351/// clearing the items in the list box using functions in TListBox.
352//
353void
355{
356 int count = GetCount();
357 if (count == 1)
358 return;
359 if (count) {
360 ClearList();
361 DirtyFlag = true;
362 SetHorizontalExtent(MaxWidth = 0);
363 }
364 long style = GetWindowLong(GWL_STYLE);
365 if (!(style & LBS_SORT))
367}
368
369static int linePrompt(TWindow* parent, int index, UINT id,
370 LPTSTR buf, int buflen)
371{
372 tchar msg[41];
373 _stprintf(msg, parent->LoadString(IDS_LISTNUM).c_str(), index);
374 return TInputDialog(parent, msg, parent->LoadString(id).c_str(), buf, buflen).Execute();
375}
376
377//
378/// Automatically responds to CM_LISTADD message by getting the length of the input
379/// string and inserting the text string into the list view. Sets the data member
380/// DirtyFlag to true.
381//
382void
384{
385 tchar inputText[101];
386 *inputText = 0;
387
388 int index = GetSelIndex();
389 if (index < 0)
390 index = 0;
391
392 if (linePrompt(this,index+1,CM_EDITADD,inputText,COUNTOF(inputText)) == IDOK) {
393 InsertString(inputText, index);
394 SetSelIndex(index+1);
396 DirtyFlag = true;
397 }
398}
399
400//
401/// Automatically responds to a CM_LISTEDIT message by getting the input text and
402/// inserting the text into the list view. Sets DirtyFlag to a nonzero value to
403/// indicate that the view has been changed and not saved.
404//
405void
407{
408 int index = GetSelIndex();
409 int count = GetCount();
410
411 if (index == count-1) {
412 CmEditAdd();
413 return;
414 }
415
416 if (index < 0 || index >= count-1)
417 return;
418
419 tchar inputText[101];
421
422 if (linePrompt(this,index+1,CM_EDITEDIT,inputText,COUNTOF(inputText))==IDOK) {
423 DeleteString(index);
424 InsertString(inputText, index);
426 SetSelIndex(index);
427 DirtyFlag = true;
428 }
429}
430
431//
432// To prevent interpreting as unprocessed accelerator
433//
434/// Automatically responds to a LBN_SELCHANGE message (which indicates that the
435/// contents of the list view have changed) by calling TWindow::DefaultProcessing.
436//
437void
441
442
444
445#if OWL_PERSISTENT_STREAMS
446
447//
448//
449//
450void*
451TListBoxView::Streamer::Read(ipstream& is, uint32 /*version*/) const
452{
453 ReadBaseObject((TListBox*)GetObject(), is);
454 ReadBaseObject((TView*)GetObject(), is);
455 is >> GetObject()->Origin;
456 return GetObject();
457}
458
459//
460//
461//
462void
463TListBoxView::Streamer::Write(opstream &os) const
464{
465 WriteBaseObject((TListBox*)GetObject(), os);
466 WriteBaseObject((TView*)GetObject(), os);
467 os << GetObject()->Origin;
468}
469
470#endif
471
472} // OWL namespace
473/* ========================================================================== */
474
#define DIAG_DECLARE_GROUP(group)
Definition checks.h:404
A device context class derived from TWindowDC, TClientDC provides access to the client area owned by ...
Definition dc.h:640
The clipboard class encapsulates the methods for the clipboard object of Windows.
Definition clipboar.h:32
auto Execute() -> int override
Creates and executes a modal dialog box interface element associated with the TDialog object.
Definition dialog.cpp:595
An abstract base class, TDocument is the base class for all document objects and serves as an interfa...
Definition docview.h:187
Provides a generic dialog box to retrieve text input by a user.
Definition inputdia.h:35
An interface object that represents a corresponding list box element.
Definition listbox.h:43
tstring GetSelString() const
String-aware overload.
Definition listbox.cpp:700
virtual int SetSelIndex(int index)
For single-selection list boxes.
Definition listbox.cpp:589
virtual void SetHorizontalExtent(int horzExtent)
Sets the number of pixels by which the list box can be scrolled horizontally.
Definition listbox.h:241
virtual int AddString(LPCTSTR str)
Adds str to the list box, returning its position in the list (0 is the first position).
Definition listbox.h:344
virtual int GetStringLen(int index) const
Returns the string length (excluding the terminating NULL) of the item at the position index supplied...
Definition listbox.h:464
virtual int SetTopIndex(int index)
The system scrolls the list box contents so that either the specified item appears at the top of the ...
Definition listbox.h:219
virtual int InsertString(LPCTSTR str, int index)
Inserts str in the list box at the position supplied in index, and returns the item's actual position...
Definition listbox.h:355
virtual int GetCount() const
Returns the number of items in the list box, or a negative value if an error occurs.
Definition listbox.h:383
virtual int DeleteString(int index)
Deletes the item in the list at the position (starting at 0) supplied in index.
Definition listbox.h:365
virtual void ClearList()
Clears all the entries in the associated listbox.
Definition listbox.h:373
virtual int GetTopIndex() const
Returns the index of the first item displayed at the top of the list box.
Definition listbox.h:211
virtual int GetString(TCHAR *str, int index) const
Retrieves the contents of the string at the passed index of the associated listbox.
Definition listbox.h:452
virtual int GetSelIndex() const
For single-selection list boxes.
Definition listbox.cpp:538
Provides views for list boxes.
Definition listboxview.h:31
bool LoadData(int top, int sel)
Reads the view from the stream and closes the file.
bool VnDocClosed(int omode)
Indicates that the document has been closed.
void CmEditClear()
Automatically responds to a menu selection with a menu ID of CM_EDITCLEAR by clearing the items in th...
void CmEditCopy()
Automatically responds to a menu selection with a menu ID of CM_EDITCOPY and copies the selected text...
void SetExtent(const tstring &str)
Sets the maximum horizontal extent for the list view window.
void CmEditAdd()
Automatically responds to CM_LISTADD message by getting the length of the input string and inserting ...
void CmEditItem()
Automatically responds to a CM_LISTEDIT message by getting the input text and inserting the text into...
auto AddString(LPCTSTR) -> int override
< String-aware overload
void CmEditCut()
Automatically responds to a menu selection with a menu ID of CM_EDITCUT by calling CmEditCopy and CmE...
bool VnRevert(bool clear)
Indicates if changes made to the view should be erased, and if the data from the document should be r...
void CmSelChange()
Automatically responds to a LBN_SELCHANGE message (which indicates that the contents of the list view...
void CmEditPaste()
Automatically responds to a menu selection with a menu ID of CM_EDITPASTE by inserting text into the ...
TListBoxView(TDocument &doc, TWindow *parent=0)
Creates a TListBoxView object associated with the specified document and parent window.
void CmEditDelete()
Automatically responds to a menu selection with a menu ID of CM_EDITDELETE by deleting the currently ...
void CmEditUndo()
Handler to undo the last operation performed on the underlying ListBox.
bool VnCommit(bool force)
Commits changes made in the view to the document.
uint EvGetDlgCode(const MSG *)
Overrides TWindow's response to a WM_GETDLGCODE message (an input procedure associated with a control...
auto Create() -> bool override
Overrides TWindow::Create and creates the view's window.
Menu with information used to allow merging.
Definition menu.h:206
The tagSIZE struct is defined as.
Definition geometry.h:234
Abstract base class for view access from document.
Definition docview.h:397
void SetViewMenu(TMenuDescr *menu)
Definition view.cpp:95
void NotOK()
To flag errors in creation.
Definition docview.h:1093
TWindow, derived from TEventHandler and TStreamableBase, provides window-specific behavior and encaps...
Definition window.h:414
tstring LoadString(uint id) const
Definition window.h:608
virtual bool Create()
Creates the window interface element to be associated with this ObjectWindows interface element.
Definition window.cpp:2399
long GetWindowLong(int index) const
Retrieves information about the window depending on the value stored in index.
Definition window.h:2388
TModule * GetModule() const
Returns a pointer to the module object.
Definition window.h:1841
TResult DefaultProcessing()
Handles default processing of events, which includes continued processing of menu/accelerators comman...
Definition window.cpp:852
int MessageBox(LPCTSTR text, LPCTSTR caption=0, uint flags=MB_OK) const
Creates and displays a message box that contains a message (text), a title (caption),...
Definition window.cpp:4284
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
#define _stprintf
Definition cygwin.h:88
#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
void ReadBaseObject(Base *base, ipstream &in)
Definition objstrm.h:1159
#define IMPLEMENT_STREAMABLE2(cls, base1, base2)
Definition objstrm.h:1726
void WriteBaseObject(Base *base, opstream &out)
Definition objstrm.h:1150
@ ofWrite
ios::out, open for writing
Definition docview.h:64
Definition of TInputDialog class.
Definition of class TListBoxView.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
EV_VN_REVERT
Definition editview.cpp:30
EV_VN_DOCCLOSED
Definition editview.cpp:26
EV_VN_ISWINDOW
Definition commview.cpp:26
const tchar VirtualLastLineStr[]
bool ToBool(const T &t)
Definition defs.h:291
EV_VN_COMMIT
Definition editview.cpp:29
OWL_DIAGINFO
Definition animctrl.cpp:14
END_RESPONSE_TABLE
Definition button.cpp:26
std::istream tistream
Definition strmdefs.h:39
std::string tstring
Definition defs.h:79
EV_WM_GETDLGCODE
Definition button.cpp:24
unsigned int uint
Definition number.h:25
std::ostream tostream
Definition strmdefs.h:40
EV_VN_ISDIRTY
Definition editview.cpp:28
#define COUNTOF(s)
Array element count Important: Only use this with an argument of array type.
Definition defs.h:376
#define EV_NOTIFY_AT_CHILD(notifyCode, method)
Response table entry for a child ID notification handled at the child.
Definition windowev.h:162
#define EV_COMMAND(id, method)
Response table entry for a menu/accelerator/push button message.
Definition windowev.h:171