Le 29/01/2006, Ilkka Lehtoranta a écrit:
>> I have a multicolumn List object to display the result of a SQL request
>> done with SQLite. Everything is right with my hooks and now I want to add
>> title to the columns. Reading the autodoc file, the title naming seems to
>> be quite static. The problem is I obtain the column names dynamically.
> Titles are set in the display hook. MUIM_InitChange/ExitChange probably
> does the trick here (call it once you have received titles).
Titles are set in the display hook but we can't give our own data structure
to this function because if we set MUIA_List_Title to TRUE (means
multicolumn title), MUI calls the display hook with a NULL value at the
third parameter.
I don't understand what I have to do with MUIM_Group_InitChange/ExitChange
... :-(
The autodoc tells about this line " struct Data *data = hook->h_Data; " but
the data variable is not used in the example ...
You can have a look at what I do here :
http://amigadev.free.fr/sqlite/AP34_03.jpg
and the source :
http://amigadev.free.fr/sqlite/AP34_03.c (attached to this mail too)
--
Mathias PARNAUDEAU
Visit http://www.amiga.dk/tumult for MUI-related
information, especially about MUI custom classes.
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/MUI/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
/*
* ap34_02.c
*
* Affichage des résultats de requêtes grâce aux classes List/Listview
*
* - GCC MorphOS
* gcc -I. -L. -o ap34_ex03 ap34_ex03.c -lsqlite3
*
*/
#include <stdio.h>
#include <string.h>
#include <libraries/mui.h>
#include <proto/muimaster.h>
#include <proto/exec.h>
#include <clib/alib_protos.h>
#include <mui/TextEditor_mcc.h>
#include <SDI_hook.h>
#include <sqlite3.h>
#define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 |
(ULONG) (d))
struct Library *MUIMasterBase = NULL;
struct IntuitionBase *IntuitionBase = NULL;
#define MUIA_Application_UsedClasses 0x8042e9a7 /* V20 STRPTR * i..
*/
static char *ClassList[] =
{
"TextEditor.mcc",
NULL
};
static Object *app = NULL;
static Object *window = NULL;
static Object *txt_result = NULL;
static sqlite3 *db = NULL;
static int print_col_names = 1;
Object *lv_result, *lst_result;
struct ReqData {
int nb;
char **columns;
};
#define APPAUTHOR "Corto"
#define APPBASE "BASE"
#define APPTITLE "SQLite "
#define APPVERSION "$VER title 1.00 (28/01/2006)"
#define APPCOPYRIGHT "Copyright"
#define APPDESCRIPTION "Intégration de la bibliothèque libsqlite3 dans un
programme graphique"
#define WINTITLE "SQLite3 : Test d'intégration de la
bibilothèque"
/****************** Partie concernant les hooks de gestion de la liste
*******************/
HOOKPROTONH(ResultList_Construct, ULONG, APTR pool, struct ReqData *reqdata)
{
struct ReqData *newreqdata;
int n;
newreqdata = AllocPooled(pool, sizeof(struct ReqData));
if (newreqdata){
newreqdata->nb = reqdata->nb;
newreqdata->columns = AllocPooled(pool, reqdata->nb *
sizeof(char *));
for (n=0 ; n<reqdata->nb ; n++){
if (reqdata->columns[n]){
newreqdata->columns[n] = AllocPooled(pool,
strlen(reqdata->columns[n]) + 1);
strcpy(newreqdata->columns[n],
reqdata->columns[n]);
}else{
newreqdata->columns[n] = AllocPooled(pool, 5);
strcpy(newreqdata->columns[n], "NULL");
}
}
}
return (ULONG)newreqdata;
}
MakeStaticHook(hook_ResultList_Construct, ResultList_Construct);
HOOKPROTONH(ResultList_Destruct, ULONG, APTR pool, struct ReqData *reqdata)
{
int n;
for (n=0 ; n<reqdata->nb ; n++){
FreePooled(pool, reqdata->columns[n],
strlen(reqdata->columns[n]) + 1);
}
FreePooled(pool, reqdata->columns, reqdata->nb * sizeof(char *));
FreePooled(pool, reqdata, sizeof(struct ReqData));
return 0;
}
MakeStaticHook(hook_ResultList_Destruct, ResultList_Destruct);
HOOKPROTONH(ResultList_Display, ULONG, char **array, struct ReqData *reqdata)
{
int n;
for (n=0; n<reqdata->nb ; n++){
*array++ = reqdata->columns[n];
}
return 0;
}
MakeStaticHook(hook_ResultList_Display, ResultList_Display);
/****************** Suite du programme *******************************/
static int DisplayResults(void *NotUsed, int argc, char **argv, char
**azColName){
int i;
struct ReqData reqdata;
char str_format[64];
// Construction dynamique de la chaine qui décrit le nombre de colonnes
for (i=0 ; i<argc-1 ; i++){
str_format[i] = ',';
}
str_format[i] = 0;
set(lst_result, MUIA_List_Format, str_format);
// Au tout premier appel de cette fonction, on affiche le nom des
colonnes
if (print_col_names==1){
reqdata.nb = argc;
reqdata.columns = azColName;
DoMethod(lst_result, MUIA_List_Title, TRUE);
DoMethod(lst_result, MUIM_List_InsertSingle, &reqdata,
MUIV_List_Insert_Bottom);
print_col_names = 0;
}
reqdata.nb = argc;
reqdata.columns = argv;
DoMethod(lst_result, MUIM_List_InsertSingle, &reqdata,
MUIV_List_Insert_Bottom);
return 0;
}
/*
* Fonction du hook appelé lors de l'appui sur le bouton "Execute requete".
* La requete saisie est exécutée par sqlite3_exec() qui appelle la fonction
ci-dessus
* destinée à présenter les résultats, pour l'instant sous forme de texte.
*/
//static ULONG hook_ExecuteRequestFunc(struct Hook *hook, APTR obj, struct
TagItem *tag_list)
HOOKPROTONH(hook_ExecuteRequestFunc, ULONG, APTR obj, struct TagItem *tag_list)
{
char *request;
int rc;
char *zErrMsg = 0;
request = (char *)DoMethod(obj, MUIM_TextEditor_ExportText);
if (request){
DoMethod(txt_result, MUIM_TextEditor_ClearText);
DoMethod(lst_result, MUIM_List_Clear);
// Transmission et exécution de la requête SQL
print_col_names = 1;
rc = sqlite3_exec (db, request, DisplayResults, lst_result,
&zErrMsg);
if( rc!=SQLITE_OK ){
DoMethod(txt_result, MUIM_TextEditor_InsertText, "SQL
error :\n");
DoMethod(txt_result, MUIM_TextEditor_InsertText,
zErrMsg);
}
FreeVec(request);
}
return TRUE;
}
MakeStaticHook(hook_ExecuteRequest, hook_ExecuteRequestFunc);
Object * OpenMainWindow(void)
{
Object *txt_request, *bt_execute;
/* Description de l'interface */
app = (Object *)ApplicationObject,
MUIA_Application_Author, APPAUTHOR,
MUIA_Application_Base, APPBASE,
MUIA_Application_Title, APPTITLE,
MUIA_Application_Version, APPVERSION,
MUIA_Application_Copyright, APPCOPYRIGHT,
MUIA_Application_Description, APPDESCRIPTION,
MUIA_Application_HelpFile, NULL,
MUIA_Application_UsedClasses, ClassList,
SubWindow, window = WindowObject,
MUIA_Window_Title, WINTITLE,
MUIA_Window_ID, MAKE_ID('W', 'I', 'N', '1'),
WindowContents, VGroup,
/* Tree of components here ... */
Child, txt_request = TextEditorObject,
End,
Child, bt_execute = KeyButton("Execute
requete", 'e'),
Child, lv_result = ListviewObject,
MUIA_Listview_List, lst_result =
ListObject,
MUIA_Frame,
MUIV_Frame_InputList,
MUIA_List_ConstructHook,
&hook_ResultList_Construct,
MUIA_List_DestructHook,
&hook_ResultList_Destruct,
MUIA_List_DisplayHook,
&hook_ResultList_Display,
End,
End,
Child, txt_result = TextEditorObject,
MUIA_TextEditor_ReadOnly, TRUE,
MUIA_Weight, 40,
End,
End,
End,
End;
if (app){
/* On fixe quelques valeurs et notifications */
DoMethod(window,
MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
app, 2,
MUIM_Application_ReturnID,
MUIV_Application_ReturnID_Quit);
DoMethod(bt_execute, MUIM_Notify, MUIA_Pressed, FALSE,
txt_request, 2, MUIM_CallHook,
&hook_ExecuteRequest);
SetAttrs(window, MUIA_Window_Open, TRUE, TAG_END);
set(window, MUIA_Window_ActiveObject, txt_request);
}
return app;
}
/*
* Initialisation et vérification de tout ce qui est nécessaire à la bonne
exécution
* de l'application : ouverture des bibliothèques, test de présence des classes
MCC, ...
*/
int Initialize(void)
{
int res = 1;
Object *texted;
IntuitionBase = (struct IntuitionBase
*)OpenLibrary("intuition.library", 39L);
MUIMasterBase = OpenLibrary(MUIMASTER_NAME, MUIMASTER_VMIN);
if (IntuitionBase == NULL){
printf("Impossible d'ouvrir 'intuition.library' V39\n");
res = 0;
}
if (MUIMasterBase == NULL){
printf("Impossible d'ouvrir '%s' V%d\n", MUIMASTER_NAME,
MUIMASTER_VMIN);
res = 0;
}
texted = TextEditorObject, End;
if (texted == NULL){
printf("Classe TextEditor manquante\n");
res = 0;
}
MUI_DisposeObject(texted);
return res;
}
/*
* Fermeture et libération de tout ce qui a été initialisé au démarrage.
*/
void DeInitialize(void)
{
CloseLibrary(MUIMasterBase);
CloseLibrary((struct Library *)IntuitionBase);
}
/*
* Programme principal : il appelle les initialisations, ouvre la fenêtre puis
gère
* les événements jusqu'à ce qu'on ferme l'application, condition de libération
* des ressources
*/
int main(int argc, char **argv)
{
int res = 0;
int rc;
if (argc != 2){
fprintf(stderr, "Usage : %s DATABASE\n", argv[0]);
exit(1);
}
rc = sqlite3_open (argv[1], &db);
if( rc ){
fprintf(stderr, "Impossible d'ouvrir la base de donnes : %s\n",
sqlite3_errmsg(db));
exit(1);
}
if (Initialize()){
app = OpenMainWindow();
if (app){
/* Boucle de gestion des évènements, toujours la même */
ULONG sigs = 0;
while (DoMethod(app,MUIM_Application_NewInput,&sigs) !=
MUIV_Application_ReturnID_Quit)
{
if (sigs)
{
sigs = Wait(sigs | SIGBREAKF_CTRL_C);
if (sigs & SIGBREAKF_CTRL_C) break;
}
}
/* Libération des ressources et fermeture */
set(window, MUIA_Window_Open, FALSE);
MUI_DisposeObject(app);
}else{
res = 2;
}
}else{
res = 1;
}
DeInitialize();
// Fermeture de la base avec laquelle on a fini de travailler
sqlite3_close (db);
return res;
}