source: CMT/v1r18p20041201/source/install_utils.cxx @ 1

Last change on this file since 1 was 1, checked in by arnault, 19 years ago

Import all tags

File size: 16.9 KB
Line 
1//-----------------------------------------------------------
2// Copyright Christian Arnault LAL-Orsay CNRS
3// arnault@lal.in2p3.fr
4// See the complete license in cmt_license.txt "http://www.cecill.info".
5//-----------------------------------------------------------
6
7//---------------------------------------------------------------------
8#include "install_utils.h"
9#include <windowsx.h>
10#include "install_resource.h"
11//---------------------------------------------------------------------
12
13//---------------------------------------------------------------------
14WWindow::WWindow ()
15{
16  _window = NULL;
17}
18
19void WWindow::enable ()
20{
21  EnableWindow (_window, TRUE);
22}
23
24void WWindow::disable ()
25{
26  EnableWindow (_window, FALSE);
27}
28//---------------------------------------------------------------------
29
30//---------------------------------------------------------------------
31WEdit::WEdit ()
32{
33  _edit = NULL;
34}
35
36WEdit::WEdit (HWND edit)
37{
38  _edit = edit;
39  _window = _edit;
40}
41
42WEdit::WEdit (HWND container, int id)
43{
44  _edit = GetDlgItem (container, id);
45  _window = _edit;
46}
47
48void WEdit::init (HWND container, int id)
49{
50  _edit = GetDlgItem (container, id);
51  _window = _edit;
52}
53
54WEdit& WEdit::operator = (HWND edit)
55{
56  _edit = edit;
57  _window = _edit;
58  return (*this);
59}
60
61void WEdit::set (const char* text)
62{
63  int status = SendMessage (_edit, WM_SETTEXT, (WPARAM) 0, (LPARAM) text);
64}
65
66void WEdit::set (const cmt_string& text)
67{
68  set (text.c_str ());
69}
70
71void WEdit::clear ()
72{
73  set ("");
74}
75
76cmt_string WEdit::get () const
77{
78  int size = SendMessage (_edit, WM_GETTEXTLENGTH, (WPARAM) 0, (LPARAM) 0);
79
80  cmt_string text (size);
81
82  SendMessage (_edit, WM_GETTEXT, (WPARAM) size + 1, (LPARAM) text.c_str ());
83
84  return (text);
85}
86
87void WEdit::copy (const WEdit& other)
88{
89  clear ();
90  set (other.get ());
91}
92//---------------------------------------------------------------------
93
94//---------------------------------------------------------------------
95WList::WList ()
96{
97  _list = NULL;
98  _selected_index = -1;
99  _width = 0;
100}
101
102WList::WList (HWND list)
103{
104  _list = list;
105  _window = _list;
106  _width = get_width ();
107  set_extent ();
108  get_selected_index ();
109}
110
111WList::WList (HWND container, int id)
112{
113  _list = GetDlgItem (container, id);
114  _window = _list;
115  _width = get_width ();
116  set_extent ();
117  get_selected_index ();
118}
119
120void WList::init (HWND container, int id)
121{
122  _list = GetDlgItem (container, id);
123  _window = _list;
124  _width = get_width ();
125  set_extent ();
126  get_selected_index ();
127}
128
129WList& WList::operator = (HWND list)
130{
131  _list = list;
132  _window = _list;
133  _width = get_width ();
134  set_extent ();
135  get_selected_index ();
136  return (*this);
137}
138
139void WList::append (const char* text)
140{
141  SendMessage (_list, LB_INSERTSTRING, (WPARAM) -1, (LPARAM) text);
142  int w = strlen (text);
143  if (w > _width) 
144  {
145    _width = w;
146    set_extent ();
147  }
148}
149
150void WList::append (const cmt_string& text)
151{
152  SendMessage (_list, LB_INSERTSTRING, (WPARAM) -1, (LPARAM) text.c_str ());
153  int w = text.size ();
154  if (w > _width) 
155  {
156    _width = w;
157    set_extent ();
158  }
159}
160
161void WList::insert (int index, const cmt_string& text)
162{
163  SendMessage (_list, LB_INSERTSTRING, (WPARAM) index, (LPARAM) text.c_str ());
164  int w = text.size ();
165  if (w > _width) 
166  {
167    _width = w;
168    set_extent ();
169  }
170}
171
172void WList::modify (int index, const cmt_string& text)
173{
174  SendMessage (_list, LB_DELETESTRING, (WPARAM) index, (LPARAM) 0);
175  SendMessage (_list, LB_INSERTSTRING, (WPARAM) index, (LPARAM) text.c_str ());
176
177  _width = get_width ();
178  set_extent ();
179}
180
181void WList::delete_item (int index)
182{
183  SendMessage (_list, LB_DELETESTRING, (WPARAM) index, (LPARAM) 0);
184
185  _width = get_width ();
186  set_extent ();
187}
188
189void WList::clear ()
190{
191  SendMessage (_list, LB_RESETCONTENT, (WPARAM) 0, (LPARAM) 0);
192  _selected_index = -1;
193  _width = 0;
194  set_extent ();
195}
196
197void WList::cancel_selection ()
198{
199  SendMessage (_list, LB_SETCURSEL, (WPARAM) -1, (LPARAM) 0);
200  _selected_index = -1;
201}
202
203int WList::get_selected_index ()
204{
205  int index;
206               
207  index = SendMessage (_list, LB_GETCURSEL, (WPARAM) 0, (LPARAM) 0);
208  _selected_index = index;
209
210  return (index);
211}
212
213void WList::set_selected_index (int index)
214{
215  SendMessage (_list, LB_SETCURSEL, (WPARAM) index, (LPARAM) 0);
216  _selected_index = index;
217}
218
219cmt_string WList::get_selected ()
220{
221  int index = get_selected_index ();
222
223  int size = SendMessage (_list, LB_GETTEXTLEN, (WPARAM) index, (LPARAM) 0);
224
225  cmt_string text (size + 1);
226
227  SendMessage (_list, LB_GETTEXT, (WPARAM) index, (LPARAM) text.c_str ());
228
229  return (text);
230}
231
232int WList::get_item_count () const
233{
234  int item;
235       
236  item = SendMessage (_list, LB_GETCOUNT, (WPARAM) 0, (LPARAM) 0);
237
238  return (item);
239}
240
241cmt_string WList::get_item (int index) const
242{
243  int size = SendMessage (_list, LB_GETTEXTLEN, (WPARAM) index, (LPARAM) 0);
244
245  cmt_string text (size + 1);
246       
247  SendMessage (_list, LB_GETTEXT, (WPARAM) index, (LPARAM) text.c_str ());
248
249  return (text);
250}
251
252void WList::get_item (int index, cmt_string& text) const
253{
254  int size = SendMessage (_list, LB_GETTEXTLEN, (WPARAM) index, (LPARAM) 0);
255
256  static cmt_string temp;
257  temp.resize (size);
258         
259  SendMessage (_list, LB_GETTEXT, (WPARAM) index, (LPARAM) temp.c_str ());
260
261  text = temp;
262}
263
264int WList::get_item_length (int index) const
265{
266  int size = SendMessage (_list, LB_GETTEXTLEN, (WPARAM) index, (LPARAM) 0);
267
268  return (size);
269}
270
271void WList::copy (const WList& other)
272{
273  int items;
274  int i;
275
276  clear ();
277
278  items = other.get_item_count ();
279  for (i = 0; i < items; i++)
280    {
281      append (other.get_item (i));
282    }
283}
284
285void WList::set (const CmtSystem::cmt_string_vector& v, int first)
286{
287  int i;
288
289  clear ();
290
291  for (i = first; i < v.size (); i++)
292    {
293      append (v[i]);
294    }
295}
296
297void WList::set (const cmt_string& file_name)
298{
299  clear ();
300
301  cmt_string text;
302  cmt_string line;
303  int pos;
304  int max_pos;
305
306  text.read (file_name);
307
308  pos = 0;
309  max_pos = text.size ();
310
311  for (pos = 0; pos < max_pos;)
312    {
313      int cr = text.find (pos, "\r\n");
314      int nl = text.find (pos, '\n'); 
315      int first = nl;
316      int length = 1;
317
318      if (cr != cmt_string::npos)
319        {
320          if (nl == cmt_string::npos)
321            {
322              first = cr;
323              length = 2;
324            }
325          else
326            {
327              first = (nl < cr) ? nl : cr;
328              length = (nl < cr) ? 1 : 2;
329            }
330        }
331
332      if (first == cmt_string::npos)
333        {
334          text.substr (pos, line);
335          pos = max_pos;
336        }
337      else if (first > pos)
338        {
339          text.substr (pos, first - pos, line);
340          pos = first + length;
341        }
342      else
343        {
344          line = "";
345          pos += length;
346        }
347
348      append (line);
349    }
350}
351
352void WList::get (CmtSystem::cmt_string_vector& v) const
353{
354  int count = get_item_count ();
355  int i;
356
357  v.clear ();
358
359  for (i = 0; i < count; i++)
360    {
361      v.push_back (get_item (i));
362    }
363}
364
365void WList::get (cmt_string& s) const
366{
367  int count = get_item_count ();
368  int i;
369
370  s = "";
371
372  for (i = 0; i < count; i++)
373    {
374      if (i > 0) s += '\n';
375      s += get_item (i);
376    }
377}
378
379void WList::set_extent ()
380{
381  int width = get_extent ();
382  ListBox_SetHorizontalExtent (_list, width);
383}
384
385int WList::get_width () const
386{
387  if (_list == 0) return (0);
388
389  int count = get_item_count ();
390  int w = 0;
391
392  int i;
393  for (i = 0; i < count; i++)
394  {
395          int length = get_item_length (i);
396          if (length > w) w = length;
397  }
398
399  return (w);
400}
401
402int WList::get_extent () const
403{
404  if (_width == 0) return (0);
405
406  cmt_string text (_width + 1);
407
408  HDC context = GetDC (_list);
409  int saved_context = SaveDC (context);
410  HFONT font = GetWindowFont (_list);
411  if (font != NULL) SelectFont (context, font);
412
413  int w = 0;
414
415  int count = get_item_count ();
416
417  int i;
418  for (i = 0; i < count; i++)
419  {
420          SIZE size;
421
422          get_item (i, text);
423          GetTextExtentPoint32 (context, text.c_str (), text.size (), &size);
424          if (size.cx > w) w = size.cx;
425  }
426
427  RestoreDC (context, saved_context);
428  ReleaseDC (_list, context);
429
430  w += GetSystemMetrics (SM_CXVSCROLL);
431
432  return (w);
433}
434//---------------------------------------------------------------------
435
436//---------------------------------------------------------------------
437mysb::mysb (WList& list) : _list (list)
438{
439    //list_clear (list);
440  _buf = 0;
441#ifdef OLD_HEADERS
442  _old = cout.rdbuf ();
443  cout = this;
444#else
445  _old = cout.rdbuf (this);
446#endif
447}
448
449mysb::~mysb ()
450{
451  if (_buf != 0)
452  {
453    //delete _buf;
454        _buf = 0;
455  }
456
457#ifdef OLD_HEADERS
458  cout = _old;
459#else
460  cout.rdbuf (_old);
461#endif
462}
463
464int_type mysb::overflow (int_type c)
465{
466  if (_buf == 0)
467  {
468    _buf = new cmt_string;
469  }
470
471  cmt_string& buf = *_buf;
472
473  if (c == '\n')
474    {
475      _list.append (buf);
476      buf = "";
477    }
478  else
479    {
480      buf += c;
481    }
482  return (c);
483}
484       
485int mysb::underflow ()
486{
487  return (EOF);
488}
489//---------------------------------------------------------------------
490
491//---------------------------------------------------------------------
492WDialog::WDialog ()
493{
494}
495
496void WDialog::action (UINT message_type,
497                      WPARAM parameter1,
498                      LPARAM parameter2)
499{
500}
501
502void WDialog::init_action ()
503{
504}
505
506void WDialog::menu_action (int menu_item,
507                           WPARAM parameter1,
508                           LPARAM parameter2)
509{
510  switch (menu_item) 
511    {
512      case IDOK:
513        break;
514      case IDCANCEL:
515        break;
516      default:
517        break;
518    }
519}
520
521int WDialog::run (HINSTANCE instance, HWND father, int id)
522{
523  return (DialogBoxParam (instance, 
524                          MAKEINTRESOURCE(id), 
525                          father,
526                          (DLGPROC) proc,
527                          (LPARAM) this));
528}
529
530LRESULT CALLBACK WDialog::proc (HWND dialog,
531                                UINT message_type,
532                                WPARAM parameter1,
533                                LPARAM parameter2)
534{
535  static cmt_vector <WDialog*> objects;
536  WDialog* object = NULL;
537  int menu_item;
538
539  switch (message_type) 
540    {
541      case WM_CREATE:
542        break;
543      case WM_SETFONT:
544        break;
545      case WM_INITDIALOG:
546        object = (WDialog*) parameter2;
547        if (object != NULL)
548          {
549            objects.push_back (object);
550
551            object->_dialog = dialog;
552            object->init_action ();
553          }
554        break;
555      case WM_COMMAND:
556        menu_item = LOWORD (parameter1);
557
558        if (objects.size () > 0)
559          {
560            object = objects.back ();
561          }
562
563        if (object != NULL)
564          {
565            object->menu_action (menu_item, parameter1, parameter2);
566          }
567
568        switch (menu_item) 
569          {
570            case IDOK:
571              EndDialog (dialog, IDOK);
572              objects.pop_back ();
573              break;
574            case IDCANCEL:
575              EndDialog (dialog, IDCANCEL);
576              objects.pop_back ();
577              break;
578            default:
579              break;
580          }
581        break;
582      default:
583        if (objects.size () > 0)
584          {
585            object = objects.back ();
586          }
587
588        if (object != NULL)
589          {
590            object->action (message_type, parameter1, parameter2);
591          }
592        return (FALSE);
593    }
594  return (TRUE);
595}
596//---------------------------------------------------------------------
597
598//---------------------------------------------------------------------
599TextEditor::TextEditor (cmt_string& ref) : _ref (ref)
600{
601}
602
603int TextEditor::run (HINSTANCE instance, HWND father)
604{
605  return (WDialog::run (instance, father, IDD_TEXTEDIT));
606}
607
608void TextEditor::action (UINT message_type,
609                         WPARAM parameter1,
610                         LPARAM parameter2)
611{
612}
613
614void TextEditor::init_action ()
615{
616  _edit = GetDlgItem (_dialog, IDTE_TEXT);
617  _edit.set (_ref);
618}
619
620void TextEditor::menu_action (int menu_item, WPARAM parameter1, LPARAM parameter2)
621{
622  switch (menu_item) 
623    {
624      case IDOK:
625        _ref = _edit.get ();
626        break;
627      case IDCANCEL:
628        break;
629      default:
630        break;
631    }
632}
633//---------------------------------------------------------------------
634
635//---------------------------------------------------------------------
636ListEditor::ListEditor (WList& list) : _ref_list (list)
637{
638}
639
640int ListEditor::run (HINSTANCE instance, HWND father)
641{
642  _instance = instance;
643  return (WDialog::run (instance, father, IDD_LISTEDIT));
644}
645
646void ListEditor::action (UINT message_type,
647                         WPARAM parameter1,
648                         LPARAM parameter2)
649{
650}
651
652void ListEditor::set_position (int index)
653{
654  _list.set_selected_index (index);
655  _selected_index = index;
656  _last_index = index;
657}
658
659void ListEditor::init_action ()
660{
661  _list.init (_dialog, IDLE_LIST);
662  _list.copy (_ref_list);
663  _ref_selected_index = _ref_list.get_selected_index ();
664  _list.set_selected_index (_ref_selected_index);
665  _selected_index = _ref_selected_index;
666  _last_index = _ref_selected_index;
667}
668
669void ListEditor::menu_action (int menu_item, WPARAM parameter1, LPARAM parameter2)
670{
671  int index = _list.get_selected_index ();
672
673  switch (menu_item) 
674    {
675      case IDOK:
676        _ref_list.copy (_list);
677        _ref_list.set_selected_index (_ref_selected_index);
678        break;
679      case IDCANCEL:
680        break;
681      case IDLE_ADD:
682      {
683        cmt_string s;
684        TextEditor editor (s);
685
686        if (editor.run (_instance, _dialog) == IDOK)
687          {
688            _list.insert (index, s);
689          }
690      }
691      break;
692      case IDLE_REMOVE:
693      {
694        if (index != -1)
695          {
696            _list.delete_item (index);
697          }
698      }
699      break;
700      case IDLE_LIST:
701     {
702        switch (HIWORD(parameter1))
703          {
704            case LBN_DBLCLK:
705            {
706              if (index == -1) index = _last_index;
707              cmt_string s = _list.get_item (index);
708              TextEditor editor (s);
709       
710              if (editor.run (_instance, _dialog) == IDOK)
711                {
712                  _list.modify (index, s);
713                }
714              _list.set_selected_index (index);
715              _selected_index = index;
716              _last_index = index;
717            }
718            break;
719            case LBN_SELCHANGE:
720              if (_selected_index == -1)
721                {
722                  _selected_index = index;
723                  _last_index = index;
724                }
725              else if (_selected_index == index)
726                {
727                  _list.cancel_selection ();
728                  _selected_index = -1;
729                }
730              else
731                {
732                  _selected_index = index;
733                  _last_index = index;
734                }
735              break;
736            case LBN_SELCANCEL:
737            {
738            }
739            break;
740          }
741      }
742      break;
743      default:
744        break;
745    }
746}
747//---------------------------------------------------------------------
748
749//---------------------------------------------------------------------
750CmtMutex::CmtMutex ()
751{
752  InitializeCriticalSection (&_section);
753}
754
755CmtMutex::~CmtMutex ()
756{
757  DeleteCriticalSection (&_section);
758}
759
760void CmtMutex::lock ()
761{
762  EnterCriticalSection (&_section);
763}
764
765void CmtMutex::unlock ()
766{
767  LeaveCriticalSection (&_section);
768}
769//---------------------------------------------------------------------
770
771//---------------------------------------------------------------------
772CmtThread::CmtThread ()
773{
774  _shutdown = FALSE;
775  _running = FALSE;
776}
777
778CmtThread::~CmtThread ()
779{
780}
781
782void CmtThread::run ()
783{
784}
785
786bool CmtThread::is_running ()
787{
788  bool result = false;
789
790  _mutex.lock ();
791  result = _running;
792  _mutex.unlock ();
793
794  return (result);
795}
796
797void CmtThread::set_running ()
798{
799  _mutex.lock ();
800  _running = TRUE;
801  _mutex.unlock ();
802}
803
804void CmtThread::reset_running ()
805{
806  _mutex.lock ();
807  _running = FALSE;
808  _mutex.unlock ();
809}
810
811bool CmtThread::start ()
812{
813  if (!is_running ())
814    {
815      _thread = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) run_it,
816                              this, 0, &_id);
817
818          return (TRUE);
819    }
820  else
821    {
822          return (FALSE);
823    }
824}
825
826void CmtThread::stop ()
827{
828  _mutex.lock ();
829  _shutdown = TRUE;
830  _mutex.unlock ();
831
832  WaitForSingleObject (_thread, INFINITE);
833}
834
835void CmtThread::run_it (CmtThread* thread)
836{
837  thread->set_running ();
838  thread->run ();
839  thread->reset_running ();
840}
841//---------------------------------------------------------------------
842
Note: See TracBrowser for help on using the repository browser.