Source

wxPython / wx / tools / Editra / src / eclib / finddlg.py

Full commit
   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
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
###############################################################################
# Name: finddlg.py                                                            #
# Purpose: Custom advanced find dialog                                        #
# Author: Cody Precord <cprecord@editra.org>                                  #
# Copyright: (c) 2008 Cody Precord <staff@editra.org>                         #
# License: wxWindows License                                                  #
###############################################################################

"""
Editra Control Library: Advanced Find Replace Dialog

The AdvancedFindReplaceDialog is a custom FindReplaceDlg that functions
similarly to the standard wx.FindReplaceDialog but provides more search
configuration and presentation options. 

The following items are the options that the AdvancedFindReplaceDialog offers
over the basic FindReplaceDialog.

  * Hide/Show each option or section individually (basic dialog only disables them)
  * Multi-Find/Replace event action for Find All / Replace All actions
  * Switch dialog from Find mode to Replace mode or visa-versa once its already
    been created.
  * Options for specifying the location to look in.
  * Regular Expression option
  * Use standard dialog or a floating MiniFrame (default)

Requirements:
python 2.4+
wxPython 2.8+
eclib.platebtn
eclib.ctrlbox

@todo: Make Look In location strings configurable

"""

__author__ = "Cody Precord <cprecord@editra.org>"
__svnid__ = "$Id: finddlg.py 70025 2011-12-17 18:16:54Z CJP $"
__revision__ = "$Revision: 70025 $"

__all__ = ["FindBox", "FindEvent", "FindPanel", "FindReplaceDlg",
           "MiniFindReplaceDlg", "AdvFindReplaceDlg",

           "AFR_STYLE_FINDDIALOG", "AFR_STYLE_REPLACEDIALOG",
           "AFR_STYLE_NON_FLOATING", "AFR_STYLE_NO_MODE_SELECT",
           
           "AFR_UP", "AFR_WHOLEWORD", 
           "AFR_MATCHCASE", "AFR_REGEX", "AFR_RECURSIVE", "AFR_NOLOOKIN",
           "AFR_NOUPDOWN", "AFR_NOWHOLEWORD", "AFR_NOMATCHCASE", "AFR_NOREGEX",
           "AFR_NOFILTER", "AFR_NOOPTIONS", "AFR_NO_COUNT", "AFR_NO_ALL_BTN",
           "AFR_SIMPLE",

           "LOCATION_CURRENT_DOC", "LOCATION_IN_SELECTION",
           "LOCATION_OPEN_DOCS", "LOCATION_IN_CURRENT_DIR",
           "LOCATION_IN_FILES", "LOCATION_MAX",

           "edEVT_FIND_CLOSE", "EVT_FIND_CLOSE", "edEVT_FIND", "EVT_FIND",
           "edEVT_FIND_NEXT", "EVT_FIND_NEXT", "edEVT_FIND_ALL", "EVT_FIND_ALL",
           "edEVT_REPLACE", "EVT_REPLACE", "edEVT_REPLACE_ALL",
           "EVT_REPLACE_ALL", "EVT_OPTION_CHANGED", "edEVT_OPTION_CHANGED",
           "EVT_COUNT", "edEVT_COUNT"]

#--------------------------------------------------------------------------#
# Imports
import os
import wx

# Local Imports
import ctrlbox
import platebtn

#--------------------------------------------------------------------------#
# Globals

# Style Flags
AFR_STYLE_FINDDIALOG        = 0 # Start dialog in Find mode
AFR_STYLE_REPLACEDIALOG     = 1 # Start dialog in Replace mode
AFR_STYLE_NON_FLOATING      = 2 # Use a standard dialog as the tlw
AFR_STYLE_NO_MODE_SELECT    = 4 # Hide the mode selector buttons

# FindReplaceData Flags
AFR_UP          = 1             # Set dialogs Search Up flag
AFR_WHOLEWORD   = 2             # Set dialogs whole word search flag
AFR_MATCHCASE   = 4             # Set dialogs match case search flag
AFR_REGEX       = 8             # Set dialogs use regular expression flag
AFR_RECURSIVE   = 16            # Set dialogs recursive directory search flag
AFR_NOLOOKIN    = 32            # Hide the Looking combobox in the dialog
AFR_NOUPDOWN    = 64            # Hide the Direction options in the dialog
AFR_NOWHOLEWORD = 128           # Hide the Whole Word option in the dialog
AFR_NOMATCHCASE = 256           # Hide the Match Case option in the dialog
AFR_NOREGEX     = 512           # Hide the Regular Expression option
AFR_NOFILTER    = 1024          # Hide the File Filter option
AFR_NOOPTIONS   = 2048          # Hide all options in the dialog
AFR_NO_COUNT    = 4096          # Hide Count Button
AFR_NO_ALL_BTN  = 8192          # Hide Find All / Replace All buttons

# Convenience Flags
AFR_SIMPLE = (AFR_NOLOOKIN | AFR_NOOPTIONS | AFR_NOUPDOWN | \
              AFR_NO_COUNT | AFR_NO_ALL_BTN)

# Search Location Parameters (NOTE: must be kept in sync with Lookin List)
LOCATION_CURRENT_DOC    = 0
LOCATION_IN_SELECTION   = 1
LOCATION_OPEN_DOCS      = 2
LOCATION_IN_CURRENT_DIR = 3
LOCATION_IN_FILES       = 4
LOCATION_MAX            = LOCATION_IN_CURRENT_DIR

# Control Names
FindBoxName = "EdFindBox"
FindPanelName = "EdFindPanel"

# Find Panel Control Ids
ID_LOOKIN = wx.NewId()
ID_FIND_LBL = wx.NewId()
ID_REPLACE_LBL = wx.NewId()
ID_MATCH_CASE = wx.NewId()
ID_WHOLE_WORD = wx.NewId()
ID_REGEX = wx.NewId()
ID_RECURSE = wx.NewId()
ID_COUNT = wx.NewId()
ID_FIND_ALL = wx.NewId()
ID_REPLACE_ALL = wx.NewId()
ID_OPTION_CHANGE = wx.NewId()
ID_CHOOSE_DIR = wx.NewId()
ID_DIR_BOX = wx.NewId()

