1 ///////////////////////////////////////////////////////////
2 // Softwarebauelemente II, C2.1
3 //
4 // author: Stephan Brumme
5 // last changes: August 2, 2001
6
7
8 #include "stdafx.h"
9 #include "C2_1.h"
10 #include "C2_1Dlg.h"
11 #include "PolymorphicSet.h"
12
13 #ifdef _DEBUG
14 #define new DEBUG_NEW
15 #undef THIS_FILE
16 static char THIS_FILE[] = __FILE__;
17 #endif
18
19 /////////////////////////////////////////////////////////////////////////////
20 // CC2_1Dlg Dialogfeld
21
22 CC2_1Dlg::CC2_1Dlg(CWnd* pParent /*=NULL*/)
23 : CDialog(CC2_1Dlg::IDD, pParent)
24 {
25 //{{AFX_DATA_INIT(CC2_1Dlg)
26 m_Date = 0;
27 m_Time = 0;
28 //}}AFX_DATA_INIT
29 // Beachten Sie, dass LoadIcon unter Win32 keinen nachfolgenden DestroyIcon-Aufruf benötigt
30 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
31 }
32
33 void CC2_1Dlg::DoDataExchange(CDataExchange* pDX)
34 {
35 CDialog::DoDataExchange(pDX);
36 //{{AFX_DATA_MAP(CC2_1Dlg)
37 DDX_Control(pDX, IDC_LIST, m_List);
38 DDX_DateTimeCtrl(pDX, IDC_DATEPICKER, m_Date);
39 DDX_DateTimeCtrl(pDX, IDC_TIMEPICKER, m_Time);
40 //}}AFX_DATA_MAP
41 }
42
43 BEGIN_MESSAGE_MAP(CC2_1Dlg, CDialog)
44 //{{AFX_MSG_MAP(CC2_1Dlg)
45 ON_WM_PAINT()
46 ON_WM_QUERYDRAGICON()
47 ON_BN_CLICKED(IDC_BUTTON_DELETE, OnDelete)
48 ON_BN_CLICKED(IDC_BUTTON_LOAD, OnLoad)
49 ON_BN_CLICKED(IDC_BUTTON_SAVE, OnSave)
50 ON_LBN_DBLCLK(IDC_LIST, OnDblclkList)
51 //}}AFX_MSG_MAP
52 END_MESSAGE_MAP()
53
54 /////////////////////////////////////////////////////////////////////////////
55 // CC2_1Dlg Nachrichten-Handler
56
57 BOOL CC2_1Dlg::OnInitDialog()
58 {
59 CDialog::OnInitDialog();
60
61 // Symbol für dieses Dialogfeld festlegen. Wird automatisch erledigt
62 // wenn das Hauptfenster der Anwendung kein Dialogfeld ist
63 SetIcon(m_hIcon, TRUE); // Großes Symbol verwenden
64 SetIcon(m_hIcon, FALSE); // Kleines Symbol verwenden
65
66 // ZU ERLEDIGEN: Hier zusätzliche Initialisierung einfügen
67
68 // initialize random generator
69 srand((unsigned)time(NULL));
70
71 // set timer to refresh m_Time
72 SetTimer(1, 1000, NULL);
73
74 m_Date = m_Time = m_Moment;
75 UpdateData(FALSE);
76
77 return TRUE; // Geben Sie TRUE zurück, außer ein Steuerelement soll den Fokus erhalten
78 }
79
80 // Wollen Sie Ihrem Dialogfeld eine Schaltfläche "Minimieren" hinzufügen, benötigen Sie
81 // den nachstehenden Code, um das Symbol zu zeichnen. Für MFC-Anwendungen, die das
82 // Dokument/Ansicht-Modell verwenden, wird dies automatisch für Sie erledigt.
83
84 void CC2_1Dlg::OnPaint()
85 {
86 if (IsIconic())
87 {
88 CPaintDC dc(this); // Gerätekontext für Zeichnen
89
90 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
91
92 // Symbol in Client-Rechteck zentrieren
93 int cxIcon = GetSystemMetrics(SM_CXICON);
94 int cyIcon = GetSystemMetrics(SM_CYICON);
95 CRect rect;
96 GetClientRect(&rect);
97 int x = (rect.Width() - cxIcon + 1) / 2;
98 int y = (rect.Height() - cyIcon + 1) / 2;
99
100 // Symbol zeichnen
101 dc.DrawIcon(x, y, m_hIcon);
102 }
103 else
104 {
105 CDialog::OnPaint();
106 }
107 }
108
109 // Die Systemaufrufe fragen den Cursorform ab, die angezeigt werden soll, während der Benutzer
110 // das zum Symbol verkleinerte Fenster mit der Maus zieht.
111 HCURSOR CC2_1Dlg::OnQueryDragIcon()
112 {
113 return (HCURSOR) m_hIcon;
114 }
115
116
117 // a derived class, just used to show polymorphism
118 // no new attributes and/or operations are introduced
119 class CMomentDerived : public CMoment
120 {
121 DECLARE_SERIAL(CMomentDerived)
122
123 public:
124 CMomentDerived() {};
125 ~CMomentDerived() {};
126 };
127 IMPLEMENT_SERIAL(CMomentDerived, CMoment, 1)
128
129
130
131 // insert element
132 void CC2_1Dlg::OnOK()
133 {
134 CMomentDerived derived;
135 CMoment* moment;
136
137 // use derived class at random
138 if (rand()<RAND_MAX/3)
139 moment = &derived;
140 else
141 moment = &m_Moment;
142
143 // actually set time/date
144 UpdateData(TRUE);
145 moment->SetYear (m_Date.GetYear());
146 moment->SetMonth (m_Date.GetMonth());
147 moment->SetDay (m_Date.GetDay());
148 moment->SetHour (m_Time.GetHour());
149 moment->SetMinute(m_Time.GetMinute());
150 moment->SetSecond(m_Time.GetSecond());
151
152 // add to the set
153 if (m_Set.Insert(moment) != -1)
154 UpdateListBox();
155 else
156 AfxMessageBox("Zeitpunkt schon in der Menge enthalten !", MB_ICONSTOP);
157 }
158
159
160 // delete a entry from the listbox
161 void CC2_1Dlg::OnDelete()
162 {
163 // save current selection
164 int nSelected = m_List.GetCurSel();
165 if (nSelected < 0)
166 return;
167
168 // move cursor to the selection
169 delete m_Set.GetFirst();
170 while (nSelected-- > 0)
171 delete m_Set.GetNext();
172
173 // and delete
174 m_Set.Scratch();
175
176 // redraw the listbox
177 UpdateListBox();
178 }
179
180
181 // deserialize
182 void CC2_1Dlg::OnLoad()
183 {
184 CFileDialog dlg(TRUE);
185 dlg.DoModal();
186
187 CFile file(dlg.GetPathName(), CFile::modeRead);
188 CArchive ar (&file, CArchive::load);
189
190 m_Set.Serialize(ar);
191
192 // redraw the listbox
193 UpdateListBox();
194 }
195
196
197 // serialize
198 void CC2_1Dlg::OnSave()
199 {
200 CFileDialog dlg(FALSE);
201 dlg.DoModal();
202
203 CFile file(dlg.GetPathName(), CFile::modeCreate|CFile::modeWrite);
204 CArchive ar (&file, CArchive::store);
205
206 m_Set.Serialize(ar);
207 }
208
209
210 // redraw listbox
211 void CC2_1Dlg::UpdateListBox()
212 {
213 // save index of selected item
214 int nSelected = m_List.GetCurSel();
215
216 // delete all elements
217 m_List.ResetContent();
218
219 int nElements = m_Set.Card();
220
221 // empty list ?
222 if (nElements == 0)
223 return;
224
225 CMoment* obj = m_Set.GetFirst();
226 while (nElements > 0)
227 {
228 // display some info
229 CString info;
230 info.Format("%02d.%02d.%04d - %02d:%02d:%02d",
231 obj->GetDay(), obj->GetMonth(), obj->GetYear(),
232 obj->GetHour(), obj->GetMinute(), obj->GetSecond());
233
234 // look for derived classes (they prove polymorphism !)
235 if (obj->IsKindOf(RUNTIME_CLASS(CMomentDerived)))
236 info += " (abgeleitet)";
237
238 delete obj;
239 m_List.AddString(info);
240
241 nElements--;
242 // if the end is not reached then get next element
243 if (nElements > 0)
244 obj = m_Set.GetNext();
245 }
246
247 // set selection
248 if (nSelected == -1 || nSelected > m_List.GetCount())
249 nSelected = m_List.GetCount()-1;
250 m_List.SetCurSel(nSelected);
251 }
252
253
254 // change both pickers
255 void CC2_1Dlg::OnDblclkList()
256 {
257 // save current selection
258 int nSelected = m_List.GetCurSel();
259 if (nSelected < 0)
260 return;
261
262 // move cursor to the selection, delete all generated elements
263 delete m_Set.GetFirst();
264 while (nSelected-- > 0)
265 delete m_Set.GetNext();
266
267 // even CMomentDerived can be handled this way
268 CMoment* moment = m_Set.GetCurrent();
269 m_Date = *moment;
270 m_Time = *moment;
271 delete moment;
272
273 // update both pickers
274 UpdateData(FALSE);
275
276 #ifdef _DEBUG
277 afxDump << m_Set;
278 #endif
279 }
280