Suppressed error in window initialisation.
[mp-5.x.git] / mpv_qt4.cpp
blob6c5d81aae5a88b2a7098be3405ed48bc49475009
1 /*
3 Minimum Profit - Programmer Text Editor
5 Qt4 driver.
7 Copyright (C) 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 qt4_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>
42 /** data **/
44 class MPWindow : public QMainWindow
46 public:
47 MPWindow(QWidget *parent = 0);
48 bool queryExit(void);
49 bool event(QEvent *event);
51 QSettings *settings;
54 class MPArea : public QWidget
56 Q_OBJECT
58 public:
59 MPArea(QWidget *parent = 0);
60 void inputMethodEvent(QInputMethodEvent *event);
61 void keyPressEvent(QKeyEvent *event);
62 void keyReleaseEvent(QKeyEvent *event);
63 void mousePressEvent(QMouseEvent *event);
64 void mouseReleaseEvent(QMouseEvent *event);
65 void mouseMoveEvent(QMouseEvent *event);
66 void wheelEvent(QWheelEvent *event);
67 void dragEnterEvent(QDragEnterEvent *event);
68 void dropEvent(QDropEvent *event);
69 bool event(QEvent *event);
71 protected:
72 void paintEvent(QPaintEvent *event);
74 public slots:
75 void from_scrollbar(int);
76 void from_filetabs(int);
77 void from_menu(QAction *);
80 /* global data */
81 QApplication *app;
82 MPWindow *window;
83 QMenuBar *menubar;
84 //QStatusBar *statusbar;
85 QLabel *statusbar;
86 QTabBar *file_tabs;
88 #define MENU_CLASS QMenu
90 #include "mpv_qk_common.cpp"
92 static void draw_status(void)
94 statusbar->setText(str_to_qstring(mp_build_status_line()));
98 /** MPWindow methods **/
100 MPWindow::MPWindow(QWidget *parent) : QMainWindow(parent)
102 setWindowTitle("mp-5");
104 menubar = this->menuBar();
105 build_menu();
107 statusbar = new QLabel();
108 this->statusBar()->addWidget(statusbar);
110 /* the full container */
111 QVBoxLayout *vb = new QVBoxLayout();
113 file_tabs = new QTabBar();
114 file_tabs->setFocusPolicy(Qt::NoFocus);
116 QHBoxLayout *hb = new QHBoxLayout();
118 /* main area */
119 area = new MPArea();
120 scrollbar = new QScrollBar();
121 scrollbar->setFocusPolicy(Qt::NoFocus);
123 hb->addWidget(area);
124 hb->addWidget(scrollbar);
125 QWidget *cc = new QWidget();
126 cc->setLayout(hb);
128 vb->addWidget(file_tabs);
129 vb->addWidget(cc);
130 QWidget *mc = new QWidget();
131 mc->setLayout(vb);
133 setCentralWidget(mc);
135 connect(scrollbar, SIGNAL(valueChanged(int)),
136 area, SLOT(from_scrollbar(int)));
138 connect(file_tabs, SIGNAL(currentChanged(int)),
139 area, SLOT(from_filetabs(int)));
141 connect(menubar, SIGNAL(triggered(QAction *)),
142 area, SLOT(from_menu(QAction *)));
144 this->setWindowIcon(QIcon(QPixmap(mp_xpm)));
146 settings = new QSettings("triptico.com", "MinimumProfit");
148 QPoint pos = settings->value("pos", QPoint(20, 20)).toPoint();
149 QSize size = settings->value("size", QSize(600, 400)).toSize();
151 move(pos);
152 resize(size);
156 static void save_settings(MPWindow *w)
158 w->settings->setValue("pos", w->pos());
159 w->settings->setValue("size", w->size());
160 w->settings->sync();
164 bool MPWindow::queryExit(void)
166 mp_process_event(MPDM_LS(L"close-window"));
168 save_settings(this);
170 return mp_exit_requested ? true : false;
174 bool MPWindow::event(QEvent *event)
176 /* do the processing */
177 bool r = QWidget::event(event);
179 if (mp_exit_requested) {
180 save_settings(this);
181 exit(0);
184 return r;
188 /** driver functions **/
190 static mpdm_t qt4_drv_alert(mpdm_t a)
192 /* 1# arg: prompt */
193 QMessageBox::information(0, "mp " VERSION, str_to_qstring(mpdm_aget(a, 0)));
195 return NULL;
199 static mpdm_t qt4_drv_confirm(mpdm_t a)
201 int r;
203 /* 1# arg: prompt */
204 r = QMessageBox::question(0, "mp" VERSION,
205 str_to_qstring(mpdm_aget(a, 0)),
206 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel
209 switch (r) {
210 case QMessageBox::Yes:
211 r = 1;
212 break;
214 case QMessageBox::No:
215 r = 2;
216 break;
218 case QMessageBox::Cancel:
219 r = 0;
220 break;
223 return MPDM_I(r);
227 static mpdm_t qt4_drv_openfile(mpdm_t a)
229 QString r;
230 char tmp[128];
232 getcwd(tmp, sizeof(tmp));
234 /* 1# arg: prompt */
235 r = QFileDialog::getOpenFileName(window,
236 str_to_qstring(mpdm_aget(a, 0)), tmp);
238 return qstring_to_str(r);
242 static mpdm_t qt4_drv_savefile(mpdm_t a)
244 QString r;
245 char tmp[128];
247 getcwd(tmp, sizeof(tmp));
249 /* 1# arg: prompt */
250 r = QFileDialog::getSaveFileName(window,
251 str_to_qstring(mpdm_aget(a, 0)), tmp);
253 return qstring_to_str(r);
257 class MPForm : public QDialog
259 public:
260 QDialogButtonBox *button_box;
262 MPForm(QWidget *parent = 0) : QDialog(parent)
264 button_box = new QDialogButtonBox(QDialogButtonBox::Ok |
265 QDialogButtonBox::Cancel);
267 connect(button_box, SIGNAL(accepted()), this, SLOT(accept()));
268 connect(button_box, SIGNAL(rejected()), this, SLOT(reject()));
273 static mpdm_t qt4_drv_form(mpdm_t a)
275 int n;
276 mpdm_t widget_list;
277 QWidget *qlist[100];
278 mpdm_t r;
280 MPForm *dialog = new MPForm(window);
281 dialog->setWindowTitle("mp " VERSION);
283 dialog->setModal(true);
285 widget_list = mpdm_aget(a, 0);
287 QWidget *form = new QWidget();
288 QFormLayout *fl = new QFormLayout();
290 for (n = 0; n < mpdm_size(widget_list); n++) {
291 mpdm_t w = mpdm_aget(widget_list, n);
292 wchar_t *type;
293 mpdm_t t;
294 QLabel *ql = new QLabel("");
296 type = mpdm_string(mpdm_hget_s(w, L"type"));
298 if ((t = mpdm_hget_s(w, L"label")) != NULL) {
299 ql->setText(str_to_qstring(mpdm_gettext(t)));
302 t = mpdm_hget_s(w, L"value");
304 if (wcscmp(type, L"text") == 0) {
305 mpdm_t h;
306 QComboBox *qc = new QComboBox();
308 qc->setEditable(true);
309 qc->setMinimumContentsLength(30);
310 qc->setMaxVisibleItems(8);
312 if (t != NULL)
313 qc->setEditText(str_to_qstring(t));
315 qlist[n] = qc;
317 if ((h = mpdm_hget_s(w, L"history")) != NULL) {
318 int i;
320 /* has history; fill it */
321 h = mp_get_history(h);
323 for (i = mpdm_size(h) - 1; i >= 0; i--)
324 qc->addItem(str_to_qstring(mpdm_aget(h, i)));
327 fl->addRow(ql, qc);
329 else
330 if (wcscmp(type, L"password") == 0) {
331 QLineEdit *qe = new QLineEdit();
333 qe->setEchoMode(QLineEdit::Password);
335 qlist[n] = qe;
337 fl->addRow(ql, qe);
339 else
340 if (wcscmp(type, L"checkbox") == 0) {
341 QCheckBox *qc = new QCheckBox();
343 if (mpdm_ival(t))
344 qc->setCheckState(Qt::Checked);
346 qlist[n] = qc;
348 fl->addRow(ql, qc);
350 else
351 if (wcscmp(type, L"list") == 0) {
352 int i;
353 QListWidget *qlw = new QListWidget();
354 qlw->setMinimumWidth(480);
356 /* use a monospaced font */
357 qlw->setFont(QFont(QString("Mono")));
359 mpdm_t l = mpdm_hget_s(w, L"list");
361 for (i = 0; i < mpdm_size(l); i++)
362 qlw->addItem(str_to_qstring(mpdm_aget(l, i)));
364 qlw->setCurrentRow(mpdm_ival(t));
366 qlist[n] = qlw;
368 fl->addRow(ql, qlw);
371 if (n == 0)
372 qlist[n]->setFocus(Qt::OtherFocusReason);
375 form->setLayout(fl);
377 QVBoxLayout *ml = new QVBoxLayout();
378 ml->addWidget(form);
379 ml->addWidget(dialog->button_box);
381 dialog->setLayout(ml);
383 n = dialog->exec();
385 if (!n)
386 return NULL;
388 r = MPDM_A(mpdm_size(widget_list));
390 /* fill the return values */
391 for (n = 0; n < mpdm_size(widget_list); n++) {
392 mpdm_t w = mpdm_aget(widget_list, n);
393 mpdm_t v = NULL;
394 wchar_t *type;
396 type = mpdm_string(mpdm_hget_s(w, L"type"));
398 if (wcscmp(type, L"text") == 0) {
399 mpdm_t h;
400 QComboBox *ql = (QComboBox *)qlist[n];
402 v = qstring_to_str(ql->currentText());
404 /* if it has history, add to it */
405 if ((h = mpdm_hget_s(w, L"history")) != NULL &&
406 v != NULL && mpdm_cmp(v, MPDM_LS(L"")) != 0) {
407 h = mp_get_history(h);
409 if (mpdm_cmp(v, mpdm_aget(h, -1)) != 0)
410 mpdm_push(h, v);
413 else
414 if (wcscmp(type, L"password") == 0) {
415 QLineEdit *ql = (QLineEdit *)qlist[n];
417 v = qstring_to_str(ql->text());
419 else
420 if (wcscmp(type, L"checkbox") == 0) {
421 QCheckBox *qb = (QCheckBox *)qlist[n];
423 v = MPDM_I(qb->checkState() == Qt::Checked);
425 else
426 if (wcscmp(type, L"list") == 0) {
427 QListWidget *ql = (QListWidget *)qlist[n];
429 v = MPDM_I(ql->currentRow());
432 mpdm_aset(r, v, n);
435 return r;
439 static void register_functions(void)
441 mpdm_t drv;
443 drv = mpdm_hget_s(mp, L"drv");
444 mpdm_hset_s(drv, L"main_loop", MPDM_X(qt4_drv_main_loop));
445 mpdm_hset_s(drv, L"shutdown", MPDM_X(qt4_drv_shutdown));
447 mpdm_hset_s(drv, L"clip_to_sys", MPDM_X(qt4_drv_clip_to_sys));
448 mpdm_hset_s(drv, L"sys_to_clip", MPDM_X(qt4_drv_sys_to_clip));
449 mpdm_hset_s(drv, L"update_ui", MPDM_X(qt4_drv_update_ui));
450 /* mpdm_hset_s(drv, L"timer", MPDM_X(qt4_drv_timer));*/
451 mpdm_hset_s(drv, L"busy", MPDM_X(qt4_drv_busy));
453 mpdm_hset_s(drv, L"alert", MPDM_X(qt4_drv_alert));
454 mpdm_hset_s(drv, L"confirm", MPDM_X(qt4_drv_confirm));
455 mpdm_hset_s(drv, L"openfile", MPDM_X(qt4_drv_openfile));
456 mpdm_hset_s(drv, L"savefile", MPDM_X(qt4_drv_savefile));
457 mpdm_hset_s(drv, L"form", MPDM_X(qt4_drv_form));
461 static mpdm_t qt4_drv_startup(mpdm_t a)
462 /* driver initialization */
464 register_functions();
466 build_font(1);
467 build_colors();
469 window = new MPWindow();
470 window->show();
472 return NULL;
475 extern "C" Display *XOpenDisplay(char *);
477 extern "C" int qt4_drv_detect(int * argc, char *** argv)
479 mpdm_t drv;
480 Display *x11_display;
482 /* try connecting directly to the Xserver */
483 if ((x11_display = XOpenDisplay((char *)NULL)) == NULL)
484 return 0;
486 /* this is where it crashes if no X server */
487 app = new QApplication(x11_display);
489 drv = mpdm_hget_s(mp, L"drv");
490 mpdm_hset_s(drv, L"id", MPDM_LS(L"qt4"));
491 mpdm_hset_s(drv, L"startup", MPDM_X(qt4_drv_startup));
493 return 1;
496 #include "mpv_qt4.moc"