// C++ code Copyright (C) David R. Evans G4AMJ/NQ0I

// classes for Windows mode of MIDAS

// These use the Borland OWL libraries. This may be a mistake, since the
// libraries are the most difficult, contrary piece of uphill coding I
// have ever seen. Compare the contortions necessary to get this lot to
// work with the equivalent code for any of the other platforms.

// They could have simplified things significantly by permitting me to
// multiply derive from both TWindow and TApplication but no, that is
// obviously asking too much.

#ifndef SMWINDOWSMODEH
#define SMWINDOWSMODEH

#include <sm_mode.h>
#include <surf_win.h>
//#include <surf_wp.h>

#include <owl\applicat.h>
#include <owl\button.h>
#include <owl\framewin.h>

// define some types to make it easier to port from OWL 1.0 to OWL 2.0
typedef TWindow* PTWindowsObject;

const int N_SECONDARY_CONTROLS = 2;

// Because of an idiocy in the design of ObjectWindows, the following
// is necessary

#include <sm_resource.h>

class windows_mode;

extern windows_mode* wmode;  // vital to OS kludge

// Ideally, the class "midas application" would not exist; windows_mode
// would be multiply inherited from both TApplication and operating_mode.
// Unfortunately, operating_mode is far and TApplication is huge.
// Intel strikes again....
// This also requires some extra friend declarations (sigh).

class popup;
class midas_window;

class midas_application : public TApplication
{ friend class windows_mode;
  friend class midas_window;

  char rev[30];                 // the program title bar
  midas_window* mwp;

// primary buttons
  primary_button * bin_button, * change_button, * clipboard_button,
					  * comment_button, * first_button, * invert_button,
					  * last_button, * next_button, * open_button,
					  * plot_button, * plotall_button, * plotrmdr_button,
                 * print_button, * printer_button,
					  * prior_button, * replot_button, * set_button,
					  * show_button, * test_button;

// secondary buttons
  secondary_button * image_button, * range_button;

  TStatic * busy_display, * status_display;

  TWindow * invisible_window;
  popup* cp, *winp;
  PLOGFONT logfont_pointer;
  
  HINSTANCE this_instance;

  windows_mode* wm;

public:
  midas_application(LPSTR AName, windows_mode*);
  ~midas_application(void);
  virtual void InitMainWindow(void);
};

// this is the main window
class midas_window : public TFrameWindow
{ friend class midas_application;
  friend class windows_mode;

  HINSTANCE this_instance;
  popup* cwp; 									// the canvas
  listbox* _lbp;
  windows_mode* wm;

  typedef void (midas_window::*PMFCI)(const int);
  PMFCI menu_handler;
  HBRUSH green_brush, red_brush;

public:
  midas_window(windows_mode*, LPSTR ATitle);

  LPSTR GetClassName(void)
    { return "midas"; }
  void GetWindowClass(WNDCLASS&);

  void cp(popup* cw)
    { cwp = cw; }
  popup* cp(void)
	 { return cwp; }
  void lbp(listbox* LBP)
    { _lbp = LBP; }
  listbox* lbp(void)
	 { return _lbp; }

// handle the primary buttons
  void handle_bin_button(UINT);
  void handle_change_button(UINT);
  void handle_clipboard_button(UINT);
  void handle_comment_button(UINT);
  void handle_first_button(UINT);
  void handle_invert_button(UINT);
  void handle_last_button(UINT);
  void handle_next_button(UINT);
  void handle_open_button(UINT);
  void handle_plot_button(UINT);
  void handle_plotall_button(UINT);
  void handle_plotrmdr_button(UINT);
  void handle_print_button(UINT);
  void handle_printer_button(UINT);
  void handle_prior_button(UINT);
  void handle_replot_button(UINT);
  void handle_set_button(UINT);
  void handle_show_button(UINT);
  void handle_test_button(UINT) /* = [ID_FIRST + ID_TEST_BUTTON] */ ;

// handle the secondary buttons
  void handle_image_button(UINT);
  void handle_range_button(UINT);

  void handle_menu(UINT) /* = [ID_FIRST + ID_SHOW_LIST] */ ;

// handle the statics
//  void handle_static(UINT) /* = [WM_FIRST + WM_CTLCOLOR] */ ;
// bozoesque OWL 2.0 is happiest with the following stupid signature
  HBRUSH EvCtlColor(HDC, HWND, UINT);

// OWL Programmer's [sic] Guide, p. 356.
  DECLARE_RESPONSE_TABLE(midas_window);
};

class windows_mode : public operating_mode
{ enum secondary_control { image = 0, range };

  friend class midas_window;

  midas_application* app;
  windows31* w31p;
  windows_printer* win_printer_pointer;

  DREstring _plot_mode_name[N_PLOT_MODES];

  set secondary_controls_present;

// functions
  void _clear_text_panel(void);
  void _display_in_window(const char*, DREstring& /*, const colour,
                          const colour*/);
  void _pol_contrast(void);
  void _pol_threshold(void);
  void _set_display();
  void _set_file_type(void);
  void _set_plot_mode(void);
  void _set_pra_mode(void);
  void _set_sc_mode(void);
  
// verbose does nothing
  void verbose(DREstring&) 
	 { }

  void _popup(DREstring& title, DREstring& msg)
	 { app->cp->MessageBox(msg, title); }

  void _create_image_control(void);
  void _create_range_control(void);

  void _delete_image_control(void);

  void _retitle_canvas(void);
  
public:
  windows_mode(const double);
 ~windows_mode(void);
 
 inline void execute(void)
   { app->MessageLoop(); }

  void comment(void);
  void print(void);
  void printer(void);

  void delete_secondary_controls(void);
  void delete_secondary_control(const int);

// non-commands
  void clear_status(void)
    { display_status((DREstring)""); }
  void display_status(DREstring&);
  void error(DREstring&);
  void busy(DREstring&);
  void unbusy(void);
  void test(void);
};

#endif
