summaryrefslogtreecommitdiffstats
path: root/Source/MFCDialog/PropertyList.cpp
blob: 92f701e5d7ea26cca62e4b66c886333fb6e578a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
// PropertyList.cpp : implementation file
//

#include "stdafx.h"
#include "PropertyList.h"

#define IDC_PROPCMBBOX   712
#define IDC_PROPEDITBOX  713
#define IDC_PROPBTNCTRL  714
#define IDC_PROPCHECKBOXCTRL 715

/////////////////////////////////////////////////////////////////////////////
// CPropertyList

CPropertyList::CPropertyList()
{
  m_Dirty = false;
}

CPropertyList::~CPropertyList()
{
  for(std::set<CPropertyItem*>::iterator i = m_PropertyItems.begin();
      i != m_PropertyItems.end(); ++i)
    {
    delete *i;
    }
}


BEGIN_MESSAGE_MAP(CPropertyList, CListBox)
  //{{AFX_MSG_MAP(CPropertyList)
  ON_WM_CREATE()
  ON_CONTROL_REFLECT(LBN_SELCHANGE, OnSelchange)
  ON_WM_LBUTTONUP()
  ON_WM_KILLFOCUS()
  ON_WM_LBUTTONDOWN()
  ON_WM_RBUTTONUP()
  ON_WM_MOUSEMOVE()
  //}}AFX_MSG_MAP
  ON_CBN_KILLFOCUS(IDC_PROPCMBBOX, OnKillfocusCmbBox)
  ON_CBN_SELCHANGE(IDC_PROPCMBBOX, OnSelchangeCmbBox)
  ON_EN_KILLFOCUS(IDC_PROPEDITBOX, OnKillfocusEditBox)
  ON_EN_CHANGE(IDC_PROPEDITBOX, OnChangeEditBox)
  ON_BN_CLICKED(IDC_PROPBTNCTRL, OnButton)
  ON_BN_CLICKED(IDC_PROPCHECKBOXCTRL, OnCheckBox)
  ON_COMMAND(42, OnDelete)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPropertyList message handlers

BOOL CPropertyList::PreCreateWindow(CREATESTRUCT& cs) 
{
  if (!CListBox::PreCreateWindow(cs))
    return FALSE;

  cs.style &= ~(LBS_OWNERDRAWVARIABLE | LBS_SORT);
  cs.style |= LBS_OWNERDRAWFIXED;

  m_bTracking = FALSE;
  m_nDivider = 0;
  m_bDivIsSet = FALSE;

  return TRUE;
}

void CPropertyList::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
  lpMeasureItemStruct->itemHeight = 20; //pixels
}


void CPropertyList::DrawItem(LPDRAWITEMSTRUCT lpDIS) 
{
  CDC dc;
  dc.Attach(lpDIS->hDC);
  CRect rectFull = lpDIS->rcItem;
  CRect rect = rectFull;
  if (m_nDivider==0)
    m_nDivider = rect.Width() / 2;
  rect.left = m_nDivider;
  CRect rect2 = rectFull;
  rect2.right = rect.left - 1;
  UINT nIndex = lpDIS->itemID;

  if (nIndex != (UINT) -1)
    {
    //draw two rectangles, one for each row column
    dc.FillSolidRect(rect2,RGB(192,192,192));
    dc.DrawEdge(rect2,EDGE_SUNKEN,BF_BOTTOMRIGHT);
    dc.DrawEdge(rect,EDGE_SUNKEN,BF_BOTTOM);

    //get the CPropertyItem for the current row
    CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(nIndex);

    //write the property name in the first rectangle
    dc.SetBkMode(TRANSPARENT);
    dc.DrawText(pItem->m_propName,CRect(rect2.left+3,rect2.top+3,
                                        rect2.right-3,rect2.bottom+3),
                DT_LEFT | DT_SINGLELINE);

    //write the initial property value in the second rectangle
    dc.DrawText(pItem->m_curValue,CRect(rect.left+3,rect.top+3,
                                        rect.right+3,rect.bottom+3),
                DT_LEFT | DT_SINGLELINE);
    }
  dc.Detach();
}

