Skip to content

Commit ef46178

Browse files
committed
Implement getcellpixels() for MacVim
Don't use `gui.char_width` / `char_height` unlike the other GVim implementations. Those are used for deriving screen pixel sizes and MacVim has been hard-coding them to 1 for simplicity since the actual GUI functionality is handled out of the Vim process anyway. Changing those values would require some refactoring. Instead, just use a new variable to store them. Related: vim/vim#16004
1 parent 6eea0b8 commit ef46178

File tree

9 files changed

+59
-1
lines changed

9 files changed

+59
-1
lines changed

src/MacVim/MMBackend.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
#endif
5858
}
5959

60+
@property (nonatomic, readonly) NSSize cellSize;
61+
6062
+ (MMBackend *)sharedInstance;
6163

6264
- (void)setBackgroundColor:(int)color;

src/MacVim/MMBackend.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,6 +2253,8 @@ - (void)handleInputEvent:(int)msgid data:(NSData *)data
22532253
[self handleScrollbarEvent:data];
22542254
} else if (SetFontMsgID == msgid) {
22552255
[self handleSetFont:data];
2256+
} else if (UpdateCellSizeMsgID == msgid) {
2257+
[self handleCellSize:data];
22562258
} else if (VimShouldCloseMsgID == msgid) {
22572259
gui_shell_closed();
22582260
} else if (DropFilesMsgID == msgid) {
@@ -2705,6 +2707,18 @@ - (void)handleSetFont:(NSData *)data
27052707
CONVERT_FROM_UTF8_FREE(s);
27062708
}
27072709

2710+
- (void)handleCellSize:(NSData *)data
2711+
{
2712+
if (!data) return;
2713+
2714+
const void *bytes = [data bytes];
2715+
2716+
// Don't use gui.char_width/height because for simplicity we set those to
2717+
// 1. We store the cell size separately (it's only used for
2718+
// getcellpixels()).
2719+
memcpy(&_cellSize, bytes, sizeof(NSSize));
2720+
}
2721+
27082722
- (void)handleDropFiles:(NSData *)data
27092723
{
27102724
// TODO: Get rid of this method; instead use Vim script directly. At the

src/MacVim/MMVimController.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,10 @@ - (void)handleMessage:(int)msgid data:(NSData *)data
10121012
}
10131013

10141014
[windowController setFont:font];
1015+
1016+
NSSize cellsize = windowController.vimView.textView.cellSize;
1017+
[self sendMessage:UpdateCellSizeMsgID
1018+
data:[NSData dataWithBytes:&cellsize length:sizeof(cellsize)]];
10151019
[name release];
10161020
}
10171021
break;

src/MacVim/MacVim.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ extern const char * const MMVimMsgIDStrings[];
300300
MSG(ScrollbarEventMsgID) \
301301
MSG(SetFontMsgID) \
302302
MSG(SetWideFontMsgID) \
303+
MSG(UpdateCellSizeMsgID) \
303304
MSG(VimShouldCloseMsgID) \
304305
MSG(SetDefaultColorsMsgID) \
305306
MSG(SetTablineColorsMsgID) \

src/MacVim/gui_macvim.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@
247247
// correspondence (assuming all characters have the same dimensions).
248248
gui.scrollbar_width = gui.scrollbar_height = 0;
249249

250+
// For simplicity we just set char width/height to 1 as the GUI is
251+
// decoupled from Vim anyway so Vim doesn't need to know the accurate
252+
// pixel sizes.
250253
gui.char_height = 1;
251254
gui.char_width = 1;
252255
gui.char_ascent = 0;
@@ -1664,6 +1667,18 @@
16641667
return OK;
16651668
}
16661669

1670+
int
1671+
gui_mch_get_cell_width(void)
1672+
{
1673+
return [MMBackend sharedInstance].cellSize.width;
1674+
}
1675+
1676+
int
1677+
gui_mch_get_cell_height(void)
1678+
{
1679+
return [MMBackend sharedInstance].cellSize.height;
1680+
}
1681+
16671682

16681683
void
16691684
gui_mch_beep(void)

src/evalfunc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5407,8 +5407,13 @@ f_getcellpixels(typval_T *argvars UNUSED, typval_T *rettv)
54075407
if (gui.in_use)
54085408
{
54095409
// success pixel size and no gui.
5410+
#ifdef FEAT_GUI_MACVIM
5411+
list_append_number(rettv->vval.v_list, (varnumber_T)gui_mch_get_cell_width());
5412+
list_append_number(rettv->vval.v_list, (varnumber_T)gui_mch_get_cell_height());
5413+
#else
54105414
list_append_number(rettv->vval.v_list, (varnumber_T)gui.char_width);
54115415
list_append_number(rettv->vval.v_list, (varnumber_T)gui.char_height);
5416+
#endif
54125417
}
54135418
else
54145419
#endif

src/os_unix.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4442,7 +4442,17 @@ mch_report_winsize(int fd, int rows, int cols)
44424442

44434443
// calcurate and set tty pixel size
44444444
struct cellsize cs;
4445-
mch_calc_cell_size(&cs);
4445+
#if defined(FEAT_GUI) && defined(FEAT_GUI_MACVIM)
4446+
if (gui.in_use)
4447+
{
4448+
cs.cs_xpixel = gui_mch_get_cell_width();
4449+
cs.cs_ypixel = gui_mch_get_cell_height();
4450+
}
4451+
else
4452+
#endif
4453+
{
4454+
mch_calc_cell_size(&cs);
4455+
}
44464456

44474457
if (cs.cs_xpixel == -1)
44484458
{

src/proto/gui_macvim.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ void gui_mch_set_font(GuiFont font);
4343
void gui_mch_expand_font(optexpand_T *args, void *param, int (*add_match)(char_u *val));
4444
int gui_mch_adjust_charheight(void);
4545
int gui_mch_adjust_charwidth(void);
46+
int gui_mch_get_cell_width(void);
47+
int gui_mch_get_cell_height(void);
4648
void gui_mch_beep(void);
4749
char_u *gui_mch_browse(int saving, char_u *title, char_u *dflt, char_u *ext, char_u *initdir, char_u *filter);
4850
char_u *gui_mch_browsedir(char_u *title, char_u *initdir);

src/terminal.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4831,8 +4831,13 @@ parse_csi(
48314831
#ifdef FEAT_GUI
48324832
if (gui.in_use)
48334833
{
4834+
#ifdef FEAT_GUI_MACVIM
4835+
x += wp->w_wincol * gui_mch_get_cell_width();
4836+
y += W_WINROW(wp) * gui_mch_get_cell_height();
4837+
#else
48344838
x += wp->w_wincol * gui.char_width;
48354839
y += W_WINROW(wp) * gui.char_height;
4840+
#endif
48364841
}
48374842
else
48384843
#endif

0 commit comments

Comments
 (0)