3 Minimum Profit - Programmer Text Editor
5 Code common to Qt4 and KDE4 drivers.
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
28 QScrollBar
*scrollbar
;
29 static int font_width
= -1;
30 static int font_height
= -1;
31 static int mouse_down
= 0;
32 static int key_down
= 0;
36 static void draw_status(void);
37 static void build_menu(void);
39 static mpdm_t
qstring_to_str(QString s
)
40 /* converts a QString to an MPDM string */
46 wchar_t *wptr
= (wchar_t *)malloc((t
+ 1) * sizeof(wchar_t));
48 r
= MPDM_ENS(wptr
, t
);
58 QString
str_to_qstring(mpdm_t s
)
59 /* converts an MPDM string to a QString */
61 wchar_t *wptr
= mpdm_string(s
);
62 return QString::fromWCharArray(wptr
);
66 #define MAX_COLORS 1000
67 QPen inks
[MAX_COLORS
];
68 QBrush papers
[MAX_COLORS
];
69 bool underlines
[MAX_COLORS
];
72 static void build_colors(void)
73 /* builds the colors */
80 /* gets the color definitions and attribute names */
81 colors
= mpdm_hget_s(mp
, L
"colors");
82 l
= mpdm_keys(colors
);
86 for (n
= 0; n
< s
&& (c
= mpdm_aget(l
, n
)) != NULL
; n
++) {
88 mpdm_t d
= mpdm_hget(colors
, c
);
89 mpdm_t v
= mpdm_hget_s(d
, L
"gui");
91 /* store the 'normal' attribute */
92 if (wcscmp(mpdm_string(c
), L
"normal") == 0)
96 mpdm_hset_s(d
, L
"attr", MPDM_I(n
));
98 rgbi
= mpdm_ival(mpdm_aget(v
, 0));
99 rgbp
= mpdm_ival(mpdm_aget(v
, 1));
102 v
= mpdm_hget_s(d
, L
"flags");
104 if (mpdm_seek_s(v
, L
"reverse", 1) != -1) {
110 underlines
[n
] = mpdm_seek_s(v
, L
"underline", 1) != -1 ? true : false;
112 inks
[n
] = QPen(QColor::fromRgbF(
113 (float) ((rgbi
& 0x00ff0000) >> 16) / 256.0,
114 (float) ((rgbi
& 0x0000ff00) >> 8) / 256.0,
115 (float) ((rgbi
& 0x000000ff)) / 256.0,
118 papers
[n
] = QBrush(QColor::fromRgbF(
119 (float) ((rgbp
& 0x00ff0000) >> 16) / 256.0,
120 (float) ((rgbp
& 0x0000ff00) >> 8) / 256.0,
121 (float) ((rgbp
& 0x000000ff)) / 256.0,
127 static QFont
build_font(int rebuild
)
128 /* (re)builds the font */
134 char * font_face
= (char *)"Mono";
137 if ((c
= mpdm_hget_s(mp
, L
"config")) != NULL
) {
140 if ((v
= mpdm_hget_s(c
, L
"font_size")) != NULL
)
141 font_size
= mpdm_ival(v
);
143 mpdm_hset_s(c
, L
"font_size", MPDM_I(font_size
));
145 if ((v
= mpdm_hget_s(c
, L
"font_face")) != NULL
) {
146 v
= MPDM_2MBS((wchar_t *)v
->data
);
147 font_face
= (char *)v
->data
;
150 mpdm_hset_s(c
, L
"font_face", MPDM_MBS(font_face
));
153 font
= QFont(QString(font_face
), font_size
);
160 static int ignore_scrollbar_signal
= 0;
162 static void draw_scrollbar(void)
167 mpdm_t txt
= mpdm_hget_s(mp_active(), L
"txt");
168 mpdm_t window
= mpdm_hget_s(mp
, L
"window");
169 int vy
= mpdm_ival(mpdm_hget_s(txt
, L
"vy"));
170 int ty
= mpdm_ival(mpdm_hget_s(window
, L
"ty"));
171 int l
= mpdm_size(mpdm_hget_s(txt
, L
"lines")) - ty
;
173 if (ol
!= l
|| ovy
!= vy
|| oty
!= ty
) {
175 ignore_scrollbar_signal
= 1;
177 scrollbar
->setMinimum(0);
178 scrollbar
->setMaximum(ol
= l
);
179 scrollbar
->setValue(ovy
= vy
);
180 scrollbar
->setPageStep(oty
= ty
);
182 ignore_scrollbar_signal
= 0;
187 static void draw_filetabs(void)
189 static mpdm_t last
= NULL
;
193 names
= mp_get_doc_names();
195 /* get mp.active_i now, because it can be changed
196 from the signal handler */
197 i
= mpdm_ival(mpdm_hget_s(mp
, L
"active_i"));
199 /* is the list different from the previous one? */
200 if (mpdm_cmp(names
, last
) != 0) {
202 while (file_tabs
->count())
203 file_tabs
->removeTab(0);
205 /* create the new ones */
206 for (n
= 0; n
< mpdm_size(names
); n
++)
207 file_tabs
->addTab(str_to_qstring(mpdm_aget(names
, n
)));
209 /* store for the next time */
211 last
= mpdm_ref(names
);
214 /* set the active one */
215 file_tabs
->setCurrentIndex(i
);
219 /** MPArea methods **/
221 MPArea::MPArea(QWidget
*parent
) : QWidget(parent
)
223 setAttribute(Qt::WA_InputMethodEnabled
, true);
225 setAcceptDrops(true);
229 bool MPArea::event(QEvent
*event
)
231 /* special tab processing */
232 if (event
->type() == QEvent::KeyPress
) {
233 QKeyEvent
*ke
= (QKeyEvent
*)event
;
235 if (ke
->key() == Qt::Key_Tab
) {
236 mp_process_event(MPDM_LS(L
"tab"));
242 /* keep normal processing */
243 return QWidget::event(event
);
246 void MPArea::paintEvent(QPaintEvent
*)
251 bool underline
= false;
253 QPainter
painter(this);
255 font
= build_font(0);
256 font
.setUnderline(false);
257 painter
.setFont(font
);
259 font_width
= painter
.fontMetrics().width("M");
260 font_height
= painter
.fontMetrics().height();
262 /* calculate window size */
263 w
= mpdm_hget_s(mp
, L
"window");
264 mpdm_hset_s(w
, L
"tx", MPDM_I(this->width() / font_width
));
265 mpdm_hset_s(w
, L
"ty", MPDM_I(this->height() / font_height
));
267 w
= mp_draw(mp_active(), 0);
268 y
= painter
.fontMetrics().ascent() + 1;
270 painter
.setBackgroundMode(Qt::OpaqueMode
);
272 painter
.setBrush(papers
[normal_attr
]);
273 painter
.drawRect(0, 0, this->width(), this->height());
275 for (n
= 0; n
< mpdm_size(w
); n
++) {
276 mpdm_t l
= mpdm_aget(w
, n
);
282 for (m
= 0; m
< mpdm_size(l
); m
++) {
286 /* get the attribute and the string */
287 attr
= mpdm_ival(mpdm_aget(l
, m
++));
290 painter
.setPen(inks
[attr
]);
291 painter
.setBackground(papers
[attr
]);
293 QString qs
= str_to_qstring(s
);
295 if (underline
!= underlines
[attr
]) {
296 underline
= underlines
[attr
];
297 font
.setUnderline(underline
);
298 painter
.setFont(font
);
301 painter
.drawText(x
, y
, qs
);
303 x
+= painter
.fontMetrics().width(qs
);
313 area
->setFocus(Qt::OtherFocusReason
);
317 void MPArea::inputMethodEvent(QInputMethodEvent
*event
)
319 QString s
= event
->commitString();
321 mp_process_event(qstring_to_str(s
));
326 void MPArea::keyReleaseEvent(QKeyEvent
*event
)
328 if (!event
->isAutoRepeat()) {
331 if (mp_keypress_throttle(0))
337 void MPArea::keyPressEvent(QKeyEvent
*event
)
344 /* set mp.shift_pressed */
345 if (event
->modifiers() & Qt::ShiftModifier
)
346 mpdm_hset_s(mp
, L
"shift_pressed", MPDM_I(1));
348 if (event
->modifiers() & Qt::ControlModifier
) {
349 switch (event
->key()) {
350 case Qt::Key_Up
: ptr
= (wchar_t *) L
"ctrl-cursor-up"; break;
351 case Qt::Key_Down
: ptr
= (wchar_t *) L
"ctrl-cursor-down"; break;
352 case Qt::Key_Left
: ptr
= (wchar_t *) L
"ctrl-cursor-left"; break;
353 case Qt::Key_Right
: ptr
= (wchar_t *) L
"ctrl-cursor-right"; break;
354 case Qt::Key_PageUp
: ptr
= (wchar_t *) L
"ctrl-page-up"; break;
355 case Qt::Key_PageDown
: ptr
= (wchar_t *) L
"ctrl-page-down"; break;
356 case Qt::Key_Home
: ptr
= (wchar_t *) L
"ctrl-home"; break;
357 case Qt::Key_End
: ptr
= (wchar_t *) L
"ctrl-end"; break;
358 case Qt::Key_Space
: ptr
= (wchar_t *) L
"ctrl-space"; break;
359 case Qt::Key_F1
: ptr
= (wchar_t *) L
"ctrl-f1"; break;
360 case Qt::Key_F2
: ptr
= (wchar_t *) L
"ctrl-f2"; break;
361 case Qt::Key_F3
: ptr
= (wchar_t *) L
"ctrl-f3"; break;
362 case Qt::Key_F4
: ptr
= (wchar_t *) L
"ctrl-f4"; break;
363 case Qt::Key_F5
: ptr
= (wchar_t *) L
"ctrl-f5"; break;
364 case Qt::Key_F6
: ptr
= (wchar_t *) L
"ctrl-f6"; break;
365 case Qt::Key_F7
: ptr
= (wchar_t *) L
"ctrl-f7"; break;
366 case Qt::Key_F8
: ptr
= (wchar_t *) L
"ctrl-f8"; break;
367 case Qt::Key_F9
: ptr
= (wchar_t *) L
"ctrl-f9"; break;
368 case Qt::Key_F10
: ptr
= (wchar_t *) L
"ctrl-f10"; break;
369 case Qt::Key_F11
: ptr
= (wchar_t *) L
"ctrl-f11"; break;
370 case Qt::Key_F12
: ptr
= (wchar_t *) L
"ctrl-f12"; break;
371 case 'A': ptr
= (wchar_t *) L
"ctrl-a"; break;
372 case 'B': ptr
= (wchar_t *) L
"ctrl-b"; break;
373 case 'C': ptr
= (wchar_t *) L
"ctrl-c"; break;
374 case 'D': ptr
= (wchar_t *) L
"ctrl-d"; break;
375 case 'E': ptr
= (wchar_t *) L
"ctrl-e"; break;
376 case 'F': ptr
= (wchar_t *) L
"ctrl-f"; break;
377 case 'G': ptr
= (wchar_t *) L
"ctrl-g"; break;
378 case 'H': ptr
= (wchar_t *) L
"ctrl-h"; break;
379 case 'I': ptr
= (wchar_t *) L
"ctrl-i"; break;
380 case 'J': ptr
= (wchar_t *) L
"ctrl-j"; break;
381 case 'K': ptr
= (wchar_t *) L
"ctrl-k"; break;
382 case 'L': ptr
= (wchar_t *) L
"ctrl-l"; break;
383 case 'M': ptr
= (wchar_t *) L
"ctrl-m"; break;
384 case 'N': ptr
= (wchar_t *) L
"ctrl-n"; break;
385 case 'O': ptr
= (wchar_t *) L
"ctrl-o"; break;
386 case 'P': ptr
= (wchar_t *) L
"ctrl-p"; break;
387 case 'Q': ptr
= (wchar_t *) L
"ctrl-q"; break;
388 case 'R': ptr
= (wchar_t *) L
"ctrl-r"; break;
389 case 'S': ptr
= (wchar_t *) L
"ctrl-s"; break;
390 case 'T': ptr
= (wchar_t *) L
"ctrl-t"; break;
391 case 'U': ptr
= (wchar_t *) L
"ctrl-u"; break;
392 case 'V': ptr
= (wchar_t *) L
"ctrl-v"; break;
393 case 'W': ptr
= (wchar_t *) L
"ctrl-w"; break;
394 case 'X': ptr
= (wchar_t *) L
"ctrl-x"; break;
395 case 'Y': ptr
= (wchar_t *) L
"ctrl-y"; break;
396 case 'Z': ptr
= (wchar_t *) L
"ctrl-z"; break;
398 case Qt::Key_Enter
: ptr
= (wchar_t *) L
"ctrl-enter"; break;
405 switch (event
->key()) {
406 case Qt::Key_Up
: ptr
= (wchar_t *) L
"cursor-up"; break;
407 case Qt::Key_Down
: ptr
= (wchar_t *) L
"cursor-down"; break;
408 case Qt::Key_Left
: ptr
= (wchar_t *) L
"cursor-left"; break;
409 case Qt::Key_Right
: ptr
= (wchar_t *) L
"cursor-right"; break;
410 case Qt::Key_PageUp
: ptr
= (wchar_t *) L
"page-up"; break;
411 case Qt::Key_PageDown
: ptr
= (wchar_t *) L
"page-down"; break;
412 case Qt::Key_Home
: ptr
= (wchar_t *) L
"home"; break;
413 case Qt::Key_End
: ptr
= (wchar_t *) L
"end"; break;
414 case Qt::Key_Space
: ptr
= (wchar_t *) L
"space"; break;
415 case Qt::Key_F1
: ptr
= (wchar_t *) L
"f1"; break;
416 case Qt::Key_F2
: ptr
= (wchar_t *) L
"f2"; break;
417 case Qt::Key_F3
: ptr
= (wchar_t *) L
"f3"; break;
418 case Qt::Key_F4
: ptr
= (wchar_t *) L
"f4"; break;
419 case Qt::Key_F5
: ptr
= (wchar_t *) L
"f5"; break;
420 case Qt::Key_F6
: ptr
= (wchar_t *) L
"f6"; break;
421 case Qt::Key_F7
: ptr
= (wchar_t *) L
"f7"; break;
422 case Qt::Key_F8
: ptr
= (wchar_t *) L
"f8"; break;
423 case Qt::Key_F9
: ptr
= (wchar_t *) L
"f9"; break;
424 case Qt::Key_F10
: ptr
= (wchar_t *) L
"f10"; break;
425 case Qt::Key_F11
: ptr
= (wchar_t *) L
"f11"; break;
426 case Qt::Key_F12
: ptr
= (wchar_t *) L
"f12"; break;
427 case Qt::Key_Insert
: ptr
= (wchar_t *) L
"insert"; break;
428 case Qt::Key_Backspace
: ptr
= (wchar_t *) L
"backspace"; break;
429 case Qt::Key_Delete
: ptr
= (wchar_t *) L
"delete"; break;
431 case Qt::Key_Enter
: ptr
= (wchar_t *) L
"enter"; break;
432 case Qt::Key_Escape
: ptr
= (wchar_t *) L
"escape"; break;
440 k
= qstring_to_str(event
->text());
447 if (mp_keypress_throttle(1))
452 void MPArea::mousePressEvent(QMouseEvent
*event
)
458 QPoint pos
= event
->pos();
460 mpdm_hset_s(mp
, L
"mouse_x", MPDM_I(pos
.x() / font_width
));
461 mpdm_hset_s(mp
, L
"mouse_y", MPDM_I(pos
.y() / font_height
));
463 switch (event
->button()) {
464 case Qt::LeftButton
: ptr
= (wchar_t *)L
"mouse-left-button"; break;
465 case Qt::MidButton
: ptr
= (wchar_t *)L
"mouse-middle-button"; break;
466 case Qt::RightButton
: ptr
= (wchar_t *)L
"mouse-right-button"; break;
472 mp_process_event(MPDM_S(ptr
));
478 void MPArea::mouseReleaseEvent(QMouseEvent
*event
)
484 void MPArea::mouseMoveEvent(QMouseEvent
*event
)
492 QPoint pos
= event
->pos();
495 x
= pos
.x() / font_width
;
496 y
= pos
.y() / font_height
;
498 if (ox
!= x
&& oy
!= y
) {
499 mpdm_hset_s(mp
, L
"mouse_to_x", MPDM_I(x
));
500 mpdm_hset_s(mp
, L
"mouse_to_y", MPDM_I(y
));
502 mp_process_event(MPDM_LS(L
"mouse-drag"));
510 void MPArea::wheelEvent(QWheelEvent
*event
)
512 if (event
->delta() > 0)
513 mp_process_event(MPDM_S(L
"mouse-wheel-up"));
515 mp_process_event(MPDM_S(L
"mouse-wheel-down"));
521 void MPArea::dragEnterEvent(QDragEnterEvent
*event
)
523 if (event
->mimeData()->hasFormat("text/uri-list"))
524 event
->acceptProposedAction();
528 void MPArea::dropEvent(QDropEvent
*event
)
531 mpdm_t v
= qstring_to_str(event
->mimeData()->text());
532 mpdm_t l
= MPDM_A(0);
534 /* split the list of files */
535 v
= mpdm_split(MPDM_LS(L
"\n"), v
);
537 for (n
= 0; n
< mpdm_size(v
); n
++) {
539 mpdm_t w
= mpdm_aget(v
, n
);
541 /* strip file:///, if found */
542 ptr
= mpdm_string(w
);
544 if (wcsncmp(ptr
, L
"file://", 7) == 0)
548 mpdm_push(l
, MPDM_S(ptr
));
551 mpdm_hset_s(mp
, L
"dropped_files", l
);
553 event
->acceptProposedAction();
554 mp_process_event(MPDM_LS(L
"dropped-files"));
562 void MPArea::from_scrollbar(int value
)
564 if (!ignore_scrollbar_signal
) {
565 mpdm_t v
= mp_active();
569 /* set the vy to the same value */
570 v
= mpdm_hget_s(v
, L
"txt");
571 mpdm_hset_s(v
, L
"vy", MPDM_I(value
));
578 void MPArea::from_filetabs(int value
)
581 /* sets the active one */
582 mpdm_hset_s(mp
, L
"active_i", MPDM_I(value
));
588 void MPArea::from_menu(QAction
*action
)
590 mpdm_t label
= qstring_to_str(action
->text());
591 label
= mpdm_sregex(MPDM_LS(L
"/&/"), label
, NULL
, 0);
593 mpdm_t a
= mpdm_hget_s(mp
, L
"actions_by_menu_label");
595 mp_process_action(mpdm_hget(a
, label
));
600 /** driver functions **/
602 static mpdm_t
qt4_drv_update_ui(mpdm_t a
)
612 static mpdm_t
qt4_drv_busy(mpdm_t a
)
614 int onoff
= mpdm_ival(mpdm_aget(a
, 0));
616 window
->setCursor(onoff
? Qt::WaitCursor
: Qt::ArrowCursor
);
622 static mpdm_t
qt4_drv_main_loop(mpdm_t a
)
630 static mpdm_t
qt4_drv_shutdown(mpdm_t a
)
634 if ((v
= mpdm_hget_s(mp
, L
"exit_message")) != NULL
) {
635 mpdm_write_wcs(stdout
, mpdm_string(v
));
643 static mpdm_t
qt4_drv_clip_to_sys(mpdm_t a
)
647 QClipboard
*qc
= QApplication::clipboard();
649 /* gets the clipboard and joins */
650 v
= mpdm_hget_s(mp
, L
"clipboard");
652 if (mpdm_size(v
) != 0) {
653 v
= mpdm_join(MPDM_LS(L
"\n"), v
);
654 qc
->setText(str_to_qstring(v
), QClipboard::Selection
);
661 static mpdm_t
qt4_drv_sys_to_clip(mpdm_t a
)
663 QClipboard
*qc
= QApplication::clipboard();
664 QString qs
= qc
->text(QClipboard::Selection
);
666 /* split and set as the clipboard */
667 mpdm_hset_s(mp
, L
"clipboard", mpdm_split(MPDM_LS(L
"\n"),
668 qstring_to_str(qs
)));
669 mpdm_hset_s(mp
, L
"clipboard_vertical", MPDM_I(0));