shared_code.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : shared code                                                       *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1995-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include "implem_only.h"
00016 #include "shared_code.h"
00017 #include "shared_code.rh"
00018 
00019 #include <geometric/rectangle.cpp>
00020 #include <rpc_ext/rpc_common.h>
00021 
00022 #include <rpc.h>
00023 
00024 using namespace geometric;
00025 
00026 MIDL_ALLOC;
00027 
00028 const int RPC_TIMER_ID = WM_USER + 2005;
00029 
00031 
00032 BEGIN_MESSAGE_MAP(drawing_window, CView)
00033   //{{AFX_MSG_MAP(drawing_window)
00034   ON_WM_CLOSE()
00035   ON_WM_TIMER()
00036   ON_WM_LBUTTONDOWN()
00037   ON_WM_PAINT()
00038   //}}AFX_MSG_MAP
00039 END_MESSAGE_MAP()
00040 
00042 
00043 drawing_window::drawing_window(mouserciser &_mouse_alert)
00044 : CView(),
00045   my_parent(NIL),
00046   alert(NIL),
00047   mouse_alert(_mouse_alert),
00048   mice(0),
00049   dot_size(5, 5),
00050   locker(1, 1),
00051   locking_mode(false)
00052 {}
00053 
00054 drawing_window::~drawing_window()
00055 {}
00056 
00057 bool drawing_window::Create(CFrameWnd *parent, const istring &title,
00058     window_handle _alert, bool locking)
00059 {
00060   my_parent = parent;
00061   alert = _alert;
00062   locking_mode = locking;
00063   RECT cli;
00064   parent->GetClientRect(&cli);
00065   if (!CView::Create(NIL, title.s(), WS_CHILD, cli, parent, 0)) return false;
00066   ShowWindow(SW_RESTORE);
00067   UpdateWindow();
00068   return true;
00069 }
00070 
00071 void drawing_window::lock()
00072 {
00073   if (!locking_mode) return;
00074   locker.wait();
00075 }
00076 
00077 void drawing_window::unlock()
00078 {
00079   if (!locking_mode) return;
00080   locker.signal();
00081 }
00082 
00083 void drawing_window::be_gone() { ShowWindow(SW_HIDE); }
00084 
00085 int drawing_window::length()
00086 {
00087   int to_return;
00088   lock();
00089   to_return = mice.length();
00090   unlock();
00091   return to_return;
00092 }
00093 
00094 void drawing_window::get_mouse_list(array<rpc_point> &to_stuff)
00095 { lock(); to_stuff = mice; unlock(); }
00096 
00097 void drawing_window::start_timer(UINT interval)
00098 { stop_timer(); SetTimer(RPC_TIMER_ID, interval, NIL); }
00099 
00100 void drawing_window::stop_timer() { KillTimer(RPC_TIMER_ID); }
00101 
00102 void drawing_window::OnClose() { stop_timer(); CView::OnClose(); }
00103 
00104 void drawing_window::OnLButtonDown(UINT nFlags, CPoint point)
00105 {
00106   CView::OnLButtonDown(nFlags, point);
00107   rpc_point posn(point.x, point.y);  // converted.
00108   screen_point middle(posn.x(), posn.y());
00109   screen_rectangle dotsize(middle - dot_size, middle + dot_size);
00110   // just update the display without invalidating to cause a redraw.  the
00111   // actual list entry for this dot will come from the server.
00112   CDC *dc = GetDC();
00113   RECT r = dotsize;
00114   dc->Ellipse(&r);
00115   mouse_alert.mouse_hit(posn);  // notify the parent of a new dot.
00116 }
00117 
00118 void drawing_window::OnDraw(CDC *pDC) { CView::OnDraw(pDC); }
00119 
00120 void drawing_window::OnPaint()
00121 {
00122   FUNCDEF("OnPaint");
00123   if (GetUpdateRect(NIL)) {
00124     PAINTSTRUCT paint_info;
00125     CDC *dc = BeginPaint(&paint_info);
00126 
00127     array<rpc_point> temp_list;
00128     get_mouse_list(temp_list);
00129       // grab the current list; since the accessor function locks the semaphore,
00130       // we don't have to.
00131     for (int i = 0; i < temp_list.length(); i++) {
00132       // calculate whether this point intersects with the update region.
00133       screen_point mickey(temp_list[i].x(), temp_list[i].y());
00134       screen_rectangle dotsize(mickey - dot_size, mickey + dot_size);
00135       // draw a tasty ellipse if the dot intersected us.
00136       screen_rectangle rect = paint_info.rcPaint;
00137       if (rect.intersect(dotsize)) {
00138         RECT r = dotsize;
00139         dc->Ellipse(&r);
00140       }
00141     }
00142 
00143     EndPaint(&paint_info);
00144   }
00145 }
00146 
00147 void drawing_window::OnTimer(u_int formal(timer_id))
00148 {
00149   FUNCDEF("OnTimer");
00150   stop_timer();  // zap it because we've hit the update.
00151   LOG("timer hit, doing update.");
00152   mouse_alert.update();
00153 }
00154 
00155 void drawing_window::set_mouse_list(const array<rpc_point> &new_mice)
00156 {
00157   lock();
00158   mice = new_mice;
00159   unlock();
00160   
00161   RECT r;
00162   GetClientRect(&r);
00163   InvalidateRect(&r);
00164 }
00165 
00166 void drawing_window::add_dot(const rpc_point &draw_at)
00167 {
00168   lock();
00169   mice.concatenate(draw_at);
00170   unlock();
00171   screen_point middle(draw_at.x(), draw_at.y());
00172   screen_rectangle dotsize(middle - dot_size, middle + dot_size);
00173   RECT temp = dotsize;
00174   InvalidateRect(&temp);
00175 }
00176 
00177 void drawing_window::pull_out_mice(const byte_array &received,
00178     array<rpc_point> &original)
00179 {
00180   original.reset();
00181   if (received.length() < 2) return;  // check format.
00182   u_int num_mice = received.get(0) * 0x100 + received.get(1);
00183   if (num_mice == 0) return;  // check number of points.
00184   original.reset(num_mice, (rpc_point *)&(received.get(2)));
00185     // suck in the mice points, but skip the chunk with the size.
00186 }
00187 
00188 void drawing_window::push_in_mice(const array<rpc_point> &mice,
00189     byte_array &to_stuff)
00190 {
00191   to_stuff.reset();
00192   // to_stuff gets the # of mice first.
00193   to_stuff += byte(mice.length() / 0x100);
00194   to_stuff += byte(mice.length() % 0x100);
00195   if (mice.length()) {
00196     // now we calculate the necessary size for the number of points we have.
00197     int list_size = sizeof(rpc_point) * mice.length();
00198     // the array is adjusted to the proper size.
00199     to_stuff.insert(2, list_size);
00200     // now a simple copy of the mouse list and we're done.
00201     mice.stuff(mice.length(), (rpc_point *)&(to_stuff[2]));
00202   }
00203 }
00204 
00206 
00207 u_int POINT_ALERT()
00208 {
00209   const char *RPC_UNIQUIFYING_STRING = "rpc_event_alerter";
00210     // this string is used to register a unique windows message.
00211   return RegisterWindowMessage(RPC_UNIQUIFYING_STRING);
00212 }
00213 
00215 

Generated on Fri Nov 21 04:30:00 2008 for HOOPLE Libraries by  doxygen 1.5.1