blob: 499db7754b4ba22677b071846ecf524140fca1ed [file] [log] [blame]
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00001/* vi:set ts=8 sts=4 sw=4:
Bram Moolenaar843ee412004-06-30 16:16:41 +00002 *
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#include <assert.h>
21#include <errno.h>
22#include <string.h>
23#include <kmenubar.h>
24#include <kfiledialog.h>
25#include <kiconloader.h>
26
27#include <qscrollbar.h>
28#include <qcursor.h>
29#include <qmessagebox.h>
30#include <qiconset.h>
31#include <qtextcodec.h>
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000032#include "gui_kde_wid.h"
Bram Moolenaar843ee412004-06-30 16:16:41 +000033
34extern "C" {
35#include "vim.h"
36}
37
38#undef dbf
39#undef db
40#undef mputs
41
42#if 1
43#define dbf( format, args... ) { printf( "%s" " : " format "\n" , __FUNCTION__ , ## args ); fflush(stdout); }
44#define db() { printf( "%s\n", __FUNCTION__ );fflush(stdout); }
45#else
46#define dbf(format, args... )
47#define db()
48#endif
49
50
51#ifdef FEAT_TOOLBAR
52#ifndef FEAT_KDETOOLBAR
53/*
54 * Icons used by the toolbar code.
55 *///{{{
56#include "../pixmaps/tb_new.xpm"
57#include "../pixmaps/tb_open.xpm"
58#include "../pixmaps/tb_close.xpm"
59#include "../pixmaps/tb_save.xpm"
60#include "../pixmaps/tb_print.xpm"
61#include "../pixmaps/tb_cut.xpm"
62#include "../pixmaps/tb_copy.xpm"
63#include "../pixmaps/tb_paste.xpm"
64#include "../pixmaps/tb_find.xpm"
65#include "../pixmaps/tb_find_next.xpm"
66#include "../pixmaps/tb_find_prev.xpm"
67#include "../pixmaps/tb_find_help.xpm"
68#include "../pixmaps/tb_exit.xpm"
69#include "../pixmaps/tb_undo.xpm"
70#include "../pixmaps/tb_redo.xpm"
71#include "../pixmaps/tb_help.xpm"
72#include "../pixmaps/tb_macro.xpm"
73#include "../pixmaps/tb_make.xpm"
74#include "../pixmaps/tb_save_all.xpm"
75#include "../pixmaps/tb_jump.xpm"
76#include "../pixmaps/tb_ctags.xpm"
77#include "../pixmaps/tb_load_session.xpm"
78#include "../pixmaps/tb_save_session.xpm"
79#include "../pixmaps/tb_new_session.xpm"
80#include "../pixmaps/tb_blank.xpm"
81#include "../pixmaps/tb_maximize.xpm"
82#include "../pixmaps/tb_split.xpm"
83#include "../pixmaps/tb_minimize.xpm"
84#include "../pixmaps/tb_shell.xpm"
85#include "../pixmaps/tb_replace.xpm"
86#include "../pixmaps/tb_vsplit.xpm"
87#include "../pixmaps/tb_maxwidth.xpm"
88#include "../pixmaps/tb_minwidth.xpm"
89//}}}
90/*
91 * These are the pixmaps used for the default buttons.
92 * Order must exactly match toolbar_names[] in menu.c!
93 *///{{{
94static char **(built_in_pixmaps[]) =
95{
96 tb_new_xpm,
97 tb_open_xpm,
98 tb_save_xpm,
99 tb_undo_xpm,
100 tb_redo_xpm,
101 tb_cut_xpm,
102 tb_copy_xpm,
103 tb_paste_xpm,
104 tb_print_xpm,
105 tb_help_xpm,
106 tb_find_xpm,
107 tb_save_all_xpm,
108 tb_save_session_xpm,
109 tb_new_session_xpm,
110 tb_load_session_xpm,
111 tb_macro_xpm,
112 tb_replace_xpm,
113 tb_close_xpm,
114 tb_maximize_xpm,
115 tb_minimize_xpm,
116 tb_split_xpm,
117 tb_shell_xpm,
118 tb_find_prev_xpm,
119 tb_find_next_xpm,
120 tb_find_help_xpm,
121 tb_make_xpm,
122 tb_jump_xpm,
123 tb_ctags_xpm,
124 tb_vsplit_xpm,
125 tb_maxwidth_xpm,
126 tb_minwidth_xpm,
127 tb_exit_xpm
128};//}}}
129#else
130const char *kdeicons[] = {
131 "filenew",
132 "fileopen",
133 "filesave",
134 "undo",
135 "redo",
136 "editcut",
137 "editcopy",
138 "editpaste",
139 "fileprint",
140 "contents2",
141 "filefind",
142 "save_all",
143 "fileexport",
144 "filenew",
145 "fileimport",
146 "run",
147 "edit",
148 "fileclose",
149 "",
150 "",
151 "split",
152 "openterm",
153 "previous",
154 "next",
155 "help",
156 "make",
157 "goto",
158 "run",
159 "vsplit",
160 "maxwidth",
161 "minwidth",
162 "quit"
163};
164#endif
165/*
166 * creates a blank pixmap using tb_blank
167 */
168 QPixmap
169pixmap_create_from_xpm(char **xpm)//{{{
170{
171 return(QPixmap((const char **)xpm));
172}//}}}
173
174/*
175 * creates a pixmap by using a built-in number
176 */
177 QPixmap
178pixmap_create_by_num(int pixmap_num)//{{{
179{
180#ifdef FEAT_KDETOOLBAR
181 if (pixmap_num >= 0 && (unsigned)pixmap_num < (sizeof(kdeicons)
182 / sizeof(kdeicons[0])) - 1) {
183
184 KIconLoader *il = kapp->iconLoader(); //new KIconLoader();
185 QString icon;
186 icon=QString(kdeicons[pixmap_num]);
187 return il->loadIcon(icon,KIcon::MainToolbar);
188 }
189 return QPixmap();
190#else
191 if (pixmap_num >= 0 && (unsigned)pixmap_num < (sizeof(built_in_pixmaps)
192 / sizeof(built_in_pixmaps[0])) - 1)
193 return pixmap_create_from_xpm(built_in_pixmaps[pixmap_num]);
194 else return QPixmap();
195#endif
196}//}}}
197
198/*
199 * Creates a pixmap by using the pixmap "name" found in 'runtimepath'/bitmaps/
200 */
201 QPixmap
202pixmap_create_by_dir(char_u *name)//{{{
203{
204 char_u full_pathname[MAXPATHL + 1];
205
206 if (gui_find_bitmap(name, full_pathname, "xpm") == OK) {
207 return QPixmap((const char *)full_pathname);
208 }
209 else return QPixmap();
210}//}}}
211
212
213 QPixmap
214pixmap_create_from_file(char_u *file)
215{
216 return QPixmap((const char*)file);
217}
218#endif
219
220 void
221gui_mch_add_menu(vimmenu_T * menu, int idx)//{{{
222{
223#ifdef FEAT_MENU
224 QPopupMenu *me;
225 vimmenu_T *parent = menu->parent;
226
227 if (menu_is_popup(menu->name)) {
228 menu->widget = new QPopupMenu(vmw , (const char *) menu->name);
229 QObject::connect( menu->widget, SIGNAL(activated(int)), vmw, SLOT(menu_activated(int)) );
230 return;
231 }
232
233 if (!menu_is_menubar(menu->name))
234 return;
235
236 if (parent) {
237 idx++; // for tearoffs to be first in menus
238 me = new QPopupMenu(parent->widget, (const char *) menu->name);
239 parent->widget->insertItem( QString((const char *)menu->name), me, (int)me, idx);
240 } else {
241 me = new QPopupMenu(vmw->menuBar() , (const char *) menu->name);
242 vmw->menuBar()->insertItem( QString((const char *)menu->name), me , (int) me, idx);
243 }
244
245 me->setCaption((const char*)( menu->dname ));
246 if (vmw->have_tearoff) me->insertTearOffHandle(0,0);
247 QObject::connect( me, SIGNAL(activated(int)), vmw, SLOT(menu_activated(int)) );
248 menu->widget = me;
249#endif
250}//}}}
251
252
253 void
254gui_mch_add_menu_item(vimmenu_T * menu, int idx)//{{{
255{
256#ifdef FEAT_MENU
257 vimmenu_T *parent = menu->parent;
258#ifdef FEAT_TOOLBAR
259 if (menu_is_toolbar(parent->name)) {
260 QPixmap pix;
261 if ( menu_is_separator(menu->name) )
262 {
263 vmw->toolBar()->insertSeparator();
264 return;
265 }
266 if (menu->iconfile != NULL) {
267 pix = pixmap_create_from_file(menu->iconfile);
268 }
269 if (!menu->icon_builtin) {
270 pix = pixmap_create_by_dir(menu->name);
271 }
272 if (pix.isNull() && menu->iconidx >= 0) {
273 pix = pixmap_create_by_num(menu->iconidx);
274 }
275#ifndef FEAT_KDETOOLBAR
276 if (pix.isNull()) {
277 pix = pixmap_create_from_xpm(tb_blank_xpm);
278 }
279#endif
280 if (pix.isNull()) return; // failed
281 vmw->toolBar()->insertButton (
282 pix,
283 (int) menu , // id
284 true,
285 (char *) (menu->strings[MENU_INDEX_TIP]) , // tooltip or text
286 idx
287 );
288 menu->parent=parent;
289 return;
290 }
291#endif // FEAT_TOOLBAR
292
293 idx++;
294 if ( menu_is_separator(menu->name) ) {
295 parent->widget->insertSeparator();
296 return;
297 }
298 parent->widget->insertItem(QString((const char *)menu->name), (int)menu, idx );
299#endif
300}//}}}
301
302
303 void
304gui_mch_set_text_area_pos(int x, int y, int w, int h)//{{{
305{
306 int X = 0;
307 int Y = 0;
308 if (vmw->menuBar()->isVisible() && vmw->menuBar()->isEnabled()
309#if QT_VERSION>=300
310 && !vmw->menuBar()->isTopLevelMenu()
311#endif
312 )
313 Y += vmw->menuBar()->height();
314#ifdef FEAT_TOOLBAR
315 if (vmw->toolBar()->isVisible() && vmw->toolBar()->isEnabled() &&
316 vmw->toolBar()->barPos()==KToolBar::Top)
317 Y += vmw->toolBar()->height();
318
319 if (vmw->toolBar()->isVisible() && vmw->toolBar()->isEnabled() &&
320 vmw->toolBar()->barPos()==KToolBar::Left)
321 X += vmw->toolBar()->width();
322#endif // FEAT_TOOLBAR
323
324 gui.w->setGeometry(x+X,y+Y,w,h);
325}//}}}
326
327
328#if defined(FEAT_MENU) || defined(PROTO)
329/*
330 * Enable or disable mnemonics for the toplevel menus.
331 */
332 void
333gui_gtk_set_mnemonics(int enable)//{{{ // TO BE REMOVED
334{
335}//}}}
336
337 void
338toggle_tearoffs(vimmenu_T *menu, int enable)//{{{
339{
340 while (menu != NULL) {
341 if (!menu_is_popup(menu->name)) {
342 if (menu->widget != 0) {
343 if (enable) menu->widget->insertTearOffHandle(0,0);
344 else menu->widget->removeItem(0);
345 }
346 toggle_tearoffs(menu->children, enable);
347 }
348 menu = menu->next;
349 }
350}//}}}
351
352 void
353gui_mch_toggle_tearoffs(int enable)//{{{
354{
355 vmw->have_tearoff=enable;
356 toggle_tearoffs(root_menu, enable);
357}//}}}
358#endif
359
360
361#if defined(FEAT_MENU) || defined(PROTO)
362/*
363 * Destroy the machine specific menu widget.
364 */
365 void
366gui_mch_destroy_menu(vimmenu_T * menu)//{{{
367{
368#ifdef FEAT_TOOLBAR
369 if (menu->parent && menu_is_toolbar(menu->parent->name)) {
370 vmw->toolBar()->removeItem( (int) menu );
371 return;
372 }
373#endif
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000374 if (menu->parent)
375 menu->parent->widget->removeItem((int)menu);
376 if (menu->widget)
Bram Moolenaar843ee412004-06-30 16:16:41 +0000377 delete menu->widget;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000378 menu->widget = 0;
379}//}}}
380#endif /* FEAT_MENU */
381
382
383/*
384 * Scrollbar stuff.
385 */
386
387 void
388gui_mch_set_scrollbar_thumb(scrollbar_T * sb, long val, long size, long max)//{{{
389{
390 if (!sb->w) return;
391
392 sb->w->setRange(0, max+1-size);
393 sb->w->setValue(val);
394
395 sb->w->setLineStep(1);
396 sb->w->setPageStep(size);
397}//}}}
398
399 void
400gui_mch_set_scrollbar_pos(scrollbar_T * sb, int x, int y, int w, int h)//{{{
401{
402 if (!sb->w) return;
403 //we add the menubar and toolbar height/width
404 int X = 0;
405 int Y = 0;
406
407 if (vmw->menuBar()->isVisible() && vmw->menuBar()->isEnabled()
408#if QT_VERSION>=300
409 && !vmw->menuBar()->isTopLevelMenu()
410#endif
411 )
412 Y += vmw->menuBar()->height();
413#ifdef FEAT_TOOLBAR
414 if (vmw->toolBar()->isVisible() && vmw->toolBar()->isEnabled() &&
415 vmw->toolBar()->barPos()==KToolBar::Top)
416 Y += vmw->toolBar()->height();
417
418 if (vmw->toolBar()->isVisible() && vmw->toolBar()->isEnabled() &&
419 vmw->toolBar()->barPos()==KToolBar::Left)
420 X += vmw->toolBar()->width();
421#endif //FEAT_TOOLBAR
422 if (sb->w->orientation() == Qt::Vertical) {
423 bool leftscroll=gui.which_scrollbars[SBAR_LEFT];
424 bool rightscroll=gui.which_scrollbars[SBAR_RIGHT];
425 if (x<20) leftscroll=true;
426 else rightscroll=true;
427 if (x<20) sb->w->setGeometry(X,y+Y,w,h);
428 else sb->w->setGeometry(vmw->width()-w-1+X, y+Y,w,h);
429 } else {
430 sb->w->setGeometry(x+X,y+Y,w,h);
431 }
432}//}}}
433
434/* SBAR_VERT or SBAR_HORIZ */
435 void
436gui_mch_create_scrollbar(scrollbar_T * sb, int orient)//{{{
437{
438 sbpool->create(sb,orient);
439 if (orient==SBAR_VERT)
440 gui.scrollbar_width = sb->w->sizeHint().width();
441 else
442 gui.scrollbar_height = sb->w->sizeHint().height();
443}//}}}
444
445 void
446gui_mch_destroy_scrollbar(scrollbar_T * sb)//{{{
447{
448 sbpool->destroy(sb);
449}//}}}
450
451#if defined(FEAT_BROWSE) || defined(PROTO)
452/*
453 * Implementation of the file selector related stuff
454 */
455
456/*
457 * Put up a file requester.
458 * Returns the selected name in allocated memory, or NULL for Cancel.
459 * saving, select file to write
460 * title title for the window
461 * dflt default name
462 * ext not used (extension added)
463 * initdir initial directory, NULL for current dir
464 * filter not used (file name filter)
465 */
466/*ARGSUSED*/
467char_u *
468gui_mch_browse(int saving,//{{{
469 char_u * title,
470 char_u * dflt,
471 char_u * ext,
472 char_u * initdir,
473 char_u * filter)
474{
475 char * filt_glob;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000476
477 if (filter != (char_u *) 0x0 )
478 {
Bram Moolenaar843ee412004-06-30 16:16:41 +0000479 filter = vim_strsave(filter);
480 strtok((char *) filter, "(");
481 filt_glob = strtok(0L, ")");
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000482 }
483 else
Bram Moolenaar843ee412004-06-30 16:16:41 +0000484 filt_glob = (char *) filter;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000485
Bram Moolenaar843ee412004-06-30 16:16:41 +0000486 gui_mch_mousehide(FALSE);
487
488 QString s;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000489 if (!saving)
Bram Moolenaar843ee412004-06-30 16:16:41 +0000490 s = KFileDialog::getOpenFileName( (char *) initdir, (char *) filt_glob, vmw, (char *) title );
491 else
492 s = KFileDialog::getSaveFileName( );
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000493
Bram Moolenaar843ee412004-06-30 16:16:41 +0000494 if (filter)
495 vim_free(filter);
496
497 if (s.isNull())
498 return NULL;
499 QCString unistring = vmw->codec->fromUnicode(s);
500 char_u * s2 = (char_u *)(const char*)unistring;
501 if (s2)
502 s2 = vim_strsave( s2 );
503
504 return s2;
505}//}}}
506
507#endif /* FEAT_BROWSE */
508
509#ifdef FEAT_GUI_DIALOG
510
511/* ARGSUSED */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000512 int
Bram Moolenaar843ee412004-06-30 16:16:41 +0000513gui_mch_dialog(int type, /* type of dialog *///{{{
514 char_u * title, /* title of dialog */
515 char_u * message, /* message text */
516 char_u * buttons, /* names of buttons */
517 int def_but, /* default button */
518 char_u *textfield)
519{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000520 gui_mch_mousehide(FALSE);
521 VimDialog vd(type, title, message, buttons, def_but,textfield);
522 int ret = vd.exec();
523 return ret;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000524}//}}}
525
526
527#endif /* FEAT_GUI_DIALOG */
528
529#if defined(FEAT_MENU) || defined(PROTO)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000530 void
Bram Moolenaar843ee412004-06-30 16:16:41 +0000531gui_mch_show_popupmenu(vimmenu_T * menu)//{{{
532{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000533 menu->widget->popup(QCursor::pos());
Bram Moolenaar843ee412004-06-30 16:16:41 +0000534}//}}}
535
536void
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000537gui_make_popup (char_u *pathname)//{{{
538{
539 vimmenu_T *menu = gui_find_menu(pathname);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000540
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000541 if (menu != NULL)
542 menu->widget->popup(QCursor::pos());
Bram Moolenaar843ee412004-06-30 16:16:41 +0000543}//}}}
544#endif
545
546
547
548/* Find and Replace implementations */
549 void
550gui_mch_find_dialog(exarg_T * eap)//{{{
551{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000552 // char_u* entry_text;
553 //int exact_word=FALSE;
554 // entry_text = get_find_dialog_text(eap->arg,&exact_word);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000555
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000556 vmw->finddlg->setCaseSensitive(true);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000557
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000558 /* if (entry_text!=NULL) {
559 vmw->finddlg->setText(QString((char*)entry_text));
560 // exact match should go there, hopefully KDE old KEdFind/KEdReplace will be replaced in KDE 4 as pple wanted KDE 3's Find/Replace to be kept
561 }*/ // Don't use it, KDE keeps old search in memory and vim give \\Csearch, which is difficult to handle
562 // vim_free(entry_text);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000563
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000564 vmw->finddlg->show();
Bram Moolenaar843ee412004-06-30 16:16:41 +0000565}//}}}
566
567 void
568gui_mch_replace_dialog(exarg_T * eap)//{{{
569{
570 // char_u* entry_text;
571 //int exact_word=FALSE;
572
573 // entry_text = get_find_dialog_text(eap->arg,&exact_word);
574
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000575 /* if (entry_text!=NULL) {
Bram Moolenaar843ee412004-06-30 16:16:41 +0000576 vmw->repldlg->setText(QString((char*)entry_text));
577 // exact match should go there, hopefully KDE old KEdFind/KEdReplace will be replaced in KDE 4 as pple wanted KDE 3's Find/Replace to be kept
578 }*/
579 //vim_free(entry_text);
580
581 vmw->repldlg->show();
582}//}}}
583
584 void
585ex_helpfind(exarg_T *eap)//{{{
586{
587 do_cmdline_cmd((char_u *)"emenu ToolBar.FindHelp");
588}//}}}