summaryrefslogtreecommitdiff
path: root/src/gui_kde.cc
blob: 4b88e98d49c9cb657fa1573b2eb9be2a486ad431 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/* vi:set ts=8 sts=4 sw=4:
 *
 * VIM - Vi IMproved		by Bram Moolenaar
 *
 * Do ":help uganda"  in Vim to read copying and usage conditions.
 * Do ":help credits" in Vim to see a list of people who contributed.
 */

/*
 * Porting to KDE(2) was done by
 *
 *  (C) 2000 by Thomas Capricelli <orzel@freehackers.org>
 *
 *  Please visit http://freehackers.org/kvim for other vim- or
 *  kde-related coding. (URL currently doesn't work...)
 *
 *  $Id$
 *
 */
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <kmenubar.h>
#include <kfiledialog.h>
#include <kiconloader.h>

#include <qscrollbar.h>
#include <qcursor.h>
#include <qmessagebox.h>
#include <qiconset.h>
#include <qtextcodec.h>
#include "gui_kde_wid.h"

extern "C" {
#include "vim.h"
}

#undef dbf
#undef db
#undef mputs

#if 1
#define dbf(format, args...) { printf("%s" " : " format "\n" , __FUNCTION__ , ## args ); fflush(stdout); }
#define db()       { printf("%s\n", __FUNCTION__ );fflush(stdout); }
#else
#define dbf(format, args... )
#define db()
#endif


#ifdef FEAT_TOOLBAR
#ifndef FEAT_KDETOOLBAR
/*
 * Icons used by the toolbar code.
 *///{{{
#include "../pixmaps/tb_new.xpm"
#include "../pixmaps/tb_open.xpm"
#include "../pixmaps/tb_close.xpm"
#include "../pixmaps/tb_save.xpm"
#include "../pixmaps/tb_print.xpm"
#include "../pixmaps/tb_cut.xpm"
#include "../pixmaps/tb_copy.xpm"
#include "../pixmaps/tb_paste.xpm"
#include "../pixmaps/tb_find.xpm"
#include "../pixmaps/tb_find_next.xpm"
#include "../pixmaps/tb_find_prev.xpm"
#include "../pixmaps/tb_find_help.xpm"
#include "../pixmaps/tb_exit.xpm"
#include "../pixmaps/tb_undo.xpm"
#include "../pixmaps/tb_redo.xpm"
#include "../pixmaps/tb_help.xpm"
#include "../pixmaps/tb_macro.xpm"
#include "../pixmaps/tb_make.xpm"
#include "../pixmaps/tb_save_all.xpm"
#include "../pixmaps/tb_jump.xpm"
#include "../pixmaps/tb_ctags.xpm"
#include "../pixmaps/tb_load_session.xpm"
#include "../pixmaps/tb_save_session.xpm"
#include "../pixmaps/tb_new_session.xpm"
#include "../pixmaps/tb_blank.xpm"
#include "../pixmaps/tb_maximize.xpm"
#include "../pixmaps/tb_split.xpm"
#include "../pixmaps/tb_minimize.xpm"
#include "../pixmaps/tb_shell.xpm"
#include "../pixmaps/tb_replace.xpm"
#include "../pixmaps/tb_vsplit.xpm"
#include "../pixmaps/tb_maxwidth.xpm"
#include "../pixmaps/tb_minwidth.xpm"
//}}}
/*
 * These are the pixmaps used for the default buttons.
 * Order must exactly match toolbar_names[] in menu.c!
 *///{{{