int CPropertyList::AddItem(CString txt)
{
  int nIndex = AddString(txt);
  return nIndex;
}

int CPropertyList::AddPropItem(CPropertyItem* pItem)
{
  int nIndex = AddString(_T(""));
  SetItemDataPtr(nIndex,pItem);
  m_PropertyItems.insert(pItem);
  return nIndex;
}

int CPropertyList::AddProperty(const char* name,
                               const char* value,
                               int type,
                               const char* comboItems)
{ 
  CPropertyItem* pItem = 0;
  for(int i =0; i < this->GetCount(); ++i)
    {
    CPropertyItem* item = this->GetItem(i);
    if(item->m_propName == name)
      {
      pItem = item;
      if(pItem->m_curValue != value)
        {
        pItem->m_curValue = value;
        m_Dirty = true;
        Invalidate();
        }
      return i;
      }
    }
  // if it is not in the displayed list, then
  // check for it in the m_PropertyItems list as
  // a removed item
  for(std::set<CPropertyItem*>::iterator 
        p = m_PropertyItems.begin();
      p != m_PropertyItems.end(); ++p)
    {
    if((*p)->m_propName == name)
      {
      pItem = *p;
      pItem->m_Removed = false;
      pItem->m_curValue = value; 
      Invalidate();
      }
    }
  // if it is not found, then create a new one
  if(!pItem)
    {
    pItem = new CPropertyItem(name, value, type, comboItems);
    }
  
  return this->AddPropItem(pItem);
}

int CPropertyList::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
  if (CListBox::OnCreate(lpCreateStruct) == -1)
    return -1;

  m_bDivIsSet = FALSE;
  m_nDivider = 0;
  m_bTracking = FALSE;

  m_hCursorSize = AfxGetApp()->LoadStandardCursor(IDC_SIZEWE);
  m_hCursorArrow = AfxGetApp()->LoadStandardCursor(IDC_ARROW);

  m_SSerif8Font.CreatePointFont(80,_T("MS Sans Serif"));

  return 0;
}

void CPropertyList::OnSelchange() 
{
  CRect rect;
  CString lBoxSelText;

  GetItemRect(m_curSel,rect);
  rect.left = m_nDivider;

  CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);

  if (m_btnCtrl)
    m_btnCtrl.ShowWindow(SW_HIDE);
  if (m_CheckBoxControl)
    m_CheckBoxControl.ShowWindow(SW_HIDE);
  
  if (pItem->m_nItemType==CPropertyList::COMBO)
    {
    //display the combo box.  If the combo box has already been
    //created then simply move it to the new location, else create it
    m_nLastBox = 0;
    if (m_cmbBox)
      m_cmbBox.MoveWindow(rect);
    else
      {	
      rect.bottom += 100;
      m_cmbBox.Create(CBS_DROPDOWNLIST 
                      | CBS_NOINTEGRALHEIGHT | WS_VISIBLE 
                      | WS_CHILD | WS_BORDER,
                      rect,this,IDC_PROPCMBBOX);
      m_cmbBox.SetFont(&m_SSerif8Font);
      }

    //add the choices for this particular property
    CString cmbItems = pItem->m_cmbItems;
    lBoxSelText = pItem->m_curValue;
		
    m_cmbBox.ResetContent();
    int i,i2;
    i=0;
    while ((i2=cmbItems.Find('|',i)) != -1)
      {
      m_cmbBox.AddString(cmbItems.Mid(i,i2-i));
      i=i2+1;
      }
    if(i != 0)
      m_cmbBox.AddString(cmbItems.Mid(i));

    m_cmbBox.ShowWindow(SW_SHOW);
    m_cmbBox.SetFocus();

    //jump to the property's current value in the combo box
    int j = m_cmbBox.FindStringExact(0,lBoxSelText);
    if (j != CB_ERR)
      m_cmbBox.SetCurSel(j);
    else
      m_cmbBox.SetCurSel(0);
    }
  else if (pItem->m_nItemType==CPropertyList::EDIT)
    {
    //display edit box
    m_nLastBox = 1;
    m_prevSel = m_curSel;
    rect.bottom -= 3;
    if (m_editBox)
      m_editBox.MoveWindow(rect);
    else
      {	
      m_editBox.Create(ES_LEFT | ES_AUTOHSCROLL | WS_VISIBLE 
                       | WS_CHILD | WS_BORDER,
                       rect,this,IDC_PROPEDITBOX);
      m_editBox.SetFont(&m_SSerif8Font);
      }

    lBoxSelText = pItem->m_curValue;

    m_editBox.ShowWindow(SW_SHOW);
    m_editBox.SetFocus();
    //set the text in the edit box to the property's current value
    m_editBox.SetWindowText(lBoxSelText);
    }
  else if (pItem->m_nItemType == CPropertyList::CHECKBOX)
    {
    rect.bottom -= 3;
    if (m_CheckBoxControl)
      m_CheckBoxControl.MoveWindow(rect);
    else
      {	
      m_CheckBoxControl.Create("check",BS_CHECKBOX 
                               | BM_SETCHECK |BS_LEFTTEXT 
                               | WS_VISIBLE | WS_CHILD,
                               rect,this,IDC_PROPCHECKBOXCTRL);
      m_CheckBoxControl.SetFont(&m_SSerif8Font);
      }

    lBoxSelText = pItem->m_curValue;

    m_CheckBoxControl.ShowWindow(SW_SHOW);
    m_CheckBoxControl.SetFocus();
    //set the text in the edit box to the property's current value
    if(lBoxSelText == "ON")
      {
      m_CheckBoxControl.SetCheck(1);
      }
    else
      {
      m_CheckBoxControl.SetCheck(0);
      }
    }
        
  else
    DisplayButton(rect);
}