_ = wx.GetTranslation

#--------------------------------------------------------------------------#

# Events Definitions

# Find dialog has been closed
edEVT_FIND_CLOSE = wx.NewEventType()
EVT_FIND_CLOSE = wx.PyEventBinder(edEVT_FIND_CLOSE, 1)

# Find cutton clicked
edEVT_FIND = wx.NewEventType()
EVT_FIND = wx.PyEventBinder(edEVT_FIND, 1)

# Find cutton clicked again with the same search string
edEVT_FIND_NEXT = wx.NewEventType()
EVT_FIND_NEXT = wx.PyEventBinder(edEVT_FIND_NEXT, 1)

# Find All button clicked
edEVT_FIND_ALL = wx.NewEventType()
EVT_FIND_ALL = wx.PyEventBinder(edEVT_FIND_ALL, 1)

# Replace button clicked
edEVT_REPLACE = wx.NewEventType()
EVT_REPLACE = wx.PyEventBinder(edEVT_REPLACE, 1)

# Replace button clicked
edEVT_COUNT = wx.NewEventType()
EVT_COUNT = wx.PyEventBinder(edEVT_COUNT, 1)

# Replace All button clicked
edEVT_REPLACE_ALL = wx.NewEventType()
EVT_REPLACE_ALL = wx.PyEventBinder(edEVT_REPLACE_ALL, 1)

# Find option has changed
edEVT_OPTION_CHANGED = wx.NewEventType()
EVT_OPTION_CHANGED = wx.PyEventBinder(edEVT_OPTION_CHANGED, 1)

# Convenience for generating events
_EVENT_MAP = { wx.ID_FIND : edEVT_FIND,
               wx.ID_REPLACE : edEVT_REPLACE,
               ID_COUNT : edEVT_COUNT,
               ID_FIND_ALL : edEVT_FIND_ALL,
               ID_REPLACE_ALL : edEVT_REPLACE_ALL,
               ID_OPTION_CHANGE : edEVT_OPTION_CHANGED }

class FindEvent(wx.PyCommandEvent):
    """Event sent by the FindReplaceDialog that contains all
    options of the FindReplaceData and requested action of the
    find dialog

    """
    def __init__(self, etype, eid=wx.ID_ANY, flags=0):
        """Creates the event object
        @keyword flags: Find/Replace flags

        """
        super(FindEvent, self).__init__(etype, eid)

        # Attributes
        self._flags = flags
        self._loc = 0
        self._find = u''
        self._replace = u''
        self._dir = u''
        self._filters = None
        self._count = 0

    def GetCount(self):
        """Get the number of matches
        @return: int

        """
        return self._count

    def GetDirectory(self):
        """Get the directory of files to search in
        @return: string

        """
        return self._dir

    def GetFileFilters(self):
        """Get the filter to search with
        @return: list or None

        """
        return self._filters

    def GetFindString(self):
        """Get the find string
        @return: string

        """
        return self._find

    def GetFlags(self):
        """Get the search flags
        @return: long

        """
        return self._flags

    def GetReplaceString(self):
        """Set the find String
        @return: string

        """
        return self._replace

    def GetSearchType(self):
        """Get the type of search (current buffer, open documents, ect...)
        @return: int (see LOCATION_* flags)

        """
        return self._loc

    def SetCount(self, count):
        """Set the count
        @param count: int

        """
        self._count = count

    def SetDirectory(self, directory):
        """Set the directory of files to search in
        @param directory: string

        """
        self._dir = directory

    def SetFileFilters(self, filters):
        """Set the file filters to use for a Search In Files event
        @param filters: string or list of strings

        """
        if isinstance(filters, basestring):
            self._filters = filters.split()
        else:
            self._filters = filters

        if self._filters is not None:
            self._filters = [ f.strip() for f in self._filters ]

    def SetFindString(self, find):
        """Set the find String
        @param find: string

        """
        self._find = find

    def SetFlags(self, flags):
        """Returns the value from the event.
        @return: the value of this event

        """
        self._flags = flags

    def SetReplaceString(self, rstring):
        """Set the find String
        @param rstring: string

        """
        self._replace = rstring

    def SetSearchType(self, stype):
        """Set the type of search (current buffer, open documents, ect...)
        @param stype: int (see LOCATION_* flags)

        """
        self._loc = stype

    def IsMatchCase(self):
        """Is this a match case search
        @return: bool

        """
        return bool(self._flags & AFR_MATCHCASE)

    def IsRecursive(self):
        """Is the search option for recursive directory search enabled
        @return: bool

        """
        return bool(self._flags & AFR_RECURSIVE)

    def IsRegEx(self):
        """Is RegEx enabled in the dialog
        @return: bool

        """
        return bool(self._flags & AFR_REGEX)

    def IsWholeWord(self):
        """Is this a whole word search
        @return: bool

        """
        return bool(self._flags & AFR_WHOLEWORD)

    def IsUp(self):
        """Is the search searching up
        @return: bool

        """
        return bool(self._flags & AFR_UP)

#--------------------------------------------------------------------------#

def AdvFindReplaceDlg(parent, fdata, title, style=AFR_STYLE_FINDDIALOG):
    """Advanced FindReplaceDialog. Create and return the requested dialog type
    @param parent: parent
    @param fdata: FindReplaceData
    @param title: Dialog Title. Pass a single string to set the title for both
                  modes of the dialog or a tuple of two strings to set the title
                  for (find, replace) modes.
    @keyword style: Dialog Style and type
    @note: this is a function not a class

    """
    if style & AFR_STYLE_NON_FLOATING:
        dlg = FindReplaceDlg(parent, fdata, title, style)
    else:
        dlg = MiniFindReplaceDlg(parent, fdata, title, style)

    return dlg

