Imported version 0.5-1
[mstardict] / src / transwin.cpp
1 /*
2  *  MStarDict - International dictionary for Maemo.
3  *  Copyright (C) 2010 Roman Moravcik
4  *
5  *  base on code of stardict:
6  *  Copyright (C) 2003-2007 Hu Zheng <huzheng_001@163.com>
7  *
8  *  based on code of sdcv:
9  *  Copyright (C) 2005-2006 Evgeniy <dushistov@mail.ru>
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include <string>
31 #include <vector>
32 #include <memory>
33 #include <list>
34
35 #include <glib.h>
36 #include <glib/gi18n.h>
37
38 #include <gtk/gtk.h>
39 #include <hildon/hildon.h>
40
41 #include "libwrapper.hpp"
42 #include "mstardict.hpp"
43 #include "tts.hpp"
44 #include "transwin.hpp"
45
46 TransWin::TransWin(MStarDict *mStarDict)
47 {
48     oStarDict = mStarDict;
49     window = NULL;
50     sExpression = NULL;
51 }
52
53 TransWin::~TransWin()
54 {
55     if (sExpression)
56         g_free(sExpression);
57 }
58
59 gboolean
60 TransWin::onSayMenuItemClicked(GtkButton *button,
61                                TransWin *mTransWin)
62 {
63     mTransWin->oStarDict->oTts->SayText(mTransWin->sExpression);
64     return true;
65 }
66
67 GtkWidget *
68 TransWin::CreateTransWidget(SearchResult *result)
69 {
70     GtkWidget *vbox, *hbox, *label;
71     char *bookname = NULL, *def = NULL, *exp = NULL;
72
73     bookname = g_markup_printf_escaped("<span color=\"dimgray\" size=\"x-small\">%s</span>",
74                                        result->bookname);
75     exp = g_markup_printf_escaped("<span color=\"darkred\" weight=\"heavy\" size=\"large\">%s</span>",
76                                   result->exp);
77     def = g_strdup(result->def);
78
79     vbox = gtk_vbox_new(FALSE, 0);
80
81     hbox = gtk_hbox_new(FALSE, 0);
82     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
83
84     label = gtk_label_new("Expression");
85     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
86     gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
87     if (exp) {
88         gtk_label_set_markup(GTK_LABEL(label), exp);
89         g_free(exp);
90     }
91
92     label = gtk_label_new("Bookname");
93     gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
94     gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
95     if (bookname) {
96         gtk_label_set_markup(GTK_LABEL(label), bookname);
97         g_free(bookname);
98     }
99
100     label = gtk_label_new("Definition");
101     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
102     gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
103     gtk_widget_set_size_request(label, 750, -1);
104     gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
105     if (def) {
106         gtk_label_set_markup(GTK_LABEL(label), def);
107         g_free(def);
108     }
109
110     if (sExpression == NULL)
111         sExpression = g_strdup(result->exp);
112
113     return vbox;
114 }
115
116 void
117 TransWin::CreateTransWindow(GList *results)
118 {
119     GtkWidget *alignment, *pannable, *vbox, *trans, *separator;
120     GList *result = NULL;
121
122     window = hildon_stackable_window_new();
123     gtk_window_set_title(GTK_WINDOW(window), _("Translation"));
124
125     alignment = gtk_alignment_new(0.0, 0.0, 1.0, 1.0);
126     gtk_alignment_set_padding(GTK_ALIGNMENT(alignment),
127                               HILDON_MARGIN_DEFAULT,
128                               HILDON_MARGIN_DEFAULT, HILDON_MARGIN_DOUBLE, HILDON_MARGIN_DEFAULT);
129     gtk_container_add(GTK_CONTAINER(window), alignment);
130
131     pannable = hildon_pannable_area_new();
132     gtk_container_add(GTK_CONTAINER(alignment), pannable);
133
134     vbox = gtk_vbox_new(FALSE, 16);
135     hildon_pannable_area_add_with_viewport(HILDON_PANNABLE_AREA(pannable), vbox);
136
137     if (sExpression)
138         g_free(sExpression);
139     sExpression = NULL;
140
141     for (result = results; result != NULL; result = result->next) {
142         trans = CreateTransWidget((SearchResult *) result->data);
143         gtk_box_pack_start(GTK_BOX(vbox), trans, FALSE, FALSE, 0);
144
145         separator = gtk_hseparator_new();
146         gtk_box_pack_start(GTK_BOX(vbox), separator, FALSE, FALSE, 0);
147     }
148
149     /* create translation menu */
150     CreateTransMenu();
151
152     gtk_widget_show_all(window);
153 }
154
155 void
156 TransWin::CreateTransMenu()
157 {
158     HildonAppMenu *menu;
159     GtkWidget *item;
160
161     menu = HILDON_APP_MENU(hildon_app_menu_new());
162     hildon_window_set_app_menu(HILDON_WINDOW(window), menu);
163
164     /* Say menu item */
165     if (oStarDict->oTts->IsEnabled()) {
166         item = hildon_gtk_button_new(HILDON_SIZE_AUTO);
167         gtk_button_set_label(GTK_BUTTON(item), _("Say"));
168         hildon_app_menu_append(menu, GTK_BUTTON(item));
169         g_signal_connect(item, "clicked", G_CALLBACK(onSayMenuItemClicked), this);
170     }
171
172     /* show main menu */
173     gtk_widget_show_all(GTK_WIDGET(menu));
174 }
175