list_dialog.cpp

Go to the documentation of this file.
00001 #ifndef LIST_DIALOG_IMPLEMENTATION_FILE
00002 #define LIST_DIALOG_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : list_dialog                                                       *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 2008-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "list_dialog.h"
00019 #include "mfc_extensions.rh"
00020 
00021 #include <basis/byte_array.h>
00022 #include <basis/istring.h>
00023 #include <basis/string_array.h>
00024 
00026 
00027 class list_dialog_implementation : public CListBox
00028 {
00029 public:
00030   list_dialog_implementation() {}
00031   virtual ~list_dialog_implementation() {}
00032   virtual void DrawItem(LPDRAWITEMSTRUCT item_posn);
00033 };
00034 
00035 void list_dialog_implementation::DrawItem(LPDRAWITEMSTRUCT item_posn)
00036 {
00037   if (!item_posn) return;
00038 
00039   CRect rect(item_posn->rcItem);
00040   CDC *dc_ptr = CDC::FromHandle(item_posn->hDC);
00041   CString item_text = "     ";
00042   GetText(item_posn->itemID, item_text);
00043   dc_ptr->SetBkMode(TRANSPARENT);
00044 
00045   if ( (item_posn->itemState & ODS_SELECTED) && (item_posn->itemData > 0) ) {
00046     CBrush brush(GetSysColor(COLOR_HIGHLIGHT));
00047     dc_ptr->FillRect(rect, &brush);
00048     dc_ptr->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT));
00049   } else {
00050     CBrush brush(GetSysColor(COLOR_WINDOW));
00051     dc_ptr->FillRect(rect, &brush);
00052     if ( item_posn->itemData > 0 )
00053       dc_ptr->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
00054     else
00055       dc_ptr->SetTextColor(GetSysColor(COLOR_GRAYTEXT));
00056   }
00057   dc_ptr->DrawText(item_text, -1, rect,
00058       DT_SINGLELINE | DT_LEFT | DT_VCENTER | DT_NOPREFIX);
00059   if ( (item_posn->itemState & ODS_FOCUS) && (item_posn->itemData > 0) )
00060     dc_ptr->DrawFocusRect(rect);
00061 }
00062 
00064 
00065 const int IDD = IDD_LIST_DIALOG_DIALOG;
00066 
00067 BEGIN_MESSAGE_MAP(list_dialog, CDialog)
00068   ON_LBN_DBLCLK(IDC_LIST_DIALOG_IMPLEM, OnDblClkListbox)
00069   ON_LBN_SELCHANGE(IDC_LIST_DIALOG_IMPLEM, OnSelchangeTypesListbox)
00070 END_MESSAGE_MAP()
00071 
00072 list_dialog::list_dialog(CWnd *parent, const istring &title,
00073     bool on_top)
00074 : CDialog(IDD, parent),
00075   _implementation(new list_dialog_implementation),
00076   _title(new istring(title)),
00077   _on_top(on_top),
00078   _selection_indy(-1),
00079   _last_selection_indy(-1),
00080   _enabled_items(new byte_array),
00081   _list_items(new string_array)
00082 {}
00083 
00084 list_dialog::~list_dialog()
00085 {
00086   WHACK(_implementation);
00087   WHACK(_title);  
00088   WHACK(_list_items); 
00089   WHACK(_enabled_items);  
00090 }
00091 
00092 void list_dialog::OnDblClkListbox() { OnOK(); }
00093 
00094 void list_dialog::OnOK() { UpdateData(true); CDialog::OnOK(); }
00095 
00096 void list_dialog::DoDataExchange(CDataExchange* ddx_ptr)
00097 {
00098   CDialog::DoDataExchange(ddx_ptr);
00099   DDX_Control(ddx_ptr, IDC_LIST_DIALOG_IMPLEM, *_implementation);
00100 }
00101 
00102 int list_dialog::add_string(const istring &text, bool enabled)
00103 {
00104   *_enabled_items += byte(enabled);
00105   *_list_items += text;
00106   return _list_items->last();  // return position of newest item.
00107 }
00108 
00109 BOOL list_dialog::OnInitDialog()
00110 {
00111   CDialog::OnInitDialog();
00112   SetWindowText(to_unicode_temp(*_title));
00113 
00114   for (int i = 0; i < _list_items->length(); i++) {
00115     int indy = _implementation->AddString(to_unicode_temp((*_list_items)[i]));
00116     _implementation->SetItemData(indy, (*_enabled_items)[i]);
00117     if ( (_selection_indy == -1) && (*_enabled_items)[i]) {
00118       _selection_indy = indy;
00119       _last_selection_indy = _selection_indy;
00120       _implementation->SetCurSel(_selection_indy);
00121     }
00122   }
00123 
00124   if (_on_top)
00125     SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
00126 
00127   return true;
00128 }
00129 
00130 void list_dialog::OnSelchangeTypesListbox()
00131 {
00132   int indy = _implementation->GetCurSel();
00133   DWORD_PTR available = _implementation->GetItemData(indy);
00134   if (!available) {
00135     // if the item is not enabled, select the last one that had been chosen.
00136     _implementation->PostMessage(LB_SETCURSEL, _last_selection_indy, 0);
00137   } else {
00138     _last_selection_indy = indy;
00139     _selection_indy = indy;
00140   }
00141 }
00142 
00143 int list_dialog::get_current(istring &selected)
00144 {
00145   if (_selection_indy < 0) {
00146     selected = "";
00147     return _selection_indy;
00148   }
00149   selected = (*_list_items)[_selection_indy];
00150   return _selection_indy;
00151 }
00152 
00153 
00154 #endif //LIST_DIALOG_IMPLEMENTATION_FILE
00155 

Generated on Fri Nov 28 04:28:59 2008 for HOOPLE Libraries by  doxygen 1.5.1