static char **(built_in_pixmaps[]) =
{
	tb_new_xpm,
	tb_open_xpm,
	tb_save_xpm,
	tb_undo_xpm,
	tb_redo_xpm,
	tb_cut_xpm,
	tb_copy_xpm,
	tb_paste_xpm,
	tb_print_xpm,
	tb_help_xpm,
	tb_find_xpm,
	tb_save_all_xpm,
	tb_save_session_xpm,
	tb_new_session_xpm,
	tb_load_session_xpm,
	tb_macro_xpm,
	tb_replace_xpm,
	tb_close_xpm,
	tb_maximize_xpm,
	tb_minimize_xpm,
	tb_split_xpm,
	tb_shell_xpm,
	tb_find_prev_xpm,
	tb_find_next_xpm,
	tb_find_help_xpm,
	tb_make_xpm,
	tb_jump_xpm,
	tb_ctags_xpm,
	tb_vsplit_xpm,
	tb_maxwidth_xpm,
	tb_minwidth_xpm,
	tb_exit_xpm
};//}}}
#else
const char *kdeicons[] = {
	"filenew",
	"fileopen",
	"filesave",
	"undo",
	"redo",
	"editcut",
	"editcopy",
	"editpaste",
	"fileprint",
	"contents2",
	"filefind",
	"save_all",
	"fileexport",
	"filenew",
	"fileimport",
	"run",
	"edit",
	"fileclose",
	"",
	"",
	"split",
	"openterm",
	"previous",
	"next",
	"help",
	"make",
	"goto",
	"run",
	"vsplit",
	"maxwidth",
	"minwidth",
	"quit"
};
#endif
/*
 * creates a blank pixmap using tb_blank
 */
    QPixmap
pixmap_create_from_xpm(char **xpm)//{{{
{
    return (QPixmap((const char **)xpm));
}//}}}

/*
 * creates a pixmap by using a built-in number
 */
    QPixmap
pixmap_create_by_num(int pixmap_num)//{{{
{
#ifdef FEAT_KDETOOLBAR
    if (pixmap_num >= 0 && (unsigned)pixmap_num < (sizeof(kdeicons)
						   / sizeof(kdeicons[0])) - 1)
    {

	KIconLoader *il = kapp->iconLoader(); //new KIconLoader();
	QString icon;
	icon = QString(kdeicons[pixmap_num]);
	return il->loadIcon(icon, KIcon::MainToolbar);
    }
    return QPixmap();
#else
    if (pixmap_num >= 0 && (unsigned)pixmap_num < (sizeof(built_in_pixmaps)
					   / sizeof(built_in_pixmaps[0])) - 1)
	return pixmap_create_from_xpm(built_in_pixmaps[pixmap_num]);
    else
	return QPixmap();
#endif
}//}}}

/*
 * Creates a pixmap by using the pixmap "name" found in 'runtimepath'/bitmaps/
 */
    QPixmap
pixmap_create_by_dir(char_u *name)//{{{
{
    char_u full_pathname[MAXPATHL + 1];

    if (gui_find_bitmap(name, full_pathname, "xpm") == OK)
	return QPixmap((const char *)full_pathname);
    else
	return QPixmap();
}//}}}


    QPixmap
pixmap_create_from_file(char_u *file)
{
    return QPixmap((const char *)file);
}
#endif

    void
gui_mch_add_menu(vimmenu_T *menu, int idx)//{{{
{
#ifdef FEAT_MENU
    QPopupMenu *me;
    vimmenu_T *parent = menu->parent;

    if (menu_is_popup(menu->name))
    {
	menu->widget = new QPopupMenu(vmw , QSTR(menu->name));
	QObject::connect(menu->widget, SIGNAL(activated(int)), vmw,
						   SLOT(menu_activated(int)));
	return;
    }

    if (!menu_is_menubar(menu->name))
	return;

    if (parent)
    {
	idx++; // for tearoffs to be first in menus
	me = new QPopupMenu(parent->widget, QSTR(menu->name));
	parent->widget->insertItem(QSTR(menu->name), me, (long)me, idx);
    }
    else
    {
	me = new QPopupMenu(vmw->menuBar(), QSTR(menu->name));
	vmw->menuBar()->insertItem(QSTR(menu->name), me, (long)me, idx);
    }

    me->setCaption((const char *)(menu->dname));
    if (vmw->have_tearoff)
	me->insertTearOffHandle(0, 0);
    QObject::connect(me, SIGNAL(activated(int)), vmw,
						   SLOT(menu_activated(int)));
    menu->widget = me;
#endif
}//}}}


    void
