34 #include "config_auto.h"
40 #pragma warning(disable:4786) // Don't give stupid warnings for stl
41 #pragma warning(disable:4018) // signed/unsigned warnings
42 #pragma warning(disable:4530) // exception warnings
51 #include "allheaders.h"
60 static std::map<int, ScrollView*> svmap;
63 static std::map<std::pair<ScrollView*, SVEventType>,
64 std::pair<SVSemaphore*, SVEvent*> > waiting_for_events;
65 static SVMutex* waiting_for_events_mu;
83 #ifndef GRAPHICS_DISABLED
88 void* ScrollView::MessageReceiver(
void* a) {
89 int counter_event_id = 0;
93 message = ScrollView::GetStream()->
Receive();
94 }
while (message ==
NULL);
99 while (strlen(message) != -1) {
109 sscanf(message,
"%d,%d,%d,%d,%d,%d,%d,%n", &window_id, &ev_type, &cur->
x,
111 char* p = (message + n);
114 cur->
window = svmap[window_id];
117 cur->
parameter =
new char[strlen(p) + 1];
118 strncpy(cur->
parameter, p, strlen(p) + 1);
133 if (cur->
window->y_axis_is_reversed_)
135 cur->
counter = counter_event_id;
139 counter_event_id += 2;
145 cur->
window->SetEvent(cur);
148 std::pair<ScrollView*, SVEventType> awaiting_list(cur->
window,
150 std::pair<ScrollView*, SVEventType> awaiting_list_any(cur->
window,
152 std::pair<ScrollView*, SVEventType> awaiting_list_any_window((
ScrollView*)0,
154 waiting_for_events_mu->
Lock();
155 if (waiting_for_events.count(awaiting_list) > 0) {
156 waiting_for_events[awaiting_list].second = cur;
157 waiting_for_events[awaiting_list].first->Signal();
158 }
else if (waiting_for_events.count(awaiting_list_any) > 0) {
159 waiting_for_events[awaiting_list_any].second = cur;
160 waiting_for_events[awaiting_list_any].first->Signal();
161 }
else if (waiting_for_events.count(awaiting_list_any_window) > 0) {
162 waiting_for_events[awaiting_list_any_window].second = cur;
163 waiting_for_events[awaiting_list_any_window].first->Signal();
168 waiting_for_events_mu->
Unlock();
180 message = ScrollView::GetStream()->
Receive();
181 }
while (message ==
NULL);
190 {255, 255, 255, 255},
199 {128, 128, 255, 255},
203 {192, 192, 255, 255},
206 {255, 128, 128, 255},
210 {192, 192, 128, 255},
215 {192, 255, 192, 255},
217 {192, 192, 192, 255},
220 {128, 128, 128, 255},
225 {255, 192, 192, 255},
230 {255, 192, 128, 255},
245 int ScrollView::nr_created_windows_ = 0;
246 int ScrollView::image_index_ = 0;
250 int y_size,
int x_canvas_size,
int y_canvas_size,
251 bool y_axis_reversed,
const char* server_name) {
252 Initialize(name, x_pos, y_pos, x_size, y_size, x_canvas_size, y_canvas_size,
253 y_axis_reversed, server_name);}
257 int y_size,
int x_canvas_size,
int y_canvas_size,
258 bool y_axis_reversed) {
259 Initialize(name, x_pos, y_pos, x_size, y_size, x_canvas_size, y_canvas_size,
260 y_axis_reversed,
"localhost");
265 int y_size,
int x_canvas_size,
int y_canvas_size) {
266 Initialize(name, x_pos, y_pos, x_size, y_size, x_canvas_size, y_canvas_size,
271 void ScrollView::Initialize(
const char* name,
int x_pos,
int y_pos,
int x_size,
272 int y_size,
int x_canvas_size,
int y_canvas_size,
273 bool y_axis_reversed,
const char* server_name) {
276 if (stream_ ==
NULL) {
277 nr_created_windows_ = 0;
279 waiting_for_events_mu =
new SVMutex();
282 "svmain = luajava.bindClass('com.google.scrollview.ScrollView')\n");
287 nr_created_windows_++;
288 event_handler_ =
NULL;
289 event_handler_ended_ =
false;
290 y_axis_is_reversed_ = y_axis_reversed;
291 y_size_ = y_canvas_size;
293 window_id_ = nr_created_windows_;
296 points_->
empty =
true;
299 svmap[window_id_] =
this;
303 event_table_[i] =
NULL;
311 snprintf(message,
sizeof(message),
312 "w%u = luajava.newInstance('com.google.scrollview.ui"
313 ".SVWindow','%s',%u,%u,%u,%u,%u,%u,%u)\n",
314 window_id_, window_name_, window_id_,
315 x_pos, y_pos, x_size, y_size, x_canvas_size, y_canvas_size);
322 void* ScrollView::StartEventHandler(
void* a) {
328 sv->semaphore_->
Wait();
336 if (sv->event_table_[i] !=
NULL &&
337 (serial < 0 || sv->event_table_[i]->counter < serial)) {
338 new_event = sv->event_table_[i];
339 serial = sv->event_table_[i]->
counter;
344 if (new_event !=
NULL) {
345 sv->event_table_[k] =
NULL;
347 if (sv->event_handler_ !=
NULL) { sv->event_handler_->
Notify(new_event); }
350 sv->event_handler_ended_ =
true;
354 }
else { sv->mutex_->
Unlock(); }
356 }
while (sv !=
NULL);
359 #endif // GRAPHICS_DISABLED
362 #ifndef GRAPHICS_DISABLED
364 if (svmap[window_id_] !=
NULL) {
372 svmap[window_id_] =
NULL;
377 while (!event_handler_ended_)
385 #endif // GRAPHICS_DISABLED
388 #ifndef GRAPHICS_DISABLED
396 va_start(args, format);
401 snprintf(form,
kMaxMsgSize,
"w%u:%s\n", window_id_, message);
414 event_handler_ = listener;
417 void ScrollView::Signal() {
421 void ScrollView::SetEvent(
SVEvent* svevent) {
430 if (event_table_[specific->
type] !=
NULL) {
431 delete event_table_[specific->
type]; }
435 event_table_[specific->
type] = specific;
447 std::pair<ScrollView*, SVEventType> ea(
this, type);
448 waiting_for_events_mu->
Lock();
449 waiting_for_events[ea] = std::pair<SVSemaphore*, SVEvent*> (sem, (
SVEvent*)0);
450 waiting_for_events_mu->
Unlock();
455 waiting_for_events_mu->
Lock();
456 SVEvent* ret = waiting_for_events[ea].second;
457 waiting_for_events.erase(ea);
459 waiting_for_events_mu->
Unlock();
469 waiting_for_events_mu->
Lock();
470 waiting_for_events[ea] = std::pair<SVSemaphore*, SVEvent*> (sem, (
SVEvent*)0);
471 waiting_for_events_mu->
Unlock();
476 waiting_for_events_mu->
Lock();
477 SVEvent* ret = waiting_for_events[ea].second;
478 waiting_for_events.erase(ea);
479 waiting_for_events_mu->
Unlock();
484 void ScrollView::SendPolygon() {
485 if (!points_->
empty) {
486 points_->
empty =
true;
487 int length = points_->
xcoords.size();
492 SendMsg(
"drawLine(%d,%d,%d,%d)",
495 }
else if (length > 2) {
497 SendMsg(
"createPolyline(%d)", length);
499 std::string decimal_coords;
500 for (
int i = 0; i < length; ++i) {
503 decimal_coords += coordpair;
505 decimal_coords +=
'\n';
529 points_->
empty =
false;
538 }
else if (!points_->
xcoords.empty() && x2 == points_->
xcoords.back() &&
551 if (visible) {
SendMsg(
"setVisible(true)");
552 }
else {
SendMsg(
"setVisible(false)"); }
557 if (b) {
SendMsg(
"setAlwaysOnTop(true)");
558 }
else {
SendMsg(
"setAlwaysOnTop(false)"); }
567 va_start(args, format);
571 snprintf(form,
kMaxMsgSize,
"w%u:%s", window_id_, message);
573 char* esc = AddEscapeChars(form);
574 SendMsg(
"addMessage(\"%s\")", esc);
596 SendMsg(
"setStrokeWidth(%f)", width);
602 if (x1 == x2 && y1 == y2)
604 SendMsg(
"drawRectangle(%d,%d,%d,%d)",
611 SendMsg(
"drawEllipse(%d,%d,%u,%u)",
617 SendMsg(
"pen(%d,%d,%d)", red, green, blue);
622 SendMsg(
"pen(%d,%d,%d,%d)", red, green, blue, alpha);
627 SendMsg(
"brush(%d,%d,%d)", red, green, blue);
632 SendMsg(
"brush(%d,%d,%d,%d)", red, green, blue, alpha);
637 bool bold,
bool italic,
bool underlined) {
642 if (bold) { b =
"true";
643 }
else { b =
"false"; }
644 if (italic) { i =
"true";
645 }
else { i =
"false"; }
646 if (underlined) { u =
"true";
647 }
else { u =
"false"; }
648 SendMsg(
"textAttributes('%s',%u,%s,%s,%s)", font, pixel_size,
659 SendMsg(
"openImage('%s')", image);
660 SendMsg(
"drawImage('%s',%d,%d)",
666 int cmdEvent,
bool flag) {
667 if (parent ==
NULL) { parent =
""; }
668 if (flag) {
SendMsg(
"addMenuBarItem('%s','%s',%d,true)",
669 parent, name, cmdEvent);
670 }
else {
SendMsg(
"addMenuBarItem('%s','%s',%d,false)",
671 parent, name, cmdEvent); }
676 if (parent ==
NULL) { parent =
""; }
677 SendMsg(
"addMenuBarItem('%s','%s',%d)", parent, name, cmdEvent);
682 if (parent ==
NULL) { parent =
""; }
683 SendMsg(
"addMenuBarItem('%s','%s')", parent, name);
688 if (parent ==
NULL) { parent =
""; }
689 SendMsg(
"addPopupMenuItem('%s','%s')", parent, name);
694 int cmdEvent,
const char* value,
const char* desc) {
695 if (parent ==
NULL) { parent =
""; }
696 char* esc = AddEscapeChars(value);
697 char* esc2 = AddEscapeChars(desc);
698 SendMsg(
"addPopupMenuItem('%s','%s',%d,'%s','%s')", parent, name,
699 cmdEvent, esc, esc2);
712 for (std::map<int, ScrollView*>::iterator iter = svmap.begin();
713 iter != svmap.end(); ++iter) {
714 if (iter->second !=
NULL)
715 iter->second->UpdateWindow();
722 Pen(table_colors[color][0], table_colors[color][1],
723 table_colors[color][2], table_colors[color][3]);
728 Brush(table_colors[color][0],
729 table_colors[color][1],
730 table_colors[color][2],
731 table_colors[color][3]);
736 SendMsg(
"showInputDialog(\"%s\")", msg);
740 char* p =
new char[strlen(ev->
parameter) + 1];
749 SendMsg(
"showYesNoDialog(\"%s\")", msg);
763 SendMsg(
"zoomRectangle(%d,%d,%d,%d)",
769 int width = image->w;
770 int height = image->h;
771 l_uint32 bpp = image->d;
774 SendMsg(
"createImage('lept%d',%d,%d,%d)", image_index_, width, height, bpp);
777 Transfer32bppImage(image);
778 }
else if (bpp == 8) {
779 TransferGrayImage(image);
780 }
else if (bpp == 1) {
781 TransferBinaryImage(image);
784 SendMsg(
"drawImage('lept%d',%d,%d)", image_index_, x_pos, y_pos);
788 void ScrollView::Transfer32bppImage(
PIX* image) {
789 int ppL = pixGetWidth(image);
790 int h = pixGetHeight(image);
791 int wpl = pixGetWpl(image);
792 int transfer_size= ppL * 7 + 2;
793 char* pixel_data =
new char[transfer_size];
794 for (
int y = 0; y < h; ++y) {
795 l_uint32* data = pixGetData(image) + y*wpl;
796 for (
int x = 0; x < ppL; ++x, ++data) {
797 snprintf(&pixel_data[x*7], 7,
"#%.2x%.2x%.2x",
798 GET_DATA_BYTE(data, COLOR_RED),
799 GET_DATA_BYTE(data, COLOR_GREEN),
800 GET_DATA_BYTE(data, COLOR_BLUE));
802 pixel_data[transfer_size - 2] =
'\n';
803 pixel_data[transfer_size - 1] =
'\0';
810 void ScrollView::TransferGrayImage(
PIX* image) {
811 char* pixel_data =
new char[image->w * 2 + 2];
812 for (
int y = 0; y < image->h; y++) {
813 l_uint32* data = pixGetData(image) + y * pixGetWpl(image);
814 for (
int x = 0; x < image->w; x++) {
815 snprintf(&pixel_data[x*2], 2,
"%.2x", (GET_DATA_BYTE(data, x)));
816 pixel_data[image->w * 2] =
'\n';
817 pixel_data[image->w * 2 + 1] =
'\0';
821 delete [] pixel_data;
825 void ScrollView::TransferBinaryImage(
PIX* image) {
826 char* pixel_data =
new char[image->w + 2];
827 for (
int y = 0; y < image->h; y++) {
828 l_uint32* data = pixGetData(image) + y * pixGetWpl(image);
829 for (
int x = 0; x < image->w; x++) {
830 if (GET_DATA_BIT(data, x))
835 pixel_data[image->w] =
'\n';
836 pixel_data[image->w + 1] =
'\0';
839 delete [] pixel_data;
844 char* ScrollView::AddEscapeChars(
const char* input) {
845 const char* nextptr = strchr(input,
'\'');
846 const char* lastptr = input;
849 while (nextptr !=
NULL) {
850 strncpy(message+pos, lastptr, nextptr-lastptr);
851 pos += nextptr - lastptr;
855 nextptr = strchr(nextptr+1,
'\'');
857 strncpy(message+pos, lastptr, strlen(lastptr));
858 message[pos+strlen(lastptr)] =
'\0';
864 if (!y_axis_is_reversed_) {
return y;
865 }
else {
return y_size_ - y; }
868 #endif // GRAPHICS_DISABLED