[C+GTK] Scrollbar qui pose problème

Scrollbar qui pose problème [C+GTK] - C - Programmation

Marsh Posté le 08-05-2007 à 22:01:57    

Bonjour tlm,
je voudrais créer un programme de chat, qui contient donc 1 zone ou arrive le texte (label), une zone de saisie de texte (textview) et enfin un bouton pour envoyer le texte saisi (button).
Je voudrais inclure tout ca dans une fenetre de taille fixe, avec scrollbar. Alors je ne sais pas s'il me faut 1 scrollbar pour les 2 zones de texte, ou s'il m'en faut 2.

 

Mais pour l'instant je tourne en rond :( Pouvez-vous me dire ce qui ne convient pas dans mon code, j'avoue ne pas tout saisir :(

 
Code :
  1. GtkWidget* pBox;
  2. GtkWidget* pTextView;
  3. GtkWidget* pButton;
  4. GtkWidget *scrollbar;
  5. GtkWidget *scrollbar2;
  6. GtkWidget* pWindow;
  7. gtk_init(&argc, &argv);
  8. pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  9. gtk_window_set_default_size(GTK_WINDOW(pWindow), 320, 200);
  10. gtk_window_set_title(GTK_WINDOW(pWindow), "OBMSN" );
  11. g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(gtk_main_quit), NULL);
  12. scrollbar=gtk_scrolled_window_new(NULL, NULL);
  13. gtk_container_add(GTK_CONTAINER(pWindow),scrollbar);
  14. pBox = gtk_vbox_new(FALSE, 5);
  15. printf("%s\n",received);
  16. // la fonction utf8 permet d'afficher les caracteres accentues
  17. sUtf8 = g_locale_to_utf8(received, -1, NULL, NULL, NULL);
  18.    pLabel = gtk_label_new(sUtf8);
  19. g_free(sUtf8);
  20. gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrollbar), pBox);
  21. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
  22.         gtk_label_set_text(GTK_LABEL(pLabel), received);
  23. gtk_box_pack_start(GTK_BOX(pBox), pLabel, FALSE, FALSE, 5);
  24. pTextView = gtk_text_view_new();
  25. gtk_box_pack_start(GTK_BOX(pBox), pTextView, FALSE, FALSE, 0);
  26. pButton=gtk_button_new_with_label("Send" );
  27. gtk_box_pack_start(GTK_BOX(pBox),pButton,FALSE,FALSE,0);
  28. g_signal_connect(G_OBJECT(pButton), "clicked", G_CALLBACK(Sending), (gpointer) pTextView);
  29. gtk_widget_show_all(pWindow);
  30. gtk_main();
 

Merci :jap:


Message édité par Bourreau le 08-05-2007 à 22:03:47
Reply

Marsh Posté le 08-05-2007 à 22:01:57   

Reply

Marsh Posté le 08-05-2007 à 22:20:16    

Quel est le problème exactement ?
Si tu as des messages d'erreurs à la compilation ou à l'exécution, quels sont-ils ?


---------------
Me: Django Localization, Yogo Puzzle, Chrome Grapher, C++ Signals, Brainf*ck.
Reply

Marsh Posté le 08-05-2007 à 22:24:11    

Non je n'ai pas en l'état de problème à la compilation. Le problème est que celà ne rend pas ce que je voudrais.
 
Donc j'imagine que je fais mal, et j'aurais aimé connaitre le cheminement exact, cad ce que je dois inclure dans la window, ce que je dois inclure dans la box et à quoi j'applique les scrollbar.
 
La j'ai une fenetre avec scrollbar, et à l'interieur de la fenetre, j'ai tt le reste. Moi je voudrais plutot que les scrollbar s'appliquent aux zones de texte, et en particulier pas au bouton :/

Reply

Marsh Posté le 08-05-2007 à 22:31:21    

Crée une vbox avec 3 cases que tu met dans la fenêtre
Dans la case du haut tu met une scrollview et dedans ta zone de texte recu.
Dans la case du milieu tu met une scrollview et dedans ta zone de texte a ecrire.
Dans la case du bas tu met une GTKHButtonBox et dedans tu met ton bouton send.
 
Quand t'as finit ça, tu ajuste les propriétés expand, les marges, et les polices de tes scrollbars et tu aura à peu près ce que tu veut normalement.
 