void CPropertyList::DisplayButton(CRect region)
{
  //displays a button if the property is a file/color/font chooser
  m_nLastBox = 2;
  m_prevSel = m_curSel;

  if (region.Width() > 25)
    region.left = region.right - 25;
  region.bottom -= 3;

  if (m_btnCtrl)
    m_btnCtrl.MoveWindow(region);
  else
    {	
    m_btnCtrl.Create("...",BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,
                     region,this,IDC_PROPBTNCTRL);
    m_btnCtrl.SetFont(&m_SSerif8Font);
    }

  m_btnCtrl.ShowWindow(SW_SHOW);
  m_btnCtrl.SetFocus();
}

void CPropertyList::OnKillFocus(CWnd* pNewWnd) 
{
  //m_btnCtrl.ShowWindow(SW_HIDE);

  CListBox::OnKillFocus(pNewWnd);
}

void CPropertyList::OnKillfocusCmbBox() 
{
  m_cmbBox.ShowWindow(SW_HIDE);

  Invalidate();
}

void CPropertyList::OnKillfocusEditBox()
{
  CString newStr;
  m_editBox.ShowWindow(SW_HIDE);

  Invalidate();
}

void CPropertyList::OnSelchangeCmbBox()
{
  CString selStr;
  if (m_cmbBox)
    {
    m_cmbBox.GetLBText(m_cmbBox.GetCurSel(),selStr);
    CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
    pItem->m_curValue = selStr;
    m_Dirty = true;
    }
}

void CPropertyList::OnChangeEditBox()
{
  CString newStr;
  m_editBox.GetWindowText(newStr);
	
  CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  pItem->m_curValue = newStr;
  m_Dirty = true;
}

void CPropertyList::OnCheckBox()
{ 
  CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  if(m_CheckBoxControl.GetCheck())
    {
    pItem->m_curValue = "ON";
    }
  else
    {
    pItem->m_curValue = "OFF";
    }
  m_Dirty = true;
}

