3 Minimum Profit - Programmer Text Editor
7 Copyright (C) 2009/2010 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
);
44 class MPWindow
: public QMainWindow
47 MPWindow(QWidget
* parent
= 0);
49 bool event(QEvent
* event
);
54 class MPArea
: public QWidget
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
);
74 void paintEvent(QPaintEvent
* event
);
76 public slots
: void from_scrollbar(int);
77 void from_filetabs(int);
78 void from_menu(QAction
*);
79 void from_timer(void);
86 //QStatusBar *statusbar;
90 #define MENU_CLASS QMenu
92 #include "mpv_qk_common.cpp"
94 static void draw_status(void)
96 statusbar
->setText(str_to_qstring(mp_build_status_line()));
100 /** MPWindow methods **/
102 MPWindow::MPWindow(QWidget
* parent
):QMainWindow(parent
)
108 setWindowTitle("mp " VERSION
);
110 menubar
= this->menuBar();
113 /* pick an optimal height for the menu & tabs */
114 height
= menubar
->sizeHint().height();
116 file_tabs
= new QTabBar();
117 file_tabs
->setFocusPolicy(Qt::NoFocus
);
120 hb
= new QHBoxLayout();
121 hb
->setContentsMargins(0, 0, 0, 0);
123 hb
->addWidget(menubar
);
124 hb
->addWidget(file_tabs
);
125 QWidget
*ta
= new QWidget();
127 ta
->setMaximumHeight(height
);
130 hb
= new QHBoxLayout();
131 hb
->setContentsMargins(0, 0, 0, 0);
134 scrollbar
= new QScrollBar();
135 scrollbar
->setFocusPolicy(Qt::NoFocus
);
138 hb
->addWidget(scrollbar
);
139 QWidget
*cc
= new QWidget();
142 /* the full container */
143 vb
= new QVBoxLayout();
148 QWidget
*mc
= new QWidget();
151 statusbar
= new QLabel();
152 this->statusBar()->addWidget(statusbar
);
154 setCentralWidget(mc
);
156 connect(scrollbar
, SIGNAL(valueChanged(int)),
157 area
, SLOT(from_scrollbar(int)));
159 connect(file_tabs
, SIGNAL(currentChanged(int)),
160 area
, SLOT(from_filetabs(int)));
162 connect(menubar
, SIGNAL(triggered(QAction
*)),
163 area
, SLOT(from_menu(QAction
*)));
165 this->setWindowIcon(QIcon(QPixmap(mp_xpm
)));
167 settings
= new QSettings("triptico.com", "MinimumProfit");
169 QPoint pos
= settings
->value("pos", QPoint(20, 20)).toPoint();
170 QSize size
= settings
->value("size", QSize(600, 400)).toSize();
177 static void save_settings(MPWindow
* w
)
179 w
->settings
->setValue("pos", w
->pos());
180 w
->settings
->setValue("size", w
->size());
185 bool MPWindow::queryExit(void)
187 mp_process_event(MPDM_LS(L
"close-window"));
191 return mp_exit_requested
? true : false;
195 bool MPWindow::event(QEvent
* event
)
197 /* do the processing */
198 bool r
= QWidget::event(event
);
200 if (mp_exit_requested
) {
202 qt4_drv_shutdown(NULL
, NULL
);
203 QApplication::exit(0);
210 /** driver functions **/
212 static mpdm_t
qt4_drv_alert(mpdm_t a
, mpdm_t ctxt
)
215 QMessageBox::information(window
, "mp " VERSION
,
216 str_to_qstring(mpdm_aget(a
, 0)));
222 static mpdm_t
qt4_drv_confirm(mpdm_t a
, mpdm_t ctxt
)
227 r
= QMessageBox::question(window
, "mp" VERSION
,
228 str_to_qstring(mpdm_aget(a
, 0)),
229 QMessageBox::Yes
| QMessageBox::
230 No
| QMessageBox::Cancel
);
233 case QMessageBox::Yes
:
237 case QMessageBox::No
:
241 case QMessageBox::Cancel
:
250 static mpdm_t
qt4_drv_openfile(mpdm_t a
, mpdm_t ctxt
)
255 getcwd(tmp
, sizeof(tmp
));
258 r
= QFileDialog::getOpenFileName(window
,
259 str_to_qstring(mpdm_aget(a
, 0)), tmp
);
261 return qstring_to_str(r
);
265 static mpdm_t
qt4_drv_savefile(mpdm_t a
, mpdm_t ctxt
)
270 getcwd(tmp
, sizeof(tmp
));
273 r
= QFileDialog::getSaveFileName(window
,
274 str_to_qstring(mpdm_aget(a
, 0)), tmp
);
276 return qstring_to_str(r
);
280 class MPForm
: public QDialog
283 QDialogButtonBox
* button_box
;
285 MPForm(QWidget
* parent
= 0) : QDialog(parent
)
287 button_box
= new QDialogButtonBox(QDialogButtonBox::Ok
|
288 QDialogButtonBox::Cancel
);
290 connect(button_box
, SIGNAL(accepted()), this, SLOT(accept()));
291 connect(button_box
, SIGNAL(rejected()), this, SLOT(reject()));
296 static mpdm_t
qt4_drv_form(mpdm_t a
, mpdm_t ctxt
)
303 MPForm
*dialog
= new MPForm(window
);
304 dialog
->setWindowTitle("mp " VERSION
);
306 dialog
->setModal(true);
308 widget_list
= mpdm_aget(a
, 0);
310 QWidget
*form
= new QWidget();
311 QFormLayout
*fl
= new QFormLayout();
313 for (n
= 0; n
< mpdm_size(widget_list
); n
++) {
314 mpdm_t w
= mpdm_aget(widget_list
, n
);
317 QLabel
*ql
= new QLabel("");
319 type
= mpdm_string(mpdm_hget_s(w
, L
"type"));
321 if ((t
= mpdm_hget_s(w
, L
"label")) != NULL
) {
322 ql
->setText(str_to_qstring(mpdm_gettext(t
)));
325 t
= mpdm_hget_s(w
, L
"value");
327 if (wcscmp(type
, L
"text") == 0) {
329 QComboBox
*qc
= new QComboBox();
331 qc
->setEditable(true);
332 qc
->setMinimumContentsLength(30);
333 qc
->setMaxVisibleItems(8);
336 qc
->setEditText(str_to_qstring(t
));
340 if ((h
= mpdm_hget_s(w
, L
"history")) != NULL
) {
343 /* has history; fill it */
344 h
= mp_get_history(h
);
346 for (i
= mpdm_size(h
) - 1; i
>= 0; i
--)
347 qc
->addItem(str_to_qstring(mpdm_aget(h
, i
)));
350 /* select all the editable field */
351 qc
->lineEdit()->selectAll();
356 if (wcscmp(type
, L
"password") == 0) {
357 QLineEdit
*qe
= new QLineEdit();
359 qe
->setEchoMode(QLineEdit::Password
);
366 if (wcscmp(type
, L
"checkbox") == 0) {
367 QCheckBox
*qc
= new QCheckBox();
370 qc
->setCheckState(Qt::Checked
);
377 if (wcscmp(type
, L
"list") == 0) {
379 QListWidget
*qlw
= new QListWidget();
380 qlw
->setMinimumWidth(480);
382 /* use a monospaced font */
383 qlw
->setFont(QFont(QString("Mono")));
385 mpdm_t l
= mpdm_hget_s(w
, L
"list");
387 for (i
= 0; i
< mpdm_size(l
); i
++)
388 qlw
->addItem(str_to_qstring(mpdm_aget(l
, i
)));
390 qlw
->setCurrentRow(mpdm_ival(t
));
398 qlist
[n
]->setFocus(Qt::OtherFocusReason
);
403 QVBoxLayout
*ml
= new QVBoxLayout();
405 ml
->addWidget(dialog
->button_box
);
407 dialog
->setLayout(ml
);
414 r
= MPDM_A(mpdm_size(widget_list
));
417 /* fill the return values */
418 for (n
= 0; n
< mpdm_size(widget_list
); n
++) {
419 mpdm_t w
= mpdm_aget(widget_list
, n
);
423 type
= mpdm_string(mpdm_hget_s(w
, L
"type"));
425 if (wcscmp(type
, L
"text") == 0) {
427 QComboBox
*ql
= (QComboBox
*) qlist
[n
];
429 v
= mpdm_ref(qstring_to_str(ql
->currentText()));
431 /* if it has history, add to it */
432 if ((h
= mpdm_hget_s(w
, L
"history")) != NULL
&&
433 v
!= NULL
&& mpdm_cmp_s(v
, L
"") != 0) {
434 h
= mp_get_history(h
);
436 if (mpdm_cmp(v
, mpdm_aget(h
, -1)) != 0)
443 if (wcscmp(type
, L
"password") == 0) {
444 QLineEdit
*ql
= (QLineEdit
*) qlist
[n
];
446 v
= qstring_to_str(ql
->text());
449 if (wcscmp(type
, L
"checkbox") == 0) {
450 QCheckBox
*qb
= (QCheckBox
*) qlist
[n
];
452 v
= MPDM_I(qb
->checkState() == Qt::Checked
);
455 if (wcscmp(type
, L
"list") == 0) {
456 QListWidget
*ql
= (QListWidget
*) qlist
[n
];
458 v
= MPDM_I(ql
->currentRow());
470 static void register_functions(void)
474 drv
= mpdm_hget_s(mp
, L
"drv");
475 mpdm_hset_s(drv
, L
"main_loop", MPDM_X(qt4_drv_main_loop
));
476 mpdm_hset_s(drv
, L
"shutdown", MPDM_X(qt4_drv_shutdown
));
477 mpdm_hset_s(drv
, L
"clip_to_sys", MPDM_X(qt4_drv_clip_to_sys
));
478 mpdm_hset_s(drv
, L
"sys_to_clip", MPDM_X(qt4_drv_sys_to_clip
));
479 mpdm_hset_s(drv
, L
"update_ui", MPDM_X(qt4_drv_update_ui
));
480 mpdm_hset_s(drv
, L
"timer", MPDM_X(qt4_drv_timer
));
481 mpdm_hset_s(drv
, L
"busy", MPDM_X(qt4_drv_busy
));
482 mpdm_hset_s(drv
, L
"alert", MPDM_X(qt4_drv_alert
));
483 mpdm_hset_s(drv
, L
"confirm", MPDM_X(qt4_drv_confirm
));
484 mpdm_hset_s(drv
, L
"openfile", MPDM_X(qt4_drv_openfile
));
485 mpdm_hset_s(drv
, L
"savefile", MPDM_X(qt4_drv_savefile
));
486 mpdm_hset_s(drv
, L
"form", MPDM_X(qt4_drv_form
));
490 static mpdm_t
qt4_drv_startup(mpdm_t a
, mpdm_t ctxt
)
491 /* driver initialization */
493 register_functions();
498 window
= new MPWindow();
504 extern "C" Display
* XOpenDisplay(char *);
506 extern "C" int qt4_drv_detect(int *argc
, char ***argv
)
509 Display
*x11_display
;
512 for (n
= 0; n
< *argc
; n
++) {
513 if (strcmp(argv
[0][n
], "-txt") == 0 ||
514 strcmp(argv
[0][n
], "-h") == 0)
518 /* try connecting directly to the Xserver */
519 if ((x11_display
= XOpenDisplay((char *) NULL
)) == NULL
)
522 /* this is where it crashes if no X server */
523 app
= new QApplication(x11_display
);
525 drv
= mpdm_hget_s(mp
, L
"drv");
526 mpdm_hset_s(drv
, L
"id", MPDM_LS(L
"qt4"));
527 mpdm_hset_s(drv
, L
"startup", MPDM_X(qt4_drv_startup
));
532 #include "mpv_qt4.moc"