blob: 6517cd2f89810cab70d2d4541ba89669282b012a [file] [log] [blame]
Bram Moolenaar843ee412004-06-30 16:16:41 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8
9/*
10 * Porting to KDE(2) was done by
11 *
12 * (C) 2000 by Thomas Capricelli <orzel@freehackers.org>
13 *
14 * Please visit http://freehackers.org/kvim for other vim- or
15 * kde-related coding.
16 *
17 * $Id$
18 *
19 */
20
21#include <qscrollbar.h>
22#include <qcstring.h>
23#include <qdatetime.h>
24#include <qcursor.h>
25#include <qfontmetrics.h>
26#include <qpaintdevice.h>
27#include <qclipboard.h>
28#include <qregexp.h>
29#include <kaboutkde.h>
30#include <kiconloader.h>
31#include <kfontdialog.h>
32#include <kmessagebox.h>
33#include <dcopclient.h>
34#include <kwin.h>
35#include <kmenubar.h>
36#include <kconfig.h>
37#if (QT_VERSION>=300)
38#include <qnamespace.h>
39#include <ktip.h>
40#endif
41#include <qpopupmenu.h>
42#include <qpainter.h>
43#include <qtextcodec.h>
44#include <qfontmetrics.h>
45#include <qfont.h>
46
47
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000048#include "gui_kde_wid.h"
Bram Moolenaar843ee412004-06-30 16:16:41 +000049
50
51extern "C" {
52#include "vim.h"
53#include "version.h"
54}
55
56#include <stdio.h>
57
58/*
59 * global variable for KDE, we can't put them in Gui, cause there are C++ types
60 */
61VimMainWindow *vmw=0;
62SBPool *sbpool=0;
63QString *argServerName=0;
64
65#ifdef FEAT_MOUSESHAPE
66/* The last set mouse pointer shape is remembered, to be used when it goes
67 * from hidden to not hidden. */
68static int last_shape = 0;
69#endif
70
71/*
72 * Arguments handled by KDE internally.
73 */
74
75#if QT_VERSION>=300
76static int tip=0; // 1 no dialog, 0 use it if enabled in conf, 2 force the tip
77#endif
78static int reverse=0; // 0 bg : white, 1 : bg : black
79QString *startfont;
80QSize *startsize;
81static int gui_argc = 0;
82static char **gui_argv = NULL;
83
84/*
85 * Parse the GUI related command-line arguments. Any arguments used are
86 * deleted from argv, and *argc is decremented accordingly. This is called
87 * when vim is started, whether or not the GUI has been started.
88 */
89 void
90gui_mch_prepare(int *argc, char **argv)// {{{
91{
92 //copy args for KDE/Qt
93 gui_argc = 0;
94 //this one is not really good as all options are not for KDE/Qt ...
95 gui_argv = (char **)lalloc((long_u)(*argc * sizeof(char *)), FALSE);
96 if (gui_argv == NULL)
97 return;
98 gui_argv[gui_argc++] = argv[0];
99
100 int found = 0;
101 for (int i = 1; i < *argc ; i++)
102 {
103 if (found!=2)
104 found = 0;
105 else {
106 found=0;
107 //remove from the list of argv
108 if (--*argc>i) {
109 mch_memmove(&argv[i], &argv[i + 1],
110 (*argc - i) * sizeof(char *));
111 }
112 i--;
113 continue;
114 }
115
116 if (strcmp(argv[i],"--servername")==0) {
117 argServerName = new QString(argv[i+1]); // to get the serverName now
118 }
119#if QT_VERSION>+300
120 if (strcmp(argv[i],"-tip")==0 ) {
121 tip=2;
122 found=1;
123 }
124 if (strcmp(argv[i],"-notip")==0 ) {
125 tip=1;
126 found=1;
127 }
128#endif
129 if (strcmp(argv[i],"-black")==0 ) {
130 reverse=1;
131 found=1;
132 }
133 /* replaced by -black */
134/* if (strcmp(argv[i],"-rv")==0 ) {
135 reverse=1;
136 found=1;
137 }*/
138 if (strcmp(argv[i],"-font")==0 || strcmp(argv[i], "-fn")==0 ) {
139 startfont=new QString(argv[i+1]);
140 found=2;
141 }
142 if (strcmp(argv[i],"-geometry")==0 || strcmp (argv[i],"-geom")==0 ) {
143 found=2;
144 QString text(argv[i+1]);
145 QStringList list = QStringList::split(QChar('x'), text);
146 startsize = new QSize(list[0].toInt(),list[1].toInt());
147 }
148 if (strcmp (argv[i],"-display")==0 ) { //XXX: this does not work,
149 // too many -display options in main.c !
150 // ask Bram ...
151 gui_argv[gui_argc++] = strdup("--display");
152 gui_argv[gui_argc++] = argv[i+1];
153 found=0;
154 }
155 if (strcmp (argv[i],"--display")==0 ) {
156 gui_argv[gui_argc++] = argv[i];
157 gui_argv[gui_argc++] = argv[i+1];
158 found=2;
159 }
160 //KDE/Qt options with no args
161 if (strcmp(argv[i],"--help-kde")==0 || strcmp (argv[i],"--help-qt")==0
162 || strcmp(argv[i], "--help-all")==0
163 || strcmp(argv[i], "--reverse")==0
164 || strcmp(argv[i], "--author")==0
165// || strcmp(argv[i], "--version")==0 //disabled we need these for kcmvim
166// || strcmp(argv[i], "-v")==0
167 || strcmp(argv[i], "--license")==0
168 || strcmp(argv[i], "--cmap")==0
169 || strcmp(argv[i], "--nograb")==0
170 || strcmp(argv[i], "--dograb")==0
171 || strcmp(argv[i], "--sync")==0
172 || strcmp(argv[i], "--noxim")==0
173 || strcmp(argv[i], "--nocrashhandler")==0
174 || strcmp(argv[i], "--waitforwm")==0
175 ) {
176 gui_argv[gui_argc++] = argv[i];
177 found=1;
178 }
179 //this outputs KDE and Vim versions :)
180 if (strcmp(argv[i],"--version")==0
181 || strcmp (argv[i],"-v")==0
182 ) {
183 gui_argv[gui_argc++] = argv[i];
184 }
185
186
187 //KDE/Qt options with one arg
188 if ( strcmp(argv[i],"--session")==0
189 || strcmp(argv[i],"--ncols")==0
190 || strcmp(argv[i],"--bg")==0
191 || strcmp(argv[i],"--background")==0
192 || strcmp(argv[i],"--fg")==0
193 || strcmp(argv[i],"--foreground")==0
194 || strcmp(argv[i],"--btn")==0
195 || strcmp(argv[i],"--name")==0
196 || strcmp(argv[i],"--title")==0
197 || strcmp(argv[i],"--inputstyle")==0
198 || strcmp(argv[i],"--im")==0
199 || strcmp(argv[i],"--caption")==0
200 || strcmp(argv[i],"--icon")==0
201 || strcmp(argv[i],"--miniicon")==0
202 || strcmp(argv[i],"--config")==0
203 || strcmp(argv[i],"--dcopserver")==0
204 || strcmp(argv[i],"--style")==0
205 || strcmp(argv[i],"--geometry")==0
206 || strcmp(argv[i],"--smkey")==0
207 || strcmp(argv[i],"-smkey")==0
208 || strcmp(argv[i],"-session")==0
209 ) {
210 gui_argv[gui_argc++] = argv[i];
211 gui_argv[gui_argc++] = argv[i+1];
212 found=2;
213 }
214
215 //remove from the list of argv
216 if (found >= 1 && --*argc>i) {
217 mch_memmove(&argv[i], &argv[i + 1],
218 (*argc - i) * sizeof(char *));
219 i--;
220 }
221 }
222 KCmdLineArgs::init( gui_argc,gui_argv,"kvim", I18N_NOOP("Vim inside KDE"),VIM_VERSION_SHORT);
223}// }}}
224
225/****************************************************************************
226 * Focus handlers:
227 */
228
229/*
230 * Initialises time intervals for the cursor blinking
231 */
232 void
233gui_mch_set_blinking(long waittime, long on, long off)//{{{
234{
235 gui.w->set_blink_time( waittime, on, off );
236}//}}}
237
238/*
239 * Stop the cursor blinking. Show the cursor if it wasn't shown.
240 */
241 void
242gui_mch_stop_blink()//{{{
243{
244 gui.w->stop_cursor_blinking();
245}//}}}
246
247/*
248 * Start the cursor blinking. If it was already blinking, this restarts the
249 * waiting time and shows the cursor.
250 */
251 void
252gui_mch_start_blink()//{{{
253{
254 gui.w->start_cursor_blinking();
255}//}}}
256
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000257#ifdef FEAT_MZSCHEME
258 void
259mzscheme_kde_start_timer()//{{{
260{
261 gui.w->enable_mzscheme_threads();
262}//}}}
263 void
264mzscheme_kde_stop_timer()//{{{
265{
266 gui.w->disable_mzscheme_threads();
267}//}}}
268#endif
269
Bram Moolenaar843ee412004-06-30 16:16:41 +0000270/*
271 * Check if the GUI can be started. Called before gvimrc is sourced.
272 * Return OK or FAIL.
273 */
274 int
275gui_mch_init_check(void)//{{{
276{
277 gui.dpy = qt_xdisplay();
278 return OK;
279}//}}}
280
281/*
282 * Initialise the X GUI. Create all the windows, set up all the call-backs etc.
283 * Returns OK for success, FAIL when the GUI can't be started.
284 */
285 int
286gui_mch_init()//{{{
287{
288 (void) new KApplication();
289 KApplication::kApplication()->dcopClient()->registerAs(KApplication::kApplication()->name(),false);
290// dbf("%s %s",KGlobal::locale()->language().latin1(),KLocale::defaultLanguage().latin1());
291
292 vmw = new VimMainWindow("KVim",0);
293 vmw->setFrameBorderWidth(0);
294 kapp->setMainWidget(vmw);
295 kapp->setTopWidget(vmw);
296
297 sbpool = new SBPool;
298
299#if QT_VERSION>=300
300 vmw->connect(kapp->clipboard(),SIGNAL(selectionChanged()),vmw,SLOT(clipboard_selection_update()));
301#endif
302 vmw->connect(kapp->clipboard(),SIGNAL(dataChanged()),vmw,SLOT(clipboard_data_update()));
303 clip_lose_selection(&clip_plus);
304 clip_lose_selection(&clip_star);
305
306 gui.in_focus = FALSE; // will be updated
307
308 if (reverse) {
309 gui.def_norm_pixel = gui_get_color((char_u *)"White");
310 gui.def_back_pixel = gui_get_color((char_u *)"Black");
311#if QT_VERSION>=300
312 gui.w->setEraseColor ( QColor(Qt::black) );
313#else
314 gui.w->setBackgroundColor ( QColor(Qt::black) );
315#endif
316 } else {
317 gui.def_norm_pixel = gui_get_color((char_u *)"Black");
318 gui.def_back_pixel = gui_get_color((char_u *)"White");
319#if QT_VERSION>=300
320 gui.w->setEraseColor ( QColor(Qt::white) );
321#else
322 gui.w->setBackgroundColor ( QColor(Qt::white) );
323#endif
324 }
325
326 gui.norm_pixel = gui.def_norm_pixel;
327 gui.back_pixel = gui.def_back_pixel;
328
329 gui.border_width = 1;
330 gui.border_offset = 1;//gui.border_width;
331 gui.scrollbar_width=SB_DEFAULT_WIDTH;
332 gui.scrollbar_height=SB_DEFAULT_WIDTH;
333
334 //gui.menu_height=vmw->menuBar()->height()+1;
335 //gui.toolbar_height=vmw->toolBar()->height();
336
337 return OK;
338}//}}}
339
340
341/*
342 * Called when the foreground or background color has been changed.
343 */
344 void
345gui_mch_new_colors()//{{{
346{
347 QColor rgb;
348 rgb.setRgb(gui.back_pixel);
349#if QT_VERSION>=300
350 gui.w->setEraseColor(rgb);
351#else
352 gui.w->setBackgroundColor(rgb);
353#endif
354}//}}}
355
356/*
357 * Open the GUI window which was created by a call to gui_mch_init().
358 */
359 int
360gui_mch_open()//{{{
361{
362 gui.dpy=qt_xdisplay();
363 set_normal_colors();
364
365 /* Check that none of the colors are the same as the background color */
366 gui_check_colors();
367
368 /* Get the colors for the highlight groups (gui_check_colors() might have
369 * changed them).
370 */
371 highlight_gui_started(); /* re-init colors and fonts */
372#ifdef FEAT_MENU
373 vmw->w->menu = new QPopupMenu(vmw);
374
375#if QT_VERSION>=300
376 vmw->w->menu->insertItem(SmallIcon("ktip"), i18n("&Tip of the day..."), vmw, SLOT(showTipOfTheDay()));
377 vmw->w->menu->insertSeparator();
378#endif
379 if (vmw->have_tearoff) vmw->w->menu->insertTearOffHandle(0,0);
380 vmw->w->menu->insertItem(i18n("&Report Bug ..."), vmw, SLOT(showBugReport()));
381 vmw->w->menu->insertSeparator();
382 vmw->w->menu->insertItem(SmallIcon("kvim"), i18n("&About KVim..."), vmw, SLOT(showAboutApplication()));
383 vmw->w->menu->insertItem(SmallIcon("about_kde"), i18n("About &KDE..."), vmw, SLOT(showAboutKDE()));
384 vmw->menuBar()->insertItem("&KVim", vmw->w->menu);
385#endif
386 if (startfont!=NULL)
387 gui_mch_init_font((char_u*)startfont->latin1(),0);
388
389 if (startsize!=NULL)
390 vmw->resize(startsize->width(), startsize->height());
391
392 gui_mch_update_codec();
393
394 if (kapp->isRestored())
395 if (KMainWindow::canBeRestored(1))
396 vmw->restore(1);
397
398 vmw->show();
399#if QT_VERSION>=300
400 if (tip==2) KTipDialog::showTip (vmw,QString::null,true);
401 else if (tip==0) KTipDialog::showTip (vmw);
402#endif
403
404 return OK;
405}//}}}
406
407 void
408gui_mch_exit(int rc)//{{{
409{
410 kapp->quit();
411}//}}}
412
413/*
414 * Get the position of the top left corner of the window.
415 */
416 int
417gui_mch_get_winpos(int *x, int *y)//{{{
418{
419 *x = vmw->x();
420 *y = vmw->y();
421 return OK;
422}//}}}
423
424/*
425 * Set the position of the top left corner of the window to the given
426 * coordinates.
427 */
428 void
429gui_mch_set_winpos(int x, int y)//{{{
430{
431 vmw->move(x,y);
432}//}}}
433
434/*
435 * Set the windows size.
436 * ->resize VimWidget
437 * ->resize vmw (block any events generated from here)
438 */
439void
440gui_mch_set_shellsize(int width, int height,//{{{
441 int min_width, int min_height,
442 int base_width, int base_height)
443{
444 //resize VimWidget
445 vmw->w->resize(width,height);
446
447 //resize vmw
448 int vheight, vwidth;
449 vheight = height;
450 vwidth = width;
451
452 if (gui.which_scrollbars[SBAR_LEFT]) vwidth+=gui.scrollbar_width;
453 if (gui.which_scrollbars[SBAR_RIGHT]) vwidth+=gui.scrollbar_width;
454 if (gui.which_scrollbars[SBAR_BOTTOM]) vheight+=gui.scrollbar_height;
455
456 if (vmw->menuBar()->isVisible() && vmw->menuBar()->isEnabled()
457#if QT_VERSION>=300
458 && !vmw->menuBar()->isTopLevelMenu()
459#endif
460 )
461 vheight += vmw->menuBar()->height();
462#ifdef FEAT_TOOLBAR
463 if (vmw->toolBar()->isVisible() && vmw->toolBar()->isEnabled() &&
464 (vmw->toolBar()->barPos()==KToolBar::Top ||
465 vmw->toolBar()->barPos()==KToolBar::Bottom))
466 vheight += vmw->toolBar()->height();
467
468 if (vmw->toolBar()->isVisible() && vmw->toolBar()->isEnabled() &&
469 (vmw->toolBar()->barPos()==KToolBar::Left ||
470 vmw->toolBar()->barPos()==KToolBar::Right))
471 vwidth += vmw->toolBar()->width();
472#endif
473 vmw->lock();
474 vmw->resize(vwidth,vheight);
475 gui_mch_update();
476 //size should be nearly perfect, update baseSize and sizeIncrement
477 vmw->setBaseSize(base_width,vmw->menuBar()->height()+1+vmw->toolBar()->height()+gui.char_height*2);
478 vmw->setSizeIncrement( ( ( int )( gui.char_width/2 )*2 ),gui.char_height);
479 vmw->unlock();
480}//}}}
481
482
483/*
484 * The screen size is used to make sure the initial window doesn't get bigger
485 * then the screen. This subtracts some room for menubar, toolbar and window
486 * decorations.
487 */
488 void
489gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)//{{{
490{
491 *screen_w = kapp->desktop()->width();
492 *screen_h = kapp->desktop()->height();
493}//}}}
494
495#if defined(FEAT_MENU) || defined(PROTO)
496 void
497gui_mch_enable_menu(int showit)//{{{
498{
499 if (showit)
500 vmw->menuBar()->show();
501 else
502 vmw->menuBar()->hide();
503 vmw->resize(vmw->width(),vmw->height());
504}//}}}
505#endif
506
507
508#if defined(FEAT_TOOLBAR) || defined(PROTO)
509 void
510gui_mch_show_toolbar(int showit)//{{{
511{
512 if (showit)
513 vmw->toolBar()->show();
514 else
515 vmw->toolBar()->hide();
516 vmw->resize(vmw->width(),vmw->height());
517}//}}}
518#endif
519
520/*
521 * Put up a font dialog and return the selected font name in allocated memory.
522 * "oldval" is the previous value.
523 * Return NULL when cancelled.
524 */
525
526char_u *gui_mch_font_dialog (char_u *oldval)//{{{
527{
528 QFont myFont( vmw->w->font() );
529 if (gui.fontname) {
530 gui.fontname=NULL;
531 }
532 int result = KFontDialog::getFont( myFont, true );
533 if ( result != KFontDialog::Accepted ) {
534 return NULL;
535 }
536// myFont.setFixedPitch(true);
537#if QT_VERSION>=300
538 QString n = myFont.toString();
539#else
540 QString n = KVimUtils::toString(&myFont);
541#endif
542 n.replace(QRegExp(","),"/");
543 gui.fontname = (char_u *)strdup((const char *)n);
544 n.replace(QRegExp(" "),"\\ ");
545 n=QString("To set this font as your default font for KVim, edit your ~/.gvimrc file and add the following lines : \nif has(\"gui_kde\")\nset guifont=")+n+QString("\nendif");// \n OR \n use the control center of KDE and choose the correct fixed font");
546
547 //display a message box which explains how to save your font settings
548 KMessageBox::information(vmw, n,"Font Selection", "kvimselectfont");
549
550 return vim_strsave(gui.fontname);
551}//}}}
552
553/*
554 * Initialise vim to use the font with the given name.
555 * Return FAIL if the font could not be loaded, OK otherwise.
556 */
557 int
558gui_mch_init_font(char_u * font_name, int fontset)//{{{
559{
560 QString fontname;
561 GuiFont font=NULL;
562
563 if (font_name==NULL) {
564#if 0
565#if QT_VERSION>=300
566 KConfig *base = KGlobal::config();
567#else
568 KConfigBase *base = KGlobal::config();
569#endif
570 base->setGroup("General");
571 if(!base->hasKey("fixed")) {
572 KMessageBox::error(KApplication::kApplication()->mainWidget(),"Cannot load default fixed font\n\nConfigure fonts in KDE Control Center.\n(Just click 'Choose...', 'OK' and then 'Apply')");
573 return FAIL;
574 }
575#if QT_VERSION>=300
576 QString f = base->readEntry("fixed");
577#else
578 QFont ft = base->readFontEntry("fixed", NULL);
579 QString f = KVimUtils::toString(&ft);
580#endif
581 font_name = (char_u*)strdup(f.latin1()); //latin1 ?
582#else
583 font_name = (char_u*)strdup("misc-fixed/10/-1/5/50/0/0/0/1/0");
584#endif
585 }
586 fontname = (const char *)font_name;
587/* fontname.replace(QRegExp("/"),",");
588 font = new QFont();
589 font->fromString( fontname );
590*/
591 gui_mch_free_font(gui.norm_font);
592#ifdef FEAT_XFONTSET
593 gui_mch_free_fontset(gui.fontset);
594 if (fontset)
595 font = gui_mch_get_fontset(font_name,TRUE,TRUE);
596#endif
597 if (font == NULL) {
598 font = gui_mch_get_font(font_name,FALSE);
599 gui.norm_font = font;
600#ifdef FEAT_XFONTSET
601 gui.fontset=NOFONTSET;
602#endif
603 }
604#ifdef FEAT_XFONTSET
605 else {
606 gui.fontset=font;
607 gui.norm_font=NOFONT;
608 }
609#endif
610
611 if (font == NULL)
612 return FAIL;
613
614 if (fontname.contains('*') && fontname.contains('-'))
615 return FAIL;
616
617 QFontMetrics f(*font);
618 gui.char_width = f.maxWidth();
619 gui.char_height = f.height()+p_linespace;
620 gui.char_ascent = f.ascent()+p_linespace/2;
621
622 //check values, just to make sure and avoid a crash
623 if (gui.char_width<=0) gui.char_width=8;
624 if (gui.char_height<=0) gui.char_height=1;
625
626 hl_set_font_name(font_name);
627
628 return OK;
629}//}}}
630
631 GuiFont
632gui_mch_get_font(char_u * name, int report_error)//{{{
633{
634 QString fontname((const char *)name);
635 if (!gui.in_use || name == NULL)
636 return NOFONT;
637 if (fontname.contains('*') && fontname.contains('-'))
638 return NOFONT; // XFLD names not allowed anymore
639 QFont *myFont = new QFont();
640 fontname.replace(QRegExp("/"),",");
641// myFont->setRawMode(TRUE);
642
643#if QT_VERSION>=300
644 myFont->fromString(fontname);
645#else
646 KVimUtils::fromString(myFont,fontname);
647#endif
648 myFont->setFixedPitch(true);
649 if (!myFont->fixedPitch()) dbf("Non fixed-width font");
650 return (GuiFont) myFont;
651}//}}}
652
653/*
654 * Set the current text font.
655 * Since we create all GC on demand, we use just gui.current_font to
656 * indicate the desired current font.
657 */
658 void
659gui_mch_set_font(GuiFont font)//{{{
660{
661 gui.current_font=font;
662 gui.w->painter->setFont( *(gui.current_font) );
663}//}}}
664
665/*
666 * If a font is not going to be used, free its structure.
667 */
668 void
669gui_mch_free_font(GuiFont font)//{{{
670{
671 if (font)
672 delete font; // this is a QFont , we can delete it :)
673}//}}}
674
675GuiFontset gui_mch_get_fontset (char_u *name, int report_error, int fixed_width)
676{
677 return (GuiFontset)gui_mch_get_font(name,report_error);
678}
679
680void gui_mch_set_fontset (GuiFontset fontset)
681{
682 gui_mch_set_font((GuiFont)fontset);
683}
684
685void gui_mch_free_fontset (GuiFontset fontset)
686{
687 if (fontset)
688 delete fontset;
689}
690
691void gui_mch_settitle (char_u *title, char_u *icon)//{{{
692{
693 if (!gui.in_use) /* can't do this when GUI not running */
694 return;
695 vmw->setPlainCaption((const char *)title);
696 QPixmap p((const char *)icon);
697 vmw->setIcon(p); //FIXME
698}//}}}
699
700/*
701 * Return the Pixel value (color) for the given color name. This routine was
702 * pretty much taken from example code in the Silicon Graphics OSF/Motif
703 * Programmer's Guide.
704 * Return -1 for error.
705 */
706 guicolor_T
707gui_mch_get_color(char_u * name)//{{{
708{
709 int i;
710 static char *(vimnames[][2]) =
711 {
712 /* A number of colors that some X11 systems don't have */
713 {"LightRed", "#FFA0A0"},
714 {"LightGreen", "#80FF80"},
715 {"LightMagenta", "#FFA0FF"},
716 {"DarkCyan", "#008080"},
717 {"DarkBlue", "#0000C0"},
718 {"DarkRed", "#C00000"},
719 {"DarkMagenta", "#C000C0"},
720 {"DarkGrey", "#C0C0C0"},
721 {NULL, NULL}
722 };
723
724 if (!gui.in_use) /* can't do this when GUI not running */
725 return (guicolor_T)(-1);
726
727 QColor _color((const char *)name);
728
729
730 if (_color.isValid()) {
731 //return (_color.red() << 16) + ((_color.green() << 8)) + (_color.blue());
732 return _color.rgb();
733 //return (guicolor_T) _color.pixel();
734 }
735
736 /* add a few builtin names */
737 for (i = 0;; ++i) {
738 if (vimnames[i][0] == NULL)
739 return (guicolor_T)(-1);
740 if (STRICMP(name, vimnames[i][0]) == 0) {
741 name = (char_u *) vimnames[i][1];
742 return gui_mch_get_color(name);
743 }
744 }
745
746 return (guicolor_T)(-1); // dead code, should not be reached..
747}//}}}
748
749/*
750 * Set the current text foreground color.
751 */
752 void
753gui_mch_set_fg_color(guicolor_T color)//{{{
754{
755 QColor rgb;
756 rgb.setRgb(color);
757 gui.w->painter->setPen( rgb );
758}//}}}
759
760/*
761 * Set the current text background color.
762 */
763 void
764gui_mch_set_bg_color(guicolor_T color)//{{{
765{
766 QColor rgb;
767 rgb.setRgb(color);
768 gui.w->painter->setBackgroundColor(rgb);
769}//}}}
770
771/*
772 * Use the blank mouse pointer or not.
773 *
774 * hide: TRUE = use blank ptr, FALSE = use parent ptr
775 */
776 void
777gui_mch_mousehide(int hide)//{{{
778{
779 if (hide == gui.pointer_hidden) return;
780 //#ifdef FEAT_MOUSESHAPE
781 // if( !hide) mch_set_mouse_shape(last_shape);
782 //#else
783# if (QT_VERSION<300)
784 gui.w->setCursor((hide)?BlankCursor:ArrowCursor);
785# else
786 gui.w->setCursor((hide)?Qt::BlankCursor:Qt::ArrowCursor);
787# endif
788 //#endif
789 gui.pointer_hidden = hide;
790}//}}}
791
792 void
793gui_mch_update_codec()
794{
795#ifdef FEAT_MBYTE
796 if (!gui.in_use) return;
797 vmw->codec = QTextCodec::codecForName((const char *)p_enc);
798 if (vmw->codec==NULL)
799 vmw->codec = QTextCodec::codecForName(KVimUtils::convertEncodingName(QString((const char*)p_enc)));
800 if (vmw->codec==NULL)
801 vmw->codec = QTextCodec::codecForLocale();
802#else
803 vmw->codec = QTextCodec::codecForLocale();
804#endif
805 if (vmw->codec==NULL)
806 vmw->codec = QTextCodec::codecForName("ISO-8859-1"); //fallback
807}
808
809 void
810gui_mch_draw_string(int row, int col, char_u * s, int len, int flags)//{{{
811{
812 QString text = vmw->codec->toUnicode((const char *)s,len);
813 gui.w->draw_string( TEXT_X(col), TEXT_Y(row), text, text.length(), flags );
814}//}}}
815
816#if defined(FEAT_TITLE) || defined(PROTO)
817/*
818 * Return the text window-id and display. Only required for X-based GUI's
819 */
820 int
821gui_get_x11_windis(Window * win, Display ** dis)//{{{
822{
823 *win = /*vmw*/gui.w->winId();
824 *dis = qt_xdisplay();
825 return OK;
826}//}}}
827#endif
828
829 void
830gui_mch_beep()//{{{
831{
832 kapp->beep();
833}//}}}
834
835 void
836gui_mch_flash(int msec)//{{{
837{
838 gui.w->flash();
839}//}}}
840
841/*
842 * Invert a rectangle from row r, column c, for nr rows and nc columns.
843 */
844 void
845gui_mch_invert_rectangle(int r, int c, int nr, int nc)//{{{
846{
847 bitBlt (
848 gui.w,
849 FILL_X(c), FILL_Y(r),
850 gui.w,
851 FILL_X(c), FILL_Y(r),
852 (nc) * gui.char_width,
853 (nr) * gui.char_height,
854 Qt::NotROP, // raster Operation
855 true ); // ignoreMask
856}//}}}
857
858/*
859 * Iconify the GUI window.
860 */
861 void
862gui_mch_iconify()//{{{
863{
864 vmw->showMinimized();
865}//}}}
866
867/*
868 * Draw a cursor without focus.
869 */
870 void
871gui_mch_draw_hollow_cursor(guicolor_T color)//{{{
872{
873 QPainter p(gui.w);
874 p.setPen( color );
875
876 p.drawRect(FILL_X(gui.col), FILL_Y(gui.row), gui.char_width - 1, gui.char_height - 1 );
877
878 p.end();
879}//}}}
880
881/*
882 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
883 * color "color".
884 */
885 void
886gui_mch_draw_part_cursor(int w, int h, guicolor_T color)//{{{
887{
888 QPainter p(gui.w);
889 p.setPen( color );
890 p.fillRect(
891 FILL_X(gui.col),
892 FILL_Y(gui.row) + gui.char_height - h +1,
893 w, h-2, QColor( color, color));
894 p.drawRect(FILL_X(gui.col),FILL_Y(gui.row) + gui.char_height - h + (int)p_linespace / 2,
895 w, h - (int)p_linespace );
896
897}//}}}
898
899
900/*
901 * Catch up with any queued X11 events. This may put keyboard input into the
902 * input buffer, call resize call-backs, trigger timers etc. If there is
903 * nothing in the X11 event queue (& no timers pending), then we return
904 * immediately.
905 */
906 void
907gui_mch_update()//{{{
908{
909 kapp->processEvents();
910}//}}}
911
912
913/*
914 * GUI input routine called by gui_wait_for_chars(). Waits for a character
915 * from the keyboard.
916 * wtime == -1 Wait forever.
917 * wtime == 0 This should never happen.
918 * wtime > 0 Wait wtime milliseconds for a character.
919 * Returns OK if a character was found to be available within the given time,
920 * or FAIL otherwise.
921 */
922 int
923gui_mch_wait_for_chars(long wtime)//{{{
924{
925 // malte@kde.org's gift to KVim ;), thanks to him :) for this hard to find bug
926 if (wtime>0) {
927 gui.w->wait( wtime );
928 while ( vim_is_input_buf_empty() && !gui.w->wait_done )
929 kapp->processOneEvent();
930 return vim_is_input_buf_empty() ? FAIL : OK;
931 } else
932 while (vim_is_input_buf_empty() ) {
933 kapp->processOneEvent();
934 }
935
936 return OK;
937}//}}}
938
939
940/****************************************************************************
941 * Output drawing routines.
942 ****************************************************************************/
943
944
945/* Flush any output to the screen */
946 void
947gui_mch_flush()//{{{
948{
949 kapp->flushX();
950}//}}}
951
952/*
953 * Clear a rectangular region of the screen from text pos (row1, col1) to
954 * (row2, col2) inclusive.
955 */
956 void
957gui_mch_clear_block(int row1, int col1, int row2, int col2)//{{{
958{
959 gui.w->erase (FILL_X(col1), FILL_Y(row1),
960 (col2 - col1 + 1) * gui.char_width+ (col2 == Columns - 1),
961 (row2 - row1 + 1) * gui.char_height );
962}//}}}
963
964 void
965gui_mch_clear_all(void)//{{{
966{
967 gui.w->erase();
968}//}}}
969
970
971/*
972 * Delete the given number of lines from the given row, scrolling up any
973 * text further down within the scroll region.
974 */
975 void
976gui_mch_delete_lines(int row, int num_lines)//{{{
977{
978 if (num_lines <= 0)
979 return;
980
981 if (row + num_lines > gui.scroll_region_bot) {
982 /* Scrolled out of region, just blank the lines out */
983 gui_clear_block(row, gui.scroll_region_left, gui.scroll_region_bot, gui.scroll_region_right);
984 } else {
985 bitBlt (
986 gui.w,
987 FILL_X(gui.scroll_region_left), FILL_Y(row),
988 gui.w,
989 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
990 gui.char_width * (gui.scroll_region_right -gui.scroll_region_left + 1) + 1,
991 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
992 Qt::CopyROP, // raster Operation
993 true ); // ignoreMask
994
995 /* Update gui.cursor_row if the cursor scrolled or copied over */
996 if (gui.cursor_row >= row) {
997 if (gui.cursor_row < row + num_lines)
998 gui.cursor_is_valid = FALSE;
999 else if (gui.cursor_row <= gui.scroll_region_bot)
1000 gui.cursor_row -= num_lines;
1001 }
1002
1003 gui_clear_block(gui.scroll_region_bot - num_lines + 1, gui.scroll_region_left,
1004 gui.scroll_region_bot, gui.scroll_region_right);
1005
1006 }
1007}//}}}
1008
1009/*
1010 * Insert the given number of lines before the given row, scrolling down any
1011 * following text within the scroll region.
1012 */
1013 void
1014gui_mch_insert_lines(int row, int num_lines)//{{{
1015{
1016 if (num_lines <= 0)
1017 return;
1018
1019 if (row + num_lines > gui.scroll_region_bot) {
1020 /* Scrolled out of region, just blank the lines out */
1021 gui_clear_block(row, gui.scroll_region_left, gui.scroll_region_bot, gui.scroll_region_right - 1);
1022 } else {
1023 bitBlt (
1024 gui.w,
1025 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
1026 gui.w,
1027 FILL_X(gui.scroll_region_left), FILL_Y(row),
1028 gui.char_width * ( gui.scroll_region_right - gui.scroll_region_left + 1 ) + 1,
1029 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1),
1030 Qt::CopyROP, // raster Operation
1031 true ); // ignoreMask
1032
1033 /* Update gui.cursor_row if the cursor scrolled or copied over */
1034 if (gui.cursor_row >= gui.row) {
1035 if (gui.cursor_row <= gui.scroll_region_bot - num_lines)
1036 gui.cursor_row += num_lines;
1037 else if (gui.cursor_row <= gui.scroll_region_bot)
1038 gui.cursor_is_valid = FALSE;
1039 }
1040
1041 gui_clear_block(row, gui.scroll_region_left, row + num_lines - 1, gui.scroll_region_right);
1042 }
1043}//}}}
1044
1045/*
1046 * X Selection stuff, for cutting and pasting text to other windows.
1047 */
1048 void
1049clip_mch_request_selection(VimClipboard *cbd)//{{{
1050{
1051#if QT_VERSION>=300
1052 if (cbd==&clip_star) kapp->clipboard()->setSelectionMode(true);
1053#endif
1054 QString selection = kapp->clipboard()->text();
1055
1056 QCString unistring = vmw->codec->fromUnicode(selection);
1057 clip_yank_selection(MCHAR,(char_u *)(const char*)unistring,(long) unistring.length(),cbd);
1058#if QT_VERSION>=300
1059 if (cbd==&clip_star) kapp->clipboard()->setSelectionMode(false);
1060#endif
1061}//}}}
1062
1063 void
1064clip_mch_lose_selection(VimClipboard *cbd)//{{{
1065{
1066 //Don't need to do anything here
1067 gui_mch_update();
1068}//}}}
1069
1070/*
1071 * Check whatever we allready own the selection.
1072 */
1073 int
1074clip_mch_own_selection(VimClipboard *cbd)//{{{
1075{
1076 if (kapp->clipboard()->ownsSelection())
1077 return OK;
1078 else {
1079#if QT_VERSION>=300
1080 kapp->clipboard()->setSelectionMode(true);
1081#endif
1082 return OK;
1083 }
1084}//}}}
1085
1086/*
1087 * Send the current selection to the clipboard.
1088 */
1089void
1090clip_mch_set_selection(VimClipboard *cbd){//{{{
1091 char_u *data;
1092 long_u length;
1093
1094 clip_get_selection(cbd);
1095 if(clip_convert_selection(&data,&length,cbd)<0) return;
1096
1097 QString selection((const char *) data);
1098 //We must turncate the string because it is not
1099 // null terminated
1100 selection.truncate((uint) length);
1101
1102#if QT_VERSION>=300
1103 if (cbd==&clip_star) kapp->clipboard()->setSelectionMode(true);
1104#endif
1105 kapp->clipboard()->setText(selection);
1106#if QT_VERSION>=300
1107 kapp->clipboard()->setSelectionMode(false);
1108#endif
1109}//}}}
1110
1111
1112#if defined(FEAT_MENU) || defined(PROTO)
1113/*
1114 * Make a menu item appear either active or not active (grey or not grey).
1115 */
1116 void
1117gui_mch_menu_grey(vimmenu_T * menu, int grey)//{{{
1118{
1119 if ( !menu || !menu->parent || !menu->parent->widget ) return;
1120 menu->parent->widget->setItemEnabled((int)menu, !grey);
1121 gui_mch_update();
1122}//}}}
1123
1124/*
1125 * Make menu item hidden or not hidden.
1126 */
1127 void
1128gui_mch_menu_hidden(vimmenu_T * menu, int hidden)//{{{
1129{
1130 //FIXME: cannot be fixed AFAIK
1131 gui_mch_menu_grey(menu,hidden); // it's hard to remove an item in a QPopupMenu
1132}//}}}
1133
1134/*
1135 * This is called after setting all the menus to grey/hidden or not.
1136 */
1137 void
1138gui_mch_draw_menubar()//{{{
1139{
1140 // nothing to do under kde
1141}//}}}
1142#endif
1143
1144/*
1145 * Scrollbar stuff.
1146 */
1147 void
1148gui_mch_enable_scrollbar(scrollbar_T * sb, int flag)//{{{
1149{
1150 if (!sb->w) return;
1151 int width = gui.w->width();
1152 int height = gui.w->height();
1153 int neww = vmw->width();
1154 int newh = vmw->height();
1155
1156 if (gui.which_scrollbars[SBAR_LEFT]) width += gui.scrollbar_width;
1157 if (gui.which_scrollbars[SBAR_RIGHT]) width += gui.scrollbar_width;
1158 if (gui.which_scrollbars[SBAR_BOTTOM]) height += gui.scrollbar_height;
1159
1160 if (vmw->menuBar()->isVisible() && vmw->menuBar()->isEnabled()
1161#if QT_VERSION>=300
1162 && !vmw->menuBar()->isTopLevelMenu()
1163#endif
1164 )
1165 height += vmw->menuBar()->height();
1166#ifdef FEAT_TOOLBAR
1167 if (vmw->toolBar()->isVisible() && vmw->toolBar()->isEnabled() &&
1168 (vmw->toolBar()->barPos()==KToolBar::Top ||
1169 vmw->toolBar()->barPos()==KToolBar::Bottom))
1170 height += vmw->toolBar()->height();
1171
1172 if (vmw->toolBar()->isVisible() && vmw->toolBar()->isEnabled() &&
1173 (vmw->toolBar()->barPos()==KToolBar::Left ||
1174 vmw->toolBar()->barPos()==KToolBar::Right))
1175 width += vmw->toolBar()->width();
1176#endif
1177 if ( abs(vmw->width() - width)>5 && (sb->type==SBAR_LEFT || sb->type==SBAR_RIGHT) )
1178 neww=width;
1179 if (abs(vmw->height() - height)>5 && (sb->type==SBAR_BOTTOM) )
1180 newh=height;
1181
1182 if (flag)
1183 sb->w->show();
1184 else
1185 sb->w->hide();
1186 gui_mch_update();
1187 vmw->lock();
1188 vmw->resize(neww,newh);
1189 vmw->unlock();
1190 gui_mch_update();
1191}//}}}
1192
1193/*
1194 * Return the RGB value of a pixel as "#RRGGBB".
1195 */
1196 long_u
1197gui_mch_get_rgb(guicolor_T pixel)//{{{
1198{
1199// QColor c(pixel,pixel);
1200// return (c.red() << 16) + ((c.green() << 8)) + (c.blue());
1201 return pixel; // funny no ? it looks like with Qt we can always use directly the rgb value (i hope i don't break colors again ;p)
1202}//}}}
1203
1204/*
1205 * Get current y mouse coordinate in text window.
1206 * Return -1 when unknown.
1207 */
1208 int
1209gui_mch_get_mouse_x(void)//{{{
1210{
1211 return vmw->mapFromGlobal( QCursor::pos() ).x();
1212}//}}}
1213
1214 int
1215gui_mch_get_mouse_y(void)//{{{
1216{
1217 return vmw->mapFromGlobal( QCursor::pos() ).y();
1218}//}}}
1219
1220 void
1221gui_mch_setmouse(int x, int y)//{{{
1222{
1223 QCursor::setPos( vmw->mapToGlobal( QPoint(x,y)) );
1224}//}}}
1225
1226#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
1227#if QT_VERSION>=300
1228static int mshape_ids[] = {//{{{
1229 Qt::ArrowCursor, /* arrow */
1230 Qt::BlankCursor, /* blank */
1231 Qt::IbeamCursor, /* beam */
1232 Qt::SizeVerCursor, /* updown */
1233 Qt::SplitHCursor, /* udsizing */
1234 Qt::SizeHorCursor, /* leftright */
1235 Qt::SizeHorCursor, /* lrsizing */
1236 Qt::WaitCursor, /* busy */
1237 Qt::ForbiddenCursor, /* no */
1238 Qt::CrossCursor, /* crosshair */
1239 Qt::PointingHandCursor, /* hand1 */
1240 Qt::PointingHandCursor, /* hand2 */
1241 Qt::ArrowCursor, /* pencil */
1242 Qt::WhatsThisCursor, /* question */
1243 Qt::ArrowCursor, /* right-arrow */
1244 Qt::UpArrowCursor, /* up-arrow */
1245 Qt::ArrowCursor /* last one */
1246};//}}}
1247#else
1248static int mshape_ids[] = {//{{{
1249 ArrowCursor, /* arrow */
1250 BlankCursor, /* blank */
1251 IbeamCursor, /* beam */
1252 SizeVerCursor, /* updown */
1253 SplitHCursor, /* udsizing */
1254 SizeHorCursor, /* leftright */
1255 SizeHorCursor, /* lrsizing */
1256 WaitCursor, /* busy */
1257 ForbiddenCursor, /* no */
1258 CrossCursor, /* crosshair */
1259 PointingHandCursor, /* hand1 */
1260 PointingHandCursor, /* hand2 */
1261 ArrowCursor, /* pencil */
1262 ArrowCursor, /* question */
1263 ArrowCursor, /* right-arrow */
1264 UpArrowCursor, /* up-arrow */
1265 ArrowCursor /* last one */
1266};//}}}
1267#endif
1268
1269 void
1270mch_set_mouse_shape (int shape)//{{{
1271{
1272 int id;
1273
1274 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
1275#if QT_VERSION>=300
1276 gui.w->setCursor(Qt::BlankCursor);
1277#else
1278 gui.w->setCursor(BlankCursor);
1279#endif
1280 else
1281 {
1282 if (shape >= MSHAPE_NUMBERED)
1283 {
1284 id = shape - MSHAPE_NUMBERED;
1285 /* if (id >= GDK_NUM_GLYPHS)
1286 id = GDK_LEFT_PTR;
1287 else
1288 id &= ~1;*/ /* they are always even (why?) */
1289 id &= -1;
1290 }
1291 else
1292 id = mshape_ids[shape];
1293
1294 gui.w->setCursor(id);
1295 }
1296 if (shape != MSHAPE_HIDE)
1297 last_shape = shape;
1298}//}}}
1299#endif
1300
1301 int
1302gui_mch_adjust_charsize ()//{{{
1303{
1304 QFont f(*(gui.current_font));
1305 QFontMetrics fm (f);
1306 gui.char_height = fm.height() + p_linespace;
1307 //gui.char_height = fm.ascent() + fm.descent() + p_linespace;
1308 gui.char_ascent = fm.ascent() + p_linespace/2;
1309
1310 return OK;
1311}//}}}
1312
1313 void
1314gui_mch_set_foreground ()//{{{
1315{
1316 KWin::setActiveWindow(vmw->winId());
1317}//}}}
1318
1319
1320