MozillaZine


XUL alert box with Yes/No instead of OK/Cancel?

Talk about add-ons and extension development.
ericjung
 
Posts: 846
Joined: August 4th, 2003, 9:32 am

Post Posted March 17th, 2005, 11:58 am

Hi,
I use <code>nsiPromptService.confirm()</code> to prompt the user often, but the buttons "OK" and "Cancel" aren't really appropriate. The appropriate answers are always "Yes" or "No". Is there any way to display a prompt with Yes/No buttons? The <dialog/> element doesn't seem to be much help -- there doesn't appear to be a way to change the text of the buttons it displays.

Is the only solution to create my own <window/> with <button/> elements in a new XUL file???

Thank you for any advice,
grimholtz

asqueella
 
Posts: 4019
Joined: November 16th, 2003, 3:05 am
Location: Russia, Moscow

Post Posted March 17th, 2005, 12:27 pm

nsIPromptService::confirmEx is the appropriate way to do this. You can see an example in QuickNote (current dev, quicknote.js, note delete confirmation).

BTW, you can change dialog buttons' captions

ericjung
 
Posts: 846
Joined: August 4th, 2003, 9:32 am

Post Posted March 17th, 2005, 1:10 pm

Interesting. I had tried confirmEx() but always got a prompt with a checkbox. I'll take a look at your code. Thanks.

asqueella
 
Posts: 4019
Joined: November 16th, 2003, 3:05 am
Location: Russia, Moscow

Post Posted March 17th, 2005, 1:22 pm

Just pass null for checkMsg parameter and the checkbox won't show. For you and those who may find this thread in future, here's the code that makes a dialog with customized button labels, but without teh checkbox:

Code: Select all
var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
         .getService(Components.interfaces.nsIPromptService);
var rv = ps.confirmEx(window, "title", "text",
                          ps.BUTTON_TITLE_IS_STRING * ps.BUTTON_POS_0 +
                          ps.BUTTON_TITLE_IS_STRING * ps.BUTTON_POS_1,
                          "Yes", "No", null, null, {});

ericjung
 
Posts: 846
Joined: August 4th, 2003, 9:32 am

Post Posted March 17th, 2005, 1:37 pm

Follow up: don't check that
Code: Select all
rv == ps.BUTTON_TITLE_YES || rv == ps.BUTTON_TITLE_NO
You have to explicitly check if rv equals 0 (first button was clicked) or 1 (second button was clicked). There doesn't appear to be constants for first/second/n button.

Ihoss

User avatar
 
Posts: 376
Joined: July 4th, 2004, 11:11 am

Post Posted March 17th, 2005, 1:45 pm

Have a look at my tutorials in my signature or click here: My extensions and tutorials

asqueella
 
Posts: 4019
Joined: November 16th, 2003, 3:05 am
Location: Russia, Moscow

Post Posted March 17th, 2005, 1:51 pm

Clarification: BUTTON_TITLE_* set title for given button when passed in buttonFlags. You pass a sum of products of form (BUTTON_TITLE_.. * BUTTON_POS_..). BUTTON_TITLE_* sets title for the button specified with BUTTON_POS_*.

ericjung
 
Posts: 846
Joined: August 4th, 2003, 9:32 am

Post Posted March 17th, 2005, 1:59 pm

IHoss, excellent tutorial. I wish you would have contributed it to the Example Code Knowledge Base, though!! :(

asqueella
 
Posts: 4019
Joined: November 16th, 2003, 3:05 am
Location: Russia, Moscow

Post Posted March 17th, 2005, 2:07 pm

That's what I was going to say. I've added it to that page already.

Ihoss

User avatar
 
Posts: 376
Joined: July 4th, 2004, 11:11 am

Post Posted March 17th, 2005, 2:34 pm

I tried asking somewhere else about how I can add pages to the knowledge base but I got no reply. Thats why I haven't added it.

asqueella
 
Posts: 4019
Joined: November 16th, 2003, 3:05 am
Location: Russia, Moscow

Post Posted March 17th, 2005, 2:40 pm

It's easy. First, you need to register, then log in (it's all done on the login page linked from top-right corner).

Then to edit a page, click the [edit] link at the top of the page. To add a new article, add a link to it from an existing page (by typing [[Article name]]), then save the page and follow the (red) link.

Ihoss

User avatar
 
Posts: 376
Joined: July 4th, 2004, 11:11 am

Post Posted March 17th, 2005, 2:43 pm

aha, cool! Thanks, ill do that now then :)

Thanks for the comments too btw. If you find any spelling mistakes (more than likely) just give me a nudge and I'll fix it :)

asqueella
 
Posts: 4019
Joined: November 16th, 2003, 3:05 am
Location: Russia, Moscow

Post Posted March 17th, 2005, 2:59 pm

What's cool about wiki is that others fix the minor mistakes for you (well, eventually they do).

em_te

User avatar
 
Posts: 361
Joined: June 13th, 2004, 1:03 am

Post Posted March 20th, 2005, 3:24 am

asqueella wrote:Clarification: BUTTON_TITLE_* set title for given button when passed in buttonFlags. You pass a sum of products of form (BUTTON_TITLE_.. * BUTTON_POS_..). BUTTON_TITLE_* sets title for the button specified with BUTTON_POS_*.

Do the ps.BUTTON_POS_X flags for button positions vary from platform to platform? What if I want the "yes" button on the left on Windows and on the right for the Mac?

Ihoss

User avatar
 
Posts: 376
Joined: July 4th, 2004, 11:11 am

Post Posted March 20th, 2005, 4:30 am

I don't think you can do that. Linux has it opposite to windows and I don't think you can change it. If you say "flag = 4*256+3*1;" then it will take the sum and not care what order it is in. It will put the yes and no where it is apropriate. The only way to change it would be to set the title of the buttons as strings with locale values.

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 0 guests