Voir un article

illustration

© Tito

Amiga Aros : simple translate GTK to MUI

It's possible to translate some GTK calls to MUI calls without the gtk-mui and glib and gobjects libs. I wrote a partial (only GTK calls from helloworld.gtk.c example) \"C header translator\" for test it ...
1/ get "hello world in GTK" :
http://developer.gnome.org/gtk-tutorial/2.90/c39.html

this is the original source code of this example (without comment lines) :

#include <gtk/gtk.h>

static void hello( GtkWidget *widget, gpointer   data ) { g_print ("Hello Worldn"); }
static gboolean delete_event( GtkWidget *widget,  GdkEvent  *event, gpointer   data )  { g_print ("delete event occurredn"); return TRUE;}
static void destroy( GtkWidget *widget, gpointer   data ) {    gtk_main_quit (); }

int main( int   argc, char *argv[] ) {
  
       GtkWidget *window ;
       GtkWidget *button ;
      
       gtk_init (&argc, &argv);
          
       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);      

       g_signal_connect (window, "delete-event",   G_CALLBACK (delete_event), NULL);       
       g_signal_connect (window, "destroy",  G_CALLBACK (destroy), NULL);
       
      gtk_container_set_border_width (GTK_CONTAINER (window), 100);
       
      button = gtk_button_new_with_label ("Hello World");

    g_signal_connect (button, "clicked",  G_CALLBACK (hello), NULL);
    g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy),  window);
        
     gtk_container_add (GTK_CONTAINER (window), button);
       
     gtk_widget_show (button);       
       gtk_widget_show (window);
       
       gtk_main ();
                
       return 0;
}




2/ for the test, please comment the include gtk.h line and add the green lines (my custom direct wrapper) as below  :

// #include <gtk/gtk.h>


// --- GTK 2 MUI direct wrapper v0.1 - Tito of Amigang - Amigang.fr

#include <stdio.h>
#include <stdlib.h>

#include <exec/types.h>
#include <libraries/mui.h>
 
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/muimaster.h>
#include <clib/alib_protos.h>
 
#define GTK_WINDOW_TOPLEVEL 1

#define G_CALLBACK(f)  (f)
#define GTK_CONTAINER(f) (Object *)XGET(f,WindowContents)

Object* app,  *win,* ptrtmp;

struct {int (*ptr)();} tab[256];  // functions ref stack (direct hooks)
unsigned short int tabidx=1; // 0 is reserved

typedef Object  GtkWidget;
typedef struct MUIP_HandleEvent GdkEvent;
typedef char* gpointer;
typedef BOOL  gboolean;

#define g_print(s) printf("%s",s)

#define gtk_widget_destroy  gtk_main_quit
void gtk_main_quit() {MUI_DisposeObject(app);exit(0);}

#define gtk_init(c,v) { app = ApplicationObject, End;   if ( !app) {printf("error app muin"); exit(1);}}

#define gtk_window_new(mode)  ptrtmp=WindowObject, WindowContents,VGroup, End,End; if (ptrtmp) { DoMethod(app,OM_ADDMEMBER,ptrtmp); }

#define gtk_widget_show(a) set(a,MUIA_Window_Open,TRUE);

#define gtk_container_set_border_width(o,n) set(o,MUIA_InnerTop,n)

#define gtk_container_add(o,b) DoMethod(o,OM_ADDMEMBER,b)

#define gtk_button_new_with_label(l) SimpleButton(l)

#define  g_signal_connect_swapped    g_signal_connect

// --- connect
void g_signal_connect( Object* objet , char *eventType, APTR fonctionHook , Object* pointeur )  {
  // --- delete destroy
   if (strcmp(eventType, "delete-event")==0 || strcmp(eventType, "destroy")==0) {
 DoMethod(objet,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,(IPTR)app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  }
  // ---  clicked    
   else if (strcmp(eventType, "clicked")==0){
    tab[tabidx].ptr=fonctionHook; // stack the function ref
  DoMethod(objet,MUIM_Notify,MUIA_Pressed,FALSE,(IPTR)app,2,MUIM_Application_ReturnID,tabidx);
    tabidx++;
   }
}

void gtk_main() {
      ULONG sigs = 0;
      ULONG rc;   
      unsigned short quit=0;
   while (!quit) {
     rc=DoMethod(app,MUIM_Application_NewInput,&sigs);
     if (rc==MUIV_Application_ReturnID_Quit) quit=1;
     if (rc>0 && rc<tabidx) { (tab[rc].ptr)();} // call function (direct hook without mui hook struct)
     if (sigs) {      
         sigs = Wait(sigs | SIGBREAKF_CTRL_C);
         if (sigs & SIGBREAKF_CTRL_C) break;
      }
     }
   MUI_DisposeObject(app);     
}

// GTK helloWorld.c original source code :

static void hello( GtkWidget *widget, gpointer   data ) { g_print ("Hello Worldn"); }
static gboolean delete_event( GtkWidget *widget,  GdkEvent  *event, gpointer   data )  { g_print ("delete event occurredn"); return TRUE;}
static void destroy( GtkWidget *widget, gpointer   data ) {    gtk_main_quit (); }

int main( int   argc, char *argv[] ) {
  
       GtkWidget *window ;
       GtkWidget *button ;
      
       gtk_init (&argc, &argv);
          
       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);      

       g_signal_connect (window, "delete-event",   G_CALLBACK (delete_event), NULL);       
       g_signal_connect (window, "destroy",  G_CALLBACK (destroy), NULL);
       
      gtk_container_set_border_width (GTK_CONTAINER (window), 100);
       
      button = gtk_button_new_with_label ("Hello World");

    g_signal_connect (button, "clicked",  G_CALLBACK (hello), NULL);
    g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy),  window);
        
     gtk_container_add (GTK_CONTAINER (window), button);
       
     gtk_widget_show (button);       
       gtk_widget_show (window);
       
       gtk_main ();
                
       return 0;
}



And now, compile it with MUI lib only. On Aros :
gcc -o exe thistest.c -lmui

4 964 clics - Créé le 11/08/2011 par Tito - Modifié le 25/08/2011



Réagissez, commentez, discutez ...

Soyez le premier à réagir !



Partager ?

diaspora  G+  facebook  twitter  Digg  Yahoo  Delicious  Technorati  myspace


Voir d'autres articles en rapport avec celui-ci ?



Stats des clics sur cet article : cliquez ici »



Vous voulez contribuer et publier un article dans cette rubrique ?

Merci de vous identifier ou de vous créer un compte si ce n'est pas déjà fait.