Suppressed error in window initialisation.
[mp-5.x.git] / mpv_kde4.cpp
blob8dac3c65975f5d283e34465402086342df9fb843
1 /*
3 Minimum Profit - Programmer Text Editor
5 KDE4 driver.
7 Copyright (C) 2008-2009 Angel Ortega <angel@triptico.com>
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 http://www.triptico.com
27 /* override auto-generated definition in config.h */
28 extern "C" int kde4_drv_detect(int * argc, char *** argv);
30 #include "config.h"
32 #include <stdio.h>
33 #include <wchar.h>
34 #include <unistd.h>
35 #include "mpdm.h"
36 #include "mpsl.h"
37 #include "mp.h"
38 #include "mp.xpm"
40 #include <QtGui/QKeyEvent>
41 #include <QtGui/QPainter>
42 #include <QtGui/QMenu>
44 #include <QtGui/QLabel>
45 #include <QtGui/QComboBox>
46 #include <QtGui/QLineEdit>
47 #include <QtGui/QCheckBox>
48 #include <QtGui/QListWidget>
49 #include <QtGui/QScrollBar>
50 #include <QtGui/QClipboard>
52 #include <KApplication>
53 #include <KAboutData>
54 #include <KCmdLineArgs>
56 #include <KMainWindow>
57 #include <KMenuBar>
58 #include <KStatusBar>
59 #include <KMenu>
60 #include <KTabBar>
62 #include <KVBox>
63 #include <KHBox>
65 #include <KDialog>
66 #include <KMessageBox>
67 #include <KFileDialog>
68 #include <KUrl>
70 /** data **/
72 class MPWindow : public KMainWindow
74 public:
75 MPWindow(QWidget *parent = 0);
76 bool queryExit(void);
77 bool event(QEvent *event);
80 class MPArea : public QWidget
82 Q_OBJECT
84 public:
85 MPArea(QWidget *parent = 0);
86 void inputMethodEvent(QInputMethodEvent *event);
87 void keyPressEvent(QKeyEvent *event);
88 void keyReleaseEvent(QKeyEvent *event);
89 void mousePressEvent(QMouseEvent *event);
90 void mouseReleaseEvent(QMouseEvent *event);
91 void mouseMoveEvent(QMouseEvent *event);
92 void wheelEvent(QWheelEvent *event);
93 void dragEnterEvent(QDragEnterEvent *event);
94 void dropEvent(QDropEvent *event);
95 bool event(QEvent *event);
97 protected:
98 void paintEvent(QPaintEvent *event);
100 public slots:
101 void from_scrollbar(int);
102 void from_filetabs(int);
103 void from_menu(QAction *);
106 /* global data */
107 KApplication *app;
108 MPWindow *window;
109 KMenuBar *menubar;
110 KStatusBar *statusbar;
111 KTabBar *file_tabs;
113 #define MENU_CLASS KMenu
115 #include "mpv_qk_common.cpp"
117 static void draw_status(void)
119 statusbar->changeItem(str_to_qstring(mp_build_status_line()), 0);
122 /** MPWindow methods **/
124 MPWindow::MPWindow(QWidget *parent) : KMainWindow(parent)
126 menubar = this->menuBar();
127 build_menu();
129 statusbar = this->statusBar();
130 statusbar->insertItem("mp " VERSION, 0);
132 /* the full container */
133 KVBox *vb = new KVBox(this);
135 file_tabs = new KTabBar(vb);
136 file_tabs->setFocusPolicy(Qt::NoFocus);
138 KHBox *hb = new KHBox(vb);
140 /* main area */
141 area = new MPArea(hb);
142 scrollbar = new QScrollBar(hb);
143 scrollbar->setFocusPolicy(Qt::NoFocus);
145 setCentralWidget(vb);
147 connect(scrollbar, SIGNAL(valueChanged(int)),
148 area, SLOT(from_scrollbar(int)));
150 connect(file_tabs, SIGNAL(currentChanged(int)),
151 area, SLOT(from_filetabs(int)));
153 connect(menubar, SIGNAL(triggered(QAction *)),
154 area, SLOT(from_menu(QAction *)));
156 this->setWindowIcon(QIcon(QPixmap(mp_xpm)));
158 this->setAutoSaveSettings(QLatin1String("MinimumProfit"), true);
162 bool MPWindow::queryExit(void)
164 mp_process_event(MPDM_LS(L"close-window"));
166 this->saveAutoSaveSettings();
168 return mp_exit_requested ? true : false;
172 bool MPWindow::event(QEvent *event)
174 /* do the processing */
175 bool r = QWidget::event(event);
177 if (mp_exit_requested) {
178 this->saveAutoSaveSettings();
179 exit(0);
182 return r;
186 /** driver functions **/
188 static mpdm_t kde4_drv_update_ui(mpdm_t a)
190 return qt4_drv_update_ui(a);
194 static mpdm_t kde4_drv_alert(mpdm_t a)
195 /* alert driver function */
197 /* 1# arg: prompt */
198 KMessageBox::information(0, str_to_qstring(mpdm_aget(a, 0)),
199 i18n("mp " VERSION));
201 return NULL;
204 static mpdm_t kde4_drv_confirm(mpdm_t a)
205 /* confirm driver function */
207 int r;
209 /* 1# arg: prompt */
210 r = KMessageBox::questionYesNoCancel(0,
211 str_to_qstring(mpdm_aget(a, 0)), i18n("mp" VERSION));
213 switch (r) {
214 case KMessageBox::Yes:
215 r = 1;
216 break;
218 case KMessageBox::No:
219 r = 2;
220 break;
222 case KMessageBox::Cancel:
223 r = 0;
224 break;
227 return MPDM_I(r);
231 static mpdm_t kde4_drv_openfile(mpdm_t a)
233 QString r;
234 char tmp[128];
236 getcwd(tmp, sizeof(tmp));
238 /* 1# arg: prompt */
239 r = KFileDialog::getOpenFileName(KUrl::fromPath(tmp), "*", 0,
240 str_to_qstring(mpdm_aget(a, 0)));
242 return qstring_to_str(r);
246 static mpdm_t kde4_drv_savefile(mpdm_t a)
248 QString r;
249 char tmp[128];
251 getcwd(tmp, sizeof(tmp));
253 /* 1# arg: prompt */
254 r = KFileDialog::getSaveFileName(KUrl::fromPath(tmp), "*", 0,
255 str_to_qstring(mpdm_aget(a, 0)));
257 return qstring_to_str(r);
261 static mpdm_t kde4_drv_form(mpdm_t a)
263 int n;
264 mpdm_t widget_list;
265 QWidget *qlist[100];
266 mpdm_t r;
268 KDialog *dialog = new KDialog(window);
270 dialog->setModal(true);
271 dialog->setButtons(KDialog::Ok | KDialog::Cancel);
273 widget_list = mpdm_aget(a, 0);
275 KVBox *vb = new KVBox(dialog);
276 dialog->setMainWidget(vb);
278 for (n = 0; n < mpdm_size(widget_list); n++) {
279 mpdm_t w = mpdm_aget(widget_list, n);
280 wchar_t *type;
281 mpdm_t t;
282 KHBox *hb = new KHBox(vb);
284 type = mpdm_string(mpdm_hget_s(w, L"type"));
286 if ((t = mpdm_hget_s(w, L"label")) != NULL) {
287 QLabel *ql = new QLabel(hb);
288 ql->setText(str_to_qstring(mpdm_gettext(t)));
291 t = mpdm_hget_s(w, L"value");
293 if (wcscmp(type, L"text") == 0) {
294 mpdm_t h;
295 QComboBox *ql = new QComboBox(hb);
297 ql->setEditable(true);
298 ql->setMinimumContentsLength(30);
299 ql->setMaxVisibleItems(8);
301 if (t != NULL)
302 ql->setEditText(str_to_qstring(t));
304 qlist[n] = ql;
306 if ((h = mpdm_hget_s(w, L"history")) != NULL) {
307 int i;
309 /* has history; fill it */
310 h = mp_get_history(h);
312 for (i = mpdm_size(h) - 1; i >= 0; i--)
313 ql->addItem(str_to_qstring(mpdm_aget(h, i)));
316 else
317 if (wcscmp(type, L"password") == 0) {
318 QLineEdit *ql = new QLineEdit(hb);
320 ql->setEchoMode(QLineEdit::Password);
322 qlist[n] = ql;
324 else
325 if (wcscmp(type, L"checkbox") == 0) {
326 QCheckBox *qc = new QCheckBox(hb);
328 if (mpdm_ival(t))
329 qc->setCheckState(Qt::Checked);
331 qlist[n] = qc;
333 else
334 if (wcscmp(type, L"list") == 0) {
335 int i;
336 QListWidget *ql = new QListWidget(hb);
337 ql->setMinimumWidth(480);
339 /* use a monospaced font */
340 ql->setFont(QFont(QString("Mono")));
342 mpdm_t l = mpdm_hget_s(w, L"list");
344 for (i = 0; i < mpdm_size(l); i++)
345 ql->addItem(str_to_qstring(mpdm_aget(l, i)));
347 ql->setCurrentRow(mpdm_ival(t));
349 qlist[n] = ql;
352 if (n == 0)
353 qlist[n]->setFocus(Qt::OtherFocusReason);
356 n = dialog->exec();
358 if (!n)
359 return NULL;
361 r = MPDM_A(mpdm_size(widget_list));
363 /* fill the return values */
364 for (n = 0; n < mpdm_size(widget_list); n++) {
365 mpdm_t w = mpdm_aget(widget_list, n);
366 mpdm_t v = NULL;
367 wchar_t *type;
369 type = mpdm_string(mpdm_hget_s(w, L"type"));
371 if (wcscmp(type, L"text") == 0) {
372 mpdm_t h;
373 QComboBox *ql = (QComboBox *)qlist[n];
375 v = qstring_to_str(ql->currentText());
377 /* if it has history, add to it */
378 if ((h = mpdm_hget_s(w, L"history")) != NULL &&
379 v != NULL && mpdm_cmp(v, MPDM_LS(L"")) != 0) {
380 h = mp_get_history(h);
382 if (mpdm_cmp(v, mpdm_aget(h, -1)) != 0)
383 mpdm_push(h, v);
386 else
387 if (wcscmp(type, L"password") == 0) {
388 QLineEdit *ql = (QLineEdit *)qlist[n];
390 v = qstring_to_str(ql->text());
392 else
393 if (wcscmp(type, L"checkbox") == 0) {
394 QCheckBox *qb = (QCheckBox *)qlist[n];
396 v = MPDM_I(qb->checkState() == Qt::Checked);
398 else
399 if (wcscmp(type, L"list") == 0) {
400 QListWidget *ql = (QListWidget *)qlist[n];
402 v = MPDM_I(ql->currentRow());
405 mpdm_aset(r, v, n);
408 return r;
412 static mpdm_t kde4_drv_busy(mpdm_t a)
414 return qt4_drv_busy(a);
418 static mpdm_t kde4_drv_main_loop(mpdm_t a)
420 return qt4_drv_main_loop(a);
424 static mpdm_t kde4_drv_shutdown(mpdm_t a)
426 return qt4_drv_shutdown(a);
430 static mpdm_t kde4_drv_clip_to_sys(mpdm_t a)
432 return qt4_drv_clip_to_sys(a);
436 static mpdm_t kde4_drv_sys_to_clip(mpdm_t a)
438 return qt4_drv_sys_to_clip(a);
442 static void register_functions(void)
444 mpdm_t drv;
446 drv = mpdm_hget_s(mp, L"drv");
447 mpdm_hset_s(drv, L"main_loop", MPDM_X(kde4_drv_main_loop));
448 mpdm_hset_s(drv, L"shutdown", MPDM_X(kde4_drv_shutdown));
450 mpdm_hset_s(drv, L"clip_to_sys", MPDM_X(kde4_drv_clip_to_sys));
451 mpdm_hset_s(drv, L"sys_to_clip", MPDM_X(kde4_drv_sys_to_clip));
452 mpdm_hset_s(drv, L"update_ui", MPDM_X(kde4_drv_update_ui));
453 /* mpdm_hset_s(drv, L"timer", MPDM_X(kde4_drv_timer));*/
454 mpdm_hset_s(drv, L"busy", MPDM_X(kde4_drv_busy));
456 mpdm_hset_s(drv, L"alert", MPDM_X(kde4_drv_alert));
457 mpdm_hset_s(drv, L"confirm", MPDM_X(kde4_drv_confirm));
458 mpdm_hset_s(drv, L"openfile", MPDM_X(kde4_drv_openfile));
459 mpdm_hset_s(drv, L"savefile", MPDM_X(kde4_drv_savefile));
460 mpdm_hset_s(drv, L"form", MPDM_X(kde4_drv_form));
464 static mpdm_t kde4_drv_startup(mpdm_t a)
465 /* driver initialization */
467 register_functions();
469 build_font(1);
470 build_colors();
472 window = new MPWindow();
473 window->show();
475 return NULL;
478 extern "C" Display *XOpenDisplay(char *);
480 extern "C" int kde4_drv_detect(int * argc, char *** argv)
482 mpdm_t drv;
483 KCmdLineOptions opts;
484 Display *x11_display;
486 /* try connecting directly to the Xserver */
487 if ((x11_display = XOpenDisplay((char *)NULL)) == NULL)
488 return 0;
490 KAboutData aboutData(
491 "mp", 0,
492 ki18n("Minimum Profit"), VERSION,
493 ki18n("A programmer's text editor"),
494 KAboutData::License_GPL,
495 ki18n("Copyright (c) 1991-2009 Angel Ortega"),
496 ki18n(""),
497 "http://triptico.com",
498 "angel@triptico.com"
501 KCmdLineArgs::init(*argc, *argv, &aboutData);
503 /* command line options should be inserted here (I don't like this) */
504 opts.add("t {tag}", ki18n("Edits the file where tag is defined"));
505 opts.add("e {mpsl_code}", ki18n("Executes MPSL code"));
506 opts.add("f {mpsl_script}", ki18n("Executes MPSL script file"));
507 opts.add("d {directory}", ki18n("Sets working directory"));
508 opts.add("x {file}", ki18n("Open file in the hexadecimal viewer"));
509 opts.add(" +NNN", ki18n("Moves to line number NNN of last file"));
510 opts.add("+[file(s)]", ki18n("Documents to open"));
511 KCmdLineArgs::addCmdLineOptions(opts);
513 /* this is where it crashes if no X server */
514 app = new KApplication(x11_display);
516 drv = mpdm_hget_s(mp, L"drv");
517 mpdm_hset_s(drv, L"id", MPDM_LS(L"kde4"));
518 mpdm_hset_s(drv, L"startup", MPDM_X(kde4_drv_startup));
520 return 1;
523 #include "mpv_kde4.moc"