Ensuite si t'en a marre de galérer 107 ans, tu utilise glade 3, c'est quand même vachement plus pratique ;)


---------------
Me: Django Localization, Yogo Puzzle, Chrome Grapher, C++ Signals, Brainf*ck.
Reply

Marsh Posté le 08-05-2007 à 22:35:45    

Oula, je vais essayer ca (les 3 cases, pas glade, je peux pas, cai dans le cadre d'un projet, je n'ai pas le choix :sweat:).

Reply

Marsh Posté le 08-05-2007 à 22:37:28    

Bourreau a écrit :

Oula, je vais essayer ca (les 3 cases, pas glade, je peux pas, cai dans le cadre d'un projet, je n'ai pas le choix :sweat:).

 

Sers t'en pour prototyper et tester les propriétés et les différents arrangements, quand t'as finit tu le code à la main et t'as évité 42 recompilations pour tester des détails d'1pixel de large ;)


Message édité par 0x90 le 08-05-2007 à 22:37:39

---------------
Me: Django Localization, Yogo Puzzle, Chrome Grapher, C++ Signals, Brainf*ck.
Reply

Marsh Posté le 08-05-2007 à 22:45:25    

Bon je vais voir à quoi ressemble le glade 3.
Par contre j'ai testé la vbox, mais je ne parviens pas à rajouter le GTKHBUTTON dans la fenetre, avec la box qui contient le tout :
 

Citation :


Gtk-WARNING **: Attempting to add a widget with type GtkHButtonBox to a GtkWindow, but as a GtkBin subclass a GtkWindow can only contain one widget at a time; it already contains a widget of type GtkScrolledWindow

Reply

Marsh Posté le 08-05-2007 à 22:52:22    

tu dois ajouter la buttonbox a la vbox, pas a la window.


---------------
Me: Django Localization, Yogo Puzzle, Chrome Grapher, C++ Signals, Brainf*ck.
Reply

Marsh Posté le 08-05-2007 à 23:07:11    

Yep, merci bcp, effectivement ca marche comme ca :):)
 
Par contre j'ai encore un ptit problème, le texte qui arrive dans ma premiere zone de texte n'est pas pris en compte par la premiere scrollbar. La scrollbar de la zone de saisie est active, mais dans la premiere partie, la scrollbar reste fixe.
 
Un label ne peut-il pas avoir de scrollbar ?

Reply

Marsh Posté le 08-05-2007 à 23:37:37    

Voila ou j'en suis :

Code :
  1. GtkWidget* pBox;
  2. GtkWidget* pTextView;
  3. GtkWidget* pButton;
  4. GtkWidget *scrollbar;
  5. GtkWidget *scrollbar2;
  6. GtkWidget* pWindow;
  7. GtkWidget* hbuttonbox;
  8. gtk_init(&argc, &argv);
  9. pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  10. gtk_window_set_default_size(GTK_WINDOW(pWindow), 320, 200);
  11. gtk_window_set_title(GTK_WINDOW(pWindow), "OBMSN" );
  12. g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(gtk_main_quit), NULL);
  13. pBox = gtk_vbox_new(FALSE, 5);
  14. gtk_container_add(GTK_CONTAINER(pWindow),pBox);
  15. scrollbar = gtk_scrolled_window_new(NULL, NULL);
  16. scrollbar2 = gtk_scrolled_window_new(NULL, NULL);
  17. gtk_box_pack_start(GTK_BOX(pBox), scrollbar, TRUE, TRUE, 5);
  18. // la fonction utf8 permet d'afficher les caracteres accentues
  19. sUtf8 = g_locale_to_utf8(received, -1, NULL, NULL, NULL);
  20.    pLabel = gtk_label_new(sUtf8);
  21. g_free(sUtf8);
  22.         gtk_label_set_text(GTK_LABEL(pLabel), received);
  23. gtk_container_add(GTK_CONTAINER(scrollbar),pLabel);
  24. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
  25. gtk_box_pack_start(GTK_BOX(pBox), scrollbar2, TRUE, TRUE, 5);
  26. pTextView = gtk_text_view_new();
  27. gtk_container_add(GTK_CONTAINER(scrollbar2),pTextView);
  28. gtk_box_pack_start(GTK_BOX(scrollbar2), pTextView, FALSE, FALSE, 0);
  29. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar2), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
  30. pButton=gtk_button_new_with_label("Send" );
  31.    hbuttonbox = gtk_hbutton_box_new();
  32. gtk_box_pack_start(GTK_BOX(hbuttonbox),pButton,FALSE,FALSE,0);
  33.    gtk_container_add(GTK_CONTAINER (pBox), hbuttonbox);
  34. g_signal_connect(G_OBJECT(pButton), "clicked", G_CALLBACK(Sending), (gpointer) pTextView);
  35. gtk_widget_show_all(pWindow);
  36. gtk_main();

