Added open and save file names to Qt4.
[mp-5.x.git] / mpv_qt4.cpp
blob58bb915d4fe288d8bfb824d5cf725facc7770aad
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);
52 class MPArea : public QWidget
54 Q_OBJECT
56 public:
57 MPArea(QWidget *parent = 0);
58 void inputMethodEvent(QInputMethodEvent *event);
59 void keyPressEvent(QKeyEvent *event);
60 void keyReleaseEvent(QKeyEvent *event);
61 void mousePressEvent(QMouseEvent *event);
62 void mouseReleaseEvent(QMouseEvent *event);
63 void mouseMoveEvent(QMouseEvent *event);
64 void wheelEvent(QWheelEvent *event);
65 void dragEnterEvent(QDragEnterEvent *event);
66 void dropEvent(QDropEvent *event);
67 bool event(QEvent *event);
69 protected:
70 void paintEvent(QPaintEvent *event);
72 public slots:
73 void from_scrollbar(int);
74 void from_filetabs(int);
75 void from_menu(QAction *);
78 /* global data */
79 QApplication *app;
80 MPWindow *window;
81 QMenuBar *menubar;
82 QStatusBar *statusbar;
83 QTabBar *file_tabs;
85 #include "mpv_qk_common.cpp"
87 static void build_menu(void)
88 /* builds the menu */
93 static void draw_status(void)
95 statusbar->showMessage(str_to_qstring(mp_build_status_line()));
99 /** MPWindow methods **/
101 MPWindow::MPWindow(QWidget *parent) : QMainWindow(parent)
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 // this->setAutoSaveSettings(QLatin1String("MinimumProfit"), true);
148 bool MPWindow::queryExit(void)
150 mp_process_event(MPDM_LS(L"close-window"));
152 // this->saveAutoSaveSettings();
154 return mp_exit_requested ? true : false;
158 bool MPWindow::event(QEvent *event)
160 /* do the processing */
161 bool r = QWidget::event(event);
163 if (mp_exit_requested) {
164 // this->saveAutoSaveSettings();
165 exit(0);
168 return r;
172 /** driver functions **/
174 static mpdm_t qt4_drv_alert(mpdm_t a)
176 return NULL;
180 static mpdm_t qt4_drv_confirm(mpdm_t a)
182 return NULL;
186 static mpdm_t qt4_drv_openfile(mpdm_t a)
188 QString r;
189 char tmp[128];
191 getcwd(tmp, sizeof(tmp));
193 /* 1# arg: prompt */
194 r = QFileDialog::getOpenFileName(window,
195 str_to_qstring(mpdm_aget(a, 0)), tmp);
197 return qstring_to_str(r);
201 static mpdm_t qt4_drv_savefile(mpdm_t a)
203 QString r;
204 char tmp[128];
206 getcwd(tmp, sizeof(tmp));
208 /* 1# arg: prompt */
209 r = QFileDialog::getSaveFileName(window,
210 str_to_qstring(mpdm_aget(a, 0)), tmp);
212 return qstring_to_str(r);
216 static mpdm_t qt4_drv_form(mpdm_t a)
218 return NULL;
222 static void register_functions(void)
224 mpdm_t drv;
226 drv = mpdm_hget_s(mp, L"drv");
227 mpdm_hset_s(drv, L"main_loop", MPDM_X(qt4_drv_main_loop));
228 mpdm_hset_s(drv, L"shutdown", MPDM_X(qt4_drv_shutdown));
230 mpdm_hset_s(drv, L"clip_to_sys", MPDM_X(qt4_drv_clip_to_sys));
231 mpdm_hset_s(drv, L"sys_to_clip", MPDM_X(qt4_drv_sys_to_clip));
232 mpdm_hset_s(drv, L"update_ui", MPDM_X(qt4_drv_update_ui));
233 /* mpdm_hset_s(drv, L"timer", MPDM_X(qt4_drv_timer));*/
234 mpdm_hset_s(drv, L"busy", MPDM_X(qt4_drv_busy));
236 mpdm_hset_s(drv, L"alert", MPDM_X(qt4_drv_alert));
237 mpdm_hset_s(drv, L"confirm", MPDM_X(qt4_drv_confirm));
238 mpdm_hset_s(drv, L"openfile", MPDM_X(qt4_drv_openfile));
239 mpdm_hset_s(drv, L"savefile", MPDM_X(qt4_drv_savefile));
240 mpdm_hset_s(drv, L"form", MPDM_X(qt4_drv_form));
244 static mpdm_t qt4_drv_startup(mpdm_t a)
245 /* driver initialization */
247 register_functions();
249 build_font(1);
250 build_colors();
252 window = new MPWindow();
253 window->show();
255 return NULL;
258 extern "C" Display *XOpenDisplay(char *);
260 extern "C" int qt4_drv_detect(int * argc, char *** argv)
262 mpdm_t drv;
263 Display *x11_display;
265 /* try connecting directly to the Xserver */
266 if ((x11_display = XOpenDisplay((char *)NULL)) == NULL)
267 return 0;
269 /* this is where it crashes if no X server */
270 app = new QApplication(x11_display);
272 drv = mpdm_hget_s(mp, L"drv");
273 mpdm_hset_s(drv, L"id", MPDM_LS(L"qt4"));
274 mpdm_hset_s(drv, L"startup", MPDM_X(qt4_drv_startup));
276 return 1;
279 #include "mpv_qt4.moc"