Back to index

im-sdk  12.3.91
neima_aux.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 #include "aux_module.h"
00004 #include <gdk/gdkx.h>
00005 #include <gtk/gtk.h>
00006 
00007 AuxResult neima_aux_Start(aux_ext_data_t *aux_ext_data);
00008 AuxResult neima_aux_Draw(aux_ext_data_t *aux_ext_data);
00009 AuxResult neima_aux_Done(aux_ext_data_t *aux_ext_data);
00010 
00011 AuxModuleMethodsRec aux_module_methods = {
00012        neima_aux_Start,
00013        neima_aux_Draw,
00014        neima_aux_Done
00015 };
00016 
00017 AuxModuleRec neima_aux_module = {
00018        NULL,
00019        NULL,
00020        "com.sun.iiim.cle.aux.neima",
00021        &aux_module_methods,
00022 };
00023 
00024 static GtkWidget *_toolbar = NULL;
00025 static GtkWidget *_label = NULL;
00026 
00027 static gboolean      _toolbar_draging      = FALSE;
00028 static gint   _toolbar_drag_x       = 0;
00029 static gint   _toolbar_drag_y       = 0;
00030 
00031 static gint   _toolbar_pos_x            = 0;
00032 static gint   _toolbar_pos_y            = 0;
00033 
00034 static gboolean      _toolbar_show_status  = FALSE;
00035 
00036 static int
00037 ui_screen_width (void)
00038 {
00039     return gdk_screen_width ();
00040 }
00041 
00042 static int
00043 ui_screen_height (void)
00044 {
00045     return gdk_screen_height ();
00046 }
00047 
00048 static void ui_toolbar_set_position (gint pos_x, gint pos_y)
00049 {
00050     GtkRequisition ws;
00051 
00052     gtk_widget_size_request (_toolbar, &ws);
00053                                                                                                          
00054     if (pos_x <= 0) {
00055         pos_x = ui_screen_width () - 400;
00056     } else if (pos_x + ws.width > ui_screen_width ()) {
00057         pos_x = ui_screen_width () - ws.width;
00058     }
00059 
00060     if (pos_y <= 0) {
00061         pos_y = ui_screen_height () - 76;
00062     } else if (pos_y + ws.height > ui_screen_height ()) {
00063         pos_y = ui_screen_height () - ws.height;
00064     }
00065                                                                                                                   
00066     if (_toolbar_pos_x != pos_x || _toolbar_pos_y != pos_y) {
00067         gtk_window_move (GTK_WINDOW (_toolbar), pos_x, pos_y);
00068         _toolbar_pos_x = pos_x;
00069         _toolbar_pos_y = pos_y;
00070     }
00071 }
00072 
00073 static gboolean
00074 ui_toolbar_motion_cb (GtkWidget *window,
00075                              GdkEventMotion *event,
00076                              gpointer user_data)
00077 {
00078     gint pos_x, pos_y;
00079     if ((event->state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK)) != 0 &&
00080         _toolbar_draging) {
00081         gtk_window_get_position (GTK_WINDOW (window), &pos_x, &pos_y);
00082         ui_toolbar_set_position(
00083             pos_x + ((gint) event->x_root - _toolbar_drag_x),
00084             pos_y + ((gint) event->y_root - _toolbar_drag_y));
00085 
00086         _toolbar_drag_x = (gint) event->x_root;
00087         _toolbar_drag_y = (gint) event->y_root;
00088 
00089         return TRUE;
00090     }
00091     return FALSE;
00092 }
00093                                                                                                           
00094 static gboolean
00095 ui_toolbar_click_cb (GtkWidget *window,
00096                             GdkEventButton *event,
00097                             gpointer user_data)
00098 {
00099     int click_type = GPOINTER_TO_INT (user_data);
00100     static gulong motion_handler;
00101     GdkCursor *cursor;
00102                                                                                                           
00103     if (click_type == 0 && event->button <= 1) {
00104         if (_toolbar_draging)
00105             return FALSE;
00106                                                                                                           
00107         // Connection pointer motion handler to this window.
00108         motion_handler = g_signal_connect (G_OBJECT (window), "motion-notify-event",
00109                                            G_CALLBACK (ui_toolbar_motion_cb),
00110                                            NULL);
00111                                                                                                           
00112         _toolbar_draging = TRUE;
00113         _toolbar_drag_x = (gint) event->x_root;
00114         _toolbar_drag_y = (gint) event->y_root;
00115                                                                                                           
00116         cursor = gdk_cursor_new (GDK_TOP_LEFT_ARROW);
00117                                                                                                           
00118         // Grab the cursor to prevent losing events.
00119         gdk_pointer_grab (window->window, TRUE,
00120                           (GdkEventMask) (GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK),
00121                           NULL, cursor, event->time);
00122         gdk_cursor_unref (cursor);
00123         return TRUE;
00124     } else if (click_type == 1 && event->button <= 1) {
00125         if (!_toolbar_draging)
00126             return FALSE;
00127                                                                                                           
00128         g_signal_handler_disconnect (G_OBJECT (window), motion_handler);
00129         gdk_pointer_ungrab (event->time);
00130         _toolbar_draging = FALSE;
00131 
00132         return TRUE;
00133     } else if (click_type == 1 && event->button > 1) {
00134         return TRUE;
00135     }
00136     return FALSE;
00137 }
00138 
00139 static void GetWindowPosition(Display *display, Window clientwindow, int *win_x, int *win_y)
00140 {
00141        int x=0, y=0;
00142        Window w_none;
00143        XWindowAttributes attr;
00144 
00145        if (clientwindow && display) {
00146               XGetWindowAttributes(display, clientwindow, &attr);
00147               if (attr.root)
00148                      XTranslateCoordinates(display, clientwindow, 
00149                             attr.root, -(attr.border_width), -(attr.border_width), &x, &y,
00150                              &w_none );
00151        }
00152        *win_x = x;
00153        *win_y = y;
00154 }
00155 
00156 static void GetDefaultPosition(int *win_x, int *win_y)
00157 {
00158        GtkRequisition ws;
00159 
00160        gtk_widget_size_request (_toolbar, &ws);
00161 
00162        *win_x = (gdk_screen_width () > 640)?200:100;
00163        *win_y = (gdk_screen_height () - ws.height - 10 - 36);
00164 }
00165 
00166 
00167 static void SetCandidateAuxPosition(aux_ext_data_t *aux_ext_data)
00168 {
00169        int win_x = 0, win_y = 0;
00170 
00171        GetWindowPosition((Display *)GDK_WINDOW_XDISPLAY(_toolbar->window), aux_ext_data->clientwin, &win_x, &win_y);
00172        if ((win_x == 0 && win_y == 0) || 
00173            (aux_ext_data->point.x == 0 && aux_ext_data->point.y == 0)) {
00174               GetDefaultPosition(&win_x, &win_y);
00175        } else {
00176               win_x += aux_ext_data->point.x;
00177               win_y += aux_ext_data->point.y + 22;
00178        }
00179 
00180        ui_toolbar_set_position(win_x, win_y);
00181 }
00182 
00183 AuxResult neima_aux_Start(aux_ext_data_t *aux_ext_data)
00184 {
00185        GtkWidget *frame;
00186        GtkWidget *hbox;
00187 
00188        printf("_toolbar: %p\n", _toolbar);
00189 
00190        if (_toolbar != NULL) {
00191               return (AUX_OK);
00192        }
00193 
00194         _toolbar = gtk_window_new (GTK_WINDOW_POPUP);
00195         gtk_window_set_policy (GTK_WINDOW (_toolbar), TRUE, TRUE, FALSE);
00196         gtk_window_set_resizable (GTK_WINDOW (_toolbar), FALSE);
00197         gtk_widget_add_events (_toolbar,GDK_BUTTON_PRESS_MASK);
00198         gtk_widget_add_events (_toolbar,GDK_BUTTON_RELEASE_MASK);
00199         gtk_widget_add_events (_toolbar,GDK_POINTER_MOTION_MASK);
00200         g_signal_connect (G_OBJECT (_toolbar), "button-press-event",
00201                           G_CALLBACK (ui_toolbar_click_cb),
00202                           GINT_TO_POINTER (0));
00203         g_signal_connect (G_OBJECT (_toolbar), "button-release-event",
00204                           G_CALLBACK (ui_toolbar_click_cb),
00205                           GINT_TO_POINTER (1));
00206 
00207        gtk_container_set_border_width (GTK_CONTAINER (_toolbar), 0);
00208 
00209         frame = gtk_frame_new (NULL);
00210        gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_ETCHED_OUT);
00211         gtk_container_add (GTK_CONTAINER (_toolbar), frame);
00212 
00213         //New status button.
00214         _label = gtk_label_new("1.    2.    3.    4.    5.    6.    7.    8.    9.    0.    ");
00215         gtk_container_add (GTK_CONTAINER (frame), _label);
00216 
00217        gtk_widget_realize(_toolbar);
00218        _toolbar_show_status = TRUE;
00219                                                                                                          
00220         ui_toolbar_set_position(0, 0);
00221        return (AUX_OK);
00222 }
00223 
00224 AuxResult neima_aux_Draw(aux_ext_data_t *aux_ext_data)
00225 {
00226        int integer_count;
00227        int string_count;
00228        char candidate_buf[2048];
00229        int encoding;
00230        int i;
00231        
00232        integer_count = aux_ext_data->integer_count;
00233        printf("integer_count: %d\n", integer_count);
00234        if (integer_count < 1)
00235               return (AUX_OK);
00236 
00237        string_count = aux_ext_data->string_count;
00238        printf("string_count: %d\n", string_count);
00239        if (string_count <= 1)
00240               return (AUX_OK);
00241 
00242        encoding = aux_ext_data->integer_list[0];
00243        printf("encoding: %d\n", encoding);
00244 
00245        memset(candidate_buf, 0, sizeof(char) * 2048);
00246        for (i=0; i < string_count; i++) {
00247               char label[10];
00248               unsigned char tmp_buf[512];
00249               unsigned char *tmp_ptr = tmp_buf;
00250               int from_len, to_left;
00251               int ret;
00252 
00253               aux_ext_data->string_list[i].ptr[aux_ext_data->string_list[i].length] = 0;
00254               printf("string: %s, len:  %d\n", aux_ext_data->string_list[i].ptr,  aux_ext_data->string_list[i].length);
00255 /*
00256 */
00257 
00258               from_len = aux_ext_data->string_list[i].length;
00259               to_left = 512;
00260               memset(tmp_buf, 0, sizeof(char) * 512);
00261               ret = Convert_Native_To_UTF8(   encoding,
00262                                           aux_ext_data->string_list[i].ptr,
00263                                           from_len,
00264                                           (char **)&tmp_ptr,
00265                                           (size_t *)&to_left);
00266               
00267               if (ret == -1) return (AUX_FAIL);
00268 
00269               tmp_buf[512-to_left] = 0;
00270               snprintf(label, 10, " %d: ", (i+1)%10);
00271               strcat(candidate_buf, label);
00272               strcat(candidate_buf, tmp_buf);
00273               if (i != string_count - 1) strcat(candidate_buf, " \n");
00274        }
00275 
00276        if (_toolbar != NULL) {
00277               printf("%s\n", candidate_buf);
00278               gtk_label_set_text(GTK_LABEL(_label), candidate_buf);
00279               gtk_widget_show_all(_toolbar);
00280               printf("x: %d, y: %d\n", aux_ext_data->point.x, aux_ext_data->point.y);
00281               SetCandidateAuxPosition(aux_ext_data);
00282 /*
00283               printf("neima screen num: %d\n", gtk_widget_get_screen(GTK_WIDGET(_toolbar)));
00284               gtk_window_set_screen(GTK_WINDOW(_toolbar), gtk_widget_get_screen(GTK_WIDGET(_toolbar)));
00285 */
00286        }
00287 
00288        return (AUX_OK);
00289 }
00290 
00291 AuxResult neima_aux_Done(aux_ext_data_t *aux_ext_data)
00292 {
00293        if (_toolbar != NULL) {
00294               gtk_widget_hide(_toolbar);
00295        }
00296        return (AUX_OK);
00297 }