searchpanel.cc

Go to the documentation of this file.
00001 /* Eliot                                                                     */
00002 /* Copyright (C) 1999  Antoine Fraboulet                                     */
00003 /*                                                                           */
00004 /* This file is part of Eliot.                                               */
00005 /*                                                                           */
00006 /* Eliot is free software; you can redistribute it and/or modify             */
00007 /* it under the terms of the GNU General Public License as published by      */
00008 /* the Free Software Foundation; either version 2 of the License, or         */
00009 /* (at your option) any later version.                                       */
00010 /*                                                                           */
00011 /* Eliot is distributed in the hope that it will be useful,                  */
00012 /* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
00013 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
00014 /* GNU General Public License for more details.                              */
00015 /*                                                                           */
00016 /* You should have received a copy of the GNU General Public License         */
00017 /* along with this program; if not, write to the Free Software               */
00018 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00019 
00020 /**
00021  *  \file   searchpanel.cc
00022  *  \brief  Panel used in Eliot search window
00023  *  \author Antoine Fraboulet
00024  *  \date   2005
00025  */
00026 
00027 #include <string.h>
00028 #include "wx/wx.h"
00029 
00030 #include "ewx.h"
00031 #include "dic.h"
00032 #include "dic_search.h"
00033 #include "regexp.h"
00034 #include "searchpanel.h"
00035 #include "tile.h"
00036 #include "configdb.h"
00037 
00038 enum {
00039   ID_PANEL_CROSS,
00040   ID_PANEL_PLUS1,
00041   ID_PANEL_REGEXP,
00042 
00043   ID_LIST,
00044   ID_TEXT,
00045   ID_OPTION1,
00046   ID_OPTION2,
00047   ID_OPTION3
00048 };
00049 
00050 // ************************************************************
00051 // ************************************************************
00052 // ************************************************************
00053 
00054 class SimpleSearchPanel : public wxPanel
00055 {
00056 protected:
00057   ConfigDB   config;
00058   Dictionary dic;
00059   wxTextCtrl *t;
00060   wxListBox  *l;
00061   wxBoxSizer *sizer;
00062 
00063   int  check_dic();
00064   void check_end();
00065   void panel_build();
00066   virtual void panel_options() = 0;
00067 public:
00068   SimpleSearchPanel(wxWindow* parent, int id, Dictionary d) : wxPanel(parent,id) { dic = d; };
00069   virtual void compute_char(wxCommandEvent&) {};
00070   virtual void compute_enter(wxCommandEvent&) {};
00071   DECLARE_EVENT_TABLE()
00072 };
00073 
00074 BEGIN_EVENT_TABLE(SimpleSearchPanel, wxPanel)
00075   EVT_TEXT      (ID_TEXT   , SimpleSearchPanel::compute_char)
00076   EVT_TEXT_ENTER(ID_TEXT   , SimpleSearchPanel::compute_enter)
00077   EVT_TEXT_ENTER(ID_OPTION1, SimpleSearchPanel::compute_enter)
00078   EVT_TEXT_ENTER(ID_OPTION2, SimpleSearchPanel::compute_enter)
00079   EVT_TEXT_ENTER(ID_OPTION3, SimpleSearchPanel::compute_enter)
00080 END_EVENT_TABLE()
00081 
00082 void SimpleSearchPanel::panel_build()
00083 {
00084   t = new wxTextCtrl(this,ID_TEXT,wxT(""),wxPoint(0,0),wxSize(-1,-1),wxTE_PROCESS_ENTER);
00085   t->SetFont(config.getFont(LISTFONT));
00086   l = new wxListBox(this,ID_LIST);
00087   l->SetFont(config.getFont(LISTFONT));
00088   l->Show(TRUE);
00089 
00090   sizer = new wxBoxSizer( wxVERTICAL );
00091   sizer->Add(t, 0, wxEXPAND | wxALL, 0);
00092   panel_options();
00093   sizer->Add(l, 1, wxEXPAND | wxALL, 0);
00094 
00095   SetAutoLayout(TRUE);
00096   SetSizer(sizer);
00097   sizer->Fit(this);
00098   sizer->SetSizeHints(this);
00099 }
00100 
00101 int
00102 SimpleSearchPanel::check_dic()
00103 {
00104   wxString msg = wxT("");
00105   if (dic == NULL)
00106     {
00107       l->Clear();
00108       msg << wxT("Pas de dictionnaire");
00109       l->Append(msg);
00110       return 0;
00111     }
00112   return 1;
00113 }
00114 
00115 void
00116 SimpleSearchPanel::check_end()
00117 {
00118   if (l->GetCount() == 0)
00119     {
00120       l->Append(wxT("Aucun resultat"));
00121     }
00122 }
00123 
00124 // ************************************************************
00125 // ************************************************************
00126 // ************************************************************
00127 
00128 class PCross : public SimpleSearchPanel
00129 {
00130 protected:
00131   virtual void panel_options() {};
00132 public:
00133   void compute_char(wxCommandEvent&) { };
00134   void compute_enter(wxCommandEvent&);
00135   PCross(wxWindow* parent, int id, Dictionary d) : SimpleSearchPanel(parent,id,d) { panel_build(); };
00136 };
00137 
00138 void
00139 PCross::compute_enter(wxCommandEvent&)
00140 {
00141   int  i;
00142   char rack[DIC_WORD_MAX];
00143   char buff[RES_CROS_MAX][DIC_WORD_MAX];
00144 
00145   if (!check_dic())
00146     return;
00147 
00148   if (t->GetValue().Len() >= DIC_WORD_MAX)
00149     {
00150       wxString msg = wxT("");
00151 // XXX:      msg << wxT("La recherche est limitée à ") << DIC_WORD_MAX - 1 << wxT(" lettres");
00152       msg << wxT("La recherche est limitee a ") << DIC_WORD_MAX - 1 << wxT(" lettres");
00153       l->Append(msg);
00154       return;
00155     }
00156 
00157   strncpy(rack, t->GetValue().mb_str(), DIC_WORD_MAX);
00158   Dic_search_Cros(dic,rack,buff);
00159 
00160   int resnum = 0;
00161   wxString res[RES_CROS_MAX];
00162   for(i=0; i < RES_CROS_MAX && buff[i][0]; i++)
00163     res[resnum++] =  wxU(buff[i]);
00164   l->Set(resnum,res);
00165   check_end();
00166 }
00167 
00168 // ************************************************************
00169 // ************************************************************
00170 // ************************************************************
00171 
00172 class PPlus1 : public SimpleSearchPanel
00173 {
00174 protected:
00175   virtual void panel_options() {};
00176 public:
00177   void compute_char(wxCommandEvent&) { };
00178   void compute_enter(wxCommandEvent&);
00179   PPlus1(wxWindow* parent, int id, Dictionary dic) : SimpleSearchPanel(parent,id,dic) { panel_build(); };
00180 };
00181 
00182 void
00183 PPlus1::compute_enter(wxCommandEvent&)
00184 {
00185   int  i,j;
00186   char rack[DIC_WORD_MAX];
00187   char buff[DIC_LETTERS][RES_7PL1_MAX][DIC_WORD_MAX];
00188 
00189   if (!check_dic())
00190     return;
00191 
00192   if (t->GetValue().Len() >= DIC_WORD_MAX)
00193     {
00194       wxString msg = wxT("");
00195 // XXX:      msg << wxT("La recherche est limitée à ") << DIC_WORD_MAX - 1 << wxT(" lettres");
00196       msg << wxT("La recherche est limitee a ") << DIC_WORD_MAX - 1 << wxT(" lettres");
00197       l->Append(msg);
00198       return;
00199     }
00200 
00201   strncpy(rack, t->GetValue().mb_str(), DIC_WORD_MAX);
00202   Dic_search_7pl1(dic,rack,buff,TRUE);
00203 
00204   int resnum = 0;
00205   wxString res[DIC_LETTERS*(RES_7PL1_MAX+1)];
00206   for(i=0; i < DIC_LETTERS; i++)
00207     {
00208       if (i && buff[i][0][0])
00209           res[resnum++] = wxString(wxT("+")) + (wxChar)(i+'A'-1);
00210       for(j=0; j < RES_7PL1_MAX && buff[i][j][0]; j++)
00211           res[resnum++] = wxString(wxT("  ")) + wxU(buff[i][j]);
00212     }
00213   l->Set(resnum,res);
00214   check_end();
00215 }
00216 
00217 // ************************************************************
00218 // ************************************************************
00219 // ************************************************************
00220 
00221 class PRegExp : public SimpleSearchPanel
00222 {
00223 protected:
00224   wxTextCtrl                *omin;
00225   wxTextCtrl                *omax;
00226   struct search_RegE_list_t llist;
00227 
00228   virtual void build_letter_lists();
00229   virtual void panel_options();
00230 public:
00231   void compute_char(wxCommandEvent&) { };
00232   void compute_enter(wxCommandEvent&);
00233   PRegExp(wxWindow* parent, int id, Dictionary d) : SimpleSearchPanel(parent,id,d) { panel_build(); };
00234 };
00235 
00236 void
00237 PRegExp::build_letter_lists()
00238 {
00239   int i;
00240   std::list<Tile> all_tiles;
00241 
00242   memset (&llist,0,sizeof(llist));
00243 
00244   llist.minlength = 1;
00245   llist.maxlength = 15;
00246 
00247   llist.symbl[0] = RE_ALL_MATCH;
00248   llist.symbl[1] = RE_VOWL_MATCH;
00249   llist.symbl[2] = RE_CONS_MATCH;
00250   llist.symbl[3] = RE_USR1_MATCH;
00251   llist.symbl[5] = RE_USR2_MATCH;
00252 
00253   llist.valid[0] = 1; // all letters
00254   llist.valid[1] = 1; // vowels
00255   llist.valid[2] = 1; // consonants
00256   llist.valid[3] = 0; // user defined list 1
00257   llist.valid[4] = 0; // user defined list 2
00258 
00259   for(i=0; i < DIC_SEARCH_REGE_LIST; i++)
00260     {
00261       memset(llist.letters[i],0,sizeof(llist.letters[i]));
00262     }
00263 
00264   const std::list<Tile>& allTiles = Tile::getAllTiles();
00265   std::list<Tile>::const_iterator it;
00266   for (it = allTiles.begin(); it != allTiles.end(); it++)
00267     {
00268       if (! it->isJoker() && ! it->isEmpty())
00269         {
00270           // all tiles
00271           llist.letters[0][it->toCode()] = 1;
00272           // vowels
00273           if (it->isVowel())
00274             {
00275               llist.letters[1][it->toCode()] = 1;
00276             }
00277           // consonants
00278           if (it->isConsonant())
00279             {
00280               llist.letters[2][it->toCode()] = 1;
00281             }
00282         }
00283     }
00284 }
00285 
00286 void
00287 PRegExp::panel_options()
00288 {
00289   wxStaticText *otmin;
00290   wxStaticText *otmax;
00291 
00292   otmin = new wxStaticText(this,wxID_ANY,wxT("Longueur min."));
00293   omin  = new wxTextCtrl(this,ID_OPTION1,wxT( "1"),wxDefaultPosition,wxDefaultSize,wxTE_PROCESS_ENTER);
00294   otmax = new wxStaticText(this,wxID_ANY,wxT("max."));
00295   omax  = new wxTextCtrl(this,ID_OPTION2,wxT("15"),wxDefaultPosition,wxDefaultSize,wxTE_PROCESS_ENTER);
00296 
00297   wxBoxSizer *s = new wxBoxSizer( wxHORIZONTAL );
00298   s->Add(otmin, 0, wxALIGN_CENTRE_VERTICAL | wxLEFT | wxRIGHT, 4);
00299   s->Add(omin , 1, wxALIGN_CENTRE_VERTICAL, 0);
00300   s->Add(otmax, 0, wxALIGN_CENTRE_VERTICAL | wxLEFT | wxRIGHT, 4);
00301   s->Add(omax , 1, wxALIGN_CENTRE_VERTICAL, 0);
00302   sizer->Add(s, 0, wxEXPAND | wxALL, 1);
00303 }
00304 
00305 
00306 #define DIC_RE_MAX (3*DIC_WORD_MAX) // yes, it's 3
00307 
00308 void
00309 PRegExp::compute_enter(wxCommandEvent&)
00310 {
00311   char re[DIC_RE_MAX];
00312   char buff[RES_REGE_MAX][DIC_WORD_MAX];
00313 
00314   if (!check_dic())
00315     return;
00316 
00317   build_letter_lists();
00318   strncpy(re, t->GetValue().mb_str(),DIC_RE_MAX);
00319   debug("PRegExp::compute_enter for %s",re);
00320 
00321   int lmin = atoi((const char*)omin->GetValue().mb_str());
00322   int lmax = atoi((const char*)omax->GetValue().mb_str());
00323   if (lmax <= (DIC_WORD_MAX - 1) && lmin >= 1 && lmin <= lmax)
00324     {
00325       llist.minlength = lmin;
00326       llist.maxlength = lmax;
00327       debug(" length %d,%d",lmin,lmax);
00328     }
00329   else
00330     {
00331       debug(" bad length -%s,%s-",
00332             (const char*)omin->GetValue().mb_str(),
00333             (const char*)omax->GetValue().mb_str());
00334     }
00335   debug("\n");
00336 
00337   Dic_search_RegE(dic,re,buff,&llist);
00338 
00339   int resnum = 0;
00340   wxString res[RES_REGE_MAX];
00341   for(int i=0; i < RES_REGE_MAX && buff[i][0]; i++)
00342     res[resnum++] =  wxU(buff[i]);
00343 
00344   l->Set(resnum,res);
00345   check_end();
00346 }
00347 
00348 // ************************************************************
00349 // ************************************************************
00350 // ************************************************************
00351 
00352 SearchPanel::SearchPanel(wxFrame *parent, Dictionary dic) :
00353   wxNotebook(parent, -1)
00354 {
00355   AddPage(new PCross (this,ID_PANEL_CROSS ,dic),wxT("Mots croises"));
00356   AddPage(new PPlus1 (this,ID_PANEL_PLUS1 ,dic),wxT("Plus 1"));
00357   AddPage(new PRegExp(this,ID_PANEL_REGEXP,dic),wxT("Exp. Rationnelle"));
00358   SetSelection(2);
00359 }
00360 
00361 SearchPanel::~SearchPanel()
00362 {
00363 }
00364 
00365 // ************************************************************
00366 // ************************************************************
00367 // ************************************************************
00368 
00369 
00370 /// Local Variables:
00371 /// mode: hs-minor
00372 /// c-basic-offset: 4
00373 /// End:

Generated on Thu Dec 29 02:01:15 2005 for Eliot by  doxygen 1.4.5