Tizen 2.1 base
[platform/core/uifw/ise-engine-sunpinyin.git] / wrapper / xim / ic_skin.c
1 /*
2  * Copyright (c) 2010 Mike Qin <mikeandmore@gmail.com>
3  *
4  * The contents of this file are subject to the terms of either the GNU Lesser
5  * General Public License Version 2.1 only ("LGPL") or the Common Development and
6  * Distribution License ("CDDL")(collectively, the "License"). You may not use this
7  * file except in compliance with the License. You can obtain a copy of the CDDL at
8  * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
9  * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
10  * specific language governing permissions and limitations under the License. When
11  * distributing the software, include this License Header Notice in each file and
12  * include the full text of the License in the License file as well as the
13  * following notice:
14  *
15  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
16  * (CDDL)
17  * For Covered Software in this distribution, this License shall be governed by the
18  * laws of the State of California (excluding conflict-of-law provisions).
19  * Any litigation relating to this License shall be subject to the jurisdiction of
20  * the Federal Courts of the Northern District of California and the state courts
21  * of the State of California, with venue lying in Santa Clara County, California.
22  *
23  * Contributor(s):
24  *
25  * If you wish your version of this file to be governed by only the CDDL or only
26  * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
27  * include this software in this distribution under the [CDDL or LGPL Version 2.1]
28  * license." If you don't indicate a single choice of license, a recipient has the
29  * option to distribute your version of this file under either the CDDL or the LGPL
30  * Version 2.1, or to extend the choice of license to its licensees as provided
31  * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
32  * Version 2 license, then the option applies only if the new code is made subject
33  * to such option by the copyright holder.
34  */
35
36 #include <gtk/gtk.h>
37 #include "ic.h"
38 #include "ui.h"
39 #include "skin.h"
40 #include "common.h"
41 #include "settings.h"
42
43 static skin_info_t* info;
44 static char* skin_name;
45 static skin_window_t* icbar_wind;
46 static skin_button_t* icbar_status_btn;
47 static skin_button_t* icbar_full_btn;
48 static skin_button_t* icbar_punc_btn;
49
50 static void
51 toggle_english(GtkWidget* wid, GdkEventButton* evt, void* userdata)
52 {
53     icmgr_toggle_english();
54     icmgr_refresh();
55 }
56
57 static void
58 toggle_full(GtkWidget* wid, GdkEventButton* evt, void* userdata)
59 {
60     icmgr_toggle_full();
61     icmgr_refresh();
62 }
63
64 static void
65 toggle_punc(GtkWidget* wid, GdkEventButton* evt, void* userdata)
66 {
67     icmgr_toggle_punc();
68     icmgr_refresh();
69 }
70
71 static void
72 icbar_on_release(GtkWidget* wid, GdkEventMotion* evt, void* userdata)
73 {
74     position_t pos;
75     gtk_window_get_position(GTK_WINDOW(wid), &(pos.x), &(pos.y));
76     settings_set(ICBAR_POS, &pos);
77 }
78
79 static gboolean
80 icmgr_skin_init(const char* name)
81 {
82     info = ui_skin_new(name);
83     if (!info) return FALSE;
84     skin_name = strdup(name);
85     icbar_wind = skin_window_new(GTK_WINDOW(ui_create_window()),
86                                  info->icbar_background,
87                                  0, 0, 0, 0, 1);
88     skin_window_set_drag_to_move(icbar_wind, TRUE);
89
90     skin_window_set_release_cb(icbar_wind, G_CALLBACK(icbar_on_release), NULL);
91
92     /* creating the buttons */
93     icbar_status_btn = skin_button_new(info->eng_btn.normal1,
94                                        info->eng_btn.highlight1,
95                                        info->eng_btn.pressdown1);
96     icbar_full_btn = skin_button_new(info->full_btn.normal1,
97                                      info->full_btn.highlight1,
98                                      info->full_btn.pressdown1);
99     icbar_punc_btn = skin_button_new(info->punc_btn.normal1,
100                                      info->punc_btn.highlight1,
101                                      info->punc_btn.pressdown1);
102
103     /* setting up events */
104     skin_button_set_release_cb(icbar_status_btn,
105                                G_CALLBACK(toggle_english), NULL);
106     skin_button_set_release_cb(icbar_full_btn,
107                                G_CALLBACK(toggle_full), NULL);
108     skin_button_set_release_cb(icbar_punc_btn,
109                                G_CALLBACK(toggle_punc), NULL);
110
111     /* adding the buttons to the icbar */
112     skin_window_add_button(icbar_wind, icbar_status_btn, info->eng_btn.x,
113                            info->eng_btn.y);
114     skin_window_add_button(icbar_wind, icbar_full_btn, info->full_btn.x,
115                            info->full_btn.y);
116     skin_window_add_button(icbar_wind, icbar_punc_btn, info->punc_btn.x,
117                            info->punc_btn.y);
118     return TRUE;
119 }
120
121 static void
122 toggle_mode(gboolean mode, skin_button_t* btn, skin_button_info_t* info)
123 {
124     if (mode) {
125         skin_button_set_image(btn, info->normal1, info->highlight1,
126                               info->pressdown1);
127     } else {
128         skin_button_set_image(btn, info->normal2, info->highlight2,
129                               info->pressdown2);
130     }
131 }
132
133 static void
134 icmgr_skin_refresh(void)
135 {
136     if (settings_get_int(HIDE_ICBAR)) {
137         gtk_widget_hide(icbar_wind->widget);
138         return;
139     }
140     IC* ic = icmgr_get_current();
141     if (ic == NULL || !ic->is_enabled) {
142         gtk_widget_hide(GTK_WIDGET(icbar_wind->widget));
143         return;
144     }
145     toggle_mode(ic->is_english, icbar_status_btn, &(info->eng_btn));
146     toggle_mode(ic->is_full, icbar_full_btn, &(info->full_btn));
147     toggle_mode(ic->is_chn_punc, icbar_punc_btn, &(info->punc_btn));
148
149     position_t pos;
150     int width, height;
151     gtk_window_get_size(GTK_WINDOW(icbar_wind->widget), &width, &height);
152     settings_get(ICBAR_POS, &pos);
153     adjust_position(&(pos.x), &(pos.y), width, height);
154     /* write back the adjustion */
155     settings_set(ICBAR_POS, &pos);
156
157     gtk_window_move(GTK_WINDOW(icbar_wind->widget), pos.x, pos.y);
158     gtk_widget_show(icbar_wind->widget);
159 }
160
161 static void
162 icmgr_skin_dispose(void)
163 {
164     ui_skin_destroy(info);
165     free(skin_name);
166     skin_button_destroy(icbar_status_btn);
167     skin_button_destroy(icbar_full_btn);
168     skin_button_destroy(icbar_punc_btn);
169     skin_window_destroy(icbar_wind);
170 }
171
172 static const char*
173 icmgr_skin_get_name(void)
174 {
175     return skin_name;
176 }
177
178 IC_UI icmgr_skin = {
179     .get_name = icmgr_skin_get_name,
180     .init = icmgr_skin_init,
181     .refresh = icmgr_skin_refresh,
182     .dispose = icmgr_skin_dispose,
183 };
184