1 /*
2 * This file is part of SpellChecker plugin for Code::Blocks Studio
3 * Copyright (C) 2009 Daniel Anselmi
4 *
5 * SpellChecker plugin is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * SpellChecker plugin is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with SpellChecker. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 #include "ThesaurusDialog.h"
20 
21 #include "prep.h"
22 
23 #include <wx/xrc/xmlres.h>
24 
25 
BEGIN_EVENT_TABLE(ThesaurusDialog,wxDialog)26 BEGIN_EVENT_TABLE(ThesaurusDialog,wxDialog)
27 END_EVENT_TABLE()
28 
29 ThesaurusDialog::ThesaurusDialog(wxWindow* parent, wxString Word, std::map<wxString, std::vector< wxString > > &syn):
30     wxDialog( parent, wxID_ANY, wxString(_T("Thesaurus"))),
31     m_syn(syn)
32 {
33     this->SetSizeHints( wxDefaultSize, wxDefaultSize );
34 
35     wxBoxSizer* bSizer1;
36     bSizer1 = new wxBoxSizer( wxVERTICAL );
37 
38     wxBoxSizer* bSizer3;
39     bSizer3 = new wxBoxSizer( wxHORIZONTAL );
40 
41     wxBoxSizer* bSizer2;
42     bSizer2 = new wxBoxSizer( wxVERTICAL );
43 
44     wxStaticText *staticText;
45 
46     staticText = new wxStaticText( this, wxID_ANY, wxT("Looked up:"), wxDefaultPosition, wxDefaultSize, 0 );
47     staticText->Wrap( -1 );
48     bSizer2->Add( staticText, 0, wxALL, 0 );
49 
50     wxArrayString m_ChoiceLookedUpChoices;
51     m_ChoiceLookedUp = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, m_ChoiceLookedUpChoices, 0 );
52     m_ChoiceLookedUp->SetSelection( 0 );
53     bSizer2->Add( m_ChoiceLookedUp, 0, wxALL|wxEXPAND, 5 );
54 
55     staticText = new wxStaticText( this, wxID_ANY, wxT("Meanings:"), wxDefaultPosition, wxDefaultSize, 0 );
56     staticText->Wrap( -1 );
57     bSizer2->Add( staticText, 0, wxALL, 0 );
58 
59     m_ListBoxMeanings = new wxListBox( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, NULL, 0 );
60     bSizer2->Add( m_ListBoxMeanings, 1, wxALL|wxEXPAND, 5 );
61 
62     bSizer3->Add( bSizer2, 1, wxEXPAND, 5 );
63 
64     wxBoxSizer* bSizer4;
65     bSizer4 = new wxBoxSizer( wxVERTICAL );
66 
67     staticText = new wxStaticText( this, wxID_ANY, wxT("Replace with Synonym:"), wxDefaultPosition, wxDefaultSize, 0 );
68     staticText->Wrap( -1 );
69     bSizer4->Add( staticText, 0, wxALL, 0 );
70 
71     m_TextCtrlReplaceSynonym = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
72     bSizer4->Add( m_TextCtrlReplaceSynonym, 0, wxALL|wxEXPAND, 5 );
73 
74     m_ListBoxSynonyme = new wxListBox( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, NULL, 0 );
75     bSizer4->Add( m_ListBoxSynonyme, 1, wxALL|wxEXPAND, 5 );
76 
77     bSizer3->Add( bSizer4, 1, wxEXPAND, 5 );
78 
79     bSizer1->Add( bSizer3, 1, wxEXPAND, 5 );
80 
81     m_sdbSizer1 = new wxStdDialogButtonSizer();
82     m_sdbSizer1OK = new wxButton( this, wxID_OK );
83     m_sdbSizer1->AddButton( m_sdbSizer1OK );
84     m_sdbSizer1Cancel = new wxButton( this, wxID_CANCEL );
85     m_sdbSizer1->AddButton( m_sdbSizer1Cancel );
86     m_sdbSizer1->Realize();
87     bSizer1->Add( m_sdbSizer1, 0, wxALIGN_CENTER_HORIZONTAL, 5 );
88 
89 
90     m_ChoiceLookedUp->Append(Word);
91     m_ChoiceLookedUp->Select(0);
92     std::map<wxString, std::vector< wxString > >::iterator it;
93     for ( it = syn.begin() ; it != syn.end(); it++ )
94         m_ListBoxMeanings->Append(it->first);
95     m_ListBoxMeanings->Select(0);
96     UpdateSynonyme();
97 
98 
99     this->SetSizer( bSizer1 );
100     this->Layout();
101     bSizer1->Fit( this );
102 
103 
104     Connect(XRCID("m_listBoxMeanings"),wxEVT_COMMAND_LISTBOX_SELECTED,(wxObjectEventFunction)&ThesaurusDialog::OnMeaningsSelected);
105     Connect(XRCID("m_listBoxSynonym"),wxEVT_COMMAND_LISTBOX_SELECTED,(wxObjectEventFunction)&ThesaurusDialog::OnSynonymeSelected);
106 
107     // Connect Events
108     m_ListBoxMeanings->Connect( wxEVT_COMMAND_LISTBOX_SELECTED, wxCommandEventHandler( ThesaurusDialog::OnMeaningsSelected ), NULL, this );
109     m_ListBoxSynonyme->Connect( wxEVT_COMMAND_LISTBOX_SELECTED, wxCommandEventHandler( ThesaurusDialog::OnSynonymeSelected ), NULL, this );
110 }
111 
UpdateSynonyme()112 void ThesaurusDialog::UpdateSynonyme()
113 {
114     wxString meaning = m_ListBoxMeanings->GetString(m_ListBoxMeanings->GetSelection());
115 
116     m_ListBoxSynonyme->Clear();
117 
118     std::vector< wxString > syns = m_syn[meaning];
119     for ( unsigned int i = 0 ; i < syns.size() ; i++ )
120         m_ListBoxSynonyme->Append(syns[i]);
121     m_ListBoxSynonyme->Select(0);
122     UpdateSelectedSynonym();
123 
124 }
125 
UpdateSelectedSynonym()126 void ThesaurusDialog::UpdateSelectedSynonym()
127 {
128     wxString str = m_ListBoxSynonyme->GetString( m_ListBoxSynonyme->GetSelection() );
129 
130     int pos = str.find( _T('(') );
131     if ( pos != wxNOT_FOUND )
132     {
133         str = str.Mid(0, pos-1);
134         str.Trim();
135     }
136     m_TextCtrlReplaceSynonym->SetValue(str);
137 }
138 
~ThesaurusDialog()139 ThesaurusDialog::~ThesaurusDialog()
140 {
141     m_ListBoxMeanings->Disconnect( wxEVT_COMMAND_LISTBOX_SELECTED, wxCommandEventHandler( ThesaurusDialog::OnMeaningsSelected ), NULL, this );
142     m_ListBoxSynonyme->Disconnect( wxEVT_COMMAND_LISTBOX_SELECTED, wxCommandEventHandler( ThesaurusDialog::OnSynonymeSelected ), NULL, this );
143 }
144 
GetSelection()145 wxString ThesaurusDialog::GetSelection()
146 {
147     return m_TextCtrlReplaceSynonym->GetValue();
148 }
149 
OnMeaningsSelected(cb_unused wxCommandEvent & event)150 void ThesaurusDialog::OnMeaningsSelected(cb_unused wxCommandEvent& event)
151 {
152     UpdateSynonyme();
153 }
154 
OnSynonymeSelected(cb_unused wxCommandEvent & event)155 void ThesaurusDialog::OnSynonymeSelected(cb_unused wxCommandEvent& event)
156 {
157     UpdateSelectedSynonym();
158 }
159