blob: 3865a4f4df58cf9786f34d99579b7882707d19b5 [file] [log] [blame]
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * BeBox GUI support Copyright 1998 by Olaf Seibert.
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005 * All Rights Reserved.
Bram Moolenaarb3f74062020-02-26 16:16:53 +01006 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 *
10 * Based on "GUI support for the Buzzword Enhanced Operating System."
11 *
12 * Ported to R4 by Richard Offer <richard@whitequeen.com> Jul 99
13 *
14 * Haiku support by Siarzhuk Zharski <imker@gmx.li> Apr-Mai 2009
15 *
16 */
17
18/*
19 * Structure of the Haiku GUI code:
20 *
21 * There are 3 threads.
22 * 1. The initial thread. In gui_mch_prepare() this gets to run the
23 * BApplication message loop. But before it starts doing that,
24 * it creates thread 2
25 * 2. The main() thread. This thread is created in gui_mch_prepare()
26 * and its purpose in life is to call main(argc, argv) again.
27 * This thread is doing the bulk of the work.
28 * 3. Sooner or later, a window is opened by the main() thread. This
29 * causes a second message loop to be created: the window thread.
30 *
31 * == alternatively ===
32 *
33 * #if RUN_BAPPLICATION_IN_NEW_THREAD...
34 *
35 * 1. The initial thread. In gui_mch_prepare() this gets to spawn
36 * thread 2. After doing that, it returns to main() to do the
37 * bulk of the work, being the main() thread.
38 * 2. Runs the BApplication.
39 * 3. The window thread, just like in the first case.
40 *
41 * This second alternative is cleaner from Vim's viewpoint. However,
42 * the BeBook seems to assume everywhere that the BApplication *must*
43 * run in the initial thread. So perhaps doing otherwise is very wrong.
44 *
45 * However, from a B_SINGLE_LAUNCH viewpoint, the first is better.
46 * If Vim is marked "Single Launch" in its application resources,
47 * and a file is dropped on the Vim icon, and another Vim is already
48 * running, the file is passed on to the earlier Vim. This happens
49 * in BApplication::Run(). So we want Vim to terminate if
50 * BApplication::Run() terminates. (See the BeBook, on BApplication.
51 * However, it seems that the second copy of Vim isn't even started
52 * in this case... which is for the better since I wouldn't know how
53 * to detect this case.)
54 *
55 * Communication between these threads occurs mostly by translating
56 * BMessages that come in and posting an appropriate translation on
57 * the VDCMP (Vim Direct Communication Message Port). Therefore the
58 * actions required for keypresses and window resizes, etc, are mostly
59 * performed in the main() thread.
60 *
61 * A notable exception to this is the Draw() event. The redrawing of
62 * the window contents is performed asynchronously from the window
63 * thread. To make this work correctly, a locking protocol is used when
64 * any thread is accessing the essential variables that are used by
65 * the window thread.
66 *
67 * This locking protocol consists of locking Vim's window. This is both
68 * convenient and necessary.
69 */
70
71extern "C" {
72
73#include <assert.h>
74#include <float.h>
75#include <syslog.h>
76
77#include "vim.h"
Bram Moolenaarb3f74062020-02-26 16:16:53 +010078#include "version.h"
79
Bram Moolenaarb52575f2020-04-24 22:16:13 +020080} // extern "C"
Bram Moolenaarb3f74062020-02-26 16:16:53 +010081
82// ---------------- start of header part ----------------
83
84//#include <Alert.h>
85#include <Application.h>
86#include <Beep.h>
87#include <Bitmap.h>
88#include <Box.h>
89#include <Button.h>
90#include <Clipboard.h>
91#include <Debug.h>
92//#include <Directory.h>
93//#include <Entry.h>
94#include <File.h>
95#include <FilePanel.h>
96#include <FindDirectory.h>
97//#include <Font.h>
98#include <IconUtils.h>
99#include <Input.h>
100#include <ListView.h>
101#include <MenuBar.h>
102#include <MenuItem.h>
103//#include <MessageQueue.h>
104//#include <OS.h>
105#include <Path.h>
106#include <PictureButton.h>
107#include <PopUpMenu.h>
108//#include <Region.h>
109#include <Resources.h>
110//#include <Roster.h>
111#include <Screen.h>
112#include <ScrollBar.h>
113#include <ScrollView.h>
114#include <String.h>
115#include <StringView.h>
116//#include <SupportDefs.h>
117#include <TabView.h>
118#include <TextControl.h>
119#include <TextView.h>
120#include <TranslationUtils.h>
121#include <TranslatorFormats.h>
122#include <View.h>
123#include <Window.h>
124
125class VimApp;
126class VimFormView;
127class VimTextAreaView;
128class VimWindow;
129class VimToolbar;
130class VimTabLine;
131
132extern key_map *keyMap;
133extern char *keyMapChars;
134
135extern int main(int argc, char **argv);
136
137#ifndef B_MAX_PORT_COUNT
138#define B_MAX_PORT_COUNT 255
139#endif
140
141// VimApp seems comparable to the X "vimShell"
142class VimApp: public BApplication
143{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200144 typedef BApplication Inherited;
145 public:
146 VimApp(const char *appsig);
147 ~VimApp();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100148
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200149 // callbacks:
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100150#if 0
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200151 virtual void DispatchMessage(BMessage *m, BHandler *h)
152 {
153 m->PrintToStream();
154 Inherited::DispatchMessage(m, h);
155 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100156#endif
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200157 virtual void ReadyToRun();
158 virtual void ArgvReceived(int32 argc, char **argv);
159 virtual void RefsReceived(BMessage *m);
160 virtual bool QuitRequested();
161 virtual void MessageReceived(BMessage *m);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100162
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200163 static void SendRefs(BMessage *m, bool changedir);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100164
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200165 sem_id fFilePanelSem;
166 BFilePanel* fFilePanel;
167 BPath fBrowsedPath;
168 private:
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100169};
170
171class VimWindow: public BWindow
172{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200173 typedef BWindow Inherited;
174 public:
175 VimWindow();
176 ~VimWindow();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100177
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200178 // virtual void DispatchMessage(BMessage *m, BHandler *h);
179 virtual void WindowActivated(bool active);
180 virtual bool QuitRequested();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100181
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200182 VimFormView *formView;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100183
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200184 private:
185 void init();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100186
187};
188
189class VimFormView: public BView
190{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200191 typedef BView Inherited;
192 public:
193 VimFormView(BRect frame);
194 ~VimFormView();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100195
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200196 // callbacks:
197 virtual void AllAttached();
198 virtual void FrameResized(float new_width, float new_height);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100199
200#define MENUBAR_MARGIN 1
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200201 float MenuHeight() const
202 { return menuBar ? menuBar->Frame().Height() + MENUBAR_MARGIN: 0; }
203 BMenuBar *MenuBar() const
204 { return menuBar; }
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100205
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200206 private:
207 void init(BRect);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100208
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200209 BMenuBar *menuBar;
210 VimTextAreaView *textArea;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100211
212#ifdef FEAT_TOOLBAR
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200213 public:
214 float ToolbarHeight() const;
215 VimToolbar *ToolBar() const
216 { return toolBar; }
217 private:
218 VimToolbar *toolBar;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100219#endif
220
221#ifdef FEAT_GUI_TABLINE
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200222 public:
223 VimTabLine *TabLine() const { return tabLine; }
224 bool IsShowingTabLine() const { return showingTabLine; }
225 void SetShowingTabLine(bool showing) { showingTabLine = showing; }
226 float TablineHeight() const;
227 private:
228 VimTabLine *tabLine;
229 int showingTabLine;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100230#endif
231};
232
233class VimTextAreaView: public BView
234{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200235 typedef BView Inherited;
236 public:
237 VimTextAreaView(BRect frame);
238 ~VimTextAreaView();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100239
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200240 // callbacks:
241 virtual void Draw(BRect updateRect);
242 virtual void KeyDown(const char *bytes, int32 numBytes);
243 virtual void MouseDown(BPoint point);
244 virtual void MouseUp(BPoint point);
245 virtual void MouseMoved(BPoint point, uint32 transit, const BMessage *message);
246 virtual void MessageReceived(BMessage *m);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100247
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200248 // own functions:
249 int mchInitFont(char_u *name);
250 void mchDrawString(int row, int col, char_u *s, int len, int flags);
251 void mchClearBlock(int row1, int col1, int row2, int col2);
252 void mchClearAll();
253 void mchDeleteLines(int row, int num_lines);
254 void mchInsertLines(int row, int num_lines);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100255
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200256 static void guiSendMouseEvent(int button, int x, int y, int repeated_click, int_u modifiers);
257 static void guiMouseMoved(int x, int y);
258 static void guiBlankMouse(bool should_hide);
259 static int_u mouseModifiersToVim(int32 beModifiers);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100260
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200261 int32 mouseDragEventCount;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100262
263#ifdef FEAT_MBYTE_IME
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200264 void DrawIMString(void);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100265#endif
266
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200267 private:
268 void init(BRect);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100269
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200270 int_u vimMouseButton;
271 int_u vimMouseModifiers;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100272
273#ifdef FEAT_MBYTE_IME
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200274 struct {
275 BMessenger* messenger;
276 BMessage* message;
277 BPoint location;
278 int row;
279 int col;
280 int count;
281 } IMData;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100282#endif
283};
284
285class VimScrollBar: public BScrollBar
286{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200287 typedef BScrollBar Inherited;
288 public:
289 VimScrollBar(scrollbar_T *gsb, orientation posture);
290 ~VimScrollBar();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100291
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200292 virtual void ValueChanged(float newValue);
293 virtual void MouseUp(BPoint where);
294 void SetValue(float newval);
295 scrollbar_T *getGsb()
296 { return gsb; }
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100297
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200298 int32 scrollEventCount;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100299
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200300 private:
301 scrollbar_T *gsb;
302 float ignoreValue;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100303};
304
305
306#ifdef FEAT_TOOLBAR
307
308class VimToolbar : public BBox
309{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200310 static BBitmap *normalButtonsBitmap;
311 static BBitmap *grayedButtonsBitmap;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100312
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200313 BBitmap *LoadVimBitmap(const char* fileName);
314 bool GetPictureFromBitmap(BPicture *pictureTo, int32 index, BBitmap *bitmapFrom, bool pressed);
315 bool ModifyBitmapToGrayed(BBitmap *bitmap);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100316
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200317 BList fButtonsList;
318 void InvalidateLayout();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100319
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200320 public:
321 VimToolbar(BRect frame, const char * name);
322 ~VimToolbar();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100323
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200324 bool PrepareButtonBitmaps();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100325
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200326 bool AddButton(int32 index, vimmenu_T *menu);
327 bool RemoveButton(vimmenu_T *menu);
328 bool GrayButton(vimmenu_T *menu, int grey);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100329
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200330 float ToolbarHeight() const;
331 virtual void AttachedToWindow();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100332};
333
334BBitmap *VimToolbar::normalButtonsBitmap = NULL;
335BBitmap *VimToolbar::grayedButtonsBitmap = NULL;
336
337const float ToolbarMargin = 3.;
338const float ButtonMargin = 3.;
339
340#endif //FEAT_TOOLBAR
341
342#ifdef FEAT_GUI_TABLINE
343
344class VimTabLine : public BTabView
345{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200346 public:
347 class VimTab : public BTab {
348 public:
349 VimTab() : BTab(new BView(BRect(), "-Empty-", 0, 0)) {}
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100350
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200351 virtual void Select(BView* owner);
352 };
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100353
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200354 VimTabLine(BRect r) : BTabView(r, "vimTabLine", B_WIDTH_FROM_LABEL,
355 B_FOLLOW_LEFT | B_FOLLOW_TOP | B_FOLLOW_RIGHT, B_WILL_DRAW | B_FRAME_EVENTS) {}
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100356
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200357 float TablineHeight() const;
358 virtual void MouseDown(BPoint point);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100359};
360
361#endif //FEAT_GUI_TABLINE
362
363
364// For caching the fonts that are used;
365// Vim seems rather sloppy in this regard.
366class VimFont: public BFont
367{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200368 typedef BFont Inherited;
369 public:
370 VimFont();
371 VimFont(const VimFont *rhs);
372 VimFont(const BFont *rhs);
373 VimFont(const VimFont &rhs);
374 ~VimFont();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100375
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200376 VimFont *next;
377 int refcount;
378 char_u *name;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100379
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200380 private:
381 void init();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100382};
383
384#if defined(FEAT_GUI_DIALOG)
385
386class VimDialog : public BWindow
387{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200388 typedef BWindow Inherited;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100389
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200390 BButton* _CreateButton(int32 which, const char* label);
391
392 public:
393
394 class View : public BView {
395 typedef BView Inherited;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100396
397 public:
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200398 View(BRect frame);
399 ~View();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100400
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200401 virtual void Draw(BRect updateRect);
402 void InitIcon(int32 type);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100403
404 private:
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200405 BBitmap* fIconBitmap;
406 };
407
408 VimDialog(int type, const char *title, const char *message,
409 const char *buttons, int dfltbutton, const char *textfield,
410 int ex_cmd);
411 ~VimDialog();
412
413 int Go();
414
415 virtual void MessageReceived(BMessage *msg);
416
417 private:
418 sem_id fDialogSem;
419 int fDialogValue;
420 BList fButtonsList;
421 BTextView* fMessageView;
422 BTextControl* fInputControl;
423 const char* fInputValue;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100424};
425
426class VimSelectFontDialog : public BWindow
427{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200428 typedef BWindow Inherited;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100429
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200430 void _CleanList(BListView* list);
431 void _UpdateFontStyles();
432 void _UpdateSizeInputPreview();
433 void _UpdateFontPreview();
434 bool _UpdateFromListItem(BListView* list, char* text, int textSize);
435 public:
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100436
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200437 VimSelectFontDialog(font_family* family, font_style* style, float* size);
438 ~VimSelectFontDialog();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100439
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200440 bool Go();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100441
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200442 virtual void MessageReceived(BMessage *msg);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100443
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200444 private:
445 status_t fStatus;
446 sem_id fDialogSem;
447 bool fDialogValue;
448 font_family* fFamily;
449 font_style* fStyle;
450 float* fSize;
451 font_family fFontFamily;
452 font_style fFontStyle;
453 float fFontSize;
454 BStringView* fPreview;
455 BListView* fFamiliesList;
456 BListView* fStylesList;
457 BListView* fSizesList;
458 BTextControl* fSizesInput;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100459};
460
461#endif // FEAT_GUI_DIALOG
462
463// ---------------- end of GUI classes ----------------
464
465struct MainArgs {
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200466 int argc;
467 char **argv;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100468};
469
470// These messages are copied through the VDCMP.
471// Therefore they ought not to have anything fancy.
472// They must be of POD type (Plain Old Data)
473// as the C++ standard calls them.
474
475#define KEY_MSG_BUFSIZ 7
476#if KEY_MSG_BUFSIZ < MAX_KEY_CODE_LEN
477#error Increase KEY_MSG_BUFSIZ!
478#endif
479
480struct VimKeyMsg {
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200481 char_u length;
482 char_u chars[KEY_MSG_BUFSIZ]; // contains Vim encoding
483 bool csi_escape;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100484};
485
486struct VimResizeMsg {
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200487 int width;
488 int height;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100489};
490
491struct VimScrollBarMsg {
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200492 VimScrollBar *sb;
493 long value;
494 int stillDragging;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100495};
496
497struct VimMenuMsg {
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200498 vimmenu_T *guiMenu;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100499};
500
501struct VimMouseMsg {
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200502 int button;
503 int x;
504 int y;
505 int repeated_click;
506 int_u modifiers;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100507};
508
509struct VimMouseMovedMsg {
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200510 int x;
511 int y;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100512};
513
514struct VimFocusMsg {
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200515 bool active;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100516};
517
518struct VimRefsMsg {
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200519 BMessage *message;
520 bool changedir;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100521};
522
523struct VimTablineMsg {
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200524 int index;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100525};
526
527struct VimTablineMenuMsg {
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200528 int index;
529 int event;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100530};
531
532struct VimMsg {
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200533 enum VimMsgType {
534 Key, Resize, ScrollBar, Menu, Mouse, MouseMoved, Focus, Refs, Tabline, TablineMenu
535 };
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100536
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200537 union {
538 struct VimKeyMsg Key;
539 struct VimResizeMsg NewSize;
540 struct VimScrollBarMsg Scroll;
541 struct VimMenuMsg Menu;
542 struct VimMouseMsg Mouse;
543 struct VimMouseMovedMsg MouseMoved;
544 struct VimFocusMsg Focus;
545 struct VimRefsMsg Refs;
546 struct VimTablineMsg Tabline;
547 struct VimTablineMenuMsg TablineMenu;
548 } u;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100549};
550
551#define RGB(r, g, b) ((char_u)(r) << 16 | (char_u)(g) << 8 | (char_u)(b) << 0)
Bram Moolenaar9e175142020-04-30 22:51:01 +0200552#define GUI_TO_RGB(g) { (char_u)((g) >> 16), (char_u)((g) >> 8), (char_u)((g) >> 0), 255 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100553
554// ---------------- end of header part ----------------
555
556static struct specialkey
557{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200558 uint16 BeKeys;
559#define KEY(a,b) ((a)<<8|(b))
560#define K(a) KEY(0,a) // for ASCII codes
561#define F(b) KEY(1,b) // for scancodes
562 char_u vim_code0;
563 char_u vim_code1;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100564} special_keys[] =
565{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200566 {K(B_UP_ARROW), 'k', 'u'},
567 {K(B_DOWN_ARROW), 'k', 'd'},
568 {K(B_LEFT_ARROW), 'k', 'l'},
569 {K(B_RIGHT_ARROW), 'k', 'r'},
570 {K(B_BACKSPACE), 'k', 'b'},
571 {K(B_INSERT), 'k', 'I'},
572 {K(B_DELETE), 'k', 'D'},
573 {K(B_HOME), 'k', 'h'},
574 {K(B_END), '@', '7'},
575 {K(B_PAGE_UP), 'k', 'P'}, // XK_Prior
576 {K(B_PAGE_DOWN), 'k', 'N'}, // XK_Next,
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100577
578#define FIRST_FUNCTION_KEY 11
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200579 {F(B_F1_KEY), 'k', '1'},
580 {F(B_F2_KEY), 'k', '2'},
581 {F(B_F3_KEY), 'k', '3'},
582 {F(B_F4_KEY), 'k', '4'},
583 {F(B_F5_KEY), 'k', '5'},
584 {F(B_F6_KEY), 'k', '6'},
585 {F(B_F7_KEY), 'k', '7'},
586 {F(B_F8_KEY), 'k', '8'},
587 {F(B_F9_KEY), 'k', '9'},
588 {F(B_F10_KEY), 'k', ';'},
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100589
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200590 {F(B_F11_KEY), 'F', '1'},
591 {F(B_F12_KEY), 'F', '2'},
592 // {XK_F13, 'F', '3'}, // would be print screen
593 // sysreq
594 {F(0x0F), 'F', '4'}, // scroll lock
595 {F(0x10), 'F', '5'}, // pause/break
596 // {XK_F16, 'F', '6'},
597 // {XK_F17, 'F', '7'},
598 // {XK_F18, 'F', '8'},
599 // {XK_F19, 'F', '9'},
600 // {XK_F20, 'F', 'A'},
601 // {XK_F21, 'F', 'B'},
602 // {XK_F22, 'F', 'C'},
603 // {XK_F23, 'F', 'D'},
604 // {XK_F24, 'F', 'E'},
605 // {XK_F25, 'F', 'F'},
606 // {XK_F26, 'F', 'G'},
607 // {XK_F27, 'F', 'H'},
608 // {XK_F28, 'F', 'I'},
609 // {XK_F29, 'F', 'J'},
610 // {XK_F30, 'F', 'K'},
611 // {XK_F31, 'F', 'L'},
612 // {XK_F32, 'F', 'M'},
613 // {XK_F33, 'F', 'N'},
614 // {XK_F34, 'F', 'O'},
615 // {XK_F35, 'F', 'P'}, // keysymdef.h defines up to F35
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100616
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200617 // {XK_Help, '%', '1'}, // XK_Help
618 {F(B_PRINT_KEY), '%', '9'},
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100619
620#if 0
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200621 // Keypad keys:
622 {F(0x48), 'k', 'l'}, // XK_KP_Left
623 {F(0x4A), 'k', 'r'}, // XK_KP_Right
624 {F(0x38), 'k', 'u'}, // XK_KP_Up
625 {F(0x59), 'k', 'd'}, // XK_KP_Down
626 {F(0x64), 'k', 'I'}, // XK_KP_Insert
627 {F(0x65), 'k', 'D'}, // XK_KP_Delete
628 {F(0x37), 'k', 'h'}, // XK_KP_Home
629 {F(0x58), '@', '7'}, // XK_KP_End
630 {F(0x39), 'k', 'P'}, // XK_KP_Prior
631 {F(0x60), 'k', 'N'}, // XK_KP_Next
632 {F(0x49), '&', '8'}, // XK_Undo, keypad 5
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100633#endif
634
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200635 // End of list marker:
636 {0, 0, 0}
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100637};
638
K.Takataeeec2542021-06-02 13:28:16 +0200639#define NUM_SPECIAL_KEYS ARRAY_LENGTH(special_keys)
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100640
641// ---------------- VimApp ----------------
642
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200643 static void
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100644docd(BPath &path)
645{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200646 mch_chdir((char *)path.Path());
647 // Do this to get the side effects of a :cd command
648 do_cmdline_cmd((char_u *)"cd .");
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100649}
650
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200651 static void
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100652drop_callback(void *cookie)
653{
654 // TODO here we could handle going to a specific position in the dropped
Bram Moolenaarbe7529e2020-08-13 21:05:39 +0200655 // file (see src/gui_mac.c, deleted in 8.2.1422)
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100656 // Update the screen display
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100657 update_screen(UPD_NOT_VALID);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100658}
659
660 // Really handle dropped files and folders.
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200661 static void
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100662RefsReceived(BMessage *m, bool changedir)
663{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200664 uint32 type;
665 int32 count;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100666
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200667 m->PrintToStream();
668 switch (m->what) {
669 case B_REFS_RECEIVED:
670 case B_SIMPLE_DATA:
671 m->GetInfo("refs", &type, &count);
672 if (type != B_REF_TYPE)
673 goto bad;
674 break;
675 case B_ARGV_RECEIVED:
676 m->GetInfo("argv", &type, &count);
677 if (type != B_STRING_TYPE)
678 goto bad;
679 if (changedir) {
680 char *dirname;
681 if (m->FindString("cwd", (const char **) &dirname) == B_OK) {
682 chdir(dirname);
683 do_cmdline_cmd((char_u *)"cd .");
684 }
685 }
686 break;
687 default:
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100688bad:
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200689 /*fprintf(stderr, "bad!\n"); */
690 delete m;
691 return;
692 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100693
694#ifdef FEAT_VISUAL
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200695 reset_VIsual();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100696#endif
697
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200698 char_u **fnames;
699 fnames = (char_u **) alloc(count * sizeof(char_u *));
700 int fname_index = 0;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100701
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200702 switch (m->what) {
703 case B_REFS_RECEIVED:
704 case B_SIMPLE_DATA:
705 // fprintf(stderr, "case B_REFS_RECEIVED\n");
706 for (int i = 0; i < count; ++i)
707 {
708 entry_ref ref;
709 if (m->FindRef("refs", i, &ref) == B_OK) {
710 BEntry entry(&ref, false);
711 BPath path;
712 entry.GetPath(&path);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100713
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200714 // Change to parent directory?
715 if (changedir) {
716 BPath parentpath;
717 path.GetParent(&parentpath);
718 docd(parentpath);
719 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100720
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200721 // Is it a directory? If so, cd into it.
722 BDirectory bdir(&ref);
723 if (bdir.InitCheck() == B_OK) {
724 // don't cd if we already did it
725 if (!changedir)
726 docd(path);
727 } else {
728 mch_dirname(IObuff, IOSIZE);
729 char_u *fname = shorten_fname((char_u *)path.Path(), IObuff);
730 if (fname == NULL)
731 fname = (char_u *)path.Path();
732 fnames[fname_index++] = vim_strsave(fname);
733 // fprintf(stderr, "%s\n", fname);
734 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100735
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200736 // Only do it for the first file/dir
737 changedir = false;
738 }
739 }
740 break;
741 case B_ARGV_RECEIVED:
742 // fprintf(stderr, "case B_ARGV_RECEIVED\n");
743 for (int i = 1; i < count; ++i)
744 {
745 char *fname;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100746
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200747 if (m->FindString("argv", i, (const char **) &fname) == B_OK) {
748 fnames[fname_index++] = vim_strsave((char_u *)fname);
749 }
750 }
751 break;
752 default:
753 // fprintf(stderr, "case default\n");
754 break;
755 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100756
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200757 delete m;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100758
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200759 // Handle the drop, :edit to get to the file
760 if (fname_index > 0) {
761 handle_drop(fname_index, fnames, FALSE, drop_callback, NULL);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100762
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200763 setcursor();
764 out_flush();
765 } else {
766 vim_free(fnames);
767 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100768}
769
770VimApp::VimApp(const char *appsig):
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200771 BApplication(appsig),
772 fFilePanelSem(-1),
773 fFilePanel(NULL)
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100774{
775}
776
777VimApp::~VimApp()
778{
779}
780
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200781 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100782VimApp::ReadyToRun()
783{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200784 /*
785 * Apparently signals are inherited by the created thread -
786 * disable the most annoying ones.
787 */
ichizok378447f2023-05-11 22:25:42 +0100788 mch_signal(SIGINT, SIG_IGN);
789 mch_signal(SIGQUIT, SIG_IGN);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100790}
791
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200792 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100793VimApp::ArgvReceived(int32 arg_argc, char **arg_argv)
794{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200795 if (!IsLaunching()) {
796 /*
797 * This can happen if we are set to Single or Exclusive
798 * Launch. Be nice and open the file(s).
799 */
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100800 if (gui.vimWindow)
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200801 gui.vimWindow->Minimize(false);
802 BMessage *m = CurrentMessage();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100803 DetachCurrentMessage();
804 SendRefs(m, true);
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200805 }
806}
807
808 void
809VimApp::RefsReceived(BMessage *m)
810{
811 // Horrible hack!!! XXX XXX XXX
812 // The real problem is that b_start_ffc is set too late for
813 // the initial empty buffer. As a result the window will be
814 // split instead of abandoned.
815 int limit = 15;
816 while (--limit >= 0 && (curbuf == NULL || curbuf->b_start_ffc == 0))
817 snooze(100000); // 0.1 s
818 if (gui.vimWindow)
819 gui.vimWindow->Minimize(false);
820 DetachCurrentMessage();
821 SendRefs(m, true);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100822}
823
824/*
825 * Pass a BMessage on to the main() thread.
826 * Caller must have detached the message.
827 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200828 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100829VimApp::SendRefs(BMessage *m, bool changedir)
830{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200831 VimRefsMsg rm;
832 rm.message = m;
833 rm.changedir = changedir;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100834
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200835 write_port(gui.vdcmp, VimMsg::Refs, &rm, sizeof(rm));
836 // calls ::RefsReceived
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100837}
838
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200839 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100840VimApp::MessageReceived(BMessage *m)
841{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200842 switch (m->what) {
843 case 'save':
844 {
845 entry_ref refDirectory;
846 m->FindRef("directory", &refDirectory);
847 fBrowsedPath.SetTo(&refDirectory);
848 BString strName;
849 m->FindString("name", &strName);
850 fBrowsedPath.Append(strName.String());
851 }
852 break;
853 case 'open':
854 {
855 entry_ref ref;
856 m->FindRef("refs", &ref);
857 fBrowsedPath.SetTo(&ref);
858 }
859 break;
860 case B_CANCEL:
861 {
862 BFilePanel *panel;
863 m->FindPointer("source", (void**)&panel);
864 if (fFilePanelSem != -1 && panel == fFilePanel)
865 {
866 delete_sem(fFilePanelSem);
867 fFilePanelSem = -1;
868 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100869
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200870 }
871 break;
872 default:
873 Inherited::MessageReceived(m);
874 break;
875 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100876}
877
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200878 bool
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100879VimApp::QuitRequested()
880{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200881 (void)Inherited::QuitRequested();
882 return false;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100883}
884
885// ---------------- VimWindow ----------------
886
887VimWindow::VimWindow():
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200888 BWindow(BRect(40, 40, 150, 150),
889 "Vim",
890 B_TITLED_WINDOW,
891 0,
892 B_CURRENT_WORKSPACE)
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100893
894{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200895 init();
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100896}
897
898VimWindow::~VimWindow()
899{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200900 if (formView) {
901 RemoveChild(formView);
902 delete formView;
903 }
904 gui.vimWindow = NULL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100905}
906
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200907 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100908VimWindow::init()
909{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200910 // Attach the VimFormView
911 formView = new VimFormView(Bounds());
912 if (formView != NULL) {
913 AddChild(formView);
914 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100915}
916
917#if 0 // disabled in zeta patch
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200918 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100919VimWindow::DispatchMessage(BMessage *m, BHandler *h)
920{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200921 /*
922 * Route B_MOUSE_UP messages to MouseUp(), in
923 * a manner that should be compatible with the
924 * intended future system behaviour.
925 */
926 switch (m->what) {
927 case B_MOUSE_UP:
928 // if (!h) h = PreferredHandler();
929 // gcc isn't happy without this extra set of braces, complains about
930 // jump to case label crosses init of 'class BView * v'
931 // richard@whitequeen.com jul 99
932 {
933 BView *v = dynamic_cast<BView *>(h);
934 if (v) {
935 // m->PrintToStream();
936 BPoint where;
937 m->FindPoint("where", &where);
938 v->MouseUp(where);
939 } else {
940 Inherited::DispatchMessage(m, h);
941 }
942 }
943 break;
944 default:
945 Inherited::DispatchMessage(m, h);
946 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100947}
948#endif
949
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200950 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100951VimWindow::WindowActivated(bool active)
952{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200953 Inherited::WindowActivated(active);
954 // the textArea gets the keyboard action
955 if (active && gui.vimTextArea)
956 gui.vimTextArea->MakeFocus(true);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100957
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200958 struct VimFocusMsg fm;
959 fm.active = active;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100960
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200961 write_port(gui.vdcmp, VimMsg::Focus, &fm, sizeof(fm));
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100962}
963
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200964 bool
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100965VimWindow::QuitRequested()
966{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200967 struct VimKeyMsg km;
968 km.length = 5;
969 memcpy((char *)km.chars, "\033:qa\r", km.length);
970 km.csi_escape = false;
971 write_port(gui.vdcmp, VimMsg::Key, &km, sizeof(km));
972 return false;
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100973}
974
975// ---------------- VimFormView ----------------
976
977VimFormView::VimFormView(BRect frame):
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200978 BView(frame, "VimFormView", B_FOLLOW_ALL_SIDES,
979 B_WILL_DRAW | B_FRAME_EVENTS),
980 menuBar(NULL),
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100981#ifdef FEAT_TOOLBAR
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200982 toolBar(NULL),
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100983#endif
984#ifdef FEAT_GUI_TABLINE
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200985// showingTabLine(false),
986 tabLine(NULL),
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100987#endif
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200988 textArea(NULL)
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100989{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200990 init(frame);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100991}
992
993VimFormView::~VimFormView()
994{
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200995 if (menuBar) {
996 RemoveChild(menuBar);
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100997#ifdef never
Bram Moolenaarb52575f2020-04-24 22:16:13 +0200998 // deleting the menuBar leads to SEGV on exit
999 // richard@whitequeen.com Jul 99
1000 delete menuBar;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001001#endif
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001002 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001003
1004#ifdef FEAT_TOOLBAR
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001005 delete toolBar;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001006#endif
1007
1008#ifdef FEAT_GUI_TABLINE
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001009 delete tabLine;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001010#endif
1011
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001012 if (textArea) {
1013 RemoveChild(textArea);
1014 delete textArea;
1015 }
1016 gui.vimForm = NULL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001017}
1018
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001019 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001020VimFormView::init(BRect frame)
1021{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001022 menuBar = new BMenuBar(BRect(0,0,-MENUBAR_MARGIN,-MENUBAR_MARGIN),
1023 "VimMenuBar");
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001024
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001025 AddChild(menuBar);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001026
1027#ifdef FEAT_TOOLBAR
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001028 toolBar = new VimToolbar(BRect(0,0,0,0), "VimToolBar");
1029 toolBar->PrepareButtonBitmaps();
1030 AddChild(toolBar);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001031#endif
1032
1033#ifdef FEAT_GUI_TABLINE
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001034 tabLine = new VimTabLine(BRect(0,0,0,0));
1035// tabLine->PrepareButtonBitmaps();
1036 AddChild(tabLine);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001037#endif
1038
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001039 BRect remaining = frame;
1040 textArea = new VimTextAreaView(remaining);
1041 AddChild(textArea);
1042 // The textArea will be resized later when menus are added
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001043
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001044 gui.vimForm = this;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001045}
1046
1047#ifdef FEAT_TOOLBAR
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001048 float
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001049VimFormView::ToolbarHeight() const
1050{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001051 return toolBar ? toolBar->ToolbarHeight() : 0.;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001052}
1053#endif
1054
1055#ifdef FEAT_GUI_TABLINE
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001056 float
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001057VimFormView::TablineHeight() const
1058{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001059 return (tabLine && IsShowingTabLine()) ? tabLine->TablineHeight() : 0.;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001060}
1061#endif
1062
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001063 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001064VimFormView::AllAttached()
1065{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001066 /*
1067 * Apparently signals are inherited by the created thread -
1068 * disable the most annoying ones.
1069 */
ichizok378447f2023-05-11 22:25:42 +01001070 mch_signal(SIGINT, SIG_IGN);
1071 mch_signal(SIGQUIT, SIG_IGN);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001072
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001073 if (menuBar && textArea) {
1074 /*
1075 * Resize the textArea to fill the space left over by the menu.
1076 * This is somewhat futile since it will be done again once
1077 * menus are added to the menu bar.
1078 */
1079 BRect remaining = Bounds();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001080
1081#ifdef FEAT_MENU
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001082 remaining.top += MenuHeight();
1083 menuBar->ResizeTo(remaining.right, remaining.top);
1084 gui.menu_height = (int) MenuHeight();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001085#endif
1086
1087#ifdef FEAT_TOOLBAR
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001088 toolBar->MoveTo(remaining.left, remaining.top);
1089 toolBar->ResizeTo(remaining.right, ToolbarHeight());
1090 remaining.top += ToolbarHeight();
1091 gui.toolbar_height = ToolbarHeight();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001092#endif
1093
1094#ifdef FEAT_GUI_TABLINE
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001095 tabLine->MoveTo(remaining.left, remaining.top);
1096 tabLine->ResizeTo(remaining.right + 1, TablineHeight());
1097 remaining.top += TablineHeight();
1098 gui.tabline_height = TablineHeight();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001099#endif
1100
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001101 textArea->ResizeTo(remaining.Width(), remaining.Height());
1102 textArea->MoveTo(remaining.left, remaining.top);
1103 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001104
1105
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001106 Inherited::AllAttached();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001107}
1108
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001109 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001110VimFormView::FrameResized(float new_width, float new_height)
1111{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001112 struct VimResizeMsg sm;
1113 int adjust_h, adjust_w;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001114
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001115 new_width += 1; // adjust from width to number of pixels occupied
1116 new_height += 1;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001117
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001118 sm.width = (int) new_width;
1119 sm.height = (int) new_height;
1120 adjust_w = ((int)new_width - gui_get_base_width()) % gui.char_width;
1121 adjust_h = ((int)new_height - gui_get_base_height()) % gui.char_height;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001122
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001123 if (adjust_w > 0 || adjust_h > 0) {
1124 sm.width -= adjust_w;
1125 sm.height -= adjust_h;
1126 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001127
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001128 write_port(gui.vdcmp, VimMsg::Resize, &sm, sizeof(sm));
1129 // calls gui_resize_shell(new_width, new_height);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001130
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001131 return;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001132
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001133 /*
1134 * The area below the vertical scrollbar is erased to the colour
1135 * set with SetViewColor() automatically, because we had set
1136 * B_WILL_DRAW. Resizing the window tight around the vertical
1137 * scroll bar also helps to avoid debris.
1138 */
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001139}
1140
1141// ---------------- VimTextAreaView ----------------
1142
1143VimTextAreaView::VimTextAreaView(BRect frame):
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001144 BView(frame, "VimTextAreaView", B_FOLLOW_ALL_SIDES,
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001145#ifdef FEAT_MBYTE_IME
Bram Moolenaar80a8d382020-05-03 22:57:32 +02001146 B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE | B_INPUT_METHOD_AWARE
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001147#else
Bram Moolenaar80a8d382020-05-03 22:57:32 +02001148 B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001149#endif
Bram Moolenaar80a8d382020-05-03 22:57:32 +02001150 ),
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001151 mouseDragEventCount(0)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001152{
1153#ifdef FEAT_MBYTE_IME
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001154 IMData.messenger = NULL;
1155 IMData.message = NULL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001156#endif
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001157 init(frame);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001158}
1159
1160VimTextAreaView::~VimTextAreaView()
1161{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001162 gui.vimTextArea = NULL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001163}
1164
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001165 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001166VimTextAreaView::init(BRect frame)
1167{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001168 // set up global var for fast access
1169 gui.vimTextArea = this;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001170
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001171 /*
1172 * Tell the app server not to erase the view: we will
1173 * fill it in completely by ourselves.
1174 * (Does this really work? Even if not, it won't harm either.)
1175 */
1176 SetViewColor(B_TRANSPARENT_32_BIT);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001177#define PEN_WIDTH 1
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001178 SetPenSize(PEN_WIDTH);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001179#define W_WIDTH(curwin) 0
1180}
1181
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001182 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001183VimTextAreaView::Draw(BRect updateRect)
1184{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001185 /*
1186 * XXX Other ports call here:
1187 * out_flush(); * make sure all output has been processed *
1188 * but we can't do that, since it involves too much information
1189 * that is owned by other threads...
1190 */
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001191
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001192 /*
1193 * No need to use gui.vimWindow->Lock(): we are locked already.
1194 * However, it would not hurt.
1195 */
1196 rgb_color rgb = GUI_TO_RGB(gui.back_pixel);
1197 SetLowColor(rgb);
1198 FillRect(updateRect, B_SOLID_LOW);
1199 gui_redraw((int) updateRect.left, (int) updateRect.top,
1200 (int) (updateRect.Width() + PEN_WIDTH), (int) (updateRect.Height() + PEN_WIDTH));
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001201
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001202 // Clear the border areas if needed
1203 SetLowColor(rgb);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001204
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001205 if (updateRect.left < FILL_X(0)) // left border
1206 FillRect(BRect(updateRect.left, updateRect.top,
1207 FILL_X(0)-PEN_WIDTH, updateRect.bottom), B_SOLID_LOW);
1208 if (updateRect.top < FILL_Y(0)) // top border
1209 FillRect(BRect(updateRect.left, updateRect.top,
1210 updateRect.right, FILL_Y(0)-PEN_WIDTH), B_SOLID_LOW);
1211 if (updateRect.right >= FILL_X(Columns)) // right border
1212 FillRect(BRect(FILL_X((int)Columns), updateRect.top,
1213 updateRect.right, updateRect.bottom), B_SOLID_LOW);
1214 if (updateRect.bottom >= FILL_Y(Rows)) // bottom border
1215 FillRect(BRect(updateRect.left, FILL_Y((int)Rows),
1216 updateRect.right, updateRect.bottom), B_SOLID_LOW);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001217
1218#ifdef FEAT_MBYTE_IME
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001219 DrawIMString();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001220#endif
1221}
1222
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001223 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001224VimTextAreaView::KeyDown(const char *bytes, int32 numBytes)
1225{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001226 struct VimKeyMsg km;
1227 char_u *dest = km.chars;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001228
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001229 bool canHaveVimModifiers = false;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001230
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001231 BMessage *msg = Window()->CurrentMessage();
1232 assert(msg);
1233 // msg->PrintToStream();
1234
1235 /*
1236 * Convert special keys to Vim codes.
1237 * I think it is better to do it in the window thread
1238 * so we use at least a little bit of the potential
1239 * of our 2 CPUs. Besides, due to the fantastic mapping
1240 * of special keys to UTF-8, we have quite some work to
1241 * do...
1242 * TODO: I'm not quite happy with detection of special
1243 * keys. Perhaps I should use scan codes after all...
1244 */
1245 if (numBytes > 1) {
1246 // This cannot be a special key
1247 if (numBytes > KEY_MSG_BUFSIZ)
1248 numBytes = KEY_MSG_BUFSIZ; // should never happen... ???
1249 km.length = numBytes;
1250 memcpy((char *)dest, bytes, numBytes);
1251 km.csi_escape = true;
1252 } else {
1253 int32 scancode = 0;
1254 msg->FindInt32("key", &scancode);
1255
1256 int32 beModifiers = 0;
1257 msg->FindInt32("modifiers", &beModifiers);
1258
1259 char_u string[3];
1260 int len = 0;
1261 km.length = 0;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001262
1263 /*
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001264 * For normal, printable ASCII characters, don't look them up
1265 * to check if they might be a special key. They aren't.
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001266 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001267 assert(B_BACKSPACE <= 0x20);
1268 assert(B_DELETE == 0x7F);
1269 if (((char_u)bytes[0] <= 0x20 || (char_u)bytes[0] == 0x7F) &&
1270 numBytes == 1) {
1271 /*
1272 * Due to the great nature of Be's mapping of special keys,
1273 * viz. into the range of the control characters,
1274 * we can only be sure it is *really* a special key if
1275 * if it is special without using ctrl. So, only if ctrl is
1276 * used, we need to check it unmodified.
1277 */
1278 if (beModifiers & B_CONTROL_KEY) {
1279 int index = keyMap->normal_map[scancode];
1280 int newNumBytes = keyMapChars[index];
1281 char_u *newBytes = (char_u *)&keyMapChars[index + 1];
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001282
1283 /*
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001284 * Check if still special without the control key.
1285 * This is needed for BACKSPACE: that key does produce
1286 * different values with modifiers (DEL).
1287 * Otherwise we could simply have checked for equality.
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001288 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001289 if (newNumBytes != 1 || (*newBytes > 0x20 &&
1290 *newBytes != 0x7F )) {
1291 goto notspecial;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001292 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001293 bytes = (char *)newBytes;
1294 }
1295 canHaveVimModifiers = true;
1296
1297 uint16 beoskey;
1298 int first, last;
1299
1300 /*
1301 * If numBytes == 0 that probably always indicates a special key.
1302 * (does not happen yet)
1303 */
1304 if (numBytes == 0 || bytes[0] == B_FUNCTION_KEY) {
1305 beoskey = F(scancode);
1306 first = FIRST_FUNCTION_KEY;
1307 last = NUM_SPECIAL_KEYS;
1308 } else if (*bytes == '\n' && scancode == 0x47) {
1309 // remap the (non-keypad) ENTER key from \n to \r.
1310 string[0] = '\r';
1311 len = 1;
1312 first = last = 0;
1313 } else {
1314 beoskey = K(bytes[0]);
1315 first = 0;
1316 last = FIRST_FUNCTION_KEY;
1317 }
1318
1319 for (int i = first; i < last; i++) {
1320 if (special_keys[i].BeKeys == beoskey) {
1321 string[0] = CSI;
1322 string[1] = special_keys[i].vim_code0;
1323 string[2] = special_keys[i].vim_code1;
1324 len = 3;
1325 }
1326 }
1327 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001328notspecial:
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001329 if (len == 0) {
1330 string[0] = bytes[0];
1331 len = 1;
1332 }
1333
1334 // Special keys (and a few others) may have modifiers
1335#if 0
1336 if (len == 3 ||
1337 bytes[0] == B_SPACE || bytes[0] == B_TAB ||
1338 bytes[0] == B_RETURN || bytes[0] == '\r' ||
1339 bytes[0] == B_ESCAPE)
1340#else
1341 if (canHaveVimModifiers)
1342#endif
1343 {
1344 int modifiers;
1345 modifiers = 0;
1346 if (beModifiers & B_SHIFT_KEY)
1347 modifiers |= MOD_MASK_SHIFT;
1348 if (beModifiers & B_CONTROL_KEY)
1349 modifiers |= MOD_MASK_CTRL;
1350 if (beModifiers & B_OPTION_KEY)
1351 modifiers |= MOD_MASK_ALT;
1352
1353 /*
1354 * For some keys a shift modifier is translated into another key
1355 * code. Do we need to handle the case where len != 1 and
1356 * string[0] != CSI? (Not for BeOS, since len == 3 implies
1357 * string[0] == CSI...)
1358 */
1359 int key;
1360 if (string[0] == CSI && len == 3)
1361 key = TO_SPECIAL(string[1], string[2]);
1362 else
1363 key = string[0];
1364 key = simplify_key(key, &modifiers);
1365 if (IS_SPECIAL(key))
1366 {
1367 string[0] = CSI;
1368 string[1] = K_SECOND(key);
1369 string[2] = K_THIRD(key);
1370 len = 3;
1371 }
1372 else
1373 {
1374 string[0] = key;
1375 len = 1;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001376 }
1377
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001378 if (modifiers)
1379 {
1380 *dest++ = CSI;
1381 *dest++ = KS_MODIFIER;
1382 *dest++ = modifiers;
1383 km.length = 3;
1384 }
1385 }
1386 memcpy((char *)dest, string, len);
1387 km.length += len;
1388 km.csi_escape = false;
1389 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001390
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001391 write_port(gui.vdcmp, VimMsg::Key, &km, sizeof(km));
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001392
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001393 /*
1394 * blank out the pointer if necessary
1395 */
1396 if (p_mh && !gui.pointer_hidden)
1397 {
1398 guiBlankMouse(true);
1399 gui.pointer_hidden = TRUE;
1400 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001401}
1402void
1403VimTextAreaView::guiSendMouseEvent(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001404 int button,
1405 int x,
1406 int y,
1407 int repeated_click,
1408 int_u modifiers)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001409{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001410 VimMouseMsg mm;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001411
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001412 mm.button = button;
1413 mm.x = x;
1414 mm.y = y;
1415 mm.repeated_click = repeated_click;
1416 mm.modifiers = modifiers;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001417
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001418 write_port(gui.vdcmp, VimMsg::Mouse, &mm, sizeof(mm));
1419 // calls gui_send_mouse_event()
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001420
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001421 /*
1422 * if our pointer is currently hidden, then we should show it.
1423 */
1424 if (gui.pointer_hidden)
1425 {
1426 guiBlankMouse(false);
1427 gui.pointer_hidden = FALSE;
1428 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001429}
1430
1431void
1432VimTextAreaView::guiMouseMoved(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001433 int x,
1434 int y)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001435{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001436 VimMouseMovedMsg mm;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001437
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001438 mm.x = x;
1439 mm.y = y;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001440
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001441 write_port(gui.vdcmp, VimMsg::MouseMoved, &mm, sizeof(mm));
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001442
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001443 if (gui.pointer_hidden)
1444 {
1445 guiBlankMouse(false);
1446 gui.pointer_hidden = FALSE;
1447 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001448}
1449
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001450 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001451VimTextAreaView::guiBlankMouse(bool should_hide)
1452{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001453 if (should_hide) {
1454 // gui.vimApp->HideCursor();
1455 gui.vimApp->ObscureCursor();
1456 /*
1457 * ObscureCursor() would even be easier, but then
1458 * Vim's idea of mouse visibility does not necessarily
1459 * correspond to reality.
1460 */
1461 } else {
1462 // gui.vimApp->ShowCursor();
1463 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001464}
1465
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001466 int_u
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001467VimTextAreaView::mouseModifiersToVim(int32 beModifiers)
1468{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001469 int_u vim_modifiers = 0x0;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001470
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001471 if (beModifiers & B_SHIFT_KEY)
1472 vim_modifiers |= MOUSE_SHIFT;
1473 if (beModifiers & B_CONTROL_KEY)
1474 vim_modifiers |= MOUSE_CTRL;
1475 if (beModifiers & B_OPTION_KEY) // Alt or Meta key
1476 vim_modifiers |= MOUSE_ALT;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001477
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001478 return vim_modifiers;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001479}
1480
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001481 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001482VimTextAreaView::MouseDown(BPoint point)
1483{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001484 BMessage *m = Window()->CurrentMessage();
1485 assert(m);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001486
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001487 int32 buttons = 0;
1488 m->FindInt32("buttons", &buttons);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001489
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001490 int vimButton;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001491
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001492 if (buttons & B_PRIMARY_MOUSE_BUTTON)
1493 vimButton = MOUSE_LEFT;
1494 else if (buttons & B_SECONDARY_MOUSE_BUTTON)
1495 vimButton = MOUSE_RIGHT;
1496 else if (buttons & B_TERTIARY_MOUSE_BUTTON)
1497 vimButton = MOUSE_MIDDLE;
1498 else
1499 return; // Unknown button
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001500
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001501 vimMouseButton = 1; // don't care which one
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001502
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001503 // Handle multiple clicks
1504 int32 clicks = 0;
1505 m->FindInt32("clicks", &clicks);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001506
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001507 int32 modifiers = 0;
1508 m->FindInt32("modifiers", &modifiers);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001509
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001510 vimMouseModifiers = mouseModifiersToVim(modifiers);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001511
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001512 guiSendMouseEvent(vimButton, point.x, point.y,
1513 clicks > 1 /* = repeated_click*/, vimMouseModifiers);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001514}
1515
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001516 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001517VimTextAreaView::MouseUp(BPoint point)
1518{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001519 vimMouseButton = 0;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001520
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001521 BMessage *m = Window()->CurrentMessage();
1522 assert(m);
1523 // m->PrintToStream();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001524
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001525 int32 modifiers = 0;
1526 m->FindInt32("modifiers", &modifiers);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001527
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001528 vimMouseModifiers = mouseModifiersToVim(modifiers);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001529
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001530 guiSendMouseEvent(MOUSE_RELEASE, point.x, point.y,
1531 0 /* = repeated_click*/, vimMouseModifiers);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001532
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001533 Inherited::MouseUp(point);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001534}
1535
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001536 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001537VimTextAreaView::MouseMoved(BPoint point, uint32 transit, const BMessage *message)
1538{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001539 /*
1540 * if our pointer is currently hidden, then we should show it.
1541 */
1542 if (gui.pointer_hidden)
1543 {
1544 guiBlankMouse(false);
1545 gui.pointer_hidden = FALSE;
1546 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001547
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001548 if (!vimMouseButton) { // could also check m->"buttons"
1549 guiMouseMoved(point.x, point.y);
1550 return;
1551 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001552
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001553 atomic_add(&mouseDragEventCount, 1);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001554
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001555 // Don't care much about "transit"
1556 guiSendMouseEvent(MOUSE_DRAG, point.x, point.y, 0, vimMouseModifiers);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001557}
1558
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001559 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001560VimTextAreaView::MessageReceived(BMessage *m)
1561{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001562 switch (m->what) {
1563 case 'menu':
1564 {
1565 VimMenuMsg mm;
1566 mm.guiMenu = NULL; // in case no pointer in msg
1567 m->FindPointer("VimMenu", (void **)&mm.guiMenu);
1568 write_port(gui.vdcmp, VimMsg::Menu, &mm, sizeof(mm));
1569 }
1570 break;
1571 case B_MOUSE_WHEEL_CHANGED:
1572 {
1573 VimScrollBar* scb = curwin->w_scrollbars[1].id;
1574 float small=0, big=0, dy=0;
1575 m->FindFloat("be:wheel_delta_y", &dy);
1576 scb->GetSteps(&small, &big);
1577 scb->SetValue(scb->Value()+small*dy*3);
1578 scb->ValueChanged(scb->Value());
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001579#if 0
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001580 scb = curwin->w_scrollbars[0].id;
1581 scb->GetSteps(&small, &big);
1582 scb->SetValue(scb->Value()+small*dy);
1583 scb->ValueChanged(scb->Value());
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001584#endif
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001585 }
1586 break;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001587#ifdef FEAT_MBYTE_IME
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001588 case B_INPUT_METHOD_EVENT:
1589 {
1590 int32 opcode;
1591 m->FindInt32("be:opcode", &opcode);
1592 switch(opcode)
1593 {
1594 case B_INPUT_METHOD_STARTED:
1595 if (!IMData.messenger) delete IMData.messenger;
1596 IMData.messenger = new BMessenger();
1597 m->FindMessenger("be:reply_to", IMData.messenger);
1598 break;
1599 case B_INPUT_METHOD_CHANGED:
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001600 {
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001601 BString str;
1602 bool confirmed;
1603 if (IMData.message) *(IMData.message) = *m;
1604 else IMData.message = new BMessage(*m);
1605 DrawIMString();
1606 m->FindBool("be:confirmed", &confirmed);
1607 if (confirmed)
1608 {
1609 m->FindString("be:string", &str);
1610 char_u *chars = (char_u*)str.String();
1611 struct VimKeyMsg km;
1612 km.csi_escape = true;
1613 int clen;
1614 int i = 0;
1615 while (i < str.Length())
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001616 {
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001617 clen = utf_ptr2len(chars+i);
1618 memcpy(km.chars, chars+i, clen);
1619 km.length = clen;
1620 write_port(gui.vdcmp, VimMsg::Key, &km, sizeof(km));
1621 i += clen;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001622 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001623 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001624 }
1625 break;
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001626 case B_INPUT_METHOD_LOCATION_REQUEST:
1627 {
1628 BMessage msg(B_INPUT_METHOD_EVENT);
1629 msg.AddInt32("be:opcode", B_INPUT_METHOD_LOCATION_REQUEST);
1630 msg.AddPoint("be:location_reply", IMData.location);
1631 msg.AddFloat("be:height_reply", FILL_Y(1));
1632 IMData.messenger->SendMessage(&msg);
1633 }
1634 break;
1635 case B_INPUT_METHOD_STOPPED:
1636 delete IMData.messenger;
1637 delete IMData.message;
1638 IMData.messenger = NULL;
1639 IMData.message = NULL;
1640 break;
1641 }
1642 }
1643 // TODO: sz: break here???
1644#endif
1645 default:
1646 if (m->WasDropped()) {
1647 BWindow *w = Window();
1648 w->DetachCurrentMessage();
1649 w->Minimize(false);
1650 VimApp::SendRefs(m, (modifiers() & B_SHIFT_KEY) != 0);
1651 } else {
1652 Inherited::MessageReceived(m);
1653 }
1654 break;
1655 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001656}
1657
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001658 int
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001659VimTextAreaView::mchInitFont(char_u *name)
1660{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001661 VimFont *newFont = (VimFont *)gui_mch_get_font(name, 1);
1662 if (newFont != NOFONT) {
1663 gui.norm_font = (GuiFont)newFont;
1664 gui_mch_set_font((GuiFont)newFont);
1665 if (name && STRCMP(name, "*") != 0)
1666 hl_set_font_name(name);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001667
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001668 SetDrawingMode(B_OP_COPY);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001669
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001670 /*
1671 * Try to load other fonts for bold, italic, and bold-italic.
1672 * We should also try to work out what font to use for these when they are
1673 * not specified by X resources, but we don't yet.
1674 */
1675 return OK;
1676 }
1677 return FAIL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001678}
1679
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001680 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001681VimTextAreaView::mchDrawString(int row, int col, char_u *s, int len, int flags)
1682{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001683 /*
1684 * First we must erase the area, because DrawString won't do
1685 * that for us. XXX Most of the time this is a waste of effort
1686 * since the bachground has been erased already... DRAW_TRANSP
1687 * should be set when appropriate!!!
1688 * (Rectangles include the bottom and right edge)
1689 */
1690 if (!(flags & DRAW_TRANSP)) {
1691 int cells;
1692 cells = 0;
1693 for (int i=0; i<len; i++) {
1694 int cn = utf_ptr2cells((char_u *)(s+i));
1695 if (cn<4) cells += cn;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001696 }
1697
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001698 BRect r(FILL_X(col), FILL_Y(row),
1699 FILL_X(col + cells) - PEN_WIDTH, FILL_Y(row + 1) - PEN_WIDTH);
1700 FillRect(r, B_SOLID_LOW);
1701 }
1702
1703 BFont font;
1704 this->GetFont(&font);
1705 if (!font.IsFixed())
1706 {
1707 char* p = (char*)s;
1708 int32 clen, lastpos = 0;
1709 BPoint where;
1710 int cells;
1711 while ((p - (char*)s) < len) {
1712 clen = utf_ptr2len((u_char*)p);
1713 where.Set(TEXT_X(col+lastpos), TEXT_Y(row));
1714 DrawString(p, clen, where);
1715 if (flags & DRAW_BOLD) {
1716 where.x += 1.0;
1717 SetDrawingMode(B_OP_BLEND);
1718 DrawString(p, clen, where);
1719 SetDrawingMode(B_OP_COPY);
1720 }
1721 cells = utf_ptr2cells((char_u *)p);
1722 if (cells<4) lastpos += cells;
1723 else lastpos++;
1724 p += clen;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001725 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001726 }
1727 else
1728 {
1729 BPoint where(TEXT_X(col), TEXT_Y(row));
1730 DrawString((char*)s, len, where);
1731 if (flags & DRAW_BOLD) {
1732 where.x += 1.0;
1733 SetDrawingMode(B_OP_BLEND);
1734 DrawString((char*)s, len, where);
1735 SetDrawingMode(B_OP_COPY);
1736 }
1737 }
1738
1739 if (flags & DRAW_UNDERL) {
1740 int cells;
1741 cells = 0;
1742 for (int i=0; i<len; i++) {
1743 int cn = utf_ptr2cells((char_u *)(s+i));
1744 if (cn<4) cells += cn;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001745 }
1746
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001747 BPoint start(FILL_X(col), FILL_Y(row + 1) - PEN_WIDTH);
1748 BPoint end(FILL_X(col + cells) - PEN_WIDTH, start.y);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001749
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001750 StrokeLine(start, end);
1751 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001752}
1753
1754void
1755VimTextAreaView::mchClearBlock(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001756 int row1,
1757 int col1,
1758 int row2,
1759 int col2)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001760{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001761 BRect r(FILL_X(col1), FILL_Y(row1),
1762 FILL_X(col2 + 1) - PEN_WIDTH, FILL_Y(row2 + 1) - PEN_WIDTH);
1763 gui_mch_set_bg_color(gui.back_pixel);
1764 FillRect(r, B_SOLID_LOW);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001765}
1766
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001767 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001768VimTextAreaView::mchClearAll()
1769{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001770 gui_mch_set_bg_color(gui.back_pixel);
1771 FillRect(Bounds(), B_SOLID_LOW);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001772}
1773
1774/*
1775 * mchDeleteLines() Lock()s the window by itself.
1776 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001777 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001778VimTextAreaView::mchDeleteLines(int row, int num_lines)
1779{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001780 BRect source, dest;
1781 source.left = FILL_X(gui.scroll_region_left);
1782 source.top = FILL_Y(row + num_lines);
1783 source.right = FILL_X(gui.scroll_region_right + 1) - PEN_WIDTH;
1784 source.bottom = FILL_Y(gui.scroll_region_bot + 1) - PEN_WIDTH;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001785
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001786 dest.left = FILL_X(gui.scroll_region_left);
1787 dest.top = FILL_Y(row);
1788 dest.right = FILL_X(gui.scroll_region_right + 1) - PEN_WIDTH;
1789 dest.bottom = FILL_Y(gui.scroll_region_bot - num_lines + 1) - PEN_WIDTH;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001790
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001791 if (gui.vimWindow->Lock()) {
1792 // Clear one column more for when bold has spilled over
1793 CopyBits(source, dest);
1794 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
1795 gui.scroll_region_left,
1796 gui.scroll_region_bot, gui.scroll_region_right);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001797
1798
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001799 gui.vimWindow->Unlock();
1800 /*
1801 * The Draw() callback will be called now if some of the source
1802 * bits were not in the visible region.
1803 */
1804 }
1805 // gui_x11_check_copy_area();
1806 // }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001807}
1808
1809/*
1810 * mchInsertLines() Lock()s the window by itself.
1811 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001812 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001813VimTextAreaView::mchInsertLines(int row, int num_lines)
1814{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001815 BRect source, dest;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001816
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001817 // XXX Attempt at a hack:
1818 gui.vimWindow->UpdateIfNeeded();
1819 source.left = FILL_X(gui.scroll_region_left);
1820 source.top = FILL_Y(row);
1821 source.right = FILL_X(gui.scroll_region_right + 1) - PEN_WIDTH;
1822 source.bottom = FILL_Y(gui.scroll_region_bot - num_lines + 1) - PEN_WIDTH;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001823
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001824 dest.left = FILL_X(gui.scroll_region_left);
1825 dest.top = FILL_Y(row + num_lines);
1826 dest.right = FILL_X(gui.scroll_region_right + 1) - PEN_WIDTH;
1827 dest.bottom = FILL_Y(gui.scroll_region_bot + 1) - PEN_WIDTH;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001828
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001829 if (gui.vimWindow->Lock()) {
1830 // Clear one column more for when bold has spilled over
1831 CopyBits(source, dest);
1832 gui_clear_block(row, gui.scroll_region_left,
1833 row + num_lines - 1, gui.scroll_region_right);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001834
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001835 gui.vimWindow->Unlock();
1836 /*
1837 * The Draw() callback will be called now if some of the source
1838 * bits were not in the visible region.
1839 * However, if we scroll too fast it can't keep up and the
1840 * update region gets messed up. This seems to be because copying
1841 * un-Draw()n bits does not generate Draw() calls for the copy...
1842 * I moved the hack to before the CopyBits() to reduce the
1843 * amount of additional waiting needed.
1844 */
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001845
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001846 // gui_x11_check_copy_area();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001847
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001848 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001849}
1850
1851#ifdef FEAT_MBYTE_IME
1852/*
1853 * DrawIMString draws string with IMData.message.
1854 */
1855void VimTextAreaView::DrawIMString(void)
1856{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001857 static const rgb_color r_highlight = {255, 152, 152, 255},
1858 b_highlight = {152, 203, 255, 255};
1859 BString str;
1860 const char* s;
1861 int len;
1862 BMessage* msg = IMData.message;
1863 if (!msg)
1864 return;
1865 gui_redraw_block(IMData.row, 0,
1866 IMData.row + IMData.count, W_WIDTH(curwin), GUI_MON_NOCLEAR);
1867 bool confirmed = false;
1868 msg->FindBool("be:confirmed", &confirmed);
1869 if (confirmed)
1870 return;
1871 rgb_color hcolor = HighColor(), lcolor = LowColor();
1872 msg->FindString("be:string", &str);
1873 s = str.String();
1874 len = str.Length();
1875 SetHighColor(0, 0, 0);
1876 IMData.row = gui.row;
1877 IMData.col = gui.col;
1878 int32 sel_start = 0, sel_end = 0;
1879 msg->FindInt32("be:selection", 0, &sel_start);
1880 msg->FindInt32("be:selection", 1, &sel_end);
1881 int clen, cn;
1882 BPoint pos(IMData.col, 0);
1883 BRect r;
1884 BPoint where;
1885 IMData.location = ConvertToScreen(
1886 BPoint(FILL_X(pos.x), FILL_Y(IMData.row + pos.y)));
1887 for (int i=0; i<len; i+=clen)
1888 {
1889 cn = utf_ptr2cells((char_u *)(s+i));
1890 clen = utf_ptr2len((char_u *)(s+i));
1891 if (pos.x + cn > W_WIDTH(curwin))
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001892 {
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001893 pos.y++;
1894 pos.x = 0;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001895 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001896 if (sel_start<=i && i<sel_end)
1897 {
1898 SetLowColor(r_highlight);
1899 IMData.location = ConvertToScreen(
1900 BPoint(FILL_X(pos.x), FILL_Y(IMData.row + pos.y)));
1901 }
1902 else
1903 {
1904 SetLowColor(b_highlight);
1905 }
1906 r.Set(FILL_X(pos.x), FILL_Y(IMData.row + pos.y),
1907 FILL_X(pos.x + cn) - PEN_WIDTH,
1908 FILL_Y(IMData.row + pos.y + 1) - PEN_WIDTH);
1909 FillRect(r, B_SOLID_LOW);
1910 where.Set(TEXT_X(pos.x), TEXT_Y(IMData.row + pos.y));
1911 DrawString((s+i), clen, where);
1912 pos.x += cn;
1913 }
1914 IMData.count = (int)pos.y;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001915
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001916 SetHighColor(hcolor);
1917 SetLowColor(lcolor);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001918}
1919#endif
1920// ---------------- VimScrollBar ----------------
1921
1922/*
1923 * BUG: XXX
1924 * It seems that BScrollBar determine their direction not from
1925 * "posture" but from if they are "tall" or "wide" in shape...
1926 *
1927 * Also, place them out of sight, because Vim enables them before
1928 * they are positioned.
1929 */
1930VimScrollBar::VimScrollBar(scrollbar_T *g, orientation posture):
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001931 BScrollBar(posture == B_HORIZONTAL ? BRect(-100,-100,-10,-90) :
1932 BRect(-100,-100,-90,-10),
1933 "vim scrollbar", (BView *)NULL,
1934 0.0, 10.0, posture),
1935 ignoreValue(-1),
1936 scrollEventCount(0)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001937{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001938 gsb = g;
1939 SetResizingMode(B_FOLLOW_NONE);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001940}
1941
1942VimScrollBar::~VimScrollBar()
1943{
1944}
1945
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001946 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001947VimScrollBar::ValueChanged(float newValue)
1948{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001949 if (ignoreValue >= 0.0 && newValue == ignoreValue) {
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001950 ignoreValue = -1;
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001951 return;
1952 }
1953 ignoreValue = -1;
1954 /*
1955 * We want to throttle the amount of scroll messages generated.
1956 * Normally I presume you won't get a new message before we've
1957 * handled the previous one, but because we're passing them on this
1958 * happens very quickly. So instead we keep a counter of how many
1959 * scroll events there are (or will be) in the VDCMP, and the
1960 * throttling happens at the receiving end.
1961 */
1962 atomic_add(&scrollEventCount, 1);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001963
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001964 struct VimScrollBarMsg sm;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001965
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001966 sm.sb = this;
1967 sm.value = (long) newValue;
1968 sm.stillDragging = TRUE;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001969
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001970 write_port(gui.vdcmp, VimMsg::ScrollBar, &sm, sizeof(sm));
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001971
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001972 // calls gui_drag_scrollbar(sb, newValue, TRUE);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001973}
1974
1975/*
1976 * When the mouse goes up, report that scrolling has stopped.
1977 * MouseUp() is NOT called when the mouse-up occurs outside
1978 * the window, even though the thumb does move while the mouse
1979 * is outside... This has some funny effects... XXX
1980 * So we do special processing when the window de/activates.
1981 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001982 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001983VimScrollBar::MouseUp(BPoint where)
1984{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001985 // BMessage *m = Window()->CurrentMessage();
1986 // m->PrintToStream();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001987
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001988 atomic_add(&scrollEventCount, 1);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001989
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001990 struct VimScrollBarMsg sm;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001991
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001992 sm.sb = this;
1993 sm.value = (long) Value();
1994 sm.stillDragging = FALSE;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001995
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001996 write_port(gui.vdcmp, VimMsg::ScrollBar, &sm, sizeof(sm));
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001997
Bram Moolenaarb52575f2020-04-24 22:16:13 +02001998 // calls gui_drag_scrollbar(sb, newValue, FALSE);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001999
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002000 Inherited::MouseUp(where);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002001}
2002
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002003 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002004VimScrollBar::SetValue(float newValue)
2005{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002006 if (newValue == Value())
2007 return;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002008
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002009 ignoreValue = newValue;
2010 Inherited::SetValue(newValue);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002011}
2012
2013// ---------------- VimFont ----------------
2014
2015VimFont::VimFont(): BFont()
2016{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002017 init();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002018}
2019
2020VimFont::VimFont(const VimFont *rhs): BFont(rhs)
2021{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002022 init();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002023}
2024
2025VimFont::VimFont(const BFont *rhs): BFont(rhs)
2026{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002027 init();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002028}
2029
2030VimFont::VimFont(const VimFont &rhs): BFont(rhs)
2031{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002032 init();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002033}
2034
2035VimFont::~VimFont()
2036{
2037}
2038
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002039 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002040VimFont::init()
2041{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002042 next = NULL;
2043 refcount = 1;
2044 name = NULL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002045}
2046
2047// ---------------- VimDialog ----------------
2048
2049#if defined(FEAT_GUI_DIALOG)
2050
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002051const unsigned int kVimDialogButtonMsg = 'VMDB';
2052const unsigned int kVimDialogIconStripeWidth = 30;
2053const unsigned int kVimDialogButtonsSpacingX = 9;
2054const unsigned int kVimDialogButtonsSpacingY = 4;
2055const unsigned int kVimDialogSpacingX = 6;
2056const unsigned int kVimDialogSpacingY = 10;
2057const unsigned int kVimDialogMinimalWidth = 310;
2058const unsigned int kVimDialogMinimalHeight = 75;
2059const BRect kDefaultRect =
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002060BRect(0, 0, kVimDialogMinimalWidth, kVimDialogMinimalHeight);
2061
2062VimDialog::VimDialog(int type, const char *title, const char *message,
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002063 const char *buttons, int dfltbutton, const char *textfield, int ex_cmd)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002064: BWindow(kDefaultRect, title, B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL,
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002065 B_NOT_CLOSABLE | B_NOT_RESIZABLE |
2066 B_NOT_ZOOMABLE | B_NOT_MINIMIZABLE | B_ASYNCHRONOUS_CONTROLS)
2067 , fDialogSem(-1)
2068 , fDialogValue(dfltbutton)
2069 , fMessageView(NULL)
2070 , fInputControl(NULL)
2071 , fInputValue(textfield)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002072{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002073 // master view
2074 VimDialog::View* view = new VimDialog::View(Bounds());
2075 if (view == NULL)
2076 return;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002077
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002078 if (title == NULL)
2079 SetTitle("Vim " VIM_VERSION_MEDIUM);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002080
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002081 AddChild(view);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002082
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002083 // icon
2084 view->InitIcon(type);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002085
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002086 // buttons
2087 int32 which = 1;
2088 float maxButtonWidth = 0;
2089 float maxButtonHeight = 0;
2090 float buttonsWidth = 0;
2091 float buttonsHeight = 0;
2092 BString strButtons(buttons);
2093 strButtons.RemoveAll("&");
Hirohito Higashia4a00a72025-05-08 22:58:31 +02002094 do
2095 {
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002096 int32 end = strButtons.FindFirst('\n');
2097 if (end != B_ERROR)
2098 strButtons.SetByteAt(end, '\0');
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002099
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002100 BButton *button = _CreateButton(which++, strButtons.String());
2101 view->AddChild(button);
2102 fButtonsList.AddItem(button);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002103
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002104 maxButtonWidth = max_c(maxButtonWidth, button->Bounds().Width());
2105 maxButtonHeight = max_c(maxButtonHeight, button->Bounds().Height());
2106 buttonsWidth += button->Bounds().Width();
2107 buttonsHeight += button->Bounds().Height();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002108
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002109 if (end == B_ERROR)
2110 break;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002111
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002112 strButtons.Remove(0, end + 1);
2113 } while (true);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002114
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002115 int32 buttonsCount = fButtonsList.CountItems();
2116 buttonsWidth += kVimDialogButtonsSpacingX * (buttonsCount - 1);
2117 buttonsHeight += kVimDialogButtonsSpacingY * (buttonsCount - 1);
2118 float dialogWidth = buttonsWidth + kVimDialogIconStripeWidth +
2119 kVimDialogSpacingX * 2;
2120 float dialogHeight = maxButtonHeight + kVimDialogSpacingY * 3;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002121
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002122 // Check 'v' flag in 'guioptions': vertical button placement.
2123 bool vertical = (vim_strchr(p_go, GO_VERTICAL) != NULL) ||
2124 dialogWidth >= gui.vimWindow->Bounds().Width();
2125 if (vertical) {
2126 dialogWidth -= buttonsWidth;
2127 dialogWidth += maxButtonWidth;
2128 dialogHeight -= maxButtonHeight;
2129 dialogHeight += buttonsHeight;
2130 }
2131
2132 dialogWidth = max_c(dialogWidth, kVimDialogMinimalWidth);
2133
2134 // message view
2135 BRect rect(0, 0, dialogWidth, 0);
2136 rect.left += kVimDialogIconStripeWidth + 16 + kVimDialogSpacingX;
2137 rect.top += kVimDialogSpacingY;
2138 rect.right -= kVimDialogSpacingX;
2139 rect.bottom = rect.top;
2140 fMessageView = new BTextView(rect, "_tv_", rect.OffsetByCopy(B_ORIGIN),
2141 B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW);
2142
2143 fMessageView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
2144 rgb_color textColor = ui_color(B_PANEL_TEXT_COLOR);
2145 fMessageView->SetFontAndColor(be_plain_font, B_FONT_ALL, &textColor);
2146 fMessageView->SetText(message);
2147 fMessageView->MakeEditable(false);
2148 fMessageView->MakeSelectable(false);
2149 fMessageView->SetWordWrap(true);
2150 AddChild(fMessageView);
2151
2152 float messageHeight = fMessageView->TextHeight(0, fMessageView->CountLines());
2153 fMessageView->ResizeBy(0, messageHeight);
2154 fMessageView->SetTextRect(BRect(0, 0, rect.Width(), messageHeight));
2155
2156 dialogHeight += messageHeight;
2157
2158 // input view
2159 if (fInputValue != NULL) {
2160 rect.top =
2161 rect.bottom += messageHeight + kVimDialogSpacingY;
2162 fInputControl = new BTextControl(rect, "_iv_", NULL, fInputValue, NULL,
2163 B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW | B_NAVIGABLE | B_PULSE_NEEDED);
2164 fInputControl->TextView()->SetText(fInputValue);
2165 fInputControl->TextView()->SetWordWrap(false);
2166 AddChild(fInputControl);
2167
2168 float width = 0.f, height = 0.f;
2169 fInputControl->GetPreferredSize(&width, &height);
2170 fInputControl->MakeFocus(true);
2171
2172 dialogHeight += height + kVimDialogSpacingY * 1.5;
2173 }
2174
2175 dialogHeight = max_c(dialogHeight, kVimDialogMinimalHeight);
2176
2177 ResizeTo(dialogWidth, dialogHeight);
2178 MoveTo((gui.vimWindow->Bounds().Width() - dialogWidth) / 2,
2179 (gui.vimWindow->Bounds().Height() - dialogHeight) / 2);
2180
2181 // adjust layout of buttons
2182 float buttonWidth = max_c(maxButtonWidth, rect.Width() * 0.66);
2183 BPoint origin(dialogWidth, dialogHeight);
2184 origin.x -= kVimDialogSpacingX + (vertical ? buttonWidth : buttonsWidth);
2185 origin.y -= kVimDialogSpacingY + (vertical ? buttonsHeight : maxButtonHeight);
2186
2187 for (int32 i = 0 ; i < buttonsCount; i++) {
2188 BButton *button = (BButton*)fButtonsList.ItemAt(i);
2189 button->MoveTo(origin);
2190 if (vertical) {
2191 origin.y += button->Frame().Height() + kVimDialogButtonsSpacingY;
2192 button->ResizeTo(buttonWidth, button->Frame().Height());
2193 } else
2194 origin.x += button->Frame().Width() + kVimDialogButtonsSpacingX;
2195
2196 if (dfltbutton == i + 1) {
2197 button->MakeDefault(true);
2198 button->MakeFocus(fInputControl == NULL);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002199 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002200 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002201}
2202
2203VimDialog::~VimDialog()
2204{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002205 if (fDialogSem > B_OK)
2206 delete_sem(fDialogSem);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002207}
2208
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002209 int
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002210VimDialog::Go()
2211{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002212 fDialogSem = create_sem(0, "VimDialogSem");
2213 if (fDialogSem < B_OK) {
2214 Quit();
2215 return fDialogValue;
2216 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002217
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002218 Show();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002219
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002220 while (acquire_sem(fDialogSem) == B_INTERRUPTED);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002221
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002222 int retValue = fDialogValue;
2223 if (fInputValue != NULL)
2224 vim_strncpy((char_u*)fInputValue, (char_u*)fInputControl->Text(), IOSIZE - 1);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002225
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002226 if (Lock())
2227 Quit();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002228
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002229 return retValue;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002230}
2231
2232void VimDialog::MessageReceived(BMessage *msg)
2233{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002234 int32 which = 0;
2235 if (msg->what != kVimDialogButtonMsg ||
2236 msg->FindInt32("which", &which) != B_OK)
2237 return BWindow::MessageReceived(msg);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002238
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002239 fDialogValue = which;
2240 delete_sem(fDialogSem);
2241 fDialogSem = -1;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002242}
2243
2244BButton* VimDialog::_CreateButton(int32 which, const char* label)
2245{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002246 BMessage *message = new BMessage(kVimDialogButtonMsg);
2247 message->AddInt32("which", which);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002248
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002249 BRect rect(0, 0, 0, 0);
2250 BString name;
2251 name << "_b" << which << "_";
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002252
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002253 BButton* button = new BButton(rect, name.String(), label, message,
2254 B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002255
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002256 float width = 0.f, height = 0.f;
2257 button->GetPreferredSize(&width, &height);
2258 button->ResizeTo(width, height);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002259
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002260 return button;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002261}
2262
2263VimDialog::View::View(BRect frame)
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002264 : BView(frame, "VimDialogView", B_FOLLOW_ALL_SIDES, B_WILL_DRAW),
2265 fIconBitmap(NULL)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002266{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002267 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002268}
2269
2270VimDialog::View::~View()
2271{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002272 delete fIconBitmap;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002273}
2274
2275void VimDialog::View::Draw(BRect updateRect)
2276{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002277 BRect stripeRect = Bounds();
2278 stripeRect.right = kVimDialogIconStripeWidth;
2279 SetHighColor(tint_color(ViewColor(), B_DARKEN_1_TINT));
2280 FillRect(stripeRect);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002281
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002282 if (fIconBitmap == NULL)
2283 return;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002284
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002285 SetDrawingMode(B_OP_ALPHA);
2286 SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
2287 DrawBitmapAsync(fIconBitmap, BPoint(18, 6));
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002288}
2289
2290void VimDialog::View::InitIcon(int32 type)
2291{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002292 if (type == VIM_GENERIC)
2293 return;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002294
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002295 BPath path;
2296 status_t status = find_directory(B_BEOS_SERVERS_DIRECTORY, &path);
2297 if (status != B_OK) {
2298 fprintf(stderr, "Cannot retrieve app info:%s\n", strerror(status));
2299 return;
2300 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002301
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002302 path.Append("app_server");
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002303
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002304 BFile file(path.Path(), O_RDONLY);
2305 if (file.InitCheck() != B_OK) {
2306 fprintf(stderr, "App file assignment failed:%s\n",
2307 strerror(file.InitCheck()));
2308 return;
2309 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002310
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002311 BResources resources(&file);
2312 if (resources.InitCheck() != B_OK) {
2313 fprintf(stderr, "App server resources assignment failed:%s\n",
2314 strerror(resources.InitCheck()));
2315 return;
2316 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002317
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002318 const char *name = "";
2319 switch(type) {
2320 case VIM_ERROR: name = "stop"; break;
2321 case VIM_WARNING: name = "warn"; break;
2322 case VIM_INFO: name = "info"; break;
2323 case VIM_QUESTION: name = "idea"; break;
2324 default: return;
2325 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002326
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002327 int32 iconSize = 32;
2328 fIconBitmap = new BBitmap(BRect(0, 0, iconSize - 1, iconSize - 1), 0, B_RGBA32);
2329 if (fIconBitmap == NULL || fIconBitmap->InitCheck() != B_OK) {
2330 fprintf(stderr, "Icon bitmap allocation failed:%s\n",
2331 (fIconBitmap == NULL) ? "null" : strerror(fIconBitmap->InitCheck()));
2332 return;
2333 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002334
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002335 size_t size = 0;
2336 const uint8* iconData = NULL;
2337 // try vector icon first?
2338 iconData = (const uint8*)resources.LoadResource(B_VECTOR_ICON_TYPE, name, &size);
2339 if (iconData != NULL && BIconUtils::GetVectorIcon(iconData, size, fIconBitmap) == B_OK)
2340 return;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002341
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002342 // try bitmap icon now
2343 iconData = (const uint8*)resources.LoadResource(B_LARGE_ICON_TYPE, name, &size);
2344 if (iconData == NULL) {
2345 fprintf(stderr, "Bitmap icon resource not found\n");
2346 delete fIconBitmap;
2347 fIconBitmap = NULL;
2348 return;
2349 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002350
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002351 if (fIconBitmap->ColorSpace() != B_CMAP8)
2352 BIconUtils::ConvertFromCMAP8(iconData, iconSize, iconSize, iconSize, fIconBitmap);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002353}
2354
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002355const unsigned int kVimDialogOKButtonMsg = 'FDOK';
2356const unsigned int kVimDialogCancelButtonMsg = 'FDCN';
2357const unsigned int kVimDialogSizeInputMsg = 'SICH';
2358const unsigned int kVimDialogFamilySelectMsg = 'MSFM';
2359const unsigned int kVimDialogStyleSelectMsg = 'MSST';
2360const unsigned int kVimDialogSizeSelectMsg = 'MSSZ';
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002361
2362VimSelectFontDialog::VimSelectFontDialog(font_family* family, font_style* style, float* size)
2363: BWindow(kDefaultRect, "Font Selection", B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL,
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002364 B_NOT_CLOSABLE | B_NOT_RESIZABLE |
2365 B_NOT_ZOOMABLE | B_NOT_MINIMIZABLE | B_ASYNCHRONOUS_CONTROLS)
2366 , fStatus(B_NO_INIT)
2367 , fDialogSem(-1)
2368 , fDialogValue(false)
2369 , fFamily(family)
2370 , fStyle(style)
2371 , fSize(size)
2372 , fFontSize(*size)
2373 , fPreview(0)
2374 , fFamiliesList(0)
2375 , fStylesList(0)
2376 , fSizesList(0)
2377 , fSizesInput(0)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002378{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002379 strncpy(fFontFamily, *family, B_FONT_FAMILY_LENGTH);
2380 strncpy(fFontStyle, *style, B_FONT_STYLE_LENGTH);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002381
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002382 // "client" area view
2383 BBox *clientBox = new BBox(Bounds(), B_EMPTY_STRING, B_FOLLOW_ALL_SIDES,
2384 B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE_JUMP | B_PULSE_NEEDED,
2385 B_PLAIN_BORDER);
2386 AddChild(clientBox);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002387
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002388 // client view
2389 BRect RC = clientBox->Bounds();
2390 RC.InsetBy(kVimDialogSpacingX, kVimDialogSpacingY);
2391 BRect rc(RC.LeftTop(), RC.LeftTop());
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002392
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002393 // at first create all controls
2394 fPreview = new BStringView(rc, "preview", "DejaVu Sans Mono");
2395 clientBox->AddChild(fPreview);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002396
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002397 BBox* boxDivider = new BBox(rc, B_EMPTY_STRING,
2398 B_FOLLOW_NONE, B_WILL_DRAW, B_FANCY_BORDER);
2399 clientBox->AddChild(boxDivider);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002400
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002401 BStringView *labelFamily = new BStringView(rc, "labelFamily", "Family:");
2402 clientBox->AddChild(labelFamily);
2403 labelFamily->ResizeToPreferred();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002404
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002405 BStringView *labelStyle = new BStringView(rc, "labelStyle", "Style:");
2406 clientBox->AddChild(labelStyle);
2407 labelStyle->ResizeToPreferred();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002408
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002409 BStringView *labelSize = new BStringView(rc, "labelSize", "Size:");
2410 clientBox->AddChild(labelSize);
2411 labelSize->ResizeToPreferred();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002412
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002413 fFamiliesList = new BListView(rc, "listFamily",
2414 B_SINGLE_SELECTION_LIST, B_FOLLOW_ALL_SIDES);
2415 BScrollView *scrollFamilies = new BScrollView("scrollFamily",
2416 fFamiliesList, B_FOLLOW_LEFT_RIGHT, 0, false, true);
2417 clientBox->AddChild(scrollFamilies);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002418
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002419 fStylesList= new BListView(rc, "listStyles",
2420 B_SINGLE_SELECTION_LIST, B_FOLLOW_ALL_SIDES);
2421 BScrollView *scrollStyles = new BScrollView("scrollStyle",
2422 fStylesList, B_FOLLOW_LEFT_RIGHT, 0, false, true);
2423 clientBox->AddChild(scrollStyles);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002424
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002425 fSizesInput = new BTextControl(rc, "inputSize", NULL, "???",
2426 new BMessage(kVimDialogSizeInputMsg));
2427 clientBox->AddChild(fSizesInput);
2428 fSizesInput->ResizeToPreferred();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002429
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002430 fSizesList = new BListView(rc, "listSizes",
2431 B_SINGLE_SELECTION_LIST, B_FOLLOW_ALL_SIDES);
2432 BScrollView *scrollSizes = new BScrollView("scrollSize",
2433 fSizesList, B_FOLLOW_LEFT_RIGHT, 0, false, true);
2434 clientBox->AddChild(scrollSizes);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002435
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002436 BButton *buttonOK = new BButton(rc, "buttonOK", "OK",
2437 new BMessage(kVimDialogOKButtonMsg));
2438 clientBox->AddChild(buttonOK);
2439 buttonOK->ResizeToPreferred();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002440
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002441 BButton *buttonCancel = new BButton(rc, "buttonCancel", "Cancel",
2442 new BMessage(kVimDialogCancelButtonMsg));
2443 clientBox->AddChild(buttonCancel);
2444 buttonCancel->ResizeToPreferred();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002445
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002446 // layout controls
2447 float lineHeight = labelFamily->Bounds().Height();
2448 float previewHeight = lineHeight * 3;
2449 float offsetYLabels = previewHeight + kVimDialogSpacingY;
2450 float offsetYLists = offsetYLabels + lineHeight + kVimDialogSpacingY / 2;
2451 float offsetYSizes = offsetYLists + fSizesInput->Bounds().Height() + kVimDialogSpacingY / 2;
2452 float listsHeight = lineHeight * 9;
2453 float offsetYButtons = offsetYLists + listsHeight + kVimDialogSpacingY;
2454 float maxControlsHeight = offsetYButtons + buttonOK->Bounds().Height();
2455 float familiesWidth = labelFamily->Bounds().Width() * 5;
2456 float offsetXStyles = familiesWidth + kVimDialogSpacingX;
2457 float stylesWidth = labelStyle->Bounds().Width() * 4;
2458 float offsetXSizes = offsetXStyles + stylesWidth + kVimDialogSpacingX;
2459 float sizesWidth = labelSize->Bounds().Width() * 2;
2460 float maxControlsWidth = offsetXSizes + sizesWidth;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002461
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002462 ResizeTo(maxControlsWidth + kVimDialogSpacingX * 2,
2463 maxControlsHeight + kVimDialogSpacingY * 2);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002464
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002465 BRect rcVim = gui.vimWindow->Frame();
2466 MoveTo(rcVim.left + (rcVim.Width() - Frame().Width()) / 2,
2467 rcVim.top + (rcVim.Height() - Frame().Height()) / 2);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002468
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002469 fPreview->ResizeTo(maxControlsWidth, previewHeight);
2470 fPreview->SetAlignment(B_ALIGN_CENTER);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002471
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002472 boxDivider->MoveBy(0.f, previewHeight + kVimDialogSpacingY / 2);
2473 boxDivider->ResizeTo(maxControlsWidth, 1.f);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002474
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002475 labelFamily->MoveBy(0.f, offsetYLabels);
2476 labelStyle->MoveBy(offsetXStyles, offsetYLabels);
2477 labelSize->MoveBy(offsetXSizes, offsetYLabels);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002478
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002479 // text control alignment issues
2480 float insetX = fSizesInput->TextView()->Bounds().Width() - fSizesInput->Bounds().Width();
2481 float insetY = fSizesInput->TextView()->Bounds().Width() - fSizesInput->Bounds().Width();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002482
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002483 scrollFamilies->MoveBy(0.f, offsetYLists);
2484 scrollStyles->MoveBy(offsetXStyles, offsetYLists);
2485 fSizesInput->MoveBy(offsetXSizes + insetX / 2, offsetYLists + insetY / 2);
2486 scrollSizes->MoveBy(offsetXSizes, offsetYSizes);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002487
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002488 fSizesInput->SetAlignment(B_ALIGN_CENTER, B_ALIGN_CENTER);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002489
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002490 scrollFamilies->ResizeTo(familiesWidth, listsHeight);
2491 scrollStyles->ResizeTo(stylesWidth, listsHeight);
2492 fSizesInput->ResizeTo(sizesWidth, fSizesInput->Bounds().Height());
2493 scrollSizes->ResizeTo(sizesWidth,
2494 listsHeight - (offsetYSizes - offsetYLists));
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002495
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002496 buttonOK->MoveBy(maxControlsWidth - buttonOK->Bounds().Width(), offsetYButtons);
2497 buttonCancel->MoveBy(maxControlsWidth - buttonOK->Bounds().Width()
2498 - buttonCancel->Bounds().Width() - kVimDialogSpacingX, offsetYButtons);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002499
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002500 // fill lists
2501 int selIndex = -1;
2502 int count = count_font_families();
2503 for (int i = 0; i < count; i++) {
2504 font_family family;
2505 if (get_font_family(i, &family ) == B_OK) {
2506 fFamiliesList->AddItem(new BStringItem((const char*)family));
2507 if (strncmp(family, fFontFamily, B_FONT_FAMILY_LENGTH) == 0)
2508 selIndex = i;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002509 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002510 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002511
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002512 if (selIndex >= 0) {
2513 fFamiliesList->Select(selIndex);
2514 fFamiliesList->ScrollToSelection();
2515 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002516
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002517 _UpdateFontStyles();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002518
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002519 selIndex = -1;
2520 for (int size = 8, index = 0; size <= 18; size++, index++) {
2521 BString str;
2522 str << size;
2523 fSizesList->AddItem(new BStringItem(str));
2524 if (size == fFontSize)
2525 selIndex = index;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002526
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002527 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002528
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002529 if (selIndex >= 0) {
2530 fSizesList->Select(selIndex);
2531 fSizesList->ScrollToSelection();
2532 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002533
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002534 fFamiliesList->SetSelectionMessage(new BMessage(kVimDialogFamilySelectMsg));
2535 fStylesList->SetSelectionMessage(new BMessage(kVimDialogStyleSelectMsg));
2536 fSizesList->SetSelectionMessage(new BMessage(kVimDialogSizeSelectMsg));
2537 fSizesInput->SetModificationMessage(new BMessage(kVimDialogSizeInputMsg));
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002538
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002539 _UpdateSizeInputPreview();
2540 _UpdateFontPreview();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002541
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002542 fStatus = B_OK;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002543}
2544
2545VimSelectFontDialog::~VimSelectFontDialog()
2546{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002547 _CleanList(fFamiliesList);
2548 _CleanList(fStylesList);
2549 _CleanList(fSizesList);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002550
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002551 if (fDialogSem > B_OK)
2552 delete_sem(fDialogSem);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002553}
2554
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002555 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002556VimSelectFontDialog::_CleanList(BListView* list)
2557{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002558 while (0 < list->CountItems())
2559 delete (dynamic_cast<BStringItem*>(list->RemoveItem((int32)0)));
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002560}
2561
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002562 bool
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002563VimSelectFontDialog::Go()
2564{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002565 if (fStatus != B_OK) {
2566 Quit();
2567 return NOFONT;
2568 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002569
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002570 fDialogSem = create_sem(0, "VimFontSelectDialogSem");
2571 if (fDialogSem < B_OK) {
2572 Quit();
2573 return fDialogValue;
2574 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002575
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002576 Show();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002577
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002578 while (acquire_sem(fDialogSem) == B_INTERRUPTED);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002579
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002580 bool retValue = fDialogValue;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002581
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002582 if (Lock())
2583 Quit();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002584
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002585 return retValue;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002586}
2587
2588
2589void VimSelectFontDialog::_UpdateFontStyles()
2590{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002591 _CleanList(fStylesList);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002592
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002593 int32 selIndex = -1;
2594 int32 count = count_font_styles(fFontFamily);
2595 for (int32 i = 0; i < count; i++) {
2596 font_style style;
2597 uint32 flags = 0;
2598 if (get_font_style(fFontFamily, i, &style, &flags) == B_OK) {
2599 fStylesList->AddItem(new BStringItem((const char*)style));
2600 if (strncmp(style, fFontStyle, B_FONT_STYLE_LENGTH) == 0)
2601 selIndex = i;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002602 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002603 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002604
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002605 if (selIndex >= 0) {
2606 fStylesList->Select(selIndex);
2607 fStylesList->ScrollToSelection();
2608 } else
2609 fStylesList->Select(0);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002610}
2611
2612
2613void VimSelectFontDialog::_UpdateSizeInputPreview()
2614{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002615 char buf[10] = {0};
2616 vim_snprintf(buf, sizeof(buf), (char*)"%.0f", fFontSize);
2617 fSizesInput->SetText(buf);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002618}
2619
2620
2621void VimSelectFontDialog::_UpdateFontPreview()
2622{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002623 BFont font;
2624 fPreview->GetFont(&font);
2625 font.SetSize(fFontSize);
2626 font.SetFamilyAndStyle(fFontFamily, fFontStyle);
2627 fPreview->SetFont(&font, B_FONT_FAMILY_AND_STYLE | B_FONT_SIZE);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002628
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002629 BString str;
2630 str << fFontFamily << " " << fFontStyle << ", " << (int)fFontSize << " pt.";
2631 fPreview->SetText(str);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002632}
2633
2634
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002635 bool
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002636VimSelectFontDialog::_UpdateFromListItem(BListView* list, char* text, int textSize)
2637{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002638 int32 index = list->CurrentSelection();
2639 if (index < 0)
2640 return false;
2641 BStringItem* item = (BStringItem*)list->ItemAt(index);
2642 if (item == NULL)
2643 return false;
2644 strncpy(text, item->Text(), textSize);
2645 return true;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002646}
2647
2648
2649void VimSelectFontDialog::MessageReceived(BMessage *msg)
2650{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002651 switch (msg->what) {
2652 case kVimDialogOKButtonMsg:
2653 strncpy(*fFamily, fFontFamily, B_FONT_FAMILY_LENGTH);
2654 strncpy(*fStyle, fFontStyle, B_FONT_STYLE_LENGTH);
2655 *fSize = fFontSize;
2656 fDialogValue = true;
2657 case kVimDialogCancelButtonMsg:
2658 delete_sem(fDialogSem);
2659 fDialogSem = -1;
2660 return;
2661 case B_KEY_UP:
2662 {
2663 int32 key = 0;
2664 if (msg->FindInt32("raw_char", &key) == B_OK
2665 && key == B_ESCAPE) {
2666 delete_sem(fDialogSem);
2667 fDialogSem = -1;
2668 }
2669 }
2670 break;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002671
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002672 case kVimDialogFamilySelectMsg:
2673 if (_UpdateFromListItem(fFamiliesList,
2674 fFontFamily, B_FONT_FAMILY_LENGTH)) {
2675 _UpdateFontStyles();
2676 _UpdateFontPreview();
2677 }
2678 break;
2679 case kVimDialogStyleSelectMsg:
2680 if (_UpdateFromListItem(fStylesList,
2681 fFontStyle, B_FONT_STYLE_LENGTH))
2682 _UpdateFontPreview();
2683 break;
2684 case kVimDialogSizeSelectMsg:
2685 {
2686 char buf[10] = {0};
2687 if (_UpdateFromListItem(fSizesList, buf, sizeof(buf))) {
2688 float size = atof(buf);
2689 if (size > 0.f) {
2690 fFontSize = size;
2691 _UpdateSizeInputPreview();
2692 _UpdateFontPreview();
2693 }
2694 }
2695 }
2696 break;
2697 case kVimDialogSizeInputMsg:
2698 {
2699 float size = atof(fSizesInput->Text());
2700 if (size > 0.f) {
2701 fFontSize = size;
2702 _UpdateFontPreview();
2703 }
2704 }
2705 break;
2706 default:
2707 break;
2708 }
2709 return BWindow::MessageReceived(msg);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002710}
2711
2712#endif // FEAT_GUI_DIALOG
2713
2714#ifdef FEAT_TOOLBAR
2715
2716// some forward declaration required by toolbar functions...
2717static BMessage * MenuMessage(vimmenu_T *menu);
2718
2719VimToolbar::VimToolbar(BRect frame, const char *name) :
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002720 BBox(frame, name, B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW | B_FRAME_EVENTS, B_PLAIN_BORDER)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002721{
2722}
2723
2724VimToolbar::~VimToolbar()
2725{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002726 int32 count = fButtonsList.CountItems();
2727 for (int32 i = 0; i < count; i++)
2728 delete (BPictureButton*)fButtonsList.ItemAt(i);
2729 fButtonsList.MakeEmpty();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002730
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002731 delete normalButtonsBitmap;
2732 delete grayedButtonsBitmap;
2733 normalButtonsBitmap = NULL;
2734 grayedButtonsBitmap = NULL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002735}
2736
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002737 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002738VimToolbar::AttachedToWindow()
2739{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002740 BBox::AttachedToWindow();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002741
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002742 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002743}
2744
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002745 float
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002746VimToolbar::ToolbarHeight() const
2747{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002748 float size = NULL == normalButtonsBitmap ? 18. : normalButtonsBitmap->Bounds().Height();
2749 return size + ToolbarMargin * 2 + ButtonMargin * 2 + 1;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002750}
2751
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002752 bool
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002753VimToolbar::ModifyBitmapToGrayed(BBitmap *bitmap)
2754{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002755 float height = bitmap->Bounds().Height();
2756 float width = bitmap->Bounds().Width();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002757
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002758 rgb_color *bits = (rgb_color*)bitmap->Bits();
2759 int32 pixels = bitmap->BitsLength() / 4;
2760 for (int32 i = 0; i < pixels; i++) {
2761 bits[i].red = bits[i].green =
2762 bits[i].blue = ((uint32)bits[i].red + bits[i].green + bits[i].blue) / 3;
2763 bits[i].alpha /= 4;
2764 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002765
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002766 return true;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002767}
2768
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002769 bool
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002770VimToolbar::PrepareButtonBitmaps()
2771{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002772 // first try to load potentially customized $VIRUNTIME/bitmaps/builtin-tools.png
2773 normalButtonsBitmap = LoadVimBitmap("builtin-tools.png");
2774 if (normalButtonsBitmap == NULL)
2775 // customized not found? dig application resources for "builtin-tools" one
2776 normalButtonsBitmap = BTranslationUtils::GetBitmap(B_PNG_FORMAT, "builtin-tools");
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002777
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002778 if (normalButtonsBitmap == NULL)
2779 return false;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002780
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002781 BMessage archive;
2782 normalButtonsBitmap->Archive(&archive);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002783
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002784 grayedButtonsBitmap = new BBitmap(&archive);
2785 if (grayedButtonsBitmap == NULL)
2786 return false;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002787
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002788 // modify grayed bitmap
2789 ModifyBitmapToGrayed(grayedButtonsBitmap);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002790
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002791 return true;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002792}
2793
2794BBitmap *VimToolbar::LoadVimBitmap(const char* fileName)
2795{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002796 BBitmap *bitmap = NULL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002797
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002798 int mustfree = 0;
2799 char_u* runtimePath = vim_getenv((char_u*)"VIMRUNTIME", &mustfree);
2800 if (runtimePath != NULL && fileName != NULL) {
2801 BString strPath((char*)runtimePath);
2802 strPath << "/bitmaps/" << fileName;
2803 bitmap = BTranslationUtils::GetBitmap(strPath.String());
2804 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002805
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002806 if (mustfree)
2807 vim_free(runtimePath);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002808
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002809 return bitmap;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002810}
2811
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002812 bool
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002813VimToolbar::GetPictureFromBitmap(BPicture *pictureTo, int32 index, BBitmap *bitmapFrom, bool pressed)
2814{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002815 float size = bitmapFrom->Bounds().Height() + 1.;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002816
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002817 BView view(BRect(0, 0, size, size), "", 0, 0);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002818
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002819 AddChild(&view);
2820 view.BeginPicture(pictureTo);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002821
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002822 view.SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR));
2823 view.FillRect(view.Bounds());
2824 view.SetDrawingMode(B_OP_OVER);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002825
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002826 BRect source(0, 0, size - 1, size - 1);
2827 BRect destination(source);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002828
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002829 source.OffsetBy(size * index, 0);
2830 destination.OffsetBy(ButtonMargin, ButtonMargin);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002831
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002832 view.DrawBitmap(bitmapFrom, source, destination);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002833
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002834 if (pressed) {
2835 rgb_color shineColor = ui_color(B_SHINE_COLOR);
2836 rgb_color shadowColor = ui_color(B_SHADOW_COLOR);
2837 size += ButtonMargin * 2 - 1;
2838 view.BeginLineArray(4);
2839 view.AddLine(BPoint(0, 0), BPoint(size, 0), shadowColor);
2840 view.AddLine(BPoint(size, 0), BPoint(size, size), shineColor);
2841 view.AddLine(BPoint(size, size), BPoint(0, size), shineColor);
2842 view.AddLine(BPoint(0, size), BPoint(0, 0), shadowColor);
2843 view.EndLineArray();
2844 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002845
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002846 view.EndPicture();
2847 RemoveChild(&view);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002848
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002849 return true;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002850}
2851
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002852 bool
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002853VimToolbar::AddButton(int32 index, vimmenu_T *menu)
2854{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002855 BPictureButton *button = NULL;
2856 if (!menu_is_separator(menu->name)) {
2857 float size = normalButtonsBitmap ?
2858 normalButtonsBitmap->Bounds().Height() + 1. + ButtonMargin * 2 : 18.;
2859 BRect frame(0, 0, size, size);
2860 BPicture pictureOn;
2861 BPicture pictureOff;
2862 BPicture pictureGray;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002863
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002864 if (menu->iconfile == NULL && menu->iconidx >= 0 && normalButtonsBitmap) {
2865 GetPictureFromBitmap(&pictureOn, menu->iconidx, normalButtonsBitmap, true);
2866 GetPictureFromBitmap(&pictureOff, menu->iconidx, normalButtonsBitmap, false);
2867 GetPictureFromBitmap(&pictureGray, menu->iconidx, grayedButtonsBitmap, false);
2868 } else {
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002869
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002870 char_u buffer[MAXPATHL] = {0};
2871 BBitmap *bitmap = NULL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002872
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002873 if (menu->iconfile) {
2874 gui_find_iconfile(menu->iconfile, buffer, (char*)"png");
2875 bitmap = BTranslationUtils::GetBitmap((char*)buffer);
2876 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002877
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002878 if (bitmap == NULL && gui_find_bitmap(menu->name, buffer, (char*)"png") == OK)
2879 bitmap = BTranslationUtils::GetBitmap((char*)buffer);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002880
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002881 if (bitmap == NULL)
2882 bitmap = new BBitmap(BRect(0, 0, size, size), B_RGB32);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002883
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002884 GetPictureFromBitmap(&pictureOn, 0, bitmap, true);
2885 GetPictureFromBitmap(&pictureOff, 0, bitmap, false);
2886 ModifyBitmapToGrayed(bitmap);
2887 GetPictureFromBitmap(&pictureGray, 0, bitmap, false);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002888
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002889 delete bitmap;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002890 }
2891
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002892 button = new BPictureButton(frame, (char*)menu->name,
2893 &pictureOff, &pictureOn, MenuMessage(menu));
2894
2895 button->SetDisabledOn(&pictureGray);
2896 button->SetDisabledOff(&pictureGray);
2897
2898 button->SetTarget(gui.vimTextArea);
2899
2900 AddChild(button);
2901
2902 menu->button = button;
2903 }
2904
2905 bool result = fButtonsList.AddItem(button, index);
2906 InvalidateLayout();
2907 return result;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002908}
2909
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002910 bool
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002911VimToolbar::RemoveButton(vimmenu_T *menu)
2912{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002913 if (menu->button) {
2914 if (fButtonsList.RemoveItem(menu->button)) {
2915 delete menu->button;
2916 menu->button = NULL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002917 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002918 }
2919 return true;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002920}
2921
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002922 bool
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002923VimToolbar::GrayButton(vimmenu_T *menu, int grey)
2924{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002925 if (menu->button) {
2926 int32 index = fButtonsList.IndexOf(menu->button);
2927 if (index >= 0)
2928 menu->button->SetEnabled(grey ? false : true);
2929 }
2930 return true;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002931}
2932
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002933 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002934VimToolbar::InvalidateLayout()
2935{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002936 int32 offset = ToolbarMargin;
2937 int32 count = fButtonsList.CountItems();
2938 for (int32 i = 0; i < count; i++) {
2939 BPictureButton *button = (BPictureButton *)fButtonsList.ItemAt(i);
2940 if (button) {
2941 button->MoveTo(offset, ToolbarMargin);
2942 offset += button->Bounds().Width() + ToolbarMargin;
2943 } else
2944 offset += ToolbarMargin * 3;
2945 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002946}
2947
2948#endif /*FEAT_TOOLBAR*/
2949
2950#if defined(FEAT_GUI_TABLINE)
2951
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002952 float
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002953VimTabLine::TablineHeight() const
2954{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002955// float size = NULL == normalButtonsBitmap ? 18. : normalButtonsBitmap->Bounds().Height();
2956// return size + ToolbarMargin * 2 + ButtonMargin * 2 + 1;
2957 return TabHeight(); // + ToolbarMargin;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002958}
2959
2960void
2961VimTabLine::MouseDown(BPoint point)
2962{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002963 if (!gui_mch_showing_tabline())
2964 return;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002965
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002966 BMessage *m = Window()->CurrentMessage();
2967 assert(m);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002968
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002969 int32 buttons = 0;
2970 m->FindInt32("buttons", &buttons);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002971
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002972 int32 clicks = 0;
2973 m->FindInt32("clicks", &clicks);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002974
Bram Moolenaarb52575f2020-04-24 22:16:13 +02002975 int index = 0; // 0 means here - no tab found
2976 for (int i = 0; i < CountTabs(); i++) {
2977 if (TabFrame(i).Contains(point)) {
2978 index = i + 1; // indexes are 1-based
2979 break;
2980 }
2981 }
2982
2983 int event = -1;
2984
2985 if ((buttons & B_PRIMARY_MOUSE_BUTTON) && clicks > 1)
2986 // left button double click on - create new tab
2987 event = TABLINE_MENU_NEW;
2988
2989 else if (buttons & B_TERTIARY_MOUSE_BUTTON)
2990 // middle button click - close the pointed tab
2991 // or create new one in case empty space
2992 event = index > 0 ? TABLINE_MENU_CLOSE : TABLINE_MENU_NEW;
2993
2994 else if (buttons & B_SECONDARY_MOUSE_BUTTON) {
2995 // right button click - show context menu
2996 BPopUpMenu* popUpMenu = new BPopUpMenu("tabLineContextMenu", false, false);
2997 popUpMenu->AddItem(new BMenuItem(_("Close tabi R"), new BMessage(TABLINE_MENU_CLOSE)));
2998 popUpMenu->AddItem(new BMenuItem(_("New tab T"), new BMessage(TABLINE_MENU_NEW)));
2999 popUpMenu->AddItem(new BMenuItem(_("Open tab..."), new BMessage(TABLINE_MENU_OPEN)));
3000
3001 ConvertToScreen(&point);
3002 BMenuItem* item = popUpMenu->Go(point);
3003 if (item != NULL) {
3004 event = item->Command();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003005 }
3006
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003007 delete popUpMenu;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003008
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003009 } else {
3010 // default processing
3011 BTabView::MouseDown(point);
3012 return;
3013 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003014
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003015 if (event < 0)
3016 return;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003017
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003018 VimTablineMenuMsg tmm;
3019 tmm.index = index;
3020 tmm.event = event;
3021 write_port(gui.vdcmp, VimMsg::TablineMenu, &tmm, sizeof(tmm));
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003022}
3023
3024void
3025VimTabLine::VimTab::Select(BView* owner)
3026{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003027 BTab::Select(owner);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003028
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003029 VimTabLine *tabLine = gui.vimForm->TabLine();
3030 if (tabLine != NULL) {
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003031
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003032 int32 i = 0;
3033 for (; i < tabLine->CountTabs(); i++)
3034 if (this == tabLine->TabAt(i))
3035 break;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003036
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003037// printf("%d:%d:%s\n", i, tabLine->CountTabs(), tabLine->TabAt(i)->Label());
3038 if (i < tabLine->CountTabs()) {
3039 VimTablineMsg tm;
3040 tm.index = i + 1;
3041 write_port(gui.vdcmp, VimMsg::Tabline, &tm, sizeof(tm));
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003042 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003043 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003044}
3045
3046#endif // defined(FEAT_GUI_TABLINE)
3047
3048// ---------------- ----------------
3049
3050// some global variables
3051static char appsig[] = "application/x-vnd.Haiku-Vim-8";
3052key_map *keyMap;
3053char *keyMapChars;
3054int main_exitcode = 127;
3055
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003056 status_t
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003057gui_haiku_process_event(bigtime_t timeout)
3058{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003059 struct VimMsg vm;
3060 int32 what;
3061 ssize_t size;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003062
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003063 size = read_port_etc(gui.vdcmp, &what, &vm, sizeof(vm),
3064 B_TIMEOUT, timeout);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003065
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003066 if (size >= 0) {
3067 switch (what) {
3068 case VimMsg::Key:
3069 {
3070 char_u *string = vm.u.Key.chars;
3071 int len = vm.u.Key.length;
3072 if (len == 1 && string[0] == Ctrl_chr('C')) {
3073 trash_input_buf();
3074 got_int = TRUE;
3075 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003076
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003077 if (vm.u.Key.csi_escape)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003078#ifndef FEAT_MBYTE_IME
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003079 {
3080 int i;
3081 char_u buf[2];
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003082
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003083 for (i = 0; i < len; ++i)
3084 {
3085 add_to_input_buf(string + i, 1);
3086 if (string[i] == CSI)
3087 {
3088 // Turn CSI into K_CSI.
3089 buf[0] = KS_EXTRA;
3090 buf[1] = (int)KE_CSI;
3091 add_to_input_buf(buf, 2);
3092 }
3093 }
3094 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003095#else
Bram Moolenaar80a8d382020-05-03 22:57:32 +02003096 add_to_input_buf_csi(string, len);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003097#endif
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003098 else
3099 add_to_input_buf(string, len);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003100 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003101 break;
3102 case VimMsg::Resize:
3103 gui_resize_shell(vm.u.NewSize.width, vm.u.NewSize.height);
3104 break;
3105 case VimMsg::ScrollBar:
3106 {
3107 /*
3108 * If loads of scroll messages queue up, use only the last
3109 * one. Always report when the scrollbar stops dragging.
3110 * This is not perfect yet anyway: these events are queued
3111 * yet again, this time in the keyboard input buffer.
3112 */
3113 int32 oldCount =
3114 atomic_add(&vm.u.Scroll.sb->scrollEventCount, -1);
3115 if (oldCount <= 1 || !vm.u.Scroll.stillDragging)
3116 gui_drag_scrollbar(vm.u.Scroll.sb->getGsb(),
3117 vm.u.Scroll.value, vm.u.Scroll.stillDragging);
3118 }
3119 break;
3120#if defined(FEAT_MENU)
3121 case VimMsg::Menu:
3122 gui_menu_cb(vm.u.Menu.guiMenu);
3123 break;
3124#endif
3125 case VimMsg::Mouse:
3126 {
3127 int32 oldCount;
3128 if (vm.u.Mouse.button == MOUSE_DRAG)
3129 oldCount =
3130 atomic_add(&gui.vimTextArea->mouseDragEventCount, -1);
3131 else
3132 oldCount = 0;
3133 if (oldCount <= 1)
3134 gui_send_mouse_event(vm.u.Mouse.button, vm.u.Mouse.x,
3135 vm.u.Mouse.y, vm.u.Mouse.repeated_click,
3136 vm.u.Mouse.modifiers);
3137 }
3138 break;
3139 case VimMsg::MouseMoved:
3140 {
3141 gui_mouse_moved(vm.u.MouseMoved.x, vm.u.MouseMoved.y);
3142 }
3143 break;
3144 case VimMsg::Focus:
3145 gui.in_focus = vm.u.Focus.active;
3146 // XXX Signal that scrollbar dragging has stopped?
3147 // This is needed because we don't get a MouseUp if
3148 // that happens while outside the window... :-(
3149 if (gui.dragged_sb) {
3150 gui.dragged_sb = SBAR_NONE;
3151 }
3152 // gui_update_cursor(TRUE, FALSE);
3153 break;
3154 case VimMsg::Refs:
3155 ::RefsReceived(vm.u.Refs.message, vm.u.Refs.changedir);
3156 break;
3157 case VimMsg::Tabline:
3158 send_tabline_event(vm.u.Tabline.index);
3159 break;
3160 case VimMsg::TablineMenu:
3161 send_tabline_menu_event(vm.u.TablineMenu.index, vm.u.TablineMenu.event);
3162 break;
3163 default:
3164 // unrecognised message, ignore it
3165 break;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003166 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003167 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003168
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003169 /*
3170 * If size < B_OK, it is an error code.
3171 */
3172 return size;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003173}
3174
3175/*
3176 * Here are some functions to protect access to ScreenLines[] and
3177 * LineOffset[]. These are used from the window thread to respond
3178 * to a Draw() callback. When that occurs, the window is already
3179 * locked by the system.
3180 *
3181 * Other code that needs to lock is any code that changes these
3182 * variables. Other read-only access, or access merely to the
3183 * contents of the screen buffer, need not be locked.
3184 *
3185 * If there is no window, don't call Lock() but do succeed.
3186 */
3187
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003188 int
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003189vim_lock_screen()
3190{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003191 return !gui.vimWindow || gui.vimWindow->Lock();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003192}
3193
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003194 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003195vim_unlock_screen()
3196{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003197 if (gui.vimWindow)
3198 gui.vimWindow->Unlock();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003199}
3200
3201#define RUN_BAPPLICATION_IN_NEW_THREAD 0
3202
3203#if RUN_BAPPLICATION_IN_NEW_THREAD
3204
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003205 int32
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003206run_vimapp(void *args)
3207{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003208 VimApp app(appsig);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003209
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003210 gui.vimApp = &app;
3211 app.Run(); // Run until Quit() called
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003212
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003213 return 0;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003214}
3215
3216#else
3217
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003218 int32
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003219call_main(void *args)
3220{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003221 struct MainArgs *ma = (MainArgs *)args;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003222
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003223 return main(ma->argc, ma->argv);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003224}
3225#endif
3226
3227/*
3228 * Parse the GUI related command-line arguments. Any arguments used are
3229 * deleted from argv, and *argc is decremented accordingly. This is called
3230 * when vim is started, whether or not the GUI has been started.
3231 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003232 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003233gui_mch_prepare(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003234 int *argc,
3235 char **argv)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003236{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003237 /*
3238 * We don't have any command line arguments for the BeOS GUI yet,
3239 * but this is an excellent place to create our Application object.
3240 */
3241 if (!gui.vimApp) {
3242 thread_info tinfo;
3243 get_thread_info(find_thread(NULL), &tinfo);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003244
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003245 // May need the port very early on to process RefsReceived()
3246 gui.vdcmp = create_port(B_MAX_PORT_COUNT, "vim VDCMP");
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003247
3248#if RUN_BAPPLICATION_IN_NEW_THREAD
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003249 thread_id tid = spawn_thread(run_vimapp, "vim VimApp",
3250 tinfo.priority, NULL);
3251 if (tid >= B_OK) {
3252 resume_thread(tid);
3253 } else {
3254 getout(1);
3255 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003256#else
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003257 MainArgs ma = { *argc, argv };
3258 thread_id tid = spawn_thread(call_main, "vim main()",
3259 tinfo.priority, &ma);
3260 if (tid >= B_OK) {
3261 VimApp app(appsig);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003262
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003263 gui.vimApp = &app;
3264 resume_thread(tid);
3265 /*
3266 * This is rather horrible.
3267 * call_main will call main() again...
3268 * There will be no infinite recursion since
3269 * gui.vimApp is set now.
3270 */
3271 app.Run(); // Run until Quit() called
3272 // fprintf(stderr, "app.Run() returned...\n");
3273 status_t dummy_exitcode;
3274 (void)wait_for_thread(tid, &dummy_exitcode);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003275
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003276 /*
3277 * This path should be the normal one taken to exit Vim.
3278 * The main() thread calls mch_exit() which calls
3279 * gui_mch_exit() which terminates its thread.
3280 */
3281 exit(main_exitcode);
3282 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003283#endif
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003284 }
3285 // Don't fork() when starting the GUI. Spawned threads are not
3286 // duplicated with a fork(). The result is a mess.
3287 gui.dofork = FALSE;
3288 /*
3289 * XXX Try to determine whether we were started from
3290 * the Tracker or the terminal.
3291 * It would be nice to have this work, because the Tracker
3292 * follows symlinks, so even if you double-click on gvim,
3293 * when it is a link to vim it will still pass a command name
3294 * of vim...
3295 * We try here to see if stdin comes from /dev/null. If so,
3296 * (or if there is an error, which should never happen) start the GUI.
3297 * This does the wrong thing for vim - </dev/null, and we're
3298 * too early to see the command line parsing. Tough.
3299 * On the other hand, it starts the gui for vim file & which is nice.
3300 */
3301 if (!isatty(0)) {
3302 struct stat stat_stdin, stat_dev_null;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003303
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003304 if (fstat(0, &stat_stdin) == -1 ||
3305 stat("/dev/null", &stat_dev_null) == -1 ||
3306 (stat_stdin.st_dev == stat_dev_null.st_dev &&
3307 stat_stdin.st_ino == stat_dev_null.st_ino))
3308 gui.starting = TRUE;
3309 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003310}
3311
3312/*
3313 * Check if the GUI can be started. Called before gvimrc is sourced.
3314 * Return OK or FAIL.
3315 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003316 int
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003317gui_mch_init_check(void)
3318{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003319 return OK; // TODO: GUI can always be started?
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003320}
3321
3322/*
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003323 * Initialise the GUI. Create all the windows, set up all the call-backs
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003324 * etc.
3325 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003326 int
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003327gui_mch_init()
3328{
3329 display_errors();
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003330 gui.def_norm_pixel = RGB(0x00, 0x00, 0x00); // black
3331 gui.def_back_pixel = RGB(0xFF, 0xFF, 0xFF); // white
3332 gui.norm_pixel = gui.def_norm_pixel;
3333 gui.back_pixel = gui.def_back_pixel;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003334
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003335 gui.scrollbar_width = (int) B_V_SCROLL_BAR_WIDTH;
3336 gui.scrollbar_height = (int) B_H_SCROLL_BAR_HEIGHT;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003337#ifdef FEAT_MENU
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003338 gui.menu_height = 19; // initial guess -
3339 // correct for my default settings
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003340#endif
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003341 gui.border_offset = 3; // coordinates are inside window borders
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003342
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003343 if (gui.vdcmp < B_OK)
3344 return FAIL;
3345 get_key_map(&keyMap, &keyMapChars);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003346
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003347 gui.vimWindow = new VimWindow(); // hidden and locked
3348 if (!gui.vimWindow)
3349 return FAIL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003350
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003351 gui.vimWindow->Run(); // Run() unlocks but does not show
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003352
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003353 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
3354 // file)
3355 set_normal_colors();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003356
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003357 /*
3358 * Check that none of the colors are the same as the background color
3359 */
3360 gui_check_colors();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003361
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003362 // Get the colors for the highlight groups (gui_check_colors() might have
3363 // changed them)
3364 highlight_gui_started(); // re-init colors and fonts
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003365
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003366 gui_mch_new_colors(); // window must exist for this
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003367
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003368 return OK;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003369}
3370
3371/*
3372 * Called when the foreground or background color has been changed.
3373 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003374 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003375gui_mch_new_colors()
3376{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003377 rgb_color rgb = GUI_TO_RGB(gui.back_pixel);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003378
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003379 if (gui.vimWindow->Lock()) {
3380 gui.vimForm->SetViewColor(rgb);
3381 // Does this not have too much effect for those small rectangles?
3382 gui.vimForm->Invalidate();
3383 gui.vimWindow->Unlock();
3384 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003385}
3386
3387/*
3388 * Open the GUI window which was created by a call to gui_mch_init().
3389 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003390 int
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003391gui_mch_open()
3392{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003393 if (gui_win_x != -1 && gui_win_y != -1)
3394 gui_mch_set_winpos(gui_win_x, gui_win_y);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003395
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003396 // Actually open the window
3397 if (gui.vimWindow->Lock()) {
3398 gui.vimWindow->Show();
3399 gui.vimWindow->Unlock();
3400 return OK;
3401 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003402
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003403 return FAIL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003404}
3405
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003406 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003407gui_mch_exit(int vim_exitcode)
3408{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003409 if (gui.vimWindow) {
3410 thread_id tid = gui.vimWindow->Thread();
3411 gui.vimWindow->Lock();
3412 gui.vimWindow->Quit();
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003413 // Wait until it is truly gone
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003414 int32 exitcode;
3415 wait_for_thread(tid, &exitcode);
3416 }
3417 delete_port(gui.vdcmp);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003418#if !RUN_BAPPLICATION_IN_NEW_THREAD
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003419 /*
3420 * We are in the main() thread - quit the App thread and
3421 * quit ourselves (passing on the exitcode). Use a global since the
3422 * value from exit_thread() is only used if wait_for_thread() is
3423 * called in time (race condition).
3424 */
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003425#endif
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003426 if (gui.vimApp) {
3427 VimTextAreaView::guiBlankMouse(false);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003428
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003429 main_exitcode = vim_exitcode;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003430#if RUN_BAPPLICATION_IN_NEW_THREAD
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003431 thread_id tid = gui.vimApp->Thread();
3432 int32 exitcode;
3433 gui.vimApp->Lock();
3434 gui.vimApp->Quit();
3435 gui.vimApp->Unlock();
3436 wait_for_thread(tid, &exitcode);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003437#else
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003438 gui.vimApp->Lock();
3439 gui.vimApp->Quit();
3440 gui.vimApp->Unlock();
3441 // suicide
3442 exit_thread(vim_exitcode);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003443#endif
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003444 }
3445 // If we are somehow still here, let mch_exit() handle things.
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003446}
3447
3448/*
3449 * Get the position of the top left corner of the window.
3450 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003451 int
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003452gui_mch_get_winpos(int *x, int *y)
3453{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003454 if (gui.vimWindow->Lock()) {
3455 BRect r;
3456 r = gui.vimWindow->Frame();
3457 gui.vimWindow->Unlock();
3458 *x = (int)r.left;
3459 *y = (int)r.top;
3460 return OK;
3461 }
3462 else
3463 return FAIL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003464}
3465
3466/*
3467 * Set the position of the top left corner of the window to the given
3468 * coordinates.
3469 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003470 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003471gui_mch_set_winpos(int x, int y)
3472{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003473 if (gui.vimWindow->Lock()) {
3474 gui.vimWindow->MoveTo(x, y);
3475 gui.vimWindow->Unlock();
3476 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003477}
3478
3479/*
3480 * Set the size of the window to the given width and height in pixels.
3481 */
3482void
3483gui_mch_set_shellsize(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003484 int width,
3485 int height,
3486 int min_width,
3487 int min_height,
3488 int base_width,
3489 int base_height,
3490 int direction) // TODO: utilize?
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003491{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003492 /*
3493 * We are basically given the size of the VimForm, if I understand
3494 * correctly. Since it fills the window completely, this will also
3495 * be the size of the window.
3496 */
3497 if (gui.vimWindow->Lock()) {
3498 gui.vimWindow->ResizeTo(width - PEN_WIDTH, height - PEN_WIDTH);
3499
3500 // set size limits
3501 float minWidth, maxWidth, minHeight, maxHeight;
3502
3503 gui.vimWindow->GetSizeLimits(&minWidth, &maxWidth,
3504 &minHeight, &maxHeight);
3505 gui.vimWindow->SetSizeLimits(min_width, maxWidth,
3506 min_height, maxHeight);
3507
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003508 /*
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003509 * Set the resizing alignment depending on font size.
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003510 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003511 gui.vimWindow->SetWindowAlignment(
3512 B_PIXEL_ALIGNMENT, // window_alignment mode,
3513 1, // int32 h,
3514 0, // int32 hOffset = 0,
3515 gui.char_width, // int32 width = 0,
3516 base_width, // int32 widthOffset = 0,
3517 1, // int32 v = 0,
3518 0, // int32 vOffset = 0,
3519 gui.char_height, // int32 height = 0,
3520 base_height // int32 heightOffset = 0
3521 );
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003522
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003523 gui.vimWindow->Unlock();
3524 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003525}
3526
3527void
3528gui_mch_get_screen_dimensions(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003529 int *screen_w,
3530 int *screen_h)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003531{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003532 BRect frame;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003533
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003534 {
3535 BScreen screen(gui.vimWindow);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003536
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003537 if (screen.IsValid()) {
3538 frame = screen.Frame();
3539 } else {
3540 frame.right = 640;
3541 frame.bottom = 480;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003542 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003543 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003544
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003545 // XXX approximations...
3546 *screen_w = (int) frame.right - 2 * gui.scrollbar_width - 20;
3547 *screen_h = (int) frame.bottom - gui.scrollbar_height
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003548#ifdef FEAT_MENU
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003549 - gui.menu_height
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003550#endif
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003551 - 30;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003552}
3553
3554void
3555gui_mch_set_text_area_pos(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003556 int x,
3557 int y,
3558 int w,
3559 int h)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003560{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003561 if (!gui.vimTextArea)
3562 return;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003563
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003564 if (gui.vimWindow->Lock()) {
3565 gui.vimTextArea->MoveTo(x, y);
3566 gui.vimTextArea->ResizeTo(w - PEN_WIDTH, h - PEN_WIDTH);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003567
Bram Moolenaarbeae4082020-04-23 15:41:49 +02003568#ifdef FEAT_GUI_TABLINE
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003569 if (gui.vimForm->TabLine() != NULL) {
3570 gui.vimForm->TabLine()->ResizeTo(w, gui.vimForm->TablineHeight());
3571 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003572#endif // FEAT_GUI_TABLINE
3573
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003574 gui.vimWindow->Unlock();
3575 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003576}
3577
3578
3579/*
3580 * Scrollbar stuff:
3581 */
3582
3583void
3584gui_mch_enable_scrollbar(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003585 scrollbar_T *sb,
3586 int flag)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003587{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003588 VimScrollBar *vsb = sb->id;
3589 if (gui.vimWindow->Lock()) {
3590 /*
3591 * This function is supposed to be idempotent, but Show()/Hide()
3592 * is not. Therefore we test if they are needed.
3593 */
3594 if (flag) {
3595 if (vsb->IsHidden()) {
3596 vsb->Show();
3597 }
3598 } else {
3599 if (!vsb->IsHidden()) {
3600 vsb->Hide();
3601 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003602 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003603 gui.vimWindow->Unlock();
3604 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003605}
3606
3607void
3608gui_mch_set_scrollbar_thumb(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003609 scrollbar_T *sb,
3610 int val,
3611 int size,
3612 int max)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003613{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003614 if (gui.vimWindow->Lock()) {
3615 VimScrollBar *s = sb->id;
3616 if (max == 0) {
3617 s->SetValue(0);
3618 s->SetRange(0.0, 0.0);
3619 } else {
3620 s->SetProportion((float)size / (max + 1.0));
3621 s->SetSteps(1.0, size > 5 ? size - 2 : size);
3622#ifndef SCROLL_PAST_END // really only defined in gui.c...
3623 max = max + 1 - size;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003624#endif
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003625 if (max < s->Value()) {
3626 /*
3627 * If the new maximum is lower than the current value,
3628 * setting it would cause the value to be clipped and
3629 * therefore a ValueChanged() call.
3630 * We avoid this by setting the value first, because
3631 * it presumably is <= max.
3632 */
3633 s->SetValue(val);
3634 s->SetRange(0.0, max);
3635 } else {
3636 /*
3637 * In the other case, set the range first, since the
3638 * new value might be higher than the current max.
3639 */
3640 s->SetRange(0.0, max);
3641 s->SetValue(val);
3642 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003643 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003644 gui.vimWindow->Unlock();
3645 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003646}
3647
3648void
3649gui_mch_set_scrollbar_pos(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003650 scrollbar_T *sb,
3651 int x,
3652 int y,
3653 int w,
3654 int h)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003655{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003656 if (gui.vimWindow->Lock()) {
3657 BRect winb = gui.vimWindow->Bounds();
3658 float vsbx = x, vsby = y;
3659 VimScrollBar *vsb = sb->id;
3660 vsb->ResizeTo(w - PEN_WIDTH, h - PEN_WIDTH);
3661 if (winb.right-(x+w)<w) vsbx = winb.right - (w - PEN_WIDTH);
3662 vsb->MoveTo(vsbx, vsby);
3663 gui.vimWindow->Unlock();
3664 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003665}
3666
Bram Moolenaar203ec772020-07-17 20:43:43 +02003667 int
3668gui_mch_get_scrollbar_xpadding(void)
3669{
3670 // TODO: Calculate the padding for adjust scrollbar position when the
3671 // Window is maximized.
3672 return 0;
3673}
3674
3675 int
3676gui_mch_get_scrollbar_ypadding(void)
3677{
3678 // TODO: Calculate the padding for adjust scrollbar position when the
3679 // Window is maximized.
3680 return 0;
3681}
3682
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003683void
3684gui_mch_create_scrollbar(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003685 scrollbar_T *sb,
3686 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003687{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003688 orientation posture =
3689 (orient == SBAR_HORIZ) ? B_HORIZONTAL : B_VERTICAL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003690
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003691 VimScrollBar *vsb = sb->id = new VimScrollBar(sb, posture);
3692 if (gui.vimWindow->Lock()) {
3693 vsb->SetTarget(gui.vimTextArea);
3694 vsb->Hide();
3695 gui.vimForm->AddChild(vsb);
3696 gui.vimWindow->Unlock();
3697 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003698}
3699
Bram Moolenaarbeae4082020-04-23 15:41:49 +02003700#if defined(FEAT_WINDOWS) || defined(FEAT_GUI_HAIKU) || defined(PROTO)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003701void
3702gui_mch_destroy_scrollbar(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003703 scrollbar_T *sb)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003704{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003705 if (gui.vimWindow->Lock()) {
3706 sb->id->RemoveSelf();
3707 delete sb->id;
3708 gui.vimWindow->Unlock();
3709 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003710}
3711#endif
3712
3713/*
3714 * Cursor does not flash
3715 */
3716 int
3717gui_mch_is_blink_off(void)
3718{
3719 return FALSE;
3720}
3721
3722/*
3723 * Cursor blink functions.
3724 *
3725 * This is a simple state machine:
3726 * BLINK_NONE not blinking at all
3727 * BLINK_OFF blinking, cursor is not shown
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003728 * BLINK_ON blinking, cursor is shown
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003729 */
3730
3731#define BLINK_NONE 0
3732#define BLINK_OFF 1
3733#define BLINK_ON 2
3734
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003735static int blink_state = BLINK_NONE;
3736static long_u blink_waittime = 700;
3737static long_u blink_ontime = 400;
3738static long_u blink_offtime = 250;
3739static int blink_timer = 0;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003740
3741void
3742gui_mch_set_blinking(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003743 long waittime,
3744 long on,
3745 long off)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003746{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003747 // TODO
3748 blink_waittime = waittime;
3749 blink_ontime = on;
3750 blink_offtime = off;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003751}
3752
3753/*
3754 * Stop the cursor blinking. Show the cursor if it wasn't shown.
3755 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003756 void
Bram Moolenaarbeae4082020-04-23 15:41:49 +02003757gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003758{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003759 // TODO
3760 if (blink_timer != 0)
3761 {
3762 // XtRemoveTimeOut(blink_timer);
3763 blink_timer = 0;
3764 }
3765 if (blink_state == BLINK_OFF)
3766 gui_update_cursor(TRUE, FALSE);
3767 blink_state = BLINK_NONE;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003768}
3769
3770/*
3771 * Start the cursor blinking. If it was already blinking, this restarts the
3772 * waiting time and shows the cursor.
3773 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003774 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003775gui_mch_start_blink()
3776{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003777 // TODO
3778 if (blink_timer != 0)
3779 ;// XtRemoveTimeOut(blink_timer);
3780 // Only switch blinking on if none of the times is zero
3781 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
3782 {
3783 blink_timer = 1; // XtAppAddTimeOut(app_context, blink_waittime,
3784 blink_state = BLINK_ON;
3785 gui_update_cursor(TRUE, FALSE);
3786 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003787}
3788
3789/*
3790 * Initialise vim to use the font with the given name. Return FAIL if the font
3791 * could not be loaded, OK otherwise.
3792 */
3793int
3794gui_mch_init_font(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003795 char_u *font_name,
3796 int fontset)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003797{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003798 if (gui.vimWindow->Lock())
3799 {
3800 int rc = gui.vimTextArea->mchInitFont(font_name);
3801 gui.vimWindow->Unlock();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003802
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003803 return rc;
3804 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003805
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003806 return FAIL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003807}
3808
3809
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003810 int
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003811gui_mch_adjust_charsize()
3812{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003813 return FAIL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003814}
3815
3816
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003817 int
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003818gui_mch_font_dialog(font_family* family, font_style* style, float* size)
3819{
3820#if defined(FEAT_GUI_DIALOG)
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003821 // gui.vimWindow->Unlock();
3822 VimSelectFontDialog *dialog = new VimSelectFontDialog(family, style, size);
jinyaoguoae31d7b2025-06-11 21:30:01 +02003823 bool ret = dialog->Go();
3824 delete dialog;
3825 return ret;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003826#else
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003827 return NOFONT;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003828#endif // FEAT_GUI_DIALOG
3829}
3830
3831
3832GuiFont
3833gui_mch_get_font(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003834 char_u *name,
3835 int giveErrorIfMissing)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003836{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003837 static VimFont *fontList = NULL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003838
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003839 if (!gui.in_use) // can't do this when GUI not running
3840 return NOFONT;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003841
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003842 // storage for locally modified name;
3843 const int buff_size = B_FONT_FAMILY_LENGTH + B_FONT_STYLE_LENGTH + 20;
3844 static char font_name[buff_size] = {0};
3845 font_family family = {0};
3846 font_style style = {0};
3847 float size = 0.f;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003848
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003849 if (name == 0 && be_fixed_font == 0) {
3850 if (giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003851 semsg(_(e_unknown_font_str), name);
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003852 return NOFONT;
3853 }
3854
3855 bool useSelectGUI = false;
3856 if (name != NULL)
3857 if (STRCMP(name, "*") == 0) {
3858 useSelectGUI = true;
3859 STRNCPY(font_name, hl_get_font_name(), buff_size);
3860 } else
3861 STRNCPY(font_name, name, buff_size);
3862
3863 if (font_name[0] == 0) {
3864 be_fixed_font->GetFamilyAndStyle(&family, &style);
3865 size = be_fixed_font->Size();
3866 vim_snprintf(font_name, buff_size,
3867 (char*)"%s/%s/%.0f", family, style, size);
3868 }
3869
3870 // replace underscores with spaces
3871 char* end = 0;
3872 while (end = strchr((char *)font_name, '_'))
3873 *end = ' ';
3874
3875 // store the name before strtok corrupt the buffer ;-)
3876 static char buff[buff_size] = {0};
3877 STRNCPY(buff, font_name, buff_size);
3878 STRNCPY(family, strtok(buff, "/\0"), B_FONT_FAMILY_LENGTH);
3879 char* style_s = strtok(0, "/\0");
3880 if (style_s != 0)
3881 STRNCPY(style, style_s, B_FONT_STYLE_LENGTH);
3882 size = atof((style_s != 0) ? strtok(0, "/\0") : "0");
3883
3884 if (useSelectGUI) {
3885 if (gui_mch_font_dialog(&family, &style, &size) == NOFONT)
3886 return FAIL;
3887 // compose for further processing
3888 vim_snprintf(font_name, buff_size,
3889 (char*)"%s/%s/%.0f", family, style, size);
3890 hl_set_font_name((char_u*)font_name);
3891
3892 // Set guifont to the name of the selected font.
3893 char_u* new_p_guifont = (char_u*)alloc(STRLEN(font_name) + 1);
3894 if (new_p_guifont != NULL) {
3895 STRCPY(new_p_guifont, font_name);
3896 vim_free(p_guifont);
3897 p_guifont = new_p_guifont;
3898 // Replace spaces in the font name with underscores.
3899 for ( ; *new_p_guifont; ++new_p_guifont)
3900 if (*new_p_guifont == ' ')
3901 *new_p_guifont = '_';
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003902 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003903 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003904
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003905 VimFont *flp;
3906 for (flp = fontList; flp; flp = flp->next) {
3907 if (STRCMP(font_name, flp->name) == 0) {
3908 flp->refcount++;
3909 return (GuiFont)flp;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003910 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003911 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003912
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003913 VimFont *font = new VimFont();
3914 font->name = vim_strsave((char_u*)font_name);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003915
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003916 if (count_font_styles(family) <= 0) {
3917 if (giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003918 semsg(_(e_unknown_font_str), font->name);
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003919 delete font;
3920 return NOFONT;
3921 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003922
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003923 // Remember font in the static list for later use
3924 font->next = fontList;
3925 fontList = font;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003926
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003927 font->SetFamilyAndStyle(family, style);
3928 if (size > 0.f)
3929 font->SetSize(size);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003930
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003931 font->SetSpacing(B_FIXED_SPACING);
3932 font->SetEncoding(B_UNICODE_UTF8);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003933
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003934 return (GuiFont)font;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003935}
3936
3937/*
3938 * Set the current text font.
3939 */
3940void
3941gui_mch_set_font(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003942 GuiFont font)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003943{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003944 if (gui.vimWindow->Lock()) {
3945 VimFont *vf = (VimFont *)font;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003946
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003947 gui.vimTextArea->SetFont(vf);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003948
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003949 gui.char_width = (int) vf->StringWidth("n");
3950 font_height fh;
3951 vf->GetHeight(&fh);
3952 gui.char_height = (int)(fh.ascent + 0.9999)
3953 + (int)(fh.descent + 0.9999) + (int)(fh.leading + 0.9999);
3954 gui.char_ascent = (int)(fh.ascent + 0.9999);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003955
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003956 gui.vimWindow->Unlock();
3957 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003958}
3959
3960// XXX TODO This is apparently never called...
3961void
3962gui_mch_free_font(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003963 GuiFont font)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003964{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003965 if (font == NOFONT)
3966 return;
3967 VimFont *f = (VimFont *)font;
3968 if (--f->refcount <= 0) {
3969 if (f->refcount < 0)
3970 fprintf(stderr, "VimFont: refcount < 0\n");
3971 delete f;
3972 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003973}
3974
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003975 char_u *
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003976gui_mch_get_fontname(GuiFont font, char_u *name)
3977{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003978 if (name == NULL)
3979 return NULL;
3980 return vim_strsave(name);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003981}
3982
3983/*
3984 * Adjust gui.char_height (after 'linespace' was changed).
3985 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003986 int
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003987gui_mch_adjust_charheight()
3988{
3989
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003990 // TODO: linespace support?
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003991
3992// #ifdef FEAT_XFONTSET
Bram Moolenaarb52575f2020-04-24 22:16:13 +02003993// if (gui.fontset != NOFONTSET)
3994// {
3995// gui.char_height = fontset_height((XFontSet)gui.fontset) + p_linespace;
3996// gui.char_ascent = fontset_ascent((XFontSet)gui.fontset)
3997// + p_linespace / 2;
3998// }
3999// else
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004000// #endif
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004001 {
4002 VimFont *font = (VimFont *)gui.norm_font;
4003 font_height fh = {0};
4004 font->GetHeight(&fh);
4005 gui.char_height = (int)(fh.ascent + fh.descent + 0.5) + p_linespace;
4006 gui.char_ascent = (int)(fh.ascent + 0.5) + p_linespace / 2;
4007 }
4008 return OK;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004009}
4010
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004011 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004012gui_mch_getmouse(int *x, int *y)
4013{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004014 fprintf(stderr, "gui_mch_getmouse");
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004015
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004016 /*int rootx, rooty, winx, winy;
4017 Window root, child;
4018 unsigned int mask;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004019
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004020 if (gui.wid && XQueryPointer(gui.dpy, gui.wid, &root, &child,
4021 &rootx, &rooty, &winx, &winy, &mask)) {
4022 *x = winx;
4023 *y = winy;
4024 } else*/ {
4025 *x = -1;
4026 *y = -1;
4027 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004028}
4029
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004030 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004031gui_mch_mousehide(int hide)
4032{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004033 fprintf(stderr, "gui_mch_getmouse");
4034 // TODO
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004035}
4036
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004037/*
4038 * This function has been lifted from gui_w32.c and extended a bit.
4039 *
4040 * Return the Pixel value (color) for the given color name.
4041 * Return INVALCOLOR for error.
4042 */
4043guicolor_T
4044gui_mch_get_color(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004045 char_u *name)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004046{
Drew Vogele30d1022021-10-24 20:35:07 +01004047 return gui_get_color_cmn(name);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004048}
4049
4050/*
4051 * Set the current text foreground color.
4052 */
4053void
4054gui_mch_set_fg_color(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004055 guicolor_T color)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004056{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004057 rgb_color rgb = GUI_TO_RGB(color);
4058 if (gui.vimWindow->Lock()) {
4059 gui.vimTextArea->SetHighColor(rgb);
4060 gui.vimWindow->Unlock();
4061 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004062}
4063
4064/*
4065 * Set the current text background color.
4066 */
4067void
4068gui_mch_set_bg_color(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004069 guicolor_T color)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004070{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004071 rgb_color rgb = GUI_TO_RGB(color);
4072 if (gui.vimWindow->Lock()) {
4073 gui.vimTextArea->SetLowColor(rgb);
4074 gui.vimWindow->Unlock();
4075 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004076}
4077
4078/*
4079 * Set the current text special color.
4080 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004081 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004082gui_mch_set_sp_color(guicolor_T color)
4083{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004084 // prev_sp_color = color;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004085}
4086
4087void
4088gui_mch_draw_string(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004089 int row,
4090 int col,
4091 char_u *s,
4092 int len,
4093 int flags)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004094{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004095 if (gui.vimWindow->Lock()) {
4096 gui.vimTextArea->mchDrawString(row, col, s, len, flags);
4097 gui.vimWindow->Unlock();
4098 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004099}
4100
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004101 guicolor_T
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004102gui_mch_get_rgb_color(int r, int g, int b)
4103{
4104 return gui_get_rgb_color_cmn(r, g, b);
4105}
4106
4107
4108// Return OK if the key with the termcap name "name" is supported.
4109int
4110gui_mch_haskey(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004111 char_u *name)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004112{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004113 int i;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004114
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004115 for (i = 0; special_keys[i].BeKeys != 0; i++)
4116 if (name[0] == special_keys[i].vim_code0 &&
4117 name[1] == special_keys[i].vim_code1)
4118 return OK;
4119 return FAIL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004120}
4121
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004122 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004123gui_mch_beep()
4124{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004125 ::beep();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004126}
4127
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004128 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004129gui_mch_flash(int msec)
4130{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004131 // Do a visual beep by reversing the foreground and background colors
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004132
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004133 if (gui.vimWindow->Lock()) {
4134 BRect rect = gui.vimTextArea->Bounds();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004135
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004136 gui.vimTextArea->SetDrawingMode(B_OP_INVERT);
4137 gui.vimTextArea->FillRect(rect);
4138 gui.vimTextArea->Sync();
4139 snooze(msec * 1000); // wait for a few msec
4140 gui.vimTextArea->FillRect(rect);
4141 gui.vimTextArea->SetDrawingMode(B_OP_COPY);
4142 gui.vimTextArea->Flush();
4143 gui.vimWindow->Unlock();
4144 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004145}
4146
4147/*
4148 * Invert a rectangle from row r, column c, for nr rows and nc columns.
4149 */
4150void
4151gui_mch_invert_rectangle(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004152 int r,
4153 int c,
4154 int nr,
4155 int nc)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004156{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004157 BRect rect;
4158 rect.left = FILL_X(c);
4159 rect.top = FILL_Y(r);
4160 rect.right = rect.left + nc * gui.char_width - PEN_WIDTH;
4161 rect.bottom = rect.top + nr * gui.char_height - PEN_WIDTH;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004162
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004163 if (gui.vimWindow->Lock()) {
4164 gui.vimTextArea->SetDrawingMode(B_OP_INVERT);
4165 gui.vimTextArea->FillRect(rect);
4166 gui.vimTextArea->SetDrawingMode(B_OP_COPY);
4167 gui.vimWindow->Unlock();
4168 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004169}
4170
4171/*
4172 * Iconify the GUI window.
4173 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004174 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004175gui_mch_iconify()
4176{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004177 if (gui.vimWindow->Lock()) {
4178 gui.vimWindow->Minimize(true);
4179 gui.vimWindow->Unlock();
4180 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004181}
4182
4183#if defined(FEAT_EVAL) || defined(PROTO)
4184/*
4185 * Bring the Vim window to the foreground.
4186 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004187 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004188gui_mch_set_foreground(void)
4189{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004190 // TODO
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004191}
4192#endif
4193
4194/*
4195 * Set the window title
4196 */
4197void
4198gui_mch_settitle(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004199 char_u *title,
4200 char_u *icon)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004201{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004202 if (gui.vimWindow->Lock()) {
4203 gui.vimWindow->SetTitle((char *)title);
4204 gui.vimWindow->Unlock();
4205 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004206}
4207
4208/*
4209 * Draw a cursor without focus.
4210 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004211 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004212gui_mch_draw_hollow_cursor(guicolor_T color)
4213{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004214 gui_mch_set_fg_color(color);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004215
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004216 BRect r;
4217 r.left = FILL_X(gui.col);
4218 r.top = FILL_Y(gui.row);
4219 int cells = utf_off2cells(LineOffset[gui.row] + gui.col, 100); // TODO-TODO
4220 if (cells>=4) cells = 1;
4221 r.right = r.left + cells*gui.char_width - PEN_WIDTH;
4222 r.bottom = r.top + gui.char_height - PEN_WIDTH;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004223
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004224 if (gui.vimWindow->Lock()) {
4225 gui.vimTextArea->StrokeRect(r);
4226 gui.vimWindow->Unlock();
4227 // gui_mch_flush();
4228 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004229}
4230
4231/*
4232 * Draw part of a cursor, only w pixels wide, and h pixels high.
4233 */
4234void
4235gui_mch_draw_part_cursor(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004236 int w,
4237 int h,
4238 guicolor_T color)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004239{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004240 gui_mch_set_fg_color(color);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004241
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004242 BRect r;
4243 r.left =
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004244#ifdef FEAT_RIGHTLEFT
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004245 // vertical line should be on the right of current point
4246 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004247#endif
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004248 FILL_X(gui.col);
4249 r.right = r.left + w - PEN_WIDTH;
4250 r.bottom = FILL_Y(gui.row + 1) - PEN_WIDTH;
4251 r.top = r.bottom - h + PEN_WIDTH;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004252
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004253 if (gui.vimWindow->Lock()) {
4254 gui.vimTextArea->FillRect(r);
4255 gui.vimWindow->Unlock();
4256 // gui_mch_flush();
4257 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004258}
4259
4260/*
4261 * Catch up with any queued events. This may put keyboard input into the
4262 * input buffer, call resize call-backs, trigger timers etc. If there is
4263 * nothing in the event queue (& no timers pending), then we return
4264 * immediately.
4265 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004266 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004267gui_mch_update()
4268{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004269 gui_mch_flush();
4270 while (port_count(gui.vdcmp) > 0 &&
4271 !vim_is_input_buf_full() &&
4272 gui_haiku_process_event(0) >= B_OK)
4273 /* nothing */ ;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004274}
4275
4276/*
4277 * GUI input routine called by gui_wait_for_chars(). Waits for a character
4278 * from the keyboard.
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004279 * wtime == -1 Wait forever.
4280 * wtime == 0 This should never happen.
4281 * wtime > 0 Wait wtime milliseconds for a character.
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004282 * Returns OK if a character was found to be available within the given time,
4283 * or FAIL otherwise.
4284 */
4285int
4286gui_mch_wait_for_chars(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004287 int wtime)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004288{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004289 int focus;
Bram Moolenaar80a8d382020-05-03 22:57:32 +02004290 bigtime_t until, timeout;
4291 status_t st;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004292
Bram Moolenaar80a8d382020-05-03 22:57:32 +02004293 if (wtime >= 0)
4294 {
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004295 timeout = wtime * 1000;
4296 until = system_time() + timeout;
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004297 }
Bram Moolenaar80a8d382020-05-03 22:57:32 +02004298 else
4299 timeout = B_INFINITE_TIMEOUT;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004300
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004301 focus = gui.in_focus;
4302 for (;;)
4303 {
4304 // Stop or start blinking when focus changes
4305 if (gui.in_focus != focus)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004306 {
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004307 if (gui.in_focus)
4308 gui_mch_start_blink();
4309 else
4310 gui_mch_stop_blink(TRUE);
4311 focus = gui.in_focus;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004312 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004313
4314 gui_mch_flush();
Bram Moolenaar80a8d382020-05-03 22:57:32 +02004315
4316#ifdef MESSAGE_QUEUE
4317# ifdef FEAT_TIMERS
4318 did_add_timer = FALSE;
4319# endif
4320 parse_queued_messages();
4321# ifdef FEAT_TIMERS
4322 if (did_add_timer)
4323 // Need to recompute the waiting time.
4324 break;
4325# endif
4326# ifdef FEAT_JOB_CHANNEL
4327 if (has_any_channel())
4328 {
4329 if (wtime < 0 || timeout > 20000)
4330 timeout = 20000;
4331 }
4332 else if (wtime < 0)
4333 timeout = B_INFINITE_TIMEOUT;
4334# endif
4335#endif
4336
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004337 /*
4338 * Don't use gui_mch_update() because then we will spin-lock until a
4339 * char arrives, instead we use gui_haiku_process_event() to hang until
4340 * an event arrives. No need to check for input_buf_full because we
4341 * are returning as soon as it contains a single char.
4342 */
4343 st = gui_haiku_process_event(timeout);
4344
4345 if (input_available())
4346 return OK;
4347 if (st < B_OK) // includes B_TIMED_OUT
4348 return FAIL;
4349
4350 /*
4351 * Calculate how much longer we're willing to wait for the
4352 * next event.
4353 */
Bram Moolenaar80a8d382020-05-03 22:57:32 +02004354 if (wtime >= 0)
4355 {
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004356 timeout = until - system_time();
4357 if (timeout < 0)
4358 break;
4359 }
4360 }
4361 return FAIL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004362
4363}
4364
4365/*
4366 * Output routines.
4367 */
4368
4369/*
4370 * Flush any output to the screen. This is typically called before
4371 * the app goes to sleep.
4372 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004373 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004374gui_mch_flush()
4375{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004376 // does this need to lock the window? Apparently not but be safe.
4377 if (gui.vimWindow->Lock()) {
4378 gui.vimWindow->Flush();
4379 gui.vimWindow->Unlock();
4380 }
4381 return;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004382}
4383
4384/*
4385 * Clear a rectangular region of the screen from text pos (row1, col1) to
4386 * (row2, col2) inclusive.
4387 */
4388void
4389gui_mch_clear_block(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004390 int row1,
4391 int col1,
4392 int row2,
4393 int col2)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004394{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004395 if (gui.vimWindow->Lock()) {
4396 gui.vimTextArea->mchClearBlock(row1, col1, row2, col2);
4397 gui.vimWindow->Unlock();
4398 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004399}
4400
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004401 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004402gui_mch_clear_all()
4403{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004404 if (gui.vimWindow->Lock()) {
4405 gui.vimTextArea->mchClearAll();
4406 gui.vimWindow->Unlock();
4407 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004408}
4409
4410/*
4411 * Delete the given number of lines from the given row, scrolling up any
4412 * text further down within the scroll region.
4413 */
4414void
4415gui_mch_delete_lines(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004416 int row,
4417 int num_lines)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004418{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004419 gui.vimTextArea->mchDeleteLines(row, num_lines);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004420}
4421
4422/*
4423 * Insert the given number of lines before the given row, scrolling down any
4424 * following text within the scroll region.
4425 */
4426void
4427gui_mch_insert_lines(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004428 int row,
4429 int num_lines)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004430{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004431 gui.vimTextArea->mchInsertLines(row, num_lines);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004432}
4433
4434#if defined(FEAT_MENU) || defined(PROTO)
4435/*
4436 * Menu stuff.
4437 */
4438
4439void
4440gui_mch_enable_menu(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004441 int flag)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004442{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004443 if (gui.vimWindow->Lock())
4444 {
4445 BMenuBar *menubar = gui.vimForm->MenuBar();
4446 menubar->SetEnabled(flag);
4447 gui.vimWindow->Unlock();
4448 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004449}
4450
4451void
4452gui_mch_set_menu_pos(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004453 int x,
4454 int y,
4455 int w,
4456 int h)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004457{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004458 // It will be in the right place anyway
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004459}
4460
4461/*
4462 * Add a sub menu to the menu bar.
4463 */
4464void
4465gui_mch_add_menu(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004466 vimmenu_T *menu,
4467 int idx)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004468{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004469 vimmenu_T *parent = menu->parent;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004470
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004471 // popup menu - just create it unattached
4472 if (menu_is_popup(menu->name) && parent == NULL) {
4473 BPopUpMenu* popUpMenu = new BPopUpMenu((const char*)menu->name, false, false);
4474 menu->submenu_id = popUpMenu;
4475 menu->id = NULL;
4476 return;
4477 }
4478
4479 if (!menu_is_menubar(menu->name)
4480 || (parent != NULL && parent->submenu_id == NULL))
4481 return;
4482
4483 if (gui.vimWindow->Lock())
4484 {
4485 // Major re-write of the menu code, it was failing with memory corruption when
4486 // we started loading multiple files (the Buffer menu)
4487 //
4488 // Note we don't use the preference values yet, all are inserted into the
4489 // menubar on a first come-first served basis...
4490 //
4491 // richard@whitequeen.com jul 99
4492
4493 BMenu *tmp;
4494
4495 if ( parent )
4496 tmp = parent->submenu_id;
4497 else
4498 tmp = gui.vimForm->MenuBar();
4499 // make sure we don't try and add the same menu twice. The Buffers menu tries to
4500 // do this and Be starts to crash...
4501
4502 if ( ! tmp->FindItem((const char *) menu->dname)) {
4503
4504 BMenu *bmenu = new BMenu((char *)menu->dname);
4505
4506 menu->submenu_id = bmenu;
4507
4508 // when we add a BMenu to another Menu, it creates the interconnecting BMenuItem
4509 tmp->AddItem(bmenu);
4510
Dominique Pelleb49dfd02023-04-14 21:54:25 +01004511 // Now it's safe to query the menu for the associated MenuItem...
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004512 menu->id = tmp->FindItem((const char *) menu->dname);
4513
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004514 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004515 gui.vimWindow->Unlock();
4516 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004517}
4518
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004519 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004520gui_mch_toggle_tearoffs(int enable)
4521{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004522 // no tearoff menus
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004523}
4524
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004525 static BMessage *
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004526MenuMessage(vimmenu_T *menu)
4527{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004528 BMessage *m = new BMessage('menu');
4529 m->AddPointer("VimMenu", (void *)menu);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004530
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004531 return m;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004532}
4533
4534/*
4535 * Add a menu item to a menu
4536 */
4537void
4538gui_mch_add_menu_item(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004539 vimmenu_T *menu,
4540 int idx)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004541{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004542 int mnemonic = 0;
4543 vimmenu_T *parent = menu->parent;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004544
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004545 // TODO: use menu->actext
4546 // This is difficult, since on Be, an accelerator must be a single char
4547 // and a lot of Vim ones are the standard VI commands.
4548 //
4549 // Punt for Now...
4550 // richard@whiequeen.com jul 99
4551 if (gui.vimWindow->Lock())
4552 {
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004553#ifdef FEAT_TOOLBAR
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004554 if (menu_is_toolbar(parent->name)) {
4555 VimToolbar *toolbar = gui.vimForm->ToolBar();
4556 if (toolbar != NULL) {
4557 toolbar->AddButton(idx, menu);
4558 }
4559 } else
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004560#endif
4561
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004562 if (parent->submenu_id != NULL || menu_is_popup(parent->name)) {
4563 if (menu_is_separator(menu->name)) {
4564 BSeparatorItem *item = new BSeparatorItem();
4565 parent->submenu_id->AddItem(item);
4566 menu->id = item;
4567 menu->submenu_id = NULL;
4568 }
4569 else {
4570 BMenuItem *item = new BMenuItem((char *)menu->dname,
4571 MenuMessage(menu));
4572 item->SetTarget(gui.vimTextArea);
4573 item->SetTrigger((char) menu->mnemonic);
4574 parent->submenu_id->AddItem(item);
4575 menu->id = item;
4576 menu->submenu_id = NULL;
4577 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004578 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004579 gui.vimWindow->Unlock();
4580 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004581}
4582
4583/*
4584 * Destroy the machine specific menu widget.
4585 */
4586void
4587gui_mch_destroy_menu(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004588 vimmenu_T *menu)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004589{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004590 if (gui.vimWindow->Lock())
4591 {
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004592#ifdef FEAT_TOOLBAR
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004593 if (menu->parent && menu_is_toolbar(menu->parent->name)) {
4594 VimToolbar *toolbar = gui.vimForm->ToolBar();
4595 if (toolbar != NULL) {
4596 toolbar->RemoveButton(menu);
4597 }
4598 } else
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004599#endif
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004600 {
4601 assert(menu->submenu_id == NULL || menu->submenu_id->CountItems() == 0);
4602 /*
4603 * Detach this menu from its parent, so that it is not deleted
4604 * twice once we get to delete that parent.
4605 * Deleting a BMenuItem also deletes the associated BMenu, if any
4606 * (which does not have any items anymore since they were
4607 * removed and deleted before).
4608 */
4609 BMenu *bmenu = menu->id->Menu();
4610 if (bmenu)
4611 {
4612 bmenu->RemoveItem(menu->id);
4613 /*
4614 * If we removed the last item from the menu bar,
4615 * resize it out of sight.
4616 */
4617 if (bmenu == gui.vimForm->MenuBar() && bmenu->CountItems() == 0)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004618 {
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004619 bmenu->ResizeTo(-MENUBAR_MARGIN, -MENUBAR_MARGIN);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004620 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004621 }
4622 delete menu->id;
4623 menu->id = NULL;
4624 menu->submenu_id = NULL;
4625
4626 gui.menu_height = (int) gui.vimForm->MenuHeight();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004627 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004628 gui.vimWindow->Unlock();
4629 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004630}
4631
4632/*
4633 * Make a menu either grey or not grey.
4634 */
4635void
4636gui_mch_menu_grey(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004637 vimmenu_T *menu,
4638 int grey)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004639{
4640#ifdef FEAT_TOOLBAR
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004641 if (menu->parent && menu_is_toolbar(menu->parent->name)) {
4642 if (gui.vimWindow->Lock()) {
4643 VimToolbar *toolbar = gui.vimForm->ToolBar();
4644 if (toolbar != NULL) {
4645 toolbar->GrayButton(menu, grey);
4646 }
4647 gui.vimWindow->Unlock();
4648 }
4649 } else
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004650#endif
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004651 if (menu->id != NULL)
4652 menu->id->SetEnabled(!grey);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004653}
4654
4655/*
4656 * Make menu item hidden or not hidden
4657 */
4658void
4659gui_mch_menu_hidden(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004660 vimmenu_T *menu,
4661 int hidden)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004662{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004663 if (menu->id != NULL)
4664 menu->id->SetEnabled(!hidden);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004665}
4666
4667/*
4668 * This is called after setting all the menus to grey/hidden or not.
4669 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004670 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004671gui_mch_draw_menubar()
4672{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004673 // Nothing to do in BeOS
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004674}
4675
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004676 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004677gui_mch_show_popupmenu(vimmenu_T *menu)
4678{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004679 if (!menu_is_popup(menu->name) || menu->submenu_id == NULL)
4680 return;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004681
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004682 BPopUpMenu* popupMenu = dynamic_cast<BPopUpMenu*>(menu->submenu_id);
4683 if (popupMenu == NULL)
4684 return;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004685
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004686 BPoint point;
4687 if (gui.vimWindow->Lock()) {
4688 uint32 buttons = 0;
4689 gui.vimTextArea->GetMouse(&point, &buttons);
4690 gui.vimTextArea->ConvertToScreen(&point);
4691 gui.vimWindow->Unlock();
4692 }
4693 popupMenu->Go(point, true);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004694}
4695
4696#endif // FEAT_MENU
4697
4698// Mouse stuff
4699
4700#ifdef FEAT_CLIPBOARD
4701/*
4702 * Clipboard stuff, for cutting and pasting text to other windows.
4703 */
4704char textplain[] = "text/plain";
4705char vimselectiontype[] = "application/x-vnd.Rhialto-Vim-selectiontype";
4706
4707/*
4708 * Get the current selection and put it in the clipboard register.
4709 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004710 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004711clip_mch_request_selection(Clipboard_T *cbd)
4712{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004713 if (be_clipboard->Lock())
4714 {
4715 BMessage *m = be_clipboard->Data();
4716 // m->PrintToStream();
4717
4718 char_u *string = NULL;
4719 ssize_t stringlen = -1;
4720
4721 if (m->FindData(textplain, B_MIME_TYPE,
4722 (const void **)&string, &stringlen) == B_OK
4723 || m->FindString("text", (const char **)&string) == B_OK)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004724 {
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004725 if (stringlen == -1)
4726 stringlen = STRLEN(string);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004727
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004728 int type;
4729 char *seltype;
4730 ssize_t seltypelen;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004731
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004732 /*
4733 * Try to get the special vim selection type first
4734 */
4735 if (m->FindData(vimselectiontype, B_MIME_TYPE,
4736 (const void **)&seltype, &seltypelen) == B_OK)
4737 {
4738 switch (*seltype)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004739 {
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004740 default:
4741 case 'L': type = MLINE; break;
4742 case 'C': type = MCHAR; break;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004743#ifdef FEAT_VISUAL
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004744 case 'B': type = MBLOCK; break;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004745#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004746 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004747 }
4748 else
4749 {
4750 // Otherwise use heuristic as documented
4751 type = memchr(string, stringlen, '\n') ? MLINE : MCHAR;
4752 }
4753 clip_yank_selection(type, string, (long)stringlen, cbd);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004754 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004755 be_clipboard->Unlock();
4756 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004757}
4758/*
4759 * Make vim the owner of the current selection.
4760 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004761 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004762clip_mch_lose_selection(Clipboard_T *cbd)
4763{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004764 // Nothing needs to be done here
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004765}
4766
4767/*
4768 * Make vim the owner of the current selection. Return OK upon success.
4769 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004770 int
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004771clip_mch_own_selection(Clipboard_T *cbd)
4772{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004773 /*
4774 * Never actually own the clipboard. If another application sets the
4775 * clipboard, we don't want to think that we still own it.
4776 */
4777 return FAIL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004778}
4779
4780/*
4781 * Send the current selection to the clipboard.
4782 */
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004783 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004784clip_mch_set_selection(Clipboard_T *cbd)
4785{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004786 if (be_clipboard->Lock())
4787 {
4788 be_clipboard->Clear();
4789 BMessage *m = be_clipboard->Data();
4790 assert(m);
4791
4792 // If the '*' register isn't already filled in, fill it in now
4793 cbd->owned = TRUE;
4794 clip_get_selection(cbd);
4795 cbd->owned = FALSE;
4796
4797 char_u *str = NULL;
4798 long_u count;
4799 int type;
4800
4801 type = clip_convert_selection(&str, &count, cbd);
4802
4803 if (type < 0)
4804 return;
4805
4806 m->AddData(textplain, B_MIME_TYPE, (void *)str, count);
4807
4808 // Add type of selection
4809 char vtype;
4810 switch (type)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004811 {
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004812 default:
4813 case MLINE: vtype = 'L'; break;
4814 case MCHAR: vtype = 'C'; break;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004815#ifdef FEAT_VISUAL
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004816 case MBLOCK: vtype = 'B'; break;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004817#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004818 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004819 m->AddData(vimselectiontype, B_MIME_TYPE, (void *)&vtype, 1);
4820
4821 vim_free(str);
4822
4823 be_clipboard->Commit();
4824 be_clipboard->Unlock();
4825 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004826}
4827
4828#endif // FEAT_CLIPBOARD
4829
4830#ifdef FEAT_BROWSE
4831/*
4832 * Pop open a file browser and return the file selected, in allocated memory,
4833 * or NULL if Cancel is hit.
4834 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
4835 * title - Title message for the file browser dialog.
4836 * dflt - Default name of file.
4837 * ext - Default extension to be added to files without extensions.
4838 * initdir - directory in which to open the browser (NULL = current dir)
4839 * filter - Filter for matched files to choose from.
4840 * Has a format like this:
4841 * "C Files (*.c)\0*.c\0"
4842 * "All Files\0*.*\0\0"
4843 * If these two strings were concatenated, then a choice of two file
4844 * filters will be selectable to the user. Then only matching files will
4845 * be shown in the browser. If NULL, the default allows all files.
4846 *
4847 * *NOTE* - the filter string must be terminated with TWO nulls.
4848 */
4849char_u *
4850gui_mch_browse(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004851 int saving,
4852 char_u *title,
4853 char_u *dflt,
4854 char_u *ext,
4855 char_u *initdir,
4856 char_u *filter)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004857{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004858 gui.vimApp->fFilePanel = new BFilePanel((saving == TRUE) ? B_SAVE_PANEL : B_OPEN_PANEL,
4859 NULL, NULL, 0, false,
4860 new BMessage((saving == TRUE) ? 'save' : 'open'), NULL, true);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004861
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004862 gui.vimApp->fBrowsedPath.Unset();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004863
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004864 gui.vimApp->fFilePanel->Window()->SetTitle((char*)title);
4865 gui.vimApp->fFilePanel->SetPanelDirectory((const char*)initdir);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004866
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004867 gui.vimApp->fFilePanel->Show();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004868
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004869 gui.vimApp->fFilePanelSem = create_sem(0, "FilePanelSem");
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004870
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004871 while (acquire_sem(gui.vimApp->fFilePanelSem) == B_INTERRUPTED);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004872
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004873 char_u *fileName = NULL;
4874 status_t result = gui.vimApp->fBrowsedPath.InitCheck();
4875 if (result == B_OK) {
4876 fileName = vim_strsave((char_u*)gui.vimApp->fBrowsedPath.Path());
4877 } else
4878 if (result != B_NO_INIT) {
4879 fprintf(stderr, "gui_mch_browse: BPath error: %#08x (%s)\n",
4880 result, strerror(result));
4881 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004882
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004883 delete gui.vimApp->fFilePanel;
4884 gui.vimApp->fFilePanel = NULL;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004885
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004886 return fileName;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004887}
4888#endif // FEAT_BROWSE
4889
4890
4891#if defined(FEAT_GUI_DIALOG)
4892
4893/*
4894 * Create a dialog dynamically from the parameter strings.
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004895 * type = type of dialog (question, alert, etc.)
4896 * title = dialog title. may be NULL for default title.
4897 * message = text to display. Dialog sizes to accommodate it.
4898 * buttons = '\n' separated list of button captions, default first.
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004899 * dfltbutton = number of default button.
4900 *
4901 * This routine returns 1 if the first button is pressed,
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004902 * 2 for the second, etc.
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004903 *
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004904 * 0 indicates Esc was pressed.
4905 * -1 for unexpected error
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004906 *
4907 * If stubbing out this fn, return 1.
4908 */
4909
4910int
4911gui_mch_dialog(
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004912 int type,
4913 char_u *title,
4914 char_u *message,
4915 char_u *buttons,
4916 int dfltbutton,
4917 char_u *textfield,
4918 int ex_cmd)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004919{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004920 VimDialog *dialog = new VimDialog(type, (char*)title, (char*)message,
4921 (char*)buttons, dfltbutton, (char*)textfield, ex_cmd);
jinyaoguoae31d7b2025-06-11 21:30:01 +02004922 bool ret = dialog->Go();
4923 delete dialog;
4924 return ret;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004925}
4926
4927#endif // FEAT_GUI_DIALOG
4928
4929
4930/*
4931 * Return the RGB value of a pixel as long.
4932 */
4933 guicolor_T
4934gui_mch_get_rgb(guicolor_T pixel)
4935{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004936 rgb_color rgb = GUI_TO_RGB(pixel);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004937
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004938 return ((rgb.red & 0xff) << 16) + ((rgb.green & 0xff) << 8)
4939 + (rgb.blue & 0xff);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004940}
4941
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004942 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004943gui_mch_setmouse(int x, int y)
4944{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004945 TRACE();
4946 // TODO
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004947}
4948
4949#ifdef FEAT_MBYTE_IME
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004950 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004951im_set_position(int row, int col)
4952{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004953 if (gui.vimWindow->Lock())
4954 {
4955 gui.vimTextArea->DrawIMString();
4956 gui.vimWindow->Unlock();
4957 }
4958 return;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004959}
4960#endif
4961
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004962 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004963gui_mch_show_toolbar(int showit)
4964{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004965 VimToolbar *toolbar = gui.vimForm->ToolBar();
4966 gui.toolbar_height = (toolbar && showit) ? toolbar->ToolbarHeight() : 0.;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004967}
4968
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004969 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004970gui_mch_set_toolbar_pos(int x, int y, int w, int h)
4971{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004972 VimToolbar *toolbar = gui.vimForm->ToolBar();
4973 if (toolbar != NULL) {
4974 if (gui.vimWindow->Lock()) {
4975 toolbar->MoveTo(x, y);
4976 toolbar->ResizeTo(w - 1, h - 1);
4977 gui.vimWindow->Unlock();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004978 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004979 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004980}
4981
4982#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
4983
4984/*
4985 * Show or hide the tabline.
4986 */
4987 void
4988gui_mch_show_tabline(int showit)
4989{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004990 VimTabLine *tabLine = gui.vimForm->TabLine();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004991
4992 if (tabLine == NULL)
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004993 return;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004994
4995 if (!showit != !gui.vimForm->IsShowingTabLine()) {
Bram Moolenaarb52575f2020-04-24 22:16:13 +02004996 gui.vimForm->SetShowingTabLine(showit != 0);
4997 gui.tabline_height = gui.vimForm->TablineHeight();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004998 }
4999}
5000
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005001 void
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005002gui_mch_set_tabline_pos(int x, int y, int w, int h)
5003{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005004 VimTabLine *tabLine = gui.vimForm->TabLine();
5005 if (tabLine != NULL) {
5006 if (gui.vimWindow->Lock()) {
5007 tabLine->MoveTo(x, y);
5008 tabLine->ResizeTo(w - 1, h - 1);
5009 gui.vimWindow->Unlock();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005010 }
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005011 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005012}
5013
5014/*
5015 * Return TRUE when tabline is displayed.
5016 */
5017 int
5018gui_mch_showing_tabline()
5019{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005020 VimTabLine *tabLine = gui.vimForm->TabLine();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005021 return tabLine != NULL && gui.vimForm->IsShowingTabLine();
5022}
5023
5024/*
5025 * Update the labels of the tabline.
5026 */
5027 void
5028gui_mch_update_tabline()
5029{
5030 tabpage_T *tp;
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005031 int nr = 0;
5032 int curtabidx = 0;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005033
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005034 VimTabLine *tabLine = gui.vimForm->TabLine();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005035
5036 if (tabLine == NULL)
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005037 return;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005038
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005039 gui.vimWindow->Lock();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005040
5041 // Add a label for each tab page. They all contain the same text area.
5042 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr) {
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005043 if (tp == curtab)
5044 curtabidx = nr;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005045
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005046 BTab* tab = tabLine->TabAt(nr);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005047
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005048 if (tab == NULL) {
5049 tab = new VimTabLine::VimTab();
5050 tabLine->AddTab(NULL, tab);
5051 }
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005052
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005053 get_tabline_label(tp, FALSE);
5054 tab->SetLabel((const char*)NameBuff);
5055 tabLine->Invalidate();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005056 }
5057
5058 // Remove any old labels.
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005059 while (nr < tabLine->CountTabs())
5060 tabLine->RemoveTab(nr);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005061
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005062 if (tabLine->Selection() != curtabidx)
5063 tabLine->Select(curtabidx);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005064
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005065 gui.vimWindow->Unlock();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005066}
5067
5068/*
5069 * Set the current tab to "nr". First tab is 1.
5070 */
5071 void
5072gui_mch_set_curtab(int nr)
5073{
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005074 VimTabLine *tabLine = gui.vimForm->TabLine();
5075 if (tabLine == NULL)
5076 return;
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005077
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005078 gui.vimWindow->Lock();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005079
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005080 if (tabLine->Selection() != nr -1)
5081 tabLine->Select(nr -1);
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005082
Bram Moolenaarb52575f2020-04-24 22:16:13 +02005083 gui.vimWindow->Unlock();
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005084}
5085
5086#endif // FEAT_GUI_TABLINE