Qt4 forms finally work.
[mp-5.x.git] / mpv_qt4.cpp
bloba5f2d5b707d6060db8566505fcb5d260f9333e78
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 QTabBar *file_tabs;
87 #define MENU_CLASS QMenu
89 #include "mpv_qk_common.cpp"
91 static void draw_status(void)
93 statusbar->showMessage(str_to_qstring(mp_build_status_line()));
97 /** MPWindow methods **/
99 MPWindow::MPWindow(QWidget *parent) : QMainWindow(parent)
101 setWindowTitle("mp-5");
103 menubar = this->menuBar();
104 build_menu();
106 statusbar = this->statusBar();
108 /* the full container */
109 QVBoxLayout *vb = new QVBoxLayout(this);
111 file_tabs = new QTabBar();
112 file_tabs->setFocusPolicy(Qt::NoFocus);
114 QHBoxLayout *hb = new QHBoxLayout();
116 /* main area */
117 area = new MPArea();
118 scrollbar = new QScrollBar();
119 scrollbar->setFocusPolicy(Qt::NoFocus);
121 hb->addWidget(area);
122 hb->addWidget(scrollbar);
123 QWidget *cc = new QWidget();
124 cc->setLayout(hb);
126 vb->addWidget(file_tabs);
127 vb->addWidget(cc);
128 QWidget *mc = new QWidget();
129 mc->setLayout(vb);
131 setCentralWidget(mc);
133 connect(scrollbar, SIGNAL(valueChanged(int)),
134 area, SLOT(from_scrollbar(int)));
136 connect(file_tabs, SIGNAL(currentChanged(int)),
137 area, SLOT(from_filetabs(int)));
139 connect(menubar, SIGNAL(triggered(QAction *)),
140 area, SLOT(from_menu(QAction *)));
142 this->setWindowIcon(QIcon(QPixmap(mp_xpm)));
144 settings = new QSettings("triptico.com", "MinimumProfit");
146 QPoint pos = settings->value("pos", QPoint(20, 20)).toPoint();
147 QSize size = settings->value("size", QSize(600, 400)).toSize();
149 move(pos);
150 resize(size);
154 static void save_settings(MPWindow *w)
156 w->settings->setValue("pos", w->pos());
157 w->settings->setValue("size", w->size());
158 w->settings->sync();
162 bool MPWindow::queryExit(void)
164 mp_process_event(MPDM_LS(L"close-window"));
166 save_settings(this);
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 save_settings(this);
179 exit(0);
182 return r;
186 /** driver functions **/
188 static mpdm_t qt4_drv_alert(mpdm_t a)
190 /* 1# arg: prompt */
191 QMessageBox::information(0, "mp " VERSION, str_to_qstring(mpdm_aget(a, 0)));
193 return NULL;
197 static mpdm_t qt4_drv_confirm(mpdm_t a)
199 int r;
201 /* 1# arg: prompt */
202 r = QMessageBox::question(0, "mp" VERSION,
203 str_to_qstring(mpdm_aget(a, 0)),
204 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel
207 switch (r) {
208 case QMessageBox::Yes:
209 r = 1;
210 break;
212 case QMessageBox::No:
213 r = 2;
214 break;
216 case QMessageBox::Cancel:
217 r = 0;
218 break;
221 return MPDM_I(r);
225 static mpdm_t qt4_drv_openfile(mpdm_t a)
227 QString r;
228 char tmp[128];
230 getcwd(tmp, sizeof(tmp));
232 /* 1# arg: prompt */
233 r = QFileDialog::getOpenFileName(window,
234 str_to_qstring(mpdm_aget(a, 0)), tmp);
236 return qstring_to_str(r);
240 static mpdm_t qt4_drv_savefile(mpdm_t a)
242 QString r;
243 char tmp[128];
245 getcwd(tmp, sizeof(tmp));
247 /* 1# arg: prompt */
248 r = QFileDialog::getSaveFileName(window,
249 str_to_qstring(mpdm_aget(a, 0)), tmp);
251 return qstring_to_str(r);
255 class MPForm : public QDialog
257 public:
258 QDialogButtonBox *button_box;
260 MPForm(QWidget *parent = 0) : QDialog(parent)
262 button_box = new QDialogButtonBox(QDialogButtonBox::Ok |
263 QDialogButtonBox::Cancel);
265 connect(button_box, SIGNAL(accepted()), this, SLOT(accept()));
266 connect(button_box, SIGNAL(rejected()), this, SLOT(reject()));
271 static mpdm_t qt4_drv_form(mpdm_t a)
273 int n;
274 mpdm_t widget_list;
275 QWidget *qlist[100];
276 mpdm_t r;
278 MPForm *dialog = new MPForm(window);
279 dialog->setWindowTitle("mp " VERSION);
281 dialog->setModal(true);
283 widget_list = mpdm_aget(a, 0);
285 QWidget *form = new QWidget();
286 QFormLayout *fl = new QFormLayout();
288 for (n = 0; n < mpdm_size(widget_list); n++) {
289 mpdm_t w = mpdm_aget(widget_list, n);
290 wchar_t *type;
291 mpdm_t t;
292 QLabel *ql = new QLabel("");
294 type = mpdm_string(mpdm_hget_s(w, L"type"));
296 if ((t = mpdm_hget_s(w, L"label")) != NULL) {
297 ql->setText(str_to_qstring(mpdm_gettext(t)));
300 t = mpdm_hget_s(w, L"value");
302 if (wcscmp(type, L"text") == 0) {
303 mpdm_t h;
304 QComboBox *qc = new QComboBox();
306 qc->setEditable(true);
307 qc->setMinimumContentsLength(30);
308 qc->setMaxVisibleItems(8);
310 if (t != NULL)
311 qc->setEditText(str_to_qstring(t));
313 qlist[n] = qc;
315 if ((h = mpdm_hget_s(w, L"history")) != NULL) {
316 int i;
318 /* has history; fill it */
319 h = mp_get_history(h);
321 for (i = mpdm_size(h) - 1; i >= 0; i--)
322 qc->addItem(str_to_qstring(mpdm_aget(h, i)));
325 fl->addRow(ql, qc);
327 else
328 if (wcscmp(type, L"password") == 0) {
329 QLineEdit *qe = new QLineEdit();
331 qe->setEchoMode(QLineEdit::Password);
333 qlist[n] = qe;
335 fl->addRow(ql, qe);
337 else
338 if (wcscmp(type, L"checkbox") == 0) {
339 QCheckBox *qc = new QCheckBox();
341 if (mpdm_ival(t))
342 qc->setCheckState(Qt::Checked);
344 qlist[n] = qc;
346 fl->addRow(ql, qc);
348 else
349 if (wcscmp(type, L"list") == 0) {
350 int i;
351 QListWidget *qlw = new QListWidget();
352 qlw->setMinimumWidth(480);
354 /* use a monospaced font */
355 qlw->setFont(QFont(QString("Mono")));
357 mpdm_t l = mpdm_hget_s(w, L"list");
359 for (i = 0; i < mpdm_size(l); i++)
360 qlw->addItem(str_to_qstring(mpdm_aget(l, i)));
362 qlw->setCurrentRow(mpdm_ival(t));
364 qlist[n] = qlw;
366 fl->addRow(ql, qlw);
369 if (n == 0)
370 qlist[n]->setFocus(Qt::OtherFocusReason);
373 form->setLayout(fl);
375 QVBoxLayout *ml = new QVBoxLayout();
376 ml->addWidget(form);
377 ml->addWidget(dialog->button_box);
379 dialog->setLayout(ml);
381 n = dialog->exec();
383 if (!n)
384 return NULL;
386 r = MPDM_A(mpdm_size(widget_list));
388 /* fill the return values */
389 for (n = 0; n < mpdm_size(widget_list); n++) {
390 mpdm_t w = mpdm_aget(widget_list, n);
391 mpdm_t v = NULL;
392 wchar_t *type;
394 type = mpdm_string(mpdm_hget_s(w, L"type"));
396 if (wcscmp(type, L"text") == 0) {
397 mpdm_t h;
398 QComboBox *ql = (QComboBox *)qlist[n];
400 v = qstring_to_str(ql->currentText());
402 /* if it has history, add to it */
403 if ((h = mpdm_hget_s(w, L"history")) != NULL &&
404 v != NULL && mpdm_cmp(v, MPDM_LS(L"")) != 0) {
405 h = mp_get_history(h);
407 if (mpdm_cmp(v, mpdm_aget(h, -1)) != 0)
408 mpdm_push(h, v);
411 else
412 if (wcscmp(type, L"password") == 0) {
413 QLineEdit *ql = (QLineEdit *)qlist[n];
415 v = qstring_to_str(ql->text());
417 else
418 if (wcscmp(type, L"checkbox") == 0) {
419 QCheckBox *qb = (QCheckBox *)qlist[n];
421 v = MPDM_I(qb->checkState() == Qt::Checked);
423 else
424 if (wcscmp(type, L"list") == 0) {
425 QListWidget *ql = (QListWidget *)qlist[n];
427 v = MPDM_I(ql->currentRow());
430 mpdm_aset(r, v, n);
433 return r;
437 static void register_functions(void)
439 mpdm_t drv;
441 drv = mpdm_hget_s(mp, L"drv");
442 mpdm_hset_s(drv, L"main_loop", MPDM_X(qt4_drv_main_loop));
443 mpdm_hset_s(drv, L"shutdown", MPDM_X(qt4_drv_shutdown));
445 mpdm_hset_s(drv, L"clip_to_sys", MPDM_X(qt4_drv_clip_to_sys));
446 mpdm_hset_s(drv, L"sys_to_clip", MPDM_X(qt4_drv_sys_to_clip));
447 mpdm_hset_s(drv, L"update_ui", MPDM_X(qt4_drv_update_ui));
448 /* mpdm_hset_s(drv, L"timer", MPDM_X(qt4_drv_timer));*/
449 mpdm_hset_s(drv, L"busy", MPDM_X(qt4_drv_busy));
451 mpdm_hset_s(drv, L"alert", MPDM_X(qt4_drv_alert));
452 mpdm_hset_s(drv, L"confirm", MPDM_X(qt4_drv_confirm));
453 mpdm_hset_s(drv, L"openfile", MPDM_X(qt4_drv_openfile));
454 mpdm_hset_s(drv, L"savefile", MPDM_X(qt4_drv_savefile));
455 mpdm_hset_s(drv, L"form", MPDM_X(qt4_drv_form));
459 static mpdm_t qt4_drv_startup(mpdm_t a)
460 /* driver initialization */
462 register_functions();
464 build_font(1);
465 build_colors();
467 window = new MPWindow();
468 window->show();
470 return NULL;
473 extern "C" Display *XOpenDisplay(char *);
475 extern "C" int qt4_drv_detect(int * argc, char *** argv)
477 mpdm_t drv;
478 Display *x11_display;
480 /* try connecting directly to the Xserver */
481 if ((x11_display = XOpenDisplay((char *)NULL)) == NULL)
482 return 0;
484 /* this is where it crashes if no X server */
485 app = new QApplication(x11_display);
487 drv = mpdm_hget_s(mp, L"drv");
488 mpdm_hset_s(drv, L"id", MPDM_LS(L"qt4"));
489 mpdm_hset_s(drv, L"startup", MPDM_X(qt4_drv_startup));
491 return 1;
494 #include "mpv_qt4.moc"