<commit_before>/*
* Copyright (c) 2003-2008 <NAME> <<EMAIL>>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/

// ManageFiltersDlg.cpp : implementation file
//

#include "../stdafx.h"
#include "ManageFiltersDlg.h"
#include "../DboxMain.h"
#include "../PWResizeDialog.h"
#include "../corelib/PWSFilters.h"
#include "../corelib/match.h"

#include "../resource3.h"
#include "../corelib/corelib.h"

#include <vector>
#include <map>
#include <algorithm>

// ManageFiltersDlg dialog

IMPLEMENT_DYNAMIC(CManageFiltersDlg, CPWDialog)

CManageFiltersDlg::CManageFiltersDlg(CWnd* pParent,
                               bool bFilterActive,
                               PWSFilters &mapfilters)
  : CPWDialog(CManageFiltersDlg::IDD, pParent),
  m_bFilterActive(bFilterActive), m_MapFilters(mapfilters),
  m_selectedfilterpool(FPOOL_LAST), m_selectedfiltername(_T("")),
  m_activefilterpool(FPOOL_LAST), m_activefiltername(_T("")),
  m_selectedfilter(-1), m_activefilter(-1),
  m_bDBFiltersChanged(false),
  m_num_to_export(0), m_num_to_copy(0),
  m_pCheckImageList(NULL), m_pImageList(NULL),
  m_iSortColumn(-1), m_bSortAscending(-1)
{
  m_pDbx = static_cast<DboxMain *>(pParent);

  PWSFilters::iterator mf_iter;

  for (mf_iter = m_MapFilters.begin();
       mf_iter != m_MapFilters.end();
       mf_iter++) {
    m_vcs_filters.push_back(mf_iter->first);
  }

  const COLORREF crTransparent = RGB(192, 192, 192);
  CBitmap bitmap;
  BITMAP bm;
  bitmap.LoadBitmap(IDB_CHECKED);
  bitmap.GetBitmap(&bm); // should be 13 x 13

  m_pCheckImageList = new CImageList;
  BOOL status = m_pCheckImageList->Create(bm.bmWidth, bm.bmHeight,
                                     ILC_MASK | ILC_COLOR, 3, 0);
  ASSERT(status != 0);

  m_pCheckImageList->Add(&bitmap, crTransparent);
  bitmap.DeleteObject();
  bitmap.LoadBitmap(IDB_UNCHECKED);
  m_pCheckImageList->Add(&bitmap, crTransparent);
  bitmap.DeleteObject();
  bitmap.LoadBitmap(IDB_EMPTY);
  m_pCheckImageList->Add(&bitmap, crTransparent);
  bitmap.DeleteObject();
  bitmap.LoadBitmap(IDB_BLANK);
  m_pCheckImageList->Add(&bitmap, crTransparent);
  bitmap.DeleteObject();
}

CManageFiltersDlg::~CManageFiltersDlg()
{
}

void CManageFiltersDlg::OnDestroy()
{
  // Tidy up filter ItemData
  int nCount = m_FilterLC.GetItemCount();
  for (int i = 0; i < nCount; i++) {
    st_FilterItemData *pflt_idata = (st_FilterItemData *)m_FilterLC.GetItemData(0);
    delete pflt_idata;
    m_FilterLC.DeleteItem(0);
  }

  // Tidy up images
  m_pImageList->DeleteImageList();
  delete m_pImageList;

  m_pCheckImageList->DeleteImageList();
  delete m_pCheckImageList;

  CPWDialog::OnDestroy();
}

void CManageFiltersDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  DDX_Control(pDX, IDC_FILTERLC, m_FilterLC);
  DDX_Control(pDX, IDC_FILTERPROPERTIES, m_FilterProperties);
}

BEGIN_MESSAGE_MAP(CManageFiltersDlg, CPWDialog)
  ON_WM_DESTROY()
  ON_BN_CLICKED(IDC_FILTERNEW, OnFilterNew)
  ON_BN_CLICKED(IDC_FILTEREDIT, OnFilterEdit)
  ON_BN_CLICKED(IDC_FILTERCOPY, OnFilterCopy)
  ON_BN_CLICKED(IDC_FILTERDELETE, OnFilterDelete)
  ON_BN_CLICKED(IDC_FILTERIMPORT, OnFilterImport)
  ON_BN_CLICKED(IDC_FILTEREXPORT, OnFilterExport)
  ON_NOTIFY(NM_CLICK, IDC_FILTERLC, OnClick)
  ON_NOTIFY(NM_CLICK, IDC_FILTERPROPERTIES, OnClick)
  ON_NOTIFY(NM_CUSTOMDRAW, IDC_FILTERLC, OnCustomDraw)
  ON_NOTIFY(LVN_ITEMCHANGING, IDC_FILTERLC, OnItemChanging)
  ON_NOTIFY(LVN_ITEMCHANGING, IDC_FILTERPROPERTIES, OnItemChanging)
  ON_NOTIFY(LVN_COLUMNCLICK, IDC_FILTERLC, OnColumnClick)
END_MESSAGE_MAP()

// ManageFilters message handlers

BOOL CManageFiltersDlg::OnInitDialog()
{
  CPWDialog::OnInitDialog();

  // Make some columns centered
  LVCOLUMN lvc;
  lvc.mask = LVCF_FMT;
  lvc.fmt = LVCFMT_CENTER;

  DWORD dwExStyle = m_FilterLC.GetExtendedStyle();
  dwExStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
  m_FilterLC.SetExtendedStyle(dwExStyle);

  CString cs_text;
  cs_text.LoadString(IDS_FILTERNAME);
  m_FilterLC.InsertColumn(MFLC_FILTER_NAME, cs_text);
  cs_text.LoadString(IDS_FILTERSOURCE);
  m_FilterLC.InsertColumn(MFLC_FILTER_SOURCE, cs_text);
  cs_text.LoadString(IDS_FILTERAPPLY);
  m_FilterLC.InsertColumn(MFLC_INUSE, cs_text);
  cs_text.LoadString(IDS_FILTERCOPY2DB);
  m_FilterLC.InsertColumn(MFLC_COPYTODATABASE, cs_text);
  cs_text.LoadString(IDS_FILTEREXPORT);
  m_FilterLC.InsertColumn(MFLC_EXPORT, cs_text);

  // Make some columns centered
  m_FilterLC.SetColumn(MFLC_FILTER_SOURCE, &lvc);
  m_FilterLC.SetColumn(MFLC_INUSE, &lvc);
  m_FilterLC.SetColumn(MFLC_COPYTODATABASE, &lvc);
  m_FilterLC.SetColumn(MFLC_EXPORT, &lvc);

  // Populate CListCtrl
  UpdateFilterList();

  // Set row height to take image by adding a dummy ImageList
  CRect rect;
  m_FilterLC.GetItemRect(0, &rect, LVIR_BOUNDS);
  IMAGEINFO imageinfo;
  m_pCheckImageList->GetImageInfo(0, &imageinfo);
  int irowheight = max(rect.Height(),
                       abs(imageinfo.rcImage.top - imageinfo.rcImage.bottom));
  m_pImageList = new CImageList;
  m_pImageList->Create(1, irowheight, ILC_COLOR4, 1, 1);
  m_FilterLC.SetImageList(m_pImageList, LVSIL_SMALL);

  dwExStyle = m_FilterProperties.GetExtendedStyle();
  dwExStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
  m_FilterProperties.SetExtendedStyle(dwExStyle);

  cs_text = _T(" # ");
  m_FilterProperties.InsertColumn(MFPRP_FILTER_NUMBER, cs_text);
  cs_text.LoadString(IDS_FILTERACTIVE);
  cs_text += _T("?");
  m_FilterProperties.InsertColumn(MFPRP_FILTER_ACTIVE, cs_text);<commit_msg>Ensure filter row is high enough for images if no entries there at first<commit_after>/*
* Copyright (c) 2003-2008 <NAME> <<EMAIL>>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/

// ManageFiltersDlg.cpp : implementation file
//

#include "../stdafx.h"
#include "ManageFiltersDlg.h"
#include "../DboxMain.h"
#include "../PWResizeDialog.h"
#include "../corelib/PWSFilters.h"
#include "../corelib/match.h"

#include "../resource3.h"
#include "../corelib/corelib.h"

#include <vector>
#include <map>
#include <algorithm>

// ManageFiltersDlg dialog

IMPLEMENT_DYNAMIC(CManageFiltersDlg, CPWDialog)

CManageFiltersDlg::CManageFiltersDlg(CWnd* pParent,
                               bool bFilterActive,
                               PWSFilters &mapfilters)
  : CPWDialog(CManageFiltersDlg::IDD, pParent),
  m_bFilterActive(bFilterActive), m_MapFilters(mapfilters),
  m_selectedfilterpool(FPOOL_LAST), m_selectedfiltername(_T("")),
  m_activefilterpool(FPOOL_LAST), m_activefiltername(_T("")),
  m_selectedfilter(-1), m_activefilter(-1),
  m_bDBFiltersChanged(false),
  m_num_to_export(0), m_num_to_copy(0),
  m_pCheckImageList(NULL), m_pImageList(NULL),
  m_iSortColumn(-1), m_bSortAscending(-1)
{
  m_pDbx = static_cast<DboxMain *>(pParent);

  PWSFilters::iterator mf_iter;

  for (mf_iter = m_MapFilters.begin();
       mf_iter != m_MapFilters.end();
       mf_iter++) {
    m_vcs_filters.push_back(mf_iter->first);
  }

  const COLORREF crTransparent = RGB(192, 192, 192);
  CBitmap bitmap;
  BITMAP bm;
  bitmap.LoadBitmap(IDB_CHECKED);
  bitmap.GetBitmap(&bm); // should be 13 x 13

  m_pCheckImageList = new CImageList;
  BOOL status = m_pCheckImageList->Create(bm.bmWidth, bm.bmHeight,
                                     ILC_MASK | ILC_COLOR, 3, 0);
  ASSERT(status != 0);

  m_pCheckImageList->Add(&bitmap, crTransparent);
  bitmap.DeleteObject();
  bitmap.LoadBitmap(IDB_UNCHECKED);
  m_pCheckImageList->Add(&bitmap, crTransparent);
  bitmap.DeleteObject();
  bitmap.LoadBitmap(IDB_EMPTY);
  m_pCheckImageList->Add(&bitmap, crTransparent);
  bitmap.DeleteObject();
  bitmap.LoadBitmap(IDB_BLANK);
  m_pCheckImageList->Add(&bitmap, crTransparent);
  bitmap.DeleteObject();
}

CManageFiltersDlg::~CManageFiltersDlg()
{
}

void CManageFiltersDlg::OnDestroy()
{
  // Tidy up filter ItemData
  int nCount = m_FilterLC.GetItemCount();
  for (int i = 0; i < nCount; i++) {
    st_FilterItemData *pflt_idata = (st_FilterItemData *)m_FilterLC.GetItemData(0);
    delete pflt_idata;
    m_FilterLC.DeleteItem(0);
  }

  // Tidy up images
  m_pImageList->DeleteImageList();
  delete m_pImageList;

  m_pCheckImageList->DeleteImageList();
  delete m_pCheckImageList;

  CPWDialog::OnDestroy();
}

void CManageFiltersDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  DDX_Control(pDX, IDC_FILTERLC, m_FilterLC);
  DDX_Control(pDX, IDC_FILTERPROPERTIES, m_FilterProperties);
}

BEGIN_MESSAGE_MAP(CManageFiltersDlg, CPWDialog)
  ON_WM_DESTROY()
  ON_BN_CLICKED(IDC_FILTERNEW, OnFilterNew)
  ON_BN_CLICKED(IDC_FILTEREDIT, OnFilterEdit)
  ON_BN_CLICKED(IDC_FILTERCOPY, OnFilterCopy)
  ON_BN_CLICKED(IDC_FILTERDELETE, OnFilterDelete)
  ON_BN_CLICKED(IDC_FILTERIMPORT, OnFilterImport)
  ON_BN_CLICKED(IDC_FILTEREXPORT, OnFilterExport)
  ON_NOTIFY(NM_CLICK, IDC_FILTERLC, OnClick)
  ON_NOTIFY(NM_CLICK, IDC_FILTERPROPERTIES, OnClick)
  ON_NOTIFY(NM_CUSTOMDRAW, IDC_FILTERLC, OnCustomDraw)
  ON_NOTIFY(LVN_ITEMCHANGING, IDC_FILTERLC, OnItemChanging)
  ON_NOTIFY(LVN_ITEMCHANGING, IDC_FILTERPROPERTIES, OnItemChanging)
  ON_NOTIFY(LVN_COLUMNCLICK, IDC_FILTERLC, OnColumnClick)
END_MESSAGE_MAP()

// ManageFilters message handlers

BOOL CManageFiltersDlg::OnInitDialog()
{
  CPWDialog::OnInitDialog();

  // Make some columns centered
  LVCOLUMN lvc;
  lvc.mask = LVCF_FMT;
  lvc.fmt = LVCFMT_CENTER;

  DWORD dwExStyle = m_FilterLC.GetExtendedStyle();
  dwExStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
  m_FilterLC.SetExtendedStyle(dwExStyle);

  CString cs_text;
  cs_text.LoadString(IDS_FILTERNAME);
  m_FilterLC.InsertColumn(MFLC_FILTER_NAME, cs_text);
  cs_text.LoadString(IDS_FILTERSOURCE);
  m_FilterLC.InsertColumn(MFLC_FILTER_SOURCE, cs_text);
  cs_text.LoadString(IDS_FILTERAPPLY);
  m_FilterLC.InsertColumn(MFLC_INUSE, cs_text);
  cs_text.LoadString(IDS_FILTERCOPY2DB);
  m_FilterLC.InsertColumn(MFLC_COPYTODATABASE, cs_text);
  cs_text.LoadString(IDS_FILTEREXPORT);
  m_FilterLC.InsertColumn(MFLC_EXPORT, cs_text);

  // Make some columns centered
  m_FilterLC.SetColumn(MFLC_FILTER_SOURCE, &lvc);
  m_FilterLC.SetColumn(MFLC_INUSE, &lvc);
  m_FilterLC.SetColumn(MFLC_COPYTODATABASE, &lvc);
  m_FilterLC.SetColumn(MFLC_EXPORT, &lvc);

  // Populate CListCtrl
  UpdateFilterList();

  // if no filters - add a dummy before adjusting item height
  bool bAddDummy(false);
  if (m_FilterLC.GetItemCount() == 0) {
    bAddDummy = true;
    int iItem = m_FilterLC.InsertItem(0 /* MFLC_FILTER_NAME */,_T(""));
    CString cs_source = GetFilterPoolName(FPOOL_SESSION);

    m_FilterLC.SetItemText(iItem, MFLC_FILTER_SOURCE, cs_source);
    m_FilterLC.SetItemText(iItem, MFLC_INUSE, _T("."));
    m_FilterLC.SetItemText(iItem, MFLC_COPYTODATABASE, _T("."));
    m_FilterLC.SetItemText(iItem, MFLC_EXPORT, _T("."));

    st_FilterItemData *pflt_idata = new st_FilterItemData;
    pflt_idata->flt_key.cs_filtername = _T(".");
    pflt_idata->flt_key.fpool = FPOOL_SESSION;
    pflt_idata->flt_flags = MFLT_REQUEST_COPY_TO_DB | MFLT_REQUEST_EXPORT | MFLT_INUSE;
    m_FilterLC.SetItemData(iItem, (DWORD)pflt_idata);
  }

  // Set row height to take image by adding a dummy ImageList
  CRect rect;
  m_FilterLC.GetItemRect(0, &rect, LVIR_BOUNDS);
  IMAGEINFO imageinfo;
  m_pCheckImageList->GetImageInfo(0, &imageinfo);
  int irowheight = max(rect.Height(),
                       abs(imageinfo.rcImage.top - imageinfo.rcImage.bottom));
  m_pImageList = new CImageList;
  m_pImageList->Create(1, irowheight, ILC_COLOR4, 1, 1);
  m_FilterLC.SetImageList(m_pImageList, LVSIL_SMALL);

  // If we added a dummy row - delete it
  if (bAddDummy) {
    st_FilterItemData *pflt_idata = (st_FilterItemData *)m_FilterLC.GetItemData(0);
    delete pflt_idata;
    m_FilterLC.DeleteItem(0);
  }

  dwExStyle = m_FilterProperties.GetExtendedStyle();
  dwExStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
  m_FilterProperties.SetExtendedStyle(dwExStyle);

  cs_text = _T(" # ");
  m_FilterProperties.InsertColumn(MFPRP_FILTER_NUMBER, cs_text);
  cs_text.LoadString(IDS_FILTERACTIVE);
  cs_text += _T("?");
  m_FilterProperties.InsertColumn(MFPRP_FILTER_ACTIVE, cs_text);
{'fraction_non_alphanumeric': 0.11463022508038585, 'fraction_numerical': 0.007556270096463023, 'mean_word_length': 5.195717131474104, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '120189', 'n_tokens_mistral': 4834, 'n_tokens_neox': 4774, 'n_words': 734}