Changeset 3411 for trunk/jons-gtk2-gui

Show
Ignore:
Timestamp:
03/24/03 04:04:03 (6 years ago)
Author:
bostjanlah
Message:

Fix display of colors and a few simplifications

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/jons-gtk2-gui/src/convo.cpp

    r3400 r3411  
    4242    GtkWidget *send_list; 
    4343    GtkWidget *charset; 
    44     GdkColor *clrFore; 
    45     GdkColor *clrBack; 
    4644    gchar *for_user; 
    4745    ICQUser *user; 
     
    5351/********** Structures ******************/ 
    5452/* Functions in convo.cpp */ 
    55 conversation *convo_find(unsigned long); 
    5653void convo_show(conversation *); 
    5754void convo_nick_timestamp(GtkWidget *, const char *, time_t, GdkColor *); 
    5855void convo_send(GtkWidget *, conversation *c); 
    59 gboolean key_press_convo(GtkWidget *, GdkEventKey *, gpointer); 
     56gboolean key_press_convo(GtkWidget *, GdkEventKey *, conversation *); 
    6057void verify_convo_send(GtkWidget *, guint, gchar *, conversation *); 
    6158void convo_cancel(GtkWidget *, conversation *); 
    62 gint convo_delete(GtkWidget *, GdkEvent *, conversation *); 
    63 void convo_close(GtkWidget *, conversation *); 
    6459void convo_recv(gulong uin); 
    6560 
    66 void convo_open_cb(ICQUser *user) 
    67 { 
    68     convo_open(user, true); 
    69 } 
    70  
    71 void convo_open(ICQUser *user, bool refresh) 
     61conversation * 
     62convo_find(unsigned long uin) 
     63{ 
     64    for (list<conversation *>::iterator i = cnv.begin(); i != cnv.end(); ++i) 
     65        if ((*i)->user->Uin() == uin) 
     66            return *i; 
     67 
     68    return 0; 
     69} 
     70 
     71void  
     72convo_open(ICQUser *user) 
    7273{ 
    7374    conversation *c = convo_find(user->Uin()); 
    7475 
    75     if(c != 0) 
     76    if (c != 0) 
    7677        gtk_window_present(GTK_WINDOW(c->window)); 
    77     else 
    78     { 
     78    else { 
    7979        c = g_new0(conversation, 1); 
    80  
    81         c->window = 0; 
     80        cnv.push_back(c); 
     81 
    8282        c->user = user; 
    8383         
    84         c->clrBack = new GdkColor; 
    85         c->clrFore = new GdkColor; 
    86         c->for_user = 0; 
    87  
    88         cnv.push_back(c); 
    8984        convo_show(c); 
    9085 
     
    9287            convo_recv(c->user->Uin()); 
    9388         
    94         if (refresh) 
    95             system_status_refresh(); 
     89        system_status_refresh(); 
    9690 
    9791        // Stop the flashing if necessary 
     
    10296    if (c->user->Status() == ICQ_STATUS_OFFLINE) 
    10397        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(c->send_server), TRUE); 
    104 } 
    105  
    106 conversation * 
    107 convo_find(unsigned long uin) 
    108 { 
    109     for (list<conversation *>::iterator i = cnv.begin(); i != cnv.end(); ++i) 
    110         if ((*i)->user->Uin() == uin) 
    111             return *i; 
    112  
    113     return 0; 
    11498} 
    11599 
     
    123107} 
    124108 
    125 void convo_close_or_cancel(GtkWidget *widget, conversation *c) 
     109void 
     110convo_close_or_cancel(GtkWidget *widget, conversation *c) 
    126111{ 
    127112    if (strcmp(gtk_button_get_label(GTK_BUTTON(widget)), GTK_STOCK_CANCEL) == 0) 
    128113        convo_cancel(widget, c); 
    129114    else 
    130         convo_close(widget, c); 
     115        window_close(0, c->window); 
    131116} 
    132117 
     
    169154} 
    170155     
    171 void convo_show(conversation *c) 
     156//#include <iostream> 
     157//using namespace std; 
     158 
     159void 
     160convo_destroy(GtkWidget *widget, conversation *c) 
     161{ 
     162    // remove this convo from convos list and catcher list 
     163    cnv.remove(c); 
     164    catcher = g_slist_remove(catcher, c->etag); 
     165 
     166    // remove all dynamically alloc'd data in c as well as c 
     167    if (c->for_user != 0) 
     168        g_free(c->for_user); 
     169    g_free(c->etag); 
     170    g_free(c); 
     171} 
     172 
     173void 
     174convo_show(conversation *c) 
    172175{ 
    173176    GtkWidget *scroll; 
     
    217220  gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(c->entry), GTK_WRAP_WORD); 
    218221    g_signal_connect(G_OBJECT(c->entry), "key_press_event", 
    219             G_CALLBACK(key_press_convo), (gpointer)c); 
     222            G_CALLBACK(key_press_convo), c); 
    220223 
    221224    gtk_widget_set_size_request(c->entry, -1, 75);  
     
    297300 
    298301    /* Don't forget the delete_event signal */ 
     302    /* 
    299303    g_signal_connect(G_OBJECT(c->window), "delete_event", 
    300304            G_CALLBACK(convo_delete), c); 
    301      
     305    */ 
     306    g_signal_connect(G_OBJECT(c->window), "destroy", 
     307            G_CALLBACK(convo_destroy), c); 
    302308    gtk_widget_show_all(c->window); 
    303309    gtk_button_set_label(GTK_BUTTON(c->close_or_cancel), GTK_STOCK_CLOSE); 
    304310} 
    305311 
    306 gboolean key_press_convo(GtkWidget *entry, GdkEventKey *eventkey, gpointer data) 
    307 { 
    308     if (eventkey->keyval == GDK_Return) { 
    309         conversation *c = (conversation *)data; 
    310         guint state = eventkey->state; 
    311          
    312         // We send when: 
    313         // - enter_sends is true and plain Enter was presses 
    314         // - enter_sends is false and Shift/Ctrl+Enter was pressed 
    315         if ((!enter_sends && (state & GDK_SHIFT_MASK)) || 
    316                 (enter_sends && !(state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)))) { 
    317             convo_send(0, c); 
    318             return TRUE; 
    319         } 
     312gboolean 
     313key_press_convo(GtkWidget *entry, GdkEventKey *eventkey, struct conversation *c) 
     314{ 
     315    // we only react when Enter was pressed 
     316    if (eventkey->keyval != GDK_Return) 
     317        return FALSE; 
     318 
     319    // We send when: 
     320    // - enter_sends is true and plain Enter was presses 
     321    // - enter_sends is false and Shift/Ctrl+Enter was pressed 
     322    bool plain_enter = !(eventkey->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)); 
     323    if ((enter_sends && plain_enter) || (!enter_sends && !plain_enter)) { 
     324        convo_send(0, c); 
     325        return TRUE; 
    320326    } 
    321327 
     
    323329} 
    324330 
    325 void convo_nick_timestamp(GtkWidget *text, const char *nick,  
     331void  
     332convo_nick_timestamp(GtkWidget *text, const char *nick,  
    326333        time_t message_time, const char *color) 
    327334{ 
    328335    GtkTextBuffer *tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)); 
     336    /* 
     337    We use this special addnl thing so that we only add newlines a new message 
     338    instead of after - this gives us the nicest possible 
     339    scrolling - i.e. to the bottom with no gap below the vertical scrollbar 
     340    */ 
    329341    bool addnl = (gtk_text_buffer_get_char_count(tb) > 0); 
    330342    GtkTextIter iter; 
     343    gtk_text_buffer_get_end_iter(tb, &iter); 
    331344     
    332345    // How about their alias and an optional timestamp? 
    333346    if (show_convo_timestamp) { 
    334347        char szTime[26]; 
    335         struct tm *_tm = localtime(&message_time); 
    336         strftime(szTime, 26, timestamp_format, _tm); 
     348        strftime(szTime, 26, timestamp_format, localtime(&message_time)); 
    337349        szTime[25] = '\0'; 
    338350 
    339         char *szTempStamp = g_strdup_printf("[%s] ", szTime); 
    340         gtk_text_buffer_get_end_iter(tb, &iter); 
    341351        if (addnl) { 
    342352            gtk_text_buffer_insert(tb, &iter, "\n", 1); 
    343353            addnl = false; 
    344             gtk_text_buffer_get_end_iter(tb, &iter); 
    345354        } 
     355         
     356        char *szTempStamp = g_strdup_printf("[%s] ", szTime); 
    346357        gtk_text_buffer_insert(tb, &iter, szTempStamp, -1); 
    347358        g_free(szTempStamp); 
    348359    } 
    349360     
    350     gtk_text_buffer_get_end_iter(tb, &iter); 
    351     if (addnl) { 
     361    if (addnl) 
    352362        gtk_text_buffer_insert(tb, &iter, "\n", 1); 
    353         gtk_text_buffer_get_end_iter(tb, &iter); 
    354     } 
    355  
     363         
    356364    gtk_text_buffer_insert_with_tags_by_name(tb, &iter, nick, -1, color, NULL); 
    357     gtk_text_buffer_get_end_iter(tb, &iter); 
    358365    gtk_text_buffer_insert_with_tags_by_name(tb, &iter, ": ", 2, color, NULL); 
    359366} 
    360367 
    361 void convo_send(GtkWidget *widget, conversation *c) 
     368void  
     369convo_send(GtkWidget *widget, conversation *c) 
    362370{ 
    363371    /* Set the 2 button widgets */ 
     
    366374    toggle_close_cancel(c, 2); 
    367375 
    368     gchar *buf; 
    369     gboolean urgent = FALSE; 
    370  
    371376    GtkTextIter start, end; 
    372377    GtkTextBuffer *tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(c->entry)); 
    373378    gtk_text_buffer_get_start_iter(tb, &start); 
    374379    gtk_text_buffer_get_end_iter(tb, &end); 
    375     buf = gtk_text_buffer_get_text(tb, &start, &end, FALSE); 
    376     const gchar *message = buf;  
    377380    if (c->for_user != 0) 
    378381        g_free(c->for_user); 
    379     c->for_user = buf; 
    380  
    381     c->user->SetSendServer(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(c->send_server))); 
     382    // we store it here so we can later on show it (once we know it was 
     383    // successfully sent 
     384    c->for_user = gtk_text_buffer_get_text(tb, &start, &end, FALSE); 
     385    bool send_server =  
     386            gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(c->send_server)); 
     387             
     388    c->user->SetSendServer(send_server); 
    382389 
    383390    /* I don't like those popups to send urgent... so just send it ** 
    384391    ** urgently unless the user says to send it to the contact list*/    
    385     if ((c->user->Status() == ICQ_STATUS_DND || 
    386        c->user->Status() == ICQ_STATUS_OCCUPIED) && 
    387        !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(c->send_urgent))) 
     392    // i.e. we'll send urgently even when user didn't specifically check 
     393    // urgent box 
     394    gboolean urgent = 
     395            gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(c->send_urgent)); 
     396    if (!urgent && 
     397            (c->user->Status() == ICQ_STATUS_DND ||  
     398             c->user->Status() == ICQ_STATUS_OCCUPIED)) 
    388399        urgent = TRUE; 
    389400 
     
    394405    else 
    395406        strcat(c->etag->buf, "through server ... "); 
     407    status_change(c->etag->statusbar, "sta", c->etag->buf); 
    396408 
    397409    /* Send the message */ 
    398     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(c->send_urgent)) || 
    399           urgent) 
    400       c->etag->e_tag = icq_daemon->icqSendMessage(c->user->Uin(), message, 
    401             (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(c->send_server))), 
    402             ICQ_TCPxMSG_URGENT); 
     410    char *message = convert_from_utf8(c->for_user, c->user->UserEncoding()); 
     411    unsigned short msg_type; 
     412    if (urgent) 
     413        msg_type = ICQ_TCPxMSG_URGENT; 
    403414    /* Send to contact list */ 
    404415    else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(c->send_list))) 
    405       c->etag->e_tag = icq_daemon->icqSendMessage(c->user->Uin(), message, 
    406                 (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(c->send_server))), 
    407         ICQ_TCPxMSG_LIST); 
     416        msg_type = ICQ_TCPxMSG_LIST; 
    408417    else /* Just send it normally */ 
    409       c->etag->e_tag = icq_daemon->icqSendMessage(c->user->Uin(), message, 
    410             (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(c->send_server))), 
    411         ICQ_TCPxMSG_NORMAL); 
    412  
     418        msg_type = ICQ_TCPxMSG_NORMAL; 
     419  c->etag->e_tag = icq_daemon->icqSendMessage(c->user->Uin(), message, 
     420        !send_server, msg_type); 
     421    g_free(message); 
     422     
    413423    /* Take care of the etd buffer and add it to the slist */ 
    414     status_change(c->etag->statusbar, "sta", c->etag->buf); 
    415424    catcher = g_slist_append(catcher, c->etag); 
    416425} 
    417426 
    418 void convo_cancel(GtkWidget *widget, conversation *c) 
     427void 
     428convo_cancel(GtkWidget *widget, conversation *c) 
    419429{ 
    420430    /* Set the buttons sensitivity accordingly */ 
     
    431441    status_change(c->etag->statusbar, "sta", c->etag->buf); 
    432442} 
    433  
    434 #include <iostream> 
    435 using namespace std; 
    436443 
    437444void 
     
    446453} 
    447454 
    448 void convo_recv(gulong uin) 
     455void 
     456convo_recv(gulong uin) 
    449457{ 
    450458    conversation *c = convo_find(uin); 
     
    462470        return; 
    463471 
    464     // Use theme colors if it is black on white 
    465     bool bIgnoreBW = false; 
    466  
    467472    // Get the color that it was sent in if it's wanted 
     473    GtkTextBuffer *tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(c->text)); 
     474    GtkTextTag *tag = NULL; 
    468475    if (recv_colors) { 
    469         if (!c->clrBack) 
    470             c->clrBack = new GdkColor; 
    471         if (!c->clrFore) 
    472             c->clrFore = new GdkColor; 
    473  
    474476        CICQColor *pIcqColor = u_event->Color(); 
    475477 
    476         if (pIcqColor->Foreground() == 0x00000000 && 
    477                 pIcqColor->Background() == 0x00FFFFFF) 
    478             bIgnoreBW = true; 
    479         else { 
    480             c->clrFore->red   = pIcqColor->ForeRed() * 257; 
    481             c->clrFore->green = pIcqColor->ForeGreen() * 257; 
    482             c->clrFore->blue  = pIcqColor->ForeBlue() * 257; 
    483             c->clrFore->pixel = 255; 
    484             c->clrBack->red   = pIcqColor->BackRed() * 257; 
    485             c->clrBack->green = pIcqColor->BackGreen() * 257; 
    486             c->clrBack->blue  = pIcqColor->BackBlue() * 257; 
    487             c->clrBack->pixel = 255; 
     478        // we'll consider it only when bg isn't white and fg isn't black 
     479        if (pIcqColor->Foreground() != 0x00000000 || 
     480                pIcqColor->Background() != 0x00FFFFFF) { 
     481            char fg[8], bg[8]; 
     482            snprintf(fg, 8, "#%02lx%02lx%02lx",  
     483                    pIcqColor->ForeRed(), pIcqColor->ForeGreen(), pIcqColor->ForeBlue()); 
     484            snprintf(fg, 8, "#%02lx%02lx%02lx",  
     485                    pIcqColor->BackRed(), pIcqColor->BackGreen(), pIcqColor->BackBlue()); 
     486            tag = gtk_text_buffer_create_tag(tb, NULL,  
     487                    "foreground", fg, "background", bg, NULL); 
    488488        } 
    489489    } 
    490     else { 
    491         if (c->clrFore) { 
    492             delete c->clrFore; 
    493             c->clrFore = 0; 
    494         } 
    495         if (c->clrBack) { 
    496             delete c->clrBack; 
    497             c->clrBack = 0; 
    498         } 
    499     } 
    500490 
    501491    // How about their alias and an optional timestamp? 
    502     GtkTextBuffer *tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(c->text)); 
    503     convo_nick_timestamp(c->text,  
    504             s_convert_to_utf8(c->user->GetAlias(), c->user->UserEncoding()).c_str(),  
    505             u_event->Time(), "remote"); 
     492    gchar *alias = convert_to_utf8(c->user->GetAlias(), c->user->UserEncoding()); 
     493    convo_nick_timestamp(c->text, alias, u_event->Time(), "remote"); 
    506494    GtkTextIter iter; 
    507495    gtk_text_buffer_get_end_iter(tb, &iter); 
    508     gchar *txt; 
     496    gchar *txt = convert_to_utf8(u_event->Text(), c->user->UserEncoding()); 
    509497     
    510498    switch (u_event->SubCommand()) { 
    511499      case ICQ_CMDxSUB_MSG: 
    512             txt = convert_to_utf8(u_event->Text(), c->user->UserEncoding()); 
    513             if (!bIgnoreBW) { 
    514                 GtkTextTag *tag = gtk_text_buffer_create_tag(tb, NULL,  
    515                         "foreground-gdk", c->clrFore, 
    516                         "background-gdk", c->clrBack, 
    517                         NULL); 
     500            if (tag != NULL)    { 
    518501                gtk_text_buffer_insert_with_tags(tb, &iter, txt, -1, tag, NULL); 
     502                // we have to add at least one character otherwise changed colors 
     503                // don't show (no idea why...) 
     504                gtk_text_buffer_insert(tb, &iter, " ", 1); 
    519505            } 
    520506            else 
    521507                gtk_text_buffer_insert(tb, &iter, txt, -1); 
    522             g_free(txt); 
    523508            break; 
    524509 
    525510      case ICQ_CMDxSUB_URL: 
    526             txt = convert_to_utf8(u_event->Text(), c->user->UserEncoding()); 
    527             if (true) { 
     511            { 
    528512                gchar *for_user_u = 
    529                  g_strdup_printf("\n%s has sent you a URL!\n%s\n", 
    530                             c->user->GetAlias(), 
    531                             txt); 
    532  
     513                    g_strdup_printf("%s has sent you a URL:\n%s", alias, txt); 
    533514                gtk_text_buffer_insert(tb, &iter, for_user_u, -1); 
    534515                g_free(for_user_u); 
    535516            } 
    536             g_free(txt); 
    537517            break; 
    538518 
    539519      case ICQ_CMDxSUB_CHAT: 
    540             txt = convert_to_utf8(u_event->Text(), c->user->UserEncoding()); 
    541  
    542             if(u_event->IsCancelled()) 
     520            if (u_event->IsCancelled()) 
    543521                gtk_text_buffer_insert(tb, &iter, txt, -1); 
    544             else 
    545             { 
     522            else { 
    546523                gchar *for_user_c = 
    547                     g_strdup_printf("\n%s requests to chat with you!\n%s\n", 
    548                     c->user->GetAlias(), txt); 
     524                        g_strdup_printf("%s requests to chat with you:\n%s", alias, txt); 
    549525                gtk_text_buffer_insert(tb, &iter, for_user_c, -1); 
     526                g_free(for_user_c); 
    550527 
    551528                CEventChat *c_event = (CEventChat *)u_event; 
    552529                chat_accept_window(c_event, uin); 
    553                 g_free(for_user_c); 
    554530            } 
    555             g_free(txt); 
    556531            break; 
    557532 
    558533      case ICQ_CMDxSUB_FILE: 
    559             if(u_event->IsCancelled()) 
    560                 gtk_text_buffer_insert(tb, &iter, u_event->Text(), -1); 
    561             else 
    562             { 
     534            if (u_event->IsCancelled()) 
     535                gtk_text_buffer_insert(tb, &iter, txt, -1); 
     536            else { 
    563537                gchar *for_user_f = 
    564                     g_strdup_printf("\n%s requests to send you a file!\n%s\n", 
    565                         c->user->GetAlias(), u_event->Text()); 
    566  
     538                    g_strdup_printf("%s requests to send you a file:\n%s", alias, txt); 
    567539                gtk_text_buffer_insert(tb, &iter, for_user_f, -1); 
     540                g_free(for_user_f); 
     541 
    568542                file_accept_window(c->user, u_event); 
    569                 g_free(for_user_f); 
    570543            } 
    571544            break; 
     
    578551} 
    579552 
    580 gboolean convo_delete(GtkWidget *widget, GdkEvent *event, conversation *c) 
    581 { 
    582     convo_close(0, c); 
    583     return false; 
    584 } 
    585  
    586 void convo_close(GtkWidget *widget, conversation *c) 
    587 { 
    588     if (c->clrBack) 
    589         delete c->clrBack; 
    590     if (c->clrFore) 
    591         delete c->clrFore; 
    592  
    593     cnv.remove(c); 
    594     catcher = g_slist_remove(catcher, c->etag); 
    595  
    596     gtk_widget_destroy(c->window); 
    597  
    598     if (c->for_user != 0) 
    599         g_free(c->for_user); 
    600     g_free(c->etag); 
    601     g_free(c); 
    602 } 
    603  
    604 void finish_message(ICQEvent *event) 
     553void 
     554finish_message(ICQEvent *event) 
    605555{ 
    606556    conversation *c = convo_find(event->Uin()); 
     
    613563    if (event->Result() == EVENT_ACKED || event->Result() == EVENT_SUCCESS) { 
    614564        ICQOwner *owner = gUserManager.FetchOwner(LOCK_R); 
    615         const gchar *name = owner->GetAlias(); 
     565        gchar *name = convert_to_utf8(owner->GetAlias(), owner->UserEncoding()); 
    616566        gUserManager.DropOwner(); 
    617567 
     
    621571         
    622572        convo_nick_timestamp(c->text, name, time(NULL), "local"); 
     573        g_free(name); 
    623574        tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(c->text)); 
    624575        GtkTextIter iter; 
     
    631582    toggle_close_cancel(c, 1); 
    632583} 
    633