gui_mch_add_menu_item(vimmenu_T *menu, int idx)//{{{
{
#ifdef FEAT_MENU
    vimmenu_T *parent = menu->parent;
#ifdef FEAT_TOOLBAR
    if (menu_is_toolbar(parent->name))
    {
	QPixmap pix;

	if (menu_is_separator(menu->name))
	{
	    vmw->toolBar()->insertSeparator();
	    return;
	}
	if (menu->iconfile != NULL)
	{
	    pix = pixmap_create_from_file(menu->iconfile);
	}
	if (!menu->icon_builtin)
	{
	    pix = pixmap_create_by_dir(menu->name);
	}
	if (pix.isNull() && menu->iconidx >= 0)
	{
	    pix = pixmap_create_by_num(menu->iconidx);
	}
#ifndef FEAT_KDETOOLBAR
	if (pix.isNull())
	{
	    pix = pixmap_create_from_xpm(tb_blank_xpm);
	}
#endif
	if (pix.isNull())
	    return; // failed
	vmw->toolBar()->insertButton(
		pix,
		(long)menu, // id
		true,
		QSTR(menu->strings[MENU_INDEX_TIP]), // tooltip or text
		idx);
	menu->parent=parent;
	return;
    }
#endif // FEAT_TOOLBAR

    idx++;
    if (menu_is_separator(menu->name))
    {
	parent->widget->insertSeparator();
	return;
    }
    parent->widget->insertItem(QSTR(menu->name), (long)menu, idx);
#endif
}//}}}


    void
gui_mch_set_text_area_pos(int x, int y, int w, int h)//{{{
{
    int X = 0;
    int Y = 0;

    if (vmw->menuBar()->isVisible() && vmw->menuBar()->isEnabled()
#if QT_VERSION>=300
	    && !vmw->menuBar()->isTopLevelMenu()
#endif
       )
	Y += vmw->menuBar()->height();
#ifdef FEAT_TOOLBAR
    if (vmw->toolBar()->isVisible() && vmw->toolBar()->isEnabled()
				   && vmw->toolBar()->barPos()==KToolBar::Top)
	Y += vmw->toolBar()->height();

    if (vmw->toolBar()->isVisible() && vmw->toolBar()->isEnabled()
				  && vmw->toolBar()->barPos()==KToolBar::Left)
	X += vmw->toolBar()->width();
#endif // FEAT_TOOLBAR

    gui.w->setGeometry(x + X, y + Y, w, h);
}//}}}


#if defined(FEAT_MENU) || defined(PROTO)
/*
 * Enable or disable mnemonics for the toplevel menus.
 */
    void
gui_gtk_set_mnemonics(int enable)//{{{ // TO BE REMOVED
{
}//}}}

    void
toggle_tearoffs(vimmenu_T *menu, int enable)//{{{
{
    while (menu != NULL)
    {
	if (!menu_is_popup(menu->name))
	{
	    if (menu->widget != 0)
	    {
		if (enable)
		    menu->widget->insertTearOffHandle(0,0);
		else
		    menu->widget->removeItem(0);
	    }
	    toggle_tearoffs(menu->children, enable);
	}
	menu = menu->next;
    }
}//}}}

	void
gui_mch_toggle_tearoffs(int enable)//{{{
{
    vmw->have_tearoff=enable;
    toggle_tearoffs(root_menu, enable);
}//}}}
#endif


#if defined(FEAT_MENU) || defined(PROTO)
/*
 * Destroy the machine specific menu widget.
 */
    void
gui_mch_destroy_menu(vimmenu_T *menu)//{{{
{
#ifdef FEAT_TOOLBAR
    if (menu->parent && menu_is_toolbar(menu->parent->name))
    {
	vmw->toolBar()->removeItem((long)menu);
	return;
    }
#endif
    if (menu->parent)
	menu->parent->widget->removeItem((long)menu);
    if (menu->widget)
	delete menu->widget;
    menu->widget = 0;
}//}}}
#endif /* FEAT_MENU */


