3 Minimum Profit - Programmer Text Editor
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
);
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
);
72 void paintEvent(QPaintEvent
*event
);
75 void from_scrollbar(int);
76 void from_filetabs(int);
77 void from_menu(QAction
*);
84 //QStatusBar *statusbar;
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();
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();
120 scrollbar
= new QScrollBar();
121 scrollbar
->setFocusPolicy(Qt::NoFocus
);
124 hb
->addWidget(scrollbar
);
125 QWidget
*cc
= new QWidget();
128 vb
->addWidget(file_tabs
);
130 QWidget
*mc
= new QWidget();
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();
156 static void save_settings(MPWindow
*w
)
158 w
->settings
->setValue("pos", w
->pos());
159 w
->settings
->setValue("size", w
->size());
164 bool MPWindow::queryExit(void)
166 mp_process_event(MPDM_LS(L
"close-window"));
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
) {
181 qt4_drv_shutdown(NULL
);
189 /** driver functions **/
191 static mpdm_t
qt4_drv_alert(mpdm_t a
)
194 QMessageBox::information(0, "mp " VERSION
, str_to_qstring(mpdm_aget(a
, 0)));
200 static mpdm_t
qt4_drv_confirm(mpdm_t a
)
205 r
= QMessageBox::question(0, "mp" VERSION
,
206 str_to_qstring(mpdm_aget(a
, 0)),
207 QMessageBox::Yes
| QMessageBox::No
| QMessageBox::Cancel
211 case QMessageBox::Yes
:
215 case QMessageBox::No
:
219 case QMessageBox::Cancel
:
228 static mpdm_t
qt4_drv_openfile(mpdm_t a
)
233 getcwd(tmp
, sizeof(tmp
));
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
)
248 getcwd(tmp
, sizeof(tmp
));
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
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
)
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
);
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) {
307 QComboBox
*qc
= new QComboBox();
309 qc
->setEditable(true);
310 qc
->setMinimumContentsLength(30);
311 qc
->setMaxVisibleItems(8);
314 qc
->setEditText(str_to_qstring(t
));
318 if ((h
= mpdm_hget_s(w
, L
"history")) != NULL
) {
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
)));
331 if (wcscmp(type
, L
"password") == 0) {
332 QLineEdit
*qe
= new QLineEdit();
334 qe
->setEchoMode(QLineEdit::Password
);
341 if (wcscmp(type
, L
"checkbox") == 0) {
342 QCheckBox
*qc
= new QCheckBox();
345 qc
->setCheckState(Qt::Checked
);
352 if (wcscmp(type
, L
"list") == 0) {
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
));
373 qlist
[n
]->setFocus(Qt::OtherFocusReason
);
378 QVBoxLayout
*ml
= new QVBoxLayout();
380 ml
->addWidget(dialog
->button_box
);
382 dialog
->setLayout(ml
);
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
);
397 type
= mpdm_string(mpdm_hget_s(w
, L
"type"));
399 if (wcscmp(type
, L
"text") == 0) {
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)
415 if (wcscmp(type
, L
"password") == 0) {
416 QLineEdit
*ql
= (QLineEdit
*)qlist
[n
];
418 v
= qstring_to_str(ql
->text());
421 if (wcscmp(type
, L
"checkbox") == 0) {
422 QCheckBox
*qb
= (QCheckBox
*)qlist
[n
];
424 v
= MPDM_I(qb
->checkState() == Qt::Checked
);
427 if (wcscmp(type
, L
"list") == 0) {
428 QListWidget
*ql
= (QListWidget
*)qlist
[n
];
430 v
= MPDM_I(ql
->currentRow());
440 static void register_functions(void)
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();
470 window
= new MPWindow();
476 extern "C" Display
*XOpenDisplay(char *);
478 extern "C" int qt4_drv_detect(int * argc
, char *** argv
)
481 Display
*x11_display
;
483 /* try connecting directly to the Xserver */
484 if ((x11_display
= XOpenDisplay((char *)NULL
)) == NULL
)
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
));
497 #include "mpv_qt4.moc"