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
|
/***************************************************************************
* Copyright (C) 2005-2006 by Niklas Knutsson *
* [email protected] *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "kqalculate.h"
#include "preferences.h"
#include <string>
#include <twin.h>
#include <tdeuniqueapplication.h>
#include <tdeaboutdata.h>
#include <tdecmdlineargs.h>
#include <tdelocale.h>
#include <unistd.h>
#include <tqtextcodec.h>
#include <tqobject.h>
#include <tdemessagebox.h>
#include <tqtimer.h>
#include <klineedit.h>
#include <tqtooltip.h>
#include <tqclipboard.h>
#include "qalculateexpressionedit.h"
#include "qalculateresultdisplay.h"
#include "qalculate_tde_utils.h"
static const char description[] = I18N_NOOP("A powerful and easy to use desktop calculator");
static const char version[] = VERSION;
static TDECmdLineOptions options[] = {
{"c", 0, 0},
{"clipboard", I18N_NOOP("Calculate X11-clipboard content (selected text)"), 0},
{"+[Expression]", I18N_NOOP( "Initial expression to calculate" ), 0},
TDECmdLineLastOption
};
extern KnownVariable *vans[5];
extern MathStructure *mstruct, *matrix_mstruct, *parsed_mstruct, *parsed_tostruct;
extern TQString result_text, result_history_text, parsed_text;
extern TQWidget *expressionWidget, *resultWidget, *statusWidget_l;
extern TQWidget *topWidget;
extern KQalculate *mainWin;
extern bool load_global_defs, fetch_exchange_rates_at_startup, first_time, first_qalculate_run;
extern bool b_busy;
extern bool canplot;
extern bool close_to_systray;
extern FILE *view_pipe_r, *view_pipe_w, *command_pipe_r, *command_pipe_w;
extern pthread_t view_thread, command_thread;
extern pthread_attr_t view_thread_attr, command_thread_attr;
extern bool command_thread_started;
extern TQValueVector<TQString> recent_functions_pre;
extern TQValueVector<TQString> recent_variables_pre;
extern TQValueVector<TQString> recent_units_pre;
TQTimer *error_timer;
void create_qalculate_window(TDEUniqueApplication *parent) {
TQTextCodec::setCodecForCStrings(TQTextCodec::codecForName("utf8"));
b_busy = false;
new Calculator();
if(TDEGlobal::locale()->decimalSymbol() == ",") CALCULATOR->useDecimalComma();
CALCULATOR->place_currency_code_before = TDEGlobal::locale()->positivePrefixCurrencySymbol();
//load application specific preferences
load_preferences();
mstruct = new MathStructure();
parsed_mstruct = new MathStructure();
parsed_tostruct = new MathStructure();
parsed_tostruct->setUndefined();
matrix_mstruct = new MathStructure();
canplot = CALCULATOR->canPlot();
mainWin = new KQalculate(parent);
topWidget = mainWin;
expressionWidget = (TQWidget*) mainWin->expressionEdit;
resultWidget = (TQWidget*) mainWin->resultLabel;
statusWidget_l = (TQWidget*) mainWin->statusLabel_l;
}
TQString parse_expression_arguments(TDECmdLineArgs *args) {
TQString calc_arg;
if(args->isSet("clipboard")) {
calc_arg = tqApp->clipboard()->text(TQClipboard::Selection);
if(calc_arg.isEmpty()) {
calc_arg = tqApp->clipboard()->text(TQClipboard::Clipboard);
}
} else {
for(int i = 0; i < args->count(); i++) {
if(i > 0) {
calc_arg += " ";
}
calc_arg += args->arg(i);
}
}
return calc_arg;
}
void start_qalculate() {
TDECmdLineArgs* args = TDECmdLineArgs::parsedArgs();
TQString calc_arg = parse_expression_arguments(args);
args->clear();
if(!calc_arg.isEmpty()) {
mainWin->expressionEdit->setText(calc_arg);
} else if(first_time) {
mainWin->resultLabel->setText(i18n("Enter a mathematical expression above.<br>Ex. 5 + 2 / 3"));
} else {
mainWin->resultLabel->setText("<div align=\"right\"><font size=2> <br></font><font size=6>= 0</font></div>");
}
tqApp->processEvents();
//exchange rates
if(first_qalculate_run) {
if(KMessageBox::questionYesNo(mainWin, i18n("You need to download exchange rates to be able to convert between different currencies. You can later get current exchange rates by selecting \"Update Exchange Rates\" under the File menu.\n\nDo you want to fetch exchange rates now from the Internet?"), i18n("Update exchange rates?")) == KMessageBox::Yes) {
tqApp->processEvents();
mainWin->fetch_exchange_rates(5);
}
tqApp->processEvents();
} else if(fetch_exchange_rates_at_startup) {
mainWin->fetch_exchange_rates(5);
tqApp->processEvents();
}
CALCULATOR->loadExchangeRates();
std::string ans_str = i18n("ans").ascii();
vans[0] = (KnownVariable*) CALCULATOR->addVariable(new KnownVariable(i18n("Temporary").ascii(), ans_str, m_undefined, i18n("Last Answer").ascii(), false));
vans[0]->addName(i18n("answer").ascii());
vans[0]->addName(ans_str + "1");
vans[1] = (KnownVariable*) CALCULATOR->addVariable(new KnownVariable(i18n("Temporary").ascii(), ans_str + "2", m_undefined, i18n("Answer 2").ascii(), false));
vans[2] = (KnownVariable*) CALCULATOR->addVariable(new KnownVariable(i18n("Temporary").ascii(), ans_str + "3", m_undefined, i18n("Answer 3").ascii(), false));
vans[3] = (KnownVariable*) CALCULATOR->addVariable(new KnownVariable(i18n("Temporary").ascii(), ans_str + "4", m_undefined, i18n("Answer 4").ascii(), false));
vans[4] = (KnownVariable*) CALCULATOR->addVariable(new KnownVariable(i18n("Temporary").ascii(), ans_str + "5", m_undefined, i18n("Answer 5").ascii(), false));
//load global definitions
if(load_global_defs && !CALCULATOR->loadGlobalDefinitions()) {
KMessageBox::error(mainWin, i18n("Failed to load global definitions!"), i18n("Error"));
tqApp->processEvents();
}
//load local definitions
CALCULATOR->loadLocalDefinitions();
//check for calculation errros regularly
error_timer = new TQTimer(mainWin);
TQObject::connect(error_timer, TQ_SIGNAL(timeout()), mainWin, TQ_SLOT(onErrorTimeout()));
generate_units_tree_struct();
generate_functions_tree_struct();
generate_variables_tree_struct();
mainWin->create_fmenu();
mainWin->create_vmenu();
mainWin->create_umenu();
mainWin->create_toumenu();
mainWin->create_setpmenu();
for(int i = ((int) recent_functions_pre.size()) - 1; i >= 0; i--) {
mainWin->function_inserted(CALCULATOR->getActiveFunction(recent_functions_pre[i].ascii()));
}
for(int i = ((int) recent_variables_pre.size()) - 1; i >= 0; i--) {
mainWin->variable_inserted(CALCULATOR->getActiveVariable(recent_variables_pre[i].ascii()));
}
for(int i = ((int) recent_units_pre.size()) - 1; i >= 0; i--) {
Unit *u = CALCULATOR->getActiveUnit(recent_units_pre[i].ascii());
if(!u) u = CALCULATOR->getCompositeUnit(recent_units_pre[i].ascii());
mainWin->unit_inserted(u);
}
mainWin->update_completion();
int pipe_wr[] = {0, 0};
pipe(pipe_wr);
view_pipe_r = fdopen(pipe_wr[0], "r");
view_pipe_w = fdopen(pipe_wr[1], "w");
pthread_attr_init(&view_thread_attr);
pthread_create(&view_thread, &view_thread_attr, view_proc, view_pipe_r);
int pipe_wr2[] = {0, 0};
pipe(pipe_wr2);
command_pipe_r = fdopen(pipe_wr2[0], "r");
command_pipe_w = fdopen(pipe_wr2[1], "w");
pthread_attr_init(&command_thread_attr);
command_thread_started = false;
TQObject::connect(tqApp, TQ_SIGNAL(lastWindowClosed()), tqApp, TQ_SLOT(quit()));
TQObject::connect(tqApp, TQ_SIGNAL(aboutToQuit()), mainWin, TQ_SLOT(aboutToQuit()));
error_timer->start(100);
if(!calc_arg.isEmpty()) {
mainWin->execute_expression();
}
}
class QalculateApp : public TDEUniqueApplication {
public:
bool started;
QalculateApp() {started = false;}
virtual ~QalculateApp() {}
int newInstance() {
if(mainWin && started) {
TDECmdLineArgs* args = TDECmdLineArgs::parsedArgs();
TQString calc_arg = parse_expression_arguments(args);
args->clear();
#if TDE_VERSION_MAJOR < 4 && TDE_VERSION_MINOR < 2
KWin::Info info = KWin::info(mainWin->winId());
KWin::setOnDesktop(mainWin->winId(), KWin::currentDesktop());
mainWin->move(info.geometry.topLeft());
#else
KWin::WindowInfo info = KWin::windowInfo(mainWin->winId(), (unsigned long) NET::WMGeometry);
KWin::setOnDesktop(mainWin->winId(), KWin::currentDesktop());
mainWin->move(info.geometry().topLeft());
#endif
mainWin->setShown(true);
mainWin->show();
mainWin->raise();
#if TDE_VERSION_MAJOR < 4 && TDE_VERSION_MINOR < 2
KWin::setActiveWindow(mainWin->winId());
#else
KWin::activateWindow(mainWin->winId());
#endif
if(!calc_arg.isEmpty()) {
mainWin->expressionEdit->setText(calc_arg);
mainWin->execute_expression();
}
return 0;
} else {
if(isRestored() && TDEMainWindow::canBeRestored(1)) {
create_qalculate_window(this);
setMainWidget(mainWin);
mainWin->restore(1, false);
start_qalculate();
} else {
create_qalculate_window(this);
setMainWidget(mainWin);
mainWin->show();
start_qalculate();
}
started = true;
return TDEUniqueApplication::newInstance();
}
started = true;
return 0;
}
};
int main(int argc, char **argv) {
mainWin = NULL;
TDEAboutData about(PACKAGE, I18N_NOOP("Qalculate!"), version, description, TDEAboutData::License_GPL, "(C) 2005-2006 Niklas Knutsson", 0, "http://qalculate.sourceforge.net/", "[email protected]");
about.addAuthor("Niklas Knutsson", 0, "[email protected]");
TDECmdLineArgs::init(argc, argv, &about);
TDECmdLineArgs::addCmdLineOptions(options);
TDEUniqueApplication::addCmdLineOptions();
if(!QalculateApp::start()) {
return 0;
}
QalculateApp app;
return app.exec();
}
|