Reply

Marsh Posté le 08-05-2007 à 23:37:37   

Reply

Marsh Posté le 09-05-2007 à 00:04:27    

un label n'a pas de viewport, utilise add_with_viewport pour insérer le label.


Message édité par 0x90 le 09-05-2007 à 00:04:44

---------------
Me: Django Localization, Yogo Puzzle, Chrome Grapher, C++ Signals, Brainf*ck.
Reply

Marsh Posté le 09-05-2007 à 22:17:48    

Arf, je n'ai que des problèmes, j'ai tenté qq évolutions, mais plus rien ne marche. J'ai voulu modifier, de telle sorte que j'ai une scrollbar sur chaque zone de texte, donc 2 box dans une frame. Mais j'ai plein d'erreurs :/
 
Je fais qq de mal dans le code ?
 

Code :
  1. GtkWidget* pBox;
  2. GtkWidget* pBoxSending;
  3. GtkWidget* pFrame;
  4. GtkWidget* pColorBox;
  5. GtkWidget* pColorBox2;
  6. GtkWidget* pTextView;
  7. GtkWidget* pButton;
  8. GtkWidget *scrollbar;
  9. GtkWidget *scrollbar2;
  10. GtkWidget* pWindow;
  11. GtkWidget* hbuttonbox;
  12.      GtkWidget *pMenuBar;
  13. GtkItemFactory *pItemFactory;
  14.      GtkAccelGroup *pAccel;
  15. gtk_init(&argc, &argv);
  16. pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  17. gtk_window_set_default_size(GTK_WINDOW(pWindow), 320, 200);
  18. gtk_window_set_title(GTK_WINDOW(pWindow), "OBMSN" );
  19. g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(gtk_main_quit), NULL);
  20.      pAccel = gtk_accel_group_new ();
  21. pBox = gtk_vbox_new(FALSE, 5);
  22.     pFrame = gtk_frame_new("" );
  23.      pItemFactory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<main>", pAccel);
  24.      gtk_item_factory_create_items(pItemFactory, iNbMenuItem, MenuItem, (GtkWidget*)pWindow);
  25.      pMenuBar = gtk_item_factory_get_widget(pItemFactory, "<main>" );
  26.      gtk_box_pack_start(GTK_BOX(pBox), pMenuBar, FALSE, FALSE, 0);
  27.      gtk_window_add_accel_group(GTK_WINDOW(pWindow), pAccel);
  28. gtk_container_add(GTK_CONTAINER(pWindow),pBox);
  29.     gtk_box_pack_start(GTK_BOX(pBox), pFrame, FALSE, FALSE, 0);
  30. pBoxReceived = gtk_vbox_new(FALSE, 5);
  31. pBoxSending = gtk_vbox_new(FALSE, 5);
  32. gtk_container_add(GTK_CONTAINER(pFrame),pBoxReceived);
  33. gtk_container_add(GTK_CONTAINER(pFrame),pBoxSending);
  34. scrollbar = gtk_scrolled_window_new(NULL, NULL);
  35. scrollbar2 = gtk_scrolled_window_new(NULL, NULL);
  36. gtk_container_add(GTK_CONTAINER(pBoxReceived),scrollbar);
  37. gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrollbar), pBoxReceived);
  38. // la fonction utf8 permet d'afficher les caracteres accentues
  39. sUtf8 = g_locale_to_utf8(received, -1, NULL, NULL, NULL);
  40.    pLabel = gtk_label_new(sUtf8);
  41. g_free(sUtf8);
  42.         gtk_label_set_text(GTK_LABEL(pLabel), received);
  43. //  gtk_box_pack_start(GTK_BOX(pBoxSending), pLabel, FALSE, FALSE, 5);
  44. gtk_scrolled_window_add_with_viewport(pLabel,pBoxSending);
  45. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
  46. gtk_box_pack_start(GTK_BOX(pBoxSending), scrollbar2, TRUE, TRUE, 5);
  47. gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrollbar2), pBoxSending);
  48. pTextView = gtk_text_view_new();
  49. gtk_container_add(GTK_CONTAINER(scrollbar2),pTextView);
  50. gtk_box_pack_start(GTK_BOX(scrollbar2), pTextView, FALSE, FALSE, 0);
  51. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar2), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
  52. pButton=gtk_button_new_with_label("Send" );
  53.    hbuttonbox = gtk_hbutton_box_new();
  54. gtk_box_pack_start(GTK_BOX(hbuttonbox),pButton,FALSE,FALSE,0);
  55.    gtk_container_add(GTK_CONTAINER (pBox), hbuttonbox);
  56. g_signal_connect(G_OBJECT(pButton), "clicked", G_CALLBACK(Sending), (gpointer) pTextView);