#--------------------------------------------------------------------------#

class FindReplaceDlgBase:
    """Delegate mixin base class for deriving FindReplaceDialogs, delegates
    calls to the widgets and containers that make up the dialog.
    @note: The mixin must be initialized after the class its being mixed into

    """
    def __init__(self, parent, fdata, title, style=AFR_STYLE_FINDDIALOG):
        """Create the base object
        @param title: string or tuple (findstr, replacestr)

        """
        # Attributes
        if isinstance(title, basestring):
            self._ftitle = title
            self._rtitle = title
        else:
            self._ftitle, self._rtitle = title[:2]

        self._box = FindBox(self, fdata, style=style)
        self._panel = self._box.GetWindow()
        self._accl = wx.AcceleratorTable([(wx.ACCEL_NORMAL, wx.WXK_ESCAPE, wx.ID_CLOSE),])
        self.SetAcceleratorTable(self._accl)
        
        # Layout
        self.__DoLayout()
        tmp_title = self._ftitle
        if style & AFR_STYLE_REPLACEDIALOG:
            tmp_title = self._rtitle
        self.SetTitle(tmp_title)

        # Event handlers
        self.Bind(_EVT_MODE_CHANGE, self._OnModeChange)
        self.Bind(_EVT_DO_CLOSE_DLG, lambda evt: self._SendCloseEvent())
        self.Bind(wx.EVT_MENU, lambda evt: self._SendCloseEvent(), id=wx.ID_CLOSE)
        self.Bind(wx.EVT_SET_FOCUS, self._OnSetFocus)
        self.Bind(wx.EVT_SHOW, self._OnShow)

    def __DoLayout(self):
        """Layout the dialog"""
        vsizer = wx.BoxSizer(wx.VERTICAL)
        vsizer.Add(self._box, 1, wx.EXPAND)
        self.SetSizer(vsizer)
        self.SetAutoLayout(True)
        self.Fit()

    def _SendCloseEvent(self):
        """Send a dialog close event and hide the dialog"""
        data = self._panel.GetData()
        data.SetFindString(self._panel._ftxt.GetValue())
        data.SetReplaceString(self._panel._rtxt.GetValue())
        evt = FindEvent(edEVT_FIND_CLOSE, self.GetId())
        self.Hide()
        wx.PostEvent(self.GetParent(), evt)

    def _OnModeChange(self, evt):
        """Update the the dialog when the mode changes"""
        self.Fit()
        title = self._ftitle
        if self.GetDialogMode() != AFR_STYLE_FINDDIALOG:
            title = self._rtitle
        self.SetTitle(title)

    def _OnSetFocus(self, evt):
        if self and self._panel:
            self._panel.SetFocus()
            evt.Skip()

    def _OnShow(self, evt):
        if self and self._panel:
            self._panel.SetFocus()

    def GetData(self):
        """Get the FindReplaceData used by this dialog"""
        return self._panel.GetData()

    def GetDialogMode(self):
        """Get the current mode of the dialog
        @return: AFR_STYLE_FINDDIALOG or AFR_STYLE_REPLACEDIALOG

        """
        return self._panel.GetPanelMode()

    def GetFileFilters(self):
        """Get the file filters field value
        @return: string

        """
        return self._panel.GetFileFilters()

    def GetLookinChoices(self):
        """Get the set choices from the looking choice list
        @return: list of strings

        """
        return self._panel.GetLookinChoices()

    def GetLookinSelection(self):
        """Get the index of the selected item in the lookin choice list
        @return: int

        """
        return self._panel.GetLookinSelection()

    def RefreshFindReplaceFields(self):
        """Refresh the values of the Find and Replace fields with the
        values that are currently in the FindReplaceData.

        """
        data = self.GetData()
        self._panel.SetFindString(data.GetFindString())
        self._panel.SetReplaceString(data.GetReplaceString())

    def RefreshFindOptions(self):
        """Refresh the find options controls based on the current
        values of the FindData owned by this window.

        """
        self._panel.RefreshControls()

    def SetData(self, data):
        """Set the dialogs FindReplaceData
        @param data: FindReplaceData
        @note: causes updates in dialog

        """
        self._panel.SetData(data)

    def SetDialogMode(self, mode):
        """Set the dialog mode between find and find/replace
        @param mode: AFR_STYLE_FINDDIALOG or AFR_STYLE_REPLACEDIALOG

        """
        self._box.SetBoxMode(mode == AFR_STYLE_FINDDIALOG)

    def SetFileFilters(self, filters):
        """Set the file filters field value
        @param filters: string

        """
        self._panel.SetFileFilters(filters)

    def SetFindBitmap(self, bmp):
        """Set the find Bitmap
        @param bmp: wx.Bitmap

        """
        self._box.SetFindBitmap(bmp)

    def SetFlag(self, flags):
        """Set a search dialog flag.
        @param flags: AFR_*

        """
        self._panel.SetFlag(flags)

    def SetFlags(self, flags):
        """Set the search dialog flags.
        @param flags: bitmask of AFR_ values

        """
        self._panel.SetFlags(flags)

    def SetLookinChoices(self, paths):
        """Set the looking choices
        @param paths: list of strings

        """
        self._panel.SetLookinChoices(paths)

    def SetLookinPath(self, path):
        """Set the lookin path, adding it to the collection if it is
        not in there.
        @param path: string (path of directory)

        """
        idx = self._panel.AddLookinPath(path)
        self.SetLookinSelection(idx)

    def SetLookinSelection(self, sel):
        """Set the looking choices selection
        @param sel: int

        """
        self._panel.SetLookinSelection(sel)

    def SetReplaceBitmap(self, bmp):
        """Set the replace bitmap
        @param bmp: wx.Bitmap

        """
        self._box.SetReplaceBitmap(bmp)

    def SetFindDirectory(self, path):
        """Set the directory selection for find in files
        @param path: path to set for lookin data

        """
        self._panel.SetLookinSelection(path)

    def SetFindString(self, query):
        """Set the find controls search string
        @param query: string

        """
        self._panel.SetFindString(query)

    def SetReplaceString(self, rstring):
        """Set the replace controls search string
        @param rstring: string

        """
        self._panel.SetReplaceString(rstring)

