package Padre::Wx::ComboBox::History;


=head1 NAME

Padre::Wx::ComboBox::History - A history-enabled Wx combobox


  $dialog->{search_text} = Padre::Wx::ComboBox::History->new(
      '', # Use the last history value
      [ 'search' ], # The history queue to read from


Padre::Wx::ComboBox::History is a normal Wx ComboBox widget, but enhanced
with the ability to remember previously entered values and present the
previous values as options the next time it is used.

This type of input memory is fairly common in dialog boxes and other task
inputs. The parameters are provided to the history box in a form compatible
with an ordinary Wx::ComboBox to simplify integration with GUI generators
such as L<Padre::Plugin::FormBuilder>.

The "options" hash should contain exactly one value, which should be the
key string for the history table. This can be a simple name, allowing the
sharing of remembered history across several different dialogs.

The "value" can be defined literally, or will be pulled from the most
recent history entry if it set to the null string.


use 5.008;
use strict;
use warnings;
use Padre::Wx ();
use Padre::DB ();

our $VERSION = '1.00';
our @ISA     = 'Wx::ComboBox';

sub new {
	my $class  = shift;
	my @params = @_;

	# First key in the value list to overwrite with the history values.
	my $type = $params[5]->[0];
	if ($type) {
		$params[5] = [ Padre::DB::History->recent($type) ];

		# Initial text defaults to empty string
		$params[2] ||= '';

	my $self = $class->SUPER::new(@params);

	# Save the type, we'll need it later.
	$self->{type} = $type;


sub refresh {
	my $self = shift;
	my $text = shift;
	$text = '' unless defined $text;
	$text = '' if $text =~ /\n/;

	# Refresh the recent values
	my @recent = Padre::DB::History->recent( $self->{type} );

	# Update the Wx object from the list
	if ( length $text ) {
		unless ( grep { $text eq $_ } @recent ) {
	foreach my $option (@recent) {

	return 1;

# Save the current value of the combobox and return it as per GetValue
sub SaveValue {
	my $self  = shift;
	my $value = $self->GetValue;

	# If this is a value is not in our existing recent list, save it
	if ( length $value ) {
		if ( $self->FindString($value) == Wx::NOT_FOUND ) {
				type => $self->{type},
				name => $value,

	return $value;


# Copyright 2008-2013 The Padre development team as listed in
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl 5 itself.