/* mainwindow.c init and show mainwindow! * Copyright (c) 2004 by SmartLu All Rights Reserved * Distributed under the terms of the GNU General Public License (GPL) * See the GNU Library General Public License for more details. */ #include "srecite.h" #include #include #include /* mouse press including left right middle button*/ static void mouse_press(GtkWidget *widget,GdkEventButton *event,gpointer *data); /* mouse release */ static void mouse_release(GtkWidget *widget,GdkEventButton *event,gpointer *data); /* motion notify */ static void motion_notify(GtkWidget *widget,GdkEventButton *event,gpointer *data); static int mouse_x; static int mouse_y; GtkWidget *label,*window;//window and label GtkWidget *menu;//popupmenu PangoFontDescription *font;//font gchar font_desc[255]; PangoFontDescription *font2;//font gchar font2_desc[255]; //home path gchar home_path[1024]; struct conf_st S_conf; struct state_st S_state; struct line_st S_line; /* all start here */ int main(int argc,char *argv[]) { init_conf(); init_state(); init_all(); main_window(argc,argv); return 0; } void init_conf() { strcpy(home_path, (char *) getenv ("HOME")); strcat(home_path, "/.srecite/"); //load and init from srecite.conf load_conf(); } // init state void init_state() { S_state.wordfile = NULL; S_state.toolbar_show = FALSE; S_state.style_step = 0; } void init_all() { time_t t; //random rand seed srand( (unsigned) time(&t)); //font //sprintf(font_desc,"%s %d",S_conf.font,S_conf.fontsize); strcpy(font_desc,S_conf.font); strcat(font_desc," "); strcat(font_desc,S_conf.fontsize); //g_print("font:%s\n",font_desc); font = pango_font_description_from_string(font_desc); //font2 strcpy(font2_desc,S_conf.font); strcat(font2_desc," "); strcat(font2_desc,"12"); font2 = pango_font_description_from_string(font2_desc); } void uninit_conf() { } void uninit_state() { } void uninit_all() { timer_uninstall(); pango_font_description_free(font); pango_font_description_free(font2); } void main_window(int argc,char *argv[]) { GtkWidget *vbox,*eventbox; //gchar *font_desc = "sans 18"; //gchar *font_desc = "lucida sans unicode 18"; //gchar *font_desc = "Kingsoft Phonetic Plain"; gtk_set_locale(); gtk_init(&argc,&argv); //gtk_rc_parse_string(gtkrc_string); gtk_rc_parse(PRE_DIR"srecite.rc"); /* window */ window=gtk_window_new(GTK_WINDOW_POPUP); gtk_window_set_title(GTK_WINDOW(window),LOGO); gtk_window_set_policy(GTK_WINDOW(window),FALSE,FALSE,FALSE); gtk_widget_set_name(window, "mywindow"); //set window size gtk_widget_set_usize(window,0,0); //set window left gtk_widget_set_uposition(window,S_conf.win_x,S_conf.win_y); //set window border size gtk_container_border_width(GTK_CONTAINER(window),1); gtk_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC (quit_window),window); /* vbox */ vbox=gtk_vbox_new(FALSE,0); gtk_widget_set_name(vbox,"mywidget"); gtk_container_add(GTK_CONTAINER(window),vbox); gtk_widget_show(vbox); /* eventbox */ eventbox = gtk_event_box_new(); gtk_widget_set_name(eventbox,"mywidget"); gtk_box_pack_start(GTK_BOX(vbox),eventbox,FALSE,FALSE,0); gtk_widget_show(eventbox); gtk_widget_set_events(eventbox,GDK_BUTTON_PRESS_MASK); gtk_signal_connect (GTK_OBJECT(eventbox),"button_press_event", GTK_SIGNAL_FUNC (mouse_press),window); gtk_signal_connect (GTK_OBJECT(eventbox),"button_release_event", GTK_SIGNAL_FUNC (mouse_release),window); gtk_signal_connect (GTK_OBJECT(eventbox),"motion_notify_event", GTK_SIGNAL_FUNC (motion_notify),window); gtk_widget_realize(eventbox); gdk_window_set_cursor(eventbox->window,gdk_cursor_new(GDK_HAND1)); /* to show popupmenu */ init_menu();//init menuitem menu and root_menu gtk_widget_set_name(menu, "mymenu"); //caution this sometimes wrong! g_signal_connect_swapped(GTK_OBJECT(eventbox),"button_press_event", GTK_SIGNAL_FUNC(popupmenu_show),menu); /* label */ label=gtk_label_new(LOGO); gtk_widget_set_name(label, "mylabel"); gtk_container_add(GTK_CONTAINER(eventbox),label); gtk_widget_show(label); // font gtk_widget_modify_font((GtkWidget *) label, font); /* toolbar */ create_toolbar (window); gtk_widget_set_name(toolbar, "mytoolbar"); gtk_box_pack_start(GTK_BOX(vbox),toolbar,FALSE,TRUE,0); //gtk_widget_show(toolbar); gtk_widget_show(window); /* other need to do */ timer_install(); // auto load // including init word book load_state_0(); transparent_init(); gtk_main(); } //quit all void quit_window() { save_state_0(); //save conf when quit save_conf(); uninit_conf(); uninit_state(); uninit_all(); gtk_main_quit(); g_print("Save and Quit!Bye bye!\n"); } /* mouse press including left right middle button*/ static void mouse_press(GtkWidget *widget,GdkEventButton *event,gpointer *data) { //int x,y,ma; if (event->button == 3) { //right button popupmenu_show((GtkWidget *)menu,(GdkEvent *)event); } else if (event->button == 1) { //left button gdk_window_set_cursor (event->window,gdk_cursor_new(GDK_HAND2)); gdk_window_get_origin(widget->window,&S_conf.win_x,&S_conf.win_y); mouse_x = event->x ; mouse_y = event->y ; } } /* mouse release */ static void mouse_release(GtkWidget *widget,GdkEventButton *event,gpointer *data) { if (event->button == 1) { gdk_window_set_cursor (event->window,gdk_cursor_new(GDK_HAND1)); gdk_window_get_origin(widget->window,&S_conf.win_x,&S_conf.win_y); //mouse_x = event->x ; //mouse_y = event->y ; S_conf.win_x = S_conf.win_x + (event->x - mouse_x); S_conf.win_y = S_conf.win_y + (event->y - mouse_y); if(S_conf.win_x < 0) S_conf.win_x=0; if(S_conf.win_y < 0) S_conf.win_y=0; //g_print("%d %d \n",S_conf.win_x,S_conf.win_y); gtk_widget_set_uposition (window,S_conf.win_x,S_conf.win_y); } } /* motion notify */ static void motion_notify(GtkWidget *widget,GdkEventButton *event,gpointer *data) { gdk_window_get_origin(widget->window,&S_conf.win_x,&S_conf.win_y); S_conf.win_x = S_conf.win_x + (event->x - mouse_x); S_conf.win_y = S_conf.win_y + (event->y - mouse_y); if(S_conf.win_x < 0) S_conf.win_x=0; if(S_conf.win_y < 0) S_conf.win_y=0; // g_print("%d %d \n",S_conf.win_x,S_conf.win_y); gtk_widget_set_uposition (window,S_conf.win_x,S_conf.win_y); } /* install timer 1 and 2 */ void timer_install() { if(S_conf.style == 0) S_state.timer = gtk_timeout_add(S_conf.time_interval,show_word,NULL); else S_state.timer = gtk_timeout_add(S_conf.time_interval/2,show_word,NULL); } /* uninstall timer 1 and 2 */ void timer_uninstall() { gtk_timeout_remove(S_state.timer); } void about() { gchar * title = "About SRecite"; gchar * msg = LOGO"\n" "Copyright (c) 2004 SmartLu@USTC \n" "Homepage: http://sourceforge.net/projects/srecite/ \n" "Email: smartlu@eyou.com \n" "Distributed under the terms of the GNU General Public License (GPL)"; create_about_dialog(title,msg,"O K",PRE_DIR"icon/exit.png"); }