Reply

Marsh Posté le 11-05-2007 à 16:30:09    

Personne ? :(

 

Je suis tjs encore bloqué à ce prob et ne comprends vraiment pas ce qu'il ya ? Ou bien qqn aurait une idée pour faire ce que je veux faire ? Cad un label avec scrollbar, une textview avec une autre scrollbar, et encore en dessous, un bouton. Ca ne doit pourtant pas etre trop difficile.

 

Pour l'instant j'ai une box dans une window, dans cette box j'ai une frame qui contient 2 box (pour chaque zone de texte) et un bouton. Apparemmetn ca ne convient pas :/


Message édité par Bourreau le 11-05-2007 à 16:36:39
Reply

Marsh Posté le 11-05-2007 à 17:19:26    

J'ai essayé de refaire une version plus light, mais tjs pas du tout ce que je veux :/

 

Soit mes 2 srollbar encadrent mon label, soit le programme ne compile pas (en fonction de mes modifs), pourtant je crois faire ce qu'il faut :(

 
Code :
  1. #include <stdlib.h>
  2. #include <gtk/gtk.h>
  3. #include "include.h"
  4. #include <pthread.h>
  5. #include <stdio.h>
  6. static char received[250];
  7. static GtkWidget *pLabel;
  8. static reply message;
  9. static char pseudo[20];
  10. static GtkWidget *pWindow;
  11. static gchar* sUtf8;
  12. void redraw(GtkWidget * widget)
  13. {
  14.     gtk_widget_queue_draw(GTK_WIDGET(widget));
  15. }
  16. void writing (char *pseudo_sender, char *msg_sender)
  17. {
  18. strcat(received,pseudo_sender);
  19. strcat(received," : " );
  20. strcat(received,msg_sender);
  21. strcat(received,"\n" );
  22. sUtf8 = g_locale_to_utf8(received, -1, NULL, NULL, NULL);
  23. //  gtk_label_set_label(pLabel,sUtf8);
  24. g_free(sUtf8);
  25. }
  26. void Sending (GtkWidget *pWidget, gpointer *data)
  27. {
  28. GtkWidget *pDialog;
  29. GtkWidget *pTextView;
  30. GtkTextBuffer* pTextBuffer;
  31. GtkTextIter iStart;
  32. GtkTextIter iEnd;
  33. gchar* sBuffer;
  34. pthread_t thread2;
  35. pTextView = GTK_WIDGET(data);
  36. pTextBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pTextView));
  37. gtk_text_buffer_get_start_iter(pTextBuffer, &iStart);
  38. gtk_text_buffer_get_end_iter(pTextBuffer, &iEnd);
  39. sBuffer = gtk_text_buffer_get_text(pTextBuffer, &iStart, &iEnd, TRUE);
  40. strcpy(message.pseudo,pseudo);
  41. strcpy(message.msg,sBuffer);
  42.  if (pthread_create(&thread2, NULL, client, &message))
  43.   fprintf(stderr, "error creating a new thread (client) \n" );
  44. strcat(received,message.pseudo);
  45. strcat(received," : " );
  46. strcat(received,sBuffer);
  47. strcat(received,"\n" );
  48. sUtf8 = g_locale_to_utf8(received, -1, NULL, NULL, NULL);
  49. //  gtk_label_set_label(pLabel,sUtf8);
  50. g_free(sUtf8);
  51. g_free(sBuffer);
  52. pthread_detach(thread2);
  53. }
  54. int main(int argc, char* argv[])
  55. {
  56. // Partie chat normal
  57. pthread_t thread1;
  58. int i;
  59. char txt[250];
  60. if(argc!=2){
  61.  printf("Usage: %s <nickname>\n", argv[0]);
  62.  return(-1);
  63. }
  64. strcpy(pseudo,argv[1]);
  65. if (pthread_create(&thread1, NULL, server, NULL/*(void *)(buf)*/)) {
  66.  fprintf(stderr, "error creating a new thread (server) \n" );
  67.    return (-1);
  68.  }
  69. // Partie GTK
  70. GtkWidget* pBox;
  71. GtkWidget* pBox1;
  72. GtkWidget* pBox2;
  73. GtkWidget* pTextView;
  74. GtkWidget* pButton;
  75. GtkWidget *scrollbar;
  76. GtkWidget *scrollbar2;
  77. GtkWidget* pWindow;
  78. GtkWidget* hbuttonbox;
  79. gtk_init(&argc, &argv);
  80. pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  81. gtk_window_set_default_size(GTK_WINDOW(pWindow), 320, 200);
  82. gtk_window_set_title(GTK_WINDOW(pWindow), "OBMSN" );
  83. g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(gtk_main_quit), NULL);
  84. pBox = gtk_vbox_new(FALSE, 5);
  85. pBox1 = gtk_vbox_new(FALSE, 5);
  86. pBox2 = gtk_vbox_new(FALSE, 5);
  87. gtk_container_add(GTK_CONTAINER(pWindow),pBox);
  88. scrollbar = gtk_scrolled_window_new(NULL, NULL);
  89. scrollbar2 = gtk_scrolled_window_new(NULL, NULL);
  90.  gtk_box_pack_start(GTK_BOX(pBox), pBox1, TRUE, TRUE, 5);
  91.  gtk_box_pack_start(GTK_BOX(pBox), pBox2, TRUE, TRUE, 5);
  92. gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW(scrollbar),pBox1);
  93. gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW(scrollbar2),pBox2);
  94. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
  95. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar2), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
  96. sUtf8 = g_locale_to_utf8(received, -1, NULL, NULL, NULL);
  97.    pLabel = gtk_label_new(sUtf8);
  98. g_free(sUtf8);
  99.         gtk_label_set_text(GTK_LABEL(pLabel), received);
  100. gtk_box_pack_start(GTK_BOX(pBox1),pLabel,FALSE,FALSE,5);
  101. pTextView = gtk_text_view_new();
  102. gtk_container_add(GTK_CONTAINER(pBox2),pTextView);
  103. pButton=gtk_button_new_with_label("Send" );
  104.    hbuttonbox = gtk_hbutton_box_new();
  105. gtk_box_pack_start(GTK_BOX(hbuttonbox),pButton,FALSE,FALSE,0);
  106.    gtk_container_add(GTK_CONTAINER (pBox), hbuttonbox);
  107. g_signal_connect(G_OBJECT(pButton), "clicked", G_CALLBACK(Sending), (gpointer) pTextView);
  108. gtk_widget_show_all(pWindow);
  109. gtk_main();
  110. return EXIT_SUCCESS;
  111. pthread_detach(thread1);
  112. pthread_exit(NULL);
  113. return 0;
  114. }


Message édité par Bourreau le 12-05-2007 à 12:59:35
Reply

Marsh Posté le 12-05-2007 à 16:06:28    

Je reste vraiment bloqué sur ce prob des scrollbar et je vois mon échéance arriver à grand pas, ya vraiment personne ? :(

Reply

Marsh Posté le 12-05-2007 à 19:18:20    

Personne ne peut me dire comment mettre 2 scrollbar verticales dans 1 fenetre ? :(

 

edit : j'ai compris que mon prob est le fait que mes child ont déja des parents. Je donne d'abord comme parent ma pBox et ensuite mes scrollbars. Par contre je ne vois pas comment y échapper :/


Message édité par Bourreau le 12-05-2007 à 19:39:58
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed