File: tray-indicator.cpp

package info (click to toggle)
gcin 2.9.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 22,776 kB
  • sloc: cpp: 33,947; ansic: 9,313; makefile: 653; sh: 557
file content (302 lines) | stat: -rw-r--r-- 8,267 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "gcin.h"
#include "pho.h"
#include "gtab.h"
#include "win-sym.h"
#include "eggtrayicon.h"
#include <string.h>
#include <signal.h>
#include "gst.h"
#include "pho-kbm-name.h"
#include <libayatana-appindicator/app-indicator.h>
#include <dlfcn.h>
#include "mitem.h"

void cb_sim2trad(GtkCheckMenuItem *checkmenuitem, gpointer dat);
void cb_trad2sim(GtkCheckMenuItem *checkmenuitem, gpointer dat);
void cb_tog_phospeak(GtkCheckMenuItem *checkmenuitem, gpointer dat);
void restart_gcin(GtkCheckMenuItem *checkmenuitem, gpointer dat);
void cb_tog_gcb(GtkCheckMenuItem *checkmenuitem, gpointer dat);
void cb_trad2sim(GtkCheckMenuItem *checkmenuitem, gpointer dat);
void cb_trad_sim_toggle_(GtkCheckMenuItem *checkmenuitem, gpointer dat);
void cb_stat_toggle_(GtkCheckMenuItem *checkmenuitem, gpointer dat);
void cb_about_window(GtkCheckMenuItem *checkmenuitem, gpointer dat);
void exec_gcin_setup_(GtkCheckMenuItem *checkmenuitem, gpointer dat);
void kbm_toggle_(GtkCheckMenuItem *checkmenuitem, gpointer dat);
void cb_set_output_buffer_bak_to_clipboard(GtkCheckMenuItem *checkmenuitem, gpointer dat);
void cb_half_full_char(GtkCheckMenuItem *checkmenuitem, gpointer dat);

void show_inmd_menu();
static void cb_inmd_menu(GtkCheckMenuItem *checkmenuitem, gpointer dat)
{
  show_inmd_menu();
}

gboolean tsin_pho_mode();
extern int tsin_half_full, gb_output;
extern int win32_tray_disabled;

#define ICON_FULL 0

#if ICON_FULL
void (*f_app_indicator_set_icon_full)(AppIndicator *self, const gchar *icon_name, const gchar *icon_desc);
#else
void (*f_app_indicator_set_icon)(AppIndicator *self, const gchar *icon_name);
#endif

static AppIndicator *indicator_main, *indicator_state;

void get_icon_path(char *iconame, char fname[]);


void show_inmd_menu();

void close_all_clients();
void do_exit();

void kbm_open_close(gboolean b_show);


gint inmd_switch_popup_handler (GtkWidget *widget, GdkEvent *event);
extern gboolean win_kbm_inited;


extern int win_kbm_on;

void load_setttings(), load_tab_pho_file();;
void update_win_kbm();
void update_win_kbm_inited(), toggle_half_full_char();
extern gboolean win_kbm_inited, stat_enabled;
void cb_fast_phonetic_kbd_switch(GtkCheckMenuItem *checkmenuitem, gpointer dat);


static GtkWidget *tray_menu=NULL, *tray_menu_state=NULL;


static MITEM mitems_main[] = {
  {"關於gcin/常見問題", 
#if GTK_CHECK_VERSION(3,10,0)
	NULL
#else	  
	  GTK_STOCK_ABOUT
#endif	  
	  , cb_about_window},
  {"設定/工具", 
#if GTK_CHECK_VERSION(3,10,0)
	NULL
#else	  
	  GTK_STOCK_PREFERENCES
#endif	  
	  , exec_gcin_setup_},
  {"gcb(剪貼區暫存)", NULL, cb_tog_gcb, &gcb_enabled},
  {"重新執行gcin", 
#if GTK_CHECK_VERSION(3,10,0)
	NULL
#else	  
	  GTK_STOCK_QUIT
#endif	  
	  , restart_gcin},
  {"念出發音", NULL, cb_tog_phospeak, &phonetic_speak},
  {"小鍵盤", NULL, kbm_toggle_, &win_kbm_on},
  {"選擇輸入法", 
#if GTK_CHECK_VERSION(3,10,0)
	NULL
#else	  
	  GTK_STOCK_INDEX
#endif	  
	  , cb_inmd_menu, NULL},
  {NULL}
};

static MITEM mitems_state[] = {
  {"切到舊的注音鍵盤", NULL, cb_fast_phonetic_kbd_switch},
  {"正→簡體", NULL, cb_trad2sim},
  {"簡→正體", NULL, cb_sim2trad},
  {"简体输出", NULL, cb_trad_sim_toggle_, &gb_output},
  {"打字速度", NULL, cb_stat_toggle_, &stat_enabled},
  {"全半形切換", NULL, cb_half_full_char, NULL},
  {"送字到剪貼區", NULL, cb_set_output_buffer_bak_to_clipboard},
  {NULL}
};



static void strip_png(char *s, char *t)
{
	strcpy(t, s);
	char *p = strchr(t, '.');
	*p=0;
}

