Version 5.1.3 RELEASED.
[mp-5.x.git] / mpv_qt4.cpp
blob7a856704993d704eda55ba987fa02a8dd63b8246
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 qt4_drv_shutdown(NULL);
182 exit(0);
185 return r;
189 /** driver functions **/
191 static mpdm_t qt4_drv_alert(mpdm_t a)
193 /* 1# arg: prompt */
194 QMessageBox::information(0, "mp " VERSION, str_to_qstring(mpdm_aget(a, 0)));
196 return NULL;
200 static mpdm_t qt4_drv_confirm(mpdm_t a)
202 int r;
204 /* 1# arg: prompt */
205 r = QMessageBox::question(0, "mp" VERSION,
206 str_to_qstring(mpdm_aget(a, 0)),
207 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel
210 switch (r) {
211 case QMessageBox::Yes:
212 r = 1;
213 break;
215 case QMessageBox::No:
216 r = 2;
217 break;
219 case QMessageBox::Cancel:
220 r = 0;
221 break;
224 return MPDM_I(r);
228 static mpdm_t qt4_drv_openfile(mpdm_t a)
230 QString r;
231 char tmp[128];
233 getcwd(tmp, sizeof(tmp));
235 /* 1# arg: prompt */
236 r = QFileDialog::getOpenFileName(window,
237 str_to_qstring(mpdm_aget(a, 0)), tmp);
239 return qstring_to_str(r);
243 static mpdm_t qt4_drv_savefile(mpdm_t a)
245 QString r;
246 char tmp[128];
248 getcwd(tmp, sizeof(tmp));
250 /* 1# arg: prompt */
251 r = QFileDialog::getSaveFileName(window,
252 str_to_qstring(mpdm_aget(a, 0)), tmp);
254 return qstring_to_str(r);
258 class MPForm : public QDialog
260 public:
261 QDialogButtonBox *button_box;
263 MPForm(QWidget *parent = 0) : QDialog(parent)
265 button_box = new QDialogButtonBox(QDialogButtonBox::Ok |
266 QDialogButtonBox::Cancel);
268 connect(button_box, SIGNAL(accepted()), this, SLOT(accept()));
269 connect(button_box, SIGNAL(rejected()), this, SLOT(reject()));
274 static mpdm_t qt4_drv_form(mpdm_t a)
276 int n;
277 mpdm_t widget_list;
278 QWidget *qlist[100];
279 mpdm_t r;
281 MPForm *dialog = new MPForm(window);
282 dialog->setWindowTitle("mp " VERSION);
284 dialog->setModal(true);
286 widget_list = mpdm_aget(a, 0);
288 QWidget *form = new QWidget();
289 QFormLayout *fl = new QFormLayout();
291 for (n = 0; n < mpdm_size(widget_list); n++) {
292 mpdm_t w = mpdm_aget(widget_list, n);
293 wchar_t *type;
294 mpdm_t t;
295 QLabel *ql = new QLabel("");
297 type = mpdm_string(mpdm_hget_s(w, L"type"));
299 if ((t = mpdm_hget_s(w, L"label")) != NULL) {
300 ql->setText(str_to_qstring(mpdm_gettext(t)));
303 t = mpdm_hget_s(w, L"value");
305 if (wcscmp(type, L"text") == 0) {
306 mpdm_t h;
307 QComboBox *qc = new QComboBox();
309 qc->setEditable(true);
310 qc->setMinimumContentsLength(30);
311 qc->setMaxVisibleItems(8);
313 if (t != NULL)
314 qc->setEditText(str_to_qstring(t));
316 qlist[n] = qc;
318 if ((h = mpdm_hget_s(w, L"history")) != NULL) {
319 int i;
321 /* has history; fill it */
322 h = mp_get_history(h);
324 for (i = mpdm_size(h) - 1; i >= 0; i--)
325 qc->addItem(str_to_qstring(mpdm_aget(h, i)));
328 fl->addRow(ql, qc);
330 else
331 if (wcscmp(type, L"password") == 0) {
332 QLineEdit *qe = new QLineEdit();
334 qe->setEchoMode(QLineEdit::Password);
336 qlist[n] = qe;
338 fl->addRow(ql, qe);
340 else
341 if (wcscmp(type, L"checkbox") == 0) {
342 QCheckBox *qc = new QCheckBox();
344 if (mpdm_ival(t))
345 qc->setCheckState(Qt::Checked);
347 qlist[n] = qc;
349 fl->addRow(ql, qc);
351 else
352 if (wcscmp(type, L"list") == 0) {
353 int i;
354 QListWidget *qlw = new QListWidget();
355 qlw->setMinimumWidth(480);
357 /* use a monospaced font */
358 qlw->setFont(QFont(QString("Mono")));
360 mpdm_t l = mpdm_hget_s(w, L"list");
362 for (i = 0; i < mpdm_size(l); i++)
363 qlw->addItem(str_to_qstring(mpdm_aget(l, i)));
365 qlw->setCurrentRow(mpdm_ival(t));
367 qlist[n] = qlw;
369 fl->addRow(ql, qlw);
372 if (n == 0)
373 qlist[n]->setFocus(Qt::OtherFocusReason);
376 form->setLayout(fl);
378 QVBoxLayout *ml = new QVBoxLayout();
379 ml->addWidget(form);
380 ml->addWidget(dialog->button_box);
382 dialog->setLayout(ml);
384 n = dialog->exec();
386 if (!n)
387 return NULL;
389 r = MPDM_A(mpdm_size(widget_list));
391 /* fill the return values */
392 for (n = 0; n < mpdm_size(widget_list); n++) {
393 mpdm_t w = mpdm_aget(widget_list, n);
394 mpdm_t v = NULL;
395 wchar_t *type;
397 type = mpdm_string(mpdm_hget_s(w, L"type"));
399 if (wcscmp(type, L"text") == 0) {
400 mpdm_t h;
401 QComboBox *ql = (QComboBox *)qlist[n];
403 v = qstring_to_str(ql->currentText());
405 /* if it has history, add to it */
406 if ((h = mpdm_hget_s(w, L"history")) != NULL &&
407 v != NULL && mpdm_cmp(v, MPDM_LS(L"")) != 0) {
408 h = mp_get_history(h);
410 if (mpdm_cmp(v, mpdm_aget(h, -1)) != 0)
411 mpdm_push(h, v);
414 else
415 if (wcscmp(type, L"password") == 0) {
416 QLineEdit *ql = (QLineEdit *)qlist[n];
418 v = qstring_to_str(ql->text());
420 else
421 if (wcscmp(type, L"checkbox") == 0) {
422 QCheckBox *qb = (QCheckBox *)qlist[n];
424 v = MPDM_I(qb->checkState() == Qt::Checked);
426 else
427 if (wcscmp(type, L"list") == 0) {
428 QListWidget *ql = (QListWidget *)qlist[n];
430 v = MPDM_I(ql->currentRow());
433 mpdm_aset(r, v, n);
436 return r;
440 static void register_functions(void)
442 mpdm_t drv;
444 drv = mpdm_hget_s(mp, L"drv");
445 mpdm_hset_s(drv, L"main_loop", MPDM_X(qt4_drv_main_loop));
446 mpdm_hset_s(drv, L"shutdown", MPDM_X(qt4_drv_shutdown));
448 mpdm_hset_s(drv, L"clip_to_sys", MPDM_X(qt4_drv_clip_to_sys));
449 mpdm_hset_s(drv, L"sys_to_clip", MPDM_X(qt4_drv_sys_to_clip));
450 mpdm_hset_s(drv, L"update_ui", MPDM_X(qt4_drv_update_ui));
451 /* mpdm_hset_s(drv, L"timer", MPDM_X(qt4_drv_timer));*/
452 mpdm_hset_s(drv, L"busy", MPDM_X(qt4_drv_busy));
454 mpdm_hset_s(drv, L"alert", MPDM_X(qt4_drv_alert));
455 mpdm_hset_s(drv, L"confirm", MPDM_X(qt4_drv_confirm));
456 mpdm_hset_s(drv, L"openfile", MPDM_X(qt4_drv_openfile));
457 mpdm_hset_s(drv, L"savefile", MPDM_X(qt4_drv_savefile));
458 mpdm_hset_s(drv, L"form", MPDM_X(qt4_drv_form));
462 static mpdm_t qt4_drv_startup(mpdm_t a)
463 /* driver initialization */
465 register_functions();
467 build_font(1);
468 build_colors();
470 window = new MPWindow();
471 window->show();
473 return NULL;
476 extern "C" Display *XOpenDisplay(char *);
478 extern "C" int qt4_drv_detect(int * argc, char *** argv)
480 mpdm_t drv;
481 Display *x11_display;
483 /* try connecting directly to the Xserver */
484 if ((x11_display = XOpenDisplay((char *)NULL)) == NULL)
485 return 0;
487 /* this is where it crashes if no X server */
488 app = new QApplication(x11_display);
490 drv = mpdm_hget_s(mp, L"drv");
491 mpdm_hset_s(drv, L"id", MPDM_LS(L"qt4"));
492 mpdm_hset_s(drv, L"startup", MPDM_X(qt4_drv_startup));
494 return 1;
497 #include "mpv_qt4.moc"