#--------------------------------------------------------------------------#

class MiniFindReplaceDlg(wx.MiniFrame, FindReplaceDlgBase):
    """Advanced Find Replace Dialog this version of the dialog uses a 
    MiniFrame that will float on top of its parent

    """
    def __init__(self, parent, fdata, title, style=AFR_STYLE_FINDDIALOG):
        """Create the Dialog
        @param parent: Parent Window
        @param fdata: wx.FindReplaceData
        @param title: Dialog Title. Pass a single string to set the title for
                      both modes of the dialog or a tuple of two strings to set
                      the title for (find, replace) modes.
        @keyword style: Dialog Style

        """
        wx.MiniFrame.__init__(self, parent, wx.ID_ANY, u'',
                              style=wx.DEFAULT_DIALOG_STYLE)
        FindReplaceDlgBase.__init__(self, parent, fdata, title, style)

        # Event handlers
        self.Bind(wx.EVT_CLOSE, lambda evt: self._SendCloseEvent())

#--------------------------------------------------------------------------#

class FindReplaceDlg(wx.Dialog, FindReplaceDlgBase):
    """Advanced Find Replace Dialog this version of the dialog uses a standard
    dialog window.

    """
    def __init__(self, parent, fdata, title, style=AFR_STYLE_FINDDIALOG):
        """Create the Dialog
        @param parent: Parent Window
        @param fdata: wx.FindReplaceData
        @param title: Dialog Title can be a string to set the title for both
                      modes or a tuple of two strings to set the (find, replace)
                      mode titles.
        @keyword style: Dialog Style

        """
        wx.Dialog.__init__(self, parent, wx.ID_ANY, u'',
                           style=wx.DEFAULT_DIALOG_STYLE)
        FindReplaceDlgBase.__init__(self, parent, fdata, title, style)

        # Event handlers
        self.Bind(wx.EVT_CLOSE, lambda evt: self._SendCloseEvent())

#--------------------------------------------------------------------------#

class FindBox(ctrlbox.ControlBox):
    """Container box that allows for switching the L{FindPanel}'s mode
    through the ui. Contains a L{FindPanel} and two PlateButtons for switching
    the mode.

    """
    def __init__(self, parent, fdata, id=wx.ID_ANY, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=AFR_STYLE_FINDDIALOG,
                 name=FindBoxName):
        """Create the container box
        @param fdata: wx.FindReplaceData

        """
        super(FindBox, self).__init__(parent, id, pos, size,
                                      wx.TAB_TRAVERSAL|wx.NO_BORDER, name)

        # Attributes
        self._fpanel = FindPanel(self, fdata, style=style)
        ctrlbar = ctrlbox.ControlBar(self, style=ctrlbox.CTRLBAR_STYLE_GRADIENT)
        bmp = wx.ArtProvider.GetBitmap(wx.ART_FIND, wx.ART_MENU)
        self.find = platebtn.PlateButton(ctrlbar, label=_("Find"), bmp=bmp,
                                         style=platebtn.PB_STYLE_NOBG)
        bmp = wx.ArtProvider.GetBitmap(wx.ART_FIND_AND_REPLACE, wx.ART_MENU)
        self.replace = platebtn.PlateButton(ctrlbar, label=_("Replace"),
                                            bmp=bmp,
                                            style=platebtn.PB_STYLE_NOBG)

        # Setup
        if wx.Platform == '__WXGTK__':
            ctrlbar.SetWindowStyle(ctrlbox.CTRLBAR_STYLE_BORDER_BOTTOM)

        ctrlbar.SetVMargin(2, 2)
        ctrlbar.AddControl(self.find, wx.ALIGN_LEFT)
        ctrlbar.AddControl(self.replace, wx.ALIGN_LEFT)
        self.SetControlBar(ctrlbar)
        self.SetWindow(self._fpanel)

        if style & AFR_STYLE_NO_MODE_SELECT:
            self.GetControlBar().Hide()

        # Event Handlers
        self.Bind(wx.EVT_BUTTON, self.OnButton)

    def OnButton(self, evt):
        """Change the mode
        @param evt: wx.EVT_BUTTON

        """
        eobj = evt.GetEventObject()
        if eobj in (self.find, self.replace):
            self.SetBoxMode(eobj == self.find)
        else:
            evt.Skip()

    def SetFindBitmap(self, bmp):
        """Set the bitmap of the Find Button
        @param bmp: wx.Bitmap

        """
        self.find.SetBitmap(bmp)
        self.GetControlBar().Layout()

    def SetBoxMode(self, find=True):
        """Set the box to find or find/replace mode
        @keyword find: set to find mode

        """
        self._fpanel.SetFindMode(find)
        self.Layout()
        evt = _AdvFindDlgInternalEvent(self.GetId(), _edEVT_MODE_CHANGE)
        wx.PostEvent(self.GetParent(), evt)

    def SetReplaceBitmap(self, bmp):
        """Set the bitmap of the Replace Button
        @param bmp: wx.Bitmap

        """
        self.replace.SetBitmap(bmp)
        self.GetControlBar().Layout()

#--------------------------------------------------------------------------#