void CPropertyList::OnButton()
{
  CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);

  //display the appropriate common dialog depending on what type
  //of chooser is associated with the property
  if (pItem->m_nItemType == CPropertyList::COLOR)
    {
    COLORREF initClr;
    CString currClr = pItem->m_curValue;
    //parse the property's current color value
    if (currClr.Find("RGB") > -1)
      {
      int j = currClr.Find(',',3);
      CString bufr = currClr.Mid(4,j-4);
      int RVal = atoi(bufr);
      int j2 = currClr.Find(',',j+1);
      bufr = currClr.Mid(j+1,j2-(j+1));
      int GVal = atoi(bufr);
      int j3 = currClr.Find(')',j2+1);
      bufr = currClr.Mid(j2+1,j3-(j2+1));
      int BVal = atoi(bufr);
      initClr = RGB(RVal,GVal,BVal);
      }
    else
      initClr = 0;
		
    CColorDialog ClrDlg(initClr);
		
    if (IDOK == ClrDlg.DoModal())
      {
      COLORREF selClr = ClrDlg.GetColor();
      CString clrStr;
      clrStr.Format("RGB(%d,%d,%d)",GetRValue(selClr),
                    GetGValue(selClr),GetBValue(selClr));
      m_btnCtrl.ShowWindow(SW_HIDE);

      pItem->m_curValue = clrStr;
      m_Dirty = true;
      Invalidate();
      }
    }
  else if (pItem->m_nItemType == CPropertyList::FILE)
    {
    CString SelectedFile; 
    CString Filter("Gif Files (*.gif)|*.gif||");
	
    CFileDialog FileDlg(TRUE, NULL, NULL, NULL,
			Filter);
		
    CString currPath = pItem->m_curValue;
    FileDlg.m_ofn.lpstrTitle = "Select file";
    if (currPath.GetLength() > 0)
      FileDlg.m_ofn.lpstrInitialDir = currPath.Left(
        currPath.GetLength() - currPath.ReverseFind('\\'));

    if(IDOK == FileDlg.DoModal())
      {
      SelectedFile = FileDlg.GetPathName();
			
      m_btnCtrl.ShowWindow(SW_HIDE);
			
      pItem->m_curValue = SelectedFile;
      m_Dirty = true;
      Invalidate();
      }
    }
   else if (pItem->m_nItemType == CPropertyList::PATH)
    {
    char szPathName[4096];
    BROWSEINFO bi;
    
    bi.hwndOwner = m_hWnd;
    bi.pidlRoot = NULL;
    bi.pszDisplayName = (LPTSTR)szPathName;
    bi.lpszTitle = "Select Directory";
    bi.ulFlags = BIF_EDITBOX | BIF_RETURNONLYFSDIRS;
    bi.lpfn = NULL;
    
    LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
    
    BOOL bSuccess = SHGetPathFromIDList(pidl, szPathName);
    CString SelectedFile; 
    if(bSuccess)
      {
      SelectedFile = szPathName;
      m_btnCtrl.ShowWindow(SW_HIDE);
      pItem->m_curValue = SelectedFile;
      m_Dirty = true;
      Invalidate();
      }
    }
  else if (pItem->m_nItemType == CPropertyList::FONT)
    {	
    CFontDialog FontDlg(NULL,CF_EFFECTS | CF_SCREENFONTS,NULL,this);
		
    if(IDOK == FontDlg.DoModal())
      {
      CString faceName = FontDlg.GetFaceName();
			
      m_btnCtrl.ShowWindow(SW_HIDE);
			
      pItem->m_curValue = faceName;
      m_Dirty = true;
      Invalidate();
      }
    }
}

void CPropertyList::OnLButtonUp(UINT nFlags, CPoint point) 
{
  if (m_bTracking)
    {
    //if columns were being resized then this indicates
    //that mouse is up so resizing is done.  Need to redraw
    //columns to reflect their new widths.
		
    m_bTracking = FALSE;
    //if mouse was captured then release it
    if (GetCapture()==this)
      ::ReleaseCapture();

    ::ClipCursor(NULL);

    CClientDC dc(this);
    InvertLine(&dc,CPoint(point.x,m_nDivTop),CPoint(point.x,m_nDivBtm));
    //set the divider position to the new value
    m_nDivider = point.x;

    //redraw
    Invalidate();
    }
  else
    {
    BOOL loc;
    int i = ItemFromPoint(point,loc);
    m_curSel = i;
    CListBox::OnLButtonUp(nFlags, point);
    }
}

