Skip to content

Commit

Permalink
Add search capability over message headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Dijji committed Nov 26, 2019
1 parent 8de40db commit 50a22a4
Show file tree
Hide file tree
Showing 14 changed files with 1,114 additions and 3 deletions.
17 changes: 16 additions & 1 deletion MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@
<Window x:Class="XstReader.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:stb="clr-namespace:SearchTextBox"
Title="Xst Reader" Height="800" Width="1500"
Closing="Window_Closing">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="VisibleIfTrue"/>
<Style x:Key="ListViewSelect" TargetType="{x:Type ListViewItem}">
<Setter x:Uid="Setter_10" Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter x:Uid="Setter_11" Property="FontWeight" Value="Normal" />
<Style.Triggers>
<Trigger x:Uid="Trigger_1" Property="IsSelected"
Value="True">
<Setter x:Uid="Setter_12" Property="FontWeight"
Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
Expand Down Expand Up @@ -48,7 +60,8 @@
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<ListView Grid.Row="0" Name="listMessages" Margin="0,0,0,0" DataContext="{Binding Path=SelectedFolder}" ItemsSource="{Binding Path=Messages}"
SelectionChanged="listMessages_SelectionChanged" FontSize="12">
SelectionChanged="listMessages_SelectionChanged" FontSize="12"
ItemContainerStyle="{StaticResource ResourceKey=ListViewSelect}">
<ListView.View>
<GridView>
<GridViewColumn Width="15" >
Expand Down Expand Up @@ -95,6 +108,8 @@
</GridView>
</ListView.View>
</ListView>
<stb:SearchTextBox Grid.Row="1" Width="250" x:Name="searchTextBox" SectionsStyle="CheckBoxStyle"
OnSearch="searchTextBox_OnSearch"/>
</Grid>
<GridSplitter Grid.Column="3" Width="3" HorizontalAlignment="Stretch" />
<Grid Grid.Column="4">
Expand Down
83 changes: 83 additions & 0 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2016, Dijji, and released under Ms-PL. This can be found in the root of this distribution.

using SearchTextBox;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand All @@ -22,11 +23,16 @@ public partial class MainWindow : Window
private XstFile xstFile = null;
private GridViewColumnHeader listViewSortCol = null;
private SortAdorner listViewSortAdorner = null;
private int searchIndex = -1;

