今天折腾了下Ubuntu,但是安装了Sublime Text3 之后不能写中文,最后谷歌到记录一下
view sourceprint?
001 1.保存下面代码为sublime-imfix.c
002
003 /*
004 sublime-imfix.c
005 Use LD_PRELOAD to interpose some function to fix sublime input method support for linux.
006 By Cjacker Huang <jianzhong.huang at i-soft.com.cn>
007
008 gcc -shared -o libsublime-imfix.so sublime_imfix.c `pkg-config --libs --cflags gtk+-2.0` -fPIC
009 LD_PRELOAD=./libsublime-imfix.so sublime_text
010 */
011 #include <gtk/gtk.h>
012 #include <gdk/gdkx.h>
013 typedef GdkSegment GdkRegionBox;
014
015 struct _GdkRegion
016 {
017 long size;
018 long numRects;
019 GdkRegionBox *rects;
020 GdkRegionBox extents;
021 };
022
023 GtkIMContext *local_context;
024
025 void
026 gdk_region_get_clipbox (const GdkRegion *region,
027 GdkRectangle *rectangle)
028 {
029 g_return_if_fail (region != NULL);
030 g_return_if_fail (rectangle != NULL);
031
032 rectangle->x = region->extents.x1;
033 rectangle->y = region->extents.y1;
034 rectangle->width = region->extents.x2 - region->extents.x1;
035 rectangle->height = region->extents.y2 - region->extents.y1;
036 GdkRectangle rect;
037 rect.x = rectangle->x;
038 rect.y = rectangle->y;
039 rect.width = 0;
040 rect.height = rectangle->height;
041 //The caret width is 2;
042 //Maybe sometimes we will make a mistake, but for most of the time, it should be the caret.
043 if(rectangle->width == 2 && GTK_IS_IM_CONTEXT(local_context)) {
044 gtk_im_context_set_cursor_location(local_context, rectangle);
045 }
046 }
047
048 //this is needed, for example, if you input something in file dialog and return back the edit area
049 //context will lost, so here we set it again.
050
051 static GdkFilterReturn event_filter (GdkXEvent *xevent, GdkEvent *event, gpointer im_context)
052 {
053 XEvent *xev = (XEvent *)xevent;
054 if(xev->type == KeyRelease && GTK_IS_IM_CONTEXT(im_context)) {
055 GdkWindow * win = g_object_get_data(G_OBJECT(im_context),"window");
056 if(GDK_IS_WINDOW(win))
057 gtk_im_context_set_client_window(im_context, win);
058 }
059 return GDK_FILTER_CONTINUE;
060 }
061
062 void gtk_im_context_set_client_window (GtkIMContext *context,
063 GdkWindow *window)
064 {
065 GtkIMContextClass *klass;
066 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
067 klass = GTK_IM_CONTEXT_GET_CLASS (context);
068 if (klass->set_client_window)
069 klass->set_client_window (context, window);
070
071 if(!GDK_IS_WINDOW (window))
072 return;
073 g_object_set_data(G_OBJECT(context),"window",window);
074 int width = gdk_window_get_width(window);
075 int height = gdk_window_get_height(window);
076 if(width != 0 && height !=0) {
077 gtk_im_context_focus_in(context);
078 local_context = context;
079 }
080 gdk_window_add_filter (window, event_filter, context);
081 }
082
083 2.安装C/C++的编译环境和gtk libgtk2.0-dev
084
085 sudo apt-get install build-essential
086 sudo apt-get install libgtk2.0-dev
087
088 3.编译下文件 生成libsublime-imfix.so
089
090 gcc -shared -o libsublime-imfix.so sublime_imfix.c `pkg-config --libs --cflags gtk+-2.0` -fPIC
091
092 4.打开终端进入applications修改sublime-text-2.desktop
093
094 cd /usr/share/applications
095
096 sudo subl sublime_text.desktop
097
098 打开sublime-text-2.desktop后,将
099
100 Exec=/opt/sublime_text/sublime_text %F
101 修改为
102
103 Exec=bash -c *LD_PRELOAD=/usr/lib/libsublime-imfix.so /opt/sublime_text/sublime_text %F*
104 还有将
105
106 Exec=/opt/sublime_text/sublime_text --new-window
107 修改为
108
109 Exec=bash -c *LD_PRELOAD=/usr/lib/libsublime-imfix.so /opt/sublime_text/sublime_text --new-window*
110 好了,接下来你在dash中点击打开sublime text2吧,开始你的代码之旅吧。
view sourceprint?
001 1.保存下面代码为sublime-imfix.c
002
003 /*
004 sublime-imfix.c
005 Use LD_PRELOAD to interpose some function to fix sublime input method support for linux.
006 By Cjacker Huang <jianzhong.huang at i-soft.com.cn>
007
008 gcc -shared -o libsublime-imfix.so sublime_imfix.c `pkg-config --libs --cflags gtk+-2.0` -fPIC
009 LD_PRELOAD=./libsublime-imfix.so sublime_text
010 */
011 #include <gtk/gtk.h>
012 #include <gdk/gdkx.h>
013 typedef GdkSegment GdkRegionBox;
014
015 struct _GdkRegion
016 {
017 long size;
018 long numRects;
019 GdkRegionBox *rects;
020 GdkRegionBox extents;
021 };
022
023 GtkIMContext *local_context;
024
025 void
026 gdk_region_get_clipbox (const GdkRegion *region,
027 GdkRectangle *rectangle)
028 {
029 g_return_if_fail (region != NULL);
030 g_return_if_fail (rectangle != NULL);
031
032 rectangle->x = region->extents.x1;
033 rectangle->y = region->extents.y1;
034 rectangle->width = region->extents.x2 - region->extents.x1;
035 rectangle->height = region->extents.y2 - region->extents.y1;
036 GdkRectangle rect;
037 rect.x = rectangle->x;
038 rect.y = rectangle->y;
039 rect.width = 0;
040 rect.height = rectangle->height;
041 //The caret width is 2;
042 //Maybe sometimes we will make a mistake, but for most of the time, it should be the caret.
043 if(rectangle->width == 2 && GTK_IS_IM_CONTEXT(local_context)) {
044 gtk_im_context_set_cursor_location(local_context, rectangle);
045 }
046 }
047
048 //this is needed, for example, if you input something in file dialog and return back the edit area
049 //context will lost, so here we set it again.
050
051 static GdkFilterReturn event_filter (GdkXEvent *xevent, GdkEvent *event, gpointer im_context)
052 {
053 XEvent *xev = (XEvent *)xevent;
054 if(xev->type == KeyRelease && GTK_IS_IM_CONTEXT(im_context)) {
055 GdkWindow * win = g_object_get_data(G_OBJECT(im_context),"window");
056 if(GDK_IS_WINDOW(win))
057 gtk_im_context_set_client_window(im_context, win);
058 }
059 return GDK_FILTER_CONTINUE;
060 }
061
062 void gtk_im_context_set_client_window (GtkIMContext *context,
063 GdkWindow *window)
064 {
065 GtkIMContextClass *klass;
066 g_return_if_fail (GTK_IS_IM_CONTEXT (context));
067 klass = GTK_IM_CONTEXT_GET_CLASS (context);
068 if (klass->set_client_window)
069 klass->set_client_window (context, window);
070
071 if(!GDK_IS_WINDOW (window))
072 return;
073 g_object_set_data(G_OBJECT(context),"window",window);
074 int width = gdk_window_get_width(window);
075 int height = gdk_window_get_height(window);
076 if(width != 0 && height !=0) {
077 gtk_im_context_focus_in(context);
078 local_context = context;
079 }
080 gdk_window_add_filter (window, event_filter, context);
081 }
082
083 2.安装C/C++的编译环境和gtk libgtk2.0-dev
084
085 sudo apt-get install build-essential
086 sudo apt-get install libgtk2.0-dev
087
088 3.编译下文件 生成libsublime-imfix.so
089
090 gcc -shared -o libsublime-imfix.so sublime_imfix.c `pkg-config --libs --cflags gtk+-2.0` -fPIC
091
092 4.打开终端进入applications修改sublime-text-2.desktop
093
094 cd /usr/share/applications
095
096 sudo subl sublime_text.desktop
097
098 打开sublime-text-2.desktop后,将
099
100 Exec=/opt/sublime_text/sublime_text %F
101 修改为
102
103 Exec=bash -c *LD_PRELOAD=/usr/lib/libsublime-imfix.so /opt/sublime_text/sublime_text %F*
104 还有将
105
106 Exec=/opt/sublime_text/sublime_text --new-window
107 修改为
108
109 Exec=bash -c *LD_PRELOAD=/usr/lib/libsublime-imfix.so /opt/sublime_text/sublime_text --new-window*
110 好了,接下来你在dash中点击打开sublime text2吧,开始你的代码之旅吧。