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.

156 lines
3.8KB

  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. #if !__APPLE__
  37. PawfalYesNo pi(300, 100,buffer);
  38. if (pi.go()) return true;
  39. #else
  40. Str255 copy;
  41. strncpy((char*)copy + 1, buffer, 253);
  42. copy[0] = strlen((char*)copy+1);
  43. AlertStdAlertParamRec rec = { true, false, NULL,
  44. (StringPtr)-1, (StringPtr)-1, NULL,
  45. kAlertStdAlertOKButton, 0, kWindowAlertPositionParentWindowScreen };
  46. SInt16 ret = 0;
  47. StandardAlert(kAlertCautionAlert, copy, NULL, &rec, &ret);
  48. return ret == kAlertStdAlertOKButton;
  49. #endif
  50. return false;
  51. }
  52. PawfalYesNo::PawfalYesNo(int w, int h, const char *label):
  53. Fl_Window(w,h,"Question"),
  54. lbl(new Fl_Box(50, 15, w-60,20, label)),
  55. keyhit(false),
  56. result(false)
  57. {
  58. lbl->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
  59. lbl->labelfont(FL_SCREEN_BOLD);
  60. lbl->labelsize(12);
  61. int message_w, message_h;
  62. fl_font(FL_SCREEN_BOLD, 12);
  63. message_w = message_h = 0;
  64. fl_measure(label, message_w, message_h);
  65. resize(x(),y(),message_w+60,h);
  66. w=message_w+60;
  67. yes = new Fl_Button(w-120, h-25, 50, 20, "yes");
  68. no = new Fl_Button(w-60, h-25, 50, 20, "no");
  69. yes->labelfont(FL_SCREEN_BOLD);
  70. yes->labelsize(12);
  71. no->labelfont(FL_SCREEN_BOLD);
  72. no->labelsize(12);
  73. logo = new Fl_Pixmap(tv_xpm);
  74. end();
  75. set_modal();
  76. //clear_border();
  77. }
  78. PawfalYesNo::~PawfalYesNo()
  79. {
  80. delete logo;
  81. }
  82. int PawfalYesNo::handle(int e)
  83. {
  84. int result = Fl_Window::handle(e);
  85. if (e==FL_KEYBOARD)
  86. {
  87. if (Fl::event_key()==FL_Escape||Fl::event_key()==FL_Enter)
  88. keyhit=true;
  89. }
  90. return result;
  91. }
  92. void PawfalYesNo::draw()
  93. {
  94. Fl_Window::draw();
  95. logo->draw(10, 10);
  96. }
  97. void PawfalYesNo::ok_cb(Fl_Button *b, void *d)
  98. {
  99. assert(b!=NULL);
  100. PawfalYesNo *ptr = (PawfalYesNo *)d;
  101. assert(ptr!=NULL);
  102. }
  103. void PawfalYesNo::cancel_cb(Fl_Button *b, void *d)
  104. {
  105. assert(b!=NULL);
  106. PawfalYesNo *ptr = (PawfalYesNo *)d;
  107. assert(ptr!=NULL);
  108. }
  109. bool PawfalYesNo::go()
  110. {
  111. result = false;
  112. show();
  113. for (;;)
  114. {
  115. Fl_Widget *o = Fl::readqueue();
  116. if (!o&&!keyhit) Fl::wait();
  117. else if (keyhit)
  118. {
  119. int key = Fl::event_key();
  120. if (key==FL_Escape)
  121. {
  122. result = false;
  123. break;
  124. }
  125. if (key==FL_Enter)
  126. {
  127. result=true;
  128. break;
  129. }
  130. }
  131. else if (o == yes) {result = true; break;}
  132. else if (o == no) {result = false; break;}
  133. }
  134. hide();
  135. return result;
  136. }