/*
 * Scrollbar stuff.
 */

    void
gui_mch_set_scrollbar_thumb(scrollbar_T *sb, long val, long size, long max)//{{{
{
    if (!sb->w)
	return;

    sb->w->setRange(0, max + 1 - size);
    sb->w->setValue(val);

    sb->w->setLineStep(1);
    sb->w->setPageStep(size);
}//}}}

    void
gui_mch_set_scrollbar_pos(scrollbar_T *sb, int x, int y, int w, int h)//{{{
{
    if (!sb->w)
	return;

    //we add the menubar and toolbar height/width
    int X = 0;
    int Y = 0;

    if (vmw->menuBar()->isVisible() && vmw->menuBar()->isEnabled()
#if QT_VERSION>=300
					  && !vmw->menuBar()->isTopLevelMenu()
#endif
       )
	Y += vmw->menuBar()->height();
#ifdef FEAT_TOOLBAR
    if (vmw->toolBar()->isVisible() && vmw->toolBar()->isEnabled()
				   && vmw->toolBar()->barPos()==KToolBar::Top)
	Y += vmw->toolBar()->height();

    if (vmw->toolBar()->isVisible() && vmw->toolBar()->isEnabled()
				  && vmw->toolBar()->barPos()==KToolBar::Left)
	X += vmw->toolBar()->width();
#endif //FEAT_TOOLBAR
    if (sb->w->orientation() == Qt::Vertical)
    {
	bool leftscroll=gui.which_scrollbars[SBAR_LEFT];
	bool rightscroll=gui.which_scrollbars[SBAR_RIGHT];

	if (x < 20)
	    leftscroll = true;
	else
	    rightscroll = true;
	if (x < 20)
	    sb->w->setGeometry(X, y+Y, w, h);
	else
	    sb->w->setGeometry(vmw->width() - w - 1 + X, y + Y, w, h);
    }
    else
    {
	sb->w->setGeometry(x + X, y + Y, w, h);
    }
}//}}}

/* SBAR_VERT or SBAR_HORIZ */
    void
gui_mch_create_scrollbar(scrollbar_T *sb, int orient)//{{{
{
    sbpool->create(sb,orient);
    if (orient == SBAR_VERT)
	gui.scrollbar_width = sb->w->sizeHint().width();
    else
	gui.scrollbar_height = sb->w->sizeHint().height();
}//}}}

    void
gui_mch_destroy_scrollbar(scrollbar_T *sb)//{{{
{
    sbpool->destroy(sb);
}//}}}

#if defined(FEAT_BROWSE) || defined(PROTO)
/*
 * Implementation of the file selector related stuff
 */

/*
 * Convert the Vim-style filter specification 's' to the KDE-style
 * filter specification.
 *      Vim-style:      {label}\t{pattern1};{pattern2}\n
 *      KDE-style:      {pattern1} {pattern2}|{label}\n
 *
 * The newly constructed filter is returned in allocated memory and
 * must be freed by the calling program.
 */
    static char *
convert_filter(char_u *s)
{
    char	*res;
    unsigned	i;
    unsigned	pattern_len;
    char	*filter_label;
    char	*filter_pattern;

    // The conversion generates a string of equal length to the original
    // pattern, so allocate enough memory to hold the original string.
    res = new char[STRLEN(s) + 1];
    s = vim_strsave(s);
    if (res != NULL && s != NULL)
    {
	// Make sure the first byte is a NUL so that strcat()
	// will append at the beginning of the string.
	res[0] = '\0';
	filter_label = strtok((char *) s, "\t");
	while (filter_label != NULL)
	{
	    filter_pattern = strtok( 0L, "\n");
	    if (filter_pattern != NULL)
	    {
		pattern_len = (unsigned) STRLEN(filter_pattern);
		for (i = 0; i < pattern_len; ++i)
		    if (filter_pattern[i] == ';')
			filter_pattern[i] = ' ';

		strcat(res, filter_pattern);
		strcat(res, "|");
		strcat(res, filter_label);
		strcat(res, "\n");
	    }
	    filter_label = strtok(0L, "\t");
	}
    }
    if (s)
	vim_free(s);
    return res;
}