void CPropertyList::OnLButtonDown(UINT nFlags, CPoint point) 
{
  if ((point.x>=m_nDivider-5) && (point.x<=m_nDivider+5))
    {
    //if mouse clicked on divider line, then start resizing

    ::SetCursor(m_hCursorSize);

    CRect windowRect;
    GetWindowRect(windowRect);
    windowRect.left += 10; windowRect.right -= 10;
    //do not let mouse leave the list box boundary
    ::ClipCursor(windowRect);
		
    if (m_cmbBox)
      m_cmbBox.ShowWindow(SW_HIDE);
    if (m_editBox)
      m_editBox.ShowWindow(SW_HIDE);

    CRect clientRect;
    GetClientRect(clientRect);

    m_bTracking = TRUE;
    m_nDivTop = clientRect.top;
    m_nDivBtm = clientRect.bottom;
    m_nOldDivX = point.x;

    CClientDC dc(this);
    InvertLine(&dc,CPoint(m_nOldDivX,m_nDivTop),CPoint(m_nOldDivX,m_nDivBtm));

    //capture the mouse
    SetCapture();
    }
  else
    {
    m_bTracking = FALSE;
    CListBox::OnLButtonDown(nFlags, point);
    }
}

void CPropertyList::OnMouseMove(UINT nFlags, CPoint point) 
{	
  if (m_bTracking)
    {
    //move divider line to the mouse pos. if columns are
    //currently being resized
    CClientDC dc(this);
    //remove old divider line
    InvertLine(&dc,CPoint(m_nOldDivX,m_nDivTop),CPoint(m_nOldDivX,m_nDivBtm));
    //draw new divider line
    InvertLine(&dc,CPoint(point.x,m_nDivTop),CPoint(point.x,m_nDivBtm));
    m_nOldDivX = point.x;
    }
  else if ((point.x >= m_nDivider-5) && (point.x <= m_nDivider+5))
    //set the cursor to a sizing cursor if the cursor is over the row divider
    ::SetCursor(m_hCursorSize);
  else
    CListBox::OnMouseMove(nFlags, point);
}

void CPropertyList::InvertLine(CDC* pDC,CPoint ptFrom,CPoint ptTo)
{
  int nOldMode = pDC->SetROP2(R2_NOT);
	
  pDC->MoveTo(ptFrom);
  pDC->LineTo(ptTo);

  pDC->SetROP2(nOldMode);
}

void CPropertyList::PreSubclassWindow() 
{
  m_bDivIsSet = FALSE;
  m_nDivider = 0;
  m_bTracking = FALSE;
  m_curSel = 1;

  m_hCursorSize = AfxGetApp()->LoadStandardCursor(IDC_SIZEWE);
  m_hCursorArrow = AfxGetApp()->LoadStandardCursor(IDC_ARROW);

  m_SSerif8Font.CreatePointFont(80,_T("MS Sans Serif"));
}

CPropertyItem* CPropertyList::GetItem(int index)
{
  return (CPropertyItem*)GetItemDataPtr(index);
}

void CPropertyList::OnRButtonUp( UINT nFlags, CPoint point )
{
  CMenu menu;
  CRect rect;
  this->GetWindowRect(&rect);
  BOOL loc;
  m_curSel = ItemFromPoint(point,loc);
  menu.CreatePopupMenu();
  menu.AppendMenu(MF_STRING | MF_ENABLED, 42, "Delete Cache Entry");
  menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, 
                      rect.TopLeft().x + point.x,
                      rect.TopLeft().y + point.y, this, NULL);
}


void CPropertyList::OnDelete()
{ 
  CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  pItem->m_Removed = true;
  this->DeleteString(m_curSel);
  Invalidate();
}

void CPropertyList::RemoveAll()
{
  int c = this->GetCount();
  for(int i =0; i < c; ++i)
    {
    CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(0);
    pItem->m_Removed = true;
    this->DeleteString(0);
    }
  Invalidate();
}