public MainWindow()
{
InitializeComponent();
this.DataContext = view;

// Supply the Search control with the list of sections
searchTextBox.SectionsList = new List<string> { "Subject", "From/To", "Date" };

if (Properties.Settings.Default.Top != 0.0)
{
this.Top = Properties.Settings.Default.Top;
Expand Down Expand Up @@ -172,6 +178,8 @@ private void treeFolders_SelectedItemChanged(object sender, RoutedPropertyChange

private void listMessages_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
searchIndex = listMessages.SelectedIndex;
searchTextBox.ShowSearch = true;
Message m = (Message)listMessages.SelectedItem;

if (m != null)
Expand Down Expand Up @@ -208,6 +216,9 @@ private void listMessagesColumnHeader_Click(object sender, RoutedEventArgs e)
listViewSortAdorner = new SortAdorner(listViewSortCol, newDir);
AdornerLayer.GetAdornerLayer(listViewSortCol).Add(listViewSortAdorner);
listMessages.Items.SortDescriptions.Add(new SortDescription(sortBy, newDir));

searchIndex = listMessages.SelectedIndex;
listMessages.ScrollIntoView(listMessages.SelectedItem);
}

private void listRecipients_SelectionChanged(object sender, SelectionChangedEventArgs e)
Expand Down Expand Up @@ -275,6 +286,78 @@ private void rbProperties_Click(object sender, RoutedEventArgs e)
view.ShowContent = false;
}

private void searchTextBox_OnSearch(object sender, RoutedEventArgs e)
{
try
{
var args = e as SearchEventArgs;
bool subject = args.Sections.Contains("Subject");
bool fromTo = args.Sections.Contains("From/To");
bool date = args.Sections.Contains("Date");
bool found = false;
switch (args.SearchEventType)
{
case SearchEventType.Search:
for (int i = 0; i < listMessages.Items.Count; i++)
{
found = PropertyHitTest(i, args.Keyword, subject, fromTo, date);
if (found)
break;
}

if (!found)
searchIndex = -1;
break;
case SearchEventType.Next:
for (int i = searchIndex + 1; i < listMessages.Items.Count; i++)
{
found = PropertyHitTest(i, args.Keyword, subject, fromTo, date);
if (found)
{
searchTextBox.ShowSearch = true;
break;
}
}
break;
case SearchEventType.Previous:
for (int i = searchIndex - 1; i >= 0; i--)
{
found = PropertyHitTest(i, args.Keyword, subject, fromTo, date);
if (found)
{
searchTextBox.ShowSearch = true;
break;
}
}
break;
}

if (!found)
searchTextBox.IndicateSearchFailed(args.SearchEventType);
}
catch (Exception ex)
{
// Unclear what we can do here, as we were invoked by an event from the search text box control
}
}

private bool PropertyHitTest(int index, string text, bool subject, bool fromTo, bool date)
{
Message m = listMessages.Items[index] as Message;
if ((subject && m.Subject != null && m.Subject.IndexOf(text, StringComparison.CurrentCultureIgnoreCase) >= 0) ||
(fromTo && m.FromTo != null && m.FromTo.IndexOf(text, StringComparison.CurrentCultureIgnoreCase) >= 0) ||
(date && m.DisplayDate != null && m.DisplayDate.IndexOf(text, StringComparison.CurrentCultureIgnoreCase) >= 0))
{
searchIndex = index;
listMessages.UnselectAll();
m.IsSelected = true;
listMessages.ScrollIntoView(m);
return true;
}
else
return false;
}

private void ShowStatus(string status)
{
if (status != null)
Expand Down
Binary file added Resources/ic_add_black_18dp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/ic_clear_black_18dp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/ic_keyboard_arrow_down_black_18dp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/ic_keyboard_arrow_up_black_18dp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/ic_search_black_18dp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/ic_unfold_more_black_18dp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions SearchTextBox/ListBoxEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//Copied from www.codeproject.com/Articles/101975/Building-a-Search-Text-Box-Control-with-WPF
//and modified to add next and previous buttons, and improve icons and general behaviour
//Licensed under The Code Project Open License (CPOL)
using System;
using System.Windows;
using System.Windows.Controls;

namespace SearchTextBox
{

public class ListBoxEx : ListBox
{
public enum ItemStyles{
NormalStyle,
CheckBoxStyle,
RadioBoxStyle
}
private ItemStyles m_extendedStyle;

public ItemStyles ExtendedStyle
{
get { return m_extendedStyle; }
set {
m_extendedStyle = value;

// load resources
ResourceDictionary resDict = new ResourceDictionary();
resDict.Source = new Uri("/Themes/ListBoxEx.xaml", UriKind.Relative);
if (resDict.Source == null)
throw new SystemException();

switch (value)
{
case ItemStyles.NormalStyle:
this.Style = (Style)resDict["NormalListBox"];
break;
case ItemStyles.CheckBoxStyle:
this.Style = (Style)resDict["CheckListBox"];
break;
case ItemStyles.RadioBoxStyle:
this.Style = (Style)resDict["RadioListBox"];
break;
}
}
}

static ListBoxEx()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ListBoxEx), new FrameworkPropertyMetadata(typeof(ListBoxEx)));
}

public ListBoxEx(ItemStyles style)
: base()
{
ExtendedStyle = style;
}

public ListBoxEx(): base(){
ExtendedStyle = ItemStyles.NormalStyle;
}


}
}

Loading

0 comments on commit 50a22a4

Please sign in to comment.