/*
 * Put up a file requester.
 * Returns the selected name in allocated memory, or NULL for Cancel.
 * saving,			select file to write
 * title			title for the window
 * dflt				default name
 * ext				not used (extension added)
 * initdir			initial directory, NULL for current dir
 * filter			not used (file name filter)
 */
/*ARGSUSED*/
    char_u *
gui_mch_browse(int saving,//{{{
		char_u *title,
		char_u *dflt,
		char_u *ext,
		char_u *initdir,
		char_u *filter)
{
    char *filt_glob;

    filt_glob = convert_filter(filter);

    gui_mch_mousehide(FALSE);

    QString s;
    if (!saving)
	s = KFileDialog::getOpenFileName(QSTR(initdir), QSTR(filt_glob),
							  vmw, QSTR(title));
    else
	s = KFileDialog::getSaveFileName();

    if (filt_glob)
	delete filt_glob;

    if (s.isNull())
	return NULL;
    QCString unistring = vmw->codec->fromUnicode(s);
    char_u *s2 = (char_u *)(const char *)unistring;
    if (s2)
	s2 = vim_strsave(s2);

    return s2;
}//}}}

#endif	/* FEAT_BROWSE */

#ifdef FEAT_GUI_DIALOG

/* ARGSUSED */
    int
gui_mch_dialog(int type,		/* type of dialog *///{{{
		char_u *title,		/* title of dialog */
		char_u *message,	/* message text */
		char_u *buttons,	/* names of buttons */
		int    def_but,		/* default button */
		char_u *textfield)
{
    gui_mch_mousehide(FALSE);
    VimDialog vd(type, title, message, buttons, def_but,textfield);
    int ret = vd.exec();
    return ret;
}//}}}


#endif	/* FEAT_GUI_DIALOG */

#if defined(FEAT_MENU) || defined(PROTO)
    void
gui_mch_show_popupmenu(vimmenu_T *menu)//{{{
{
    menu->widget->popup(QCursor::pos());
}//}}}

/* TODO: use mouse_pos */
void
gui_make_popup (char_u *pathname, int mouse_pos)//{{{
{
    vimmenu_T *menu = gui_find_menu(pathname);

    if (menu != NULL)
	menu->widget->popup(QCursor::pos());
}//}}}
#endif



/* Find and Replace implementations */
    void
gui_mch_find_dialog(exarg_T *eap)//{{{
{
    // char_u* entry_text;
    //int exact_word=FALSE;
    //    entry_text = get_find_dialog_text(eap->arg,&exact_word);

    vmw->finddlg->setCaseSensitive(true);

    /*    if (entry_text!=NULL)
     *    {
	  vmw->finddlg->setText(QString((char *)entry_text));
    // 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
    }*/ // Don't use it, KDE keeps old search in memory and vim give \\Csearch, which is difficult to handle
    //   vim_free(entry_text);

    vmw->finddlg->show();
}//}}}

    void
gui_mch_replace_dialog(exarg_T *eap)//{{{
{
    //  char_u* entry_text;
    //int exact_word=FALSE;

    //    entry_text = get_find_dialog_text(eap->arg,&exact_word);

    /*    if (entry_text!=NULL)
     *    {
     vmw->repldlg->setText(QString((char *)entry_text));
    // 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
    }*/
    //vim_free(entry_text);

    vmw->repldlg->show();
}//}}}

    void
ex_helpfind(exarg_T *eap)//{{{
{
    do_cmdline_cmd((char_u *)"emenu ToolBar.FindHelp");
}//}}}