class FindPanel(wx.Panel):
    """Find controls panel"""
    def __init__(self, parent, fdata, id=wx.ID_ANY, 
                 pos=wx.DefaultPosition, size=wx.DefaultSize,
                 style=AFR_STYLE_FINDDIALOG, name=FindPanelName):
        """Create the panel
        @param fdata: wx.FindReplaceData

        """
        super(FindPanel, self).__init__(parent, id, pos, size,
                                        wx.TAB_TRAVERSAL|wx.NO_BORDER, name)

        # Attributes
        # TODO: change to editable combo box when wxMac has native widget
        #       so that we can set a search history to choose from.       
        self._mode = style
        self._ftxt = wx.TextCtrl(self, value=fdata.GetFindString())
        self._rtxt = wx.TextCtrl(self, value=fdata.GetReplaceString())
        locations = [_("Current Document"), _("Selected Text"),
                     _("Open Documents"), _("Current Directory")]
        self._lookin = wx.Choice(self, ID_LOOKIN, choices=locations)
        self._lookin.SetSelection(0)
        self._filterlbl = wx.StaticText(self, label=_("File Filters:"))
        self._filters = None # Created in __DoLayout
        self._sizers = dict()
        self._paths = dict()
        self._fdata = fdata
        self._lastSearch = u''

        # Layout
        self.__DoLayout()
        self.SetInitialSize()
        self._ConfigureControls()

        # Setup
        self.SetFindMode(not (self._mode & AFR_STYLE_REPLACEDIALOG))

        # Event Handlers
        self.Bind(wx.EVT_BUTTON, self.OnChooseDir, id=ID_CHOOSE_DIR)
        self.Bind(wx.EVT_BUTTON,
                  lambda evt: self.FireEvent(evt.GetId()) or evt.Skip())
        self.Bind(wx.EVT_CHECKBOX, self.OnOption)
        self.Bind(wx.EVT_RADIOBUTTON, self.OnOption)
        self.Bind(wx.EVT_CHOICE, self.OnChoice, id=ID_LOOKIN)
        for bid in (wx.ID_FIND, wx.ID_REPLACE, ID_COUNT,
                    ID_FIND_ALL, ID_REPLACE_ALL):
            self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateUI, id=bid)
        self.Bind(wx.EVT_SET_FOCUS, lambda evt: self.__SetFocus())
        self._ftxt.Bind(wx.EVT_SET_FOCUS, lambda evt: self._ftxt.SelectAll())
        self._rtxt.Bind(wx.EVT_SET_FOCUS, lambda evt: self._rtxt.SelectAll())

        # Key handling HACK for windows
        if wx.Platform == '__WXMSW__':
            for child in self.GetChildren():
                child.Bind(wx.EVT_KEY_UP, self._OnKeyUp)

    def __SetFocus(self):
        """Set the focus to the find box"""
        self._ftxt.SetFocus()
        self._ftxt.SelectAll()

    def __DoLayout(self):
        """Layout the panel"""
        hsizer = wx.BoxSizer(wx.HORIZONTAL)
        vsizer = wx.BoxSizer(wx.VERTICAL)

        # Top Section
        topvsizer = wx.BoxSizer(wx.VERTICAL)

        # Platform dependant labels
        if wx.Platform == '__WXMSW__':
            findlbl = wx.StaticText(self, ID_FIND_LBL, _("Find what") + u":")
        else:
            findlbl = wx.StaticText(self, ID_FIND_LBL, _("Find") + u":")

        # Search Field
        fhsizer = wx.BoxSizer(wx.HORIZONTAL)
        fhsizer.Add(self._ftxt, 1, wx.EXPAND)
        topvsizer.AddMany([(findlbl, 0, wx.ALIGN_LEFT), ((3, 3), 0),
                          (fhsizer, 0, wx.EXPAND)])

        # Replace field
        rhsizer = wx.BoxSizer(wx.HORIZONTAL)
        rhsizer.Add(self._rtxt, 1, wx.EXPAND)
        rlbl = wx.StaticText(self, ID_REPLACE_LBL, _("Replace with") + u":")
        self._sizers[ID_REPLACE_LBL] = wx.BoxSizer(wx.VERTICAL)
        self._sizers[ID_REPLACE_LBL].AddMany([((5, 5), 0),
                                             (rlbl, 0, wx.ALIGN_CENTER_VERTICAL),
                                             ((3, 3), 0), (rhsizer, 0, wx.EXPAND)])
        topvsizer.AddMany([(self._sizers[ID_REPLACE_LBL], 0, wx.EXPAND),
                           ((5, 5), 0)])

        # Look in field
        self._sizers['look'] = wx.BoxSizer(wx.VERTICAL)
        li_sz = wx.BoxSizer(wx.HORIZONTAL)
        dirbtn = wx.Button(self, ID_CHOOSE_DIR, u"...", style=wx.BU_EXACTFIT)
        dirbtn.SetToolTipString(_("Choose Folder"))
        li_sz.AddMany([(self._lookin, 1, wx.ALIGN_CENTER_VERTICAL), ((5, 5), 0),
                       (dirbtn, 0, wx.ALIGN_CENTER_VERTICAL)])
        li_lbl = wx.StaticText(self, label=_("Look in") + u":")
        self._sizers['look'].AddMany([(li_lbl, 0, wx.ALIGN_LEFT),
                                      ((3, 3), 0),
                                      (li_sz, 0, wx.EXPAND),
                                      ((5, 5), 0)])
        topvsizer.Add(self._sizers['look'], 0, wx.EXPAND)

        # Search Direction Box
        self._sizers['dir'] = wx.BoxSizer(wx.VERTICAL)
        dbox = wx.StaticBox(self, id=ID_DIR_BOX, label=_("Direction"))
        dboxsz = wx.StaticBoxSizer(dbox, wx.HORIZONTAL)
        dboxsz.AddMany([(wx.RadioButton(self, wx.ID_UP, _("Up")), 0),
                        ((20, 5), 0),
                        (wx.RadioButton(self, wx.ID_DOWN, _("Down")), 0),
                        ((5, 5), 1)])
        self._sizers['dir'].AddMany([((5, 5), 0), (dboxsz, 0, wx.EXPAND)])

        # Search Options Box
        self._sizers['opt'] = wx.BoxSizer(wx.VERTICAL)
        statbox = wx.StaticBox(self, label=_("Find Options"))
        sboxsz = wx.StaticBoxSizer(statbox, wx.VERTICAL)
        for cid, clbl in [(ID_MATCH_CASE, _("Match case")),
                          (ID_WHOLE_WORD, _("Whole word")),
                          (ID_REGEX, _("Regular expression")),
                          (ID_RECURSE, _("Search Recursively"))]:
            sboxsz.AddMany([((3, 3), 0), (wx.CheckBox(self, cid, clbl), 0)])

        # File Filters
        self._sizers['filter'] = wx.BoxSizer(wx.VERTICAL)
        self._filters = wx.TextCtrl(self)
        tt_txt = _("Wildcard shell patterns for matching files (*.txt *.html).")
        self._filters.SetToolTipString(tt_txt)

        # Disable spell checking on mac for this control
        if wx.Platform == '__WXMAC__':
            self._filters.MacCheckSpelling(False)

        f_sz = wx.BoxSizer(wx.HORIZONTAL)
        f_sz.Add(self._filters, 1, wx.EXPAND)
        self._sizers['filter'].AddMany([(self._filterlbl, 0, wx.ALIGN_LEFT),
                                        ((3, 3), 0), (f_sz, 0, wx.EXPAND),
                                        ((5, 5), 0)])
        sboxsz.AddMany([((3, 3), 0), (self._sizers['filter'], 0, wx.EXPAND)])

        self._sizers['opt'].AddMany([((5, 5), 0), (sboxsz, 0, wx.EXPAND)])

        # Buttons
        bsizer = wx.BoxSizer(wx.HORIZONTAL)
        self._sizers['fspacer'] = bsizer.Add((100, 1), 1)
        self._sizers['frspacer'] = bsizer.Add((50, 1), 1)
        for bid, blbl in [(wx.ID_FIND, _("Find")),
                          (wx.ID_REPLACE, _("Replace")),
                          (ID_COUNT, _("Count")),
                          (ID_FIND_ALL, _("Find All")),
                          (ID_REPLACE_ALL, _("Replace All"))]:
            self._sizers[bid] = wx.BoxSizer(wx.HORIZONTAL)
            self._sizers[bid].Add((3, 3), 0)
            self._sizers[bid].Add(wx.Button(self, bid, blbl), 0, wx.ALIGN_RIGHT)
            bsizer.Add(self._sizers[bid], 0)
        self.FindWindowById(wx.ID_FIND).SetDefault()

        # Final Layout
        vsizer.AddMany([((5, 5), 0), (topvsizer, 0, wx.EXPAND),
                        (self._sizers['dir'], 0, wx.EXPAND),
                        (self._sizers['opt'], 0, wx.EXPAND), ((10, 10), 0),
                        (bsizer, 0), ((10, 10), 0)])
        hsizer.AddMany([((10, 10), 0), (vsizer, 0, wx.EXPAND), ((10, 10), 0)])
        self.SetSizer(hsizer)
        self.SetAutoLayout(True)

    def _ConfigureControls(self):
        """Configure the state of the controls based on the FindReplaceData"""
        flags = self._fdata.GetFlags()
        self._ftxt.SelectAll()
        self.FindWindowById(ID_MATCH_CASE).SetValue(flags & AFR_MATCHCASE)
        self.FindWindowById(ID_WHOLE_WORD).SetValue(flags & AFR_WHOLEWORD)
        self.FindWindowById(ID_REGEX).SetValue(flags & AFR_REGEX)
        self.FindWindowById(wx.ID_DOWN).SetValue(not (flags & AFR_UP))
        self.FindWindowById(wx.ID_UP).SetValue(flags & AFR_UP)
        self.ShowLookinCombo(not (flags & AFR_NOLOOKIN))
        self.ShowDirectionBox(not (flags & AFR_NOUPDOWN))
        self.ShowOptionsBox(not (flags & AFR_NOOPTIONS))
        self.FindWindowById(ID_WHOLE_WORD).Enable(not (flags & AFR_NOWHOLEWORD))
        self.FindWindowById(ID_MATCH_CASE).Enable(not (flags & AFR_NOMATCHCASE))
        self.FindWindowById(ID_REGEX).Enable(not (flags & AFR_NOREGEX))
        in_files = bool(self._lookin.GetSelection() >= LOCATION_MAX)
        recurse = self.FindWindowById(ID_RECURSE)
        recurse.SetValue(flags & AFR_RECURSIVE)
        recurse.Enable(in_files)
        self.ShowFileFilters(in_files and not (flags & AFR_NOFILTER))

    def _OnKeyUp(self, evt):
        """Check if the dialog should be closed
        @note: msw only

        """
        if evt.GetKeyCode() == wx.WXK_ESCAPE:
            evt = _AdvFindDlgInternalEvent(self.GetId(), _edEVT_DO_CLOSE_DLG)
            wx.PostEvent(self.GetTopLevelParent(), evt)
        evt.Skip()

    def _ShowButtons(self, find=True):
        """Toggle the visiblity of a button set
        @param find: Show Find Buttons or Show Replace Buttons

        """
        if find:
            show = [wx.ID_FIND, ID_COUNT, ID_FIND_ALL, 'fspacer']
            hide = [wx.ID_REPLACE, ID_REPLACE_ALL, 'frspacer']
        else:
            show = [wx.ID_REPLACE, ID_REPLACE_ALL, 'frspacer']
            hide = [ID_FIND_ALL, ID_COUNT, 'fspacer']

        # Hide extra buttons as per configured preference
        flags = self._fdata.GetFlags()
        if flags & AFR_NO_COUNT and ID_COUNT in show:
            show.remove(ID_COUNT)
            hide.append(ID_COUNT)

        if flags & AFR_NO_ALL_BTN:
            if ID_FIND_ALL in show:
                show.remove(ID_FIND_ALL)
                hide.append(ID_FIND_ALL)
            if ID_REPLACE_ALL in show:
                show.remove(ID_REPLACE_ALL)
                hide.append(ID_REPLACE_ALL)

        for ctrl in show:
            if isinstance(ctrl, basestring):
                self._sizers[ctrl].Show(True)
            else:
                self._sizers[ctrl].ShowItems(True)

        for ctrl in hide:
            if isinstance(ctrl, basestring):
                self._sizers[ctrl].Show(False)
            else:
                self._sizers[ctrl].ShowItems(False)

    def _UpdateContext(self):
        """Update available dialog options based on the selected search context

        """
        self._UpdateDefaultBtn()
        in_files = bool(self._lookin.GetSelection() >= LOCATION_MAX)
        self.FindWindowById(ID_RECURSE).Enable(in_files)
        flags = self._fdata.GetFlags()

        # Disable direction settings when searcing in file since they
        # are ignored anyway.
        for cid in (wx.ID_UP, wx.ID_DOWN, ID_DIR_BOX):
            self.FindWindowById(cid).Enable(not in_files)

        # Only update visibility of file filter field if it is enabled
        if not (flags & AFR_NOFILTER):
            self.ShowFileFilters(in_files)

    def _UpdateDefaultBtn(self):
        """Change the default button depending on what the search context
        has been changed to.

        """
        find = self.FindWindowById(wx.ID_FIND)
        find_all = self.FindWindowById(ID_FIND_ALL)
        replace = self.FindWindowById(wx.ID_REPLACE)
        replace_all = self.FindWindowById(ID_REPLACE_ALL)
        count = self.FindWindowById(ID_COUNT)

        lookin = self._lookin.GetSelection()
        if  lookin > LOCATION_IN_SELECTION:
            if self._mode == AFR_STYLE_FINDDIALOG:
                find_all.SetDefault()
            else:
                replace_all.SetDefault()
            find.Disable()
            replace.Disable()
            count.Disable()
        elif lookin == LOCATION_IN_SELECTION:
            find.Disable()
            replace.Disable()
            replace_all.SetDefault()
        else:
            find.SetDefault()
            find.Enable()
            replace.Enable()
            count.Enable()

    #------------------------------------------------------#

    def AddLookinPath(self, path):
        """Add a path to the lookin path collection
        @param path: string
        @return: index of the items location

        """
        if not len(path):
            return None

        # If its a path we already have then just return its index
        if path in self._paths.values():
            for idx, pname in self._paths.iteritems():
                if path == pname:
                    return idx

        # Get the short directory name
        the_dir = u''
        for dname in reversed(path.split(os.sep)):
            if len(dname):
                the_dir = dname
                break

        self._paths[self._lookin.GetCount()] = path
        self._lookin.Append(the_dir)
        rval = self._lookin.GetCount() - 1

        return rval

    def ClearFlag(self, flag):
        """Clear a search flag
        @param flag: AFR_*

        """
        flags = self._fdata.GetFlags()
        flags &= ~flag
        self.SetFlags(flags)

    def FireEvent(self, eid):
        """Fire an event
        @param eid: Event id

        """
        etype = _EVENT_MAP.get(eid, None)
        query = self._ftxt.GetValue()
        if eid == wx.ID_FIND:
            if self._lastSearch == query:
                etype = edEVT_FIND_NEXT
            self._lastSearch = query
            self._fdata.SetFindString(query)

        if etype is not None:
            evt = FindEvent(etype, eid, self._fdata.GetFlags())
            evt.SetEventObject(self)
            lookin_idx = self._lookin.GetSelection()
            stype = min(LOCATION_IN_FILES, max(LOCATION_CURRENT_DOC, lookin_idx))
            evt.SetSearchType(stype)
            evt.SetFindString(query)

            if self._mode & AFR_STYLE_REPLACEDIALOG:
                rstring = self._rtxt.GetValue()
                self._fdata.SetReplaceString(rstring)
                evt.SetReplaceString(rstring)
            else:
                evt.SetReplaceString(None)

            if stype >= LOCATION_IN_FILES or stype == LOCATION_IN_CURRENT_DIR:
                evt.SetFileFilters(self.GetFileFilters())
            if stype >= LOCATION_IN_FILES:
                # For IN_CURRENT_DIR it is up to client to determine directory
                # based on what current means to the application.
                evt.SetDirectory(self._paths.get(lookin_idx, u''))

            wx.PostEvent(self.GetParent(), evt)
            return True
        else:
            return False

    def GetData(self):
        """Get the FindReplaceData used by this panel
        @return: wx.FindReplaceData

        """
        return self._fdata

    def GetFileFilters(self):
        """Get the currently set file filters
        @return: string

        """
        return self._filters.GetValue()

    def GetLookinChoices(self):
        """Get the looking choices
        @return: list of strings

        """
        choices = list()
        for key in sorted(self._paths.keys()):
            choices.append(self._paths[key])
        return choices

    def GetLookinSelection(self):
        """Get the index of the currently selected lookin choice
        @return: int

        """
        return self._lookin.GetSelection()

    def GetPanelMode(self):
        """Get the current display mode of the panel
        @return: AFR_STYLE_FINDDIALOG or AFR_STYLE_REPLACEDIALOG

        """
        return self._mode

    def OnChoice(self, evt):
        """Handle choice control events
        @param evt: wx.EVT_CHOICE

        """
        e_id = evt.GetId()
        if e_id == ID_LOOKIN:
            self._UpdateContext()
            choice = self._lookin.GetSelection()
            if choice >= LOCATION_IN_FILES:
                tts = self._paths.get(choice, u'')
                self._lookin.SetToolTipString(tts)
            else:
                self._lookin.SetToolTipString(u'')
        else:
            evt.Skip()

    def OnChooseDir(self, evt):
        """Open the choose directory dialog for selecting what
        path to do a search in files in.
        @param evt: wx.EVT_BUTTON

        """
        if evt.GetId() == ID_CHOOSE_DIR:
            dlg = wx.DirDialog(self, _("Choose Search Folder"))
            if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPath()
                if path is not None and len(path):
                    idx = self.AddLookinPath(path)
                    self.SetLookinSelection(idx)
                self._UpdateContext()
            dlg.Destroy()
        else:
            evt.Skip()

    def OnOption(self, evt):
        """Update search flags
        @param evt: wx.EVT_CHECKBOX

        """
        fmap = { ID_MATCH_CASE : AFR_MATCHCASE,
                 ID_WHOLE_WORD : AFR_WHOLEWORD,
                 ID_REGEX : AFR_REGEX,
                 ID_RECURSE : AFR_RECURSIVE,
                 wx.ID_UP : AFR_UP }
        eid = evt.GetId()
        eobj = evt.GetEventObject()
        if eid in fmap:
            if eobj.GetValue():
                self.SetFlag(fmap[eid])
            else:
                self.ClearFlag(fmap[eid])
            self.FireEvent(ID_OPTION_CHANGE)
        elif eid == wx.ID_DOWN:
            self.ClearFlag(fmap[wx.ID_UP])
            self.FireEvent(ID_OPTION_CHANGE)
        else:
            evt.Skip()

    def OnUpdateUI(self, evt):
        """Enable and disable buttons depending on state of find entry box.
        @param evt: wx.UpdateUIEvent

        """
        # TODO: support count in files?
        if evt.GetId() in (wx.ID_FIND, ID_COUNT, wx.ID_REPLACE) and \
           self._lookin.GetSelection() > LOCATION_CURRENT_DOC:
            evt.Enable(False)
        else:
            txt = len(self._ftxt.GetValue())
            evt.Enable(txt)

    def SetFindMode(self, find=True):
        """Set the mode of the dialog Replace
        @param find: Set Find Mode or Replace Mode

        """
        self._rtxt.Show(not find)
        self._sizers[ID_REPLACE_LBL].ShowItems(not find)
        if find:
            self._mode = AFR_STYLE_FINDDIALOG
        else:
            self._mode = AFR_STYLE_REPLACEDIALOG

        self._UpdateDefaultBtn()
        self._ShowButtons(find)
        self.Layout()

    def SetFindString(self, query):
        """Set the find fields string
        @param query: string

        """
        self._ftxt.SetValue(query)
        self._fdata.SetFindString(query)

    def RefreshControls(self):
        """Refresh the state of the controls from the current FindData."""
        self._ConfigureControls()

    def SetData(self, data):
        """Set the FindReplaceData and update the dialog with that data
        @param data: wxFindReplaceData

        """
        self._fdata.Destroy()
        self._fdata = None
        self._fdata = data
        self._ConfigureControls()

    def SetFileFilters(self, filters):
        """Set the file filters field values
        @param filters: string

        """
        self._filters.SetValue(filters)

    def SetFlag(self, flag):
        """Set a search flag
        @param flag: AFR_* flag value

        """
        flags = self._fdata.GetFlags()
        flags |= flag
        self.SetFlags(flags)

    def SetFlags(self, flags):
        """Set the search flags
        @param flags: Bitmask of AFR_* values

        """
        self._fdata.SetFlags(flags)
        self._ConfigureControls()

    def SetLookinChoices(self, paths):
        """Set the looking choices
        @param paths: list of strings

        """
        for path in paths:
            self.AddLookinPath(path)

    def SetLookinSelection(self, idx):
        """Set the selection of the lookin control.
        @param idx: int

        """
        if idx <= self._lookin.GetCount():
            self._lookin.SetSelection(idx)

            # If the selection is a Look in Files make sure the filter
            # field is shown
            flags = self._fdata.GetFlags()
            in_files = bool(idx >= LOCATION_MAX)
            self.ShowFileFilters(in_files and not (flags & AFR_NOFILTER))

    def SetReplaceString(self, rstring):
        """Set the replace fields string
        @param rstring: string

        """
        self._rtxt.SetValue(rstring)
        self._fdata.SetReplaceString(rstring)

    def ShowDirectionBox(self, show=True):
        """Show or hide the Direction group box
        @keyword show: bool

        """
        if 'dir' in self._sizers:
            self._sizers['dir'].ShowItems(show)
            self.Layout()

    def ShowFileFilters(self, show=True):
        """Show or hide the File Filters filed
        @keyword show: bool

        """
        if 'filter' in self._sizers:
            self._sizers['filter'].ShowItems(show)
            self.Layout()

            # Require additional resizing
            evt = _AdvFindDlgInternalEvent(self.GetId(), _edEVT_MODE_CHANGE)
            wx.PostEvent(self.GetTopLevelParent(), evt)

    def ShowLookinCombo(self, show=True):
        """Show the lookin choice and directory chooser control
        @keyword show: bool

        """
        if 'look' in self._sizers:
            self._sizers['look'].ShowItems(show)
            self.Layout()

    def ShowOptionsBox(self, show=True):
        """Show the find options group box
        @keyword show: bool

        """
        if 'opt' in self._sizers:
            self._sizers['opt'].ShowItems(show)
            self.Layout()

#--------------------------------------------------------------------------#
# Private Module Api

_edEVT_MODE_CHANGE = wx.NewEventType()
_EVT_MODE_CHANGE = wx.PyEventBinder(_edEVT_MODE_CHANGE, 1)

_edEVT_DO_CLOSE_DLG = wx.NewEventType()
_EVT_DO_CLOSE_DLG = wx.PyEventBinder(_edEVT_DO_CLOSE_DLG, 1)

class _AdvFindDlgInternalEvent(wx.PyEvent):
    def __init__(self, winid=wx.ID_ANY, etype=wx.wxEVT_NULL):
        """Create the Event"""
        wx.PyEvent.__init__(self, winid, etype)

#--------------------------------------------------------------------------#