GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

MessageBox implementation

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> General GTK+ Discussion
Author Message
Bernard



Joined: 31 Jul 2008
Posts: 2

PostPosted: Thu Jul 31, 2008 1:04 am    Post subject: MessageBox implementation Reply with quote

Hi,
I'm currently porting a game originally written for Win32 to Linux. I am currently trying to implement MessageBox, and this is where GTK comes in.

Code: (C++)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

int MessageBox(HWND hwnd, const char* text, const char* caption, UINT type)
{
    GtkWidget *window = NULL;
    GtkWidget *dialog = NULL;
   
    gtk_init(&gtkArgc, &gtkArgv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(delete_event), NULL);
    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
     // gcallback calls gtk_main_quit()
   
gtk_init_add((GtkFunction)gcallback, NULL);
   
    if (type & MB_YESNO) {
        dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, text);
    } else {
        dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, text);
    }
   
    gtk_window_set_title(GTK_WINDOW(dialog), caption);
    gint result = gtk_dialog_run(GTK_DIALOG(dialog));
   
    gtk_main();
   
    gtk_widget_destroy(dialog);
   
    if (type & MB_YESNO) {
        switch (result) {
        default:
        case GTK_RESPONSE_DELETE_EVENT:
       
case GTK_RESPONSE_NO:
           
return IDNO;
            break;
        case GTK_RESPONSE_YES:
           
return IDYES;
            break;
        }
    }

    return IDOK;
}


Now know that I have little to no experience with GTK; I am probably doing a lot of stuff The Wrong Way - if so, tell me! My problem is that the MessageBox hangs around (not being redrawn) until the program exits. If MessageBox is called straight again, the first MessageBox disappears. I really don't know what I'm doing, so I humbly request help.

It's probably worth noting that the app is using SDL/OpenGL, and this is the only GTK code that appears or is required.

Thanks in advance.

EDIT:Looks like this is the wrong forum. Sorry!
Back to top
Lino
Familiar Face


Joined: 27 Dec 2007
Posts: 26

PostPosted: Thu Jul 31, 2008 5:12 am    Post subject: Reply with quote

try with this

Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

//---------------------------------------------------------------------------
int MessageBox(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
{
       MSGBOXPARAMS p={0};

       p.dwStyle = uType;
       p.lpszText = lpText;
       p.hwndOwner = hWnd;
       p.lpszCaption = lpCaption;
    return MessageBoxIndirect(&p);
}
//---------------------------------------------------------------------------
int MessageBoxIndirect(LPMSGBOXPARAMS p)
{
    GtkWidget *w;
    gint res;
    GtkMessageType type;
    GtkButtonsType btnType;
    GtkImage *img;

    if(p->dwStyle & MB_ICONERROR)
        type = GTK_MESSAGE_ERROR;
    else if(p->dwStyle & MB_ICONQUESTION)
        type = GTK_MESSAGE_QUESTION;
    else if(p->dwStyle & MB_ICONWARNING)
        type = GTK_MESSAGE_WARNING;
    else
        type = GTK_MESSAGE_INFO;
    if(p->dwStyle & MB_YESNO)
        btnType = GTK_BUTTONS_YES_NO;
    else
        btnType = GTK_BUTTONS_OK;
    w = gtk_message_dialog_new((GtkWindow *)p->hwndOwner,GTK_DIALOG_MODAL,type,btnType,p->lpszText);
    gtk_window_set_title((GtkWindow *)w,p->lpszCaption);
    if(p->lpszIcon != NULL){
        img = (GtkImage *)gtk_image_new_from_pixbuf(LoadIcon(p->hInstance,p->lpszIcon,32,32,0));
        gtk_widget_show((HWND)img);
        gtk_message_dialog_set_image((GtkMessageDialog *)w,(GtkWidget *)img);
    }
    //gtk_message_dialog_format_secondary_text((GtkMessageDialog *)w,lpText);
    gtk_widget_show (w);
    res = gtk_dialog_run((GtkDialog *)w);
    gtk_widget_destroy(w);
    switch(res){
        case GTK_RESPONSE_OK:
            return IDOK;
        case GTK_RESPONSE_NO:
            return IDNO;
        case GTK_RESPONSE_YES:
            return IDYES;
    }
    return 0;
}
Back to top
Bernard



Joined: 31 Jul 2008
Posts: 2

PostPosted: Thu Jul 31, 2008 8:17 am    Post subject: Reply with quote

Hey, thanks for your reply.

hwnd is never a valid value, so your example as written segfaults. I replaced it for NULL, and the same behaviour as mine occurs. For what it's worth, when I use a non-modal dialog it seems to work okay, but I just couldn't figure out how to get the results out of it. I checked the gtk docs but couldn't find it.

EDIT:

Nevermind, I got it.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> General GTK+ Discussion All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP