You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.4KB

  1. /* PawfalYesNo Fltk Dialog
  2. * Copyleft (C) 2001 Dan Bethell <dan@pawfal.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include "PawfalYesNo.h"
  19. #include <FL/fl_draw.H>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <stdarg.h>
  24. bool Pawfal_YesNo(const char *a,...)
  25. {
  26. va_list ap;
  27. va_start(ap, a);
  28. char buffer[1024];
  29. if (!strcmp(a,"%s")) {
  30. strcpy(buffer,va_arg(ap, const char*));
  31. } else {
  32. //: matt: MacOS provides two equally named vsnprintf's...
  33. ::vsnprintf(buffer, 1024, a, ap);
  34. }
  35. va_end(ap);
  36. PawfalYesNo pi(300, 100,buffer);
  37. if (pi.go()) return true;
  38. return false;
  39. }
  40. PawfalYesNo::PawfalYesNo(int w, int h, const char *label):
  41. Fl_Window(w,h,"Question"),
  42. lbl(new Fl_Box(50, 15, w-60,20, label)),
  43. keyhit(false),
  44. result(false)
  45. {
  46. lbl->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
  47. lbl->labelfont(FL_SCREEN_BOLD);
  48. lbl->labelsize(12);
  49. int message_w, message_h;
  50. fl_font(FL_SCREEN_BOLD, 12);
  51. message_w = message_h = 0;
  52. fl_measure(label, message_w, message_h);
  53. resize(x(),y(),message_w+60,h);
  54. w=message_w+60;
  55. yes = new Fl_Button(w-120, h-25, 50, 20, "yes");
  56. no = new Fl_Button(w-60, h-25, 50, 20, "no");
  57. yes->labelfont(FL_SCREEN_BOLD);
  58. yes->labelsize(12);
  59. no->labelfont(FL_SCREEN_BOLD);
  60. no->labelsize(12);
  61. logo = new Fl_Pixmap(tv_xpm);
  62. end();
  63. set_modal();
  64. //clear_border();
  65. }
  66. PawfalYesNo::~PawfalYesNo()
  67. {
  68. delete logo;
  69. }
  70. int PawfalYesNo::handle(int e)
  71. {
  72. int result = Fl_Window::handle(e);
  73. if (e==FL_KEYBOARD)
  74. {
  75. if (Fl::event_key()==FL_Escape||Fl::event_key()==FL_Enter)
  76. keyhit=true;
  77. }
  78. return result;
  79. }
  80. void PawfalYesNo::draw()
  81. {
  82. Fl_Window::draw();
  83. logo->draw(10, 10);
  84. }
  85. void PawfalYesNo::ok_cb(Fl_Button *b, void *d)
  86. {
  87. assert(b!=NULL);
  88. PawfalYesNo *ptr = (PawfalYesNo *)d;
  89. assert(ptr!=NULL);
  90. }
  91. void PawfalYesNo::cancel_cb(Fl_Button *b, void *d)
  92. {
  93. assert(b!=NULL);
  94. PawfalYesNo *ptr = (PawfalYesNo *)d;
  95. assert(ptr!=NULL);
  96. }
  97. bool PawfalYesNo::go()
  98. {
  99. result = false;
  100. show();
  101. for (;;)
  102. {
  103. Fl_Widget *o = Fl::readqueue();
  104. if (!o&&!keyhit) Fl::wait();
  105. else if (keyhit)
  106. {
  107. int key = Fl::event_key();
  108. if (key==FL_Escape)
  109. {
  110. result = false;
  111. break;
  112. }
  113. if (key==FL_Enter)
  114. {
  115. result=true;
  116. break;
  117. }
  118. }
  119. else if (o == yes) {result = true; break;}
  120. else if (o == no) {result = false; break;}
  121. }
  122. hide();
  123. return result;
  124. }