Major directory structure changes, plus a bug fix.
[jben.git] / src / frame_kanjipad.cpp
blobf849e56c3f259f9df9d56a5367be358435cc1a50
1 /*
2 Project: wxKanjiPad
3 Author: Paul Goins
4 Website: http://www.vultaire.net/software/wxkanjipad/files/
5 License: GNU General Public License (GPL) version 2
6 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt)
8 File: frame_kanjipad.cpp
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License along
21 with this program; if not, write to the Free Software Foundation, Inc.,
22 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "frame_kanjipad.h"
27 /* Clipboard Workaround */
28 #ifdef __WXGTK__
29 #include <gtk/gtk.h>
30 #else
31 #include <wx/clipbrd.h>
32 #endif
34 enum {
35 ID_btnSearch=1,
36 ID_btnClear,
37 ID_lcKanjiSelector
40 BEGIN_EVENT_TABLE(FrameKanjiPad, wxFrame)
41 EVT_CLOSE(FrameKanjiPad::OnClose)
42 EVT_BUTTON(ID_btnSearch, FrameKanjiPad::OnButtonSearch)
43 EVT_BUTTON(ID_btnClear, FrameKanjiPad::OnButtonClear)
44 EVT_LIST_ITEM_ACTIVATED(ID_lcKanjiSelector, FrameKanjiPad::OnKanjiSelected)
45 END_EVENT_TABLE()
47 FrameKanjiPad
48 ::FrameKanjiPad(wxWindow *owner, wxWindowID id, const wxString& title,
49 const wxPoint& pos, const wxSize& size)
50 : wxFrame(owner, id, title, pos, size) {
51 /* Create all the simple stuff */
52 pnlKanjiPad = new PanelKanjiPad(this, -1,
53 wxDefaultPosition, wxDefaultSize,
54 wxBORDER_SUNKEN);
55 #ifdef __WXMSW__
56 /* Windows builds will not automatically handle Alt-S/Alt-C yet, so
57 don't display underlines for Search and Clear. */
58 btnSearch = new wxButton(this, ID_btnSearch, _T("Search"));
59 btnClear = new wxButton(this, ID_btnClear, _T("Clear"));
60 #else
61 btnSearch = new wxButton(this, ID_btnSearch, _T("&Search"));
62 btnClear = new wxButton(this, ID_btnClear, _T("&Clear"));
63 #endif
65 /* Create the kanji selector */
66 /* Our kanji selector should use a larger font than standard.
67 Let's do a hard-coded 32-pt font for our purposes. */
68 lcKanjiSelector = new wxListCtrl(this, ID_lcKanjiSelector,
69 wxDefaultPosition,
70 wxSize(-1, 25),
71 wxLC_LIST | wxLC_SINGLE_SEL
72 | wxHSCROLL | wxBORDER_SUNKEN);
73 wxFont fntKanjiSelector(this->GetFont());
74 fntKanjiSelector.SetPointSize(32);
75 lcKanjiSelector->SetFont(fntKanjiSelector);
76 /* Resize the kanji selector to fit the font appropriately */
77 /* NOTE: to get the proper size under WIN32, we must explicitly pass the
78 font into the control's wxClientDC, even though the font for the object
79 was already set before obtaining the DC. */
80 wxClientDC cdc(lcKanjiSelector);
81 cdc.SetFont(fntKanjiSelector);
82 wxSize szFnt = cdc.GetTextExtent(_T("漢"));
83 lcKanjiSelector->SetColumnWidth(-1, szFnt.GetWidth()+6); /*hard-coded val*/
84 szFnt.SetWidth(-1);
85 szFnt.SetHeight(szFnt.GetHeight()+12); /* Hard-coded padding */
86 lcKanjiSelector->SetSize(szFnt);
87 lcKanjiSelector->SetMinSize(szFnt);
89 /* Arrange controls and add to frame */
90 wxBoxSizer *vertSizer = new wxBoxSizer(wxVERTICAL);
91 wxBoxSizer *horizSizer = new wxBoxSizer(wxHORIZONTAL);
92 vertSizer->Add(pnlKanjiPad, 1, wxEXPAND);
93 vertSizer->Add(lcKanjiSelector, 0, wxEXPAND);
94 horizSizer->AddStretchSpacer(1);
95 horizSizer->Add(btnSearch, 0, wxALIGN_CENTER_HORIZONTAL);
96 horizSizer->AddStretchSpacer(1);
97 horizSizer->Add(btnClear, 0, wxALIGN_CENTER_HORIZONTAL);
98 horizSizer->AddStretchSpacer(1);
99 vertSizer->Add(horizSizer, 0, wxEXPAND);
100 SetSizer(vertSizer);
103 void FrameKanjiPad::OnClose(wxCloseEvent& ev) {
104 this->Destroy();
107 void FrameKanjiPad::OnButtonSearch(wxCommandEvent& ev) {
108 if(pnlKanjiPad->Search()) {
109 lcKanjiSelector->ClearAll();
110 for(int i=0;i<KANJIPAD_MAX_KANJI;i++) {
111 if(pnlKanjiPad->results[i]==_T('\0')) break;
112 lcKanjiSelector->InsertItem(i, wxString(pnlKanjiPad->results[i]));
117 void FrameKanjiPad::OnButtonClear(wxCommandEvent& ev) {
118 pnlKanjiPad->Clear();
119 lcKanjiSelector->ClearAll();
120 Refresh();
121 /*pnlKanjiPad->Refresh();*/
124 void FrameKanjiPad::OnKanjiSelected(wxListEvent& ev) {
125 int i = (int) lcKanjiSelector->
126 GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
128 /* Check bounds */
129 if(i < 0 || i > KANJIPAD_MAX_KANJI-1) {
130 wxMessageBox(wxString::Format(
131 _T("Error: selected item is reported as %d."), i),
132 _T("Error"), wxOK | wxICON_ERROR, this);
133 return;
136 #ifdef __WXGTK__
137 /* Copy to clipboard - GTK Style*/
138 /* Convert from wide char to multibyte char in native encoding */
139 wxString ws(pnlKanjiPad->results[i]);
140 int bufSize = sizeof(wxChar) * (ws.length()+1);
141 gchar *buffer = new gchar[bufSize];
142 memset(buffer, '\0', bufSize*sizeof(gchar));
143 wxConvUTF8.WC2MB(buffer, ws.c_str(), bufSize);
145 /* Copy to clipboard */
146 GtkClipboard *gClip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
147 gtk_clipboard_set_text(gClip, buffer, strlen(buffer));
148 delete[] buffer;
149 #else
150 /* Copy to clipboard - wxWidgets Style */
151 if(wxTheClipboard->Open()) {
152 wxTheClipboard->Clear();
153 wxTheClipboard->SetData(
154 new wxTextDataObject(wxString(pnlKanjiPad->results[i])));
155 if(!wxTheClipboard->Flush()) {
156 #if 0
157 /* Flush fails often on non-Windows platforms, so for now I'm
158 just gonna disable this message box. */
159 wxMessageBox(_T("Error: Could not flush data to clipboard."),
160 _T("Error"), wxOK | wxICON_ERROR, this);
161 #endif
163 wxTheClipboard->Close();
164 } else {
165 wxMessageBox(_T("Error: Could not open clipboard."),
166 _T("Error"), wxOK | wxICON_ERROR, this);
168 #endif