static void create_indicator_menu(char *icon, char *icon_state)
{
  int i;
//  dbg("create_indicator_menu %s %s %s\n", GCIN_ICON_DIR, icon, icon_state);

  tray_menu = create_tray_menu(mitems_main);
  tray_menu_state = create_tray_menu(mitems_state);

  static void *handle;
  char *ld_error;

  if (!handle) {
    if (!(handle = dlopen("libayatana-appindicator3.so.1", RTLD_LAZY))) {
      if ((ld_error = dlerror()) != NULL)
        box_warn(ld_error);
      return;
    }
  }

  AppIndicator  *(*f_app_indicator_new_with_path) (const gchar *id, const gchar *icon_name, AppIndicatorCategory category, const gchar *icon_theme_path) = dlsym(handle, "app_indicator_new_with_path");
  void (*f_app_indicator_set_status) (AppIndicator *self, AppIndicatorStatus  status) = dlsym(handle, "app_indicator_set_status");
  #if ICON_FULL
  f_app_indicator_set_icon_full  = dlsym(handle, "app_indicator_set_icon_full");
  #else
  f_app_indicator_set_icon  = dlsym(handle, "app_indicator_set_icon");
  #endif
  void (*f_app_indicator_set_menu) (AppIndicator *self, GtkMenu *menu) = dlsym(handle, "app_indicator_set_menu");

  indicator_main = f_app_indicator_new_with_path ("gcin-main", icon, APP_INDICATOR_CATEGORY_APPLICATION_STATUS, GCIN_ICON_DIR);
  f_app_indicator_set_status (indicator_main, APP_INDICATOR_STATUS_ACTIVE);
  f_app_indicator_set_menu (indicator_main, GTK_MENU (tray_menu));

  indicator_state = f_app_indicator_new_with_path ("gcin-state", icon_state, APP_INDICATOR_CATEGORY_APPLICATION_STATUS, GCIN_ICON_DIR);
  f_app_indicator_set_status (indicator_state, APP_INDICATOR_STATUS_ACTIVE);
  f_app_indicator_set_menu (indicator_state, GTK_MENU (tray_menu_state));
}


void update_item_active_unix();

void inmd_popup_tray();

void toggle_half_full_char();



#define GCIN_TRAY_PNG "gcin_tray.png"

void disp_win_screen_status(char *in_method, char *half_status);

void set_indicator_icon(AppIndicator *ind, char *icon)
{
//	dbg("set_indicator_icon %s\n", icon);
#if ICON_FULL
	f_app_indicator_set_icon_full(ind, icon, "zz");
#else
  f_app_indicator_set_icon(ind, icon);
#endif
}

extern gboolean capslock_on;

void load_tray_icon_indicator()
{
  dbg("load_tray_icon_indicator\n");
  char *tip;
  tip="";

  char *iconame;
  if (!current_CS || current_CS->im_state == GCIN_STATE_DISABLED||current_CS->im_state == GCIN_STATE_ENG_FULL) {
    iconame=GCIN_TRAY_PNG;
  } else {
    iconame=inmd[current_CS->in_method].icon;
  }

//  dbg("tsin_pho_mode() %d\n", tsin_pho_mode());

  gboolean is_tsin = current_method_type()==method_type_TSIN;
    
   
  char tt[32];
  if (current_CS && current_CS->im_state == GCIN_STATE_CHINESE && !tsin_pho_mode()) {
    if ((current_method_type()==method_type_TSIN || current_method_type()==method_type_MODULE)) {
      strcpy(tt, "en-");
      strcat(tt, iconame);
      if (capslock_on && is_tsin)
        strcpy(tt, "en-tsin-A.png");
    } else {
      if (current_method_type()==method_type_GTAB) {
        strcpy(tt, capslock_on?"en-gtab-A.png":"en-gtab.png");
      }
    }

    iconame = tt;
  }

  dbg("iconame %s\n", iconame);
  
  char fname[128];
  fname[0]=0;
  if (iconame)
    get_icon_path(iconame, fname);


  char *icon_st=NULL;
  char fname_state[128];

//  dbg("%d %d\n",current_CS->im_state,current_CS->b_half_full_char);

  if (current_CS && (current_CS->im_state == GCIN_STATE_ENG_FULL ||
      current_CS->im_state != GCIN_STATE_DISABLED && current_CS->b_half_full_char ||
      current_method_type()==method_type_TSIN && tss.tsin_half_full)) {
      if (gb_output) {
        icon_st="full-simp.png";
        tip = _L("全形/簡體輸出");
      }
      else {
        icon_st="full-trad.png";
        tip = _L("全形/正體輸出");
      }
  } else {
    if (gb_output) {
      icon_st="half-simp.png";
      tip= _L("半形/簡體輸出");
    } else {
      icon_st="half-trad.png";
      tip = _L("半形/正體輸出");
    }
  }

  get_icon_path(icon_st, fname_state);
//  dbg("wwwwwwww %s\n", fname_state);

  if (gcin_status_win)
    disp_win_screen_status(fname, fname_state);


  if (!gcin_win32_icon || !gcin_status_tray)
    return;

  char icon_[32];
  char icon_state_[32];
  strip_png(iconame, icon_);
//  dbg("ggg %s %s\n", iconame, icon_);
  strip_png(icon_st, icon_state_);


  if (!indicator_main) {
	create_indicator_menu(icon_, icon_state_);
  }

//  dbg("final %s %s\n", icon_, icon_state_);

  set_indicator_icon(indicator_main, icon_);
  set_indicator_icon(indicator_state, icon_state_);

  return;
}

void init_tray_indicator()
{
  load_tray_icon_indicator();
}

void destroy_tray_indicator()
{
#if 1
  if (!indicator_main)
    return;
  g_object_unref(indicator_main);  indicator_main = NULL;
  g_object_unref(indicator_state);  indicator_state = NULL;
#endif  
}